1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/atomic.h>
3 #include <linux/percpu.h>
4 #include <linux/wait.h>
5 #include <linux/lockdep.h>
6 #include <linux/percpu-rwsem.h>
7 #include <linux/rcupdate.h>
8 #include <linux/sched.h>
9 #include <linux/sched/task.h>
10 #include <linux/sched/debug.h>
11 #include <linux/errno.h>
12 #include <trace/events/lock.h>
13
14 #include <trace/hooks/dtask.h>
15
16 /*
17 * trace_android_vh_record_pcpu_rwsem_starttime is called in
18 * include/linux/percpu-rwsem.h by including include/hooks/dtask.h, which
19 * will result to build-err. So we create
20 * func:_trace_android_vh_record_pcpu_rwsem_starttime for percpu-rwsem.h to call.
21 */
_trace_android_vh_record_pcpu_rwsem_starttime(struct percpu_rw_semaphore * sem,unsigned long settime)22 void _trace_android_vh_record_pcpu_rwsem_starttime(struct percpu_rw_semaphore *sem,
23 unsigned long settime)
24 {
25 trace_android_vh_record_pcpu_rwsem_starttime(sem, settime);
26 }
27 EXPORT_SYMBOL_GPL(_trace_android_vh_record_pcpu_rwsem_starttime);
28
__percpu_init_rwsem(struct percpu_rw_semaphore * sem,const char * name,struct lock_class_key * key)29 int __percpu_init_rwsem(struct percpu_rw_semaphore *sem,
30 const char *name, struct lock_class_key *key)
31 {
32 sem->read_count = alloc_percpu(int);
33 if (unlikely(!sem->read_count))
34 return -ENOMEM;
35
36 rcu_sync_init(&sem->rss);
37 rcuwait_init(&sem->writer);
38 init_waitqueue_head(&sem->waiters);
39 atomic_set(&sem->block, 0);
40 #ifdef CONFIG_DEBUG_LOCK_ALLOC
41 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
42 lockdep_init_map(&sem->dep_map, name, key, 0);
43 #endif
44 return 0;
45 }
46 EXPORT_SYMBOL_GPL(__percpu_init_rwsem);
47
percpu_free_rwsem(struct percpu_rw_semaphore * sem)48 void percpu_free_rwsem(struct percpu_rw_semaphore *sem)
49 {
50 /*
51 * XXX: temporary kludge. The error path in alloc_super()
52 * assumes that percpu_free_rwsem() is safe after kzalloc().
53 */
54 if (!sem->read_count)
55 return;
56
57 rcu_sync_dtor(&sem->rss);
58 free_percpu(sem->read_count);
59 sem->read_count = NULL; /* catch use after free bugs */
60 }
61 EXPORT_SYMBOL_GPL(percpu_free_rwsem);
62
__percpu_down_read_trylock(struct percpu_rw_semaphore * sem)63 static bool __percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
64 {
65 this_cpu_inc(*sem->read_count);
66
67 /*
68 * Due to having preemption disabled the decrement happens on
69 * the same CPU as the increment, avoiding the
70 * increment-on-one-CPU-and-decrement-on-another problem.
71 *
72 * If the reader misses the writer's assignment of sem->block, then the
73 * writer is guaranteed to see the reader's increment.
74 *
75 * Conversely, any readers that increment their sem->read_count after
76 * the writer looks are guaranteed to see the sem->block value, which
77 * in turn means that they are guaranteed to immediately decrement
78 * their sem->read_count, so that it doesn't matter that the writer
79 * missed them.
80 */
81
82 smp_mb(); /* A matches D */
83
84 /*
85 * If !sem->block the critical section starts here, matched by the
86 * release in percpu_up_write().
87 */
88 if (likely(!atomic_read_acquire(&sem->block)))
89 return true;
90
91 this_cpu_dec(*sem->read_count);
92
93 /* Prod writer to re-evaluate readers_active_check() */
94 rcuwait_wake_up(&sem->writer);
95
96 return false;
97 }
98
__percpu_down_write_trylock(struct percpu_rw_semaphore * sem)99 static inline bool __percpu_down_write_trylock(struct percpu_rw_semaphore *sem)
100 {
101 if (atomic_read(&sem->block))
102 return false;
103
104 return atomic_xchg(&sem->block, 1) == 0;
105 }
106
__percpu_rwsem_trylock(struct percpu_rw_semaphore * sem,bool reader)107 static bool __percpu_rwsem_trylock(struct percpu_rw_semaphore *sem, bool reader)
108 {
109 if (reader) {
110 bool ret;
111
112 preempt_disable();
113 ret = __percpu_down_read_trylock(sem);
114 preempt_enable();
115
116 return ret;
117 }
118 return __percpu_down_write_trylock(sem);
119 }
120
121 /*
122 * The return value of wait_queue_entry::func means:
123 *
124 * <0 - error, wakeup is terminated and the error is returned
125 * 0 - no wakeup, a next waiter is tried
126 * >0 - woken, if EXCLUSIVE, counted towards @nr_exclusive.
127 *
128 * We use EXCLUSIVE for both readers and writers to preserve FIFO order,
129 * and play games with the return value to allow waking multiple readers.
130 *
131 * Specifically, we wake readers until we've woken a single writer, or until a
132 * trylock fails.
133 */
percpu_rwsem_wake_function(struct wait_queue_entry * wq_entry,unsigned int mode,int wake_flags,void * key)134 static int percpu_rwsem_wake_function(struct wait_queue_entry *wq_entry,
135 unsigned int mode, int wake_flags,
136 void *key)
137 {
138 bool reader = wq_entry->flags & WQ_FLAG_CUSTOM;
139 struct percpu_rw_semaphore *sem = key;
140 struct task_struct *p;
141
142 /* concurrent against percpu_down_write(), can get stolen */
143 if (!__percpu_rwsem_trylock(sem, reader))
144 return 1;
145
146 p = get_task_struct(wq_entry->private);
147 list_del_init(&wq_entry->entry);
148 smp_store_release(&wq_entry->private, NULL);
149
150 wake_up_process(p);
151 put_task_struct(p);
152
153 return !reader; /* wake (readers until) 1 writer */
154 }
155
percpu_rwsem_wait(struct percpu_rw_semaphore * sem,bool reader)156 static void percpu_rwsem_wait(struct percpu_rw_semaphore *sem, bool reader)
157 {
158 DEFINE_WAIT_FUNC(wq_entry, percpu_rwsem_wake_function);
159 bool wait;
160
161 spin_lock_irq(&sem->waiters.lock);
162 /*
163 * Serialize against the wakeup in percpu_up_write(), if we fail
164 * the trylock, the wakeup must see us on the list.
165 */
166 wait = !__percpu_rwsem_trylock(sem, reader);
167 if (wait) {
168 wq_entry.flags |= WQ_FLAG_EXCLUSIVE | reader * WQ_FLAG_CUSTOM;
169 __add_wait_queue_entry_tail(&sem->waiters, &wq_entry);
170 trace_android_vh_percpu_rwsem_wq_add(sem, reader);
171 }
172 spin_unlock_irq(&sem->waiters.lock);
173
174 while (wait) {
175 set_current_state(TASK_UNINTERRUPTIBLE);
176 if (!smp_load_acquire(&wq_entry.private))
177 break;
178 schedule();
179 }
180 __set_current_state(TASK_RUNNING);
181 }
182
__percpu_down_read(struct percpu_rw_semaphore * sem,bool try)183 bool __sched __percpu_down_read(struct percpu_rw_semaphore *sem, bool try)
184 {
185 bool ret = false;
186
187 if (__percpu_down_read_trylock(sem))
188 return true;
189
190 trace_android_vh_percpu_rwsem_down_read(sem, try, &ret);
191 if (ret)
192 return true;
193
194 if (try)
195 return false;
196
197 trace_contention_begin(sem, LCB_F_PERCPU | LCB_F_READ);
198 preempt_enable();
199 percpu_rwsem_wait(sem, /* .reader = */ true);
200 preempt_disable();
201 trace_contention_end(sem, 0);
202
203 return true;
204 }
205 EXPORT_SYMBOL_GPL(__percpu_down_read);
206
207 #define per_cpu_sum(var) \
208 ({ \
209 typeof(var) __sum = 0; \
210 int cpu; \
211 compiletime_assert_atomic_type(__sum); \
212 for_each_possible_cpu(cpu) \
213 __sum += per_cpu(var, cpu); \
214 __sum; \
215 })
216
percpu_is_read_locked(struct percpu_rw_semaphore * sem)217 bool percpu_is_read_locked(struct percpu_rw_semaphore *sem)
218 {
219 return per_cpu_sum(*sem->read_count) != 0 && !atomic_read(&sem->block);
220 }
221 EXPORT_SYMBOL_GPL(percpu_is_read_locked);
222
223 /*
224 * Return true if the modular sum of the sem->read_count per-CPU variable is
225 * zero. If this sum is zero, then it is stable due to the fact that if any
226 * newly arriving readers increment a given counter, they will immediately
227 * decrement that same counter.
228 *
229 * Assumes sem->block is set.
230 */
readers_active_check(struct percpu_rw_semaphore * sem)231 static bool readers_active_check(struct percpu_rw_semaphore *sem)
232 {
233 if (per_cpu_sum(*sem->read_count) != 0)
234 return false;
235
236 /*
237 * If we observed the decrement; ensure we see the entire critical
238 * section.
239 */
240
241 smp_mb(); /* C matches B */
242
243 return true;
244 }
245
percpu_down_write(struct percpu_rw_semaphore * sem)246 void __sched percpu_down_write(struct percpu_rw_semaphore *sem)
247 {
248 bool contended = false;
249 bool complete = false;
250
251 might_sleep();
252 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
253
254 /* Notify readers to take the slow path. */
255 rcu_sync_enter(&sem->rss);
256
257 /*
258 * Try set sem->block; this provides writer-writer exclusion.
259 * Having sem->block set makes new readers block.
260 */
261 if (!__percpu_down_write_trylock(sem)) {
262 trace_contention_begin(sem, LCB_F_PERCPU | LCB_F_WRITE);
263 percpu_rwsem_wait(sem, /* .reader = */ false);
264 contended = true;
265 }
266
267 /* smp_mb() implied by __percpu_down_write_trylock() on success -- D matches A */
268
269 /*
270 * If they don't see our store of sem->block, then we are guaranteed to
271 * see their sem->read_count increment, and therefore will wait for
272 * them.
273 */
274
275 /* Wait for all active readers to complete. */
276 trace_android_rvh_percpu_rwsem_wait_complete(sem, TASK_UNINTERRUPTIBLE, &complete);
277 if (!complete)
278 rcuwait_wait_event(&sem->writer, readers_active_check(sem), TASK_UNINTERRUPTIBLE);
279 if (contended)
280 trace_contention_end(sem, 0);
281 trace_android_vh_record_pcpu_rwsem_starttime(sem, jiffies);
282 }
283 EXPORT_SYMBOL_GPL(percpu_down_write);
284
percpu_up_write(struct percpu_rw_semaphore * sem)285 void percpu_up_write(struct percpu_rw_semaphore *sem)
286 {
287 rwsem_release(&sem->dep_map, _RET_IP_);
288
289 trace_android_vh_percpu_rwsem_up_write(sem);
290
291 /*
292 * Signal the writer is done, no fast path yet.
293 *
294 * One reason that we cannot just immediately flip to readers_fast is
295 * that new readers might fail to see the results of this writer's
296 * critical section.
297 *
298 * Therefore we force it through the slow path which guarantees an
299 * acquire and thereby guarantees the critical section's consistency.
300 */
301 atomic_set_release(&sem->block, 0);
302
303 /*
304 * Prod any pending reader/writer to make progress.
305 */
306 __wake_up(&sem->waiters, TASK_NORMAL, 1, sem);
307
308 /*
309 * Once this completes (at least one RCU-sched grace period hence) the
310 * reader fast path will be available again. Safe to use outside the
311 * exclusive write lock because its counting.
312 */
313 rcu_sync_exit(&sem->rss);
314 trace_android_vh_record_pcpu_rwsem_starttime(sem, 0);
315 }
316 EXPORT_SYMBOL_GPL(percpu_up_write);
317