Re: ACCESS_ONCE usage inside llist_add_batch function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Akturk,

On Sun, Mar 1, 2015 at 1:42 AM, Cihangir Akturk <cakturk@xxxxxxxxx> wrote:
Reading the lib/llist.c file in the kernel sources, I came across
the llist_add_bach function defined like this;

bool llist_add_batch(struct llist_node *new_first, struct llist_node *new_last,
                     struct llist_head *head)
{
        struct llist_node *first;

        do {
                new_last->next = first = ACCESS_ONCE(head->first);
        } while (cmpxchg(&head->first, first, new_first) != first);

        return !first;
}

One thing bugging my mind is the ACCESS_ONCE macro. Is it really
needed here ? I mean I would write this function with ACCES_ONCE
moved outside the loop like as follows;

ACCESS_ONCE avoids compiler optimization. A kind of compiler optimization can be caching a value in a register and reusing it.
Why compiler does this? Its his job to create optimized code for speed by default.

Now llist is lock less linked list, where multiple consumers and producers can work simultaneously.
Hence during each iteration of the while loop, you want to access the current value of head->first.

And if you remove ACCESS_ONCE, compiler can keep a copy of head->list in a register during first iteration and reuse it from register for the subsequent iterations. 

Thanks,
Arun
 

bool llist_add_batch(struct llist_node *new_first, struct llist_node *new_last,
                     struct llist_head *head)
{
        struct llist_node *first, *old;

        old = ACCESS_ONCE(head->first);
        for (;;) {
                first = old;
                new_last->next = old;
                old = cmpxchg(&head->first, first, new_first);
                if (old == first)
                        break;
        }

        return !first;
}

I think that it may be faster to just use the return value of cmpxchg.
But I am not sure about this.

Is my understanding correct ?

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux