The Linux Kernel Data Structures Journey — “struct thread_struct”

Shlomi Boutnaru, Ph.D.
1 min readAug 5, 2023

Overall, the goal of “struct thread_struct” which hold CPU-specific state of a task (https://elixir.bootlin.com/linux/v6.5-rc4/source/include/linux/sched.h#L1540). Among the information “struct thread_struct” holds we can include things like page fault information (like the address that caused the page fault and fault code). Also, it can include a set of registers of the current CPU — as shown in the the diagram below.

On x86 the variable which is part of “struct task_struct” (https://medium.com/@boutnaru/linux-kernel-task-struct-829f51d97275) must be at the end of the struct. The reason for that is it contain a variable-sized structure (https://elixir.bootlin.com/linux/v6.5-rc4/source/include/linux/sched.h#L1544).

Moreover, like “struct thread_info” (https://medium.com/@boutnaru/the-linux-kernel-data-structure-journey-struct-thread-info-4e70bc20d279) also “struct thread_sturct” is CPU/Architecture dependent and thus it is defined in the source code in the following location “/arch/[CPU_ARCH]/include/asm/processor.h” (https://elixir.bootlin.com/linux/v6.5-rc4/C/ident/thread_struct). For example x86 (https://elixir.bootlin.com/linux/v6.5-rc4/source/arch/x86/include/asm/processor.h#L414) and arm64 (https://elixir.bootlin.com/linux/v6.5-rc4/source/arch/arm64/include/asm/processor.h#L147).

See you next time ;-) You can also follow me on twitter — @boutnaru (https://twitter.com/boutnaru).

https://kernel.0voice.com/forum.php?mod=viewthread&tid=2920

--

--