Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kernel stack for linux process

Tags:

Is the kernel stack for all process shared or there is a seperate kernel stack for each process? If it is seperate for each process where is this stack pointer stored? In task_struct ?

like image 512
suresh Avatar asked May 20 '09 08:05

suresh


People also ask

Is process kernel stacked?

Each process has a kernel stack (or more generally, each thread has its own stack) Just like there has to be a separate place for each process to hold its set of saved registers (in its process table entry), each process also needs its own kernel stack, to work as its execution stack when it is executing in the kernel.

Where is the kernel stack located?

There is not a kernel stack. For each thread, there is a memory region that is used as stack space when the process makes a system call. There are also separate "interrupt stacks", one per CPU, which are used by the interrupt handler.

Why is kernel stack needed?

A separate kernel stack is needed for each process to save the state of the process. The state needs to be saved in case a task switch is performed, i.e. the current process is put to sleep, and some other process scheduled to run.

What are saved in kernel stack?

The saved registers on the kernel stack are used to get out of kernel mode. The context process block saves the entire register set in order to change processes.

What is kernel stack in Linux?

The kernel stack is part of the kernel space. Hence, it is not directly accessible from a user process. Whenever a user process uses a syscall, the CPU mode switches to kernel mode. During the syscall, the kernel stack of the running process is used. The size of the kernel stack is configured during compilation and remains fixed.

Why do we have separate user and kernel stacks in Linux?

With separate user and kernel stacks for each process or thread, we have better isolation. Problems in the user stack can’t cause a crash in the kernel. This isolation makes the kernel more secure because it only trusts the stack area that is under its control.

What is the size of the kernel stack during syscall?

During the syscall, the kernel stack of the running process is used. The size of the kernel stack is configured during compilation and remains fixed. This is usually two pages (8KB) for each thread. Moreover, additional per-CPU interrupt stacks are used to process external interrupts.

How many stacks does a Linux process have?

In a Linux system, every user process has 2 stacks, a user stack and a dedicated kernel stack for the process.


1 Answers

There is just one common kernel memory. In it each process has it's own task_struct + kernel stack (by default 8K).

In a context switch the old stack pointer is saved somewhere and the actual stack pointer is made to point to the top of the stack (or bottom depending on the hardware architecture) of the new process which is going to run.

like image 62
robert.berger Avatar answered Oct 12 '22 09:10

robert.berger