Re: Spinlocks under uniprocessor running non preemptible kernel



On Sep 21, 2:38 pm, Rainer Weikusat <rweiku...@xxxxxxxxxxx> wrote:
MAx <mahesh1...@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).

Thanks :-)

.



Relevant Pages

  • Re: Spinlocks under uniprocessor running non preemptible kernel
    ... uniprocessor system which has a nonpremptible kernel running. ... A 'spin lock' is something which roughly works like the code below ...
    (comp.os.linux.development.system)
  • Re: Spinlocks under uniprocessor running non preemptible kernel
    ... uniprocessor system which has a nonpremptible kernel running. ... A 'spin lock' is something which roughly works like the code below ...
    (comp.os.linux.development.system)
  • [patch 00/10] PI-futex: -V1
    ... We are pleased to announce "lightweight userspace priority inheritance" ... No registration, no extra kernel ... only a single owner may own a lock (i.e. no ... Priority Inheritance - why, oh why??? ...
    (Linux-Kernel)
  • Re: CFR: New NFS Lock Manager
    ... It also includes minor fixes to support 64bit architectures and RELENG_7. ... Lock Manager which runs in kernel mode and uses the normal local locking infrastructure for its state. ... A single thread should be sufficient for the NLM since it should rarely block in normal operation. ...
    (freebsd-current)
  • [patch 3/6] lightweight robust futexes: docs
    ... +The robust futex ABI ... for kernel assist of cleanup of held locks on task exit. ... will attempt to process both lists on each task ... pointer to a single linked list of 'lock entries', one per lock, ...
    (Linux-Kernel)