summaryrefslogtreecommitdiffstats
path: root/include/linux/list.h
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2016-07-23 19:27:50 +0100
committerPaul E. McKenney <paulmck@linux.vnet.ibm.com>2016-09-14 12:57:43 -0700
commit12adfd882c5f37548acaba4f043a158b3c54468b (patch)
tree83c46e074a1044db2364cd712111550c994c3bc8 /include/linux/list.h
parent3563a438f124cb0b8cfd350c86de2f26c63d8837 (diff)
downloadlinux-12adfd882c5f37548acaba4f043a158b3c54468b.tar.gz
linux-12adfd882c5f37548acaba4f043a158b3c54468b.tar.bz2
linux-12adfd882c5f37548acaba4f043a158b3c54468b.zip
list: Expand list_first_entry_or_null()
Due to the use of READ_ONCE() in list_empty() the compiler cannot optimise !list_empty() ? list_first_entry() : NULL very well. By manually expanding list_first_entry_or_null() we can take advantage of the READ_ONCE() to avoid the list element changing under the test while the compiler can generate smaller code. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Jan Kara <jack@suse.cz> Cc: Josef Bacik <jbacik@fb.com> Cc: linux-kernel@vger.kernel.org Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Diffstat (limited to 'include/linux/list.h')
-rw-r--r--include/linux/list.h7
1 files changed, 5 insertions, 2 deletions
diff --git a/include/linux/list.h b/include/linux/list.h
index 5183138aa932..5809e9a2de5b 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -381,8 +381,11 @@ static inline void list_splice_tail_init(struct list_head *list,
*
* Note that if the list is empty, it returns NULL.
*/
-#define list_first_entry_or_null(ptr, type, member) \
- (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
+#define list_first_entry_or_null(ptr, type, member) ({ \
+ struct list_head *head__ = (ptr); \
+ struct list_head *pos__ = READ_ONCE(head__->next); \
+ pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
+})
/**
* list_next_entry - get the next element in list