Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need for thread_info structure in Linux 2.6 Kernel?

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?

like image 683
nitin_cherian Avatar asked May 26 '11 05:05

nitin_cherian


People also ask

What was the need of thread_info structure?

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.

What is thread_info structure?

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.

What important concept task_struct is representing in Linux?

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.

Where is task_struct stored?

From the perspective of Virtual memory system, task_struct is allocated by the Slab allocator, so that it's located in the kernel space.


2 Answers

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

like image 81
Tux Avatar answered Nov 02 '22 23:11

Tux


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.

like image 38
PaulDaviesC Avatar answered Nov 03 '22 01:11

PaulDaviesC