[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20210425200637.31298-1-longman@redhat.com>
Date: Sun, 25 Apr 2021 16:06:37 -0400
From: Waiman Long <longman@...hat.com>
To: Peter Zijlstra <peterz@...radead.org>,
Ingo Molnar <mingo@...hat.com>, Will Deacon <will@...nel.org>,
Boqun Feng <boqun.feng@...il.com>
Cc: linux-kernel@...r.kernel.org, Borislav Petkov <bp@...e.de>,
Ali Saidi <alisaidi@...zon.com>,
Steve Capper <steve.capper@....com>,
Waiman Long <longman@...hat.com>
Subject: [PATCH] locking/qrwlock: queued_write_lock_slowpath() cleanup
Make the code more readable by replacing the atomic_cmpxchg_acquire()
by an equivalent atomic_try_cmpxchg_acquire() and change atomic_add()
to atomic_or().
For architectures that use qrwlock, I do not find one that has an
atomic_add() defined but not an atomic_or(). I guess it should be fine
by changing atomic_add() to atomic_or(). I add a comment to state that
we can change it back to atomic_add() if there is an architecture that
has a more performant atomic_add() than an atomic_or().
Suggested-by: Linus Torvalds <torvalds@...ux-foundation.org>
Signed-off-by: Waiman Long <longman@...hat.com>
---
kernel/locking/qrwlock.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/kernel/locking/qrwlock.c b/kernel/locking/qrwlock.c
index b94f3831e963..a1fa0f636b07 100644
--- a/kernel/locking/qrwlock.c
+++ b/kernel/locking/qrwlock.c
@@ -60,18 +60,26 @@ EXPORT_SYMBOL(queued_read_lock_slowpath);
*/
void queued_write_lock_slowpath(struct qrwlock *lock)
{
- int cnts;
+ int cnts = 0;
/* Put the writer into the wait queue */
arch_spin_lock(&lock->wait_lock);
/* Try to acquire the lock directly if no reader is present */
if (!atomic_read(&lock->cnts) &&
- (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0))
+ atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED))
goto unlock;
- /* Set the waiting flag to notify readers that a writer is pending */
- atomic_add(_QW_WAITING, &lock->cnts);
+ /*
+ * Set the waiting flag to notify readers that a writer is pending
+ *
+ * As only one writer who is the wait_lock owner can set the waiting
+ * flag which will be cleared later on when acquiring the write lock,
+ * we can easily replace atomic_or() by an atomic_add() if there is
+ * an architecture where an atomic_add() performs better than an
+ * atomic_or().
+ */
+ atomic_or(_QW_WAITING, &lock->cnts);
/* When no more readers or writers, set the locked flag */
do {
--
2.18.1
Powered by blists - more mailing lists