Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need separate kernel stack for each CPU [closed]

In practice, many operating systems are designed to have one kernel stack for each thread, or at least one for each CPU. But for an operating system that the kernel is locked every time a process traps, it seems unnecessary to have separated kernel stacks for each CPU. Since the kernel (with its own stack) only allows single CPU access, the CPUs should never be in kernel mode simultaneously. A CPU is always blocked until the previous CPU leaves and cleans up the kernel stack, even in the nested trap case. So in which case will multiple kernel stack be necessary in such an OS? Thanks.

like image 870
Phil Guo Avatar asked Dec 19 '12 13:12

Phil Guo


1 Answers

You're right; in such a case multiple kernel stacks wouldn't be useful as long as you have a solid multi-core locking feature.

We usually have multiple kernel stacks (i.e. at least one kernel stack for each thread) for the following purposes:

  • When you want your kernel to be threaded and interruptible, like in a micro-kernel architecture.
  • When you want to migrate threads from one core to another, when load-balancing for instance. It would be cumbersome to copy the kernel stack of CPU1 to ther kernel stack of CPU2. Needless to say it would also be a performance killer since you are freezing two cores for that purpose.
  • Depending on the underlying architecture, having multiple kernel stacks makes MMU things easier (AS management, ...).
like image 177
Benoit Avatar answered Oct 02 '22 23:10

Benoit