Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Kernel Preemption during spin_lock and mutex_lock

When a process in the kernel space is holding a spin_lock, the process cannot be preempted due to any of the following conditions :

  1. When the time-slice of the process gets exhausted
  2. When a high priority process becomes runnable
  3. When an interrupt occurs

However the process can yield the processor if it blocks, sleeps, or explicitly call schedule(). Is my understanding correct?

When a process in the kernel space is holding a mutex_lock, can the process be preempted due to the above conditions listed as 1, 2 and 3.

like image 410
nitin_cherian Avatar asked Jul 02 '11 05:07

nitin_cherian


People also ask

Is the Linux kernel preemptive?

4, Linux Kernel was not preemptive which means a process running in kernel mode cannot be moved out of processor until it itself leaves the processor or it starts waiting for some input output operation to get complete. Generally a process in user mode can enter into kernel mode using system calls.

Does spinlock disable preemption?

spinlock automatically disables preemption, which avoids deadlock caused by interrupts. when data is shared with interrupt handler, before holding spinlock we must disable interrupts. when data is shared with bottom halves, before holding spinlock we must disable bottom halves.

What is preemption in Linux kernel?

In computer operating system design, kernel preemption is a property possessed by some kernels (the cores of operating systems), in which the CPU can be interrupted in the middle of executing kernel code and assigned other tasks (from which it later returns to finish its kernel tasks.

Can mutex lock be preempted?

When a pthread holding a lock(mutex or rwlock or spinlock), it can be preempted ? No, it can't be preempted in case of non preemptive kernel.


1 Answers

Current implementations of spin locks use two entirely separate mechanisms to ensure mutual exclusion, one for dealing with inter-processor exclusion and one for dealing with the local processor threads and interrupt handlers.

  • There is the spin_lock itself which is only there to provide mutex between two or more processor cores. Any processor hitting a locked spin lock is basically stuck until another processor releases it. Spin locks serve no purpose on single processor systems - other than to increase the chance of total deadlock - so are usually removed at kernel compile time.

  • To provide local processor mutex, spin_lock() calls preempt_disable() (on pre-emptive scheduling systems) to prevent any other thread from running whilst the lock is held; similarly spin_lock_irqsave() also does the equivalent of local_irq_save() to disable interrupts to prevent anything else at all running on the local processor.

As should be obvious from the above, using spin locks can gum up the whole machine so spin locks should just be used for very short periods of time and you should never do anything that might cause a reschedule whilst holding a lock.

The case with mutex_lock is totally different - only threads attempting to access the lock are affected and if a thread hits a locked mutex then a reschedule will occur. For this reason mutex_locks cannot be used in interrupt (or other atomic) contexts.

like image 57
Dipstick Avatar answered Sep 28 '22 09:09

Dipstick