• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
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 
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 #include <linux/android_vendor.h>
23 
24 /*
25  * For an uncontended rwsem, count and owner are the only fields a task
26  * needs to touch when acquiring the rwsem. So they are put next to each
27  * other to increase the chance that they will share the same cacheline.
28  *
29  * In a contended rwsem, the owner is likely the most frequently accessed
30  * field in the structure as the optimistic waiter that holds the osq lock
31  * will spin on owner. For an embedded rwsem, other hot fields in the
32  * containing structure should be moved further away from the rwsem to
33  * reduce the chance that they will share the same cacheline causing
34  * cacheline bouncing problem.
35  */
36 struct rw_semaphore {
37 	atomic_long_t count;
38 	/*
39 	 * Write owner or one of the read owners as well flags regarding
40 	 * the current state of the rwsem. Can be used as a speculative
41 	 * check to see if the write owner is running on the cpu.
42 	 */
43 	atomic_long_t owner;
44 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
45 	struct optimistic_spin_queue osq; /* spinner MCS lock */
46 #endif
47 	raw_spinlock_t wait_lock;
48 	struct list_head wait_list;
49 #ifdef CONFIG_DEBUG_RWSEMS
50 	void *magic;
51 #endif
52 #ifdef CONFIG_DEBUG_LOCK_ALLOC
53 	struct lockdep_map	dep_map;
54 #endif
55 	ANDROID_VENDOR_DATA(1);
56 	ANDROID_OEM_DATA_ARRAY(1, 2);
57 };
58 
59 enum rwsem_waiter_type {
60 	RWSEM_WAITING_FOR_WRITE,
61 	RWSEM_WAITING_FOR_READ
62 };
63 
64 struct rwsem_waiter {
65 	struct list_head list;
66 	struct task_struct *task;
67 	enum rwsem_waiter_type type;
68 	unsigned long timeout;
69 	unsigned long last_rowner;
70 };
71 
72 /* In all implementations count != 0 means locked */
rwsem_is_locked(struct rw_semaphore * sem)73 static inline int rwsem_is_locked(struct rw_semaphore *sem)
74 {
75 	return atomic_long_read(&sem->count) != 0;
76 }
77 
78 #define RWSEM_UNLOCKED_VALUE		0L
79 #define __RWSEM_COUNT_INIT(name)	.count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
80 
81 /* Common initializer macros and functions */
82 
83 #ifdef CONFIG_DEBUG_LOCK_ALLOC
84 # define __RWSEM_DEP_MAP_INIT(lockname)			\
85 	.dep_map = {					\
86 		.name = #lockname,			\
87 		.wait_type_inner = LD_WAIT_SLEEP,	\
88 	},
89 #else
90 # define __RWSEM_DEP_MAP_INIT(lockname)
91 #endif
92 
93 #ifdef CONFIG_DEBUG_RWSEMS
94 # define __RWSEM_DEBUG_INIT(lockname) .magic = &lockname,
95 #else
96 # define __RWSEM_DEBUG_INIT(lockname)
97 #endif
98 
99 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
100 #define __RWSEM_OPT_INIT(lockname) .osq = OSQ_LOCK_UNLOCKED,
101 #else
102 #define __RWSEM_OPT_INIT(lockname)
103 #endif
104 
105 #define __RWSEM_INITIALIZER(name)				\
106 	{ __RWSEM_COUNT_INIT(name),				\
107 	  .owner = ATOMIC_LONG_INIT(0),				\
108 	  __RWSEM_OPT_INIT(name)				\
109 	  .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock),\
110 	  .wait_list = LIST_HEAD_INIT((name).wait_list),	\
111 	  __RWSEM_DEBUG_INIT(name)				\
112 	  __RWSEM_DEP_MAP_INIT(name) }
113 
114 #define DECLARE_RWSEM(name) \
115 	struct rw_semaphore name = __RWSEM_INITIALIZER(name)
116 
117 extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
118 			 struct lock_class_key *key);
119 
120 #define init_rwsem(sem)						\
121 do {								\
122 	static struct lock_class_key __key;			\
123 								\
124 	__init_rwsem((sem), #sem, &__key);			\
125 } while (0)
126 
127 /*
128  * This is the same regardless of which rwsem implementation that is being used.
129  * It is just a heuristic meant to be called by somebody alreadying holding the
130  * rwsem to see if somebody from an incompatible type is wanting access to the
131  * lock.
132  */
rwsem_is_contended(struct rw_semaphore * sem)133 static inline int rwsem_is_contended(struct rw_semaphore *sem)
134 {
135 	return !list_empty(&sem->wait_list);
136 }
137 
138 /*
139  * lock for reading
140  */
141 extern void down_read(struct rw_semaphore *sem);
142 extern int __must_check down_read_interruptible(struct rw_semaphore *sem);
143 extern int __must_check down_read_killable(struct rw_semaphore *sem);
144 
145 /*
146  * trylock for reading -- returns 1 if successful, 0 if contention
147  */
148 extern int down_read_trylock(struct rw_semaphore *sem);
149 
150 /*
151  * lock for writing
152  */
153 extern void down_write(struct rw_semaphore *sem);
154 extern int __must_check down_write_killable(struct rw_semaphore *sem);
155 
156 /*
157  * trylock for writing -- returns 1 if successful, 0 if contention
158  */
159 extern int down_write_trylock(struct rw_semaphore *sem);
160 
161 /*
162  * release a read lock
163  */
164 extern void up_read(struct rw_semaphore *sem);
165 
166 /*
167  * release a write lock
168  */
169 extern void up_write(struct rw_semaphore *sem);
170 
171 /*
172  * downgrade write lock to read lock
173  */
174 extern void downgrade_write(struct rw_semaphore *sem);
175 
176 #ifdef CONFIG_DEBUG_LOCK_ALLOC
177 /*
178  * nested locking. NOTE: rwsems are not allowed to recurse
179  * (which occurs if the same task tries to acquire the same
180  * lock instance multiple times), but multiple locks of the
181  * same lock class might be taken, if the order of the locks
182  * is always the same. This ordering rule can be expressed
183  * to lockdep via the _nested() APIs, but enumerating the
184  * subclasses that are used. (If the nesting relationship is
185  * static then another method for expressing nested locking is
186  * the explicit definition of lock class keys and the use of
187  * lockdep_set_class() at lock initialization time.
188  * See Documentation/locking/lockdep-design.rst for more details.)
189  */
190 extern void down_read_nested(struct rw_semaphore *sem, int subclass);
191 extern int __must_check down_read_killable_nested(struct rw_semaphore *sem, int subclass);
192 extern void down_write_nested(struct rw_semaphore *sem, int subclass);
193 extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
194 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
195 
196 # define down_write_nest_lock(sem, nest_lock)			\
197 do {								\
198 	typecheck(struct lockdep_map *, &(nest_lock)->dep_map);	\
199 	_down_write_nest_lock(sem, &(nest_lock)->dep_map);	\
200 } while (0);
201 
202 /*
203  * Take/release a lock when not the owner will release it.
204  *
205  * [ This API should be avoided as much as possible - the
206  *   proper abstraction for this case is completions. ]
207  */
208 extern void down_read_non_owner(struct rw_semaphore *sem);
209 extern void up_read_non_owner(struct rw_semaphore *sem);
210 #else
211 # define down_read_nested(sem, subclass)		down_read(sem)
212 # define down_read_killable_nested(sem, subclass)	down_read_killable(sem)
213 # define down_write_nest_lock(sem, nest_lock)	down_write(sem)
214 # define down_write_nested(sem, subclass)	down_write(sem)
215 # define down_write_killable_nested(sem, subclass)	down_write_killable(sem)
216 # define down_read_non_owner(sem)		down_read(sem)
217 # define up_read_non_owner(sem)			up_read(sem)
218 #endif
219 
220 #endif /* _LINUX_RWSEM_H */
221