1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_LIST_H
31da177e4SLinus Torvalds #define _LINUX_LIST_H
41da177e4SLinus Torvalds
5cd7187e1SAndy Shevchenko #include <linux/container_of.h>
6de5d9bf6SChris Metcalf #include <linux/types.h>
71da177e4SLinus Torvalds #include <linux/stddef.h>
8c9cf5528SRandy Dunlap #include <linux/poison.h>
9e66eed65SLinus Torvalds #include <linux/const.h>
10cd7187e1SAndy Shevchenko
11cd7187e1SAndy Shevchenko #include <asm/barrier.h>
121da177e4SLinus Torvalds
131da177e4SLinus Torvalds /*
141eafe075SAsif Rasheed * Circular doubly linked list implementation.
151da177e4SLinus Torvalds *
161da177e4SLinus Torvalds * Some of the internal functions ("__xxx") are useful when
171da177e4SLinus Torvalds * manipulating whole lists rather than single entries, as
181da177e4SLinus Torvalds * sometimes we already know the next/prev entries and we can
191da177e4SLinus Torvalds * generate better code by using them directly rather than
201da177e4SLinus Torvalds * using the generic single-entry routines.
211da177e4SLinus Torvalds */
221da177e4SLinus Torvalds
231da177e4SLinus Torvalds #define LIST_HEAD_INIT(name) { &(name), &(name) }
241da177e4SLinus Torvalds
251da177e4SLinus Torvalds #define LIST_HEAD(name) \
261da177e4SLinus Torvalds struct list_head name = LIST_HEAD_INIT(name)
271da177e4SLinus Torvalds
2846deb744SPaul E. McKenney /**
2946deb744SPaul E. McKenney * INIT_LIST_HEAD - Initialize a list_head structure
3046deb744SPaul E. McKenney * @list: list_head structure to be initialized.
3146deb744SPaul E. McKenney *
3246deb744SPaul E. McKenney * Initializes the list_head to point to itself. If it is a list header,
3346deb744SPaul E. McKenney * the result is an empty list.
3446deb744SPaul E. McKenney */
INIT_LIST_HEAD(struct list_head * list)35490d6ab1SZach Brown static inline void INIT_LIST_HEAD(struct list_head *list)
36490d6ab1SZach Brown {
372f073848SPaul E. McKenney WRITE_ONCE(list->next, list);
38d679ae94SKuniyuki Iwashima WRITE_ONCE(list->prev, list);
39490d6ab1SZach Brown }
401da177e4SLinus Torvalds
41aebc7b0dSMarco Elver #ifdef CONFIG_LIST_HARDENED
42aebc7b0dSMarco Elver
43d7c81673SKees Cook #ifdef CONFIG_DEBUG_LIST
44aebc7b0dSMarco Elver # define __list_valid_slowpath
45aebc7b0dSMarco Elver #else
46aebc7b0dSMarco Elver # define __list_valid_slowpath __cold __preserve_most
47aebc7b0dSMarco Elver #endif
48aebc7b0dSMarco Elver
49b16c42c8SMarco Elver /*
50b16c42c8SMarco Elver * Performs the full set of list corruption checks before __list_add().
51b16c42c8SMarco Elver * On list corruption reports a warning, and returns false.
52b16c42c8SMarco Elver */
53aebc7b0dSMarco Elver extern bool __list_valid_slowpath __list_add_valid_or_report(struct list_head *new,
54d7c81673SKees Cook struct list_head *prev,
55d7c81673SKees Cook struct list_head *next);
56b16c42c8SMarco Elver
57b16c42c8SMarco Elver /*
58b16c42c8SMarco Elver * Performs list corruption checks before __list_add(). Returns false if a
59b16c42c8SMarco Elver * corruption is detected, true otherwise.
60aebc7b0dSMarco Elver *
61aebc7b0dSMarco Elver * With CONFIG_LIST_HARDENED only, performs minimal list integrity checking
62aebc7b0dSMarco Elver * inline to catch non-faulting corruptions, and only if a corruption is
63aebc7b0dSMarco Elver * detected calls the reporting function __list_add_valid_or_report().
64b16c42c8SMarco Elver */
__list_add_valid(struct list_head * new,struct list_head * prev,struct list_head * next)65b16c42c8SMarco Elver static __always_inline bool __list_add_valid(struct list_head *new,
66b16c42c8SMarco Elver struct list_head *prev,
67b16c42c8SMarco Elver struct list_head *next)
68b16c42c8SMarco Elver {
69aebc7b0dSMarco Elver bool ret = true;
70aebc7b0dSMarco Elver
71aebc7b0dSMarco Elver if (!IS_ENABLED(CONFIG_DEBUG_LIST)) {
72aebc7b0dSMarco Elver /*
73aebc7b0dSMarco Elver * With the hardening version, elide checking if next and prev
74aebc7b0dSMarco Elver * are NULL, since the immediate dereference of them below would
75aebc7b0dSMarco Elver * result in a fault if NULL.
76aebc7b0dSMarco Elver *
77aebc7b0dSMarco Elver * With the reduced set of checks, we can afford to inline the
78aebc7b0dSMarco Elver * checks, which also gives the compiler a chance to elide some
79aebc7b0dSMarco Elver * of them completely if they can be proven at compile-time. If
80aebc7b0dSMarco Elver * one of the pre-conditions does not hold, the slow-path will
81aebc7b0dSMarco Elver * show a report which pre-condition failed.
82aebc7b0dSMarco Elver */
83aebc7b0dSMarco Elver if (likely(next->prev == prev && prev->next == next && new != prev && new != next))
84aebc7b0dSMarco Elver return true;
85aebc7b0dSMarco Elver ret = false;
86aebc7b0dSMarco Elver }
87aebc7b0dSMarco Elver
88aebc7b0dSMarco Elver ret &= __list_add_valid_or_report(new, prev, next);
89aebc7b0dSMarco Elver return ret;
90b16c42c8SMarco Elver }
91b16c42c8SMarco Elver
92b16c42c8SMarco Elver /*
93b16c42c8SMarco Elver * Performs the full set of list corruption checks before __list_del_entry().
94b16c42c8SMarco Elver * On list corruption reports a warning, and returns false.
95b16c42c8SMarco Elver */
96aebc7b0dSMarco Elver extern bool __list_valid_slowpath __list_del_entry_valid_or_report(struct list_head *entry);
97b16c42c8SMarco Elver
98b16c42c8SMarco Elver /*
99b16c42c8SMarco Elver * Performs list corruption checks before __list_del_entry(). Returns false if a
100b16c42c8SMarco Elver * corruption is detected, true otherwise.
101aebc7b0dSMarco Elver *
102aebc7b0dSMarco Elver * With CONFIG_LIST_HARDENED only, performs minimal list integrity checking
103aebc7b0dSMarco Elver * inline to catch non-faulting corruptions, and only if a corruption is
104aebc7b0dSMarco Elver * detected calls the reporting function __list_del_entry_valid_or_report().
105b16c42c8SMarco Elver */
__list_del_entry_valid(struct list_head * entry)106b16c42c8SMarco Elver static __always_inline bool __list_del_entry_valid(struct list_head *entry)
107b16c42c8SMarco Elver {
108aebc7b0dSMarco Elver bool ret = true;
109aebc7b0dSMarco Elver
110aebc7b0dSMarco Elver if (!IS_ENABLED(CONFIG_DEBUG_LIST)) {
111aebc7b0dSMarco Elver struct list_head *prev = entry->prev;
112aebc7b0dSMarco Elver struct list_head *next = entry->next;
113aebc7b0dSMarco Elver
114aebc7b0dSMarco Elver /*
115aebc7b0dSMarco Elver * With the hardening version, elide checking if next and prev
116aebc7b0dSMarco Elver * are NULL, LIST_POISON1 or LIST_POISON2, since the immediate
117aebc7b0dSMarco Elver * dereference of them below would result in a fault.
118aebc7b0dSMarco Elver */
119aebc7b0dSMarco Elver if (likely(prev->next == entry && next->prev == entry))
120aebc7b0dSMarco Elver return true;
121aebc7b0dSMarco Elver ret = false;
122aebc7b0dSMarco Elver }
123aebc7b0dSMarco Elver
124aebc7b0dSMarco Elver ret &= __list_del_entry_valid_or_report(entry);
125aebc7b0dSMarco Elver return ret;
126b16c42c8SMarco Elver }
127d7c81673SKees Cook #else
__list_add_valid(struct list_head * new,struct list_head * prev,struct list_head * next)128d7c81673SKees Cook static inline bool __list_add_valid(struct list_head *new,
129d7c81673SKees Cook struct list_head *prev,
130d7c81673SKees Cook struct list_head *next)
131d7c81673SKees Cook {
132d7c81673SKees Cook return true;
133d7c81673SKees Cook }
__list_del_entry_valid(struct list_head * entry)1340cd340dcSKees Cook static inline bool __list_del_entry_valid(struct list_head *entry)
1350cd340dcSKees Cook {
1360cd340dcSKees Cook return true;
1370cd340dcSKees Cook }
138d7c81673SKees Cook #endif
139d7c81673SKees Cook
1401da177e4SLinus Torvalds /*
1411da177e4SLinus Torvalds * Insert a new entry between two known consecutive entries.
1421da177e4SLinus Torvalds *
1431da177e4SLinus Torvalds * This is only for internal list manipulation where we know
1441da177e4SLinus Torvalds * the prev/next entries already!
1451da177e4SLinus Torvalds */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)1461da177e4SLinus Torvalds static inline void __list_add(struct list_head *new,
1471da177e4SLinus Torvalds struct list_head *prev,
1481da177e4SLinus Torvalds struct list_head *next)
1491da177e4SLinus Torvalds {
150d7c81673SKees Cook if (!__list_add_valid(new, prev, next))
151d7c81673SKees Cook return;
152d7c81673SKees Cook
1531da177e4SLinus Torvalds next->prev = new;
1541da177e4SLinus Torvalds new->next = next;
1551da177e4SLinus Torvalds new->prev = prev;
1561c97be67SPaul E. McKenney WRITE_ONCE(prev->next, new);
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds
1591da177e4SLinus Torvalds /**
1601da177e4SLinus Torvalds * list_add - add a new entry
1611da177e4SLinus Torvalds * @new: new entry to be added
1621da177e4SLinus Torvalds * @head: list head to add it after
1631da177e4SLinus Torvalds *
1641da177e4SLinus Torvalds * Insert a new entry after the specified head.
1651da177e4SLinus Torvalds * This is good for implementing stacks.
1661da177e4SLinus Torvalds */
list_add(struct list_head * new,struct list_head * head)1671da177e4SLinus Torvalds static inline void list_add(struct list_head *new, struct list_head *head)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds __list_add(new, head, head->next);
1701da177e4SLinus Torvalds }
171199a9afcSDave Jones
1721da177e4SLinus Torvalds
1731da177e4SLinus Torvalds /**
1741da177e4SLinus Torvalds * list_add_tail - add a new entry
1751da177e4SLinus Torvalds * @new: new entry to be added
1761da177e4SLinus Torvalds * @head: list head to add it before
1771da177e4SLinus Torvalds *
1781da177e4SLinus Torvalds * Insert a new entry before the specified head.
1791da177e4SLinus Torvalds * This is useful for implementing queues.
1801da177e4SLinus Torvalds */
list_add_tail(struct list_head * new,struct list_head * head)1811da177e4SLinus Torvalds static inline void list_add_tail(struct list_head *new, struct list_head *head)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds __list_add(new, head->prev, head);
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds
1861da177e4SLinus Torvalds /*
1871da177e4SLinus Torvalds * Delete a list entry by making the prev/next entries
1881da177e4SLinus Torvalds * point to each other.
1891da177e4SLinus Torvalds *
1901da177e4SLinus Torvalds * This is only for internal list manipulation where we know
1911da177e4SLinus Torvalds * the prev/next entries already!
1921da177e4SLinus Torvalds */
__list_del(struct list_head * prev,struct list_head * next)1931da177e4SLinus Torvalds static inline void __list_del(struct list_head * prev, struct list_head * next)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds next->prev = prev;
1967f5f873cSPaul E. McKenney WRITE_ONCE(prev->next, next);
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds
199c8af5cd7SToke Høiland-Jørgensen /*
200c8af5cd7SToke Høiland-Jørgensen * Delete a list entry and clear the 'prev' pointer.
201c8af5cd7SToke Høiland-Jørgensen *
202c8af5cd7SToke Høiland-Jørgensen * This is a special-purpose list clearing method used in the networking code
203c8af5cd7SToke Høiland-Jørgensen * for lists allocated as per-cpu, where we don't want to incur the extra
204c8af5cd7SToke Høiland-Jørgensen * WRITE_ONCE() overhead of a regular list_del_init(). The code that uses this
205c8af5cd7SToke Høiland-Jørgensen * needs to check the node 'prev' pointer instead of calling list_empty().
206c8af5cd7SToke Høiland-Jørgensen */
__list_del_clearprev(struct list_head * entry)207c8af5cd7SToke Høiland-Jørgensen static inline void __list_del_clearprev(struct list_head *entry)
208c8af5cd7SToke Høiland-Jørgensen {
209c8af5cd7SToke Høiland-Jørgensen __list_del(entry->prev, entry->next);
210c8af5cd7SToke Høiland-Jørgensen entry->prev = NULL;
211c8af5cd7SToke Høiland-Jørgensen }
212c8af5cd7SToke Høiland-Jørgensen
__list_del_entry(struct list_head * entry)2133c18d4deSLinus Torvalds static inline void __list_del_entry(struct list_head *entry)
2143c18d4deSLinus Torvalds {
2150cd340dcSKees Cook if (!__list_del_entry_valid(entry))
2160cd340dcSKees Cook return;
2170cd340dcSKees Cook
2183c18d4deSLinus Torvalds __list_del(entry->prev, entry->next);
2193c18d4deSLinus Torvalds }
2203c18d4deSLinus Torvalds
22146deb744SPaul E. McKenney /**
22246deb744SPaul E. McKenney * list_del - deletes entry from list.
22346deb744SPaul E. McKenney * @entry: the element to delete from the list.
22446deb744SPaul E. McKenney * Note: list_empty() on entry does not return true after this, the entry is
22546deb744SPaul E. McKenney * in an undefined state.
22646deb744SPaul E. McKenney */
list_del(struct list_head * entry)2271da177e4SLinus Torvalds static inline void list_del(struct list_head *entry)
2281da177e4SLinus Torvalds {
2290cd340dcSKees Cook __list_del_entry(entry);
2301da177e4SLinus Torvalds entry->next = LIST_POISON1;
2311da177e4SLinus Torvalds entry->prev = LIST_POISON2;
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds
2341da177e4SLinus Torvalds /**
23554e73770SOleg Nesterov * list_replace - replace old entry by new one
23654e73770SOleg Nesterov * @old : the element to be replaced
23754e73770SOleg Nesterov * @new : the new element to insert
23872fd4a35SRobert P. J. Day *
23972fd4a35SRobert P. J. Day * If @old was empty, it will be overwritten.
24054e73770SOleg Nesterov */
list_replace(struct list_head * old,struct list_head * new)24154e73770SOleg Nesterov static inline void list_replace(struct list_head *old,
24254e73770SOleg Nesterov struct list_head *new)
24354e73770SOleg Nesterov {
24454e73770SOleg Nesterov new->next = old->next;
24554e73770SOleg Nesterov new->next->prev = new;
24654e73770SOleg Nesterov new->prev = old->prev;
24754e73770SOleg Nesterov new->prev->next = new;
24854e73770SOleg Nesterov }
24954e73770SOleg Nesterov
25046deb744SPaul E. McKenney /**
25146deb744SPaul E. McKenney * list_replace_init - replace old entry by new one and initialize the old one
25246deb744SPaul E. McKenney * @old : the element to be replaced
25346deb744SPaul E. McKenney * @new : the new element to insert
25446deb744SPaul E. McKenney *
25546deb744SPaul E. McKenney * If @old was empty, it will be overwritten.
25646deb744SPaul E. McKenney */
list_replace_init(struct list_head * old,struct list_head * new)25754e73770SOleg Nesterov static inline void list_replace_init(struct list_head *old,
25854e73770SOleg Nesterov struct list_head *new)
25954e73770SOleg Nesterov {
26054e73770SOleg Nesterov list_replace(old, new);
26154e73770SOleg Nesterov INIT_LIST_HEAD(old);
26254e73770SOleg Nesterov }
26354e73770SOleg Nesterov
26445f8bde0SRobert P. J. Day /**
265e900a918SDan Williams * list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position
266e900a918SDan Williams * @entry1: the location to place entry2
267e900a918SDan Williams * @entry2: the location to place entry1
268e900a918SDan Williams */
list_swap(struct list_head * entry1,struct list_head * entry2)269e900a918SDan Williams static inline void list_swap(struct list_head *entry1,
270e900a918SDan Williams struct list_head *entry2)
271e900a918SDan Williams {
272e900a918SDan Williams struct list_head *pos = entry2->prev;
273e900a918SDan Williams
274e900a918SDan Williams list_del(entry2);
275e900a918SDan Williams list_replace(entry1, entry2);
276e900a918SDan Williams if (pos == entry1)
277e900a918SDan Williams pos = entry2;
278e900a918SDan Williams list_add(entry1, pos);
279e900a918SDan Williams }
280e900a918SDan Williams
281e900a918SDan Williams /**
2821da177e4SLinus Torvalds * list_del_init - deletes entry from list and reinitialize it.
2831da177e4SLinus Torvalds * @entry: the element to delete from the list.
2841da177e4SLinus Torvalds */
list_del_init(struct list_head * entry)2851da177e4SLinus Torvalds static inline void list_del_init(struct list_head *entry)
2861da177e4SLinus Torvalds {
2873c18d4deSLinus Torvalds __list_del_entry(entry);
2881da177e4SLinus Torvalds INIT_LIST_HEAD(entry);
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds
2911da177e4SLinus Torvalds /**
2921da177e4SLinus Torvalds * list_move - delete from one list and add as another's head
2931da177e4SLinus Torvalds * @list: the entry to move
2941da177e4SLinus Torvalds * @head: the head that will precede our entry
2951da177e4SLinus Torvalds */
list_move(struct list_head * list,struct list_head * head)2961da177e4SLinus Torvalds static inline void list_move(struct list_head *list, struct list_head *head)
2971da177e4SLinus Torvalds {
2983c18d4deSLinus Torvalds __list_del_entry(list);
2991da177e4SLinus Torvalds list_add(list, head);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds
3021da177e4SLinus Torvalds /**
3031da177e4SLinus Torvalds * list_move_tail - delete from one list and add as another's tail
3041da177e4SLinus Torvalds * @list: the entry to move
3051da177e4SLinus Torvalds * @head: the head that will follow our entry
3061da177e4SLinus Torvalds */
list_move_tail(struct list_head * list,struct list_head * head)3071da177e4SLinus Torvalds static inline void list_move_tail(struct list_head *list,
3081da177e4SLinus Torvalds struct list_head *head)
3091da177e4SLinus Torvalds {
3103c18d4deSLinus Torvalds __list_del_entry(list);
3111da177e4SLinus Torvalds list_add_tail(list, head);
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds
3141da177e4SLinus Torvalds /**
315df2fc43dSChristian König * list_bulk_move_tail - move a subsection of a list to its tail
316df2fc43dSChristian König * @head: the head that will follow our entry
317df2fc43dSChristian König * @first: first entry to move
318df2fc43dSChristian König * @last: last entry to move, can be the same as first
319df2fc43dSChristian König *
320df2fc43dSChristian König * Move all entries between @first and including @last before @head.
321df2fc43dSChristian König * All three entries must belong to the same linked list.
322df2fc43dSChristian König */
list_bulk_move_tail(struct list_head * head,struct list_head * first,struct list_head * last)323df2fc43dSChristian König static inline void list_bulk_move_tail(struct list_head *head,
324df2fc43dSChristian König struct list_head *first,
325df2fc43dSChristian König struct list_head *last)
326df2fc43dSChristian König {
327df2fc43dSChristian König first->prev->next = last->next;
328df2fc43dSChristian König last->next->prev = first->prev;
329df2fc43dSChristian König
330df2fc43dSChristian König head->prev->next = first;
331df2fc43dSChristian König first->prev = head->prev;
332df2fc43dSChristian König
333df2fc43dSChristian König last->next = head;
334df2fc43dSChristian König head->prev = last;
335df2fc43dSChristian König }
336df2fc43dSChristian König
337df2fc43dSChristian König /**
33870b44595SMel Gorman * list_is_first -- tests whether @list is the first entry in list @head
33970b44595SMel Gorman * @list: the entry to test
34070b44595SMel Gorman * @head: the head of the list
34170b44595SMel Gorman */
list_is_first(const struct list_head * list,const struct list_head * head)34204254730SAndy Shevchenko static inline int list_is_first(const struct list_head *list, const struct list_head *head)
34370b44595SMel Gorman {
34470b44595SMel Gorman return list->prev == head;
34570b44595SMel Gorman }
34670b44595SMel Gorman
34770b44595SMel Gorman /**
348e8f4d97eSShailabh Nagar * list_is_last - tests whether @list is the last entry in list @head
349e8f4d97eSShailabh Nagar * @list: the entry to test
350e8f4d97eSShailabh Nagar * @head: the head of the list
351e8f4d97eSShailabh Nagar */
list_is_last(const struct list_head * list,const struct list_head * head)35204254730SAndy Shevchenko static inline int list_is_last(const struct list_head *list, const struct list_head *head)
353e8f4d97eSShailabh Nagar {
354e8f4d97eSShailabh Nagar return list->next == head;
355e8f4d97eSShailabh Nagar }
356e8f4d97eSShailabh Nagar
357e8f4d97eSShailabh Nagar /**
35804254730SAndy Shevchenko * list_is_head - tests whether @list is the list @head
35904254730SAndy Shevchenko * @list: the entry to test
36004254730SAndy Shevchenko * @head: the head of the list
36104254730SAndy Shevchenko */
list_is_head(const struct list_head * list,const struct list_head * head)36204254730SAndy Shevchenko static inline int list_is_head(const struct list_head *list, const struct list_head *head)
36304254730SAndy Shevchenko {
36404254730SAndy Shevchenko return list == head;
36504254730SAndy Shevchenko }
36604254730SAndy Shevchenko
36704254730SAndy Shevchenko /**
3681da177e4SLinus Torvalds * list_empty - tests whether a list is empty
3691da177e4SLinus Torvalds * @head: the list to test.
3701da177e4SLinus Torvalds */
list_empty(const struct list_head * head)3711da177e4SLinus Torvalds static inline int list_empty(const struct list_head *head)
3721da177e4SLinus Torvalds {
3731658d35eSPaul E. McKenney return READ_ONCE(head->next) == head;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds
3761da177e4SLinus Torvalds /**
377c6fe44d9SLinus Torvalds * list_del_init_careful - deletes entry from list and reinitialize it.
378c6fe44d9SLinus Torvalds * @entry: the element to delete from the list.
379c6fe44d9SLinus Torvalds *
380c6fe44d9SLinus Torvalds * This is the same as list_del_init(), except designed to be used
381c6fe44d9SLinus Torvalds * together with list_empty_careful() in a way to guarantee ordering
382c6fe44d9SLinus Torvalds * of other memory operations.
383c6fe44d9SLinus Torvalds *
384c6fe44d9SLinus Torvalds * Any memory operations done before a list_del_init_careful() are
385c6fe44d9SLinus Torvalds * guaranteed to be visible after a list_empty_careful() test.
386c6fe44d9SLinus Torvalds */
list_del_init_careful(struct list_head * entry)387c6fe44d9SLinus Torvalds static inline void list_del_init_careful(struct list_head *entry)
388c6fe44d9SLinus Torvalds {
389c6fe44d9SLinus Torvalds __list_del_entry(entry);
390d679ae94SKuniyuki Iwashima WRITE_ONCE(entry->prev, entry);
391c6fe44d9SLinus Torvalds smp_store_release(&entry->next, entry);
392c6fe44d9SLinus Torvalds }
393c6fe44d9SLinus Torvalds
394c6fe44d9SLinus Torvalds /**
395fe96e57dSRandy Dunlap * list_empty_careful - tests whether a list is empty and not being modified
396fe96e57dSRandy Dunlap * @head: the list to test
397fe96e57dSRandy Dunlap *
398fe96e57dSRandy Dunlap * Description:
399fe96e57dSRandy Dunlap * tests whether a list is empty _and_ checks that no other CPU might be
400fe96e57dSRandy Dunlap * in the process of modifying either member (next or prev)
4011da177e4SLinus Torvalds *
4021da177e4SLinus Torvalds * NOTE: using list_empty_careful() without synchronization
4031da177e4SLinus Torvalds * can only be safe if the only activity that can happen
4041da177e4SLinus Torvalds * to the list entry is list_del_init(). Eg. it cannot be used
4051da177e4SLinus Torvalds * if another CPU could re-list_add() it.
4061da177e4SLinus Torvalds */
list_empty_careful(const struct list_head * head)4071da177e4SLinus Torvalds static inline int list_empty_careful(const struct list_head *head)
4081da177e4SLinus Torvalds {
409c6fe44d9SLinus Torvalds struct list_head *next = smp_load_acquire(&head->next);
410d679ae94SKuniyuki Iwashima return list_is_head(next, head) && (next == READ_ONCE(head->prev));
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds
41399602572SMasami Hiramatsu /**
4145908cdc8SFrederic Weisbecker * list_rotate_left - rotate the list to the left
4155908cdc8SFrederic Weisbecker * @head: the head of the list
4165908cdc8SFrederic Weisbecker */
list_rotate_left(struct list_head * head)4175908cdc8SFrederic Weisbecker static inline void list_rotate_left(struct list_head *head)
4185908cdc8SFrederic Weisbecker {
4195908cdc8SFrederic Weisbecker struct list_head *first;
4205908cdc8SFrederic Weisbecker
4215908cdc8SFrederic Weisbecker if (!list_empty(head)) {
4225908cdc8SFrederic Weisbecker first = head->next;
4235908cdc8SFrederic Weisbecker list_move_tail(first, head);
4245908cdc8SFrederic Weisbecker }
4255908cdc8SFrederic Weisbecker }
4265908cdc8SFrederic Weisbecker
4275908cdc8SFrederic Weisbecker /**
428a16b5384STobin C. Harding * list_rotate_to_front() - Rotate list to specific item.
429a16b5384STobin C. Harding * @list: The desired new front of the list.
430a16b5384STobin C. Harding * @head: The head of the list.
431a16b5384STobin C. Harding *
432a16b5384STobin C. Harding * Rotates list so that @list becomes the new front of the list.
433a16b5384STobin C. Harding */
list_rotate_to_front(struct list_head * list,struct list_head * head)434a16b5384STobin C. Harding static inline void list_rotate_to_front(struct list_head *list,
435a16b5384STobin C. Harding struct list_head *head)
436a16b5384STobin C. Harding {
437a16b5384STobin C. Harding /*
438a16b5384STobin C. Harding * Deletes the list head from the list denoted by @head and
439a16b5384STobin C. Harding * places it as the tail of @list, this effectively rotates the
440a16b5384STobin C. Harding * list so that @list is at the front.
441a16b5384STobin C. Harding */
442a16b5384STobin C. Harding list_move_tail(head, list);
443a16b5384STobin C. Harding }
444a16b5384STobin C. Harding
445a16b5384STobin C. Harding /**
44699602572SMasami Hiramatsu * list_is_singular - tests whether a list has just one entry.
44799602572SMasami Hiramatsu * @head: the list to test.
44899602572SMasami Hiramatsu */
list_is_singular(const struct list_head * head)44999602572SMasami Hiramatsu static inline int list_is_singular(const struct list_head *head)
45099602572SMasami Hiramatsu {
45199602572SMasami Hiramatsu return !list_empty(head) && (head->next == head->prev);
45299602572SMasami Hiramatsu }
45399602572SMasami Hiramatsu
__list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)45400e8a4daSLuis R. Rodriguez static inline void __list_cut_position(struct list_head *list,
45500e8a4daSLuis R. Rodriguez struct list_head *head, struct list_head *entry)
45600e8a4daSLuis R. Rodriguez {
45700e8a4daSLuis R. Rodriguez struct list_head *new_first = entry->next;
45800e8a4daSLuis R. Rodriguez list->next = head->next;
45900e8a4daSLuis R. Rodriguez list->next->prev = list;
46000e8a4daSLuis R. Rodriguez list->prev = entry;
46100e8a4daSLuis R. Rodriguez entry->next = list;
46200e8a4daSLuis R. Rodriguez head->next = new_first;
46300e8a4daSLuis R. Rodriguez new_first->prev = head;
46400e8a4daSLuis R. Rodriguez }
46500e8a4daSLuis R. Rodriguez
46600e8a4daSLuis R. Rodriguez /**
46700e8a4daSLuis R. Rodriguez * list_cut_position - cut a list into two
46800e8a4daSLuis R. Rodriguez * @list: a new list to add all removed entries
46900e8a4daSLuis R. Rodriguez * @head: a list with entries
47000e8a4daSLuis R. Rodriguez * @entry: an entry within head, could be the head itself
47100e8a4daSLuis R. Rodriguez * and if so we won't cut the list
47200e8a4daSLuis R. Rodriguez *
47300e8a4daSLuis R. Rodriguez * This helper moves the initial part of @head, up to and
47400e8a4daSLuis R. Rodriguez * including @entry, from @head to @list. You should
47500e8a4daSLuis R. Rodriguez * pass on @entry an element you know is on @head. @list
47600e8a4daSLuis R. Rodriguez * should be an empty list or a list you do not care about
47700e8a4daSLuis R. Rodriguez * losing its data.
47800e8a4daSLuis R. Rodriguez *
47900e8a4daSLuis R. Rodriguez */
list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)48000e8a4daSLuis R. Rodriguez static inline void list_cut_position(struct list_head *list,
48100e8a4daSLuis R. Rodriguez struct list_head *head, struct list_head *entry)
48200e8a4daSLuis R. Rodriguez {
48300e8a4daSLuis R. Rodriguez if (list_empty(head))
48400e8a4daSLuis R. Rodriguez return;
48504254730SAndy Shevchenko if (list_is_singular(head) && !list_is_head(entry, head) && (entry != head->next))
48600e8a4daSLuis R. Rodriguez return;
48704254730SAndy Shevchenko if (list_is_head(entry, head))
48800e8a4daSLuis R. Rodriguez INIT_LIST_HEAD(list);
48900e8a4daSLuis R. Rodriguez else
49000e8a4daSLuis R. Rodriguez __list_cut_position(list, head, entry);
49100e8a4daSLuis R. Rodriguez }
49200e8a4daSLuis R. Rodriguez
4934ce0017aSEdward Cree /**
4944ce0017aSEdward Cree * list_cut_before - cut a list into two, before given entry
4954ce0017aSEdward Cree * @list: a new list to add all removed entries
4964ce0017aSEdward Cree * @head: a list with entries
4974ce0017aSEdward Cree * @entry: an entry within head, could be the head itself
4984ce0017aSEdward Cree *
4994ce0017aSEdward Cree * This helper moves the initial part of @head, up to but
5004ce0017aSEdward Cree * excluding @entry, from @head to @list. You should pass
5014ce0017aSEdward Cree * in @entry an element you know is on @head. @list should
5024ce0017aSEdward Cree * be an empty list or a list you do not care about losing
5034ce0017aSEdward Cree * its data.
5044ce0017aSEdward Cree * If @entry == @head, all entries on @head are moved to
5054ce0017aSEdward Cree * @list.
5064ce0017aSEdward Cree */
list_cut_before(struct list_head * list,struct list_head * head,struct list_head * entry)5074ce0017aSEdward Cree static inline void list_cut_before(struct list_head *list,
5084ce0017aSEdward Cree struct list_head *head,
5094ce0017aSEdward Cree struct list_head *entry)
5104ce0017aSEdward Cree {
5114ce0017aSEdward Cree if (head->next == entry) {
5124ce0017aSEdward Cree INIT_LIST_HEAD(list);
5134ce0017aSEdward Cree return;
5144ce0017aSEdward Cree }
5154ce0017aSEdward Cree list->next = head->next;
5164ce0017aSEdward Cree list->next->prev = list;
5174ce0017aSEdward Cree list->prev = entry->prev;
5184ce0017aSEdward Cree list->prev->next = list;
5194ce0017aSEdward Cree head->next = entry;
5204ce0017aSEdward Cree entry->prev = head;
5214ce0017aSEdward Cree }
5224ce0017aSEdward Cree
__list_splice(const struct list_head * list,struct list_head * prev,struct list_head * next)52395d8c365SRobert P. J. Day static inline void __list_splice(const struct list_head *list,
5247d283aeeSLuis R. Rodriguez struct list_head *prev,
5257d283aeeSLuis R. Rodriguez struct list_head *next)
5261da177e4SLinus Torvalds {
5271da177e4SLinus Torvalds struct list_head *first = list->next;
5281da177e4SLinus Torvalds struct list_head *last = list->prev;
5291da177e4SLinus Torvalds
5307d283aeeSLuis R. Rodriguez first->prev = prev;
5317d283aeeSLuis R. Rodriguez prev->next = first;
5321da177e4SLinus Torvalds
5337d283aeeSLuis R. Rodriguez last->next = next;
5347d283aeeSLuis R. Rodriguez next->prev = last;
5351da177e4SLinus Torvalds }
5361da177e4SLinus Torvalds
5371da177e4SLinus Torvalds /**
5387d283aeeSLuis R. Rodriguez * list_splice - join two lists, this is designed for stacks
5391da177e4SLinus Torvalds * @list: the new list to add.
5401da177e4SLinus Torvalds * @head: the place to add it in the first list.
5411da177e4SLinus Torvalds */
list_splice(const struct list_head * list,struct list_head * head)54295d8c365SRobert P. J. Day static inline void list_splice(const struct list_head *list,
54395d8c365SRobert P. J. Day struct list_head *head)
5441da177e4SLinus Torvalds {
5451da177e4SLinus Torvalds if (!list_empty(list))
5467d283aeeSLuis R. Rodriguez __list_splice(list, head, head->next);
5477d283aeeSLuis R. Rodriguez }
5487d283aeeSLuis R. Rodriguez
5497d283aeeSLuis R. Rodriguez /**
5507d283aeeSLuis R. Rodriguez * list_splice_tail - join two lists, each list being a queue
5517d283aeeSLuis R. Rodriguez * @list: the new list to add.
5527d283aeeSLuis R. Rodriguez * @head: the place to add it in the first list.
5537d283aeeSLuis R. Rodriguez */
list_splice_tail(struct list_head * list,struct list_head * head)5547d283aeeSLuis R. Rodriguez static inline void list_splice_tail(struct list_head *list,
5557d283aeeSLuis R. Rodriguez struct list_head *head)
5567d283aeeSLuis R. Rodriguez {
5577d283aeeSLuis R. Rodriguez if (!list_empty(list))
5587d283aeeSLuis R. Rodriguez __list_splice(list, head->prev, head);
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds
5611da177e4SLinus Torvalds /**
5621da177e4SLinus Torvalds * list_splice_init - join two lists and reinitialise the emptied list.
5631da177e4SLinus Torvalds * @list: the new list to add.
5641da177e4SLinus Torvalds * @head: the place to add it in the first list.
5651da177e4SLinus Torvalds *
5661da177e4SLinus Torvalds * The list at @list is reinitialised
5671da177e4SLinus Torvalds */
list_splice_init(struct list_head * list,struct list_head * head)5681da177e4SLinus Torvalds static inline void list_splice_init(struct list_head *list,
5691da177e4SLinus Torvalds struct list_head *head)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds if (!list_empty(list)) {
5727d283aeeSLuis R. Rodriguez __list_splice(list, head, head->next);
5737d283aeeSLuis R. Rodriguez INIT_LIST_HEAD(list);
5747d283aeeSLuis R. Rodriguez }
5757d283aeeSLuis R. Rodriguez }
5767d283aeeSLuis R. Rodriguez
5777d283aeeSLuis R. Rodriguez /**
5786724cce8SRandy Dunlap * list_splice_tail_init - join two lists and reinitialise the emptied list
5797d283aeeSLuis R. Rodriguez * @list: the new list to add.
5807d283aeeSLuis R. Rodriguez * @head: the place to add it in the first list.
5817d283aeeSLuis R. Rodriguez *
5826724cce8SRandy Dunlap * Each of the lists is a queue.
5837d283aeeSLuis R. Rodriguez * The list at @list is reinitialised
5847d283aeeSLuis R. Rodriguez */
list_splice_tail_init(struct list_head * list,struct list_head * head)5857d283aeeSLuis R. Rodriguez static inline void list_splice_tail_init(struct list_head *list,
5867d283aeeSLuis R. Rodriguez struct list_head *head)
5877d283aeeSLuis R. Rodriguez {
5887d283aeeSLuis R. Rodriguez if (!list_empty(list)) {
5897d283aeeSLuis R. Rodriguez __list_splice(list, head->prev, head);
5901da177e4SLinus Torvalds INIT_LIST_HEAD(list);
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds
5941da177e4SLinus Torvalds /**
5951da177e4SLinus Torvalds * list_entry - get the struct for this entry
5961da177e4SLinus Torvalds * @ptr: the &struct list_head pointer.
5971da177e4SLinus Torvalds * @type: the type of the struct this is embedded in.
5983943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
5991da177e4SLinus Torvalds */
6001da177e4SLinus Torvalds #define list_entry(ptr, type, member) \
6011da177e4SLinus Torvalds container_of(ptr, type, member)
6021da177e4SLinus Torvalds
6031da177e4SLinus Torvalds /**
604b5e61818SPavel Emelianov * list_first_entry - get the first element from a list
605b5e61818SPavel Emelianov * @ptr: the list head to take the element from.
606b5e61818SPavel Emelianov * @type: the type of the struct this is embedded in.
6073943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
608b5e61818SPavel Emelianov *
609b5e61818SPavel Emelianov * Note, that list is expected to be not empty.
610b5e61818SPavel Emelianov */
611b5e61818SPavel Emelianov #define list_first_entry(ptr, type, member) \
612b5e61818SPavel Emelianov list_entry((ptr)->next, type, member)
613b5e61818SPavel Emelianov
614b5e61818SPavel Emelianov /**
61593be3c2eSOleg Nesterov * list_last_entry - get the last element from a list
61693be3c2eSOleg Nesterov * @ptr: the list head to take the element from.
61793be3c2eSOleg Nesterov * @type: the type of the struct this is embedded in.
6183943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
61993be3c2eSOleg Nesterov *
62093be3c2eSOleg Nesterov * Note, that list is expected to be not empty.
62193be3c2eSOleg Nesterov */
62293be3c2eSOleg Nesterov #define list_last_entry(ptr, type, member) \
62393be3c2eSOleg Nesterov list_entry((ptr)->prev, type, member)
62493be3c2eSOleg Nesterov
62593be3c2eSOleg Nesterov /**
6266d7581e6SJiri Pirko * list_first_entry_or_null - get the first element from a list
6276d7581e6SJiri Pirko * @ptr: the list head to take the element from.
6286d7581e6SJiri Pirko * @type: the type of the struct this is embedded in.
6293943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
6306d7581e6SJiri Pirko *
6316d7581e6SJiri Pirko * Note that if the list is empty, it returns NULL.
6326d7581e6SJiri Pirko */
63312adfd88SChris Wilson #define list_first_entry_or_null(ptr, type, member) ({ \
63412adfd88SChris Wilson struct list_head *head__ = (ptr); \
63512adfd88SChris Wilson struct list_head *pos__ = READ_ONCE(head__->next); \
63612adfd88SChris Wilson pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
63712adfd88SChris Wilson })
6386d7581e6SJiri Pirko
6396d7581e6SJiri Pirko /**
640008208c6SOleg Nesterov * list_next_entry - get the next element in list
641008208c6SOleg Nesterov * @pos: the type * to cursor
6423943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
643008208c6SOleg Nesterov */
644008208c6SOleg Nesterov #define list_next_entry(pos, member) \
645008208c6SOleg Nesterov list_entry((pos)->member.next, typeof(*(pos)), member)
646008208c6SOleg Nesterov
647008208c6SOleg Nesterov /**
6482fbdf45dSRicardo Martinez * list_next_entry_circular - get the next element in list
6492fbdf45dSRicardo Martinez * @pos: the type * to cursor.
6502fbdf45dSRicardo Martinez * @head: the list head to take the element from.
6512fbdf45dSRicardo Martinez * @member: the name of the list_head within the struct.
6522fbdf45dSRicardo Martinez *
6532fbdf45dSRicardo Martinez * Wraparound if pos is the last element (return the first element).
6542fbdf45dSRicardo Martinez * Note, that list is expected to be not empty.
6552fbdf45dSRicardo Martinez */
6562fbdf45dSRicardo Martinez #define list_next_entry_circular(pos, head, member) \
6572fbdf45dSRicardo Martinez (list_is_last(&(pos)->member, head) ? \
6582fbdf45dSRicardo Martinez list_first_entry(head, typeof(*(pos)), member) : list_next_entry(pos, member))
6592fbdf45dSRicardo Martinez
6602fbdf45dSRicardo Martinez /**
661008208c6SOleg Nesterov * list_prev_entry - get the prev element in list
662008208c6SOleg Nesterov * @pos: the type * to cursor
6633943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
664008208c6SOleg Nesterov */
665008208c6SOleg Nesterov #define list_prev_entry(pos, member) \
666008208c6SOleg Nesterov list_entry((pos)->member.prev, typeof(*(pos)), member)
667008208c6SOleg Nesterov
668008208c6SOleg Nesterov /**
6692fbdf45dSRicardo Martinez * list_prev_entry_circular - get the prev element in list
6702fbdf45dSRicardo Martinez * @pos: the type * to cursor.
6712fbdf45dSRicardo Martinez * @head: the list head to take the element from.
6722fbdf45dSRicardo Martinez * @member: the name of the list_head within the struct.
6732fbdf45dSRicardo Martinez *
6742fbdf45dSRicardo Martinez * Wraparound if pos is the first element (return the last element).
6752fbdf45dSRicardo Martinez * Note, that list is expected to be not empty.
6762fbdf45dSRicardo Martinez */
6772fbdf45dSRicardo Martinez #define list_prev_entry_circular(pos, head, member) \
6782fbdf45dSRicardo Martinez (list_is_first(&(pos)->member, head) ? \
6792fbdf45dSRicardo Martinez list_last_entry(head, typeof(*(pos)), member) : list_prev_entry(pos, member))
6802fbdf45dSRicardo Martinez
6812fbdf45dSRicardo Martinez /**
6821da177e4SLinus Torvalds * list_for_each - iterate over a list
6838e3a67a9SRandy Dunlap * @pos: the &struct list_head to use as a loop cursor.
6841da177e4SLinus Torvalds * @head: the head for your list.
6851da177e4SLinus Torvalds */
6861da177e4SLinus Torvalds #define list_for_each(pos, head) \
68704254730SAndy Shevchenko for (pos = (head)->next; !list_is_head(pos, (head)); pos = pos->next)
6881da177e4SLinus Torvalds
6891da177e4SLinus Torvalds /**
690ad25f5cbSDavid Howells * list_for_each_rcu - Iterate over a list in an RCU-safe fashion
691ad25f5cbSDavid Howells * @pos: the &struct list_head to use as a loop cursor.
692ad25f5cbSDavid Howells * @head: the head for your list.
693ad25f5cbSDavid Howells */
694ad25f5cbSDavid Howells #define list_for_each_rcu(pos, head) \
695ad25f5cbSDavid Howells for (pos = rcu_dereference((head)->next); \
696ad25f5cbSDavid Howells !list_is_head(pos, (head)); \
697ad25f5cbSDavid Howells pos = rcu_dereference(pos->next))
698ad25f5cbSDavid Howells
699ad25f5cbSDavid Howells /**
70028ca0d6dSPavel Begunkov * list_for_each_continue - continue iteration over a list
70128ca0d6dSPavel Begunkov * @pos: the &struct list_head to use as a loop cursor.
70228ca0d6dSPavel Begunkov * @head: the head for your list.
70328ca0d6dSPavel Begunkov *
70428ca0d6dSPavel Begunkov * Continue to iterate over a list, continuing after the current position.
70528ca0d6dSPavel Begunkov */
70628ca0d6dSPavel Begunkov #define list_for_each_continue(pos, head) \
70704254730SAndy Shevchenko for (pos = pos->next; !list_is_head(pos, (head)); pos = pos->next)
70828ca0d6dSPavel Begunkov
70928ca0d6dSPavel Begunkov /**
7101da177e4SLinus Torvalds * list_for_each_prev - iterate over a list backwards
7118e3a67a9SRandy Dunlap * @pos: the &struct list_head to use as a loop cursor.
7121da177e4SLinus Torvalds * @head: the head for your list.
7131da177e4SLinus Torvalds */
7141da177e4SLinus Torvalds #define list_for_each_prev(pos, head) \
71504254730SAndy Shevchenko for (pos = (head)->prev; !list_is_head(pos, (head)); pos = pos->prev)
7161da177e4SLinus Torvalds
7171da177e4SLinus Torvalds /**
7181da177e4SLinus Torvalds * list_for_each_safe - iterate over a list safe against removal of list entry
7198e3a67a9SRandy Dunlap * @pos: the &struct list_head to use as a loop cursor.
7201da177e4SLinus Torvalds * @n: another &struct list_head to use as temporary storage
7211da177e4SLinus Torvalds * @head: the head for your list.
7221da177e4SLinus Torvalds */
7231da177e4SLinus Torvalds #define list_for_each_safe(pos, n, head) \
72404254730SAndy Shevchenko for (pos = (head)->next, n = pos->next; \
72504254730SAndy Shevchenko !list_is_head(pos, (head)); \
7261da177e4SLinus Torvalds pos = n, n = pos->next)
7271da177e4SLinus Torvalds
7281da177e4SLinus Torvalds /**
7298f731f7dSRandy Dunlap * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
73037c42524SDenis V. Lunev * @pos: the &struct list_head to use as a loop cursor.
73137c42524SDenis V. Lunev * @n: another &struct list_head to use as temporary storage
73237c42524SDenis V. Lunev * @head: the head for your list.
73337c42524SDenis V. Lunev */
73437c42524SDenis V. Lunev #define list_for_each_prev_safe(pos, n, head) \
73537c42524SDenis V. Lunev for (pos = (head)->prev, n = pos->prev; \
73604254730SAndy Shevchenko !list_is_head(pos, (head)); \
73737c42524SDenis V. Lunev pos = n, n = pos->prev)
73837c42524SDenis V. Lunev
73937c42524SDenis V. Lunev /**
7404d70c746SAndy Shevchenko * list_count_nodes - count nodes in the list
7414d70c746SAndy Shevchenko * @head: the head for your list.
7424d70c746SAndy Shevchenko */
list_count_nodes(struct list_head * head)7434d70c746SAndy Shevchenko static inline size_t list_count_nodes(struct list_head *head)
7444d70c746SAndy Shevchenko {
7454d70c746SAndy Shevchenko struct list_head *pos;
7464d70c746SAndy Shevchenko size_t count = 0;
7474d70c746SAndy Shevchenko
7484d70c746SAndy Shevchenko list_for_each(pos, head)
7494d70c746SAndy Shevchenko count++;
7504d70c746SAndy Shevchenko
7514d70c746SAndy Shevchenko return count;
7524d70c746SAndy Shevchenko }
7534d70c746SAndy Shevchenko
7544d70c746SAndy Shevchenko /**
755e1308161SAndy Shevchenko * list_entry_is_head - test if the entry points to the head of the list
756e1308161SAndy Shevchenko * @pos: the type * to cursor
757e1308161SAndy Shevchenko * @head: the head for your list.
758e1308161SAndy Shevchenko * @member: the name of the list_head within the struct.
759e1308161SAndy Shevchenko */
760e1308161SAndy Shevchenko #define list_entry_is_head(pos, head, member) \
761*2932fb0aSWei Yang list_is_head(&pos->member, (head))
762e1308161SAndy Shevchenko
763e1308161SAndy Shevchenko /**
7641da177e4SLinus Torvalds * list_for_each_entry - iterate over list of given type
7658e3a67a9SRandy Dunlap * @pos: the type * to use as a loop cursor.
7661da177e4SLinus Torvalds * @head: the head for your list.
7673943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
7681da177e4SLinus Torvalds */
7691da177e4SLinus Torvalds #define list_for_each_entry(pos, head, member) \
77093be3c2eSOleg Nesterov for (pos = list_first_entry(head, typeof(*pos), member); \
771e1308161SAndy Shevchenko !list_entry_is_head(pos, head, member); \
7728120e2e5SOleg Nesterov pos = list_next_entry(pos, member))
7731da177e4SLinus Torvalds
7741da177e4SLinus Torvalds /**
7751da177e4SLinus Torvalds * list_for_each_entry_reverse - iterate backwards over list of given type.
7768e3a67a9SRandy Dunlap * @pos: the type * to use as a loop cursor.
7771da177e4SLinus Torvalds * @head: the head for your list.
7783943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
7791da177e4SLinus Torvalds */
7801da177e4SLinus Torvalds #define list_for_each_entry_reverse(pos, head, member) \
78193be3c2eSOleg Nesterov for (pos = list_last_entry(head, typeof(*pos), member); \
782e1308161SAndy Shevchenko !list_entry_is_head(pos, head, member); \
7838120e2e5SOleg Nesterov pos = list_prev_entry(pos, member))
7841da177e4SLinus Torvalds
7851da177e4SLinus Torvalds /**
78672fd4a35SRobert P. J. Day * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
7871da177e4SLinus Torvalds * @pos: the type * to use as a start point
7881da177e4SLinus Torvalds * @head: the head of the list
7893943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
790fe96e57dSRandy Dunlap *
79172fd4a35SRobert P. J. Day * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
7921da177e4SLinus Torvalds */
7931da177e4SLinus Torvalds #define list_prepare_entry(pos, head, member) \
7941da177e4SLinus Torvalds ((pos) ? : list_entry(head, typeof(*pos), member))
7951da177e4SLinus Torvalds
7961da177e4SLinus Torvalds /**
797fe96e57dSRandy Dunlap * list_for_each_entry_continue - continue iteration over list of given type
7988e3a67a9SRandy Dunlap * @pos: the type * to use as a loop cursor.
7991da177e4SLinus Torvalds * @head: the head for your list.
8003943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
801fe96e57dSRandy Dunlap *
802fe96e57dSRandy Dunlap * Continue to iterate over list of given type, continuing after
803fe96e57dSRandy Dunlap * the current position.
8041da177e4SLinus Torvalds */
8051da177e4SLinus Torvalds #define list_for_each_entry_continue(pos, head, member) \
8068120e2e5SOleg Nesterov for (pos = list_next_entry(pos, member); \
807e1308161SAndy Shevchenko !list_entry_is_head(pos, head, member); \
8088120e2e5SOleg Nesterov pos = list_next_entry(pos, member))
8091da177e4SLinus Torvalds
8101da177e4SLinus Torvalds /**
811768f3591SPavel Emelyanov * list_for_each_entry_continue_reverse - iterate backwards from the given point
812768f3591SPavel Emelyanov * @pos: the type * to use as a loop cursor.
813768f3591SPavel Emelyanov * @head: the head for your list.
8143943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
815768f3591SPavel Emelyanov *
816768f3591SPavel Emelyanov * Start to iterate over list of given type backwards, continuing after
817768f3591SPavel Emelyanov * the current position.
818768f3591SPavel Emelyanov */
819768f3591SPavel Emelyanov #define list_for_each_entry_continue_reverse(pos, head, member) \
8208120e2e5SOleg Nesterov for (pos = list_prev_entry(pos, member); \
821e1308161SAndy Shevchenko !list_entry_is_head(pos, head, member); \
8228120e2e5SOleg Nesterov pos = list_prev_entry(pos, member))
823768f3591SPavel Emelyanov
824768f3591SPavel Emelyanov /**
825fe96e57dSRandy Dunlap * list_for_each_entry_from - iterate over list of given type from the current point
8268e3a67a9SRandy Dunlap * @pos: the type * to use as a loop cursor.
827e229c2fbSArnaldo Carvalho de Melo * @head: the head for your list.
8283943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
829fe96e57dSRandy Dunlap *
830fe96e57dSRandy Dunlap * Iterate over list of given type, continuing from current position.
831e229c2fbSArnaldo Carvalho de Melo */
832e229c2fbSArnaldo Carvalho de Melo #define list_for_each_entry_from(pos, head, member) \
833e1308161SAndy Shevchenko for (; !list_entry_is_head(pos, head, member); \
8348120e2e5SOleg Nesterov pos = list_next_entry(pos, member))
835e229c2fbSArnaldo Carvalho de Melo
836e229c2fbSArnaldo Carvalho de Melo /**
837b862815cSJiri Pirko * list_for_each_entry_from_reverse - iterate backwards over list of given type
838b862815cSJiri Pirko * from the current point
839b862815cSJiri Pirko * @pos: the type * to use as a loop cursor.
840b862815cSJiri Pirko * @head: the head for your list.
841b862815cSJiri Pirko * @member: the name of the list_head within the struct.
842b862815cSJiri Pirko *
843b862815cSJiri Pirko * Iterate backwards over list of given type, continuing from current position.
844b862815cSJiri Pirko */
845b862815cSJiri Pirko #define list_for_each_entry_from_reverse(pos, head, member) \
846e1308161SAndy Shevchenko for (; !list_entry_is_head(pos, head, member); \
847b862815cSJiri Pirko pos = list_prev_entry(pos, member))
848b862815cSJiri Pirko
849b862815cSJiri Pirko /**
8501da177e4SLinus Torvalds * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
8518e3a67a9SRandy Dunlap * @pos: the type * to use as a loop cursor.
8521da177e4SLinus Torvalds * @n: another type * to use as temporary storage
8531da177e4SLinus Torvalds * @head: the head for your list.
8543943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
8551da177e4SLinus Torvalds */
8561da177e4SLinus Torvalds #define list_for_each_entry_safe(pos, n, head, member) \
85793be3c2eSOleg Nesterov for (pos = list_first_entry(head, typeof(*pos), member), \
8588120e2e5SOleg Nesterov n = list_next_entry(pos, member); \
859e1308161SAndy Shevchenko !list_entry_is_head(pos, head, member); \
8608120e2e5SOleg Nesterov pos = n, n = list_next_entry(n, member))
8611da177e4SLinus Torvalds
8621da177e4SLinus Torvalds /**
8639a86e2baSBen Hutchings * list_for_each_entry_safe_continue - continue list iteration safe against removal
8648e3a67a9SRandy Dunlap * @pos: the type * to use as a loop cursor.
86574459dc7SArnaldo Carvalho de Melo * @n: another type * to use as temporary storage
86674459dc7SArnaldo Carvalho de Melo * @head: the head for your list.
8673943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
868fe96e57dSRandy Dunlap *
869fe96e57dSRandy Dunlap * Iterate over list of given type, continuing after current point,
870fe96e57dSRandy Dunlap * safe against removal of list entry.
87174459dc7SArnaldo Carvalho de Melo */
87274459dc7SArnaldo Carvalho de Melo #define list_for_each_entry_safe_continue(pos, n, head, member) \
8738120e2e5SOleg Nesterov for (pos = list_next_entry(pos, member), \
8748120e2e5SOleg Nesterov n = list_next_entry(pos, member); \
875e1308161SAndy Shevchenko !list_entry_is_head(pos, head, member); \
8768120e2e5SOleg Nesterov pos = n, n = list_next_entry(n, member))
87774459dc7SArnaldo Carvalho de Melo
87874459dc7SArnaldo Carvalho de Melo /**
8799a86e2baSBen Hutchings * list_for_each_entry_safe_from - iterate over list from current point safe against removal
8808e3a67a9SRandy Dunlap * @pos: the type * to use as a loop cursor.
881d8dcffeeSArnaldo Carvalho de Melo * @n: another type * to use as temporary storage
882d8dcffeeSArnaldo Carvalho de Melo * @head: the head for your list.
8833943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
884fe96e57dSRandy Dunlap *
885fe96e57dSRandy Dunlap * Iterate over list of given type from current point, safe against
886fe96e57dSRandy Dunlap * removal of list entry.
887d8dcffeeSArnaldo Carvalho de Melo */
888d8dcffeeSArnaldo Carvalho de Melo #define list_for_each_entry_safe_from(pos, n, head, member) \
8898120e2e5SOleg Nesterov for (n = list_next_entry(pos, member); \
890e1308161SAndy Shevchenko !list_entry_is_head(pos, head, member); \
8918120e2e5SOleg Nesterov pos = n, n = list_next_entry(n, member))
892d8dcffeeSArnaldo Carvalho de Melo
893d8dcffeeSArnaldo Carvalho de Melo /**
8949a86e2baSBen Hutchings * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
8958e3a67a9SRandy Dunlap * @pos: the type * to use as a loop cursor.
8960ad42352SDavid Howells * @n: another type * to use as temporary storage
8970ad42352SDavid Howells * @head: the head for your list.
8983943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
899fe96e57dSRandy Dunlap *
900fe96e57dSRandy Dunlap * Iterate backwards over list of given type, safe against removal
901fe96e57dSRandy Dunlap * of list entry.
9020ad42352SDavid Howells */
9030ad42352SDavid Howells #define list_for_each_entry_safe_reverse(pos, n, head, member) \
90493be3c2eSOleg Nesterov for (pos = list_last_entry(head, typeof(*pos), member), \
9058120e2e5SOleg Nesterov n = list_prev_entry(pos, member); \
906e1308161SAndy Shevchenko !list_entry_is_head(pos, head, member); \
9078120e2e5SOleg Nesterov pos = n, n = list_prev_entry(n, member))
9080ad42352SDavid Howells
90957439f87S[email protected] /**
91057439f87S[email protected] * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
91157439f87S[email protected] * @pos: the loop cursor used in the list_for_each_entry_safe loop
91257439f87S[email protected] * @n: temporary storage used in list_for_each_entry_safe
9133943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
91457439f87S[email protected] *
91557439f87S[email protected] * list_safe_reset_next is not safe to use in general if the list may be
91657439f87S[email protected] * modified concurrently (eg. the lock is dropped in the loop body). An
91757439f87S[email protected] * exception to this is if the cursor element (pos) is pinned in the list,
91857439f87S[email protected] * and list_safe_reset_next is called after re-taking the lock and before
91957439f87S[email protected] * completing the current iteration of the loop body.
92057439f87S[email protected] */
92157439f87S[email protected] #define list_safe_reset_next(pos, n, member) \
9228120e2e5SOleg Nesterov n = list_next_entry(pos, member)
92357439f87S[email protected]
9241da177e4SLinus Torvalds /*
9251da177e4SLinus Torvalds * Double linked lists with a single pointer list head.
9261da177e4SLinus Torvalds * Mostly useful for hash tables where the two pointer list head is
9271da177e4SLinus Torvalds * too wasteful.
9281da177e4SLinus Torvalds * You lose the ability to access the tail in O(1).
9291da177e4SLinus Torvalds */
9301da177e4SLinus Torvalds
9311da177e4SLinus Torvalds #define HLIST_HEAD_INIT { .first = NULL }
9321da177e4SLinus Torvalds #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
9331da177e4SLinus Torvalds #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
INIT_HLIST_NODE(struct hlist_node * h)934490d6ab1SZach Brown static inline void INIT_HLIST_NODE(struct hlist_node *h)
935490d6ab1SZach Brown {
936490d6ab1SZach Brown h->next = NULL;
937490d6ab1SZach Brown h->pprev = NULL;
938490d6ab1SZach Brown }
9391da177e4SLinus Torvalds
94046deb744SPaul E. McKenney /**
94146deb744SPaul E. McKenney * hlist_unhashed - Has node been removed from list and reinitialized?
94246deb744SPaul E. McKenney * @h: Node to be checked
94346deb744SPaul E. McKenney *
94446deb744SPaul E. McKenney * Not that not all removal functions will leave a node in unhashed
94546deb744SPaul E. McKenney * state. For example, hlist_nulls_del_init_rcu() does leave the
94646deb744SPaul E. McKenney * node in unhashed state, but hlist_nulls_del() does not.
94746deb744SPaul E. McKenney */
hlist_unhashed(const struct hlist_node * h)9481da177e4SLinus Torvalds static inline int hlist_unhashed(const struct hlist_node *h)
9491da177e4SLinus Torvalds {
9501da177e4SLinus Torvalds return !h->pprev;
9511da177e4SLinus Torvalds }
9521da177e4SLinus Torvalds
95346deb744SPaul E. McKenney /**
95446deb744SPaul E. McKenney * hlist_unhashed_lockless - Version of hlist_unhashed for lockless use
95546deb744SPaul E. McKenney * @h: Node to be checked
95646deb744SPaul E. McKenney *
95746deb744SPaul E. McKenney * This variant of hlist_unhashed() must be used in lockless contexts
95846deb744SPaul E. McKenney * to avoid potential load-tearing. The READ_ONCE() is paired with the
95946deb744SPaul E. McKenney * various WRITE_ONCE() in hlist helpers that are defined below.
960c54a2744SEric Dumazet */
hlist_unhashed_lockless(const struct hlist_node * h)961c54a2744SEric Dumazet static inline int hlist_unhashed_lockless(const struct hlist_node *h)
962c54a2744SEric Dumazet {
963c54a2744SEric Dumazet return !READ_ONCE(h->pprev);
964c54a2744SEric Dumazet }
965c54a2744SEric Dumazet
96646deb744SPaul E. McKenney /**
96746deb744SPaul E. McKenney * hlist_empty - Is the specified hlist_head structure an empty hlist?
96846deb744SPaul E. McKenney * @h: Structure to check.
96946deb744SPaul E. McKenney */
hlist_empty(const struct hlist_head * h)9701da177e4SLinus Torvalds static inline int hlist_empty(const struct hlist_head *h)
9711da177e4SLinus Torvalds {
9721658d35eSPaul E. McKenney return !READ_ONCE(h->first);
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds
__hlist_del(struct hlist_node * n)9751da177e4SLinus Torvalds static inline void __hlist_del(struct hlist_node *n)
9761da177e4SLinus Torvalds {
9771da177e4SLinus Torvalds struct hlist_node *next = n->next;
9781da177e4SLinus Torvalds struct hlist_node **pprev = n->pprev;
9797f5f873cSPaul E. McKenney
9807f5f873cSPaul E. McKenney WRITE_ONCE(*pprev, next);
9811da177e4SLinus Torvalds if (next)
982c54a2744SEric Dumazet WRITE_ONCE(next->pprev, pprev);
9831da177e4SLinus Torvalds }
9841da177e4SLinus Torvalds
98546deb744SPaul E. McKenney /**
98646deb744SPaul E. McKenney * hlist_del - Delete the specified hlist_node from its list
98746deb744SPaul E. McKenney * @n: Node to delete.
98846deb744SPaul E. McKenney *
98946deb744SPaul E. McKenney * Note that this function leaves the node in hashed state. Use
99046deb744SPaul E. McKenney * hlist_del_init() or similar instead to unhash @n.
99146deb744SPaul E. McKenney */
hlist_del(struct hlist_node * n)9921da177e4SLinus Torvalds static inline void hlist_del(struct hlist_node *n)
9931da177e4SLinus Torvalds {
9941da177e4SLinus Torvalds __hlist_del(n);
9951da177e4SLinus Torvalds n->next = LIST_POISON1;
9961da177e4SLinus Torvalds n->pprev = LIST_POISON2;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds
99946deb744SPaul E. McKenney /**
100046deb744SPaul E. McKenney * hlist_del_init - Delete the specified hlist_node from its list and initialize
100146deb744SPaul E. McKenney * @n: Node to delete.
100246deb744SPaul E. McKenney *
100346deb744SPaul E. McKenney * Note that this function leaves the node in unhashed state.
100446deb744SPaul E. McKenney */
hlist_del_init(struct hlist_node * n)10051da177e4SLinus Torvalds static inline void hlist_del_init(struct hlist_node *n)
10061da177e4SLinus Torvalds {
1007da753beaSAkinobu Mita if (!hlist_unhashed(n)) {
10081da177e4SLinus Torvalds __hlist_del(n);
10091da177e4SLinus Torvalds INIT_HLIST_NODE(n);
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds
101346deb744SPaul E. McKenney /**
101446deb744SPaul E. McKenney * hlist_add_head - add a new entry at the beginning of the hlist
101546deb744SPaul E. McKenney * @n: new entry to be added
101646deb744SPaul E. McKenney * @h: hlist head to add it after
101746deb744SPaul E. McKenney *
101846deb744SPaul E. McKenney * Insert a new entry after the specified head.
101946deb744SPaul E. McKenney * This is good for implementing stacks.
102046deb744SPaul E. McKenney */
hlist_add_head(struct hlist_node * n,struct hlist_head * h)10211da177e4SLinus Torvalds static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
10221da177e4SLinus Torvalds {
10231da177e4SLinus Torvalds struct hlist_node *first = h->first;
1024c54a2744SEric Dumazet WRITE_ONCE(n->next, first);
10251da177e4SLinus Torvalds if (first)
1026c54a2744SEric Dumazet WRITE_ONCE(first->pprev, &n->next);
10271c97be67SPaul E. McKenney WRITE_ONCE(h->first, n);
1028c54a2744SEric Dumazet WRITE_ONCE(n->pprev, &h->first);
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds
103146deb744SPaul E. McKenney /**
103246deb744SPaul E. McKenney * hlist_add_before - add a new entry before the one specified
103346deb744SPaul E. McKenney * @n: new entry to be added
103446deb744SPaul E. McKenney * @next: hlist node to add it before, which must be non-NULL
103546deb744SPaul E. McKenney */
hlist_add_before(struct hlist_node * n,struct hlist_node * next)10361da177e4SLinus Torvalds static inline void hlist_add_before(struct hlist_node *n,
10371da177e4SLinus Torvalds struct hlist_node *next)
10381da177e4SLinus Torvalds {
1039c54a2744SEric Dumazet WRITE_ONCE(n->pprev, next->pprev);
1040c54a2744SEric Dumazet WRITE_ONCE(n->next, next);
1041c54a2744SEric Dumazet WRITE_ONCE(next->pprev, &n->next);
10421c97be67SPaul E. McKenney WRITE_ONCE(*(n->pprev), n);
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds
104546deb744SPaul E. McKenney /**
10464704bd31SMauro Carvalho Chehab * hlist_add_behind - add a new entry after the one specified
104746deb744SPaul E. McKenney * @n: new entry to be added
104846deb744SPaul E. McKenney * @prev: hlist node to add it after, which must be non-NULL
104946deb744SPaul E. McKenney */
hlist_add_behind(struct hlist_node * n,struct hlist_node * prev)10501d023284SKen Helias static inline void hlist_add_behind(struct hlist_node *n,
10511d023284SKen Helias struct hlist_node *prev)
10521da177e4SLinus Torvalds {
1053c54a2744SEric Dumazet WRITE_ONCE(n->next, prev->next);
1054c54a2744SEric Dumazet WRITE_ONCE(prev->next, n);
1055c54a2744SEric Dumazet WRITE_ONCE(n->pprev, &prev->next);
10561da177e4SLinus Torvalds
1057bc18dd33SKen Helias if (n->next)
1058c54a2744SEric Dumazet WRITE_ONCE(n->next->pprev, &n->next);
10591da177e4SLinus Torvalds }
10601da177e4SLinus Torvalds
106146deb744SPaul E. McKenney /**
106246deb744SPaul E. McKenney * hlist_add_fake - create a fake hlist consisting of a single headless node
106346deb744SPaul E. McKenney * @n: Node to make a fake list out of
106446deb744SPaul E. McKenney *
106546deb744SPaul E. McKenney * This makes @n appear to be its own predecessor on a headless hlist.
106646deb744SPaul E. McKenney * The point of this is to allow things like hlist_del() to work correctly
106746deb744SPaul E. McKenney * in cases where there is no list.
106846deb744SPaul E. McKenney */
hlist_add_fake(struct hlist_node * n)1069756acc2dSAl Viro static inline void hlist_add_fake(struct hlist_node *n)
1070756acc2dSAl Viro {
1071756acc2dSAl Viro n->pprev = &n->next;
1072756acc2dSAl Viro }
1073756acc2dSAl Viro
107446deb744SPaul E. McKenney /**
107546deb744SPaul E. McKenney * hlist_fake: Is this node a fake hlist?
107646deb744SPaul E. McKenney * @h: Node to check for being a self-referential fake hlist.
107746deb744SPaul E. McKenney */
hlist_fake(struct hlist_node * h)1078cbedaac6SJosef Bacik static inline bool hlist_fake(struct hlist_node *h)
1079cbedaac6SJosef Bacik {
1080cbedaac6SJosef Bacik return h->pprev == &h->next;
1081cbedaac6SJosef Bacik }
1082cbedaac6SJosef Bacik
108346deb744SPaul E. McKenney /**
108446deb744SPaul E. McKenney * hlist_is_singular_node - is node the only element of the specified hlist?
108546deb744SPaul E. McKenney * @n: Node to check for singularity.
108646deb744SPaul E. McKenney * @h: Header for potentially singular list.
108746deb744SPaul E. McKenney *
108815dba1e3SThomas Gleixner * Check whether the node is the only node of the head without
108946deb744SPaul E. McKenney * accessing head, thus avoiding unnecessary cache misses.
109015dba1e3SThomas Gleixner */
109115dba1e3SThomas Gleixner static inline bool
hlist_is_singular_node(struct hlist_node * n,struct hlist_head * h)109215dba1e3SThomas Gleixner hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
109315dba1e3SThomas Gleixner {
109415dba1e3SThomas Gleixner return !n->next && n->pprev == &h->first;
109515dba1e3SThomas Gleixner }
109615dba1e3SThomas Gleixner
109746deb744SPaul E. McKenney /**
109846deb744SPaul E. McKenney * hlist_move_list - Move an hlist
109946deb744SPaul E. McKenney * @old: hlist_head for old list.
110046deb744SPaul E. McKenney * @new: hlist_head for new list.
110146deb744SPaul E. McKenney *
1102673d62ccSVegard Nossum * Move a list from one list head to another. Fixup the pprev
1103673d62ccSVegard Nossum * reference of the first entry if it exists.
1104673d62ccSVegard Nossum */
hlist_move_list(struct hlist_head * old,struct hlist_head * new)1105673d62ccSVegard Nossum static inline void hlist_move_list(struct hlist_head *old,
1106673d62ccSVegard Nossum struct hlist_head *new)
1107673d62ccSVegard Nossum {
1108673d62ccSVegard Nossum new->first = old->first;
1109673d62ccSVegard Nossum if (new->first)
1110673d62ccSVegard Nossum new->first->pprev = &new->first;
1111673d62ccSVegard Nossum old->first = NULL;
1112673d62ccSVegard Nossum }
1113673d62ccSVegard Nossum
1114083772c9SJakub Kicinski /**
1115083772c9SJakub Kicinski * hlist_splice_init() - move all entries from one list to another
1116083772c9SJakub Kicinski * @from: hlist_head from which entries will be moved
1117083772c9SJakub Kicinski * @last: last entry on the @from list
1118083772c9SJakub Kicinski * @to: hlist_head to which entries will be moved
1119083772c9SJakub Kicinski *
1120083772c9SJakub Kicinski * @to can be empty, @from must contain at least @last.
1121083772c9SJakub Kicinski */
hlist_splice_init(struct hlist_head * from,struct hlist_node * last,struct hlist_head * to)1122083772c9SJakub Kicinski static inline void hlist_splice_init(struct hlist_head *from,
1123083772c9SJakub Kicinski struct hlist_node *last,
1124083772c9SJakub Kicinski struct hlist_head *to)
1125083772c9SJakub Kicinski {
1126083772c9SJakub Kicinski if (to->first)
1127083772c9SJakub Kicinski to->first->pprev = &last->next;
1128083772c9SJakub Kicinski last->next = to->first;
1129083772c9SJakub Kicinski to->first = from->first;
1130083772c9SJakub Kicinski from->first->pprev = &to->first;
1131083772c9SJakub Kicinski from->first = NULL;
1132083772c9SJakub Kicinski }
1133083772c9SJakub Kicinski
11341da177e4SLinus Torvalds #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
11351da177e4SLinus Torvalds
11361da177e4SLinus Torvalds #define hlist_for_each(pos, head) \
113775d65a42SLinus Torvalds for (pos = (head)->first; pos ; pos = pos->next)
11381da177e4SLinus Torvalds
11391da177e4SLinus Torvalds #define hlist_for_each_safe(pos, n, head) \
11401da177e4SLinus Torvalds for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
11411da177e4SLinus Torvalds pos = n)
11421da177e4SLinus Torvalds
1143b67bfe0dSSasha Levin #define hlist_entry_safe(ptr, type, member) \
1144f65846a1SPaul E. McKenney ({ typeof(ptr) ____ptr = (ptr); \
1145f65846a1SPaul E. McKenney ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
1146f65846a1SPaul E. McKenney })
1147b67bfe0dSSasha Levin
11481da177e4SLinus Torvalds /**
11491da177e4SLinus Torvalds * hlist_for_each_entry - iterate over list of given type
1150b67bfe0dSSasha Levin * @pos: the type * to use as a loop cursor.
11511da177e4SLinus Torvalds * @head: the head for your list.
11521da177e4SLinus Torvalds * @member: the name of the hlist_node within the struct.
11531da177e4SLinus Torvalds */
1154b67bfe0dSSasha Levin #define hlist_for_each_entry(pos, head, member) \
1155b67bfe0dSSasha Levin for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
1156b67bfe0dSSasha Levin pos; \
1157b67bfe0dSSasha Levin pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
11581da177e4SLinus Torvalds
11591da177e4SLinus Torvalds /**
1160fe96e57dSRandy Dunlap * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1161b67bfe0dSSasha Levin * @pos: the type * to use as a loop cursor.
11621da177e4SLinus Torvalds * @member: the name of the hlist_node within the struct.
11631da177e4SLinus Torvalds */
1164b67bfe0dSSasha Levin #define hlist_for_each_entry_continue(pos, member) \
1165b67bfe0dSSasha Levin for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
1166b67bfe0dSSasha Levin pos; \
1167b67bfe0dSSasha Levin pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
11681da177e4SLinus Torvalds
11691da177e4SLinus Torvalds /**
1170fe96e57dSRandy Dunlap * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1171b67bfe0dSSasha Levin * @pos: the type * to use as a loop cursor.
11721da177e4SLinus Torvalds * @member: the name of the hlist_node within the struct.
11731da177e4SLinus Torvalds */
1174b67bfe0dSSasha Levin #define hlist_for_each_entry_from(pos, member) \
1175b67bfe0dSSasha Levin for (; pos; \
1176b67bfe0dSSasha Levin pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
11771da177e4SLinus Torvalds
11781da177e4SLinus Torvalds /**
11791da177e4SLinus Torvalds * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1180b67bfe0dSSasha Levin * @pos: the type * to use as a loop cursor.
1181c84716c4SNeilBrown * @n: a &struct hlist_node to use as temporary storage
11821da177e4SLinus Torvalds * @head: the head for your list.
11831da177e4SLinus Torvalds * @member: the name of the hlist_node within the struct.
11841da177e4SLinus Torvalds */
1185b67bfe0dSSasha Levin #define hlist_for_each_entry_safe(pos, n, head, member) \
1186b67bfe0dSSasha Levin for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
1187b67bfe0dSSasha Levin pos && ({ n = pos->member.next; 1; }); \
1188b67bfe0dSSasha Levin pos = hlist_entry_safe(n, typeof(*pos), member))
11891da177e4SLinus Torvalds
1190a43c4756SPierre Gondois /**
1191a43c4756SPierre Gondois * hlist_count_nodes - count nodes in the hlist
1192a43c4756SPierre Gondois * @head: the head for your hlist.
1193a43c4756SPierre Gondois */
hlist_count_nodes(struct hlist_head * head)1194a43c4756SPierre Gondois static inline size_t hlist_count_nodes(struct hlist_head *head)
1195a43c4756SPierre Gondois {
1196a43c4756SPierre Gondois struct hlist_node *pos;
1197a43c4756SPierre Gondois size_t count = 0;
1198a43c4756SPierre Gondois
1199a43c4756SPierre Gondois hlist_for_each(pos, head)
1200a43c4756SPierre Gondois count++;
1201a43c4756SPierre Gondois
1202a43c4756SPierre Gondois return count;
1203a43c4756SPierre Gondois }
1204a43c4756SPierre Gondois
12051da177e4SLinus Torvalds #endif
1206