Merge tag 'media/v4.12-1' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6/btrfs-unstable.git] / kernel / locking / qrwlock.c
blobcc3ed0ccdfa28622971ada92668bd3b1e45b4ee0
1 /*
2 * Queued read/write locks
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.
16 * Authors: Waiman Long <waiman.long@hp.com>
18 #include <linux/smp.h>
19 #include <linux/bug.h>
20 #include <linux/cpumask.h>
21 #include <linux/percpu.h>
22 #include <linux/hardirq.h>
23 #include <asm/qrwlock.h>
26 * This internal data structure is used for optimizing access to some of
27 * the subfields within the atomic_t cnts.
29 struct __qrwlock {
30 union {
31 atomic_t cnts;
32 struct {
33 #ifdef __LITTLE_ENDIAN
34 u8 wmode; /* Writer mode */
35 u8 rcnts[3]; /* Reader counts */
36 #else
37 u8 rcnts[3]; /* Reader counts */
38 u8 wmode; /* Writer mode */
39 #endif
42 arch_spinlock_t lock;
45 /**
46 * rspin_until_writer_unlock - inc reader count & spin until writer is gone
47 * @lock : Pointer to queue rwlock structure
48 * @writer: Current queue rwlock writer status byte
50 * In interrupt context or at the head of the queue, the reader will just
51 * increment the reader count & wait until the writer releases the lock.
53 static __always_inline void
54 rspin_until_writer_unlock(struct qrwlock *lock, u32 cnts)
56 while ((cnts & _QW_WMASK) == _QW_LOCKED) {
57 cpu_relax();
58 cnts = atomic_read_acquire(&lock->cnts);
62 /**
63 * queued_read_lock_slowpath - acquire read lock of a queue rwlock
64 * @lock: Pointer to queue rwlock structure
65 * @cnts: Current qrwlock lock value
67 void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts)
70 * Readers come here when they cannot get the lock without waiting
72 if (unlikely(in_interrupt())) {
74 * Readers in interrupt context will get the lock immediately
75 * if the writer is just waiting (not holding the lock yet).
76 * The rspin_until_writer_unlock() function returns immediately
77 * in this case. Otherwise, they will spin (with ACQUIRE
78 * semantics) until the lock is available without waiting in
79 * the queue.
81 rspin_until_writer_unlock(lock, cnts);
82 return;
84 atomic_sub(_QR_BIAS, &lock->cnts);
87 * Put the reader into the wait queue
89 arch_spin_lock(&lock->wait_lock);
92 * The ACQUIRE semantics of the following spinning code ensure
93 * that accesses can't leak upwards out of our subsequent critical
94 * section in the case that the lock is currently held for write.
96 cnts = atomic_fetch_add_acquire(_QR_BIAS, &lock->cnts);
97 rspin_until_writer_unlock(lock, cnts);
100 * Signal the next one in queue to become queue head
102 arch_spin_unlock(&lock->wait_lock);
104 EXPORT_SYMBOL(queued_read_lock_slowpath);
107 * queued_write_lock_slowpath - acquire write lock of a queue rwlock
108 * @lock : Pointer to queue rwlock structure
110 void queued_write_lock_slowpath(struct qrwlock *lock)
112 u32 cnts;
114 /* Put the writer into the wait queue */
115 arch_spin_lock(&lock->wait_lock);
117 /* Try to acquire the lock directly if no reader is present */
118 if (!atomic_read(&lock->cnts) &&
119 (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0))
120 goto unlock;
123 * Set the waiting flag to notify readers that a writer is pending,
124 * or wait for a previous writer to go away.
126 for (;;) {
127 struct __qrwlock *l = (struct __qrwlock *)lock;
129 if (!READ_ONCE(l->wmode) &&
130 (cmpxchg_relaxed(&l->wmode, 0, _QW_WAITING) == 0))
131 break;
133 cpu_relax();
136 /* When no more readers, set the locked flag */
137 for (;;) {
138 cnts = atomic_read(&lock->cnts);
139 if ((cnts == _QW_WAITING) &&
140 (atomic_cmpxchg_acquire(&lock->cnts, _QW_WAITING,
141 _QW_LOCKED) == _QW_WAITING))
142 break;
144 cpu_relax();
146 unlock:
147 arch_spin_unlock(&lock->wait_lock);
149 EXPORT_SYMBOL(queued_write_lock_slowpath);