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