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_MUTEX_H_ 30 #define _LINUXKPI_LINUX_MUTEX_H_ 31 32 #include <sys/param.h> 33 #include <sys/proc.h> 34 #include <sys/lock.h> 35 #include <sys/sx.h> 36 37 #include <linux/kernel.h> 38 #include <linux/cleanup.h> 39 #include <linux/list.h> 40 #include <linux/spinlock.h> 41 #include <asm/atomic.h> 42 43 typedef struct mutex { 44 struct sx sx; 45 } mutex_t; 46 47 /* 48 * By defining CONFIG_NO_MUTEX_SKIP LinuxKPI mutexes and asserts will 49 * not be skipped during panic(). 50 */ 51 #ifdef CONFIG_NO_MUTEX_SKIP 52 #define MUTEX_SKIP(void) 0 53 #else 54 #define MUTEX_SKIP(void) unlikely(SCHEDULER_STOPPED() || kdb_active) 55 #endif 56 57 #define mutex_lock(_m) do { \ 58 if (MUTEX_SKIP()) \ 59 break; \ 60 sx_xlock(&(_m)->sx); \ 61 } while (0) 62 63 #define mutex_lock_nested(_m, _s) mutex_lock(_m) 64 #define mutex_lock_nest_lock(_m, _s) mutex_lock(_m) 65 66 #define mutex_lock_interruptible(_m) ({ \ 67 MUTEX_SKIP() ? 0 : \ 68 linux_mutex_lock_interruptible(_m); \ 69 }) 70 71 #define mutex_lock_interruptible_nested(m, c) mutex_lock_interruptible(m) 72 73 /* 74 * Reuse the interruptable method since the SX 75 * lock handles both signals and interrupts: 76 */ 77 #define mutex_lock_killable(_m) ({ \ 78 MUTEX_SKIP() ? 0 : \ 79 linux_mutex_lock_interruptible(_m); \ 80 }) 81 82 #define mutex_lock_killable_nested(_m, _sub) \ 83 mutex_lock_killable(_m) 84 85 #define mutex_unlock(_m) do { \ 86 if (MUTEX_SKIP()) \ 87 break; \ 88 sx_xunlock(&(_m)->sx); \ 89 } while (0) 90 91 #define mutex_trylock(_m) ({ \ 92 MUTEX_SKIP() ? 1 : \ 93 !!sx_try_xlock(&(_m)->sx); \ 94 }) 95 96 enum mutex_trylock_recursive_enum { 97 MUTEX_TRYLOCK_FAILED = 0, 98 MUTEX_TRYLOCK_SUCCESS = 1, 99 MUTEX_TRYLOCK_RECURSIVE = 2, 100 }; 101 102 static inline __must_check enum mutex_trylock_recursive_enum 103 mutex_trylock_recursive(struct mutex *lock) 104 { 105 if (unlikely(sx_xholder(&lock->sx) == curthread)) 106 return (MUTEX_TRYLOCK_RECURSIVE); 107 108 return (mutex_trylock(lock)); 109 } 110 111 #define mutex_init(_m) \ 112 linux_mutex_init(_m, mutex_name(#_m), SX_NOWITNESS) 113 114 #define __mutex_init(_m, _n, _l) \ 115 linux_mutex_init(_m, _n, SX_NOWITNESS) 116 117 #define mutex_init_witness(_m) \ 118 linux_mutex_init(_m, mutex_name(#_m), SX_DUPOK) 119 120 #define mutex_destroy(_m) \ 121 linux_mutex_destroy(_m) 122 123 static inline bool 124 mutex_is_locked(mutex_t *m) 125 { 126 return ((struct thread *)SX_OWNER(m->sx.sx_lock) != NULL); 127 } 128 129 static inline bool 130 mutex_is_owned(mutex_t *m) 131 { 132 return (sx_xlocked(&m->sx)); 133 } 134 135 static inline int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *m) 136 { 137 if (atomic_dec_and_test(cnt)) { 138 mutex_lock(m); 139 return (1); 140 } 141 142 return (0); 143 } 144 145 #ifdef WITNESS_ALL 146 /* NOTE: the maximum WITNESS name is 64 chars */ 147 #define __mutex_name(name, file, line) \ 148 (((const char *){file ":" #line "-" name}) + \ 149 (sizeof(file) > 16 ? sizeof(file) - 16 : 0)) 150 #else 151 #define __mutex_name(name, file, line) name 152 #endif 153 #define _mutex_name(...) __mutex_name(__VA_ARGS__) 154 #define mutex_name(name) _mutex_name(name, __FILE__, __LINE__) 155 156 #define DEFINE_MUTEX(lock) \ 157 mutex_t lock; \ 158 SX_SYSINIT_FLAGS(lock, &(lock).sx, mutex_name(#lock), SX_DUPOK) 159 160 static inline void 161 linux_mutex_init(mutex_t *m, const char *name, int flags) 162 { 163 memset(m, 0, sizeof(*m)); 164 sx_init_flags(&m->sx, name, flags); 165 } 166 167 static inline void 168 linux_mutex_destroy(mutex_t *m) 169 { 170 if (mutex_is_owned(m)) 171 mutex_unlock(m); 172 sx_destroy(&m->sx); 173 } 174 175 extern int linux_mutex_lock_interruptible(mutex_t *m); 176 177 #endif /* _LINUXKPI_LINUX_MUTEX_H_ */ 178