0
votes

I have read that linux kernel is multi threaded and there can be multiple threads running concurrently in each core. In a SMP (symmetric multiprocessing) environment where a single OS manages all the processors/cores how is multithreading implemented?

Is that kernel threads are spawned and each dedicated to manage a core. If so when are these kernel threads created? Is it during bootup at kern_init() after the bootstrapping is complete and immediately after the Application processors are enabled by the bootstrap processor.

So does each core have its own scheduler(implemented by the core's kernel thread) that manages the tasks from a common pool shared by all kernel threads?

How does (direct) messaging between kernel threads residing on different cores happen when they need to intimate some events that another kernel thread might be interested in?

I also thought if one particular selected core with one kernel scheduler that on every system timer interrupt acquire a big kernel lock and decide/schedule what to run on each core?

So I would appreciate any clarity in the implementation details. Thanks in advance for your help.

1
As far as I can tell there is nothing special about kernel threads besides running with elevated privileges. They use the same scheduler and cores as all the other threads and the same rules for locking and messaging apply.nwp
Your question seems to be based on a misunderstanding about the relationship between threads and cores. Threads are free to move from core to core.David Schwartz
@DavidSchwartz So you mean that kernel is a single process that has one scheduler to schedule the threads(during every system timer interrupt) in different available execution units(cores)?Shyam
@Shyam Yes. Linux's scheduler code is heavily optimized to minimize contention even when it's running on many cores at once. Many system calls wind up calling the scheduler either because the thread is no longer ready-to-run or to check if the scheduler would prefer to switch to some other thread.David Schwartz
Every thread that can make a syscall has its own user stack and its own kernel stack. It uses the user stack when running in user space and the kernel stack when running in kernel space.David Schwartz

1 Answers

1
votes

Early in kernel startup, a thread is started for each core. It is set to the lowest possible priority and generally does nothing but reduce the CPU power and wait for an interrupt. When actual work needs to get done, it's either done by threads other than these threads or by hardware interrupts which interrupt either this thread or some other thread.

The scheduler is typically invoked either by a timer interrupt or by a thread transitioning from running to a state in which it's no longer ready to run. Kernel calls that transition a thread to a state in which it's no longer ready to run typically invoke the scheduler to let the core perform some other task.