1.. SPDX-License-Identifier: GPL-2.0+ 2 3===================== 4Linked Lists in Linux 5===================== 6 7:Author: Nicolas Frattaroli <nicolas.frattaroli@collabora.com> 8 9.. contents:: 10 11Introduction 12============ 13 14Linked lists are one of the most basic data structures used in many programs. 15The Linux kernel implements several different flavours of linked lists. The 16purpose of this document is not to explain linked lists in general, but to show 17new kernel developers how to use the Linux kernel implementations of linked 18lists. 19 20Please note that while linked lists certainly are ubiquitous, they are rarely 21the best data structure to use in cases where a simple array doesn't already 22suffice. In particular, due to their poor data locality, linked lists are a bad 23choice in situations where performance may be of consideration. Familiarizing 24oneself with other in-kernel generic data structures, especially for concurrent 25accesses, is highly encouraged. 26 27Linux implementation of doubly linked lists 28=========================================== 29 30Linux's linked list implementations can be used by including the header file 31``<linux/list.h>``. 32 33The doubly-linked list will likely be the most familiar to many readers. It's a 34list that can efficiently be traversed forwards and backwards. 35 36The Linux kernel's doubly-linked list is circular in nature. This means that to 37get from the head node to the tail, we can just travel one edge backwards. 38Similarly, to get from the tail node to the head, we can simply travel forwards 39"beyond" the tail and arrive back at the head. 40 41Declaring a node 42---------------- 43 44A node in a doubly-linked list is declared by adding a struct list_head 45member to the data structure you wish to be contained in the list: 46 47.. code-block:: c 48 49 struct clown { 50 unsigned long long shoe_size; 51 const char *name; 52 struct list_head node; /* the aforementioned member */ 53 }; 54 55This may be an unfamiliar approach to some, as the classical explanation of a 56linked list is a list node data structure with pointers to the previous and next 57list node, as well the payload data. Linux chooses this approach because it 58allows for generic list modification code regardless of what data structure is 59contained within the list. Since the struct list_head member is not a pointer 60but part of the data structure proper, the container_of() pattern can be used by 61the list implementation to access the payload data regardless of its type, while 62staying oblivious to what said type actually is. 63 64Declaring and initializing a list 65--------------------------------- 66 67A doubly-linked list can then be declared as just another struct list_head, 68and initialized with the LIST_HEAD_INIT() macro during initial assignment, or 69with the INIT_LIST_HEAD() function later: 70 71.. code-block:: c 72 73 struct clown_car { 74 int tyre_pressure[4]; 75 struct list_head clowns; /* Looks like a node! */ 76 }; 77 78 /* ... Somewhere later in our driver ... */ 79 80 static int circus_init(struct circus_priv *circus) 81 { 82 struct clown_car other_car = { 83 .tyre_pressure = {10, 12, 11, 9}, 84 .clowns = LIST_HEAD_INIT(other_car.clowns) 85 }; 86 87 INIT_LIST_HEAD(&circus->car.clowns); 88 89 return 0; 90 } 91 92A further point of confusion to some may be that the list itself doesn't really 93have its own type. The concept of the entire linked list and a 94struct list_head member that points to other entries in the list are one and 95the same. 96 97Adding nodes to the list 98------------------------ 99 100Adding a node to the linked list is done through the list_add() macro. 101 102We'll return to our clown car example to illustrate how nodes get added to the 103list: 104 105.. code-block:: c 106 107 static int circus_fill_car(struct circus_priv *circus) 108 { 109 struct clown_car *car = &circus->car; 110 struct clown *grock; 111 struct clown *dimitri; 112 113 /* State 1 */ 114 115 grock = kzalloc(sizeof(*grock), GFP_KERNEL); 116 if (!grock) 117 return -ENOMEM; 118 grock->name = "Grock"; 119 grock->shoe_size = 1000; 120 121 /* Note that we're adding the "node" member */ 122 list_add(&grock->node, &car->clowns); 123 124 /* State 2 */ 125 126 dimitri = kzalloc(sizeof(*dimitri), GFP_KERNEL); 127 if (!dimitri) 128 return -ENOMEM; 129 dimitri->name = "Dimitri"; 130 dimitri->shoe_size = 50; 131 132 list_add(&dimitri->node, &car->clowns); 133 134 /* State 3 */ 135 136 return 0; 137 } 138 139In State 1, our list of clowns is still empty:: 140 141 .------. 142 v | 143 .--------. | 144 | clowns |--' 145 '--------' 146 147This diagram shows the singular "clowns" node pointing at itself. In this 148diagram, and all following diagrams, only the forward edges are shown, to aid in 149clarity. 150 151In State 2, we've added Grock after the list head:: 152 153 .--------------------. 154 v | 155 .--------. .-------. | 156 | clowns |---->| Grock |--' 157 '--------' '-------' 158 159This diagram shows the "clowns" node pointing at a new node labeled "Grock". 160The Grock node is pointing back at the "clowns" node. 161 162In State 3, we've added Dimitri after the list head, resulting in the following:: 163 164 .------------------------------------. 165 v | 166 .--------. .---------. .-------. | 167 | clowns |---->| Dimitri |---->| Grock |--' 168 '--------' '---------' '-------' 169 170This diagram shows the "clowns" node pointing at a new node labeled "Dimitri", 171which then points at the node labeled "Grock". The "Grock" node still points 172back at the "clowns" node. 173 174If we wanted to have Dimitri inserted at the end of the list instead, we'd use 175list_add_tail(). Our code would then look like this: 176 177.. code-block:: c 178 179 static int circus_fill_car(struct circus_priv *circus) 180 { 181 /* ... */ 182 183 list_add_tail(&dimitri->node, &car->clowns); 184 185 /* State 3b */ 186 187 return 0; 188 } 189 190This results in the following list:: 191 192 .------------------------------------. 193 v | 194 .--------. .-------. .---------. | 195 | clowns |---->| Grock |---->| Dimitri |--' 196 '--------' '-------' '---------' 197 198This diagram shows the "clowns" node pointing at the node labeled "Grock", 199which points at the new node labeled "Dimitri". The node labeled "Dimitri" 200points back at the "clowns" node. 201 202Traversing the list 203------------------- 204 205To iterate the list, we can loop through all nodes within the list with 206list_for_each(). 207 208In our clown example, this results in the following somewhat awkward code: 209 210.. code-block:: c 211 212 static unsigned long long circus_get_max_shoe_size(struct circus_priv *circus) 213 { 214 unsigned long long res = 0; 215 struct clown *e; 216 struct list_head *cur; 217 218 list_for_each(cur, &circus->car.clowns) { 219 e = list_entry(cur, struct clown, node); 220 if (e->shoe_size > res) 221 res = e->shoe_size; 222 } 223 224 return res; 225 } 226 227The list_entry() macro internally uses the aforementioned container_of() to 228retrieve the data structure instance that ``node`` is a member of. 229 230Note how the additional list_entry() call is a little awkward here. It's only 231there because we're iterating through the ``node`` members, but we really want 232to iterate through the payload, i.e. the ``struct clown`` that contains each 233node's struct list_head. For this reason, there is a second macro: 234list_for_each_entry() 235 236Using it would change our code to something like this: 237 238.. code-block:: c 239 240 static unsigned long long circus_get_max_shoe_size(struct circus_priv *circus) 241 { 242 unsigned long long res = 0; 243 struct clown *e; 244 245 list_for_each_entry(e, &circus->car.clowns, node) { 246 if (e->shoe_size > res) 247 res = e->shoe_size; 248 } 249 250 return res; 251 } 252 253This eliminates the need for the list_entry() step, and our loop cursor is now 254of the type of our payload. The macro is given the member name that corresponds 255to the list's struct list_head within the clown data structure so that it can 256still walk the list. 257 258Removing nodes from the list 259---------------------------- 260 261The list_del() function can be used to remove entries from the list. It not only 262removes the given entry from the list, but poisons the entry's ``prev`` and 263``next`` pointers, so that unintended use of the entry after removal does not 264go unnoticed. 265 266We can extend our previous example to remove one of the entries: 267 268.. code-block:: c 269 270 static int circus_fill_car(struct circus_priv *circus) 271 { 272 /* ... */ 273 274 list_add(&dimitri->node, &car->clowns); 275 276 /* State 3 */ 277 278 list_del(&dimitri->node); 279 280 /* State 4 */ 281 282 return 0; 283 } 284 285The result of this would be this:: 286 287 .--------------------. 288 v | 289 .--------. .-------. | .---------. 290 | clowns |---->| Grock |--' | Dimitri | 291 '--------' '-------' '---------' 292 293This diagram shows the "clowns" node pointing at the node labeled "Grock", 294which points back at the "clowns" node. Off to the side is a lone node labeled 295"Dimitri", which has no arrows pointing anywhere. 296 297Note how the Dimitri node does not point to itself; its pointers are 298intentionally set to a "poison" value that the list code refuses to traverse. 299 300If we wanted to reinitialize the removed node instead to make it point at itself 301again like an empty list head, we can use list_del_init() instead: 302 303.. code-block:: c 304 305 static int circus_fill_car(struct circus_priv *circus) 306 { 307 /* ... */ 308 309 list_add(&dimitri->node, &car->clowns); 310 311 /* State 3 */ 312 313 list_del_init(&dimitri->node); 314 315 /* State 4b */ 316 317 return 0; 318 } 319 320This results in the deleted node pointing to itself again:: 321 322 .--------------------. .-------. 323 v | v | 324 .--------. .-------. | .---------. | 325 | clowns |---->| Grock |--' | Dimitri |--' 326 '--------' '-------' '---------' 327 328This diagram shows the "clowns" node pointing at the node labeled "Grock", 329which points back at the "clowns" node. Off to the side is a lone node labeled 330"Dimitri", which points to itself. 331 332Traversing whilst removing nodes 333-------------------------------- 334 335Deleting entries while we're traversing the list will cause problems if we use 336list_for_each() and list_for_each_entry(), as deleting the current entry would 337modify the ``next`` pointer of it, which means the traversal can't properly 338advance to the next list entry. 339 340There is a solution to this however: list_for_each_safe() and 341list_for_each_entry_safe(). These take an additional parameter of a pointer to 342a struct list_head to use as temporary storage for the next entry during 343iteration, solving the issue. 344 345An example of how to use it: 346 347.. code-block:: c 348 349 static void circus_eject_insufficient_clowns(struct circus_priv *circus) 350 { 351 struct clown *e; 352 struct clown *n; /* temporary storage for safe iteration */ 353 354 list_for_each_entry_safe(e, n, &circus->car.clowns, node) { 355 if (e->shoe_size < 500) 356 list_del(&e->node); 357 } 358 } 359 360Proper memory management (i.e. freeing the deleted node while making sure 361nothing still references it) in this case is left as an exercise to the reader. 362 363Cutting a list 364-------------- 365 366There are two helper functions to cut lists with. Both take elements from the 367list ``head``, and replace the contents of the list ``list``. 368 369The first such function is list_cut_position(). It removes all list entries from 370``head`` up to and including ``entry``, placing them in ``list`` instead. 371 372In this example, it's assumed we start with the following list:: 373 374 .----------------------------------------------------------------. 375 v | 376 .--------. .-------. .---------. .-----. .---------. | 377 | clowns |---->| Grock |---->| Dimitri |---->| Pic |---->| Alfredo |--' 378 '--------' '-------' '---------' '-----' '---------' 379 380With the following code, every clown up to and including "Pic" is moved from 381the "clowns" list head to a separate struct list_head initialized at local 382stack variable ``retirement``: 383 384.. code-block:: c 385 386 static void circus_retire_clowns(struct circus_priv *circus) 387 { 388 struct list_head retirement = LIST_HEAD_INIT(retirement); 389 struct clown *grock, *dimitri, *pic, *alfredo; 390 struct clown_car *car = &circus->car; 391 392 /* ... clown initialization, list adding ... */ 393 394 list_cut_position(&retirement, &car->clowns, &pic->node); 395 396 /* State 1 */ 397 } 398 399The resulting ``car->clowns`` list would be this:: 400 401 .----------------------. 402 v | 403 .--------. .---------. | 404 | clowns |---->| Alfredo |--' 405 '--------' '---------' 406 407Meanwhile, the ``retirement`` list is transformed to the following:: 408 409 .--------------------------------------------------. 410 v | 411 .------------. .-------. .---------. .-----. | 412 | retirement |---->| Grock |---->| Dimitri |---->| Pic |--' 413 '------------' '-------' '---------' '-----' 414 415The second function, list_cut_before(), is much the same, except it cuts before 416the ``entry`` node, i.e. it removes all list entries from ``head`` up to but 417excluding ``entry``, placing them in ``list`` instead. This example assumes the 418same initial starting list as the previous example: 419 420.. code-block:: c 421 422 static void circus_retire_clowns(struct circus_priv *circus) 423 { 424 struct list_head retirement = LIST_HEAD_INIT(retirement); 425 struct clown *grock, *dimitri, *pic, *alfredo; 426 struct clown_car *car = &circus->car; 427 428 /* ... clown initialization, list adding ... */ 429 430 list_cut_before(&retirement, &car->clowns, &pic->node); 431 432 /* State 1b */ 433 } 434 435The resulting ``car->clowns`` list would be this:: 436 437 .----------------------------------. 438 v | 439 .--------. .-----. .---------. | 440 | clowns |---->| Pic |---->| Alfredo |--' 441 '--------' '-----' '---------' 442 443Meanwhile, the ``retirement`` list is transformed to the following:: 444 445 .--------------------------------------. 446 v | 447 .------------. .-------. .---------. | 448 | retirement |---->| Grock |---->| Dimitri |--' 449 '------------' '-------' '---------' 450 451It should be noted that both functions will destroy links to any existing nodes 452in the destination ``struct list_head *list``. 453 454Moving entries and partial lists 455-------------------------------- 456 457The list_move() and list_move_tail() functions can be used to move an entry 458from one list to another, to either the start or end respectively. 459 460In the following example, we'll assume we start with two lists ("clowns" and 461"sidewalk" in the following initial state "State 0":: 462 463 .----------------------------------------------------------------. 464 v | 465 .--------. .-------. .---------. .-----. .---------. | 466 | clowns |---->| Grock |---->| Dimitri |---->| Pic |---->| Alfredo |--' 467 '--------' '-------' '---------' '-----' '---------' 468 469 .-------------------. 470 v | 471 .----------. .-----. | 472 | sidewalk |---->| Pio |--' 473 '----------' '-----' 474 475We apply the following example code to the two lists: 476 477.. code-block:: c 478 479 static void circus_clowns_exit_car(struct circus_priv *circus) 480 { 481 struct list_head sidewalk = LIST_HEAD_INIT(sidewalk); 482 struct clown *grock, *dimitri, *pic, *alfredo, *pio; 483 struct clown_car *car = &circus->car; 484 485 /* ... clown initialization, list adding ... */ 486 487 /* State 0 */ 488 489 list_move(&pic->node, &sidewalk); 490 491 /* State 1 */ 492 493 list_move_tail(&dimitri->node, &sidewalk); 494 495 /* State 2 */ 496 } 497 498In State 1, we arrive at the following situation:: 499 500 .-----------------------------------------------------. 501 | | 502 v | 503 .--------. .-------. .---------. .---------. | 504 | clowns |---->| Grock |---->| Dimitri |---->| Alfredo |--' 505 '--------' '-------' '---------' '---------' 506 507 .-------------------------------. 508 v | 509 .----------. .-----. .-----. | 510 | sidewalk |---->| Pic |---->| Pio |--' 511 '----------' '-----' '-----' 512 513In State 2, after we've moved Dimitri to the tail of sidewalk, the situation 514changes as follows:: 515 516 .-------------------------------------. 517 | | 518 v | 519 .--------. .-------. .---------. | 520 | clowns |---->| Grock |---->| Alfredo |--' 521 '--------' '-------' '---------' 522 523 .-----------------------------------------------. 524 v | 525 .----------. .-----. .-----. .---------. | 526 | sidewalk |---->| Pic |---->| Pio |---->| Dimitri |--' 527 '----------' '-----' '-----' '---------' 528 529As long as the source and destination list head are part of the same list, we 530can also efficiently bulk move a segment of the list to the tail end of the 531list. We continue the previous example by adding a list_bulk_move_tail() after 532State 2, moving Pic and Pio to the tail end of the sidewalk list. 533 534.. code-block:: c 535 536 static void circus_clowns_exit_car(struct circus_priv *circus) 537 { 538 struct list_head sidewalk = LIST_HEAD_INIT(sidewalk); 539 struct clown *grock, *dimitri, *pic, *alfredo, *pio; 540 struct clown_car *car = &circus->car; 541 542 /* ... clown initialization, list adding ... */ 543 544 /* State 0 */ 545 546 list_move(&pic->node, &sidewalk); 547 548 /* State 1 */ 549 550 list_move_tail(&dimitri->node, &sidewalk); 551 552 /* State 2 */ 553 554 list_bulk_move_tail(&sidewalk, &pic->node, &pio->node); 555 556 /* State 3 */ 557 } 558 559For the sake of brevity, only the altered "sidewalk" list at State 3 is depicted 560in the following diagram:: 561 562 .-----------------------------------------------. 563 v | 564 .----------. .---------. .-----. .-----. | 565 | sidewalk |---->| Dimitri |---->| Pic |---->| Pio |--' 566 '----------' '---------' '-----' '-----' 567 568Do note that list_bulk_move_tail() does not do any checking as to whether all 569three supplied ``struct list_head *`` parameters really do belong to the same 570list. If you use it outside the constraints the documentation gives, then the 571result is a matter between you and the implementation. 572 573Rotating entries 574---------------- 575 576A common write operation on lists, especially when using them as queues, is 577to rotate it. A list rotation means entries at the front are sent to the back. 578 579For rotation, Linux provides us with two functions: list_rotate_left() and 580list_rotate_to_front(). The former can be pictured like a bicycle chain, taking 581the entry after the supplied ``struct list_head *`` and moving it to the tail, 582which in essence means the entire list, due to its circular nature, rotates by 583one position. 584 585The latter, list_rotate_to_front(), takes the same concept one step further: 586instead of advancing the list by one entry, it advances it *until* the specified 587entry is the new front. 588 589In the following example, our starting state, State 0, is the following:: 590 591 .-----------------------------------------------------------------. 592 v | 593 .--------. .-------. .---------. .-----. .---------. .-----. | 594 | clowns |-->| Grock |-->| Dimitri |-->| Pic |-->| Alfredo |-->| Pio |-' 595 '--------' '-------' '---------' '-----' '---------' '-----' 596 597The example code being used to demonstrate list rotations is the following: 598 599.. code-block:: c 600 601 static void circus_clowns_rotate(struct circus_priv *circus) 602 { 603 struct clown *grock, *dimitri, *pic, *alfredo, *pio; 604 struct clown_car *car = &circus->car; 605 606 /* ... clown initialization, list adding ... */ 607 608 /* State 0 */ 609 610 list_rotate_left(&car->clowns); 611 612 /* State 1 */ 613 614 list_rotate_to_front(&alfredo->node, &car->clowns); 615 616 /* State 2 */ 617 618 } 619 620In State 1, we arrive at the following situation:: 621 622 .-----------------------------------------------------------------. 623 v | 624 .--------. .---------. .-----. .---------. .-----. .-------. | 625 | clowns |-->| Dimitri |-->| Pic |-->| Alfredo |-->| Pio |-->| Grock |-' 626 '--------' '---------' '-----' '---------' '-----' '-------' 627 628Next, after the list_rotate_to_front() call, we arrive in the following 629State 2:: 630 631 .-----------------------------------------------------------------. 632 v | 633 .--------. .---------. .-----. .-------. .---------. .-----. | 634 | clowns |-->| Alfredo |-->| Pio |-->| Grock |-->| Dimitri |-->| Pic |-' 635 '--------' '---------' '-----' '-------' '---------' '-----' 636 637As is hopefully evident from the diagrams, the entries in front of "Alfredo" 638were cycled to the tail end of the list. 639 640Swapping entries 641---------------- 642 643Another common operation is that two entries need to be swapped with each other. 644 645For this, Linux provides us with list_swap(). 646 647In the following example, we have a list with three entries, and swap two of 648them. This is our starting state in "State 0":: 649 650 .-----------------------------------------. 651 v | 652 .--------. .-------. .---------. .-----. | 653 | clowns |-->| Grock |-->| Dimitri |-->| Pic |-' 654 '--------' '-------' '---------' '-----' 655 656.. code-block:: c 657 658 static void circus_clowns_swap(struct circus_priv *circus) 659 { 660 struct clown *grock, *dimitri, *pic; 661 struct clown_car *car = &circus->car; 662 663 /* ... clown initialization, list adding ... */ 664 665 /* State 0 */ 666 667 list_swap(&dimitri->node, &pic->node); 668 669 /* State 1 */ 670 } 671 672The resulting list at State 1 is the following:: 673 674 .-----------------------------------------. 675 v | 676 .--------. .-------. .-----. .---------. | 677 | clowns |-->| Grock |-->| Pic |-->| Dimitri |-' 678 '--------' '-------' '-----' '---------' 679 680As is evident by comparing the diagrams, the "Pic" and "Dimitri" nodes have 681traded places. 682 683Splicing two lists together 684--------------------------- 685 686Say we have two lists, in the following example one represented by a list head 687we call "knie" and one we call "stey". In a hypothetical circus acquisition, 688the two list of clowns should be spliced together. The following is our 689situation in "State 0":: 690 691 .-----------------------------------------. 692 | | 693 v | 694 .------. .-------. .---------. .-----. | 695 | knie |-->| Grock |-->| Dimitri |-->| Pic |--' 696 '------' '-------' '---------' '-----' 697 698 .-----------------------------. 699 v | 700 .------. .---------. .-----. | 701 | stey |-->| Alfredo |-->| Pio |--' 702 '------' '---------' '-----' 703 704The function to splice these two lists together is list_splice(). Our example 705code is as follows: 706 707.. code-block:: c 708 709 static void circus_clowns_splice(void) 710 { 711 struct clown *grock, *dimitri, *pic, *alfredo, *pio; 712 struct list_head knie = LIST_HEAD_INIT(knie); 713 struct list_head stey = LIST_HEAD_INIT(stey); 714 715 /* ... Clown allocation and initialization here ... */ 716 717 list_add_tail(&grock->node, &knie); 718 list_add_tail(&dimitri->node, &knie); 719 list_add_tail(&pic->node, &knie); 720 list_add_tail(&alfredo->node, &stey); 721 list_add_tail(&pio->node, &stey); 722 723 /* State 0 */ 724 725 list_splice(&stey, &dimitri->node); 726 727 /* State 1 */ 728 } 729 730The list_splice() call here adds all the entries in ``stey`` to the list 731``dimitri``'s ``node`` list_head is in, after the ``node`` of ``dimitri``. A 732somewhat surprising diagram of the resulting "State 1" follows:: 733 734 .-----------------------------------------------------------------. 735 | | 736 v | 737 .------. .-------. .---------. .---------. .-----. .-----. | 738 | knie |-->| Grock |-->| Dimitri |-->| Alfredo |-->| Pio |-->| Pic |--' 739 '------' '-------' '---------' '---------' '-----' '-----' 740 ^ 741 .-------------------------------' 742 | 743 .------. | 744 | stey |--' 745 '------' 746 747Traversing the ``stey`` list no longer results in correct behavior. A call of 748list_for_each() on ``stey`` results in an infinite loop, as it never returns 749back to the ``stey`` list head. 750 751This is because list_splice() did not reinitialize the list_head it took 752entries from, leaving its pointer pointing into what is now a different list. 753 754If we want to avoid this situation, list_splice_init() can be used. It does the 755same thing as list_splice(), except reinitalizes the donor list_head after the 756transplant. 757 758Concurrency considerations 759-------------------------- 760 761Concurrent access and modification of a list needs to be protected with a lock 762in most cases. Alternatively and preferably, one may use the RCU primitives for 763lists in read-mostly use-cases, where read accesses to the list are common but 764modifications to the list less so. See Documentation/RCU/listRCU.rst for more 765details. 766 767Further reading 768--------------- 769 770* `How does the kernel implements Linked Lists? - KernelNewbies <https://kernelnewbies.org/FAQ/LinkedLists>`_ 771 772Full List API 773============= 774 775.. kernel-doc:: include/linux/list.h 776 :internal: 777