Index: sys/sys/_mutex.h =================================================================== --- sys/sys/_mutex.h (revision 242016) +++ sys/sys/_mutex.h (working copy) @@ -31,12 +31,30 @@ #ifndef _SYS__MUTEX_H_ #define _SYS__MUTEX_H_ +#include + /* * Sleep/spin mutex. + * + * In order to support 2 different types of structures with the same + * KPI, the second member of mutexes must be the lock cookie, called + * "mtx_lock". Other type of locks must not use the same name for the + * lock cookie. + * An accessor macro is provided in order to extract the mutex ptr from + * the cookie address, assuming that the first member is lock_object + * as must always be for every lock. */ struct mtx { struct lock_object lock_object; /* Common lock properties. */ volatile uintptr_t mtx_lock; /* Owner and flags. */ }; +struct mtx_unshare { + struct lock_object lock_object; /* Common lock properties. */ + volatile uintptr_t mtx_lock; /* Owner and flags. */ +} __aligned(CACHE_LINE_SIZE); + +#define MTX_FROM_COOKIE(ptr) \ + ((struct mtx *)((ptr) - sizeof(struct lock_object))) + #endif /* !_SYS__MUTEX_H_ */ Index: sys/sys/mutex.h =================================================================== --- sys/sys/mutex.h (revision 242016) +++ sys/sys/mutex.h (working copy) @@ -89,7 +89,8 @@ * [See below for descriptions] * */ -void mtx_init(struct mtx *m, const char *name, const char *type, int opts); +void _mtx_init(volatile uintptr_t *c, const char *name, const char *type, + int opts); void mtx_destroy(struct mtx *m); void mtx_sysinit(void *arg); int mtx_trylock_flags_(struct mtx *m, int opts, const char *file, @@ -123,6 +124,8 @@ void thread_lock_flags_(struct thread *, int, cons #define mtx_recurse lock_object.lo_data +#define mtx_init(m, n, t, o) _mtx_init(&(m)->mtx_lock, n, t, o) + /* Very simple operations on mtx_lock. */ /* Try to obtain mtx_lock once. */ Index: sys/kern/kern_mutex.c =================================================================== --- sys/kern/kern_mutex.c (revision 242016) +++ sys/kern/kern_mutex.c (working copy) @@ -816,11 +816,14 @@ mtx_sysinit(void *arg) * witness. */ void -mtx_init(struct mtx *m, const char *name, const char *type, int opts) +_mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts) { + struct mtx *m; struct lock_class *class; int flags; + m = MTX_FROM_COOKIE(c); + MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0); ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,