1#ifndef _LINUX_LIST_H 2#define _LINUX_LIST_H 3 4#ifdef __KERNEL__ 5 6#include <linux/stddef.h> 7#include <linux/prefetch.h> 8#include <asm/system.h> 9 10/* 11 * These are non-NULL pointers that will result in page faults 12 * under normal circumstances, used to verify that nobody uses 13 * non-initialized list entries. 14 */ 15#define LIST_POISON1 ((void *) 0x00100100) 16#define LIST_POISON2 ((void *) 0x00200200) 17 18/* 19 * Simple doubly linked list implementation. 20 * 21 * Some of the internal functions ("__xxx") are useful when 22 * manipulating whole lists rather than single entries, as 23 * sometimes we already know the next/prev entries and we can 24 * generate better code by using them directly rather than 25 * using the generic single-entry routines. 26 */ 27 28struct list_head { 29 struct list_head *next, *prev; 30}; 31 32#define LIST_HEAD_INIT(name) { &(name), &(name) } 33 34#define LIST_HEAD(name) \ 35 struct list_head name = LIST_HEAD_INIT(name) 36 37#define INIT_LIST_HEAD(ptr) do { \ 38 (ptr)->next = (ptr); (ptr)->prev = (ptr); \ 39} while (0) 40 41/* 42 * Insert a new entry between two known consecutive entries. 43 * 44 * This is only for internal list manipulation where we know 45 * the prev/next entries already! 46 */ 47static inline void __list_add(struct list_head *new, 48 struct list_head *prev, 49 struct list_head *next) 50{ 51 next->prev = new; 52 new->next = next; 53 new->prev = prev; 54 prev->next = new; 55} 56 57/** 58 * list_add - add a new entry 59 * @new: new entry to be added 60 * @head: list head to add it after 61 * 62 * Insert a new entry after the specified head. 63 * This is good for implementing stacks. 64 */ 65static inline void list_add(struct list_head *new, struct list_head *head) 66{ 67 __list_add(new, head, head->next); 68} 69 70/** 71 * list_add_tail - add a new entry 72 * @new: new entry to be added 73 * @head: list head to add it before 74 * 75 * Insert a new entry before the specified head. 76 * This is useful for implementing queues. 77 */ 78static inline void list_add_tail(struct list_head *new, struct list_head *head) 79{ 80 __list_add(new, head->prev, head); 81} 82 83/* 84 * Insert a new entry between two known consecutive entries. 85 * 86 * This is only for internal list manipulation where we know 87 * the prev/next entries already! 88 */ 89static inline void __list_add_rcu(struct list_head * new, 90 struct list_head * prev, struct list_head * next) 91{ 92 new->next = next; 93 new->prev = prev; 94 smp_wmb(); 95 next->prev = new; 96 prev->next = new; 97} 98 99/** 100 * list_add_rcu - add a new entry to rcu-protected list 101 * @new: new entry to be added 102 * @head: list head to add it after 103 * 104 * Insert a new entry after the specified head. 105 * This is good for implementing stacks. 106 * 107 * The caller must take whatever precautions are necessary 108 * (such as holding appropriate locks) to avoid racing 109 * with another list-mutation primitive, such as list_add_rcu() 110 * or list_del_rcu(), running on this same list. 111 * However, it is perfectly legal to run concurrently with 112 * the _rcu list-traversal primitives, such as 113 * list_for_each_entry_rcu(). 114 */ 115static inline void list_add_rcu(struct list_head *new, struct list_head *head) 116{ 117 __list_add_rcu(new, head, head->next); 118} 119 120/** 121 * list_add_tail_rcu - add a new entry to rcu-protected list 122 * @new: new entry to be added 123 * @head: list head to add it before 124 * 125 * Insert a new entry before the specified head. 126 * This is useful for implementing queues. 127 * 128 * The caller must take whatever precautions are necessary 129 * (such as holding appropriate locks) to avoid racing 130 * with another list-mutation primitive, such as list_add_tail_rcu() 131 * or list_del_rcu(), running on this same list. 132 * However, it is perfectly legal to run concurrently with 133 * the _rcu list-traversal primitives, such as 134 * list_for_each_entry_rcu(). 135 */ 136static inline void list_add_tail_rcu(struct list_head *new, 137 struct list_head *head) 138{ 139 __list_add_rcu(new, head->prev, head); 140} 141 142/* 143 * Delete a list entry by making the prev/next entries 144 * point to each other. 145 * 146 * This is only for internal list manipulation where we know 147 * the prev/next entries already! 148 */ 149static inline void __list_del(struct list_head * prev, struct list_head * next) 150{ 151 next->prev = prev; 152 prev->next = next; 153} 154 155/** 156 * list_del - deletes entry from list. 157 * @entry: the element to delete from the list. 158 * Note: list_empty on entry does not return true after this, the entry is 159 * in an undefined state. 160 */ 161static inline void list_del(struct list_head *entry) 162{ 163 __list_del(entry->prev, entry->next); 164 entry->next = LIST_POISON1; 165 entry->prev = LIST_POISON2; 166} 167 168/** 169 * list_del_rcu - deletes entry from list without re-initialization 170 * @entry: the element to delete from the list. 171 * 172 * Note: list_empty on entry does not return true after this, 173 * the entry is in an undefined state. It is useful for RCU based 174 * lockfree traversal. 175 * 176 * In particular, it means that we can not poison the forward 177 * pointers that may still be used for walking the list. 178 * 179 * The caller must take whatever precautions are necessary 180 * (such as holding appropriate locks) to avoid racing 181 * with another list-mutation primitive, such as list_del_rcu() 182 * or list_add_rcu(), running on this same list. 183 * However, it is perfectly legal to run concurrently with 184 * the _rcu list-traversal primitives, such as 185 * list_for_each_entry_rcu(). 186 * 187 * Note that the caller is not permitted to immediately free 188 * the newly deleted entry. Instead, either synchronize_kernel() 189 * or call_rcu() must be used to defer freeing until an RCU 190 * grace period has elapsed. 191 */ 192static inline void list_del_rcu(struct list_head *entry) 193{ 194 __list_del(entry->prev, entry->next); 195 entry->prev = LIST_POISON2; 196} 197 198/** 199 * list_del_init - deletes entry from list and reinitialize it. 200 * @entry: the element to delete from the list. 201 */ 202static inline void list_del_init(struct list_head *entry) 203{ 204 __list_del(entry->prev, entry->next); 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 */ 213static inline void list_move(struct list_head *list, struct list_head *head) 214{ 215 __list_del(list->prev, list->next); 216 list_add(list, head); 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 */ 224static inline void list_move_tail(struct list_head *list, 225 struct list_head *head) 226{ 227 __list_del(list->prev, list->next); 228 list_add_tail(list, head); 229} 230 231/** 232 * list_empty - tests whether a list is empty 233 * @head: the list to test. 234 */ 235static inline int list_empty(const struct list_head *head) 236{ 237 return head->next == head; 238} 239 240/** 241 * list_empty_careful - tests whether a list is 242 * empty _and_ checks that no other CPU might be 243 * in the process of still modifying either member 244 * 245 * NOTE: using list_empty_careful() without synchronization 246 * can only be safe if the only activity that can happen 247 * to the list entry is list_del_init(). Eg. it cannot be used 248 * if another CPU could re-list_add() it. 249 * 250 * @head: the list to test. 251 */ 252static inline int list_empty_careful(const struct list_head *head) 253{ 254 struct list_head *next = head->next; 255 return (next == head) && (next == head->prev); 256} 257 258static inline void __list_splice(struct list_head *list, 259 struct list_head *head) 260{ 261 struct list_head *first = list->next; 262 struct list_head *last = list->prev; 263 struct list_head *at = head->next; 264 265 first->prev = head; 266 head->next = first; 267 268 last->next = at; 269 at->prev = last; 270} 271 272/** 273 * list_splice - join two lists 274 * @list: the new list to add. 275 * @head: the place to add it in the first list. 276 */ 277static inline void list_splice(struct list_head *list, struct list_head *head) 278{ 279 if (!list_empty(list)) 280 __list_splice(list, head); 281} 282 283/** 284 * list_splice_init - join two lists and reinitialise the emptied list. 285 * @list: the new list to add. 286 * @head: the place to add it in the first list. 287 * 288 * The list at @list is reinitialised 289 */ 290static inline void list_splice_init(struct list_head *list, 291 struct list_head *head) 292{ 293 if (!list_empty(list)) { 294 __list_splice(list, head); 295 INIT_LIST_HEAD(list); 296 } 297} 298 299/** 300 * list_entry - get the struct for this entry 301 * @ptr: the &struct list_head pointer. 302 * @type: the type of the struct this is embedded in. 303 * @member: the name of the list_struct within the struct. 304 */ 305#define list_entry(ptr, type, member) \ 306 container_of(ptr, type, member) 307 308/** 309 * list_for_each - iterate over a list 310 * @pos: the &struct list_head to use as a loop counter. 311 * @head: the head for your list. 312 */ 313#define list_for_each(pos, head) \ 314 for (pos = (head)->next, prefetch(pos->next); pos != (head); \ 315 pos = pos->next, prefetch(pos->next)) 316 317/** 318 * __list_for_each - iterate over a list 319 * @pos: the &struct list_head to use as a loop counter. 320 * @head: the head for your list. 321 * 322 * This variant differs from list_for_each() in that it's the 323 * simplest possible list iteration code, no prefetching is done. 324 * Use this for code that knows the list to be very short (empty 325 * or 1 entry) most of the time. 326 */ 327#define __list_for_each(pos, head) \ 328 for (pos = (head)->next; pos != (head); pos = pos->next) 329 330/** 331 * list_for_each_prev - iterate over a list backwards 332 * @pos: the &struct list_head to use as a loop counter. 333 * @head: the head for your list. 334 */ 335#define list_for_each_prev(pos, head) \ 336 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \ 337 pos = pos->prev, prefetch(pos->prev)) 338 339/** 340 * list_for_each_safe - iterate over a list safe against removal of list entry 341 * @pos: the &struct list_head to use as a loop counter. 342 * @n: another &struct list_head to use as temporary storage 343 * @head: the head for your list. 344 */ 345#define list_for_each_safe(pos, n, head) \ 346 for (pos = (head)->next, n = pos->next; pos != (head); \ 347 pos = n, n = pos->next) 348 349/** 350 * list_for_each_entry - iterate over list of given type 351 * @pos: the type * to use as a loop counter. 352 * @head: the head for your list. 353 * @member: the name of the list_struct within the struct. 354 */ 355#define list_for_each_entry(pos, head, member) \ 356 for (pos = list_entry((head)->next, typeof(*pos), member), \ 357 prefetch(pos->member.next); \ 358 &pos->member != (head); \ 359 pos = list_entry(pos->member.next, typeof(*pos), member), \ 360 prefetch(pos->member.next)) 361 362/** 363 * list_for_each_entry_reverse - iterate backwards over list of given type. 364 * @pos: the type * to use as a loop counter. 365 * @head: the head for your list. 366 * @member: the name of the list_struct within the struct. 367 */ 368#define list_for_each_entry_reverse(pos, head, member) \ 369 for (pos = list_entry((head)->prev, typeof(*pos), member), \ 370 prefetch(pos->member.prev); \ 371 &pos->member != (head); \ 372 pos = list_entry(pos->member.prev, typeof(*pos), member), \ 373 prefetch(pos->member.prev)) 374 375/** 376 * list_prepare_entry - prepare a pos entry for use as a start point in 377 * list_for_each_entry_continue 378 * @pos: the type * to use as a start point 379 * @head: the head of the list 380 * @member: the name of the list_struct within the struct. 381 */ 382#define list_prepare_entry(pos, head, member) \ 383 ((pos) ? : list_entry(head, typeof(*pos), member)) 384 385/** 386 * list_for_each_entry_continue - iterate over list of given type 387 * continuing after existing point 388 * @pos: the type * to use as a loop counter. 389 * @head: the head for your list. 390 * @member: the name of the list_struct within the struct. 391 */ 392#define list_for_each_entry_continue(pos, head, member) \ 393 for (pos = list_entry(pos->member.next, typeof(*pos), member), \ 394 prefetch(pos->member.next); \ 395 &pos->member != (head); \ 396 pos = list_entry(pos->member.next, typeof(*pos), member), \ 397 prefetch(pos->member.next)) 398 399/** 400 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 401 * @pos: the type * to use as a loop counter. 402 * @n: another type * to use as temporary storage 403 * @head: the head for your list. 404 * @member: the name of the list_struct within the struct. 405 */ 406#define list_for_each_entry_safe(pos, n, head, member) \ 407 for (pos = list_entry((head)->next, typeof(*pos), member), \ 408 n = list_entry(pos->member.next, typeof(*pos), member); \ 409 &pos->member != (head); \ 410 pos = n, n = list_entry(n->member.next, typeof(*n), member)) 411 412/** 413 * list_for_each_rcu - iterate over an rcu-protected list 414 * @pos: the &struct list_head to use as a loop counter. 415 * @head: the head for your list. 416 * 417 * This list-traversal primitive may safely run concurrently with 418 * the _rcu list-mutation primitives such as list_add_rcu() 419 * as long as the traversal is guarded by rcu_read_lock(). 420 */ 421#define list_for_each_rcu(pos, head) \ 422 for (pos = (head)->next, prefetch(pos->next); pos != (head); \ 423 pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next)) 424 425#define __list_for_each_rcu(pos, head) \ 426 for (pos = (head)->next; pos != (head); \ 427 pos = pos->next, ({ smp_read_barrier_depends(); 0;})) 428 429/** 430 * list_for_each_safe_rcu - iterate over an rcu-protected list safe 431 * against removal of list entry 432 * @pos: the &struct list_head to use as a loop counter. 433 * @n: another &struct list_head to use as temporary storage 434 * @head: the head for your list. 435 * 436 * This list-traversal primitive may safely run concurrently with 437 * the _rcu list-mutation primitives such as list_add_rcu() 438 * as long as the traversal is guarded by rcu_read_lock(). 439 */ 440#define list_for_each_safe_rcu(pos, n, head) \ 441 for (pos = (head)->next, n = pos->next; pos != (head); \ 442 pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next) 443 444/** 445 * list_for_each_entry_rcu - iterate over rcu list of given type 446 * @pos: the type * to use as a loop counter. 447 * @head: the head for your list. 448 * @member: the name of the list_struct within the struct. 449 * 450 * This list-traversal primitive may safely run concurrently with 451 * the _rcu list-mutation primitives such as list_add_rcu() 452 * as long as the traversal is guarded by rcu_read_lock(). 453 */ 454#define list_for_each_entry_rcu(pos, head, member) \ 455 for (pos = list_entry((head)->next, typeof(*pos), member), \ 456 prefetch(pos->member.next); \ 457 &pos->member != (head); \ 458 pos = list_entry(pos->member.next, typeof(*pos), member), \ 459 ({ smp_read_barrier_depends(); 0;}), \ 460 prefetch(pos->member.next)) 461 462 463/** 464 * list_for_each_continue_rcu - iterate over an rcu-protected list 465 * continuing after existing point. 466 * @pos: the &struct list_head to use as a loop counter. 467 * @head: the head for your list. 468 * 469 * This list-traversal primitive may safely run concurrently with 470 * the _rcu list-mutation primitives such as list_add_rcu() 471 * as long as the traversal is guarded by rcu_read_lock(). 472 */ 473#define list_for_each_continue_rcu(pos, head) \ 474 for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \ 475 (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next)) 476 477/* 478 * Double linked lists with a single pointer list head. 479 * Mostly useful for hash tables where the two pointer list head is 480 * too wasteful. 481 * You lose the ability to access the tail in O(1). 482 */ 483 484struct hlist_head { 485 struct hlist_node *first; 486}; 487 488struct hlist_node { 489 struct hlist_node *next, **pprev; 490}; 491 492#define HLIST_HEAD_INIT { .first = NULL } 493#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } 494#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) 495#define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL) 496 497static inline int hlist_unhashed(const struct hlist_node *h) 498{ 499 return !h->pprev; 500} 501 502static inline int hlist_empty(const struct hlist_head *h) 503{ 504 return !h->first; 505} 506 507static inline void __hlist_del(struct hlist_node *n) 508{ 509 struct hlist_node *next = n->next; 510 struct hlist_node **pprev = n->pprev; 511 *pprev = next; 512 if (next) 513 next->pprev = pprev; 514} 515 516static inline void hlist_del(struct hlist_node *n) 517{ 518 __hlist_del(n); 519 n->next = LIST_POISON1; 520 n->pprev = LIST_POISON2; 521} 522 523/** 524 * hlist_del_rcu - deletes entry from hash list without re-initialization 525 * @n: the element to delete from the hash list. 526 * 527 * Note: list_unhashed() on entry does not return true after this, 528 * the entry is in an undefined state. It is useful for RCU based 529 * lockfree traversal. 530 * 531 * In particular, it means that we can not poison the forward 532 * pointers that may still be used for walking the hash list. 533 * 534 * The caller must take whatever precautions are necessary 535 * (such as holding appropriate locks) to avoid racing 536 * with another list-mutation primitive, such as hlist_add_head_rcu() 537 * or hlist_del_rcu(), running on this same list. 538 * However, it is perfectly legal to run concurrently with 539 * the _rcu list-traversal primitives, such as 540 * hlist_for_each_entry(). 541 */ 542static inline void hlist_del_rcu(struct hlist_node *n) 543{ 544 __hlist_del(n); 545 n->pprev = LIST_POISON2; 546} 547 548static inline void hlist_del_init(struct hlist_node *n) 549{ 550 if (n->pprev) { 551 __hlist_del(n); 552 INIT_HLIST_NODE(n); 553 } 554} 555 556#define hlist_del_rcu_init hlist_del_init 557 558static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) 559{ 560 struct hlist_node *first = h->first; 561 n->next = first; 562 if (first) 563 first->pprev = &n->next; 564 h->first = n; 565 n->pprev = &h->first; 566} 567 568 569/** 570 * hlist_add_head_rcu - adds the specified element to the specified hlist, 571 * while permitting racing traversals. 572 * @n: the element to add to the hash list. 573 * @h: the list to add to. 574 * 575 * The caller must take whatever precautions are necessary 576 * (such as holding appropriate locks) to avoid racing 577 * with another list-mutation primitive, such as hlist_add_head_rcu() 578 * or hlist_del_rcu(), running on this same list. 579 * However, it is perfectly legal to run concurrently with 580 * the _rcu list-traversal primitives, such as 581 * hlist_for_each_entry(), but only if smp_read_barrier_depends() 582 * is used to prevent memory-consistency problems on Alpha CPUs. 583 * Regardless of the type of CPU, the list-traversal primitive 584 * must be guarded by rcu_read_lock(). 585 * 586 * OK, so why don't we have an hlist_for_each_entry_rcu()??? 587 */ 588static inline void hlist_add_head_rcu(struct hlist_node *n, 589 struct hlist_head *h) 590{ 591 struct hlist_node *first = h->first; 592 n->next = first; 593 n->pprev = &h->first; 594 smp_wmb(); 595 if (first) 596 first->pprev = &n->next; 597 h->first = n; 598} 599 600/* next must be != NULL */ 601static inline void hlist_add_before(struct hlist_node *n, 602 struct hlist_node *next) 603{ 604 n->pprev = next->pprev; 605 n->next = next; 606 next->pprev = &n->next; 607 *(n->pprev) = n; 608} 609 610static inline void hlist_add_after(struct hlist_node *n, 611 struct hlist_node *next) 612{ 613 next->next = n->next; 614 n->next = next; 615 next->pprev = &n->next; 616 617 if(next->next) 618 next->next->pprev = &next->next; 619} 620 621#define hlist_entry(ptr, type, member) container_of(ptr,type,member) 622 623#define hlist_for_each(pos, head) \ 624 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ 625 pos = pos->next) 626 627#define hlist_for_each_safe(pos, n, head) \ 628 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ 629 pos = n) 630 631/** 632 * hlist_for_each_entry - iterate over list of given type 633 * @tpos: the type * to use as a loop counter. 634 * @pos: the &struct hlist_node to use as a loop counter. 635 * @head: the head for your list. 636 * @member: the name of the hlist_node within the struct. 637 */ 638#define hlist_for_each_entry(tpos, pos, head, member) \ 639 for (pos = (head)->first; \ 640 pos && ({ prefetch(pos->next); 1;}) && \ 641 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 642 pos = pos->next) 643 644/** 645 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point 646 * @tpos: the type * to use as a loop counter. 647 * @pos: the &struct hlist_node to use as a loop counter. 648 * @member: the name of the hlist_node within the struct. 649 */ 650#define hlist_for_each_entry_continue(tpos, pos, member) \ 651 for (pos = (pos)->next; \ 652 pos && ({ prefetch(pos->next); 1;}) && \ 653 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 654 pos = pos->next) 655 656/** 657 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point 658 * @tpos: the type * to use as a loop counter. 659 * @pos: the &struct hlist_node to use as a loop counter. 660 * @member: the name of the hlist_node within the struct. 661 */ 662#define hlist_for_each_entry_from(tpos, pos, member) \ 663 for (; pos && ({ prefetch(pos->next); 1;}) && \ 664 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 665 pos = pos->next) 666 667/** 668 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry 669 * @tpos: the type * to use as a loop counter. 670 * @pos: the &struct hlist_node to use as a loop counter. 671 * @n: another &struct hlist_node to use as temporary storage 672 * @head: the head for your list. 673 * @member: the name of the hlist_node within the struct. 674 */ 675#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ 676 for (pos = (head)->first; \ 677 pos && ({ n = pos->next; 1; }) && \ 678 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 679 pos = n) 680 681/** 682 * hlist_for_each_entry_rcu - iterate over rcu list of given type 683 * @pos: the type * to use as a loop counter. 684 * @pos: the &struct hlist_node to use as a loop counter. 685 * @head: the head for your list. 686 * @member: the name of the hlist_node within the struct. 687 * 688 * This list-traversal primitive may safely run concurrently with 689 * the _rcu list-mutation primitives such as hlist_add_rcu() 690 * as long as the traversal is guarded by rcu_read_lock(). 691 */ 692#define hlist_for_each_entry_rcu(tpos, pos, head, member) \ 693 for (pos = (head)->first; \ 694 pos && ({ prefetch(pos->next); 1;}) && \ 695 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 696 pos = pos->next, ({ smp_read_barrier_depends(); 0; }) ) 697 698#else 699#warning "don't include kernel headers in userspace" 700#endif /* __KERNEL__ */ 701#endif 702

