Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_LIST_H
0003 #define _LINUX_LIST_H
0004 
0005 #include <linux/container_of.h>
0006 #include <linux/types.h>
0007 #include <linux/stddef.h>
0008 #include <linux/poison.h>
0009 #include <linux/const.h>
0010 
0011 #include <asm/barrier.h>
0012 
0013 /*
0014  * Circular doubly linked list implementation.
0015  *
0016  * Some of the internal functions ("__xxx") are useful when
0017  * manipulating whole lists rather than single entries, as
0018  * sometimes we already know the next/prev entries and we can
0019  * generate better code by using them directly rather than
0020  * using the generic single-entry routines.
0021  */
0022 
0023 #define LIST_HEAD_INIT(name) { &(name), &(name) }
0024 
0025 #define LIST_HEAD(name) \
0026     struct list_head name = LIST_HEAD_INIT(name)
0027 
0028 /**
0029  * INIT_LIST_HEAD - Initialize a list_head structure
0030  * @list: list_head structure to be initialized.
0031  *
0032  * Initializes the list_head to point to itself.  If it is a list header,
0033  * the result is an empty list.
0034  */
0035 static inline void INIT_LIST_HEAD(struct list_head *list)
0036 {
0037     WRITE_ONCE(list->next, list);
0038     WRITE_ONCE(list->prev, list);
0039 }
0040 
0041 #ifdef CONFIG_DEBUG_LIST
0042 extern bool __list_add_valid(struct list_head *new,
0043                   struct list_head *prev,
0044                   struct list_head *next);
0045 extern bool __list_del_entry_valid(struct list_head *entry);
0046 #else
0047 static inline bool __list_add_valid(struct list_head *new,
0048                 struct list_head *prev,
0049                 struct list_head *next)
0050 {
0051     return true;
0052 }
0053 static inline bool __list_del_entry_valid(struct list_head *entry)
0054 {
0055     return true;
0056 }
0057 #endif
0058 
0059 /*
0060  * Insert a new entry between two known consecutive entries.
0061  *
0062  * This is only for internal list manipulation where we know
0063  * the prev/next entries already!
0064  */
0065 static inline void __list_add(struct list_head *new,
0066                   struct list_head *prev,
0067                   struct list_head *next)
0068 {
0069     if (!__list_add_valid(new, prev, next))
0070         return;
0071 
0072     next->prev = new;
0073     new->next = next;
0074     new->prev = prev;
0075     WRITE_ONCE(prev->next, new);
0076 }
0077 
0078 /**
0079  * list_add - add a new entry
0080  * @new: new entry to be added
0081  * @head: list head to add it after
0082  *
0083  * Insert a new entry after the specified head.
0084  * This is good for implementing stacks.
0085  */
0086 static inline void list_add(struct list_head *new, struct list_head *head)
0087 {
0088     __list_add(new, head, head->next);
0089 }
0090 
0091 
0092 /**
0093  * list_add_tail - add a new entry
0094  * @new: new entry to be added
0095  * @head: list head to add it before
0096  *
0097  * Insert a new entry before the specified head.
0098  * This is useful for implementing queues.
0099  */
0100 static inline void list_add_tail(struct list_head *new, struct list_head *head)
0101 {
0102     __list_add(new, head->prev, head);
0103 }
0104 
0105 /*
0106  * Delete a list entry by making the prev/next entries
0107  * point to each other.
0108  *
0109  * This is only for internal list manipulation where we know
0110  * the prev/next entries already!
0111  */
0112 static inline void __list_del(struct list_head * prev, struct list_head * next)
0113 {
0114     next->prev = prev;
0115     WRITE_ONCE(prev->next, next);
0116 }
0117 
0118 /*
0119  * Delete a list entry and clear the 'prev' pointer.
0120  *
0121  * This is a special-purpose list clearing method used in the networking code
0122  * for lists allocated as per-cpu, where we don't want to incur the extra
0123  * WRITE_ONCE() overhead of a regular list_del_init(). The code that uses this
0124  * needs to check the node 'prev' pointer instead of calling list_empty().
0125  */
0126 static inline void __list_del_clearprev(struct list_head *entry)
0127 {
0128     __list_del(entry->prev, entry->next);
0129     entry->prev = NULL;
0130 }
0131 
0132 static inline void __list_del_entry(struct list_head *entry)
0133 {
0134     if (!__list_del_entry_valid(entry))
0135         return;
0136 
0137     __list_del(entry->prev, entry->next);
0138 }
0139 
0140 /**
0141  * list_del - deletes entry from list.
0142  * @entry: the element to delete from the list.
0143  * Note: list_empty() on entry does not return true after this, the entry is
0144  * in an undefined state.
0145  */
0146 static inline void list_del(struct list_head *entry)
0147 {
0148     __list_del_entry(entry);
0149     entry->next = LIST_POISON1;
0150     entry->prev = LIST_POISON2;
0151 }
0152 
0153 /**
0154  * list_replace - replace old entry by new one
0155  * @old : the element to be replaced
0156  * @new : the new element to insert
0157  *
0158  * If @old was empty, it will be overwritten.
0159  */
0160 static inline void list_replace(struct list_head *old,
0161                 struct list_head *new)
0162 {
0163     new->next = old->next;
0164     new->next->prev = new;
0165     new->prev = old->prev;
0166     new->prev->next = new;
0167 }
0168 
0169 /**
0170  * list_replace_init - replace old entry by new one and initialize the old one
0171  * @old : the element to be replaced
0172  * @new : the new element to insert
0173  *
0174  * If @old was empty, it will be overwritten.
0175  */
0176 static inline void list_replace_init(struct list_head *old,
0177                      struct list_head *new)
0178 {
0179     list_replace(old, new);
0180     INIT_LIST_HEAD(old);
0181 }
0182 
0183 /**
0184  * list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position
0185  * @entry1: the location to place entry2
0186  * @entry2: the location to place entry1
0187  */
0188 static inline void list_swap(struct list_head *entry1,
0189                  struct list_head *entry2)
0190 {
0191     struct list_head *pos = entry2->prev;
0192 
0193     list_del(entry2);
0194     list_replace(entry1, entry2);
0195     if (pos == entry1)
0196         pos = entry2;
0197     list_add(entry1, pos);
0198 }
0199 
0200 /**
0201  * list_del_init - deletes entry from list and reinitialize it.
0202  * @entry: the element to delete from the list.
0203  */
0204 static inline void list_del_init(struct list_head *entry)
0205 {
0206     __list_del_entry(entry);
0207     INIT_LIST_HEAD(entry);
0208 }
0209 
0210 /**
0211  * list_move - delete from one list and add as another's head
0212  * @list: the entry to move
0213  * @head: the head that will precede our entry
0214  */
0215 static inline void list_move(struct list_head *list, struct list_head *head)
0216 {
0217     __list_del_entry(list);
0218     list_add(list, head);
0219 }
0220 
0221 /**
0222  * list_move_tail - delete from one list and add as another's tail
0223  * @list: the entry to move
0224  * @head: the head that will follow our entry
0225  */
0226 static inline void list_move_tail(struct list_head *list,
0227                   struct list_head *head)
0228 {
0229     __list_del_entry(list);
0230     list_add_tail(list, head);
0231 }
0232 
0233 /**
0234  * list_bulk_move_tail - move a subsection of a list to its tail
0235  * @head: the head that will follow our entry
0236  * @first: first entry to move
0237  * @last: last entry to move, can be the same as first
0238  *
0239  * Move all entries between @first and including @last before @head.
0240  * All three entries must belong to the same linked list.
0241  */
0242 static inline void list_bulk_move_tail(struct list_head *head,
0243                        struct list_head *first,
0244                        struct list_head *last)
0245 {
0246     first->prev->next = last->next;
0247     last->next->prev = first->prev;
0248 
0249     head->prev->next = first;
0250     first->prev = head->prev;
0251 
0252     last->next = head;
0253     head->prev = last;
0254 }
0255 
0256 /**
0257  * list_is_first -- tests whether @list is the first entry in list @head
0258  * @list: the entry to test
0259  * @head: the head of the list
0260  */
0261 static inline int list_is_first(const struct list_head *list, const struct list_head *head)
0262 {
0263     return list->prev == head;
0264 }
0265 
0266 /**
0267  * list_is_last - tests whether @list is the last entry in list @head
0268  * @list: the entry to test
0269  * @head: the head of the list
0270  */
0271 static inline int list_is_last(const struct list_head *list, const struct list_head *head)
0272 {
0273     return list->next == head;
0274 }
0275 
0276 /**
0277  * list_is_head - tests whether @list is the list @head
0278  * @list: the entry to test
0279  * @head: the head of the list
0280  */
0281 static inline int list_is_head(const struct list_head *list, const struct list_head *head)
0282 {
0283     return list == head;
0284 }
0285 
0286 /**
0287  * list_empty - tests whether a list is empty
0288  * @head: the list to test.
0289  */
0290 static inline int list_empty(const struct list_head *head)
0291 {
0292     return READ_ONCE(head->next) == head;
0293 }
0294 
0295 /**
0296  * list_del_init_careful - deletes entry from list and reinitialize it.
0297  * @entry: the element to delete from the list.
0298  *
0299  * This is the same as list_del_init(), except designed to be used
0300  * together with list_empty_careful() in a way to guarantee ordering
0301  * of other memory operations.
0302  *
0303  * Any memory operations done before a list_del_init_careful() are
0304  * guaranteed to be visible after a list_empty_careful() test.
0305  */
0306 static inline void list_del_init_careful(struct list_head *entry)
0307 {
0308     __list_del_entry(entry);
0309     WRITE_ONCE(entry->prev, entry);
0310     smp_store_release(&entry->next, entry);
0311 }
0312 
0313 /**
0314  * list_empty_careful - tests whether a list is empty and not being modified
0315  * @head: the list to test
0316  *
0317  * Description:
0318  * tests whether a list is empty _and_ checks that no other CPU might be
0319  * in the process of modifying either member (next or prev)
0320  *
0321  * NOTE: using list_empty_careful() without synchronization
0322  * can only be safe if the only activity that can happen
0323  * to the list entry is list_del_init(). Eg. it cannot be used
0324  * if another CPU could re-list_add() it.
0325  */
0326 static inline int list_empty_careful(const struct list_head *head)
0327 {
0328     struct list_head *next = smp_load_acquire(&head->next);
0329     return list_is_head(next, head) && (next == READ_ONCE(head->prev));
0330 }
0331 
0332 /**
0333  * list_rotate_left - rotate the list to the left
0334  * @head: the head of the list
0335  */
0336 static inline void list_rotate_left(struct list_head *head)
0337 {
0338     struct list_head *first;
0339 
0340     if (!list_empty(head)) {
0341         first = head->next;
0342         list_move_tail(first, head);
0343     }
0344 }
0345 
0346 /**
0347  * list_rotate_to_front() - Rotate list to specific item.
0348  * @list: The desired new front of the list.
0349  * @head: The head of the list.
0350  *
0351  * Rotates list so that @list becomes the new front of the list.
0352  */
0353 static inline void list_rotate_to_front(struct list_head *list,
0354                     struct list_head *head)
0355 {
0356     /*
0357      * Deletes the list head from the list denoted by @head and
0358      * places it as the tail of @list, this effectively rotates the
0359      * list so that @list is at the front.
0360      */
0361     list_move_tail(head, list);
0362 }
0363 
0364 /**
0365  * list_is_singular - tests whether a list has just one entry.
0366  * @head: the list to test.
0367  */
0368 static inline int list_is_singular(const struct list_head *head)
0369 {
0370     return !list_empty(head) && (head->next == head->prev);
0371 }
0372 
0373 static inline void __list_cut_position(struct list_head *list,
0374         struct list_head *head, struct list_head *entry)
0375 {
0376     struct list_head *new_first = entry->next;
0377     list->next = head->next;
0378     list->next->prev = list;
0379     list->prev = entry;
0380     entry->next = list;
0381     head->next = new_first;
0382     new_first->prev = head;
0383 }
0384 
0385 /**
0386  * list_cut_position - cut a list into two
0387  * @list: a new list to add all removed entries
0388  * @head: a list with entries
0389  * @entry: an entry within head, could be the head itself
0390  *  and if so we won't cut the list
0391  *
0392  * This helper moves the initial part of @head, up to and
0393  * including @entry, from @head to @list. You should
0394  * pass on @entry an element you know is on @head. @list
0395  * should be an empty list or a list you do not care about
0396  * losing its data.
0397  *
0398  */
0399 static inline void list_cut_position(struct list_head *list,
0400         struct list_head *head, struct list_head *entry)
0401 {
0402     if (list_empty(head))
0403         return;
0404     if (list_is_singular(head) && !list_is_head(entry, head) && (entry != head->next))
0405         return;
0406     if (list_is_head(entry, head))
0407         INIT_LIST_HEAD(list);
0408     else
0409         __list_cut_position(list, head, entry);
0410 }
0411 
0412 /**
0413  * list_cut_before - cut a list into two, before given entry
0414  * @list: a new list to add all removed entries
0415  * @head: a list with entries
0416  * @entry: an entry within head, could be the head itself
0417  *
0418  * This helper moves the initial part of @head, up to but
0419  * excluding @entry, from @head to @list.  You should pass
0420  * in @entry an element you know is on @head.  @list should
0421  * be an empty list or a list you do not care about losing
0422  * its data.
0423  * If @entry == @head, all entries on @head are moved to
0424  * @list.
0425  */
0426 static inline void list_cut_before(struct list_head *list,
0427                    struct list_head *head,
0428                    struct list_head *entry)
0429 {
0430     if (head->next == entry) {
0431         INIT_LIST_HEAD(list);
0432         return;
0433     }
0434     list->next = head->next;
0435     list->next->prev = list;
0436     list->prev = entry->prev;
0437     list->prev->next = list;
0438     head->next = entry;
0439     entry->prev = head;
0440 }
0441 
0442 static inline void __list_splice(const struct list_head *list,
0443                  struct list_head *prev,
0444                  struct list_head *next)
0445 {
0446     struct list_head *first = list->next;
0447     struct list_head *last = list->prev;
0448 
0449     first->prev = prev;
0450     prev->next = first;
0451 
0452     last->next = next;
0453     next->prev = last;
0454 }
0455 
0456 /**
0457  * list_splice - join two lists, this is designed for stacks
0458  * @list: the new list to add.
0459  * @head: the place to add it in the first list.
0460  */
0461 static inline void list_splice(const struct list_head *list,
0462                 struct list_head *head)
0463 {
0464     if (!list_empty(list))
0465         __list_splice(list, head, head->next);
0466 }
0467 
0468 /**
0469  * list_splice_tail - join two lists, each list being a queue
0470  * @list: the new list to add.
0471  * @head: the place to add it in the first list.
0472  */
0473 static inline void list_splice_tail(struct list_head *list,
0474                 struct list_head *head)
0475 {
0476     if (!list_empty(list))
0477         __list_splice(list, head->prev, head);
0478 }
0479 
0480 /**
0481  * list_splice_init - join two lists and reinitialise the emptied list.
0482  * @list: the new list to add.
0483  * @head: the place to add it in the first list.
0484  *
0485  * The list at @list is reinitialised
0486  */
0487 static inline void list_splice_init(struct list_head *list,
0488                     struct list_head *head)
0489 {
0490     if (!list_empty(list)) {
0491         __list_splice(list, head, head->next);
0492         INIT_LIST_HEAD(list);
0493     }
0494 }
0495 
0496 /**
0497  * list_splice_tail_init - join two lists and reinitialise the emptied list
0498  * @list: the new list to add.
0499  * @head: the place to add it in the first list.
0500  *
0501  * Each of the lists is a queue.
0502  * The list at @list is reinitialised
0503  */
0504 static inline void list_splice_tail_init(struct list_head *list,
0505                      struct list_head *head)
0506 {
0507     if (!list_empty(list)) {
0508         __list_splice(list, head->prev, head);
0509         INIT_LIST_HEAD(list);
0510     }
0511 }
0512 
0513 /**
0514  * list_entry - get the struct for this entry
0515  * @ptr:    the &struct list_head pointer.
0516  * @type:   the type of the struct this is embedded in.
0517  * @member: the name of the list_head within the struct.
0518  */
0519 #define list_entry(ptr, type, member) \
0520     container_of(ptr, type, member)
0521 
0522 /**
0523  * list_first_entry - get the first element from a list
0524  * @ptr:    the list head to take the element from.
0525  * @type:   the type of the struct this is embedded in.
0526  * @member: the name of the list_head within the struct.
0527  *
0528  * Note, that list is expected to be not empty.
0529  */
0530 #define list_first_entry(ptr, type, member) \
0531     list_entry((ptr)->next, type, member)
0532 
0533 /**
0534  * list_last_entry - get the last element from a list
0535  * @ptr:    the list head to take the element from.
0536  * @type:   the type of the struct this is embedded in.
0537  * @member: the name of the list_head within the struct.
0538  *
0539  * Note, that list is expected to be not empty.
0540  */
0541 #define list_last_entry(ptr, type, member) \
0542     list_entry((ptr)->prev, type, member)
0543 
0544 /**
0545  * list_first_entry_or_null - get the first element from a list
0546  * @ptr:    the list head to take the element from.
0547  * @type:   the type of the struct this is embedded in.
0548  * @member: the name of the list_head within the struct.
0549  *
0550  * Note that if the list is empty, it returns NULL.
0551  */
0552 #define list_first_entry_or_null(ptr, type, member) ({ \
0553     struct list_head *head__ = (ptr); \
0554     struct list_head *pos__ = READ_ONCE(head__->next); \
0555     pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
0556 })
0557 
0558 /**
0559  * list_next_entry - get the next element in list
0560  * @pos:    the type * to cursor
0561  * @member: the name of the list_head within the struct.
0562  */
0563 #define list_next_entry(pos, member) \
0564     list_entry((pos)->member.next, typeof(*(pos)), member)
0565 
0566 /**
0567  * list_next_entry_circular - get the next element in list
0568  * @pos:    the type * to cursor.
0569  * @head:   the list head to take the element from.
0570  * @member: the name of the list_head within the struct.
0571  *
0572  * Wraparound if pos is the last element (return the first element).
0573  * Note, that list is expected to be not empty.
0574  */
0575 #define list_next_entry_circular(pos, head, member) \
0576     (list_is_last(&(pos)->member, head) ? \
0577     list_first_entry(head, typeof(*(pos)), member) : list_next_entry(pos, member))
0578 
0579 /**
0580  * list_prev_entry - get the prev element in list
0581  * @pos:    the type * to cursor
0582  * @member: the name of the list_head within the struct.
0583  */
0584 #define list_prev_entry(pos, member) \
0585     list_entry((pos)->member.prev, typeof(*(pos)), member)
0586 
0587 /**
0588  * list_prev_entry_circular - get the prev element in list
0589  * @pos:    the type * to cursor.
0590  * @head:   the list head to take the element from.
0591  * @member: the name of the list_head within the struct.
0592  *
0593  * Wraparound if pos is the first element (return the last element).
0594  * Note, that list is expected to be not empty.
0595  */
0596 #define list_prev_entry_circular(pos, head, member) \
0597     (list_is_first(&(pos)->member, head) ? \
0598     list_last_entry(head, typeof(*(pos)), member) : list_prev_entry(pos, member))
0599 
0600 /**
0601  * list_for_each    -   iterate over a list
0602  * @pos:    the &struct list_head to use as a loop cursor.
0603  * @head:   the head for your list.
0604  */
0605 #define list_for_each(pos, head) \
0606     for (pos = (head)->next; !list_is_head(pos, (head)); pos = pos->next)
0607 
0608 /**
0609  * list_for_each_rcu - Iterate over a list in an RCU-safe fashion
0610  * @pos:    the &struct list_head to use as a loop cursor.
0611  * @head:   the head for your list.
0612  */
0613 #define list_for_each_rcu(pos, head)          \
0614     for (pos = rcu_dereference((head)->next); \
0615          !list_is_head(pos, (head)); \
0616          pos = rcu_dereference(pos->next))
0617 
0618 /**
0619  * list_for_each_continue - continue iteration over a list
0620  * @pos:    the &struct list_head to use as a loop cursor.
0621  * @head:   the head for your list.
0622  *
0623  * Continue to iterate over a list, continuing after the current position.
0624  */
0625 #define list_for_each_continue(pos, head) \
0626     for (pos = pos->next; !list_is_head(pos, (head)); pos = pos->next)
0627 
0628 /**
0629  * list_for_each_prev   -   iterate over a list backwards
0630  * @pos:    the &struct list_head to use as a loop cursor.
0631  * @head:   the head for your list.
0632  */
0633 #define list_for_each_prev(pos, head) \
0634     for (pos = (head)->prev; !list_is_head(pos, (head)); pos = pos->prev)
0635 
0636 /**
0637  * list_for_each_safe - iterate over a list safe against removal of list entry
0638  * @pos:    the &struct list_head to use as a loop cursor.
0639  * @n:      another &struct list_head to use as temporary storage
0640  * @head:   the head for your list.
0641  */
0642 #define list_for_each_safe(pos, n, head) \
0643     for (pos = (head)->next, n = pos->next; \
0644          !list_is_head(pos, (head)); \
0645          pos = n, n = pos->next)
0646 
0647 /**
0648  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
0649  * @pos:    the &struct list_head to use as a loop cursor.
0650  * @n:      another &struct list_head to use as temporary storage
0651  * @head:   the head for your list.
0652  */
0653 #define list_for_each_prev_safe(pos, n, head) \
0654     for (pos = (head)->prev, n = pos->prev; \
0655          !list_is_head(pos, (head)); \
0656          pos = n, n = pos->prev)
0657 
0658 /**
0659  * list_entry_is_head - test if the entry points to the head of the list
0660  * @pos:    the type * to cursor
0661  * @head:   the head for your list.
0662  * @member: the name of the list_head within the struct.
0663  */
0664 #define list_entry_is_head(pos, head, member)               \
0665     (&pos->member == (head))
0666 
0667 /**
0668  * list_for_each_entry  -   iterate over list of given type
0669  * @pos:    the type * to use as a loop cursor.
0670  * @head:   the head for your list.
0671  * @member: the name of the list_head within the struct.
0672  */
0673 #define list_for_each_entry(pos, head, member)              \
0674     for (pos = list_first_entry(head, typeof(*pos), member);    \
0675          !list_entry_is_head(pos, head, member);            \
0676          pos = list_next_entry(pos, member))
0677 
0678 /**
0679  * list_for_each_entry_reverse - iterate backwards over list of given type.
0680  * @pos:    the type * to use as a loop cursor.
0681  * @head:   the head for your list.
0682  * @member: the name of the list_head within the struct.
0683  */
0684 #define list_for_each_entry_reverse(pos, head, member)          \
0685     for (pos = list_last_entry(head, typeof(*pos), member);     \
0686          !list_entry_is_head(pos, head, member);            \
0687          pos = list_prev_entry(pos, member))
0688 
0689 /**
0690  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
0691  * @pos:    the type * to use as a start point
0692  * @head:   the head of the list
0693  * @member: the name of the list_head within the struct.
0694  *
0695  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
0696  */
0697 #define list_prepare_entry(pos, head, member) \
0698     ((pos) ? : list_entry(head, typeof(*pos), member))
0699 
0700 /**
0701  * list_for_each_entry_continue - continue iteration over list of given type
0702  * @pos:    the type * to use as a loop cursor.
0703  * @head:   the head for your list.
0704  * @member: the name of the list_head within the struct.
0705  *
0706  * Continue to iterate over list of given type, continuing after
0707  * the current position.
0708  */
0709 #define list_for_each_entry_continue(pos, head, member)         \
0710     for (pos = list_next_entry(pos, member);            \
0711          !list_entry_is_head(pos, head, member);            \
0712          pos = list_next_entry(pos, member))
0713 
0714 /**
0715  * list_for_each_entry_continue_reverse - iterate backwards from the given point
0716  * @pos:    the type * to use as a loop cursor.
0717  * @head:   the head for your list.
0718  * @member: the name of the list_head within the struct.
0719  *
0720  * Start to iterate over list of given type backwards, continuing after
0721  * the current position.
0722  */
0723 #define list_for_each_entry_continue_reverse(pos, head, member)     \
0724     for (pos = list_prev_entry(pos, member);            \
0725          !list_entry_is_head(pos, head, member);            \
0726          pos = list_prev_entry(pos, member))
0727 
0728 /**
0729  * list_for_each_entry_from - iterate over list of given type from the current point
0730  * @pos:    the type * to use as a loop cursor.
0731  * @head:   the head for your list.
0732  * @member: the name of the list_head within the struct.
0733  *
0734  * Iterate over list of given type, continuing from current position.
0735  */
0736 #define list_for_each_entry_from(pos, head, member)             \
0737     for (; !list_entry_is_head(pos, head, member);          \
0738          pos = list_next_entry(pos, member))
0739 
0740 /**
0741  * list_for_each_entry_from_reverse - iterate backwards over list of given type
0742  *                                    from the current point
0743  * @pos:    the type * to use as a loop cursor.
0744  * @head:   the head for your list.
0745  * @member: the name of the list_head within the struct.
0746  *
0747  * Iterate backwards over list of given type, continuing from current position.
0748  */
0749 #define list_for_each_entry_from_reverse(pos, head, member)     \
0750     for (; !list_entry_is_head(pos, head, member);          \
0751          pos = list_prev_entry(pos, member))
0752 
0753 /**
0754  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
0755  * @pos:    the type * to use as a loop cursor.
0756  * @n:      another type * to use as temporary storage
0757  * @head:   the head for your list.
0758  * @member: the name of the list_head within the struct.
0759  */
0760 #define list_for_each_entry_safe(pos, n, head, member)          \
0761     for (pos = list_first_entry(head, typeof(*pos), member),    \
0762         n = list_next_entry(pos, member);           \
0763          !list_entry_is_head(pos, head, member);            \
0764          pos = n, n = list_next_entry(n, member))
0765 
0766 /**
0767  * list_for_each_entry_safe_continue - continue list iteration safe against removal
0768  * @pos:    the type * to use as a loop cursor.
0769  * @n:      another type * to use as temporary storage
0770  * @head:   the head for your list.
0771  * @member: the name of the list_head within the struct.
0772  *
0773  * Iterate over list of given type, continuing after current point,
0774  * safe against removal of list entry.
0775  */
0776 #define list_for_each_entry_safe_continue(pos, n, head, member)         \
0777     for (pos = list_next_entry(pos, member),                \
0778         n = list_next_entry(pos, member);               \
0779          !list_entry_is_head(pos, head, member);                \
0780          pos = n, n = list_next_entry(n, member))
0781 
0782 /**
0783  * list_for_each_entry_safe_from - iterate over list from current point safe against removal
0784  * @pos:    the type * to use as a loop cursor.
0785  * @n:      another type * to use as temporary storage
0786  * @head:   the head for your list.
0787  * @member: the name of the list_head within the struct.
0788  *
0789  * Iterate over list of given type from current point, safe against
0790  * removal of list entry.
0791  */
0792 #define list_for_each_entry_safe_from(pos, n, head, member)             \
0793     for (n = list_next_entry(pos, member);                  \
0794          !list_entry_is_head(pos, head, member);                \
0795          pos = n, n = list_next_entry(n, member))
0796 
0797 /**
0798  * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
0799  * @pos:    the type * to use as a loop cursor.
0800  * @n:      another type * to use as temporary storage
0801  * @head:   the head for your list.
0802  * @member: the name of the list_head within the struct.
0803  *
0804  * Iterate backwards over list of given type, safe against removal
0805  * of list entry.
0806  */
0807 #define list_for_each_entry_safe_reverse(pos, n, head, member)      \
0808     for (pos = list_last_entry(head, typeof(*pos), member),     \
0809         n = list_prev_entry(pos, member);           \
0810          !list_entry_is_head(pos, head, member);            \
0811          pos = n, n = list_prev_entry(n, member))
0812 
0813 /**
0814  * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
0815  * @pos:    the loop cursor used in the list_for_each_entry_safe loop
0816  * @n:      temporary storage used in list_for_each_entry_safe
0817  * @member: the name of the list_head within the struct.
0818  *
0819  * list_safe_reset_next is not safe to use in general if the list may be
0820  * modified concurrently (eg. the lock is dropped in the loop body). An
0821  * exception to this is if the cursor element (pos) is pinned in the list,
0822  * and list_safe_reset_next is called after re-taking the lock and before
0823  * completing the current iteration of the loop body.
0824  */
0825 #define list_safe_reset_next(pos, n, member)                \
0826     n = list_next_entry(pos, member)
0827 
0828 /*
0829  * Double linked lists with a single pointer list head.
0830  * Mostly useful for hash tables where the two pointer list head is
0831  * too wasteful.
0832  * You lose the ability to access the tail in O(1).
0833  */
0834 
0835 #define HLIST_HEAD_INIT { .first = NULL }
0836 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
0837 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
0838 static inline void INIT_HLIST_NODE(struct hlist_node *h)
0839 {
0840     h->next = NULL;
0841     h->pprev = NULL;
0842 }
0843 
0844 /**
0845  * hlist_unhashed - Has node been removed from list and reinitialized?
0846  * @h: Node to be checked
0847  *
0848  * Not that not all removal functions will leave a node in unhashed
0849  * state.  For example, hlist_nulls_del_init_rcu() does leave the
0850  * node in unhashed state, but hlist_nulls_del() does not.
0851  */
0852 static inline int hlist_unhashed(const struct hlist_node *h)
0853 {
0854     return !h->pprev;
0855 }
0856 
0857 /**
0858  * hlist_unhashed_lockless - Version of hlist_unhashed for lockless use
0859  * @h: Node to be checked
0860  *
0861  * This variant of hlist_unhashed() must be used in lockless contexts
0862  * to avoid potential load-tearing.  The READ_ONCE() is paired with the
0863  * various WRITE_ONCE() in hlist helpers that are defined below.
0864  */
0865 static inline int hlist_unhashed_lockless(const struct hlist_node *h)
0866 {
0867     return !READ_ONCE(h->pprev);
0868 }
0869 
0870 /**
0871  * hlist_empty - Is the specified hlist_head structure an empty hlist?
0872  * @h: Structure to check.
0873  */
0874 static inline int hlist_empty(const struct hlist_head *h)
0875 {
0876     return !READ_ONCE(h->first);
0877 }
0878 
0879 static inline void __hlist_del(struct hlist_node *n)
0880 {
0881     struct hlist_node *next = n->next;
0882     struct hlist_node **pprev = n->pprev;
0883 
0884     WRITE_ONCE(*pprev, next);
0885     if (next)
0886         WRITE_ONCE(next->pprev, pprev);
0887 }
0888 
0889 /**
0890  * hlist_del - Delete the specified hlist_node from its list
0891  * @n: Node to delete.
0892  *
0893  * Note that this function leaves the node in hashed state.  Use
0894  * hlist_del_init() or similar instead to unhash @n.
0895  */
0896 static inline void hlist_del(struct hlist_node *n)
0897 {
0898     __hlist_del(n);
0899     n->next = LIST_POISON1;
0900     n->pprev = LIST_POISON2;
0901 }
0902 
0903 /**
0904  * hlist_del_init - Delete the specified hlist_node from its list and initialize
0905  * @n: Node to delete.
0906  *
0907  * Note that this function leaves the node in unhashed state.
0908  */
0909 static inline void hlist_del_init(struct hlist_node *n)
0910 {
0911     if (!hlist_unhashed(n)) {
0912         __hlist_del(n);
0913         INIT_HLIST_NODE(n);
0914     }
0915 }
0916 
0917 /**
0918  * hlist_add_head - add a new entry at the beginning of the hlist
0919  * @n: new entry to be added
0920  * @h: hlist head to add it after
0921  *
0922  * Insert a new entry after the specified head.
0923  * This is good for implementing stacks.
0924  */
0925 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
0926 {
0927     struct hlist_node *first = h->first;
0928     WRITE_ONCE(n->next, first);
0929     if (first)
0930         WRITE_ONCE(first->pprev, &n->next);
0931     WRITE_ONCE(h->first, n);
0932     WRITE_ONCE(n->pprev, &h->first);
0933 }
0934 
0935 /**
0936  * hlist_add_before - add a new entry before the one specified
0937  * @n: new entry to be added
0938  * @next: hlist node to add it before, which must be non-NULL
0939  */
0940 static inline void hlist_add_before(struct hlist_node *n,
0941                     struct hlist_node *next)
0942 {
0943     WRITE_ONCE(n->pprev, next->pprev);
0944     WRITE_ONCE(n->next, next);
0945     WRITE_ONCE(next->pprev, &n->next);
0946     WRITE_ONCE(*(n->pprev), n);
0947 }
0948 
0949 /**
0950  * hlist_add_behind - add a new entry after the one specified
0951  * @n: new entry to be added
0952  * @prev: hlist node to add it after, which must be non-NULL
0953  */
0954 static inline void hlist_add_behind(struct hlist_node *n,
0955                     struct hlist_node *prev)
0956 {
0957     WRITE_ONCE(n->next, prev->next);
0958     WRITE_ONCE(prev->next, n);
0959     WRITE_ONCE(n->pprev, &prev->next);
0960 
0961     if (n->next)
0962         WRITE_ONCE(n->next->pprev, &n->next);
0963 }
0964 
0965 /**
0966  * hlist_add_fake - create a fake hlist consisting of a single headless node
0967  * @n: Node to make a fake list out of
0968  *
0969  * This makes @n appear to be its own predecessor on a headless hlist.
0970  * The point of this is to allow things like hlist_del() to work correctly
0971  * in cases where there is no list.
0972  */
0973 static inline void hlist_add_fake(struct hlist_node *n)
0974 {
0975     n->pprev = &n->next;
0976 }
0977 
0978 /**
0979  * hlist_fake: Is this node a fake hlist?
0980  * @h: Node to check for being a self-referential fake hlist.
0981  */
0982 static inline bool hlist_fake(struct hlist_node *h)
0983 {
0984     return h->pprev == &h->next;
0985 }
0986 
0987 /**
0988  * hlist_is_singular_node - is node the only element of the specified hlist?
0989  * @n: Node to check for singularity.
0990  * @h: Header for potentially singular list.
0991  *
0992  * Check whether the node is the only node of the head without
0993  * accessing head, thus avoiding unnecessary cache misses.
0994  */
0995 static inline bool
0996 hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
0997 {
0998     return !n->next && n->pprev == &h->first;
0999 }
1000 
1001 /**
1002  * hlist_move_list - Move an hlist
1003  * @old: hlist_head for old list.
1004  * @new: hlist_head for new list.
1005  *
1006  * Move a list from one list head to another. Fixup the pprev
1007  * reference of the first entry if it exists.
1008  */
1009 static inline void hlist_move_list(struct hlist_head *old,
1010                    struct hlist_head *new)
1011 {
1012     new->first = old->first;
1013     if (new->first)
1014         new->first->pprev = &new->first;
1015     old->first = NULL;
1016 }
1017 
1018 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1019 
1020 #define hlist_for_each(pos, head) \
1021     for (pos = (head)->first; pos ; pos = pos->next)
1022 
1023 #define hlist_for_each_safe(pos, n, head) \
1024     for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1025          pos = n)
1026 
1027 #define hlist_entry_safe(ptr, type, member) \
1028     ({ typeof(ptr) ____ptr = (ptr); \
1029        ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
1030     })
1031 
1032 /**
1033  * hlist_for_each_entry - iterate over list of given type
1034  * @pos:    the type * to use as a loop cursor.
1035  * @head:   the head for your list.
1036  * @member: the name of the hlist_node within the struct.
1037  */
1038 #define hlist_for_each_entry(pos, head, member)             \
1039     for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
1040          pos;                           \
1041          pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1042 
1043 /**
1044  * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1045  * @pos:    the type * to use as a loop cursor.
1046  * @member: the name of the hlist_node within the struct.
1047  */
1048 #define hlist_for_each_entry_continue(pos, member)          \
1049     for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
1050          pos;                           \
1051          pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1052 
1053 /**
1054  * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1055  * @pos:    the type * to use as a loop cursor.
1056  * @member: the name of the hlist_node within the struct.
1057  */
1058 #define hlist_for_each_entry_from(pos, member)              \
1059     for (; pos;                         \
1060          pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1061 
1062 /**
1063  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1064  * @pos:    the type * to use as a loop cursor.
1065  * @n:      a &struct hlist_node to use as temporary storage
1066  * @head:   the head for your list.
1067  * @member: the name of the hlist_node within the struct.
1068  */
1069 #define hlist_for_each_entry_safe(pos, n, head, member)         \
1070     for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
1071          pos && ({ n = pos->member.next; 1; });         \
1072          pos = hlist_entry_safe(n, typeof(*pos), member))
1073 
1074 #endif