blob: fe9ca92faa2a7a7b0915a6ae420f31b2e4fc4dd8 [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/*
Waiman Longc7114b42015-05-11 13:57:11 -04003 * Queued read/write locks
Waiman Long70af2f82014-02-03 13:18:49 +01004 *
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#include <linux/smp.h>
10#include <linux/bug.h>
11#include <linux/cpumask.h>
12#include <linux/percpu.h>
13#include <linux/hardirq.h>
Babu Moger9ab60552017-05-24 17:55:10 -060014#include <linux/spinlock.h>
Waiman Long70af2f82014-02-03 13:18:49 +010015#include <asm/qrwlock.h>
16
17/**
Waiman Longf7d71f22015-06-19 11:50:00 -040018 * queued_read_lock_slowpath - acquire read lock of a queue rwlock
Waiman Long70af2f82014-02-03 13:18:49 +010019 * @lock: Pointer to queue rwlock structure
20 */
Will Deaconb519b562017-10-12 13:20:49 +010021void queued_read_lock_slowpath(struct qrwlock *lock)
Waiman Long70af2f82014-02-03 13:18:49 +010022{
Waiman Long70af2f82014-02-03 13:18:49 +010023 /*
24 * Readers come here when they cannot get the lock without waiting
25 */
26 if (unlikely(in_interrupt())) {
27 /*
Waiman Long0e06e5b2015-06-19 11:50:01 -040028 * Readers in interrupt context will get the lock immediately
Will Deaconb519b562017-10-12 13:20:49 +010029 * if the writer is just waiting (not holding the lock yet),
30 * so spin with ACQUIRE semantics until the lock is available
31 * without waiting in the queue.
Waiman Long70af2f82014-02-03 13:18:49 +010032 */
Will Deacond1331662017-10-12 13:20:51 +010033 atomic_cond_read_acquire(&lock->cnts, !(VAL & _QW_LOCKED));
Waiman Long70af2f82014-02-03 13:18:49 +010034 return;
35 }
36 atomic_sub(_QR_BIAS, &lock->cnts);
37
38 /*
39 * Put the reader into the wait queue
40 */
Davidlohr Bueso6e1e5192015-09-14 00:37:22 -070041 arch_spin_lock(&lock->wait_lock);
Will Deaconb519b562017-10-12 13:20:49 +010042 atomic_add(_QR_BIAS, &lock->cnts);
Waiman Long70af2f82014-02-03 13:18:49 +010043
44 /*
Will Deacon77e430e2015-08-06 17:54:42 +010045 * The ACQUIRE semantics of the following spinning code ensure
46 * that accesses can't leak upwards out of our subsequent critical
47 * section in the case that the lock is currently held for write.
Waiman Long70af2f82014-02-03 13:18:49 +010048 */
Will Deacond1331662017-10-12 13:20:51 +010049 atomic_cond_read_acquire(&lock->cnts, !(VAL & _QW_LOCKED));
Waiman Long70af2f82014-02-03 13:18:49 +010050
51 /*
52 * Signal the next one in queue to become queue head
53 */
Davidlohr Bueso6e1e5192015-09-14 00:37:22 -070054 arch_spin_unlock(&lock->wait_lock);
Waiman Long70af2f82014-02-03 13:18:49 +010055}
Waiman Longf7d71f22015-06-19 11:50:00 -040056EXPORT_SYMBOL(queued_read_lock_slowpath);
Waiman Long70af2f82014-02-03 13:18:49 +010057
58/**
Waiman Longf7d71f22015-06-19 11:50:00 -040059 * queued_write_lock_slowpath - acquire write lock of a queue rwlock
Waiman Long70af2f82014-02-03 13:18:49 +010060 * @lock : Pointer to queue rwlock structure
61 */
Waiman Longf7d71f22015-06-19 11:50:00 -040062void queued_write_lock_slowpath(struct qrwlock *lock)
Waiman Long70af2f82014-02-03 13:18:49 +010063{
Waiman Long70af2f82014-02-03 13:18:49 +010064 /* Put the writer into the wait queue */
Davidlohr Bueso6e1e5192015-09-14 00:37:22 -070065 arch_spin_lock(&lock->wait_lock);
Waiman Long70af2f82014-02-03 13:18:49 +010066
67 /* Try to acquire the lock directly if no reader is present */
68 if (!atomic_read(&lock->cnts) &&
Will Deacon77e430e2015-08-06 17:54:42 +010069 (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0))
Waiman Long70af2f82014-02-03 13:18:49 +010070 goto unlock;
71
Will Deacond1331662017-10-12 13:20:51 +010072 /* Set the waiting flag to notify readers that a writer is pending */
73 atomic_add(_QW_WAITING, &lock->cnts);
Waiman Long70af2f82014-02-03 13:18:49 +010074
Will Deacond1331662017-10-12 13:20:51 +010075 /* When no more readers or writers, set the locked flag */
Will Deaconb519b562017-10-12 13:20:49 +010076 do {
77 atomic_cond_read_acquire(&lock->cnts, VAL == _QW_WAITING);
78 } while (atomic_cmpxchg_relaxed(&lock->cnts, _QW_WAITING,
79 _QW_LOCKED) != _QW_WAITING);
Waiman Long70af2f82014-02-03 13:18:49 +010080unlock:
Davidlohr Bueso6e1e5192015-09-14 00:37:22 -070081 arch_spin_unlock(&lock->wait_lock);
Waiman Long70af2f82014-02-03 13:18:49 +010082}
Waiman Longf7d71f22015-06-19 11:50:00 -040083EXPORT_SYMBOL(queued_write_lock_slowpath);