_UMTX_OP(2)
๐ _UMTX_OP(2) โ System Calls Manual
โจ NAME โจ
_umtx_op โ interface for implementation of userspace threading synchronization primitives
Okay bestie, this is the syscall that gives userspace threading libraries their kernel backup. Slay.
๐ซถ LIBRARY
Standard C Library (libc, -lc)
๐ SYNOPSIS
#include <sys/types.h>
#include <sys/umtx.h>
int
_umtx_op(void *obj, int op, u_long val, void *uaddr, void *uaddr2);
๐ฆ DESCRIPTION
The _umtx_op() system call provides kernel support for userspace implementation of the threading synchronization primitives. The 1:1 Threading Library (libthr, -lthr) uses the syscall to implement IEEE Std 1003.1-2001 (โPOSIX.1โ) pthread locks, like mutexes, condition variables and so on. Periodt.
๐ STRUCTURES
The operations, performed by the _umtx_op() syscall, operate on userspace objects which are described by the following structures. Reserved fields and paddings are omitted. All objects require ABI-mandated alignment, but this is not currently enforced consistently on all architectures.
The following flags are defined for flag fields of all structures:
USYNC_PROCESS_SHAREDโ Allow selection of the process-shared sleep queue for the thread sleep container, when the lock ownership cannot be granted immediately, and the operation must sleep. The process-shared or process-private sleep queue is selected based on the attributes of the memory mapping which contains the first byte of the structure, seemmap(2). Otherwise, if the flag is not specified, the process-private sleep queue is selected regardless of the memory mapping attributes, as an optimization.
See the โSLEEP QUEUESโ subsection below for more details on sleep queues.
๐ง Mutex
struct umutex {
volatile lwpid_t m_owner;
uint32_t m_flags;
uint32_t m_ceilings[2];
uintptr_t m_rb_lnk;
};
The m_owner field is the actual lock. It contains either the thread identifier of the lock owner in the locked state, or zero when the lock is unowned. The highest bit set indicates that there is contention on the lock. The constants are defined for special values:
UMUTEX_UNOWNEDโ Zero, the value stored in the unowned lock.UMUTEX_CONTESTEDโ The contention indicator.UMUTEX_RB_OWNERDEADโ A thread owning the robust mutex terminated. The mutex is in unlocked state.UMUTEX_RB_NOTRECOVโ The robust mutex is in a non-recoverable state. It cannot be locked until reinitialized.
The m_flags field may contain the following umutex-specific flags, in addition to the common flags:
UMUTEX_PRIO_INHERITโ Mutex implements Priority Inheritance protocol.UMUTEX_PRIO_PROTECTโ Mutex implements Priority Protection protocol.UMUTEX_ROBUSTโ Mutex is robust, as described in the โROBUST UMUTEXESโ section below.UMUTEX_NONCONSISTENTโ Robust mutex is in a transient non-consistent state. Not used by kernel.
In the manual page, mutexes not having UMUTEX_PRIO_INHERIT and UMUTEX_PRIO_PROTECT flags set, are called normal mutexes. Each type of mutex (normal, priority-inherited, and priority-protected) has a separate sleep queue associated with the given key.
For priority protected mutexes, the m_ceilings array contains priority ceiling values. The m_ceilings[0] is the ceiling value for the mutex, as specified by IEEE Std 1003.1-2008 (โPOSIX.1โ) for the Priority Protected mutex protocol. The m_ceilings[1] is used only for the unlock of a priority protected mutex, when unlock is done in an order other than the reversed lock order. In this case, m_ceilings[1] must contain the ceiling value for the last locked priority protected mutex, for proper priority reassignment. If, instead, the unlocking mutex was the last priority propagated mutex locked by the thread, m_ceilings[1] should contain -1. This is required because kernel does not maintain the ordered lock list.
๐ธ Condition variable
struct ucond {
volatile uint32_t c_has_waiters;
uint32_t c_flags;
uint32_t c_clockid;
};
A non-zero c_has_waiters value indicates that there are in-kernel waiters for the condition, executing the UMTX_OP_CV_WAIT request.
The c_flags field contains flags. Only the common flags (USYNC_PROCESS_SHARED) are defined for ucond.
The c_clockid member provides the clock identifier to use for timeout, when the UMTX_OP_CV_WAIT request has both the CVWAIT_CLOCKID flag and the timeout specified. Valid clock identifiers are a subset of those for clock_gettime(2):
CLOCK_MONOTONICCLOCK_MONOTONIC_FASTCLOCK_MONOTONIC_PRECISECLOCK_PROFCLOCK_REALTIMECLOCK_REALTIME_FASTCLOCK_REALTIME_PRECISECLOCK_SECONDCLOCK_TAICLOCK_UPTIMECLOCK_UPTIME_FASTCLOCK_UPTIME_PRECISECLOCK_VIRTUAL
๐ Reader/writer lock
struct urwlock {
volatile int32_t rw_state;
uint32_t rw_flags;
uint32_t rw_blocked_readers;
uint32_t rw_blocked_writers;
};
The rw_state field is the actual lock. It contains both the flags and counter of the read locks which were granted. Names of the rw_state bits are following:
URWLOCK_WRITE_OWNERโ Write lock was granted.URWLOCK_WRITE_WAITERSโ There are write lock waiters.URWLOCK_READ_WAITERSโ There are read lock waiters.URWLOCK_READER_COUNT(c)โ Returns the count of currently granted read locks.
At any given time there may be only one thread to which the writer lock is granted on the struct rwlock, and no threads are granted read lock. Or, at the given time, up to URWLOCK_MAX_READERS threads may be granted the read lock simultaneously, but write lock is not granted to any thread.
The following flags for the rw_flags member of struct urwlock are defined, in addition to the common flags:
URWLOCK_PREFER_READERโ If specified, immediately grant read lock requests whenurwlockis already read-locked, even in presence of unsatisfied write lock requests. By default, if there is a write lock waiter, further read requests are not granted, to prevent unfair write lock waiter starvation.
The rw_blocked_readers and rw_blocked_writers members contain the count of threads which are sleeping in kernel, waiting for the associated request type to be granted. The fields are used by kernel to update the URWLOCK_READ_WAITERS and URWLOCK_WRITE_WAITERS flags of the rw_state lock after requesting thread was woken up.
๐ Semaphore
struct _usem2 {
volatile uint32_t _count;
uint32_t _flags;
};
The _count word represents a counting semaphore. A non-zero value indicates an unlocked (posted) semaphore, while zero represents the locked state. The maximal supported semaphore count is USEM_MAX_COUNT.
The _count word, besides the counter of posts (unlocks), also contains the USEM_HAS_WAITERS bit, which indicates that locked semaphore has waiting threads.
The USEM_COUNT() macro, applied to the _count word, returns the current semaphore counter, which is the number of posts issued on the semaphore.
The following bits for the _flags member of struct _usem2 are defined, in addition to the common flags:
USEM_NAMEDโ Flag is ignored by kernel.
โฐ Timeout parameter
struct _umtx_time {
struct timespec _timeout;
uint32_t _flags;
uint32_t _clockid;
};
Several _umtx_op() operations allow the blocking time to be limited, failing the request if it cannot be satisfied in the specified time period. The timeout is specified by passing either the address of struct timespec, or its extended variant, struct _umtx_time, as the uaddr2 argument of _umtx_op(). They are distinguished by the uaddr value, which must be equal to the size of the structure pointed to by uaddr2, casted to uintptr_t.
The _timeout member specifies the time when the timeout should occur. Legal values for clock identifier _clockid are shared with the clock_id argument to the clock_gettime(2) function, and use the same underlying clocks. The specified clock is used to obtain the current time value. Interval counting is always performed by the monotonic wall clock.
The _flags argument allows the following flags to further define the timeout behaviour:
โฐ UMTX_ABSTIME โฐ
okay so if this flag is set, _timeout is the absolute time โ like, a real clock value, not a "wait this many nanoseconds" vibe. The thread gets unblocked and the request fails when the specified clock value is equal to or exceeds _timeout. periodt.
If the flag is absent, the timeout value is relative โ meaning it's the amount of time measured by the monotonic wall clock from the moment the request started. iykyk ๐
๐ด SLEEP QUEUES ๐ด
When a locking request can't be satisfied immediately, the thread is typically put to sleep โ and sleeping here is a non-runnable state, terminated by the wake operation. Lock operations include a try variant which returns an error instead of sleeping if the lock can't be obtained. Also, _umtx_op() provides requests which explicitly put the thread to sleep. ๐
Wakes need to know which threads to make runnable, so sleeping threads are grouped into containers called sleep queues. A sleep queue is identified by a key, which for _umtx_op() is defined as the physical address of some variable. Note that the physical address is what's used โ which means that the same variable mapped multiple times will give one key value. This mechanism enables the construction of process-shared locks. โจ
A related attribute of the key is shareability. Some requests always interpret keys as private for the current process, creating sleep queues with the scope of the current process even if the memory is shared. Others either select the shareability automatically from the mapping attributes, or take additional input as the USYNC_PROCESS_SHARED common flag. This is done as an optimization, allowing the lock scope to be limited regardless of the kind of backing memory. ๐ซ
Only the address of the start byte of the variable specified as key is important for determining the corresponding sleep queue. The size of the variable does not matter โ so, for example, sleep on the same address interpreted as uint32_t and long on a little-endian 64-bit platform would collide. ๐
The last attribute of the key is the object type. The sleep queue to which a sleeping thread is assigned is an individual one for simple wait requests, mutexes, rwlocks, condvars and other primitives, even when the physical address of the key is the same. ๐
When waking up a limited number of threads from a given sleep queue, the highest priority threads that have been blocked the longest on the queue are selected. โญ
๐ก๏ธ ROBUST UMUTEXES ๐ก๏ธ
The robust umutexes are provided as a substrate for a userspace library to implement POSIX robust mutexes. A robust umutex must have the UMUTEX_ROBUST flag set. no cap.
On thread termination, the kernel walks two lists of mutexes. The two list head addresses must be provided by a prior call to the UMTX_OP_ROBUST_LISTS request. The lists are singly-linked. The link to the next element is provided by the m_rb_lnk member of the struct umutex. ๐
Robust list processing is aborted if the kernel finds a mutex with any of the following conditions:
- the
UMUTEX_ROBUSTflag is not set - not owned by the current thread, except when the mutex is pointed to by the
robust_inactivemember of thestruct umtx_robust_lists_params, registered for the current thread - the combination of mutex flags is invalid
- read of the umutex memory faults
- the list length limit described in
libthr(3)is reached
Every mutex in both lists is unlocked as if the UMTX_OP_MUTEX_UNLOCK request is performed on it, but instead of the UMUTEX_UNOWNED value, the m_owner field is written with the UMUTEX_RB_OWNERDEAD value. When a mutex in the UMUTEX_RB_OWNERDEAD state is locked by the kernel due to the UMTX_OP_MUTEX_TRYLOCK and UMTX_OP_MUTEX_LOCK requests, the lock is granted and the EOWNERDEAD error is returned. ๐ญ
Also, the kernel handles the UMUTEX_RB_NOTRECOV value of the m_owner field specially โ always returning the ENOTRECOVERABLE error for lock attempts, without granting the lock. ๐
๐๏ธ OPERATIONS ๐๏ธ
The following operations, requested by the op argument to the function, are implemented:
UMTX_OP_WAIT
Wait. The arguments for the request are:
- obj โ pointer to a variable of type
long. - val โ current value of the
*obj.
The current value of the variable pointed to by the obj argument is compared with val. If they're equal, the requesting thread is put to interruptible sleep until woken up or the optionally specified timeout expires. ๐ง
The comparison and sleep are atomic. In other words, if another thread writes a new value to *obj and then issues UMTX_OP_WAKE, the request is guaranteed to not miss the wakeup, which might otherwise happen between comparison and blocking. obsessed with the correctness ngl ๐
The physical address of the memory where the *obj variable is located is used as a key to index sleeping threads.
The read of the current value of the*objvariable is not guarded by barriers. In particular, it is the user's duty to ensure the lock acquire and release memory semantics, if theUMTX_OP_WAITandUMTX_OP_WAKErequests are used as a substrate for implementing a simple lock. bestie pls be careful ๐
The request is not restartable. An unblocked signal delivered during the wait always results in sleep interruption and the EINTR error. ๐ค
Optionally, a timeout for the request may be specified.
UMTX_OP_WAKE
Wake the threads possibly sleeping due to UMTX_OP_WAIT. The arguments for the request are:
- obj โ pointer to a variable, used as a key to find sleeping threads.
- val โ up to val threads are woken up by this request. Specify
INT_MAXto wake up all waiters. ๐
UMTX_OP_MUTEX_TRYLOCK
Try to lock umutex. The arguments to the request are:
- obj โ pointer to the umutex.
Operates same as the UMTX_OP_MUTEX_LOCK request, but returns EBUSY instead of sleeping if the lock cannot be obtained immediately. ๐
UMTX_OP_MUTEX_LOCK
Lock umutex. The arguments to the request are:
- obj โ pointer to the umutex.
Locking is performed by writing the current thread id into the m_owner word of the struct umutex. The write is atomic, preserves the UMUTEX_CONTESTED contention indicator, and provides the acquire barrier for lock entrance semantic. โจ
If the lock cannot be obtained immediately because another thread owns the lock, the current thread is put to sleep, with the UMUTEX_CONTESTED bit set before. Upon wake up, the lock conditions are re-tested. ๐
The request adheres to the priority protection or inheritance protocol of the mutex, specified by the UMUTEX_PRIO_PROTECT or UMUTEX_PRIO_INHERIT flag, respectively.
Optionally, a timeout for the request may be specified.
A request with a timeout specified is not restartable. An unblocked signal delivered during the wait always results in sleep interruption and the EINTR error. A request without a timeout specified is always restarted after return from a signal handler. ๐
UMTX_OP_MUTEX_UNLOCK
Unlock umutex. The arguments to the request are:
- obj โ pointer to the umutex.
Unlocks the mutex by writing the UMUTEX_UNOWNED (zero) value into the m_owner word of the struct umutex. The write is done with a release barrier, to provide lock leave semantic. ๐ฆ
If there are threads sleeping in the sleep queue associated with the umutex, one thread is woken up. If more than one thread sleeps in the sleep queue, the UMUTEX_CONTESTED bit is set together with the write of the UMUTEX_UNOWNED value into m_owner. ๐ฅ
The request adheres to the priority protection or inheritance protocol of the mutex, specified by the UMUTEX_PRIO_PROTECT or UMUTEX_PRIO_INHERIT flag, respectively. See the description of the m_ceilings member of the struct umutex structure for additional details of the request operation on the priority protected protocol mutex. ๐
UMTX_OP_SET_CEILING
Set ceiling for the priority protected umutex. The arguments to the request are:
- obj โ pointer to the umutex.
- val โ new ceiling value.
- uaddr โ address of a variable of type
uint32_t. If notNULLand the update was successful, the previous ceiling value is written to the location pointed to by uaddr.
The request locks the umutex pointed to by the obj parameter, waiting for the lock if it's not immediately available. After the lock is obtained, the new ceiling value val is written to the m_ceilings[0] member of the struct umutex, after which the umutex is unlocked. ๐
The locking does not adhere to the priority protect protocol, to conform to the POSIX requirements for the pthread_mutex_setprioceiling(3) interface. iykyk ๐
UMTX_OP_CV_WAIT
Wait for a condition. The arguments to the request are:
- obj โ pointer to the
struct ucond. - val โ request flags, see below.
- uaddr โ pointer to the umutex.
- uaddr2 โ optional pointer to a
struct timespecfor timeout specification.
The request must be issued by the thread owning the mutex pointed to by the uaddr argument. The c_hash_waiters member of the struct ucond, pointed to by the obj argument, is set to an arbitrary non-zero value, after which the uaddr mutex is unlocked (following the appropriate protocol), and the current thread is put to sleep on the sleep queue keyed by the obj argument. ๐ค
The operations are performed atomically. It is guaranteed to not miss a wakeup from UMTX_OP_CV_SIGNAL or UMTX_OP_CV_BROADCAST sent between the mutex unlock and putting the current thread on the sleep queue. โจ
Upon wakeup, if the timeout expired and no other threads are sleeping in the same sleep queue, the c_hash_waiters member is cleared. After wakeup, the uaddr umutex is not relocked. ๐
The following flags are defined:
CVWAIT_ABSTIMEโ timeout is absolute.CVWAIT_CLOCKIDโ clockid is provided.
Okay so about the timeout for this wait, bestie: optionally you can specify one, and itโs a little different from other requests โ the timeout value is specified directly by a struct timespec, pointed to by the uaddr2 argument. If the CVWAIT_CLOCKID flag is provided, the timeout uses the clock from the c_clockid member of the struct ucond, pointed to by the obj argument. Otherwise, CLOCK_REALTIME is used, regardless of the clock identifier possibly specified in the struct _umtx_time. If the CVWAIT_ABSTIME flag is supplied, the timeout specifies an absolute time value; otherwise it denotes a relative time interval. โจ
And important caveat, no cap: the request is not restartable. An unblocked signal delivered during the wait always results in sleep interruption and EINTR error. ๐ญ
UMTX_OP_CV_SIGNAL ๐
Wake up one condition waiter. The arguments to the request are:
objโ pointer tostruct ucond.
The request wakes up at most one thread sleeping on the sleep queue keyed by the obj argument. If the woken up thread was the last on the sleep queue, the c_has_waiters member of the struct ucond is cleared. ๐ซถ
UMTX_OP_CV_BROADCAST ๐ฃ
Wake up all condition waiters. The arguments to the request are:
objโ pointer tostruct ucond.
The request wakes up all threads sleeping on the sleep queue keyed by the obj argument. The c_has_waiters member of the struct ucond is cleared. ๐
UMTX_OP_WAIT_UINT ๐ข
Same as UMTX_OP_WAIT, but the type of the variable pointed to by obj is u_int (a 32-bit integer). Slay, tiny integer moment. โจ
UMTX_OP_RW_RDLOCK ๐
Read-lock a struct rwlock lock. The arguments to the request are:
objโ pointer to the lock (of typestruct rwlock) to be read-locked.valโ additional flags to augment locking behaviour. The valid flags in thevalargument are:URWLOCK_PREFER_READER
The request obtains the read lock on the specified struct rwlock by incrementing the count of readers in the rw_state word of the structure. If the URWLOCK_WRITE_OWNER bit is set in the word rw_state, the lock was granted to a writer which has not yet relinquished its ownership. In this case the current thread is put to sleep until it makes sense to retry. ๐ด
If the URWLOCK_PREFER_READER flag is set either in the rw_flags word of the structure, or in the val argument of the request, the presence of the threads trying to obtain the write lock on the same structure does not prevent the current thread from trying to obtain the read lock. Otherwise, if the flag is not set, and the URWLOCK_WRITE_WAITERS flag is set in rw_state, the current thread does not attempt to obtain read-lock. Instead it sets the URWLOCK_READ_WAITERS in the rw_state word and puts itself to sleep on corresponding sleep queue. Upon wakeup, the locking conditions are re-evaluated. ๐
Optionally, a timeout for the request may be specified.
The request is not restartable. An unblocked signal delivered during the wait always results in sleep interruption and EINTR error. ๐ญ
UMTX_OP_RW_WRLOCK ๐
Write-lock a struct rwlock lock. The arguments to the request are:
objโ pointer to the lock (of typestruct rwlock) to be write-locked.
The request obtains a write lock on the specified struct rwlock, by setting the URWLOCK_WRITE_OWNER bit in the rw_state word of the structure. If there is already a write lock owner, as indicated by the URWLOCK_WRITE_OWNER bit being set, or there are read lock owners, as indicated by the read-lock counter, the current thread does not attempt to obtain the write-lock. Instead it sets the URWLOCK_WRITE_WAITERS in the rw_state word and puts itself to sleep on corresponding sleep queue. Upon wakeup, the locking conditions are re-evaluated. โญ
Optionally, a timeout for the request may be specified.
The request is not restartable. An unblocked signal delivered during the wait always results in sleep interruption and EINTR error. ๐
UMTX_OP_RW_UNLOCK ๐
Unlock rwlock. The arguments to the request are:
objโ pointer to the lock (of typestruct rwlock) to be unlocked.
The unlock type (read or write) is determined by the current lock state. Note that the struct rwlock does not save information about the identity of the thread which acquired the lock. ๐
If there are pending writers after the unlock, and the URWLOCK_PREFER_READER flag is not set in the rw_flags member of the *obj structure, one writer is woken up, selected as described in the โSLEEP QUEUESโ subsection. If the URWLOCK_PREFER_READER flag is set, a pending writer is woken up only if there is no pending readers. ๐ธ
If there are no pending writers, or, in the case that the URWLOCK_PREFER_READER flag is set, then all pending readers are woken up by unlock. โจ
UMTX_OP_WAIT_UINT_PRIVATE ๐ง
Same as UMTX_OP_WAIT_UINT, but unconditionally select the process-private sleep queue. ๐
UMTX_OP_WAKE_PRIVATE ๐
Same as UMTX_OP_WAKE, but unconditionally select the process-private sleep queue. ๐ซถ
UMTX_OP_MUTEX_WAIT ๐ด
Wait for mutex availability. The arguments to the request are:
objโ address of the mutex.
Similarly to the UMTX_OP_MUTEX_LOCK, put the requesting thread to sleep if the mutex lock cannot be obtained immediately. The UMUTEX_CONTESTED bit is set in the m_owner word of the mutex to indicate that there is a waiter, before the thread is added to the sleep queue. Unlike the UMTX_OP_MUTEX_LOCK request, the lock is not obtained. ๐
The operation is not implemented for priority protected and priority inherited protocol mutexes. ๐ซ
Optionally, a timeout for the request may be specified.
A request with a timeout specified is not restartable. An unblocked signal delivered during the wait always results in sleep interruption and EINTR error. A request without a timeout automatically restarts if the signal disposition requested restart via the SA_RESTART flag in struct sigaction member sa_flags. ๐ญ
UMTX_OP_NWAKE_PRIVATE ๐ฃ
Wake up a batch of sleeping threads. The arguments to the request are:
objโ pointer to the array of pointers.valโ number of elements in the array pointed to byobj.
For each element in the array pointed to by obj, wakes up all threads waiting on the private sleep queue with the key being the byte addressed by the array element. ๐
UMTX_OP_MUTEX_WAKE ๐
Check if a normal umutex is unlocked and wake up a waiter. The arguments for the request are:
objโ pointer to the umutex.
If the m_owner word of the mutex pointed to by the obj argument indicates unowned mutex, which has its contention indicator bit UMUTEX_CONTESTED set, clear the bit and wake up one waiter in the sleep queue associated with the byte addressed by the obj, if any. Only normal mutexes are supported by the request. The sleep queue is always one for a normal mutex type. ๐
This request is deprecated in favor ofUMTX_OP_MUTEX_WAKE2since mutexes using it cannot synchronize their own destruction. That is, them_ownerword has already been set toUMUTEX_UNOWNEDwhen this request is made, so that another thread can lock, unlock and destroy the mutex (if no other thread uses the mutex afterwards). Clearing theUMUTEX_CONTESTEDbit may then modify freed memory. ๐๐ฅ
UMTX_OP_MUTEX_WAKE2 โญ
Check if a umutex is unlocked and wake up a waiter. The arguments for the request are:
objโ pointer to the umutex.valโ the umutex flags.
The request does not read the m_flags member of the struct umutex; instead, the val argument supplies flag information, in particular, to determine the sleep queue where the waiters are found for wake up. ๐
If the mutex is unowned, one waiter is woken up.
If the mutex memory cannot be accessed, all waiters are woken up.
If there is more than one waiter on the sleep queue, or there is only one waiter but the mutex is owned by a thread, the UMUTEX_CONTESTED bit is set in the m_owner word of the struct umutex. โจ
UMTX_OP_SEM2_WAIT ๐
Wait until semaphore is available. The arguments to the request are:
objโ pointer to the semaphore (of typestruct _usem2).uaddrโ size of the memory passed in via theuaddr2argument.uaddr2โ optional pointer to a structure of typestruct _umtx_time, which may be followed by a structure of typestruct timespec.
Put the requesting thread onto a sleep queue if the semaphore counter is zero. If the thread is put to sleep, the USEM_HAS_WAITERS bit is set in the _count word to indicate waiters. The function returns either due to _count indicating the semaphore is available (non-zero count due to post), or due to a wakeup. The return does not guarantee that the semaphore is available, nor does it consume the semaphore lock on successful return. ๐ฎโ๐จ
Optionally, a timeout for the request may be specified.
A request with non-absolute timeout value is not restartable. An unblocked signal delivered during such wait results in sleep interruption and EINTR error. ๐ญ
If UMTX_ABSTIME was not set, and the operation was interrupted and the caller passed in a uaddr2 large enough to hold a struct timespec following the initial struct _umtx_time, then the struct timespec is updated to contain the unslept amount. ๐ฐ๏ธ
UMTX_OP_SEM2_WAKE ๐
Wake up waiters on semaphore lock. The arguments to the request are:
objโ pointer to the semaphore (of typestruct _usem2).
So when the request fires off, it wakes up one waiter for the semaphore lock. ๐ซถ And like, it does not bump the semaphore lock count โ that's not its job, bestie. But here's the lil detail: if the USEM_HAS_WAITERS bit was set in the _count word, and the last sleeping thread got woken up, then the bit gets cleared. Slay, cleanup. โจ
UMTX_OP_SHMโ manages anonymous POSIX shared memory objects (peepshm_open(2)๐), which can be attached to a byte of physical memory and mapped into the process address space. These bad boys are what libthr uses under the hood to implement process-shared locks. ๐ง
The val argument tells you which sub-request of the UMTX_OP_SHM request you're asking for:
UMTX_SHM_CREATโ creates the anonymous shared memory object, which can be looked up later using the keyuaddr. If an object already exists for thatuaddrkey, you just get that one back instead of a shiny new object. The object's size is one page. On success, the file descriptor referencing the object is returned. That descriptor is yours to use for mapping the object withmmap(2), or for whatever other shared memory shenanigans you're into. ๐UMTX_SHM_LOOKUPโ same asUMTX_SHM_CREATE, except if there's no shared memory object associated with the specified keyuaddr, you get an error back, and no new object gets created. No sneaky surprise creations. periodt.UMTX_SHM_DESTROYโ de-associates the shared object from the specified keyuaddr. The object itself gets destroyed after the last open file descriptor for it is closed and the last mapping for it is destroyed. Like, it lingers until literally nobody's holding on anymore. ๐ญUMTX_SHM_ALIVEโ checks whether there's a live shared object associated with the supplied keyuaddr. Returns zero if there is one, and an error otherwise. This one is basically an optimization of theUMTX_SHM_LOOKUPrequest โ it's cheaper when all you wanna know is whether the associated object is alive, because no file descriptor gets installed in the process fd table on success. iykyk, free real estate. ๐ฅ
The uaddr argument specifies the virtual address whose backing physical memory byte identity gets used as a key for creating or looking up the anonymous shared object.
UMTX_OP_ROBUST_LISTSโ registers the list heads for the current thread's robust mutex lists. The arguments to this request are:valโ size of the structure passed in theuaddrargument.uaddrโ pointer to the structure of typestruct umtx_robust_lists_params.
The structure is defined as
struct umtx_robust_lists_params {
uintptr_t robust_list_offset;
uintptr_t robust_priv_list_offset;
uintptr_t robust_inact_offset;
};
The robust_list_offset member holds the address of the first element in the list of locked robust shared mutexes. The robust_priv_list_offset member holds the address of the first element in the list of locked robust private mutexes. And why are private and shared robust locked lists split like that? So the shared list can be terminated fast on fork, in the child. Big brain energy. ๐ง โจ
The robust_inact_offset contains a pointer to the mutex which might be locked in the near future, or might have just been unlocked. Typically it's set by the lock or unlock mutex implementation code around the whole operation, since lists can only be changed race-free when the thread owns the mutex. The kernel inspects the robust_inact_offset in addition to walking the shared and private lists. Also โ and this is the fun bit โ the mutex pointed to by robust_inact_offset is handled more loosely at thread termination time than the other mutexes on the list. That mutex is allowed to be not owned by the current thread, in which case list processing just carries on. See the โROBUST UMUTEXESโ subsection for the details, bestie. ๐ซถ
UMTX_OP_GET_MIN_TIMEOUTโ writes out the current value of the minimal umtx operations timeout, in nanoseconds, into thelonginteger variable pointed to byuaddr1. ๐UMTX_OP_SET_MIN_TIMEOUTโ sets the minimal amount of time, in nanoseconds, that the thread is required to sleep for umtx operations that specify a timeout using absolute clocks. The value comes from thevalargument of the call. Zero means no minimum. ๐
Okay so now the flags part โ this is where it gets spicy. ๐ถ๏ธ The op argument may be a bitwise OR of a single command from above with one or more of the following flags:
UMTX_OP__I386โ requests i386 ABI compatibility from the native_umtx_opsystem call. Specifically, this implies that:objarguments that point to a word, point to a 32-bit integer.- The
UMTX_OP_NWAKE_PRIVATEobjargument is a pointer to an array of 32-bit pointers. - The
m_rb_lnkmember ofstruct umutexis a 32-bit pointer. struct timespecuses a 32-bittime_t.
UMTX_OP__32BIT has no effect if this flag is set. This flag is valid for all architectures, but it's ignored on i386. ๐
UMTX_OP__32BITโ requests non-i386, 32-bit ABI compatibility from the native_umtx_opsystem call. Specifically, this implies that:objarguments that point to a word, point to a 32-bit integer.- The
UMTX_OP_NWAKE_PRIVATEobjargument is a pointer to an array of 32-bit pointers. - The
m_rb_lnkmember ofstruct umutexis a 32-bit pointer. struct timespecuses a 64-bittime_t.
This flag has no effect if UMTX_OP__I386 is set. This flag is valid for all architectures. ๐
โ ๏ธ And note this, fr fr: if any 32-bit ABI compatibility is being requested, then care must be taken with robust lists. A single thread may not mix 32-bit compatible robust lists with native robust lists. The first UMTX_OP_ROBUST_LISTS call in a given thread determines which ABI that thread will use for robust lists going forward. No take-backs, no switching mid-character-arc. ๐
โจ RETURN VALUES โจ
If everything goes off without a hitch, all requests return zero โ with the exception of the UMTX_SHM_CREAT and UMTX_SHM_LOOKUP sub-requests of the UMTX_OP_SHM request, because those two return a shared memory file descriptor on success. ๐
On error, -1 is returned, and the errno variable is set to indicate the error. The way that errno just shows up to explain what went wrong? Iconic. ๐
๐ ERRORS ๐
The _umtx_op() operations can fail with the following errors:
[EFAULT]โ one of the arguments points to invalid memory. ๐[EINVAL]โ the clock identifier, specified for thestruct _umtx_timetimeout parameter, or in thec_clockidmember ofstruct ucond, is invalid.[EINVAL]โ the type of the mutex, encoded by them_flagsmember ofstruct umutex, is invalid.[EINVAL]โ them_ownermember of thestruct umutexchanged the lock owner thread identifier during unlock.[EINVAL]โ thetimeout.tv_secortimeout.tv_nsecmember ofstruct _umtx_timeis less than zero, ortimeout.tv_nsecis greater than1000000000.[EINVAL]โ theopargument specifies an invalid operation.[EINVAL]โ theuaddrargument for theUMTX_OP_SHMrequest specifies an invalid operation.[EINVAL]โ theUMTX_OP_SET_CEILINGrequest specifies a non priority protected mutex.[EINVAL]โ the new ceiling value for theUMTX_OP_SET_CEILINGrequest, or one or more of the values read from them_ceilingsarray during lock or unlock operations, is greater thanRTP_PRIO_MAX.[EPERM]โ unlock attempted on an object not owned by the current thread. Rude. ๐ค[EOWNERDEAD]โ the lock was requested on anumutexwhere them_ownerfield was set to theUMUTEX_RB_OWNERDEADvalue, indicating a terminated robust mutex. The lock was granted to the caller, so this error actually indicates success with additional conditions. Plot twist!! ๐ญ[ENOTRECOVERABLE]โ the lock was requested on anumutexwhosem_ownerfield is equal to theUMUTEX_RB_NOTRECOVvalue, indicating an abandoned robust mutex after termination. The lock was not granted to the caller. ๐[ENOTTY]โ the shared memory object associated with the address passed to theUMTX_SHM_ALIVEsub-request of theUMTX_OP_SHMrequest was destroyed.[ESRCH]โ for theUMTX_SHM_LOOKUP,UMTX_SHM_DESTROY, andUMTX_SHM_ALIVEsub-requests of theUMTX_OP_SHMrequest, there is no shared memory object associated with the provided key. ๐[ENOMEM]โ theUMTX_SHM_CREATsub-request of theUMTX_OP_SHMrequest cannot be satisfied, because allocation of the shared memory object would exceed theRLIMIT_UMTXPresource limit โ seesetrlimit(2).[EAGAIN]โ the maximum number of readers (URWLOCK_MAX_READERS) were already granted ownership of the givenstruct rwlockfor read.[EBUSY]โ a try mutex lock operation was not able to obtain the lock.[ETIMEDOUT]โ the request specified a timeout in theuaddranduaddr2arguments, and timed out before obtaining the lock or being woken up. โฐ[EINTR]โ a signal was delivered during wait, for a non-restartable operation. Operations with timeouts are typically non-restartable, but timeouts specified in absolute time may be restartable.[ERESTART]โ a signal was delivered during wait, for a restartable operation. Mutex lock requests without a timeout specified are restartable. This error is not returned to userspace code, since the restart is handled by the usual adjustment of the instruction counter. ๐
๐ SEE ALSO ๐
clock_gettime(2), mmap(2), setrlimit(2), shm_open(2), sigaction(2), thr_exit(2), thr_kill(2), thr_kill2(2), thr_new(2), thr_self(2), thr_set_name(2), signal(3)
๐ STANDARDS ๐
The _umtx_op() system call is non-standard, and it's used by the 1:1 Threading Library (libthr, -lthr) to implement IEEE Std 1003.1-2001 (โPOSIX.1โ) pthread(3) functionality. So yeah, she's not standard, but she's essential. main character energy tbh. ๐
๐ BUGS ๐
Okay bestie, thereโs a tiny chaotic window here and we need to talk about it. Between unlocking a robust mutex and resetting the pointer in the robust_inact_offset member of the registered struct umtx_robust_lists_params, another thread can slide in and destroy the mutex. That means the kernel may end up inspecting memory thatโs already been freed or reused. ๐ญ๐
The libthr implementation is only vulnerable to this race when itโs operating on a shared mutex. So thatโs the vibe check. ๐ซถ
A possible fix for the current implementation is to strengthen the checks for shared mutexes before terminating them โ specifically, verifying that the mutex memory is mapped from a shared memory object allocated by the UMTX_OP_SHM request. โจ
And why isnโt that check there? Because itโs believed the race is adequately covered by other consistency checks, while adding the check would prevent alternative implementations of libpthread. Periodt. ๐
โ ๏ธ So yeah โ not a โforgot to fix itโ moment, more of a โthe existing checks are believed to have it covered, and the extra check would block other libpthread implementationsโ moment. Iykyk. ๐ธ
๐ And thatโs the whole _UMTX_OP(2) page, bestie! This iconic edition is the Debian one, dated November 23, 2020 โ go forth and lock responsibly. ๐โจ๐
๐ฆ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 2016 The FreeBSD Foundation
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms