Skip to content

Commit

Permalink
llist: Move cpu_relax() to after the cmpxchg()
Browse files Browse the repository at this point in the history
If in llist_add()/etc. functions the first cmpxchg() call succeeds, it is
not necessary to use cpu_relax() before the cmpxchg(). So cpu_relax() in
a busy loop involving cmpxchg() should go after cmpxchg() instead of before
that.

This patch fixes this for all involved llist functions.

Signed-off-by: Huang Ying <[email protected]>
Acked-by: Mathieu Desnoyers <[email protected]>
Signed-off-by: Peter Zijlstra <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
yhuang-intel authored and Ingo Molnar committed Oct 4, 2011
1 parent 2c30245 commit a312733
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
7 changes: 5 additions & 2 deletions include/linux/llist.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,14 @@ static inline void llist_add(struct llist_node *new, struct llist_head *head)
struct llist_node *entry, *old_entry;

entry = head->first;
do {
for (;;) {
old_entry = entry;
new->next = entry;
entry = cmpxchg(&head->first, old_entry, new);
if (entry == old_entry)
break;
cpu_relax();
} while ((entry = cmpxchg(&head->first, old_entry, new)) != old_entry);
}
}

/**
Expand Down
14 changes: 10 additions & 4 deletions lib/llist.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ void llist_add_batch(struct llist_node *new_first, struct llist_node *new_last,
struct llist_node *entry, *old_entry;

entry = head->first;
do {
for (;;) {
old_entry = entry;
new_last->next = entry;
entry = cmpxchg(&head->first, old_entry, new_first);
if (entry == old_entry)
break;
cpu_relax();
} while ((entry = cmpxchg(&head->first, old_entry, new_first)) != old_entry);
}
}
EXPORT_SYMBOL_GPL(llist_add_batch);

Expand All @@ -68,13 +71,16 @@ struct llist_node *llist_del_first(struct llist_head *head)
struct llist_node *entry, *old_entry, *next;

entry = head->first;
do {
for (;;) {
if (entry == NULL)
return NULL;
old_entry = entry;
next = entry->next;
entry = cmpxchg(&head->first, old_entry, next);
if (entry == old_entry)
break;
cpu_relax();
} while ((entry = cmpxchg(&head->first, old_entry, next)) != old_entry);
}

return entry;
}
Expand Down

0 comments on commit a312733

Please sign in to comment.