RMLOCK(9)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

🌸 RMLOCK(9) β€” kernel reader/writer locks that are literally built different 🌸

okay bestie, welcome to the RMLOCK(9) page from the Kernel Developer's Manual πŸ’… this is the read-mostly lock of your dreams β€” rmlock, plus all its little friends rm_init, rm_init_flags, rm_destroy, rm_rlock, rm_try_rlock, rm_wlock, rm_runlock, rm_wunlock, rm_wowned, rm_sleep, rm_assert, RM_SYSINIT, RM_SYSINIT_FLAGS, rms_init, rms_destroy, rms_rlock, rms_wlock, rms_runlock, and rms_wunlock. the vibe? a kernel reader/writer lock optimized for read-mostly access patterns ✨ like, it's giving main character energy for data that nobody ever writes to.


✨ SYNOPSIS ✨

#include <sys/param.h>
#include <sys/lock.h>
#include <sys/rmlock.h>

void
rm_init(struct rmlock *rm, const char *name);

void
rm_init_flags(struct rmlock *rm, const char *name, int opts);

void
rm_destroy(struct rmlock *rm);

void
rm_rlock(struct rmlock *rm, struct rm_priotracker* tracker);

int
rm_try_rlock(struct rmlock *rm, struct rm_priotracker* tracker);

void
rm_wlock(struct rmlock *rm);

void
rm_runlock(struct rmlock *rm, struct rm_priotracker* tracker);

void
rm_wunlock(struct rmlock *rm);

int
rm_wowned(const struct rmlock *rm);

int
rm_sleep(void *wchan, struct rmlock *rm, int priority,
    const char *wmesg, int timo);

options INVARIANTS
options INVARIANT_SUPPORT

void
rm_assert(struct rmlock *rm, int what);

#include <sys/kernel.h>

RM_SYSINIT(name, struct rmlock *rm, const char *desc);

RM_SYSINIT_FLAGS(name, struct rmlock *rm, const char *desc, int flags);

void
rms_init(struct rmslock *rms, const char *name);

void
rms_destroy(struct rmslock *rms);

void
rms_rlock(struct rmslock *rms);

void
rms_wlock(struct rmslock *rms);

void
rms_runlock(struct rmslock *rms);

void
rms_wunlock(struct rmslock *rms);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so read-mostly locks let multiple threads share access to your protected data, or let one single thread have exclusive access. the shared-access girlies are called readers (obvi, they only read the protected data) and the exclusive-access one is called a writer (she modifies the protected data). iconic division of labor πŸ’…

read-mostly locks are designed to be efficient for locks that are almost exclusively used as reader locks, so use them for protecting data that rarely changes. and heads up: acquiring an exclusive lock after the lock has been locked for shared access? that's an expensive operation. like, expensive expensive 😭

normal read-mostly locks are similar to rwlock(9) locks and follow the same lock ordering rules as rwlock(9) locks. read-mostly locks have full priority propagation like mutexes. but unlike rwlock(9), read-mostly locks propagate priority to both readers and writers. how? through the rm_priotracker structure argument you hand to rm_rlock() and rm_runlock() 🫢 readers can recurse if the lock was initialized with the RM_RECURSE option β€” however, writers are never allowed to recurse. periodt.

sleeping for writers can be allowed by passing RM_SLEEPABLE to rm_init_flags(). that changes lock ordering rules to be the same as for sx(9) locks. they do not propagate priority to writers, but they do propagate priority to readers. note that readers are not permitted to sleep regardless of the flag ⭐

sleepable read-mostly locks (created with rms_init()) allow sleeping for both readers and writers, but don't do priority propagation for either. they follow sx(9) lock ordering. slay.

