Index: kern_lock.c =================================================================== RCS file: /cvs/src/sys/kern/kern_lock.c,v diff -u -p -r1.75 kern_lock.c --- kern_lock.c 3 Jul 2024 01:36:50 -0000 1.75 +++ kern_lock.c 17 Oct 2024 03:38:09 -0000 @@ -326,24 +377,31 @@ mtx_enter_try(struct mutex *mtx) void mtx_leave(struct mutex *mtx) { + struct cpu_info *ci = curcpu(); + struct cpu_info *owner; int s; /* Avoid deadlocks after panic or in DDB */ if (panicstr || db_active) return; - MUTEX_ASSERT_LOCKED(mtx); WITNESS_UNLOCK(MUTEX_LOCK_OBJECT(mtx), LOP_EXCLUSIVE); #ifdef DIAGNOSTIC - curcpu()->ci_mutex_level--; + ci->ci_mutex_level--; #endif s = mtx->mtx_oldipl; #ifdef MULTIPROCESSOR - membar_exit(); -#endif + membar_exit_before_atomic(); + owner = atomic_cas_ptr(&mtx->mtx_owner, ci, NULL); +#else + owner = mtx->mtx_owner; mtx->mtx_owner = NULL; +#endif + if (__predict_false(owner != ci)) + panic("mtx %p: not held in unlock", mtx); + if (mtx->mtx_wantipl != IPL_NONE) splx(s); }