Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | /* rwsem.h: R/W semaphores, public interface |
3 | * | |
4 | * Written by David Howells (dhowells@redhat.com). | |
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 | ||
1da177e4 LT |
13 | #include <linux/types.h> |
14 | #include <linux/kernel.h> | |
c16a87ce TG |
15 | #include <linux/list.h> |
16 | #include <linux/spinlock.h> | |
60063497 | 17 | #include <linux/atomic.h> |
d4799608 | 18 | #include <linux/err.h> |
5db6c6fe | 19 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
90631822 | 20 | #include <linux/osq_lock.h> |
5db6c6fe | 21 | #endif |
1da177e4 LT |
22 | |
23 | struct rw_semaphore; | |
24 | ||
1c8ed640 TG |
25 | /* All arch specific implementations share the same struct */ |
26 | struct rw_semaphore { | |
8ee62b18 | 27 | atomic_long_t count; |
4fc828e2 | 28 | struct list_head wait_list; |
ce069fc9 | 29 | raw_spinlock_t wait_lock; |
5db6c6fe | 30 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
ce069fc9 | 31 | struct optimistic_spin_queue osq; /* spinner MCS lock */ |
4fc828e2 DB |
32 | /* |
33 | * Write owner. Used as a speculative check to see | |
34 | * if the owner is running on the cpu. | |
35 | */ | |
36 | struct task_struct *owner; | |
4fc828e2 | 37 | #endif |
1c8ed640 TG |
38 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
39 | struct lockdep_map dep_map; | |
40 | #endif | |
41 | }; | |
42 | ||
5a817641 | 43 | /* |
925b9cd1 | 44 | * Setting bit 1 of the owner field but not bit 0 will indicate |
5a817641 WL |
45 | * that the rwsem is writer-owned with an unknown owner. |
46 | */ | |
925b9cd1 | 47 | #define RWSEM_OWNER_UNKNOWN ((struct task_struct *)-2L) |
5a817641 | 48 | |
41e5887f TG |
49 | /* In all implementations count != 0 means locked */ |
50 | static inline int rwsem_is_locked(struct rw_semaphore *sem) | |
51 | { | |
8ee62b18 | 52 | return atomic_long_read(&sem->count) != 0; |
41e5887f TG |
53 | } |
54 | ||
46ad0840 | 55 | #define RWSEM_UNLOCKED_VALUE 0L |
8ee62b18 | 56 | #define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE) |
1da177e4 | 57 | |
12249b34 TG |
58 | /* Common initializer macros and functions */ |
59 | ||
60 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
61 | # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname } | |
62 | #else | |
63 | # define __RWSEM_DEP_MAP_INIT(lockname) | |
64 | #endif | |
65 | ||
5db6c6fe | 66 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
ce069fc9 | 67 | #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL |
4fc828e2 | 68 | #else |
ce069fc9 | 69 | #define __RWSEM_OPT_INIT(lockname) |
4fc828e2 | 70 | #endif |
12249b34 | 71 | |
ce069fc9 | 72 | #define __RWSEM_INITIALIZER(name) \ |
8ee62b18 | 73 | { __RWSEM_INIT_COUNT(name), \ |
ce069fc9 JL |
74 | .wait_list = LIST_HEAD_INIT((name).wait_list), \ |
75 | .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \ | |
76 | __RWSEM_OPT_INIT(name) \ | |
77 | __RWSEM_DEP_MAP_INIT(name) } | |
78 | ||
12249b34 TG |
79 | #define DECLARE_RWSEM(name) \ |
80 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) | |
81 | ||
82 | extern void __init_rwsem(struct rw_semaphore *sem, const char *name, | |
83 | struct lock_class_key *key); | |
84 | ||
85 | #define init_rwsem(sem) \ | |
86 | do { \ | |
87 | static struct lock_class_key __key; \ | |
88 | \ | |
89 | __init_rwsem((sem), #sem, &__key); \ | |
90 | } while (0) | |
91 | ||
4a444b1f JB |
92 | /* |
93 | * This is the same regardless of which rwsem implementation that is being used. | |
94 | * It is just a heuristic meant to be called by somebody alreadying holding the | |
95 | * rwsem to see if somebody from an incompatible type is wanting access to the | |
96 | * lock. | |
97 | */ | |
98 | static inline int rwsem_is_contended(struct rw_semaphore *sem) | |
99 | { | |
100 | return !list_empty(&sem->wait_list); | |
101 | } | |
102 | ||
1da177e4 LT |
103 | /* |
104 | * lock for reading | |
105 | */ | |
4ea2176d | 106 | extern void down_read(struct rw_semaphore *sem); |
76f8507f | 107 | extern int __must_check down_read_killable(struct rw_semaphore *sem); |
1da177e4 LT |
108 | |
109 | /* | |
110 | * trylock for reading -- returns 1 if successful, 0 if contention | |
111 | */ | |
4ea2176d | 112 | extern int down_read_trylock(struct rw_semaphore *sem); |
1da177e4 LT |
113 | |
114 | /* | |
115 | * lock for writing | |
116 | */ | |
4ea2176d | 117 | extern void down_write(struct rw_semaphore *sem); |
916633a4 | 118 | extern int __must_check down_write_killable(struct rw_semaphore *sem); |
1da177e4 LT |
119 | |
120 | /* | |
121 | * trylock for writing -- returns 1 if successful, 0 if contention | |
122 | */ | |
4ea2176d | 123 | extern int down_write_trylock(struct rw_semaphore *sem); |
1da177e4 LT |
124 | |
125 | /* | |
126 | * release a read lock | |
127 | */ | |
4ea2176d | 128 | extern void up_read(struct rw_semaphore *sem); |
1da177e4 LT |
129 | |
130 | /* | |
131 | * release a write lock | |
132 | */ | |
4ea2176d | 133 | extern void up_write(struct rw_semaphore *sem); |
1da177e4 LT |
134 | |
135 | /* | |
136 | * downgrade write lock to read lock | |
137 | */ | |
4ea2176d IM |
138 | extern void downgrade_write(struct rw_semaphore *sem); |
139 | ||
140 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
141 | /* | |
5fca80e8 IM |
142 | * nested locking. NOTE: rwsems are not allowed to recurse |
143 | * (which occurs if the same task tries to acquire the same | |
144 | * lock instance multiple times), but multiple locks of the | |
145 | * same lock class might be taken, if the order of the locks | |
146 | * is always the same. This ordering rule can be expressed | |
147 | * to lockdep via the _nested() APIs, but enumerating the | |
148 | * subclasses that are used. (If the nesting relationship is | |
149 | * static then another method for expressing nested locking is | |
150 | * the explicit definition of lock class keys and the use of | |
151 | * lockdep_set_class() at lock initialization time. | |
214e0aed | 152 | * See Documentation/locking/lockdep-design.txt for more details.) |
4ea2176d IM |
153 | */ |
154 | extern void down_read_nested(struct rw_semaphore *sem, int subclass); | |
155 | extern void down_write_nested(struct rw_semaphore *sem, int subclass); | |
887bddfa | 156 | extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass); |
1b963c81 JK |
157 | extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock); |
158 | ||
159 | # define down_write_nest_lock(sem, nest_lock) \ | |
160 | do { \ | |
161 | typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \ | |
162 | _down_write_nest_lock(sem, &(nest_lock)->dep_map); \ | |
163 | } while (0); | |
164 | ||
84759c6d KO |
165 | /* |
166 | * Take/release a lock when not the owner will release it. | |
167 | * | |
168 | * [ This API should be avoided as much as possible - the | |
169 | * proper abstraction for this case is completions. ] | |
170 | */ | |
171 | extern void down_read_non_owner(struct rw_semaphore *sem); | |
172 | extern void up_read_non_owner(struct rw_semaphore *sem); | |
4ea2176d IM |
173 | #else |
174 | # define down_read_nested(sem, subclass) down_read(sem) | |
e65b9ad2 | 175 | # define down_write_nest_lock(sem, nest_lock) down_write(sem) |
4ea2176d | 176 | # define down_write_nested(sem, subclass) down_write(sem) |
887bddfa | 177 | # define down_write_killable_nested(sem, subclass) down_write_killable(sem) |
84759c6d KO |
178 | # define down_read_non_owner(sem) down_read(sem) |
179 | # define up_read_non_owner(sem) up_read(sem) | |
4ea2176d | 180 | #endif |
1da177e4 | 181 | |
1da177e4 | 182 | #endif /* _LINUX_RWSEM_H */ |