WITNESS(4)
๐ WITNESS(4) โ the lock validation facility, bestie edition ๐
okay so this is a kernel interfaces manual page, and honestly? witness is the most main-character debugging tool in the whole kernel. let me tell you everything ๐
โจ SYNOPSIS โจ
options WITNESS
options WITNESS_COUNT
options WITNESS_KDB
options WITNESS_NO_VNODE
options WITNESS_SKIPSPIN
like, that's the whole vibe right there. five options lines and you're in ๐ซถ
๐ธ DESCRIPTION ๐ธ
okay so the witness module is literally the lock babysitter. it keeps track of the locks acquired and released by each thread. it also keeps track of the order in which locks are acquired with respect to each other. two lists, one job ๐
every single time a lock is acquired, witness uses these two lists to verify that a lock is not being acquired in the wrong order. and if a lock order violation is detected? a message is output to the kernel console or log detailing the locks involved and the locations in question. it's giving receipts, fr fr ๐
witness can also be configured to drop into the kernel debugger when an order violation occurs. iconic behavior honestly.
the witness code also checks various other conditions, like:
- verifying that one does not recurse on a non-recursive lock
- verifying that one does not attempt an upgrade on a shared lock held by another thread
and if any of these checks fail, then the kernel will panic. ๐ญ no cap. not a vibe, but that's the deal.
๐งฎ the WITNESS_COUNT situation
the WITNESS_COUNT kernel option controls the maximum number of witness entries that are tracked in the kernel.
- the maximum number of entries can be queried via the
debug.witness.witness_countsysctl - it can also be set from the
loader(8)via thedebug.witness.witness_countenvironment variable
slay, two ways to do the thing โจ
๐ฆ WITNESS_NO_VNODE โ the vnode peace treaty
the WITNESS_NO_VNODE kernel option tells witness to ignore locking issues between vnode(9) objects. like, they're on a break. let them live ๐
โญ the kdb flag โ how do we get into the debugger
the flag that controls whether or not the kernel debugger is entered when a lock order violation is detected can be set in a variety of ways:
- by default, the flag is off
- but if the
WITNESS_KDBkernel option is specified, then the flag will default to on - it can also be set from the
loader(8)via thedebug.witness.kdbenvironment variable - or after the kernel has booted via the
debug.witness.kdbsysctl
if the flag is set to zero, then the debugger will not be entered. if the flag is non-zero, then the debugger will be entered. that's it, that's the rule ๐
๐ WITNESS_SKIPSPIN โ spin mutexes can chill
the witness code can also be configured to skip all checks on spin mutexes.
- by default, this flag defaults to off
- but it can be turned on by specifying the
WITNESS_SKIPSPINkernel option - the flag can also be set via the
loader(8)environment variabledebug.witness.skipspin - if the variable is set to a non-zero value, then spin mutexes are skipped
and once the kernel has booted, the status of this flag can be examined but not set via the read-only sysctl debug.witness.skipspin. read-only, bestie. look, don't touch ๐ซ
๐ debug.witness.watch โ how involved is witness, exactly
the sysctl debug.witness.watch specifies the level of witness involvement in the system. the values:
1โ specifies thatwitnessis enabled0โ specifies thatwitnessis disabled, but that can be enabled again. this will maintain a small amount of overhead in the system-1โ specifies thatwitnessis disabled permanently and cannot be enabled again
the sysctl debug.witness.watch can be set via loader(8). periodt ๐
๐ข debug.witness.output_channel โ where do the warnings go
the sysctl debug.witness.output_channel specifies the output channel used to display warnings emitted by witness. the possible values are:
'console'โ indicating that warnings are to be printed to the system console'log'โ indicating that warnings are to be logged vialog(9)'none'
this sysctl can be set via loader(8). pick your aesthetic ๐
๐ฅ๏ธ the ddb commands โ bonus content, iykyk
the witness code also provides three extra ddb(4) commands if both witness and ddb(4) are compiled into the kernel:
show locks [thread]โ outputs the list of locks held by a thread to the kernel console along with the filename and line number at which each lock was last acquired by the thread. the optional thread argument may be either a TID, PID, or pointer to a thread structure. if thread is not specified, then the locks held by the current thread are displayed. ๐show all locksโ outputs the list of locks held by all threads in the system to the kernel console. everybody's locks, all at once, it's giving group chat screenshot ๐ซถshow witnessโ dumps the current order list to the kernel console. the code first displays the lock order tree for all of the sleep locks. then it displays the lock order tree for all of the spin locks. finally, it displays a list of locks that have not yet been acquired. three courses, chef's kiss ๐
๐ SEE ALSO ๐
ddb(4), loader(8), sysctl(8), mutex(9)
go read them, bestie, they're literally all connected ๐ง
๐ HISTORY ๐
the witness code first appeared in BSD/OS 5.0 and was imported from there into FreeBSD 5.0. a whole journey, honestly ๐
and that's the page!! this one's from the Debian crew, dated March 5, 2025, and yes the manual name is right there in the title: WITNESS(4) ๐โจ go forth and never violate a lock order again, ily ๐ซถ๐
๐ฆ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 2001 John H. Baldwin <jhb@FreeBSD.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms