1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* rwsem.h: R/W semaphores, public interface 3 * 4 * Written by David Howells ([email protected]). 5 * Derived from asm-i386/semaphore.h 6 */ 7 8 #ifndef _LINUX_RWSEM_H 9 #define _LINUX_RWSEM_H 10 11 #include <linux/linkage.h> 12 13 #include <linux/types.h> 14 #include <linux/kernel.h> 15 #include <linux/list.h> 16 #include <linux/spinlock.h> 17 #include <linux/atomic.h> 18 #include <linux/err.h> 19 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 20 #include <linux/osq_lock.h> 21 #endif 22 23 /* 24 * For an uncontended rwsem, count and owner are the only fields a task 25 * needs to touch when acquiring the rwsem. So they are put next to each 26 * other to increase the chance that they will share the same cacheline. 27 * 28 * In a contended rwsem, the owner is likely the most frequently accessed 29 * field in the structure as the optimistic waiter that holds the osq lock 30 * will spin on owner. For an embedded rwsem, other hot fields in the 31 * containing structure should be moved further away from the rwsem to 32 * reduce the chance that they will share the same cacheline causing 33 * cacheline bouncing problem. 34 */ 35 struct rw_semaphore { 36 atomic_long_t count; 37 /* 38 * Write owner or one of the read owners. Can be used as a 39 * speculative check to see if the owner is running on the cpu. 40 */ 41 struct task_struct *owner; 42 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 43 struct optimistic_spin_queue osq; /* spinner MCS lock */ 44 #endif 45 raw_spinlock_t wait_lock; 46 struct list_head wait_list; 47 #ifdef CONFIG_DEBUG_LOCK_ALLOC 48 struct lockdep_map dep_map; 49 #endif 50 }; 51 52 /* 53 * Setting bit 1 of the owner field but not bit 0 will indicate 54 * that the rwsem is writer-owned with an unknown owner. 55 */ 56 #define RWSEM_OWNER_UNKNOWN ((struct task_struct *)-2L) 57 58 /* In all implementations count != 0 means locked */ 59 static inline int rwsem_is_locked(struct rw_semaphore *sem) 60 { 61 return atomic_long_read(&sem->count) != 0; 62 } 63 64 #define RWSEM_UNLOCKED_VALUE 0L 65 #define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE) 66 67 /* Common initializer macros and functions */ 68 69 #ifdef CONFIG_DEBUG_LOCK_ALLOC 70 # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname } 71 #else 72 # define __RWSEM_DEP_MAP_INIT(lockname) 73 #endif 74 75 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 76 #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED 77 #else 78 #define __RWSEM_OPT_INIT(lockname) 79 #endif 80 81 #define __RWSEM_INITIALIZER(name) \ 82 { __RWSEM_INIT_COUNT(name), \ 83 .owner = NULL, \ 84 .wait_list = LIST_HEAD_INIT((name).wait_list), \ 85 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \ 86 __RWSEM_OPT_INIT(name) \ 87 __RWSEM_DEP_MAP_INIT(name) } 88 89 #define DECLARE_RWSEM(name) \ 90 struct rw_semaphore name = __RWSEM_INITIALIZER(name) 91 92 extern void __init_rwsem(struct rw_semaphore *sem, const char *name, 93 struct lock_class_key *key); 94 95 #define init_rwsem(sem) \ 96 do { \ 97 static struct lock_class_key __key; \ 98 \ 99 __init_rwsem((sem), #sem, &__key); \ 100 } while (0) 101 102 /* 103 * This is the same regardless of which rwsem implementation that is being used. 104 * It is just a heuristic meant to be called by somebody alreadying holding the 105 * rwsem to see if somebody from an incompatible type is wanting access to the 106 * lock. 107 */ 108 static inline int rwsem_is_contended(struct rw_semaphore *sem) 109 { 110 return !list_empty(&sem->wait_list); 111 } 112 113 /* 114 * lock for reading 115 */ 116 extern void down_read(struct rw_semaphore *sem); 117 extern int __must_check down_read_killable(struct rw_semaphore *sem); 118 119 /* 120 * trylock for reading -- returns 1 if successful, 0 if contention 121 */ 122 extern int down_read_trylock(struct rw_semaphore *sem); 123 124 /* 125 * lock for writing 126 */ 127 extern void down_write(struct rw_semaphore *sem); 128 extern int __must_check down_write_killable(struct rw_semaphore *sem); 129 130 /* 131 * trylock for writing -- returns 1 if successful, 0 if contention 132 */ 133 extern int down_write_trylock(struct rw_semaphore *sem); 134 135 /* 136 * release a read lock 137 */ 138 extern void up_read(struct rw_semaphore *sem); 139 140 /* 141 * release a write lock 142 */ 143 extern void up_write(struct rw_semaphore *sem); 144 145 /* 146 * downgrade write lock to read lock 147 */ 148 extern void downgrade_write(struct rw_semaphore *sem); 149 150 #ifdef CONFIG_DEBUG_LOCK_ALLOC 151 /* 152 * nested locking. NOTE: rwsems are not allowed to recurse 153 * (which occurs if the same task tries to acquire the same 154 * lock instance multiple times), but multiple locks of the 155 * same lock class might be taken, if the order of the locks 156 * is always the same. This ordering rule can be expressed 157 * to lockdep via the _nested() APIs, but enumerating the 158 * subclasses that are used. (If the nesting relationship is 159 * static then another method for expressing nested locking is 160 * the explicit definition of lock class keys and the use of 161 * lockdep_set_class() at lock initialization time. 162 * See Documentation/locking/lockdep-design.txt for more details.) 163 */ 164 extern void down_read_nested(struct rw_semaphore *sem, int subclass); 165 extern void down_write_nested(struct rw_semaphore *sem, int subclass); 166 extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass); 167 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock); 168 169 # define down_write_nest_lock(sem, nest_lock) \ 170 do { \ 171 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \ 172 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \ 173 } while (0); 174 175 /* 176 * Take/release a lock when not the owner will release it. 177 * 178 * [ This API should be avoided as much as possible - the 179 * proper abstraction for this case is completions. ] 180 */ 181 extern void down_read_non_owner(struct rw_semaphore *sem); 182 extern void up_read_non_owner(struct rw_semaphore *sem); 183 #else 184 # define down_read_nested(sem, subclass) down_read(sem) 185 # define down_write_nest_lock(sem, nest_lock) down_write(sem) 186 # define down_write_nested(sem, subclass) down_write(sem) 187 # define down_write_killable_nested(sem, subclass) down_write_killable(sem) 188 # define down_read_non_owner(sem) down_read(sem) 189 # define up_read_non_owner(sem) up_read(sem) 190 #endif 191 192 #endif /* _LINUX_RWSEM_H */ 193