xref: /freebsd/sys/compat/linuxkpi/common/include/linux/mutex.h (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	_LINUXKPI_LINUX_MUTEX_H_
32 #define	_LINUXKPI_LINUX_MUTEX_H_
33 
34 #include <sys/param.h>
35 #include <sys/proc.h>
36 #include <sys/lock.h>
37 #include <sys/sx.h>
38 
39 #include <linux/kernel.h>
40 #include <linux/list.h>
41 #include <linux/spinlock.h>
42 #include <asm/atomic.h>
43 
44 typedef struct mutex {
45 	struct sx sx;
46 } mutex_t;
47 
48 /*
49  * By defining CONFIG_NO_MUTEX_SKIP LinuxKPI mutexes and asserts will
50  * not be skipped during panic().
51  */
52 #ifdef CONFIG_NO_MUTEX_SKIP
53 #define	MUTEX_SKIP(void) 0
54 #else
55 #define	MUTEX_SKIP(void) unlikely(SCHEDULER_STOPPED() || kdb_active)
56 #endif
57 
58 #define	mutex_lock(_m) do {			\
59 	if (MUTEX_SKIP())			\
60 		break;				\
61 	sx_xlock(&(_m)->sx);			\
62 } while (0)
63 
64 #define	mutex_lock_nested(_m, _s)	mutex_lock(_m)
65 #define	mutex_lock_nest_lock(_m, _s)	mutex_lock(_m)
66 
67 #define	mutex_lock_interruptible(_m) ({		\
68 	MUTEX_SKIP() ? 0 :			\
69 	linux_mutex_lock_interruptible(_m);	\
70 })
71 
72 #define	mutex_lock_interruptible_nested(m, c)	mutex_lock_interruptible(m)
73 
74 /*
75  * Reuse the interruptable method since the SX
76  * lock handles both signals and interrupts:
77  */
78 #define	mutex_lock_killable(_m) ({		\
79 	MUTEX_SKIP() ? 0 :			\
80 	linux_mutex_lock_interruptible(_m);	\
81 })
82 
83 #define	mutex_lock_killable_nested(_m, _sub)	\
84 	mutex_lock_killable(_m)
85 
86 #define	mutex_unlock(_m) do {			\
87 	if (MUTEX_SKIP())			\
88 		break;				\
89 	sx_xunlock(&(_m)->sx);			\
90 } while (0)
91 
92 #define	mutex_trylock(_m) ({			\
93 	MUTEX_SKIP() ? 1 :			\
94 	!!sx_try_xlock(&(_m)->sx);		\
95 })
96 
97 enum mutex_trylock_recursive_enum {
98 	MUTEX_TRYLOCK_FAILED = 0,
99 	MUTEX_TRYLOCK_SUCCESS = 1,
100 	MUTEX_TRYLOCK_RECURSIVE = 2,
101 };
102 
103 static inline __must_check enum mutex_trylock_recursive_enum
104 mutex_trylock_recursive(struct mutex *lock)
105 {
106 	if (unlikely(sx_xholder(&lock->sx) == curthread))
107 		return (MUTEX_TRYLOCK_RECURSIVE);
108 
109 	return (mutex_trylock(lock));
110 }
111 
112 #define	mutex_init(_m) \
113 	linux_mutex_init(_m, mutex_name(#_m), SX_NOWITNESS)
114 
115 #define	__mutex_init(_m, _n, _l) \
116 	linux_mutex_init(_m, _n, SX_NOWITNESS)
117 
118 #define	mutex_init_witness(_m) \
119 	linux_mutex_init(_m, mutex_name(#_m), SX_DUPOK)
120 
121 #define	mutex_destroy(_m) \
122 	linux_mutex_destroy(_m)
123 
124 static inline bool
125 mutex_is_locked(mutex_t *m)
126 {
127 	return ((struct thread *)SX_OWNER(m->sx.sx_lock) != NULL);
128 }
129 
130 static inline bool
131 mutex_is_owned(mutex_t *m)
132 {
133 	return (sx_xlocked(&m->sx));
134 }
135 
136 static inline int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *m)
137 {
138 	if (atomic_dec_and_test(cnt)) {
139 		mutex_lock(m);
140 		return (1);
141 	}
142 
143 	return (0);
144 }
145 
146 #ifdef WITNESS_ALL
147 /* NOTE: the maximum WITNESS name is 64 chars */
148 #define	__mutex_name(name, file, line)		\
149 	(((const char *){file ":" #line "-" name}) +	\
150 	(sizeof(file) > 16 ? sizeof(file) - 16 : 0))
151 #else
152 #define	__mutex_name(name, file, line)	name
153 #endif
154 #define	_mutex_name(...)	__mutex_name(__VA_ARGS__)
155 #define	mutex_name(name)	_mutex_name(name, __FILE__, __LINE__)
156 
157 #define	DEFINE_MUTEX(lock)						\
158 	mutex_t lock;							\
159 	SX_SYSINIT_FLAGS(lock, &(lock).sx, mutex_name(#lock), SX_DUPOK)
160 
161 static inline void
162 linux_mutex_init(mutex_t *m, const char *name, int flags)
163 {
164 	memset(m, 0, sizeof(*m));
165 	sx_init_flags(&m->sx, name, flags);
166 }
167 
168 static inline void
169 linux_mutex_destroy(mutex_t *m)
170 {
171 	if (mutex_is_owned(m))
172 		mutex_unlock(m);
173 	sx_destroy(&m->sx);
174 }
175 
176 extern int linux_mutex_lock_interruptible(mutex_t *m);
177 
178 #endif					/* _LINUXKPI_LINUX_MUTEX_H_ */
179