1*a376eb32SXin Li #ifndef _LINUX_LIST_H
2*a376eb32SXin Li #define _LINUX_LIST_H
3*a376eb32SXin Li
4*a376eb32SXin Li #include <stddef.h>
5*a376eb32SXin Li
6*a376eb32SXin Li #undef offsetof
7*a376eb32SXin Li #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
8*a376eb32SXin Li
9*a376eb32SXin Li /**
10*a376eb32SXin Li * container_of - cast a member of a structure out to the containing structure
11*a376eb32SXin Li *
12*a376eb32SXin Li * @ptr: the pointer to the member.
13*a376eb32SXin Li * @type: the type of the container struct this is embedded in.
14*a376eb32SXin Li * @member: the name of the member within the struct.
15*a376eb32SXin Li *
16*a376eb32SXin Li */
17*a376eb32SXin Li #define container_of(ptr, type, member) ({ \
18*a376eb32SXin Li typeof( ((type *)0)->member ) *__mptr = (ptr); \
19*a376eb32SXin Li (type *)( (char *)__mptr - offsetof(type,member) );})
20*a376eb32SXin Li
21*a376eb32SXin Li /*
22*a376eb32SXin Li * Check at compile time that something is of a particular type.
23*a376eb32SXin Li * Always evaluates to 1 so you may use it easily in comparisons.
24*a376eb32SXin Li */
25*a376eb32SXin Li #define typecheck(type,x) \
26*a376eb32SXin Li ({ type __dummy; \
27*a376eb32SXin Li typeof(x) __dummy2; \
28*a376eb32SXin Li (void)(&__dummy == &__dummy2); \
29*a376eb32SXin Li 1; \
30*a376eb32SXin Li })
31*a376eb32SXin Li
32*a376eb32SXin Li #define prefetch(x) 1
33*a376eb32SXin Li
34*a376eb32SXin Li /* empty define to make this work in userspace -HW */
35*a376eb32SXin Li #ifndef smp_wmb
36*a376eb32SXin Li #define smp_wmb()
37*a376eb32SXin Li #endif
38*a376eb32SXin Li
39*a376eb32SXin Li /*
40*a376eb32SXin Li * These are non-NULL pointers that will result in page faults
41*a376eb32SXin Li * under normal circumstances, used to verify that nobody uses
42*a376eb32SXin Li * non-initialized list entries.
43*a376eb32SXin Li */
44*a376eb32SXin Li #define LIST_POISON1 ((void *) 0x00100100)
45*a376eb32SXin Li #define LIST_POISON2 ((void *) 0x00200200)
46*a376eb32SXin Li
47*a376eb32SXin Li /*
48*a376eb32SXin Li * Simple doubly linked list implementation.
49*a376eb32SXin Li *
50*a376eb32SXin Li * Some of the internal functions ("__xxx") are useful when
51*a376eb32SXin Li * manipulating whole lists rather than single entries, as
52*a376eb32SXin Li * sometimes we already know the next/prev entries and we can
53*a376eb32SXin Li * generate better code by using them directly rather than
54*a376eb32SXin Li * using the generic single-entry routines.
55*a376eb32SXin Li */
56*a376eb32SXin Li
57*a376eb32SXin Li struct list_head {
58*a376eb32SXin Li struct list_head *next, *prev;
59*a376eb32SXin Li };
60*a376eb32SXin Li
61*a376eb32SXin Li #define LIST_HEAD_INIT(name) { &(name), &(name) }
62*a376eb32SXin Li
63*a376eb32SXin Li #define LIST_HEAD(name) \
64*a376eb32SXin Li struct list_head name = LIST_HEAD_INIT(name)
65*a376eb32SXin Li
66*a376eb32SXin Li #define INIT_LIST_HEAD(ptr) do { \
67*a376eb32SXin Li (ptr)->next = (ptr); (ptr)->prev = (ptr); \
68*a376eb32SXin Li } while (0)
69*a376eb32SXin Li
70*a376eb32SXin Li /*
71*a376eb32SXin Li * Insert a new entry between two known consecutive entries.
72*a376eb32SXin Li *
73*a376eb32SXin Li * This is only for internal list manipulation where we know
74*a376eb32SXin Li * the prev/next entries already!
75*a376eb32SXin Li */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)76*a376eb32SXin Li static inline void __list_add(struct list_head *new,
77*a376eb32SXin Li struct list_head *prev,
78*a376eb32SXin Li struct list_head *next)
79*a376eb32SXin Li {
80*a376eb32SXin Li next->prev = new;
81*a376eb32SXin Li new->next = next;
82*a376eb32SXin Li new->prev = prev;
83*a376eb32SXin Li prev->next = new;
84*a376eb32SXin Li }
85*a376eb32SXin Li
86*a376eb32SXin Li /**
87*a376eb32SXin Li * list_add - add a new entry
88*a376eb32SXin Li * @new: new entry to be added
89*a376eb32SXin Li * @head: list head to add it after
90*a376eb32SXin Li *
91*a376eb32SXin Li * Insert a new entry after the specified head.
92*a376eb32SXin Li * This is good for implementing stacks.
93*a376eb32SXin Li */
list_add(struct list_head * new,struct list_head * head)94*a376eb32SXin Li static inline void list_add(struct list_head *new, struct list_head *head)
95*a376eb32SXin Li {
96*a376eb32SXin Li __list_add(new, head, head->next);
97*a376eb32SXin Li }
98*a376eb32SXin Li
99*a376eb32SXin Li /**
100*a376eb32SXin Li * list_add_tail - add a new entry
101*a376eb32SXin Li * @new: new entry to be added
102*a376eb32SXin Li * @head: list head to add it before
103*a376eb32SXin Li *
104*a376eb32SXin Li * Insert a new entry before the specified head.
105*a376eb32SXin Li * This is useful for implementing queues.
106*a376eb32SXin Li */
list_add_tail(struct list_head * new,struct list_head * head)107*a376eb32SXin Li static inline void list_add_tail(struct list_head *new, struct list_head *head)
108*a376eb32SXin Li {
109*a376eb32SXin Li __list_add(new, head->prev, head);
110*a376eb32SXin Li }
111*a376eb32SXin Li
112*a376eb32SXin Li /*
113*a376eb32SXin Li * Insert a new entry between two known consecutive entries.
114*a376eb32SXin Li *
115*a376eb32SXin Li * This is only for internal list manipulation where we know
116*a376eb32SXin Li * the prev/next entries already!
117*a376eb32SXin Li */
__list_add_rcu(struct list_head * new,struct list_head * prev,struct list_head * next)118*a376eb32SXin Li static inline void __list_add_rcu(struct list_head * new,
119*a376eb32SXin Li struct list_head * prev, struct list_head * next)
120*a376eb32SXin Li {
121*a376eb32SXin Li new->next = next;
122*a376eb32SXin Li new->prev = prev;
123*a376eb32SXin Li smp_wmb();
124*a376eb32SXin Li next->prev = new;
125*a376eb32SXin Li prev->next = new;
126*a376eb32SXin Li }
127*a376eb32SXin Li
128*a376eb32SXin Li /**
129*a376eb32SXin Li * list_add_rcu - add a new entry to rcu-protected list
130*a376eb32SXin Li * @new: new entry to be added
131*a376eb32SXin Li * @head: list head to add it after
132*a376eb32SXin Li *
133*a376eb32SXin Li * Insert a new entry after the specified head.
134*a376eb32SXin Li * This is good for implementing stacks.
135*a376eb32SXin Li *
136*a376eb32SXin Li * The caller must take whatever precautions are necessary
137*a376eb32SXin Li * (such as holding appropriate locks) to avoid racing
138*a376eb32SXin Li * with another list-mutation primitive, such as list_add_rcu()
139*a376eb32SXin Li * or list_del_rcu(), running on this same list.
140*a376eb32SXin Li * However, it is perfectly legal to run concurrently with
141*a376eb32SXin Li * the _rcu list-traversal primitives, such as
142*a376eb32SXin Li * list_for_each_entry_rcu().
143*a376eb32SXin Li */
list_add_rcu(struct list_head * new,struct list_head * head)144*a376eb32SXin Li static inline void list_add_rcu(struct list_head *new, struct list_head *head)
145*a376eb32SXin Li {
146*a376eb32SXin Li __list_add_rcu(new, head, head->next);
147*a376eb32SXin Li }
148*a376eb32SXin Li
149*a376eb32SXin Li /**
150*a376eb32SXin Li * list_add_tail_rcu - add a new entry to rcu-protected list
151*a376eb32SXin Li * @new: new entry to be added
152*a376eb32SXin Li * @head: list head to add it before
153*a376eb32SXin Li *
154*a376eb32SXin Li * Insert a new entry before the specified head.
155*a376eb32SXin Li * This is useful for implementing queues.
156*a376eb32SXin Li *
157*a376eb32SXin Li * The caller must take whatever precautions are necessary
158*a376eb32SXin Li * (such as holding appropriate locks) to avoid racing
159*a376eb32SXin Li * with another list-mutation primitive, such as list_add_tail_rcu()
160*a376eb32SXin Li * or list_del_rcu(), running on this same list.
161*a376eb32SXin Li * However, it is perfectly legal to run concurrently with
162*a376eb32SXin Li * the _rcu list-traversal primitives, such as
163*a376eb32SXin Li * list_for_each_entry_rcu().
164*a376eb32SXin Li */
list_add_tail_rcu(struct list_head * new,struct list_head * head)165*a376eb32SXin Li static inline void list_add_tail_rcu(struct list_head *new,
166*a376eb32SXin Li struct list_head *head)
167*a376eb32SXin Li {
168*a376eb32SXin Li __list_add_rcu(new, head->prev, head);
169*a376eb32SXin Li }
170*a376eb32SXin Li
171*a376eb32SXin Li /*
172*a376eb32SXin Li * Delete a list entry by making the prev/next entries
173*a376eb32SXin Li * point to each other.
174*a376eb32SXin Li *
175*a376eb32SXin Li * This is only for internal list manipulation where we know
176*a376eb32SXin Li * the prev/next entries already!
177*a376eb32SXin Li */
__list_del(struct list_head * prev,struct list_head * next)178*a376eb32SXin Li static inline void __list_del(struct list_head * prev, struct list_head * next)
179*a376eb32SXin Li {
180*a376eb32SXin Li next->prev = prev;
181*a376eb32SXin Li prev->next = next;
182*a376eb32SXin Li }
183*a376eb32SXin Li
184*a376eb32SXin Li /**
185*a376eb32SXin Li * list_del - deletes entry from list.
186*a376eb32SXin Li * @entry: the element to delete from the list.
187*a376eb32SXin Li * Note: list_empty on entry does not return true after this, the entry is
188*a376eb32SXin Li * in an undefined state.
189*a376eb32SXin Li */
list_del(struct list_head * entry)190*a376eb32SXin Li static inline void list_del(struct list_head *entry)
191*a376eb32SXin Li {
192*a376eb32SXin Li __list_del(entry->prev, entry->next);
193*a376eb32SXin Li entry->next = LIST_POISON1;
194*a376eb32SXin Li entry->prev = LIST_POISON2;
195*a376eb32SXin Li }
196*a376eb32SXin Li
197*a376eb32SXin Li /**
198*a376eb32SXin Li * list_del_rcu - deletes entry from list without re-initialization
199*a376eb32SXin Li * @entry: the element to delete from the list.
200*a376eb32SXin Li *
201*a376eb32SXin Li * Note: list_empty on entry does not return true after this,
202*a376eb32SXin Li * the entry is in an undefined state. It is useful for RCU based
203*a376eb32SXin Li * lockfree traversal.
204*a376eb32SXin Li *
205*a376eb32SXin Li * In particular, it means that we can not poison the forward
206*a376eb32SXin Li * pointers that may still be used for walking the list.
207*a376eb32SXin Li *
208*a376eb32SXin Li * The caller must take whatever precautions are necessary
209*a376eb32SXin Li * (such as holding appropriate locks) to avoid racing
210*a376eb32SXin Li * with another list-mutation primitive, such as list_del_rcu()
211*a376eb32SXin Li * or list_add_rcu(), running on this same list.
212*a376eb32SXin Li * However, it is perfectly legal to run concurrently with
213*a376eb32SXin Li * the _rcu list-traversal primitives, such as
214*a376eb32SXin Li * list_for_each_entry_rcu().
215*a376eb32SXin Li *
216*a376eb32SXin Li * Note that the caller is not permitted to immediately free
217*a376eb32SXin Li * the newly deleted entry. Instead, either synchronize_kernel()
218*a376eb32SXin Li * or call_rcu() must be used to defer freeing until an RCU
219*a376eb32SXin Li * grace period has elapsed.
220*a376eb32SXin Li */
list_del_rcu(struct list_head * entry)221*a376eb32SXin Li static inline void list_del_rcu(struct list_head *entry)
222*a376eb32SXin Li {
223*a376eb32SXin Li __list_del(entry->prev, entry->next);
224*a376eb32SXin Li entry->prev = LIST_POISON2;
225*a376eb32SXin Li }
226*a376eb32SXin Li
227*a376eb32SXin Li /**
228*a376eb32SXin Li * list_del_init - deletes entry from list and reinitialize it.
229*a376eb32SXin Li * @entry: the element to delete from the list.
230*a376eb32SXin Li */
list_del_init(struct list_head * entry)231*a376eb32SXin Li static inline void list_del_init(struct list_head *entry)
232*a376eb32SXin Li {
233*a376eb32SXin Li __list_del(entry->prev, entry->next);
234*a376eb32SXin Li INIT_LIST_HEAD(entry);
235*a376eb32SXin Li }
236*a376eb32SXin Li
237*a376eb32SXin Li /**
238*a376eb32SXin Li * list_move - delete from one list and add as another's head
239*a376eb32SXin Li * @list: the entry to move
240*a376eb32SXin Li * @head: the head that will precede our entry
241*a376eb32SXin Li */
list_move(struct list_head * list,struct list_head * head)242*a376eb32SXin Li static inline void list_move(struct list_head *list, struct list_head *head)
243*a376eb32SXin Li {
244*a376eb32SXin Li __list_del(list->prev, list->next);
245*a376eb32SXin Li list_add(list, head);
246*a376eb32SXin Li }
247*a376eb32SXin Li
248*a376eb32SXin Li /**
249*a376eb32SXin Li * list_move_tail - delete from one list and add as another's tail
250*a376eb32SXin Li * @list: the entry to move
251*a376eb32SXin Li * @head: the head that will follow our entry
252*a376eb32SXin Li */
list_move_tail(struct list_head * list,struct list_head * head)253*a376eb32SXin Li static inline void list_move_tail(struct list_head *list,
254*a376eb32SXin Li struct list_head *head)
255*a376eb32SXin Li {
256*a376eb32SXin Li __list_del(list->prev, list->next);
257*a376eb32SXin Li list_add_tail(list, head);
258*a376eb32SXin Li }
259*a376eb32SXin Li
260*a376eb32SXin Li /**
261*a376eb32SXin Li * list_empty - tests whether a list is empty
262*a376eb32SXin Li * @head: the list to test.
263*a376eb32SXin Li */
list_empty(const struct list_head * head)264*a376eb32SXin Li static inline int list_empty(const struct list_head *head)
265*a376eb32SXin Li {
266*a376eb32SXin Li return head->next == head;
267*a376eb32SXin Li }
268*a376eb32SXin Li
269*a376eb32SXin Li /**
270*a376eb32SXin Li * list_empty_careful - tests whether a list is
271*a376eb32SXin Li * empty _and_ checks that no other CPU might be
272*a376eb32SXin Li * in the process of still modifying either member
273*a376eb32SXin Li *
274*a376eb32SXin Li * NOTE: using list_empty_careful() without synchronization
275*a376eb32SXin Li * can only be safe if the only activity that can happen
276*a376eb32SXin Li * to the list entry is list_del_init(). Eg. it cannot be used
277*a376eb32SXin Li * if another CPU could re-list_add() it.
278*a376eb32SXin Li *
279*a376eb32SXin Li * @head: the list to test.
280*a376eb32SXin Li */
list_empty_careful(const struct list_head * head)281*a376eb32SXin Li static inline int list_empty_careful(const struct list_head *head)
282*a376eb32SXin Li {
283*a376eb32SXin Li struct list_head *next = head->next;
284*a376eb32SXin Li return (next == head) && (next == head->prev);
285*a376eb32SXin Li }
286*a376eb32SXin Li
__list_splice(struct list_head * list,struct list_head * head)287*a376eb32SXin Li static inline void __list_splice(struct list_head *list,
288*a376eb32SXin Li struct list_head *head)
289*a376eb32SXin Li {
290*a376eb32SXin Li struct list_head *first = list->next;
291*a376eb32SXin Li struct list_head *last = list->prev;
292*a376eb32SXin Li struct list_head *at = head->next;
293*a376eb32SXin Li
294*a376eb32SXin Li first->prev = head;
295*a376eb32SXin Li head->next = first;
296*a376eb32SXin Li
297*a376eb32SXin Li last->next = at;
298*a376eb32SXin Li at->prev = last;
299*a376eb32SXin Li }
300*a376eb32SXin Li
301*a376eb32SXin Li /**
302*a376eb32SXin Li * list_splice - join two lists
303*a376eb32SXin Li * @list: the new list to add.
304*a376eb32SXin Li * @head: the place to add it in the first list.
305*a376eb32SXin Li */
list_splice(struct list_head * list,struct list_head * head)306*a376eb32SXin Li static inline void list_splice(struct list_head *list, struct list_head *head)
307*a376eb32SXin Li {
308*a376eb32SXin Li if (!list_empty(list))
309*a376eb32SXin Li __list_splice(list, head);
310*a376eb32SXin Li }
311*a376eb32SXin Li
312*a376eb32SXin Li /**
313*a376eb32SXin Li * list_splice_init - join two lists and reinitialise the emptied list.
314*a376eb32SXin Li * @list: the new list to add.
315*a376eb32SXin Li * @head: the place to add it in the first list.
316*a376eb32SXin Li *
317*a376eb32SXin Li * The list at @list is reinitialised
318*a376eb32SXin Li */
list_splice_init(struct list_head * list,struct list_head * head)319*a376eb32SXin Li static inline void list_splice_init(struct list_head *list,
320*a376eb32SXin Li struct list_head *head)
321*a376eb32SXin Li {
322*a376eb32SXin Li if (!list_empty(list)) {
323*a376eb32SXin Li __list_splice(list, head);
324*a376eb32SXin Li INIT_LIST_HEAD(list);
325*a376eb32SXin Li }
326*a376eb32SXin Li }
327*a376eb32SXin Li
328*a376eb32SXin Li /**
329*a376eb32SXin Li * list_entry - get the struct for this entry
330*a376eb32SXin Li * @ptr: the &struct list_head pointer.
331*a376eb32SXin Li * @type: the type of the struct this is embedded in.
332*a376eb32SXin Li * @member: the name of the list_struct within the struct.
333*a376eb32SXin Li */
334*a376eb32SXin Li #define list_entry(ptr, type, member) \
335*a376eb32SXin Li container_of(ptr, type, member)
336*a376eb32SXin Li
337*a376eb32SXin Li /**
338*a376eb32SXin Li * list_for_each - iterate over a list
339*a376eb32SXin Li * @pos: the &struct list_head to use as a loop counter.
340*a376eb32SXin Li * @head: the head for your list.
341*a376eb32SXin Li */
342*a376eb32SXin Li #define list_for_each(pos, head) \
343*a376eb32SXin Li for (pos = (head)->next, prefetch(pos->next); pos != (head); \
344*a376eb32SXin Li pos = pos->next, prefetch(pos->next))
345*a376eb32SXin Li
346*a376eb32SXin Li /**
347*a376eb32SXin Li * __list_for_each - iterate over a list
348*a376eb32SXin Li * @pos: the &struct list_head to use as a loop counter.
349*a376eb32SXin Li * @head: the head for your list.
350*a376eb32SXin Li *
351*a376eb32SXin Li * This variant differs from list_for_each() in that it's the
352*a376eb32SXin Li * simplest possible list iteration code, no prefetching is done.
353*a376eb32SXin Li * Use this for code that knows the list to be very short (empty
354*a376eb32SXin Li * or 1 entry) most of the time.
355*a376eb32SXin Li */
356*a376eb32SXin Li #define __list_for_each(pos, head) \
357*a376eb32SXin Li for (pos = (head)->next; pos != (head); pos = pos->next)
358*a376eb32SXin Li
359*a376eb32SXin Li /**
360*a376eb32SXin Li * list_for_each_prev - iterate over a list backwards
361*a376eb32SXin Li * @pos: the &struct list_head to use as a loop counter.
362*a376eb32SXin Li * @head: the head for your list.
363*a376eb32SXin Li */
364*a376eb32SXin Li #define list_for_each_prev(pos, head) \
365*a376eb32SXin Li for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
366*a376eb32SXin Li pos = pos->prev, prefetch(pos->prev))
367*a376eb32SXin Li
368*a376eb32SXin Li /**
369*a376eb32SXin Li * list_for_each_safe - iterate over a list safe against removal of list entry
370*a376eb32SXin Li * @pos: the &struct list_head to use as a loop counter.
371*a376eb32SXin Li * @n: another &struct list_head to use as temporary storage
372*a376eb32SXin Li * @head: the head for your list.
373*a376eb32SXin Li */
374*a376eb32SXin Li #define list_for_each_safe(pos, n, head) \
375*a376eb32SXin Li for (pos = (head)->next, n = pos->next; pos != (head); \
376*a376eb32SXin Li pos = n, n = pos->next)
377*a376eb32SXin Li
378*a376eb32SXin Li /**
379*a376eb32SXin Li * list_for_each_entry - iterate over list of given type
380*a376eb32SXin Li * @pos: the type * to use as a loop counter.
381*a376eb32SXin Li * @head: the head for your list.
382*a376eb32SXin Li * @member: the name of the list_struct within the struct.
383*a376eb32SXin Li */
384*a376eb32SXin Li #define list_for_each_entry(pos, head, member) \
385*a376eb32SXin Li for (pos = list_entry((head)->next, typeof(*pos), member), \
386*a376eb32SXin Li prefetch(pos->member.next); \
387*a376eb32SXin Li &pos->member != (head); \
388*a376eb32SXin Li pos = list_entry(pos->member.next, typeof(*pos), member), \
389*a376eb32SXin Li prefetch(pos->member.next))
390*a376eb32SXin Li
391*a376eb32SXin Li /**
392*a376eb32SXin Li * list_for_each_entry_reverse - iterate backwards over list of given type.
393*a376eb32SXin Li * @pos: the type * to use as a loop counter.
394*a376eb32SXin Li * @head: the head for your list.
395*a376eb32SXin Li * @member: the name of the list_struct within the struct.
396*a376eb32SXin Li */
397*a376eb32SXin Li #define list_for_each_entry_reverse(pos, head, member) \
398*a376eb32SXin Li for (pos = list_entry((head)->prev, typeof(*pos), member), \
399*a376eb32SXin Li prefetch(pos->member.prev); \
400*a376eb32SXin Li &pos->member != (head); \
401*a376eb32SXin Li pos = list_entry(pos->member.prev, typeof(*pos), member), \
402*a376eb32SXin Li prefetch(pos->member.prev))
403*a376eb32SXin Li
404*a376eb32SXin Li /**
405*a376eb32SXin Li * list_prepare_entry - prepare a pos entry for use as a start point in
406*a376eb32SXin Li * list_for_each_entry_continue
407*a376eb32SXin Li * @pos: the type * to use as a start point
408*a376eb32SXin Li * @head: the head of the list
409*a376eb32SXin Li * @member: the name of the list_struct within the struct.
410*a376eb32SXin Li */
411*a376eb32SXin Li #define list_prepare_entry(pos, head, member) \
412*a376eb32SXin Li ((pos) ? : list_entry(head, typeof(*pos), member))
413*a376eb32SXin Li
414*a376eb32SXin Li /**
415*a376eb32SXin Li * list_for_each_entry_continue - iterate over list of given type
416*a376eb32SXin Li * continuing after existing point
417*a376eb32SXin Li * @pos: the type * to use as a loop counter.
418*a376eb32SXin Li * @head: the head for your list.
419*a376eb32SXin Li * @member: the name of the list_struct within the struct.
420*a376eb32SXin Li */
421*a376eb32SXin Li #define list_for_each_entry_continue(pos, head, member) \
422*a376eb32SXin Li for (pos = list_entry(pos->member.next, typeof(*pos), member), \
423*a376eb32SXin Li prefetch(pos->member.next); \
424*a376eb32SXin Li &pos->member != (head); \
425*a376eb32SXin Li pos = list_entry(pos->member.next, typeof(*pos), member), \
426*a376eb32SXin Li prefetch(pos->member.next))
427*a376eb32SXin Li
428*a376eb32SXin Li /**
429*a376eb32SXin Li * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
430*a376eb32SXin Li * @pos: the type * to use as a loop counter.
431*a376eb32SXin Li * @n: another type * to use as temporary storage
432*a376eb32SXin Li * @head: the head for your list.
433*a376eb32SXin Li * @member: the name of the list_struct within the struct.
434*a376eb32SXin Li */
435*a376eb32SXin Li #define list_for_each_entry_safe(pos, n, head, member) \
436*a376eb32SXin Li for (pos = list_entry((head)->next, typeof(*pos), member), \
437*a376eb32SXin Li n = list_entry(pos->member.next, typeof(*pos), member); \
438*a376eb32SXin Li &pos->member != (head); \
439*a376eb32SXin Li pos = n, n = list_entry(n->member.next, typeof(*n), member))
440*a376eb32SXin Li
441*a376eb32SXin Li /**
442*a376eb32SXin Li * list_for_each_rcu - iterate over an rcu-protected list
443*a376eb32SXin Li * @pos: the &struct list_head to use as a loop counter.
444*a376eb32SXin Li * @head: the head for your list.
445*a376eb32SXin Li *
446*a376eb32SXin Li * This list-traversal primitive may safely run concurrently with
447*a376eb32SXin Li * the _rcu list-mutation primitives such as list_add_rcu()
448*a376eb32SXin Li * as long as the traversal is guarded by rcu_read_lock().
449*a376eb32SXin Li */
450*a376eb32SXin Li #define list_for_each_rcu(pos, head) \
451*a376eb32SXin Li for (pos = (head)->next, prefetch(pos->next); pos != (head); \
452*a376eb32SXin Li pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
453*a376eb32SXin Li
454*a376eb32SXin Li #define __list_for_each_rcu(pos, head) \
455*a376eb32SXin Li for (pos = (head)->next; pos != (head); \
456*a376eb32SXin Li pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
457*a376eb32SXin Li
458*a376eb32SXin Li /**
459*a376eb32SXin Li * list_for_each_safe_rcu - iterate over an rcu-protected list safe
460*a376eb32SXin Li * against removal of list entry
461*a376eb32SXin Li * @pos: the &struct list_head to use as a loop counter.
462*a376eb32SXin Li * @n: another &struct list_head to use as temporary storage
463*a376eb32SXin Li * @head: the head for your list.
464*a376eb32SXin Li *
465*a376eb32SXin Li * This list-traversal primitive may safely run concurrently with
466*a376eb32SXin Li * the _rcu list-mutation primitives such as list_add_rcu()
467*a376eb32SXin Li * as long as the traversal is guarded by rcu_read_lock().
468*a376eb32SXin Li */
469*a376eb32SXin Li #define list_for_each_safe_rcu(pos, n, head) \
470*a376eb32SXin Li for (pos = (head)->next, n = pos->next; pos != (head); \
471*a376eb32SXin Li pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
472*a376eb32SXin Li
473*a376eb32SXin Li /**
474*a376eb32SXin Li * list_for_each_entry_rcu - iterate over rcu list of given type
475*a376eb32SXin Li * @pos: the type * to use as a loop counter.
476*a376eb32SXin Li * @head: the head for your list.
477*a376eb32SXin Li * @member: the name of the list_struct within the struct.
478*a376eb32SXin Li *
479*a376eb32SXin Li * This list-traversal primitive may safely run concurrently with
480*a376eb32SXin Li * the _rcu list-mutation primitives such as list_add_rcu()
481*a376eb32SXin Li * as long as the traversal is guarded by rcu_read_lock().
482*a376eb32SXin Li */
483*a376eb32SXin Li #define list_for_each_entry_rcu(pos, head, member) \
484*a376eb32SXin Li for (pos = list_entry((head)->next, typeof(*pos), member), \
485*a376eb32SXin Li prefetch(pos->member.next); \
486*a376eb32SXin Li &pos->member != (head); \
487*a376eb32SXin Li pos = list_entry(pos->member.next, typeof(*pos), member), \
488*a376eb32SXin Li ({ smp_read_barrier_depends(); 0;}), \
489*a376eb32SXin Li prefetch(pos->member.next))
490*a376eb32SXin Li
491*a376eb32SXin Li
492*a376eb32SXin Li /**
493*a376eb32SXin Li * list_for_each_continue_rcu - iterate over an rcu-protected list
494*a376eb32SXin Li * continuing after existing point.
495*a376eb32SXin Li * @pos: the &struct list_head to use as a loop counter.
496*a376eb32SXin Li * @head: the head for your list.
497*a376eb32SXin Li *
498*a376eb32SXin Li * This list-traversal primitive may safely run concurrently with
499*a376eb32SXin Li * the _rcu list-mutation primitives such as list_add_rcu()
500*a376eb32SXin Li * as long as the traversal is guarded by rcu_read_lock().
501*a376eb32SXin Li */
502*a376eb32SXin Li #define list_for_each_continue_rcu(pos, head) \
503*a376eb32SXin Li for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
504*a376eb32SXin Li (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
505*a376eb32SXin Li
506*a376eb32SXin Li /*
507*a376eb32SXin Li * Double linked lists with a single pointer list head.
508*a376eb32SXin Li * Mostly useful for hash tables where the two pointer list head is
509*a376eb32SXin Li * too wasteful.
510*a376eb32SXin Li * You lose the ability to access the tail in O(1).
511*a376eb32SXin Li */
512*a376eb32SXin Li
513*a376eb32SXin Li struct hlist_head {
514*a376eb32SXin Li struct hlist_node *first;
515*a376eb32SXin Li };
516*a376eb32SXin Li
517*a376eb32SXin Li struct hlist_node {
518*a376eb32SXin Li struct hlist_node *next, **pprev;
519*a376eb32SXin Li };
520*a376eb32SXin Li
521*a376eb32SXin Li #define HLIST_HEAD_INIT { .first = NULL }
522*a376eb32SXin Li #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
523*a376eb32SXin Li #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
524*a376eb32SXin Li #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
525*a376eb32SXin Li
hlist_unhashed(const struct hlist_node * h)526*a376eb32SXin Li static inline int hlist_unhashed(const struct hlist_node *h)
527*a376eb32SXin Li {
528*a376eb32SXin Li return !h->pprev;
529*a376eb32SXin Li }
530*a376eb32SXin Li
hlist_empty(const struct hlist_head * h)531*a376eb32SXin Li static inline int hlist_empty(const struct hlist_head *h)
532*a376eb32SXin Li {
533*a376eb32SXin Li return !h->first;
534*a376eb32SXin Li }
535*a376eb32SXin Li
__hlist_del(struct hlist_node * n)536*a376eb32SXin Li static inline void __hlist_del(struct hlist_node *n)
537*a376eb32SXin Li {
538*a376eb32SXin Li struct hlist_node *next = n->next;
539*a376eb32SXin Li struct hlist_node **pprev = n->pprev;
540*a376eb32SXin Li *pprev = next;
541*a376eb32SXin Li if (next)
542*a376eb32SXin Li next->pprev = pprev;
543*a376eb32SXin Li }
544*a376eb32SXin Li
hlist_del(struct hlist_node * n)545*a376eb32SXin Li static inline void hlist_del(struct hlist_node *n)
546*a376eb32SXin Li {
547*a376eb32SXin Li __hlist_del(n);
548*a376eb32SXin Li n->next = LIST_POISON1;
549*a376eb32SXin Li n->pprev = LIST_POISON2;
550*a376eb32SXin Li }
551*a376eb32SXin Li
552*a376eb32SXin Li /**
553*a376eb32SXin Li * hlist_del_rcu - deletes entry from hash list without re-initialization
554*a376eb32SXin Li * @n: the element to delete from the hash list.
555*a376eb32SXin Li *
556*a376eb32SXin Li * Note: list_unhashed() on entry does not return true after this,
557*a376eb32SXin Li * the entry is in an undefined state. It is useful for RCU based
558*a376eb32SXin Li * lockfree traversal.
559*a376eb32SXin Li *
560*a376eb32SXin Li * In particular, it means that we can not poison the forward
561*a376eb32SXin Li * pointers that may still be used for walking the hash list.
562*a376eb32SXin Li *
563*a376eb32SXin Li * The caller must take whatever precautions are necessary
564*a376eb32SXin Li * (such as holding appropriate locks) to avoid racing
565*a376eb32SXin Li * with another list-mutation primitive, such as hlist_add_head_rcu()
566*a376eb32SXin Li * or hlist_del_rcu(), running on this same list.
567*a376eb32SXin Li * However, it is perfectly legal to run concurrently with
568*a376eb32SXin Li * the _rcu list-traversal primitives, such as
569*a376eb32SXin Li * hlist_for_each_entry().
570*a376eb32SXin Li */
hlist_del_rcu(struct hlist_node * n)571*a376eb32SXin Li static inline void hlist_del_rcu(struct hlist_node *n)
572*a376eb32SXin Li {
573*a376eb32SXin Li __hlist_del(n);
574*a376eb32SXin Li n->pprev = LIST_POISON2;
575*a376eb32SXin Li }
576*a376eb32SXin Li
hlist_del_init(struct hlist_node * n)577*a376eb32SXin Li static inline void hlist_del_init(struct hlist_node *n)
578*a376eb32SXin Li {
579*a376eb32SXin Li if (n->pprev) {
580*a376eb32SXin Li __hlist_del(n);
581*a376eb32SXin Li INIT_HLIST_NODE(n);
582*a376eb32SXin Li }
583*a376eb32SXin Li }
584*a376eb32SXin Li
585*a376eb32SXin Li #define hlist_del_rcu_init hlist_del_init
586*a376eb32SXin Li
hlist_add_head(struct hlist_node * n,struct hlist_head * h)587*a376eb32SXin Li static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
588*a376eb32SXin Li {
589*a376eb32SXin Li struct hlist_node *first = h->first;
590*a376eb32SXin Li n->next = first;
591*a376eb32SXin Li if (first)
592*a376eb32SXin Li first->pprev = &n->next;
593*a376eb32SXin Li h->first = n;
594*a376eb32SXin Li n->pprev = &h->first;
595*a376eb32SXin Li }
596*a376eb32SXin Li
597*a376eb32SXin Li
598*a376eb32SXin Li /**
599*a376eb32SXin Li * hlist_add_head_rcu - adds the specified element to the specified hlist,
600*a376eb32SXin Li * while permitting racing traversals.
601*a376eb32SXin Li * @n: the element to add to the hash list.
602*a376eb32SXin Li * @h: the list to add to.
603*a376eb32SXin Li *
604*a376eb32SXin Li * The caller must take whatever precautions are necessary
605*a376eb32SXin Li * (such as holding appropriate locks) to avoid racing
606*a376eb32SXin Li * with another list-mutation primitive, such as hlist_add_head_rcu()
607*a376eb32SXin Li * or hlist_del_rcu(), running on this same list.
608*a376eb32SXin Li * However, it is perfectly legal to run concurrently with
609*a376eb32SXin Li * the _rcu list-traversal primitives, such as
610*a376eb32SXin Li * hlist_for_each_entry(), but only if smp_read_barrier_depends()
611*a376eb32SXin Li * is used to prevent memory-consistency problems on Alpha CPUs.
612*a376eb32SXin Li * Regardless of the type of CPU, the list-traversal primitive
613*a376eb32SXin Li * must be guarded by rcu_read_lock().
614*a376eb32SXin Li *
615*a376eb32SXin Li * OK, so why don't we have an hlist_for_each_entry_rcu()???
616*a376eb32SXin Li */
hlist_add_head_rcu(struct hlist_node * n,struct hlist_head * h)617*a376eb32SXin Li static inline void hlist_add_head_rcu(struct hlist_node *n,
618*a376eb32SXin Li struct hlist_head *h)
619*a376eb32SXin Li {
620*a376eb32SXin Li struct hlist_node *first = h->first;
621*a376eb32SXin Li n->next = first;
622*a376eb32SXin Li n->pprev = &h->first;
623*a376eb32SXin Li smp_wmb();
624*a376eb32SXin Li if (first)
625*a376eb32SXin Li first->pprev = &n->next;
626*a376eb32SXin Li h->first = n;
627*a376eb32SXin Li }
628*a376eb32SXin Li
629*a376eb32SXin Li /* next must be != NULL */
hlist_add_before(struct hlist_node * n,struct hlist_node * next)630*a376eb32SXin Li static inline void hlist_add_before(struct hlist_node *n,
631*a376eb32SXin Li struct hlist_node *next)
632*a376eb32SXin Li {
633*a376eb32SXin Li n->pprev = next->pprev;
634*a376eb32SXin Li n->next = next;
635*a376eb32SXin Li next->pprev = &n->next;
636*a376eb32SXin Li *(n->pprev) = n;
637*a376eb32SXin Li }
638*a376eb32SXin Li
hlist_add_after(struct hlist_node * n,struct hlist_node * next)639*a376eb32SXin Li static inline void hlist_add_after(struct hlist_node *n,
640*a376eb32SXin Li struct hlist_node *next)
641*a376eb32SXin Li {
642*a376eb32SXin Li next->next = n->next;
643*a376eb32SXin Li n->next = next;
644*a376eb32SXin Li next->pprev = &n->next;
645*a376eb32SXin Li
646*a376eb32SXin Li if(next->next)
647*a376eb32SXin Li next->next->pprev = &next->next;
648*a376eb32SXin Li }
649*a376eb32SXin Li
650*a376eb32SXin Li #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
651*a376eb32SXin Li
652*a376eb32SXin Li #define hlist_for_each(pos, head) \
653*a376eb32SXin Li for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
654*a376eb32SXin Li pos = pos->next)
655*a376eb32SXin Li
656*a376eb32SXin Li #define hlist_for_each_safe(pos, n, head) \
657*a376eb32SXin Li for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
658*a376eb32SXin Li pos = n)
659*a376eb32SXin Li
660*a376eb32SXin Li /**
661*a376eb32SXin Li * hlist_for_each_entry - iterate over list of given type
662*a376eb32SXin Li * @tpos: the type * to use as a loop counter.
663*a376eb32SXin Li * @pos: the &struct hlist_node to use as a loop counter.
664*a376eb32SXin Li * @head: the head for your list.
665*a376eb32SXin Li * @member: the name of the hlist_node within the struct.
666*a376eb32SXin Li */
667*a376eb32SXin Li #define hlist_for_each_entry(tpos, pos, head, member) \
668*a376eb32SXin Li for (pos = (head)->first; \
669*a376eb32SXin Li pos && ({ prefetch(pos->next); 1;}) && \
670*a376eb32SXin Li ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
671*a376eb32SXin Li pos = pos->next)
672*a376eb32SXin Li
673*a376eb32SXin Li /**
674*a376eb32SXin Li * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
675*a376eb32SXin Li * @tpos: the type * to use as a loop counter.
676*a376eb32SXin Li * @pos: the &struct hlist_node to use as a loop counter.
677*a376eb32SXin Li * @member: the name of the hlist_node within the struct.
678*a376eb32SXin Li */
679*a376eb32SXin Li #define hlist_for_each_entry_continue(tpos, pos, member) \
680*a376eb32SXin Li for (pos = (pos)->next; \
681*a376eb32SXin Li pos && ({ prefetch(pos->next); 1;}) && \
682*a376eb32SXin Li ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
683*a376eb32SXin Li pos = pos->next)
684*a376eb32SXin Li
685*a376eb32SXin Li /**
686*a376eb32SXin Li * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
687*a376eb32SXin Li * @tpos: the type * to use as a loop counter.
688*a376eb32SXin Li * @pos: the &struct hlist_node to use as a loop counter.
689*a376eb32SXin Li * @member: the name of the hlist_node within the struct.
690*a376eb32SXin Li */
691*a376eb32SXin Li #define hlist_for_each_entry_from(tpos, pos, member) \
692*a376eb32SXin Li for (; pos && ({ prefetch(pos->next); 1;}) && \
693*a376eb32SXin Li ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
694*a376eb32SXin Li pos = pos->next)
695*a376eb32SXin Li
696*a376eb32SXin Li /**
697*a376eb32SXin Li * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
698*a376eb32SXin Li * @tpos: the type * to use as a loop counter.
699*a376eb32SXin Li * @pos: the &struct hlist_node to use as a loop counter.
700*a376eb32SXin Li * @n: another &struct hlist_node to use as temporary storage
701*a376eb32SXin Li * @head: the head for your list.
702*a376eb32SXin Li * @member: the name of the hlist_node within the struct.
703*a376eb32SXin Li */
704*a376eb32SXin Li #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
705*a376eb32SXin Li for (pos = (head)->first; \
706*a376eb32SXin Li pos && ({ n = pos->next; 1; }) && \
707*a376eb32SXin Li ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
708*a376eb32SXin Li pos = n)
709*a376eb32SXin Li
710*a376eb32SXin Li /**
711*a376eb32SXin Li * hlist_for_each_entry_rcu - iterate over rcu list of given type
712*a376eb32SXin Li * @pos: the type * to use as a loop counter.
713*a376eb32SXin Li * @pos: the &struct hlist_node to use as a loop counter.
714*a376eb32SXin Li * @head: the head for your list.
715*a376eb32SXin Li * @member: the name of the hlist_node within the struct.
716*a376eb32SXin Li *
717*a376eb32SXin Li * This list-traversal primitive may safely run concurrently with
718*a376eb32SXin Li * the _rcu list-mutation primitives such as hlist_add_rcu()
719*a376eb32SXin Li * as long as the traversal is guarded by rcu_read_lock().
720*a376eb32SXin Li */
721*a376eb32SXin Li #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
722*a376eb32SXin Li for (pos = (head)->first; \
723*a376eb32SXin Li pos && ({ prefetch(pos->next); 1;}) && \
724*a376eb32SXin Li ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
725*a376eb32SXin Li pos = pos->next, ({ smp_read_barrier_depends(); 0; }) )
726*a376eb32SXin Li
727*a376eb32SXin Li #endif
728