Understanding the Linux Kernel list.h Linked List Implementation
This article explains the classic Linux kernel list linked‑list data structure, covering its definition, file locations, core operations such as initialization, insertion, deletion, replacement, moving, splitting and merging, the list_entry macro, usage examples, and important multithreading considerations.
Linux kernel contains many classic data structures, and the list linked list is one of them. This article introduces the list definition, its operations, precautions, and usage examples.
Contents
list definition
list operations
precautions
usage example
File locations
All list operations are defined in include/linux/list.h . The list_head definition is in include/linux/types.h .
Definition
The list is a doubly‑circular linked list with a head pointer. The list_head structure contains only forward and backward pointers and is meant to be embedded in user‑defined structs to chain them together.
Operations
Initialization
Insertion
Insert a new element between two existing elements (the basis for head and tail insertion).
Head insertion places the new element between the head and the first element; tail insertion places it between the last element and the head (because the list is circular).
Deletion
Remove an element between two others or delete a known entry.
Replacement
Pointer manipulation to replace one element with another.
Move
Move an element to the head or tail of another list.
Split
Divide a list at a specified entry into two separate lists.
Merge
Insert one list into another at the head or tail.
list_entry macro
The macro uses the embedded list_head pointer to retrieve the containing struct via the classic Linux container_of macro.
Various helper macros are provided for traversal and entry retrieval.
Precautions
When multiple threads operate on the same list, proper locking is required.
Usage Example
360 Tech Engineering
Official tech channel of 360, building the most professional technology aggregation platform for the brand.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.