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 struct rw_semaphore; 37 * For an uncontended rwsem, count and owner a !! 24 38 * needs to touch when acquiring the rwsem. So !! 25 #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK 39 * other to increase the chance that they will !! 26 #include <linux/rwsem-spinlock.h> /* use a generic implementation */ 40 * !! 27 #define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE 41 * In a contended rwsem, the owner is likely t !! 28 #else 42 * field in the structure as the optimistic wa !! 29 /* All arch specific implementations share the same struct */ 43 * will spin on owner. For an embedded rwsem, << 44 * containing structure should be moved furthe << 45 * reduce the chance that they will share the << 46 * cacheline bouncing problem. << 47 */ << 48 struct rw_semaphore { 30 struct rw_semaphore { 49 atomic_long_t count; 31 atomic_long_t count; 50 /* !! 32 struct list_head wait_list; 51 * Write owner or one of the read owne !! 33 raw_spinlock_t wait_lock; 52 * the current state of the rwsem. Can << 53 * check to see if the write owner is << 54 */ << 55 atomic_long_t owner; << 56 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 34 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 57 struct optimistic_spin_queue osq; /* s 35 struct optimistic_spin_queue osq; /* spinner MCS lock */ 58 #endif !! 36 /* 59 raw_spinlock_t wait_lock; !! 37 * Write owner. Used as a speculative check to see 60 struct list_head wait_list; !! 38 * if the owner is running on the cpu. 61 #ifdef CONFIG_DEBUG_RWSEMS !! 39 */ 62 void *magic; !! 40 struct task_struct *owner; 63 #endif 41 #endif 64 #ifdef CONFIG_DEBUG_LOCK_ALLOC 42 #ifdef CONFIG_DEBUG_LOCK_ALLOC 65 struct lockdep_map dep_map; 43 struct lockdep_map dep_map; 66 #endif 44 #endif 67 }; 45 }; 68 46 69 #define RWSEM_UNLOCKED_VALUE 0UL !! 47 /* 70 #define RWSEM_WRITER_LOCKED (1UL < !! 48 * Setting bit 1 of the owner field but not bit 0 will indicate 71 #define __RWSEM_COUNT_INIT(name) .count !! 49 * that the rwsem is writer-owned with an unknown owner. >> 50 */ >> 51 #define RWSEM_OWNER_UNKNOWN ((struct task_struct *)-2L) 72 52 73 static inline int rwsem_is_locked(struct rw_se !! 53 extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem); 74 { !! 54 extern struct rw_semaphore *rwsem_down_read_failed_killable(struct rw_semaphore *sem); 75 return atomic_long_read(&sem->count) ! !! 55 extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem); 76 } !! 56 extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem); >> 57 extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *); >> 58 extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem); 77 59 78 static inline void rwsem_assert_held_nolockdep !! 60 /* Include the arch specific part */ 79 { !! 61 #include <asm/rwsem.h> 80 WARN_ON(atomic_long_read(&sem->count) << 81 } << 82 62 83 static inline void rwsem_assert_held_write_nol !! 63 /* In all implementations count != 0 means locked */ >> 64 static inline int rwsem_is_locked(struct rw_semaphore *sem) 84 { 65 { 85 WARN_ON(!(atomic_long_read(&sem->count !! 66 return atomic_long_read(&sem->count) != 0; 86 } 67 } 87 68 >> 69 #define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE) >> 70 #endif >> 71 88 /* Common initializer macros and functions */ 72 /* Common initializer macros and functions */ 89 73 90 #ifdef CONFIG_DEBUG_RWSEMS !! 74 #ifdef CONFIG_DEBUG_LOCK_ALLOC 91 # define __RWSEM_DEBUG_INIT(lockname) .magic = !! 75 # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname } 92 #else 76 #else 93 # define __RWSEM_DEBUG_INIT(lockname) !! 77 # define __RWSEM_DEP_MAP_INIT(lockname) 94 #endif 78 #endif 95 79 96 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 80 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 97 #define __RWSEM_OPT_INIT(lockname) .osq = OSQ_ !! 81 #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL 98 #else 82 #else 99 #define __RWSEM_OPT_INIT(lockname) 83 #define __RWSEM_OPT_INIT(lockname) 100 #endif 84 #endif 101 85 102 #define __RWSEM_INITIALIZER(name) 86 #define __RWSEM_INITIALIZER(name) \ 103 { __RWSEM_COUNT_INIT(name), !! 87 { __RWSEM_INIT_COUNT(name), \ 104 .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 88 .wait_list = LIST_HEAD_INIT((name).wait_list), \ 108 __RWSEM_DEBUG_INIT(name) !! 89 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \ >> 90 __RWSEM_OPT_INIT(name) \ 109 __RWSEM_DEP_MAP_INIT(name) } 91 __RWSEM_DEP_MAP_INIT(name) } 110 92 111 #define DECLARE_RWSEM(name) \ 93 #define DECLARE_RWSEM(name) \ 112 struct rw_semaphore name = __RWSEM_INI 94 struct rw_semaphore name = __RWSEM_INITIALIZER(name) 113 95 114 extern void __init_rwsem(struct rw_semaphore * 96 extern void __init_rwsem(struct rw_semaphore *sem, const char *name, 115 struct lock_class_key 97 struct lock_class_key *key); 116 98 117 #define init_rwsem(sem) 99 #define init_rwsem(sem) \ 118 do { 100 do { \ 119 static struct lock_class_key __key; 101 static struct lock_class_key __key; \ 120 102 \ 121 __init_rwsem((sem), #sem, &__key); 103 __init_rwsem((sem), #sem, &__key); \ 122 } while (0) 104 } while (0) 123 105 124 /* 106 /* 125 * This is the same regardless of which rwsem 107 * This is the same regardless of which rwsem implementation that is being used. 126 * It is just a heuristic meant to be called b !! 108 * It is just a heuristic meant to be called by somebody alreadying holding the 127 * rwsem to see if somebody from an incompatib 109 * rwsem to see if somebody from an incompatible type is wanting access to the 128 * lock. 110 * lock. 129 */ 111 */ 130 static inline int rwsem_is_contended(struct rw 112 static inline int rwsem_is_contended(struct rw_semaphore *sem) 131 { 113 { 132 return !list_empty(&sem->wait_list); 114 return !list_empty(&sem->wait_list); 133 } 115 } 134 116 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 /* 117 /* 209 * lock for reading 118 * lock for reading 210 */ 119 */ 211 extern void down_read(struct rw_semaphore *sem 120 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 121 extern int __must_check down_read_killable(struct rw_semaphore *sem); 214 122 215 /* 123 /* 216 * trylock for reading -- returns 1 if success 124 * trylock for reading -- returns 1 if successful, 0 if contention 217 */ 125 */ 218 extern int down_read_trylock(struct rw_semapho 126 extern int down_read_trylock(struct rw_semaphore *sem); 219 127 220 /* 128 /* 221 * lock for writing 129 * lock for writing 222 */ 130 */ 223 extern void down_write(struct rw_semaphore *se 131 extern void down_write(struct rw_semaphore *sem); 224 extern int __must_check down_write_killable(st 132 extern int __must_check down_write_killable(struct rw_semaphore *sem); 225 133 226 /* 134 /* 227 * trylock for writing -- returns 1 if success 135 * trylock for writing -- returns 1 if successful, 0 if contention 228 */ 136 */ 229 extern int down_write_trylock(struct rw_semaph 137 extern int down_write_trylock(struct rw_semaphore *sem); 230 138 231 /* 139 /* 232 * release a read lock 140 * release a read lock 233 */ 141 */ 234 extern void up_read(struct rw_semaphore *sem); 142 extern void up_read(struct rw_semaphore *sem); 235 143 236 /* 144 /* 237 * release a write lock 145 * release a write lock 238 */ 146 */ 239 extern void up_write(struct rw_semaphore *sem) 147 extern void up_write(struct rw_semaphore *sem); 240 148 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 /* 149 /* 249 * downgrade write lock to read lock 150 * downgrade write lock to read lock 250 */ 151 */ 251 extern void downgrade_write(struct rw_semaphor 152 extern void downgrade_write(struct rw_semaphore *sem); 252 153 253 #ifdef CONFIG_DEBUG_LOCK_ALLOC 154 #ifdef CONFIG_DEBUG_LOCK_ALLOC 254 /* 155 /* 255 * nested locking. NOTE: rwsems are not allowe 156 * nested locking. NOTE: rwsems are not allowed to recurse 256 * (which occurs if the same task tries to acq 157 * (which occurs if the same task tries to acquire the same 257 * lock instance multiple times), but multiple 158 * lock instance multiple times), but multiple locks of the 258 * same lock class might be taken, if the orde 159 * same lock class might be taken, if the order of the locks 259 * is always the same. This ordering rule can 160 * is always the same. This ordering rule can be expressed 260 * to lockdep via the _nested() APIs, but enum 161 * to lockdep via the _nested() APIs, but enumerating the 261 * subclasses that are used. (If the nesting r 162 * subclasses that are used. (If the nesting relationship is 262 * static then another method for expressing n 163 * static then another method for expressing nested locking is 263 * the explicit definition of lock class keys 164 * the explicit definition of lock class keys and the use of 264 * lockdep_set_class() at lock initialization 165 * lockdep_set_class() at lock initialization time. 265 * See Documentation/locking/lockdep-design.rs !! 166 * See Documentation/locking/lockdep-design.txt for more details.) 266 */ 167 */ 267 extern void down_read_nested(struct rw_semapho 168 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 169 extern void down_write_nested(struct rw_semaphore *sem, int subclass); 270 extern int down_write_killable_nested(struct r 170 extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass); 271 extern void _down_write_nest_lock(struct rw_se 171 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock); 272 172 273 # define down_write_nest_lock(sem, nest_lock) 173 # define down_write_nest_lock(sem, nest_lock) \ 274 do { 174 do { \ 275 typecheck(struct lockdep_map *, &(nest 175 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \ 276 _down_write_nest_lock(sem, &(nest_lock 176 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \ 277 } while (0) !! 177 } while (0); 278 178 279 /* 179 /* 280 * Take/release a lock when not the owner will 180 * Take/release a lock when not the owner will release it. 281 * 181 * 282 * [ This API should be avoided as much as pos 182 * [ This API should be avoided as much as possible - the 283 * proper abstraction for this case is compl 183 * proper abstraction for this case is completions. ] 284 */ 184 */ 285 extern void down_read_non_owner(struct rw_sema 185 extern void down_read_non_owner(struct rw_semaphore *sem); 286 extern void up_read_non_owner(struct rw_semaph 186 extern void up_read_non_owner(struct rw_semaphore *sem); 287 #else 187 #else 288 # define down_read_nested(sem, subclass) 188 # 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) 189 # define down_write_nest_lock(sem, nest_lock) down_write(sem) 291 # define down_write_nested(sem, subclass) 190 # define down_write_nested(sem, subclass) down_write(sem) 292 # define down_write_killable_nested(sem, subcl 191 # define down_write_killable_nested(sem, subclass) down_write_killable(sem) 293 # define down_read_non_owner(sem) 192 # define down_read_non_owner(sem) down_read(sem) 294 # define up_read_non_owner(sem) 193 # define up_read_non_owner(sem) up_read(sem) 295 #endif 194 #endif 296 195 297 #endif /* _LINUX_RWSEM_H */ 196 #endif /* _LINUX_RWSEM_H */ 298 197
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.