diff options
author | William Allen Simpson <william.allen.simpson@gmail.com> | 2009-12-13 15:12:46 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-12-14 12:46:56 -0500 |
commit | fb0bbb92d42d5bd0ab224605444efdfed06d6934 (patch) | |
tree | a33df719be258cc1d15036160d644d76986d2cdc /Documentation/spinlocks.txt | |
parent | f40542532e96dda5506eb76badea322f2ae4731c (diff) |
Documentation: rw_lock lessons learned
In recent months, two different network projects erroneously
strayed down the rw_lock path. Update the Documentation
based upon comments by Eric Dumazet and Paul E. McKenney in
those threads.
Further updates await somebody else with more expertise.
Changes:
- Merged with extensive content by Stephen Hemminger.
- Fix one of the comments by Linus Torvalds.
Signed-off-by: William.Allen.Simpson@gmail.com
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'Documentation/spinlocks.txt')
-rw-r--r-- | Documentation/spinlocks.txt | 184 |
1 files changed, 84 insertions, 100 deletions
diff --git a/Documentation/spinlocks.txt b/Documentation/spinlocks.txt index 619699dde593..178c831b907d 100644 --- a/Documentation/spinlocks.txt +++ b/Documentation/spinlocks.txt | |||
@@ -1,73 +1,8 @@ | |||
1 | SPIN_LOCK_UNLOCKED and RW_LOCK_UNLOCKED defeat lockdep state tracking and | 1 | Lesson 1: Spin locks |
2 | are hence deprecated. | ||
3 | 2 | ||
4 | Please use DEFINE_SPINLOCK()/DEFINE_RWLOCK() or | 3 | The most basic primitive for locking is spinlock. |
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; | ||
70 | 4 | ||
5 | static DEFINE_SPINLOCK(xxx_lock); | ||
71 | 6 | ||
72 | unsigned long flags; | 7 | unsigned long flags; |
73 | 8 | ||
@@ -75,13 +10,11 @@ See <asm/spinlock.h>. The basic version is: | |||
75 | ... critical section here .. | 10 | ... critical section here .. |
76 | spin_unlock_irqrestore(&xxx_lock, flags); | 11 | spin_unlock_irqrestore(&xxx_lock, flags); |
77 | 12 | ||
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 |
79 | spinlock itself will guarantee the global lock, so it will guarantee that | 14 | spinlock itself will guarantee the global lock, so it will guarantee that |
80 | there is only one thread-of-control within the region(s) protected by that | 15 | there is only one thread-of-control within the region(s) protected by that |
81 | lock. | 16 | lock. This works well even under UP. The above sequence under UP |
82 | 17 | essentially is just the same as doing | |
83 | Note that it works well even under UP - the above sequence under UP | ||
84 | essentially is just the same as doing a | ||
85 | 18 | ||
86 | unsigned long flags; | 19 | unsigned long flags; |
87 | 20 | ||
@@ -91,15 +24,13 @@ essentially is just the same as doing a | |||
91 | 24 | ||
92 | so the code does _not_ need to worry about UP vs SMP issues: the spinlocks | 25 | so the code does _not_ need to worry about UP vs SMP issues: the spinlocks |
93 | work correctly under both (and spinlocks are actually more efficient on | 26 | work correctly under both (and spinlocks are actually more efficient on |
94 | architectures that allow doing the "save_flags + cli" in one go because I | 27 | architectures that allow doing the "save_flags + cli" in one operation). |
95 | don't export that interface normally). | 28 | |
29 | NOTE! Implications of spin_locks for memory are further described in: | ||
96 | 30 | ||
97 | NOTE NOTE NOTE! The reason the spinlock is so much faster than a global | 31 | Documentation/memory-barriers.txt |
98 | interrupt lock under SMP is exactly because it disables interrupts only on | 32 | (5) LOCK operations. |
99 | the local CPU. The spin-lock is safe only when you _also_ use the lock | 33 | (6) UNLOCK operations. |
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. | ||
103 | 34 | ||
104 | The above is usually pretty simple (you usually need and want only one | 35 | The above is usually pretty simple (you usually need and want only one |
105 | spinlock for most things - using more than one spinlock can make things a | 36 | spinlock for most things - using more than one spinlock can make things a |
@@ -120,20 +51,24 @@ and another sequence that does | |||
120 | then they are NOT mutually exclusive, and the critical regions can happen | 51 | then they are NOT mutually exclusive, and the critical regions can happen |
121 | at the same time on two different CPU's. That's fine per se, but the | 52 | at the same time on two different CPU's. That's fine per se, but the |
122 | critical regions had better be critical for different things (ie they | 53 | critical regions had better be critical for different things (ie they |
123 | can't stomp on each other). | 54 | can't stomp on each other). |
124 | 55 | ||
125 | The above is a problem mainly if you end up mixing code - for example the | 56 | The above is a problem mainly if you end up mixing code - for example the |
126 | routines in ll_rw_block() tend to use cli/sti to protect the atomicity of | 57 | routines in ll_rw_block() tend to use cli/sti to protect the atomicity of |
127 | their actions, and if a driver uses spinlocks instead then you should | 58 | their 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. |
129 | 60 | ||
130 | This is really the only really hard part about spinlocks: once you start | 61 | This is really the only really hard part about spinlocks: once you start |
131 | using spinlocks they tend to expand to areas you might not have noticed | 62 | using spinlocks they tend to expand to areas you might not have noticed |
132 | before, because you have to make sure the spinlocks correctly protect the | 63 | before, because you have to make sure the spinlocks correctly protect the |
133 | shared data structures _everywhere_ they are used. The spinlocks are most | 64 | shared data structures _everywhere_ they are used. The spinlocks are most |
134 | easily added to places that are completely independent of other code (ie | 65 | easily added to places that are completely independent of other code (for |
135 | internal driver data structures that nobody else ever touches, for | 66 | example, internal driver data structures that nobody else ever touches). |
136 | example). | 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. | ||
137 | 72 | ||
138 | ---- | 73 | ---- |
139 | 74 | ||
@@ -141,13 +76,17 @@ Lesson 2: reader-writer spinlocks. | |||
141 | 76 | ||
142 | If your data accesses have a very natural pattern where you usually tend | 77 | If your data accesses have a very natural pattern where you usually tend |
143 | to mostly read from the shared variables, the reader-writer locks | 78 | to 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 |
145 | readers to be in the same critical region at once, but if somebody wants | 80 | readers 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 | 81 | to change the variables it has to get an exclusive write lock. |
147 | routines look the same as above: | ||
148 | 82 | ||
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. | ||
150 | 86 | ||
87 | The routines look the same as above: | ||
88 | |||
89 | rwlock_t xxx_lock = RW_LOCK_UNLOCKED; | ||
151 | 90 | ||
152 | unsigned long flags; | 91 | unsigned long flags; |
153 | 92 | ||
@@ -159,18 +98,21 @@ routines look the same as above: | |||
159 | .. read and write exclusive access to the info ... | 98 | .. read and write exclusive access to the info ... |
160 | write_unlock_irqrestore(&xxx_lock, flags); | 99 | write_unlock_irqrestore(&xxx_lock, flags); |
161 | 100 | ||
162 | The above kind of lock is useful for complex data structures like linked | 101 | The above kind of lock may be useful for complex data structures like |
163 | lists etc, especially when you know that most of the work is to just | 102 | linked lists, especially searching for entries without changing the list |
164 | traverse the list searching for entries without changing the list itself, | 103 | itself. The read lock allows many concurrent readers. Anything that |
165 | for example. Then you can use the read lock for that kind of list | 104 | _changes_ the list will have to get the write lock. |
166 | traversal, which allows many concurrent readers. Anything that _changes_ | 105 | |
167 | the list will have to get the write lock. | 106 | NOTE! RCU is better for list traversal, but requires careful |
107 | attention to design detail (see Documentation/RCU/listRCU.txt). | ||
168 | 108 | ||
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_ |
170 | time need to do any changes (even if you don't do it every time), you have | 110 | time 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 | 111 | to get the write-lock at the very beginning. |
172 | primitive to create a "upgradeable" read-lock, but it hasn't been an issue | 112 | |
173 | yet. Tell me if you'd want one. | 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.) | ||
174 | 116 | ||
175 | ---- | 117 | ---- |
176 | 118 | ||
@@ -233,4 +175,46 @@ indeed), while write-locks need to protect themselves against interrupts. | |||
233 | 175 | ||
234 | Linus | 176 | Linus |
235 | 177 | ||
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: | ||
236 | 215 | ||
216 | struct foo bar { | ||
217 | .lock = __SPIN_LOCK_UNLOCKED(bar.lock); | ||
218 | }; | ||
219 | |||
220 | Declaration of static rw_locks undergo a similar transformation. | ||