LOCK(9)
π LOCK(9) β the lockmgr family of functions π
okay bestie, welcome to section 9 of the Kernel Developer's Manual, aka the Kernel Developer's Manual page for LOCK(9). this is the lock girlies' page. it's giving "i hold the entire kernel together and i have opinions about it" and honestly? iconic. πΈ
buckle up, we're going flag by flag, panic by panic. no skipping, no skimming. π
β¨ NAME β¨
lockinit, lockdestroy, lockmgr, lockmgr_args, lockmgr_args_rw, lockmgr_disown, lockmgr_printinfo, lockmgr_recursed, lockmgr_rw, lockstatus, lockmgr_assert β the whole lockmgr family of functions. one big happy chaotic family reunion. π«Ά
β¨ SYNOPSIS β¨
#include <sys/types.h>
#include <sys/lock.h>
#include <sys/lockmgr.h>
void
lockinit(struct lock *lkp, int prio, const char *wmesg, int timo,
int flags);
void
lockdestroy(struct lock *lkp);
int
lockmgr(struct lock *lkp, u_int flags, struct mtx *ilk);
int
lockmgr_args(struct lock *lkp, u_int flags, struct mtx *ilk,
const char *wmesg, int prio, int timo);
int
lockmgr_args_rw(struct lock *lkp, u_int flags, struct rwlock *ilk,
const char *wmesg, int prio, int timo);
void
lockmgr_disown(struct lock *lkp);
void
lockmgr_printinfo(const struct lock *lkp);
int
lockmgr_recursed(const struct lock *lkp);
int
lockmgr_rw(struct lock *lkp, u_int flags, struct rwlock *ilk);
int
lockstatus(const struct lock *lkp);
options INVARIANTS
options INVARIANT_SUPPORT
void
lockmgr_assert(const struct lock *lkp, int what);
β¨ DESCRIPTION β¨
π· lockinit()
the lockinit() function is how you birth a lock. it must be called before you do literally anything else with that lock β no cap, it's the prerequisites-before-the-prereqs situation. π¦
its arguments are:
lkpβ a pointer to the lock you wanna initialize.prioβ the priority passed tosleep(9).wmesgβ the lock message. used for both debugging output andsleep(9). multitasking queen.timoβ the timeout value passed tosleep(9).flagsβ the flags the lock gets initialized with:LK_CANRECURSEβ allow recursive exclusive locks. yes bestie, go off, lock it again.LK_NOPROFILEβ disable lock profiling for this lock. no stats for you.LK_NOSHAREβ allow exclusive locks only. shared is not an option here.LK_NOWITNESSβ tellwitness(4)to ignore this lock. it's not on speaking terms with witness.LK_NODUPβwitness(4)should log messages about duplicate locks being acquired.LK_QUIETβ disablektr(4)logging for this lock. shhh.LK_TIMELOCKβ usetimoduring a sleep; otherwise,0is used.
π· lockdestroy()
the lockdestroy() function destroys a lock, and β plot twist β even though it's called in a whole bunch of places in the kernel, it currently does nothing. it's the friend who says "omw" and then never leaves the house. π
π· lockmgr() and lockmgr_rw()
these two handle general locking functionality inside the kernel, including support for shared and exclusive locks, plus recursion. they can also upgrade and downgrade locks, which is the glow-up/downgrade arc of locking. β¨
their arguments are:
lkpβ a pointer to the lock you want to manipulate.flagsβ flags indicating what action is to be taken:LK_SHAREDβ acquire a shared lock. if an exclusive lock is currently held,EDEADLKwill be returned.LK_EXCLUSIVEβ acquire an exclusive lock. if an exclusive lock is already held andLK_CANRECURSEis not set, the system willpanic(9). πLK_DOWNGRADEβ downgrade an exclusive lock to a shared lock. downgrading a shared lock is not permitted. if an exclusive lock has been recursed, the system willpanic(9).LK_UPGRADEβ upgrade a shared lock to an exclusive lock. if this call fails, the shared lock is lost, even if theLK_NOWAITflag is specified. during the upgrade, the shared lock could be temporarily dropped. attempts to upgrade an exclusive lock will cause apanic(9).LK_TRYUPGRADEβ try to upgrade a shared lock to an exclusive lock. failing to upgrade does not result in the dropping of the shared lock ownership. respectful king.LK_RELEASEβ release the lock. releasing a lock that is not held can cause apanic(9).LK_DRAINβ wait for all activity on the lock to end, then mark it decommissioned. this is used before freeing a lock that is part of a piece of memory that is about to be freed. (as documented in<sys/lockmgr.h>.)LK_SLEEPFAILβ fail if the operation has slept.LK_NOWAITβ do not allow the call to sleep. this can be used to test the lock.LK_NOWITNESSβ skip thewitness(4)checks for this instance.LK_CANRECURSEβ allow recursion on an exclusive lock. for every lock there must be a release. (receipts required.)LK_INTERLOCKβ unlock the interlock (which should be locked already).LK_NODDLKTREATβ normally,lockmgr()postpones serving further shared requests for a shared-locked lock if there is an exclusive waiter, to avoid exclusive lock starvation. but, if the thread requesting the shared lock already owns a sharedlockmgrlock, the request is granted even in presence of the parallel exclusive lock request, which is done to avoid deadlocks with recursive shared acquisition.
the LK_NODDLKTREAT flag can only be used by code which requests a shared non-recursive lock. the flag allows exclusive requests to preempt the current shared request even if the current thread owns shared locks. this is safe since shared lock is guaranteed to not recurse, and is used when the thread is known to hold unrelated shared locks, to not cause unnecessary starvation. an example is vp locking in VFS lookup(9), when dvp is already locked.
ilkβ an interlock mutex for controlling group access to the lock. ifLK_INTERLOCKis specified,lockmgr()andlockmgr_rw()assumeilkis currently owned and not recursed, and will return it unlocked. seemtx_assert(9).
π· lockmgr_args() and lockmgr_args_rw()
these work exactly like lockmgr() and lockmgr_rw() but accept a wmesg, timo and prio on a per-instance basis. the specified values will override the default ones, but this can still be used passing, respectively, LK_WMESG_DEFAULT, LK_PRIO_DEFAULT and LK_TIMO_DEFAULT. customize your lock, it's your moment. π
π· lockmgr_disown()
switches the owner from the current thread to be LK_KERNPROC, if the lock is already held. it's giving "i'm not the parent anymore, you are." π§
π· lockmgr_printinfo()
prints debugging information about the lock. used primarily by VOP_PRINT(9) functions. it's the lock's tell-all. π
π· lockmgr_recursed()
returns true if the lock is recursed, 0 otherwise. simple. clean. slay.
π· lockstatus()
returns the status of the lock in relation to the current thread. it's not about the lock's feelings, it's about your relationship with the lock. π
π· lockmgr_assert() β only with INVARIANTS
when compiled with options INVARIANTS and options INVARIANT_SUPPORT, the lockmgr_assert() function tests lkp for the assertions specified in what, and panics if they are not met. dramatic? yes. necessary? also yes.
one of the following assertions must be specified:
KA_LOCKEDβ assert that the current thread has either a shared or an exclusive lock on thelkplock pointed to by the first argument.KA_SLOCKEDβ assert that the current thread has a shared lock on thelkplock pointed to by the first argument.KA_XLOCKEDβ assert that the current thread has an exclusive lock on thelkplock pointed to by the first argument.KA_UNLOCKEDβ assert that the current thread has no lock on thelkplock pointed to by the first argument.
in addition, one of the following optional assertions can be used with either a KA_LOCKED, KA_SLOCKED, or KA_XLOCKED assertion:
KA_RECURSEDβ assert that the current thread has a recursed lock onlkp.KA_NOTRECURSEDβ assert that the current thread does not have a recursed lock onlkp.
β¨ RETURN VALUES β¨
the lockmgr() and lockmgr_rw() functions return 0 on success and non-zero on failure.
the lockstatus() function returns:
LK_EXCLUSIVEβ an exclusive lock is held by the current thread. it's yours, bestie. πLK_EXCLOTHERβ an exclusive lock is held by someone other than the current thread. not yours, bestie. πLK_SHAREDβ a shared lock is held.0β the lock is not held by anyone.
β¨ ERRORS β¨
lockmgr() and lockmgr_rw() fail if:
[EBUSY]βLK_FORCEUPGRADEwas requested and another thread had already requested a lock upgrade.[EBUSY]βLK_NOWAITwas set, and a sleep would have been required, orLK_TRYUPGRADEoperation was not able to upgrade the lock.[ENOLCK]βLK_SLEEPFAILwas set andlockmgr()orlockmgr_rw()did sleep.[EINTR]βPCATCHwas set in the lock priority, and a signal was delivered during a sleep. note theERESTARTerror below.[ERESTART]βPCATCHwas set in the lock priority, a signal was delivered during a sleep, and the system call is to be restarted.[EWOULDBLOCK]β a non-zero timeout was given, and the timeout expired.
β¨ LOCKS β¨
if LK_INTERLOCK is passed in the flags argument to lockmgr() or lockmgr_rw(), the ilk must be held prior to calling lockmgr() or lockmgr_rw(), and will be returned unlocked.
β οΈ upgrade attempts that fail result in the loss of the lock that is currently held. also, it is invalid to upgrade an exclusive lock, and a panic(9) will be the result of trying. this is the "f around and find out" clause. π
β¨ SEE ALSO β¨
condvar(9), locking(9), mtx_assert(9), mutex(9), panic(9), rwlock(9), sleep(9), sx(9), VOP_PRINT(9)
β¨ AUTHORS β¨
this manual page was written by Chad David <davidc@acns.ab.ca>. thank you Chad, you ate. π
and that's the whole page, bestie!! π this beauty is from Debian, dated November 17, 2017, and it's the LOCK(9) page from the Kernel Developer's Manual. go forth and lock responsibly β every lock needs its release, periodt. πβ¨π¦
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (C) 2002 Chad David <davidc@acns.ab.ca>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms