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