Fundamentals 5 min read

Unlocking Linux Kernel Magic: How container_of Retrieves Struct Addresses

This article explains the Linux kernel’s widely used container_of macro, showing how it converts a member’s address into the containing structure’s base address through examples, typeof and offsetof usage, and demonstrates the underlying calculations with clear code snippets and illustrations.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Unlocking Linux Kernel Magic: How container_of Retrieves Struct Addresses

In the Linux kernel, the macro container_of(ptr, type, member) is extensively used, for example in the linked list implementation list_head and work queue structures work_struct.

The macro is defined roughly as:

#define container_of(ptr, type, member) \
    ((type *)((char *)(ptr) - offsetof(type, member)))

It allows you to obtain the address of the whole structure when you only have the address of one of its members.

Consider the following structure:

struct test {
    int i;
    int j;
    char k;
};

struct test temp;

If you have the address of temp.j, you can retrieve the address of temp itself with: container_of(&temp.j, struct test, j) The macro works by first creating a temporary pointer to the member using GNU C’s typeof extension:

const typeof(((struct test *)0)->j) * __mptr = (&temp.j);

Here typeof deduces the type of j (an int) and stores the member’s address in __mptr. The final address of the containing structure is then calculated as:

(struct test *)((char *)__mptr - offsetof(struct test, j))

The offsetof macro, defined in the kernel as:

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

produces the byte offset of j within struct test. Subtracting this offset from the member’s address yields the base address of the structure.

The accompanying images illustrate the macro expansion, the offset calculation, and the final result of the example.

Thus, a seemingly simple macro in the Linux kernel encapsulates a clever technique for navigating from a member pointer back to its enclosing structure.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linux kernelSystems ProgrammingC macrocontainer_ofoffsetof
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.