2002-03-04 05:19:02

by sridhar vaidyanathan

[permalink] [raw]
Subject: interrupt - spin lock question

I have a question related to spin locking on UP systems.Before that I would
like to point out my understanding of the background stuff
1. spinlocks shud be used in intr handlers
2. interrupts can preempt kernel code
3. spinlocks are turned to empty when kernel is compiled without SMP support.

If a particular driver is running( not the intr handler part) and at this time
an interrupt occurs. The handler has to be invoked now. Won't the preemption
cause race conditions/inconsistencies? Is any other mechanism used?
Pl correct me if I have not understood any part of this correctly
-sridhar




2002-03-04 05:28:04

by Robert Love

[permalink] [raw]
Subject: Re: interrupt - spin lock question

On Mon, 2002-03-04 at 00:18, [email protected] wrote:
> I have a question related to spin locking on UP systems.Before that I would
> like to point out my understanding of the background stuff
> 1. spinlocks shud be used in intr handlers
> 2. interrupts can preempt kernel code
> 3. spinlocks are turned to empty when kernel is compiled without SMP support.

This is right.

> If a particular driver is running( not the intr handler part) and at this time
> an interrupt occurs. The handler has to be invoked now. Won't the preemption
> cause race conditions/inconsistencies? Is any other mechanism used?
> Pl correct me if I have not understood any part of this correctly
> -sridhar

Right, that is why you would use a spin_lock ! :)

Further, you would want to use a spin_lock_irq and related friends. The
irq disable prevents the race wrt interrupts and the spin_lock prevents
racing wrt SMP.

Robert Love

2002-03-04 06:35:18

by Kittur Sameer

[permalink] [raw]
Subject: Re: interrupt - spin lock question

On Sunday 03 March 2002 09:18 pm, [email protected] wrote:
> I have a question related to spin locking on UP systems.Before that I would
> like to point out my understanding of the background stuff
> 1. spinlocks shud be used in intr handlers

It should be used in the interrupt handler, if you need to prevent any race
conditions with other interrupt/non-interrupt context code that may be
executing on some other CPU on an SMP system. Thus spinlocks need to be held
for as short a duration as possible. You would need to use the
spin_lock_irqsave/spin_unlock_irqrestore variant pair to prevent your
interrupt handler from running on the same processor while holding the lock.
This may be needed if the interrupt handler may try to acquire the same lock
thus causing a deadlock.

> 2. interrupts can preempt kernel code
> 3. spinlocks are turned to empty when kernel is compiled without SMP
> support.
>
> If a particular driver is running( not the intr handler part) and at this
> time an interrupt occurs. The handler has to be invoked now. Won't the
> preemption cause race conditions/inconsistencies? Is any other mechanism
> used? Pl correct me if I have not understood any part of this correctly

On a UP kernel the spin_lock_irqsave/spin_unlock_irqrestore pair expand to
save_flags(flag); cli()/restore_flags(flag).

The masking of interrupts on the processor between spin_lock_irqsave and
spin_unlock_irqrestore pair prevent the user context code from being
preempted by the interrupt handler.


Sameer.

2002-03-04 06:33:48

by sridhar vaidyanathan

[permalink] [raw]
Subject: Re: interrupt - spin lock question


> Right, that is why you would use a spin_lock ! :)
>
> Further, you would want to use a spin_lock_irq and related friends. The
> irq disable prevents the race wrt interrupts and the spin_lock prevents
> racing wrt SMP.
>

ok things are clear now. so the spin_lock_irq friends are actually for 2
purposes - preventing racing from interrupts and from SMP. so if SMP is not
chosen only the local_irq_disable() part works right??

do { local_irq_disable(); spin_lock(lock); } while (0)

2002-03-04 07:19:25

by Robert Love

[permalink] [raw]
Subject: Re: interrupt - spin lock question

On Mon, 2002-03-04 at 01:33, [email protected] wrote:

> ok things are clear now. so the spin_lock_irq friends are actually for 2
> purposes - preventing racing from interrupts and from SMP. so if SMP is not
> chosen only the local_irq_disable() part works right??
>
> do { local_irq_disable(); spin_lock(lock); } while (0)

Correct. Although, you would want to use spin_lock_irqsave most of the
time, not spin_lock_irq (difference is, the stored flags value is used
to restore interrupts to there previous condition instead of
unconditionally reenable them).

Btw, Go Gators!

Robert Love