1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_LIST_H
3 #define _LINUX_LIST_H
4
5 #include <linux/container_of.h>
6 #include <linux/types.h>
7 #include <linux/stddef.h>
8 #include <linux/poison.h>
9 #include <linux/const.h>
10
11 #include <asm/barrier.h>
12
13 /*
14 * Circular doubly linked list implementation.
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
23 /**
24 * LIST_HEAD_INIT - initialize a &struct list_head's links to point to itself
25 * @name: name of the list_head
26 */
27 #define LIST_HEAD_INIT(name) { &(name), &(name) }
28
29 /**
30 * LIST_HEAD - definition of a &struct list_head with initialization values
31 * @name: name of the list_head
32 */
33 #define LIST_HEAD(name) \
34 struct list_head name = LIST_HEAD_INIT(name)
35
36 /**
37 * INIT_LIST_HEAD - Initialize a list_head structure
38 * @list: list_head structure to be initialized.
39 *
40 * Initializes the list_head to point to itself. If it is a list header,
41 * the result is an empty list.
42 */
INIT_LIST_HEAD(struct list_head * list)43 static inline void INIT_LIST_HEAD(struct list_head *list)
44 {
45 WRITE_ONCE(list->next, list);
46 WRITE_ONCE(list->prev, list);
47 }
48
49 #ifdef CONFIG_LIST_HARDENED
50
51 #ifdef CONFIG_DEBUG_LIST
52 # define __list_valid_slowpath
53 #else
54 # define __list_valid_slowpath __cold __preserve_most
55 #endif
56
57 /*
58 * Performs the full set of list corruption checks before __list_add().
59 * On list corruption reports a warning, and returns false.
60 */
61 bool __list_valid_slowpath __list_add_valid_or_report(struct list_head *new,
62 struct list_head *prev,
63 struct list_head *next);
64
65 /*
66 * Performs list corruption checks before __list_add(). Returns false if a
67 * corruption is detected, true otherwise.
68 *
69 * With CONFIG_LIST_HARDENED only, performs minimal list integrity checking
70 * inline to catch non-faulting corruptions, and only if a corruption is
71 * detected calls the reporting function __list_add_valid_or_report().
72 */
__list_add_valid(struct list_head * new,struct list_head * prev,struct list_head * next)73 static __always_inline bool __list_add_valid(struct list_head *new,
74 struct list_head *prev,
75 struct list_head *next)
76 {
77 bool ret = true;
78
79 if (!IS_ENABLED(CONFIG_DEBUG_LIST)) {
80 /*
81 * With the hardening version, elide checking if next and prev
82 * are NULL, since the immediate dereference of them below would
83 * result in a fault if NULL.
84 *
85 * With the reduced set of checks, we can afford to inline the
86 * checks, which also gives the compiler a chance to elide some
87 * of them completely if they can be proven at compile-time. If
88 * one of the pre-conditions does not hold, the slow-path will
89 * show a report which pre-condition failed.
90 */
91 if (likely(next->prev == prev && prev->next == next && new != prev && new != next))
92 return true;
93 ret = false;
94 }
95
96 ret &= __list_add_valid_or_report(new, prev, next);
97 return ret;
98 }
99
100 /*
101 * Performs the full set of list corruption checks before __list_del_entry().
102 * On list corruption reports a warning, and returns false.
103 */
104 bool __list_valid_slowpath __list_del_entry_valid_or_report(struct list_head *entry);
105
106 /*
107 * Performs list corruption checks before __list_del_entry(). Returns false if a
108 * corruption is detected, true otherwise.
109 *
110 * With CONFIG_LIST_HARDENED only, performs minimal list integrity checking
111 * inline to catch non-faulting corruptions, and only if a corruption is
112 * detected calls the reporting function __list_del_entry_valid_or_report().
113 */
__list_del_entry_valid(struct list_head * entry)114 static __always_inline bool __list_del_entry_valid(struct list_head *entry)
115 {
116 bool ret = true;
117
118 if (!IS_ENABLED(CONFIG_DEBUG_LIST)) {
119 struct list_head *prev = entry->prev;
120 struct list_head *next = entry->next;
121
122 /*
123 * With the hardening version, elide checking if next and prev
124 * are NULL, LIST_POISON1 or LIST_POISON2, since the immediate
125 * dereference of them below would result in a fault.
126 */
127 if (likely(prev->next == entry && next->prev == entry))
128 return true;
129 ret = false;
130 }
131
132 ret &= __list_del_entry_valid_or_report(entry);
133 return ret;
134 }
135 #else
__list_add_valid(struct list_head * new,struct list_head * prev,struct list_head * next)136 static inline bool __list_add_valid(struct list_head *new,
137 struct list_head *prev,
138 struct list_head *next)
139 {
140 return true;
141 }
__list_del_entry_valid(struct list_head * entry)142 static inline bool __list_del_entry_valid(struct list_head *entry)
143 {
144 return true;
145 }
146 #endif
147
148 /*
149 * Insert a new entry between two known consecutive entries.
150 *
151 * This is only for internal list manipulation where we know
152 * the prev/next entries already!
153 */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)154 static inline void __list_add(struct list_head *new,
155 struct list_head *prev,
156 struct list_head *next)
157 {
158 if (!__list_add_valid(new, prev, next))
159 return;
160
161 next->prev = new;
162 new->next = next;
163 new->prev = prev;
164 WRITE_ONCE(prev->next, new);
165 }
166
167 /**
168 * list_add - add a new entry
169 * @new: new entry to be added
170 * @head: list head to add it after
171 *
172 * Insert a new entry after the specified head.
173 * This is good for implementing stacks.
174 */
list_add(struct list_head * new,struct list_head * head)175 static inline void list_add(struct list_head *new, struct list_head *head)
176 {
177 __list_add(new, head, head->next);
178 }
179
180
181 /**
182 * list_add_tail - add a new entry
183 * @new: new entry to be added
184 * @head: list head to add it before
185 *
186 * Insert a new entry before the specified head.
187 * This is useful for implementing queues.
188 */
list_add_tail(struct list_head * new,struct list_head * head)189 static inline void list_add_tail(struct list_head *new, struct list_head *head)
190 {
191 __list_add(new, head->prev, head);
192 }
193
194 /**
195 * list_add_tail_release - add a new entry with release barrier
196 * @new: new entry to be added
197 * @head: list head to add it before
198 *
199 * Insert a new entry before the specified head, using a release barrier to set
200 * the ->next pointer that points to it. This is useful for implementing
201 * queues, in particular one that the elements will be walked through forwards
202 * locklessly.
203 */
list_add_tail_release(struct list_head * new,struct list_head * head)204 static inline void list_add_tail_release(struct list_head *new,
205 struct list_head *head)
206 {
207 struct list_head *prev = head->prev;
208
209 if (__list_add_valid(new, prev, head)) {
210 new->next = head;
211 new->prev = prev;
212 head->prev = new;
213 smp_store_release(&prev->next, new);
214 }
215 }
216
217 /*
218 * Delete a list entry by making the prev/next entries
219 * point to each other.
220 *
221 * This is only for internal list manipulation where we know
222 * the prev/next entries already!
223 */
__list_del(struct list_head * prev,struct list_head * next)224 static inline void __list_del(struct list_head * prev, struct list_head * next)
225 {
226 next->prev = prev;
227 WRITE_ONCE(prev->next, next);
228 }
229
230 /*
231 * Delete a list entry and clear the 'prev' pointer.
232 *
233 * This is a special-purpose list clearing method used in the networking code
234 * for lists allocated as per-cpu, where we don't want to incur the extra
235 * WRITE_ONCE() overhead of a regular list_del_init(). The code that uses this
236 * needs to check the node 'prev' pointer instead of calling list_empty().
237 */
__list_del_clearprev(struct list_head * entry)238 static inline void __list_del_clearprev(struct list_head *entry)
239 {
240 __list_del(entry->prev, entry->next);
241 entry->prev = NULL;
242 }
243
__list_del_entry(struct list_head * entry)244 static inline void __list_del_entry(struct list_head *entry)
245 {
246 if (!__list_del_entry_valid(entry))
247 return;
248
249 __list_del(entry->prev, entry->next);
250 }
251
252 /**
253 * list_del - deletes entry from list.
254 * @entry: the element to delete from the list.
255 * Note: list_empty() on entry does not return true after this, the entry is
256 * in an undefined state.
257 */
list_del(struct list_head * entry)258 static inline void list_del(struct list_head *entry)
259 {
260 __list_del_entry(entry);
261 entry->next = LIST_POISON1;
262 entry->prev = LIST_POISON2;
263 }
264
265 /**
266 * list_replace - replace old entry by new one
267 * @old : the element to be replaced
268 * @new : the new element to insert
269 *
270 * If @old was empty, it will be overwritten.
271 */
list_replace(struct list_head * old,struct list_head * new)272 static inline void list_replace(struct list_head *old,
273 struct list_head *new)
274 {
275 new->next = old->next;
276 new->next->prev = new;
277 new->prev = old->prev;
278 new->prev->next = new;
279 }
280
281 /**
282 * list_replace_init - replace old entry by new one and initialize the old one
283 * @old : the element to be replaced
284 * @new : the new element to insert
285 *
286 * If @old was empty, it will be overwritten.
287 */
list_replace_init(struct list_head * old,struct list_head * new)288 static inline void list_replace_init(struct list_head *old,
289 struct list_head *new)
290 {
291 list_replace(old, new);
292 INIT_LIST_HEAD(old);
293 }
294
295 /**
296 * list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position
297 * @entry1: the location to place entry2
298 * @entry2: the location to place entry1
299 */
list_swap(struct list_head * entry1,struct list_head * entry2)300 static inline void list_swap(struct list_head *entry1,
301 struct list_head *entry2)
302 {
303 struct list_head *pos = entry2->prev;
304
305 list_del(entry2);
306 list_replace(entry1, entry2);
307 if (pos == entry1)
308 pos = entry2;
309 list_add(entry1, pos);
310 }
311
312 /**
313 * list_del_init - deletes entry from list and reinitialize it.
314 * @entry: the element to delete from the list.
315 */
list_del_init(struct list_head * entry)316 static inline void list_del_init(struct list_head *entry)
317 {
318 __list_del_entry(entry);
319 INIT_LIST_HEAD(entry);
320 }
321
322 /**
323 * list_move - delete from one list and add as another's head
324 * @list: the entry to move
325 * @head: the head that will precede our entry
326 */
list_move(struct list_head * list,struct list_head * head)327 static inline void list_move(struct list_head *list, struct list_head *head)
328 {
329 __list_del_entry(list);
330 list_add(list, head);
331 }
332
333 /**
334 * list_move_tail - delete from one list and add as another's tail
335 * @list: the entry to move
336 * @head: the head that will follow our entry
337 */
list_move_tail(struct list_head * list,struct list_head * head)338 static inline void list_move_tail(struct list_head *list,
339 struct list_head *head)
340 {
341 __list_del_entry(list);
342 list_add_tail(list, head);
343 }
344
345 /**
346 * list_bulk_move_tail - move a subsection of a list to its tail
347 * @head: the head that will follow our entry
348 * @first: first entry to move
349 * @last: last entry to move, can be the same as first
350 *
351 * Move all entries between @first and including @last before @head.
352 * All three entries must belong to the same linked list.
353 */
list_bulk_move_tail(struct list_head * head,struct list_head * first,struct list_head * last)354 static inline void list_bulk_move_tail(struct list_head *head,
355 struct list_head *first,
356 struct list_head *last)
357 {
358 first->prev->next = last->next;
359 last->next->prev = first->prev;
360
361 head->prev->next = first;
362 first->prev = head->prev;
363
364 last->next = head;
365 head->prev = last;
366 }
367
368 /**
369 * list_is_first -- tests whether @list is the first entry in list @head
370 * @list: the entry to test
371 * @head: the head of the list
372 */
list_is_first(const struct list_head * list,const struct list_head * head)373 static inline int list_is_first(const struct list_head *list, const struct list_head *head)
374 {
375 return list->prev == head;
376 }
377
378 /**
379 * list_is_last - tests whether @list is the last entry in list @head
380 * @list: the entry to test
381 * @head: the head of the list
382 */
list_is_last(const struct list_head * list,const struct list_head * head)383 static inline int list_is_last(const struct list_head *list, const struct list_head *head)
384 {
385 return list->next == head;
386 }
387
388 /**
389 * list_is_head - tests whether @list is the list @head
390 * @list: the entry to test
391 * @head: the head of the list
392 */
list_is_head(const struct list_head * list,const struct list_head * head)393 static inline int list_is_head(const struct list_head *list, const struct list_head *head)
394 {
395 return list == head;
396 }
397
398 /**
399 * list_empty - tests whether a list is empty
400 * @head: the list to test.
401 */
list_empty(const struct list_head * head)402 static inline int list_empty(const struct list_head *head)
403 {
404 return READ_ONCE(head->next) == head;
405 }
406
407 /**
408 * list_del_init_careful - deletes entry from list and reinitialize it.
409 * @entry: the element to delete from the list.
410 *
411 * This is the same as list_del_init(), except designed to be used
412 * together with list_empty_careful() in a way to guarantee ordering
413 * of other memory operations.
414 *
415 * Any memory operations done before a list_del_init_careful() are
416 * guaranteed to be visible after a list_empty_careful() test.
417 */
list_del_init_careful(struct list_head * entry)418 static inline void list_del_init_careful(struct list_head *entry)
419 {
420 __list_del_entry(entry);
421 WRITE_ONCE(entry->prev, entry);
422 smp_store_release(&entry->next, entry);
423 }
424
425 /**
426 * list_empty_careful - tests whether a list is empty and not being modified
427 * @head: the list to test
428 *
429 * Description:
430 * tests whether a list is empty _and_ checks that no other CPU might be
431 * in the process of modifying either member (next or prev)
432 *
433 * NOTE: using list_empty_careful() without synchronization
434 * can only be safe if the only activity that can happen
435 * to the list entry is list_del_init(). Eg. it cannot be used
436 * if another CPU could re-list_add() it.
437 */
list_empty_careful(const struct list_head * head)438 static inline int list_empty_careful(const struct list_head *head)
439 {
440 struct list_head *next = smp_load_acquire(&head->next);
441 return list_is_head(next, head) && (next == READ_ONCE(head->prev));
442 }
443
444 /**
445 * list_rotate_left - rotate the list to the left
446 * @head: the head of the list
447 */
list_rotate_left(struct list_head * head)448 static inline void list_rotate_left(struct list_head *head)
449 {
450 struct list_head *first;
451
452 if (!list_empty(head)) {
453 first = head->next;
454 list_move_tail(first, head);
455 }
456 }
457
458 /**
459 * list_rotate_to_front() - Rotate list to specific item.
460 * @list: The desired new front of the list.
461 * @head: The head of the list.
462 *
463 * Rotates list so that @list becomes the new front of the list.
464 */
list_rotate_to_front(struct list_head * list,struct list_head * head)465 static inline void list_rotate_to_front(struct list_head *list,
466 struct list_head *head)
467 {
468 /*
469 * Deletes the list head from the list denoted by @head and
470 * places it as the tail of @list, this effectively rotates the
471 * list so that @list is at the front.
472 */
473 list_move_tail(head, list);
474 }
475
476 /**
477 * list_is_singular - tests whether a list has just one entry.
478 * @head: the list to test.
479 */
list_is_singular(const struct list_head * head)480 static inline int list_is_singular(const struct list_head *head)
481 {
482 return !list_empty(head) && (head->next == head->prev);
483 }
484
__list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)485 static inline void __list_cut_position(struct list_head *list,
486 struct list_head *head, struct list_head *entry)
487 {
488 struct list_head *new_first = entry->next;
489 list->next = head->next;
490 list->next->prev = list;
491 list->prev = entry;
492 entry->next = list;
493 head->next = new_first;
494 new_first->prev = head;
495 }
496
497 /**
498 * list_cut_position - cut a list into two
499 * @list: a new list to add all removed entries
500 * @head: a list with entries
501 * @entry: an entry within head, could be the head itself
502 * and if so we won't cut the list
503 *
504 * This helper moves the initial part of @head, up to and
505 * including @entry, from @head to @list. You should
506 * pass on @entry an element you know is on @head. @list
507 * should be an empty list or a list you do not care about
508 * losing its data.
509 *
510 */
list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)511 static inline void list_cut_position(struct list_head *list,
512 struct list_head *head, struct list_head *entry)
513 {
514 if (list_empty(head))
515 return;
516 if (list_is_singular(head) && !list_is_head(entry, head) && (entry != head->next))
517 return;
518 if (list_is_head(entry, head))
519 INIT_LIST_HEAD(list);
520 else
521 __list_cut_position(list, head, entry);
522 }
523
524 /**
525 * list_cut_before - cut a list into two, before given entry
526 * @list: a new list to add all removed entries
527 * @head: a list with entries
528 * @entry: an entry within head, could be the head itself
529 *
530 * This helper moves the initial part of @head, up to but
531 * excluding @entry, from @head to @list. You should pass
532 * in @entry an element you know is on @head. @list should
533 * be an empty list or a list you do not care about losing
534 * its data.
535 * If @entry == @head, all entries on @head are moved to
536 * @list.
537 */
list_cut_before(struct list_head * list,struct list_head * head,struct list_head * entry)538 static inline void list_cut_before(struct list_head *list,
539 struct list_head *head,
540 struct list_head *entry)
541 {
542 if (head->next == entry) {
543 INIT_LIST_HEAD(list);
544 return;
545 }
546 list->next = head->next;
547 list->next->prev = list;
548 list->prev = entry->prev;
549 list->prev->next = list;
550 head->next = entry;
551 entry->prev = head;
552 }
553
__list_splice(const struct list_head * list,struct list_head * prev,struct list_head * next)554 static inline void __list_splice(const struct list_head *list,
555 struct list_head *prev,
556 struct list_head *next)
557 {
558 struct list_head *first = list->next;
559 struct list_head *last = list->prev;
560
561 first->prev = prev;
562 prev->next = first;
563
564 last->next = next;
565 next->prev = last;
566 }
567
568 /**
569 * list_splice - join two lists, this is designed for stacks
570 * @list: the new list to add.
571 * @head: the place to add it in the first list.
572 */
list_splice(const struct list_head * list,struct list_head * head)573 static inline void list_splice(const struct list_head *list,
574 struct list_head *head)
575 {
576 if (!list_empty(list))
577 __list_splice(list, head, head->next);
578 }
579
580 /**
581 * list_splice_tail - join two lists, each list being a queue
582 * @list: the new list to add.
583 * @head: the place to add it in the first list.
584 */
list_splice_tail(struct list_head * list,struct list_head * head)585 static inline void list_splice_tail(struct list_head *list,
586 struct list_head *head)
587 {
588 if (!list_empty(list))
589 __list_splice(list, head->prev, head);
590 }
591
592 /**
593 * list_splice_init - join two lists and reinitialise the emptied list.
594 * @list: the new list to add.
595 * @head: the place to add it in the first list.
596 *
597 * The list at @list is reinitialised
598 */
list_splice_init(struct list_head * list,struct list_head * head)599 static inline void list_splice_init(struct list_head *list,
600 struct list_head *head)
601 {
602 if (!list_empty(list)) {
603 __list_splice(list, head, head->next);
604 INIT_LIST_HEAD(list);
605 }
606 }
607
608 /**
609 * list_splice_tail_init - join two lists and reinitialise the emptied list
610 * @list: the new list to add.
611 * @head: the place to add it in the first list.
612 *
613 * Each of the lists is a queue.
614 * The list at @list is reinitialised
615 */
list_splice_tail_init(struct list_head * list,struct list_head * head)616 static inline void list_splice_tail_init(struct list_head *list,
617 struct list_head *head)
618 {
619 if (!list_empty(list)) {
620 __list_splice(list, head->prev, head);
621 INIT_LIST_HEAD(list);
622 }
623 }
624
625 /**
626 * list_entry - get the struct for this entry
627 * @ptr: the &struct list_head pointer.
628 * @type: the type of the struct this is embedded in.
629 * @member: the name of the list_head within the struct.
630 */
631 #define list_entry(ptr, type, member) \
632 container_of(ptr, type, member)
633
634 /**
635 * list_first_entry - get the first element from a list
636 * @ptr: the list head to take the element from.
637 * @type: the type of the struct this is embedded in.
638 * @member: the name of the list_head within the struct.
639 *
640 * Note, that list is expected to be not empty.
641 */
642 #define list_first_entry(ptr, type, member) \
643 list_entry((ptr)->next, type, member)
644
645 /**
646 * list_last_entry - get the last element from a list
647 * @ptr: the list head to take the element from.
648 * @type: the type of the struct this is embedded in.
649 * @member: the name of the list_head within the struct.
650 *
651 * Note, that list is expected to be not empty.
652 */
653 #define list_last_entry(ptr, type, member) \
654 list_entry((ptr)->prev, type, member)
655
656 /**
657 * list_first_entry_or_null - get the first element from a list
658 * @ptr: the list head to take the element from.
659 * @type: the type of the struct this is embedded in.
660 * @member: the name of the list_head within the struct.
661 *
662 * Note that if the list is empty, it returns NULL.
663 */
664 #define list_first_entry_or_null(ptr, type, member) ({ \
665 struct list_head *head__ = (ptr); \
666 struct list_head *pos__ = READ_ONCE(head__->next); \
667 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
668 })
669
670 /**
671 * list_first_entry_or_null_acquire - get the first element from a list with barrier
672 * @ptr: the list head to take the element from.
673 * @type: the type of the struct this is embedded in.
674 * @member: the name of the list_head within the struct.
675 *
676 * Note that if the list is empty, it returns NULL.
677 */
678 #define list_first_entry_or_null_acquire(ptr, type, member) ({ \
679 struct list_head *head__ = (ptr); \
680 struct list_head *pos__ = smp_load_acquire(&head__->next); \
681 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
682 })
683
684 /**
685 * list_last_entry_or_null - get the last element from a list
686 * @ptr: the list head to take the element from.
687 * @type: the type of the struct this is embedded in.
688 * @member: the name of the list_head within the struct.
689 *
690 * Note that if the list is empty, it returns NULL.
691 */
692 #define list_last_entry_or_null(ptr, type, member) ({ \
693 struct list_head *head__ = (ptr); \
694 struct list_head *pos__ = READ_ONCE(head__->prev); \
695 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
696 })
697
698 /**
699 * list_next_entry - get the next element in list
700 * @pos: the type * to cursor
701 * @member: the name of the list_head within the struct.
702 */
703 #define list_next_entry(pos, member) \
704 list_entry((pos)->member.next, typeof(*(pos)), member)
705
706 /**
707 * list_next_entry_circular - get the next element in list
708 * @pos: the type * to cursor.
709 * @head: the list head to take the element from.
710 * @member: the name of the list_head within the struct.
711 *
712 * Wraparound if pos is the last element (return the first element).
713 * Note, that list is expected to be not empty.
714 */
715 #define list_next_entry_circular(pos, head, member) \
716 (list_is_last(&(pos)->member, head) ? \
717 list_first_entry(head, typeof(*(pos)), member) : list_next_entry(pos, member))
718
719 /**
720 * list_prev_entry - get the prev element in list
721 * @pos: the type * to cursor
722 * @member: the name of the list_head within the struct.
723 */
724 #define list_prev_entry(pos, member) \
725 list_entry((pos)->member.prev, typeof(*(pos)), member)
726
727 /**
728 * list_prev_entry_circular - get the prev element in list
729 * @pos: the type * to cursor.
730 * @head: the list head to take the element from.
731 * @member: the name of the list_head within the struct.
732 *
733 * Wraparound if pos is the first element (return the last element).
734 * Note, that list is expected to be not empty.
735 */
736 #define list_prev_entry_circular(pos, head, member) \
737 (list_is_first(&(pos)->member, head) ? \
738 list_last_entry(head, typeof(*(pos)), member) : list_prev_entry(pos, member))
739
740 /**
741 * list_for_each - iterate over a list
742 * @pos: the &struct list_head to use as a loop cursor.
743 * @head: the head for your list.
744 */
745 #define list_for_each(pos, head) \
746 for (pos = (head)->next; !list_is_head(pos, (head)); pos = pos->next)
747
748 /**
749 * list_for_each_continue - continue iteration over a list
750 * @pos: the &struct list_head to use as a loop cursor.
751 * @head: the head for your list.
752 *
753 * Continue to iterate over a list, continuing after the current position.
754 */
755 #define list_for_each_continue(pos, head) \
756 for (pos = pos->next; !list_is_head(pos, (head)); pos = pos->next)
757
758 /**
759 * list_for_each_prev - iterate over a list backwards
760 * @pos: the &struct list_head to use as a loop cursor.
761 * @head: the head for your list.
762 */
763 #define list_for_each_prev(pos, head) \
764 for (pos = (head)->prev; !list_is_head(pos, (head)); pos = pos->prev)
765
766 /**
767 * list_for_each_safe - iterate over a list safe against removal of list entry
768 * @pos: the &struct list_head to use as a loop cursor.
769 * @n: another &struct list_head to use as temporary storage
770 * @head: the head for your list.
771 */
772 #define list_for_each_safe(pos, n, head) \
773 for (pos = (head)->next, n = pos->next; \
774 !list_is_head(pos, (head)); \
775 pos = n, n = pos->next)
776
777 /**
778 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
779 * @pos: the &struct list_head to use as a loop cursor.
780 * @n: another &struct list_head to use as temporary storage
781 * @head: the head for your list.
782 */
783 #define list_for_each_prev_safe(pos, n, head) \
784 for (pos = (head)->prev, n = pos->prev; \
785 !list_is_head(pos, (head)); \
786 pos = n, n = pos->prev)
787
788 /**
789 * list_count_nodes - count nodes in the list
790 * @head: the head for your list.
791 */
list_count_nodes(struct list_head * head)792 static inline size_t list_count_nodes(struct list_head *head)
793 {
794 struct list_head *pos;
795 size_t count = 0;
796
797 list_for_each(pos, head)
798 count++;
799
800 return count;
801 }
802
803 /**
804 * list_entry_is_head - test if the entry points to the head of the list
805 * @pos: the type * to cursor
806 * @head: the head for your list.
807 * @member: the name of the list_head within the struct.
808 */
809 #define list_entry_is_head(pos, head, member) \
810 list_is_head(&pos->member, (head))
811
812 /**
813 * list_for_each_entry - iterate over list of given type
814 * @pos: the type * to use as a loop cursor.
815 * @head: the head for your list.
816 * @member: the name of the list_head within the struct.
817 */
818 #define list_for_each_entry(pos, head, member) \
819 for (pos = list_first_entry(head, typeof(*pos), member); \
820 !list_entry_is_head(pos, head, member); \
821 pos = list_next_entry(pos, member))
822
823 /**
824 * list_for_each_entry_reverse - iterate backwards over list of given type.
825 * @pos: the type * to use as a loop cursor.
826 * @head: the head for your list.
827 * @member: the name of the list_head within the struct.
828 */
829 #define list_for_each_entry_reverse(pos, head, member) \
830 for (pos = list_last_entry(head, typeof(*pos), member); \
831 !list_entry_is_head(pos, head, member); \
832 pos = list_prev_entry(pos, member))
833
834 /**
835 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
836 * @pos: the type * to use as a start point
837 * @head: the head of the list
838 * @member: the name of the list_head within the struct.
839 *
840 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
841 */
842 #define list_prepare_entry(pos, head, member) \
843 ((pos) ? : list_entry(head, typeof(*pos), member))
844
845 /**
846 * list_for_each_entry_continue - continue iteration over list of given type
847 * @pos: the type * to use as a loop cursor.
848 * @head: the head for your list.
849 * @member: the name of the list_head within the struct.
850 *
851 * Continue to iterate over list of given type, continuing after
852 * the current position.
853 */
854 #define list_for_each_entry_continue(pos, head, member) \
855 for (pos = list_next_entry(pos, member); \
856 !list_entry_is_head(pos, head, member); \
857 pos = list_next_entry(pos, member))
858
859 /**
860 * list_for_each_entry_continue_reverse - iterate backwards from the given point
861 * @pos: the type * to use as a loop cursor.
862 * @head: the head for your list.
863 * @member: the name of the list_head within the struct.
864 *
865 * Start to iterate over list of given type backwards, continuing after
866 * the current position.
867 */
868 #define list_for_each_entry_continue_reverse(pos, head, member) \
869 for (pos = list_prev_entry(pos, member); \
870 !list_entry_is_head(pos, head, member); \
871 pos = list_prev_entry(pos, member))
872
873 /**
874 * list_for_each_entry_from - iterate over list of given type from the current point
875 * @pos: the type * to use as a loop cursor.
876 * @head: the head for your list.
877 * @member: the name of the list_head within the struct.
878 *
879 * Iterate over list of given type, continuing from current position.
880 */
881 #define list_for_each_entry_from(pos, head, member) \
882 for (; !list_entry_is_head(pos, head, member); \
883 pos = list_next_entry(pos, member))
884
885 /**
886 * list_for_each_entry_from_reverse - iterate backwards over list of given type
887 * from the current point
888 * @pos: the type * to use as a loop cursor.
889 * @head: the head for your list.
890 * @member: the name of the list_head within the struct.
891 *
892 * Iterate backwards over list of given type, continuing from current position.
893 */
894 #define list_for_each_entry_from_reverse(pos, head, member) \
895 for (; !list_entry_is_head(pos, head, member); \
896 pos = list_prev_entry(pos, member))
897
898 /**
899 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
900 * @pos: the type * to use as a loop cursor.
901 * @n: another type * to use as temporary storage
902 * @head: the head for your list.
903 * @member: the name of the list_head within the struct.
904 */
905 #define list_for_each_entry_safe(pos, n, head, member) \
906 for (pos = list_first_entry(head, typeof(*pos), member), \
907 n = list_next_entry(pos, member); \
908 !list_entry_is_head(pos, head, member); \
909 pos = n, n = list_next_entry(n, member))
910
911 /**
912 * list_for_each_entry_safe_continue - continue list iteration safe against removal
913 * @pos: the type * to use as a loop cursor.
914 * @n: another type * to use as temporary storage
915 * @head: the head for your list.
916 * @member: the name of the list_head within the struct.
917 *
918 * Iterate over list of given type, continuing after current point,
919 * safe against removal of list entry.
920 */
921 #define list_for_each_entry_safe_continue(pos, n, head, member) \
922 for (pos = list_next_entry(pos, member), \
923 n = list_next_entry(pos, member); \
924 !list_entry_is_head(pos, head, member); \
925 pos = n, n = list_next_entry(n, member))
926
927 /**
928 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
929 * @pos: the type * to use as a loop cursor.
930 * @n: another type * to use as temporary storage
931 * @head: the head for your list.
932 * @member: the name of the list_head within the struct.
933 *
934 * Iterate over list of given type from current point, safe against
935 * removal of list entry.
936 */
937 #define list_for_each_entry_safe_from(pos, n, head, member) \
938 for (n = list_next_entry(pos, member); \
939 !list_entry_is_head(pos, head, member); \
940 pos = n, n = list_next_entry(n, member))
941
942 /**
943 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
944 * @pos: the type * to use as a loop cursor.
945 * @n: another type * to use as temporary storage
946 * @head: the head for your list.
947 * @member: the name of the list_head within the struct.
948 *
949 * Iterate backwards over list of given type, safe against removal
950 * of list entry.
951 */
952 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
953 for (pos = list_last_entry(head, typeof(*pos), member), \
954 n = list_prev_entry(pos, member); \
955 !list_entry_is_head(pos, head, member); \
956 pos = n, n = list_prev_entry(n, member))
957
958 /**
959 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
960 * @pos: the loop cursor used in the list_for_each_entry_safe loop
961 * @n: temporary storage used in list_for_each_entry_safe
962 * @member: the name of the list_head within the struct.
963 *
964 * list_safe_reset_next is not safe to use in general if the list may be
965 * modified concurrently (eg. the lock is dropped in the loop body). An
966 * exception to this is if the cursor element (pos) is pinned in the list,
967 * and list_safe_reset_next is called after re-taking the lock and before
968 * completing the current iteration of the loop body.
969 */
970 #define list_safe_reset_next(pos, n, member) \
971 n = list_next_entry(pos, member)
972
973 /*
974 * Double linked lists with a single pointer list head.
975 * Mostly useful for hash tables where the two pointer list head is
976 * too wasteful.
977 * You lose the ability to access the tail in O(1).
978 */
979
980 #define HLIST_HEAD_INIT { .first = NULL }
981 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
982 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
INIT_HLIST_NODE(struct hlist_node * h)983 static inline void INIT_HLIST_NODE(struct hlist_node *h)
984 {
985 h->next = NULL;
986 h->pprev = NULL;
987 }
988
989 /**
990 * hlist_unhashed - Has node been removed from list and reinitialized?
991 * @h: Node to be checked
992 *
993 * Not that not all removal functions will leave a node in unhashed
994 * state. For example, hlist_nulls_del_init_rcu() does leave the
995 * node in unhashed state, but hlist_nulls_del() does not.
996 */
hlist_unhashed(const struct hlist_node * h)997 static inline int hlist_unhashed(const struct hlist_node *h)
998 {
999 return !h->pprev;
1000 }
1001
1002 /**
1003 * hlist_unhashed_lockless - Version of hlist_unhashed for lockless use
1004 * @h: Node to be checked
1005 *
1006 * This variant of hlist_unhashed() must be used in lockless contexts
1007 * to avoid potential load-tearing. The READ_ONCE() is paired with the
1008 * various WRITE_ONCE() in hlist helpers that are defined below.
1009 */
hlist_unhashed_lockless(const struct hlist_node * h)1010 static inline int hlist_unhashed_lockless(const struct hlist_node *h)
1011 {
1012 return !READ_ONCE(h->pprev);
1013 }
1014
1015 /**
1016 * hlist_empty - Is the specified hlist_head structure an empty hlist?
1017 * @h: Structure to check.
1018 */
hlist_empty(const struct hlist_head * h)1019 static inline int hlist_empty(const struct hlist_head *h)
1020 {
1021 return !READ_ONCE(h->first);
1022 }
1023
__hlist_del(struct hlist_node * n)1024 static inline void __hlist_del(struct hlist_node *n)
1025 {
1026 struct hlist_node *next = n->next;
1027 struct hlist_node **pprev = n->pprev;
1028
1029 WRITE_ONCE(*pprev, next);
1030 if (next)
1031 WRITE_ONCE(next->pprev, pprev);
1032 }
1033
1034 /**
1035 * hlist_del - Delete the specified hlist_node from its list
1036 * @n: Node to delete.
1037 *
1038 * Note that this function leaves the node in hashed state. Use
1039 * hlist_del_init() or similar instead to unhash @n.
1040 */
hlist_del(struct hlist_node * n)1041 static inline void hlist_del(struct hlist_node *n)
1042 {
1043 __hlist_del(n);
1044 n->next = LIST_POISON1;
1045 n->pprev = LIST_POISON2;
1046 }
1047
1048 /**
1049 * hlist_del_init - Delete the specified hlist_node from its list and initialize
1050 * @n: Node to delete.
1051 *
1052 * Note that this function leaves the node in unhashed state.
1053 */
hlist_del_init(struct hlist_node * n)1054 static inline void hlist_del_init(struct hlist_node *n)
1055 {
1056 if (!hlist_unhashed(n)) {
1057 __hlist_del(n);
1058 INIT_HLIST_NODE(n);
1059 }
1060 }
1061
1062 /**
1063 * hlist_add_head - add a new entry at the beginning of the hlist
1064 * @n: new entry to be added
1065 * @h: hlist head to add it after
1066 *
1067 * Insert a new entry after the specified head.
1068 * This is good for implementing stacks.
1069 */
hlist_add_head(struct hlist_node * n,struct hlist_head * h)1070 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
1071 {
1072 struct hlist_node *first = h->first;
1073 WRITE_ONCE(n->next, first);
1074 if (first)
1075 WRITE_ONCE(first->pprev, &n->next);
1076 WRITE_ONCE(h->first, n);
1077 WRITE_ONCE(n->pprev, &h->first);
1078 }
1079
1080 /**
1081 * hlist_add_before - add a new entry before the one specified
1082 * @n: new entry to be added
1083 * @next: hlist node to add it before, which must be non-NULL
1084 */
hlist_add_before(struct hlist_node * n,struct hlist_node * next)1085 static inline void hlist_add_before(struct hlist_node *n,
1086 struct hlist_node *next)
1087 {
1088 WRITE_ONCE(n->pprev, next->pprev);
1089 WRITE_ONCE(n->next, next);
1090 WRITE_ONCE(next->pprev, &n->next);
1091 WRITE_ONCE(*(n->pprev), n);
1092 }
1093
1094 /**
1095 * hlist_add_behind - add a new entry after the one specified
1096 * @n: new entry to be added
1097 * @prev: hlist node to add it after, which must be non-NULL
1098 */
hlist_add_behind(struct hlist_node * n,struct hlist_node * prev)1099 static inline void hlist_add_behind(struct hlist_node *n,
1100 struct hlist_node *prev)
1101 {
1102 WRITE_ONCE(n->next, prev->next);
1103 WRITE_ONCE(prev->next, n);
1104 WRITE_ONCE(n->pprev, &prev->next);
1105
1106 if (n->next)
1107 WRITE_ONCE(n->next->pprev, &n->next);
1108 }
1109
1110 /**
1111 * hlist_add_fake - create a fake hlist consisting of a single headless node
1112 * @n: Node to make a fake list out of
1113 *
1114 * This makes @n appear to be its own predecessor on a headless hlist.
1115 * The point of this is to allow things like hlist_del() to work correctly
1116 * in cases where there is no list.
1117 */
hlist_add_fake(struct hlist_node * n)1118 static inline void hlist_add_fake(struct hlist_node *n)
1119 {
1120 n->pprev = &n->next;
1121 }
1122
1123 /**
1124 * hlist_fake: Is this node a fake hlist?
1125 * @h: Node to check for being a self-referential fake hlist.
1126 */
hlist_fake(struct hlist_node * h)1127 static inline bool hlist_fake(struct hlist_node *h)
1128 {
1129 return h->pprev == &h->next;
1130 }
1131
1132 /**
1133 * hlist_is_singular_node - is node the only element of the specified hlist?
1134 * @n: Node to check for singularity.
1135 * @h: Header for potentially singular list.
1136 *
1137 * Check whether the node is the only node of the head without
1138 * accessing head, thus avoiding unnecessary cache misses.
1139 */
1140 static inline bool
hlist_is_singular_node(struct hlist_node * n,struct hlist_head * h)1141 hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
1142 {
1143 return !n->next && n->pprev == &h->first;
1144 }
1145
1146 /**
1147 * hlist_move_list - Move an hlist
1148 * @old: hlist_head for old list.
1149 * @new: hlist_head for new list.
1150 *
1151 * Move a list from one list head to another. Fixup the pprev
1152 * reference of the first entry if it exists.
1153 */
hlist_move_list(struct hlist_head * old,struct hlist_head * new)1154 static inline void hlist_move_list(struct hlist_head *old,
1155 struct hlist_head *new)
1156 {
1157 new->first = old->first;
1158 if (new->first)
1159 new->first->pprev = &new->first;
1160 old->first = NULL;
1161 }
1162
1163 /**
1164 * hlist_splice_init() - move all entries from one list to another
1165 * @from: hlist_head from which entries will be moved
1166 * @last: last entry on the @from list
1167 * @to: hlist_head to which entries will be moved
1168 *
1169 * @to can be empty, @from must contain at least @last.
1170 */
hlist_splice_init(struct hlist_head * from,struct hlist_node * last,struct hlist_head * to)1171 static inline void hlist_splice_init(struct hlist_head *from,
1172 struct hlist_node *last,
1173 struct hlist_head *to)
1174 {
1175 if (to->first)
1176 to->first->pprev = &last->next;
1177 last->next = to->first;
1178 to->first = from->first;
1179 from->first->pprev = &to->first;
1180 from->first = NULL;
1181 }
1182
1183 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1184
1185 #define hlist_for_each(pos, head) \
1186 for (pos = (head)->first; pos ; pos = pos->next)
1187
1188 #define hlist_for_each_safe(pos, n, head) \
1189 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1190 pos = n)
1191
1192 #define hlist_entry_safe(ptr, type, member) \
1193 ({ typeof(ptr) ____ptr = (ptr); \
1194 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
1195 })
1196
1197 /**
1198 * hlist_for_each_entry - iterate over list of given type
1199 * @pos: the type * to use as a loop cursor.
1200 * @head: the head for your list.
1201 * @member: the name of the hlist_node within the struct.
1202 */
1203 #define hlist_for_each_entry(pos, head, member) \
1204 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
1205 pos; \
1206 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1207
1208 /**
1209 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1210 * @pos: the type * to use as a loop cursor.
1211 * @member: the name of the hlist_node within the struct.
1212 */
1213 #define hlist_for_each_entry_continue(pos, member) \
1214 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
1215 pos; \
1216 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1217
1218 /**
1219 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1220 * @pos: the type * to use as a loop cursor.
1221 * @member: the name of the hlist_node within the struct.
1222 */
1223 #define hlist_for_each_entry_from(pos, member) \
1224 for (; pos; \
1225 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1226
1227 /**
1228 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1229 * @pos: the type * to use as a loop cursor.
1230 * @n: a &struct hlist_node to use as temporary storage
1231 * @head: the head for your list.
1232 * @member: the name of the hlist_node within the struct.
1233 */
1234 #define hlist_for_each_entry_safe(pos, n, head, member) \
1235 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
1236 pos && ({ n = pos->member.next; 1; }); \
1237 pos = hlist_entry_safe(n, typeof(*pos), member))
1238
1239 /**
1240 * hlist_count_nodes - count nodes in the hlist
1241 * @head: the head for your hlist.
1242 */
hlist_count_nodes(struct hlist_head * head)1243 static inline size_t hlist_count_nodes(struct hlist_head *head)
1244 {
1245 struct hlist_node *pos;
1246 size_t count = 0;
1247
1248 hlist_for_each(pos, head)
1249 count++;
1250
1251 return count;
1252 }
1253
1254 #endif
1255