xref: /freebsd/sys/compat/linuxkpi/common/include/linux/spinlock.h (revision b43c4d84c8d172858fc4bbf59e07888bfb8ac472)
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 #ifndef	_LINUXKPI_LINUX_SPINLOCK_H_
30 #define	_LINUXKPI_LINUX_SPINLOCK_H_
31 
32 #include "opt_linuxkpi.h"
33 
34 #include <asm/atomic.h>
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/kdb.h>
40 
41 #include <linux/cleanup.h>
42 #include <linux/compiler.h>
43 #include <linux/rwlock.h>
44 #include <linux/bottom_half.h>
45 #include <linux/lockdep.h>
46 #include <linux/preempt.h>
47 
48 typedef struct mtx spinlock_t;
49 
50 /*
51  * By defining CONFIG_SPIN_SKIP LinuxKPI spinlocks and asserts will be
52  * skipped during panic(). By default it is disabled due to
53  * performance reasons.
54  */
55 #ifdef CONFIG_SPIN_SKIP
56 #define	SPIN_SKIP(void)	unlikely(SCHEDULER_STOPPED() || kdb_active)
57 #else
58 #define	SPIN_SKIP(void) 0
59 #endif
60 
61 #define	spin_lock(_l) do {			\
62 	if (SPIN_SKIP())			\
63 		break;				\
64 	mtx_lock(_l);				\
65 	local_bh_disable();			\
66 } while (0)
67 
68 #define	spin_lock_bh(_l) do {			\
69 	spin_lock(_l);				\
70 	local_bh_disable();			\
71 } while (0)
72 
73 #define	spin_lock_irq(_l) do {			\
74 	spin_lock(_l);				\
75 } while (0)
76 
77 #define	spin_unlock(_l)	do {			\
78 	if (SPIN_SKIP())			\
79 		break;				\
80 	local_bh_enable();			\
81 	mtx_unlock(_l);				\
82 } while (0)
83 
84 #define	spin_unlock_bh(_l) do {			\
85 	local_bh_enable();			\
86 	spin_unlock(_l);			\
87 } while (0)
88 
89 #define	spin_unlock_irq(_l) do {		\
90 	spin_unlock(_l);			\
91 } while (0)
92 
93 #define	spin_trylock(_l) ({			\
94 	int __ret;				\
95 	if (SPIN_SKIP()) {			\
96 		__ret = 1;			\
97 	} else {				\
98 		__ret = mtx_trylock(_l);	\
99 		if (likely(__ret != 0))		\
100 			local_bh_disable();	\
101 	}					\
102 	__ret;					\
103 })
104 
105 #define	spin_trylock_irq(_l)			\
106 	spin_trylock(_l)
107 
108 #define	spin_trylock_irqsave(_l, flags) ({	\
109 	(flags) = 0;				\
110 	spin_trylock(_l);			\
111 })
112 
113 #define	spin_lock_nested(_l, _n) do {		\
114 	if (SPIN_SKIP())			\
115 		break;				\
116 	mtx_lock_flags(_l, MTX_DUPOK);		\
117 	local_bh_disable();			\
118 } while (0)
119 
120 #define	spin_lock_irqsave(_l, flags) do {	\
121 	(flags) = 0;				\
122 	spin_lock(_l);				\
123 } while (0)
124 
125 #define	spin_lock_irqsave_nested(_l, flags, _n) do {	\
126 	(flags) = 0;					\
127 	spin_lock_nested(_l, _n);			\
128 } while (0)
129 
130 #define	spin_unlock_irqrestore(_l, flags) do {		\
131 	(void)(flags);					\
132 	spin_unlock(_l);				\
133 } while (0)
134 
135 #if defined(LINUXKPI_DEBUG) && defined(WITNESS)
136 /* NOTE: the maximum WITNESS name is 64 chars */
137 #define	__spin_lock_name(name, file, line)		\
138 	(((const char *){file ":" #line "-" name}) +	\
139 	(sizeof(file) > 16 ? sizeof(file) - 16 : 0))
140 #else
141 #define	__spin_lock_name(name, file, line)	name
142 #endif
143 #define	_spin_lock_name(...)		__spin_lock_name(__VA_ARGS__)
144 #define	spin_lock_name(name)		_spin_lock_name(name, __FILE__, __LINE__)
145 
146 #define	spin_lock_init(lock)	mtx_init(lock, spin_lock_name("lnxspin"), \
147 				  NULL, MTX_DEF | _LKPI_MTX_NOWITNESS | MTX_NEW)
148 
149 #define	spin_lock_destroy(_l)	mtx_destroy(_l)
150 
151 #define	DEFINE_SPINLOCK(lock)					\
152 	spinlock_t lock;					\
153 	MTX_SYSINIT(lock, &lock, spin_lock_name("lnxspin"), MTX_DEF)
154 
155 #define	assert_spin_locked(_l) do {		\
156 	if (SPIN_SKIP())			\
157 		break;				\
158 	mtx_assert(_l, MA_OWNED);		\
159 } while (0)
160 
161 #define	local_irq_save(flags) do {		\
162 	(flags) = 0;				\
163 } while (0)
164 
165 #define	local_irq_restore(flags) do {		\
166 	(void)(flags);				\
167 } while (0)
168 
169 #define	atomic_dec_and_lock_irqsave(cnt, lock, flags) \
170 	_atomic_dec_and_lock_irqsave(cnt, lock, &(flags))
171 static inline int
172 _atomic_dec_and_lock_irqsave(atomic_t *cnt, spinlock_t *lock,
173     unsigned long *flags)
174 {
175 	if (atomic_add_unless(cnt, -1, 1))
176 		return (0);
177 
178 	spin_lock_irqsave(lock, *flags);
179 	if (atomic_dec_and_test(cnt))
180 		return (1);
181 	spin_unlock_irqrestore(lock, *flags);
182 	return (0);
183 }
184 
185 /*
186  * struct raw_spinlock
187  */
188 
189 typedef struct raw_spinlock {
190 	struct mtx	lock;
191 } raw_spinlock_t;
192 
193 #define	raw_spin_lock_init(rlock) \
194 	mtx_init(&(rlock)->lock, spin_lock_name("lnxspin_raw"), \
195 	    NULL, MTX_DEF | _LKPI_MTX_NOWITNESS | MTX_NEW)
196 
197 #define	raw_spin_lock(rl)	spin_lock(&(rl)->lock)
198 #define	raw_spin_trylock(rl)	spin_trylock(&(rl)->lock)
199 #define	raw_spin_unlock(rl)	spin_unlock(&(rl)->lock)
200 
201 #define	raw_spin_lock_irqsave(rl, f)		spin_lock_irqsave(&(rl)->lock, (f))
202 #define	raw_spin_trylock_irqsave(rl, f)		spin_trylock_irqsave(&(rl)->lock, (f))
203 #define	raw_spin_unlock_irqrestore(rl, f)	spin_unlock_irqrestore(&(rl)->lock, (f))
204 
205 /*
206  * cleanup.h related pre-defined cases.
207  */
208 DEFINE_LOCK_GUARD_1(spinlock_irqsave,
209     spinlock_t,
210     spin_lock_irqsave(_T->lock, _T->flags),
211     spin_unlock_irqrestore(_T->lock, _T->flags),
212     unsigned long flags)
213 
214 #endif					/* _LINUXKPI_LINUX_SPINLOCK_H_ */
215