blob: 619699dde5938b64c6d63e1059cf0ed702c128af [file] [log] [blame]
SPIN_LOCK_UNLOCKED and RW_LOCK_UNLOCKED defeat lockdep state tracking and
are hence deprecated.
__SPIN_LOCK_UNLOCKED()/__RW_LOCK_UNLOCKED() as appropriate for static
Most of the time, you can simply turn:
static spinlock_t xxx_lock = SPIN_LOCK_UNLOCKED;
static DEFINE_SPINLOCK(xxx_lock);
Static structure member variables go from:
struct foo bar {
struct foo bar {
.lock = __SPIN_LOCK_UNLOCKED(bar.lock);
Declaration of static rw_locks undergo a similar transformation.
Dynamic initialization, when necessary, may be performed as
demonstrated below.
spinlock_t xxx_lock;
rwlock_t xxx_rw_lock;
static int __init xxx_init(void)
The following discussion is still valid, however, with the dynamic
initialization of spinlocks or with DEFINE_SPINLOCK, etc., used
On Fri, 2 Jan 1998, Doug Ledford wrote:
> I'm working on making the aic7xxx driver more SMP friendly (as well as
> importing the latest FreeBSD sequencer code to have 7895 support) and wanted
> to get some info from you. The goal here is to make the various routines
> SMP safe as well as UP safe during interrupts and other manipulating
> routines. So far, I've added a spin_lock variable to things like my queue
> structs. Now, from what I recall, there are some spin lock functions I can
> use to lock these spin locks from other use as opposed to a (nasty)
> save_flags(); cli(); stuff; restore_flags(); construct. Where do I find
> these routines and go about making use of them? Do they only lock on a
> per-processor basis or can they also lock say an interrupt routine from
> mucking with a queue if the queue routine was manipulating it when the
> interrupt occurred, or should I still use a cli(); based construct on that
> one?
See <asm/spinlock.h>. The basic version is:
spinlock_t xxx_lock = SPIN_LOCK_UNLOCKED;
unsigned long flags;
spin_lock_irqsave(&xxx_lock, flags);
... critical section here ..
spin_unlock_irqrestore(&xxx_lock, flags);
and the above is always safe. It will disable interrupts _locally_, but the
Note that it works well even under UP - the above sequence under UP
essentially is just the same as doing a
unsigned long flags;
save_flags(flags); cli();
... critical section ...
so the code does _not_ need to worry about UP vs SMP issues: the spinlocks
work correctly under both (and spinlocks are actually more efficient on
architectures that allow doing the "save_flags + cli" in one go because I
don't export that interface normally).
NOTE NOTE NOTE! The reason the spinlock is so much faster than a global
interrupt lock under SMP is exactly because it disables interrupts only on
the local CPU. The spin-lock is safe only when you _also_ use the lock
itself to do locking across CPU's, which implies that EVERYTHING that
touches a shared variable has to agree about the spinlock they want to
aren't sure). HOWEVER, it _does_ mean that if you have some code that does
.. critical section ..
and another sequence that does
.. critical section ..
then they are NOT mutually exclusive, and the critical regions can happen
at the same time on two different CPU's. That's fine per se, but the
critical regions had better be critical for different things (ie they
can't stomp on each other).
The above is a problem mainly if you end up mixing code - for example the
routines in ll_rw_block() tend to use cli/sti to protect the atomicity of
their actions, and if a driver uses spinlocks instead then you should
think about issues like the above..
shared data structures _everywhere_ they are used. The spinlocks are most
easily added to places that are completely independent of other code (ie
internal driver data structures that nobody else ever touches, for
Lesson 2: reader-writer spinlocks.
(rw_lock) versions of the spinlocks are often nicer. They allow multiple
to change the variables it has to get an exclusive write lock. The
routines look the same as above:
rwlock_t xxx_lock = RW_LOCK_UNLOCKED;
unsigned long flags;
read_lock_irqsave(&xxx_lock, flags);
.. critical section that only reads the info ...
read_unlock_irqrestore(&xxx_lock, flags);
write_lock_irqsave(&xxx_lock, flags);
write_unlock_irqrestore(&xxx_lock, flags);
The above kind of lock is useful for complex data structures like linked
lists etc, especially when you know that most of the work is to just
traverse the list searching for entries without changing the list itself,
for example. Then you can use the read lock for that kind of list
traversal, which allows many concurrent readers. Anything that _changes_
the list will have to get the write lock.
Note: you cannot "upgrade" a read-lock to a write-lock, so if you at _any_
to get the write-lock at the very beginning. I could fairly easily add a
primitive to create a "upgradeable" read-lock, but it hasn't been an issue
yet. Tell me if you'd want one.
Lesson 3: spinlocks revisited.
much faster than a generic global cli/sti pair, but slower than they'd
need to be, because they do have to disable interrupts (which is just a
single instruction on a x86, but it's an expensive one - and on other
architectures it can be worse).
several CPU's and you want to use spinlocks you can potentially use
The reasons you mustn't use these versions if you have interrupts that
play with the spinlock is that you can get deadlocks:
<- interrupt comes in:
lock will obviously never be released (because the interrupt is waiting
not continue until the interrupt has been processed).
releases the lock).
handling in kernel/sched.c - nothing ever _changes_ a wait-queue from