]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/rmlock.9
Merge commit 'd0e943077d94e6266ece9856789c5d5313676e38'
[FreeBSD/FreeBSD.git] / share / man / man9 / rmlock.9
1 .\" Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org>
2 .\" Copyright (c) 2006 Gleb Smirnoff <glebius@FreeBSD.org>
3 .\" All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\"
14 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 .\" SUCH DAMAGE.
25 .\"
26 .\" $FreeBSD$
27 .\"
28 .\" Based on rwlock.9 man page
29 .Dd April 12, 2021
30 .Dt RMLOCK 9
31 .Os
32 .Sh NAME
33 .Nm rmlock ,
34 .Nm rm_init ,
35 .Nm rm_init_flags ,
36 .Nm rm_destroy ,
37 .Nm rm_rlock ,
38 .Nm rm_try_rlock ,
39 .Nm rm_wlock ,
40 .Nm rm_runlock ,
41 .Nm rm_wunlock ,
42 .Nm rm_wowned ,
43 .Nm rm_sleep ,
44 .Nm rm_assert ,
45 .Nm RM_SYSINIT ,
46 .Nm RM_SYSINIT_FLAGS ,
47 .Nm rms_init ,
48 .Nm rms_destroy ,
49 .Nm rms_rlock ,
50 .Nm rms_wlock ,
51 .Nm rms_runlock ,
52 .Nm rms_wunlock
53 .Nd kernel reader/writer lock optimized for read-mostly access patterns
54 .Sh SYNOPSIS
55 .In sys/param.h
56 .In sys/lock.h
57 .In sys/rmlock.h
58 .Ft void
59 .Fn rm_init "struct rmlock *rm" "const char *name"
60 .Ft void
61 .Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts"
62 .Ft void
63 .Fn rm_destroy "struct rmlock *rm"
64 .Ft void
65 .Fn rm_rlock "struct rmlock *rm"  "struct rm_priotracker* tracker"
66 .Ft int
67 .Fn rm_try_rlock "struct rmlock *rm"  "struct rm_priotracker* tracker"
68 .Ft void
69 .Fn rm_wlock "struct rmlock *rm"
70 .Ft void
71 .Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
72 .Ft void
73 .Fn rm_wunlock "struct rmlock *rm"
74 .Ft int
75 .Fn rm_wowned "const struct rmlock *rm"
76 .Ft int
77 .Fn rm_sleep "void *wchan" "struct rmlock *rm" "int priority" "const char *wmesg" "int timo"
78 .Pp
79 .Cd "options INVARIANTS"
80 .Cd "options INVARIANT_SUPPORT"
81 .Ft void
82 .Fn rm_assert "struct rmlock *rm" "int what"
83 .In sys/kernel.h
84 .Fn RM_SYSINIT "name" "struct rmlock *rm" "const char *desc"
85 .Fn RM_SYSINIT_FLAGS "name" "struct rmlock *rm" "const char *desc" "int flags"
86 .Ft void
87 .Fn rms_init "struct rmslock *rms" "const char *name"
88 .Ft void
89 .Fn rms_destroy "struct rmslock *rms"
90 .Ft void
91 .Fn rms_rlock "struct rmslock *rms"
92 .Ft void
93 .Fn rms_wlock "struct rmslock *rms"
94 .Ft void
95 .Fn rms_runlock "struct rmslock *rms"
96 .Ft void
97 .Fn rms_wunlock "struct rmslock *rms"
98 .Sh DESCRIPTION
99 Read-mostly locks allow shared access to protected data by multiple threads,
100 or exclusive access by a single thread.
101 The threads with shared access are known as
102 .Em readers
103 since they only read the protected data.
104 A thread with exclusive access is known as a
105 .Em writer
106 since it can modify protected data.
107 .Pp
108 Read-mostly locks are designed to be efficient for locks almost exclusively
109 used as reader locks and as such should be used for protecting data that
110 rarely changes.
111 Acquiring an exclusive lock after the lock has been locked for shared access
112 is an expensive operation.
113 .Pp
114 Normal read-mostly locks are similar to
115 .Xr rwlock 9
116 locks and follow the same lock ordering rules as
117 .Xr rwlock 9
118 locks.
119 Read-mostly locks have full priority propagation like mutexes.
120 Unlike
121 .Xr rwlock 9 ,
122 read-mostly locks propagate priority to both readers and writers.
123 This is implemented via the
124 .Va rm_priotracker
125 structure argument supplied to
126 .Fn rm_rlock
127 and
128 .Fn rm_runlock .
129 Readers can recurse if the lock is initialized with the
130 .Dv RM_RECURSE
131 option;
132 however, writers are never allowed to recurse.
133 .Pp
134 Sleeping for writers can be allowed by passing
135 .Dv RM_SLEEPABLE
136 to
137 .Fn rm_init_flags .
138 It changes lock ordering rules to the same as for
139 .Xr sx 9
140 locks.
141 They do not propagate priority to writers, but they do propagate priority to readers.
142 Note that readers are not permitted to sleep regardless of the flag.
143 .Pp
144 Sleepable read-mostly locks (created with
145 .Fn rms_init )
146 allow sleeping for both readers and writers, but don't do priority propagation
147 for either.
148 They follow
149 .Xr sx 9
150 lock ordering.
151 .Ss Macros and Functions
152 .Bl -tag -width indent
153 .It Fn rm_init "struct rmlock *rm" "const char *name"
154 Initialize the read-mostly lock
155 .Fa rm .
156 The
157 .Fa name
158 description is used solely for debugging purposes.
159 This function must be called before any other operations
160 on the lock.
161 .It Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts"
162 Similar to
163 .Fn rm_init ,
164 initialize the read-mostly lock
165 .Fa rm
166 with a set of optional flags.
167 The
168 .Fa opts
169 arguments contains one or more of the following flags:
170 .Bl -tag -width ".Dv RM_NOWITNESS"
171 .It Dv RM_NOWITNESS
172 Instruct
173 .Xr witness 4
174 to ignore this lock.
175 .It Dv RM_RECURSE
176 Allow threads to recursively acquire shared locks for
177 .Fa rm .
178 .It Dv RM_SLEEPABLE
179 Create a sleepable read-mostly lock.
180 .It Dv RM_NEW
181 If the kernel has been compiled with
182 .Cd "option INVARIANTS" ,
183 .Fn rm_init_flags
184 will assert that the
185 .Fa rm
186 has not been initialized multiple times without intervening calls to
187 .Fn rm_destroy
188 unless this option is specified.
189 .It Dv RM_DUPOK
190 .Xr witness 4
191 should not log messages about duplicate locks being acquired.
192 .El
193 .It Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
194 Lock
195 .Fa rm
196 as a reader using
197 .Fa tracker
198 to track read owners of a lock for priority propagation.
199 This data structure is only used internally by
200 .Nm
201 and must persist until
202 .Fn rm_runlock
203 has been called.
204 This data structure can be allocated on the stack since
205 readers cannot sleep.
206 If any thread holds this lock exclusively, the current thread blocks,
207 and its priority is propagated to the exclusive holder.
208 If the lock was initialized with the
209 .Dv RM_RECURSE
210 option the
211 .Fn rm_rlock
212 function can be called when the current thread has already acquired reader
213 access on
214 .Fa rm .
215 .It Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
216 Try to lock
217 .Fa rm
218 as a reader.
219 .Fn rm_try_rlock
220 will return 0 if the lock cannot be acquired immediately;
221 otherwise,
222 the lock will be acquired and a non-zero value will be returned.
223 Note that
224 .Fn rm_try_rlock
225 may fail even while the lock is not currently held by a writer.
226 If the lock was initialized with the
227 .Dv RM_RECURSE
228 option,
229 .Fn rm_try_rlock
230 will succeed if the current thread has already acquired reader access.
231 .It Fn rm_wlock "struct rmlock *rm"
232 Lock
233 .Fa rm
234 as a writer.
235 If there are any shared owners of the lock, the current thread blocks.
236 The
237 .Fn rm_wlock
238 function cannot be called recursively.
239 .It Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
240 This function releases a shared lock previously acquired by
241 .Fn rm_rlock .
242 The
243 .Fa tracker
244 argument must match the
245 .Fa tracker
246 argument used for acquiring the shared lock
247 .It Fn rm_wunlock "struct rmlock *rm"
248 This function releases an exclusive lock previously acquired by
249 .Fn rm_wlock .
250 .It Fn rm_destroy "struct rmlock *rm"
251 This functions destroys a lock previously initialized with
252 .Fn rm_init .
253 The
254 .Fa rm
255 lock must be unlocked.
256 .It Fn rm_wowned "const struct rmlock *rm"
257 This function returns a non-zero value if the current thread owns an
258 exclusive lock on
259 .Fa rm .
260 .It Fn rm_sleep "void *wchan" "struct rmlock *rm" "int priority" "const char *wmesg" "int timo"
261 This function atomically releases
262 .Fa rm
263 while waiting for an event.
264 The
265 .Fa rm
266 lock must be exclusively locked.
267 For more details on the parameters to this function,
268 see
269 .Xr sleep 9 .
270 .It Fn rm_assert "struct rmlock *rm" "int what"
271 This function asserts that the
272 .Fa rm
273 lock is in the state specified by
274 .Fa what .
275 If the assertions are not true and the kernel is compiled with
276 .Cd "options INVARIANTS"
277 and
278 .Cd "options INVARIANT_SUPPORT" ,
279 the kernel will panic.
280 Currently the following base assertions are supported:
281 .Bl -tag -width ".Dv RA_UNLOCKED"
282 .It Dv RA_LOCKED
283 Assert that current thread holds either a shared or exclusive lock
284 of
285 .Fa rm .
286 .It Dv RA_RLOCKED
287 Assert that current thread holds a shared lock of
288 .Fa rm .
289 .It Dv RA_WLOCKED
290 Assert that current thread holds an exclusive lock of
291 .Fa rm .
292 .It Dv RA_UNLOCKED
293 Assert that current thread holds neither a shared nor exclusive lock of
294 .Fa rm .
295 .El
296 .Pp
297 In addition, one of the following optional flags may be specified with
298 .Dv RA_LOCKED ,
299 .Dv RA_RLOCKED ,
300 or
301 .Dv RA_WLOCKED :
302 .Bl -tag -width ".Dv RA_NOTRECURSED"
303 .It Dv RA_RECURSED
304 Assert that the current thread holds a recursive lock of
305 .Fa rm .
306 .It Dv RA_NOTRECURSED
307 Assert that the current thread does not hold a recursive lock of
308 .Fa rm .
309 .El
310 .El
311 .Bl -tag -width indent
312 .It Fn rms_init "struct rmslock *rms" "const char *name"
313 Initialize the sleepable read-mostly lock
314 .Fa rms .
315 The
316 .Fa name
317 description is used as
318 .Fa wmesg
319 parameter to the
320 .Xr msleep 9
321 routine.
322 This function must be called before any other operations on the lock.
323 .It Fn rms_rlock "struct rmlock *rm"
324 Lock
325 .Fa rms
326 as a reader.
327 If any thread holds this lock exclusively, the current thread blocks.
328 .It Fn rms_wlock "struct rmslock *rms"
329 Lock
330 .Fa rms
331 as a writer.
332 If the lock is already taken, the current thread blocks.
333 The
334 .Fn rms_wlock
335 function cannot be called recursively.
336 .It Fn rms_runlock "struct rmslock *rms"
337 This function releases a shared lock previously acquired by
338 .Fn rms_rlock .
339 .It Fn rms_wunlock "struct rmslock *rms"
340 This function releases an exclusive lock previously acquired by
341 .Fn rms_wlock .
342 .It Fn rms_destroy "struct rmslock *rms"
343 This functions destroys a lock previously initialized with
344 .Fn rms_init .
345 The
346 .Fa rms
347 lock must be unlocked.
348 .Sh SEE ALSO
349 .Xr locking 9 ,
350 .Xr mutex 9 ,
351 .Xr panic 9 ,
352 .Xr rwlock 9 ,
353 .Xr sema 9 ,
354 .Xr sleep 9 ,
355 .Xr sx 9
356 .Sh HISTORY
357 These functions appeared in
358 .Fx 7.0 .
359 .Sh AUTHORS
360 .An -nosplit
361 The
362 .Nm
363 facility was written by
364 .An "Stephan Uphoff" .
365 This manual page was written by
366 .An "Gleb Smirnoff"
367 for rwlock and modified to reflect rmlock by
368 .An "Stephan Uphoff" .
369 .Sh BUGS
370 The
371 .Nm
372 implementation is currently not optimized for single processor systems.
373 .Pp
374 .Fn rm_try_rlock
375 can fail transiently even when there is no writer, while another reader
376 updates the state on the local CPU.
377 .Pp
378 The
379 .Nm
380 implementation uses a single per CPU list shared by all
381 rmlocks in the system.
382 If rmlocks become popular, hashing to multiple per CPU queues may
383 be needed to speed up the writer lock process.