2003-11-30 01:00:31

by Chris Peterson

[permalink] [raw]
Subject: question about preempt_disable()

I just bought Robert Love's new book "Linux Kernel Development". The book
has been very informative, but I have some unanswered questions about kernel
preemption.

>From what I understand, SMP-safe code is also preempt-safe. The preempt
count is the number of spinlocks held by the current kernel thread. If the
preempt code is greater zero, then the kernel thread cannot be preempted.

My question is: if the code is already SMP-safe and holding the necessary
spinlocks, why is the preempt count necessary? Why must preemption be
disabled and re-enabled as spinlocks are acquired and released? Is it just
an optimization for accessing per-cpu data? Or is it necessary to prevent
priority inversion of kernel threads, when a low priority thread holds
spinlock X and is preempted by a high priority thread that hogs the CPU,
forever spinning in spin_lock(&X)?


chris


2003-11-30 17:40:04

by Matthias Urlichs

[permalink] [raw]
Subject: Re: question about preempt_disable()

Hi, Chris Peterson wrote:

> My question is: if the code is already SMP-safe and holding the necessary
> spinlocks, why is the preempt count necessary? Why must preemption be
> disabled and re-enabled as spinlocks are acquired and released?

You need to prevent deadlocks. Imagine process A grabbing a spinlock, then
getting preempted. Process B now sits there and waits on the spinlock.
Forward progress may or may not happen when the scheduler preempts B and
restarts A, some indeterminate time later.

Scheduling when waiting for a spinlock doesn't make sense because usually
the spinlock is held for just a few cycles (that's why it's a spin lock
and not a semaphore / wait queue / whatever), and rescheduling would take
more time than just waiting.

--
Matthias Urlichs | {M:U} IT Design @ m-u-it.de | [email protected]
Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de
- -
You will have many recoverable tape errors.

2003-12-01 03:53:43

by Robert Love

[permalink] [raw]
Subject: Re: question about preempt_disable()

On Sun, 2003-11-30 at 12:39, Matthias Urlichs wrote:

> You need to prevent deadlocks. Imagine process A grabbing a spinlock, then
> getting preempted. Process B now sits there and waits on the spinlock.
> Forward progress may or may not happen when the scheduler preempts B and
> restarts A, some indeterminate time later.

Further, on uniprocessor systems, we don't have deadlocks so it is the
preempt_disable() that actually ensures concurrency is prevented in the
critical region.

Rob Love