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