xref: /freebsd/share/man/man9/lock.9 (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1.\"
2.\" Copyright (C) 2002 Chad David <davidc@acns.ab.ca>. 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(s), this list of conditions and the following disclaimer as
9.\"    the first lines of this file unmodified other than the possible
10.\"    addition of one or more copyright notices.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice(s), this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
16.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18.\" DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
19.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25.\" DAMAGE.
26.\"
27.\" $FreeBSD$
28.\"
29.Dd November 17, 2017
30.Dt LOCK 9
31.Os
32.Sh NAME
33.Nm lockinit ,
34.Nm lockdestroy ,
35.Nm lockmgr ,
36.Nm lockmgr_args ,
37.Nm lockmgr_args_rw ,
38.Nm lockmgr_disown ,
39.Nm lockmgr_printinfo ,
40.Nm lockmgr_recursed ,
41.Nm lockmgr_rw ,
42.Nm lockstatus ,
43.Nm lockmgr_assert
44.Nd "lockmgr family of functions"
45.Sh SYNOPSIS
46.In sys/types.h
47.In sys/lock.h
48.In sys/lockmgr.h
49.Ft void
50.Fn lockinit "struct lock *lkp" "int prio" "const char *wmesg" "int timo" "int flags"
51.Ft void
52.Fn lockdestroy "struct lock *lkp"
53.Ft int
54.Fn lockmgr "struct lock *lkp" "u_int flags" "struct mtx *ilk"
55.Ft int
56.Fn lockmgr_args "struct lock *lkp" "u_int flags" "struct mtx *ilk" "const char *wmesg" "int prio" "int timo"
57.Ft int
58.Fn lockmgr_args_rw "struct lock *lkp" "u_int flags" "struct rwlock *ilk" "const char *wmesg" "int prio" "int timo"
59.Ft void
60.Fn lockmgr_disown "struct lock *lkp"
61.Ft void
62.Fn lockmgr_printinfo "const struct lock *lkp"
63.Ft int
64.Fn lockmgr_recursed "const struct lock *lkp"
65.Ft int
66.Fn lockmgr_rw "struct lock *lkp" "u_int flags" "struct rwlock *ilk"
67.Ft int
68.Fn lockstatus "const struct lock *lkp"
69.Pp
70.Cd "options INVARIANTS"
71.Cd "options INVARIANT_SUPPORT"
72.Ft void
73.Fn lockmgr_assert "const struct lock *lkp" "int what"
74.Sh DESCRIPTION
75The
76.Fn lockinit
77function is used to initialize a lock.
78It must be called before any operation can be performed on a lock.
79Its arguments are:
80.Bl -tag -width ".Fa wmesg"
81.It Fa lkp
82A pointer to the lock to initialize.
83.It Fa prio
84The priority passed to
85.Xr sleep 9 .
86.It Fa wmesg
87The lock message.
88This is used for both debugging output and
89.Xr sleep 9 .
90.It Fa timo
91The timeout value passed to
92.Xr sleep 9 .
93.It Fa flags
94The flags the lock is to be initialized with:
95.Bl -tag -width ".Dv LK_CANRECURSE"
96.It Dv LK_CANRECURSE
97Allow recursive exclusive locks.
98.It Dv LK_NOPROFILE
99Disable lock profiling for this lock.
100.It Dv LK_NOSHARE
101Allow exclusive locks only.
102.It Dv LK_NOWITNESS
103Instruct
104.Xr witness 4
105to ignore this lock.
106.It Dv LK_NODUP
107.Xr witness 4
108should log messages about duplicate locks being acquired.
109.It Dv LK_QUIET
110Disable
111.Xr ktr 4
112logging for this lock.
113.It Dv LK_TIMELOCK
114Use
115.Fa timo
116during a sleep; otherwise, 0 is used.
117.El
118.El
119.Pp
120The
121.Fn lockdestroy
122function is used to destroy a lock, and while it is called in a number of
123places in the kernel, it currently does nothing.
124.Pp
125The
126.Fn lockmgr
127and
128.Fn lockmgr_rw
129functions handle general locking functionality within the kernel, including
130support for shared and exclusive locks, and recursion.
131.Fn lockmgr
132and
133.Fn lockmgr_rw
134are also able to upgrade and downgrade locks.
135.Pp
136Their arguments are:
137.Bl -tag -width ".Fa flags"
138.It Fa lkp
139A pointer to the lock to manipulate.
140.It Fa flags
141Flags indicating what action is to be taken.
142.Bl -tag -width ".Dv LK_NODDLKTREAT"
143.It Dv LK_SHARED
144Acquire a shared lock.
145If an exclusive lock is currently held,
146.Dv EDEADLK
147will be returned.
148.It Dv LK_EXCLUSIVE
149Acquire an exclusive lock.
150If an exclusive lock is already held, and
151.Dv LK_CANRECURSE
152is not set, the system will
153.Xr panic 9 .
154.It Dv LK_DOWNGRADE
155Downgrade exclusive lock to a shared lock.
156Downgrading a shared lock is not permitted.
157If an exclusive lock has been recursed, the system will
158.Xr panic 9 .
159.It Dv LK_UPGRADE
160Upgrade a shared lock to an exclusive lock.
161If this call fails, the shared lock is lost, even if the
162.Dv LK_NOWAIT
163flag is specified.
164During the upgrade, the shared lock could
165be temporarily dropped.
166Attempts to upgrade an exclusive lock will cause a
167.Xr panic 9 .
168.It Dv LK_TRYUPGRADE
169Try to upgrade a shared lock to an exclusive lock.
170The failure to upgrade does not result in the dropping
171of the shared lock ownership.
172.It Dv LK_RELEASE
173Release the lock.
174Releasing a lock that is not held can cause a
175.Xr panic 9 .
176.It Dv LK_DRAIN
177Wait for all activity on the lock to end, then mark it decommissioned.
178This is used before freeing a lock that is part of a piece of memory that is
179about to be freed.
180(As documented in
181.In sys/lockmgr.h . )
182.It Dv LK_SLEEPFAIL
183Fail if operation has slept.
184.It Dv LK_NOWAIT
185Do not allow the call to sleep.
186This can be used to test the lock.
187.It Dv LK_NOWITNESS
188Skip the
189.Xr witness 4
190checks for this instance.
191.It Dv LK_CANRECURSE
192Allow recursion on an exclusive lock.
193For every lock there must be a release.
194.It Dv LK_INTERLOCK
195Unlock the interlock (which should be locked already).
196.It Dv LK_NODDLKTREAT
197Normally,
198.Fn lockmgr
199postpones serving further shared requests for shared-locked lock if there is
200exclusive waiter, to avoid exclusive lock starvation.
201But, if the thread requesting the shared lock already owns a shared lockmgr
202lock, the request is granted even in presence of the parallel exclusive lock
203request, which is done to avoid deadlocks with recursive shared acquisition.
204.Pp
205The
206.Dv LK_NODDLKTREAT
207flag can only be used by code which requests shared non-recursive lock.
208The flag allows exclusive requests to preempt the current shared request
209even if the current thread owns shared locks.
210This is safe since shared lock is guaranteed to not recurse, and is used
211when thread is known to held unrelated shared locks, to not cause
212unnecessary starvation.
213An example is
214.Dv vp
215locking in VFS
216.Xr lookup 9 ,
217when
218.Dv dvp
219is already locked.
220.El
221.It Fa ilk
222An interlock mutex for controlling group access to the lock.
223If
224.Dv LK_INTERLOCK
225is specified,
226.Fn lockmgr
227and
228.Fn lockmgr_rw
229assume
230.Fa ilk
231is currently owned and not recursed, and will return it unlocked.
232See
233.Xr mtx_assert 9 .
234.El
235.Pp
236The
237.Fn lockmgr_args
238and
239.Fn lockmgr_args_rw
240function work like
241.Fn lockmgr
242and
243.Fn lockmgr_rw
244but accepting a
245.Fa wmesg ,
246.Fa timo
247and
248.Fa prio
249on a per-instance basis.
250The specified values will override the default
251ones, but this can still be used passing, respectively,
252.Dv LK_WMESG_DEFAULT ,
253.Dv LK_PRIO_DEFAULT
254and
255.Dv LK_TIMO_DEFAULT .
256.Pp
257The
258.Fn lockmgr_disown
259function switches the owner from the current thread to be
260.Dv LK_KERNPROC ,
261if the lock is already held.
262.Pp
263The
264.Fn lockmgr_printinfo
265function prints debugging information about the lock.
266It is used primarily by
267.Xr VOP_PRINT 9
268functions.
269.Pp
270The
271.Fn lockmgr_recursed
272function returns true if the lock is recursed, 0
273otherwise.
274.Pp
275The
276.Fn lockstatus
277function returns the status of the lock in relation to the current thread.
278.Pp
279When compiled with
280.Cd "options INVARIANTS"
281and
282.Cd "options INVARIANT_SUPPORT" ,
283the
284.Fn lockmgr_assert
285function tests
286.Fa lkp
287for the assertions specified in
288.Fa what ,
289and panics if they are not met.
290One of the following assertions must be specified:
291.Bl -tag -width ".Dv KA_UNLOCKED"
292.It Dv KA_LOCKED
293Assert that the current thread has either a shared or an exclusive lock on the
294.Vt lkp
295lock pointed to by the first argument.
296.It Dv KA_SLOCKED
297Assert that the current thread has a shared lock on the
298.Vt lkp
299lock pointed to by the first argument.
300.It Dv KA_XLOCKED
301Assert that the current thread has an exclusive lock on the
302.Vt lkp
303lock pointed to by the first argument.
304.It Dv KA_UNLOCKED
305Assert that the current thread has no lock on the
306.Vt lkp
307lock pointed to by the first argument.
308.El
309.Pp
310In addition, one of the following optional assertions can be used with
311either an
312.Dv KA_LOCKED ,
313.Dv KA_SLOCKED ,
314or
315.Dv KA_XLOCKED
316assertion:
317.Bl -tag -width ".Dv KA_NOTRECURSED"
318.It Dv KA_RECURSED
319Assert that the current thread has a recursed lock on
320.Fa lkp .
321.It Dv KA_NOTRECURSED
322Assert that the current thread does not have a recursed lock on
323.Fa lkp .
324.El
325.Sh RETURN VALUES
326The
327.Fn lockmgr
328and
329.Fn lockmgr_rw
330functions return 0 on success and non-zero on failure.
331.Pp
332The
333.Fn lockstatus
334function returns:
335.Bl -tag -width ".Dv LK_EXCLUSIVE"
336.It Dv LK_EXCLUSIVE
337An exclusive lock is held by the current thread.
338.It Dv LK_EXCLOTHER
339An exclusive lock is held by someone other than the current thread.
340.It Dv LK_SHARED
341A shared lock is held.
342.It Li 0
343The lock is not held by anyone.
344.El
345.Sh ERRORS
346.Fn lockmgr
347and
348.Fn lockmgr_rw
349fail if:
350.Bl -tag -width Er
351.It Bq Er EBUSY
352.Dv LK_FORCEUPGRADE
353was requested and another thread had already requested a lock upgrade.
354.It Bq Er EBUSY
355.Dv LK_NOWAIT
356was set, and a sleep would have been required, or
357.Dv LK_TRYUPGRADE
358operation was not able to upgrade the lock.
359.It Bq Er ENOLCK
360.Dv LK_SLEEPFAIL
361was set and
362.Fn lockmgr
363or
364.Fn lockmgr_rw
365did sleep.
366.It Bq Er EINTR
367.Dv PCATCH
368was set in the lock priority, and a signal was delivered during a sleep.
369Note the
370.Er ERESTART
371error below.
372.It Bq Er ERESTART
373.Dv PCATCH
374was set in the lock priority, a signal was delivered during a sleep,
375and the system call is to be restarted.
376.It Bq Er EWOULDBLOCK
377a non-zero timeout was given, and the timeout expired.
378.El
379.Sh LOCKS
380If
381.Dv LK_INTERLOCK
382is passed in the
383.Fa flags
384argument to
385.Fn lockmgr
386or
387.Fn lockmgr_rw ,
388the
389.Fa ilk
390must be held prior to calling
391.Fn lockmgr
392or
393.Fn lockmgr_rw ,
394and will be returned unlocked.
395.Pp
396Upgrade attempts that fail result in the loss of the lock that
397is currently held.
398Also, it is invalid to upgrade an
399exclusive lock, and a
400.Xr panic 9
401will be the result of trying.
402.Sh SEE ALSO
403.Xr condvar 9 ,
404.Xr locking 9 ,
405.Xr mtx_assert 9 ,
406.Xr mutex 9 ,
407.Xr panic 9 ,
408.Xr rwlock 9 ,
409.Xr sleep 9 ,
410.Xr sx 9 ,
411.Xr VOP_PRINT 9
412.Sh AUTHORS
413This manual page was written by
414.An Chad David Aq Mt davidc@acns.ab.ca .
415