tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: kernel_lock prememption



Rohan Akela <rohanakela%gmail.com@localhost> wrote:
> 
> I would like to know if a thread holding kernel_lock which is a spinlock
> can be preempted or not ?

As documented in kpreempt(9) manual page - no, it can not.

Think about it in general - getting some code to be preemption-safe is
usually trivial when that code is already MP-safe, not the other way round.
Therefore, the older code which still has to run under the kernel lock is
unlikely to be preemption-safe, right?

> The implementation of kernel_lock in kern_lock.c seems to suggests that it
> can be preempted because the it uses splvm which implies IPL_VM. So a
> thread holding kernel_lock can be interrupted by clock interrupt and then
> scheduler can put the thread to sleep. This behavior is contrary to the
> classic definition of spinlock.

The kernel lock uses IPL_VM just to protect its own state while acquiring.
This is because interrupts at IPL_VM are allowed to take the kernel lock
while interrupts at a higher level are not.  Of course, any interrupts can
happen while the kernel lock is taken - it would not make sense otherwise.

The decisions whether to preempt or not are made in the kpreempt() handler:

http://nxr.netbsd.org/xref/src/sys/kern/kern_synch.c#kpreempt

It checks whether the kernel lock is acquired.

-- 
Mindaugas


Home | Main Index | Thread Index | Old Index