Skip to contentSkip to conversation

docs: document linked lists

PATCH In review Nicolas Frattaroli 3 participants 3 messages Jul 14, 2025 – Jul 17, 2025 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 docs: document linked lists

Add example-driven documentation for the kernel's generic linked list data structure. This includes discussion of situations where linked lists are likely inappropriate, and references to further reading.

---
This series started out with a much more ambitious goal of documenting every undocumented generic kernel data structure. About 6 hours in I had to tell myself that maybe it's better to do this piece-meal. So here is the first of what will hopefully be many similar series: documenting linked lists.

As an aside note, I didn't set out to exclusively use Swiss clowns in my examples, but as I reached for more names to illustrate longer lists, I discovered that the tiny nation of Switzerland has a rich history of clowning. You learn something new every day.

For future en-beautification, we could add integration for svgbob
(<https://github.com/ivanceras/svgbob>) into the kernel docs sphinx in the future, which would let us generate drawn diagrams from the text doodles, therefore serving both audiences a satisfactory viewing experience.

Based against lwn/docs-next, but b4 should let you know already.
---
Changes in v3:
- Reword commit message.
- Fix INIT_LIST_HEAD invocation.
- Stronger wording to advice against using linked lists, mention data
locality.
- Fix wording of "data struct" to be "data structure" instead.
- Fix automagic markup of struct list_head et al., including removing
some false-positives by changing "struct" to "data structure" where a
reference would be inappropriate.
- list_add() function -> list_add() macro.
- Add a sentence explaining what list_entry() does.
- Remove explanatory paragraphs after each diagram starting from where
corbet pointed out they're getting tiresome. They were originally
intended to be there to assist people using screen readers, but in
retrospect, the lack of suitable markup for them as "alt text" so to
speak makes it frustratingly verbose for regular readers.
- Move list kerneldoc from kernel-api.rst to the end of list.rst.
- Change "initialised" to "initialized" for consistency.
- Remove stray comma after "during".
- "left up" -> "left"
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- drop unrelated sphinx theme patch.
- replace graphviz diagrams with simpler ascii art in literal blocks,
which omit the back edges for clarity and are readable as plain text.
- add explanatory paragraphs after every diagram that describe what is
depicted in plain English. This is to help users that use assistive technologies like screen readers.
- add examples illustrating the use of list_cut_position and
list_cut_before.
- add examples illustrating the use of list_move, list_move_tail and list_bulk_move_tail.
- add examples illustrating the use of list_rotate_left and
list_rotate_to_front.
- add example illustrating the use of list_swap.
- add example illustrating the use of list_splice, and its caveats with
regards to stale pointers.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 Documentation/core-api/index.rst      |   1 +
 Documentation/core-api/kernel-api.rst |   6 -
 Documentation/core-api/list.rst       | 776 ++++++++++++++++++++++++++++++++++
 3 files changed, 777 insertions(+), 6 deletions(-)
Documentation/core-api/index.rst +1 -0
index 7a4ca18ca6e2d37644553cfcb0c41c9f05513294..a03a99c2cac56f4ade418f43da723e49216b1062 100644
@@ -54,6 +54,7 @@ Library functionality that is used throughout the kernel.
    union_find
    min_heap
    parser
+   list
 
 Low level entry and exit
 ========================
Documentation/core-api/kernel-api.rst +0 -6
index ae92a2571388ae7fc0b7c5f23a19ed59ac1312cc..ab8563b013f52465225e6ec49d337948e82a42ae 100644
@@ -3,12 +3,6 @@ The Linux Kernel API
 ====================
 
 
-List Management Functions
-=========================
-
-.. kernel-doc:: include/linux/list.h
-   :internal:
-
 Basic C Library Functions
 =========================
 
Documentation/core-api/list.rst new file +776 -0

776 lines not on this page · raw message

---
base-commit: f55b3ca3cf1d1652c4b3481b671940461331d69f
change-id: 20250520-linked-list-docs-ce5b956d4602

Best regards,

Signature · 1 line
Nicolas Frattaroli <[email protected]>
#1 lore raw

On 14.07.25 г. 11:14 ч., Nicolas Frattaroli wrote:
<snip>

Quoted 45 lines
Documentation/core-api/list.rst new file +45 -0
+In State 1, we arrive at the following situation::
+
+        .-----------------------------------------------------.
+        |                                                     |
+        v                                                     |
+    .--------.     .-------.     .---------.     .---------.  |
+    | clowns |---->| Grock |---->| Dimitri |---->| Alfredo |--'
+    '--------'     '-------'     '---------'     '---------'
+
+          .-------------------------------.
+          v                               |
+    .----------.     .-----.     .-----.  |
+    | sidewalk |---->| Pic |---->| Pio |--'
+    '----------'     '-----'     '-----'
+
+In State 2, after we've moved Dimitri to the tail of sidewalk, the situation
+changes as follows::
+
+        .-------------------------------------.
+        |                                     |
+        v                                     |
+    .--------.     .-------.     .---------.  |
+    | clowns |---->| Grock |---->| Alfredo |--'
+    '--------'     '-------'     '---------'
+
+          .-----------------------------------------------.
+          v                                               |
+    .----------.     .-----.     .-----.     .---------.  |
+    | sidewalk |---->| Pic |---->| Pio |---->| Dimitri |--'
+    '----------'     '-----'     '-----'     '---------'
+
+As long as the source and destination list head are part of the same list, we
+can also efficiently bulk move a segment of the list to the tail end of the
+list. We continue the previous example by adding a list_bulk_move_tail() after
+State 2, moving Pic and Pio to the tail end of the sidewalk list.
+
+.. code-block:: c
+
+  static void circus_clowns_exit_car(struct circus_priv *circus)
+  {
+          struct list_head sidewalk = LIST_HEAD_INIT(sidewalk);
+          struct clown *grock, *dimitri, *pic, *alfredo, *pio;
+          struct clown_car *car = &circus->car;
+
+          /* ... clown initialization, list adding ... */

Documentation/core-api/list.rst line 552

Quoted 10 lines
Documentation/core-api/list.rst new file +10 -0
+
+          /* State 0 */
+
+          list_move(&pic->node, &sidewalk);
+
+          /* State 1 */
+
+          list_move_tail(&dimitri->node, &sidewalk);
+
+          /* State 2 */

Nit: I think it's unnecessary to repeat the initilization in code since you've already explicitly mentioned you continue form State 2 of the previous example, so the only salient information is the additional list_bulk_move_tail call.

Quoted 9 lines
Documentation/core-api/list.rst new file +9 -0
+
+          list_bulk_move_tail(&sidewalk, &pic->node, &pio->node);
+
+          /* State 3 */
+  }
+
+For the sake of brevity, only the altered "sidewalk" list at State 3 is depicted
+in the following diagram::
+

Documentation/core-api/list.rst line 571

Quoted 10 lines
Documentation/core-api/list.rst new file +10 -0
+          .-----------------------------------------------.
+          v                                               |
+    .----------.     .---------.     .-----.     .-----.  |
+    | sidewalk |---->| Dimitri |---->| Pic |---->| Pio |--'
+    '----------'     '---------'     '-----'     '-----'
+
+Do note that list_bulk_move_tail() does not do any checking as to whether all
+three supplied ``struct list_head *`` parameters really do belong to the same
+list. If you use it outside the constraints the documentation gives, then the
+result is a matter between you and the implementation.

The last part of the sentence can be simplified to just state the result is undefined.

<snip>

Quoted 28 lines
Documentation/core-api/list.rst new file +28 -0
+
+Splicing two lists together
+---------------------------
+
+Say we have two lists, in the following example one represented by a list head
+we call "knie" and one we call "stey". In a hypothetical circus acquisition,
+the two list of clowns should be spliced together. The following is our
+situation in "State 0"::
+
+        .-----------------------------------------.
+        |                                         |
+        v                                         |
+    .------.   .-------.   .---------.   .-----.  |
+    | knie |-->| Grock |-->| Dimitri |-->| Pic |--'
+    '------'   '-------'   '---------'   '-----'
+
+        .-----------------------------.
+        v                             |
+    .------.   .---------.   .-----.  |
+    | stey |-->| Alfredo |-->| Pio |--'
+    '------'   '---------'   '-----'
+
+The function to splice these two lists together is list_splice(). Our example
+code is as follows:
+
+.. code-block:: c
+
+  static void circus_clowns_splice(void)

Documentation/core-api/list.rst line 719

Quoted 10 lines
Documentation/core-api/list.rst new file +10 -0
+  {
+          struct clown *grock, *dimitri, *pic, *alfredo, *pio;
+          struct list_head knie = LIST_HEAD_INIT(knie);
+          struct list_head stey = LIST_HEAD_INIT(stey);
+
+          /* ... Clown allocation and initialization here ... */
+
+          list_add_tail(&grock->node, &knie);
+          list_add_tail(&dimitri->node, &knie);
+          list_add_tail(&pic->node, &knie);

nit: Might add a new line to make it more visually clear that you are adding nodes to different lists.

Quoted 62 lines
Documentation/core-api/list.rst new file +57 -1
+          list_add_tail(&alfredo->node, &stey);
+          list_add_tail(&pio->node, &stey);
+
+          /* State 0 */
+
+          list_splice(&stey, &dimitri->node);
+
+          /* State 1 */
+  }
+
+The list_splice() call here adds all the entries in ``stey`` to the list
+``dimitri``'s ``node`` list_head is in, after the ``node`` of ``dimitri``. A
+somewhat surprising diagram of the resulting "State 1" follows::
+
+        .-----------------------------------------------------------------.
+        |                                                                 |
+        v                                                                 |
+    .------.   .-------.   .---------.   .---------.   .-----.   .-----.  |
+    | knie |-->| Grock |-->| Dimitri |-->| Alfredo |-->| Pio |-->| Pic |--'
+    '------'   '-------'   '---------'   '---------'   '-----'   '-----'
+                                              ^
+              .-------------------------------'
+              |
+    .------.  |
+    | stey |--'
+    '------'
+
+Traversing the ``stey`` list no longer results in correct behavior. A call of
+list_for_each() on ``stey`` results in an infinite loop, as it never returns
+back to the ``stey`` list head.
+
+This is because list_splice() did not reinitialize the list_head it took
+entries from, leaving its pointer pointing into what is now a different list.
+
+If we want to avoid this situation, list_splice_init() can be used. It does the
+same thing as list_splice(), except reinitalizes the donor list_head after the
+transplant.
+
+Concurrency considerations
+--------------------------
+
+Concurrent access and modification of a list needs to be protected with a lock
+in most cases. Alternatively and preferably, one may use the RCU primitives for
+lists in read-mostly use-cases, where read accesses to the list are common but
+modifications to the list less so. See Documentation/RCU/listRCU.rst for more
+details.
+
+Further reading
+---------------
+
+* `How does the kernel implements Linked Lists? - KernelNewbies <https://kernelnewbies.org/FAQ/LinkedLists>`_
+
+Full List API
+=============
+
+.. kernel-doc:: include/linux/list.h
+   :internal:
 
---

base-commit: f55b3ca3cf1d1652c4b3481b671940461331d69f
change-id: 20250520-linked-list-docs-ce5b956d4602

Quoted 1 line

Best regards,

One suggestion, how about adding O() complexity of the documented functions.

maintainer
#2 lore raw
Quoted 4 lines · Nikolay Borisov

Nit: I think it's unnecessary to repeat the initilization in code since you've already explicitly mentioned you continue form State 2 of the previous example, so the only salient information is the additional list_bulk_move_tail call.

[...]

I have a nit of my own: now that the kerneldoc for the hlist functions is here (it naturally moved with the rest), it would be nice to have some sort of mention, even if just a paragraph saying why hlists exist and when one might want to use them.

That said, I have gone ahead and applied this; we've put Nicolas through enough rounds, and I'd like to get this in for the 6.17 merge window. The nits can be addressed at leisure with follow-on patches.

Thanks,

jon