ACCESS_ONCE usage inside llist_add_batch function

Cihangir Akturk cakturk at gmail.com
Sat Feb 28 15:12:23 EST 2015


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;

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 ?



More information about the Kernelnewbies mailing list