πŸŽ€ Macros and Functions πŸŽ€

  • rm_init(struct rmlock *rm, const char *name) β€” initialize the read-mostly lock rm. the name description is used solely for debugging purposes. this function must be called before any other operations on the lock, no cap.
  • rm_init_flags(struct rmlock *rm, const char *name, int opts) β€” similar to rm_init(), initializes the read-mostly lock rm with a set of optional flags. the opts argument contains one or more of these flags:
    • RM_NOWITNESS β€” instruct witness(4) to ignore this lock.
    • RM_RECURSE β€” allow threads to recursively acquire shared locks for rm.
    • RM_SLEEPABLE β€” create a sleepable read-mostly lock.
    • RM_NEW β€” if the kernel has been compiled with option INVARIANTS, rm_init_flags() will assert that the rm has not been initialized multiple times without intervening calls to rm_destroy() unless this option is specified.
    • RM_DUPOK β€” witness(4) should not log messages about duplicate locks being acquired.
  • rm_rlock(struct rmlock *rm, struct rm_priotracker* tracker) β€” lock rm as a reader, using tracker to track read owners of a lock for priority propagation. this data structure is only used internally by rmlock and must persist until rm_runlock() has been called. it can be allocated on the stack since readers cannot sleep 🧚 if any thread holds this lock exclusively, the current thread blocks, and its priority is propagated to the exclusive holder. if the lock was initialized with the RM_RECURSE option, rm_rlock() can be called when the current thread has already acquired reader access on rm.
  • rm_try_rlock(struct rmlock *rm, struct rm_priotracker* tracker) β€” try to lock rm as a reader. rm_try_rlock() returns 0 if the lock cannot be acquired immediately; otherwise the lock will be acquired and a non-zero value will be returned. note that rm_try_rlock() may fail even while the lock is not currently held by a writer 😭 if the lock was initialized with the RM_RECURSE option, rm_try_rlock() will succeed if the current thread has already acquired reader access.
  • rm_wlock(struct rmlock *rm) β€” lock rm as a writer. if there are any shared owners of the lock, the current thread blocks. and rm_wlock() cannot be called recursively 🚫
  • rm_runlock(struct rmlock *rm, struct rm_priotracker* tracker) β€” releases a shared lock previously acquired by rm_rlock(). the tracker argument must match the tracker argument used for acquiring the shared lock.
  • rm_wunlock(struct rmlock *rm) β€” releases an exclusive lock previously acquired by rm_wlock().
  • rm_destroy(struct rmlock *rm) β€” destroys a lock previously initialized with rm_init(). the rm lock must be unlocked ⚠️
  • rm_wowned(const struct rmlock *rm) β€” returns a non-zero value if the current thread owns an exclusive lock on rm.
  • rm_sleep(void *wchan, struct rmlock *rm, int priority, const char *wmesg, int timo) β€” atomically releases rm while waiting for an event. the rm lock must be exclusively locked. for more details on the parameters to this function, see sleep(9).
  • rm_assert(struct rmlock *rm, int what) β€” asserts that the rm lock is in the state specified by what. if the assertions are not true and the kernel is compiled with options INVARIANTS and options INVARIANT_SUPPORT, the kernel will panic πŸ’€ currently the following base assertions are supported:
    • RA_LOCKED β€” assert that current thread holds either a shared or exclusive lock of rm.
    • RA_RLOCKED β€” assert that current thread holds a shared lock of rm.
    • RA_WLOCKED β€” assert that current thread holds an exclusive lock of rm.
    • RA_UNLOCKED β€” assert that current thread holds neither a shared nor exclusive lock of rm.

in addition, one of the following optional flags may be specified with RA_LOCKED, RA_RLOCKED, or RA_WLOCKED:

  • RA_RECURSED β€” assert that the current thread holds a recursive lock of rm.
  • RA_NOTRECURSED β€” assert that the current thread does not hold a recursive lock of rm.
  • rms_init(struct rmslock *rms, const char *name) β€” initialize the sleepable read-mostly lock rms. the name description is used as the wmesg parameter to the msleep(9) routine. this function must be called before any other operations on the lock.
  • rms_rlock(struct rmlock *rm) β€” lock rms as a reader. if any thread holds this lock exclusively, the current thread blocks.
  • rms_wlock(struct rmslock *rms) β€” lock rms as a writer. if the lock is already taken, the current thread blocks. rms_wlock() cannot be called recursively 🚫
  • rms_runlock(struct rmslock *rms) β€” releases a shared lock previously acquired by rms_rlock().
  • rms_wunlock(struct rmslock *rms) β€” releases an exclusive lock previously acquired by rms_wlock().
  • rms_destroy(struct rmslock *rms) β€” destroys a lock previously initialized with rms_init(). the rms lock must be unlocked ⚠️
bestie note: RM_SYSINIT(name, struct rmlock *rm, const char *desc) and RM_SYSINIT_FLAGS(name, struct rmlock *rm, const char *desc, int flags) come from <sys/kernel.h> β€” they're in the synopsis up there, go look, i'm not retyping the whole thing πŸ’–

πŸ”₯ SEE ALSO πŸ”₯

locking(9), mutex(9), panic(9), rwlock(9), sema(9), sleep(9), sx(9)

the whole squad, fr fr πŸ‘‘


🌈 HISTORY 🌈

these functions appeared in FreeBSD 7.0. a whole era πŸ’…


πŸ’œ AUTHORS πŸ’œ

the rmlock facility was written by Stephan Uphoff. this manual page was written by Gleb Smirnoff for rwlock and modified to reflect rmlock by Stephan Uphoff. kings, both of them 🫢


πŸ’€ BUGS πŸ’€

  • the rmlock implementation is currently not optimized for single processor systems. ngl, awkward.
  • rm_try_rlock() can fail transiently even when there is no writer, while another reader updates the state on the local CPU. yeah. that's a thing. 😭
  • the rmlock implementation uses a single per CPU list shared by all rmlocks in the system. if rmlocks become popular, hashing to multiple per CPU queues may be needed to speed up the writer lock process.

okay that's the whole page, you're officially an rmlock girlie now πŸŽ€

and that's a wrap on this one, bestie β€” brought to you by Debian, dated April 12, 2021 βœ¨πŸ¦‹ go forth and lock responsibly πŸ’…

πŸ“¦ where this page comes from