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