diff --git a/sys/kern/kern_mutex.c b/sys/kern/kern_mutex.c index 634fa9d82627..04368e51708b 100644 --- a/sys/kern/kern_mutex.c +++ b/sys/kern/kern_mutex.c @@ -276,6 +276,8 @@ _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) { MPASS(curthread != NULL); + KASSERT(m->mtx_lock != MTX_DESTROYED, + ("mtx_lock() of destroyed mutex @ %s:%d", file, line)); KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep, ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, file, line)); @@ -301,6 +303,8 @@ _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) { MPASS(curthread != NULL); + KASSERT(m->mtx_lock != MTX_DESTROYED, + ("mtx_unlock() of destroyed mutex @ %s:%d", file, line)); KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep, ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, file, line)); @@ -380,6 +384,8 @@ _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) { MPASS(curthread != NULL); + KASSERT(m->mtx_lock != MTX_DESTROYED, + ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line)); KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin, ("mtx_lock_spin() of sleep mutex %s @ %s:%d", m->mtx_object.lo_name, file, line)); @@ -396,6 +402,8 @@ _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) { MPASS(curthread != NULL); + KASSERT(m->mtx_lock != MTX_DESTROYED, + ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line)); KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin, ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", m->mtx_object.lo_name, file, line)); @@ -417,6 +425,8 @@ _mtx_trylock(struct mtx *m, int opts, const char *file, int line) int rval; MPASS(curthread != NULL); + KASSERT(m->mtx_lock != MTX_DESTROYED, + ("mtx_trylock() of destroyed mutex @ %s:%d", file, line)); KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep, ("mtx_trylock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, file, line)); @@ -885,6 +895,7 @@ mtx_destroy(struct mtx *m) __LINE__); } + m->mtx_lock = MTX_DESTROYED; lock_destroy(&m->mtx_object); } diff --git a/sys/sys/mutex.h b/sys/sys/mutex.h index a28ea3123b0d..1a13e1a61a28 100644 --- a/sys/sys/mutex.h +++ b/sys/sys/mutex.h @@ -73,6 +73,11 @@ #define MTX_UNOWNED 0x00000004 /* Cookie for free mutex */ #define MTX_FLAGMASK (MTX_RECURSED | MTX_CONTESTED | MTX_UNOWNED) +/* + * Value stored in mutex->mtx_lock to denote a destroyed mutex. + */ +#define MTX_DESTROYED (MTX_CONTESTED | MTX_UNOWNED) + #endif /* _KERNEL */ #ifndef LOCORE