Re: Spinlocks under uniprocessor running non preemptible kernel
- From: Rainer Weikusat <rweikusat@xxxxxxxxxxx>
- Date: Fri, 21 Sep 2007 11:38:15 +0200
MAx <mahesh1280@xxxxxxxxx> writes:
I've read that spinlocks can cause system hangs if used on a
uniprocessor system which has a nonpremptible kernel running.
I've seen many spinlocks used in linux 2.4.x version running on
uniprocessor systems and there doesn't seem to be any problem with
them.
My question is how is it possible?
A 'spin lock' is something which roughly works like the code below
struct lock {
int locked;
}
void lock_it(struct lock *lock)
{
while (swap(lock->locked, 1) == 1);
}
void unlock_it(struct lock *lock)
{
lock->locked = 0;
}
'swap' is supposed to be a primitive (usually, a special assembly
instruction) which atomically stores the new value in the location
given as first argument and returns the old value. If a process
running on an uniprocessor system tries to acquired a locked spin lock
with interrupts disabled, the loop in lock_it can never terminate
because no other code will ever be executed by this CPU again. If the
kernel is non-preemtible, interrupt handlers may still execute, but
the scheduler will never schedule another process.
But the only purpose of spin locks is to prevent that processes
running on other CPUs enter the critical section and for the local
CPU, where the busy-loop cannot work, the same is accomplished by
disabling interrupts on this CPU (spin_lock_irq/ spin_lock_irqsave/
spin_lock_irqrestore). Because there are no other CPUs on an
uniprocessor, the 'spinning' parts of the spin locking code are only
actually compiled if building a kernel for a
mulitprocessor.
If a multiprocessor kernel is running on an uniprocessor system,
disabling interrupts/ preemption before trying locking the spin lock
means that neither an interrupt handler nor another process can try to
lock a locked spin lock, so the busy-wait-loop never actually loops
(interrupts/ preemption is are reenabled after unlocking the spin
lock).
.
- Follow-Ups:
- References:
- Prev by Date: Spinlocks under uniprocessor running non preemptible kernel
- Next by Date: Re: Spinlocks under uniprocessor running non preemptible kernel
- Previous by thread: Spinlocks under uniprocessor running non preemptible kernel
- Next by thread: Re: Spinlocks under uniprocessor running non preemptible kernel
- Index(es):
Relevant Pages
|