1 /* SPDX-License-Identifier: GPL-2.0 */ 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* rwsem.h: R/W semaphores, public interface 2 /* rwsem.h: R/W semaphores, public interface 3 * 3 * 4 * Written by David Howells (dhowells@redhat.c 4 * Written by David Howells (dhowells@redhat.com). 5 * Derived from asm-i386/semaphore.h 5 * Derived from asm-i386/semaphore.h 6 */ 6 */ 7 7 8 #ifndef _LINUX_RWSEM_H 8 #ifndef _LINUX_RWSEM_H 9 #define _LINUX_RWSEM_H 9 #define _LINUX_RWSEM_H 10 10 11 #include <linux/linkage.h> 11 #include <linux/linkage.h> 12 12 13 #include <linux/types.h> 13 #include <linux/types.h> >> 14 #include <linux/kernel.h> 14 #include <linux/list.h> 15 #include <linux/list.h> 15 #include <linux/spinlock.h> 16 #include <linux/spinlock.h> 16 #include <linux/atomic.h> 17 #include <linux/atomic.h> 17 #include <linux/err.h> 18 #include <linux/err.h> 18 #include <linux/cleanup.h> << 19 << 20 #ifdef CONFIG_DEBUG_LOCK_ALLOC << 21 # define __RWSEM_DEP_MAP_INIT(lockname) << 22 .dep_map = { << 23 .name = #lockname, << 24 .wait_type_inner = LD_WAIT_SLE << 25 }, << 26 #else << 27 # define __RWSEM_DEP_MAP_INIT(lockname) << 28 #endif << 29 << 30 #ifndef CONFIG_PREEMPT_RT << 31 << 32 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 19 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 33 #include <linux/osq_lock.h> 20 #include <linux/osq_lock.h> 34 #endif 21 #endif 35 22 36 /* 23 /* 37 * For an uncontended rwsem, count and owner a 24 * For an uncontended rwsem, count and owner are the only fields a task 38 * needs to touch when acquiring the rwsem. So 25 * needs to touch when acquiring the rwsem. So they are put next to each 39 * other to increase the chance that they will 26 * other to increase the chance that they will share the same cacheline. 40 * 27 * 41 * In a contended rwsem, the owner is likely t 28 * In a contended rwsem, the owner is likely the most frequently accessed 42 * field in the structure as the optimistic wa 29 * field in the structure as the optimistic waiter that holds the osq lock 43 * will spin on owner. For an embedded rwsem, 30 * will spin on owner. For an embedded rwsem, other hot fields in the 44 * containing structure should be moved furthe 31 * containing structure should be moved further away from the rwsem to 45 * reduce the chance that they will share the 32 * reduce the chance that they will share the same cacheline causing 46 * cacheline bouncing problem. 33 * cacheline bouncing problem. 47 */ 34 */ 48 struct rw_semaphore { 35 struct rw_semaphore { 49 atomic_long_t count; 36 atomic_long_t count; 50 /* 37 /* 51 * Write owner or one of the read owne 38 * Write owner or one of the read owners as well flags regarding 52 * the current state of the rwsem. Can 39 * the current state of the rwsem. Can be used as a speculative 53 * check to see if the write owner is 40 * check to see if the write owner is running on the cpu. 54 */ 41 */ 55 atomic_long_t owner; 42 atomic_long_t owner; 56 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 43 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 57 struct optimistic_spin_queue osq; /* s 44 struct optimistic_spin_queue osq; /* spinner MCS lock */ 58 #endif 45 #endif 59 raw_spinlock_t wait_lock; 46 raw_spinlock_t wait_lock; 60 struct list_head wait_list; 47 struct list_head wait_list; 61 #ifdef CONFIG_DEBUG_RWSEMS << 62 void *magic; << 63 #endif << 64 #ifdef CONFIG_DEBUG_LOCK_ALLOC 48 #ifdef CONFIG_DEBUG_LOCK_ALLOC 65 struct lockdep_map dep_map; 49 struct lockdep_map dep_map; 66 #endif 50 #endif 67 }; 51 }; 68 52 69 #define RWSEM_UNLOCKED_VALUE 0UL !! 53 /* 70 #define RWSEM_WRITER_LOCKED (1UL < !! 54 * Setting all bits of the owner field except bit 0 will indicate 71 #define __RWSEM_COUNT_INIT(name) .count !! 55 * that the rwsem is writer-owned with an unknown owner. >> 56 */ >> 57 #define RWSEM_OWNER_UNKNOWN (-2L) 72 58 >> 59 /* In all implementations count != 0 means locked */ 73 static inline int rwsem_is_locked(struct rw_se 60 static inline int rwsem_is_locked(struct rw_semaphore *sem) 74 { 61 { 75 return atomic_long_read(&sem->count) ! !! 62 return atomic_long_read(&sem->count) != 0; 76 } << 77 << 78 static inline void rwsem_assert_held_nolockdep << 79 { << 80 WARN_ON(atomic_long_read(&sem->count) << 81 } 63 } 82 64 83 static inline void rwsem_assert_held_write_nol !! 65 #define RWSEM_UNLOCKED_VALUE 0L 84 { !! 66 #define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE) 85 WARN_ON(!(atomic_long_read(&sem->count << 86 } << 87 67 88 /* Common initializer macros and functions */ 68 /* Common initializer macros and functions */ 89 69 90 #ifdef CONFIG_DEBUG_RWSEMS !! 70 #ifdef CONFIG_DEBUG_LOCK_ALLOC 91 # define __RWSEM_DEBUG_INIT(lockname) .magic = !! 71 # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname } 92 #else 72 #else 93 # define __RWSEM_DEBUG_INIT(lockname) !! 73 # define __RWSEM_DEP_MAP_INIT(lockname) 94 #endif 74 #endif 95 75 96 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 76 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 97 #define __RWSEM_OPT_INIT(lockname) .osq = OSQ_ !! 77 #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED 98 #else 78 #else 99 #define __RWSEM_OPT_INIT(lockname) 79 #define __RWSEM_OPT_INIT(lockname) 100 #endif 80 #endif 101 81 102 #define __RWSEM_INITIALIZER(name) 82 #define __RWSEM_INITIALIZER(name) \ 103 { __RWSEM_COUNT_INIT(name), !! 83 { __RWSEM_INIT_COUNT(name), \ 104 .owner = ATOMIC_LONG_INIT(0), 84 .owner = ATOMIC_LONG_INIT(0), \ 105 __RWSEM_OPT_INIT(name) << 106 .wait_lock = __RAW_SPIN_LOCK_UNLOCKE << 107 .wait_list = LIST_HEAD_INIT((name).w 85 .wait_list = LIST_HEAD_INIT((name).wait_list), \ 108 __RWSEM_DEBUG_INIT(name) !! 86 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \ >> 87 __RWSEM_OPT_INIT(name) \ 109 __RWSEM_DEP_MAP_INIT(name) } 88 __RWSEM_DEP_MAP_INIT(name) } 110 89 111 #define DECLARE_RWSEM(name) \ 90 #define DECLARE_RWSEM(name) \ 112 struct rw_semaphore name = __RWSEM_INI 91 struct rw_semaphore name = __RWSEM_INITIALIZER(name) 113 92 114 extern void __init_rwsem(struct rw_semaphore * 93 extern void __init_rwsem(struct rw_semaphore *sem, const char *name, 115 struct lock_class_key 94 struct lock_class_key *key); 116 95 117 #define init_rwsem(sem) 96 #define init_rwsem(sem) \ 118 do { 97 do { \ 119 static struct lock_class_key __key; 98 static struct lock_class_key __key; \ 120 99 \ 121 __init_rwsem((sem), #sem, &__key); 100 __init_rwsem((sem), #sem, &__key); \ 122 } while (0) 101 } while (0) 123 102 124 /* 103 /* 125 * This is the same regardless of which rwsem 104 * This is the same regardless of which rwsem implementation that is being used. 126 * It is just a heuristic meant to be called b !! 105 * It is just a heuristic meant to be called by somebody alreadying holding the 127 * rwsem to see if somebody from an incompatib 106 * rwsem to see if somebody from an incompatible type is wanting access to the 128 * lock. 107 * lock. 129 */ 108 */ 130 static inline int rwsem_is_contended(struct rw 109 static inline int rwsem_is_contended(struct rw_semaphore *sem) 131 { 110 { 132 return !list_empty(&sem->wait_list); 111 return !list_empty(&sem->wait_list); 133 } 112 } 134 113 135 #else /* !CONFIG_PREEMPT_RT */ << 136 << 137 #include <linux/rwbase_rt.h> << 138 << 139 struct rw_semaphore { << 140 struct rwbase_rt rwbase; << 141 #ifdef CONFIG_DEBUG_LOCK_ALLOC << 142 struct lockdep_map dep_map; << 143 #endif << 144 }; << 145 << 146 #define __RWSEM_INITIALIZER(name) << 147 { << 148 .rwbase = __RWBASE_INITIALIZER << 149 __RWSEM_DEP_MAP_INIT(name) << 150 } << 151 << 152 #define DECLARE_RWSEM(lockname) \ << 153 struct rw_semaphore lockname = __RWSEM << 154 << 155 extern void __init_rwsem(struct rw_semaphore << 156 struct lock_class_ke << 157 << 158 #define init_rwsem(sem) << 159 do { << 160 static struct lock_class_key __key; << 161 << 162 __init_rwsem((sem), #sem, &__key); << 163 } while (0) << 164 << 165 static __always_inline int rwsem_is_locked(con << 166 { << 167 return rw_base_is_locked(&sem->rwbase) << 168 } << 169 << 170 static __always_inline void rwsem_assert_held_ << 171 { << 172 WARN_ON(!rwsem_is_locked(sem)); << 173 } << 174 << 175 static __always_inline void rwsem_assert_held_ << 176 { << 177 WARN_ON(!rw_base_is_write_locked(&sem- << 178 } << 179 << 180 static __always_inline int rwsem_is_contended( << 181 { << 182 return rw_base_is_contended(&sem->rwba << 183 } << 184 << 185 #endif /* CONFIG_PREEMPT_RT */ << 186 << 187 /* << 188 * The functions below are the same for all rw << 189 * the RT specific variant. << 190 */ << 191 << 192 static inline void rwsem_assert_held(const str << 193 { << 194 if (IS_ENABLED(CONFIG_LOCKDEP)) << 195 lockdep_assert_held(sem); << 196 else << 197 rwsem_assert_held_nolockdep(se << 198 } << 199 << 200 static inline void rwsem_assert_held_write(con << 201 { << 202 if (IS_ENABLED(CONFIG_LOCKDEP)) << 203 lockdep_assert_held_write(sem) << 204 else << 205 rwsem_assert_held_write_nolock << 206 } << 207 << 208 /* 114 /* 209 * lock for reading 115 * lock for reading 210 */ 116 */ 211 extern void down_read(struct rw_semaphore *sem 117 extern void down_read(struct rw_semaphore *sem); 212 extern int __must_check down_read_interruptibl << 213 extern int __must_check down_read_killable(str 118 extern int __must_check down_read_killable(struct rw_semaphore *sem); 214 119 215 /* 120 /* 216 * trylock for reading -- returns 1 if success 121 * trylock for reading -- returns 1 if successful, 0 if contention 217 */ 122 */ 218 extern int down_read_trylock(struct rw_semapho 123 extern int down_read_trylock(struct rw_semaphore *sem); 219 124 220 /* 125 /* 221 * lock for writing 126 * lock for writing 222 */ 127 */ 223 extern void down_write(struct rw_semaphore *se 128 extern void down_write(struct rw_semaphore *sem); 224 extern int __must_check down_write_killable(st 129 extern int __must_check down_write_killable(struct rw_semaphore *sem); 225 130 226 /* 131 /* 227 * trylock for writing -- returns 1 if success 132 * trylock for writing -- returns 1 if successful, 0 if contention 228 */ 133 */ 229 extern int down_write_trylock(struct rw_semaph 134 extern int down_write_trylock(struct rw_semaphore *sem); 230 135 231 /* 136 /* 232 * release a read lock 137 * release a read lock 233 */ 138 */ 234 extern void up_read(struct rw_semaphore *sem); 139 extern void up_read(struct rw_semaphore *sem); 235 140 236 /* 141 /* 237 * release a write lock 142 * release a write lock 238 */ 143 */ 239 extern void up_write(struct rw_semaphore *sem) 144 extern void up_write(struct rw_semaphore *sem); 240 145 241 DEFINE_GUARD(rwsem_read, struct rw_semaphore * << 242 DEFINE_GUARD_COND(rwsem_read, _try, down_read_ << 243 DEFINE_GUARD_COND(rwsem_read, _intr, down_read << 244 << 245 DEFINE_GUARD(rwsem_write, struct rw_semaphore << 246 DEFINE_GUARD_COND(rwsem_write, _try, down_writ << 247 << 248 /* 146 /* 249 * downgrade write lock to read lock 147 * downgrade write lock to read lock 250 */ 148 */ 251 extern void downgrade_write(struct rw_semaphor 149 extern void downgrade_write(struct rw_semaphore *sem); 252 150 253 #ifdef CONFIG_DEBUG_LOCK_ALLOC 151 #ifdef CONFIG_DEBUG_LOCK_ALLOC 254 /* 152 /* 255 * nested locking. NOTE: rwsems are not allowe 153 * nested locking. NOTE: rwsems are not allowed to recurse 256 * (which occurs if the same task tries to acq 154 * (which occurs if the same task tries to acquire the same 257 * lock instance multiple times), but multiple 155 * lock instance multiple times), but multiple locks of the 258 * same lock class might be taken, if the orde 156 * same lock class might be taken, if the order of the locks 259 * is always the same. This ordering rule can 157 * is always the same. This ordering rule can be expressed 260 * to lockdep via the _nested() APIs, but enum 158 * to lockdep via the _nested() APIs, but enumerating the 261 * subclasses that are used. (If the nesting r 159 * subclasses that are used. (If the nesting relationship is 262 * static then another method for expressing n 160 * static then another method for expressing nested locking is 263 * the explicit definition of lock class keys 161 * the explicit definition of lock class keys and the use of 264 * lockdep_set_class() at lock initialization 162 * lockdep_set_class() at lock initialization time. 265 * See Documentation/locking/lockdep-design.rs 163 * See Documentation/locking/lockdep-design.rst for more details.) 266 */ 164 */ 267 extern void down_read_nested(struct rw_semapho 165 extern void down_read_nested(struct rw_semaphore *sem, int subclass); 268 extern int __must_check down_read_killable_nes << 269 extern void down_write_nested(struct rw_semaph 166 extern void down_write_nested(struct rw_semaphore *sem, int subclass); 270 extern int down_write_killable_nested(struct r 167 extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass); 271 extern void _down_write_nest_lock(struct rw_se 168 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock); 272 169 273 # define down_write_nest_lock(sem, nest_lock) 170 # define down_write_nest_lock(sem, nest_lock) \ 274 do { 171 do { \ 275 typecheck(struct lockdep_map *, &(nest 172 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \ 276 _down_write_nest_lock(sem, &(nest_lock 173 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \ 277 } while (0) !! 174 } while (0); 278 175 279 /* 176 /* 280 * Take/release a lock when not the owner will 177 * Take/release a lock when not the owner will release it. 281 * 178 * 282 * [ This API should be avoided as much as pos 179 * [ This API should be avoided as much as possible - the 283 * proper abstraction for this case is compl 180 * proper abstraction for this case is completions. ] 284 */ 181 */ 285 extern void down_read_non_owner(struct rw_sema 182 extern void down_read_non_owner(struct rw_semaphore *sem); 286 extern void up_read_non_owner(struct rw_semaph 183 extern void up_read_non_owner(struct rw_semaphore *sem); 287 #else 184 #else 288 # define down_read_nested(sem, subclass) 185 # define down_read_nested(sem, subclass) down_read(sem) 289 # define down_read_killable_nested(sem, subcla << 290 # define down_write_nest_lock(sem, nest_lock) 186 # define down_write_nest_lock(sem, nest_lock) down_write(sem) 291 # define down_write_nested(sem, subclass) 187 # define down_write_nested(sem, subclass) down_write(sem) 292 # define down_write_killable_nested(sem, subcl 188 # define down_write_killable_nested(sem, subclass) down_write_killable(sem) 293 # define down_read_non_owner(sem) 189 # define down_read_non_owner(sem) down_read(sem) 294 # define up_read_non_owner(sem) 190 # define up_read_non_owner(sem) up_read(sem) 295 #endif 191 #endif 296 192 297 #endif /* _LINUX_RWSEM_H */ 193 #endif /* _LINUX_RWSEM_H */ 298 194
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.