0

I am having a doubt on the premptiveness of the Linux kernel. I know that user space processes and threads are premptible, but what about kernel level threads, those created using kthread_create.

If a thread is created using kthread_create, can it be preempted by scheduler and another kernel thread be executed?

If suppose the kernel thread just performs while(1) {}, will it always run to completion occupying a particular CPU, or the scheduler can schedule out the thread ? Can it cause softlockup message?

I am trying to understand RCU, but I realised, I need to understand, preemptive non-preemptive kernels first.

Haswell
  • 1,573
  • 1
  • 18
  • 45
  • Timer interrupts on each CPU run scheduling. If the kernel thread is interruptible then it can be preempted. – stark May 21 '23 at 10:40
  • The kernel thread is not taking any locks, so interrupts are enabled here. Just doing a nop while(1) {nop}. – Haswell May 21 '23 at 11:19
  • @stark as far as I know new kernels can schedule on _any_ interrupt. Timer is a legacy approach. – 0andriy May 21 '23 at 14:24
  • Even the tickless kernels set timers so that there is a maximum scheduling latency. Timers are still the basic mechanism. – stark May 21 '23 at 15:36

1 Answers1

1

Yes, kernel threads (created e.g. with kthread_create()) are preemptible. For all it matters, kthreads and userspace threads are treated in the same way when it comes to scheduling. See also: What is the default scheduling policy of a Linux kernel thread?

0andriy
  • 4,183
  • 1
  • 24
  • 37
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128