]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #ifndef _LINUX_LIST_H |
2 | #define _LINUX_LIST_H | |
3 | ||
4 | #ifdef __KERNEL__ | |
5 | ||
6 | #include <linux/stddef.h> | |
c9cf5528 | 7 | #include <linux/poison.h> |
1da177e4 LT |
8 | #include <linux/prefetch.h> |
9 | #include <asm/system.h> | |
10 | ||
1da177e4 LT |
11 | /* |
12 | * Simple doubly linked list implementation. | |
13 | * | |
14 | * Some of the internal functions ("__xxx") are useful when | |
15 | * manipulating whole lists rather than single entries, as | |
16 | * sometimes we already know the next/prev entries and we can | |
17 | * generate better code by using them directly rather than | |
18 | * using the generic single-entry routines. | |
19 | */ | |
20 | ||
21 | struct list_head { | |
22 | struct list_head *next, *prev; | |
23 | }; | |
24 | ||
25 | #define LIST_HEAD_INIT(name) { &(name), &(name) } | |
26 | ||
27 | #define LIST_HEAD(name) \ | |
28 | struct list_head name = LIST_HEAD_INIT(name) | |
29 | ||
490d6ab1 ZB |
30 | static inline void INIT_LIST_HEAD(struct list_head *list) |
31 | { | |
32 | list->next = list; | |
33 | list->prev = list; | |
34 | } | |
1da177e4 LT |
35 | |
36 | /* | |
37 | * Insert a new entry between two known consecutive entries. | |
38 | * | |
39 | * This is only for internal list manipulation where we know | |
40 | * the prev/next entries already! | |
41 | */ | |
199a9afc | 42 | #ifndef CONFIG_DEBUG_LIST |
1da177e4 LT |
43 | static inline void __list_add(struct list_head *new, |
44 | struct list_head *prev, | |
45 | struct list_head *next) | |
46 | { | |
47 | next->prev = new; | |
48 | new->next = next; | |
49 | new->prev = prev; | |
50 | prev->next = new; | |
51 | } | |
199a9afc DJ |
52 | #else |
53 | extern void __list_add(struct list_head *new, | |
54 | struct list_head *prev, | |
55 | struct list_head *next); | |
56 | #endif | |
1da177e4 LT |
57 | |
58 | /** | |
59 | * list_add - add a new entry | |
60 | * @new: new entry to be added | |
61 | * @head: list head to add it after | |
62 | * | |
63 | * Insert a new entry after the specified head. | |
64 | * This is good for implementing stacks. | |
65 | */ | |
199a9afc | 66 | #ifndef CONFIG_DEBUG_LIST |
1da177e4 LT |
67 | static inline void list_add(struct list_head *new, struct list_head *head) |
68 | { | |
69 | __list_add(new, head, head->next); | |
70 | } | |
199a9afc DJ |
71 | #else |
72 | extern void list_add(struct list_head *new, struct list_head *head); | |
73 | #endif | |
74 | ||
1da177e4 LT |
75 | |
76 | /** | |
77 | * list_add_tail - add a new entry | |
78 | * @new: new entry to be added | |
79 | * @head: list head to add it before | |
80 | * | |
81 | * Insert a new entry before the specified head. | |
82 | * This is useful for implementing queues. | |
83 | */ | |
84 | static inline void list_add_tail(struct list_head *new, struct list_head *head) | |
85 | { | |
86 | __list_add(new, head->prev, head); | |
87 | } | |
88 | ||
89 | /* | |
90 | * Insert a new entry between two known consecutive entries. | |
91 | * | |
92 | * This is only for internal list manipulation where we know | |
93 | * the prev/next entries already! | |
94 | */ | |
95 | static inline void __list_add_rcu(struct list_head * new, | |
96 | struct list_head * prev, struct list_head * next) | |
97 | { | |
98 | new->next = next; | |
99 | new->prev = prev; | |
100 | smp_wmb(); | |
101 | next->prev = new; | |
102 | prev->next = new; | |
103 | } | |
104 | ||
105 | /** | |
106 | * list_add_rcu - add a new entry to rcu-protected list | |
107 | * @new: new entry to be added | |
108 | * @head: list head to add it after | |
109 | * | |
110 | * Insert a new entry after the specified head. | |
111 | * This is good for implementing stacks. | |
112 | * | |
113 | * The caller must take whatever precautions are necessary | |
114 | * (such as holding appropriate locks) to avoid racing | |
115 | * with another list-mutation primitive, such as list_add_rcu() | |
116 | * or list_del_rcu(), running on this same list. | |
117 | * However, it is perfectly legal to run concurrently with | |
118 | * the _rcu list-traversal primitives, such as | |
119 | * list_for_each_entry_rcu(). | |
120 | */ | |
121 | static inline void list_add_rcu(struct list_head *new, struct list_head *head) | |
122 | { | |
123 | __list_add_rcu(new, head, head->next); | |
124 | } | |
125 | ||
126 | /** | |
127 | * list_add_tail_rcu - add a new entry to rcu-protected list | |
128 | * @new: new entry to be added | |
129 | * @head: list head to add it before | |
130 | * | |
131 | * Insert a new entry before the specified head. | |
132 | * This is useful for implementing queues. | |
133 | * | |
134 | * The caller must take whatever precautions are necessary | |
135 | * (such as holding appropriate locks) to avoid racing | |
136 | * with another list-mutation primitive, such as list_add_tail_rcu() | |
137 | * or list_del_rcu(), running on this same list. | |
138 | * However, it is perfectly legal to run concurrently with | |
139 | * the _rcu list-traversal primitives, such as | |
140 | * list_for_each_entry_rcu(). | |
141 | */ | |
142 | static inline void list_add_tail_rcu(struct list_head *new, | |
143 | struct list_head *head) | |
144 | { | |
145 | __list_add_rcu(new, head->prev, head); | |
146 | } | |
147 | ||
148 | /* | |
149 | * Delete a list entry by making the prev/next entries | |
150 | * point to each other. | |
151 | * | |
152 | * This is only for internal list manipulation where we know | |
153 | * the prev/next entries already! | |
154 | */ | |
155 | static inline void __list_del(struct list_head * prev, struct list_head * next) | |
156 | { | |
157 | next->prev = prev; | |
158 | prev->next = next; | |
159 | } | |
160 | ||
161 | /** | |
162 | * list_del - deletes entry from list. | |
163 | * @entry: the element to delete from the list. | |
72fd4a35 | 164 | * Note: list_empty() on entry does not return true after this, the entry is |
1da177e4 LT |
165 | * in an undefined state. |
166 | */ | |
199a9afc | 167 | #ifndef CONFIG_DEBUG_LIST |
1da177e4 LT |
168 | static inline void list_del(struct list_head *entry) |
169 | { | |
170 | __list_del(entry->prev, entry->next); | |
171 | entry->next = LIST_POISON1; | |
172 | entry->prev = LIST_POISON2; | |
173 | } | |
199a9afc DJ |
174 | #else |
175 | extern void list_del(struct list_head *entry); | |
176 | #endif | |
1da177e4 LT |
177 | |
178 | /** | |
179 | * list_del_rcu - deletes entry from list without re-initialization | |
180 | * @entry: the element to delete from the list. | |
181 | * | |
72fd4a35 | 182 | * Note: list_empty() on entry does not return true after this, |
1da177e4 LT |
183 | * the entry is in an undefined state. It is useful for RCU based |
184 | * lockfree traversal. | |
185 | * | |
186 | * In particular, it means that we can not poison the forward | |
187 | * pointers that may still be used for walking the list. | |
188 | * | |
189 | * The caller must take whatever precautions are necessary | |
190 | * (such as holding appropriate locks) to avoid racing | |
191 | * with another list-mutation primitive, such as list_del_rcu() | |
192 | * or list_add_rcu(), running on this same list. | |
193 | * However, it is perfectly legal to run concurrently with | |
194 | * the _rcu list-traversal primitives, such as | |
195 | * list_for_each_entry_rcu(). | |
196 | * | |
197 | * Note that the caller is not permitted to immediately free | |
b2b18660 | 198 | * the newly deleted entry. Instead, either synchronize_rcu() |
1da177e4 LT |
199 | * or call_rcu() must be used to defer freeing until an RCU |
200 | * grace period has elapsed. | |
201 | */ | |
202 | static inline void list_del_rcu(struct list_head *entry) | |
203 | { | |
204 | __list_del(entry->prev, entry->next); | |
205 | entry->prev = LIST_POISON2; | |
206 | } | |
207 | ||
54e73770 ON |
208 | /** |
209 | * list_replace - replace old entry by new one | |
210 | * @old : the element to be replaced | |
211 | * @new : the new element to insert | |
72fd4a35 RD |
212 | * |
213 | * If @old was empty, it will be overwritten. | |
54e73770 ON |
214 | */ |
215 | static inline void list_replace(struct list_head *old, | |
216 | struct list_head *new) | |
217 | { | |
218 | new->next = old->next; | |
219 | new->next->prev = new; | |
220 | new->prev = old->prev; | |
221 | new->prev->next = new; | |
222 | } | |
223 | ||
224 | static inline void list_replace_init(struct list_head *old, | |
225 | struct list_head *new) | |
226 | { | |
227 | list_replace(old, new); | |
228 | INIT_LIST_HEAD(old); | |
229 | } | |
230 | ||
45f8bde0 | 231 | /** |
1da177e4 LT |
232 | * list_replace_rcu - replace old entry by new one |
233 | * @old : the element to be replaced | |
234 | * @new : the new element to insert | |
235 | * | |
45f8bde0 RD |
236 | * The @old entry will be replaced with the @new entry atomically. |
237 | * Note: @old should not be empty. | |
1da177e4 | 238 | */ |
b88cb424 IM |
239 | static inline void list_replace_rcu(struct list_head *old, |
240 | struct list_head *new) | |
241 | { | |
1da177e4 LT |
242 | new->next = old->next; |
243 | new->prev = old->prev; | |
244 | smp_wmb(); | |
245 | new->next->prev = new; | |
246 | new->prev->next = new; | |
b88cb424 | 247 | old->prev = LIST_POISON2; |
1da177e4 LT |
248 | } |
249 | ||
250 | /** | |
251 | * list_del_init - deletes entry from list and reinitialize it. | |
252 | * @entry: the element to delete from the list. | |
253 | */ | |
254 | static inline void list_del_init(struct list_head *entry) | |
255 | { | |
256 | __list_del(entry->prev, entry->next); | |
257 | INIT_LIST_HEAD(entry); | |
258 | } | |
259 | ||
260 | /** | |
261 | * list_move - delete from one list and add as another's head | |
262 | * @list: the entry to move | |
263 | * @head: the head that will precede our entry | |
264 | */ | |
265 | static inline void list_move(struct list_head *list, struct list_head *head) | |
266 | { | |
267 | __list_del(list->prev, list->next); | |
268 | list_add(list, head); | |
269 | } | |
270 | ||
271 | /** | |
272 | * list_move_tail - delete from one list and add as another's tail | |
273 | * @list: the entry to move | |
274 | * @head: the head that will follow our entry | |
275 | */ | |
276 | static inline void list_move_tail(struct list_head *list, | |
277 | struct list_head *head) | |
278 | { | |
279 | __list_del(list->prev, list->next); | |
280 | list_add_tail(list, head); | |
281 | } | |
282 | ||
e8f4d97e SN |
283 | /** |
284 | * list_is_last - tests whether @list is the last entry in list @head | |
285 | * @list: the entry to test | |
286 | * @head: the head of the list | |
287 | */ | |
288 | static inline int list_is_last(const struct list_head *list, | |
289 | const struct list_head *head) | |
290 | { | |
291 | return list->next == head; | |
292 | } | |
293 | ||
1da177e4 LT |
294 | /** |
295 | * list_empty - tests whether a list is empty | |
296 | * @head: the list to test. | |
297 | */ | |
298 | static inline int list_empty(const struct list_head *head) | |
299 | { | |
300 | return head->next == head; | |
301 | } | |
302 | ||
303 | /** | |
fe96e57d RD |
304 | * list_empty_careful - tests whether a list is empty and not being modified |
305 | * @head: the list to test | |
306 | * | |
307 | * Description: | |
308 | * tests whether a list is empty _and_ checks that no other CPU might be | |
309 | * in the process of modifying either member (next or prev) | |
1da177e4 LT |
310 | * |
311 | * NOTE: using list_empty_careful() without synchronization | |
312 | * can only be safe if the only activity that can happen | |
313 | * to the list entry is list_del_init(). Eg. it cannot be used | |
314 | * if another CPU could re-list_add() it. | |
1da177e4 LT |
315 | */ |
316 | static inline int list_empty_careful(const struct list_head *head) | |
317 | { | |
318 | struct list_head *next = head->next; | |
319 | return (next == head) && (next == head->prev); | |
320 | } | |
321 | ||
322 | static inline void __list_splice(struct list_head *list, | |
323 | struct list_head *head) | |
324 | { | |
325 | struct list_head *first = list->next; | |
326 | struct list_head *last = list->prev; | |
327 | struct list_head *at = head->next; | |
328 | ||
329 | first->prev = head; | |
330 | head->next = first; | |
331 | ||
332 | last->next = at; | |
333 | at->prev = last; | |
334 | } | |
335 | ||
336 | /** | |
337 | * list_splice - join two lists | |
338 | * @list: the new list to add. | |
339 | * @head: the place to add it in the first list. | |
340 | */ | |
341 | static inline void list_splice(struct list_head *list, struct list_head *head) | |
342 | { | |
343 | if (!list_empty(list)) | |
344 | __list_splice(list, head); | |
345 | } | |
346 | ||
347 | /** | |
348 | * list_splice_init - join two lists and reinitialise the emptied list. | |
349 | * @list: the new list to add. | |
350 | * @head: the place to add it in the first list. | |
351 | * | |
352 | * The list at @list is reinitialised | |
353 | */ | |
354 | static inline void list_splice_init(struct list_head *list, | |
355 | struct list_head *head) | |
356 | { | |
357 | if (!list_empty(list)) { | |
358 | __list_splice(list, head); | |
359 | INIT_LIST_HEAD(list); | |
360 | } | |
361 | } | |
362 | ||
3678d62f CM |
363 | /** |
364 | * list_splice_init_rcu - splice an RCU-protected list into an existing list. | |
365 | * @list: the RCU-protected list to splice | |
366 | * @head: the place in the list to splice the first list into | |
367 | * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ... | |
368 | * | |
369 | * @head can be RCU-read traversed concurrently with this function. | |
370 | * | |
371 | * Note that this function blocks. | |
372 | * | |
373 | * Important note: the caller must take whatever action is necessary to | |
374 | * prevent any other updates to @head. In principle, it is possible | |
375 | * to modify the list as soon as sync() begins execution. | |
376 | * If this sort of thing becomes necessary, an alternative version | |
377 | * based on call_rcu() could be created. But only if -really- | |
378 | * needed -- there is no shortage of RCU API members. | |
379 | */ | |
380 | static inline void list_splice_init_rcu(struct list_head *list, | |
381 | struct list_head *head, | |
382 | void (*sync)(void)) | |
383 | { | |
384 | struct list_head *first = list->next; | |
385 | struct list_head *last = list->prev; | |
386 | struct list_head *at = head->next; | |
387 | ||
388 | if (list_empty(head)) | |
389 | return; | |
390 | ||
391 | /* "first" and "last" tracking list, so initialize it. */ | |
392 | ||
393 | INIT_LIST_HEAD(list); | |
394 | ||
395 | /* | |
396 | * At this point, the list body still points to the source list. | |
397 | * Wait for any readers to finish using the list before splicing | |
398 | * the list body into the new list. Any new readers will see | |
399 | * an empty list. | |
400 | */ | |
401 | ||
402 | sync(); | |
403 | ||
404 | /* | |
405 | * Readers are finished with the source list, so perform splice. | |
406 | * The order is important if the new list is global and accessible | |
407 | * to concurrent RCU readers. Note that RCU readers are not | |
408 | * permitted to traverse the prev pointers without excluding | |
409 | * this function. | |
410 | */ | |
411 | ||
412 | last->next = at; | |
413 | smp_wmb(); | |
414 | head->next = first; | |
415 | first->prev = head; | |
416 | at->prev = last; | |
417 | } | |
418 | ||
1da177e4 LT |
419 | /** |
420 | * list_entry - get the struct for this entry | |
421 | * @ptr: the &struct list_head pointer. | |
422 | * @type: the type of the struct this is embedded in. | |
423 | * @member: the name of the list_struct within the struct. | |
424 | */ | |
425 | #define list_entry(ptr, type, member) \ | |
426 | container_of(ptr, type, member) | |
427 | ||
428 | /** | |
429 | * list_for_each - iterate over a list | |
8e3a67a9 | 430 | * @pos: the &struct list_head to use as a loop cursor. |
1da177e4 LT |
431 | * @head: the head for your list. |
432 | */ | |
433 | #define list_for_each(pos, head) \ | |
434 | for (pos = (head)->next; prefetch(pos->next), pos != (head); \ | |
435 | pos = pos->next) | |
436 | ||
437 | /** | |
438 | * __list_for_each - iterate over a list | |
8e3a67a9 | 439 | * @pos: the &struct list_head to use as a loop cursor. |
1da177e4 LT |
440 | * @head: the head for your list. |
441 | * | |
442 | * This variant differs from list_for_each() in that it's the | |
443 | * simplest possible list iteration code, no prefetching is done. | |
444 | * Use this for code that knows the list to be very short (empty | |
445 | * or 1 entry) most of the time. | |
446 | */ | |
447 | #define __list_for_each(pos, head) \ | |
448 | for (pos = (head)->next; pos != (head); pos = pos->next) | |
449 | ||
450 | /** | |
451 | * list_for_each_prev - iterate over a list backwards | |
8e3a67a9 | 452 | * @pos: the &struct list_head to use as a loop cursor. |
1da177e4 LT |
453 | * @head: the head for your list. |
454 | */ | |
455 | #define list_for_each_prev(pos, head) \ | |
456 | for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ | |
457 | pos = pos->prev) | |
458 | ||
459 | /** | |
fe96e57d | 460 | * list_for_each_safe - iterate over a list safe against removal of list entry |
8e3a67a9 | 461 | * @pos: the &struct list_head to use as a loop cursor. |
1da177e4 LT |
462 | * @n: another &struct list_head to use as temporary storage |
463 | * @head: the head for your list. | |
464 | */ | |
465 | #define list_for_each_safe(pos, n, head) \ | |
466 | for (pos = (head)->next, n = pos->next; pos != (head); \ | |
467 | pos = n, n = pos->next) | |
468 | ||
469 | /** | |
470 | * list_for_each_entry - iterate over list of given type | |
8e3a67a9 | 471 | * @pos: the type * to use as a loop cursor. |
1da177e4 LT |
472 | * @head: the head for your list. |
473 | * @member: the name of the list_struct within the struct. | |
474 | */ | |
475 | #define list_for_each_entry(pos, head, member) \ | |
476 | for (pos = list_entry((head)->next, typeof(*pos), member); \ | |
477 | prefetch(pos->member.next), &pos->member != (head); \ | |
478 | pos = list_entry(pos->member.next, typeof(*pos), member)) | |
479 | ||
480 | /** | |
481 | * list_for_each_entry_reverse - iterate backwards over list of given type. | |
8e3a67a9 | 482 | * @pos: the type * to use as a loop cursor. |
1da177e4 LT |
483 | * @head: the head for your list. |
484 | * @member: the name of the list_struct within the struct. | |
485 | */ | |
486 | #define list_for_each_entry_reverse(pos, head, member) \ | |
487 | for (pos = list_entry((head)->prev, typeof(*pos), member); \ | |
488 | prefetch(pos->member.prev), &pos->member != (head); \ | |
489 | pos = list_entry(pos->member.prev, typeof(*pos), member)) | |
490 | ||
491 | /** | |
72fd4a35 | 492 | * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() |
1da177e4 LT |
493 | * @pos: the type * to use as a start point |
494 | * @head: the head of the list | |
495 | * @member: the name of the list_struct within the struct. | |
fe96e57d | 496 | * |
72fd4a35 | 497 | * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). |
1da177e4 LT |
498 | */ |
499 | #define list_prepare_entry(pos, head, member) \ | |
500 | ((pos) ? : list_entry(head, typeof(*pos), member)) | |
501 | ||
502 | /** | |
fe96e57d | 503 | * list_for_each_entry_continue - continue iteration over list of given type |
8e3a67a9 | 504 | * @pos: the type * to use as a loop cursor. |
1da177e4 LT |
505 | * @head: the head for your list. |
506 | * @member: the name of the list_struct within the struct. | |
fe96e57d RD |
507 | * |
508 | * Continue to iterate over list of given type, continuing after | |
509 | * the current position. | |
1da177e4 LT |
510 | */ |
511 | #define list_for_each_entry_continue(pos, head, member) \ | |
512 | for (pos = list_entry(pos->member.next, typeof(*pos), member); \ | |
513 | prefetch(pos->member.next), &pos->member != (head); \ | |
514 | pos = list_entry(pos->member.next, typeof(*pos), member)) | |
515 | ||
e229c2fb | 516 | /** |
fe96e57d | 517 | * list_for_each_entry_from - iterate over list of given type from the current point |
8e3a67a9 | 518 | * @pos: the type * to use as a loop cursor. |
e229c2fb ACM |
519 | * @head: the head for your list. |
520 | * @member: the name of the list_struct within the struct. | |
fe96e57d RD |
521 | * |
522 | * Iterate over list of given type, continuing from current position. | |
e229c2fb ACM |
523 | */ |
524 | #define list_for_each_entry_from(pos, head, member) \ | |
525 | for (; prefetch(pos->member.next), &pos->member != (head); \ | |
526 | pos = list_entry(pos->member.next, typeof(*pos), member)) | |
527 | ||
1da177e4 LT |
528 | /** |
529 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry | |
8e3a67a9 | 530 | * @pos: the type * to use as a loop cursor. |
1da177e4 LT |
531 | * @n: another type * to use as temporary storage |
532 | * @head: the head for your list. | |
533 | * @member: the name of the list_struct within the struct. | |
534 | */ | |
535 | #define list_for_each_entry_safe(pos, n, head, member) \ | |
536 | for (pos = list_entry((head)->next, typeof(*pos), member), \ | |
537 | n = list_entry(pos->member.next, typeof(*pos), member); \ | |
538 | &pos->member != (head); \ | |
539 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |
540 | ||
74459dc7 | 541 | /** |
fe96e57d | 542 | * list_for_each_entry_safe_continue |
8e3a67a9 | 543 | * @pos: the type * to use as a loop cursor. |
74459dc7 ACM |
544 | * @n: another type * to use as temporary storage |
545 | * @head: the head for your list. | |
546 | * @member: the name of the list_struct within the struct. | |
fe96e57d RD |
547 | * |
548 | * Iterate over list of given type, continuing after current point, | |
549 | * safe against removal of list entry. | |
74459dc7 ACM |
550 | */ |
551 | #define list_for_each_entry_safe_continue(pos, n, head, member) \ | |
8c60f3fa ACM |
552 | for (pos = list_entry(pos->member.next, typeof(*pos), member), \ |
553 | n = list_entry(pos->member.next, typeof(*pos), member); \ | |
d8dcffee ACM |
554 | &pos->member != (head); \ |
555 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |
556 | ||
557 | /** | |
fe96e57d | 558 | * list_for_each_entry_safe_from |
8e3a67a9 | 559 | * @pos: the type * to use as a loop cursor. |
d8dcffee ACM |
560 | * @n: another type * to use as temporary storage |
561 | * @head: the head for your list. | |
562 | * @member: the name of the list_struct within the struct. | |
fe96e57d RD |
563 | * |
564 | * Iterate over list of given type from current point, safe against | |
565 | * removal of list entry. | |
d8dcffee ACM |
566 | */ |
567 | #define list_for_each_entry_safe_from(pos, n, head, member) \ | |
568 | for (n = list_entry(pos->member.next, typeof(*pos), member); \ | |
74459dc7 ACM |
569 | &pos->member != (head); \ |
570 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |
571 | ||
0ad42352 | 572 | /** |
fe96e57d | 573 | * list_for_each_entry_safe_reverse |
8e3a67a9 | 574 | * @pos: the type * to use as a loop cursor. |
0ad42352 DH |
575 | * @n: another type * to use as temporary storage |
576 | * @head: the head for your list. | |
577 | * @member: the name of the list_struct within the struct. | |
fe96e57d RD |
578 | * |
579 | * Iterate backwards over list of given type, safe against removal | |
580 | * of list entry. | |
0ad42352 DH |
581 | */ |
582 | #define list_for_each_entry_safe_reverse(pos, n, head, member) \ | |
583 | for (pos = list_entry((head)->prev, typeof(*pos), member), \ | |
584 | n = list_entry(pos->member.prev, typeof(*pos), member); \ | |
585 | &pos->member != (head); \ | |
586 | pos = n, n = list_entry(n->member.prev, typeof(*n), member)) | |
587 | ||
1da177e4 LT |
588 | /** |
589 | * list_for_each_rcu - iterate over an rcu-protected list | |
8e3a67a9 | 590 | * @pos: the &struct list_head to use as a loop cursor. |
1da177e4 LT |
591 | * @head: the head for your list. |
592 | * | |
593 | * This list-traversal primitive may safely run concurrently with | |
594 | * the _rcu list-mutation primitives such as list_add_rcu() | |
595 | * as long as the traversal is guarded by rcu_read_lock(). | |
596 | */ | |
597 | #define list_for_each_rcu(pos, head) \ | |
b24d18aa HX |
598 | for (pos = (head)->next; \ |
599 | prefetch(rcu_dereference(pos)->next), pos != (head); \ | |
600 | pos = pos->next) | |
1da177e4 LT |
601 | |
602 | #define __list_for_each_rcu(pos, head) \ | |
b24d18aa HX |
603 | for (pos = (head)->next; \ |
604 | rcu_dereference(pos) != (head); \ | |
605 | pos = pos->next) | |
1da177e4 LT |
606 | |
607 | /** | |
fe96e57d | 608 | * list_for_each_safe_rcu |
8e3a67a9 | 609 | * @pos: the &struct list_head to use as a loop cursor. |
1da177e4 LT |
610 | * @n: another &struct list_head to use as temporary storage |
611 | * @head: the head for your list. | |
612 | * | |
fe96e57d RD |
613 | * Iterate over an rcu-protected list, safe against removal of list entry. |
614 | * | |
1da177e4 LT |
615 | * This list-traversal primitive may safely run concurrently with |
616 | * the _rcu list-mutation primitives such as list_add_rcu() | |
617 | * as long as the traversal is guarded by rcu_read_lock(). | |
618 | */ | |
619 | #define list_for_each_safe_rcu(pos, n, head) \ | |
b24d18aa HX |
620 | for (pos = (head)->next; \ |
621 | n = rcu_dereference(pos)->next, pos != (head); \ | |
622 | pos = n) | |
1da177e4 LT |
623 | |
624 | /** | |
625 | * list_for_each_entry_rcu - iterate over rcu list of given type | |
8e3a67a9 | 626 | * @pos: the type * to use as a loop cursor. |
1da177e4 LT |
627 | * @head: the head for your list. |
628 | * @member: the name of the list_struct within the struct. | |
629 | * | |
630 | * This list-traversal primitive may safely run concurrently with | |
631 | * the _rcu list-mutation primitives such as list_add_rcu() | |
632 | * as long as the traversal is guarded by rcu_read_lock(). | |
633 | */ | |
b24d18aa HX |
634 | #define list_for_each_entry_rcu(pos, head, member) \ |
635 | for (pos = list_entry((head)->next, typeof(*pos), member); \ | |
636 | prefetch(rcu_dereference(pos)->member.next), \ | |
637 | &pos->member != (head); \ | |
638 | pos = list_entry(pos->member.next, typeof(*pos), member)) | |
1da177e4 LT |
639 | |
640 | ||
641 | /** | |
fe96e57d | 642 | * list_for_each_continue_rcu |
8e3a67a9 | 643 | * @pos: the &struct list_head to use as a loop cursor. |
1da177e4 LT |
644 | * @head: the head for your list. |
645 | * | |
fe96e57d RD |
646 | * Iterate over an rcu-protected list, continuing after current point. |
647 | * | |
1da177e4 LT |
648 | * This list-traversal primitive may safely run concurrently with |
649 | * the _rcu list-mutation primitives such as list_add_rcu() | |
650 | * as long as the traversal is guarded by rcu_read_lock(). | |
651 | */ | |
652 | #define list_for_each_continue_rcu(pos, head) \ | |
b24d18aa HX |
653 | for ((pos) = (pos)->next; \ |
654 | prefetch(rcu_dereference((pos))->next), (pos) != (head); \ | |
655 | (pos) = (pos)->next) | |
1da177e4 LT |
656 | |
657 | /* | |
658 | * Double linked lists with a single pointer list head. | |
659 | * Mostly useful for hash tables where the two pointer list head is | |
660 | * too wasteful. | |
661 | * You lose the ability to access the tail in O(1). | |
662 | */ | |
663 | ||
664 | struct hlist_head { | |
665 | struct hlist_node *first; | |
666 | }; | |
667 | ||
668 | struct hlist_node { | |
669 | struct hlist_node *next, **pprev; | |
670 | }; | |
671 | ||
672 | #define HLIST_HEAD_INIT { .first = NULL } | |
673 | #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } | |
674 | #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) | |
490d6ab1 ZB |
675 | static inline void INIT_HLIST_NODE(struct hlist_node *h) |
676 | { | |
677 | h->next = NULL; | |
678 | h->pprev = NULL; | |
679 | } | |
1da177e4 LT |
680 | |
681 | static inline int hlist_unhashed(const struct hlist_node *h) | |
682 | { | |
683 | return !h->pprev; | |
684 | } | |
685 | ||
686 | static inline int hlist_empty(const struct hlist_head *h) | |
687 | { | |
688 | return !h->first; | |
689 | } | |
690 | ||
691 | static inline void __hlist_del(struct hlist_node *n) | |
692 | { | |
693 | struct hlist_node *next = n->next; | |
694 | struct hlist_node **pprev = n->pprev; | |
695 | *pprev = next; | |
696 | if (next) | |
697 | next->pprev = pprev; | |
698 | } | |
699 | ||
700 | static inline void hlist_del(struct hlist_node *n) | |
701 | { | |
702 | __hlist_del(n); | |
703 | n->next = LIST_POISON1; | |
704 | n->pprev = LIST_POISON2; | |
705 | } | |
706 | ||
707 | /** | |
708 | * hlist_del_rcu - deletes entry from hash list without re-initialization | |
709 | * @n: the element to delete from the hash list. | |
710 | * | |
711 | * Note: list_unhashed() on entry does not return true after this, | |
712 | * the entry is in an undefined state. It is useful for RCU based | |
713 | * lockfree traversal. | |
714 | * | |
715 | * In particular, it means that we can not poison the forward | |
716 | * pointers that may still be used for walking the hash list. | |
717 | * | |
718 | * The caller must take whatever precautions are necessary | |
719 | * (such as holding appropriate locks) to avoid racing | |
720 | * with another list-mutation primitive, such as hlist_add_head_rcu() | |
721 | * or hlist_del_rcu(), running on this same list. | |
722 | * However, it is perfectly legal to run concurrently with | |
723 | * the _rcu list-traversal primitives, such as | |
724 | * hlist_for_each_entry(). | |
725 | */ | |
726 | static inline void hlist_del_rcu(struct hlist_node *n) | |
727 | { | |
728 | __hlist_del(n); | |
729 | n->pprev = LIST_POISON2; | |
730 | } | |
731 | ||
732 | static inline void hlist_del_init(struct hlist_node *n) | |
733 | { | |
da753bea | 734 | if (!hlist_unhashed(n)) { |
1da177e4 LT |
735 | __hlist_del(n); |
736 | INIT_HLIST_NODE(n); | |
737 | } | |
738 | } | |
739 | ||
45f8bde0 | 740 | /** |
b88cb424 IM |
741 | * hlist_replace_rcu - replace old entry by new one |
742 | * @old : the element to be replaced | |
743 | * @new : the new element to insert | |
744 | * | |
45f8bde0 | 745 | * The @old entry will be replaced with the @new entry atomically. |
b88cb424 IM |
746 | */ |
747 | static inline void hlist_replace_rcu(struct hlist_node *old, | |
748 | struct hlist_node *new) | |
749 | { | |
750 | struct hlist_node *next = old->next; | |
751 | ||
752 | new->next = next; | |
753 | new->pprev = old->pprev; | |
754 | smp_wmb(); | |
755 | if (next) | |
756 | new->next->pprev = &new->next; | |
757 | *new->pprev = new; | |
758 | old->pprev = LIST_POISON2; | |
759 | } | |
760 | ||
1da177e4 LT |
761 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
762 | { | |
763 | struct hlist_node *first = h->first; | |
764 | n->next = first; | |
765 | if (first) | |
766 | first->pprev = &n->next; | |
767 | h->first = n; | |
768 | n->pprev = &h->first; | |
769 | } | |
770 | ||
771 | ||
772 | /** | |
fe96e57d | 773 | * hlist_add_head_rcu |
1da177e4 LT |
774 | * @n: the element to add to the hash list. |
775 | * @h: the list to add to. | |
776 | * | |
fe96e57d RD |
777 | * Description: |
778 | * Adds the specified element to the specified hlist, | |
779 | * while permitting racing traversals. | |
780 | * | |
1da177e4 LT |
781 | * The caller must take whatever precautions are necessary |
782 | * (such as holding appropriate locks) to avoid racing | |
783 | * with another list-mutation primitive, such as hlist_add_head_rcu() | |
784 | * or hlist_del_rcu(), running on this same list. | |
785 | * However, it is perfectly legal to run concurrently with | |
786 | * the _rcu list-traversal primitives, such as | |
665a7583 | 787 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
1da177e4 LT |
788 | * problems on Alpha CPUs. Regardless of the type of CPU, the |
789 | * list-traversal primitive must be guarded by rcu_read_lock(). | |
790 | */ | |
791 | static inline void hlist_add_head_rcu(struct hlist_node *n, | |
792 | struct hlist_head *h) | |
793 | { | |
794 | struct hlist_node *first = h->first; | |
795 | n->next = first; | |
796 | n->pprev = &h->first; | |
797 | smp_wmb(); | |
798 | if (first) | |
799 | first->pprev = &n->next; | |
800 | h->first = n; | |
801 | } | |
802 | ||
803 | /* next must be != NULL */ | |
804 | static inline void hlist_add_before(struct hlist_node *n, | |
805 | struct hlist_node *next) | |
806 | { | |
807 | n->pprev = next->pprev; | |
808 | n->next = next; | |
809 | next->pprev = &n->next; | |
810 | *(n->pprev) = n; | |
811 | } | |
812 | ||
813 | static inline void hlist_add_after(struct hlist_node *n, | |
814 | struct hlist_node *next) | |
815 | { | |
816 | next->next = n->next; | |
817 | n->next = next; | |
818 | next->pprev = &n->next; | |
819 | ||
820 | if(next->next) | |
821 | next->next->pprev = &next->next; | |
822 | } | |
823 | ||
cf4ef014 | 824 | /** |
fe96e57d | 825 | * hlist_add_before_rcu |
cf4ef014 PM |
826 | * @n: the new element to add to the hash list. |
827 | * @next: the existing element to add the new element before. | |
828 | * | |
fe96e57d RD |
829 | * Description: |
830 | * Adds the specified element to the specified hlist | |
831 | * before the specified node while permitting racing traversals. | |
832 | * | |
cf4ef014 PM |
833 | * The caller must take whatever precautions are necessary |
834 | * (such as holding appropriate locks) to avoid racing | |
835 | * with another list-mutation primitive, such as hlist_add_head_rcu() | |
836 | * or hlist_del_rcu(), running on this same list. | |
837 | * However, it is perfectly legal to run concurrently with | |
838 | * the _rcu list-traversal primitives, such as | |
665a7583 | 839 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
cf4ef014 PM |
840 | * problems on Alpha CPUs. |
841 | */ | |
e5b43760 RO |
842 | static inline void hlist_add_before_rcu(struct hlist_node *n, |
843 | struct hlist_node *next) | |
844 | { | |
845 | n->pprev = next->pprev; | |
846 | n->next = next; | |
847 | smp_wmb(); | |
848 | next->pprev = &n->next; | |
849 | *(n->pprev) = n; | |
850 | } | |
851 | ||
cf4ef014 | 852 | /** |
fe96e57d | 853 | * hlist_add_after_rcu |
cf4ef014 PM |
854 | * @prev: the existing element to add the new element after. |
855 | * @n: the new element to add to the hash list. | |
856 | * | |
fe96e57d RD |
857 | * Description: |
858 | * Adds the specified element to the specified hlist | |
859 | * after the specified node while permitting racing traversals. | |
860 | * | |
cf4ef014 PM |
861 | * The caller must take whatever precautions are necessary |
862 | * (such as holding appropriate locks) to avoid racing | |
863 | * with another list-mutation primitive, such as hlist_add_head_rcu() | |
864 | * or hlist_del_rcu(), running on this same list. | |
865 | * However, it is perfectly legal to run concurrently with | |
866 | * the _rcu list-traversal primitives, such as | |
665a7583 | 867 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
cf4ef014 PM |
868 | * problems on Alpha CPUs. |
869 | */ | |
e5b43760 RO |
870 | static inline void hlist_add_after_rcu(struct hlist_node *prev, |
871 | struct hlist_node *n) | |
872 | { | |
873 | n->next = prev->next; | |
874 | n->pprev = &prev->next; | |
875 | smp_wmb(); | |
876 | prev->next = n; | |
877 | if (n->next) | |
878 | n->next->pprev = &n->next; | |
879 | } | |
880 | ||
1da177e4 LT |
881 | #define hlist_entry(ptr, type, member) container_of(ptr,type,member) |
882 | ||
883 | #define hlist_for_each(pos, head) \ | |
884 | for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ | |
885 | pos = pos->next) | |
886 | ||
887 | #define hlist_for_each_safe(pos, n, head) \ | |
888 | for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ | |
889 | pos = n) | |
890 | ||
1da177e4 LT |
891 | /** |
892 | * hlist_for_each_entry - iterate over list of given type | |
8e3a67a9 RD |
893 | * @tpos: the type * to use as a loop cursor. |
894 | * @pos: the &struct hlist_node to use as a loop cursor. | |
1da177e4 LT |
895 | * @head: the head for your list. |
896 | * @member: the name of the hlist_node within the struct. | |
897 | */ | |
898 | #define hlist_for_each_entry(tpos, pos, head, member) \ | |
899 | for (pos = (head)->first; \ | |
900 | pos && ({ prefetch(pos->next); 1;}) && \ | |
901 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |
902 | pos = pos->next) | |
903 | ||
904 | /** | |
fe96e57d | 905 | * hlist_for_each_entry_continue - iterate over a hlist continuing after current point |
8e3a67a9 RD |
906 | * @tpos: the type * to use as a loop cursor. |
907 | * @pos: the &struct hlist_node to use as a loop cursor. | |
1da177e4 LT |
908 | * @member: the name of the hlist_node within the struct. |
909 | */ | |
910 | #define hlist_for_each_entry_continue(tpos, pos, member) \ | |
911 | for (pos = (pos)->next; \ | |
912 | pos && ({ prefetch(pos->next); 1;}) && \ | |
913 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |
914 | pos = pos->next) | |
915 | ||
916 | /** | |
fe96e57d | 917 | * hlist_for_each_entry_from - iterate over a hlist continuing from current point |
8e3a67a9 RD |
918 | * @tpos: the type * to use as a loop cursor. |
919 | * @pos: the &struct hlist_node to use as a loop cursor. | |
1da177e4 LT |
920 | * @member: the name of the hlist_node within the struct. |
921 | */ | |
922 | #define hlist_for_each_entry_from(tpos, pos, member) \ | |
923 | for (; pos && ({ prefetch(pos->next); 1;}) && \ | |
924 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |
925 | pos = pos->next) | |
926 | ||
927 | /** | |
928 | * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry | |
8e3a67a9 RD |
929 | * @tpos: the type * to use as a loop cursor. |
930 | * @pos: the &struct hlist_node to use as a loop cursor. | |
1da177e4 LT |
931 | * @n: another &struct hlist_node to use as temporary storage |
932 | * @head: the head for your list. | |
933 | * @member: the name of the hlist_node within the struct. | |
934 | */ | |
935 | #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ | |
936 | for (pos = (head)->first; \ | |
937 | pos && ({ n = pos->next; 1; }) && \ | |
938 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |
939 | pos = n) | |
940 | ||
941 | /** | |
942 | * hlist_for_each_entry_rcu - iterate over rcu list of given type | |
8e3a67a9 RD |
943 | * @tpos: the type * to use as a loop cursor. |
944 | * @pos: the &struct hlist_node to use as a loop cursor. | |
1da177e4 LT |
945 | * @head: the head for your list. |
946 | * @member: the name of the hlist_node within the struct. | |
947 | * | |
948 | * This list-traversal primitive may safely run concurrently with | |
e1ba0dab | 949 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
1da177e4 LT |
950 | * as long as the traversal is guarded by rcu_read_lock(). |
951 | */ | |
952 | #define hlist_for_each_entry_rcu(tpos, pos, head, member) \ | |
953 | for (pos = (head)->first; \ | |
b24d18aa | 954 | rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) && \ |
1da177e4 | 955 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
b24d18aa | 956 | pos = pos->next) |
1da177e4 LT |
957 | |
958 | #else | |
959 | #warning "don't include kernel headers in userspace" | |
960 | #endif /* __KERNEL__ */ | |
961 | #endif |