Merge tag 'pull-work.iov_iter-rebased' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / include / linux / list.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef _LINUX_LIST_H
3#define _LINUX_LIST_H
4
cd7187e1 5#include <linux/container_of.h>
de5d9bf6 6#include <linux/types.h>
1da177e4 7#include <linux/stddef.h>
c9cf5528 8#include <linux/poison.h>
e66eed65 9#include <linux/const.h>
cd7187e1
AS
10
11#include <asm/barrier.h>
1da177e4 12
1da177e4 13/*
1eafe075 14 * Circular doubly linked list implementation.
1da177e4
LT
15 *
16 * Some of the internal functions ("__xxx") are useful when
17 * manipulating whole lists rather than single entries, as
18 * sometimes we already know the next/prev entries and we can
19 * generate better code by using them directly rather than
20 * using the generic single-entry routines.
21 */
22
1da177e4
LT
23#define LIST_HEAD_INIT(name) { &(name), &(name) }
24
25#define LIST_HEAD(name) \
26 struct list_head name = LIST_HEAD_INIT(name)
27
46deb744
PM
28/**
29 * INIT_LIST_HEAD - Initialize a list_head structure
30 * @list: list_head structure to be initialized.
31 *
32 * Initializes the list_head to point to itself. If it is a list header,
33 * the result is an empty list.
34 */
490d6ab1
ZB
35static inline void INIT_LIST_HEAD(struct list_head *list)
36{
2f073848 37 WRITE_ONCE(list->next, list);
d679ae94 38 WRITE_ONCE(list->prev, list);
490d6ab1 39}
1da177e4 40
d7c81673
KC
41#ifdef CONFIG_DEBUG_LIST
42extern bool __list_add_valid(struct list_head *new,
43 struct list_head *prev,
44 struct list_head *next);
0cd340dc 45extern bool __list_del_entry_valid(struct list_head *entry);
d7c81673
KC
46#else
47static inline bool __list_add_valid(struct list_head *new,
48 struct list_head *prev,
49 struct list_head *next)
50{
51 return true;
52}
0cd340dc
KC
53static inline bool __list_del_entry_valid(struct list_head *entry)
54{
55 return true;
56}
d7c81673
KC
57#endif
58
1da177e4
LT
59/*
60 * Insert a new entry between two known consecutive entries.
61 *
62 * This is only for internal list manipulation where we know
63 * the prev/next entries already!
64 */
65static inline void __list_add(struct list_head *new,
66 struct list_head *prev,
67 struct list_head *next)
68{
d7c81673
KC
69 if (!__list_add_valid(new, prev, next))
70 return;
71
1da177e4
LT
72 next->prev = new;
73 new->next = next;
74 new->prev = prev;
1c97be67 75 WRITE_ONCE(prev->next, new);
1da177e4
LT
76}
77
78/**
79 * list_add - add a new entry
80 * @new: new entry to be added
81 * @head: list head to add it after
82 *
83 * Insert a new entry after the specified head.
84 * This is good for implementing stacks.
85 */
86static inline void list_add(struct list_head *new, struct list_head *head)
87{
88 __list_add(new, head, head->next);
89}
199a9afc 90
1da177e4
LT
91
92/**
93 * list_add_tail - add a new entry
94 * @new: new entry to be added
95 * @head: list head to add it before
96 *
97 * Insert a new entry before the specified head.
98 * This is useful for implementing queues.
99 */
100static inline void list_add_tail(struct list_head *new, struct list_head *head)
101{
102 __list_add(new, head->prev, head);
103}
104
1da177e4
LT
105/*
106 * Delete a list entry by making the prev/next entries
107 * point to each other.
108 *
109 * This is only for internal list manipulation where we know
110 * the prev/next entries already!
111 */
112static inline void __list_del(struct list_head * prev, struct list_head * next)
113{
114 next->prev = prev;
7f5f873c 115 WRITE_ONCE(prev->next, next);
1da177e4
LT
116}
117
c8af5cd7
THJ
118/*
119 * Delete a list entry and clear the 'prev' pointer.
120 *
121 * This is a special-purpose list clearing method used in the networking code
122 * for lists allocated as per-cpu, where we don't want to incur the extra
123 * WRITE_ONCE() overhead of a regular list_del_init(). The code that uses this
124 * needs to check the node 'prev' pointer instead of calling list_empty().
125 */
126static inline void __list_del_clearprev(struct list_head *entry)
127{
128 __list_del(entry->prev, entry->next);
129 entry->prev = NULL;
130}
131
3c18d4de
LT
132static inline void __list_del_entry(struct list_head *entry)
133{
0cd340dc
KC
134 if (!__list_del_entry_valid(entry))
135 return;
136
3c18d4de
LT
137 __list_del(entry->prev, entry->next);
138}
139
46deb744
PM
140/**
141 * list_del - deletes entry from list.
142 * @entry: the element to delete from the list.
143 * Note: list_empty() on entry does not return true after this, the entry is
144 * in an undefined state.
145 */
1da177e4
LT
146static inline void list_del(struct list_head *entry)
147{
0cd340dc 148 __list_del_entry(entry);
1da177e4
LT
149 entry->next = LIST_POISON1;
150 entry->prev = LIST_POISON2;
151}
152
54e73770
ON
153/**
154 * list_replace - replace old entry by new one
155 * @old : the element to be replaced
156 * @new : the new element to insert
72fd4a35
RD
157 *
158 * If @old was empty, it will be overwritten.
54e73770
ON
159 */
160static inline void list_replace(struct list_head *old,
161 struct list_head *new)
162{
163 new->next = old->next;
164 new->next->prev = new;
165 new->prev = old->prev;
166 new->prev->next = new;
167}
168
46deb744
PM
169/**
170 * list_replace_init - replace old entry by new one and initialize the old one
171 * @old : the element to be replaced
172 * @new : the new element to insert
173 *
174 * If @old was empty, it will be overwritten.
175 */
54e73770 176static inline void list_replace_init(struct list_head *old,
46deb744 177 struct list_head *new)
54e73770
ON
178{
179 list_replace(old, new);
180 INIT_LIST_HEAD(old);
181}
182
e900a918
DW
183/**
184 * list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position
185 * @entry1: the location to place entry2
186 * @entry2: the location to place entry1
187 */
188static inline void list_swap(struct list_head *entry1,
189 struct list_head *entry2)
190{
191 struct list_head *pos = entry2->prev;
192
193 list_del(entry2);
194 list_replace(entry1, entry2);
195 if (pos == entry1)
196 pos = entry2;
197 list_add(entry1, pos);
198}
199
1da177e4
LT
200/**
201 * list_del_init - deletes entry from list and reinitialize it.
202 * @entry: the element to delete from the list.
203 */
204static inline void list_del_init(struct list_head *entry)
205{
3c18d4de 206 __list_del_entry(entry);
1da177e4
LT
207 INIT_LIST_HEAD(entry);
208}
209
210/**
211 * list_move - delete from one list and add as another's head
212 * @list: the entry to move
213 * @head: the head that will precede our entry
214 */
215static inline void list_move(struct list_head *list, struct list_head *head)
216{
3c18d4de 217 __list_del_entry(list);
78db2ad6 218 list_add(list, head);
1da177e4
LT
219}
220
221/**
222 * list_move_tail - delete from one list and add as another's tail
223 * @list: the entry to move
224 * @head: the head that will follow our entry
225 */
226static inline void list_move_tail(struct list_head *list,
227 struct list_head *head)
228{
3c18d4de 229 __list_del_entry(list);
78db2ad6 230 list_add_tail(list, head);
1da177e4
LT
231}
232
df2fc43d
CK
233/**
234 * list_bulk_move_tail - move a subsection of a list to its tail
235 * @head: the head that will follow our entry
236 * @first: first entry to move
237 * @last: last entry to move, can be the same as first
238 *
239 * Move all entries between @first and including @last before @head.
240 * All three entries must belong to the same linked list.
241 */
242static inline void list_bulk_move_tail(struct list_head *head,
243 struct list_head *first,
244 struct list_head *last)
245{
246 first->prev->next = last->next;
247 last->next->prev = first->prev;
248
249 head->prev->next = first;
250 first->prev = head->prev;
251
252 last->next = head;
253 head->prev = last;
254}
255
70b44595 256/**
b736523f 257 * list_is_first -- tests whether @list is the first entry in list @head
70b44595
MG
258 * @list: the entry to test
259 * @head: the head of the list
260 */
04254730 261static inline int list_is_first(const struct list_head *list, const struct list_head *head)
70b44595
MG
262{
263 return list->prev == head;
264}
265
e8f4d97e
SN
266/**
267 * list_is_last - tests whether @list is the last entry in list @head
268 * @list: the entry to test
269 * @head: the head of the list
270 */
04254730 271static inline int list_is_last(const struct list_head *list, const struct list_head *head)
e8f4d97e
SN
272{
273 return list->next == head;
274}
275
04254730
AS
276/**
277 * list_is_head - tests whether @list is the list @head
278 * @list: the entry to test
279 * @head: the head of the list
280 */
281static inline int list_is_head(const struct list_head *list, const struct list_head *head)
282{
283 return list == head;
284}
285
1da177e4
LT
286/**
287 * list_empty - tests whether a list is empty
288 * @head: the list to test.
289 */
290static inline int list_empty(const struct list_head *head)
291{
1658d35e 292 return READ_ONCE(head->next) == head;
1da177e4
LT
293}
294
c6fe44d9
LT
295/**
296 * list_del_init_careful - deletes entry from list and reinitialize it.
297 * @entry: the element to delete from the list.
298 *
299 * This is the same as list_del_init(), except designed to be used
300 * together with list_empty_careful() in a way to guarantee ordering
301 * of other memory operations.
302 *
303 * Any memory operations done before a list_del_init_careful() are
304 * guaranteed to be visible after a list_empty_careful() test.
305 */
306static inline void list_del_init_careful(struct list_head *entry)
307{
308 __list_del_entry(entry);
d679ae94 309 WRITE_ONCE(entry->prev, entry);
c6fe44d9
LT
310 smp_store_release(&entry->next, entry);
311}
312
1da177e4 313/**
fe96e57d
RD
314 * list_empty_careful - tests whether a list is empty and not being modified
315 * @head: the list to test
316 *
317 * Description:
318 * tests whether a list is empty _and_ checks that no other CPU might be
319 * in the process of modifying either member (next or prev)
1da177e4
LT
320 *
321 * NOTE: using list_empty_careful() without synchronization
322 * can only be safe if the only activity that can happen
323 * to the list entry is list_del_init(). Eg. it cannot be used
324 * if another CPU could re-list_add() it.
1da177e4
LT
325 */
326static inline int list_empty_careful(const struct list_head *head)
327{
c6fe44d9 328 struct list_head *next = smp_load_acquire(&head->next);
d679ae94 329 return list_is_head(next, head) && (next == READ_ONCE(head->prev));
99602572
MH
330}
331
5908cdc8
FW
332/**
333 * list_rotate_left - rotate the list to the left
334 * @head: the head of the list
335 */
336static inline void list_rotate_left(struct list_head *head)
337{
338 struct list_head *first;
339
340 if (!list_empty(head)) {
341 first = head->next;
342 list_move_tail(first, head);
343 }
344}
345
a16b5384
TH
346/**
347 * list_rotate_to_front() - Rotate list to specific item.
348 * @list: The desired new front of the list.
349 * @head: The head of the list.
350 *
351 * Rotates list so that @list becomes the new front of the list.
352 */
353static inline void list_rotate_to_front(struct list_head *list,
354 struct list_head *head)
355{
356 /*
357 * Deletes the list head from the list denoted by @head and
358 * places it as the tail of @list, this effectively rotates the
359 * list so that @list is at the front.
360 */
361 list_move_tail(head, list);
362}
363
99602572
MH
364/**
365 * list_is_singular - tests whether a list has just one entry.
366 * @head: the list to test.
367 */
368static inline int list_is_singular(const struct list_head *head)
369{
370 return !list_empty(head) && (head->next == head->prev);
1da177e4
LT
371}
372
00e8a4da
LR
373static inline void __list_cut_position(struct list_head *list,
374 struct list_head *head, struct list_head *entry)
375{
376 struct list_head *new_first = entry->next;
377 list->next = head->next;
378 list->next->prev = list;
379 list->prev = entry;
380 entry->next = list;
381 head->next = new_first;
382 new_first->prev = head;
383}
384
385/**
386 * list_cut_position - cut a list into two
387 * @list: a new list to add all removed entries
388 * @head: a list with entries
389 * @entry: an entry within head, could be the head itself
390 * and if so we won't cut the list
391 *
392 * This helper moves the initial part of @head, up to and
393 * including @entry, from @head to @list. You should
394 * pass on @entry an element you know is on @head. @list
395 * should be an empty list or a list you do not care about
396 * losing its data.
397 *
398 */
399static inline void list_cut_position(struct list_head *list,
400 struct list_head *head, struct list_head *entry)
401{
402 if (list_empty(head))
403 return;
04254730 404 if (list_is_singular(head) && !list_is_head(entry, head) && (entry != head->next))
00e8a4da 405 return;
04254730 406 if (list_is_head(entry, head))
00e8a4da
LR
407 INIT_LIST_HEAD(list);
408 else
409 __list_cut_position(list, head, entry);
410}
411
4ce0017a
EC
412/**
413 * list_cut_before - cut a list into two, before given entry
414 * @list: a new list to add all removed entries
415 * @head: a list with entries
416 * @entry: an entry within head, could be the head itself
417 *
418 * This helper moves the initial part of @head, up to but
419 * excluding @entry, from @head to @list. You should pass
420 * in @entry an element you know is on @head. @list should
421 * be an empty list or a list you do not care about losing
422 * its data.
423 * If @entry == @head, all entries on @head are moved to
424 * @list.
425 */
426static inline void list_cut_before(struct list_head *list,
427 struct list_head *head,
428 struct list_head *entry)
429{
430 if (head->next == entry) {
431 INIT_LIST_HEAD(list);
432 return;
433 }
434 list->next = head->next;
435 list->next->prev = list;
436 list->prev = entry->prev;
437 list->prev->next = list;
438 head->next = entry;
439 entry->prev = head;
440}
441
95d8c365 442static inline void __list_splice(const struct list_head *list,
7d283aee
LR
443 struct list_head *prev,
444 struct list_head *next)
1da177e4
LT
445{
446 struct list_head *first = list->next;
447 struct list_head *last = list->prev;
1da177e4 448
7d283aee
LR
449 first->prev = prev;
450 prev->next = first;
1da177e4 451
7d283aee
LR
452 last->next = next;
453 next->prev = last;
1da177e4
LT
454}
455
456/**
7d283aee 457 * list_splice - join two lists, this is designed for stacks
1da177e4
LT
458 * @list: the new list to add.
459 * @head: the place to add it in the first list.
460 */
95d8c365
RD
461static inline void list_splice(const struct list_head *list,
462 struct list_head *head)
1da177e4
LT
463{
464 if (!list_empty(list))
7d283aee
LR
465 __list_splice(list, head, head->next);
466}
467
468/**
469 * list_splice_tail - join two lists, each list being a queue
470 * @list: the new list to add.
471 * @head: the place to add it in the first list.
472 */
473static inline void list_splice_tail(struct list_head *list,
474 struct list_head *head)
475{
476 if (!list_empty(list))
477 __list_splice(list, head->prev, head);
1da177e4
LT
478}
479
480/**
481 * list_splice_init - join two lists and reinitialise the emptied list.
482 * @list: the new list to add.
483 * @head: the place to add it in the first list.
484 *
485 * The list at @list is reinitialised
486 */
487static inline void list_splice_init(struct list_head *list,
488 struct list_head *head)
489{
490 if (!list_empty(list)) {
7d283aee
LR
491 __list_splice(list, head, head->next);
492 INIT_LIST_HEAD(list);
493 }
494}
495
496/**
6724cce8 497 * list_splice_tail_init - join two lists and reinitialise the emptied list
7d283aee
LR
498 * @list: the new list to add.
499 * @head: the place to add it in the first list.
500 *
6724cce8 501 * Each of the lists is a queue.
7d283aee
LR
502 * The list at @list is reinitialised
503 */
504static inline void list_splice_tail_init(struct list_head *list,
505 struct list_head *head)
506{
507 if (!list_empty(list)) {
508 __list_splice(list, head->prev, head);
1da177e4
LT
509 INIT_LIST_HEAD(list);
510 }
511}
512
513/**
514 * list_entry - get the struct for this entry
515 * @ptr: the &struct list_head pointer.
516 * @type: the type of the struct this is embedded in.
3943f42c 517 * @member: the name of the list_head within the struct.
1da177e4
LT
518 */
519#define list_entry(ptr, type, member) \
520 container_of(ptr, type, member)
521
b5e61818
PE
522/**
523 * list_first_entry - get the first element from a list
524 * @ptr: the list head to take the element from.
525 * @type: the type of the struct this is embedded in.
3943f42c 526 * @member: the name of the list_head within the struct.
b5e61818
PE
527 *
528 * Note, that list is expected to be not empty.
529 */
530#define list_first_entry(ptr, type, member) \
531 list_entry((ptr)->next, type, member)
532
93be3c2e
ON
533/**
534 * list_last_entry - get the last element from a list
535 * @ptr: the list head to take the element from.
536 * @type: the type of the struct this is embedded in.
3943f42c 537 * @member: the name of the list_head within the struct.
93be3c2e
ON
538 *
539 * Note, that list is expected to be not empty.
540 */
541#define list_last_entry(ptr, type, member) \
542 list_entry((ptr)->prev, type, member)
543
6d7581e6
JP
544/**
545 * list_first_entry_or_null - get the first element from a list
546 * @ptr: the list head to take the element from.
547 * @type: the type of the struct this is embedded in.
3943f42c 548 * @member: the name of the list_head within the struct.
6d7581e6
JP
549 *
550 * Note that if the list is empty, it returns NULL.
551 */
12adfd88
CW
552#define list_first_entry_or_null(ptr, type, member) ({ \
553 struct list_head *head__ = (ptr); \
554 struct list_head *pos__ = READ_ONCE(head__->next); \
555 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
556})
6d7581e6 557
008208c6
ON
558/**
559 * list_next_entry - get the next element in list
560 * @pos: the type * to cursor
3943f42c 561 * @member: the name of the list_head within the struct.
008208c6
ON
562 */
563#define list_next_entry(pos, member) \
564 list_entry((pos)->member.next, typeof(*(pos)), member)
565
2fbdf45d
RM
566/**
567 * list_next_entry_circular - get the next element in list
568 * @pos: the type * to cursor.
569 * @head: the list head to take the element from.
570 * @member: the name of the list_head within the struct.
571 *
572 * Wraparound if pos is the last element (return the first element).
573 * Note, that list is expected to be not empty.
574 */
575#define list_next_entry_circular(pos, head, member) \
576 (list_is_last(&(pos)->member, head) ? \
577 list_first_entry(head, typeof(*(pos)), member) : list_next_entry(pos, member))
578
008208c6
ON
579/**
580 * list_prev_entry - get the prev element in list
581 * @pos: the type * to cursor
3943f42c 582 * @member: the name of the list_head within the struct.
008208c6
ON
583 */
584#define list_prev_entry(pos, member) \
585 list_entry((pos)->member.prev, typeof(*(pos)), member)
586
2fbdf45d
RM
587/**
588 * list_prev_entry_circular - get the prev element in list
589 * @pos: the type * to cursor.
590 * @head: the list head to take the element from.
591 * @member: the name of the list_head within the struct.
592 *
593 * Wraparound if pos is the first element (return the last element).
594 * Note, that list is expected to be not empty.
595 */
596#define list_prev_entry_circular(pos, head, member) \
597 (list_is_first(&(pos)->member, head) ? \
598 list_last_entry(head, typeof(*(pos)), member) : list_prev_entry(pos, member))
599
1da177e4
LT
600/**
601 * list_for_each - iterate over a list
8e3a67a9 602 * @pos: the &struct list_head to use as a loop cursor.
1da177e4
LT
603 * @head: the head for your list.
604 */
605#define list_for_each(pos, head) \
04254730 606 for (pos = (head)->next; !list_is_head(pos, (head)); pos = pos->next)
1da177e4 607
ad25f5cb
DH
608/**
609 * list_for_each_rcu - Iterate over a list in an RCU-safe fashion
610 * @pos: the &struct list_head to use as a loop cursor.
611 * @head: the head for your list.
612 */
613#define list_for_each_rcu(pos, head) \
614 for (pos = rcu_dereference((head)->next); \
615 !list_is_head(pos, (head)); \
616 pos = rcu_dereference(pos->next))
617
28ca0d6d
PB
618/**
619 * list_for_each_continue - continue iteration over a list
620 * @pos: the &struct list_head to use as a loop cursor.
621 * @head: the head for your list.
622 *
623 * Continue to iterate over a list, continuing after the current position.
624 */
625#define list_for_each_continue(pos, head) \
04254730 626 for (pos = pos->next; !list_is_head(pos, (head)); pos = pos->next)
28ca0d6d 627
1da177e4
LT
628/**
629 * list_for_each_prev - iterate over a list backwards
8e3a67a9 630 * @pos: the &struct list_head to use as a loop cursor.
1da177e4
LT
631 * @head: the head for your list.
632 */
633#define list_for_each_prev(pos, head) \
04254730 634 for (pos = (head)->prev; !list_is_head(pos, (head)); pos = pos->prev)
1da177e4
LT
635
636/**
fe96e57d 637 * list_for_each_safe - iterate over a list safe against removal of list entry
8e3a67a9 638 * @pos: the &struct list_head to use as a loop cursor.
1da177e4
LT
639 * @n: another &struct list_head to use as temporary storage
640 * @head: the head for your list.
641 */
642#define list_for_each_safe(pos, n, head) \
04254730
AS
643 for (pos = (head)->next, n = pos->next; \
644 !list_is_head(pos, (head)); \
645 pos = n, n = pos->next)
1da177e4 646
37c42524 647/**
8f731f7d 648 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
37c42524
DL
649 * @pos: the &struct list_head to use as a loop cursor.
650 * @n: another &struct list_head to use as temporary storage
651 * @head: the head for your list.
652 */
653#define list_for_each_prev_safe(pos, n, head) \
654 for (pos = (head)->prev, n = pos->prev; \
04254730 655 !list_is_head(pos, (head)); \
37c42524
DL
656 pos = n, n = pos->prev)
657
e1308161
AS
658/**
659 * list_entry_is_head - test if the entry points to the head of the list
660 * @pos: the type * to cursor
661 * @head: the head for your list.
662 * @member: the name of the list_head within the struct.
663 */
664#define list_entry_is_head(pos, head, member) \
665 (&pos->member == (head))
666
1da177e4
LT
667/**
668 * list_for_each_entry - iterate over list of given type
8e3a67a9 669 * @pos: the type * to use as a loop cursor.
1da177e4 670 * @head: the head for your list.
3943f42c 671 * @member: the name of the list_head within the struct.
1da177e4
LT
672 */
673#define list_for_each_entry(pos, head, member) \
93be3c2e 674 for (pos = list_first_entry(head, typeof(*pos), member); \
e1308161 675 !list_entry_is_head(pos, head, member); \
8120e2e5 676 pos = list_next_entry(pos, member))
1da177e4
LT
677
678/**
679 * list_for_each_entry_reverse - iterate backwards over list of given type.
8e3a67a9 680 * @pos: the type * to use as a loop cursor.
1da177e4 681 * @head: the head for your list.
3943f42c 682 * @member: the name of the list_head within the struct.
1da177e4
LT
683 */
684#define list_for_each_entry_reverse(pos, head, member) \
93be3c2e 685 for (pos = list_last_entry(head, typeof(*pos), member); \
e1308161 686 !list_entry_is_head(pos, head, member); \
8120e2e5 687 pos = list_prev_entry(pos, member))
1da177e4
LT
688
689/**
72fd4a35 690 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1da177e4
LT
691 * @pos: the type * to use as a start point
692 * @head: the head of the list
3943f42c 693 * @member: the name of the list_head within the struct.
fe96e57d 694 *
72fd4a35 695 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1da177e4
LT
696 */
697#define list_prepare_entry(pos, head, member) \
698 ((pos) ? : list_entry(head, typeof(*pos), member))
699
700/**
fe96e57d 701 * list_for_each_entry_continue - continue iteration over list of given type
8e3a67a9 702 * @pos: the type * to use as a loop cursor.
1da177e4 703 * @head: the head for your list.
3943f42c 704 * @member: the name of the list_head within the struct.
fe96e57d
RD
705 *
706 * Continue to iterate over list of given type, continuing after
707 * the current position.
1da177e4
LT
708 */
709#define list_for_each_entry_continue(pos, head, member) \
8120e2e5 710 for (pos = list_next_entry(pos, member); \
e1308161 711 !list_entry_is_head(pos, head, member); \
8120e2e5 712 pos = list_next_entry(pos, member))
1da177e4 713
768f3591
PE
714/**
715 * list_for_each_entry_continue_reverse - iterate backwards from the given point
716 * @pos: the type * to use as a loop cursor.
717 * @head: the head for your list.
3943f42c 718 * @member: the name of the list_head within the struct.
768f3591
PE
719 *
720 * Start to iterate over list of given type backwards, continuing after
721 * the current position.
722 */
723#define list_for_each_entry_continue_reverse(pos, head, member) \
8120e2e5 724 for (pos = list_prev_entry(pos, member); \
e1308161 725 !list_entry_is_head(pos, head, member); \
8120e2e5 726 pos = list_prev_entry(pos, member))
768f3591 727
e229c2fb 728/**
fe96e57d 729 * list_for_each_entry_from - iterate over list of given type from the current point
8e3a67a9 730 * @pos: the type * to use as a loop cursor.
e229c2fb 731 * @head: the head for your list.
3943f42c 732 * @member: the name of the list_head within the struct.
fe96e57d
RD
733 *
734 * Iterate over list of given type, continuing from current position.
e229c2fb
ACM
735 */
736#define list_for_each_entry_from(pos, head, member) \
e1308161 737 for (; !list_entry_is_head(pos, head, member); \
8120e2e5 738 pos = list_next_entry(pos, member))
e229c2fb 739
b862815c
JP
740/**
741 * list_for_each_entry_from_reverse - iterate backwards over list of given type
742 * from the current point
743 * @pos: the type * to use as a loop cursor.
744 * @head: the head for your list.
745 * @member: the name of the list_head within the struct.
746 *
747 * Iterate backwards over list of given type, continuing from current position.
748 */
749#define list_for_each_entry_from_reverse(pos, head, member) \
e1308161 750 for (; !list_entry_is_head(pos, head, member); \
b862815c
JP
751 pos = list_prev_entry(pos, member))
752
1da177e4
LT
753/**
754 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
8e3a67a9 755 * @pos: the type * to use as a loop cursor.
1da177e4
LT
756 * @n: another type * to use as temporary storage
757 * @head: the head for your list.
3943f42c 758 * @member: the name of the list_head within the struct.
1da177e4
LT
759 */
760#define list_for_each_entry_safe(pos, n, head, member) \
93be3c2e 761 for (pos = list_first_entry(head, typeof(*pos), member), \
8120e2e5 762 n = list_next_entry(pos, member); \
e1308161 763 !list_entry_is_head(pos, head, member); \
8120e2e5 764 pos = n, n = list_next_entry(n, member))
1da177e4 765
74459dc7 766/**
9a86e2ba 767 * list_for_each_entry_safe_continue - continue list iteration safe against removal
8e3a67a9 768 * @pos: the type * to use as a loop cursor.
74459dc7
ACM
769 * @n: another type * to use as temporary storage
770 * @head: the head for your list.
3943f42c 771 * @member: the name of the list_head within the struct.
fe96e57d
RD
772 *
773 * Iterate over list of given type, continuing after current point,
774 * safe against removal of list entry.
74459dc7
ACM
775 */
776#define list_for_each_entry_safe_continue(pos, n, head, member) \
8120e2e5
ON
777 for (pos = list_next_entry(pos, member), \
778 n = list_next_entry(pos, member); \
e1308161 779 !list_entry_is_head(pos, head, member); \
8120e2e5 780 pos = n, n = list_next_entry(n, member))
d8dcffee
ACM
781
782/**
9a86e2ba 783 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
8e3a67a9 784 * @pos: the type * to use as a loop cursor.
d8dcffee
ACM
785 * @n: another type * to use as temporary storage
786 * @head: the head for your list.
3943f42c 787 * @member: the name of the list_head within the struct.
fe96e57d
RD
788 *
789 * Iterate over list of given type from current point, safe against
790 * removal of list entry.
d8dcffee
ACM
791 */
792#define list_for_each_entry_safe_from(pos, n, head, member) \
8120e2e5 793 for (n = list_next_entry(pos, member); \
e1308161 794 !list_entry_is_head(pos, head, member); \
8120e2e5 795 pos = n, n = list_next_entry(n, member))
74459dc7 796
0ad42352 797/**
9a86e2ba 798 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
8e3a67a9 799 * @pos: the type * to use as a loop cursor.
0ad42352
DH
800 * @n: another type * to use as temporary storage
801 * @head: the head for your list.
3943f42c 802 * @member: the name of the list_head within the struct.
fe96e57d
RD
803 *
804 * Iterate backwards over list of given type, safe against removal
805 * of list entry.
0ad42352
DH
806 */
807#define list_for_each_entry_safe_reverse(pos, n, head, member) \
93be3c2e 808 for (pos = list_last_entry(head, typeof(*pos), member), \
8120e2e5 809 n = list_prev_entry(pos, member); \
e1308161 810 !list_entry_is_head(pos, head, member); \
8120e2e5 811 pos = n, n = list_prev_entry(n, member))
0ad42352 812
57439f87 813/**
814 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
815 * @pos: the loop cursor used in the list_for_each_entry_safe loop
816 * @n: temporary storage used in list_for_each_entry_safe
3943f42c 817 * @member: the name of the list_head within the struct.
57439f87 818 *
819 * list_safe_reset_next is not safe to use in general if the list may be
820 * modified concurrently (eg. the lock is dropped in the loop body). An
821 * exception to this is if the cursor element (pos) is pinned in the list,
822 * and list_safe_reset_next is called after re-taking the lock and before
823 * completing the current iteration of the loop body.
824 */
825#define list_safe_reset_next(pos, n, member) \
8120e2e5 826 n = list_next_entry(pos, member)
57439f87 827
1da177e4
LT
828/*
829 * Double linked lists with a single pointer list head.
830 * Mostly useful for hash tables where the two pointer list head is
831 * too wasteful.
832 * You lose the ability to access the tail in O(1).
833 */
834
1da177e4
LT
835#define HLIST_HEAD_INIT { .first = NULL }
836#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
837#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
490d6ab1
ZB
838static inline void INIT_HLIST_NODE(struct hlist_node *h)
839{
840 h->next = NULL;
841 h->pprev = NULL;
842}
1da177e4 843
46deb744
PM
844/**
845 * hlist_unhashed - Has node been removed from list and reinitialized?
846 * @h: Node to be checked
847 *
848 * Not that not all removal functions will leave a node in unhashed
849 * state. For example, hlist_nulls_del_init_rcu() does leave the
850 * node in unhashed state, but hlist_nulls_del() does not.
851 */
1da177e4
LT
852static inline int hlist_unhashed(const struct hlist_node *h)
853{
854 return !h->pprev;
855}
856
46deb744
PM
857/**
858 * hlist_unhashed_lockless - Version of hlist_unhashed for lockless use
859 * @h: Node to be checked
860 *
861 * This variant of hlist_unhashed() must be used in lockless contexts
862 * to avoid potential load-tearing. The READ_ONCE() is paired with the
863 * various WRITE_ONCE() in hlist helpers that are defined below.
c54a2744
ED
864 */
865static inline int hlist_unhashed_lockless(const struct hlist_node *h)
866{
867 return !READ_ONCE(h->pprev);
868}
869
46deb744
PM
870/**
871 * hlist_empty - Is the specified hlist_head structure an empty hlist?
872 * @h: Structure to check.
873 */
1da177e4
LT
874static inline int hlist_empty(const struct hlist_head *h)
875{
1658d35e 876 return !READ_ONCE(h->first);
1da177e4
LT
877}
878
879static inline void __hlist_del(struct hlist_node *n)
880{
881 struct hlist_node *next = n->next;
882 struct hlist_node **pprev = n->pprev;
7f5f873c
PM
883
884 WRITE_ONCE(*pprev, next);
1da177e4 885 if (next)
c54a2744 886 WRITE_ONCE(next->pprev, pprev);
1da177e4
LT
887}
888
46deb744
PM
889/**
890 * hlist_del - Delete the specified hlist_node from its list
891 * @n: Node to delete.
892 *
893 * Note that this function leaves the node in hashed state. Use
894 * hlist_del_init() or similar instead to unhash @n.
895 */
1da177e4
LT
896static inline void hlist_del(struct hlist_node *n)
897{
898 __hlist_del(n);
899 n->next = LIST_POISON1;
900 n->pprev = LIST_POISON2;
901}
902
46deb744
PM
903/**
904 * hlist_del_init - Delete the specified hlist_node from its list and initialize
905 * @n: Node to delete.
906 *
907 * Note that this function leaves the node in unhashed state.
908 */
1da177e4
LT
909static inline void hlist_del_init(struct hlist_node *n)
910{
da753bea 911 if (!hlist_unhashed(n)) {
1da177e4
LT
912 __hlist_del(n);
913 INIT_HLIST_NODE(n);
914 }
915}
916
46deb744
PM
917/**
918 * hlist_add_head - add a new entry at the beginning of the hlist
919 * @n: new entry to be added
920 * @h: hlist head to add it after
921 *
922 * Insert a new entry after the specified head.
923 * This is good for implementing stacks.
924 */
1da177e4
LT
925static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
926{
927 struct hlist_node *first = h->first;
c54a2744 928 WRITE_ONCE(n->next, first);
1da177e4 929 if (first)
c54a2744 930 WRITE_ONCE(first->pprev, &n->next);
1c97be67 931 WRITE_ONCE(h->first, n);
c54a2744 932 WRITE_ONCE(n->pprev, &h->first);
1da177e4
LT
933}
934
46deb744
PM
935/**
936 * hlist_add_before - add a new entry before the one specified
937 * @n: new entry to be added
938 * @next: hlist node to add it before, which must be non-NULL
939 */
1da177e4 940static inline void hlist_add_before(struct hlist_node *n,
46deb744 941 struct hlist_node *next)
1da177e4 942{
c54a2744
ED
943 WRITE_ONCE(n->pprev, next->pprev);
944 WRITE_ONCE(n->next, next);
945 WRITE_ONCE(next->pprev, &n->next);
1c97be67 946 WRITE_ONCE(*(n->pprev), n);
1da177e4
LT
947}
948
46deb744 949/**
4704bd31 950 * hlist_add_behind - add a new entry after the one specified
46deb744
PM
951 * @n: new entry to be added
952 * @prev: hlist node to add it after, which must be non-NULL
953 */
1d023284
KH
954static inline void hlist_add_behind(struct hlist_node *n,
955 struct hlist_node *prev)
1da177e4 956{
c54a2744
ED
957 WRITE_ONCE(n->next, prev->next);
958 WRITE_ONCE(prev->next, n);
959 WRITE_ONCE(n->pprev, &prev->next);
1da177e4 960
bc18dd33 961 if (n->next)
c54a2744 962 WRITE_ONCE(n->next->pprev, &n->next);
1da177e4
LT
963}
964
46deb744
PM
965/**
966 * hlist_add_fake - create a fake hlist consisting of a single headless node
967 * @n: Node to make a fake list out of
968 *
969 * This makes @n appear to be its own predecessor on a headless hlist.
970 * The point of this is to allow things like hlist_del() to work correctly
971 * in cases where there is no list.
972 */
756acc2d
AV
973static inline void hlist_add_fake(struct hlist_node *n)
974{
975 n->pprev = &n->next;
976}
977
46deb744
PM
978/**
979 * hlist_fake: Is this node a fake hlist?
980 * @h: Node to check for being a self-referential fake hlist.
981 */
cbedaac6
JB
982static inline bool hlist_fake(struct hlist_node *h)
983{
984 return h->pprev == &h->next;
985}
986
46deb744
PM
987/**
988 * hlist_is_singular_node - is node the only element of the specified hlist?
989 * @n: Node to check for singularity.
990 * @h: Header for potentially singular list.
991 *
15dba1e3 992 * Check whether the node is the only node of the head without
46deb744 993 * accessing head, thus avoiding unnecessary cache misses.
15dba1e3
TG
994 */
995static inline bool
996hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
997{
998 return !n->next && n->pprev == &h->first;
999}
1000
46deb744
PM
1001/**
1002 * hlist_move_list - Move an hlist
1003 * @old: hlist_head for old list.
1004 * @new: hlist_head for new list.
1005 *
673d62cc
VN
1006 * Move a list from one list head to another. Fixup the pprev
1007 * reference of the first entry if it exists.
1008 */
1009static inline void hlist_move_list(struct hlist_head *old,
1010 struct hlist_head *new)
1011{
1012 new->first = old->first;
1013 if (new->first)
1014 new->first->pprev = &new->first;
1015 old->first = NULL;
1016}
1017
1da177e4
LT
1018#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1019
1020#define hlist_for_each(pos, head) \
75d65a42 1021 for (pos = (head)->first; pos ; pos = pos->next)
1da177e4
LT
1022
1023#define hlist_for_each_safe(pos, n, head) \
1024 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1025 pos = n)
1026
b67bfe0d 1027#define hlist_entry_safe(ptr, type, member) \
f65846a1
PM
1028 ({ typeof(ptr) ____ptr = (ptr); \
1029 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
1030 })
b67bfe0d 1031
1da177e4
LT
1032/**
1033 * hlist_for_each_entry - iterate over list of given type
b67bfe0d 1034 * @pos: the type * to use as a loop cursor.
1da177e4
LT
1035 * @head: the head for your list.
1036 * @member: the name of the hlist_node within the struct.
1037 */
b67bfe0d
SL
1038#define hlist_for_each_entry(pos, head, member) \
1039 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
1040 pos; \
1041 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1da177e4
LT
1042
1043/**
fe96e57d 1044 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
b67bfe0d 1045 * @pos: the type * to use as a loop cursor.
1da177e4
LT
1046 * @member: the name of the hlist_node within the struct.
1047 */
b67bfe0d
SL
1048#define hlist_for_each_entry_continue(pos, member) \
1049 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
1050 pos; \
1051 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1da177e4
LT
1052
1053/**
fe96e57d 1054 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
b67bfe0d 1055 * @pos: the type * to use as a loop cursor.
1da177e4
LT
1056 * @member: the name of the hlist_node within the struct.
1057 */
b67bfe0d
SL
1058#define hlist_for_each_entry_from(pos, member) \
1059 for (; pos; \
1060 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1da177e4
LT
1061
1062/**
1063 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
b67bfe0d 1064 * @pos: the type * to use as a loop cursor.
c84716c4 1065 * @n: a &struct hlist_node to use as temporary storage
1da177e4
LT
1066 * @head: the head for your list.
1067 * @member: the name of the hlist_node within the struct.
1068 */
b67bfe0d
SL
1069#define hlist_for_each_entry_safe(pos, n, head, member) \
1070 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
1071 pos && ({ n = pos->member.next; 1; }); \
1072 pos = hlist_entry_safe(n, typeof(*pos), member))
1da177e4 1073
1da177e4 1074#endif