xref: /freebsd/sys/compat/linuxkpi/common/include/linux/spinlock.h (revision 3e8eb5c7f4909209c042403ddee340b2ee7003a5)
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_SPINLOCK_H_
32 #define	_LINUXKPI_LINUX_SPINLOCK_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/compiler.h>
42 #include <linux/rwlock.h>
43 #include <linux/bottom_half.h>
44 #include <linux/lockdep.h>
45 
46 typedef struct {
47 	struct mtx m;
48 } 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)->m);			\
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)->m);			\
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)->m);	\
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)->m, 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 	spin_unlock(_l);				\
132 } while (0)
133 
134 #ifdef WITNESS_ALL
135 /* NOTE: the maximum WITNESS name is 64 chars */
136 #define	__spin_lock_name(name, file, line)		\
137 	(((const char *){file ":" #line "-" name}) +	\
138 	(sizeof(file) > 16 ? sizeof(file) - 16 : 0))
139 #else
140 #define	__spin_lock_name(name, file, line)	name
141 #endif
142 #define	_spin_lock_name(...)		__spin_lock_name(__VA_ARGS__)
143 #define	spin_lock_name(name)		_spin_lock_name(name, __FILE__, __LINE__)
144 
145 #define	spin_lock_init(lock)	linux_spin_lock_init(lock, spin_lock_name("lnxspin"))
146 
147 static inline void
148 linux_spin_lock_init(spinlock_t *lock, const char *name)
149 {
150 
151 	memset(lock, 0, sizeof(*lock));
152 	mtx_init(&lock->m, name, NULL, MTX_DEF | MTX_NOWITNESS);
153 }
154 
155 static inline void
156 spin_lock_destroy(spinlock_t *lock)
157 {
158 
159        mtx_destroy(&lock->m);
160 }
161 
162 #define	DEFINE_SPINLOCK(lock)					\
163 	spinlock_t lock;					\
164 	MTX_SYSINIT(lock, &(lock).m, spin_lock_name("lnxspin"), MTX_DEF)
165 
166 #define	assert_spin_locked(_l) do {		\
167 	if (SPIN_SKIP())			\
168 		break;				\
169 	mtx_assert(&(_l)->m, MA_OWNED);		\
170 } while (0)
171 
172 #define	atomic_dec_and_lock_irqsave(cnt, lock, flags) \
173 	_atomic_dec_and_lock_irqsave(cnt, lock, &(flags))
174 static inline int
175 _atomic_dec_and_lock_irqsave(atomic_t *cnt, spinlock_t *lock,
176     unsigned long *flags)
177 {
178 	if (atomic_add_unless(cnt, -1, 1))
179 		return (0);
180 
181 	spin_lock_irqsave(lock, *flags);
182 	if (atomic_dec_and_test(cnt))
183 		return (1);
184 	spin_unlock_irqrestore(lock, *flags);
185 	return (0);
186 }
187 
188 #endif					/* _LINUXKPI_LINUX_SPINLOCK_H_ */
189