xref: /linux-6.15/include/linux/llist.h (revision 8a3e5975)
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 <[email protected]>
49f49f23abSHuang Ying  */
50f49f23abSHuang Ying 
51cd074aeaSWill Deacon #include <linux/atomic.h>
5250b09d61SAndy Shevchenko #include <linux/container_of.h>
5350b09d61SAndy Shevchenko #include <linux/stddef.h>
5450b09d61SAndy 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 /**
77d6b3358aSNeilBrown  * init_llist_node - initialize lock-less list node
78d6b3358aSNeilBrown  * @node:	the node to be initialised
79d6b3358aSNeilBrown  *
80d6b3358aSNeilBrown  * In cases where there is a need to test if a node is on
81d6b3358aSNeilBrown  * a list or not, this initialises the node to clearly
82d6b3358aSNeilBrown  * not be on any list.
83d6b3358aSNeilBrown  */
init_llist_node(struct llist_node * node)84d6b3358aSNeilBrown static inline void init_llist_node(struct llist_node *node)
85d6b3358aSNeilBrown {
86d6b3358aSNeilBrown 	node->next = node;
87d6b3358aSNeilBrown }
88d6b3358aSNeilBrown 
89d6b3358aSNeilBrown /**
90d6b3358aSNeilBrown  * llist_on_list - test if a lock-list list node is on a list
91d6b3358aSNeilBrown  * @node:	the node to test
92d6b3358aSNeilBrown  *
93d6b3358aSNeilBrown  * When a node is on a list the ->next pointer will be NULL or
94d6b3358aSNeilBrown  * some other node.  It can never point to itself.  We use that
95d6b3358aSNeilBrown  * in init_llist_node() to record that a node is not on any list,
96d6b3358aSNeilBrown  * and here to test whether it is on any list.
97d6b3358aSNeilBrown  */
llist_on_list(const struct llist_node * node)98d6b3358aSNeilBrown static inline bool llist_on_list(const struct llist_node *node)
99d6b3358aSNeilBrown {
100d6b3358aSNeilBrown 	return node->next != node;
101d6b3358aSNeilBrown }
102d6b3358aSNeilBrown 
103d6b3358aSNeilBrown /**
104f49f23abSHuang Ying  * llist_entry - get the struct of this entry
105f49f23abSHuang Ying  * @ptr:	the &struct llist_node pointer.
106f49f23abSHuang Ying  * @type:	the type of the struct this is embedded in.
107f49f23abSHuang Ying  * @member:	the name of the llist_node within the struct.
108f49f23abSHuang Ying  */
109f49f23abSHuang Ying #define llist_entry(ptr, type, member)		\
110f49f23abSHuang Ying 	container_of(ptr, type, member)
111f49f23abSHuang Ying 
112f49f23abSHuang Ying /**
113beaec533SAlexander Potapenko  * member_address_is_nonnull - check whether the member address is not NULL
114beaec533SAlexander Potapenko  * @ptr:	the object pointer (struct type * that contains the llist_node)
115beaec533SAlexander Potapenko  * @member:	the name of the llist_node within the struct.
116beaec533SAlexander Potapenko  *
117beaec533SAlexander Potapenko  * This macro is conceptually the same as
118beaec533SAlexander Potapenko  *	&ptr->member != NULL
119beaec533SAlexander Potapenko  * but it works around the fact that compilers can decide that taking a member
120beaec533SAlexander Potapenko  * address is never a NULL pointer.
121beaec533SAlexander Potapenko  *
122beaec533SAlexander Potapenko  * Real objects that start at a high address and have a member at NULL are
123beaec533SAlexander Potapenko  * unlikely to exist, but such pointers may be returned e.g. by the
124beaec533SAlexander Potapenko  * container_of() macro.
125beaec533SAlexander Potapenko  */
126beaec533SAlexander Potapenko #define member_address_is_nonnull(ptr, member)	\
127beaec533SAlexander Potapenko 	((uintptr_t)(ptr) + offsetof(typeof(*(ptr)), member) != 0)
128beaec533SAlexander Potapenko 
129beaec533SAlexander Potapenko /**
130f49f23abSHuang Ying  * llist_for_each - iterate over some deleted entries of a lock-less list
131f49f23abSHuang Ying  * @pos:	the &struct llist_node to use as a loop cursor
132f49f23abSHuang Ying  * @node:	the first entry of deleted list entries
133f49f23abSHuang Ying  *
134f49f23abSHuang Ying  * In general, some entries of the lock-less list can be traversed
135f49f23abSHuang Ying  * safely only after being deleted from list, so start with an entry
136f49f23abSHuang Ying  * instead of list head.
137f49f23abSHuang Ying  *
138f49f23abSHuang Ying  * If being used on entries deleted from lock-less list directly, the
139f49f23abSHuang Ying  * traverse order is from the newest to the oldest added entry.  If
140f49f23abSHuang Ying  * you want to traverse from the oldest to the newest, you must
141f49f23abSHuang Ying  * reverse the order by yourself before traversing.
142f49f23abSHuang Ying  */
143f49f23abSHuang Ying #define llist_for_each(pos, node)			\
144f49f23abSHuang Ying 	for ((pos) = (node); pos; (pos) = (pos)->next)
145f49f23abSHuang Ying 
146f49f23abSHuang Ying /**
147d714893eSByungchul Park  * llist_for_each_safe - iterate over some deleted entries of a lock-less list
148d714893eSByungchul Park  *			 safe against removal of list entry
149d714893eSByungchul Park  * @pos:	the &struct llist_node to use as a loop cursor
150d714893eSByungchul Park  * @n:		another &struct llist_node to use as temporary storage
151d714893eSByungchul Park  * @node:	the first entry of deleted list entries
152d714893eSByungchul Park  *
153d714893eSByungchul Park  * In general, some entries of the lock-less list can be traversed
154d714893eSByungchul Park  * safely only after being deleted from list, so start with an entry
155d714893eSByungchul Park  * instead of list head.
156d714893eSByungchul Park  *
157d714893eSByungchul Park  * If being used on entries deleted from lock-less list directly, the
158d714893eSByungchul Park  * traverse order is from the newest to the oldest added entry.  If
159d714893eSByungchul Park  * you want to traverse from the oldest to the newest, you must
160d714893eSByungchul Park  * reverse the order by yourself before traversing.
161d714893eSByungchul Park  */
162d714893eSByungchul Park #define llist_for_each_safe(pos, n, node)			\
163d714893eSByungchul Park 	for ((pos) = (node); (pos) && ((n) = (pos)->next, true); (pos) = (n))
164d714893eSByungchul Park 
165d714893eSByungchul Park /**
166f49f23abSHuang Ying  * llist_for_each_entry - iterate over some deleted entries of lock-less list of given type
167f49f23abSHuang Ying  * @pos:	the type * to use as a loop cursor.
168f49f23abSHuang Ying  * @node:	the fist entry of deleted list entries.
169f49f23abSHuang Ying  * @member:	the name of the llist_node with the struct.
170f49f23abSHuang Ying  *
171f49f23abSHuang Ying  * In general, some entries of the lock-less list can be traversed
172f49f23abSHuang Ying  * safely only after being removed from list, so start with an entry
173f49f23abSHuang Ying  * instead of list head.
174f49f23abSHuang Ying  *
175f49f23abSHuang Ying  * If being used on entries deleted from lock-less list directly, the
176f49f23abSHuang Ying  * traverse order is from the newest to the oldest added entry.  If
177f49f23abSHuang Ying  * you want to traverse from the oldest to the newest, you must
178f49f23abSHuang Ying  * reverse the order by yourself before traversing.
179f49f23abSHuang Ying  */
180f49f23abSHuang Ying #define llist_for_each_entry(pos, node, member)				\
181f49f23abSHuang Ying 	for ((pos) = llist_entry((node), typeof(*(pos)), member);	\
182beaec533SAlexander Potapenko 	     member_address_is_nonnull(pos, member);			\
183f49f23abSHuang Ying 	     (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member))
184f49f23abSHuang Ying 
185f49f23abSHuang Ying /**
186809850b7SPeter Hurley  * llist_for_each_entry_safe - iterate over some deleted entries of lock-less list of given type
187809850b7SPeter Hurley  *			       safe against removal of list entry
188809850b7SPeter Hurley  * @pos:	the type * to use as a loop cursor.
189809850b7SPeter Hurley  * @n:		another type * to use as temporary storage
190809850b7SPeter Hurley  * @node:	the first entry of deleted list entries.
191809850b7SPeter Hurley  * @member:	the name of the llist_node with the struct.
192809850b7SPeter Hurley  *
193809850b7SPeter Hurley  * In general, some entries of the lock-less list can be traversed
194809850b7SPeter Hurley  * safely only after being removed from list, so start with an entry
195809850b7SPeter Hurley  * instead of list head.
196809850b7SPeter Hurley  *
197809850b7SPeter Hurley  * If being used on entries deleted from lock-less list directly, the
198809850b7SPeter Hurley  * traverse order is from the newest to the oldest added entry.  If
199809850b7SPeter Hurley  * you want to traverse from the oldest to the newest, you must
200809850b7SPeter Hurley  * reverse the order by yourself before traversing.
201809850b7SPeter Hurley  */
202809850b7SPeter Hurley #define llist_for_each_entry_safe(pos, n, node, member)			       \
203809850b7SPeter Hurley 	for (pos = llist_entry((node), typeof(*pos), member);		       \
204beaec533SAlexander Potapenko 	     member_address_is_nonnull(pos, member) &&			       \
205809850b7SPeter Hurley 	        (n = llist_entry(pos->member.next, typeof(*n), member), true); \
206809850b7SPeter Hurley 	     pos = n)
207809850b7SPeter Hurley 
208809850b7SPeter Hurley /**
209f49f23abSHuang Ying  * llist_empty - tests whether a lock-less list is empty
210f49f23abSHuang Ying  * @head:	the list to test
211f49f23abSHuang Ying  *
212f49f23abSHuang Ying  * Not guaranteed to be accurate or up to date.  Just a quick way to
213f49f23abSHuang Ying  * test whether the list is empty without deleting something from the
214f49f23abSHuang Ying  * list.
215f49f23abSHuang Ying  */
llist_empty(const struct llist_head * head)2161230db8eSHuang Ying static inline bool llist_empty(const struct llist_head *head)
217f49f23abSHuang Ying {
2186aa7de05SMark Rutland 	return READ_ONCE(head->first) == NULL;
219f49f23abSHuang Ying }
220f49f23abSHuang Ying 
llist_next(struct llist_node * node)221924f8f5aSPeter Zijlstra static inline struct llist_node *llist_next(struct llist_node *node)
222924f8f5aSPeter Zijlstra {
223924f8f5aSPeter Zijlstra 	return node->next;
224924f8f5aSPeter Zijlstra }
225924f8f5aSPeter Zijlstra 
226e9a17bd7SOleg Nesterov extern bool llist_add_batch(struct llist_node *new_first,
227e9a17bd7SOleg Nesterov 			    struct llist_node *new_last,
228e9a17bd7SOleg Nesterov 			    struct llist_head *head);
229476c5818SPeter Zijlstra 
__llist_add_batch(struct llist_node * new_first,struct llist_node * new_last,struct llist_head * head)230476c5818SPeter Zijlstra static inline bool __llist_add_batch(struct llist_node *new_first,
231476c5818SPeter Zijlstra 				     struct llist_node *new_last,
232476c5818SPeter Zijlstra 				     struct llist_head *head)
233476c5818SPeter Zijlstra {
234476c5818SPeter Zijlstra 	new_last->next = head->first;
235476c5818SPeter Zijlstra 	head->first = new_first;
236476c5818SPeter Zijlstra 	return new_last->next == NULL;
237476c5818SPeter Zijlstra }
238476c5818SPeter Zijlstra 
2391230db8eSHuang Ying /**
2401230db8eSHuang Ying  * llist_add - add a new entry
2411230db8eSHuang Ying  * @new:	new entry to be added
2421230db8eSHuang Ying  * @head:	the head for your lock-less list
243781f7fd9SHuang Ying  *
244fc23af34SAndrew Morton  * Returns true if the list was empty prior to adding this entry.
2451230db8eSHuang Ying  */
llist_add(struct llist_node * new,struct llist_head * head)246781f7fd9SHuang Ying static inline bool llist_add(struct llist_node *new, struct llist_head *head)
2471230db8eSHuang Ying {
248e9a17bd7SOleg Nesterov 	return llist_add_batch(new, new, head);
2491230db8eSHuang Ying }
2501230db8eSHuang Ying 
__llist_add(struct llist_node * new,struct llist_head * head)251476c5818SPeter Zijlstra static inline bool __llist_add(struct llist_node *new, struct llist_head *head)
252476c5818SPeter Zijlstra {
253476c5818SPeter Zijlstra 	return __llist_add_batch(new, new, head);
254476c5818SPeter Zijlstra }
255476c5818SPeter Zijlstra 
2561230db8eSHuang Ying /**
2571230db8eSHuang Ying  * llist_del_all - delete all entries from lock-less list
2581230db8eSHuang Ying  * @head:	the head of lock-less list to delete all entries
2591230db8eSHuang Ying  *
2601230db8eSHuang Ying  * If list is empty, return NULL, otherwise, delete all entries and
2611230db8eSHuang Ying  * return the pointer to the first entry.  The order of entries
2621230db8eSHuang Ying  * deleted is from the newest to the oldest added one.
2631230db8eSHuang Ying  */
llist_del_all(struct llist_head * head)2641230db8eSHuang Ying static inline struct llist_node *llist_del_all(struct llist_head *head)
2651230db8eSHuang Ying {
2661230db8eSHuang Ying 	return xchg(&head->first, NULL);
2671230db8eSHuang Ying }
268540f41edSStephen Rothwell 
__llist_del_all(struct llist_head * head)269476c5818SPeter Zijlstra static inline struct llist_node *__llist_del_all(struct llist_head *head)
270476c5818SPeter Zijlstra {
271476c5818SPeter Zijlstra 	struct llist_node *first = head->first;
272476c5818SPeter Zijlstra 
273476c5818SPeter Zijlstra 	head->first = NULL;
274476c5818SPeter Zijlstra 	return first;
275476c5818SPeter Zijlstra }
276476c5818SPeter Zijlstra 
277540f41edSStephen Rothwell extern struct llist_node *llist_del_first(struct llist_head *head);
278540f41edSStephen Rothwell 
279d6b3358aSNeilBrown /**
280d6b3358aSNeilBrown  * llist_del_first_init - delete first entry from lock-list and mark is as being off-list
281d6b3358aSNeilBrown  * @head:	the head of lock-less list to delete from.
282d6b3358aSNeilBrown  *
283d6b3358aSNeilBrown  * This behave the same as llist_del_first() except that llist_init_node() is called
284d6b3358aSNeilBrown  * on the returned node so that llist_on_list() will report false for the node.
285d6b3358aSNeilBrown  */
llist_del_first_init(struct llist_head * head)286d6b3358aSNeilBrown static inline struct llist_node *llist_del_first_init(struct llist_head *head)
287d6b3358aSNeilBrown {
288d6b3358aSNeilBrown 	struct llist_node *n = llist_del_first(head);
289d6b3358aSNeilBrown 
290d6b3358aSNeilBrown 	if (n)
291d6b3358aSNeilBrown 		init_llist_node(n);
292d6b3358aSNeilBrown 	return n;
293d6b3358aSNeilBrown }
294*8a3e5975SNeilBrown 
295*8a3e5975SNeilBrown extern bool llist_del_first_this(struct llist_head *head,
296*8a3e5975SNeilBrown 				 struct llist_node *this);
297*8a3e5975SNeilBrown 
298b89241e8SChristoph Hellwig struct llist_node *llist_reverse_order(struct llist_node *head);
299b89241e8SChristoph Hellwig 
300f49f23abSHuang Ying #endif /* LLIST_H */
301