Prior to the Linux 2.6 kernel, struct task_struct
was present at the end of the kernel stack of each process. There was no thread_info struct
concept. But in Linux 2.6 kernel, instead of task_struct
being placed at the end of the kernel stack for the process, the thread_info struct
is placed at the end. This thread_info struct contains a pointer to the task_struct
structure.
What was the need for thread_info
structure to be introduced ?. We could have accessed the task_struct
structure using the stack pointer
directly if task_struct
was placed at the end of the kernel stack of the process.
In 2.6 Kernel, task_struct
is dynamically allocated using slab_allocator
. Prior to 2.6 Kernel, was it statically allocated?
The reason why we need the thread_info is due to the fact that we are allocating the memory for task_struct using the Slab Allocator.
It is defined as an array of unsigned long. And struct thread_info is 52 bytes. struct thread_info is stored at the bottom of stack if stack grows down and up if stack grows up. Let, Kernel Stack is 8KB of size. Also, it should have struct thread_info in it.
So that Linux can manage the processes in the system, each process is represented by a task_struct data structure (task and process are terms that Linux uses interchangeably). The task vector is an array of pointers to every task_struct data structure in the system.
From the perspective of Virtual memory system, task_struct is allocated by the Slab allocator, so that it's located in the kernel space.
FrankH, he is looking (out of pure interest as I am, I suspect) for a reason for this change. This if what I've found with my l33t google skills. A bit more info behind the link:
"task_struct is huge. it's around 1,7KB on a 32 bit machine. on the other hand, you can easily see that thread_info is much slimmer.
kernel stack is either 4 or 8KB, and either way a 1.7KB is pretty much, so storing a slimmer struct, that points to task_struct, immediately saves a lot of stack space and is a scalable solution."
(c) http://www.spinics.net/lists/newbies/msg22263.html
The reason why we need the thread_info is due to the fact that we are allocating the memory for task_struct using the Slab Allocator. Now you may ask what is the relation between these?
To understand that you need to understand how Slab Allocator works.
Without the Slab Allocator , the kernel developers could allocate memory for task_struct in the kernel stack for the particular process so that it can be accessed easily. Now with the advent of Slab Allocator , the memory is allocated to the task_struct as determined by the Slab Allocator. So with the Slab Allocator you have task_struct stored somewhere else and not in the kernel stack of the particular process. Now the Kernel developers introduced thread_info and placed a pointer in it to the place where the task_struct resides. And that is why we have to live with thread_info.
You can read about Slab Allocator in Robert Love's book Linux Kernel Development.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With