Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 2 | /* kernel/rwsem.c: R/W semaphores, public implementation |
| 3 | * |
| 4 | * Written by David Howells (dhowells@redhat.com). |
| 5 | * Derived from asm-i386/semaphore.h |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 6 | * |
| 7 | * Writer lock-stealing by Alex Shi <alex.shi@intel.com> |
| 8 | * and Michel Lespinasse <walken@google.com> |
| 9 | * |
| 10 | * Optimistic spinning by Tim Chen <tim.c.chen@intel.com> |
| 11 | * and Davidlohr Bueso <davidlohr@hp.com>. Based on mutexes. |
| 12 | * |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 13 | * Rwsem count bit fields re-definition and rwsem rearchitecture by |
| 14 | * Waiman Long <longman@redhat.com> and |
| 15 | * Peter Zijlstra <peterz@infradead.org>. |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/kernel.h> |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 20 | #include <linux/sched.h> |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 21 | #include <linux/sched/rt.h> |
| 22 | #include <linux/sched/task.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 23 | #include <linux/sched/debug.h> |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 24 | #include <linux/sched/wake_q.h> |
| 25 | #include <linux/sched/signal.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 26 | #include <linux/export.h> |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 27 | #include <linux/rwsem.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 28 | #include <linux/atomic.h> |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 29 | |
Davidlohr Bueso | 7a215f8 | 2015-01-30 01:14:25 -0800 | [diff] [blame] | 30 | #include "rwsem.h" |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 31 | #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 Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 78 | * Bit 2 - lock handoff bit |
| 79 | * Bits 3-7 - reserved |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 80 | * 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 Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 84 | * |
| 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 93 | */ |
| 94 | #define RWSEM_WRITER_LOCKED (1UL << 0) |
| 95 | #define RWSEM_FLAG_WAITERS (1UL << 1) |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 96 | #define RWSEM_FLAG_HANDOFF (1UL << 2) |
| 97 | |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 98 | #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 Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 103 | #define RWSEM_READ_FAILED_MASK (RWSEM_WRITER_MASK|RWSEM_FLAG_WAITERS|\ |
| 104 | RWSEM_FLAG_HANDOFF) |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 105 | |
| 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 | */ |
| 113 | static inline void rwsem_set_owner(struct rw_semaphore *sem) |
| 114 | { |
| 115 | WRITE_ONCE(sem->owner, current); |
| 116 | } |
| 117 | |
| 118 | static 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 | */ |
| 131 | static 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 | |
| 140 | static 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 | */ |
| 150 | static 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 | */ |
| 158 | static 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 | */ |
| 170 | static 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 |
| 179 | static 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 | */ |
| 204 | void __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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 222 | EXPORT_SYMBOL(__init_rwsem); |
| 223 | |
| 224 | enum rwsem_waiter_type { |
| 225 | RWSEM_WAITING_FOR_WRITE, |
| 226 | RWSEM_WAITING_FOR_READ |
| 227 | }; |
| 228 | |
| 229 | struct rwsem_waiter { |
| 230 | struct list_head list; |
| 231 | struct task_struct *task; |
| 232 | enum rwsem_waiter_type type; |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 233 | unsigned long timeout; |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 234 | }; |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 235 | #define rwsem_first_waiter(sem) \ |
| 236 | list_first_entry(&sem->wait_list, struct rwsem_waiter, list) |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 237 | |
| 238 | enum 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 Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 244 | enum 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 257 | /* |
| 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 Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 269 | static void rwsem_mark_wake(struct rw_semaphore *sem, |
| 270 | enum rwsem_wake_type wake_type, |
| 271 | struct wake_q_head *wake_q) |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 272 | { |
| 273 | struct rwsem_waiter *waiter, *tmp; |
| 274 | long oldcount, woken = 0, adjustment = 0; |
| 275 | struct list_head wlist; |
| 276 | |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 277 | lockdep_assert_held(&sem->wait_lock); |
| 278 | |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 279 | /* |
| 280 | * Take a peek at the queue head waiter such that we can determine |
| 281 | * the wakeup(s) to perform. |
| 282 | */ |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 283 | waiter = rwsem_first_waiter(sem); |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 284 | |
| 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 Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 310 | /* |
| 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 322 | 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 Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 363 | /* |
| 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 370 | 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 Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 382 | * waiter to nil such that rwsem_down_read_slowpath() cannot |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 383 | * 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 Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 399 | * |
| 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 402 | */ |
Waiman Long | 00f3c5a | 2019-05-20 16:59:07 -0400 | [diff] [blame^] | 403 | static inline bool rwsem_try_write_lock(struct rw_semaphore *sem, |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 404 | enum writer_wait_state wstate) |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 405 | { |
Waiman Long | 00f3c5a | 2019-05-20 16:59:07 -0400 | [diff] [blame^] | 406 | long count, new; |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 407 | |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 408 | lockdep_assert_held(&sem->wait_lock); |
| 409 | |
Waiman Long | 00f3c5a | 2019-05-20 16:59:07 -0400 | [diff] [blame^] | 410 | count = atomic_long_read(&sem->count); |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 411 | 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 438 | return false; |
| 439 | |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 440 | rwsem_set_owner(sem); |
| 441 | return true; |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 442 | } |
| 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 | */ |
| 448 | static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem) |
| 449 | { |
| 450 | long count = atomic_long_read(&sem->count); |
| 451 | |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 452 | while (!(count & (RWSEM_LOCK_MASK|RWSEM_FLAG_HANDOFF))) { |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 453 | if (atomic_long_try_cmpxchg_acquire(&sem->count, &count, |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 454 | count | RWSEM_WRITER_LOCKED)) { |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 455 | rwsem_set_owner(sem); |
| 456 | lockevent_inc(rwsem_opt_wlock); |
| 457 | return true; |
| 458 | } |
| 459 | } |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | static 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 | |
| 472 | static 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 Long | 3f6d517 | 2019-05-20 16:59:05 -0400 | [diff] [blame] | 493 | * 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 502 | */ |
Waiman Long | 3f6d517 | 2019-05-20 16:59:05 -0400 | [diff] [blame] | 503 | enum 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 510 | |
Waiman Long | 3f6d517 | 2019-05-20 16:59:05 -0400 | [diff] [blame] | 511 | static 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 | |
| 525 | static 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 532 | |
| 533 | rcu_read_lock(); |
Waiman Long | 3f6d517 | 2019-05-20 16:59:05 -0400 | [diff] [blame] | 534 | for (;;) { |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 535 | if (atomic_long_read(&sem->count) & RWSEM_FLAG_HANDOFF) { |
| 536 | state = OWNER_NONSPINNABLE; |
| 537 | break; |
| 538 | } |
| 539 | |
Waiman Long | 3f6d517 | 2019-05-20 16:59:05 -0400 | [diff] [blame] | 540 | tmp = READ_ONCE(sem->owner); |
| 541 | if (tmp != owner) { |
| 542 | state = rwsem_owner_state((unsigned long)tmp); |
| 543 | break; |
| 544 | } |
| 545 | |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 546 | /* |
| 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 554 | if (need_resched() || !owner_on_cpu(owner)) { |
Waiman Long | 3f6d517 | 2019-05-20 16:59:05 -0400 | [diff] [blame] | 555 | state = OWNER_NONSPINNABLE; |
| 556 | break; |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | cpu_relax(); |
| 560 | } |
| 561 | rcu_read_unlock(); |
| 562 | |
Waiman Long | 3f6d517 | 2019-05-20 16:59:05 -0400 | [diff] [blame] | 563 | return state; |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static 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 Long | 3f6d517 | 2019-05-20 16:59:05 -0400 | [diff] [blame] | 586 | while (rwsem_spin_on_owner(sem) & OWNER_SPINNABLE) { |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 587 | /* |
| 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); |
| 613 | done: |
| 614 | preempt_enable(); |
| 615 | lockevent_cond_inc(rwsem_opt_fail, !taken); |
| 616 | return taken; |
| 617 | } |
| 618 | #else |
| 619 | static 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 Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 628 | static struct rw_semaphore __sched * |
| 629 | rwsem_down_read_slowpath(struct rw_semaphore *sem, int state) |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 630 | { |
| 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 Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 637 | waiter.timeout = jiffies + RWSEM_WAIT_TIMEOUT; |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 638 | |
| 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 Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 643 | * 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 646 | */ |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 647 | if (!(atomic_long_read(&sem->count) & |
| 648 | (RWSEM_WRITER_MASK | RWSEM_FLAG_HANDOFF))) { |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 649 | 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 Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 669 | rwsem_mark_wake(sem, RWSEM_WAKE_ANY, &wake_q); |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 670 | |
| 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; |
| 693 | out_nolock: |
| 694 | list_del(&waiter.list); |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 695 | if (list_empty(&sem->wait_list)) { |
| 696 | atomic_long_andnot(RWSEM_FLAG_WAITERS|RWSEM_FLAG_HANDOFF, |
| 697 | &sem->count); |
| 698 | } |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 699 | 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 705 | /* |
| 706 | * Wait until we successfully acquire the write lock |
| 707 | */ |
Waiman Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 708 | static struct rw_semaphore * |
| 709 | rwsem_down_write_slowpath(struct rw_semaphore *sem, int state) |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 710 | { |
| 711 | long count; |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 712 | enum writer_wait_state wstate; |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 713 | 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 Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 727 | waiter.timeout = jiffies + RWSEM_WAIT_TIMEOUT; |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 728 | |
| 729 | raw_spin_lock_irq(&sem->wait_lock); |
| 730 | |
| 731 | /* account for this before adding a new element to the list */ |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 732 | wstate = list_empty(&sem->wait_list) ? WRITER_FIRST : WRITER_NOT_FIRST; |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 733 | |
| 734 | list_add_tail(&waiter.list, &sem->wait_list); |
| 735 | |
| 736 | /* we're now waiting on the lock */ |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 737 | if (wstate == WRITER_NOT_FIRST) { |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 738 | count = atomic_long_read(&sem->count); |
| 739 | |
| 740 | /* |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 741 | * 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 747 | */ |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 748 | if (count & RWSEM_WRITER_MASK) |
| 749 | goto wait; |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 750 | |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 751 | rwsem_mark_wake(sem, (count & RWSEM_READER_MASK) |
| 752 | ? RWSEM_WAKE_READERS |
| 753 | : RWSEM_WAKE_ANY, &wake_q); |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 754 | |
Waiman Long | 00f3c5a | 2019-05-20 16:59:07 -0400 | [diff] [blame^] | 755 | 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 765 | } else { |
Waiman Long | 00f3c5a | 2019-05-20 16:59:07 -0400 | [diff] [blame^] | 766 | atomic_long_or(RWSEM_FLAG_WAITERS, &sem->count); |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 767 | } |
| 768 | |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 769 | wait: |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 770 | /* wait until we successfully acquire the lock */ |
| 771 | set_current_state(state); |
| 772 | while (true) { |
Waiman Long | 00f3c5a | 2019-05-20 16:59:07 -0400 | [diff] [blame^] | 773 | if (rwsem_try_write_lock(sem, wstate)) |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 774 | break; |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 775 | |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 776 | raw_spin_unlock_irq(&sem->wait_lock); |
| 777 | |
| 778 | /* Block until there are no active lockers. */ |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 779 | for (;;) { |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 780 | 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 Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 786 | /* |
| 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 797 | count = atomic_long_read(&sem->count); |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 798 | 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 812 | |
| 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 | |
| 822 | out_nolock: |
| 823 | __set_current_state(TASK_RUNNING); |
| 824 | raw_spin_lock_irq(&sem->wait_lock); |
| 825 | list_del(&waiter.list); |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 826 | |
| 827 | if (unlikely(wstate == WRITER_HANDOFF)) |
| 828 | atomic_long_add(-RWSEM_FLAG_HANDOFF, &sem->count); |
| 829 | |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 830 | if (list_empty(&sem->wait_list)) |
| 831 | atomic_long_andnot(RWSEM_FLAG_WAITERS, &sem->count); |
| 832 | else |
Waiman Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 833 | rwsem_mark_wake(sem, RWSEM_WAKE_ANY, &wake_q); |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 834 | 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 841 | /* |
| 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 Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 845 | static struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem, long count) |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 846 | { |
| 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 Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 853 | rwsem_mark_wake(sem, RWSEM_WAKE_ANY, &wake_q); |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 854 | |
| 855 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
| 856 | wake_up_q(&wake_q); |
| 857 | |
| 858 | return sem; |
| 859 | } |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 860 | |
| 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 Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 866 | static struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem) |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 867 | { |
| 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 Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 874 | rwsem_mark_wake(sem, RWSEM_WAKE_READ_OWNED, &wake_q); |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 875 | |
| 876 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
| 877 | wake_up_q(&wake_q); |
| 878 | |
| 879 | return sem; |
| 880 | } |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 881 | |
| 882 | /* |
| 883 | * lock for reading |
| 884 | */ |
| 885 | inline 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 Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 889 | rwsem_down_read_slowpath(sem, TASK_UNINTERRUPTIBLE); |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 890 | DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & |
| 891 | RWSEM_READER_OWNED), sem); |
| 892 | } else { |
| 893 | rwsem_set_reader_owned(sem); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | static 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 Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 901 | if (IS_ERR(rwsem_down_read_slowpath(sem, TASK_KILLABLE))) |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 902 | 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 | |
| 911 | static 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 918 | 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 | */ |
| 931 | static inline void __down_write(struct rw_semaphore *sem) |
| 932 | { |
Waiman Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 933 | 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 938 | rwsem_set_owner(sem); |
| 939 | } |
| 940 | |
| 941 | static inline int __down_write_killable(struct rw_semaphore *sem) |
| 942 | { |
Waiman Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 943 | 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 Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 948 | return -EINTR; |
Waiman Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 949 | } |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 950 | rwsem_set_owner(sem); |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | static inline int __down_write_trylock(struct rw_semaphore *sem) |
| 955 | { |
Waiman Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 956 | long tmp = RWSEM_UNLOCKED_VALUE; |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 957 | |
Waiman Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 958 | if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp, |
| 959 | RWSEM_WRITER_LOCKED)) { |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 960 | rwsem_set_owner(sem); |
| 961 | return true; |
| 962 | } |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | /* |
| 967 | * unlock after reading |
| 968 | */ |
| 969 | inline void __up_read(struct rw_semaphore *sem) |
| 970 | { |
| 971 | long tmp; |
| 972 | |
Waiman Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 973 | DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & RWSEM_READER_OWNED), sem); |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 974 | rwsem_clear_reader_owned(sem); |
| 975 | tmp = atomic_long_add_return_release(-RWSEM_READER_BIAS, &sem->count); |
Waiman Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 976 | if (unlikely((tmp & (RWSEM_LOCK_MASK|RWSEM_FLAG_WAITERS)) == |
| 977 | RWSEM_FLAG_WAITERS)) |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 978 | rwsem_wake(sem, tmp); |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /* |
| 982 | * unlock after writing |
| 983 | */ |
| 984 | static inline void __up_write(struct rw_semaphore *sem) |
| 985 | { |
Waiman Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 986 | long tmp; |
| 987 | |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 988 | DEBUG_RWSEMS_WARN_ON(sem->owner != current, sem); |
| 989 | rwsem_clear_owner(sem); |
Waiman Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 990 | tmp = atomic_long_fetch_add_release(-RWSEM_WRITER_LOCKED, &sem->count); |
| 991 | if (unlikely(tmp & RWSEM_FLAG_WAITERS)) |
Waiman Long | 4f23dbc | 2019-05-20 16:59:06 -0400 | [diff] [blame] | 992 | rwsem_wake(sem, tmp); |
Waiman Long | 5dec94d | 2019-05-20 16:59:03 -0400 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | /* |
| 996 | * downgrade write lock to read lock |
| 997 | */ |
| 998 | static 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 Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 1016 | |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1017 | /* |
| 1018 | * lock for reading |
| 1019 | */ |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 1020 | void __sched down_read(struct rw_semaphore *sem) |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1021 | { |
| 1022 | might_sleep(); |
| 1023 | rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_); |
| 1024 | |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 1025 | LOCK_CONTENDED(sem, __down_read_trylock, __down_read); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1026 | } |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1027 | EXPORT_SYMBOL(down_read); |
| 1028 | |
Kirill Tkhai | 76f8507 | 2017-09-29 19:06:38 +0300 | [diff] [blame] | 1029 | int __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 Tkhai | 76f8507 | 2017-09-29 19:06:38 +0300 | [diff] [blame] | 1039 | return 0; |
| 1040 | } |
Kirill Tkhai | 76f8507 | 2017-09-29 19:06:38 +0300 | [diff] [blame] | 1041 | EXPORT_SYMBOL(down_read_killable); |
| 1042 | |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1043 | /* |
| 1044 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 1045 | */ |
| 1046 | int down_read_trylock(struct rw_semaphore *sem) |
| 1047 | { |
| 1048 | int ret = __down_read_trylock(sem); |
| 1049 | |
Waiman Long | c7580c1 | 2019-04-04 13:43:11 -0400 | [diff] [blame] | 1050 | if (ret == 1) |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1051 | rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_); |
| 1052 | return ret; |
| 1053 | } |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1054 | EXPORT_SYMBOL(down_read_trylock); |
| 1055 | |
| 1056 | /* |
| 1057 | * lock for writing |
| 1058 | */ |
Livio Soares | c7af77b | 2007-12-18 15:21:13 +0100 | [diff] [blame] | 1059 | void __sched down_write(struct rw_semaphore *sem) |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1060 | { |
| 1061 | might_sleep(); |
| 1062 | rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_); |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 1063 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1064 | } |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1065 | EXPORT_SYMBOL(down_write); |
| 1066 | |
| 1067 | /* |
Michal Hocko | 916633a | 2016-04-07 17:12:31 +0200 | [diff] [blame] | 1068 | * lock for writing |
| 1069 | */ |
| 1070 | int __sched down_write_killable(struct rw_semaphore *sem) |
| 1071 | { |
| 1072 | might_sleep(); |
| 1073 | rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_); |
| 1074 | |
Waiman Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 1075 | if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, |
| 1076 | __down_write_killable)) { |
Michal Hocko | 916633a | 2016-04-07 17:12:31 +0200 | [diff] [blame] | 1077 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
| 1078 | return -EINTR; |
| 1079 | } |
| 1080 | |
Michal Hocko | 916633a | 2016-04-07 17:12:31 +0200 | [diff] [blame] | 1081 | return 0; |
| 1082 | } |
Michal Hocko | 916633a | 2016-04-07 17:12:31 +0200 | [diff] [blame] | 1083 | EXPORT_SYMBOL(down_write_killable); |
| 1084 | |
| 1085 | /* |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1086 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 1087 | */ |
| 1088 | int down_write_trylock(struct rw_semaphore *sem) |
| 1089 | { |
| 1090 | int ret = __down_write_trylock(sem); |
| 1091 | |
Waiman Long | c7580c1 | 2019-04-04 13:43:11 -0400 | [diff] [blame] | 1092 | if (ret == 1) |
Pavel Emelianov | 428e6ce | 2007-05-08 00:29:10 -0700 | [diff] [blame] | 1093 | rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_); |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 1094 | |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1095 | return ret; |
| 1096 | } |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1097 | EXPORT_SYMBOL(down_write_trylock); |
| 1098 | |
| 1099 | /* |
| 1100 | * release a read lock |
| 1101 | */ |
| 1102 | void up_read(struct rw_semaphore *sem) |
| 1103 | { |
| 1104 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1105 | __up_read(sem); |
| 1106 | } |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1107 | EXPORT_SYMBOL(up_read); |
| 1108 | |
| 1109 | /* |
| 1110 | * release a write lock |
| 1111 | */ |
| 1112 | void up_write(struct rw_semaphore *sem) |
| 1113 | { |
| 1114 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1115 | __up_write(sem); |
| 1116 | } |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1117 | EXPORT_SYMBOL(up_write); |
| 1118 | |
| 1119 | /* |
| 1120 | * downgrade write lock to read lock |
| 1121 | */ |
| 1122 | void downgrade_write(struct rw_semaphore *sem) |
| 1123 | { |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 1124 | lock_downgrade(&sem->dep_map, _RET_IP_); |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1125 | __downgrade_write(sem); |
| 1126 | } |
Ingo Molnar | c4e0511 | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 1127 | EXPORT_SYMBOL(downgrade_write); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 1128 | |
| 1129 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 1130 | |
| 1131 | void 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 Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 1135 | LOCK_CONTENDED(sem, __down_read_trylock, __down_read); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 1136 | } |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 1137 | EXPORT_SYMBOL(down_read_nested); |
| 1138 | |
Jiri Kosina | 1b963c8 | 2013-01-11 14:31:56 -0800 | [diff] [blame] | 1139 | void _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 Kosina | 1b963c8 | 2013-01-11 14:31:56 -0800 | [diff] [blame] | 1143 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
| 1144 | } |
Jiri Kosina | 1b963c8 | 2013-01-11 14:31:56 -0800 | [diff] [blame] | 1145 | EXPORT_SYMBOL(_down_write_nest_lock); |
| 1146 | |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 1147 | void down_read_non_owner(struct rw_semaphore *sem) |
| 1148 | { |
| 1149 | might_sleep(); |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 1150 | __down_read(sem); |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame] | 1151 | __rwsem_set_reader_owned(sem, NULL); |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 1152 | } |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 1153 | EXPORT_SYMBOL(down_read_non_owner); |
| 1154 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 1155 | void down_write_nested(struct rw_semaphore *sem, int subclass) |
| 1156 | { |
| 1157 | might_sleep(); |
| 1158 | rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_); |
Peter Zijlstra | 4fe8774 | 2007-07-19 01:48:58 -0700 | [diff] [blame] | 1159 | LOCK_CONTENDED(sem, __down_write_trylock, __down_write); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 1160 | } |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 1161 | EXPORT_SYMBOL(down_write_nested); |
| 1162 | |
Al Viro | 887bddf | 2016-05-26 00:04:58 -0400 | [diff] [blame] | 1163 | int __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 Long | 6cef7ff6 | 2019-05-20 16:59:04 -0400 | [diff] [blame] | 1168 | if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, |
| 1169 | __down_write_killable)) { |
Al Viro | 887bddf | 2016-05-26 00:04:58 -0400 | [diff] [blame] | 1170 | rwsem_release(&sem->dep_map, 1, _RET_IP_); |
| 1171 | return -EINTR; |
| 1172 | } |
| 1173 | |
Al Viro | 887bddf | 2016-05-26 00:04:58 -0400 | [diff] [blame] | 1174 | return 0; |
| 1175 | } |
Al Viro | 887bddf | 2016-05-26 00:04:58 -0400 | [diff] [blame] | 1176 | EXPORT_SYMBOL(down_write_killable_nested); |
| 1177 | |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 1178 | void up_read_non_owner(struct rw_semaphore *sem) |
| 1179 | { |
Waiman Long | 3b4ba66 | 2019-04-04 13:43:15 -0400 | [diff] [blame] | 1180 | DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & RWSEM_READER_OWNED), |
| 1181 | sem); |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 1182 | __up_read(sem); |
| 1183 | } |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 1184 | EXPORT_SYMBOL(up_read_non_owner); |
| 1185 | |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 1186 | #endif |