| .. | .. |
|---|
| 23 | 23 | #define LIST_HEAD(name) \ |
|---|
| 24 | 24 | struct list_head name = LIST_HEAD_INIT(name) |
|---|
| 25 | 25 | |
|---|
| 26 | +/** |
|---|
| 27 | + * INIT_LIST_HEAD - Initialize a list_head structure |
|---|
| 28 | + * @list: list_head structure to be initialized. |
|---|
| 29 | + * |
|---|
| 30 | + * Initializes the list_head to point to itself. If it is a list header, |
|---|
| 31 | + * the result is an empty list. |
|---|
| 32 | + */ |
|---|
| 26 | 33 | static inline void INIT_LIST_HEAD(struct list_head *list) |
|---|
| 27 | 34 | { |
|---|
| 28 | 35 | WRITE_ONCE(list->next, list); |
|---|
| .. | .. |
|---|
| 106 | 113 | WRITE_ONCE(prev->next, next); |
|---|
| 107 | 114 | } |
|---|
| 108 | 115 | |
|---|
| 109 | | -/** |
|---|
| 110 | | - * list_del - deletes entry from list. |
|---|
| 111 | | - * @entry: the element to delete from the list. |
|---|
| 112 | | - * Note: list_empty() on entry does not return true after this, the entry is |
|---|
| 113 | | - * in an undefined state. |
|---|
| 116 | +/* |
|---|
| 117 | + * Delete a list entry and clear the 'prev' pointer. |
|---|
| 118 | + * |
|---|
| 119 | + * This is a special-purpose list clearing method used in the networking code |
|---|
| 120 | + * for lists allocated as per-cpu, where we don't want to incur the extra |
|---|
| 121 | + * WRITE_ONCE() overhead of a regular list_del_init(). The code that uses this |
|---|
| 122 | + * needs to check the node 'prev' pointer instead of calling list_empty(). |
|---|
| 114 | 123 | */ |
|---|
| 124 | +static inline void __list_del_clearprev(struct list_head *entry) |
|---|
| 125 | +{ |
|---|
| 126 | + __list_del(entry->prev, entry->next); |
|---|
| 127 | + entry->prev = NULL; |
|---|
| 128 | +} |
|---|
| 129 | + |
|---|
| 115 | 130 | static inline void __list_del_entry(struct list_head *entry) |
|---|
| 116 | 131 | { |
|---|
| 117 | 132 | if (!__list_del_entry_valid(entry)) |
|---|
| .. | .. |
|---|
| 120 | 135 | __list_del(entry->prev, entry->next); |
|---|
| 121 | 136 | } |
|---|
| 122 | 137 | |
|---|
| 138 | +/** |
|---|
| 139 | + * list_del - deletes entry from list. |
|---|
| 140 | + * @entry: the element to delete from the list. |
|---|
| 141 | + * Note: list_empty() on entry does not return true after this, the entry is |
|---|
| 142 | + * in an undefined state. |
|---|
| 143 | + */ |
|---|
| 123 | 144 | static inline void list_del(struct list_head *entry) |
|---|
| 124 | 145 | { |
|---|
| 125 | 146 | __list_del_entry(entry); |
|---|
| .. | .. |
|---|
| 143 | 164 | new->prev->next = new; |
|---|
| 144 | 165 | } |
|---|
| 145 | 166 | |
|---|
| 167 | +/** |
|---|
| 168 | + * list_replace_init - replace old entry by new one and initialize the old one |
|---|
| 169 | + * @old : the element to be replaced |
|---|
| 170 | + * @new : the new element to insert |
|---|
| 171 | + * |
|---|
| 172 | + * If @old was empty, it will be overwritten. |
|---|
| 173 | + */ |
|---|
| 146 | 174 | static inline void list_replace_init(struct list_head *old, |
|---|
| 147 | | - struct list_head *new) |
|---|
| 175 | + struct list_head *new) |
|---|
| 148 | 176 | { |
|---|
| 149 | 177 | list_replace(old, new); |
|---|
| 150 | 178 | INIT_LIST_HEAD(old); |
|---|
| 179 | +} |
|---|
| 180 | + |
|---|
| 181 | +/** |
|---|
| 182 | + * list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position |
|---|
| 183 | + * @entry1: the location to place entry2 |
|---|
| 184 | + * @entry2: the location to place entry1 |
|---|
| 185 | + */ |
|---|
| 186 | +static inline void list_swap(struct list_head *entry1, |
|---|
| 187 | + struct list_head *entry2) |
|---|
| 188 | +{ |
|---|
| 189 | + struct list_head *pos = entry2->prev; |
|---|
| 190 | + |
|---|
| 191 | + list_del(entry2); |
|---|
| 192 | + list_replace(entry1, entry2); |
|---|
| 193 | + if (pos == entry1) |
|---|
| 194 | + pos = entry2; |
|---|
| 195 | + list_add(entry1, pos); |
|---|
| 151 | 196 | } |
|---|
| 152 | 197 | |
|---|
| 153 | 198 | /** |
|---|
| .. | .. |
|---|
| 184 | 229 | } |
|---|
| 185 | 230 | |
|---|
| 186 | 231 | /** |
|---|
| 232 | + * list_bulk_move_tail - move a subsection of a list to its tail |
|---|
| 233 | + * @head: the head that will follow our entry |
|---|
| 234 | + * @first: first entry to move |
|---|
| 235 | + * @last: last entry to move, can be the same as first |
|---|
| 236 | + * |
|---|
| 237 | + * Move all entries between @first and including @last before @head. |
|---|
| 238 | + * All three entries must belong to the same linked list. |
|---|
| 239 | + */ |
|---|
| 240 | +static inline void list_bulk_move_tail(struct list_head *head, |
|---|
| 241 | + struct list_head *first, |
|---|
| 242 | + struct list_head *last) |
|---|
| 243 | +{ |
|---|
| 244 | + first->prev->next = last->next; |
|---|
| 245 | + last->next->prev = first->prev; |
|---|
| 246 | + |
|---|
| 247 | + head->prev->next = first; |
|---|
| 248 | + first->prev = head->prev; |
|---|
| 249 | + |
|---|
| 250 | + last->next = head; |
|---|
| 251 | + head->prev = last; |
|---|
| 252 | +} |
|---|
| 253 | + |
|---|
| 254 | +/** |
|---|
| 255 | + * list_is_first -- tests whether @list is the first entry in list @head |
|---|
| 256 | + * @list: the entry to test |
|---|
| 257 | + * @head: the head of the list |
|---|
| 258 | + */ |
|---|
| 259 | +static inline int list_is_first(const struct list_head *list, |
|---|
| 260 | + const struct list_head *head) |
|---|
| 261 | +{ |
|---|
| 262 | + return list->prev == head; |
|---|
| 263 | +} |
|---|
| 264 | + |
|---|
| 265 | +/** |
|---|
| 187 | 266 | * list_is_last - tests whether @list is the last entry in list @head |
|---|
| 188 | 267 | * @list: the entry to test |
|---|
| 189 | 268 | * @head: the head of the list |
|---|
| .. | .. |
|---|
| 204 | 283 | } |
|---|
| 205 | 284 | |
|---|
| 206 | 285 | /** |
|---|
| 286 | + * list_del_init_careful - deletes entry from list and reinitialize it. |
|---|
| 287 | + * @entry: the element to delete from the list. |
|---|
| 288 | + * |
|---|
| 289 | + * This is the same as list_del_init(), except designed to be used |
|---|
| 290 | + * together with list_empty_careful() in a way to guarantee ordering |
|---|
| 291 | + * of other memory operations. |
|---|
| 292 | + * |
|---|
| 293 | + * Any memory operations done before a list_del_init_careful() are |
|---|
| 294 | + * guaranteed to be visible after a list_empty_careful() test. |
|---|
| 295 | + */ |
|---|
| 296 | +static inline void list_del_init_careful(struct list_head *entry) |
|---|
| 297 | +{ |
|---|
| 298 | + __list_del_entry(entry); |
|---|
| 299 | + entry->prev = entry; |
|---|
| 300 | + smp_store_release(&entry->next, entry); |
|---|
| 301 | +} |
|---|
| 302 | + |
|---|
| 303 | +/** |
|---|
| 207 | 304 | * list_empty_careful - tests whether a list is empty and not being modified |
|---|
| 208 | 305 | * @head: the list to test |
|---|
| 209 | 306 | * |
|---|
| .. | .. |
|---|
| 218 | 315 | */ |
|---|
| 219 | 316 | static inline int list_empty_careful(const struct list_head *head) |
|---|
| 220 | 317 | { |
|---|
| 221 | | - struct list_head *next = head->next; |
|---|
| 318 | + struct list_head *next = smp_load_acquire(&head->next); |
|---|
| 222 | 319 | return (next == head) && (next == head->prev); |
|---|
| 223 | 320 | } |
|---|
| 224 | 321 | |
|---|
| .. | .. |
|---|
| 234 | 331 | first = head->next; |
|---|
| 235 | 332 | list_move_tail(first, head); |
|---|
| 236 | 333 | } |
|---|
| 334 | +} |
|---|
| 335 | + |
|---|
| 336 | +/** |
|---|
| 337 | + * list_rotate_to_front() - Rotate list to specific item. |
|---|
| 338 | + * @list: The desired new front of the list. |
|---|
| 339 | + * @head: The head of the list. |
|---|
| 340 | + * |
|---|
| 341 | + * Rotates list so that @list becomes the new front of the list. |
|---|
| 342 | + */ |
|---|
| 343 | +static inline void list_rotate_to_front(struct list_head *list, |
|---|
| 344 | + struct list_head *head) |
|---|
| 345 | +{ |
|---|
| 346 | + /* |
|---|
| 347 | + * Deletes the list head from the list denoted by @head and |
|---|
| 348 | + * places it as the tail of @list, this effectively rotates the |
|---|
| 349 | + * list so that @list is at the front. |
|---|
| 350 | + */ |
|---|
| 351 | + list_move_tail(head, list); |
|---|
| 237 | 352 | } |
|---|
| 238 | 353 | |
|---|
| 239 | 354 | /** |
|---|
| .. | .. |
|---|
| 456 | 571 | for (pos = (head)->next; pos != (head); pos = pos->next) |
|---|
| 457 | 572 | |
|---|
| 458 | 573 | /** |
|---|
| 574 | + * list_for_each_continue - continue iteration over a list |
|---|
| 575 | + * @pos: the &struct list_head to use as a loop cursor. |
|---|
| 576 | + * @head: the head for your list. |
|---|
| 577 | + * |
|---|
| 578 | + * Continue to iterate over a list, continuing after the current position. |
|---|
| 579 | + */ |
|---|
| 580 | +#define list_for_each_continue(pos, head) \ |
|---|
| 581 | + for (pos = pos->next; pos != (head); pos = pos->next) |
|---|
| 582 | + |
|---|
| 583 | +/** |
|---|
| 459 | 584 | * list_for_each_prev - iterate over a list backwards |
|---|
| 460 | 585 | * @pos: the &struct list_head to use as a loop cursor. |
|---|
| 461 | 586 | * @head: the head for your list. |
|---|
| .. | .. |
|---|
| 670 | 795 | h->pprev = NULL; |
|---|
| 671 | 796 | } |
|---|
| 672 | 797 | |
|---|
| 798 | +/** |
|---|
| 799 | + * hlist_unhashed - Has node been removed from list and reinitialized? |
|---|
| 800 | + * @h: Node to be checked |
|---|
| 801 | + * |
|---|
| 802 | + * Not that not all removal functions will leave a node in unhashed |
|---|
| 803 | + * state. For example, hlist_nulls_del_init_rcu() does leave the |
|---|
| 804 | + * node in unhashed state, but hlist_nulls_del() does not. |
|---|
| 805 | + */ |
|---|
| 673 | 806 | static inline int hlist_unhashed(const struct hlist_node *h) |
|---|
| 674 | 807 | { |
|---|
| 675 | 808 | return !h->pprev; |
|---|
| 676 | 809 | } |
|---|
| 677 | 810 | |
|---|
| 811 | +/** |
|---|
| 812 | + * hlist_unhashed_lockless - Version of hlist_unhashed for lockless use |
|---|
| 813 | + * @h: Node to be checked |
|---|
| 814 | + * |
|---|
| 815 | + * This variant of hlist_unhashed() must be used in lockless contexts |
|---|
| 816 | + * to avoid potential load-tearing. The READ_ONCE() is paired with the |
|---|
| 817 | + * various WRITE_ONCE() in hlist helpers that are defined below. |
|---|
| 818 | + */ |
|---|
| 819 | +static inline int hlist_unhashed_lockless(const struct hlist_node *h) |
|---|
| 820 | +{ |
|---|
| 821 | + return !READ_ONCE(h->pprev); |
|---|
| 822 | +} |
|---|
| 823 | + |
|---|
| 824 | +/** |
|---|
| 825 | + * hlist_empty - Is the specified hlist_head structure an empty hlist? |
|---|
| 826 | + * @h: Structure to check. |
|---|
| 827 | + */ |
|---|
| 678 | 828 | static inline int hlist_empty(const struct hlist_head *h) |
|---|
| 679 | 829 | { |
|---|
| 680 | 830 | return !READ_ONCE(h->first); |
|---|
| .. | .. |
|---|
| 687 | 837 | |
|---|
| 688 | 838 | WRITE_ONCE(*pprev, next); |
|---|
| 689 | 839 | if (next) |
|---|
| 690 | | - next->pprev = pprev; |
|---|
| 840 | + WRITE_ONCE(next->pprev, pprev); |
|---|
| 691 | 841 | } |
|---|
| 692 | 842 | |
|---|
| 843 | +/** |
|---|
| 844 | + * hlist_del - Delete the specified hlist_node from its list |
|---|
| 845 | + * @n: Node to delete. |
|---|
| 846 | + * |
|---|
| 847 | + * Note that this function leaves the node in hashed state. Use |
|---|
| 848 | + * hlist_del_init() or similar instead to unhash @n. |
|---|
| 849 | + */ |
|---|
| 693 | 850 | static inline void hlist_del(struct hlist_node *n) |
|---|
| 694 | 851 | { |
|---|
| 695 | 852 | __hlist_del(n); |
|---|
| .. | .. |
|---|
| 697 | 854 | n->pprev = LIST_POISON2; |
|---|
| 698 | 855 | } |
|---|
| 699 | 856 | |
|---|
| 857 | +/** |
|---|
| 858 | + * hlist_del_init - Delete the specified hlist_node from its list and initialize |
|---|
| 859 | + * @n: Node to delete. |
|---|
| 860 | + * |
|---|
| 861 | + * Note that this function leaves the node in unhashed state. |
|---|
| 862 | + */ |
|---|
| 700 | 863 | static inline void hlist_del_init(struct hlist_node *n) |
|---|
| 701 | 864 | { |
|---|
| 702 | 865 | if (!hlist_unhashed(n)) { |
|---|
| .. | .. |
|---|
| 705 | 868 | } |
|---|
| 706 | 869 | } |
|---|
| 707 | 870 | |
|---|
| 871 | +/** |
|---|
| 872 | + * hlist_add_head - add a new entry at the beginning of the hlist |
|---|
| 873 | + * @n: new entry to be added |
|---|
| 874 | + * @h: hlist head to add it after |
|---|
| 875 | + * |
|---|
| 876 | + * Insert a new entry after the specified head. |
|---|
| 877 | + * This is good for implementing stacks. |
|---|
| 878 | + */ |
|---|
| 708 | 879 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
|---|
| 709 | 880 | { |
|---|
| 710 | 881 | struct hlist_node *first = h->first; |
|---|
| 711 | | - n->next = first; |
|---|
| 882 | + WRITE_ONCE(n->next, first); |
|---|
| 712 | 883 | if (first) |
|---|
| 713 | | - first->pprev = &n->next; |
|---|
| 884 | + WRITE_ONCE(first->pprev, &n->next); |
|---|
| 714 | 885 | WRITE_ONCE(h->first, n); |
|---|
| 715 | | - n->pprev = &h->first; |
|---|
| 886 | + WRITE_ONCE(n->pprev, &h->first); |
|---|
| 716 | 887 | } |
|---|
| 717 | 888 | |
|---|
| 718 | | -/* next must be != NULL */ |
|---|
| 889 | +/** |
|---|
| 890 | + * hlist_add_before - add a new entry before the one specified |
|---|
| 891 | + * @n: new entry to be added |
|---|
| 892 | + * @next: hlist node to add it before, which must be non-NULL |
|---|
| 893 | + */ |
|---|
| 719 | 894 | static inline void hlist_add_before(struct hlist_node *n, |
|---|
| 720 | | - struct hlist_node *next) |
|---|
| 895 | + struct hlist_node *next) |
|---|
| 721 | 896 | { |
|---|
| 722 | | - n->pprev = next->pprev; |
|---|
| 723 | | - n->next = next; |
|---|
| 724 | | - next->pprev = &n->next; |
|---|
| 897 | + WRITE_ONCE(n->pprev, next->pprev); |
|---|
| 898 | + WRITE_ONCE(n->next, next); |
|---|
| 899 | + WRITE_ONCE(next->pprev, &n->next); |
|---|
| 725 | 900 | WRITE_ONCE(*(n->pprev), n); |
|---|
| 726 | 901 | } |
|---|
| 727 | 902 | |
|---|
| 903 | +/** |
|---|
| 904 | + * hlist_add_behing - add a new entry after the one specified |
|---|
| 905 | + * @n: new entry to be added |
|---|
| 906 | + * @prev: hlist node to add it after, which must be non-NULL |
|---|
| 907 | + */ |
|---|
| 728 | 908 | static inline void hlist_add_behind(struct hlist_node *n, |
|---|
| 729 | 909 | struct hlist_node *prev) |
|---|
| 730 | 910 | { |
|---|
| 731 | | - n->next = prev->next; |
|---|
| 911 | + WRITE_ONCE(n->next, prev->next); |
|---|
| 732 | 912 | WRITE_ONCE(prev->next, n); |
|---|
| 733 | | - n->pprev = &prev->next; |
|---|
| 913 | + WRITE_ONCE(n->pprev, &prev->next); |
|---|
| 734 | 914 | |
|---|
| 735 | 915 | if (n->next) |
|---|
| 736 | | - n->next->pprev = &n->next; |
|---|
| 916 | + WRITE_ONCE(n->next->pprev, &n->next); |
|---|
| 737 | 917 | } |
|---|
| 738 | 918 | |
|---|
| 739 | | -/* after that we'll appear to be on some hlist and hlist_del will work */ |
|---|
| 919 | +/** |
|---|
| 920 | + * hlist_add_fake - create a fake hlist consisting of a single headless node |
|---|
| 921 | + * @n: Node to make a fake list out of |
|---|
| 922 | + * |
|---|
| 923 | + * This makes @n appear to be its own predecessor on a headless hlist. |
|---|
| 924 | + * The point of this is to allow things like hlist_del() to work correctly |
|---|
| 925 | + * in cases where there is no list. |
|---|
| 926 | + */ |
|---|
| 740 | 927 | static inline void hlist_add_fake(struct hlist_node *n) |
|---|
| 741 | 928 | { |
|---|
| 742 | 929 | n->pprev = &n->next; |
|---|
| 743 | 930 | } |
|---|
| 744 | 931 | |
|---|
| 932 | +/** |
|---|
| 933 | + * hlist_fake: Is this node a fake hlist? |
|---|
| 934 | + * @h: Node to check for being a self-referential fake hlist. |
|---|
| 935 | + */ |
|---|
| 745 | 936 | static inline bool hlist_fake(struct hlist_node *h) |
|---|
| 746 | 937 | { |
|---|
| 747 | 938 | return h->pprev == &h->next; |
|---|
| 748 | 939 | } |
|---|
| 749 | 940 | |
|---|
| 750 | | -/* |
|---|
| 941 | +/** |
|---|
| 942 | + * hlist_is_singular_node - is node the only element of the specified hlist? |
|---|
| 943 | + * @n: Node to check for singularity. |
|---|
| 944 | + * @h: Header for potentially singular list. |
|---|
| 945 | + * |
|---|
| 751 | 946 | * Check whether the node is the only node of the head without |
|---|
| 752 | | - * accessing head: |
|---|
| 947 | + * accessing head, thus avoiding unnecessary cache misses. |
|---|
| 753 | 948 | */ |
|---|
| 754 | 949 | static inline bool |
|---|
| 755 | 950 | hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h) |
|---|
| .. | .. |
|---|
| 757 | 952 | return !n->next && n->pprev == &h->first; |
|---|
| 758 | 953 | } |
|---|
| 759 | 954 | |
|---|
| 760 | | -/* |
|---|
| 955 | +/** |
|---|
| 956 | + * hlist_move_list - Move an hlist |
|---|
| 957 | + * @old: hlist_head for old list. |
|---|
| 958 | + * @new: hlist_head for new list. |
|---|
| 959 | + * |
|---|
| 761 | 960 | * Move a list from one list head to another. Fixup the pprev |
|---|
| 762 | 961 | * reference of the first entry if it exists. |
|---|
| 763 | 962 | */ |
|---|
| .. | .. |
|---|
| 817 | 1016 | /** |
|---|
| 818 | 1017 | * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
|---|
| 819 | 1018 | * @pos: the type * to use as a loop cursor. |
|---|
| 820 | | - * @n: another &struct hlist_node to use as temporary storage |
|---|
| 1019 | + * @n: a &struct hlist_node to use as temporary storage |
|---|
| 821 | 1020 | * @head: the head for your list. |
|---|
| 822 | 1021 | * @member: the name of the hlist_node within the struct. |
|---|
| 823 | 1022 | */ |
|---|