Re: spin_lock and interupts

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi!

On 16:44 Mon 27 Oct     , Leon Ljunggren wrote:
> To ensure that  this doesn't happen I'm trying to use
> spin_lock_irqsave, but I'm getting rather unexpected results. First of
> all it doesn't seem to disable the interupt, the interupt function is
> still called

This is strange.

> and secondly it just seems to ignore the lock. Even if
> avr_read has aquired lock2 and then gets interupted (which it
> shouldn't since interupts should be disabled, right?) avr_interupt
> will not care and still call the write/read functions instead of
> blocking as expected.

This is OK. On a single cpu system there is no lock. This is because nothing
can interrupt the critical section with interrupts disabled.

> Any pointers to what I might be doing wrong?
> 
> Thanks
> /Leon Ljunggren
> 
> ----- pseudo code ----
> 
> spinlock_t lock1 = SPIN_LOCK_UNLOCKED;
> spinlock_t lock2 = SPIN_LOCK_UNLOCKED;
> 
> i2c_read()
> {
>    spin_lock_irqsave(&lock1, lock1_flag);
>    ...
>    spin_unlock_irqrestore(&lock2, lock2_flag);
> }

This is wrong. It should be e.g.:
i2c_read()
{
	long iflags;
	spin_lock_irqsave(&lock1, iflags);
	...
	spin_unlock_irqrestore(&lock1, iflags);
}

Or if you want to nest locks (avoid if possible and make sure you *always* take
the lock in the same order if you cannot):
i2c_read()
{
        long iflags;
        spin_lock_irqsave(&lock1, iflags);
	spin_lock(&lock2)
        ...
	spin_unlock(&lock2)
        spin_unlock_irqrestore(&lock1, iflags);
}

1) Unlock the same lock you have locked, not something else
2) Declare iflags on the stack and not globally or somewhere else
3) Do not reuse or change iflags between spin_lock_irqsave and
spin_unlock_irqrestore
	-Michi
-- 
programing a layer 3+4 network protocol for mesh networks
see http://michaelblizek.twilightparadox.com


--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux