]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/locking/qrwlock.c
locking/atomic: Add atomic_cond_read_acquire()
[mirror_ubuntu-jammy-kernel.git] / kernel / locking / qrwlock.c
CommitLineData
70af2f8a 1/*
c7114b4e 2 * Queued read/write locks
70af2f8a
WL
3 *
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.
8 *
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.
13 *
14 * (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.
15 *
16 * Authors: Waiman Long <waiman.long@hp.com>
17 */
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>
9ab6055f 23#include <linux/spinlock.h>
70af2f8a
WL
24#include <asm/qrwlock.h>
25
26/**
27 * rspin_until_writer_unlock - inc reader count & spin until writer is gone
28 * @lock : Pointer to queue rwlock structure
29 * @writer: Current queue rwlock writer status byte
30 *
31 * In interrupt context or at the head of the queue, the reader will just
32 * increment the reader count & wait until the writer releases the lock.
33 */
34static __always_inline void
35rspin_until_writer_unlock(struct qrwlock *lock, u32 cnts)
36{
37 while ((cnts & _QW_WMASK) == _QW_LOCKED) {
f2f09a4c 38 cpu_relax();
77e430e3 39 cnts = atomic_read_acquire(&lock->cnts);
70af2f8a
WL
40 }
41}
42
43/**
f7d71f20 44 * queued_read_lock_slowpath - acquire read lock of a queue rwlock
70af2f8a 45 * @lock: Pointer to queue rwlock structure
0e06e5be 46 * @cnts: Current qrwlock lock value
70af2f8a 47 */
0e06e5be 48void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts)
70af2f8a 49{
70af2f8a
WL
50 /*
51 * Readers come here when they cannot get the lock without waiting
52 */
53 if (unlikely(in_interrupt())) {
54 /*
0e06e5be
WL
55 * Readers in interrupt context will get the lock immediately
56 * if the writer is just waiting (not holding the lock yet).
57 * The rspin_until_writer_unlock() function returns immediately
77e430e3
WD
58 * in this case. Otherwise, they will spin (with ACQUIRE
59 * semantics) until the lock is available without waiting in
60 * the queue.
70af2f8a 61 */
70af2f8a
WL
62 rspin_until_writer_unlock(lock, cnts);
63 return;
64 }
65 atomic_sub(_QR_BIAS, &lock->cnts);
66
67 /*
68 * Put the reader into the wait queue
69 */
6e1e5196 70 arch_spin_lock(&lock->wait_lock);
70af2f8a
WL
71
72 /*
77e430e3
WD
73 * The ACQUIRE semantics of the following spinning code ensure
74 * that accesses can't leak upwards out of our subsequent critical
75 * section in the case that the lock is currently held for write.
70af2f8a 76 */
f9852b74 77 cnts = atomic_fetch_add_acquire(_QR_BIAS, &lock->cnts);
70af2f8a
WL
78 rspin_until_writer_unlock(lock, cnts);
79
80 /*
81 * Signal the next one in queue to become queue head
82 */
6e1e5196 83 arch_spin_unlock(&lock->wait_lock);
70af2f8a 84}
f7d71f20 85EXPORT_SYMBOL(queued_read_lock_slowpath);
70af2f8a
WL
86
87/**
f7d71f20 88 * queued_write_lock_slowpath - acquire write lock of a queue rwlock
70af2f8a
WL
89 * @lock : Pointer to queue rwlock structure
90 */
f7d71f20 91void queued_write_lock_slowpath(struct qrwlock *lock)
70af2f8a
WL
92{
93 u32 cnts;
94
95 /* Put the writer into the wait queue */
6e1e5196 96 arch_spin_lock(&lock->wait_lock);
70af2f8a
WL
97
98 /* Try to acquire the lock directly if no reader is present */
99 if (!atomic_read(&lock->cnts) &&
77e430e3 100 (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0))
70af2f8a
WL
101 goto unlock;
102
103 /*
104 * Set the waiting flag to notify readers that a writer is pending,
105 * or wait for a previous writer to go away.
106 */
107 for (;;) {
e0d02285
WD
108 if (!READ_ONCE(lock->wmode) &&
109 (cmpxchg_relaxed(&lock->wmode, 0, _QW_WAITING) == 0))
70af2f8a
WL
110 break;
111
f2f09a4c 112 cpu_relax();
70af2f8a
WL
113 }
114
115 /* When no more readers, set the locked flag */
116 for (;;) {
117 cnts = atomic_read(&lock->cnts);
118 if ((cnts == _QW_WAITING) &&
77e430e3
WD
119 (atomic_cmpxchg_acquire(&lock->cnts, _QW_WAITING,
120 _QW_LOCKED) == _QW_WAITING))
70af2f8a
WL
121 break;
122
f2f09a4c 123 cpu_relax();
70af2f8a
WL
124 }
125unlock:
6e1e5196 126 arch_spin_unlock(&lock->wait_lock);
70af2f8a 127}
f7d71f20 128EXPORT_SYMBOL(queued_write_lock_slowpath);