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 _LINUX_MUTEX_H_ 32 #define _LINUX_MUTEX_H_ 33 34 #include <sys/param.h> 35 #include <sys/proc.h> 36 #include <sys/lock.h> 37 #include <sys/sx.h> 38 39 #include <linux/spinlock.h> 40 41 typedef struct mutex { 42 struct sx sx; 43 } mutex_t; 44 45 /* 46 * By defining CONFIG_NO_MUTEX_SKIP LinuxKPI mutexes and asserts will 47 * not be skipped during panic(). 48 */ 49 #ifdef CONFIG_NO_MUTEX_SKIP 50 #define MUTEX_SKIP(void) 0 51 #else 52 #define MUTEX_SKIP(void) unlikely(SCHEDULER_STOPPED() || kdb_active) 53 #endif 54 55 #define mutex_lock(_m) do { \ 56 if (MUTEX_SKIP()) \ 57 break; \ 58 sx_xlock(&(_m)->sx); \ 59 } while (0) 60 61 #define mutex_lock_nested(_m, _s) mutex_lock(_m) 62 #define mutex_lock_nest_lock(_m, _s) mutex_lock(_m) 63 64 #define mutex_lock_interruptible(_m) ({ \ 65 MUTEX_SKIP() ? 0 : \ 66 (sx_xlock_sig(&(_m)->sx) ? -EINTR : 0); \ 67 }) 68 69 #define mutex_unlock(_m) do { \ 70 if (MUTEX_SKIP()) \ 71 break; \ 72 sx_xunlock(&(_m)->sx); \ 73 } while (0) 74 75 #define mutex_trylock(_m) ({ \ 76 MUTEX_SKIP() ? 1 : \ 77 !!sx_try_xlock(&(_m)->sx); \ 78 }) 79 80 #define mutex_init(_m) \ 81 linux_mutex_init(_m, mutex_name(#_m), SX_NOWITNESS) 82 83 #define mutex_init_witness(_m) \ 84 linux_mutex_init(_m, mutex_name(#_m), SX_DUPOK) 85 86 #define mutex_destroy(_m) \ 87 linux_mutex_destroy(_m) 88 89 static inline bool 90 mutex_is_locked(mutex_t *m) 91 { 92 return ((struct thread *)SX_OWNER(m->sx.sx_lock) != NULL); 93 } 94 95 static inline bool 96 mutex_is_owned(mutex_t *m) 97 { 98 return (sx_xlocked(&m->sx)); 99 } 100 101 #ifdef WITNESS_ALL 102 /* NOTE: the maximum WITNESS name is 64 chars */ 103 #define __mutex_name(name, file, line) \ 104 (((const char *){file ":" #line "-" name}) + \ 105 (sizeof(file) > 16 ? sizeof(file) - 16 : 0)) 106 #else 107 #define __mutex_name(name, file, line) name 108 #endif 109 #define _mutex_name(...) __mutex_name(__VA_ARGS__) 110 #define mutex_name(name) _mutex_name(name, __FILE__, __LINE__) 111 112 #define DEFINE_MUTEX(lock) \ 113 mutex_t lock; \ 114 SX_SYSINIT_FLAGS(lock, &(lock).sx, mutex_name(#lock), SX_DUPOK) 115 116 static inline void 117 linux_mutex_init(mutex_t *m, const char *name, int flags) 118 { 119 memset(m, 0, sizeof(*m)); 120 sx_init_flags(&m->sx, name, flags); 121 } 122 123 static inline void 124 linux_mutex_destroy(mutex_t *m) 125 { 126 if (mutex_is_owned(m)) 127 mutex_unlock(m); 128 sx_destroy(&m->sx); 129 } 130 131 #endif /* _LINUX_MUTEX_H_ */ 132