URL
                    https://opencores.org/ocsvn/test_project/test_project/trunk
                
            Subversion Repositories test_project
[/] [test_project/] [trunk/] [linux_sd_driver/] [Documentation/] [spinlocks.txt] - Rev 62
Compare with Previous | Blame | View Log
SPIN_LOCK_UNLOCKED and RW_LOCK_UNLOCKED defeat lockdep state tracking andare hence deprecated.Please use DEFINE_SPINLOCK()/DEFINE_RWLOCK() or__SPIN_LOCK_UNLOCKED()/__RW_LOCK_UNLOCKED() as appropriate for staticinitialization.Dynamic initialization, when necessary, may be performed asdemonstrated below.spinlock_t xxx_lock;rwlock_t xxx_rw_lock;static int __init xxx_init(void){spin_lock_init(&xxx_lock);rwlock_init(&xxx_rw_lock);...}module_init(xxx_init);The following discussion is still valid, however, with the dynamicinitialization of spinlocks or with DEFINE_SPINLOCK, etc., usedinstead of SPIN_LOCK_UNLOCKED.-----------------------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 thespinlock itself will guarantee the global lock, so it will guarantee thatthere is only one thread-of-control within the region(s) protected by thatlock.Note that it works well even under UP - the above sequence under UPessentially is just the same as doing aunsigned long flags;save_flags(flags); cli();... critical section ...restore_flags(flags);so the code does _not_ need to worry about UP vs SMP issues: the spinlockswork correctly under both (and spinlocks are actually more efficient onarchitectures that allow doing the "save_flags + cli" in one go because Idon't export that interface normally).NOTE NOTE NOTE! The reason the spinlock is so much faster than a globalinterrupt lock under SMP is exactly because it disables interrupts only onthe local CPU. The spin-lock is safe only when you _also_ use the lockitself to do locking across CPU's, which implies that EVERYTHING thattouches a shared variable has to agree about the spinlock they want touse.The above is usually pretty simple (you usually need and want only onespinlock for most things - using more than one spinlock can make things alot more complex and even slower and is usually worth it only forsequences that you _know_ need to be split up: avoid it at all cost if youaren't sure). HOWEVER, it _does_ mean that if you have some code that doescli();.. critical section ..sti();and another sequence that doesspin_lock_irqsave(flags);.. critical section ..spin_unlock_irqrestore(flags);then they are NOT mutually exclusive, and the critical regions can happenat the same time on two different CPU's. That's fine per se, but thecritical regions had better be critical for different things (ie theycan't stomp on each other).The above is a problem mainly if you end up mixing code - for example theroutines in ll_rw_block() tend to use cli/sti to protect the atomicity oftheir actions, and if a driver uses spinlocks instead then you shouldthink about issues like the above..This is really the only really hard part about spinlocks: once you startusing spinlocks they tend to expand to areas you might not have noticedbefore, because you have to make sure the spinlocks correctly protect theshared data structures _everywhere_ they are used. The spinlocks are mosteasily added to places that are completely independent of other code (ieinternal driver data structures that nobody else ever touches, forexample).----Lesson 2: reader-writer spinlocks.If your data accesses have a very natural pattern where you usually tendto mostly read from the shared variables, the reader-writer locks(rw_lock) versions of the spinlocks are often nicer. They allow multiplereaders to be in the same critical region at once, but if somebody wantsto change the variables it has to get an exclusive write lock. Theroutines 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);.. read and write exclusive access to the info ...write_unlock_irqrestore(&xxx_lock, flags);The above kind of lock is useful for complex data structures like linkedlists etc, especially when you know that most of the work is to justtraverse the list searching for entries without changing the list itself,for example. Then you can use the read lock for that kind of listtraversal, 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_time need to do any changes (even if you don't do it every time), you haveto get the write-lock at the very beginning. I could fairly easily add aprimitive to create a "upgradeable" read-lock, but it hasn't been an issueyet. Tell me if you'd want one.----Lesson 3: spinlocks revisited.The single spin-lock primitives above are by no means the only ones. Theyare the most safe ones, and the ones that work under all circumstances,but partly _because_ they are safe they are also fairly slow. They aremuch faster than a generic global cli/sti pair, but slower than they'dneed to be, because they do have to disable interrupts (which is just asingle instruction on a x86, but it's an expensive one - and on otherarchitectures it can be worse).If you have a case where you have to protect a data structure acrossseveral CPU's and you want to use spinlocks you can potentially usecheaper versions of the spinlocks. IFF you know that the spinlocks arenever used in interrupt handlers, you can use the non-irq versions:spin_lock(&lock);...spin_unlock(&lock);(and the equivalent read-write versions too, of course). The spinlock willguarantee the same kind of exclusive access, and it will be much faster.This is useful if you know that the data in question is only evermanipulated from a "process context", ie no interrupts involved.The reasons you mustn't use these versions if you have interrupts thatplay with the spinlock is that you can get deadlocks:spin_lock(&lock);...<- interrupt comes in:spin_lock(&lock);where an interrupt tries to lock an already locked variable. This is ok ifthe other interrupt happens on another CPU, but it is _not_ ok if theinterrupt happens on the same CPU that already holds the lock, because thelock will obviously never be released (because the interrupt is waitingfor the lock, and the lock-holder is interrupted by the interrupt and willnot continue until the interrupt has been processed).(This is also the reason why the irq-versions of the spinlocks only needto disable the _local_ interrupts - it's ok to use spinlocks in interruptson other CPU's, because an interrupt on another CPU doesn't interrupt theCPU that holds the lock, so the lock-holder can continue and eventuallyreleases the lock).Note that you can be clever with read-write locks and interrupts. Forexample, if you know that the interrupt only ever gets a read-lock, thenyou can use a non-irq version of read locks everywhere - because theydon't block on each other (and thus there is no dead-lock wrt interrupts.But when you do the write-lock, you have to use the irq-safe version.For an example of being clever with rw-locks, see the "waitqueue_lock"handling in kernel/sched.c - nothing ever _changes_ a wait-queue fromwithin an interrupt, they only read the queue in order to know whom towake up. So read-locks are safe (which is good: they are very commonindeed), while write-locks need to protect themselves against interrupts.Linus
