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 to sleep(9).
  • wmesg β€” the lock message. used for both debugging output and sleep(9). multitasking queen.
  • timo β€” the timeout value passed to sleep(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 β€” tell witness(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 β€” disable ktr(4) logging for this lock. shhh.
    • LK_TIMELOCK β€” use timo during a sleep; otherwise, 0 is 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, EDEADLK will be returned.
    • LK_EXCLUSIVE β€” acquire an exclusive lock. if an exclusive lock is already held and LK_CANRECURSE is not set, the system will panic(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 will panic(9).
    • LK_UPGRADE β€” upgrade a shared lock to an exclusive lock. if this call fails, the shared lock is lost, even if the LK_NOWAIT flag is specified. during the upgrade, the shared lock could be temporarily dropped. attempts to upgrade an exclusive lock will cause a panic(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 a panic(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 the witness(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 shared lockmgr lock, 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. if LK_INTERLOCK is specified, lockmgr() and lockmgr_rw() assume ilk is currently owned and not recursed, and will return it unlocked. see mtx_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 the lkp lock pointed to by the first argument.
  • KA_SLOCKED β€” assert that the current thread has a shared lock on the lkp lock pointed to by the first argument.
  • KA_XLOCKED β€” assert that the current thread has an exclusive lock on the lkp lock pointed to by the first argument.
  • KA_UNLOCKED β€” assert that the current thread has no lock on the lkp lock 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 on lkp.
  • KA_NOTRECURSED β€” assert that the current thread does not have a recursed lock on lkp.

✨ 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_FORCEUPGRADE was requested and another thread had already requested a lock upgrade.
  • [EBUSY] β€” LK_NOWAIT was set, and a sleep would have been required, or LK_TRYUPGRADE operation was not able to upgrade the lock.
  • [ENOLCK] β€” LK_SLEEPFAIL was set and lockmgr() or lockmgr_rw() did sleep.
  • [EINTR] β€” PCATCH was set in the lock priority, and a signal was delivered during a sleep. note the ERESTART error below.
  • [ERESTART] β€” PCATCH was 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