xref: /openbmc/linux/kernel/locking/qrwlock.c (revision 501f7f69)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
270af2f8aSWaiman Long /*
3c7114b4eSWaiman Long  * Queued read/write locks
470af2f8aSWaiman Long  *
570af2f8aSWaiman Long  * (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.
670af2f8aSWaiman Long  *
770af2f8aSWaiman Long  * Authors: Waiman Long <waiman.long@hp.com>
870af2f8aSWaiman Long  */
970af2f8aSWaiman Long #include <linux/smp.h>
1070af2f8aSWaiman Long #include <linux/bug.h>
1170af2f8aSWaiman Long #include <linux/cpumask.h>
1270af2f8aSWaiman Long #include <linux/percpu.h>
1370af2f8aSWaiman Long #include <linux/hardirq.h>
149ab6055fSBabu Moger #include <linux/spinlock.h>
15ee042be1SNamhyung Kim #include <trace/events/lock.h>
1670af2f8aSWaiman Long 
1770af2f8aSWaiman Long /**
18434e09e7SWaiman Long  * queued_read_lock_slowpath - acquire read lock of a queued rwlock
19434e09e7SWaiman Long  * @lock: Pointer to queued rwlock structure
2070af2f8aSWaiman Long  */
queued_read_lock_slowpath(struct qrwlock * lock)21*501f7f69SNamhyung Kim void __lockfunc queued_read_lock_slowpath(struct qrwlock *lock)
2270af2f8aSWaiman Long {
2370af2f8aSWaiman Long 	/*
2470af2f8aSWaiman Long 	 * Readers come here when they cannot get the lock without waiting
2570af2f8aSWaiman Long 	 */
2670af2f8aSWaiman Long 	if (unlikely(in_interrupt())) {
2770af2f8aSWaiman Long 		/*
280e06e5beSWaiman Long 		 * Readers in interrupt context will get the lock immediately
29b519b56eSWill Deacon 		 * if the writer is just waiting (not holding the lock yet),
30b519b56eSWill Deacon 		 * so spin with ACQUIRE semantics until the lock is available
31b519b56eSWill Deacon 		 * without waiting in the queue.
3270af2f8aSWaiman Long 		 */
33d1331661SWill Deacon 		atomic_cond_read_acquire(&lock->cnts, !(VAL & _QW_LOCKED));
3470af2f8aSWaiman Long 		return;
3570af2f8aSWaiman Long 	}
3670af2f8aSWaiman Long 	atomic_sub(_QR_BIAS, &lock->cnts);
3770af2f8aSWaiman Long 
38ee042be1SNamhyung Kim 	trace_contention_begin(lock, LCB_F_SPIN | LCB_F_READ);
39ee042be1SNamhyung Kim 
4070af2f8aSWaiman Long 	/*
4170af2f8aSWaiman Long 	 * Put the reader into the wait queue
4270af2f8aSWaiman Long 	 */
436e1e5196SDavidlohr Bueso 	arch_spin_lock(&lock->wait_lock);
44b519b56eSWill Deacon 	atomic_add(_QR_BIAS, &lock->cnts);
4570af2f8aSWaiman Long 
4670af2f8aSWaiman Long 	/*
4777e430e3SWill Deacon 	 * The ACQUIRE semantics of the following spinning code ensure
4877e430e3SWill Deacon 	 * that accesses can't leak upwards out of our subsequent critical
4977e430e3SWill Deacon 	 * section in the case that the lock is currently held for write.
5070af2f8aSWaiman Long 	 */
51d1331661SWill Deacon 	atomic_cond_read_acquire(&lock->cnts, !(VAL & _QW_LOCKED));
5270af2f8aSWaiman Long 
5370af2f8aSWaiman Long 	/*
5470af2f8aSWaiman Long 	 * Signal the next one in queue to become queue head
5570af2f8aSWaiman Long 	 */
566e1e5196SDavidlohr Bueso 	arch_spin_unlock(&lock->wait_lock);
57ee042be1SNamhyung Kim 
58ee042be1SNamhyung Kim 	trace_contention_end(lock, 0);
5970af2f8aSWaiman Long }
60f7d71f20SWaiman Long EXPORT_SYMBOL(queued_read_lock_slowpath);
6170af2f8aSWaiman Long 
6270af2f8aSWaiman Long /**
63434e09e7SWaiman Long  * queued_write_lock_slowpath - acquire write lock of a queued rwlock
64434e09e7SWaiman Long  * @lock : Pointer to queued rwlock structure
6570af2f8aSWaiman Long  */
queued_write_lock_slowpath(struct qrwlock * lock)66*501f7f69SNamhyung Kim void __lockfunc queued_write_lock_slowpath(struct qrwlock *lock)
6770af2f8aSWaiman Long {
6884a24bf8SAli Saidi 	int cnts;
6984a24bf8SAli Saidi 
70ee042be1SNamhyung Kim 	trace_contention_begin(lock, LCB_F_SPIN | LCB_F_WRITE);
71ee042be1SNamhyung Kim 
7270af2f8aSWaiman Long 	/* Put the writer into the wait queue */
736e1e5196SDavidlohr Bueso 	arch_spin_lock(&lock->wait_lock);
7470af2f8aSWaiman Long 
7570af2f8aSWaiman Long 	/* Try to acquire the lock directly if no reader is present */
7628ce0e70SWaiman Long 	if (!(cnts = atomic_read(&lock->cnts)) &&
7728ce0e70SWaiman Long 	    atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED))
7870af2f8aSWaiman Long 		goto unlock;
7970af2f8aSWaiman Long 
80d1331661SWill Deacon 	/* Set the waiting flag to notify readers that a writer is pending */
8128ce0e70SWaiman Long 	atomic_or(_QW_WAITING, &lock->cnts);
8270af2f8aSWaiman Long 
83d1331661SWill Deacon 	/* When no more readers or writers, set the locked flag */
84b519b56eSWill Deacon 	do {
8584a24bf8SAli Saidi 		cnts = atomic_cond_read_relaxed(&lock->cnts, VAL == _QW_WAITING);
8684a24bf8SAli Saidi 	} while (!atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED));
8770af2f8aSWaiman Long unlock:
886e1e5196SDavidlohr Bueso 	arch_spin_unlock(&lock->wait_lock);
89ee042be1SNamhyung Kim 
90ee042be1SNamhyung Kim 	trace_contention_end(lock, 0);
9170af2f8aSWaiman Long }
92f7d71f20SWaiman Long EXPORT_SYMBOL(queued_write_lock_slowpath);
93