llist: use try_cmpxchg in llist_add_batch and llist_del_first

Use try_cmpxchg instead of cmpxchg (*ptr, old, new) == old in
llist_add_batch and llist_del_first.  x86 CMPXCHG instruction returns
success in ZF flag, so this change saves a compare after cmpxchg.

Also, try_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg
fails, enabling further code simplifications.

No functional change intended.

Link: https://lkml.kernel.org/r/20220712144917.4497-1-ubizjak@gmail.com
Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
Uros Bizjak 2022-07-12 16:49:17 +02:00 committed by Andrew Morton
parent 05c6257433
commit 4f1d2a030d

View File

@ -30,7 +30,7 @@ bool llist_add_batch(struct llist_node *new_first, struct llist_node *new_last,
do { do {
new_last->next = first = READ_ONCE(head->first); new_last->next = first = READ_ONCE(head->first);
} while (cmpxchg(&head->first, first, new_first) != first); } while (!try_cmpxchg(&head->first, &first, new_first));
return !first; return !first;
} }
@ -52,18 +52,14 @@ EXPORT_SYMBOL_GPL(llist_add_batch);
*/ */
struct llist_node *llist_del_first(struct llist_head *head) struct llist_node *llist_del_first(struct llist_head *head)
{ {
struct llist_node *entry, *old_entry, *next; struct llist_node *entry, *next;
entry = smp_load_acquire(&head->first); entry = smp_load_acquire(&head->first);
for (;;) { do {
if (entry == NULL) if (entry == NULL)
return NULL; return NULL;
old_entry = entry;
next = READ_ONCE(entry->next); next = READ_ONCE(entry->next);
entry = cmpxchg(&head->first, old_entry, next); } while (!try_cmpxchg(&head->first, &entry, next));
if (entry == old_entry)
break;
}
return entry; return entry;
} }