views:

432

answers:

1

On a SMP machine we must use spin_lock_irqsave and not spin_lock_irq from interrupt context.

Why would we want to save the flags (which contains the IF)?

Is there another interrupt routine that could interrupt us?

+1  A: 

Hi cojocar,

I am new in the kernel but from what I gather from Robert Love book "Linux Kernel Development". It says that if on the processor the interrupt state is already disable before your code starts a lock, when you call spin_unlock_irq you will release the lock in an erroneous manner. If you save the flag and release it with a flag the function spin_lock_irqsave will just return the interrupt to it's previous state.

Example with irqsave

spinlock_t mLocl = SPIN_LOCK_UNLOCK;
unsigned long flags;

spin_lock_irqsave(&mLock, flags); // save the state, if locked already it is saved in flags
// Critical section
spin_unlock_irqsave(&mLock, flags); // return to the formally state specified in flags

Example without irqsave:

spinlock_t mLocl = SPIN_LOCK_UNLOCK;
unsigned long flags;

spin_lock_irq(&mLock); // Does not know if already locked
// Critical section
spin_unlock_irq(&mLock); // Could result in an error unlock...
Hagai