blob: 5532304406f7ad38a672158d8acae7b4a3846378 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnarc4e05112006-07-03 00:24:29 -07002/* kernel/rwsem.c: R/W semaphores, public implementation
3 *
4 * Written by David Howells (dhowells@redhat.com).
5 * Derived from asm-i386/semaphore.h
Waiman Long5dec94d2019-05-20 16:59:03 -04006 *
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 *
Waiman Long4f23dbc2019-05-20 16:59:06 -040013 * Rwsem count bit fields re-definition and rwsem rearchitecture by
14 * Waiman Long <longman@redhat.com> and
15 * Peter Zijlstra <peterz@infradead.org>.
Ingo Molnarc4e05112006-07-03 00:24:29 -070016 */
17
18#include <linux/types.h>
19#include <linux/kernel.h>
Livio Soaresc7af77b2007-12-18 15:21:13 +010020#include <linux/sched.h>
Waiman Long5dec94d2019-05-20 16:59:03 -040021#include <linux/sched/rt.h>
22#include <linux/sched/task.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010023#include <linux/sched/debug.h>
Waiman Long5dec94d2019-05-20 16:59:03 -040024#include <linux/sched/wake_q.h>
25#include <linux/sched/signal.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Ingo Molnarc4e05112006-07-03 00:24:29 -070027#include <linux/rwsem.h>
Arun Sharma600634972011-07-26 16:09:06 -070028#include <linux/atomic.h>
Ingo Molnarc4e05112006-07-03 00:24:29 -070029
Davidlohr Bueso7a215f82015-01-30 01:14:25 -080030#include "rwsem.h"
Waiman Long5dec94d2019-05-20 16:59:03 -040031#include "lock_events.h"
32
33/*
34 * The least significant 2 bits of the owner value has the following
35 * meanings when set.
36 * - RWSEM_READER_OWNED (bit 0): The rwsem is owned by readers
37 * - RWSEM_ANONYMOUSLY_OWNED (bit 1): The rwsem is anonymously owned,
38 * i.e. the owner(s) cannot be readily determined. It can be reader
39 * owned or the owning writer is indeterminate.
40 *
41 * When a writer acquires a rwsem, it puts its task_struct pointer
42 * into the owner field. It is cleared after an unlock.
43 *
44 * When a reader acquires a rwsem, it will also puts its task_struct
45 * pointer into the owner field with both the RWSEM_READER_OWNED and
46 * RWSEM_ANONYMOUSLY_OWNED bits set. On unlock, the owner field will
47 * largely be left untouched. So for a free or reader-owned rwsem,
48 * the owner value may contain information about the last reader that
49 * acquires the rwsem. The anonymous bit is set because that particular
50 * reader may or may not still own the lock.
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#define RWSEM_READER_OWNED (1UL << 0)
58#define RWSEM_ANONYMOUSLY_OWNED (1UL << 1)
59
60#ifdef CONFIG_DEBUG_RWSEMS
61# define DEBUG_RWSEMS_WARN_ON(c, sem) do { \
62 if (!debug_locks_silent && \
63 WARN_ONCE(c, "DEBUG_RWSEMS_WARN_ON(%s): count = 0x%lx, owner = 0x%lx, curr 0x%lx, list %sempty\n",\
64 #c, atomic_long_read(&(sem)->count), \
65 (long)((sem)->owner), (long)current, \
66 list_empty(&(sem)->wait_list) ? "" : "not ")) \
67 debug_locks_off(); \
68 } while (0)
69#else
70# define DEBUG_RWSEMS_WARN_ON(c, sem)
71#endif
72
73/*
74 * The definition of the atomic counter in the semaphore:
75 *
76 * Bit 0 - writer locked bit
77 * Bit 1 - waiters present bit
Waiman Long4f23dbc2019-05-20 16:59:06 -040078 * Bit 2 - lock handoff bit
79 * Bits 3-7 - reserved
Waiman Long5dec94d2019-05-20 16:59:03 -040080 * Bits 8-X - 24-bit (32-bit) or 56-bit reader count
81 *
82 * atomic_long_fetch_add() is used to obtain reader lock, whereas
83 * atomic_long_cmpxchg() will be used to obtain writer lock.
Waiman Long4f23dbc2019-05-20 16:59:06 -040084 *
85 * There are three places where the lock handoff bit may be set or cleared.
86 * 1) rwsem_mark_wake() for readers.
87 * 2) rwsem_try_write_lock() for writers.
88 * 3) Error path of rwsem_down_write_slowpath().
89 *
90 * For all the above cases, wait_lock will be held. A writer must also
91 * be the first one in the wait_list to be eligible for setting the handoff
92 * bit. So concurrent setting/clearing of handoff bit is not possible.
Waiman Long5dec94d2019-05-20 16:59:03 -040093 */
94#define RWSEM_WRITER_LOCKED (1UL << 0)
95#define RWSEM_FLAG_WAITERS (1UL << 1)
Waiman Long4f23dbc2019-05-20 16:59:06 -040096#define RWSEM_FLAG_HANDOFF (1UL << 2)
97
Waiman Long5dec94d2019-05-20 16:59:03 -040098#define RWSEM_READER_SHIFT 8
99#define RWSEM_READER_BIAS (1UL << RWSEM_READER_SHIFT)
100#define RWSEM_READER_MASK (~(RWSEM_READER_BIAS - 1))
101#define RWSEM_WRITER_MASK RWSEM_WRITER_LOCKED
102#define RWSEM_LOCK_MASK (RWSEM_WRITER_MASK|RWSEM_READER_MASK)
Waiman Long4f23dbc2019-05-20 16:59:06 -0400103#define RWSEM_READ_FAILED_MASK (RWSEM_WRITER_MASK|RWSEM_FLAG_WAITERS|\
104 RWSEM_FLAG_HANDOFF)
Waiman Long5dec94d2019-05-20 16:59:03 -0400105
106/*
107 * All writes to owner are protected by WRITE_ONCE() to make sure that
108 * store tearing can't happen as optimistic spinners may read and use
109 * the owner value concurrently without lock. Read from owner, however,
110 * may not need READ_ONCE() as long as the pointer value is only used
111 * for comparison and isn't being dereferenced.
112 */
113static inline void rwsem_set_owner(struct rw_semaphore *sem)
114{
115 WRITE_ONCE(sem->owner, current);
116}
117
118static inline void rwsem_clear_owner(struct rw_semaphore *sem)
119{
120 WRITE_ONCE(sem->owner, NULL);
121}
122
123/*
124 * The task_struct pointer of the last owning reader will be left in
125 * the owner field.
126 *
127 * Note that the owner value just indicates the task has owned the rwsem
128 * previously, it may not be the real owner or one of the real owners
129 * anymore when that field is examined, so take it with a grain of salt.
130 */
131static inline void __rwsem_set_reader_owned(struct rw_semaphore *sem,
132 struct task_struct *owner)
133{
134 unsigned long val = (unsigned long)owner | RWSEM_READER_OWNED
135 | RWSEM_ANONYMOUSLY_OWNED;
136
137 WRITE_ONCE(sem->owner, (struct task_struct *)val);
138}
139
140static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
141{
142 __rwsem_set_reader_owned(sem, current);
143}
144
145/*
146 * Return true if the a rwsem waiter can spin on the rwsem's owner
147 * and steal the lock, i.e. the lock is not anonymously owned.
148 * N.B. !owner is considered spinnable.
149 */
150static inline bool is_rwsem_owner_spinnable(struct task_struct *owner)
151{
152 return !((unsigned long)owner & RWSEM_ANONYMOUSLY_OWNED);
153}
154
155/*
156 * Return true if rwsem is owned by an anonymous writer or readers.
157 */
158static inline bool rwsem_has_anonymous_owner(struct task_struct *owner)
159{
160 return (unsigned long)owner & RWSEM_ANONYMOUSLY_OWNED;
161}
162
163#ifdef CONFIG_DEBUG_RWSEMS
164/*
165 * With CONFIG_DEBUG_RWSEMS configured, it will make sure that if there
166 * is a task pointer in owner of a reader-owned rwsem, it will be the
167 * real owner or one of the real owners. The only exception is when the
168 * unlock is done by up_read_non_owner().
169 */
170static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem)
171{
172 unsigned long val = (unsigned long)current | RWSEM_READER_OWNED
173 | RWSEM_ANONYMOUSLY_OWNED;
174 if (READ_ONCE(sem->owner) == (struct task_struct *)val)
175 cmpxchg_relaxed((unsigned long *)&sem->owner, val,
176 RWSEM_READER_OWNED | RWSEM_ANONYMOUSLY_OWNED);
177}
178#else
179static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem)
180{
181}
182#endif
183
184/*
185 * Guide to the rw_semaphore's count field.
186 *
187 * When the RWSEM_WRITER_LOCKED bit in count is set, the lock is owned
188 * by a writer.
189 *
190 * The lock is owned by readers when
191 * (1) the RWSEM_WRITER_LOCKED isn't set in count,
192 * (2) some of the reader bits are set in count, and
193 * (3) the owner field has RWSEM_READ_OWNED bit set.
194 *
195 * Having some reader bits set is not enough to guarantee a readers owned
196 * lock as the readers may be in the process of backing out from the count
197 * and a writer has just released the lock. So another writer may steal
198 * the lock immediately after that.
199 */
200
201/*
202 * Initialize an rwsem:
203 */
204void __init_rwsem(struct rw_semaphore *sem, const char *name,
205 struct lock_class_key *key)
206{
207#ifdef CONFIG_DEBUG_LOCK_ALLOC
208 /*
209 * Make sure we are not reinitializing a held semaphore:
210 */
211 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
212 lockdep_init_map(&sem->dep_map, name, key, 0);
213#endif
214 atomic_long_set(&sem->count, RWSEM_UNLOCKED_VALUE);
215 raw_spin_lock_init(&sem->wait_lock);
216 INIT_LIST_HEAD(&sem->wait_list);
217 sem->owner = NULL;
218#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
219 osq_lock_init(&sem->osq);
220#endif
221}
Waiman Long5dec94d2019-05-20 16:59:03 -0400222EXPORT_SYMBOL(__init_rwsem);
223
224enum rwsem_waiter_type {
225 RWSEM_WAITING_FOR_WRITE,
226 RWSEM_WAITING_FOR_READ
227};
228
229struct rwsem_waiter {
230 struct list_head list;
231 struct task_struct *task;
232 enum rwsem_waiter_type type;
Waiman Long4f23dbc2019-05-20 16:59:06 -0400233 unsigned long timeout;
Waiman Long5dec94d2019-05-20 16:59:03 -0400234};
Waiman Long4f23dbc2019-05-20 16:59:06 -0400235#define rwsem_first_waiter(sem) \
236 list_first_entry(&sem->wait_list, struct rwsem_waiter, list)
Waiman Long5dec94d2019-05-20 16:59:03 -0400237
238enum rwsem_wake_type {
239 RWSEM_WAKE_ANY, /* Wake whatever's at head of wait list */
240 RWSEM_WAKE_READERS, /* Wake readers only */
241 RWSEM_WAKE_READ_OWNED /* Waker thread holds the read lock */
242};
243
Waiman Long4f23dbc2019-05-20 16:59:06 -0400244enum writer_wait_state {
245 WRITER_NOT_FIRST, /* Writer is not first in wait list */
246 WRITER_FIRST, /* Writer is first in wait list */
247 WRITER_HANDOFF /* Writer is first & handoff needed */
248};
249
250/*
251 * The typical HZ value is either 250 or 1000. So set the minimum waiting
252 * time to at least 4ms or 1 jiffy (if it is higher than 4ms) in the wait
253 * queue before initiating the handoff protocol.
254 */
255#define RWSEM_WAIT_TIMEOUT DIV_ROUND_UP(HZ, 250)
256
Waiman Long5dec94d2019-05-20 16:59:03 -0400257/*
258 * handle the lock release when processes blocked on it that can now run
259 * - if we come here from up_xxxx(), then the RWSEM_FLAG_WAITERS bit must
260 * have been set.
261 * - there must be someone on the queue
262 * - the wait_lock must be held by the caller
263 * - tasks are marked for wakeup, the caller must later invoke wake_up_q()
264 * to actually wakeup the blocked task(s) and drop the reference count,
265 * preferably when the wait_lock is released
266 * - woken process blocks are discarded from the list after having task zeroed
267 * - writers are only marked woken if downgrading is false
268 */
Waiman Long6cef7ff62019-05-20 16:59:04 -0400269static void rwsem_mark_wake(struct rw_semaphore *sem,
270 enum rwsem_wake_type wake_type,
271 struct wake_q_head *wake_q)
Waiman Long5dec94d2019-05-20 16:59:03 -0400272{
273 struct rwsem_waiter *waiter, *tmp;
274 long oldcount, woken = 0, adjustment = 0;
275 struct list_head wlist;
276
Waiman Long4f23dbc2019-05-20 16:59:06 -0400277 lockdep_assert_held(&sem->wait_lock);
278
Waiman Long5dec94d2019-05-20 16:59:03 -0400279 /*
280 * Take a peek at the queue head waiter such that we can determine
281 * the wakeup(s) to perform.
282 */
Waiman Long4f23dbc2019-05-20 16:59:06 -0400283 waiter = rwsem_first_waiter(sem);
Waiman Long5dec94d2019-05-20 16:59:03 -0400284
285 if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
286 if (wake_type == RWSEM_WAKE_ANY) {
287 /*
288 * Mark writer at the front of the queue for wakeup.
289 * Until the task is actually later awoken later by
290 * the caller, other writers are able to steal it.
291 * Readers, on the other hand, will block as they
292 * will notice the queued writer.
293 */
294 wake_q_add(wake_q, waiter->task);
295 lockevent_inc(rwsem_wake_writer);
296 }
297
298 return;
299 }
300
301 /*
302 * Writers might steal the lock before we grant it to the next reader.
303 * We prefer to do the first reader grant before counting readers
304 * so we can bail out early if a writer stole the lock.
305 */
306 if (wake_type != RWSEM_WAKE_READ_OWNED) {
307 adjustment = RWSEM_READER_BIAS;
308 oldcount = atomic_long_fetch_add(adjustment, &sem->count);
309 if (unlikely(oldcount & RWSEM_WRITER_MASK)) {
Waiman Long4f23dbc2019-05-20 16:59:06 -0400310 /*
311 * When we've been waiting "too" long (for writers
312 * to give up the lock), request a HANDOFF to
313 * force the issue.
314 */
315 if (!(oldcount & RWSEM_FLAG_HANDOFF) &&
316 time_after(jiffies, waiter->timeout)) {
317 adjustment -= RWSEM_FLAG_HANDOFF;
318 lockevent_inc(rwsem_rlock_handoff);
319 }
320
321 atomic_long_add(-adjustment, &sem->count);
Waiman Long5dec94d2019-05-20 16:59:03 -0400322 return;
323 }
324 /*
325 * Set it to reader-owned to give spinners an early
326 * indication that readers now have the lock.
327 */
328 __rwsem_set_reader_owned(sem, waiter->task);
329 }
330
331 /*
332 * Grant an infinite number of read locks to the readers at the front
333 * of the queue. We know that woken will be at least 1 as we accounted
334 * for above. Note we increment the 'active part' of the count by the
335 * number of readers before waking any processes up.
336 *
337 * We have to do wakeup in 2 passes to prevent the possibility that
338 * the reader count may be decremented before it is incremented. It
339 * is because the to-be-woken waiter may not have slept yet. So it
340 * may see waiter->task got cleared, finish its critical section and
341 * do an unlock before the reader count increment.
342 *
343 * 1) Collect the read-waiters in a separate list, count them and
344 * fully increment the reader count in rwsem.
345 * 2) For each waiters in the new list, clear waiter->task and
346 * put them into wake_q to be woken up later.
347 */
348 list_for_each_entry(waiter, &sem->wait_list, list) {
349 if (waiter->type == RWSEM_WAITING_FOR_WRITE)
350 break;
351
352 woken++;
353 }
354 list_cut_before(&wlist, &sem->wait_list, &waiter->list);
355
356 adjustment = woken * RWSEM_READER_BIAS - adjustment;
357 lockevent_cond_inc(rwsem_wake_reader, woken);
358 if (list_empty(&sem->wait_list)) {
359 /* hit end of list above */
360 adjustment -= RWSEM_FLAG_WAITERS;
361 }
362
Waiman Long4f23dbc2019-05-20 16:59:06 -0400363 /*
364 * When we've woken a reader, we no longer need to force writers
365 * to give up the lock and we can clear HANDOFF.
366 */
367 if (woken && (atomic_long_read(&sem->count) & RWSEM_FLAG_HANDOFF))
368 adjustment -= RWSEM_FLAG_HANDOFF;
369
Waiman Long5dec94d2019-05-20 16:59:03 -0400370 if (adjustment)
371 atomic_long_add(adjustment, &sem->count);
372
373 /* 2nd pass */
374 list_for_each_entry_safe(waiter, tmp, &wlist, list) {
375 struct task_struct *tsk;
376
377 tsk = waiter->task;
378 get_task_struct(tsk);
379
380 /*
381 * Ensure calling get_task_struct() before setting the reader
Waiman Long6cef7ff62019-05-20 16:59:04 -0400382 * waiter to nil such that rwsem_down_read_slowpath() cannot
Waiman Long5dec94d2019-05-20 16:59:03 -0400383 * race with do_exit() by always holding a reference count
384 * to the task to wakeup.
385 */
386 smp_store_release(&waiter->task, NULL);
387 /*
388 * Ensure issuing the wakeup (either by us or someone else)
389 * after setting the reader waiter to nil.
390 */
391 wake_q_add_safe(wake_q, tsk);
392 }
393}
394
395/*
396 * This function must be called with the sem->wait_lock held to prevent
397 * race conditions between checking the rwsem wait list and setting the
398 * sem->count accordingly.
Waiman Long4f23dbc2019-05-20 16:59:06 -0400399 *
400 * If wstate is WRITER_HANDOFF, it will make sure that either the handoff
401 * bit is set or the lock is acquired with handoff bit cleared.
Waiman Long5dec94d2019-05-20 16:59:03 -0400402 */
Waiman Long00f3c5a2019-05-20 16:59:07 -0400403static inline bool rwsem_try_write_lock(struct rw_semaphore *sem,
Waiman Long4f23dbc2019-05-20 16:59:06 -0400404 enum writer_wait_state wstate)
Waiman Long5dec94d2019-05-20 16:59:03 -0400405{
Waiman Long00f3c5a2019-05-20 16:59:07 -0400406 long count, new;
Waiman Long5dec94d2019-05-20 16:59:03 -0400407
Waiman Long4f23dbc2019-05-20 16:59:06 -0400408 lockdep_assert_held(&sem->wait_lock);
409
Waiman Long00f3c5a2019-05-20 16:59:07 -0400410 count = atomic_long_read(&sem->count);
Waiman Long4f23dbc2019-05-20 16:59:06 -0400411 do {
412 bool has_handoff = !!(count & RWSEM_FLAG_HANDOFF);
413
414 if (has_handoff && wstate == WRITER_NOT_FIRST)
415 return false;
416
417 new = count;
418
419 if (count & RWSEM_LOCK_MASK) {
420 if (has_handoff || (wstate != WRITER_HANDOFF))
421 return false;
422
423 new |= RWSEM_FLAG_HANDOFF;
424 } else {
425 new |= RWSEM_WRITER_LOCKED;
426 new &= ~RWSEM_FLAG_HANDOFF;
427
428 if (list_is_singular(&sem->wait_list))
429 new &= ~RWSEM_FLAG_WAITERS;
430 }
431 } while (!atomic_long_try_cmpxchg_acquire(&sem->count, &count, new));
432
433 /*
434 * We have either acquired the lock with handoff bit cleared or
435 * set the handoff bit.
436 */
437 if (new & RWSEM_FLAG_HANDOFF)
Waiman Long5dec94d2019-05-20 16:59:03 -0400438 return false;
439
Waiman Long4f23dbc2019-05-20 16:59:06 -0400440 rwsem_set_owner(sem);
441 return true;
Waiman Long5dec94d2019-05-20 16:59:03 -0400442}
443
444#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
445/*
446 * Try to acquire write lock before the writer has been put on wait queue.
447 */
448static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem)
449{
450 long count = atomic_long_read(&sem->count);
451
Waiman Long4f23dbc2019-05-20 16:59:06 -0400452 while (!(count & (RWSEM_LOCK_MASK|RWSEM_FLAG_HANDOFF))) {
Waiman Long5dec94d2019-05-20 16:59:03 -0400453 if (atomic_long_try_cmpxchg_acquire(&sem->count, &count,
Waiman Long4f23dbc2019-05-20 16:59:06 -0400454 count | RWSEM_WRITER_LOCKED)) {
Waiman Long5dec94d2019-05-20 16:59:03 -0400455 rwsem_set_owner(sem);
456 lockevent_inc(rwsem_opt_wlock);
457 return true;
458 }
459 }
460 return false;
461}
462
463static inline bool owner_on_cpu(struct task_struct *owner)
464{
465 /*
466 * As lock holder preemption issue, we both skip spinning if
467 * task is not on cpu or its cpu is preempted
468 */
469 return owner->on_cpu && !vcpu_is_preempted(task_cpu(owner));
470}
471
472static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem)
473{
474 struct task_struct *owner;
475 bool ret = true;
476
477 BUILD_BUG_ON(!rwsem_has_anonymous_owner(RWSEM_OWNER_UNKNOWN));
478
479 if (need_resched())
480 return false;
481
482 rcu_read_lock();
483 owner = READ_ONCE(sem->owner);
484 if (owner) {
485 ret = is_rwsem_owner_spinnable(owner) &&
486 owner_on_cpu(owner);
487 }
488 rcu_read_unlock();
489 return ret;
490}
491
492/*
Waiman Long3f6d5172019-05-20 16:59:05 -0400493 * The rwsem_spin_on_owner() function returns the folowing 4 values
494 * depending on the lock owner state.
495 * OWNER_NULL : owner is currently NULL
496 * OWNER_WRITER: when owner changes and is a writer
497 * OWNER_READER: when owner changes and the new owner may be a reader.
498 * OWNER_NONSPINNABLE:
499 * when optimistic spinning has to stop because either the
500 * owner stops running, is unknown, or its timeslice has
501 * been used up.
Waiman Long5dec94d2019-05-20 16:59:03 -0400502 */
Waiman Long3f6d5172019-05-20 16:59:05 -0400503enum owner_state {
504 OWNER_NULL = 1 << 0,
505 OWNER_WRITER = 1 << 1,
506 OWNER_READER = 1 << 2,
507 OWNER_NONSPINNABLE = 1 << 3,
508};
509#define OWNER_SPINNABLE (OWNER_NULL | OWNER_WRITER)
Waiman Long5dec94d2019-05-20 16:59:03 -0400510
Waiman Long3f6d5172019-05-20 16:59:05 -0400511static inline enum owner_state rwsem_owner_state(unsigned long owner)
512{
513 if (!owner)
514 return OWNER_NULL;
515
516 if (owner & RWSEM_ANONYMOUSLY_OWNED)
517 return OWNER_NONSPINNABLE;
518
519 if (owner & RWSEM_READER_OWNED)
520 return OWNER_READER;
521
522 return OWNER_WRITER;
523}
524
525static noinline enum owner_state rwsem_spin_on_owner(struct rw_semaphore *sem)
526{
527 struct task_struct *tmp, *owner = READ_ONCE(sem->owner);
528 enum owner_state state = rwsem_owner_state((unsigned long)owner);
529
530 if (state != OWNER_WRITER)
531 return state;
Waiman Long5dec94d2019-05-20 16:59:03 -0400532
533 rcu_read_lock();
Waiman Long3f6d5172019-05-20 16:59:05 -0400534 for (;;) {
Waiman Long4f23dbc2019-05-20 16:59:06 -0400535 if (atomic_long_read(&sem->count) & RWSEM_FLAG_HANDOFF) {
536 state = OWNER_NONSPINNABLE;
537 break;
538 }
539
Waiman Long3f6d5172019-05-20 16:59:05 -0400540 tmp = READ_ONCE(sem->owner);
541 if (tmp != owner) {
542 state = rwsem_owner_state((unsigned long)tmp);
543 break;
544 }
545
Waiman Long5dec94d2019-05-20 16:59:03 -0400546 /*
547 * Ensure we emit the owner->on_cpu, dereference _after_
548 * checking sem->owner still matches owner, if that fails,
549 * owner might point to free()d memory, if it still matches,
550 * the rcu_read_lock() ensures the memory stays valid.
551 */
552 barrier();
553
Waiman Long5dec94d2019-05-20 16:59:03 -0400554 if (need_resched() || !owner_on_cpu(owner)) {
Waiman Long3f6d5172019-05-20 16:59:05 -0400555 state = OWNER_NONSPINNABLE;
556 break;
Waiman Long5dec94d2019-05-20 16:59:03 -0400557 }
558
559 cpu_relax();
560 }
561 rcu_read_unlock();
562
Waiman Long3f6d5172019-05-20 16:59:05 -0400563 return state;
Waiman Long5dec94d2019-05-20 16:59:03 -0400564}
565
566static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
567{
568 bool taken = false;
569
570 preempt_disable();
571
572 /* sem->wait_lock should not be held when doing optimistic spinning */
573 if (!rwsem_can_spin_on_owner(sem))
574 goto done;
575
576 if (!osq_lock(&sem->osq))
577 goto done;
578
579 /*
580 * Optimistically spin on the owner field and attempt to acquire the
581 * lock whenever the owner changes. Spinning will be stopped when:
582 * 1) the owning writer isn't running; or
583 * 2) readers own the lock as we can't determine if they are
584 * actively running or not.
585 */
Waiman Long3f6d5172019-05-20 16:59:05 -0400586 while (rwsem_spin_on_owner(sem) & OWNER_SPINNABLE) {
Waiman Long5dec94d2019-05-20 16:59:03 -0400587 /*
588 * Try to acquire the lock
589 */
590 if (rwsem_try_write_lock_unqueued(sem)) {
591 taken = true;
592 break;
593 }
594
595 /*
596 * When there's no owner, we might have preempted between the
597 * owner acquiring the lock and setting the owner field. If
598 * we're an RT task that will live-lock because we won't let
599 * the owner complete.
600 */
601 if (!sem->owner && (need_resched() || rt_task(current)))
602 break;
603
604 /*
605 * The cpu_relax() call is a compiler barrier which forces
606 * everything in this loop to be re-loaded. We don't need
607 * memory barriers as we'll eventually observe the right
608 * values at the cost of a few extra spins.
609 */
610 cpu_relax();
611 }
612 osq_unlock(&sem->osq);
613done:
614 preempt_enable();
615 lockevent_cond_inc(rwsem_opt_fail, !taken);
616 return taken;
617}
618#else
619static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
620{
621 return false;
622}
623#endif
624
625/*
626 * Wait for the read lock to be granted
627 */
Waiman Long6cef7ff62019-05-20 16:59:04 -0400628static struct rw_semaphore __sched *
629rwsem_down_read_slowpath(struct rw_semaphore *sem, int state)
Waiman Long5dec94d2019-05-20 16:59:03 -0400630{
631 long count, adjustment = -RWSEM_READER_BIAS;
632 struct rwsem_waiter waiter;
633 DEFINE_WAKE_Q(wake_q);
634
635 waiter.task = current;
636 waiter.type = RWSEM_WAITING_FOR_READ;
Waiman Long4f23dbc2019-05-20 16:59:06 -0400637 waiter.timeout = jiffies + RWSEM_WAIT_TIMEOUT;
Waiman Long5dec94d2019-05-20 16:59:03 -0400638
639 raw_spin_lock_irq(&sem->wait_lock);
640 if (list_empty(&sem->wait_list)) {
641 /*
642 * In case the wait queue is empty and the lock isn't owned
Waiman Long4f23dbc2019-05-20 16:59:06 -0400643 * by a writer or has the handoff bit set, this reader can
644 * exit the slowpath and return immediately as its
645 * RWSEM_READER_BIAS has already been set in the count.
Waiman Long5dec94d2019-05-20 16:59:03 -0400646 */
Waiman Long4f23dbc2019-05-20 16:59:06 -0400647 if (!(atomic_long_read(&sem->count) &
648 (RWSEM_WRITER_MASK | RWSEM_FLAG_HANDOFF))) {
Waiman Long5dec94d2019-05-20 16:59:03 -0400649 raw_spin_unlock_irq(&sem->wait_lock);
650 rwsem_set_reader_owned(sem);
651 lockevent_inc(rwsem_rlock_fast);
652 return sem;
653 }
654 adjustment += RWSEM_FLAG_WAITERS;
655 }
656 list_add_tail(&waiter.list, &sem->wait_list);
657
658 /* we're now waiting on the lock, but no longer actively locking */
659 count = atomic_long_add_return(adjustment, &sem->count);
660
661 /*
662 * If there are no active locks, wake the front queued process(es).
663 *
664 * If there are no writers and we are first in the queue,
665 * wake our own waiter to join the existing active readers !
666 */
667 if (!(count & RWSEM_LOCK_MASK) ||
668 (!(count & RWSEM_WRITER_MASK) && (adjustment & RWSEM_FLAG_WAITERS)))
Waiman Long6cef7ff62019-05-20 16:59:04 -0400669 rwsem_mark_wake(sem, RWSEM_WAKE_ANY, &wake_q);
Waiman Long5dec94d2019-05-20 16:59:03 -0400670
671 raw_spin_unlock_irq(&sem->wait_lock);
672 wake_up_q(&wake_q);
673
674 /* wait to be given the lock */
675 while (true) {
676 set_current_state(state);
677 if (!waiter.task)
678 break;
679 if (signal_pending_state(state, current)) {
680 raw_spin_lock_irq(&sem->wait_lock);
681 if (waiter.task)
682 goto out_nolock;
683 raw_spin_unlock_irq(&sem->wait_lock);
684 break;
685 }
686 schedule();
687 lockevent_inc(rwsem_sleep_reader);
688 }
689
690 __set_current_state(TASK_RUNNING);
691 lockevent_inc(rwsem_rlock);
692 return sem;
693out_nolock:
694 list_del(&waiter.list);
Waiman Long4f23dbc2019-05-20 16:59:06 -0400695 if (list_empty(&sem->wait_list)) {
696 atomic_long_andnot(RWSEM_FLAG_WAITERS|RWSEM_FLAG_HANDOFF,
697 &sem->count);
698 }
Waiman Long5dec94d2019-05-20 16:59:03 -0400699 raw_spin_unlock_irq(&sem->wait_lock);
700 __set_current_state(TASK_RUNNING);
701 lockevent_inc(rwsem_rlock_fail);
702 return ERR_PTR(-EINTR);
703}
704
Waiman Long5dec94d2019-05-20 16:59:03 -0400705/*
706 * Wait until we successfully acquire the write lock
707 */
Waiman Long6cef7ff62019-05-20 16:59:04 -0400708static struct rw_semaphore *
709rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
Waiman Long5dec94d2019-05-20 16:59:03 -0400710{
711 long count;
Waiman Long4f23dbc2019-05-20 16:59:06 -0400712 enum writer_wait_state wstate;
Waiman Long5dec94d2019-05-20 16:59:03 -0400713 struct rwsem_waiter waiter;
714 struct rw_semaphore *ret = sem;
715 DEFINE_WAKE_Q(wake_q);
716
717 /* do optimistic spinning and steal lock if possible */
718 if (rwsem_optimistic_spin(sem))
719 return sem;
720
721 /*
722 * Optimistic spinning failed, proceed to the slowpath
723 * and block until we can acquire the sem.
724 */
725 waiter.task = current;
726 waiter.type = RWSEM_WAITING_FOR_WRITE;
Waiman Long4f23dbc2019-05-20 16:59:06 -0400727 waiter.timeout = jiffies + RWSEM_WAIT_TIMEOUT;
Waiman Long5dec94d2019-05-20 16:59:03 -0400728
729 raw_spin_lock_irq(&sem->wait_lock);
730
731 /* account for this before adding a new element to the list */
Waiman Long4f23dbc2019-05-20 16:59:06 -0400732 wstate = list_empty(&sem->wait_list) ? WRITER_FIRST : WRITER_NOT_FIRST;
Waiman Long5dec94d2019-05-20 16:59:03 -0400733
734 list_add_tail(&waiter.list, &sem->wait_list);
735
736 /* we're now waiting on the lock */
Waiman Long4f23dbc2019-05-20 16:59:06 -0400737 if (wstate == WRITER_NOT_FIRST) {
Waiman Long5dec94d2019-05-20 16:59:03 -0400738 count = atomic_long_read(&sem->count);
739
740 /*
Waiman Long4f23dbc2019-05-20 16:59:06 -0400741 * If there were already threads queued before us and:
742 * 1) there are no no active locks, wake the front
743 * queued process(es) as the handoff bit might be set.
744 * 2) there are no active writers and some readers, the lock
745 * must be read owned; so we try to wake any read lock
746 * waiters that were queued ahead of us.
Waiman Long5dec94d2019-05-20 16:59:03 -0400747 */
Waiman Long4f23dbc2019-05-20 16:59:06 -0400748 if (count & RWSEM_WRITER_MASK)
749 goto wait;
Waiman Long5dec94d2019-05-20 16:59:03 -0400750
Waiman Long4f23dbc2019-05-20 16:59:06 -0400751 rwsem_mark_wake(sem, (count & RWSEM_READER_MASK)
752 ? RWSEM_WAKE_READERS
753 : RWSEM_WAKE_ANY, &wake_q);
Waiman Long5dec94d2019-05-20 16:59:03 -0400754
Waiman Long00f3c5a2019-05-20 16:59:07 -0400755 if (!wake_q_empty(&wake_q)) {
756 /*
757 * We want to minimize wait_lock hold time especially
758 * when a large number of readers are to be woken up.
759 */
760 raw_spin_unlock_irq(&sem->wait_lock);
761 wake_up_q(&wake_q);
762 wake_q_init(&wake_q); /* Used again, reinit */
763 raw_spin_lock_irq(&sem->wait_lock);
764 }
Waiman Long5dec94d2019-05-20 16:59:03 -0400765 } else {
Waiman Long00f3c5a2019-05-20 16:59:07 -0400766 atomic_long_or(RWSEM_FLAG_WAITERS, &sem->count);
Waiman Long5dec94d2019-05-20 16:59:03 -0400767 }
768
Waiman Long4f23dbc2019-05-20 16:59:06 -0400769wait:
Waiman Long5dec94d2019-05-20 16:59:03 -0400770 /* wait until we successfully acquire the lock */
771 set_current_state(state);
772 while (true) {
Waiman Long00f3c5a2019-05-20 16:59:07 -0400773 if (rwsem_try_write_lock(sem, wstate))
Waiman Long5dec94d2019-05-20 16:59:03 -0400774 break;
Waiman Long4f23dbc2019-05-20 16:59:06 -0400775
Waiman Long5dec94d2019-05-20 16:59:03 -0400776 raw_spin_unlock_irq(&sem->wait_lock);
777
778 /* Block until there are no active lockers. */
Waiman Long4f23dbc2019-05-20 16:59:06 -0400779 for (;;) {
Waiman Long5dec94d2019-05-20 16:59:03 -0400780 if (signal_pending_state(state, current))
781 goto out_nolock;
782
783 schedule();
784 lockevent_inc(rwsem_sleep_writer);
785 set_current_state(state);
Waiman Long4f23dbc2019-05-20 16:59:06 -0400786 /*
787 * If HANDOFF bit is set, unconditionally do
788 * a trylock.
789 */
790 if (wstate == WRITER_HANDOFF)
791 break;
792
793 if ((wstate == WRITER_NOT_FIRST) &&
794 (rwsem_first_waiter(sem) == &waiter))
795 wstate = WRITER_FIRST;
796
Waiman Long5dec94d2019-05-20 16:59:03 -0400797 count = atomic_long_read(&sem->count);
Waiman Long4f23dbc2019-05-20 16:59:06 -0400798 if (!(count & RWSEM_LOCK_MASK))
799 break;
800
801 /*
802 * The setting of the handoff bit is deferred
803 * until rwsem_try_write_lock() is called.
804 */
805 if ((wstate == WRITER_FIRST) && (rt_task(current) ||
806 time_after(jiffies, waiter.timeout))) {
807 wstate = WRITER_HANDOFF;
808 lockevent_inc(rwsem_wlock_handoff);
809 break;
810 }
811 }
Waiman Long5dec94d2019-05-20 16:59:03 -0400812
813 raw_spin_lock_irq(&sem->wait_lock);
814 }
815 __set_current_state(TASK_RUNNING);
816 list_del(&waiter.list);
817 raw_spin_unlock_irq(&sem->wait_lock);
818 lockevent_inc(rwsem_wlock);
819
820 return ret;
821
822out_nolock:
823 __set_current_state(TASK_RUNNING);
824 raw_spin_lock_irq(&sem->wait_lock);
825 list_del(&waiter.list);
Waiman Long4f23dbc2019-05-20 16:59:06 -0400826
827 if (unlikely(wstate == WRITER_HANDOFF))
828 atomic_long_add(-RWSEM_FLAG_HANDOFF, &sem->count);
829
Waiman Long5dec94d2019-05-20 16:59:03 -0400830 if (list_empty(&sem->wait_list))
831 atomic_long_andnot(RWSEM_FLAG_WAITERS, &sem->count);
832 else
Waiman Long6cef7ff62019-05-20 16:59:04 -0400833 rwsem_mark_wake(sem, RWSEM_WAKE_ANY, &wake_q);
Waiman Long5dec94d2019-05-20 16:59:03 -0400834 raw_spin_unlock_irq(&sem->wait_lock);
835 wake_up_q(&wake_q);
836 lockevent_inc(rwsem_wlock_fail);
837
838 return ERR_PTR(-EINTR);
839}
840
Waiman Long5dec94d2019-05-20 16:59:03 -0400841/*
842 * handle waking up a waiter on the semaphore
843 * - up_read/up_write has decremented the active part of count if we come here
844 */
Waiman Long4f23dbc2019-05-20 16:59:06 -0400845static struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem, long count)
Waiman Long5dec94d2019-05-20 16:59:03 -0400846{
847 unsigned long flags;
848 DEFINE_WAKE_Q(wake_q);
849
850 raw_spin_lock_irqsave(&sem->wait_lock, flags);
851
852 if (!list_empty(&sem->wait_list))
Waiman Long6cef7ff62019-05-20 16:59:04 -0400853 rwsem_mark_wake(sem, RWSEM_WAKE_ANY, &wake_q);
Waiman Long5dec94d2019-05-20 16:59:03 -0400854
855 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
856 wake_up_q(&wake_q);
857
858 return sem;
859}
Waiman Long5dec94d2019-05-20 16:59:03 -0400860
861/*
862 * downgrade a write lock into a read lock
863 * - caller incremented waiting part of count and discovered it still negative
864 * - just wake up any readers at the front of the queue
865 */
Waiman Long6cef7ff62019-05-20 16:59:04 -0400866static struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem)
Waiman Long5dec94d2019-05-20 16:59:03 -0400867{
868 unsigned long flags;
869 DEFINE_WAKE_Q(wake_q);
870
871 raw_spin_lock_irqsave(&sem->wait_lock, flags);
872
873 if (!list_empty(&sem->wait_list))
Waiman Long6cef7ff62019-05-20 16:59:04 -0400874 rwsem_mark_wake(sem, RWSEM_WAKE_READ_OWNED, &wake_q);
Waiman Long5dec94d2019-05-20 16:59:03 -0400875
876 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
877 wake_up_q(&wake_q);
878
879 return sem;
880}
Waiman Long5dec94d2019-05-20 16:59:03 -0400881
882/*
883 * lock for reading
884 */
885inline void __down_read(struct rw_semaphore *sem)
886{
887 if (unlikely(atomic_long_fetch_add_acquire(RWSEM_READER_BIAS,
888 &sem->count) & RWSEM_READ_FAILED_MASK)) {
Waiman Long6cef7ff62019-05-20 16:59:04 -0400889 rwsem_down_read_slowpath(sem, TASK_UNINTERRUPTIBLE);
Waiman Long5dec94d2019-05-20 16:59:03 -0400890 DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner &
891 RWSEM_READER_OWNED), sem);
892 } else {
893 rwsem_set_reader_owned(sem);
894 }
895}
896
897static inline int __down_read_killable(struct rw_semaphore *sem)
898{
899 if (unlikely(atomic_long_fetch_add_acquire(RWSEM_READER_BIAS,
900 &sem->count) & RWSEM_READ_FAILED_MASK)) {
Waiman Long6cef7ff62019-05-20 16:59:04 -0400901 if (IS_ERR(rwsem_down_read_slowpath(sem, TASK_KILLABLE)))
Waiman Long5dec94d2019-05-20 16:59:03 -0400902 return -EINTR;
903 DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner &
904 RWSEM_READER_OWNED), sem);
905 } else {
906 rwsem_set_reader_owned(sem);
907 }
908 return 0;
909}
910
911static inline int __down_read_trylock(struct rw_semaphore *sem)
912{
913 /*
914 * Optimize for the case when the rwsem is not locked at all.
915 */
916 long tmp = RWSEM_UNLOCKED_VALUE;
917
Waiman Long5dec94d2019-05-20 16:59:03 -0400918 do {
919 if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
920 tmp + RWSEM_READER_BIAS)) {
921 rwsem_set_reader_owned(sem);
922 return 1;
923 }
924 } while (!(tmp & RWSEM_READ_FAILED_MASK));
925 return 0;
926}
927
928/*
929 * lock for writing
930 */
931static inline void __down_write(struct rw_semaphore *sem)
932{
Waiman Long6cef7ff62019-05-20 16:59:04 -0400933 long tmp = RWSEM_UNLOCKED_VALUE;
934
935 if (unlikely(!atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
936 RWSEM_WRITER_LOCKED)))
937 rwsem_down_write_slowpath(sem, TASK_UNINTERRUPTIBLE);
Waiman Long5dec94d2019-05-20 16:59:03 -0400938 rwsem_set_owner(sem);
939}
940
941static inline int __down_write_killable(struct rw_semaphore *sem)
942{
Waiman Long6cef7ff62019-05-20 16:59:04 -0400943 long tmp = RWSEM_UNLOCKED_VALUE;
944
945 if (unlikely(!atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
946 RWSEM_WRITER_LOCKED))) {
947 if (IS_ERR(rwsem_down_write_slowpath(sem, TASK_KILLABLE)))
Waiman Long5dec94d2019-05-20 16:59:03 -0400948 return -EINTR;
Waiman Long6cef7ff62019-05-20 16:59:04 -0400949 }
Waiman Long5dec94d2019-05-20 16:59:03 -0400950 rwsem_set_owner(sem);
951 return 0;
952}
953
954static inline int __down_write_trylock(struct rw_semaphore *sem)
955{
Waiman Long6cef7ff62019-05-20 16:59:04 -0400956 long tmp = RWSEM_UNLOCKED_VALUE;
Waiman Long5dec94d2019-05-20 16:59:03 -0400957
Waiman Long6cef7ff62019-05-20 16:59:04 -0400958 if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
959 RWSEM_WRITER_LOCKED)) {
Waiman Long5dec94d2019-05-20 16:59:03 -0400960 rwsem_set_owner(sem);
961 return true;
962 }
963 return false;
964}
965
966/*
967 * unlock after reading
968 */
969inline void __up_read(struct rw_semaphore *sem)
970{
971 long tmp;
972
Waiman Long6cef7ff62019-05-20 16:59:04 -0400973 DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & RWSEM_READER_OWNED), sem);
Waiman Long5dec94d2019-05-20 16:59:03 -0400974 rwsem_clear_reader_owned(sem);
975 tmp = atomic_long_add_return_release(-RWSEM_READER_BIAS, &sem->count);
Waiman Long6cef7ff62019-05-20 16:59:04 -0400976 if (unlikely((tmp & (RWSEM_LOCK_MASK|RWSEM_FLAG_WAITERS)) ==
977 RWSEM_FLAG_WAITERS))
Waiman Long4f23dbc2019-05-20 16:59:06 -0400978 rwsem_wake(sem, tmp);
Waiman Long5dec94d2019-05-20 16:59:03 -0400979}
980
981/*
982 * unlock after writing
983 */
984static inline void __up_write(struct rw_semaphore *sem)
985{
Waiman Long6cef7ff62019-05-20 16:59:04 -0400986 long tmp;
987
Waiman Long5dec94d2019-05-20 16:59:03 -0400988 DEBUG_RWSEMS_WARN_ON(sem->owner != current, sem);
989 rwsem_clear_owner(sem);
Waiman Long6cef7ff62019-05-20 16:59:04 -0400990 tmp = atomic_long_fetch_add_release(-RWSEM_WRITER_LOCKED, &sem->count);
991 if (unlikely(tmp & RWSEM_FLAG_WAITERS))
Waiman Long4f23dbc2019-05-20 16:59:06 -0400992 rwsem_wake(sem, tmp);
Waiman Long5dec94d2019-05-20 16:59:03 -0400993}
994
995/*
996 * downgrade write lock to read lock
997 */
998static inline void __downgrade_write(struct rw_semaphore *sem)
999{
1000 long tmp;
1001
1002 /*
1003 * When downgrading from exclusive to shared ownership,
1004 * anything inside the write-locked region cannot leak
1005 * into the read side. In contrast, anything in the
1006 * read-locked region is ok to be re-ordered into the
1007 * write side. As such, rely on RELEASE semantics.
1008 */
1009 DEBUG_RWSEMS_WARN_ON(sem->owner != current, sem);
1010 tmp = atomic_long_fetch_add_release(
1011 -RWSEM_WRITER_LOCKED+RWSEM_READER_BIAS, &sem->count);
1012 rwsem_set_reader_owned(sem);
1013 if (tmp & RWSEM_FLAG_WAITERS)
1014 rwsem_downgrade_wake(sem);
1015}
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -07001016
Ingo Molnarc4e05112006-07-03 00:24:29 -07001017/*
1018 * lock for reading
1019 */
Livio Soaresc7af77b2007-12-18 15:21:13 +01001020void __sched down_read(struct rw_semaphore *sem)
Ingo Molnarc4e05112006-07-03 00:24:29 -07001021{
1022 might_sleep();
1023 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
1024
Peter Zijlstra4fe87742007-07-19 01:48:58 -07001025 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
Ingo Molnarc4e05112006-07-03 00:24:29 -07001026}
Ingo Molnarc4e05112006-07-03 00:24:29 -07001027EXPORT_SYMBOL(down_read);
1028
Kirill Tkhai76f85072017-09-29 19:06:38 +03001029int __sched down_read_killable(struct rw_semaphore *sem)
1030{
1031 might_sleep();
1032 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
1033
1034 if (LOCK_CONTENDED_RETURN(sem, __down_read_trylock, __down_read_killable)) {
1035 rwsem_release(&sem->dep_map, 1, _RET_IP_);
1036 return -EINTR;
1037 }
1038
Kirill Tkhai76f85072017-09-29 19:06:38 +03001039 return 0;
1040}
Kirill Tkhai76f85072017-09-29 19:06:38 +03001041EXPORT_SYMBOL(down_read_killable);
1042
Ingo Molnarc4e05112006-07-03 00:24:29 -07001043/*
1044 * trylock for reading -- returns 1 if successful, 0 if contention
1045 */
1046int down_read_trylock(struct rw_semaphore *sem)
1047{
1048 int ret = __down_read_trylock(sem);
1049
Waiman Longc7580c12019-04-04 13:43:11 -04001050 if (ret == 1)
Ingo Molnarc4e05112006-07-03 00:24:29 -07001051 rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
1052 return ret;
1053}
Ingo Molnarc4e05112006-07-03 00:24:29 -07001054EXPORT_SYMBOL(down_read_trylock);
1055
1056/*
1057 * lock for writing
1058 */
Livio Soaresc7af77b2007-12-18 15:21:13 +01001059void __sched down_write(struct rw_semaphore *sem)
Ingo Molnarc4e05112006-07-03 00:24:29 -07001060{
1061 might_sleep();
1062 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
Peter Zijlstra4fe87742007-07-19 01:48:58 -07001063 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
Ingo Molnarc4e05112006-07-03 00:24:29 -07001064}
Ingo Molnarc4e05112006-07-03 00:24:29 -07001065EXPORT_SYMBOL(down_write);
1066
1067/*
Michal Hocko916633a2016-04-07 17:12:31 +02001068 * lock for writing
1069 */
1070int __sched down_write_killable(struct rw_semaphore *sem)
1071{
1072 might_sleep();
1073 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
1074
Waiman Long6cef7ff62019-05-20 16:59:04 -04001075 if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock,
1076 __down_write_killable)) {
Michal Hocko916633a2016-04-07 17:12:31 +02001077 rwsem_release(&sem->dep_map, 1, _RET_IP_);
1078 return -EINTR;
1079 }
1080
Michal Hocko916633a2016-04-07 17:12:31 +02001081 return 0;
1082}
Michal Hocko916633a2016-04-07 17:12:31 +02001083EXPORT_SYMBOL(down_write_killable);
1084
1085/*
Ingo Molnarc4e05112006-07-03 00:24:29 -07001086 * trylock for writing -- returns 1 if successful, 0 if contention
1087 */
1088int down_write_trylock(struct rw_semaphore *sem)
1089{
1090 int ret = __down_write_trylock(sem);
1091
Waiman Longc7580c12019-04-04 13:43:11 -04001092 if (ret == 1)
Pavel Emelianov428e6ce2007-05-08 00:29:10 -07001093 rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -07001094
Ingo Molnarc4e05112006-07-03 00:24:29 -07001095 return ret;
1096}
Ingo Molnarc4e05112006-07-03 00:24:29 -07001097EXPORT_SYMBOL(down_write_trylock);
1098
1099/*
1100 * release a read lock
1101 */
1102void up_read(struct rw_semaphore *sem)
1103{
1104 rwsem_release(&sem->dep_map, 1, _RET_IP_);
Ingo Molnarc4e05112006-07-03 00:24:29 -07001105 __up_read(sem);
1106}
Ingo Molnarc4e05112006-07-03 00:24:29 -07001107EXPORT_SYMBOL(up_read);
1108
1109/*
1110 * release a write lock
1111 */
1112void up_write(struct rw_semaphore *sem)
1113{
1114 rwsem_release(&sem->dep_map, 1, _RET_IP_);
Ingo Molnarc4e05112006-07-03 00:24:29 -07001115 __up_write(sem);
1116}
Ingo Molnarc4e05112006-07-03 00:24:29 -07001117EXPORT_SYMBOL(up_write);
1118
1119/*
1120 * downgrade write lock to read lock
1121 */
1122void downgrade_write(struct rw_semaphore *sem)
1123{
J. R. Okajima6419c4a2017-02-03 01:38:17 +09001124 lock_downgrade(&sem->dep_map, _RET_IP_);
Ingo Molnarc4e05112006-07-03 00:24:29 -07001125 __downgrade_write(sem);
1126}
Ingo Molnarc4e05112006-07-03 00:24:29 -07001127EXPORT_SYMBOL(downgrade_write);
Ingo Molnar4ea21762006-07-03 00:24:53 -07001128
1129#ifdef CONFIG_DEBUG_LOCK_ALLOC
1130
1131void down_read_nested(struct rw_semaphore *sem, int subclass)
1132{
1133 might_sleep();
1134 rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
Peter Zijlstra4fe87742007-07-19 01:48:58 -07001135 LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
Ingo Molnar4ea21762006-07-03 00:24:53 -07001136}
Ingo Molnar4ea21762006-07-03 00:24:53 -07001137EXPORT_SYMBOL(down_read_nested);
1138
Jiri Kosina1b963c82013-01-11 14:31:56 -08001139void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
1140{
1141 might_sleep();
1142 rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);
Jiri Kosina1b963c82013-01-11 14:31:56 -08001143 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
1144}
Jiri Kosina1b963c82013-01-11 14:31:56 -08001145EXPORT_SYMBOL(_down_write_nest_lock);
1146
Kent Overstreet84759c62011-09-21 21:43:05 -07001147void down_read_non_owner(struct rw_semaphore *sem)
1148{
1149 might_sleep();
Kent Overstreet84759c62011-09-21 21:43:05 -07001150 __down_read(sem);
Waiman Long925b9cd2018-09-06 16:18:34 -04001151 __rwsem_set_reader_owned(sem, NULL);
Kent Overstreet84759c62011-09-21 21:43:05 -07001152}
Kent Overstreet84759c62011-09-21 21:43:05 -07001153EXPORT_SYMBOL(down_read_non_owner);
1154
Ingo Molnar4ea21762006-07-03 00:24:53 -07001155void down_write_nested(struct rw_semaphore *sem, int subclass)
1156{
1157 might_sleep();
1158 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
Peter Zijlstra4fe87742007-07-19 01:48:58 -07001159 LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
Ingo Molnar4ea21762006-07-03 00:24:53 -07001160}
Ingo Molnar4ea21762006-07-03 00:24:53 -07001161EXPORT_SYMBOL(down_write_nested);
1162
Al Viro887bddf2016-05-26 00:04:58 -04001163int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
1164{
1165 might_sleep();
1166 rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
1167
Waiman Long6cef7ff62019-05-20 16:59:04 -04001168 if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock,
1169 __down_write_killable)) {
Al Viro887bddf2016-05-26 00:04:58 -04001170 rwsem_release(&sem->dep_map, 1, _RET_IP_);
1171 return -EINTR;
1172 }
1173
Al Viro887bddf2016-05-26 00:04:58 -04001174 return 0;
1175}
Al Viro887bddf2016-05-26 00:04:58 -04001176EXPORT_SYMBOL(down_write_killable_nested);
1177
Kent Overstreet84759c62011-09-21 21:43:05 -07001178void up_read_non_owner(struct rw_semaphore *sem)
1179{
Waiman Long3b4ba662019-04-04 13:43:15 -04001180 DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & RWSEM_READER_OWNED),
1181 sem);
Kent Overstreet84759c62011-09-21 21:43:05 -07001182 __up_read(sem);
1183}
Kent Overstreet84759c62011-09-21 21:43:05 -07001184EXPORT_SYMBOL(up_read_non_owner);
1185
Ingo Molnar4ea21762006-07-03 00:24:53 -07001186#endif