1- SPIN_LOCK_UNLOCKED and RW_LOCK_UNLOCKED defeat lockdep state tracking and
2- are hence deprecated.
1+ Lesson 1: Spin locks
32
4- Please use DEFINE_SPINLOCK()/DEFINE_RWLOCK() or
5- __SPIN_LOCK_UNLOCKED()/__RW_LOCK_UNLOCKED() as appropriate for static
6- initialization.
7-
8- Most of the time, you can simply turn:
9-
10- static spinlock_t xxx_lock = SPIN_LOCK_UNLOCKED;
11-
12- into:
13-
14- static DEFINE_SPINLOCK(xxx_lock);
15-
16- Static structure member variables go from:
17-
18- struct foo bar {
19- .lock = SPIN_LOCK_UNLOCKED;
20- };
21-
22- to:
23-
24- struct foo bar {
25- .lock = __SPIN_LOCK_UNLOCKED(bar.lock);
26- };
27-
28- Declaration of static rw_locks undergo a similar transformation.
29-
30- Dynamic initialization, when necessary, may be performed as
31- demonstrated below.
32-
33- spinlock_t xxx_lock;
34- rwlock_t xxx_rw_lock;
35-
36- static int __init xxx_init(void)
37- {
38- spin_lock_init(&xxx_lock);
39- rwlock_init(&xxx_rw_lock);
40- ...
41- }
42-
43- module_init(xxx_init);
44-
45- The following discussion is still valid, however, with the dynamic
46- initialization of spinlocks or with DEFINE_SPINLOCK, etc., used
47- instead of SPIN_LOCK_UNLOCKED.
48-
49- -----------------------
50-
51- On Fri, 2 Jan 1998, Doug Ledford wrote:
52- >
53- > I'm working on making the aic7xxx driver more SMP friendly (as well as
54- > importing the latest FreeBSD sequencer code to have 7895 support) and wanted
55- > to get some info from you. The goal here is to make the various routines
56- > SMP safe as well as UP safe during interrupts and other manipulating
57- > routines. So far, I've added a spin_lock variable to things like my queue
58- > structs. Now, from what I recall, there are some spin lock functions I can
59- > use to lock these spin locks from other use as opposed to a (nasty)
60- > save_flags(); cli(); stuff; restore_flags(); construct. Where do I find
61- > these routines and go about making use of them? Do they only lock on a
62- > per-processor basis or can they also lock say an interrupt routine from
63- > mucking with a queue if the queue routine was manipulating it when the
64- > interrupt occurred, or should I still use a cli(); based construct on that
65- > one?
66-
67- See <asm/spinlock.h>. The basic version is:
68-
69- spinlock_t xxx_lock = SPIN_LOCK_UNLOCKED;
3+ The most basic primitive for locking is spinlock.
704
5+ static DEFINE_SPINLOCK(xxx_lock);
716
727 unsigned long flags;
738
749 spin_lock_irqsave(&xxx_lock, flags);
7510 ... critical section here ..
7611 spin_unlock_irqrestore(&xxx_lock, flags);
7712
78- and the above is always safe. It will disable interrupts _locally_, but the
13+ The above is always safe. It will disable interrupts _locally_, but the
7914spinlock itself will guarantee the global lock, so it will guarantee that
8015there is only one thread-of-control within the region(s) protected by that
81- lock.
82-
83- Note that it works well even under UP - the above sequence under UP
84- essentially is just the same as doing a
16+ lock. This works well even under UP. The above sequence under UP
17+ essentially is just the same as doing
8518
8619 unsigned long flags;
8720
@@ -91,15 +24,13 @@ essentially is just the same as doing a
9124
9225so the code does _not_ need to worry about UP vs SMP issues: the spinlocks
9326work correctly under both (and spinlocks are actually more efficient on
94- architectures that allow doing the "save_flags + cli" in one go because I
95- don't export that interface normally).
27+ architectures that allow doing the "save_flags + cli" in one operation).
28+
29+ NOTE! Implications of spin_locks for memory are further described in:
9630
97- NOTE NOTE NOTE! The reason the spinlock is so much faster than a global
98- interrupt lock under SMP is exactly because it disables interrupts only on
99- the local CPU. The spin-lock is safe only when you _also_ use the lock
100- itself to do locking across CPU's, which implies that EVERYTHING that
101- touches a shared variable has to agree about the spinlock they want to
102- use.
31+ Documentation/memory-barriers.txt
32+ (5) LOCK operations.
33+ (6) UNLOCK operations.
10334
10435The above is usually pretty simple (you usually need and want only one
10536spinlock for most things - using more than one spinlock can make things a
@@ -120,34 +51,42 @@ and another sequence that does
12051then they are NOT mutually exclusive, and the critical regions can happen
12152at the same time on two different CPU's. That's fine per se, but the
12253critical regions had better be critical for different things (ie they
123- can't stomp on each other).
54+ can't stomp on each other).
12455
12556The above is a problem mainly if you end up mixing code - for example the
12657routines in ll_rw_block() tend to use cli/sti to protect the atomicity of
12758their actions, and if a driver uses spinlocks instead then you should
128- think about issues like the above..
59+ think about issues like the above.
12960
13061This is really the only really hard part about spinlocks: once you start
13162using spinlocks they tend to expand to areas you might not have noticed
13263before, because you have to make sure the spinlocks correctly protect the
13364shared data structures _everywhere_ they are used. The spinlocks are most
134- easily added to places that are completely independent of other code (ie
135- internal driver data structures that nobody else ever touches, for
136- example).
65+ easily added to places that are completely independent of other code (for
66+ example, internal driver data structures that nobody else ever touches).
67+
68+ NOTE! The spin-lock is safe only when you _also_ use the lock itself
69+ to do locking across CPU's, which implies that EVERYTHING that
70+ touches a shared variable has to agree about the spinlock they want
71+ to use.
13772
13873----
13974
14075Lesson 2: reader-writer spinlocks.
14176
14277If your data accesses have a very natural pattern where you usually tend
14378to mostly read from the shared variables, the reader-writer locks
144- (rw_lock) versions of the spinlocks are often nicer . They allow multiple
79+ (rw_lock) versions of the spinlocks are sometimes useful . They allow multiple
14580readers to be in the same critical region at once, but if somebody wants
146- to change the variables it has to get an exclusive write lock. The
147- routines look the same as above:
81+ to change the variables it has to get an exclusive write lock.
14882
149- rwlock_t xxx_lock = RW_LOCK_UNLOCKED;
83+ NOTE! reader-writer locks require more atomic memory operations than
84+ simple spinlocks. Unless the reader critical section is long, you
85+ are better off just using spinlocks.
15086
87+ The routines look the same as above:
88+
89+ rwlock_t xxx_lock = RW_LOCK_UNLOCKED;
15190
15291 unsigned long flags;
15392
@@ -159,18 +98,21 @@ routines look the same as above:
15998 .. read and write exclusive access to the info ...
16099 write_unlock_irqrestore(&xxx_lock, flags);
161100
162- The above kind of lock is useful for complex data structures like linked
163- lists etc, especially when you know that most of the work is to just
164- traverse the list searching for entries without changing the list itself,
165- for example. Then you can use the read lock for that kind of list
166- traversal, which allows many concurrent readers. Anything that _changes_
167- the list will have to get the write lock.
101+ The above kind of lock may be useful for complex data structures like
102+ linked lists, especially searching for entries without changing the list
103+ itself. The read lock allows many concurrent readers. Anything that
104+ _changes_ the list will have to get the write lock.
105+
106+ NOTE! RCU is better for list traversal, but requires careful
107+ attention to design detail (see Documentation/RCU/listRCU.txt).
168108
169- Note: you cannot "upgrade" a read-lock to a write-lock, so if you at _any_
109+ Also, you cannot "upgrade" a read-lock to a write-lock, so if you at _any_
170110time need to do any changes (even if you don't do it every time), you have
171- to get the write-lock at the very beginning. I could fairly easily add a
172- primitive to create a "upgradeable" read-lock, but it hasn't been an issue
173- yet. Tell me if you'd want one.
111+ to get the write-lock at the very beginning.
112+
113+ NOTE! We are working hard to remove reader-writer spinlocks in most
114+ cases, so please don't add a new one without consensus. (Instead, see
115+ Documentation/RCU/rcu.txt for complete information.)
174116
175117----
176118
@@ -233,4 +175,46 @@ indeed), while write-locks need to protect themselves against interrupts.
233175
234176 Linus
235177
178+ ----
179+
180+ Reference information:
181+
182+ For dynamic initialization, use spin_lock_init() or rwlock_init() as
183+ appropriate:
184+
185+ spinlock_t xxx_lock;
186+ rwlock_t xxx_rw_lock;
187+
188+ static int __init xxx_init(void)
189+ {
190+ spin_lock_init(&xxx_lock);
191+ rwlock_init(&xxx_rw_lock);
192+ ...
193+ }
194+
195+ module_init(xxx_init);
196+
197+ For static initialization, use DEFINE_SPINLOCK() / DEFINE_RWLOCK() or
198+ __SPIN_LOCK_UNLOCKED() / __RW_LOCK_UNLOCKED() as appropriate.
199+
200+ SPIN_LOCK_UNLOCKED and RW_LOCK_UNLOCKED are deprecated. These interfere
201+ with lockdep state tracking.
202+
203+ Most of the time, you can simply turn:
204+ static spinlock_t xxx_lock = SPIN_LOCK_UNLOCKED;
205+ into:
206+ static DEFINE_SPINLOCK(xxx_lock);
207+
208+ Static structure member variables go from:
209+
210+ struct foo bar {
211+ .lock = SPIN_LOCK_UNLOCKED;
212+ };
213+
214+ to:
236215
216+ struct foo bar {
217+ .lock = __SPIN_LOCK_UNLOCKED(bar.lock);
218+ };
219+
220+ Declaration of static rw_locks undergo a similar transformation.
0 commit comments