]>
Commit | Line | Data |
---|---|---|
017f021c EC |
1 | SPIN_LOCK_UNLOCKED and RW_LOCK_UNLOCKED defeat lockdep state tracking and |
2 | are hence deprecated. | |
1da177e4 | 3 | |
017f021c EC |
4 | Please use DEFINE_SPINLOCK()/DEFINE_RWLOCK() or |
5 | __SPIN_LOCK_UNLOCKED()/__RW_LOCK_UNLOCKED() as appropriate for static | |
6 | initialization. | |
7 | ||
14dadf1d MF |
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 | ||
017f021c EC |
30 | Dynamic initialization, when necessary, may be performed as |
31 | demonstrated below. | |
1da177e4 LT |
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); | |
7ad4a5d5 | 39 | rwlock_init(&xxx_rw_lock); |
1da177e4 LT |
40 | ... |
41 | } | |
42 | ||
43 | module_init(xxx_init); | |
44 | ||
017f021c EC |
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. | |
1da177e4 LT |
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 | ||
71 | ||
72 | unsigned long flags; | |
73 | ||
74 | spin_lock_irqsave(&xxx_lock, flags); | |
75 | ... critical section here .. | |
76 | spin_unlock_irqrestore(&xxx_lock, flags); | |
77 | ||
78 | and 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 | |
80 | there 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 | |
85 | ||
86 | unsigned long flags; | |
87 | ||
88 | save_flags(flags); cli(); | |
89 | ... critical section ... | |
90 | restore_flags(flags); | |
91 | ||
92 | 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 | |
94 | architectures that allow doing the "save_flags + cli" in one go because I | |
95 | don't export that interface normally). | |
96 | ||
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. | |
103 | ||
104 | 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 | |
106 | lot more complex and even slower and is usually worth it only for | |
107 | sequences that you _know_ need to be split up: avoid it at all cost if you | |
108 | aren't sure). HOWEVER, it _does_ mean that if you have some code that does | |
109 | ||
110 | cli(); | |
111 | .. critical section .. | |
112 | sti(); | |
113 | ||
114 | and another sequence that does | |
115 | ||
116 | spin_lock_irqsave(flags); | |
117 | .. critical section .. | |
118 | spin_unlock_irqrestore(flags); | |
119 | ||
120 | 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 | |
122 | critical regions had better be critical for different things (ie they | |
123 | can't stomp on each other). | |
124 | ||
125 | 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 | |
127 | their actions, and if a driver uses spinlocks instead then you should | |
128 | think about issues like the above.. | |
129 | ||
130 | 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 | |
132 | before, because you have to make sure the spinlocks correctly protect the | |
133 | shared 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). | |
137 | ||
138 | ---- | |
139 | ||
140 | Lesson 2: reader-writer spinlocks. | |
141 | ||
142 | 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 | |
144 | (rw_lock) versions of the spinlocks are often nicer. They allow multiple | |
145 | 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 | |
147 | routines look the same as above: | |
148 | ||
149 | rwlock_t xxx_lock = RW_LOCK_UNLOCKED; | |
150 | ||
151 | ||
152 | unsigned long flags; | |
153 | ||
154 | read_lock_irqsave(&xxx_lock, flags); | |
155 | .. critical section that only reads the info ... | |
156 | read_unlock_irqrestore(&xxx_lock, flags); | |
157 | ||
158 | write_lock_irqsave(&xxx_lock, flags); | |
159 | .. read and write exclusive access to the info ... | |
160 | write_unlock_irqrestore(&xxx_lock, flags); | |
161 | ||
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. | |
168 | ||
169 | Note: 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 | |
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. | |
174 | ||
175 | ---- | |
176 | ||
177 | Lesson 3: spinlocks revisited. | |
178 | ||
179 | The single spin-lock primitives above are by no means the only ones. They | |
180 | are the most safe ones, and the ones that work under all circumstances, | |
181 | but partly _because_ they are safe they are also fairly slow. They are | |
182 | much faster than a generic global cli/sti pair, but slower than they'd | |
183 | need to be, because they do have to disable interrupts (which is just a | |
184 | single instruction on a x86, but it's an expensive one - and on other | |
185 | architectures it can be worse). | |
186 | ||
187 | If you have a case where you have to protect a data structure across | |
188 | several CPU's and you want to use spinlocks you can potentially use | |
189 | cheaper versions of the spinlocks. IFF you know that the spinlocks are | |
190 | never used in interrupt handlers, you can use the non-irq versions: | |
191 | ||
192 | spin_lock(&lock); | |
193 | ... | |
194 | spin_unlock(&lock); | |
195 | ||
196 | (and the equivalent read-write versions too, of course). The spinlock will | |
197 | guarantee the same kind of exclusive access, and it will be much faster. | |
198 | This is useful if you know that the data in question is only ever | |
199 | manipulated from a "process context", ie no interrupts involved. | |
200 | ||
201 | The reasons you mustn't use these versions if you have interrupts that | |
202 | play with the spinlock is that you can get deadlocks: | |
203 | ||
204 | spin_lock(&lock); | |
205 | ... | |
206 | <- interrupt comes in: | |
207 | spin_lock(&lock); | |
208 | ||
209 | where an interrupt tries to lock an already locked variable. This is ok if | |
210 | the other interrupt happens on another CPU, but it is _not_ ok if the | |
211 | interrupt happens on the same CPU that already holds the lock, because the | |
212 | lock will obviously never be released (because the interrupt is waiting | |
213 | for the lock, and the lock-holder is interrupted by the interrupt and will | |
214 | not continue until the interrupt has been processed). | |
215 | ||
216 | (This is also the reason why the irq-versions of the spinlocks only need | |
217 | to disable the _local_ interrupts - it's ok to use spinlocks in interrupts | |
218 | on other CPU's, because an interrupt on another CPU doesn't interrupt the | |
219 | CPU that holds the lock, so the lock-holder can continue and eventually | |
220 | releases the lock). | |
221 | ||
222 | Note that you can be clever with read-write locks and interrupts. For | |
223 | example, if you know that the interrupt only ever gets a read-lock, then | |
224 | you can use a non-irq version of read locks everywhere - because they | |
225 | don't block on each other (and thus there is no dead-lock wrt interrupts. | |
226 | But when you do the write-lock, you have to use the irq-safe version. | |
227 | ||
228 | For an example of being clever with rw-locks, see the "waitqueue_lock" | |
229 | handling in kernel/sched.c - nothing ever _changes_ a wait-queue from | |
230 | within an interrupt, they only read the queue in order to know whom to | |
231 | wake up. So read-locks are safe (which is good: they are very common | |
232 | indeed), while write-locks need to protect themselves against interrupts. | |
233 | ||
234 | Linus | |
235 | ||
236 |