list.rst 26 KB

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