10929bf8eSJulian Elischer.\" Copyright (c) 2007 Julian Elischer (julian - freebsd org ) 20929bf8eSJulian Elischer.\" All rights reserved. 30929bf8eSJulian Elischer.\" 40929bf8eSJulian Elischer.\" Redistribution and use in source and binary forms, with or without 50929bf8eSJulian Elischer.\" modification, are permitted provided that the following conditions 60929bf8eSJulian Elischer.\" are met: 70929bf8eSJulian Elischer.\" 1. Redistributions of source code must retain the above copyright 80929bf8eSJulian Elischer.\" notice, this list of conditions and the following disclaimer. 90929bf8eSJulian Elischer.\" 2. Redistributions in binary form must reproduce the above copyright 100929bf8eSJulian Elischer.\" notice, this list of conditions and the following disclaimer in the 110929bf8eSJulian Elischer.\" documentation and/or other materials provided with the distribution. 120929bf8eSJulian Elischer.\" 130929bf8eSJulian Elischer.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 140929bf8eSJulian Elischer.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 150929bf8eSJulian Elischer.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 160929bf8eSJulian Elischer.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 170929bf8eSJulian Elischer.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 180929bf8eSJulian Elischer.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 190929bf8eSJulian Elischer.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 200929bf8eSJulian Elischer.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 210929bf8eSJulian Elischer.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 220929bf8eSJulian Elischer.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 230929bf8eSJulian Elischer.\" SUCH DAMAGE. 240929bf8eSJulian Elischer.\" 250de03c30SMark Johnston.Dd February 3, 2023 260929bf8eSJulian Elischer.Dt LOCKING 9 270929bf8eSJulian Elischer.Os 280929bf8eSJulian Elischer.Sh NAME 2946d98f57SXin LI.Nm locking 300929bf8eSJulian Elischer.Nd kernel synchronization primitives 310929bf8eSJulian Elischer.Sh DESCRIPTION 320929bf8eSJulian ElischerThe 330929bf8eSJulian Elischer.Em FreeBSD 34ca6829abSJohn Baldwinkernel is written to run across multiple CPUs and as such provides 35ca6829abSJohn Baldwinseveral different synchronization primitives to allow developers 36ca6829abSJohn Baldwinto safely access and manipulate many data types. 3720f16ca0SJulian Elischer.Ss Mutexes 38ca6829abSJohn BaldwinMutexes (also called "blocking mutexes") are the most commonly used 39ddcd2bc9SEdward Tomasz Napieralasynchronization primitive in the kernel. 402139f5e0SJulian ElischerA thread acquires (locks) a mutex before accessing data shared with other 41815e4772SEdward Tomasz Napieralathreads (including interrupt threads), and releases (unlocks) it afterwards. 422139f5e0SJulian ElischerIf the mutex cannot be acquired, the thread requesting it will wait. 43ca6829abSJohn BaldwinMutexes are adaptive by default, meaning that 442139f5e0SJulian Elischerif the owner of a contended mutex is currently running on another CPU, 45ca6829abSJohn Baldwinthen a thread attempting to acquire the mutex will spin rather than yielding 46ca6829abSJohn Baldwinthe processor. 47561205dfSEdward Tomasz NapieralaMutexes fully support priority propagation. 48815e4772SEdward Tomasz Napierala.Pp 494ff467dcSEdward Tomasz NapieralaSee 50815e4772SEdward Tomasz Napierala.Xr mutex 9 514ff467dcSEdward Tomasz Napieralafor details. 52ca6829abSJohn Baldwin.Ss Spin Mutexes 53ca6829abSJohn BaldwinSpin mutexes are a variation of basic mutexes; the main difference between 54ca6829abSJohn Baldwinthe two is that spin mutexes never block. 55ca6829abSJohn BaldwinInstead, they spin while waiting for the lock to be released. 5610038699SBenjamin KadukTo avoid deadlock, a thread that holds a spin mutex must never yield its CPU. 57ca6829abSJohn BaldwinUnlike ordinary mutexes, spin mutexes disable interrupts when acquired. 58ca6829abSJohn BaldwinSince disabling interrupts can be expensive, they are generally slower to 59ca6829abSJohn Baldwinacquire and release. 60ca6829abSJohn BaldwinSpin mutexes should be used only when absolutely necessary, 61ca6829abSJohn Baldwine.g. to protect data shared 62ddcd2bc9SEdward Tomasz Napieralawith interrupt filter code (see 63ddcd2bc9SEdward Tomasz Napierala.Xr bus_setup_intr 9 64ca6829abSJohn Baldwinfor details), 65ca6829abSJohn Baldwinor for scheduler internals. 66ca6829abSJohn Baldwin.Ss Mutex Pools 67ca6829abSJohn BaldwinWith most synchronization primitives, such as mutexes, the programmer must 68ca6829abSJohn Baldwinprovide memory to hold the primitive. 69815e4772SEdward Tomasz NapieralaFor example, a mutex may be embedded inside the structure it protects. 70ca6829abSJohn BaldwinMutex pools provide a preallocated set of mutexes to avoid this 71ca6829abSJohn Baldwinrequirement. 72ca6829abSJohn BaldwinNote that mutexes from a pool may only be used as leaf locks. 73815e4772SEdward Tomasz Napierala.Pp 744ff467dcSEdward Tomasz NapieralaSee 75815e4772SEdward Tomasz Napierala.Xr mtx_pool 9 764ff467dcSEdward Tomasz Napieralafor details. 77ca6829abSJohn Baldwin.Ss Reader/Writer Locks 78ca6829abSJohn BaldwinReader/writer locks allow shared access to protected data by multiple threads 790929bf8eSJulian Elischeror exclusive access by a single thread. 800929bf8eSJulian ElischerThe threads with shared access are known as 810929bf8eSJulian Elischer.Em readers 8220f16ca0SJulian Elischersince they should only read the protected data. 830929bf8eSJulian ElischerA thread with exclusive access is known as a 840929bf8eSJulian Elischer.Em writer 8520f16ca0SJulian Elischersince it may modify protected data. 860929bf8eSJulian Elischer.Pp 8720f16ca0SJulian ElischerReader/writer locks can be treated as mutexes (see above and 880929bf8eSJulian Elischer.Xr mutex 9 ) 890929bf8eSJulian Elischerwith shared/exclusive semantics. 90ca6829abSJohn BaldwinReader/writer locks support priority propagation like mutexes, 91ca6829abSJohn Baldwinbut priority is propagated only to an exclusive holder. 920929bf8eSJulian ElischerThis limitation comes from the fact that shared owners 930929bf8eSJulian Elischerare anonymous. 94815e4772SEdward Tomasz Napierala.Pp 954ff467dcSEdward Tomasz NapieralaSee 96815e4772SEdward Tomasz Napierala.Xr rwlock 9 974ff467dcSEdward Tomasz Napieralafor details. 98ca6829abSJohn Baldwin.Ss Read-Mostly Locks 99ca6829abSJohn BaldwinRead-mostly locks are similar to 100561205dfSEdward Tomasz Napierala.Em reader/writer 101561205dfSEdward Tomasz Napieralalocks but optimized for very infrequent write locking. 102561205dfSEdward Tomasz Napierala.Em Read-mostly 103f53d15feSStephan Uphofflocks implement full priority propagation by tracking shared owners 10488238a08SEdward Tomasz Napieralausing a caller-supplied 105f53d15feSStephan Uphoff.Em tracker 106f53d15feSStephan Uphoffdata structure. 107815e4772SEdward Tomasz Napierala.Pp 1084ff467dcSEdward Tomasz NapieralaSee 109815e4772SEdward Tomasz Napierala.Xr rmlock 9 1104ff467dcSEdward Tomasz Napieralafor details. 111ca6829abSJohn Baldwin.Ss Sleepable Read-Mostly Locks 112ca6829abSJohn BaldwinSleepable read-mostly locks are a variation on read-mostly locks. 113ca6829abSJohn BaldwinThreads holding an exclusive lock may sleep, 114ca6829abSJohn Baldwinbut threads holding a shared lock may not. 115ca6829abSJohn BaldwinPriority is propagated to shared owners but not to exclusive owners. 116815e4772SEdward Tomasz Napierala.Ss Shared/exclusive locks 117561205dfSEdward Tomasz NapieralaShared/exclusive locks are similar to reader/writer locks; the main difference 118ca6829abSJohn Baldwinbetween them is that shared/exclusive locks may be held during unbounded sleep. 119ca6829abSJohn BaldwinAcquiring a contested shared/exclusive lock can perform an unbounded sleep. 120ca6829abSJohn BaldwinThese locks do not support priority propagation. 121815e4772SEdward Tomasz Napierala.Pp 1224ff467dcSEdward Tomasz NapieralaSee 123815e4772SEdward Tomasz Napierala.Xr sx 9 1244ff467dcSEdward Tomasz Napieralafor details. 125ca6829abSJohn Baldwin.Ss Lockmanager locks 126ca6829abSJohn BaldwinLockmanager locks are sleepable shared/exclusive locks used mostly in 127ca6829abSJohn Baldwin.Xr VFS 9 128ca6829abSJohn Baldwin.Po 129ca6829abSJohn Baldwinas a 130ca6829abSJohn Baldwin.Xr vnode 9 131ca6829abSJohn Baldwinlock 132ca6829abSJohn Baldwin.Pc 133ca6829abSJohn Baldwinand in the buffer cache 134ca6829abSJohn Baldwin.Po 135ca6829abSJohn Baldwin.Xr BUF_LOCK 9 136ca6829abSJohn Baldwin.Pc . 137ca6829abSJohn BaldwinThey have features other lock types do not have such as sleep 138ca6829abSJohn Baldwintimeouts, blocking upgrades, 139ca6829abSJohn Baldwinwriter starvation avoidance, draining, and an interlock mutex, 14010038699SBenjamin Kadukbut this makes them complicated both to use and to implement; 141ca6829abSJohn Baldwinfor this reason, they should be avoided. 142ca6829abSJohn Baldwin.Pp 143ca6829abSJohn BaldwinSee 144ca6829abSJohn Baldwin.Xr lock 9 145ca6829abSJohn Baldwinfor details. 1460de03c30SMark Johnston.Ss Non-blocking synchronization 1470de03c30SMark JohnstonThe kernel has two facilities, 1480de03c30SMark Johnston.Xr epoch 9 1490de03c30SMark Johnstonand 1500de03c30SMark Johnston.Xr smr 9 , 1510de03c30SMark Johnstonwhich can be used to provide read-only access to a data structure while one or 1520de03c30SMark Johnstonmore writers are concurrently modifying the data structure. 1530de03c30SMark JohnstonSpecifically, readers using 1540de03c30SMark Johnston.Xr epoch 9 1550de03c30SMark Johnstonand 1560de03c30SMark Johnston.Xr smr 9 1570de03c30SMark Johnstonto synchronize accesses do not block writers, in contrast with reader/writer 1580de03c30SMark Johnstonlocks, and they help ensure that memory freed by writers is not reused until 1590de03c30SMark Johnstonall readers which may be accessing it have finished. 1600de03c30SMark JohnstonThus, they are a useful building block in the construction of lock-free 1610de03c30SMark Johnstondata structures. 1620de03c30SMark Johnston.Pp 1630de03c30SMark JohnstonThese facilities are difficult to use correctly and should be avoided 1640de03c30SMark Johnstonin preference to traditional mutual exclusion-based synchronization, 1650de03c30SMark Johnstonexcept when performance or non-blocking guarantees are a major concern. 1660de03c30SMark Johnston.Pp 1670de03c30SMark JohnstonSee 1680de03c30SMark Johnston.Xr epoch 9 1690de03c30SMark Johnstonand 1700de03c30SMark Johnston.Xr smr 9 1710de03c30SMark Johnstonfor details. 172815e4772SEdward Tomasz Napierala.Ss Counting semaphores 173815e4772SEdward Tomasz NapieralaCounting semaphores provide a mechanism for synchronizing access 174815e4772SEdward Tomasz Napieralato a pool of resources. 175815e4772SEdward Tomasz NapieralaUnlike mutexes, semaphores do not have the concept of an owner, 176815e4772SEdward Tomasz Napieralaso they can be useful in situations where one thread needs 177815e4772SEdward Tomasz Napieralato acquire a resource, and another thread needs to release it. 178815e4772SEdward Tomasz NapieralaThey are largely deprecated. 179815e4772SEdward Tomasz Napierala.Pp 1804ff467dcSEdward Tomasz NapieralaSee 181815e4772SEdward Tomasz Napierala.Xr sema 9 1824ff467dcSEdward Tomasz Napieralafor details. 1830929bf8eSJulian Elischer.Ss Condition variables 184ca6829abSJohn BaldwinCondition variables are used in conjunction with locks to wait for 185ca6829abSJohn Baldwina condition to become true. 186ca6829abSJohn BaldwinA thread must hold the associated lock before calling one of the 187ca6829abSJohn Baldwin.Fn cv_wait , 1880929bf8eSJulian Elischerfunctions. 189ca6829abSJohn BaldwinWhen a thread waits on a condition, the lock 190ca6829abSJohn Baldwinis atomically released before the thread yields the processor 191ca6829abSJohn Baldwinand reacquired before the function call returns. 192ca6829abSJohn BaldwinCondition variables may be used with blocking mutexes, 193ca6829abSJohn Baldwinreader/writer locks, read-mostly locks, and shared/exclusive locks. 194815e4772SEdward Tomasz Napierala.Pp 1954ff467dcSEdward Tomasz NapieralaSee 196815e4772SEdward Tomasz Napierala.Xr condvar 9 1974ff467dcSEdward Tomasz Napieralafor details. 198ca6829abSJohn Baldwin.Ss Sleep/Wakeup 1990929bf8eSJulian ElischerThe functions 2000929bf8eSJulian Elischer.Fn tsleep , 2010929bf8eSJulian Elischer.Fn msleep , 2020929bf8eSJulian Elischer.Fn msleep_spin , 2030929bf8eSJulian Elischer.Fn pause , 2040929bf8eSJulian Elischer.Fn wakeup , 2050929bf8eSJulian Elischerand 2060929bf8eSJulian Elischer.Fn wakeup_one 207ca6829abSJohn Baldwinalso handle event-based thread blocking. 208ca6829abSJohn BaldwinUnlike condition variables, 20910038699SBenjamin Kadukarbitrary addresses may be used as wait channels and a dedicated 210ca6829abSJohn Baldwinstructure does not need to be allocated. 211ca6829abSJohn BaldwinHowever, care must be taken to ensure that wait channel addresses are 212ca6829abSJohn Baldwinunique to an event. 2134ff78a9cSJulian ElischerIf a thread must wait for an external event, it is put to sleep by 2140929bf8eSJulian Elischer.Fn tsleep , 2150929bf8eSJulian Elischer.Fn msleep , 2160929bf8eSJulian Elischer.Fn msleep_spin , 2170929bf8eSJulian Elischeror 2180929bf8eSJulian Elischer.Fn pause . 2190929bf8eSJulian ElischerThreads may also wait using one of the locking primitive sleep routines 2200929bf8eSJulian Elischer.Xr mtx_sleep 9 , 2210929bf8eSJulian Elischer.Xr rw_sleep 9 , 2220929bf8eSJulian Elischeror 2230929bf8eSJulian Elischer.Xr sx_sleep 9 . 2240929bf8eSJulian Elischer.Pp 2250929bf8eSJulian ElischerThe parameter 2260929bf8eSJulian Elischer.Fa chan 2270929bf8eSJulian Elischeris an arbitrary address that uniquely identifies the event on which 2280929bf8eSJulian Elischerthe thread is being put to sleep. 2290929bf8eSJulian ElischerAll threads sleeping on a single 2300929bf8eSJulian Elischer.Fa chan 2310929bf8eSJulian Elischerare woken up later by 232ca6829abSJohn Baldwin.Fn wakeup 233ca6829abSJohn Baldwin.Pq often called from inside an interrupt routine 234ca6829abSJohn Baldwinto indicate that the 235ca6829abSJohn Baldwinevent the thread was blocking on has occurred. 2360929bf8eSJulian Elischer.Pp 2370929bf8eSJulian ElischerSeveral of the sleep functions including 2380929bf8eSJulian Elischer.Fn msleep , 2390929bf8eSJulian Elischer.Fn msleep_spin , 2400929bf8eSJulian Elischerand the locking primitive sleep routines specify an additional lock 2410929bf8eSJulian Elischerparameter. 2420929bf8eSJulian ElischerThe lock will be released before sleeping and reacquired 2430929bf8eSJulian Elischerbefore the sleep routine returns. 2440929bf8eSJulian ElischerIf 2450929bf8eSJulian Elischer.Fa priority 2460929bf8eSJulian Elischerincludes the 2470929bf8eSJulian Elischer.Dv PDROP 2484ff78a9cSJulian Elischerflag, then the lock will not be reacquired before returning. 2490929bf8eSJulian ElischerThe lock is used to ensure that a condition can be checked atomically, 2500929bf8eSJulian Elischerand that the current thread can be suspended without missing a 251ca6829abSJohn Baldwinchange to the condition or an associated wakeup. 2520929bf8eSJulian ElischerIn addition, all of the sleep routines will fully drop the 2530929bf8eSJulian Elischer.Va Giant 2540929bf8eSJulian Elischermutex 255ca6829abSJohn Baldwin.Pq even if recursed 2560929bf8eSJulian Elischerwhile the thread is suspended and will reacquire the 2570929bf8eSJulian Elischer.Va Giant 258ca6829abSJohn Baldwinmutex 259ca6829abSJohn Baldwin.Pq restoring any recursion 260ca6829abSJohn Baldwinbefore the function returns. 261ca6829abSJohn Baldwin.Pp 262ca6829abSJohn BaldwinThe 263ca6829abSJohn Baldwin.Fn pause 264ca6829abSJohn Baldwinfunction is a special sleep function that waits for a specified 265ca6829abSJohn Baldwinamount of time to pass before the thread resumes execution. 266ca6829abSJohn BaldwinThis sleep cannot be terminated early by either an explicit 267ca6829abSJohn Baldwin.Fn wakeup 268ca6829abSJohn Baldwinor a signal. 2690929bf8eSJulian Elischer.Pp 2704ff467dcSEdward Tomasz NapieralaSee 271815e4772SEdward Tomasz Napierala.Xr sleep 9 2724ff467dcSEdward Tomasz Napieralafor details. 273ca6829abSJohn Baldwin.Ss Giant 274ca6829abSJohn BaldwinGiant is a special mutex used to protect data structures that do not 275ca6829abSJohn Baldwinyet have their own locks. 276ca6829abSJohn BaldwinSince it provides semantics akin to the old 277ca6829abSJohn Baldwin.Xr spl 9 278ca6829abSJohn Baldwininterface, 279ca6829abSJohn BaldwinGiant has special characteristics: 280ca6829abSJohn Baldwin.Bl -enum 281ca6829abSJohn Baldwin.It 282ca6829abSJohn BaldwinIt is recursive. 283ca6829abSJohn Baldwin.It 284ca6829abSJohn BaldwinDrivers can request that Giant be locked around them 285ca6829abSJohn Baldwinby not marking themselves MPSAFE. 286ca6829abSJohn BaldwinNote that infrastructure to do this is slowly going away as non-MPSAFE 287ca6829abSJohn Baldwindrivers either became properly locked or disappear. 288ca6829abSJohn Baldwin.It 289ca6829abSJohn BaldwinGiant must be locked before other non-sleepable locks. 290ca6829abSJohn Baldwin.It 291ca6829abSJohn BaldwinGiant is dropped during unbounded sleeps and reacquired after wakeup. 292ca6829abSJohn Baldwin.It 293ca6829abSJohn BaldwinThere are places in the kernel that drop Giant and pick it back up 294ca6829abSJohn Baldwinagain. 295ca6829abSJohn BaldwinSleep locks will do this before sleeping. 296ca6829abSJohn BaldwinParts of the network or VM code may do this as well. 297ca6829abSJohn BaldwinThis means that you cannot count on Giant keeping other code from 298ca6829abSJohn Baldwinrunning if your code sleeps, even if you want it to. 299ca6829abSJohn Baldwin.El 300815e4772SEdward Tomasz Napierala.Sh INTERACTIONS 301ca6829abSJohn BaldwinThe primitives can interact and have a number of rules regarding how 302ddcd2bc9SEdward Tomasz Napieralathey can and can not be combined. 303ca6829abSJohn BaldwinMany of these rules are checked by 304ca6829abSJohn Baldwin.Xr witness 4 . 305ca6829abSJohn Baldwin.Ss Bounded vs. Unbounded Sleep 30610038699SBenjamin KadukIn a bounded sleep 30710038699SBenjamin Kaduk.Po also referred to as 30810038699SBenjamin Kaduk.Dq blocking 30910038699SBenjamin Kaduk.Pc 31010038699SBenjamin Kadukthe only resource needed to resume execution of a thread 311ca6829abSJohn Baldwinis CPU time for the owner of a lock that the thread is waiting to acquire. 31210038699SBenjamin KadukIn an unbounded sleep 313ca6829abSJohn Baldwin.Po 314ca6829abSJohn Baldwinoften referred to as simply 315ca6829abSJohn Baldwin.Dq sleeping 316ca6829abSJohn Baldwin.Pc 31710038699SBenjamin Kaduka thread waits for an external event or for a condition 318ca6829abSJohn Baldwinto become true. 319ca6829abSJohn BaldwinIn particular, 320ca6829abSJohn Baldwina dependency chain of threads in bounded sleeps should always make forward 32110038699SBenjamin Kadukprogress, 32210038699SBenjamin Kaduksince there is always CPU time available. 323ca6829abSJohn BaldwinThis requires that no thread in a bounded sleep is waiting for a lock held 324ca6829abSJohn Baldwinby a thread in an unbounded sleep. 325ca6829abSJohn BaldwinTo avoid priority inversions, 326ca6829abSJohn Baldwina thread in a bounded sleep lends its priority to the owner of the lock 327ca6829abSJohn Baldwinthat it is waiting for. 328561205dfSEdward Tomasz Napierala.Pp 329ca6829abSJohn BaldwinThe following primitives perform bounded sleeps: 330ca6829abSJohn Baldwinmutexes, reader/writer locks and read-mostly locks. 331561205dfSEdward Tomasz Napierala.Pp 332ca6829abSJohn BaldwinThe following primitives perform unbounded sleeps: 333ca6829abSJohn Baldwinsleepable read-mostly locks, shared/exclusive locks, lockmanager locks, 334ca6829abSJohn Baldwincounting semaphores, condition variables, and sleep/wakeup. 335ca6829abSJohn Baldwin.Ss General Principles 336ca6829abSJohn Baldwin.Bl -bullet 337ca6829abSJohn Baldwin.It 3382139f5e0SJulian ElischerIt is an error to do any operation that could result in yielding the processor 3392139f5e0SJulian Elischerwhile holding a spin mutex. 340ca6829abSJohn Baldwin.It 341ca6829abSJohn BaldwinIt is an error to do any operation that could result in unbounded sleep 342ca6829abSJohn Baldwinwhile holding any primitive from the 'bounded sleep' group. 343ca6829abSJohn BaldwinFor example, it is an error to try to acquire a shared/exclusive lock while 344ca6829abSJohn Baldwinholding a mutex, or to try to allocate memory with M_WAITOK while holding a 345ca6829abSJohn Baldwinreader/writer lock. 346561205dfSEdward Tomasz Napierala.Pp 347ca6829abSJohn BaldwinNote that the lock passed to one of the 34888238a08SEdward Tomasz Napierala.Fn sleep 349561205dfSEdward Tomasz Napieralaor 350ca6829abSJohn Baldwin.Fn cv_wait 351ca6829abSJohn Baldwinfunctions is dropped before the thread enters the unbounded sleep and does 352ca6829abSJohn Baldwinnot violate this rule. 353ca6829abSJohn Baldwin.It 3542139f5e0SJulian ElischerIt is an error to do any operation that could result in yielding of 3552139f5e0SJulian Elischerthe processor when running inside an interrupt filter. 356ca6829abSJohn Baldwin.It 3570b0b48c0SEdward Tomasz NapieralaIt is an error to do any operation that could result in unbounded sleep when 3580b0b48c0SEdward Tomasz Napieralarunning inside an interrupt thread. 359ca6829abSJohn Baldwin.El 360561205dfSEdward Tomasz Napierala.Ss Interaction table 361ddcd2bc9SEdward Tomasz NapieralaThe following table shows what you can and can not do while holding 3627851d429SEdward Tomasz Napieralaone of the locking primitives discussed. 3637851d429SEdward Tomasz NapieralaNote that 364ca6829abSJohn Baldwin.Dq sleep 365ca6829abSJohn Baldwinincludes 366ca6829abSJohn Baldwin.Fn sema_wait , 367ca6829abSJohn Baldwin.Fn sema_timedwait , 368ca6829abSJohn Baldwinany of the 369ca6829abSJohn Baldwin.Fn cv_wait 370ca6829abSJohn Baldwinfunctions, 371ca6829abSJohn Baldwinand any of the 372ca6829abSJohn Baldwin.Fn sleep 373ca6829abSJohn Baldwinfunctions. 374ca6829abSJohn Baldwin.Bl -column ".Ic xxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXXXX" ".Xr XXXXXX" -offset 3n 375ca6829abSJohn Baldwin.It Em " You want:" Ta spin mtx Ta mutex/rw Ta rmlock Ta sleep rm Ta sx/lk Ta sleep 376ca6829abSJohn Baldwin.It Em "You have: " Ta -------- Ta -------- Ta ------ Ta -------- Ta ------ Ta ------ 377ca6829abSJohn Baldwin.It spin mtx Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no-1 378ca6829abSJohn Baldwin.It mutex/rw Ta \&ok Ta \&ok Ta \&ok Ta \&no Ta \&no Ta \&no-1 379ca6829abSJohn Baldwin.It rmlock Ta \&ok Ta \&ok Ta \&ok Ta \&no Ta \&no Ta \&no-1 380ca6829abSJohn Baldwin.It sleep rm Ta \&ok Ta \&ok Ta \&ok Ta \&ok-2 Ta \&ok-2 Ta \&ok-2/3 381ca6829abSJohn Baldwin.It sx Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok-3 382ca6829abSJohn Baldwin.It lockmgr Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok 3830929bf8eSJulian Elischer.El 3840929bf8eSJulian Elischer.Pp 3850929bf8eSJulian Elischer.Em *1 386ca6829abSJohn BaldwinThere are calls that atomically release this primitive when going to sleep 387ca6829abSJohn Baldwinand reacquire it on wakeup 388ca6829abSJohn Baldwin.Po 389ca6829abSJohn Baldwin.Fn mtx_sleep , 390ca6829abSJohn Baldwin.Fn rw_sleep , 391ca6829abSJohn Baldwin.Fn msleep_spin , 392ca6829abSJohn Baldwinetc. 393ca6829abSJohn Baldwin.Pc . 3940929bf8eSJulian Elischer.Pp 3950929bf8eSJulian Elischer.Em *2 396ca6829abSJohn BaldwinThese cases are only allowed while holding a write lock on a sleepable 397ca6829abSJohn Baldwinread-mostly lock. 3980929bf8eSJulian Elischer.Pp 3990929bf8eSJulian Elischer.Em *3 400ca6829abSJohn BaldwinThough one can sleep while holding this lock, 401ca6829abSJohn Baldwinone can also use a 402ca6829abSJohn Baldwin.Fn sleep 403ca6829abSJohn Baldwinfunction to atomically release this primitive when going to sleep and 4044ff78a9cSJulian Elischerreacquire it on wakeup. 40536058c09SMax Laier.Pp 406ca6829abSJohn BaldwinNote that non-blocking try operations on locks are always permitted. 407561205dfSEdward Tomasz Napierala.Ss Context mode table 4084ff78a9cSJulian ElischerThe next table shows what can be used in different contexts. 4094ff78a9cSJulian ElischerAt this time this is a rather easy to remember table. 410ca6829abSJohn Baldwin.Bl -column ".Ic Xxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXXXX" ".Xr XXXXXX" -offset 3n 411ca6829abSJohn Baldwin.It Em "Context:" Ta spin mtx Ta mutex/rw Ta rmlock Ta sleep rm Ta sx/lk Ta sleep 412ddcd2bc9SEdward Tomasz Napierala.It interrupt filter: Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no 413ca6829abSJohn Baldwin.It interrupt thread: Ta \&ok Ta \&ok Ta \&ok Ta \&no Ta \&no Ta \&no 414ca6829abSJohn Baldwin.It callout: Ta \&ok Ta \&ok Ta \&ok Ta \&no Ta \&no Ta \&no 415e6e979f5SKonstantin Belousov.It direct callout: Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no 416ca6829abSJohn Baldwin.It system call: Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok 417c2c54c3dSJulian Elischer.El 4180929bf8eSJulian Elischer.Sh SEE ALSO 419fd126a73SMateusz Piotrowski.Xr lockstat 1 , 42088238a08SEdward Tomasz Napierala.Xr witness 4 , 4210de03c30SMark Johnston.Xr atomic 9 , 4221e9469d1SChristian Brueffer.Xr BUS_SETUP_INTR 9 , 423*8965b303SMitchell Horne.Xr callout 9 , 4240929bf8eSJulian Elischer.Xr condvar 9 , 4250de03c30SMark Johnston.Xr epoch 9 , 4263111fc98SChristian Brueffer.Xr lock 9 , 4271e9469d1SChristian Brueffer.Xr LOCK_PROFILING 9 , 4280929bf8eSJulian Elischer.Xr mtx_pool 9 , 42920f16ca0SJulian Elischer.Xr mutex 9 , 430f53d15feSStephan Uphoff.Xr rmlock 9 , 4310929bf8eSJulian Elischer.Xr rwlock 9 , 4320929bf8eSJulian Elischer.Xr sema 9 , 4330929bf8eSJulian Elischer.Xr sleep 9 , 4340de03c30SMark Johnston.Xr smr 9 , 435*8965b303SMitchell Horne.Xr sx 9 436815e4772SEdward Tomasz Napierala.Sh BUGS 437815e4772SEdward Tomasz NapieralaThere are too many locking primitives to choose from. 438