]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - share/man/man9/locking.9
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / share / man / man9 / locking.9
1 .\" Copyright (c) 2007 Julian Elischer  (julian -  freebsd org )
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\"
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 .\" SUCH DAMAGE.
24 .\"
25 .\" $FreeBSD$
26 .\"
27 .Dd June 30, 2013
28 .Dt LOCKING 9
29 .Os
30 .Sh NAME
31 .Nm locking
32 .Nd kernel synchronization primitives
33 .Sh DESCRIPTION
34 The
35 .Em FreeBSD
36 kernel is written to run across multiple CPUs and as such provides
37 several different synchronization primitives to allow developers
38 to safely access and manipulate many data types.
39 .Ss Mutexes
40 Mutexes (also called "blocking mutexes") are the most commonly used
41 synchronization primitive in the kernel.
42 A thread acquires (locks) a mutex before accessing data shared with other
43 threads (including interrupt threads), and releases (unlocks) it afterwards.
44 If the mutex cannot be acquired, the thread requesting it will wait.
45 Mutexes are adaptive by default, meaning that
46 if the owner of a contended mutex is currently running on another CPU,
47 then a thread attempting to acquire the mutex will spin rather than yielding
48 the processor.
49 Mutexes fully support priority propagation.
50 .Pp
51 See
52 .Xr mutex 9
53 for details.
54 .Ss Spin Mutexes
55 Spin mutexes are a variation of basic mutexes; the main difference between
56 the two is that spin mutexes never block.
57 Instead, they spin while waiting for the lock to be released.
58 To avoid deadlock, a thread that holds a spin mutex must never yield its CPU.
59 Unlike ordinary mutexes, spin mutexes disable interrupts when acquired.
60 Since disabling interrupts can be expensive, they are generally slower to
61 acquire and release.
62 Spin mutexes should be used only when absolutely necessary,
63 e.g. to protect data shared
64 with interrupt filter code (see
65 .Xr bus_setup_intr 9
66 for details),
67 or for scheduler internals.
68 .Ss Mutex Pools
69 With most synchronization primitives, such as mutexes, the programmer must
70 provide memory to hold the primitive.
71 For example, a mutex may be embedded inside the structure it protects.
72 Mutex pools provide a preallocated set of mutexes to avoid this
73 requirement.
74 Note that mutexes from a pool may only be used as leaf locks.
75 .Pp
76 See
77 .Xr mtx_pool 9
78 for details.
79 .Ss Reader/Writer Locks
80 Reader/writer locks allow shared access to protected data by multiple threads
81 or exclusive access by a single thread.
82 The threads with shared access are known as
83 .Em readers
84 since they should only read the protected data.
85 A thread with exclusive access is known as a
86 .Em writer
87 since it may modify protected data.
88 .Pp
89 Reader/writer locks can be treated as mutexes (see above and
90 .Xr mutex 9 )
91 with shared/exclusive semantics.
92 Reader/writer locks support priority propagation like mutexes,
93 but priority is propagated only to an exclusive holder.
94 This limitation comes from the fact that shared owners
95 are anonymous.
96 .Pp
97 See
98 .Xr rwlock 9
99 for details.
100 .Ss Read-Mostly Locks
101 Read-mostly locks are similar to
102 .Em reader/writer
103 locks but optimized for very infrequent write locking.
104 .Em Read-mostly
105 locks implement full priority propagation by tracking shared owners
106 using a caller-supplied
107 .Em tracker
108 data structure.
109 .Pp
110 See
111 .Xr rmlock 9
112 for details.
113 .Ss Sleepable Read-Mostly Locks
114 Sleepable read-mostly locks are a variation on read-mostly locks.
115 Threads holding an exclusive lock may sleep,
116 but threads holding a shared lock may not.
117 Priority is propagated to shared owners but not to exclusive owners.
118 .Ss Shared/exclusive locks
119 Shared/exclusive locks are similar to reader/writer locks; the main difference
120 between them is that shared/exclusive locks may be held during unbounded sleep.
121 Acquiring a contested shared/exclusive lock can perform an unbounded sleep.
122 These locks do not support priority propagation.
123 .Pp
124 See
125 .Xr sx 9
126 for details.
127 .Ss Lockmanager locks
128 Lockmanager locks are sleepable shared/exclusive locks used mostly in
129 .Xr VFS 9
130 .Po
131 as a
132 .Xr vnode 9
133 lock
134 .Pc
135 and in the buffer cache
136 .Po
137 .Xr BUF_LOCK 9
138 .Pc .
139 They have features other lock types do not have such as sleep
140 timeouts, blocking upgrades,
141 writer starvation avoidance, draining, and an interlock mutex,
142 but this makes them complicated both to use and to implement;
143 for this reason, they should be avoided.
144 .Pp
145 See
146 .Xr lock 9
147 for details.
148 .Ss Counting semaphores
149 Counting semaphores provide a mechanism for synchronizing access
150 to a pool of resources.
151 Unlike mutexes, semaphores do not have the concept of an owner,
152 so they can be useful in situations where one thread needs
153 to acquire a resource, and another thread needs to release it.
154 They are largely deprecated.
155 .Pp
156 See
157 .Xr sema 9
158 for details.
159 .Ss Condition variables
160 Condition variables are used in conjunction with locks to wait for
161 a condition to become true.
162 A thread must hold the associated lock before calling one of the
163 .Fn cv_wait ,
164 functions.
165 When a thread waits on a condition, the lock
166 is atomically released before the thread yields the processor
167 and reacquired before the function call returns.
168 Condition variables may be used with blocking mutexes,
169 reader/writer locks, read-mostly locks, and shared/exclusive locks.
170 .Pp
171 See
172 .Xr condvar 9
173 for details.
174 .Ss Sleep/Wakeup
175 The functions
176 .Fn tsleep ,
177 .Fn msleep ,
178 .Fn msleep_spin ,
179 .Fn pause ,
180 .Fn wakeup ,
181 and
182 .Fn wakeup_one
183 also handle event-based thread blocking.
184 Unlike condition variables,
185 arbitrary addresses may be used as wait channels and a dedicated
186 structure does not need to be allocated.
187 However, care must be taken to ensure that wait channel addresses are
188 unique to an event.
189 If a thread must wait for an external event, it is put to sleep by
190 .Fn tsleep ,
191 .Fn msleep ,
192 .Fn msleep_spin ,
193 or
194 .Fn pause .
195 Threads may also wait using one of the locking primitive sleep routines
196 .Xr mtx_sleep 9 ,
197 .Xr rw_sleep 9 ,
198 or
199 .Xr sx_sleep 9 .
200 .Pp
201 The parameter
202 .Fa chan
203 is an arbitrary address that uniquely identifies the event on which
204 the thread is being put to sleep.
205 All threads sleeping on a single
206 .Fa chan
207 are woken up later by
208 .Fn wakeup
209 .Pq often called from inside an interrupt routine
210 to indicate that the
211 event the thread was blocking on has occurred.
212 .Pp
213 Several of the sleep functions including
214 .Fn msleep ,
215 .Fn msleep_spin ,
216 and the locking primitive sleep routines specify an additional lock
217 parameter.
218 The lock will be released before sleeping and reacquired
219 before the sleep routine returns.
220 If
221 .Fa priority
222 includes the
223 .Dv PDROP
224 flag, then the lock will not be reacquired before returning.
225 The lock is used to ensure that a condition can be checked atomically,
226 and that the current thread can be suspended without missing a
227 change to the condition or an associated wakeup.
228 In addition, all of the sleep routines will fully drop the
229 .Va Giant
230 mutex
231 .Pq even if recursed
232 while the thread is suspended and will reacquire the
233 .Va Giant
234 mutex
235 .Pq restoring any recursion
236 before the function returns.
237 .Pp
238 The
239 .Fn pause
240 function is a special sleep function that waits for a specified
241 amount of time to pass before the thread resumes execution.
242 This sleep cannot be terminated early by either an explicit
243 .Fn wakeup
244 or a signal.
245 .Pp
246 See
247 .Xr sleep 9
248 for details.
249 .Ss Giant
250 Giant is a special mutex used to protect data structures that do not
251 yet have their own locks.
252 Since it provides semantics akin to the old
253 .Xr spl 9
254 interface,
255 Giant has special characteristics:
256 .Bl -enum
257 .It
258 It is recursive.
259 .It
260 Drivers can request that Giant be locked around them
261 by not marking themselves MPSAFE.
262 Note that infrastructure to do this is slowly going away as non-MPSAFE
263 drivers either became properly locked or disappear.
264 .It
265 Giant must be locked before other non-sleepable locks.
266 .It
267 Giant is dropped during unbounded sleeps and reacquired after wakeup.
268 .It
269 There are places in the kernel that drop Giant and pick it back up
270 again.
271 Sleep locks will do this before sleeping.
272 Parts of the network or VM code may do this as well.
273 This means that you cannot count on Giant keeping other code from
274 running if your code sleeps, even if you want it to.
275 .El
276 .Sh INTERACTIONS
277 The primitives can interact and have a number of rules regarding how
278 they can and can not be combined.
279 Many of these rules are checked by
280 .Xr witness 4 .
281 .Ss Bounded vs. Unbounded Sleep
282 In a bounded sleep
283 .Po also referred to as
284 .Dq blocking
285 .Pc
286 the only resource needed to resume execution of a thread
287 is CPU time for the owner of a lock that the thread is waiting to acquire.
288 In an unbounded sleep
289 .Po
290 often referred to as simply
291 .Dq sleeping
292 .Pc
293 a thread waits for an external event or for a condition
294 to become true.
295 In particular,
296 a dependency chain of threads in bounded sleeps should always make forward
297 progress,
298 since there is always CPU time available.
299 This requires that no thread in a bounded sleep is waiting for a lock held
300 by a thread in an unbounded sleep.
301 To avoid priority inversions,
302 a thread in a bounded sleep lends its priority to the owner of the lock
303 that it is waiting for.
304 .Pp
305 The following primitives perform bounded sleeps:
306 mutexes, reader/writer locks and read-mostly locks.
307 .Pp
308 The following primitives perform unbounded sleeps:
309 sleepable read-mostly locks, shared/exclusive locks, lockmanager locks,
310 counting semaphores, condition variables, and sleep/wakeup.
311 .Ss General Principles
312 .Bl -bullet
313 .It
314 It is an error to do any operation that could result in yielding the processor
315 while holding a spin mutex.
316 .It
317 It is an error to do any operation that could result in unbounded sleep
318 while holding any primitive from the 'bounded sleep' group.
319 For example, it is an error to try to acquire a shared/exclusive lock while
320 holding a mutex, or to try to allocate memory with M_WAITOK while holding a
321 reader/writer lock.
322 .Pp
323 Note that the lock passed to one of the
324 .Fn sleep
325 or
326 .Fn cv_wait
327 functions is dropped before the thread enters the unbounded sleep and does
328 not violate this rule.
329 .It
330 It is an error to do any operation that could result in yielding of
331 the processor when running inside an interrupt filter.
332 .It
333 It is an error to do any operation that could result in unbounded sleep when
334 running inside an interrupt thread.
335 .El
336 .Ss Interaction table
337 The following table shows what you can and can not do while holding
338 one of the locking primitives discussed.  Note that
339 .Dq sleep
340 includes
341 .Fn sema_wait ,
342 .Fn sema_timedwait ,
343 any of the
344 .Fn cv_wait
345 functions,
346 and any of the
347 .Fn sleep
348 functions.
349 .Bl -column ".Ic xxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXXXX" ".Xr XXXXXX" -offset 3n
350 .It Em "       You want:" Ta spin mtx Ta mutex/rw Ta rmlock Ta sleep rm Ta sx/lk Ta sleep
351 .It Em "You have:     " Ta -------- Ta -------- Ta ------ Ta -------- Ta ------ Ta ------
352 .It spin mtx  Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no-1
353 .It mutex/rw  Ta \&ok Ta \&ok Ta \&ok Ta \&no Ta \&no Ta \&no-1
354 .It rmlock    Ta \&ok Ta \&ok Ta \&ok Ta \&no Ta \&no Ta \&no-1
355 .It sleep rm  Ta \&ok Ta \&ok Ta \&ok Ta \&ok-2 Ta \&ok-2 Ta \&ok-2/3
356 .It sx        Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok-3
357 .It lockmgr   Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok
358 .El
359 .Pp
360 .Em *1
361 There are calls that atomically release this primitive when going to sleep
362 and reacquire it on wakeup
363 .Po
364 .Fn mtx_sleep ,
365 .Fn rw_sleep ,
366 .Fn msleep_spin ,
367 etc.
368 .Pc .
369 .Pp
370 .Em *2
371 These cases are only allowed while holding a write lock on a sleepable
372 read-mostly lock.
373 .Pp
374 .Em *3
375 Though one can sleep while holding this lock,
376 one can also use a
377 .Fn sleep
378 function to atomically release this primitive when going to sleep and
379 reacquire it on wakeup.
380 .Pp
381 Note that non-blocking try operations on locks are always permitted.
382 .Ss Context mode table
383 The next table shows what can be used in different contexts.
384 At this time this is a rather easy to remember table.
385 .Bl -column ".Ic Xxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXXXX" ".Xr XXXXXX" -offset 3n
386 .It Em "Context:"  Ta spin mtx Ta mutex/rw Ta rmlock Ta sleep rm Ta sx/lk Ta sleep
387 .It interrupt filter:  Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no
388 .It interrupt thread:  Ta \&ok Ta \&ok Ta \&ok Ta \&no Ta \&no Ta \&no
389 .It callout:    Ta \&ok Ta \&ok Ta \&ok Ta \&no Ta \&no Ta \&no
390 .It system call:    Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok
391 .El
392 .Sh SEE ALSO
393 .Xr witness 4 ,
394 .Xr condvar 9 ,
395 .Xr lock 9 ,
396 .Xr mtx_pool 9 ,
397 .Xr mutex 9 ,
398 .Xr rmlock 9 ,
399 .Xr rwlock 9 ,
400 .Xr sema 9 ,
401 .Xr sleep 9 ,
402 .Xr sx 9 ,
403 .Xr BUS_SETUP_INTR 9 ,
404 .Xr LOCK_PROFILING 9
405 .Sh HISTORY
406 These
407 functions appeared in
408 .Bsx 4.1
409 through
410 .Fx 7.0 .
411 .Sh BUGS
412 There are too many locking primitives to choose from.