blob: 7ae0ece07b4e4968158980a5ce71922e0cdd120c [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Waiman Long70af2f82014-02-03 13:18:49 +01002/*
3 * Queue read/write lock
4 *
Waiman Long70af2f82014-02-03 13:18:49 +01005 * (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.
6 *
7 * Authors: Waiman Long <waiman.long@hp.com>
8 */
9#ifndef __ASM_GENERIC_QRWLOCK_H
10#define __ASM_GENERIC_QRWLOCK_H
11
12#include <linux/atomic.h>
13#include <asm/barrier.h>
14#include <asm/processor.h>
15
16#include <asm-generic/qrwlock_types.h>
Waiman Longd8d0da42021-02-10 13:16:31 -050017
18/* Must be included from asm/spinlock.h after defining arch_spin_is_locked. */
Waiman Long70af2f82014-02-03 13:18:49 +010019
20/*
pan xinhui2db34e82016-07-18 17:47:39 +080021 * Writer states & reader shift and bias.
Waiman Long70af2f82014-02-03 13:18:49 +010022 */
Will Deacond1331662017-10-12 13:20:51 +010023#define _QW_WAITING 0x100 /* A writer is waiting */
24#define _QW_LOCKED 0x0ff /* A writer holds the lock */
25#define _QW_WMASK 0x1ff /* Writer mask */
26#define _QR_SHIFT 9 /* Reader count shift */
Waiman Long70af2f82014-02-03 13:18:49 +010027#define _QR_BIAS (1U << _QR_SHIFT)
28
29/*
30 * External function declarations
31 */
Will Deaconb519b562017-10-12 13:20:49 +010032extern void queued_read_lock_slowpath(struct qrwlock *lock);
Waiman Longf7d71f22015-06-19 11:50:00 -040033extern void queued_write_lock_slowpath(struct qrwlock *lock);
Waiman Long70af2f82014-02-03 13:18:49 +010034
35/**
Waiman Longf7d71f22015-06-19 11:50:00 -040036 * queued_read_trylock - try to acquire read lock of a queue rwlock
Waiman Long70af2f82014-02-03 13:18:49 +010037 * @lock : Pointer to queue rwlock structure
38 * Return: 1 if lock acquired, 0 if failed
39 */
Waiman Longf7d71f22015-06-19 11:50:00 -040040static inline int queued_read_trylock(struct qrwlock *lock)
Waiman Long70af2f82014-02-03 13:18:49 +010041{
Arnd Bergmannf44ca082020-10-19 09:09:21 +020042 int cnts;
Waiman Long70af2f82014-02-03 13:18:49 +010043
44 cnts = atomic_read(&lock->cnts);
45 if (likely(!(cnts & _QW_WMASK))) {
Will Deacon77e430e2015-08-06 17:54:42 +010046 cnts = (u32)atomic_add_return_acquire(_QR_BIAS, &lock->cnts);
Waiman Long70af2f82014-02-03 13:18:49 +010047 if (likely(!(cnts & _QW_WMASK)))
48 return 1;
49 atomic_sub(_QR_BIAS, &lock->cnts);
50 }
51 return 0;
52}
53
54/**
Waiman Longf7d71f22015-06-19 11:50:00 -040055 * queued_write_trylock - try to acquire write lock of a queue rwlock
Waiman Long70af2f82014-02-03 13:18:49 +010056 * @lock : Pointer to queue rwlock structure
57 * Return: 1 if lock acquired, 0 if failed
58 */
Waiman Longf7d71f22015-06-19 11:50:00 -040059static inline int queued_write_trylock(struct qrwlock *lock)
Waiman Long70af2f82014-02-03 13:18:49 +010060{
Arnd Bergmannf44ca082020-10-19 09:09:21 +020061 int cnts;
Waiman Long70af2f82014-02-03 13:18:49 +010062
63 cnts = atomic_read(&lock->cnts);
64 if (unlikely(cnts))
65 return 0;
66
Matthew Wilcox27df8962018-08-20 10:19:14 -040067 return likely(atomic_try_cmpxchg_acquire(&lock->cnts, &cnts,
68 _QW_LOCKED));
Waiman Long70af2f82014-02-03 13:18:49 +010069}
70/**
Waiman Longf7d71f22015-06-19 11:50:00 -040071 * queued_read_lock - acquire read lock of a queue rwlock
Waiman Long70af2f82014-02-03 13:18:49 +010072 * @lock: Pointer to queue rwlock structure
73 */
Waiman Longf7d71f22015-06-19 11:50:00 -040074static inline void queued_read_lock(struct qrwlock *lock)
Waiman Long70af2f82014-02-03 13:18:49 +010075{
Arnd Bergmannf44ca082020-10-19 09:09:21 +020076 int cnts;
Waiman Long70af2f82014-02-03 13:18:49 +010077
Will Deacon77e430e2015-08-06 17:54:42 +010078 cnts = atomic_add_return_acquire(_QR_BIAS, &lock->cnts);
Waiman Long70af2f82014-02-03 13:18:49 +010079 if (likely(!(cnts & _QW_WMASK)))
80 return;
81
82 /* The slowpath will decrement the reader count, if necessary. */
Will Deaconb519b562017-10-12 13:20:49 +010083 queued_read_lock_slowpath(lock);
Waiman Long70af2f82014-02-03 13:18:49 +010084}
85
86/**
Waiman Longf7d71f22015-06-19 11:50:00 -040087 * queued_write_lock - acquire write lock of a queue rwlock
Waiman Long70af2f82014-02-03 13:18:49 +010088 * @lock : Pointer to queue rwlock structure
89 */
Waiman Longf7d71f22015-06-19 11:50:00 -040090static inline void queued_write_lock(struct qrwlock *lock)
Waiman Long70af2f82014-02-03 13:18:49 +010091{
Arnd Bergmannf44ca082020-10-19 09:09:21 +020092 int cnts = 0;
Waiman Long70af2f82014-02-03 13:18:49 +010093 /* Optimize for the unfair lock case where the fair flag is 0. */
Matthew Wilcox27df8962018-08-20 10:19:14 -040094 if (likely(atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED)))
Waiman Long70af2f82014-02-03 13:18:49 +010095 return;
96
Waiman Longf7d71f22015-06-19 11:50:00 -040097 queued_write_lock_slowpath(lock);
Waiman Long70af2f82014-02-03 13:18:49 +010098}
99
100/**
Waiman Longf7d71f22015-06-19 11:50:00 -0400101 * queued_read_unlock - release read lock of a queue rwlock
Waiman Long70af2f82014-02-03 13:18:49 +0100102 * @lock : Pointer to queue rwlock structure
103 */
Waiman Longf7d71f22015-06-19 11:50:00 -0400104static inline void queued_read_unlock(struct qrwlock *lock)
Waiman Long70af2f82014-02-03 13:18:49 +0100105{
106 /*
107 * Atomically decrement the reader count
108 */
Will Deacon77e430e2015-08-06 17:54:42 +0100109 (void)atomic_sub_return_release(_QR_BIAS, &lock->cnts);
Waiman Long70af2f82014-02-03 13:18:49 +0100110}
111
Waiman Long70af2f82014-02-03 13:18:49 +0100112/**
Waiman Longf7d71f22015-06-19 11:50:00 -0400113 * queued_write_unlock - release write lock of a queue rwlock
Waiman Long70af2f82014-02-03 13:18:49 +0100114 * @lock : Pointer to queue rwlock structure
115 */
Waiman Longf7d71f22015-06-19 11:50:00 -0400116static inline void queued_write_unlock(struct qrwlock *lock)
Waiman Long70af2f82014-02-03 13:18:49 +0100117{
Will Deacond1331662017-10-12 13:20:51 +0100118 smp_store_release(&lock->wlocked, 0);
Waiman Long70af2f82014-02-03 13:18:49 +0100119}
Waiman Long70af2f82014-02-03 13:18:49 +0100120
Ben Gardon26128cb2021-02-02 10:57:12 -0800121/**
122 * queued_rwlock_is_contended - check if the lock is contended
123 * @lock : Pointer to queue rwlock structure
124 * Return: 1 if lock contended, 0 otherwise
125 */
126static inline int queued_rwlock_is_contended(struct qrwlock *lock)
127{
128 return arch_spin_is_locked(&lock->wait_lock);
129}
130
Waiman Long70af2f82014-02-03 13:18:49 +0100131/*
132 * Remapping rwlock architecture specific functions to the corresponding
133 * queue rwlock functions.
134 */
Ben Gardon26128cb2021-02-02 10:57:12 -0800135#define arch_read_lock(l) queued_read_lock(l)
136#define arch_write_lock(l) queued_write_lock(l)
137#define arch_read_trylock(l) queued_read_trylock(l)
138#define arch_write_trylock(l) queued_write_trylock(l)
139#define arch_read_unlock(l) queued_read_unlock(l)
140#define arch_write_unlock(l) queued_write_unlock(l)
141#define arch_rwlock_is_contended(l) queued_rwlock_is_contended(l)
Waiman Long70af2f82014-02-03 13:18:49 +0100142
143#endif /* __ASM_GENERIC_QRWLOCK_H */