xref: /openbmc/linux/include/linux/llist.h (revision 50b09d61)
145051539SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2f49f23abSHuang Ying #ifndef LLIST_H
3f49f23abSHuang Ying #define LLIST_H
4f49f23abSHuang Ying /*
5f49f23abSHuang Ying  * Lock-less NULL terminated single linked list
6f49f23abSHuang Ying  *
7d78973c3SJoel Fernandes  * Cases where locking is not needed:
8d78973c3SJoel Fernandes  * If there are multiple producers and multiple consumers, llist_add can be
9d78973c3SJoel Fernandes  * used in producers and llist_del_all can be used in consumers simultaneously
10d78973c3SJoel Fernandes  * without locking. Also a single consumer can use llist_del_first while
11d78973c3SJoel Fernandes  * multiple producers simultaneously use llist_add, without any locking.
12f49f23abSHuang Ying  *
13d78973c3SJoel Fernandes  * Cases where locking is needed:
14d78973c3SJoel Fernandes  * If we have multiple consumers with llist_del_first used in one consumer, and
15d78973c3SJoel Fernandes  * llist_del_first or llist_del_all used in other consumers, then a lock is
16d78973c3SJoel Fernandes  * needed.  This is because llist_del_first depends on list->first->next not
17d78973c3SJoel Fernandes  * changing, but without lock protection, there's no way to be sure about that
18d78973c3SJoel Fernandes  * if a preemption happens in the middle of the delete operation and on being
19d78973c3SJoel Fernandes  * preempted back, the list->first is the same as before causing the cmpxchg in
20d78973c3SJoel Fernandes  * llist_del_first to succeed. For example, while a llist_del_first operation
21d78973c3SJoel Fernandes  * is in progress in one consumer, then a llist_del_first, llist_add,
22d78973c3SJoel Fernandes  * llist_add (or llist_del_all, llist_add, llist_add) sequence in another
23d78973c3SJoel Fernandes  * consumer may cause violations.
24f49f23abSHuang Ying  *
25d78973c3SJoel Fernandes  * This can be summarized as follows:
26f49f23abSHuang Ying  *
27f49f23abSHuang Ying  *           |   add    | del_first |  del_all
28f49f23abSHuang Ying  * add       |    -     |     -     |     -
29f49f23abSHuang Ying  * del_first |          |     L     |     L
30f49f23abSHuang Ying  * del_all   |          |           |     -
31f49f23abSHuang Ying  *
32d78973c3SJoel Fernandes  * Where, a particular row's operation can happen concurrently with a column's
33d78973c3SJoel Fernandes  * operation, with "-" being no lock needed, while "L" being lock is needed.
34f49f23abSHuang Ying  *
35f49f23abSHuang Ying  * The list entries deleted via llist_del_all can be traversed with
36f49f23abSHuang Ying  * traversing function such as llist_for_each etc.  But the list
37f49f23abSHuang Ying  * entries can not be traversed safely before deleted from the list.
38f49f23abSHuang Ying  * The order of deleted entries is from the newest to the oldest added
39f49f23abSHuang Ying  * one.  If you want to traverse from the oldest to the newest, you
40f49f23abSHuang Ying  * must reverse the order by yourself before traversing.
41f49f23abSHuang Ying  *
42f49f23abSHuang Ying  * The basic atomic operation of this list is cmpxchg on long.  On
43f49f23abSHuang Ying  * architectures that don't have NMI-safe cmpxchg implementation, the
442c30245cSIngo Molnar  * list can NOT be used in NMI handlers.  So code that uses the list in
452c30245cSIngo Molnar  * an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
461230db8eSHuang Ying  *
471230db8eSHuang Ying  * Copyright 2010,2011 Intel Corp.
481230db8eSHuang Ying  *   Author: Huang Ying <ying.huang@intel.com>
49f49f23abSHuang Ying  */
50f49f23abSHuang Ying 
51cd074aeaSWill Deacon #include <linux/atomic.h>
52*50b09d61SAndy Shevchenko #include <linux/container_of.h>
53*50b09d61SAndy Shevchenko #include <linux/stddef.h>
54*50b09d61SAndy Shevchenko #include <linux/types.h>
551230db8eSHuang Ying 
56f49f23abSHuang Ying struct llist_head {
57f49f23abSHuang Ying 	struct llist_node *first;
58f49f23abSHuang Ying };
59f49f23abSHuang Ying 
60f49f23abSHuang Ying struct llist_node {
61f49f23abSHuang Ying 	struct llist_node *next;
62f49f23abSHuang Ying };
63f49f23abSHuang Ying 
64f49f23abSHuang Ying #define LLIST_HEAD_INIT(name)	{ NULL }
65f49f23abSHuang Ying #define LLIST_HEAD(name)	struct llist_head name = LLIST_HEAD_INIT(name)
66f49f23abSHuang Ying 
67f49f23abSHuang Ying /**
68f49f23abSHuang Ying  * init_llist_head - initialize lock-less list head
69f49f23abSHuang Ying  * @head:	the head for your lock-less list
70f49f23abSHuang Ying  */
init_llist_head(struct llist_head * list)71f49f23abSHuang Ying static inline void init_llist_head(struct llist_head *list)
72f49f23abSHuang Ying {
73f49f23abSHuang Ying 	list->first = NULL;
74f49f23abSHuang Ying }
75f49f23abSHuang Ying 
76f49f23abSHuang Ying /**
77f49f23abSHuang Ying  * llist_entry - get the struct of this entry
78f49f23abSHuang Ying  * @ptr:	the &struct llist_node pointer.
79f49f23abSHuang Ying  * @type:	the type of the struct this is embedded in.
80f49f23abSHuang Ying  * @member:	the name of the llist_node within the struct.
81f49f23abSHuang Ying  */
82f49f23abSHuang Ying #define llist_entry(ptr, type, member)		\
83f49f23abSHuang Ying 	container_of(ptr, type, member)
84f49f23abSHuang Ying 
85f49f23abSHuang Ying /**
86beaec533SAlexander Potapenko  * member_address_is_nonnull - check whether the member address is not NULL
87beaec533SAlexander Potapenko  * @ptr:	the object pointer (struct type * that contains the llist_node)
88beaec533SAlexander Potapenko  * @member:	the name of the llist_node within the struct.
89beaec533SAlexander Potapenko  *
90beaec533SAlexander Potapenko  * This macro is conceptually the same as
91beaec533SAlexander Potapenko  *	&ptr->member != NULL
92beaec533SAlexander Potapenko  * but it works around the fact that compilers can decide that taking a member
93beaec533SAlexander Potapenko  * address is never a NULL pointer.
94beaec533SAlexander Potapenko  *
95beaec533SAlexander Potapenko  * Real objects that start at a high address and have a member at NULL are
96beaec533SAlexander Potapenko  * unlikely to exist, but such pointers may be returned e.g. by the
97beaec533SAlexander Potapenko  * container_of() macro.
98beaec533SAlexander Potapenko  */
99beaec533SAlexander Potapenko #define member_address_is_nonnull(ptr, member)	\
100beaec533SAlexander Potapenko 	((uintptr_t)(ptr) + offsetof(typeof(*(ptr)), member) != 0)
101beaec533SAlexander Potapenko 
102beaec533SAlexander Potapenko /**
103f49f23abSHuang Ying  * llist_for_each - iterate over some deleted entries of a lock-less list
104f49f23abSHuang Ying  * @pos:	the &struct llist_node to use as a loop cursor
105f49f23abSHuang Ying  * @node:	the first entry of deleted list entries
106f49f23abSHuang Ying  *
107f49f23abSHuang Ying  * In general, some entries of the lock-less list can be traversed
108f49f23abSHuang Ying  * safely only after being deleted from list, so start with an entry
109f49f23abSHuang Ying  * instead of list head.
110f49f23abSHuang Ying  *
111f49f23abSHuang Ying  * If being used on entries deleted from lock-less list directly, the
112f49f23abSHuang Ying  * traverse order is from the newest to the oldest added entry.  If
113f49f23abSHuang Ying  * you want to traverse from the oldest to the newest, you must
114f49f23abSHuang Ying  * reverse the order by yourself before traversing.
115f49f23abSHuang Ying  */
116f49f23abSHuang Ying #define llist_for_each(pos, node)			\
117f49f23abSHuang Ying 	for ((pos) = (node); pos; (pos) = (pos)->next)
118f49f23abSHuang Ying 
119f49f23abSHuang Ying /**
120d714893eSByungchul Park  * llist_for_each_safe - iterate over some deleted entries of a lock-less list
121d714893eSByungchul Park  *			 safe against removal of list entry
122d714893eSByungchul Park  * @pos:	the &struct llist_node to use as a loop cursor
123d714893eSByungchul Park  * @n:		another &struct llist_node to use as temporary storage
124d714893eSByungchul Park  * @node:	the first entry of deleted list entries
125d714893eSByungchul Park  *
126d714893eSByungchul Park  * In general, some entries of the lock-less list can be traversed
127d714893eSByungchul Park  * safely only after being deleted from list, so start with an entry
128d714893eSByungchul Park  * instead of list head.
129d714893eSByungchul Park  *
130d714893eSByungchul Park  * If being used on entries deleted from lock-less list directly, the
131d714893eSByungchul Park  * traverse order is from the newest to the oldest added entry.  If
132d714893eSByungchul Park  * you want to traverse from the oldest to the newest, you must
133d714893eSByungchul Park  * reverse the order by yourself before traversing.
134d714893eSByungchul Park  */
135d714893eSByungchul Park #define llist_for_each_safe(pos, n, node)			\
136d714893eSByungchul Park 	for ((pos) = (node); (pos) && ((n) = (pos)->next, true); (pos) = (n))
137d714893eSByungchul Park 
138d714893eSByungchul Park /**
139f49f23abSHuang Ying  * llist_for_each_entry - iterate over some deleted entries of lock-less list of given type
140f49f23abSHuang Ying  * @pos:	the type * to use as a loop cursor.
141f49f23abSHuang Ying  * @node:	the fist entry of deleted list entries.
142f49f23abSHuang Ying  * @member:	the name of the llist_node with the struct.
143f49f23abSHuang Ying  *
144f49f23abSHuang Ying  * In general, some entries of the lock-less list can be traversed
145f49f23abSHuang Ying  * safely only after being removed from list, so start with an entry
146f49f23abSHuang Ying  * instead of list head.
147f49f23abSHuang Ying  *
148f49f23abSHuang Ying  * If being used on entries deleted from lock-less list directly, the
149f49f23abSHuang Ying  * traverse order is from the newest to the oldest added entry.  If
150f49f23abSHuang Ying  * you want to traverse from the oldest to the newest, you must
151f49f23abSHuang Ying  * reverse the order by yourself before traversing.
152f49f23abSHuang Ying  */
153f49f23abSHuang Ying #define llist_for_each_entry(pos, node, member)				\
154f49f23abSHuang Ying 	for ((pos) = llist_entry((node), typeof(*(pos)), member);	\
155beaec533SAlexander Potapenko 	     member_address_is_nonnull(pos, member);			\
156f49f23abSHuang Ying 	     (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member))
157f49f23abSHuang Ying 
158f49f23abSHuang Ying /**
159809850b7SPeter Hurley  * llist_for_each_entry_safe - iterate over some deleted entries of lock-less list of given type
160809850b7SPeter Hurley  *			       safe against removal of list entry
161809850b7SPeter Hurley  * @pos:	the type * to use as a loop cursor.
162809850b7SPeter Hurley  * @n:		another type * to use as temporary storage
163809850b7SPeter Hurley  * @node:	the first entry of deleted list entries.
164809850b7SPeter Hurley  * @member:	the name of the llist_node with the struct.
165809850b7SPeter Hurley  *
166809850b7SPeter Hurley  * In general, some entries of the lock-less list can be traversed
167809850b7SPeter Hurley  * safely only after being removed from list, so start with an entry
168809850b7SPeter Hurley  * instead of list head.
169809850b7SPeter Hurley  *
170809850b7SPeter Hurley  * If being used on entries deleted from lock-less list directly, the
171809850b7SPeter Hurley  * traverse order is from the newest to the oldest added entry.  If
172809850b7SPeter Hurley  * you want to traverse from the oldest to the newest, you must
173809850b7SPeter Hurley  * reverse the order by yourself before traversing.
174809850b7SPeter Hurley  */
175809850b7SPeter Hurley #define llist_for_each_entry_safe(pos, n, node, member)			       \
176809850b7SPeter Hurley 	for (pos = llist_entry((node), typeof(*pos), member);		       \
177beaec533SAlexander Potapenko 	     member_address_is_nonnull(pos, member) &&			       \
178809850b7SPeter Hurley 	        (n = llist_entry(pos->member.next, typeof(*n), member), true); \
179809850b7SPeter Hurley 	     pos = n)
180809850b7SPeter Hurley 
181809850b7SPeter Hurley /**
182f49f23abSHuang Ying  * llist_empty - tests whether a lock-less list is empty
183f49f23abSHuang Ying  * @head:	the list to test
184f49f23abSHuang Ying  *
185f49f23abSHuang Ying  * Not guaranteed to be accurate or up to date.  Just a quick way to
186f49f23abSHuang Ying  * test whether the list is empty without deleting something from the
187f49f23abSHuang Ying  * list.
188f49f23abSHuang Ying  */
llist_empty(const struct llist_head * head)1891230db8eSHuang Ying static inline bool llist_empty(const struct llist_head *head)
190f49f23abSHuang Ying {
1916aa7de05SMark Rutland 	return READ_ONCE(head->first) == NULL;
192f49f23abSHuang Ying }
193f49f23abSHuang Ying 
llist_next(struct llist_node * node)194924f8f5aSPeter Zijlstra static inline struct llist_node *llist_next(struct llist_node *node)
195924f8f5aSPeter Zijlstra {
196924f8f5aSPeter Zijlstra 	return node->next;
197924f8f5aSPeter Zijlstra }
198924f8f5aSPeter Zijlstra 
199e9a17bd7SOleg Nesterov extern bool llist_add_batch(struct llist_node *new_first,
200e9a17bd7SOleg Nesterov 			    struct llist_node *new_last,
201e9a17bd7SOleg Nesterov 			    struct llist_head *head);
202476c5818SPeter Zijlstra 
__llist_add_batch(struct llist_node * new_first,struct llist_node * new_last,struct llist_head * head)203476c5818SPeter Zijlstra static inline bool __llist_add_batch(struct llist_node *new_first,
204476c5818SPeter Zijlstra 				     struct llist_node *new_last,
205476c5818SPeter Zijlstra 				     struct llist_head *head)
206476c5818SPeter Zijlstra {
207476c5818SPeter Zijlstra 	new_last->next = head->first;
208476c5818SPeter Zijlstra 	head->first = new_first;
209476c5818SPeter Zijlstra 	return new_last->next == NULL;
210476c5818SPeter Zijlstra }
211476c5818SPeter Zijlstra 
2121230db8eSHuang Ying /**
2131230db8eSHuang Ying  * llist_add - add a new entry
2141230db8eSHuang Ying  * @new:	new entry to be added
2151230db8eSHuang Ying  * @head:	the head for your lock-less list
216781f7fd9SHuang Ying  *
217fc23af34SAndrew Morton  * Returns true if the list was empty prior to adding this entry.
2181230db8eSHuang Ying  */
llist_add(struct llist_node * new,struct llist_head * head)219781f7fd9SHuang Ying static inline bool llist_add(struct llist_node *new, struct llist_head *head)
2201230db8eSHuang Ying {
221e9a17bd7SOleg Nesterov 	return llist_add_batch(new, new, head);
2221230db8eSHuang Ying }
2231230db8eSHuang Ying 
__llist_add(struct llist_node * new,struct llist_head * head)224476c5818SPeter Zijlstra static inline bool __llist_add(struct llist_node *new, struct llist_head *head)
225476c5818SPeter Zijlstra {
226476c5818SPeter Zijlstra 	return __llist_add_batch(new, new, head);
227476c5818SPeter Zijlstra }
228476c5818SPeter Zijlstra 
2291230db8eSHuang Ying /**
2301230db8eSHuang Ying  * llist_del_all - delete all entries from lock-less list
2311230db8eSHuang Ying  * @head:	the head of lock-less list to delete all entries
2321230db8eSHuang Ying  *
2331230db8eSHuang Ying  * If list is empty, return NULL, otherwise, delete all entries and
2341230db8eSHuang Ying  * return the pointer to the first entry.  The order of entries
2351230db8eSHuang Ying  * deleted is from the newest to the oldest added one.
2361230db8eSHuang Ying  */
llist_del_all(struct llist_head * head)2371230db8eSHuang Ying static inline struct llist_node *llist_del_all(struct llist_head *head)
2381230db8eSHuang Ying {
2391230db8eSHuang Ying 	return xchg(&head->first, NULL);
2401230db8eSHuang Ying }
241540f41edSStephen Rothwell 
__llist_del_all(struct llist_head * head)242476c5818SPeter Zijlstra static inline struct llist_node *__llist_del_all(struct llist_head *head)
243476c5818SPeter Zijlstra {
244476c5818SPeter Zijlstra 	struct llist_node *first = head->first;
245476c5818SPeter Zijlstra 
246476c5818SPeter Zijlstra 	head->first = NULL;
247476c5818SPeter Zijlstra 	return first;
248476c5818SPeter Zijlstra }
249476c5818SPeter Zijlstra 
250540f41edSStephen Rothwell extern struct llist_node *llist_del_first(struct llist_head *head);
251540f41edSStephen Rothwell 
252b89241e8SChristoph Hellwig struct llist_node *llist_reverse_order(struct llist_node *head);
253b89241e8SChristoph Hellwig 
254f49f23abSHuang Ying #endif /* LLIST_H */
255