xref: /freebsd/share/man/man9/mutex.9 (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
1.\"
2.\" Copyright (c) 1998 Berkeley Software Design, Inc. 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.\" 3. Berkeley Software Design Inc's name may not be used to endorse or
13.\"    promote products derived from this software without specific prior
14.\"    written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"	from BSDI $Id: mutex.4,v 1.1.2.3 1998/04/27 22:53:13 ewv Exp $
29.\" $FreeBSD$
30.\"
31.Dd July 18, 2016
32.Dt MUTEX 9
33.Os
34.Sh NAME
35.Nm mutex ,
36.Nm mtx_init ,
37.Nm mtx_destroy ,
38.Nm mtx_lock ,
39.Nm mtx_lock_spin ,
40.Nm mtx_lock_flags ,
41.Nm mtx_lock_spin_flags ,
42.Nm mtx_trylock ,
43.Nm mtx_trylock_flags ,
44.Nm mtx_trylock_spin ,
45.Nm mtx_trylock_spin_flags ,
46.Nm mtx_unlock ,
47.Nm mtx_unlock_spin ,
48.Nm mtx_unlock_flags ,
49.Nm mtx_unlock_spin_flags ,
50.Nm mtx_sleep ,
51.Nm mtx_initialized ,
52.Nm mtx_owned ,
53.Nm mtx_recursed ,
54.Nm mtx_assert ,
55.Nm MTX_SYSINIT
56.Nd kernel synchronization primitives
57.Sh SYNOPSIS
58.In sys/param.h
59.In sys/lock.h
60.In sys/mutex.h
61.Ft void
62.Fn mtx_init "struct mtx *mutex" "const char *name" "const char *type" "int opts"
63.Ft void
64.Fn mtx_destroy "struct mtx *mutex"
65.Ft void
66.Fn mtx_lock "struct mtx *mutex"
67.Ft void
68.Fn mtx_lock_spin "struct mtx *mutex"
69.Ft void
70.Fn mtx_lock_flags "struct mtx *mutex" "int flags"
71.Ft void
72.Fn mtx_lock_spin_flags "struct mtx *mutex" "int flags"
73.Ft int
74.Fn mtx_trylock "struct mtx *mutex"
75.Ft int
76.Fn mtx_trylock_flags "struct mtx *mutex" "int flags"
77.Ft void
78.Fn mtx_trylock_spin "struct mtx *mutex"
79.Ft int
80.Fn mtx_trylock_spin_flags "struct mtx *mutex" "int flags"
81.Ft void
82.Fn mtx_unlock "struct mtx *mutex"
83.Ft void
84.Fn mtx_unlock_spin "struct mtx *mutex"
85.Ft void
86.Fn mtx_unlock_flags "struct mtx *mutex" "int flags"
87.Ft void
88.Fn mtx_unlock_spin_flags "struct mtx *mutex" "int flags"
89.Ft int
90.Fn mtx_sleep "void *chan" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo"
91.Ft int
92.Fn mtx_initialized "const struct mtx *mutex"
93.Ft int
94.Fn mtx_owned "const struct mtx *mutex"
95.Ft int
96.Fn mtx_recursed "const struct mtx *mutex"
97.Pp
98.Cd "options INVARIANTS"
99.Cd "options INVARIANT_SUPPORT"
100.Ft void
101.Fn mtx_assert "const struct mtx *mutex" "int what"
102.In sys/kernel.h
103.Fn MTX_SYSINIT "name" "struct mtx *mtx" "const char *description" "int opts"
104.Sh DESCRIPTION
105Mutexes are the most basic and primary method of thread synchronization.
106The major design considerations for mutexes are:
107.Bl -enum
108.It
109Acquiring and releasing uncontested mutexes should be as cheap
110as possible.
111.It
112They must have the information and storage space to support
113priority propagation.
114.It
115A thread must be able to recursively acquire a mutex,
116provided that the mutex is initialized to support recursion.
117.El
118.Pp
119There are currently two flavors of mutexes, those that context switch
120when they block and those that do not.
121.Pp
122By default,
123.Dv MTX_DEF
124mutexes will context switch when they are already held.
125As an optimization,
126they may spin for some amount
127of time before context switching.
128It is important to remember that since a thread may be preempted at any time,
129the possible context switch introduced by acquiring a mutex is guaranteed
130to not break anything that is not already broken.
131.Pp
132Mutexes which do not context switch are
133.Dv MTX_SPIN
134mutexes.
135These should only be used to protect data shared with primary interrupt
136code.
137This includes interrupt filters and low level scheduling code.
138In all architectures both acquiring and releasing of a
139uncontested spin mutex is more expensive than the same operation
140on a non-spin mutex.
141In order to protect an interrupt service routine from blocking
142against itself all interrupts are either blocked or deferred on a processor
143while holding a spin lock.
144It is permissible to hold multiple spin mutexes.
145.Pp
146Once a spin mutex has been acquired it is not permissible to acquire a
147blocking mutex.
148.Pp
149The storage needed to implement a mutex is provided by a
150.Vt struct mtx .
151In general this should be treated as an opaque object and
152referenced only with the mutex primitives.
153.Pp
154The
155.Fn mtx_init
156function must be used to initialize a mutex
157before it can be passed to any of the other mutex functions.
158The
159.Fa name
160option is used to identify the lock in debugging output etc.
161The
162.Fa type
163option is used by the witness code to classify a mutex when doing checks
164of lock ordering.
165If
166.Fa type
167is
168.Dv NULL ,
169.Fa name
170is used in its place.
171The pointer passed in as
172.Fa name
173and
174.Fa type
175is saved rather than the data it points to.
176The data pointed to must remain stable
177until the mutex is destroyed.
178The
179.Fa opts
180argument is used to set the type of mutex.
181It may contain either
182.Dv MTX_DEF
183or
184.Dv MTX_SPIN
185but not both.
186If the kernel has been compiled with
187.Cd "option INVARIANTS" ,
188.Fn mtx_init
189will assert that the
190.Fa mutex
191has not been initialized multiple times without intervening calls to
192.Fn mtx_destroy
193unless the
194.Dv MTX_NEW
195option is specified.
196See below for additional initialization options.
197.Pp
198The
199.Fn mtx_lock
200function acquires a
201.Dv MTX_DEF
202mutual exclusion lock
203on behalf of the currently running kernel thread.
204If another kernel thread is holding the mutex,
205the caller will be disconnected from the CPU
206until the mutex is available
207(i.e., it will block).
208.Pp
209The
210.Fn mtx_lock_spin
211function acquires a
212.Dv MTX_SPIN
213mutual exclusion lock
214on behalf of the currently running kernel thread.
215If another kernel thread is holding the mutex,
216the caller will spin until the mutex becomes available.
217Interrupts are disabled during the spin and remain disabled
218following the acquiring of the lock.
219.Pp
220It is possible for the same thread to recursively acquire a mutex
221with no ill effects, provided that the
222.Dv MTX_RECURSE
223bit was passed to
224.Fn mtx_init
225during the initialization of the mutex.
226.Pp
227The
228.Fn mtx_lock_flags
229and
230.Fn mtx_lock_spin_flags
231functions acquire a
232.Dv MTX_DEF
233or
234.Dv MTX_SPIN
235lock, respectively, and also accept a
236.Fa flags
237argument.
238In both cases, the only flags presently available for lock acquires are
239.Dv MTX_QUIET
240and
241.Dv MTX_RECURSE .
242If the
243.Dv MTX_QUIET
244bit is turned on in the
245.Fa flags
246argument, then if
247.Dv KTR_LOCK
248tracing is being done,
249it will be silenced during the lock acquire.
250If the
251.Dv MTX_RECURSE
252bit is turned on in the
253.Fa flags
254argument, then the mutex can be acquired recursively.
255.Pp
256The
257.Fn mtx_trylock
258and
259.Fn mtx_trylock_spin
260functions attempt to acquire a
261.Dv MTX_DEF
262or
263.Dv MTX_SPIN
264mutex, respectively, pointed to by
265.Fa mutex .
266If the mutex cannot be immediately acquired, the functions will return 0,
267otherwise the mutex will be acquired and a non-zero value will be returned.
268.Pp
269The
270.Fn mtx_trylock_flags
271and
272.Fn mtx_trylock_spin_flags
273functions have the same behavior as
274.Fn mtx_trylock
275and
276.Fn mtx_trylock_spin
277respectively, but should be used when the caller desires to pass in a
278.Fa flags
279value.
280Presently, the only valid value in the
281.Fn mtx_trylock
282and
283.Fn mtx_trylock_spin
284cases is
285.Dv MTX_QUIET ,
286and its effects are identical to those described for
287.Fn mtx_lock
288above.
289.Pp
290The
291.Fn mtx_unlock
292function releases a
293.Dv MTX_DEF
294mutual exclusion lock.
295The current thread may be preempted if a higher priority thread is waiting
296for the mutex.
297.Pp
298The
299.Fn mtx_unlock_spin
300function releases a
301.Dv MTX_SPIN
302mutual exclusion lock.
303.Pp
304The
305.Fn mtx_unlock_flags
306and
307.Fn mtx_unlock_spin_flags
308functions behave in exactly the same way as do the standard mutex
309unlock routines above, while also allowing a
310.Fa flags
311argument which may specify
312.Dv MTX_QUIET .
313The behavior of
314.Dv MTX_QUIET
315is identical to its behavior in the mutex lock routines.
316.Pp
317The
318.Fn mtx_destroy
319function is used to destroy
320.Fa mutex
321so the data associated with it may be freed
322or otherwise overwritten.
323Any mutex which is destroyed
324must previously have been initialized with
325.Fn mtx_init .
326It is permissible to have a single hold count
327on a mutex when it is destroyed.
328It is not permissible to hold the mutex recursively,
329or have another thread blocked on the mutex
330when it is destroyed.
331.Pp
332The
333.Fn mtx_sleep
334function is used to atomically release
335.Fa mtx
336while waiting for an event.
337For more details on the parameters to this function,
338see
339.Xr sleep 9 .
340.Pp
341The
342.Fn mtx_initialized
343function returns non-zero if
344.Fa mutex
345has been initialized and zero otherwise.
346.Pp
347The
348.Fn mtx_owned
349function returns non-zero
350if the current thread holds
351.Fa mutex .
352If the current thread does not hold
353.Fa mutex
354zero is returned.
355.Pp
356The
357.Fn mtx_recursed
358function returns non-zero if the
359.Fa mutex
360is recursed.
361This check should only be made if the running thread already owns
362.Fa mutex .
363.Pp
364The
365.Fn mtx_assert
366function allows assertions specified in
367.Fa what
368to be made about
369.Fa mutex .
370If the assertions are not true and the kernel is compiled with
371.Cd "options INVARIANTS"
372and
373.Cd "options INVARIANT_SUPPORT" ,
374the kernel will panic.
375Currently the following assertions are supported:
376.Bl -tag -width MA_NOTRECURSED
377.It Dv MA_OWNED
378Assert that the current thread
379holds the mutex
380pointed to by the first argument.
381.It Dv MA_NOTOWNED
382Assert that the current thread
383does not hold the mutex
384pointed to by the first argument.
385.It Dv MA_RECURSED
386Assert that the current thread has recursed on the mutex
387pointed to by the first argument.
388This assertion is only valid in conjunction with
389.Dv MA_OWNED .
390.It Dv MA_NOTRECURSED
391Assert that the current thread has not recursed on the mutex
392pointed to by the first argument.
393This assertion is only valid in conjunction with
394.Dv MA_OWNED .
395.El
396.Pp
397The
398.Fn MTX_SYSINIT
399macro is used to generate a call to the
400.Fn mtx_sysinit
401routine at system startup in order to initialize a given mutex lock.
402The parameters are the same as
403.Fn mtx_init
404but with an additional argument,
405.Fa name ,
406that is used in generating unique variable names for the related structures associated with the lock and the sysinit routine.
407.Ss The Default Mutex Type
408Most kernel code should use the default lock type,
409.Dv MTX_DEF .
410The default lock type will allow the thread
411to be disconnected from the CPU
412if the lock is already held by another thread.
413The implementation
414may treat the lock as a short term spin lock
415under some circumstances.
416However, it is always safe to use these forms of locks
417in an interrupt thread
418without fear of deadlock
419against an interrupted thread on the same CPU.
420.Ss The Spin Mutex Type
421A
422.Dv MTX_SPIN
423mutex will not relinquish the CPU
424when it cannot immediately get the requested lock,
425but will loop, waiting for the mutex to be released by another CPU.
426This could result in deadlock
427if another thread interrupted the thread which held a mutex
428and then tried to acquire the mutex.
429For this reason spin locks disable all interrupts on the local CPU.
430.Pp
431Spin locks are fairly specialized locks
432that are intended to be held for very short periods of time.
433Their primary purpose is to protect portions of the code
434that implement other synchronization primitives such as default mutexes,
435thread scheduling, and interrupt threads.
436.Ss Initialization Options
437The options passed in the
438.Fa opts
439argument of
440.Fn mtx_init
441specify the mutex type.
442One of the
443.Dv MTX_DEF
444or
445.Dv MTX_SPIN
446options is required and only one of those two options may be specified.
447The possibilities are:
448.Bl -tag -width MTX_NOWITNESS
449.It Dv MTX_DEF
450Default mutexes
451will always allow the current thread to be suspended
452to avoid deadlock conditions against interrupt threads.
453The implementation of this lock type
454may spin for a while before suspending the current thread.
455.It Dv MTX_SPIN
456Spin mutexes
457will never relinquish the CPU.
458All interrupts are disabled on the local CPU
459while any spin lock is held.
460.It Dv MTX_RECURSE
461Specifies that the initialized mutex is allowed to recurse.
462This bit must be present if the mutex is permitted to recurse.
463.Pp
464Note that neither
465.Fn mtx_trylock
466nor
467.Fn mtx_trylock_spin
468support recursion;
469that is, attempting to acquire an already-owned mutex fails.
470.It Dv MTX_QUIET
471Do not log any mutex operations for this lock.
472.It Dv MTX_NOWITNESS
473Instruct
474.Xr witness 4
475to ignore this lock.
476.It Dv MTX_DUPOK
477Witness should not log messages about duplicate locks being acquired.
478.It Dv MTX_NOPROFILE
479Do not profile this lock.
480.It Dv MTX_NEW
481Do not check for double-init.
482.El
483.Ss Lock and Unlock Flags
484The flags passed to the
485.Fn mtx_lock_flags ,
486.Fn mtx_lock_spin_flags ,
487.Fn mtx_unlock_flags ,
488and
489.Fn mtx_unlock_spin_flags
490functions provide some basic options to the caller,
491and are often used only under special circumstances to modify lock or
492unlock behavior.
493Standard locking and unlocking should be performed with the
494.Fn mtx_lock ,
495.Fn mtx_lock_spin ,
496.Fn mtx_unlock ,
497and
498.Fn mtx_unlock_spin
499functions.
500Only if a flag is required should the corresponding
501flags-accepting routines be used.
502.Pp
503Options that modify mutex behavior:
504.Bl -tag -width MTX_QUIET
505.It Dv MTX_QUIET
506This option is used to quiet logging messages during individual mutex
507operations.
508This can be used to trim superfluous logging messages for debugging purposes.
509.El
510.Ss Giant
511If
512.Va Giant
513must be acquired, it must be acquired prior to acquiring
514other mutexes.
515Put another way: it is impossible to acquire
516.Va Giant
517non-recursively while
518holding another mutex.
519It is possible to acquire other mutexes while holding
520.Va Giant ,
521and it is possible to acquire
522.Va Giant
523recursively while holding other mutexes.
524.Ss Sleeping
525Sleeping while holding a mutex (except for
526.Va Giant )
527is never safe
528and should be avoided.
529There are numerous assertions which will fail if this is attempted.
530.Ss Functions Which Access Memory in Userspace
531No mutexes should be held (except for
532.Va Giant )
533across functions which
534access memory in userspace, such as
535.Xr copyin 9 ,
536.Xr copyout 9 ,
537.Xr uiomove 9 ,
538.Xr fuword 9 ,
539etc.
540No locks are needed when calling these functions.
541.Sh SEE ALSO
542.Xr condvar 9 ,
543.Xr LOCK_PROFILING 9 ,
544.Xr locking 9 ,
545.Xr mtx_pool 9 ,
546.Xr panic 9 ,
547.Xr rwlock 9 ,
548.Xr sema 9 ,
549.Xr sleep 9 ,
550.Xr sx 9
551.Sh HISTORY
552These
553functions appeared in
554.Bsx 4.1
555and
556.Fx 5.0 .
557The
558.Fn mtx_trylock_spin
559function was added in
560.Fx 12.0 .
561