From ccb600906f152df310794f146eac54372e6b2665 Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Sun, 5 Oct 2025 15:38:06 +0000 Subject: [PATCH] mtx: retire _mtx_release_lock_quick The macro is misleading and of questionable value to begin with. For starters, it is used for both spinlocks and regular mutexes (the latter only the in the slow path), which have fundamentally different requirements on unlock -- spinlocks are guaranteed to not have blocked waiters and can blindly do a store. The commentary above the it is also head-scratching: > Release mtx_lock quickly, assuming we own it. You can't *just* release a sleepable mutex "quickly". The only legal use right now is when the turnstile lock is held. Note that unlock of a sleepable mutex without using RMW atomics is very much possible and may show up soon (tm). Sponsored by: Rubicon Communications, LLC ("Netgate") --- sys/kern/kern_mutex.c | 6 +++--- sys/sys/mutex.h | 6 +----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/sys/kern/kern_mutex.c b/sys/kern/kern_mutex.c index 8b5908f5219..b7316ea5f38 100644 --- a/sys/kern/kern_mutex.c +++ b/sys/kern/kern_mutex.c @@ -869,7 +869,7 @@ _thread_lock(struct thread *td) WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line); return; } - _mtx_release_lock_quick(m); + atomic_store_rel_ptr(&m->mtx_lock, MTX_UNOWNED); slowpath_unlocked: spinlock_exit(); slowpath_noirq: @@ -959,7 +959,7 @@ thread_lock_flags_(struct thread *td, int opts, const char *file, int line) } if (m == td->td_lock) break; - _mtx_release_lock_quick(m); + atomic_store_rel_ptr(&m->mtx_lock, MTX_UNOWNED); } LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, line); @@ -1071,7 +1071,7 @@ __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v) * can be removed from the hash list if it is empty. */ turnstile_chain_lock(&m->lock_object); - _mtx_release_lock_quick(m); + atomic_store_rel_ptr(&m->mtx_lock, MTX_UNOWNED); ts = turnstile_lookup(&m->lock_object); MPASS(ts != NULL); if (LOCK_LOG_TEST(&m->lock_object, opts)) diff --git a/sys/sys/mutex.h b/sys/sys/mutex.h index b534a74626b..83300d4eb59 100644 --- a/sys/sys/mutex.h +++ b/sys/sys/mutex.h @@ -221,10 +221,6 @@ void _thread_lock(struct thread *); #define _mtx_release_lock(mp, tid) \ atomic_cmpset_rel_ptr(&(mp)->mtx_lock, (tid), MTX_UNOWNED) -/* Release mtx_lock quickly, assuming we own it. */ -#define _mtx_release_lock_quick(mp) \ - atomic_store_rel_ptr(&(mp)->mtx_lock, MTX_UNOWNED) - #define _mtx_release_lock_fetch(mp, vp) \ atomic_fcmpset_rel_ptr(&(mp)->mtx_lock, (vp), MTX_UNOWNED) @@ -332,7 +328,7 @@ void _thread_lock(struct thread *); (mp)->mtx_recurse--; \ else { \ LOCKSTAT_PROFILE_RELEASE_SPIN_LOCK(spin__release, mp); \ - _mtx_release_lock_quick((mp)); \ + atomic_store_rel_ptr(&(mp)->mtx_lock, MTX_UNOWNED); \ } \ spinlock_exit(); \ })