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 November 11, 2017 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.Nd kernel reader/writer lock optimized for read-mostly access patterns 48.Sh SYNOPSIS 49.In sys/param.h 50.In sys/lock.h 51.In sys/rmlock.h 52.Ft void 53.Fn rm_init "struct rmlock *rm" "const char *name" 54.Ft void 55.Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts" 56.Ft void 57.Fn rm_destroy "struct rmlock *rm" 58.Ft void 59.Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker" 60.Ft int 61.Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker" 62.Ft void 63.Fn rm_wlock "struct rmlock *rm" 64.Ft void 65.Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker" 66.Ft void 67.Fn rm_wunlock "struct rmlock *rm" 68.Ft int 69.Fn rm_wowned "const struct rmlock *rm" 70.Ft int 71.Fn rm_sleep "void *wchan" "struct rmlock *rm" "int priority" "const char *wmesg" "int timo" 72.Pp 73.Cd "options INVARIANTS" 74.Cd "options INVARIANT_SUPPORT" 75.Ft void 76.Fn rm_assert "struct rmlock *rm" "int what" 77.In sys/kernel.h 78.Fn RM_SYSINIT "name" "struct rmlock *rm" "const char *desc" 79.Fn RM_SYSINIT_FLAGS "name" "struct rmlock *rm" "const char *desc" "int flags" 80.Sh DESCRIPTION 81Read-mostly locks allow shared access to protected data by multiple threads, 82or exclusive access by a single thread. 83The threads with shared access are known as 84.Em readers 85since they only read the protected data. 86A thread with exclusive access is known as a 87.Em writer 88since it can modify protected data. 89.Pp 90Read-mostly locks are designed to be efficient for locks almost exclusively 91used as reader locks and as such should be used for protecting data that 92rarely changes. 93Acquiring an exclusive lock after the lock has been locked for shared access 94is an expensive operation. 95.Pp 96Normal read-mostly locks are similar to 97.Xr rwlock 9 98locks and follow the same lock ordering rules as 99.Xr rwlock 9 100locks. 101Read-mostly locks have full priority propagation like mutexes. 102Unlike 103.Xr rwlock 9 , 104read-mostly locks propagate priority to both readers and writers. 105This is implemented via the 106.Va rm_priotracker 107structure argument supplied to 108.Fn rm_rlock 109and 110.Fn rm_runlock . 111Readers can recurse if the lock is initialized with the 112.Dv RM_RECURSE 113option; 114however, writers are never allowed to recurse. 115.Pp 116Sleepable read-mostly locks are created by passing 117.Dv RM_SLEEPABLE 118to 119.Fn rm_init_flags . 120Unlike normal read-mostly locks, 121sleepable read-mostly locks follow the same lock ordering rules as 122.Xr sx 9 123locks. 124Sleepable read-mostly locks do not propagate priority to writers, 125but they do propagate priority to readers. 126Writers are permitted to sleep while holding a read-mostly lock, 127but readers are not. 128Unlike other sleepable locks such as 129.Xr sx 9 130locks, 131readers must use try operations on other sleepable locks to avoid sleeping. 132.Ss Macros and Functions 133.Bl -tag -width indent 134.It Fn rm_init "struct rmlock *rm" "const char *name" 135Initialize the read-mostly lock 136.Fa rm . 137The 138.Fa name 139description is used solely for debugging purposes. 140This function must be called before any other operations 141on the lock. 142.It Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts" 143Similar to 144.Fn rm_init , 145initialize the read-mostly lock 146.Fa rm 147with a set of optional flags. 148The 149.Fa opts 150arguments contains one or more of the following flags: 151.Bl -tag -width ".Dv RM_NOWITNESS" 152.It Dv RM_NOWITNESS 153Instruct 154.Xr witness 4 155to ignore this lock. 156.It Dv RM_RECURSE 157Allow threads to recursively acquire shared locks for 158.Fa rm . 159.It Dv RM_SLEEPABLE 160Create a sleepable read-mostly lock. 161.It Dv RM_NEW 162If the kernel has been compiled with 163.Cd "option INVARIANTS" , 164.Fn rm_init_flags 165will assert that the 166.Fa rm 167has not been initialized multiple times without intervening calls to 168.Fn rm_destroy 169unless this option is specified. 170.El 171.It Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker" 172Lock 173.Fa rm 174as a reader using 175.Fa tracker 176to track read owners of a lock for priority propagation. 177This data structure is only used internally by 178.Nm 179and must persist until 180.Fn rm_runlock 181has been called. 182This data structure can be allocated on the stack since 183readers cannot sleep. 184If any thread holds this lock exclusively, the current thread blocks, 185and its priority is propagated to the exclusive holder. 186If the lock was initialized with the 187.Dv RM_RECURSE 188option the 189.Fn rm_rlock 190function can be called when the current thread has already acquired reader 191access on 192.Fa rm . 193.It Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker" 194Try to lock 195.Fa rm 196as a reader. 197.Fn rm_try_rlock 198will return 0 if the lock cannot be acquired immediately; 199otherwise, 200the lock will be acquired and a non-zero value will be returned. 201Note that 202.Fn rm_try_rlock 203may fail even while the lock is not currently held by a writer. 204If the lock was initialized with the 205.Dv RM_RECURSE 206option, 207.Fn rm_try_rlock 208will succeed if the current thread has already acquired reader access. 209.It Fn rm_wlock "struct rmlock *rm" 210Lock 211.Fa rm 212as a writer. 213If there are any shared owners of the lock, the current thread blocks. 214The 215.Fn rm_wlock 216function cannot be called recursively. 217.It Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker" 218This function releases a shared lock previously acquired by 219.Fn rm_rlock . 220The 221.Fa tracker 222argument must match the 223.Fa tracker 224argument used for acquiring the shared lock 225.It Fn rm_wunlock "struct rmlock *rm" 226This function releases an exclusive lock previously acquired by 227.Fn rm_wlock . 228.It Fn rm_destroy "struct rmlock *rm" 229This functions destroys a lock previously initialized with 230.Fn rm_init . 231The 232.Fa rm 233lock must be unlocked. 234.It Fn rm_wowned "const struct rmlock *rm" 235This function returns a non-zero value if the current thread owns an 236exclusive lock on 237.Fa rm . 238.It Fn rm_sleep "void *wchan" "struct rmlock *rm" "int priority" "const char *wmesg" "int timo" 239This function atomically releases 240.Fa rm 241while waiting for an event. 242The 243.Fa rm 244lock must be exclusively locked. 245For more details on the parameters to this function, 246see 247.Xr sleep 9 . 248.It Fn rm_assert "struct rmlock *rm" "int what" 249This function asserts that the 250.Fa rm 251lock is in the state specified by 252.Fa what . 253If the assertions are not true and the kernel is compiled with 254.Cd "options INVARIANTS" 255and 256.Cd "options INVARIANT_SUPPORT" , 257the kernel will panic. 258Currently the following base assertions are supported: 259.Bl -tag -width ".Dv RA_UNLOCKED" 260.It Dv RA_LOCKED 261Assert that current thread holds either a shared or exclusive lock 262of 263.Fa rm . 264.It Dv RA_RLOCKED 265Assert that current thread holds a shared lock of 266.Fa rm . 267.It Dv RA_WLOCKED 268Assert that current thread holds an exclusive lock of 269.Fa rm . 270.It Dv RA_UNLOCKED 271Assert that current thread holds neither a shared nor exclusive lock of 272.Fa rm . 273.El 274.Pp 275In addition, one of the following optional flags may be specified with 276.Dv RA_LOCKED , 277.Dv RA_RLOCKED , 278or 279.Dv RA_WLOCKED : 280.Bl -tag -width ".Dv RA_NOTRECURSED" 281.It Dv RA_RECURSED 282Assert that the current thread holds a recursive lock of 283.Fa rm . 284.It Dv RA_NOTRECURSED 285Assert that the current thread does not hold a recursive lock of 286.Fa rm . 287.El 288.El 289.Sh SEE ALSO 290.Xr locking 9 , 291.Xr mutex 9 , 292.Xr panic 9 , 293.Xr rwlock 9 , 294.Xr sema 9 , 295.Xr sleep 9 , 296.Xr sx 9 297.Sh HISTORY 298These 299functions appeared in 300.Fx 7.0 . 301.Sh AUTHORS 302.An -nosplit 303The 304.Nm 305facility was written by 306.An "Stephan Uphoff" . 307This manual page was written by 308.An "Gleb Smirnoff" 309for rwlock and modified to reflect rmlock by 310.An "Stephan Uphoff" . 311.Sh BUGS 312The 313.Nm 314implementation is currently not optimized for single processor systems. 315.Pp 316.Fn rm_try_rlock 317can fail transiently even when there is no writer, while another reader 318updates the state on the local CPU. 319.Pp 320The 321.Nm 322implementation uses a single per CPU list shared by all 323rmlocks in the system. 324If rmlocks become popular, hashing to multiple per CPU queues may 325be needed to speed up the writer lock process. 326