blob: c7471c3fb79898aa9c0176af57f72812564d2641 [file] [log] [blame]
Waiman Long70af2f82014-02-03 13:18:49 +01001/*
Waiman Longc7114b42015-05-11 13:57:11 -04002 * Queued read/write locks
Waiman Long70af2f82014-02-03 13:18:49 +01003 *
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>
Will Deacon06c69ab2017-10-12 13:20:50 +010023#include <linux/spinlock.h>
Waiman Long70af2f82014-02-03 13:18:49 +010024#include <asm/qrwlock.h>
25
26/**
Waiman Longf7d71f22015-06-19 11:50:00 -040027 * queued_read_lock_slowpath - acquire read lock of a queue rwlock
Waiman Long70af2f82014-02-03 13:18:49 +010028 * @lock: Pointer to queue rwlock structure
29 */
Will Deacon64b45522017-10-12 13:20:49 +010030void queued_read_lock_slowpath(struct qrwlock *lock)
Waiman Long70af2f82014-02-03 13:18:49 +010031{
Waiman Long70af2f82014-02-03 13:18:49 +010032 /*
33 * Readers come here when they cannot get the lock without waiting
34 */
35 if (unlikely(in_interrupt())) {
36 /*
Waiman Long0e06e5b2015-06-19 11:50:01 -040037 * Readers in interrupt context will get the lock immediately
Will Deacon64b45522017-10-12 13:20:49 +010038 * if the writer is just waiting (not holding the lock yet),
39 * so spin with ACQUIRE semantics until the lock is available
40 * without waiting in the queue.
Waiman Long70af2f82014-02-03 13:18:49 +010041 */
Will Deacon748ac622017-10-12 13:20:51 +010042 atomic_cond_read_acquire(&lock->cnts, !(VAL & _QW_LOCKED));
Waiman Long70af2f82014-02-03 13:18:49 +010043 return;
44 }
45 atomic_sub(_QR_BIAS, &lock->cnts);
46
47 /*
48 * Put the reader into the wait queue
49 */
Davidlohr Bueso6e1e5192015-09-14 00:37:22 -070050 arch_spin_lock(&lock->wait_lock);
Will Deacon64b45522017-10-12 13:20:49 +010051 atomic_add(_QR_BIAS, &lock->cnts);
Waiman Long70af2f82014-02-03 13:18:49 +010052
53 /*
Will Deacon77e430e2015-08-06 17:54:42 +010054 * The ACQUIRE semantics of the following spinning code ensure
55 * that accesses can't leak upwards out of our subsequent critical
56 * section in the case that the lock is currently held for write.
Waiman Long70af2f82014-02-03 13:18:49 +010057 */
Will Deacon748ac622017-10-12 13:20:51 +010058 atomic_cond_read_acquire(&lock->cnts, !(VAL & _QW_LOCKED));
Waiman Long70af2f82014-02-03 13:18:49 +010059
60 /*
61 * Signal the next one in queue to become queue head
62 */
Davidlohr Bueso6e1e5192015-09-14 00:37:22 -070063 arch_spin_unlock(&lock->wait_lock);
Waiman Long70af2f82014-02-03 13:18:49 +010064}
Waiman Longf7d71f22015-06-19 11:50:00 -040065EXPORT_SYMBOL(queued_read_lock_slowpath);
Waiman Long70af2f82014-02-03 13:18:49 +010066
67/**
Waiman Longf7d71f22015-06-19 11:50:00 -040068 * queued_write_lock_slowpath - acquire write lock of a queue rwlock
Waiman Long70af2f82014-02-03 13:18:49 +010069 * @lock : Pointer to queue rwlock structure
70 */
Waiman Longf7d71f22015-06-19 11:50:00 -040071void queued_write_lock_slowpath(struct qrwlock *lock)
Waiman Long70af2f82014-02-03 13:18:49 +010072{
Waiman Long70af2f82014-02-03 13:18:49 +010073 /* Put the writer into the wait queue */
Davidlohr Bueso6e1e5192015-09-14 00:37:22 -070074 arch_spin_lock(&lock->wait_lock);
Waiman Long70af2f82014-02-03 13:18:49 +010075
76 /* Try to acquire the lock directly if no reader is present */
77 if (!atomic_read(&lock->cnts) &&
Will Deacon77e430e2015-08-06 17:54:42 +010078 (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0))
Waiman Long70af2f82014-02-03 13:18:49 +010079 goto unlock;
80
Will Deacon748ac622017-10-12 13:20:51 +010081 /* Set the waiting flag to notify readers that a writer is pending */
82 atomic_add(_QW_WAITING, &lock->cnts);
Waiman Long70af2f82014-02-03 13:18:49 +010083
Will Deacon748ac622017-10-12 13:20:51 +010084 /* When no more readers or writers, set the locked flag */
Will Deacon64b45522017-10-12 13:20:49 +010085 do {
86 atomic_cond_read_acquire(&lock->cnts, VAL == _QW_WAITING);
87 } while (atomic_cmpxchg_relaxed(&lock->cnts, _QW_WAITING,
88 _QW_LOCKED) != _QW_WAITING);
Waiman Long70af2f82014-02-03 13:18:49 +010089unlock:
Davidlohr Bueso6e1e5192015-09-14 00:37:22 -070090 arch_spin_unlock(&lock->wait_lock);
Waiman Long70af2f82014-02-03 13:18:49 +010091}
Waiman Longf7d71f22015-06-19 11:50:00 -040092EXPORT_SYMBOL(queued_write_lock_slowpath);