LOCK(9)
π NAME π
okay so this is LOCK(9), and the whole lockmgr family of functions is living here: lockinit, lockdestroy, lockmgr, lockmgr_args, lockmgr_args_rw, lockmgr_disown, lockmgr_disowned, lockmgr_lock_flags, lockmgr_printinfo, lockmgr_recursed, lockmgr_rw, lockmgr_slock, lockmgr_unlock, lockmgr_xlock, lockstatus, and lockmgr_assert. bestie, that's the whole squad π
β¨
β¨ SYNOPSIS β¨
first up, the required includes and every function signature β copying these exactly because the compiler does not care about vibes π
#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);
int
lockmgr_disowned(const struct lock *lkp);
int
lockmgr_lock_flags(struct lock *lkp, u_int flags,
struct lock_object *ilk, const char *file, int line);
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
lockmgr_slock(struct lock *lkp, u_int flags, const char *file,
int line);
int
lockmgr_unlock(struct lock *lkp);
int
lockmgr_xlock(struct lock *lkp, u_int flags, const char *file,
int line);
int
lockstatus(const struct lock *lkp);
options INVARIANTS
options INVARIANT_SUPPORT
void
lockmgr_assert(const struct lock *lkp, int what);
π¦ DESCRIPTION π¦
so basically this whole page is lock lore, and every function has its own little job. stay with me, bestie π«Ά
π± lockinit() β the setup era
The lockinit() function is used to initialize a lock. It must be called before any operation can be performed on a lock. Its arguments are:
lkpβ A pointer to the lock to initialize.prioβ The priority passed tosleep(9).wmesgβ The lock message. This is used for both debugging output andsleep(9).timoβ The timeout value passed tosleep(9).flagsβ The flags the lock is to be initialized with:LK_CANRECURSEβ Allow recursive exclusive locks.LK_NOPROFILEβ Disable lock profiling for this lock.LK_NOSHAREβ Allow exclusive locks only.LK_NOWITNESSβ Instructwitness(4)to ignore this lock.LK_NODUPβwitness(4)should log messages about duplicate locks being acquired.LK_QUIETβ Disablektr(4)logging for this lock.
π§Ή lockdestroy() β the cleanup-ish one
The lockdestroy() function is used to destroy a lock, and while it is called in a number of places in the kernel, it currently does nothing. yes, literally nothing. iconic behavior, lowkey ππ
π lockmgr() and lockmgr_rw() β the main characters
The lockmgr() and lockmgr_rw() functions handle general locking functionality within the kernel, including support for shared and exclusive locks, and recursion. lockmgr() and lockmgr_rw() are also able to upgrade and downgrade locks.
Their arguments are:
lkpβ A pointer to the lock 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 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. The failure to upgrade does not result in the dropping of the shared lock ownership.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 operation has slept.LK_NOWAITβ Do not allow the call to sleep. This can be used to test the lock.LK_TIMELOCKβ Usetimoduring a sleep; otherwise,0is used.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.LK_INTERLOCKβ Unlock the interlock (which should be locked already).LK_NODDLKTREATβ Normally,lockmgr()postpones serving further shared requests for shared-locked lock if there is 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 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 thread is known to held 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() β per-instance vibes
The lockmgr_args() and lockmgr_args_rw() functions work like lockmgr() and lockmgr_rw() but accepting 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.
π lockmgr_lock_flags() β tracing-friendly
The lockmgr_lock_flags() function works like lockmgr() but accepts explicit file and line arguments for lock tracing.
β‘ lockmgr_slock(), lockmgr_xlock(), and lockmgr_unlock() β the lightweight besties
The lockmgr_slock(), lockmgr_xlock(), and lockmgr_unlock() functions are lightweight entry points that function like lockmgr() for the LK_SHARED, LK_EXCLUSIVE, and LK_RELEASE operations respectively. They provide functionality similar to sx(9) locks in that none of the additional lockmgr(9) features are supported. Specifically, these functions do not support unlocking interlocks, the LK_SLEEPFAIL flag, or locks with shared locking disabled via LK_NOSHARE. They also accept explicit file and line arguments for lock tracing.
π«Ά lockmgr_disown() and lockmgr_disowned() β owner swaps
The lockmgr_disown() function switches the owner from the current thread to be LK_KERNPROC, if the lock is already held.
The lockmgr_disowned() function returns true or false according to whether the lock is held by LK_KERNPROC.
π¨οΈ lockmgr_printinfo() β debug print moment
The lockmgr_printinfo() function prints debugging information about the lock. It is used primarily by VOP_PRINT(9) functions.
π lockmgr_recursed() β recursion check
The lockmgr_recursed() function returns true if the lock is recursed, 0 otherwise.
π lockstatus() β what's the situation?
The lockstatus() function returns the status of the lock in relation to the current thread.
π
lockmgr_assert() β the INVARIANTS girlies
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. 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 an 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.LK_EXCLOTHERβ An exclusive lock is held by someone other than the current thread.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.[EDEADLK]β A shared lock was attempted while the thread already held the exclusive 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.
Ok so back to the <pink>LK_INTERLOCK</pink> tea β β if you pass it in the flags argument to lockmgr() or lockmgr_rw(), then bestie, that ilk has got to be held before you call lockmgr() or lockmgr_rw(). And when the call comes back? It is returned unlocked, no cap. πβ¨
β οΈ Okay this next part is giving main-character-level drama so pay attention: upgrade attempts that fail result in the loss of the lock that is currently held. Like, you tried to glow up your lock and instead you just lost it entirely. π
Also, and I cannot stress this enough: it is invalid to upgrade an exclusive lock. Not "discouraged", not "a little sketchy" β invalid. And the result of trying is a panic(9). So yeah, that's an instant system-meltdown vibe check, bestie, please do not. ππ₯
π¦ SEE ALSO π¦
Like the whole friend group shows up here, periodt:
witness(4)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>. Iconic of him honestly, ty for your service π«Ά
And that's a wrap on LOCK(9)! This page is from the Debian crowd, dated June 21, 2024, and it's filed under LOCK(9) in section 9 of the manual. Go forth and lock responsibly, bestie π
β¨π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (C) 2002 Chad David <davidc@acns.ab.ca>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms