Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef _LINUX_LIST_H |
| 3 | #define _LINUX_LIST_H |
| 4 | |
Chris Metcalf | de5d9bf | 2010-07-02 13:41:14 -0400 | [diff] [blame] | 5 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/stddef.h> |
Randy Dunlap | c9cf552 | 2006-06-27 02:53:52 -0700 | [diff] [blame] | 7 | #include <linux/poison.h> |
Linus Torvalds | e66eed6 | 2011-05-19 14:15:29 -0700 | [diff] [blame] | 8 | #include <linux/const.h> |
Masahiro Yamada | 8b21d9c | 2014-10-13 15:51:30 -0700 | [diff] [blame] | 9 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
| 11 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
| 22 | |
| 23 | #define LIST_HEAD(name) \ |
| 24 | struct list_head name = LIST_HEAD_INIT(name) |
| 25 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 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 | */ |
Zach Brown | 490d6ab | 2006-02-03 03:03:56 -0800 | [diff] [blame] | 33 | static inline void INIT_LIST_HEAD(struct list_head *list) |
| 34 | { |
Paul E. McKenney | 2f07384 | 2015-10-12 16:56:42 -0700 | [diff] [blame] | 35 | WRITE_ONCE(list->next, list); |
Zach Brown | 490d6ab | 2006-02-03 03:03:56 -0800 | [diff] [blame] | 36 | list->prev = list; |
| 37 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Kees Cook | d7c8167 | 2016-08-17 14:42:08 -0700 | [diff] [blame] | 39 | #ifdef CONFIG_DEBUG_LIST |
| 40 | extern bool __list_add_valid(struct list_head *new, |
| 41 | struct list_head *prev, |
| 42 | struct list_head *next); |
Kees Cook | 0cd340d | 2016-08-17 14:42:10 -0700 | [diff] [blame] | 43 | extern bool __list_del_entry_valid(struct list_head *entry); |
Kees Cook | d7c8167 | 2016-08-17 14:42:08 -0700 | [diff] [blame] | 44 | #else |
| 45 | static inline bool __list_add_valid(struct list_head *new, |
| 46 | struct list_head *prev, |
| 47 | struct list_head *next) |
| 48 | { |
| 49 | return true; |
| 50 | } |
Kees Cook | 0cd340d | 2016-08-17 14:42:10 -0700 | [diff] [blame] | 51 | static inline bool __list_del_entry_valid(struct list_head *entry) |
| 52 | { |
| 53 | return true; |
| 54 | } |
Kees Cook | d7c8167 | 2016-08-17 14:42:08 -0700 | [diff] [blame] | 55 | #endif |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Insert a new entry between two known consecutive entries. |
| 59 | * |
| 60 | * This is only for internal list manipulation where we know |
| 61 | * the prev/next entries already! |
| 62 | */ |
| 63 | static inline void __list_add(struct list_head *new, |
| 64 | struct list_head *prev, |
| 65 | struct list_head *next) |
| 66 | { |
Kees Cook | d7c8167 | 2016-08-17 14:42:08 -0700 | [diff] [blame] | 67 | if (!__list_add_valid(new, prev, next)) |
| 68 | return; |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | next->prev = new; |
| 71 | new->next = next; |
| 72 | new->prev = prev; |
Paul E. McKenney | 1c97be6 | 2015-09-20 22:02:17 -0700 | [diff] [blame] | 73 | WRITE_ONCE(prev->next, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * list_add - add a new entry |
| 78 | * @new: new entry to be added |
| 79 | * @head: list head to add it after |
| 80 | * |
| 81 | * Insert a new entry after the specified head. |
| 82 | * This is good for implementing stacks. |
| 83 | */ |
| 84 | static inline void list_add(struct list_head *new, struct list_head *head) |
| 85 | { |
| 86 | __list_add(new, head, head->next); |
| 87 | } |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * list_add_tail - add a new entry |
| 92 | * @new: new entry to be added |
| 93 | * @head: list head to add it before |
| 94 | * |
| 95 | * Insert a new entry before the specified head. |
| 96 | * This is useful for implementing queues. |
| 97 | */ |
| 98 | static inline void list_add_tail(struct list_head *new, struct list_head *head) |
| 99 | { |
| 100 | __list_add(new, head->prev, head); |
| 101 | } |
| 102 | |
| 103 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * Delete a list entry by making the prev/next entries |
| 105 | * point to each other. |
| 106 | * |
| 107 | * This is only for internal list manipulation where we know |
| 108 | * the prev/next entries already! |
| 109 | */ |
| 110 | static inline void __list_del(struct list_head * prev, struct list_head * next) |
| 111 | { |
| 112 | next->prev = prev; |
Paul E. McKenney | 7f5f873 | 2015-09-18 08:45:22 -0700 | [diff] [blame] | 113 | WRITE_ONCE(prev->next, next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Toke Høiland-Jørgensen | c8af5cd | 2019-06-28 11:12:34 +0200 | [diff] [blame] | 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(). |
| 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 | |
Linus Torvalds | 3c18d4d | 2011-02-18 11:32:28 -0800 | [diff] [blame] | 130 | static inline void __list_del_entry(struct list_head *entry) |
| 131 | { |
Kees Cook | 0cd340d | 2016-08-17 14:42:10 -0700 | [diff] [blame] | 132 | if (!__list_del_entry_valid(entry)) |
| 133 | return; |
| 134 | |
Linus Torvalds | 3c18d4d | 2011-02-18 11:32:28 -0800 | [diff] [blame] | 135 | __list_del(entry->prev, entry->next); |
| 136 | } |
| 137 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | static inline void list_del(struct list_head *entry) |
| 145 | { |
Kees Cook | 0cd340d | 2016-08-17 14:42:10 -0700 | [diff] [blame] | 146 | __list_del_entry(entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | entry->next = LIST_POISON1; |
| 148 | entry->prev = LIST_POISON2; |
| 149 | } |
| 150 | |
| 151 | /** |
Oleg Nesterov | 54e7377 | 2006-06-23 02:05:54 -0700 | [diff] [blame] | 152 | * list_replace - replace old entry by new one |
| 153 | * @old : the element to be replaced |
| 154 | * @new : the new element to insert |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 155 | * |
| 156 | * If @old was empty, it will be overwritten. |
Oleg Nesterov | 54e7377 | 2006-06-23 02:05:54 -0700 | [diff] [blame] | 157 | */ |
| 158 | static inline void list_replace(struct list_head *old, |
| 159 | struct list_head *new) |
| 160 | { |
| 161 | new->next = old->next; |
| 162 | new->next->prev = new; |
| 163 | new->prev = old->prev; |
| 164 | new->prev->next = new; |
| 165 | } |
| 166 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 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 | */ |
Oleg Nesterov | 54e7377 | 2006-06-23 02:05:54 -0700 | [diff] [blame] | 174 | static inline void list_replace_init(struct list_head *old, |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 175 | struct list_head *new) |
Oleg Nesterov | 54e7377 | 2006-06-23 02:05:54 -0700 | [diff] [blame] | 176 | { |
| 177 | list_replace(old, new); |
| 178 | INIT_LIST_HEAD(old); |
| 179 | } |
| 180 | |
Robert P. J. Day | 45f8bde | 2007-01-26 00:57:09 -0800 | [diff] [blame] | 181 | /** |
Dan Williams | e900a91 | 2019-05-14 15:41:28 -0700 | [diff] [blame] | 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); |
| 196 | } |
| 197 | |
| 198 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | * list_del_init - deletes entry from list and reinitialize it. |
| 200 | * @entry: the element to delete from the list. |
| 201 | */ |
| 202 | static inline void list_del_init(struct list_head *entry) |
| 203 | { |
Linus Torvalds | 3c18d4d | 2011-02-18 11:32:28 -0800 | [diff] [blame] | 204 | __list_del_entry(entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | INIT_LIST_HEAD(entry); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * list_move - delete from one list and add as another's head |
| 210 | * @list: the entry to move |
| 211 | * @head: the head that will precede our entry |
| 212 | */ |
| 213 | static inline void list_move(struct list_head *list, struct list_head *head) |
| 214 | { |
Linus Torvalds | 3c18d4d | 2011-02-18 11:32:28 -0800 | [diff] [blame] | 215 | __list_del_entry(list); |
Daniel Walker | 78db2ad | 2007-05-12 16:28:35 -0700 | [diff] [blame] | 216 | list_add(list, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * list_move_tail - delete from one list and add as another's tail |
| 221 | * @list: the entry to move |
| 222 | * @head: the head that will follow our entry |
| 223 | */ |
| 224 | static inline void list_move_tail(struct list_head *list, |
| 225 | struct list_head *head) |
| 226 | { |
Linus Torvalds | 3c18d4d | 2011-02-18 11:32:28 -0800 | [diff] [blame] | 227 | __list_del_entry(list); |
Daniel Walker | 78db2ad | 2007-05-12 16:28:35 -0700 | [diff] [blame] | 228 | list_add_tail(list, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
Christian König | df2fc43 | 2018-09-13 11:17:23 +0200 | [diff] [blame] | 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 | /** |
Randy Dunlap | b736523 | 2019-03-28 20:44:05 -0700 | [diff] [blame] | 255 | * list_is_first -- tests whether @list is the first entry in list @head |
Mel Gorman | 70b4459 | 2019-03-05 15:44:54 -0800 | [diff] [blame] | 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 | /** |
Shailabh Nagar | e8f4d97 | 2006-07-14 00:24:35 -0700 | [diff] [blame] | 266 | * list_is_last - tests whether @list is the last entry in list @head |
| 267 | * @list: the entry to test |
| 268 | * @head: the head of the list |
| 269 | */ |
| 270 | static inline int list_is_last(const struct list_head *list, |
| 271 | const struct list_head *head) |
| 272 | { |
| 273 | return list->next == head; |
| 274 | } |
| 275 | |
| 276 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | * list_empty - tests whether a list is empty |
| 278 | * @head: the list to test. |
| 279 | */ |
| 280 | static inline int list_empty(const struct list_head *head) |
| 281 | { |
Paul E. McKenney | 1658d35 | 2015-09-20 17:03:16 -0700 | [diff] [blame] | 282 | return READ_ONCE(head->next) == head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 286 | * list_empty_careful - tests whether a list is empty and not being modified |
| 287 | * @head: the list to test |
| 288 | * |
| 289 | * Description: |
| 290 | * tests whether a list is empty _and_ checks that no other CPU might be |
| 291 | * in the process of modifying either member (next or prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | * |
| 293 | * NOTE: using list_empty_careful() without synchronization |
| 294 | * can only be safe if the only activity that can happen |
| 295 | * to the list entry is list_del_init(). Eg. it cannot be used |
| 296 | * if another CPU could re-list_add() it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | */ |
| 298 | static inline int list_empty_careful(const struct list_head *head) |
| 299 | { |
| 300 | struct list_head *next = head->next; |
| 301 | return (next == head) && (next == head->prev); |
| 302 | } |
| 303 | |
Masami Hiramatsu | 9960257 | 2008-04-28 02:14:27 -0700 | [diff] [blame] | 304 | /** |
Frederic Weisbecker | 5908cdc | 2010-01-09 20:53:14 +0100 | [diff] [blame] | 305 | * list_rotate_left - rotate the list to the left |
| 306 | * @head: the head of the list |
| 307 | */ |
| 308 | static inline void list_rotate_left(struct list_head *head) |
| 309 | { |
| 310 | struct list_head *first; |
| 311 | |
| 312 | if (!list_empty(head)) { |
| 313 | first = head->next; |
| 314 | list_move_tail(first, head); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
Tobin C. Harding | a16b538 | 2019-05-13 17:15:59 -0700 | [diff] [blame] | 319 | * list_rotate_to_front() - Rotate list to specific item. |
| 320 | * @list: The desired new front of the list. |
| 321 | * @head: The head of the list. |
| 322 | * |
| 323 | * Rotates list so that @list becomes the new front of the list. |
| 324 | */ |
| 325 | static inline void list_rotate_to_front(struct list_head *list, |
| 326 | struct list_head *head) |
| 327 | { |
| 328 | /* |
| 329 | * Deletes the list head from the list denoted by @head and |
| 330 | * places it as the tail of @list, this effectively rotates the |
| 331 | * list so that @list is at the front. |
| 332 | */ |
| 333 | list_move_tail(head, list); |
| 334 | } |
| 335 | |
| 336 | /** |
Masami Hiramatsu | 9960257 | 2008-04-28 02:14:27 -0700 | [diff] [blame] | 337 | * list_is_singular - tests whether a list has just one entry. |
| 338 | * @head: the list to test. |
| 339 | */ |
| 340 | static inline int list_is_singular(const struct list_head *head) |
| 341 | { |
| 342 | return !list_empty(head) && (head->next == head->prev); |
| 343 | } |
| 344 | |
Luis R. Rodriguez | 00e8a4d | 2008-08-06 13:28:54 -0700 | [diff] [blame] | 345 | static inline void __list_cut_position(struct list_head *list, |
| 346 | struct list_head *head, struct list_head *entry) |
| 347 | { |
| 348 | struct list_head *new_first = entry->next; |
| 349 | list->next = head->next; |
| 350 | list->next->prev = list; |
| 351 | list->prev = entry; |
| 352 | entry->next = list; |
| 353 | head->next = new_first; |
| 354 | new_first->prev = head; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * list_cut_position - cut a list into two |
| 359 | * @list: a new list to add all removed entries |
| 360 | * @head: a list with entries |
| 361 | * @entry: an entry within head, could be the head itself |
| 362 | * and if so we won't cut the list |
| 363 | * |
| 364 | * This helper moves the initial part of @head, up to and |
| 365 | * including @entry, from @head to @list. You should |
| 366 | * pass on @entry an element you know is on @head. @list |
| 367 | * should be an empty list or a list you do not care about |
| 368 | * losing its data. |
| 369 | * |
| 370 | */ |
| 371 | static inline void list_cut_position(struct list_head *list, |
| 372 | struct list_head *head, struct list_head *entry) |
| 373 | { |
| 374 | if (list_empty(head)) |
| 375 | return; |
| 376 | if (list_is_singular(head) && |
| 377 | (head->next != entry && head != entry)) |
| 378 | return; |
| 379 | if (entry == head) |
| 380 | INIT_LIST_HEAD(list); |
| 381 | else |
| 382 | __list_cut_position(list, head, entry); |
| 383 | } |
| 384 | |
Edward Cree | 4ce0017 | 2018-07-02 16:13:40 +0100 | [diff] [blame] | 385 | /** |
| 386 | * list_cut_before - cut a list into two, before given entry |
| 387 | * @list: a new list to add all removed entries |
| 388 | * @head: a list with entries |
| 389 | * @entry: an entry within head, could be the head itself |
| 390 | * |
| 391 | * This helper moves the initial part of @head, up to but |
| 392 | * excluding @entry, from @head to @list. You should pass |
| 393 | * in @entry an element you know is on @head. @list should |
| 394 | * be an empty list or a list you do not care about losing |
| 395 | * its data. |
| 396 | * If @entry == @head, all entries on @head are moved to |
| 397 | * @list. |
| 398 | */ |
| 399 | static inline void list_cut_before(struct list_head *list, |
| 400 | struct list_head *head, |
| 401 | struct list_head *entry) |
| 402 | { |
| 403 | if (head->next == entry) { |
| 404 | INIT_LIST_HEAD(list); |
| 405 | return; |
| 406 | } |
| 407 | list->next = head->next; |
| 408 | list->next->prev = list; |
| 409 | list->prev = entry->prev; |
| 410 | list->prev->next = list; |
| 411 | head->next = entry; |
| 412 | entry->prev = head; |
| 413 | } |
| 414 | |
Robert P. J. Day | 95d8c36 | 2008-04-29 00:59:29 -0700 | [diff] [blame] | 415 | static inline void __list_splice(const struct list_head *list, |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 416 | struct list_head *prev, |
| 417 | struct list_head *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { |
| 419 | struct list_head *first = list->next; |
| 420 | struct list_head *last = list->prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 422 | first->prev = prev; |
| 423 | prev->next = first; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 425 | last->next = next; |
| 426 | next->prev = last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /** |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 430 | * list_splice - join two lists, this is designed for stacks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | * @list: the new list to add. |
| 432 | * @head: the place to add it in the first list. |
| 433 | */ |
Robert P. J. Day | 95d8c36 | 2008-04-29 00:59:29 -0700 | [diff] [blame] | 434 | static inline void list_splice(const struct list_head *list, |
| 435 | struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
| 437 | if (!list_empty(list)) |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 438 | __list_splice(list, head, head->next); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * list_splice_tail - join two lists, each list being a queue |
| 443 | * @list: the new list to add. |
| 444 | * @head: the place to add it in the first list. |
| 445 | */ |
| 446 | static inline void list_splice_tail(struct list_head *list, |
| 447 | struct list_head *head) |
| 448 | { |
| 449 | if (!list_empty(list)) |
| 450 | __list_splice(list, head->prev, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /** |
| 454 | * list_splice_init - join two lists and reinitialise the emptied list. |
| 455 | * @list: the new list to add. |
| 456 | * @head: the place to add it in the first list. |
| 457 | * |
| 458 | * The list at @list is reinitialised |
| 459 | */ |
| 460 | static inline void list_splice_init(struct list_head *list, |
| 461 | struct list_head *head) |
| 462 | { |
| 463 | if (!list_empty(list)) { |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 464 | __list_splice(list, head, head->next); |
| 465 | INIT_LIST_HEAD(list); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /** |
Randy Dunlap | 6724cce | 2008-08-08 13:56:20 -0700 | [diff] [blame] | 470 | * list_splice_tail_init - join two lists and reinitialise the emptied list |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 471 | * @list: the new list to add. |
| 472 | * @head: the place to add it in the first list. |
| 473 | * |
Randy Dunlap | 6724cce | 2008-08-08 13:56:20 -0700 | [diff] [blame] | 474 | * Each of the lists is a queue. |
Luis R. Rodriguez | 7d283ae | 2008-08-06 15:21:26 -0700 | [diff] [blame] | 475 | * The list at @list is reinitialised |
| 476 | */ |
| 477 | static inline void list_splice_tail_init(struct list_head *list, |
| 478 | struct list_head *head) |
| 479 | { |
| 480 | if (!list_empty(list)) { |
| 481 | __list_splice(list, head->prev, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | INIT_LIST_HEAD(list); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * list_entry - get the struct for this entry |
| 488 | * @ptr: the &struct list_head pointer. |
| 489 | * @type: the type of the struct this is embedded in. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 490 | * @member: the name of the list_head within the struct. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | */ |
| 492 | #define list_entry(ptr, type, member) \ |
| 493 | container_of(ptr, type, member) |
| 494 | |
| 495 | /** |
Pavel Emelianov | b5e6181 | 2007-05-08 00:30:19 -0700 | [diff] [blame] | 496 | * list_first_entry - get the first element from a list |
| 497 | * @ptr: the list head to take the element from. |
| 498 | * @type: the type of the struct this is embedded in. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 499 | * @member: the name of the list_head within the struct. |
Pavel Emelianov | b5e6181 | 2007-05-08 00:30:19 -0700 | [diff] [blame] | 500 | * |
| 501 | * Note, that list is expected to be not empty. |
| 502 | */ |
| 503 | #define list_first_entry(ptr, type, member) \ |
| 504 | list_entry((ptr)->next, type, member) |
| 505 | |
| 506 | /** |
Oleg Nesterov | 93be3c2 | 2013-11-12 15:10:03 -0800 | [diff] [blame] | 507 | * list_last_entry - get the last element from a list |
| 508 | * @ptr: the list head to take the element from. |
| 509 | * @type: the type of the struct this is embedded in. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 510 | * @member: the name of the list_head within the struct. |
Oleg Nesterov | 93be3c2 | 2013-11-12 15:10:03 -0800 | [diff] [blame] | 511 | * |
| 512 | * Note, that list is expected to be not empty. |
| 513 | */ |
| 514 | #define list_last_entry(ptr, type, member) \ |
| 515 | list_entry((ptr)->prev, type, member) |
| 516 | |
| 517 | /** |
Jiri Pirko | 6d7581e | 2013-05-29 05:02:56 +0000 | [diff] [blame] | 518 | * list_first_entry_or_null - get the first element from a list |
| 519 | * @ptr: the list head to take the element from. |
| 520 | * @type: the type of the struct this is embedded in. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 521 | * @member: the name of the list_head within the struct. |
Jiri Pirko | 6d7581e | 2013-05-29 05:02:56 +0000 | [diff] [blame] | 522 | * |
| 523 | * Note that if the list is empty, it returns NULL. |
| 524 | */ |
Chris Wilson | 12adfd8 | 2016-07-23 19:27:50 +0100 | [diff] [blame] | 525 | #define list_first_entry_or_null(ptr, type, member) ({ \ |
| 526 | struct list_head *head__ = (ptr); \ |
| 527 | struct list_head *pos__ = READ_ONCE(head__->next); \ |
| 528 | pos__ != head__ ? list_entry(pos__, type, member) : NULL; \ |
| 529 | }) |
Jiri Pirko | 6d7581e | 2013-05-29 05:02:56 +0000 | [diff] [blame] | 530 | |
| 531 | /** |
Oleg Nesterov | 008208c | 2013-11-12 15:10:01 -0800 | [diff] [blame] | 532 | * list_next_entry - get the next element in list |
| 533 | * @pos: the type * to cursor |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 534 | * @member: the name of the list_head within the struct. |
Oleg Nesterov | 008208c | 2013-11-12 15:10:01 -0800 | [diff] [blame] | 535 | */ |
| 536 | #define list_next_entry(pos, member) \ |
| 537 | list_entry((pos)->member.next, typeof(*(pos)), member) |
| 538 | |
| 539 | /** |
| 540 | * list_prev_entry - get the prev element in list |
| 541 | * @pos: the type * to cursor |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 542 | * @member: the name of the list_head within the struct. |
Oleg Nesterov | 008208c | 2013-11-12 15:10:01 -0800 | [diff] [blame] | 543 | */ |
| 544 | #define list_prev_entry(pos, member) \ |
| 545 | list_entry((pos)->member.prev, typeof(*(pos)), member) |
| 546 | |
| 547 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | * list_for_each - iterate over a list |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 549 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | * @head: the head for your list. |
| 551 | */ |
| 552 | #define list_for_each(pos, head) \ |
Linus Torvalds | e66eed6 | 2011-05-19 14:15:29 -0700 | [diff] [blame] | 553 | for (pos = (head)->next; pos != (head); pos = pos->next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
| 555 | /** |
Pavel Begunkov | 28ca0d6 | 2019-11-29 00:11:54 +0300 | [diff] [blame] | 556 | * list_for_each_continue - continue iteration over a list |
| 557 | * @pos: the &struct list_head to use as a loop cursor. |
| 558 | * @head: the head for your list. |
| 559 | * |
| 560 | * Continue to iterate over a list, continuing after the current position. |
| 561 | */ |
| 562 | #define list_for_each_continue(pos, head) \ |
| 563 | for (pos = pos->next; pos != (head); pos = pos->next) |
| 564 | |
| 565 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | * list_for_each_prev - iterate over a list backwards |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 567 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | * @head: the head for your list. |
| 569 | */ |
| 570 | #define list_for_each_prev(pos, head) \ |
Linus Torvalds | e66eed6 | 2011-05-19 14:15:29 -0700 | [diff] [blame] | 571 | for (pos = (head)->prev; pos != (head); pos = pos->prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
| 573 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 574 | * list_for_each_safe - iterate over a list safe against removal of list entry |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 575 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | * @n: another &struct list_head to use as temporary storage |
| 577 | * @head: the head for your list. |
| 578 | */ |
| 579 | #define list_for_each_safe(pos, n, head) \ |
| 580 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
| 581 | pos = n, n = pos->next) |
| 582 | |
| 583 | /** |
Randy Dunlap | 8f731f7 | 2007-10-18 23:39:28 -0700 | [diff] [blame] | 584 | * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry |
Denis V. Lunev | 37c4252 | 2007-10-16 23:29:53 -0700 | [diff] [blame] | 585 | * @pos: the &struct list_head to use as a loop cursor. |
| 586 | * @n: another &struct list_head to use as temporary storage |
| 587 | * @head: the head for your list. |
| 588 | */ |
| 589 | #define list_for_each_prev_safe(pos, n, head) \ |
| 590 | for (pos = (head)->prev, n = pos->prev; \ |
Linus Torvalds | e66eed6 | 2011-05-19 14:15:29 -0700 | [diff] [blame] | 591 | pos != (head); \ |
Denis V. Lunev | 37c4252 | 2007-10-16 23:29:53 -0700 | [diff] [blame] | 592 | pos = n, n = pos->prev) |
| 593 | |
| 594 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | * list_for_each_entry - iterate over list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 596 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | * @head: the head for your list. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 598 | * @member: the name of the list_head within the struct. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | */ |
| 600 | #define list_for_each_entry(pos, head, member) \ |
Oleg Nesterov | 93be3c2 | 2013-11-12 15:10:03 -0800 | [diff] [blame] | 601 | for (pos = list_first_entry(head, typeof(*pos), member); \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 602 | &pos->member != (head); \ |
| 603 | pos = list_next_entry(pos, member)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | /** |
| 606 | * list_for_each_entry_reverse - iterate backwards over list of given type. |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 607 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | * @head: the head for your list. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 609 | * @member: the name of the list_head within the struct. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | */ |
| 611 | #define list_for_each_entry_reverse(pos, head, member) \ |
Oleg Nesterov | 93be3c2 | 2013-11-12 15:10:03 -0800 | [diff] [blame] | 612 | for (pos = list_last_entry(head, typeof(*pos), member); \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 613 | &pos->member != (head); \ |
| 614 | pos = list_prev_entry(pos, member)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
| 616 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 617 | * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | * @pos: the type * to use as a start point |
| 619 | * @head: the head of the list |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 620 | * @member: the name of the list_head within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 621 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 622 | * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | */ |
| 624 | #define list_prepare_entry(pos, head, member) \ |
| 625 | ((pos) ? : list_entry(head, typeof(*pos), member)) |
| 626 | |
| 627 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 628 | * list_for_each_entry_continue - continue iteration over list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 629 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | * @head: the head for your list. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 631 | * @member: the name of the list_head within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 632 | * |
| 633 | * Continue to iterate over list of given type, continuing after |
| 634 | * the current position. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | */ |
| 636 | #define list_for_each_entry_continue(pos, head, member) \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 637 | for (pos = list_next_entry(pos, member); \ |
| 638 | &pos->member != (head); \ |
| 639 | pos = list_next_entry(pos, member)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | /** |
Pavel Emelyanov | 768f3591 | 2007-09-18 13:20:41 -0700 | [diff] [blame] | 642 | * list_for_each_entry_continue_reverse - iterate backwards from the given point |
| 643 | * @pos: the type * to use as a loop cursor. |
| 644 | * @head: the head for your list. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 645 | * @member: the name of the list_head within the struct. |
Pavel Emelyanov | 768f3591 | 2007-09-18 13:20:41 -0700 | [diff] [blame] | 646 | * |
| 647 | * Start to iterate over list of given type backwards, continuing after |
| 648 | * the current position. |
| 649 | */ |
| 650 | #define list_for_each_entry_continue_reverse(pos, head, member) \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 651 | for (pos = list_prev_entry(pos, member); \ |
| 652 | &pos->member != (head); \ |
| 653 | pos = list_prev_entry(pos, member)) |
Pavel Emelyanov | 768f3591 | 2007-09-18 13:20:41 -0700 | [diff] [blame] | 654 | |
| 655 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 656 | * list_for_each_entry_from - iterate over list of given type from the current point |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 657 | * @pos: the type * to use as a loop cursor. |
Arnaldo Carvalho de Melo | e229c2f | 2006-03-20 17:19:17 -0800 | [diff] [blame] | 658 | * @head: the head for your list. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 659 | * @member: the name of the list_head within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 660 | * |
| 661 | * Iterate over list of given type, continuing from current position. |
Arnaldo Carvalho de Melo | e229c2f | 2006-03-20 17:19:17 -0800 | [diff] [blame] | 662 | */ |
| 663 | #define list_for_each_entry_from(pos, head, member) \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 664 | for (; &pos->member != (head); \ |
| 665 | pos = list_next_entry(pos, member)) |
Arnaldo Carvalho de Melo | e229c2f | 2006-03-20 17:19:17 -0800 | [diff] [blame] | 666 | |
| 667 | /** |
Jiri Pirko | b862815 | 2017-02-03 10:29:05 +0100 | [diff] [blame] | 668 | * list_for_each_entry_from_reverse - iterate backwards over list of given type |
| 669 | * from the current point |
| 670 | * @pos: the type * to use as a loop cursor. |
| 671 | * @head: the head for your list. |
| 672 | * @member: the name of the list_head within the struct. |
| 673 | * |
| 674 | * Iterate backwards over list of given type, continuing from current position. |
| 675 | */ |
| 676 | #define list_for_each_entry_from_reverse(pos, head, member) \ |
| 677 | for (; &pos->member != (head); \ |
| 678 | pos = list_prev_entry(pos, member)) |
| 679 | |
| 680 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 682 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | * @n: another type * to use as temporary storage |
| 684 | * @head: the head for your list. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 685 | * @member: the name of the list_head within the struct. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | */ |
| 687 | #define list_for_each_entry_safe(pos, n, head, member) \ |
Oleg Nesterov | 93be3c2 | 2013-11-12 15:10:03 -0800 | [diff] [blame] | 688 | for (pos = list_first_entry(head, typeof(*pos), member), \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 689 | n = list_next_entry(pos, member); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | &pos->member != (head); \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 691 | pos = n, n = list_next_entry(n, member)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
| 693 | /** |
Ben Hutchings | 9a86e2b | 2010-03-05 13:43:17 -0800 | [diff] [blame] | 694 | * list_for_each_entry_safe_continue - continue list iteration safe against removal |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 695 | * @pos: the type * to use as a loop cursor. |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 696 | * @n: another type * to use as temporary storage |
| 697 | * @head: the head for your list. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 698 | * @member: the name of the list_head within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 699 | * |
| 700 | * Iterate over list of given type, continuing after current point, |
| 701 | * safe against removal of list entry. |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 702 | */ |
| 703 | #define list_for_each_entry_safe_continue(pos, n, head, member) \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 704 | for (pos = list_next_entry(pos, member), \ |
| 705 | n = list_next_entry(pos, member); \ |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 706 | &pos->member != (head); \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 707 | pos = n, n = list_next_entry(n, member)) |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 708 | |
| 709 | /** |
Ben Hutchings | 9a86e2b | 2010-03-05 13:43:17 -0800 | [diff] [blame] | 710 | * list_for_each_entry_safe_from - iterate over list from current point safe against removal |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 711 | * @pos: the type * to use as a loop cursor. |
Arnaldo Carvalho de Melo | d8dcffe | 2006-03-20 17:18:05 -0800 | [diff] [blame] | 712 | * @n: another type * to use as temporary storage |
| 713 | * @head: the head for your list. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 714 | * @member: the name of the list_head within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 715 | * |
| 716 | * Iterate over list of given type from current point, safe against |
| 717 | * removal of list entry. |
Arnaldo Carvalho de Melo | d8dcffe | 2006-03-20 17:18:05 -0800 | [diff] [blame] | 718 | */ |
| 719 | #define list_for_each_entry_safe_from(pos, n, head, member) \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 720 | for (n = list_next_entry(pos, member); \ |
Arnaldo Carvalho de Melo | d8dcffe | 2006-03-20 17:18:05 -0800 | [diff] [blame] | 721 | &pos->member != (head); \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 722 | pos = n, n = list_next_entry(n, member)) |
Arnaldo Carvalho de Melo | d8dcffe | 2006-03-20 17:18:05 -0800 | [diff] [blame] | 723 | |
| 724 | /** |
Ben Hutchings | 9a86e2b | 2010-03-05 13:43:17 -0800 | [diff] [blame] | 725 | * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 726 | * @pos: the type * to use as a loop cursor. |
David Howells | 0ad4235 | 2006-01-09 20:51:31 -0800 | [diff] [blame] | 727 | * @n: another type * to use as temporary storage |
| 728 | * @head: the head for your list. |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 729 | * @member: the name of the list_head within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 730 | * |
| 731 | * Iterate backwards over list of given type, safe against removal |
| 732 | * of list entry. |
David Howells | 0ad4235 | 2006-01-09 20:51:31 -0800 | [diff] [blame] | 733 | */ |
| 734 | #define list_for_each_entry_safe_reverse(pos, n, head, member) \ |
Oleg Nesterov | 93be3c2 | 2013-11-12 15:10:03 -0800 | [diff] [blame] | 735 | for (pos = list_last_entry(head, typeof(*pos), member), \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 736 | n = list_prev_entry(pos, member); \ |
David Howells | 0ad4235 | 2006-01-09 20:51:31 -0800 | [diff] [blame] | 737 | &pos->member != (head); \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 738 | pos = n, n = list_prev_entry(n, member)) |
David Howells | 0ad4235 | 2006-01-09 20:51:31 -0800 | [diff] [blame] | 739 | |
npiggin@suse.de | 57439f8 | 2010-06-24 13:02:14 +1000 | [diff] [blame] | 740 | /** |
| 741 | * list_safe_reset_next - reset a stale list_for_each_entry_safe loop |
| 742 | * @pos: the loop cursor used in the list_for_each_entry_safe loop |
| 743 | * @n: temporary storage used in list_for_each_entry_safe |
Andrey Utkin | 3943f42 | 2014-11-14 05:09:55 +0400 | [diff] [blame] | 744 | * @member: the name of the list_head within the struct. |
npiggin@suse.de | 57439f8 | 2010-06-24 13:02:14 +1000 | [diff] [blame] | 745 | * |
| 746 | * list_safe_reset_next is not safe to use in general if the list may be |
| 747 | * modified concurrently (eg. the lock is dropped in the loop body). An |
| 748 | * exception to this is if the cursor element (pos) is pinned in the list, |
| 749 | * and list_safe_reset_next is called after re-taking the lock and before |
| 750 | * completing the current iteration of the loop body. |
| 751 | */ |
| 752 | #define list_safe_reset_next(pos, n, member) \ |
Oleg Nesterov | 8120e2e | 2013-11-12 15:10:02 -0800 | [diff] [blame] | 753 | n = list_next_entry(pos, member) |
npiggin@suse.de | 57439f8 | 2010-06-24 13:02:14 +1000 | [diff] [blame] | 754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | /* |
| 756 | * Double linked lists with a single pointer list head. |
| 757 | * Mostly useful for hash tables where the two pointer list head is |
| 758 | * too wasteful. |
| 759 | * You lose the ability to access the tail in O(1). |
| 760 | */ |
| 761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | #define HLIST_HEAD_INIT { .first = NULL } |
| 763 | #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } |
| 764 | #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) |
Zach Brown | 490d6ab | 2006-02-03 03:03:56 -0800 | [diff] [blame] | 765 | static inline void INIT_HLIST_NODE(struct hlist_node *h) |
| 766 | { |
| 767 | h->next = NULL; |
| 768 | h->pprev = NULL; |
| 769 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 771 | /** |
| 772 | * hlist_unhashed - Has node been removed from list and reinitialized? |
| 773 | * @h: Node to be checked |
| 774 | * |
| 775 | * Not that not all removal functions will leave a node in unhashed |
| 776 | * state. For example, hlist_nulls_del_init_rcu() does leave the |
| 777 | * node in unhashed state, but hlist_nulls_del() does not. |
| 778 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | static inline int hlist_unhashed(const struct hlist_node *h) |
| 780 | { |
| 781 | return !h->pprev; |
| 782 | } |
| 783 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 784 | /** |
| 785 | * hlist_unhashed_lockless - Version of hlist_unhashed for lockless use |
| 786 | * @h: Node to be checked |
| 787 | * |
| 788 | * This variant of hlist_unhashed() must be used in lockless contexts |
| 789 | * to avoid potential load-tearing. The READ_ONCE() is paired with the |
| 790 | * various WRITE_ONCE() in hlist helpers that are defined below. |
Eric Dumazet | c54a274 | 2019-11-07 11:37:37 -0800 | [diff] [blame] | 791 | */ |
| 792 | static inline int hlist_unhashed_lockless(const struct hlist_node *h) |
| 793 | { |
| 794 | return !READ_ONCE(h->pprev); |
| 795 | } |
| 796 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 797 | /** |
| 798 | * hlist_empty - Is the specified hlist_head structure an empty hlist? |
| 799 | * @h: Structure to check. |
| 800 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | static inline int hlist_empty(const struct hlist_head *h) |
| 802 | { |
Paul E. McKenney | 1658d35 | 2015-09-20 17:03:16 -0700 | [diff] [blame] | 803 | return !READ_ONCE(h->first); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static inline void __hlist_del(struct hlist_node *n) |
| 807 | { |
| 808 | struct hlist_node *next = n->next; |
| 809 | struct hlist_node **pprev = n->pprev; |
Paul E. McKenney | 7f5f873 | 2015-09-18 08:45:22 -0700 | [diff] [blame] | 810 | |
| 811 | WRITE_ONCE(*pprev, next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | if (next) |
Eric Dumazet | c54a274 | 2019-11-07 11:37:37 -0800 | [diff] [blame] | 813 | WRITE_ONCE(next->pprev, pprev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 816 | /** |
| 817 | * hlist_del - Delete the specified hlist_node from its list |
| 818 | * @n: Node to delete. |
| 819 | * |
| 820 | * Note that this function leaves the node in hashed state. Use |
| 821 | * hlist_del_init() or similar instead to unhash @n. |
| 822 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | static inline void hlist_del(struct hlist_node *n) |
| 824 | { |
| 825 | __hlist_del(n); |
| 826 | n->next = LIST_POISON1; |
| 827 | n->pprev = LIST_POISON2; |
| 828 | } |
| 829 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 830 | /** |
| 831 | * hlist_del_init - Delete the specified hlist_node from its list and initialize |
| 832 | * @n: Node to delete. |
| 833 | * |
| 834 | * Note that this function leaves the node in unhashed state. |
| 835 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | static inline void hlist_del_init(struct hlist_node *n) |
| 837 | { |
Akinobu Mita | da753be | 2006-04-28 15:21:23 -0700 | [diff] [blame] | 838 | if (!hlist_unhashed(n)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | __hlist_del(n); |
| 840 | INIT_HLIST_NODE(n); |
| 841 | } |
| 842 | } |
| 843 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 844 | /** |
| 845 | * hlist_add_head - add a new entry at the beginning of the hlist |
| 846 | * @n: new entry to be added |
| 847 | * @h: hlist head to add it after |
| 848 | * |
| 849 | * Insert a new entry after the specified head. |
| 850 | * This is good for implementing stacks. |
| 851 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
| 853 | { |
| 854 | struct hlist_node *first = h->first; |
Eric Dumazet | c54a274 | 2019-11-07 11:37:37 -0800 | [diff] [blame] | 855 | WRITE_ONCE(n->next, first); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (first) |
Eric Dumazet | c54a274 | 2019-11-07 11:37:37 -0800 | [diff] [blame] | 857 | WRITE_ONCE(first->pprev, &n->next); |
Paul E. McKenney | 1c97be6 | 2015-09-20 22:02:17 -0700 | [diff] [blame] | 858 | WRITE_ONCE(h->first, n); |
Eric Dumazet | c54a274 | 2019-11-07 11:37:37 -0800 | [diff] [blame] | 859 | WRITE_ONCE(n->pprev, &h->first); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 862 | /** |
| 863 | * hlist_add_before - add a new entry before the one specified |
| 864 | * @n: new entry to be added |
| 865 | * @next: hlist node to add it before, which must be non-NULL |
| 866 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | static inline void hlist_add_before(struct hlist_node *n, |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 868 | struct hlist_node *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | { |
Eric Dumazet | c54a274 | 2019-11-07 11:37:37 -0800 | [diff] [blame] | 870 | WRITE_ONCE(n->pprev, next->pprev); |
| 871 | WRITE_ONCE(n->next, next); |
| 872 | WRITE_ONCE(next->pprev, &n->next); |
Paul E. McKenney | 1c97be6 | 2015-09-20 22:02:17 -0700 | [diff] [blame] | 873 | WRITE_ONCE(*(n->pprev), n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 876 | /** |
| 877 | * hlist_add_behing - add a new entry after the one specified |
| 878 | * @n: new entry to be added |
| 879 | * @prev: hlist node to add it after, which must be non-NULL |
| 880 | */ |
Ken Helias | 1d02328 | 2014-08-06 16:09:16 -0700 | [diff] [blame] | 881 | static inline void hlist_add_behind(struct hlist_node *n, |
| 882 | struct hlist_node *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | { |
Eric Dumazet | c54a274 | 2019-11-07 11:37:37 -0800 | [diff] [blame] | 884 | WRITE_ONCE(n->next, prev->next); |
| 885 | WRITE_ONCE(prev->next, n); |
| 886 | WRITE_ONCE(n->pprev, &prev->next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
Ken Helias | bc18dd3 | 2014-08-06 16:09:14 -0700 | [diff] [blame] | 888 | if (n->next) |
Eric Dumazet | c54a274 | 2019-11-07 11:37:37 -0800 | [diff] [blame] | 889 | WRITE_ONCE(n->next->pprev, &n->next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 892 | /** |
| 893 | * hlist_add_fake - create a fake hlist consisting of a single headless node |
| 894 | * @n: Node to make a fake list out of |
| 895 | * |
| 896 | * This makes @n appear to be its own predecessor on a headless hlist. |
| 897 | * The point of this is to allow things like hlist_del() to work correctly |
| 898 | * in cases where there is no list. |
| 899 | */ |
Al Viro | 756acc2 | 2010-10-23 15:23:40 -0400 | [diff] [blame] | 900 | static inline void hlist_add_fake(struct hlist_node *n) |
| 901 | { |
| 902 | n->pprev = &n->next; |
| 903 | } |
| 904 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 905 | /** |
| 906 | * hlist_fake: Is this node a fake hlist? |
| 907 | * @h: Node to check for being a self-referential fake hlist. |
| 908 | */ |
Josef Bacik | cbedaac | 2015-03-12 08:19:11 -0400 | [diff] [blame] | 909 | static inline bool hlist_fake(struct hlist_node *h) |
| 910 | { |
| 911 | return h->pprev == &h->next; |
| 912 | } |
| 913 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 914 | /** |
| 915 | * hlist_is_singular_node - is node the only element of the specified hlist? |
| 916 | * @n: Node to check for singularity. |
| 917 | * @h: Header for potentially singular list. |
| 918 | * |
Thomas Gleixner | 15dba1e | 2016-07-04 09:50:27 +0000 | [diff] [blame] | 919 | * Check whether the node is the only node of the head without |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 920 | * accessing head, thus avoiding unnecessary cache misses. |
Thomas Gleixner | 15dba1e | 2016-07-04 09:50:27 +0000 | [diff] [blame] | 921 | */ |
| 922 | static inline bool |
| 923 | hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h) |
| 924 | { |
| 925 | return !n->next && n->pprev == &h->first; |
| 926 | } |
| 927 | |
Paul E. McKenney | 46deb744 | 2019-11-09 10:35:13 -0800 | [diff] [blame] | 928 | /** |
| 929 | * hlist_move_list - Move an hlist |
| 930 | * @old: hlist_head for old list. |
| 931 | * @new: hlist_head for new list. |
| 932 | * |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 933 | * Move a list from one list head to another. Fixup the pprev |
| 934 | * reference of the first entry if it exists. |
| 935 | */ |
| 936 | static inline void hlist_move_list(struct hlist_head *old, |
| 937 | struct hlist_head *new) |
| 938 | { |
| 939 | new->first = old->first; |
| 940 | if (new->first) |
| 941 | new->first->pprev = &new->first; |
| 942 | old->first = NULL; |
| 943 | } |
| 944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | #define hlist_entry(ptr, type, member) container_of(ptr,type,member) |
| 946 | |
| 947 | #define hlist_for_each(pos, head) \ |
Linus Torvalds | 75d65a4 | 2011-05-19 13:50:07 -0700 | [diff] [blame] | 948 | for (pos = (head)->first; pos ; pos = pos->next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
| 950 | #define hlist_for_each_safe(pos, n, head) \ |
| 951 | for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ |
| 952 | pos = n) |
| 953 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 954 | #define hlist_entry_safe(ptr, type, member) \ |
Paul E. McKenney | f65846a | 2013-03-09 07:38:41 -0800 | [diff] [blame] | 955 | ({ typeof(ptr) ____ptr = (ptr); \ |
| 956 | ____ptr ? hlist_entry(____ptr, type, member) : NULL; \ |
| 957 | }) |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | /** |
| 960 | * hlist_for_each_entry - iterate over list of given type |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 961 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | * @head: the head for your list. |
| 963 | * @member: the name of the hlist_node within the struct. |
| 964 | */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 965 | #define hlist_for_each_entry(pos, head, member) \ |
| 966 | for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ |
| 967 | pos; \ |
| 968 | pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
| 970 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 971 | * hlist_for_each_entry_continue - iterate over a hlist continuing after current point |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 972 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | * @member: the name of the hlist_node within the struct. |
| 974 | */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 975 | #define hlist_for_each_entry_continue(pos, member) \ |
| 976 | for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\ |
| 977 | pos; \ |
| 978 | pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
| 980 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 981 | * hlist_for_each_entry_from - iterate over a hlist continuing from current point |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 982 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | * @member: the name of the hlist_node within the struct. |
| 984 | */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 985 | #define hlist_for_each_entry_from(pos, member) \ |
| 986 | for (; pos; \ |
| 987 | pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
| 989 | /** |
| 990 | * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 991 | * @pos: the type * to use as a loop cursor. |
NeilBrown | c84716c | 2019-03-21 14:54:22 +1100 | [diff] [blame] | 992 | * @n: a &struct hlist_node to use as temporary storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | * @head: the head for your list. |
| 994 | * @member: the name of the hlist_node within the struct. |
| 995 | */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 996 | #define hlist_for_each_entry_safe(pos, n, head, member) \ |
| 997 | for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\ |
| 998 | pos && ({ n = pos->member.next; 1; }); \ |
| 999 | pos = hlist_entry_safe(n, typeof(*pos), member)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | #endif |