blob: b6da9b1dce4d92feb786ea8c5f9557d0626f0ee7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_LIST_H
2#define _LINUX_LIST_H
3
Chris Metcalfde5d9bf2010-07-02 13:41:14 -04004#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/stddef.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -07006#include <linux/poison.h>
Linus Torvaldse66eed62011-05-19 14:15:29 -07007#include <linux/const.h>
Masahiro Yamada8b21d9c2014-10-13 15:51:30 -07008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Simple doubly linked list implementation.
12 *
13 * Some of the internal functions ("__xxx") are useful when
14 * manipulating whole lists rather than single entries, as
15 * sometimes we already know the next/prev entries and we can
16 * generate better code by using them directly rather than
17 * using the generic single-entry routines.
18 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define LIST_HEAD_INIT(name) { &(name), &(name) }
21
22#define LIST_HEAD(name) \
23 struct list_head name = LIST_HEAD_INIT(name)
24
Zach Brown490d6ab2006-02-03 03:03:56 -080025static inline void INIT_LIST_HEAD(struct list_head *list)
26{
Paul E. McKenney2f073842015-10-12 16:56:42 -070027 WRITE_ONCE(list->next, list);
Zach Brown490d6ab2006-02-03 03:03:56 -080028 list->prev = list;
29}
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Kees Cookd7c81672016-08-17 14:42:08 -070031#ifdef CONFIG_DEBUG_LIST
32extern bool __list_add_valid(struct list_head *new,
33 struct list_head *prev,
34 struct list_head *next);
35#else
36static inline bool __list_add_valid(struct list_head *new,
37 struct list_head *prev,
38 struct list_head *next)
39{
40 return true;
41}
42#endif
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * Insert a new entry between two known consecutive entries.
46 *
47 * This is only for internal list manipulation where we know
48 * the prev/next entries already!
49 */
50static inline void __list_add(struct list_head *new,
51 struct list_head *prev,
52 struct list_head *next)
53{
Kees Cookd7c81672016-08-17 14:42:08 -070054 if (!__list_add_valid(new, prev, next))
55 return;
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 next->prev = new;
58 new->next = next;
59 new->prev = prev;
Paul E. McKenney1c97be62015-09-20 22:02:17 -070060 WRITE_ONCE(prev->next, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63/**
64 * list_add - add a new entry
65 * @new: new entry to be added
66 * @head: list head to add it after
67 *
68 * Insert a new entry after the specified head.
69 * This is good for implementing stacks.
70 */
71static inline void list_add(struct list_head *new, struct list_head *head)
72{
73 __list_add(new, head, head->next);
74}
Dave Jones199a9af2006-09-29 01:59:00 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/**
78 * list_add_tail - add a new entry
79 * @new: new entry to be added
80 * @head: list head to add it before
81 *
82 * Insert a new entry before the specified head.
83 * This is useful for implementing queues.
84 */
85static inline void list_add_tail(struct list_head *new, struct list_head *head)
86{
87 __list_add(new, head->prev, head);
88}
89
90/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * Delete a list entry by making the prev/next entries
92 * point to each other.
93 *
94 * This is only for internal list manipulation where we know
95 * the prev/next entries already!
96 */
97static inline void __list_del(struct list_head * prev, struct list_head * next)
98{
99 next->prev = prev;
Paul E. McKenney7f5f8732015-09-18 08:45:22 -0700100 WRITE_ONCE(prev->next, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103/**
104 * list_del - deletes entry from list.
105 * @entry: the element to delete from the list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800106 * Note: list_empty() on entry does not return true after this, the entry is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * in an undefined state.
108 */
Dave Jones199a9af2006-09-29 01:59:00 -0700109#ifndef CONFIG_DEBUG_LIST
Linus Torvalds3c18d4d2011-02-18 11:32:28 -0800110static inline void __list_del_entry(struct list_head *entry)
111{
112 __list_del(entry->prev, entry->next);
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static inline void list_del(struct list_head *entry)
116{
117 __list_del(entry->prev, entry->next);
118 entry->next = LIST_POISON1;
119 entry->prev = LIST_POISON2;
120}
Dave Jones199a9af2006-09-29 01:59:00 -0700121#else
Linus Torvalds3c18d4d2011-02-18 11:32:28 -0800122extern void __list_del_entry(struct list_head *entry);
Dave Jones199a9af2006-09-29 01:59:00 -0700123extern void list_del(struct list_head *entry);
124#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/**
Oleg Nesterov54e73772006-06-23 02:05:54 -0700127 * list_replace - replace old entry by new one
128 * @old : the element to be replaced
129 * @new : the new element to insert
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800130 *
131 * If @old was empty, it will be overwritten.
Oleg Nesterov54e73772006-06-23 02:05:54 -0700132 */
133static inline void list_replace(struct list_head *old,
134 struct list_head *new)
135{
136 new->next = old->next;
137 new->next->prev = new;
138 new->prev = old->prev;
139 new->prev->next = new;
140}
141
142static inline void list_replace_init(struct list_head *old,
143 struct list_head *new)
144{
145 list_replace(old, new);
146 INIT_LIST_HEAD(old);
147}
148
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800149/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * list_del_init - deletes entry from list and reinitialize it.
151 * @entry: the element to delete from the list.
152 */
153static inline void list_del_init(struct list_head *entry)
154{
Linus Torvalds3c18d4d2011-02-18 11:32:28 -0800155 __list_del_entry(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 INIT_LIST_HEAD(entry);
157}
158
159/**
160 * list_move - delete from one list and add as another's head
161 * @list: the entry to move
162 * @head: the head that will precede our entry
163 */
164static inline void list_move(struct list_head *list, struct list_head *head)
165{
Linus Torvalds3c18d4d2011-02-18 11:32:28 -0800166 __list_del_entry(list);
Daniel Walker78db2ad2007-05-12 16:28:35 -0700167 list_add(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170/**
171 * list_move_tail - delete from one list and add as another's tail
172 * @list: the entry to move
173 * @head: the head that will follow our entry
174 */
175static inline void list_move_tail(struct list_head *list,
176 struct list_head *head)
177{
Linus Torvalds3c18d4d2011-02-18 11:32:28 -0800178 __list_del_entry(list);
Daniel Walker78db2ad2007-05-12 16:28:35 -0700179 list_add_tail(list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/**
Shailabh Nagare8f4d972006-07-14 00:24:35 -0700183 * list_is_last - tests whether @list is the last entry in list @head
184 * @list: the entry to test
185 * @head: the head of the list
186 */
187static inline int list_is_last(const struct list_head *list,
188 const struct list_head *head)
189{
190 return list->next == head;
191}
192
193/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * list_empty - tests whether a list is empty
195 * @head: the list to test.
196 */
197static inline int list_empty(const struct list_head *head)
198{
Paul E. McKenney1658d352015-09-20 17:03:16 -0700199 return READ_ONCE(head->next) == head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700203 * list_empty_careful - tests whether a list is empty and not being modified
204 * @head: the list to test
205 *
206 * Description:
207 * tests whether a list is empty _and_ checks that no other CPU might be
208 * in the process of modifying either member (next or prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *
210 * NOTE: using list_empty_careful() without synchronization
211 * can only be safe if the only activity that can happen
212 * to the list entry is list_del_init(). Eg. it cannot be used
213 * if another CPU could re-list_add() it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215static inline int list_empty_careful(const struct list_head *head)
216{
217 struct list_head *next = head->next;
218 return (next == head) && (next == head->prev);
219}
220
Masami Hiramatsu99602572008-04-28 02:14:27 -0700221/**
Frederic Weisbecker5908cdc2010-01-09 20:53:14 +0100222 * list_rotate_left - rotate the list to the left
223 * @head: the head of the list
224 */
225static inline void list_rotate_left(struct list_head *head)
226{
227 struct list_head *first;
228
229 if (!list_empty(head)) {
230 first = head->next;
231 list_move_tail(first, head);
232 }
233}
234
235/**
Masami Hiramatsu99602572008-04-28 02:14:27 -0700236 * list_is_singular - tests whether a list has just one entry.
237 * @head: the list to test.
238 */
239static inline int list_is_singular(const struct list_head *head)
240{
241 return !list_empty(head) && (head->next == head->prev);
242}
243
Luis R. Rodriguez00e8a4d2008-08-06 13:28:54 -0700244static inline void __list_cut_position(struct list_head *list,
245 struct list_head *head, struct list_head *entry)
246{
247 struct list_head *new_first = entry->next;
248 list->next = head->next;
249 list->next->prev = list;
250 list->prev = entry;
251 entry->next = list;
252 head->next = new_first;
253 new_first->prev = head;
254}
255
256/**
257 * list_cut_position - cut a list into two
258 * @list: a new list to add all removed entries
259 * @head: a list with entries
260 * @entry: an entry within head, could be the head itself
261 * and if so we won't cut the list
262 *
263 * This helper moves the initial part of @head, up to and
264 * including @entry, from @head to @list. You should
265 * pass on @entry an element you know is on @head. @list
266 * should be an empty list or a list you do not care about
267 * losing its data.
268 *
269 */
270static inline void list_cut_position(struct list_head *list,
271 struct list_head *head, struct list_head *entry)
272{
273 if (list_empty(head))
274 return;
275 if (list_is_singular(head) &&
276 (head->next != entry && head != entry))
277 return;
278 if (entry == head)
279 INIT_LIST_HEAD(list);
280 else
281 __list_cut_position(list, head, entry);
282}
283
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700284static inline void __list_splice(const struct list_head *list,
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700285 struct list_head *prev,
286 struct list_head *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 struct list_head *first = list->next;
289 struct list_head *last = list->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700291 first->prev = prev;
292 prev->next = first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700294 last->next = next;
295 next->prev = last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298/**
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700299 * list_splice - join two lists, this is designed for stacks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * @list: the new list to add.
301 * @head: the place to add it in the first list.
302 */
Robert P. J. Day95d8c362008-04-29 00:59:29 -0700303static inline void list_splice(const struct list_head *list,
304 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 if (!list_empty(list))
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700307 __list_splice(list, head, head->next);
308}
309
310/**
311 * list_splice_tail - join two lists, each list being a queue
312 * @list: the new list to add.
313 * @head: the place to add it in the first list.
314 */
315static inline void list_splice_tail(struct list_head *list,
316 struct list_head *head)
317{
318 if (!list_empty(list))
319 __list_splice(list, head->prev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322/**
323 * list_splice_init - join two lists and reinitialise the emptied list.
324 * @list: the new list to add.
325 * @head: the place to add it in the first list.
326 *
327 * The list at @list is reinitialised
328 */
329static inline void list_splice_init(struct list_head *list,
330 struct list_head *head)
331{
332 if (!list_empty(list)) {
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700333 __list_splice(list, head, head->next);
334 INIT_LIST_HEAD(list);
335 }
336}
337
338/**
Randy Dunlap6724cce2008-08-08 13:56:20 -0700339 * list_splice_tail_init - join two lists and reinitialise the emptied list
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700340 * @list: the new list to add.
341 * @head: the place to add it in the first list.
342 *
Randy Dunlap6724cce2008-08-08 13:56:20 -0700343 * Each of the lists is a queue.
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700344 * The list at @list is reinitialised
345 */
346static inline void list_splice_tail_init(struct list_head *list,
347 struct list_head *head)
348{
349 if (!list_empty(list)) {
350 __list_splice(list, head->prev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 INIT_LIST_HEAD(list);
352 }
353}
354
355/**
356 * list_entry - get the struct for this entry
357 * @ptr: the &struct list_head pointer.
358 * @type: the type of the struct this is embedded in.
Andrey Utkin3943f422014-11-14 05:09:55 +0400359 * @member: the name of the list_head within the struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
361#define list_entry(ptr, type, member) \
362 container_of(ptr, type, member)
363
364/**
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700365 * list_first_entry - get the first element from a list
366 * @ptr: the list head to take the element from.
367 * @type: the type of the struct this is embedded in.
Andrey Utkin3943f422014-11-14 05:09:55 +0400368 * @member: the name of the list_head within the struct.
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700369 *
370 * Note, that list is expected to be not empty.
371 */
372#define list_first_entry(ptr, type, member) \
373 list_entry((ptr)->next, type, member)
374
375/**
Oleg Nesterov93be3c22013-11-12 15:10:03 -0800376 * list_last_entry - get the last element from a list
377 * @ptr: the list head to take the element from.
378 * @type: the type of the struct this is embedded in.
Andrey Utkin3943f422014-11-14 05:09:55 +0400379 * @member: the name of the list_head within the struct.
Oleg Nesterov93be3c22013-11-12 15:10:03 -0800380 *
381 * Note, that list is expected to be not empty.
382 */
383#define list_last_entry(ptr, type, member) \
384 list_entry((ptr)->prev, type, member)
385
386/**
Jiri Pirko6d7581e2013-05-29 05:02:56 +0000387 * list_first_entry_or_null - get the first element from a list
388 * @ptr: the list head to take the element from.
389 * @type: the type of the struct this is embedded in.
Andrey Utkin3943f422014-11-14 05:09:55 +0400390 * @member: the name of the list_head within the struct.
Jiri Pirko6d7581e2013-05-29 05:02:56 +0000391 *
392 * Note that if the list is empty, it returns NULL.
393 */
Chris Wilson12adfd82016-07-23 19:27:50 +0100394#define list_first_entry_or_null(ptr, type, member) ({ \
395 struct list_head *head__ = (ptr); \
396 struct list_head *pos__ = READ_ONCE(head__->next); \
397 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
398})
Jiri Pirko6d7581e2013-05-29 05:02:56 +0000399
400/**
Oleg Nesterov008208c2013-11-12 15:10:01 -0800401 * list_next_entry - get the next element in list
402 * @pos: the type * to cursor
Andrey Utkin3943f422014-11-14 05:09:55 +0400403 * @member: the name of the list_head within the struct.
Oleg Nesterov008208c2013-11-12 15:10:01 -0800404 */
405#define list_next_entry(pos, member) \
406 list_entry((pos)->member.next, typeof(*(pos)), member)
407
408/**
409 * list_prev_entry - get the prev element in list
410 * @pos: the type * to cursor
Andrey Utkin3943f422014-11-14 05:09:55 +0400411 * @member: the name of the list_head within the struct.
Oleg Nesterov008208c2013-11-12 15:10:01 -0800412 */
413#define list_prev_entry(pos, member) \
414 list_entry((pos)->member.prev, typeof(*(pos)), member)
415
416/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * list_for_each - iterate over a list
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700418 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * @head: the head for your list.
420 */
421#define list_for_each(pos, head) \
Linus Torvaldse66eed62011-05-19 14:15:29 -0700422 for (pos = (head)->next; pos != (head); pos = pos->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * list_for_each_prev - iterate over a list backwards
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700426 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * @head: the head for your list.
428 */
429#define list_for_each_prev(pos, head) \
Linus Torvaldse66eed62011-05-19 14:15:29 -0700430 for (pos = (head)->prev; pos != (head); pos = pos->prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700433 * list_for_each_safe - iterate over a list safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700434 * @pos: the &struct list_head to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * @n: another &struct list_head to use as temporary storage
436 * @head: the head for your list.
437 */
438#define list_for_each_safe(pos, n, head) \
439 for (pos = (head)->next, n = pos->next; pos != (head); \
440 pos = n, n = pos->next)
441
442/**
Randy Dunlap8f731f72007-10-18 23:39:28 -0700443 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
Denis V. Lunev37c42522007-10-16 23:29:53 -0700444 * @pos: the &struct list_head to use as a loop cursor.
445 * @n: another &struct list_head to use as temporary storage
446 * @head: the head for your list.
447 */
448#define list_for_each_prev_safe(pos, n, head) \
449 for (pos = (head)->prev, n = pos->prev; \
Linus Torvaldse66eed62011-05-19 14:15:29 -0700450 pos != (head); \
Denis V. Lunev37c42522007-10-16 23:29:53 -0700451 pos = n, n = pos->prev)
452
453/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * list_for_each_entry - iterate over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700455 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * @head: the head for your list.
Andrey Utkin3943f422014-11-14 05:09:55 +0400457 * @member: the name of the list_head within the struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 */
459#define list_for_each_entry(pos, head, member) \
Oleg Nesterov93be3c22013-11-12 15:10:03 -0800460 for (pos = list_first_entry(head, typeof(*pos), member); \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800461 &pos->member != (head); \
462 pos = list_next_entry(pos, member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464/**
465 * list_for_each_entry_reverse - iterate backwards over list of given type.
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700466 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * @head: the head for your list.
Andrey Utkin3943f422014-11-14 05:09:55 +0400468 * @member: the name of the list_head within the struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
470#define list_for_each_entry_reverse(pos, head, member) \
Oleg Nesterov93be3c22013-11-12 15:10:03 -0800471 for (pos = list_last_entry(head, typeof(*pos), member); \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800472 &pos->member != (head); \
473 pos = list_prev_entry(pos, member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800476 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * @pos: the type * to use as a start point
478 * @head: the head of the list
Andrey Utkin3943f422014-11-14 05:09:55 +0400479 * @member: the name of the list_head within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700480 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800481 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
483#define list_prepare_entry(pos, head, member) \
484 ((pos) ? : list_entry(head, typeof(*pos), member))
485
486/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700487 * list_for_each_entry_continue - continue iteration over list of given type
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700488 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * @head: the head for your list.
Andrey Utkin3943f422014-11-14 05:09:55 +0400490 * @member: the name of the list_head within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700491 *
492 * Continue to iterate over list of given type, continuing after
493 * the current position.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
495#define list_for_each_entry_continue(pos, head, member) \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800496 for (pos = list_next_entry(pos, member); \
497 &pos->member != (head); \
498 pos = list_next_entry(pos, member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500/**
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700501 * list_for_each_entry_continue_reverse - iterate backwards from the given point
502 * @pos: the type * to use as a loop cursor.
503 * @head: the head for your list.
Andrey Utkin3943f422014-11-14 05:09:55 +0400504 * @member: the name of the list_head within the struct.
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700505 *
506 * Start to iterate over list of given type backwards, continuing after
507 * the current position.
508 */
509#define list_for_each_entry_continue_reverse(pos, head, member) \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800510 for (pos = list_prev_entry(pos, member); \
511 &pos->member != (head); \
512 pos = list_prev_entry(pos, member))
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700513
514/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700515 * list_for_each_entry_from - iterate over list of given type from the current point
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700516 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800517 * @head: the head for your list.
Andrey Utkin3943f422014-11-14 05:09:55 +0400518 * @member: the name of the list_head within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700519 *
520 * Iterate over list of given type, continuing from current position.
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800521 */
522#define list_for_each_entry_from(pos, head, member) \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800523 for (; &pos->member != (head); \
524 pos = list_next_entry(pos, member))
Arnaldo Carvalho de Meloe229c2f2006-03-20 17:19:17 -0800525
526/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700528 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * @n: another type * to use as temporary storage
530 * @head: the head for your list.
Andrey Utkin3943f422014-11-14 05:09:55 +0400531 * @member: the name of the list_head within the struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
533#define list_for_each_entry_safe(pos, n, head, member) \
Oleg Nesterov93be3c22013-11-12 15:10:03 -0800534 for (pos = list_first_entry(head, typeof(*pos), member), \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800535 n = list_next_entry(pos, member); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 &pos->member != (head); \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800537 pos = n, n = list_next_entry(n, member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800540 * list_for_each_entry_safe_continue - continue list iteration safe against removal
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700541 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700542 * @n: another type * to use as temporary storage
543 * @head: the head for your list.
Andrey Utkin3943f422014-11-14 05:09:55 +0400544 * @member: the name of the list_head within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700545 *
546 * Iterate over list of given type, continuing after current point,
547 * safe against removal of list entry.
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700548 */
549#define list_for_each_entry_safe_continue(pos, n, head, member) \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800550 for (pos = list_next_entry(pos, member), \
551 n = list_next_entry(pos, member); \
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700552 &pos->member != (head); \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800553 pos = n, n = list_next_entry(n, member))
Arnaldo Carvalho de Melo74459dc2005-08-09 20:15:51 -0700554
555/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800556 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700557 * @pos: the type * to use as a loop cursor.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800558 * @n: another type * to use as temporary storage
559 * @head: the head for your list.
Andrey Utkin3943f422014-11-14 05:09:55 +0400560 * @member: the name of the list_head within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700561 *
562 * Iterate over list of given type from current point, safe against
563 * removal of list entry.
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800564 */
565#define list_for_each_entry_safe_from(pos, n, head, member) \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800566 for (n = list_next_entry(pos, member); \
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800567 &pos->member != (head); \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800568 pos = n, n = list_next_entry(n, member))
Arnaldo Carvalho de Melod8dcffe2006-03-20 17:18:05 -0800569
570/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800571 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
Randy Dunlap8e3a67a2006-06-25 05:47:43 -0700572 * @pos: the type * to use as a loop cursor.
David Howells0ad42352006-01-09 20:51:31 -0800573 * @n: another type * to use as temporary storage
574 * @head: the head for your list.
Andrey Utkin3943f422014-11-14 05:09:55 +0400575 * @member: the name of the list_head within the struct.
Randy Dunlapfe96e572006-06-25 05:47:42 -0700576 *
577 * Iterate backwards over list of given type, safe against removal
578 * of list entry.
David Howells0ad42352006-01-09 20:51:31 -0800579 */
580#define list_for_each_entry_safe_reverse(pos, n, head, member) \
Oleg Nesterov93be3c22013-11-12 15:10:03 -0800581 for (pos = list_last_entry(head, typeof(*pos), member), \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800582 n = list_prev_entry(pos, member); \
David Howells0ad42352006-01-09 20:51:31 -0800583 &pos->member != (head); \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800584 pos = n, n = list_prev_entry(n, member))
David Howells0ad42352006-01-09 20:51:31 -0800585
npiggin@suse.de57439f82010-06-24 13:02:14 +1000586/**
587 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
588 * @pos: the loop cursor used in the list_for_each_entry_safe loop
589 * @n: temporary storage used in list_for_each_entry_safe
Andrey Utkin3943f422014-11-14 05:09:55 +0400590 * @member: the name of the list_head within the struct.
npiggin@suse.de57439f82010-06-24 13:02:14 +1000591 *
592 * list_safe_reset_next is not safe to use in general if the list may be
593 * modified concurrently (eg. the lock is dropped in the loop body). An
594 * exception to this is if the cursor element (pos) is pinned in the list,
595 * and list_safe_reset_next is called after re-taking the lock and before
596 * completing the current iteration of the loop body.
597 */
598#define list_safe_reset_next(pos, n, member) \
Oleg Nesterov8120e2e2013-11-12 15:10:02 -0800599 n = list_next_entry(pos, member)
npiggin@suse.de57439f82010-06-24 13:02:14 +1000600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/*
602 * Double linked lists with a single pointer list head.
603 * Mostly useful for hash tables where the two pointer list head is
604 * too wasteful.
605 * You lose the ability to access the tail in O(1).
606 */
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608#define HLIST_HEAD_INIT { .first = NULL }
609#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
610#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
Zach Brown490d6ab2006-02-03 03:03:56 -0800611static inline void INIT_HLIST_NODE(struct hlist_node *h)
612{
613 h->next = NULL;
614 h->pprev = NULL;
615}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617static inline int hlist_unhashed(const struct hlist_node *h)
618{
619 return !h->pprev;
620}
621
622static inline int hlist_empty(const struct hlist_head *h)
623{
Paul E. McKenney1658d352015-09-20 17:03:16 -0700624 return !READ_ONCE(h->first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627static inline void __hlist_del(struct hlist_node *n)
628{
629 struct hlist_node *next = n->next;
630 struct hlist_node **pprev = n->pprev;
Paul E. McKenney7f5f8732015-09-18 08:45:22 -0700631
632 WRITE_ONCE(*pprev, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (next)
634 next->pprev = pprev;
635}
636
637static inline void hlist_del(struct hlist_node *n)
638{
639 __hlist_del(n);
640 n->next = LIST_POISON1;
641 n->pprev = LIST_POISON2;
642}
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644static inline void hlist_del_init(struct hlist_node *n)
645{
Akinobu Mitada753be2006-04-28 15:21:23 -0700646 if (!hlist_unhashed(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 __hlist_del(n);
648 INIT_HLIST_NODE(n);
649 }
650}
651
652static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
653{
654 struct hlist_node *first = h->first;
655 n->next = first;
656 if (first)
657 first->pprev = &n->next;
Paul E. McKenney1c97be62015-09-20 22:02:17 -0700658 WRITE_ONCE(h->first, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 n->pprev = &h->first;
660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/* next must be != NULL */
663static inline void hlist_add_before(struct hlist_node *n,
664 struct hlist_node *next)
665{
666 n->pprev = next->pprev;
667 n->next = next;
668 next->pprev = &n->next;
Paul E. McKenney1c97be62015-09-20 22:02:17 -0700669 WRITE_ONCE(*(n->pprev), n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Ken Helias1d023282014-08-06 16:09:16 -0700672static inline void hlist_add_behind(struct hlist_node *n,
673 struct hlist_node *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Ken Heliasbc18dd32014-08-06 16:09:14 -0700675 n->next = prev->next;
Paul E. McKenney1c97be62015-09-20 22:02:17 -0700676 WRITE_ONCE(prev->next, n);
Ken Heliasbc18dd32014-08-06 16:09:14 -0700677 n->pprev = &prev->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Ken Heliasbc18dd32014-08-06 16:09:14 -0700679 if (n->next)
680 n->next->pprev = &n->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Al Viro756acc22010-10-23 15:23:40 -0400683/* after that we'll appear to be on some hlist and hlist_del will work */
684static inline void hlist_add_fake(struct hlist_node *n)
685{
686 n->pprev = &n->next;
687}
688
Josef Bacikcbedaac2015-03-12 08:19:11 -0400689static inline bool hlist_fake(struct hlist_node *h)
690{
691 return h->pprev == &h->next;
692}
693
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200694/*
Thomas Gleixner15dba1e2016-07-04 09:50:27 +0000695 * Check whether the node is the only node of the head without
696 * accessing head:
697 */
698static inline bool
699hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
700{
701 return !n->next && n->pprev == &h->first;
702}
703
704/*
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200705 * Move a list from one list head to another. Fixup the pprev
706 * reference of the first entry if it exists.
707 */
708static inline void hlist_move_list(struct hlist_head *old,
709 struct hlist_head *new)
710{
711 new->first = old->first;
712 if (new->first)
713 new->first->pprev = &new->first;
714 old->first = NULL;
715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
718
719#define hlist_for_each(pos, head) \
Linus Torvalds75d65a42011-05-19 13:50:07 -0700720 for (pos = (head)->first; pos ; pos = pos->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722#define hlist_for_each_safe(pos, n, head) \
723 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
724 pos = n)
725
Sasha Levinb67bfe02013-02-27 17:06:00 -0800726#define hlist_entry_safe(ptr, type, member) \
Paul E. McKenneyf65846a2013-03-09 07:38:41 -0800727 ({ typeof(ptr) ____ptr = (ptr); \
728 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
729 })
Sasha Levinb67bfe02013-02-27 17:06:00 -0800730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/**
732 * hlist_for_each_entry - iterate over list of given type
Sasha Levinb67bfe02013-02-27 17:06:00 -0800733 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * @head: the head for your list.
735 * @member: the name of the hlist_node within the struct.
736 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800737#define hlist_for_each_entry(pos, head, member) \
738 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
739 pos; \
740 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700743 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
Sasha Levinb67bfe02013-02-27 17:06:00 -0800744 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 * @member: the name of the hlist_node within the struct.
746 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800747#define hlist_for_each_entry_continue(pos, member) \
748 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
749 pos; \
750 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752/**
Randy Dunlapfe96e572006-06-25 05:47:42 -0700753 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
Sasha Levinb67bfe02013-02-27 17:06:00 -0800754 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * @member: the name of the hlist_node within the struct.
756 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800757#define hlist_for_each_entry_from(pos, member) \
758 for (; pos; \
759 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761/**
762 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Sasha Levinb67bfe02013-02-27 17:06:00 -0800763 * @pos: the type * to use as a loop cursor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * @n: another &struct hlist_node to use as temporary storage
765 * @head: the head for your list.
766 * @member: the name of the hlist_node within the struct.
767 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800768#define hlist_for_each_entry_safe(pos, n, head, member) \
769 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
770 pos && ({ n = pos->member.next; 1; }); \
771 pos = hlist_entry_safe(n, typeof(*pos), member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773#endif