]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/list.h
rculist: Consolidate DEBUG_LIST for list_add_rcu()
[mirror_ubuntu-jammy-kernel.git] / include / linux / list.h
CommitLineData
1da177e4
LT
1#ifndef _LINUX_LIST_H
2#define _LINUX_LIST_H
3
de5d9bf6 4#include <linux/types.h>
1da177e4 5#include <linux/stddef.h>
c9cf5528 6#include <linux/poison.h>
e66eed65 7#include <linux/const.h>
8b21d9ca 8#include <linux/kernel.h>
1da177e4 9
1da177e4
LT
10/*
11 * Simple doubly linked list implementation.
12 *
13 * Some of the internal functions ("__xxx") are useful when
14 * manipulating whole lists rather than single entries, as
15 * sometimes we already know the next/prev entries and we can
16 * generate better code by using them directly rather than
17 * using the generic single-entry routines.
18 */
19
1da177e4
LT
20#define LIST_HEAD_INIT(name) { &(name), &(name) }
21
22#define LIST_HEAD(name) \
23 struct list_head name = LIST_HEAD_INIT(name)
24
490d6ab1
ZB
25static inline void INIT_LIST_HEAD(struct list_head *list)
26{
2f073848 27 WRITE_ONCE(list->next, list);
490d6ab1
ZB
28 list->prev = list;
29}
1da177e4 30
d7c81673
KC
31#ifdef CONFIG_DEBUG_LIST
32extern bool __list_add_valid(struct list_head *new,
33 struct list_head *prev,
34 struct list_head *next);
35#else
36static inline bool __list_add_valid(struct list_head *new,
37 struct list_head *prev,
38 struct list_head *next)
39{
40 return true;
41}
42#endif
43
1da177e4
LT
44/*
45 * Insert a new entry between two known consecutive entries.
46 *
47 * This is only for internal list manipulation where we know
48 * the prev/next entries already!
49 */
50static inline void __list_add(struct list_head *new,
51 struct list_head *prev,
52 struct list_head *next)
53{
d7c81673
KC
54 if (!__list_add_valid(new, prev, next))
55 return;
56
1da177e4
LT
57 next->prev = new;
58 new->next = next;
59 new->prev = prev;
1c97be67 60 WRITE_ONCE(prev->next, new);
1da177e4
LT
61}
62
63/**
64 * list_add - add a new entry
65 * @new: new entry to be added
66 * @head: list head to add it after
67 *
68 * Insert a new entry after the specified head.
69 * This is good for implementing stacks.
70 */
71static inline void list_add(struct list_head *new, struct list_head *head)
72{
73 __list_add(new, head, head->next);
74}
199a9afc 75
1da177e4
LT
76
77/**
78 * list_add_tail - add a new entry
79 * @new: new entry to be added
80 * @head: list head to add it before
81 *
82 * Insert a new entry before the specified head.
83 * This is useful for implementing queues.
84 */
85static inline void list_add_tail(struct list_head *new, struct list_head *head)
86{
87 __list_add(new, head->prev, head);
88}
89
1da177e4
LT
90/*
91 * Delete a list entry by making the prev/next entries
92 * point to each other.
93 *
94 * This is only for internal list manipulation where we know
95 * the prev/next entries already!
96 */
97static inline void __list_del(struct list_head * prev, struct list_head * next)
98{
99 next->prev = prev;
7f5f873c 100 WRITE_ONCE(prev->next, next);
1da177e4
LT
101}
102
103/**
104 * list_del - deletes entry from list.
105 * @entry: the element to delete from the list.
72fd4a35 106 * Note: list_empty() on entry does not return true after this, the entry is
1da177e4
LT
107 * in an undefined state.
108 */
199a9afc 109#ifndef CONFIG_DEBUG_LIST
3c18d4de
LT
110static inline void __list_del_entry(struct list_head *entry)
111{
112 __list_del(entry->prev, entry->next);
113}
114
1da177e4
LT
115static inline void list_del(struct list_head *entry)
116{
117 __list_del(entry->prev, entry->next);
118 entry->next = LIST_POISON1;
119 entry->prev = LIST_POISON2;
120}
199a9afc 121#else
3c18d4de 122extern void __list_del_entry(struct list_head *entry);
199a9afc
DJ
123extern void list_del(struct list_head *entry);
124#endif
1da177e4 125
54e73770
ON
126/**
127 * list_replace - replace old entry by new one
128 * @old : the element to be replaced
129 * @new : the new element to insert
72fd4a35
RD
130 *
131 * If @old was empty, it will be overwritten.
54e73770
ON
132 */
133static inline void list_replace(struct list_head *old,
134 struct list_head *new)
135{
136 new->next = old->next;
137 new->next->prev = new;
138 new->prev = old->prev;
139 new->prev->next = new;
140}
141
142static inline void list_replace_init(struct list_head *old,
143 struct list_head *new)
144{
145 list_replace(old, new);
146 INIT_LIST_HEAD(old);
147}
148
1da177e4
LT
149/**
150 * list_del_init - deletes entry from list and reinitialize it.
151 * @entry: the element to delete from the list.
152 */
153static inline void list_del_init(struct list_head *entry)
154{
3c18d4de 155 __list_del_entry(entry);
1da177e4
LT
156 INIT_LIST_HEAD(entry);
157}
158
159/**
160 * list_move - delete from one list and add as another's head
161 * @list: the entry to move
162 * @head: the head that will precede our entry
163 */
164static inline void list_move(struct list_head *list, struct list_head *head)
165{
3c18d4de 166 __list_del_entry(list);
78db2ad6 167 list_add(list, head);
1da177e4
LT
168}
169
170/**
171 * list_move_tail - delete from one list and add as another's tail
172 * @list: the entry to move
173 * @head: the head that will follow our entry
174 */
175static inline void list_move_tail(struct list_head *list,
176 struct list_head *head)
177{
3c18d4de 178 __list_del_entry(list);
78db2ad6 179 list_add_tail(list, head);
1da177e4
LT
180}
181
e8f4d97e
SN
182/**
183 * list_is_last - tests whether @list is the last entry in list @head
184 * @list: the entry to test
185 * @head: the head of the list
186 */
187static inline int list_is_last(const struct list_head *list,
188 const struct list_head *head)
189{
190 return list->next == head;
191}
192
1da177e4
LT
193/**
194 * list_empty - tests whether a list is empty
195 * @head: the list to test.
196 */
197static inline int list_empty(const struct list_head *head)
198{
1658d35e 199 return READ_ONCE(head->next) == head;
1da177e4
LT
200}
201
202/**
fe96e57d
RD
203 * list_empty_careful - tests whether a list is empty and not being modified
204 * @head: the list to test
205 *
206 * Description:
207 * tests whether a list is empty _and_ checks that no other CPU might be
208 * in the process of modifying either member (next or prev)
1da177e4
LT
209 *
210 * NOTE: using list_empty_careful() without synchronization
211 * can only be safe if the only activity that can happen
212 * to the list entry is list_del_init(). Eg. it cannot be used
213 * if another CPU could re-list_add() it.
1da177e4
LT
214 */
215static inline int list_empty_careful(const struct list_head *head)
216{
217 struct list_head *next = head->next;
218 return (next == head) && (next == head->prev);
99602572
MH
219}
220
5908cdc8
FW
221/**
222 * list_rotate_left - rotate the list to the left
223 * @head: the head of the list
224 */
225static inline void list_rotate_left(struct list_head *head)
226{
227 struct list_head *first;
228
229 if (!list_empty(head)) {
230 first = head->next;
231 list_move_tail(first, head);
232 }
233}
234
99602572
MH
235/**
236 * list_is_singular - tests whether a list has just one entry.
237 * @head: the list to test.
238 */
239static inline int list_is_singular(const struct list_head *head)
240{
241 return !list_empty(head) && (head->next == head->prev);
1da177e4
LT
242}
243
00e8a4da
LR
244static inline void __list_cut_position(struct list_head *list,
245 struct list_head *head, struct list_head *entry)
246{
247 struct list_head *new_first = entry->next;
248 list->next = head->next;
249 list->next->prev = list;
250 list->prev = entry;
251 entry->next = list;
252 head->next = new_first;
253 new_first->prev = head;
254}
255
256/**
257 * list_cut_position - cut a list into two
258 * @list: a new list to add all removed entries
259 * @head: a list with entries
260 * @entry: an entry within head, could be the head itself
261 * and if so we won't cut the list
262 *
263 * This helper moves the initial part of @head, up to and
264 * including @entry, from @head to @list. You should
265 * pass on @entry an element you know is on @head. @list
266 * should be an empty list or a list you do not care about
267 * losing its data.
268 *
269 */
270static inline void list_cut_position(struct list_head *list,
271 struct list_head *head, struct list_head *entry)
272{
273 if (list_empty(head))
274 return;
275 if (list_is_singular(head) &&
276 (head->next != entry && head != entry))
277 return;
278 if (entry == head)
279 INIT_LIST_HEAD(list);
280 else
281 __list_cut_position(list, head, entry);
282}
283
95d8c365 284static inline void __list_splice(const struct list_head *list,
7d283aee
LR
285 struct list_head *prev,
286 struct list_head *next)
1da177e4
LT
287{
288 struct list_head *first = list->next;
289 struct list_head *last = list->prev;
1da177e4 290
7d283aee
LR
291 first->prev = prev;
292 prev->next = first;
1da177e4 293
7d283aee
LR
294 last->next = next;
295 next->prev = last;
1da177e4
LT
296}
297
298/**
7d283aee 299 * list_splice - join two lists, this is designed for stacks
1da177e4
LT
300 * @list: the new list to add.
301 * @head: the place to add it in the first list.
302 */
95d8c365
RD
303static inline void list_splice(const struct list_head *list,
304 struct list_head *head)
1da177e4
LT
305{
306 if (!list_empty(list))
7d283aee
LR
307 __list_splice(list, head, head->next);
308}
309
310/**
311 * list_splice_tail - join two lists, each list being a queue
312 * @list: the new list to add.
313 * @head: the place to add it in the first list.
314 */
315static inline void list_splice_tail(struct list_head *list,
316 struct list_head *head)
317{
318 if (!list_empty(list))
319 __list_splice(list, head->prev, head);
1da177e4
LT
320}
321
322/**
323 * list_splice_init - join two lists and reinitialise the emptied list.
324 * @list: the new list to add.
325 * @head: the place to add it in the first list.
326 *
327 * The list at @list is reinitialised
328 */
329static inline void list_splice_init(struct list_head *list,
330 struct list_head *head)
331{
332 if (!list_empty(list)) {
7d283aee
LR
333 __list_splice(list, head, head->next);
334 INIT_LIST_HEAD(list);
335 }
336}
337
338/**
6724cce8 339 * list_splice_tail_init - join two lists and reinitialise the emptied list
7d283aee
LR
340 * @list: the new list to add.
341 * @head: the place to add it in the first list.
342 *
6724cce8 343 * Each of the lists is a queue.
7d283aee
LR
344 * The list at @list is reinitialised
345 */
346static inline void list_splice_tail_init(struct list_head *list,
347 struct list_head *head)
348{
349 if (!list_empty(list)) {
350 __list_splice(list, head->prev, head);
1da177e4
LT
351 INIT_LIST_HEAD(list);
352 }
353}
354
355/**
356 * list_entry - get the struct for this entry
357 * @ptr: the &struct list_head pointer.
358 * @type: the type of the struct this is embedded in.
3943f42c 359 * @member: the name of the list_head within the struct.
1da177e4
LT
360 */
361#define list_entry(ptr, type, member) \
362 container_of(ptr, type, member)
363
b5e61818
PE
364/**
365 * list_first_entry - get the first element from a list
366 * @ptr: the list head to take the element from.
367 * @type: the type of the struct this is embedded in.
3943f42c 368 * @member: the name of the list_head within the struct.
b5e61818
PE
369 *
370 * Note, that list is expected to be not empty.
371 */
372#define list_first_entry(ptr, type, member) \
373 list_entry((ptr)->next, type, member)
374
93be3c2e
ON
375/**
376 * list_last_entry - get the last element from a list
377 * @ptr: the list head to take the element from.
378 * @type: the type of the struct this is embedded in.
3943f42c 379 * @member: the name of the list_head within the struct.
93be3c2e
ON
380 *
381 * Note, that list is expected to be not empty.
382 */
383#define list_last_entry(ptr, type, member) \
384 list_entry((ptr)->prev, type, member)
385
6d7581e6
JP
386/**
387 * list_first_entry_or_null - get the first element from a list
388 * @ptr: the list head to take the element from.
389 * @type: the type of the struct this is embedded in.
3943f42c 390 * @member: the name of the list_head within the struct.
6d7581e6
JP
391 *
392 * Note that if the list is empty, it returns NULL.
393 */
12adfd88
CW
394#define list_first_entry_or_null(ptr, type, member) ({ \
395 struct list_head *head__ = (ptr); \
396 struct list_head *pos__ = READ_ONCE(head__->next); \
397 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
398})
6d7581e6 399
008208c6
ON
400/**
401 * list_next_entry - get the next element in list
402 * @pos: the type * to cursor
3943f42c 403 * @member: the name of the list_head within the struct.
008208c6
ON
404 */
405#define list_next_entry(pos, member) \
406 list_entry((pos)->member.next, typeof(*(pos)), member)
407
408/**
409 * list_prev_entry - get the prev element in list
410 * @pos: the type * to cursor
3943f42c 411 * @member: the name of the list_head within the struct.
008208c6
ON
412 */
413#define list_prev_entry(pos, member) \
414 list_entry((pos)->member.prev, typeof(*(pos)), member)
415
1da177e4
LT
416/**
417 * list_for_each - iterate over a list
8e3a67a9 418 * @pos: the &struct list_head to use as a loop cursor.
1da177e4
LT
419 * @head: the head for your list.
420 */
421#define list_for_each(pos, head) \
e66eed65 422 for (pos = (head)->next; pos != (head); pos = pos->next)
1da177e4 423
1da177e4
LT
424/**
425 * list_for_each_prev - iterate over a list backwards
8e3a67a9 426 * @pos: the &struct list_head to use as a loop cursor.
1da177e4
LT
427 * @head: the head for your list.
428 */
429#define list_for_each_prev(pos, head) \
e66eed65 430 for (pos = (head)->prev; pos != (head); pos = pos->prev)
1da177e4
LT
431
432/**
fe96e57d 433 * list_for_each_safe - iterate over a list safe against removal of list entry
8e3a67a9 434 * @pos: the &struct list_head to use as a loop cursor.
1da177e4
LT
435 * @n: another &struct list_head to use as temporary storage
436 * @head: the head for your list.
437 */
438#define list_for_each_safe(pos, n, head) \
439 for (pos = (head)->next, n = pos->next; pos != (head); \
440 pos = n, n = pos->next)
441
37c42524 442/**
8f731f7d 443 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
37c42524
DL
444 * @pos: the &struct list_head to use as a loop cursor.
445 * @n: another &struct list_head to use as temporary storage
446 * @head: the head for your list.
447 */
448#define list_for_each_prev_safe(pos, n, head) \
449 for (pos = (head)->prev, n = pos->prev; \
e66eed65 450 pos != (head); \
37c42524
DL
451 pos = n, n = pos->prev)
452
1da177e4
LT
453/**
454 * list_for_each_entry - iterate over list of given type
8e3a67a9 455 * @pos: the type * to use as a loop cursor.
1da177e4 456 * @head: the head for your list.
3943f42c 457 * @member: the name of the list_head within the struct.
1da177e4
LT
458 */
459#define list_for_each_entry(pos, head, member) \
93be3c2e 460 for (pos = list_first_entry(head, typeof(*pos), member); \
8120e2e5
ON
461 &pos->member != (head); \
462 pos = list_next_entry(pos, member))
1da177e4
LT
463
464/**
465 * list_for_each_entry_reverse - iterate backwards over list of given type.
8e3a67a9 466 * @pos: the type * to use as a loop cursor.
1da177e4 467 * @head: the head for your list.
3943f42c 468 * @member: the name of the list_head within the struct.
1da177e4
LT
469 */
470#define list_for_each_entry_reverse(pos, head, member) \
93be3c2e 471 for (pos = list_last_entry(head, typeof(*pos), member); \
8120e2e5
ON
472 &pos->member != (head); \
473 pos = list_prev_entry(pos, member))
1da177e4
LT
474
475/**
72fd4a35 476 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1da177e4
LT
477 * @pos: the type * to use as a start point
478 * @head: the head of the list
3943f42c 479 * @member: the name of the list_head within the struct.
fe96e57d 480 *
72fd4a35 481 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1da177e4
LT
482 */
483#define list_prepare_entry(pos, head, member) \
484 ((pos) ? : list_entry(head, typeof(*pos), member))
485
486/**
fe96e57d 487 * list_for_each_entry_continue - continue iteration over list of given type
8e3a67a9 488 * @pos: the type * to use as a loop cursor.
1da177e4 489 * @head: the head for your list.
3943f42c 490 * @member: the name of the list_head within the struct.
fe96e57d
RD
491 *
492 * Continue to iterate over list of given type, continuing after
493 * the current position.
1da177e4
LT
494 */
495#define list_for_each_entry_continue(pos, head, member) \
8120e2e5
ON
496 for (pos = list_next_entry(pos, member); \
497 &pos->member != (head); \
498 pos = list_next_entry(pos, member))
1da177e4 499
768f3591
PE
500/**
501 * list_for_each_entry_continue_reverse - iterate backwards from the given point
502 * @pos: the type * to use as a loop cursor.
503 * @head: the head for your list.
3943f42c 504 * @member: the name of the list_head within the struct.
768f3591
PE
505 *
506 * Start to iterate over list of given type backwards, continuing after
507 * the current position.
508 */
509#define list_for_each_entry_continue_reverse(pos, head, member) \
8120e2e5
ON
510 for (pos = list_prev_entry(pos, member); \
511 &pos->member != (head); \
512 pos = list_prev_entry(pos, member))
768f3591 513
e229c2fb 514/**
fe96e57d 515 * list_for_each_entry_from - iterate over list of given type from the current point
8e3a67a9 516 * @pos: the type * to use as a loop cursor.
e229c2fb 517 * @head: the head for your list.
3943f42c 518 * @member: the name of the list_head within the struct.
fe96e57d
RD
519 *
520 * Iterate over list of given type, continuing from current position.
e229c2fb
ACM
521 */
522#define list_for_each_entry_from(pos, head, member) \
8120e2e5
ON
523 for (; &pos->member != (head); \
524 pos = list_next_entry(pos, member))
e229c2fb 525
1da177e4
LT
526/**
527 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
8e3a67a9 528 * @pos: the type * to use as a loop cursor.
1da177e4
LT
529 * @n: another type * to use as temporary storage
530 * @head: the head for your list.
3943f42c 531 * @member: the name of the list_head within the struct.
1da177e4
LT
532 */
533#define list_for_each_entry_safe(pos, n, head, member) \
93be3c2e 534 for (pos = list_first_entry(head, typeof(*pos), member), \
8120e2e5 535 n = list_next_entry(pos, member); \
1da177e4 536 &pos->member != (head); \
8120e2e5 537 pos = n, n = list_next_entry(n, member))
1da177e4 538
74459dc7 539/**
9a86e2ba 540 * list_for_each_entry_safe_continue - continue list iteration safe against removal
8e3a67a9 541 * @pos: the type * to use as a loop cursor.
74459dc7
ACM
542 * @n: another type * to use as temporary storage
543 * @head: the head for your list.
3943f42c 544 * @member: the name of the list_head within the struct.
fe96e57d
RD
545 *
546 * Iterate over list of given type, continuing after current point,
547 * safe against removal of list entry.
74459dc7
ACM
548 */
549#define list_for_each_entry_safe_continue(pos, n, head, member) \
8120e2e5
ON
550 for (pos = list_next_entry(pos, member), \
551 n = list_next_entry(pos, member); \
d8dcffee 552 &pos->member != (head); \
8120e2e5 553 pos = n, n = list_next_entry(n, member))
d8dcffee
ACM
554
555/**
9a86e2ba 556 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
8e3a67a9 557 * @pos: the type * to use as a loop cursor.
d8dcffee
ACM
558 * @n: another type * to use as temporary storage
559 * @head: the head for your list.
3943f42c 560 * @member: the name of the list_head within the struct.
fe96e57d
RD
561 *
562 * Iterate over list of given type from current point, safe against
563 * removal of list entry.
d8dcffee
ACM
564 */
565#define list_for_each_entry_safe_from(pos, n, head, member) \
8120e2e5 566 for (n = list_next_entry(pos, member); \
74459dc7 567 &pos->member != (head); \
8120e2e5 568 pos = n, n = list_next_entry(n, member))
74459dc7 569
0ad42352 570/**
9a86e2ba 571 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
8e3a67a9 572 * @pos: the type * to use as a loop cursor.
0ad42352
DH
573 * @n: another type * to use as temporary storage
574 * @head: the head for your list.
3943f42c 575 * @member: the name of the list_head within the struct.
fe96e57d
RD
576 *
577 * Iterate backwards over list of given type, safe against removal
578 * of list entry.
0ad42352
DH
579 */
580#define list_for_each_entry_safe_reverse(pos, n, head, member) \
93be3c2e 581 for (pos = list_last_entry(head, typeof(*pos), member), \
8120e2e5 582 n = list_prev_entry(pos, member); \
0ad42352 583 &pos->member != (head); \
8120e2e5 584 pos = n, n = list_prev_entry(n, member))
0ad42352 585
57439f87
NP
586/**
587 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
588 * @pos: the loop cursor used in the list_for_each_entry_safe loop
589 * @n: temporary storage used in list_for_each_entry_safe
3943f42c 590 * @member: the name of the list_head within the struct.
57439f87
NP
591 *
592 * list_safe_reset_next is not safe to use in general if the list may be
593 * modified concurrently (eg. the lock is dropped in the loop body). An
594 * exception to this is if the cursor element (pos) is pinned in the list,
595 * and list_safe_reset_next is called after re-taking the lock and before
596 * completing the current iteration of the loop body.
597 */
598#define list_safe_reset_next(pos, n, member) \
8120e2e5 599 n = list_next_entry(pos, member)
57439f87 600
1da177e4
LT
601/*
602 * Double linked lists with a single pointer list head.
603 * Mostly useful for hash tables where the two pointer list head is
604 * too wasteful.
605 * You lose the ability to access the tail in O(1).
606 */
607
1da177e4
LT
608#define HLIST_HEAD_INIT { .first = NULL }
609#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
610#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
490d6ab1
ZB
611static inline void INIT_HLIST_NODE(struct hlist_node *h)
612{
613 h->next = NULL;
614 h->pprev = NULL;
615}
1da177e4
LT
616
617static inline int hlist_unhashed(const struct hlist_node *h)
618{
619 return !h->pprev;
620}
621
622static inline int hlist_empty(const struct hlist_head *h)
623{
1658d35e 624 return !READ_ONCE(h->first);
1da177e4
LT
625}
626
627static inline void __hlist_del(struct hlist_node *n)
628{
629 struct hlist_node *next = n->next;
630 struct hlist_node **pprev = n->pprev;
7f5f873c
PM
631
632 WRITE_ONCE(*pprev, next);
1da177e4
LT
633 if (next)
634 next->pprev = pprev;
635}
636
637static inline void hlist_del(struct hlist_node *n)
638{
639 __hlist_del(n);
640 n->next = LIST_POISON1;
641 n->pprev = LIST_POISON2;
642}
643
1da177e4
LT
644static inline void hlist_del_init(struct hlist_node *n)
645{
da753bea 646 if (!hlist_unhashed(n)) {
1da177e4
LT
647 __hlist_del(n);
648 INIT_HLIST_NODE(n);
649 }
650}
651
652static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
653{
654 struct hlist_node *first = h->first;
655 n->next = first;
656 if (first)
657 first->pprev = &n->next;
1c97be67 658 WRITE_ONCE(h->first, n);
1da177e4
LT
659 n->pprev = &h->first;
660}
661
1da177e4
LT
662/* next must be != NULL */
663static inline void hlist_add_before(struct hlist_node *n,
664 struct hlist_node *next)
665{
666 n->pprev = next->pprev;
667 n->next = next;
668 next->pprev = &n->next;
1c97be67 669 WRITE_ONCE(*(n->pprev), n);
1da177e4
LT
670}
671
1d023284
KH
672static inline void hlist_add_behind(struct hlist_node *n,
673 struct hlist_node *prev)
1da177e4 674{
bc18dd33 675 n->next = prev->next;
1c97be67 676 WRITE_ONCE(prev->next, n);
bc18dd33 677 n->pprev = &prev->next;
1da177e4 678
bc18dd33
KH
679 if (n->next)
680 n->next->pprev = &n->next;
1da177e4
LT
681}
682
756acc2d
AV
683/* after that we'll appear to be on some hlist and hlist_del will work */
684static inline void hlist_add_fake(struct hlist_node *n)
685{
686 n->pprev = &n->next;
687}
688
cbedaac6
JB
689static inline bool hlist_fake(struct hlist_node *h)
690{
691 return h->pprev == &h->next;
692}
693
15dba1e3
TG
694/*
695 * Check whether the node is the only node of the head without
696 * accessing head:
697 */
698static inline bool
699hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
700{
701 return !n->next && n->pprev == &h->first;
702}
703
673d62cc
VN
704/*
705 * Move a list from one list head to another. Fixup the pprev
706 * reference of the first entry if it exists.
707 */
708static inline void hlist_move_list(struct hlist_head *old,
709 struct hlist_head *new)
710{
711 new->first = old->first;
712 if (new->first)
713 new->first->pprev = &new->first;
714 old->first = NULL;
715}
716
1da177e4
LT
717#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
718
719#define hlist_for_each(pos, head) \
75d65a42 720 for (pos = (head)->first; pos ; pos = pos->next)
1da177e4
LT
721
722#define hlist_for_each_safe(pos, n, head) \
723 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
724 pos = n)
725
b67bfe0d 726#define hlist_entry_safe(ptr, type, member) \
f65846a1
PM
727 ({ typeof(ptr) ____ptr = (ptr); \
728 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
729 })
b67bfe0d 730
1da177e4
LT
731/**
732 * hlist_for_each_entry - iterate over list of given type
b67bfe0d 733 * @pos: the type * to use as a loop cursor.
1da177e4
LT
734 * @head: the head for your list.
735 * @member: the name of the hlist_node within the struct.
736 */
b67bfe0d
SL
737#define hlist_for_each_entry(pos, head, member) \
738 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
739 pos; \
740 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1da177e4
LT
741
742/**
fe96e57d 743 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
b67bfe0d 744 * @pos: the type * to use as a loop cursor.
1da177e4
LT
745 * @member: the name of the hlist_node within the struct.
746 */
b67bfe0d
SL
747#define hlist_for_each_entry_continue(pos, member) \
748 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
749 pos; \
750 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1da177e4
LT
751
752/**
fe96e57d 753 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
b67bfe0d 754 * @pos: the type * to use as a loop cursor.
1da177e4
LT
755 * @member: the name of the hlist_node within the struct.
756 */
b67bfe0d
SL
757#define hlist_for_each_entry_from(pos, member) \
758 for (; pos; \
759 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1da177e4
LT
760
761/**
762 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
b67bfe0d 763 * @pos: the type * to use as a loop cursor.
1da177e4
LT
764 * @n: another &struct hlist_node to use as temporary storage
765 * @head: the head for your list.
766 * @member: the name of the hlist_node within the struct.
767 */
b67bfe0d
SL
768#define hlist_for_each_entry_safe(pos, n, head, member) \
769 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
770 pos && ({ n = pos->member.next; 1; }); \
771 pos = hlist_entry_safe(n, typeof(*pos), member))
1da177e4 772
1da177e4 773#endif