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