From c4925651028d655edda3c8b85b8d1b4a0cda6fb9 Mon Sep 17 00:00:00 2001 From: visa Date: Thu, 20 Apr 2017 13:57:29 +0000 Subject: [PATCH] Hook up mutex(9) to witness(4). --- sys/arch/alpha/alpha/mutex.c | 16 ++++---- sys/arch/alpha/include/mutex.h | 18 +++++++-- sys/arch/amd64/amd64/mutex.S | 8 ++-- sys/arch/amd64/include/mutex.h | 18 +++++++-- sys/arch/arm/armv7/armv7_mutex.c | 10 ++--- sys/arch/arm/include/mutex.h | 18 +++++++-- sys/arch/arm64/include/mutex.h | 18 +++++++-- sys/arch/hppa/hppa/mutex.c | 16 ++++---- sys/arch/hppa/include/mutex.h | 26 +++++++++--- sys/arch/i386/i386/mutex.S | 10 ++--- sys/arch/i386/include/mutex.h | 18 +++++++-- sys/arch/m88k/include/mutex.h | 19 +++++++-- sys/arch/m88k/m88k/mutex.S | 8 ++-- sys/arch/mips64/include/mutex.h | 18 +++++++-- sys/arch/mips64/mips64/mutex.c | 16 ++++---- sys/arch/powerpc/include/mutex.h | 18 +++++++-- sys/arch/powerpc/powerpc/mutex.c | 16 ++++---- sys/arch/sh/include/mutex.h | 7 ++-- sys/arch/sh/sh/mutex.c | 10 ++--- sys/arch/sparc64/include/mutex.h | 18 +++++++-- sys/arch/sparc64/sparc64/mutex.S | 8 ++-- sys/conf/files | 3 +- sys/kern/kern_mutex.c | 69 ++++++++++++++++++++++++++++++++ sys/kern/kern_synch.c | 6 ++- sys/sys/mutex.h | 67 ++++++++++++++++++++++++++++--- 25 files changed, 356 insertions(+), 103 deletions(-) create mode 100644 sys/kern/kern_mutex.c diff --git a/sys/arch/alpha/alpha/mutex.c b/sys/arch/alpha/alpha/mutex.c index f3603e42154..ada28fad1cc 100644 --- a/sys/arch/alpha/alpha/mutex.c +++ b/sys/arch/alpha/alpha/mutex.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.c,v 1.16 2016/06/13 01:26:14 dlg Exp $ */ +/* $OpenBSD: mutex.c,v 1.17 2017/04/20 13:57:29 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -45,14 +45,14 @@ __mtx_init(struct mutex *mtx, int wantipl) #ifdef MULTIPROCESSOR void -mtx_enter(struct mutex *mtx) +__mtx_enter(struct mutex *mtx) { - while (mtx_enter_try(mtx) == 0) + while (__mtx_enter_try(mtx) == 0) SPINLOCK_SPIN_HOOK; } int -mtx_enter_try(struct mutex *mtx) +__mtx_enter_try(struct mutex *mtx) { struct cpu_info *owner, *ci = curcpu(); int s; @@ -82,7 +82,7 @@ mtx_enter_try(struct mutex *mtx) } #else void -mtx_enter(struct mutex *mtx) +__mtx_enter(struct mutex *mtx) { struct cpu_info *ci = curcpu(); @@ -101,15 +101,15 @@ mtx_enter(struct mutex *mtx) } int -mtx_enter_try(struct mutex *mtx) +__mtx_enter_try(struct mutex *mtx) { - mtx_enter(mtx); + __mtx_enter(mtx); return (1); } #endif void -mtx_leave(struct mutex *mtx) +__mtx_leave(struct mutex *mtx) { int s; diff --git a/sys/arch/alpha/include/mutex.h b/sys/arch/alpha/include/mutex.h index c5509881c5a..a75afb2e4e8 100644 --- a/sys/arch/alpha/include/mutex.h +++ b/sys/arch/alpha/include/mutex.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.h,v 1.7 2015/04/17 12:38:54 dlg Exp $ */ +/* $OpenBSD: mutex.h,v 1.8 2017/04/20 13:57:29 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -28,10 +28,15 @@ #ifndef _MACHINE_MUTEX_H_ #define _MACHINE_MUTEX_H_ +#include + struct mutex { void *mtx_owner; int mtx_wantipl; int mtx_oldipl; +#ifdef WITNESS + struct lock_object mtx_lock_obj; +#endif }; /* @@ -48,10 +53,16 @@ struct mutex { #define __MUTEX_IPL(ipl) (ipl) #endif -#define MUTEX_INITIALIZER(ipl) { NULL, __MUTEX_IPL((ipl)), IPL_NONE } +#ifdef WITNESS +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { NULL, __MUTEX_IPL((ipl)), IPL_NONE, MTX_LO_INITIALIZER(name, flags) } +#else +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { NULL, __MUTEX_IPL((ipl)), IPL_NONE } +#endif void __mtx_init(struct mutex *, int); -#define mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) +#define _mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) #ifdef DIAGNOSTIC #define MUTEX_ASSERT_LOCKED(mtx) do { \ @@ -68,6 +79,7 @@ void __mtx_init(struct mutex *, int); #define MUTEX_ASSERT_UNLOCKED(mtx) do { } while (0) #endif +#define MUTEX_LOCK_OBJECT(mtx) (&(mtx)->mtx_lock_obj) #define MUTEX_OLDIPL(mtx) (mtx)->mtx_oldipl #endif /* _MACHINE_MUTEX_H_ */ diff --git a/sys/arch/amd64/amd64/mutex.S b/sys/arch/amd64/amd64/mutex.S index 5d33dbcf451..e31c2198a36 100644 --- a/sys/arch/amd64/amd64/mutex.S +++ b/sys/arch/amd64/amd64/mutex.S @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.S,v 1.11 2017/03/07 14:03:22 visa Exp $ */ +/* $OpenBSD: mutex.S,v 1.12 2017/04/20 13:57:29 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -44,7 +44,7 @@ ENTRY(__mtx_init) movq $0, MTX_OWNER(%rdi) ret -ENTRY(mtx_enter) +ENTRY(__mtx_enter) 1: movl MTX_WANTIPL(%rdi), %eax movq CPUVAR(SELF), %rcx movl CPU_INFO_ILEVEL(%rcx), %edx # oipl = cpl; @@ -89,7 +89,7 @@ ENTRY(mtx_enter) 5: .asciz "mtx_enter: locking against myself" #endif -ENTRY(mtx_enter_try) +ENTRY(__mtx_enter_try) 1: movl MTX_WANTIPL(%rdi), %eax movq CPUVAR(SELF), %rcx movl CPU_INFO_ILEVEL(%rcx), %edx # oipl = cpl; @@ -133,7 +133,7 @@ ENTRY(mtx_enter_try) #endif -ENTRY(mtx_leave) +ENTRY(__mtx_leave) movq %rdi, %rax #ifdef DIAGNOSTIC movq CPUVAR(SELF), %rcx diff --git a/sys/arch/amd64/include/mutex.h b/sys/arch/amd64/include/mutex.h index 092760840fb..69685e53443 100644 --- a/sys/arch/amd64/include/mutex.h +++ b/sys/arch/amd64/include/mutex.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.h,v 1.7 2014/03/29 18:09:28 guenther Exp $ */ +/* $OpenBSD: mutex.h,v 1.8 2017/04/20 13:57:29 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -27,10 +27,15 @@ #ifndef _MACHINE_MUTEX_H_ #define _MACHINE_MUTEX_H_ +#include + struct mutex { int mtx_wantipl; int mtx_oldipl; volatile void *mtx_owner; +#ifdef WITNESS + struct lock_object mtx_lock_obj; +#endif }; /* @@ -47,10 +52,16 @@ struct mutex { #define __MUTEX_IPL(ipl) (ipl) #endif -#define MUTEX_INITIALIZER(ipl) { __MUTEX_IPL((ipl)), 0, NULL } +#ifdef WITNESS +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { __MUTEX_IPL((ipl)), 0, NULL, MTX_LO_INITIALIZER(name, flags) } +#else +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { __MUTEX_IPL((ipl)), 0, NULL } +#endif void __mtx_init(struct mutex *, int); -#define mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) +#define _mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) #define MUTEX_ASSERT_LOCKED(mtx) do { \ if ((mtx)->mtx_owner != curcpu()) \ @@ -62,6 +73,7 @@ void __mtx_init(struct mutex *, int); panic("mutex %p held in %s", (mtx), __func__); \ } while (0) +#define MUTEX_LOCK_OBJECT(mtx) (&(mtx)->mtx_lock_obj) #define MUTEX_OLDIPL(mtx) (mtx)->mtx_oldipl #endif diff --git a/sys/arch/arm/armv7/armv7_mutex.c b/sys/arch/arm/armv7/armv7_mutex.c index 47120723594..8b138116783 100644 --- a/sys/arch/arm/armv7/armv7_mutex.c +++ b/sys/arch/arm/armv7/armv7_mutex.c @@ -1,4 +1,4 @@ -/* $OpenBSD: armv7_mutex.c,v 1.2 2013/05/09 14:27:17 patrick Exp $ */ +/* $OpenBSD: armv7_mutex.c,v 1.3 2017/04/20 13:57:29 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -43,7 +43,7 @@ * raising semantics of the mutexes. */ void -mtx_init(struct mutex *mtx, int wantipl) +__mtx_init(struct mutex *mtx, int wantipl) { mtx->mtx_oldipl = 0; mtx->mtx_wantipl = wantipl; @@ -51,7 +51,7 @@ mtx_init(struct mutex *mtx, int wantipl) } void -mtx_enter(struct mutex *mtx) +__mtx_enter(struct mutex *mtx) { if (mtx->mtx_wantipl != IPL_NONE) mtx->mtx_oldipl = _splraise(mtx->mtx_wantipl); @@ -64,7 +64,7 @@ mtx_enter(struct mutex *mtx) } int -mtx_enter_try(struct mutex *mtx) +__mtx_enter_try(struct mutex *mtx) { if (mtx->mtx_wantipl != IPL_NONE) mtx->mtx_oldipl = _splraise(mtx->mtx_wantipl); @@ -82,7 +82,7 @@ mtx_enter_try(struct mutex *mtx) } void -mtx_leave(struct mutex *mtx) +__mtx_leave(struct mutex *mtx) { MUTEX_ASSERT_LOCKED(mtx); mtx->mtx_lock = 0; diff --git a/sys/arch/arm/include/mutex.h b/sys/arch/arm/include/mutex.h index 9dcea763704..a2e26718ef8 100644 --- a/sys/arch/arm/include/mutex.h +++ b/sys/arch/arm/include/mutex.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.h,v 1.2 2011/03/23 16:54:34 pirofti Exp $ */ +/* $OpenBSD: mutex.h,v 1.3 2017/04/20 13:57:29 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -28,6 +28,8 @@ #ifndef _ARM_MUTEX_H_ #define _ARM_MUTEX_H_ +#include + /* * Simple non-mp implementation. */ @@ -35,11 +37,20 @@ struct mutex { int mtx_lock; int mtx_wantipl; int mtx_oldipl; +#ifdef WITNESS + struct lock_object mtx_lock_obj; +#endif }; -void mtx_init(struct mutex *, int); +void __mtx_init(struct mutex *, int); -#define MUTEX_INITIALIZER(ipl) { 0, ipl, 0 } +#ifdef WITNESS +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { 0, ipl, 0, MTX_LO_INITIALIZER(name, flags) } +#else +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { 0, ipl, 0 } +#endif #ifdef DIAGNOSTIC #define MUTEX_ASSERT_LOCKED(mtx) do { \ @@ -56,6 +67,7 @@ void mtx_init(struct mutex *, int); #define MUTEX_ASSERT_UNLOCKED(mtx) do { } while (0) #endif +#define MUTEX_LOCK_OBJECT(mtx) (&(mtx)->mtx_lock_obj) #define MUTEX_OLDIPL(mtx) (mtx)->mtx_oldipl #endif diff --git a/sys/arch/arm64/include/mutex.h b/sys/arch/arm64/include/mutex.h index 1cad62e8b4e..5fec6284c91 100644 --- a/sys/arch/arm64/include/mutex.h +++ b/sys/arch/arm64/include/mutex.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.h,v 1.1 2016/12/17 23:38:33 patrick Exp $ */ +/* $OpenBSD: mutex.h,v 1.2 2017/04/20 13:57:29 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -27,10 +27,15 @@ #ifndef _MACHINE_MUTEX_H_ #define _MACHINE_MUTEX_H_ +#include + struct mutex { int mtx_wantipl; int mtx_oldipl; volatile void *mtx_owner; +#ifdef WITNESS + struct lock_object mtx_lock_obj; +#endif }; /* @@ -47,10 +52,16 @@ struct mutex { #define __MUTEX_IPL(ipl) (ipl) #endif -#define MUTEX_INITIALIZER(ipl) { __MUTEX_IPL((ipl)), 0, NULL } +#ifdef WITNESS +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { __MUTEX_IPL((ipl)), 0, NULL, MTX_LO_INITIALIZER(name, flags) } +#else +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { __MUTEX_IPL((ipl)), 0, NULL } +#endif void __mtx_init(struct mutex *, int); -#define mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) +#define _mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) #define MUTEX_ASSERT_LOCKED(mtx) do { \ if ((mtx)->mtx_owner != curcpu()) \ @@ -62,6 +73,7 @@ void __mtx_init(struct mutex *, int); panic("mutex %p held in %s", (mtx), __func__); \ } while (0) +#define MUTEX_LOCK_OBJECT(mtx) (&(mtx)->mtx_lock_obj) #define MUTEX_OLDIPL(mtx) (mtx)->mtx_oldipl #endif diff --git a/sys/arch/hppa/hppa/mutex.c b/sys/arch/hppa/hppa/mutex.c index 160337c1aa7..9da3224674d 100644 --- a/sys/arch/hppa/hppa/mutex.c +++ b/sys/arch/hppa/hppa/mutex.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.c,v 1.15 2015/09/20 19:19:03 kettenis Exp $ */ +/* $OpenBSD: mutex.c,v 1.16 2017/04/20 13:57:29 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -57,14 +57,14 @@ __mtx_init(struct mutex *mtx, int wantipl) #ifdef MULTIPROCESSOR void -mtx_enter(struct mutex *mtx) +__mtx_enter(struct mutex *mtx) { - while (mtx_enter_try(mtx) == 0) + while (__mtx_enter_try(mtx) == 0) ; } int -mtx_enter_try(struct mutex *mtx) +__mtx_enter_try(struct mutex *mtx) { struct cpu_info *ci = curcpu(); volatile int *lock = __mtx_lock(mtx); @@ -104,7 +104,7 @@ mtx_enter_try(struct mutex *mtx) } #else void -mtx_enter(struct mutex *mtx) +__mtx_enter(struct mutex *mtx) { struct cpu_info *ci = curcpu(); @@ -124,15 +124,15 @@ mtx_enter(struct mutex *mtx) } int -mtx_enter_try(struct mutex *mtx) +__mtx_enter_try(struct mutex *mtx) { - mtx_enter(mtx); + __mtx_enter(mtx); return (1); } #endif void -mtx_leave(struct mutex *mtx) +__mtx_leave(struct mutex *mtx) { #ifdef MULTIPROCESSOR volatile int *lock = __mtx_lock(mtx); diff --git a/sys/arch/hppa/include/mutex.h b/sys/arch/hppa/include/mutex.h index 36be0577813..48eacedd33e 100644 --- a/sys/arch/hppa/include/mutex.h +++ b/sys/arch/hppa/include/mutex.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.h,v 1.6 2015/05/02 10:59:47 dlg Exp $ */ +/* $OpenBSD: mutex.h,v 1.7 2017/04/20 13:57:29 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -28,6 +28,8 @@ #ifndef _MACHINE_MUTEX_H_ #define _MACHINE_MUTEX_H_ +#include + #define MUTEX_UNLOCKED { 1, 1, 1, 1 } /* Note: mtx_lock must be 16-byte aligned. */ @@ -38,6 +40,9 @@ struct mutex { int mtx_wantipl; int mtx_oldipl; void *mtx_owner; +#ifdef WITNESS + struct lock_object mtx_lock_obj; +#endif }; /* @@ -50,14 +55,22 @@ struct mutex { #ifdef MULTIPROCESSOR #define __MUTEX_IPL(ipl) \ (((ipl) > IPL_NONE && (ipl) < IPL_AUDIO) ? IPL_AUDIO : (ipl)) -#define MUTEX_INITIALIZER(ipl) { MUTEX_UNLOCKED, __MUTEX_IPL((ipl)), 0, NULL } -#else +#ifdef WITNESS +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { MUTEX_UNLOCKED, __MUTEX_IPL((ipl)), 0, NULL, \ + MTX_LO_INITIALIZER(name, flags) } +#else /* WITNESS */ +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { MUTEX_UNLOCKED, __MUTEX_IPL((ipl)), 0, NULL } +#endif /* WITNESS */ +#else /* MULTIPROCESSOR */ #define __MUTEX_IPL(ipl) (ipl) -#define MUTEX_INITIALIZER(ipl) { __MUTEX_IPL((ipl)), 0, NULL } -#endif +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { __MUTEX_IPL((ipl)), 0, NULL } +#endif /* MULTIPROCESSOR */ void __mtx_init(struct mutex *, int); -#define mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) +#define _mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) #ifdef DIAGNOSTIC #define MUTEX_ASSERT_LOCKED(mtx) do { \ @@ -74,6 +87,7 @@ void __mtx_init(struct mutex *, int); #define MUTEX_ASSERT_UNLOCKED(mtx) do { } while (0) #endif +#define MUTEX_LOCK_OBJECT(mtx) (&(mtx)->mtx_lock_obj) #define MUTEX_OLDIPL(mtx) (mtx)->mtx_oldipl #endif diff --git a/sys/arch/i386/i386/mutex.S b/sys/arch/i386/i386/mutex.S index f3fbd7b802b..afa3446ca71 100644 --- a/sys/arch/i386/i386/mutex.S +++ b/sys/arch/i386/i386/mutex.S @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.S,v 1.10 2017/03/07 14:03:22 visa Exp $ */ +/* $OpenBSD: mutex.S,v 1.11 2017/04/20 13:57:29 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -45,7 +45,7 @@ ENTRY(__mtx_init) #define SOFF 8 -ENTRY(mtx_enter) +ENTRY(__mtx_enter) pushl %ebp movl %esp, %ebp 1: movl SOFF(%ebp), %ecx @@ -93,7 +93,7 @@ ENTRY(mtx_enter) 6: .asciz "mtx_enter: locking against myself" #endif -ENTRY(mtx_enter_try) +ENTRY(__mtx_enter_try) pushl %ebp movl %esp, %ebp 1: movl SOFF(%ebp), %ecx @@ -140,8 +140,8 @@ ENTRY(mtx_enter_try) 5: .asciz "mtx_enter_try: locking against myself" #endif - -ENTRY(mtx_leave) + +ENTRY(__mtx_leave) pushl %ebp movl %esp, %ebp movl SOFF(%ebp), %ecx diff --git a/sys/arch/i386/include/mutex.h b/sys/arch/i386/include/mutex.h index 8fce51d2310..72d9bbf1638 100644 --- a/sys/arch/i386/include/mutex.h +++ b/sys/arch/i386/include/mutex.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.h,v 1.8 2015/07/02 23:01:19 dlg Exp $ */ +/* $OpenBSD: mutex.h,v 1.9 2017/04/20 13:57:29 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -27,6 +27,8 @@ #ifndef _MACHINE_MUTEX_H_ #define _MACHINE_MUTEX_H_ +#include + /* * XXX - we don't really need the mtx_lock field, we can use mtx_oldipl * as the lock to save some space. @@ -36,6 +38,9 @@ struct mutex { int mtx_wantipl; int mtx_oldipl; void *mtx_owner; +#ifdef WITNESS + struct lock_object mtx_lock_obj; +#endif }; /* @@ -52,10 +57,16 @@ struct mutex { #define __MUTEX_IPL(ipl) (ipl) #endif -#define MUTEX_INITIALIZER(ipl) { 0, __MUTEX_IPL((ipl)), 0, NULL } +#ifdef WITNESS +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { 0, __MUTEX_IPL(ipl), 0, NULL, MTX_LO_INITIALIZER(name, flags) } +#else +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { 0, __MUTEX_IPL(ipl), 0, NULL } +#endif void __mtx_init(struct mutex *, int); -#define mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) +#define _mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) #define MUTEX_ASSERT_LOCKED(mtx) do { \ if ((mtx)->mtx_owner != curcpu()) \ @@ -67,6 +78,7 @@ void __mtx_init(struct mutex *, int); panic("mutex %p held in %s", (mtx), __func__); \ } while (0) +#define MUTEX_LOCK_OBJECT(mtx) (&(mtx)->mtx_lock_obj) #define MUTEX_OLDIPL(mtx) (mtx)->mtx_oldipl #endif diff --git a/sys/arch/m88k/include/mutex.h b/sys/arch/m88k/include/mutex.h index 73cd600b766..dba995bf6d6 100644 --- a/sys/arch/m88k/include/mutex.h +++ b/sys/arch/m88k/include/mutex.h @@ -1,6 +1,6 @@ #ifndef _M88K_MUTEX_H_ #define _M88K_MUTEX_H_ -/* $OpenBSD: mutex.h,v 1.4 2015/07/03 15:12:49 miod Exp $ */ +/* $OpenBSD: mutex.h,v 1.5 2017/04/20 13:57:29 visa Exp $ */ /* * Copyright (c) 2005, Miodrag Vallat. @@ -27,11 +27,16 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include + struct mutex { volatile int mtx_lock; /* mutex.S relies upon this field being first */ int mtx_wantipl; int mtx_oldipl; void *mtx_owner; +#ifdef WITNESS + struct lock_object mtx_lock_obj; +#endif }; /* @@ -48,10 +53,17 @@ struct mutex { #define __MUTEX_IPL(ipl) (ipl) #endif -#define MUTEX_INITIALIZER(ipl) { 0, __MUTEX_IPL((ipl)), IPL_NONE, NULL } +#ifdef WITNESS +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { 0, __MUTEX_IPL((ipl)), IPL_NONE, NULL, \ + MTX_LO_INITIALIZER(name, flags) } +#else +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { 0, __MUTEX_IPL((ipl)), IPL_NONE, NULL } +#endif void __mtx_init(struct mutex *, int); -#define mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) +#define _mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) #ifdef DIAGNOSTIC @@ -72,6 +84,7 @@ void __mtx_init(struct mutex *, int); #endif +#define MUTEX_LOCK_OBJECT(mtx) (&(mtx)->mtx_lock_obj) #define MUTEX_OLDIPL(mtx) (mtx)->mtx_oldipl #endif /* _M88K_MUTEX_H_ */ diff --git a/sys/arch/m88k/m88k/mutex.S b/sys/arch/m88k/m88k/mutex.S index 5f0fee2edc5..2cc5a3f5a6c 100644 --- a/sys/arch/m88k/m88k/mutex.S +++ b/sys/arch/m88k/m88k/mutex.S @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.S,v 1.15 2016/06/13 23:51:59 dlg Exp $ */ +/* $OpenBSD: mutex.S,v 1.16 2017/04/20 13:57:30 visa Exp $ */ /* * Copyright (c) 2005, Miodrag Vallat. @@ -45,7 +45,7 @@ ENTRY(__mtx_init) /* * void mtx_enter(struct mutex *mtx) */ -ENTRY(mtx_enter) +ENTRY(__mtx_enter) subu %r31, %r31, 8 st %r1, %r31, 4 /* save return address */ @@ -138,7 +138,7 @@ enter_panic: /* * int mtx_enter_try(struct mutex *mtx) */ -ENTRY(mtx_enter_try) +ENTRY(__mtx_enter_try) subu %r31, %r31, 8 st %r1, %r31, 4 /* save return address */ @@ -232,7 +232,7 @@ enter_try_panic: /* * void mtx_leave(struct mutex *mtx) */ -ENTRY(mtx_leave) +ENTRY(__mtx_leave) ld %r3, %r2, MTX_OLDIPL ld %r4, %r2, MTX_WANTIPL #ifdef DIAGNOSTIC diff --git a/sys/arch/mips64/include/mutex.h b/sys/arch/mips64/include/mutex.h index 6ba15aca16e..e8cd240b8ee 100644 --- a/sys/arch/mips64/include/mutex.h +++ b/sys/arch/mips64/include/mutex.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.h,v 1.1 2015/07/08 13:37:31 dlg Exp $ */ +/* $OpenBSD: mutex.h,v 1.2 2017/04/20 13:57:30 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -28,10 +28,15 @@ #ifndef _MACHINE_MUTEX_H_ #define _MACHINE_MUTEX_H_ +#include + struct mutex { void *mtx_owner; int mtx_wantipl; int mtx_oldipl; +#ifdef WITNESS + struct lock_object mtx_lock_obj; +#endif }; /* @@ -48,10 +53,16 @@ struct mutex { #define __MUTEX_IPL(ipl) (ipl) #endif -#define MUTEX_INITIALIZER(ipl) { NULL, __MUTEX_IPL((ipl)), IPL_NONE } +#ifdef WITNESS +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { NULL, __MUTEX_IPL((ipl)), IPL_NONE, MTX_LO_INITIALIZER(name, flags) } +#else +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { NULL, __MUTEX_IPL((ipl)), IPL_NONE } +#endif void __mtx_init(struct mutex *, int); -#define mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) +#define _mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) #ifdef DIAGNOSTIC #define MUTEX_ASSERT_LOCKED(mtx) do { \ @@ -68,6 +79,7 @@ void __mtx_init(struct mutex *, int); #define MUTEX_ASSERT_UNLOCKED(mtx) do { } while (0) #endif +#define MUTEX_LOCK_OBJECT(mtx) (&(mtx)->mtx_lock_obj) #define MUTEX_OLDIPL(mtx) (mtx)->mtx_oldipl #endif diff --git a/sys/arch/mips64/mips64/mutex.c b/sys/arch/mips64/mips64/mutex.c index f05db1894ef..b0923d9605f 100644 --- a/sys/arch/mips64/mips64/mutex.c +++ b/sys/arch/mips64/mips64/mutex.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.c,v 1.4 2016/03/19 11:34:22 mpi Exp $ */ +/* $OpenBSD: mutex.c,v 1.5 2017/04/20 13:57:30 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -54,13 +54,13 @@ extern int __mp_lock_spinout; #endif void -mtx_enter(struct mutex *mtx) +__mtx_enter(struct mutex *mtx) { #ifdef MP_LOCKDEBUG int nticks = __mp_lock_spinout; #endif - while (mtx_enter_try(mtx) == 0) { + while (__mtx_enter_try(mtx) == 0) { #ifdef MP_LOCKDEBUG if (--nticks == 0) { db_printf("%s(%p): lock spun out", __func__, mtx); @@ -72,7 +72,7 @@ mtx_enter(struct mutex *mtx) } int -mtx_enter_try(struct mutex *mtx) +__mtx_enter_try(struct mutex *mtx) { struct cpu_info *owner, *ci = curcpu(); int s; @@ -102,7 +102,7 @@ mtx_enter_try(struct mutex *mtx) } #else void -mtx_enter(struct mutex *mtx) +__mtx_enter(struct mutex *mtx) { struct cpu_info *ci = curcpu(); @@ -122,15 +122,15 @@ mtx_enter(struct mutex *mtx) } int -mtx_enter_try(struct mutex *mtx) +__mtx_enter_try(struct mutex *mtx) { - mtx_enter(mtx); + __mtx_enter(mtx); return (1); } #endif void -mtx_leave(struct mutex *mtx) +__mtx_leave(struct mutex *mtx) { int s; diff --git a/sys/arch/powerpc/include/mutex.h b/sys/arch/powerpc/include/mutex.h index 8f6f2f1d57b..2826ebd6f08 100644 --- a/sys/arch/powerpc/include/mutex.h +++ b/sys/arch/powerpc/include/mutex.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.h,v 1.5 2015/08/14 06:14:19 dlg Exp $ */ +/* $OpenBSD: mutex.h,v 1.6 2017/04/20 13:57:30 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -27,10 +27,15 @@ #ifndef _POWERPC_MUTEX_H_ #define _POWERPC_MUTEX_H_ +#include + struct mutex { volatile void *mtx_owner; int mtx_wantipl; int mtx_oldipl; +#ifdef WITNESS + struct lock_object mtx_lock_obj; +#endif }; /* @@ -47,10 +52,16 @@ struct mutex { #define __MUTEX_IPL(ipl) (ipl) #endif -#define MUTEX_INITIALIZER(ipl) { NULL, __MUTEX_IPL(ipl), IPL_NONE } +#ifdef WITNESS +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { NULL, __MUTEX_IPL(ipl), IPL_NONE, MTX_LO_INITIALIZER(name, flags) } +#else +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { NULL, __MUTEX_IPL(ipl), IPL_NONE } +#endif void __mtx_init(struct mutex *, int); -#define mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) +#define _mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) #ifdef DIAGNOSTIC #define MUTEX_ASSERT_LOCKED(mtx) do { \ @@ -67,6 +78,7 @@ void __mtx_init(struct mutex *, int); #define MUTEX_ASSERT_UNLOCKED(mtx) do { } while (0) #endif +#define MUTEX_LOCK_OBJECT(mtx) (&(mtx)->mtx_lock_obj) #define MUTEX_OLDIPL(mtx) ((mtx)->mtx_oldipl) #endif diff --git a/sys/arch/powerpc/powerpc/mutex.c b/sys/arch/powerpc/powerpc/mutex.c index 1428dfb595a..699fad5a6ad 100644 --- a/sys/arch/powerpc/powerpc/mutex.c +++ b/sys/arch/powerpc/powerpc/mutex.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.c,v 1.3 2016/03/19 11:34:22 mpi Exp $ */ +/* $OpenBSD: mutex.c,v 1.4 2017/04/20 13:57:30 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -54,13 +54,13 @@ extern int __mp_lock_spinout; #endif void -mtx_enter(struct mutex *mtx) +__mtx_enter(struct mutex *mtx) { #if defined(MP_LOCKDEBUG) int nticks = __mp_lock_spinout; #endif - while (mtx_enter_try(mtx) == 0) { + while (__mtx_enter_try(mtx) == 0) { SPINLOCK_SPIN_HOOK; #if defined(MP_LOCKDEBUG) @@ -73,7 +73,7 @@ mtx_enter(struct mutex *mtx) } int -mtx_enter_try(struct mutex *mtx) +__mtx_enter_try(struct mutex *mtx) { struct cpu_info *owner, *ci = curcpu(); int s; @@ -103,7 +103,7 @@ mtx_enter_try(struct mutex *mtx) } #else void -mtx_enter(struct mutex *mtx) +__mtx_enter(struct mutex *mtx) { struct cpu_info *ci = curcpu(); @@ -122,15 +122,15 @@ mtx_enter(struct mutex *mtx) } int -mtx_enter_try(struct mutex *mtx) +__mtx_enter_try(struct mutex *mtx) { - mtx_enter(mtx); + __mtx_enter(mtx); return (1); } #endif void -mtx_leave(struct mutex *mtx) +__mtx_leave(struct mutex *mtx) { int s; diff --git a/sys/arch/sh/include/mutex.h b/sys/arch/sh/include/mutex.h index 658b3671dc6..87c82d2b5d4 100644 --- a/sys/arch/sh/include/mutex.h +++ b/sys/arch/sh/include/mutex.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.h,v 1.2 2007/05/05 12:06:20 miod Exp $ */ +/* $OpenBSD: mutex.h,v 1.3 2017/04/20 13:57:30 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -37,9 +37,10 @@ struct mutex { int mtx_oldipl; }; -void mtx_init(struct mutex *, int); +void __mtx_init(struct mutex *, int); -#define MUTEX_INITIALIZER(ipl) { 0, (ipl) << 4, 0 } +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { 0, (ipl) << 4, 0 } #ifdef DIAGNOSTIC #define MUTEX_ASSERT_LOCKED(mtx) do { \ diff --git a/sys/arch/sh/sh/mutex.c b/sys/arch/sh/sh/mutex.c index 24b1814250c..3f8cf57f4fd 100644 --- a/sys/arch/sh/sh/mutex.c +++ b/sys/arch/sh/sh/mutex.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.c,v 1.7 2011/04/21 04:34:12 miod Exp $ */ +/* $OpenBSD: mutex.c,v 1.8 2017/04/20 13:57:30 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -40,7 +40,7 @@ * raising semantics of the mutexes. */ void -mtx_init(struct mutex *mtx, int wantipl) +__mtx_init(struct mutex *mtx, int wantipl) { mtx->mtx_oldipl = 0; mtx->mtx_wantipl = wantipl << 4; @@ -48,7 +48,7 @@ mtx_init(struct mutex *mtx, int wantipl) } void -mtx_enter(struct mutex *mtx) +__mtx_enter(struct mutex *mtx) { if (mtx->mtx_wantipl != IPL_NONE << 4) mtx->mtx_oldipl = _cpu_intr_raise(mtx->mtx_wantipl); @@ -61,7 +61,7 @@ mtx_enter(struct mutex *mtx) } int -mtx_enter_try(struct mutex *mtx) +__mtx_enter_try(struct mutex *mtx) { if (mtx->mtx_wantipl != IPL_NONE) mtx->mtx_oldipl = _cpu_intr_raise(mtx->mtx_wantipl); @@ -75,7 +75,7 @@ mtx_enter_try(struct mutex *mtx) } void -mtx_leave(struct mutex *mtx) +__mtx_leave(struct mutex *mtx) { MUTEX_ASSERT_LOCKED(mtx); #ifdef DIAGNOSTIC diff --git a/sys/arch/sparc64/include/mutex.h b/sys/arch/sparc64/include/mutex.h index 0ac9deb5b5e..c022930143f 100644 --- a/sys/arch/sparc64/include/mutex.h +++ b/sys/arch/sparc64/include/mutex.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.h,v 1.4 2014/03/29 18:09:30 guenther Exp $ */ +/* $OpenBSD: mutex.h,v 1.5 2017/04/20 13:57:30 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -28,10 +28,15 @@ #ifndef _MACHINE_MUTEX_H_ #define _MACHINE_MUTEX_H_ +#include + struct mutex { volatile void *mtx_owner; /* mutex.S relies upon this being first */ int mtx_wantipl; int mtx_oldipl; +#ifdef WITNESS + struct lock_object mtx_lock_obj; +#endif }; /* @@ -48,10 +53,16 @@ struct mutex { #define __MUTEX_IPL(ipl) (ipl) #endif -#define MUTEX_INITIALIZER(ipl) { NULL, __MUTEX_IPL((ipl)), 0 } +#ifdef WITNESS +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { NULL, __MUTEX_IPL((ipl)), 0, MTX_LO_INITIALIZER(name, flags) } +#else +#define MUTEX_INITIALIZER_FLAGS(ipl, name, flags) \ + { NULL, __MUTEX_IPL((ipl)), 0 } +#endif void __mtx_init(struct mutex *, int); -#define mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) +#define _mtx_init(mtx, ipl) __mtx_init((mtx), __MUTEX_IPL((ipl))) #ifdef DIAGNOSTIC #define MUTEX_ASSERT_LOCKED(mtx) do { \ @@ -68,6 +79,7 @@ void __mtx_init(struct mutex *, int); #define MUTEX_ASSERT_UNLOCKED(mtx) do { } while (0) #endif +#define MUTEX_LOCK_OBJECT(mtx) (&(mtx)->mtx_lock_obj) #define MUTEX_OLDIPL(mtx) (mtx)->mtx_oldipl #endif diff --git a/sys/arch/sparc64/sparc64/mutex.S b/sys/arch/sparc64/sparc64/mutex.S index bfdfdf4e5c5..72816ee273d 100644 --- a/sys/arch/sparc64/sparc64/mutex.S +++ b/sys/arch/sparc64/sparc64/mutex.S @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.S,v 1.8 2013/07/14 21:22:09 kettenis Exp $ */ +/* $OpenBSD: mutex.S,v 1.9 2017/04/20 13:57:30 visa Exp $ */ /* * Copyright (c) 2007 Mark Kettenis @@ -44,7 +44,7 @@ ENTRY(__mtx_init) retl stw %g0, [%o0 + MTX_OLDIPL] -ENTRY(mtx_enter) +ENTRY(__mtx_enter) rdpr %pil, %g4 GET_CURCPU(%g1) 1: @@ -80,7 +80,7 @@ ENTRY(mtx_enter) retl membar #LoadLoad | #LoadStore -ENTRY(mtx_enter_try) +ENTRY(__mtx_enter_try) rdpr %pil, %g4 GET_CURCPU(%g1) 1: @@ -112,7 +112,7 @@ ENTRY(mtx_enter_try) retl mov 1, %o0 -ENTRY(mtx_leave) +ENTRY(__mtx_leave) #ifdef DIAGNOSTIC GET_CURCPU(%g1) ld [%g1 + CI_MUTEX_LEVEL], %g5 diff --git a/sys/conf/files b/sys/conf/files index 7942262486a..cc86def23f2 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1,4 +1,4 @@ -# $OpenBSD: files,v 1.640 2017/04/20 12:59:36 visa Exp $ +# $OpenBSD: files,v 1.641 2017/04/20 13:57:30 visa Exp $ # $NetBSD: files,v 1.87 1996/05/19 17:17:50 jonathan Exp $ # @(#)files.newconf 7.5 (Berkeley) 5/10/93 @@ -662,6 +662,7 @@ file kern/kern_ktrace.c ktrace file kern/kern_lock.c file kern/kern_malloc.c file kern/kern_malloc_debug.c malloc_debug +file kern/kern_mutex.c witness file kern/kern_rwlock.c file kern/kern_physio.c file kern/kern_proc.c diff --git a/sys/kern/kern_mutex.c b/sys/kern/kern_mutex.c new file mode 100644 index 00000000000..6ede70b2d80 --- /dev/null +++ b/sys/kern/kern_mutex.c @@ -0,0 +1,69 @@ +/* $OpenBSD: kern_mutex.c,v 1.1 2017/04/20 13:57:30 visa Exp $ */ + +/* + * Copyright (c) 2017 Visa Hankala + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +void +_mtx_init_flags(struct mutex *m, int ipl, const char *name, int flags, + struct lock_type *type) +{ + struct lock_object *lo = MUTEX_LOCK_OBJECT(m); + + lo->lo_flags = MTX_LO_FLAGS(flags); + if (name != NULL) + lo->lo_name = name; + else + lo->lo_name = type->lt_name; + WITNESS_INIT(lo, type); + + _mtx_init(m, ipl); +} + +void +_mtx_enter(struct mutex *m, const char *file, int line) +{ + struct lock_object *lo = MUTEX_LOCK_OBJECT(m); + + WITNESS_CHECKORDER(lo, LOP_EXCLUSIVE | LOP_NEWORDER, file, line, NULL); + __mtx_enter(m); + WITNESS_LOCK(lo, LOP_EXCLUSIVE, file, line); +} + +int +_mtx_enter_try(struct mutex *m, const char *file, int line) +{ + struct lock_object *lo = MUTEX_LOCK_OBJECT(m); + + if (__mtx_enter_try(m)) { + WITNESS_LOCK(lo, LOP_EXCLUSIVE, file, line); + return 1; + } + return 0; +} + +void +_mtx_leave(struct mutex *m, const char *file, int line) +{ + struct lock_object *lo = MUTEX_LOCK_OBJECT(m); + + WITNESS_UNLOCK(lo, LOP_EXCLUSIVE, file, line); + __mtx_leave(m); +} diff --git a/sys/kern/kern_synch.c b/sys/kern/kern_synch.c index 79ff976529a..9925025e304 100644 --- a/sys/kern/kern_synch.c +++ b/sys/kern/kern_synch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_synch.c,v 1.139 2017/04/20 13:33:00 visa Exp $ */ +/* $OpenBSD: kern_synch.c,v 1.140 2017/04/20 13:57:30 visa Exp $ */ /* $NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $ */ /* @@ -170,6 +170,7 @@ msleep(const volatile void *ident, struct mutex *mtx, int priority, #ifdef MULTIPROCESSOR int hold_count; #endif + WITNESS_SAVE_DECL(lock_fl); KASSERT((priority & ~(PRIMASK | PCATCH | PNORELOCK)) == 0); KASSERT(mtx != NULL); @@ -202,6 +203,8 @@ msleep(const volatile void *ident, struct mutex *mtx, int priority, sleep_setup_timeout(&sls, timo); sleep_setup_signal(&sls, priority); + WITNESS_SAVE(MUTEX_LOCK_OBJECT(mtx), lock_fl); + /* XXX - We need to make sure that the mutex doesn't * unblock splsched. This can be made a bit more * correct when the sched_lock is a mutex. @@ -217,6 +220,7 @@ msleep(const volatile void *ident, struct mutex *mtx, int priority, if ((priority & PNORELOCK) == 0) { mtx_enter(mtx); MUTEX_OLDIPL(mtx) = spl; /* put the ipl back */ + WITNESS_RESTORE(MUTEX_LOCK_OBJECT(mtx), lock_fl); } else splx(spl); diff --git a/sys/sys/mutex.h b/sys/sys/mutex.h index 227470f5d70..9b656d64cfc 100644 --- a/sys/sys/mutex.h +++ b/sys/sys/mutex.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mutex.h,v 1.7 2009/08/13 13:24:55 weingart Exp $ */ +/* $OpenBSD: mutex.h,v 1.8 2017/04/20 13:57:30 visa Exp $ */ /* * Copyright (c) 2004 Artur Grabowski @@ -44,14 +44,69 @@ #include +#define MTX_LO_FLAGS(flags) \ + ((!((flags) & MTX_NOWITNESS) ? LO_WITNESS : 0) | \ + ((flags) & MTX_DUPOK ? LO_DUPOK : 0) | \ + LO_INITIALIZED | (LO_CLASS_MUTEX << LO_CLASSSHIFT)) + +#define __MTX_S(x) #x +#define __MTX_LINE __MTX_S(__LINE__) +#define __MTX_NAME __FILE__ ":" __MTX_S(__LINE__) + +#define MTX_LO_INITIALIZER(name, flags) \ + { .lo_type = &(struct lock_type){ .lt_name = __MTX_NAME }, \ + .lo_name = (name) != NULL ? (name) : __MTX_NAME, \ + .lo_flags = MTX_LO_FLAGS(flags) } + +#define MTX_NOWITNESS 0x01 +#define MTX_DUPOK 0x02 + +#define MUTEX_INITIALIZER(ipl) \ + MUTEX_INITIALIZER_FLAGS(ipl, NULL, 0) + /* * Some architectures need to do magic for the ipl, so they need a macro. */ -#ifndef mtx_init -void mtx_init(struct mutex *, int); +#ifndef _mtx_init +void _mtx_init(struct mutex *, int); #endif -void mtx_enter(struct mutex *); -void mtx_leave(struct mutex *); -int mtx_enter_try(struct mutex *); + +void __mtx_enter(struct mutex *); +int __mtx_enter_try(struct mutex *); +void __mtx_leave(struct mutex *); + +#ifdef WITNESS + +void _mtx_init_flags(struct mutex *, int, const char *, int, + struct lock_type *); + +void _mtx_enter(struct mutex *, const char *, int); +int _mtx_enter_try(struct mutex *, const char *, int); +void _mtx_leave(struct mutex *, const char *, int); + +#define mtx_init_flags(m, ipl, name, flags) do { \ + static struct lock_type __lock_type = { .lt_name = #m }; \ + _mtx_init_flags(m, ipl, name, flags, &__lock_type); \ +} while (0) + +#define mtx_init(m, ipl) mtx_init_flags(m, ipl, NULL, 0) +#define mtx_enter(m) _mtx_enter(m, __FILE__, __LINE__) +#define mtx_enter_try(m) _mtx_enter_try(m, __FILE__, __LINE__) +#define mtx_leave(m) _mtx_leave(m, __FILE__, __LINE__) + +#else /* WITNESS */ + +#define mtx_init(m, ipl) __mtx_init(m, ipl) + +#define mtx_init_flags(m, ipl, name, flags) do { \ + (void)(name); (void)(flags); \ + __mtx_init(m, ipl); \ +} while (0) + +#define mtx_enter __mtx_enter +#define mtx_leave __mtx_leave +#define mtx_enter_try __mtx_enter_try + +#endif /* WITNESS */ #endif -- 2.20.1