xref: /OK3568_Linux_fs/kernel/kernel/locking/rwsem.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /* kernel/rwsem.c: R/W semaphores, public implementation
3  *
4  * Written by David Howells (dhowells@redhat.com).
5  * Derived from asm-i386/semaphore.h
6  *
7  * Writer lock-stealing by Alex Shi <alex.shi@intel.com>
8  * and Michel Lespinasse <walken@google.com>
9  *
10  * Optimistic spinning by Tim Chen <tim.c.chen@intel.com>
11  * and Davidlohr Bueso <davidlohr@hp.com>. Based on mutexes.
12  *
13  * Rwsem count bit fields re-definition and rwsem rearchitecture by
14  * Waiman Long <longman@redhat.com> and
15  * Peter Zijlstra <peterz@infradead.org>.
16  */
17 
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/sched/rt.h>
22 #include <linux/sched/task.h>
23 #include <linux/sched/debug.h>
24 #include <linux/sched/wake_q.h>
25 #include <linux/sched/signal.h>
26 #include <linux/sched/clock.h>
27 #include <linux/export.h>
28 #include <linux/rwsem.h>
29 #include <linux/atomic.h>
30 
31 #include "lock_events.h"
32 #include <trace/hooks/rwsem.h>
33 #include <trace/hooks/dtask.h>
34 
35 /*
36  * The least significant 3 bits of the owner value has the following
37  * meanings when set.
38  *  - Bit 0: RWSEM_READER_OWNED - The rwsem is owned by readers
39  *  - Bit 1: RWSEM_RD_NONSPINNABLE - Readers cannot spin on this lock.
40  *  - Bit 2: RWSEM_WR_NONSPINNABLE - Writers cannot spin on this lock.
41  *
42  * When the rwsem is either owned by an anonymous writer, or it is
43  * reader-owned, but a spinning writer has timed out, both nonspinnable
44  * bits will be set to disable optimistic spinning by readers and writers.
45  * In the later case, the last unlocking reader should then check the
46  * writer nonspinnable bit and clear it only to give writers preference
47  * to acquire the lock via optimistic spinning, but not readers. Similar
48  * action is also done in the reader slowpath.
49 
50  * When a writer acquires a rwsem, it puts its task_struct pointer
51  * into the owner field. It is cleared after an unlock.
52  *
53  * When a reader acquires a rwsem, it will also puts its task_struct
54  * pointer into the owner field with the RWSEM_READER_OWNED bit set.
55  * On unlock, the owner field will largely be left untouched. So
56  * for a free or reader-owned rwsem, the owner value may contain
57  * information about the last reader that acquires the rwsem.
58  *
59  * That information may be helpful in debugging cases where the system
60  * seems to hang on a reader owned rwsem especially if only one reader
61  * is involved. Ideally we would like to track all the readers that own
62  * a rwsem, but the overhead is simply too big.
63  *
64  * Reader optimistic spinning is helpful when the reader critical section
65  * is short and there aren't that many readers around. It makes readers
66  * relatively more preferred than writers. When a writer times out spinning
67  * on a reader-owned lock and set the nospinnable bits, there are two main
68  * reasons for that.
69  *
70  *  1) The reader critical section is long, perhaps the task sleeps after
71  *     acquiring the read lock.
72  *  2) There are just too many readers contending the lock causing it to
73  *     take a while to service all of them.
74  *
75  * In the former case, long reader critical section will impede the progress
76  * of writers which is usually more important for system performance. In
77  * the later case, reader optimistic spinning tends to make the reader
78  * groups that contain readers that acquire the lock together smaller
79  * leading to more of them. That may hurt performance in some cases. In
80  * other words, the setting of nonspinnable bits indicates that reader
81  * optimistic spinning may not be helpful for those workloads that cause
82  * it.
83  *
84  * Therefore, any writers that had observed the setting of the writer
85  * nonspinnable bit for a given rwsem after they fail to acquire the lock
86  * via optimistic spinning will set the reader nonspinnable bit once they
87  * acquire the write lock. Similarly, readers that observe the setting
88  * of reader nonspinnable bit at slowpath entry will set the reader
89  * nonspinnable bits when they acquire the read lock via the wakeup path.
90  *
91  * Once the reader nonspinnable bit is on, it will only be reset when
92  * a writer is able to acquire the rwsem in the fast path or somehow a
93  * reader or writer in the slowpath doesn't observe the nonspinable bit.
94  *
95  * This is to discourage reader optmistic spinning on that particular
96  * rwsem and make writers more preferred. This adaptive disabling of reader
97  * optimistic spinning will alleviate the negative side effect of this
98  * feature.
99  */
100 #define RWSEM_READER_OWNED	(1UL << 0)
101 #define RWSEM_RD_NONSPINNABLE	(1UL << 1)
102 #define RWSEM_WR_NONSPINNABLE	(1UL << 2)
103 #define RWSEM_NONSPINNABLE	(RWSEM_RD_NONSPINNABLE | RWSEM_WR_NONSPINNABLE)
104 #define RWSEM_OWNER_FLAGS_MASK	(RWSEM_READER_OWNED | RWSEM_NONSPINNABLE)
105 
106 #ifdef CONFIG_DEBUG_RWSEMS
107 # define DEBUG_RWSEMS_WARN_ON(c, sem)	do {			\
108 	if (!debug_locks_silent &&				\
109 	    WARN_ONCE(c, "DEBUG_RWSEMS_WARN_ON(%s): count = 0x%lx, magic = 0x%lx, owner = 0x%lx, curr 0x%lx, list %sempty\n",\
110 		#c, atomic_long_read(&(sem)->count),		\
111 		(unsigned long) sem->magic,			\
112 		atomic_long_read(&(sem)->owner), (long)current,	\
113 		list_empty(&(sem)->wait_list) ? "" : "not "))	\
114 			debug_locks_off();			\
115 	} while (0)
116 #else
117 # define DEBUG_RWSEMS_WARN_ON(c, sem)
118 #endif
119 
120 /*
121  * On 64-bit architectures, the bit definitions of the count are:
122  *
123  * Bit  0    - writer locked bit
124  * Bit  1    - waiters present bit
125  * Bit  2    - lock handoff bit
126  * Bits 3-7  - reserved
127  * Bits 8-62 - 55-bit reader count
128  * Bit  63   - read fail bit
129  *
130  * On 32-bit architectures, the bit definitions of the count are:
131  *
132  * Bit  0    - writer locked bit
133  * Bit  1    - waiters present bit
134  * Bit  2    - lock handoff bit
135  * Bits 3-7  - reserved
136  * Bits 8-30 - 23-bit reader count
137  * Bit  31   - read fail bit
138  *
139  * It is not likely that the most significant bit (read fail bit) will ever
140  * be set. This guard bit is still checked anyway in the down_read() fastpath
141  * just in case we need to use up more of the reader bits for other purpose
142  * in the future.
143  *
144  * atomic_long_fetch_add() is used to obtain reader lock, whereas
145  * atomic_long_cmpxchg() will be used to obtain writer lock.
146  *
147  * There are three places where the lock handoff bit may be set or cleared.
148  * 1) rwsem_mark_wake() for readers.
149  * 2) rwsem_try_write_lock() for writers.
150  * 3) Error path of rwsem_down_write_slowpath().
151  *
152  * For all the above cases, wait_lock will be held. A writer must also
153  * be the first one in the wait_list to be eligible for setting the handoff
154  * bit. So concurrent setting/clearing of handoff bit is not possible.
155  */
156 #define RWSEM_WRITER_LOCKED	(1UL << 0)
157 #define RWSEM_FLAG_WAITERS	(1UL << 1)
158 #define RWSEM_FLAG_HANDOFF	(1UL << 2)
159 #define RWSEM_FLAG_READFAIL	(1UL << (BITS_PER_LONG - 1))
160 
161 #define RWSEM_READER_SHIFT	8
162 #define RWSEM_READER_BIAS	(1UL << RWSEM_READER_SHIFT)
163 #define RWSEM_READER_MASK	(~(RWSEM_READER_BIAS - 1))
164 #define RWSEM_WRITER_MASK	RWSEM_WRITER_LOCKED
165 #define RWSEM_LOCK_MASK		(RWSEM_WRITER_MASK|RWSEM_READER_MASK)
166 #define RWSEM_READ_FAILED_MASK	(RWSEM_WRITER_MASK|RWSEM_FLAG_WAITERS|\
167 				 RWSEM_FLAG_HANDOFF|RWSEM_FLAG_READFAIL)
168 
169 /*
170  * All writes to owner are protected by WRITE_ONCE() to make sure that
171  * store tearing can't happen as optimistic spinners may read and use
172  * the owner value concurrently without lock. Read from owner, however,
173  * may not need READ_ONCE() as long as the pointer value is only used
174  * for comparison and isn't being dereferenced.
175  */
rwsem_set_owner(struct rw_semaphore * sem)176 static inline void rwsem_set_owner(struct rw_semaphore *sem)
177 {
178 	atomic_long_set(&sem->owner, (long)current);
179 	trace_android_vh_rwsem_set_owner(sem);
180 }
181 
rwsem_clear_owner(struct rw_semaphore * sem)182 static inline void rwsem_clear_owner(struct rw_semaphore *sem)
183 {
184 	atomic_long_set(&sem->owner, 0);
185 }
186 
187 /*
188  * Test the flags in the owner field.
189  */
rwsem_test_oflags(struct rw_semaphore * sem,long flags)190 static inline bool rwsem_test_oflags(struct rw_semaphore *sem, long flags)
191 {
192 	return atomic_long_read(&sem->owner) & flags;
193 }
194 
195 /*
196  * The task_struct pointer of the last owning reader will be left in
197  * the owner field.
198  *
199  * Note that the owner value just indicates the task has owned the rwsem
200  * previously, it may not be the real owner or one of the real owners
201  * anymore when that field is examined, so take it with a grain of salt.
202  *
203  * The reader non-spinnable bit is preserved.
204  */
__rwsem_set_reader_owned(struct rw_semaphore * sem,struct task_struct * owner)205 static inline void __rwsem_set_reader_owned(struct rw_semaphore *sem,
206 					    struct task_struct *owner)
207 {
208 	unsigned long val = (unsigned long)owner | RWSEM_READER_OWNED |
209 		(atomic_long_read(&sem->owner) & RWSEM_RD_NONSPINNABLE);
210 
211 	atomic_long_set(&sem->owner, val);
212 }
213 
rwsem_set_reader_owned(struct rw_semaphore * sem)214 static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
215 {
216 	__rwsem_set_reader_owned(sem, current);
217 	trace_android_vh_rwsem_set_reader_owned(sem);
218 }
219 
220 /*
221  * Return true if the rwsem is owned by a reader.
222  */
is_rwsem_reader_owned(struct rw_semaphore * sem)223 static inline bool is_rwsem_reader_owned(struct rw_semaphore *sem)
224 {
225 #ifdef CONFIG_DEBUG_RWSEMS
226 	/*
227 	 * Check the count to see if it is write-locked.
228 	 */
229 	long count = atomic_long_read(&sem->count);
230 
231 	if (count & RWSEM_WRITER_MASK)
232 		return false;
233 #endif
234 	return rwsem_test_oflags(sem, RWSEM_READER_OWNED);
235 }
236 
237 #ifdef CONFIG_DEBUG_RWSEMS
238 /*
239  * With CONFIG_DEBUG_RWSEMS configured, it will make sure that if there
240  * is a task pointer in owner of a reader-owned rwsem, it will be the
241  * real owner or one of the real owners. The only exception is when the
242  * unlock is done by up_read_non_owner().
243  */
rwsem_clear_reader_owned(struct rw_semaphore * sem)244 static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem)
245 {
246 	unsigned long val = atomic_long_read(&sem->owner);
247 
248 	while ((val & ~RWSEM_OWNER_FLAGS_MASK) == (unsigned long)current) {
249 		if (atomic_long_try_cmpxchg(&sem->owner, &val,
250 					    val & RWSEM_OWNER_FLAGS_MASK))
251 			return;
252 	}
253 }
254 #else
rwsem_clear_reader_owned(struct rw_semaphore * sem)255 static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem)
256 {
257 }
258 #endif
259 
260 /*
261  * Set the RWSEM_NONSPINNABLE bits if the RWSEM_READER_OWNED flag
262  * remains set. Otherwise, the operation will be aborted.
263  */
rwsem_set_nonspinnable(struct rw_semaphore * sem)264 static inline void rwsem_set_nonspinnable(struct rw_semaphore *sem)
265 {
266 	unsigned long owner = atomic_long_read(&sem->owner);
267 
268 	do {
269 		if (!(owner & RWSEM_READER_OWNED))
270 			break;
271 		if (owner & RWSEM_NONSPINNABLE)
272 			break;
273 	} while (!atomic_long_try_cmpxchg(&sem->owner, &owner,
274 					  owner | RWSEM_NONSPINNABLE));
275 }
276 
rwsem_read_trylock(struct rw_semaphore * sem)277 static inline bool rwsem_read_trylock(struct rw_semaphore *sem)
278 {
279 	long cnt = atomic_long_add_return_acquire(RWSEM_READER_BIAS, &sem->count);
280 	if (WARN_ON_ONCE(cnt < 0))
281 		rwsem_set_nonspinnable(sem);
282 
283 	if ((cnt & RWSEM_READ_FAILED_MASK) == 0)
284 		trace_android_vh_record_rwsem_lock_starttime(current, jiffies);
285 
286 	return !(cnt & RWSEM_READ_FAILED_MASK);
287 }
288 
289 /*
290  * Return just the real task structure pointer of the owner
291  */
rwsem_owner(struct rw_semaphore * sem)292 static inline struct task_struct *rwsem_owner(struct rw_semaphore *sem)
293 {
294 	return (struct task_struct *)
295 		(atomic_long_read(&sem->owner) & ~RWSEM_OWNER_FLAGS_MASK);
296 }
297 
298 /*
299  * Return the real task structure pointer of the owner and the embedded
300  * flags in the owner. pflags must be non-NULL.
301  */
302 static inline struct task_struct *
rwsem_owner_flags(struct rw_semaphore * sem,unsigned long * pflags)303 rwsem_owner_flags(struct rw_semaphore *sem, unsigned long *pflags)
304 {
305 	unsigned long owner = atomic_long_read(&sem->owner);
306 
307 	*pflags = owner & RWSEM_OWNER_FLAGS_MASK;
308 	return (struct task_struct *)(owner & ~RWSEM_OWNER_FLAGS_MASK);
309 }
310 
311 /*
312  * Guide to the rw_semaphore's count field.
313  *
314  * When the RWSEM_WRITER_LOCKED bit in count is set, the lock is owned
315  * by a writer.
316  *
317  * The lock is owned by readers when
318  * (1) the RWSEM_WRITER_LOCKED isn't set in count,
319  * (2) some of the reader bits are set in count, and
320  * (3) the owner field has RWSEM_READ_OWNED bit set.
321  *
322  * Having some reader bits set is not enough to guarantee a readers owned
323  * lock as the readers may be in the process of backing out from the count
324  * and a writer has just released the lock. So another writer may steal
325  * the lock immediately after that.
326  */
327 
328 /*
329  * Initialize an rwsem:
330  */
__init_rwsem(struct rw_semaphore * sem,const char * name,struct lock_class_key * key)331 void __init_rwsem(struct rw_semaphore *sem, const char *name,
332 		  struct lock_class_key *key)
333 {
334 #ifdef CONFIG_DEBUG_LOCK_ALLOC
335 	/*
336 	 * Make sure we are not reinitializing a held semaphore:
337 	 */
338 	debug_check_no_locks_freed((void *)sem, sizeof(*sem));
339 	lockdep_init_map_wait(&sem->dep_map, name, key, 0, LD_WAIT_SLEEP);
340 #endif
341 #ifdef CONFIG_DEBUG_RWSEMS
342 	sem->magic = sem;
343 #endif
344 	atomic_long_set(&sem->count, RWSEM_UNLOCKED_VALUE);
345 	raw_spin_lock_init(&sem->wait_lock);
346 	INIT_LIST_HEAD(&sem->wait_list);
347 	atomic_long_set(&sem->owner, 0L);
348 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
349 	osq_lock_init(&sem->osq);
350 #endif
351 	trace_android_vh_rwsem_init(sem);
352 }
353 EXPORT_SYMBOL(__init_rwsem);
354 
355 #define rwsem_first_waiter(sem) \
356 	list_first_entry(&sem->wait_list, struct rwsem_waiter, list)
357 
358 enum rwsem_wake_type {
359 	RWSEM_WAKE_ANY,		/* Wake whatever's at head of wait list */
360 	RWSEM_WAKE_READERS,	/* Wake readers only */
361 	RWSEM_WAKE_READ_OWNED	/* Waker thread holds the read lock */
362 };
363 
364 enum writer_wait_state {
365 	WRITER_NOT_FIRST,	/* Writer is not first in wait list */
366 	WRITER_FIRST,		/* Writer is first in wait list     */
367 	WRITER_HANDOFF		/* Writer is first & handoff needed */
368 };
369 
370 /*
371  * The typical HZ value is either 250 or 1000. So set the minimum waiting
372  * time to at least 4ms or 1 jiffy (if it is higher than 4ms) in the wait
373  * queue before initiating the handoff protocol.
374  */
375 #define RWSEM_WAIT_TIMEOUT	DIV_ROUND_UP(HZ, 250)
376 
377 /*
378  * Magic number to batch-wakeup waiting readers, even when writers are
379  * also present in the queue. This both limits the amount of work the
380  * waking thread must do and also prevents any potential counter overflow,
381  * however unlikely.
382  */
383 #define MAX_READERS_WAKEUP	0x100
384 
385 /*
386  * handle the lock release when processes blocked on it that can now run
387  * - if we come here from up_xxxx(), then the RWSEM_FLAG_WAITERS bit must
388  *   have been set.
389  * - there must be someone on the queue
390  * - the wait_lock must be held by the caller
391  * - tasks are marked for wakeup, the caller must later invoke wake_up_q()
392  *   to actually wakeup the blocked task(s) and drop the reference count,
393  *   preferably when the wait_lock is released
394  * - woken process blocks are discarded from the list after having task zeroed
395  * - writers are only marked woken if downgrading is false
396  */
rwsem_mark_wake(struct rw_semaphore * sem,enum rwsem_wake_type wake_type,struct wake_q_head * wake_q)397 static void rwsem_mark_wake(struct rw_semaphore *sem,
398 			    enum rwsem_wake_type wake_type,
399 			    struct wake_q_head *wake_q)
400 {
401 	struct rwsem_waiter *waiter, *tmp;
402 	long oldcount, woken = 0, adjustment = 0;
403 	struct list_head wlist;
404 
405 	lockdep_assert_held(&sem->wait_lock);
406 
407 	/*
408 	 * Take a peek at the queue head waiter such that we can determine
409 	 * the wakeup(s) to perform.
410 	 */
411 	waiter = rwsem_first_waiter(sem);
412 
413 	if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
414 		if (wake_type == RWSEM_WAKE_ANY) {
415 			/*
416 			 * Mark writer at the front of the queue for wakeup.
417 			 * Until the task is actually later awoken later by
418 			 * the caller, other writers are able to steal it.
419 			 * Readers, on the other hand, will block as they
420 			 * will notice the queued writer.
421 			 */
422 			wake_q_add(wake_q, waiter->task);
423 			lockevent_inc(rwsem_wake_writer);
424 		}
425 
426 		return;
427 	}
428 
429 	/*
430 	 * No reader wakeup if there are too many of them already.
431 	 */
432 	if (unlikely(atomic_long_read(&sem->count) < 0))
433 		return;
434 
435 	/*
436 	 * Writers might steal the lock before we grant it to the next reader.
437 	 * We prefer to do the first reader grant before counting readers
438 	 * so we can bail out early if a writer stole the lock.
439 	 */
440 	if (wake_type != RWSEM_WAKE_READ_OWNED) {
441 		struct task_struct *owner;
442 
443 		adjustment = RWSEM_READER_BIAS;
444 		oldcount = atomic_long_fetch_add(adjustment, &sem->count);
445 		if (unlikely(oldcount & RWSEM_WRITER_MASK)) {
446 			/*
447 			 * When we've been waiting "too" long (for writers
448 			 * to give up the lock), request a HANDOFF to
449 			 * force the issue.
450 			 */
451 			if (!(oldcount & RWSEM_FLAG_HANDOFF) &&
452 			    time_after(jiffies, waiter->timeout)) {
453 				adjustment -= RWSEM_FLAG_HANDOFF;
454 				lockevent_inc(rwsem_rlock_handoff);
455 			}
456 
457 			atomic_long_add(-adjustment, &sem->count);
458 			return;
459 		}
460 		/*
461 		 * Set it to reader-owned to give spinners an early
462 		 * indication that readers now have the lock.
463 		 * The reader nonspinnable bit seen at slowpath entry of
464 		 * the reader is copied over.
465 		 */
466 		owner = waiter->task;
467 		if (waiter->last_rowner & RWSEM_RD_NONSPINNABLE) {
468 			owner = (void *)((unsigned long)owner | RWSEM_RD_NONSPINNABLE);
469 			lockevent_inc(rwsem_opt_norspin);
470 		}
471 		__rwsem_set_reader_owned(sem, owner);
472 	}
473 
474 	/*
475 	 * Grant up to MAX_READERS_WAKEUP read locks to all the readers in the
476 	 * queue. We know that the woken will be at least 1 as we accounted
477 	 * for above. Note we increment the 'active part' of the count by the
478 	 * number of readers before waking any processes up.
479 	 *
480 	 * This is an adaptation of the phase-fair R/W locks where at the
481 	 * reader phase (first waiter is a reader), all readers are eligible
482 	 * to acquire the lock at the same time irrespective of their order
483 	 * in the queue. The writers acquire the lock according to their
484 	 * order in the queue.
485 	 *
486 	 * We have to do wakeup in 2 passes to prevent the possibility that
487 	 * the reader count may be decremented before it is incremented. It
488 	 * is because the to-be-woken waiter may not have slept yet. So it
489 	 * may see waiter->task got cleared, finish its critical section and
490 	 * do an unlock before the reader count increment.
491 	 *
492 	 * 1) Collect the read-waiters in a separate list, count them and
493 	 *    fully increment the reader count in rwsem.
494 	 * 2) For each waiters in the new list, clear waiter->task and
495 	 *    put them into wake_q to be woken up later.
496 	 */
497 	INIT_LIST_HEAD(&wlist);
498 	list_for_each_entry_safe(waiter, tmp, &sem->wait_list, list) {
499 		if (waiter->type == RWSEM_WAITING_FOR_WRITE)
500 			continue;
501 
502 		woken++;
503 		list_move_tail(&waiter->list, &wlist);
504 
505 		trace_android_vh_rwsem_mark_wake_readers(sem, waiter);
506 		/*
507 		 * Limit # of readers that can be woken up per wakeup call.
508 		 */
509 		if (woken >= MAX_READERS_WAKEUP)
510 			break;
511 	}
512 
513 	adjustment = woken * RWSEM_READER_BIAS - adjustment;
514 	lockevent_cond_inc(rwsem_wake_reader, woken);
515 	if (list_empty(&sem->wait_list)) {
516 		/* hit end of list above */
517 		adjustment -= RWSEM_FLAG_WAITERS;
518 	}
519 
520 	/*
521 	 * When we've woken a reader, we no longer need to force writers
522 	 * to give up the lock and we can clear HANDOFF.
523 	 */
524 	if (woken && (atomic_long_read(&sem->count) & RWSEM_FLAG_HANDOFF))
525 		adjustment -= RWSEM_FLAG_HANDOFF;
526 
527 	if (adjustment)
528 		atomic_long_add(adjustment, &sem->count);
529 
530 	/* 2nd pass */
531 	list_for_each_entry_safe(waiter, tmp, &wlist, list) {
532 		struct task_struct *tsk;
533 
534 		tsk = waiter->task;
535 		get_task_struct(tsk);
536 
537 		/*
538 		 * Ensure calling get_task_struct() before setting the reader
539 		 * waiter to nil such that rwsem_down_read_slowpath() cannot
540 		 * race with do_exit() by always holding a reference count
541 		 * to the task to wakeup.
542 		 */
543 		smp_store_release(&waiter->task, NULL);
544 		/*
545 		 * Ensure issuing the wakeup (either by us or someone else)
546 		 * after setting the reader waiter to nil.
547 		 */
548 		wake_q_add_safe(wake_q, tsk);
549 	}
550 }
551 
552 /*
553  * This function must be called with the sem->wait_lock held to prevent
554  * race conditions between checking the rwsem wait list and setting the
555  * sem->count accordingly.
556  *
557  * If wstate is WRITER_HANDOFF, it will make sure that either the handoff
558  * bit is set or the lock is acquired with handoff bit cleared.
559  */
rwsem_try_write_lock(struct rw_semaphore * sem,enum writer_wait_state wstate)560 static inline bool rwsem_try_write_lock(struct rw_semaphore *sem,
561 					enum writer_wait_state wstate)
562 {
563 	long count, new;
564 
565 	lockdep_assert_held(&sem->wait_lock);
566 
567 	count = atomic_long_read(&sem->count);
568 	do {
569 		bool has_handoff = !!(count & RWSEM_FLAG_HANDOFF);
570 
571 		if (has_handoff && wstate == WRITER_NOT_FIRST)
572 			return false;
573 
574 		new = count;
575 
576 		if (count & RWSEM_LOCK_MASK) {
577 			if (has_handoff || (wstate != WRITER_HANDOFF))
578 				return false;
579 
580 			new |= RWSEM_FLAG_HANDOFF;
581 		} else {
582 			new |= RWSEM_WRITER_LOCKED;
583 			new &= ~RWSEM_FLAG_HANDOFF;
584 
585 			if (list_is_singular(&sem->wait_list))
586 				new &= ~RWSEM_FLAG_WAITERS;
587 		}
588 	} while (!atomic_long_try_cmpxchg_acquire(&sem->count, &count, new));
589 
590 	/*
591 	 * We have either acquired the lock with handoff bit cleared or
592 	 * set the handoff bit.
593 	 */
594 	if (new & RWSEM_FLAG_HANDOFF)
595 		return false;
596 
597 	rwsem_set_owner(sem);
598 	return true;
599 }
600 
601 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
602 /*
603  * Try to acquire read lock before the reader is put on wait queue.
604  * Lock acquisition isn't allowed if the rwsem is locked or a writer handoff
605  * is ongoing.
606  */
rwsem_try_read_lock_unqueued(struct rw_semaphore * sem)607 static inline bool rwsem_try_read_lock_unqueued(struct rw_semaphore *sem)
608 {
609 	long count = atomic_long_read(&sem->count);
610 
611 	if (count & (RWSEM_WRITER_MASK | RWSEM_FLAG_HANDOFF))
612 		return false;
613 
614 	count = atomic_long_fetch_add_acquire(RWSEM_READER_BIAS, &sem->count);
615 	if (!(count & (RWSEM_WRITER_MASK | RWSEM_FLAG_HANDOFF))) {
616 		rwsem_set_reader_owned(sem);
617 		lockevent_inc(rwsem_opt_rlock);
618 		return true;
619 	}
620 
621 	/* Back out the change */
622 	atomic_long_add(-RWSEM_READER_BIAS, &sem->count);
623 	return false;
624 }
625 
626 /*
627  * Try to acquire write lock before the writer has been put on wait queue.
628  */
rwsem_try_write_lock_unqueued(struct rw_semaphore * sem)629 static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem)
630 {
631 	long count = atomic_long_read(&sem->count);
632 
633 	while (!(count & (RWSEM_LOCK_MASK|RWSEM_FLAG_HANDOFF))) {
634 		if (atomic_long_try_cmpxchg_acquire(&sem->count, &count,
635 					count | RWSEM_WRITER_LOCKED)) {
636 			rwsem_set_owner(sem);
637 			lockevent_inc(rwsem_opt_wlock);
638 			return true;
639 		}
640 	}
641 	return false;
642 }
643 
owner_on_cpu(struct task_struct * owner)644 static inline bool owner_on_cpu(struct task_struct *owner)
645 {
646 	/*
647 	 * As lock holder preemption issue, we both skip spinning if
648 	 * task is not on cpu or its cpu is preempted
649 	 */
650 	return owner->on_cpu && !vcpu_is_preempted(task_cpu(owner));
651 }
652 
rwsem_can_spin_on_owner(struct rw_semaphore * sem,unsigned long nonspinnable)653 static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem,
654 					   unsigned long nonspinnable)
655 {
656 	struct task_struct *owner;
657 	unsigned long flags;
658 	bool ret = true;
659 
660 	if (need_resched()) {
661 		lockevent_inc(rwsem_opt_fail);
662 		return false;
663 	}
664 
665 	preempt_disable();
666 	rcu_read_lock();
667 	owner = rwsem_owner_flags(sem, &flags);
668 	/*
669 	 * Don't check the read-owner as the entry may be stale.
670 	 */
671 	if ((flags & nonspinnable) ||
672 	    (owner && !(flags & RWSEM_READER_OWNED) && !owner_on_cpu(owner)))
673 		ret = false;
674 	rcu_read_unlock();
675 	preempt_enable();
676 
677 	lockevent_cond_inc(rwsem_opt_fail, !ret);
678 	return ret;
679 }
680 
681 /*
682  * The rwsem_spin_on_owner() function returns the folowing 4 values
683  * depending on the lock owner state.
684  *   OWNER_NULL  : owner is currently NULL
685  *   OWNER_WRITER: when owner changes and is a writer
686  *   OWNER_READER: when owner changes and the new owner may be a reader.
687  *   OWNER_NONSPINNABLE:
688  *		   when optimistic spinning has to stop because either the
689  *		   owner stops running, is unknown, or its timeslice has
690  *		   been used up.
691  */
692 enum owner_state {
693 	OWNER_NULL		= 1 << 0,
694 	OWNER_WRITER		= 1 << 1,
695 	OWNER_READER		= 1 << 2,
696 	OWNER_NONSPINNABLE	= 1 << 3,
697 };
698 #define OWNER_SPINNABLE		(OWNER_NULL | OWNER_WRITER | OWNER_READER)
699 
700 static inline enum owner_state
rwsem_owner_state(struct task_struct * owner,unsigned long flags,unsigned long nonspinnable)701 rwsem_owner_state(struct task_struct *owner, unsigned long flags, unsigned long nonspinnable)
702 {
703 	if (flags & nonspinnable)
704 		return OWNER_NONSPINNABLE;
705 
706 	if (flags & RWSEM_READER_OWNED)
707 		return OWNER_READER;
708 
709 	return owner ? OWNER_WRITER : OWNER_NULL;
710 }
711 
712 static noinline enum owner_state
rwsem_spin_on_owner(struct rw_semaphore * sem,unsigned long nonspinnable)713 rwsem_spin_on_owner(struct rw_semaphore *sem, unsigned long nonspinnable)
714 {
715 	struct task_struct *new, *owner;
716 	unsigned long flags, new_flags;
717 	enum owner_state state;
718 
719 	owner = rwsem_owner_flags(sem, &flags);
720 	state = rwsem_owner_state(owner, flags, nonspinnable);
721 	if (state != OWNER_WRITER)
722 		return state;
723 
724 	rcu_read_lock();
725 	for (;;) {
726 		/*
727 		 * When a waiting writer set the handoff flag, it may spin
728 		 * on the owner as well. Once that writer acquires the lock,
729 		 * we can spin on it. So we don't need to quit even when the
730 		 * handoff bit is set.
731 		 */
732 		new = rwsem_owner_flags(sem, &new_flags);
733 		if ((new != owner) || (new_flags != flags)) {
734 			state = rwsem_owner_state(new, new_flags, nonspinnable);
735 			break;
736 		}
737 
738 		/*
739 		 * Ensure we emit the owner->on_cpu, dereference _after_
740 		 * checking sem->owner still matches owner, if that fails,
741 		 * owner might point to free()d memory, if it still matches,
742 		 * the rcu_read_lock() ensures the memory stays valid.
743 		 */
744 		barrier();
745 
746 		if (need_resched() || !owner_on_cpu(owner)) {
747 			state = OWNER_NONSPINNABLE;
748 			break;
749 		}
750 
751 		cpu_relax();
752 	}
753 	rcu_read_unlock();
754 
755 	return state;
756 }
757 
758 /*
759  * Calculate reader-owned rwsem spinning threshold for writer
760  *
761  * The more readers own the rwsem, the longer it will take for them to
762  * wind down and free the rwsem. So the empirical formula used to
763  * determine the actual spinning time limit here is:
764  *
765  *   Spinning threshold = (10 + nr_readers/2)us
766  *
767  * The limit is capped to a maximum of 25us (30 readers). This is just
768  * a heuristic and is subjected to change in the future.
769  */
rwsem_rspin_threshold(struct rw_semaphore * sem)770 static inline u64 rwsem_rspin_threshold(struct rw_semaphore *sem)
771 {
772 	long count = atomic_long_read(&sem->count);
773 	int readers = count >> RWSEM_READER_SHIFT;
774 	u64 delta;
775 
776 	if (readers > 30)
777 		readers = 30;
778 	delta = (20 + readers) * NSEC_PER_USEC / 2;
779 
780 	return sched_clock() + delta;
781 }
782 
rwsem_optimistic_spin(struct rw_semaphore * sem,bool wlock)783 static bool rwsem_optimistic_spin(struct rw_semaphore *sem, bool wlock)
784 {
785 	bool taken = false;
786 	int prev_owner_state = OWNER_NULL;
787 	int loop = 0;
788 	u64 rspin_threshold = 0;
789 	unsigned long nonspinnable = wlock ? RWSEM_WR_NONSPINNABLE
790 					   : RWSEM_RD_NONSPINNABLE;
791 
792 	preempt_disable();
793 
794 	/* sem->wait_lock should not be held when doing optimistic spinning */
795 	if (!osq_lock(&sem->osq))
796 		goto done;
797 
798 	/*
799 	 * Optimistically spin on the owner field and attempt to acquire the
800 	 * lock whenever the owner changes. Spinning will be stopped when:
801 	 *  1) the owning writer isn't running; or
802 	 *  2) readers own the lock and spinning time has exceeded limit.
803 	 */
804 	for (;;) {
805 		enum owner_state owner_state;
806 
807 		owner_state = rwsem_spin_on_owner(sem, nonspinnable);
808 		if (!(owner_state & OWNER_SPINNABLE))
809 			break;
810 
811 		/*
812 		 * Try to acquire the lock
813 		 */
814 		taken = wlock ? rwsem_try_write_lock_unqueued(sem)
815 			      : rwsem_try_read_lock_unqueued(sem);
816 
817 		if (taken)
818 			break;
819 
820 		/*
821 		 * Time-based reader-owned rwsem optimistic spinning
822 		 */
823 		if (wlock && (owner_state == OWNER_READER)) {
824 			/*
825 			 * Re-initialize rspin_threshold every time when
826 			 * the owner state changes from non-reader to reader.
827 			 * This allows a writer to steal the lock in between
828 			 * 2 reader phases and have the threshold reset at
829 			 * the beginning of the 2nd reader phase.
830 			 */
831 			if (prev_owner_state != OWNER_READER) {
832 				if (rwsem_test_oflags(sem, nonspinnable))
833 					break;
834 				rspin_threshold = rwsem_rspin_threshold(sem);
835 				loop = 0;
836 			}
837 
838 			/*
839 			 * Check time threshold once every 16 iterations to
840 			 * avoid calling sched_clock() too frequently so
841 			 * as to reduce the average latency between the times
842 			 * when the lock becomes free and when the spinner
843 			 * is ready to do a trylock.
844 			 */
845 			else if (!(++loop & 0xf) && (sched_clock() > rspin_threshold)) {
846 				rwsem_set_nonspinnable(sem);
847 				lockevent_inc(rwsem_opt_nospin);
848 				break;
849 			}
850 		}
851 
852 		/*
853 		 * An RT task cannot do optimistic spinning if it cannot
854 		 * be sure the lock holder is running or live-lock may
855 		 * happen if the current task and the lock holder happen
856 		 * to run in the same CPU. However, aborting optimistic
857 		 * spinning while a NULL owner is detected may miss some
858 		 * opportunity where spinning can continue without causing
859 		 * problem.
860 		 *
861 		 * There are 2 possible cases where an RT task may be able
862 		 * to continue spinning.
863 		 *
864 		 * 1) The lock owner is in the process of releasing the
865 		 *    lock, sem->owner is cleared but the lock has not
866 		 *    been released yet.
867 		 * 2) The lock was free and owner cleared, but another
868 		 *    task just comes in and acquire the lock before
869 		 *    we try to get it. The new owner may be a spinnable
870 		 *    writer.
871 		 *
872 		 * To take advantage of two scenarios listed agove, the RT
873 		 * task is made to retry one more time to see if it can
874 		 * acquire the lock or continue spinning on the new owning
875 		 * writer. Of course, if the time lag is long enough or the
876 		 * new owner is not a writer or spinnable, the RT task will
877 		 * quit spinning.
878 		 *
879 		 * If the owner is a writer, the need_resched() check is
880 		 * done inside rwsem_spin_on_owner(). If the owner is not
881 		 * a writer, need_resched() check needs to be done here.
882 		 */
883 		if (owner_state != OWNER_WRITER) {
884 			if (need_resched())
885 				break;
886 			if (rt_task(current) &&
887 			   (prev_owner_state != OWNER_WRITER))
888 				break;
889 		}
890 		prev_owner_state = owner_state;
891 
892 		/*
893 		 * The cpu_relax() call is a compiler barrier which forces
894 		 * everything in this loop to be re-loaded. We don't need
895 		 * memory barriers as we'll eventually observe the right
896 		 * values at the cost of a few extra spins.
897 		 */
898 		cpu_relax();
899 	}
900 	osq_unlock(&sem->osq);
901 done:
902 	preempt_enable();
903 	lockevent_cond_inc(rwsem_opt_fail, !taken);
904 	return taken;
905 }
906 
907 /*
908  * Clear the owner's RWSEM_WR_NONSPINNABLE bit if it is set. This should
909  * only be called when the reader count reaches 0.
910  *
911  * This give writers better chance to acquire the rwsem first before
912  * readers when the rwsem was being held by readers for a relatively long
913  * period of time. Race can happen that an optimistic spinner may have
914  * just stolen the rwsem and set the owner, but just clearing the
915  * RWSEM_WR_NONSPINNABLE bit will do no harm anyway.
916  */
clear_wr_nonspinnable(struct rw_semaphore * sem)917 static inline void clear_wr_nonspinnable(struct rw_semaphore *sem)
918 {
919 	if (rwsem_test_oflags(sem, RWSEM_WR_NONSPINNABLE))
920 		atomic_long_andnot(RWSEM_WR_NONSPINNABLE, &sem->owner);
921 }
922 
923 /*
924  * This function is called when the reader fails to acquire the lock via
925  * optimistic spinning. In this case we will still attempt to do a trylock
926  * when comparing the rwsem state right now with the state when entering
927  * the slowpath indicates that the reader is still in a valid reader phase.
928  * This happens when the following conditions are true:
929  *
930  * 1) The lock is currently reader owned, and
931  * 2) The lock is previously not reader-owned or the last read owner changes.
932  *
933  * In the former case, we have transitioned from a writer phase to a
934  * reader-phase while spinning. In the latter case, it means the reader
935  * phase hasn't ended when we entered the optimistic spinning loop. In
936  * both cases, the reader is eligible to acquire the lock. This is the
937  * secondary path where a read lock is acquired optimistically.
938  *
939  * The reader non-spinnable bit wasn't set at time of entry or it will
940  * not be here at all.
941  */
rwsem_reader_phase_trylock(struct rw_semaphore * sem,unsigned long last_rowner)942 static inline bool rwsem_reader_phase_trylock(struct rw_semaphore *sem,
943 					      unsigned long last_rowner)
944 {
945 	unsigned long owner = atomic_long_read(&sem->owner);
946 
947 	if (!(owner & RWSEM_READER_OWNED))
948 		return false;
949 
950 	if (((owner ^ last_rowner) & ~RWSEM_OWNER_FLAGS_MASK) &&
951 	    rwsem_try_read_lock_unqueued(sem)) {
952 		lockevent_inc(rwsem_opt_rlock2);
953 		lockevent_add(rwsem_opt_fail, -1);
954 		return true;
955 	}
956 	return false;
957 }
958 #else
rwsem_can_spin_on_owner(struct rw_semaphore * sem,unsigned long nonspinnable)959 static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem,
960 					   unsigned long nonspinnable)
961 {
962 	return false;
963 }
964 
rwsem_optimistic_spin(struct rw_semaphore * sem,bool wlock)965 static inline bool rwsem_optimistic_spin(struct rw_semaphore *sem, bool wlock)
966 {
967 	return false;
968 }
969 
clear_wr_nonspinnable(struct rw_semaphore * sem)970 static inline void clear_wr_nonspinnable(struct rw_semaphore *sem) { }
971 
rwsem_reader_phase_trylock(struct rw_semaphore * sem,unsigned long last_rowner)972 static inline bool rwsem_reader_phase_trylock(struct rw_semaphore *sem,
973 					      unsigned long last_rowner)
974 {
975 	return false;
976 }
977 
978 static inline int
rwsem_spin_on_owner(struct rw_semaphore * sem,unsigned long nonspinnable)979 rwsem_spin_on_owner(struct rw_semaphore *sem, unsigned long nonspinnable)
980 {
981 	return 0;
982 }
983 #define OWNER_NULL	1
984 #endif
985 
986 /*
987  * Wait for the read lock to be granted
988  */
989 static struct rw_semaphore __sched *
rwsem_down_read_slowpath(struct rw_semaphore * sem,int state)990 rwsem_down_read_slowpath(struct rw_semaphore *sem, int state)
991 {
992 	long count, adjustment = -RWSEM_READER_BIAS;
993 	struct rwsem_waiter waiter;
994 	DEFINE_WAKE_Q(wake_q);
995 	bool wake = false;
996 	bool already_on_list = false;
997 
998 	/*
999 	 * Save the current read-owner of rwsem, if available, and the
1000 	 * reader nonspinnable bit.
1001 	 */
1002 	waiter.last_rowner = atomic_long_read(&sem->owner);
1003 	if (!(waiter.last_rowner & RWSEM_READER_OWNED))
1004 		waiter.last_rowner &= RWSEM_RD_NONSPINNABLE;
1005 
1006 	if (!rwsem_can_spin_on_owner(sem, RWSEM_RD_NONSPINNABLE))
1007 		goto queue;
1008 
1009 	/*
1010 	 * Undo read bias from down_read() and do optimistic spinning.
1011 	 */
1012 	atomic_long_add(-RWSEM_READER_BIAS, &sem->count);
1013 	adjustment = 0;
1014 	if (rwsem_optimistic_spin(sem, false)) {
1015 		/* rwsem_optimistic_spin() implies ACQUIRE on success */
1016 		/*
1017 		 * Wake up other readers in the wait list if the front
1018 		 * waiter is a reader.
1019 		 */
1020 		if ((atomic_long_read(&sem->count) & RWSEM_FLAG_WAITERS)) {
1021 			raw_spin_lock_irq(&sem->wait_lock);
1022 			if (!list_empty(&sem->wait_list))
1023 				rwsem_mark_wake(sem, RWSEM_WAKE_READ_OWNED,
1024 						&wake_q);
1025 			raw_spin_unlock_irq(&sem->wait_lock);
1026 			wake_up_q(&wake_q);
1027 		}
1028 		trace_android_vh_record_rwsem_lock_starttime(current, jiffies);
1029 		return sem;
1030 	} else if (rwsem_reader_phase_trylock(sem, waiter.last_rowner)) {
1031 		/* rwsem_reader_phase_trylock() implies ACQUIRE on success */
1032 		trace_android_vh_record_rwsem_lock_starttime(current, jiffies);
1033 		return sem;
1034 	}
1035 
1036 queue:
1037 	waiter.task = current;
1038 	waiter.type = RWSEM_WAITING_FOR_READ;
1039 	waiter.timeout = jiffies + RWSEM_WAIT_TIMEOUT;
1040 
1041 	raw_spin_lock_irq(&sem->wait_lock);
1042 	if (list_empty(&sem->wait_list)) {
1043 		/*
1044 		 * In case the wait queue is empty and the lock isn't owned
1045 		 * by a writer or has the handoff bit set, this reader can
1046 		 * exit the slowpath and return immediately as its
1047 		 * RWSEM_READER_BIAS has already been set in the count.
1048 		 */
1049 		if (adjustment && !(atomic_long_read(&sem->count) &
1050 		     (RWSEM_WRITER_MASK | RWSEM_FLAG_HANDOFF))) {
1051 			/* Provide lock ACQUIRE */
1052 			smp_acquire__after_ctrl_dep();
1053 			raw_spin_unlock_irq(&sem->wait_lock);
1054 			rwsem_set_reader_owned(sem);
1055 			lockevent_inc(rwsem_rlock_fast);
1056 			return sem;
1057 		}
1058 		adjustment += RWSEM_FLAG_WAITERS;
1059 	}
1060 	trace_android_vh_alter_rwsem_list_add(
1061 					&waiter,
1062 					sem, &already_on_list);
1063 	if (!already_on_list)
1064 		list_add_tail(&waiter.list, &sem->wait_list);
1065 
1066 	/* we're now waiting on the lock, but no longer actively locking */
1067 	if (adjustment)
1068 		count = atomic_long_add_return(adjustment, &sem->count);
1069 	else
1070 		count = atomic_long_read(&sem->count);
1071 
1072 	/*
1073 	 * If there are no active locks, wake the front queued process(es).
1074 	 *
1075 	 * If there are no writers and we are first in the queue,
1076 	 * wake our own waiter to join the existing active readers !
1077 	 */
1078 	if (!(count & RWSEM_LOCK_MASK)) {
1079 		clear_wr_nonspinnable(sem);
1080 		wake = true;
1081 	}
1082 	if (wake || (!(count & RWSEM_WRITER_MASK) &&
1083 		    (adjustment & RWSEM_FLAG_WAITERS)))
1084 		rwsem_mark_wake(sem, RWSEM_WAKE_ANY, &wake_q);
1085 
1086 	trace_android_vh_rwsem_wake(sem);
1087 	raw_spin_unlock_irq(&sem->wait_lock);
1088 	wake_up_q(&wake_q);
1089 
1090 	/* wait to be given the lock */
1091 	trace_android_vh_rwsem_read_wait_start(sem);
1092 	for (;;) {
1093 		set_current_state(state);
1094 		if (!smp_load_acquire(&waiter.task)) {
1095 			/* Matches rwsem_mark_wake()'s smp_store_release(). */
1096 			break;
1097 		}
1098 		if (signal_pending_state(state, current)) {
1099 			raw_spin_lock_irq(&sem->wait_lock);
1100 			if (waiter.task)
1101 				goto out_nolock;
1102 			raw_spin_unlock_irq(&sem->wait_lock);
1103 			/* Ordered by sem->wait_lock against rwsem_mark_wake(). */
1104 			break;
1105 		}
1106 		schedule();
1107 		lockevent_inc(rwsem_sleep_reader);
1108 	}
1109 
1110 	__set_current_state(TASK_RUNNING);
1111 	trace_android_vh_rwsem_read_wait_finish(sem);
1112 	lockevent_inc(rwsem_rlock);
1113 	trace_android_vh_record_rwsem_lock_starttime(current, jiffies);
1114 	return sem;
1115 
1116 out_nolock:
1117 	list_del(&waiter.list);
1118 	if (list_empty(&sem->wait_list)) {
1119 		atomic_long_andnot(RWSEM_FLAG_WAITERS|RWSEM_FLAG_HANDOFF,
1120 				   &sem->count);
1121 	}
1122 	raw_spin_unlock_irq(&sem->wait_lock);
1123 	__set_current_state(TASK_RUNNING);
1124 	trace_android_vh_rwsem_read_wait_finish(sem);
1125 	lockevent_inc(rwsem_rlock_fail);
1126 	return ERR_PTR(-EINTR);
1127 }
1128 
1129 /*
1130  * This function is called by the a write lock owner. So the owner value
1131  * won't get changed by others.
1132  */
rwsem_disable_reader_optspin(struct rw_semaphore * sem,bool disable)1133 static inline void rwsem_disable_reader_optspin(struct rw_semaphore *sem,
1134 						bool disable)
1135 {
1136 	if (unlikely(disable)) {
1137 		atomic_long_or(RWSEM_RD_NONSPINNABLE, &sem->owner);
1138 		lockevent_inc(rwsem_opt_norspin);
1139 	}
1140 }
1141 
1142 /*
1143  * Wait until we successfully acquire the write lock
1144  */
1145 static struct rw_semaphore *
rwsem_down_write_slowpath(struct rw_semaphore * sem,int state)1146 rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
1147 {
1148 	long count;
1149 	bool disable_rspin;
1150 	enum writer_wait_state wstate;
1151 	struct rwsem_waiter waiter;
1152 	struct rw_semaphore *ret = sem;
1153 	DEFINE_WAKE_Q(wake_q);
1154 	bool already_on_list = false;
1155 
1156 	/* do optimistic spinning and steal lock if possible */
1157 	if (rwsem_can_spin_on_owner(sem, RWSEM_WR_NONSPINNABLE) &&
1158 	    rwsem_optimistic_spin(sem, true)) {
1159 		/* rwsem_optimistic_spin() implies ACQUIRE on success */
1160 		trace_android_vh_record_rwsem_lock_starttime(current, jiffies);
1161 		return sem;
1162 	}
1163 
1164 	/*
1165 	 * Disable reader optimistic spinning for this rwsem after
1166 	 * acquiring the write lock when the setting of the nonspinnable
1167 	 * bits are observed.
1168 	 */
1169 	disable_rspin = atomic_long_read(&sem->owner) & RWSEM_NONSPINNABLE;
1170 
1171 	/*
1172 	 * Optimistic spinning failed, proceed to the slowpath
1173 	 * and block until we can acquire the sem.
1174 	 */
1175 	waiter.task = current;
1176 	waiter.type = RWSEM_WAITING_FOR_WRITE;
1177 	waiter.timeout = jiffies + RWSEM_WAIT_TIMEOUT;
1178 
1179 	raw_spin_lock_irq(&sem->wait_lock);
1180 
1181 	/* account for this before adding a new element to the list */
1182 	wstate = list_empty(&sem->wait_list) ? WRITER_FIRST : WRITER_NOT_FIRST;
1183 
1184 	trace_android_vh_alter_rwsem_list_add(
1185 					&waiter,
1186 					sem, &already_on_list);
1187 	if (!already_on_list)
1188 		list_add_tail(&waiter.list, &sem->wait_list);
1189 
1190 	/* we're now waiting on the lock */
1191 	if (wstate == WRITER_NOT_FIRST) {
1192 		count = atomic_long_read(&sem->count);
1193 
1194 		/*
1195 		 * If there were already threads queued before us and:
1196 		 *  1) there are no active locks, wake the front
1197 		 *     queued process(es) as the handoff bit might be set.
1198 		 *  2) there are no active writers and some readers, the lock
1199 		 *     must be read owned; so we try to wake any read lock
1200 		 *     waiters that were queued ahead of us.
1201 		 */
1202 		if (count & RWSEM_WRITER_MASK)
1203 			goto wait;
1204 
1205 		rwsem_mark_wake(sem, (count & RWSEM_READER_MASK)
1206 					? RWSEM_WAKE_READERS
1207 					: RWSEM_WAKE_ANY, &wake_q);
1208 
1209 		if (!wake_q_empty(&wake_q)) {
1210 			/*
1211 			 * We want to minimize wait_lock hold time especially
1212 			 * when a large number of readers are to be woken up.
1213 			 */
1214 			raw_spin_unlock_irq(&sem->wait_lock);
1215 			wake_up_q(&wake_q);
1216 			wake_q_init(&wake_q);	/* Used again, reinit */
1217 			raw_spin_lock_irq(&sem->wait_lock);
1218 		}
1219 	} else {
1220 		atomic_long_or(RWSEM_FLAG_WAITERS, &sem->count);
1221 	}
1222 
1223 wait:
1224 	trace_android_vh_rwsem_wake(sem);
1225 	/* wait until we successfully acquire the lock */
1226 	trace_android_vh_rwsem_write_wait_start(sem);
1227 	set_current_state(state);
1228 	for (;;) {
1229 		if (rwsem_try_write_lock(sem, wstate)) {
1230 			/* rwsem_try_write_lock() implies ACQUIRE on success */
1231 			break;
1232 		}
1233 
1234 		raw_spin_unlock_irq(&sem->wait_lock);
1235 
1236 		/*
1237 		 * After setting the handoff bit and failing to acquire
1238 		 * the lock, attempt to spin on owner to accelerate lock
1239 		 * transfer. If the previous owner is a on-cpu writer and it
1240 		 * has just released the lock, OWNER_NULL will be returned.
1241 		 * In this case, we attempt to acquire the lock again
1242 		 * without sleeping.
1243 		 */
1244 		if (wstate == WRITER_HANDOFF &&
1245 		    rwsem_spin_on_owner(sem, RWSEM_NONSPINNABLE) == OWNER_NULL)
1246 			goto trylock_again;
1247 
1248 		/* Block until there are no active lockers. */
1249 		for (;;) {
1250 			if (signal_pending_state(state, current))
1251 				goto out_nolock;
1252 
1253 			schedule();
1254 			lockevent_inc(rwsem_sleep_writer);
1255 			set_current_state(state);
1256 			/*
1257 			 * If HANDOFF bit is set, unconditionally do
1258 			 * a trylock.
1259 			 */
1260 			if (wstate == WRITER_HANDOFF)
1261 				break;
1262 
1263 			if ((wstate == WRITER_NOT_FIRST) &&
1264 			    (rwsem_first_waiter(sem) == &waiter))
1265 				wstate = WRITER_FIRST;
1266 
1267 			count = atomic_long_read(&sem->count);
1268 			if (!(count & RWSEM_LOCK_MASK))
1269 				break;
1270 
1271 			/*
1272 			 * The setting of the handoff bit is deferred
1273 			 * until rwsem_try_write_lock() is called.
1274 			 */
1275 			if ((wstate == WRITER_FIRST) && (rt_task(current) ||
1276 			    time_after(jiffies, waiter.timeout))) {
1277 				wstate = WRITER_HANDOFF;
1278 				lockevent_inc(rwsem_wlock_handoff);
1279 				break;
1280 			}
1281 		}
1282 trylock_again:
1283 		raw_spin_lock_irq(&sem->wait_lock);
1284 	}
1285 	__set_current_state(TASK_RUNNING);
1286 	trace_android_vh_rwsem_write_wait_finish(sem);
1287 	list_del(&waiter.list);
1288 	rwsem_disable_reader_optspin(sem, disable_rspin);
1289 	raw_spin_unlock_irq(&sem->wait_lock);
1290 	lockevent_inc(rwsem_wlock);
1291 	trace_android_vh_record_rwsem_lock_starttime(current, jiffies);
1292 	return ret;
1293 
1294 out_nolock:
1295 	__set_current_state(TASK_RUNNING);
1296 	trace_android_vh_rwsem_write_wait_finish(sem);
1297 	raw_spin_lock_irq(&sem->wait_lock);
1298 	list_del(&waiter.list);
1299 
1300 	if (unlikely(wstate == WRITER_HANDOFF))
1301 		atomic_long_andnot(RWSEM_FLAG_HANDOFF,  &sem->count);
1302 
1303 	if (list_empty(&sem->wait_list))
1304 		atomic_long_andnot(RWSEM_FLAG_WAITERS, &sem->count);
1305 	else
1306 		rwsem_mark_wake(sem, RWSEM_WAKE_ANY, &wake_q);
1307 	raw_spin_unlock_irq(&sem->wait_lock);
1308 	wake_up_q(&wake_q);
1309 	lockevent_inc(rwsem_wlock_fail);
1310 
1311 	return ERR_PTR(-EINTR);
1312 }
1313 
1314 /*
1315  * handle waking up a waiter on the semaphore
1316  * - up_read/up_write has decremented the active part of count if we come here
1317  */
rwsem_wake(struct rw_semaphore * sem,long count)1318 static struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem, long count)
1319 {
1320 	unsigned long flags;
1321 	DEFINE_WAKE_Q(wake_q);
1322 
1323 	raw_spin_lock_irqsave(&sem->wait_lock, flags);
1324 
1325 	if (!list_empty(&sem->wait_list))
1326 		rwsem_mark_wake(sem, RWSEM_WAKE_ANY, &wake_q);
1327 	trace_android_vh_rwsem_wake_finish(sem);
1328 
1329 	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
1330 	wake_up_q(&wake_q);
1331 
1332 	return sem;
1333 }
1334 
1335 /*
1336  * downgrade a write lock into a read lock
1337  * - caller incremented waiting part of count and discovered it still negative
1338  * - just wake up any readers at the front of the queue
1339  */
rwsem_downgrade_wake(struct rw_semaphore * sem)1340 static struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem)
1341 {
1342 	unsigned long flags;
1343 	DEFINE_WAKE_Q(wake_q);
1344 
1345 	raw_spin_lock_irqsave(&sem->wait_lock, flags);
1346 
1347 	if (!list_empty(&sem->wait_list))
1348 		rwsem_mark_wake(sem, RWSEM_WAKE_READ_OWNED, &wake_q);
1349 
1350 	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
1351 	wake_up_q(&wake_q);
1352 
1353 	return sem;
1354 }
1355 
1356 /*
1357  * lock for reading
1358  */
__down_read(struct rw_semaphore * sem)1359 static inline void __down_read(struct rw_semaphore *sem)
1360 {
1361 	if (!rwsem_read_trylock(sem)) {
1362 		rwsem_down_read_slowpath(sem, TASK_UNINTERRUPTIBLE);
1363 		DEBUG_RWSEMS_WARN_ON(!is_rwsem_reader_owned(sem), sem);
1364 	} else {
1365 		rwsem_set_reader_owned(sem);
1366 	}
1367 }
1368 
__down_read_interruptible(struct rw_semaphore * sem)1369 static inline int __down_read_interruptible(struct rw_semaphore *sem)
1370 {
1371 	if (!rwsem_read_trylock(sem)) {
1372 		if (IS_ERR(rwsem_down_read_slowpath(sem, TASK_INTERRUPTIBLE)))
1373 			return -EINTR;
1374 		DEBUG_RWSEMS_WARN_ON(!is_rwsem_reader_owned(sem), sem);
1375 	} else {
1376 		rwsem_set_reader_owned(sem);
1377 	}
1378 	return 0;
1379 }
1380 
__down_read_killable(struct rw_semaphore * sem)1381 static inline int __down_read_killable(struct rw_semaphore *sem)
1382 {
1383 	if (!rwsem_read_trylock(sem)) {
1384 		if (IS_ERR(rwsem_down_read_slowpath(sem, TASK_KILLABLE)))
1385 			return -EINTR;
1386 		DEBUG_RWSEMS_WARN_ON(!is_rwsem_reader_owned(sem), sem);
1387 	} else {
1388 		rwsem_set_reader_owned(sem);
1389 	}
1390 	return 0;
1391 }
1392 
__down_read_trylock(struct rw_semaphore * sem)1393 static inline int __down_read_trylock(struct rw_semaphore *sem)
1394 {
1395 	long tmp;
1396 
1397 	DEBUG_RWSEMS_WARN_ON(sem->magic != sem, sem);
1398 
1399 	/*
1400 	 * Optimize for the case when the rwsem is not locked at all.
1401 	 */
1402 	tmp = RWSEM_UNLOCKED_VALUE;
1403 	do {
1404 		if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
1405 					tmp + RWSEM_READER_BIAS)) {
1406 			rwsem_set_reader_owned(sem);
1407 			trace_android_vh_record_rwsem_lock_starttime(current, jiffies);
1408 			return 1;
1409 		}
1410 	} while (!(tmp & RWSEM_READ_FAILED_MASK));
1411 	return 0;
1412 }
1413 
1414 /*
1415  * lock for writing
1416  */
__down_write(struct rw_semaphore * sem)1417 static inline void __down_write(struct rw_semaphore *sem)
1418 {
1419 	long tmp = RWSEM_UNLOCKED_VALUE;
1420 
1421 	if (unlikely(!atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
1422 						      RWSEM_WRITER_LOCKED))) {
1423 		rwsem_down_write_slowpath(sem, TASK_UNINTERRUPTIBLE);
1424 	} else {
1425 		trace_android_vh_record_rwsem_lock_starttime(current, jiffies);
1426 		rwsem_set_owner(sem);
1427 	}
1428 }
1429 
__down_write_killable(struct rw_semaphore * sem)1430 static inline int __down_write_killable(struct rw_semaphore *sem)
1431 {
1432 	long tmp = RWSEM_UNLOCKED_VALUE;
1433 
1434 	if (unlikely(!atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
1435 						      RWSEM_WRITER_LOCKED))) {
1436 		if (IS_ERR(rwsem_down_write_slowpath(sem, TASK_KILLABLE)))
1437 			return -EINTR;
1438 	} else {
1439 		trace_android_vh_record_rwsem_lock_starttime(current, jiffies);
1440 		rwsem_set_owner(sem);
1441 	}
1442 	return 0;
1443 }
1444 
__down_write_trylock(struct rw_semaphore * sem)1445 static inline int __down_write_trylock(struct rw_semaphore *sem)
1446 {
1447 	long tmp;
1448 
1449 	DEBUG_RWSEMS_WARN_ON(sem->magic != sem, sem);
1450 
1451 	tmp  = RWSEM_UNLOCKED_VALUE;
1452 	if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
1453 					    RWSEM_WRITER_LOCKED)) {
1454 		rwsem_set_owner(sem);
1455 		trace_android_vh_record_rwsem_lock_starttime(current, jiffies);
1456 		return true;
1457 	}
1458 	return false;
1459 }
1460 
1461 /*
1462  * unlock after reading
1463  */
__up_read(struct rw_semaphore * sem)1464 static inline void __up_read(struct rw_semaphore *sem)
1465 {
1466 	long tmp;
1467 
1468 	DEBUG_RWSEMS_WARN_ON(sem->magic != sem, sem);
1469 	DEBUG_RWSEMS_WARN_ON(!is_rwsem_reader_owned(sem), sem);
1470 
1471 	trace_android_vh_record_rwsem_lock_starttime(current, 0);
1472 	rwsem_clear_reader_owned(sem);
1473 	tmp = atomic_long_add_return_release(-RWSEM_READER_BIAS, &sem->count);
1474 	DEBUG_RWSEMS_WARN_ON(tmp < 0, sem);
1475 	if (unlikely((tmp & (RWSEM_LOCK_MASK|RWSEM_FLAG_WAITERS)) ==
1476 		      RWSEM_FLAG_WAITERS)) {
1477 		clear_wr_nonspinnable(sem);
1478 		rwsem_wake(sem, tmp);
1479 	}
1480 	trace_android_vh_rwsem_up_read_end(sem);
1481 }
1482 
1483 /*
1484  * unlock after writing
1485  */
__up_write(struct rw_semaphore * sem)1486 static inline void __up_write(struct rw_semaphore *sem)
1487 {
1488 	long tmp;
1489 
1490 	DEBUG_RWSEMS_WARN_ON(sem->magic != sem, sem);
1491 	/*
1492 	 * sem->owner may differ from current if the ownership is transferred
1493 	 * to an anonymous writer by setting the RWSEM_NONSPINNABLE bits.
1494 	 */
1495 	DEBUG_RWSEMS_WARN_ON((rwsem_owner(sem) != current) &&
1496 			    !rwsem_test_oflags(sem, RWSEM_NONSPINNABLE), sem);
1497 
1498 	trace_android_vh_record_rwsem_lock_starttime(current, 0);
1499 	rwsem_clear_owner(sem);
1500 	tmp = atomic_long_fetch_add_release(-RWSEM_WRITER_LOCKED, &sem->count);
1501 	if (unlikely(tmp & RWSEM_FLAG_WAITERS))
1502 		rwsem_wake(sem, tmp);
1503 	trace_android_vh_rwsem_up_write_end(sem);
1504 }
1505 
1506 /*
1507  * downgrade write lock to read lock
1508  */
__downgrade_write(struct rw_semaphore * sem)1509 static inline void __downgrade_write(struct rw_semaphore *sem)
1510 {
1511 	long tmp;
1512 
1513 	/*
1514 	 * When downgrading from exclusive to shared ownership,
1515 	 * anything inside the write-locked region cannot leak
1516 	 * into the read side. In contrast, anything in the
1517 	 * read-locked region is ok to be re-ordered into the
1518 	 * write side. As such, rely on RELEASE semantics.
1519 	 */
1520 	DEBUG_RWSEMS_WARN_ON(rwsem_owner(sem) != current, sem);
1521 	tmp = atomic_long_fetch_add_release(
1522 		-RWSEM_WRITER_LOCKED+RWSEM_READER_BIAS, &sem->count);
1523 	rwsem_set_reader_owned(sem);
1524 	if (tmp & RWSEM_FLAG_WAITERS)
1525 		rwsem_downgrade_wake(sem);
1526 }
1527 
1528 /*
1529  * lock for reading
1530  */
down_read(struct rw_semaphore * sem)1531 void __sched down_read(struct rw_semaphore *sem)
1532 {
1533 	might_sleep();
1534 	rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
1535 
1536 	LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
1537 }
1538 EXPORT_SYMBOL(down_read);
1539 
down_read_interruptible(struct rw_semaphore * sem)1540 int __sched down_read_interruptible(struct rw_semaphore *sem)
1541 {
1542 	might_sleep();
1543 	rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
1544 
1545 	if (LOCK_CONTENDED_RETURN(sem, __down_read_trylock, __down_read_interruptible)) {
1546 		rwsem_release(&sem->dep_map, _RET_IP_);
1547 		return -EINTR;
1548 	}
1549 
1550 	return 0;
1551 }
1552 EXPORT_SYMBOL(down_read_interruptible);
1553 
down_read_killable(struct rw_semaphore * sem)1554 int __sched down_read_killable(struct rw_semaphore *sem)
1555 {
1556 	might_sleep();
1557 	rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
1558 
1559 	if (LOCK_CONTENDED_RETURN(sem, __down_read_trylock, __down_read_killable)) {
1560 		rwsem_release(&sem->dep_map, _RET_IP_);
1561 		return -EINTR;
1562 	}
1563 
1564 	return 0;
1565 }
1566 EXPORT_SYMBOL(down_read_killable);
1567 
1568 /*
1569  * trylock for reading -- returns 1 if successful, 0 if contention
1570  */
down_read_trylock(struct rw_semaphore * sem)1571 int down_read_trylock(struct rw_semaphore *sem)
1572 {
1573 	int ret = __down_read_trylock(sem);
1574 
1575 	if (ret == 1)
1576 		rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
1577 	return ret;
1578 }
1579 EXPORT_SYMBOL(down_read_trylock);
1580 
1581 /*
1582  * lock for writing
1583  */
down_write(struct rw_semaphore * sem)1584 void __sched down_write(struct rw_semaphore *sem)
1585 {
1586 	might_sleep();
1587 	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
1588 	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
1589 }
1590 EXPORT_SYMBOL(down_write);
1591 
1592 /*
1593  * lock for writing
1594  */
down_write_killable(struct rw_semaphore * sem)1595 int __sched down_write_killable(struct rw_semaphore *sem)
1596 {
1597 	might_sleep();
1598 	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
1599 
1600 	if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock,
1601 				  __down_write_killable)) {
1602 		rwsem_release(&sem->dep_map, _RET_IP_);
1603 		return -EINTR;
1604 	}
1605 
1606 	return 0;
1607 }
1608 EXPORT_SYMBOL(down_write_killable);
1609 
1610 /*
1611  * trylock for writing -- returns 1 if successful, 0 if contention
1612  */
down_write_trylock(struct rw_semaphore * sem)1613 int down_write_trylock(struct rw_semaphore *sem)
1614 {
1615 	int ret = __down_write_trylock(sem);
1616 
1617 	if (ret == 1)
1618 		rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
1619 
1620 	return ret;
1621 }
1622 EXPORT_SYMBOL(down_write_trylock);
1623 
1624 /*
1625  * release a read lock
1626  */
up_read(struct rw_semaphore * sem)1627 void up_read(struct rw_semaphore *sem)
1628 {
1629 	rwsem_release(&sem->dep_map, _RET_IP_);
1630 	__up_read(sem);
1631 }
1632 EXPORT_SYMBOL(up_read);
1633 
1634 /*
1635  * release a write lock
1636  */
up_write(struct rw_semaphore * sem)1637 void up_write(struct rw_semaphore *sem)
1638 {
1639 	rwsem_release(&sem->dep_map, _RET_IP_);
1640 	trace_android_vh_rwsem_write_finished(sem);
1641 	__up_write(sem);
1642 }
1643 EXPORT_SYMBOL(up_write);
1644 
1645 /*
1646  * downgrade write lock to read lock
1647  */
downgrade_write(struct rw_semaphore * sem)1648 void downgrade_write(struct rw_semaphore *sem)
1649 {
1650 	lock_downgrade(&sem->dep_map, _RET_IP_);
1651 	trace_android_vh_rwsem_write_finished(sem);
1652 	__downgrade_write(sem);
1653 }
1654 EXPORT_SYMBOL(downgrade_write);
1655 
1656 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1657 
down_read_nested(struct rw_semaphore * sem,int subclass)1658 void down_read_nested(struct rw_semaphore *sem, int subclass)
1659 {
1660 	might_sleep();
1661 	rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
1662 	LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
1663 }
1664 EXPORT_SYMBOL(down_read_nested);
1665 
down_read_killable_nested(struct rw_semaphore * sem,int subclass)1666 int down_read_killable_nested(struct rw_semaphore *sem, int subclass)
1667 {
1668 	might_sleep();
1669 	rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
1670 
1671 	if (LOCK_CONTENDED_RETURN(sem, __down_read_trylock, __down_read_killable)) {
1672 		rwsem_release(&sem->dep_map, _RET_IP_);
1673 		return -EINTR;
1674 	}
1675 
1676 	return 0;
1677 }
1678 EXPORT_SYMBOL(down_read_killable_nested);
1679 
_down_write_nest_lock(struct rw_semaphore * sem,struct lockdep_map * nest)1680 void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
1681 {
1682 	might_sleep();
1683 	rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);
1684 	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
1685 }
1686 EXPORT_SYMBOL(_down_write_nest_lock);
1687 
down_read_non_owner(struct rw_semaphore * sem)1688 void down_read_non_owner(struct rw_semaphore *sem)
1689 {
1690 	might_sleep();
1691 	__down_read(sem);
1692 	__rwsem_set_reader_owned(sem, NULL);
1693 }
1694 EXPORT_SYMBOL(down_read_non_owner);
1695 
down_write_nested(struct rw_semaphore * sem,int subclass)1696 void down_write_nested(struct rw_semaphore *sem, int subclass)
1697 {
1698 	might_sleep();
1699 	rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
1700 	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
1701 }
1702 EXPORT_SYMBOL(down_write_nested);
1703 
down_write_killable_nested(struct rw_semaphore * sem,int subclass)1704 int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
1705 {
1706 	might_sleep();
1707 	rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
1708 
1709 	if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock,
1710 				  __down_write_killable)) {
1711 		rwsem_release(&sem->dep_map, _RET_IP_);
1712 		return -EINTR;
1713 	}
1714 
1715 	return 0;
1716 }
1717 EXPORT_SYMBOL(down_write_killable_nested);
1718 
up_read_non_owner(struct rw_semaphore * sem)1719 void up_read_non_owner(struct rw_semaphore *sem)
1720 {
1721 	DEBUG_RWSEMS_WARN_ON(!is_rwsem_reader_owned(sem), sem);
1722 	__up_read(sem);
1723 }
1724 EXPORT_SYMBOL(up_read_non_owner);
1725 
1726 #endif
1727