Why do we need a separate kernel stack for each processor

In practice, many operating systems have one kernel stack for each thread, or at least one for each processor. But for the operating system, when the kernel is locked every time the process hooks, it seems there is no need to have separate kernel stacks for each processor. Since the kernel (with its own stack) allows only access to one processor, the CPU should never be in kernel mode at the same time. The CPU is always locked until the previous processor leaves and clears the kernel stack, even in the case of nested traps. So, in which case will you need several kernel stacks in such an OS? Thanks.

+4
source share
2 answers

You're right; in this case, several kernel stacks will not be useful if you have solid multi-core blocking.

Usually we have several kernel stacks (i.e. at least one kernel stack for each thread) for the following purposes:

  • If you want your kernel to be streaming and intermittent, for example, in microkernel architecture.
  • If you want to transfer threads from one core to another, for example, during load balancing. It would be cumbersome to copy the kernel stack of CPU1 to the kernel stack of CPU2. Needless to say, this will also be a performance killer since you are freezing two cores for this purpose.
  • Depending on the underlying architecture, having multiple kernel stacks makes it easier to work with MMUs (AS management, ...).
+4
source

In practice, many operating systems do not limit the number of threads that execute kernel-level code. In fact, this limitation would seriously limit kernel scalability. Without this limitation, multiple kernel stacks are required.

In addition, in practice, operating systems can allow the suspension and migration of threads that are currently running in kernel mode, which again links the kernel stack to the thread, and not to the processor.

If you are developing an operating system that does not have these functions and instead limits kernel-level execution to a single thread, then only one stack will be needed.

+1
source

All Articles