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 (void)(flags); \ 132 spin_unlock(_l); \ 133 } while (0) 134 135 #ifdef WITNESS_ALL 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) linux_spin_lock_init(lock, spin_lock_name("lnxspin")) 147 148 static inline void 149 linux_spin_lock_init(spinlock_t *lock, const char *name) 150 { 151 152 memset(lock, 0, sizeof(*lock)); 153 mtx_init(&lock->m, name, NULL, MTX_DEF | MTX_NOWITNESS); 154 } 155 156 static inline void 157 spin_lock_destroy(spinlock_t *lock) 158 { 159 160 mtx_destroy(&lock->m); 161 } 162 163 #define DEFINE_SPINLOCK(lock) \ 164 spinlock_t lock; \ 165 MTX_SYSINIT(lock, &(lock).m, spin_lock_name("lnxspin"), MTX_DEF) 166 167 #define assert_spin_locked(_l) do { \ 168 if (SPIN_SKIP()) \ 169 break; \ 170 mtx_assert(&(_l)->m, MA_OWNED); \ 171 } while (0) 172 173 #define atomic_dec_and_lock_irqsave(cnt, lock, flags) \ 174 _atomic_dec_and_lock_irqsave(cnt, lock, &(flags)) 175 static inline int 176 _atomic_dec_and_lock_irqsave(atomic_t *cnt, spinlock_t *lock, 177 unsigned long *flags) 178 { 179 if (atomic_add_unless(cnt, -1, 1)) 180 return (0); 181 182 spin_lock_irqsave(lock, *flags); 183 if (atomic_dec_and_test(cnt)) 184 return (1); 185 spin_unlock_irqrestore(lock, *flags); 186 return (0); 187 } 188 189 #endif /* _LINUXKPI_LINUX_SPINLOCK_H_ */ 190