All of lore.kernel.org
 help / color / mirror / Atom feed
From: Waiman Long <longman@redhat.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
	Boqun Feng <boqun.feng@gmail.com>, Arnd Bergmann <arnd@arndb.de>
Cc: linux-kernel@vger.kernel.org, Waiman Long <longman@redhat.com>
Subject: [PATCH 2/2] locking/qrwlock: Reduce cacheline contention for rwlocks used in interrupt context
Date: Tue, 10 May 2022 15:21:34 -0400	[thread overview]
Message-ID: <20220510192134.434753-2-longman@redhat.com> (raw)
In-Reply-To: <20220510192134.434753-1-longman@redhat.com>

Even though qrwlock is supposed to be a fair lock, it does allow readers
from interrupt context to spin on the lock until it can acquire it making
it not as fair. This exception was added due to the requirement to allow
recursive read lock in interrupt context. This can also be achieved by
just ignoring the writer waiting bit without spinning on the lock.

By making this change, we make qrwlock a bit more fair and eliminating
the problem of cacheline bouncing for rwlocks that are used heavily in
interrupt context, like the networking stack. This should also reduce
the chance of lock starvation for those interrupt context rwlocks.

Signed-off-by: Waiman Long <longman@redhat.com>
---
 include/asm-generic/qrwlock.h |  6 +++---
 kernel/locking/qrwlock.c      | 17 ++++++-----------
 2 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/include/asm-generic/qrwlock.h b/include/asm-generic/qrwlock.h
index d4cd4cc4389c..9d40cf016e0c 100644
--- a/include/asm-generic/qrwlock.h
+++ b/include/asm-generic/qrwlock.h
@@ -29,7 +29,7 @@
 /*
  * External function declarations
  */
-extern void queued_read_lock_slowpath(struct qrwlock *lock);
+extern void queued_read_lock_slowpath(struct qrwlock *lock, int cnts);
 extern void queued_write_lock_slowpath(struct qrwlock *lock);
 
 /**
@@ -80,7 +80,7 @@ static inline void queued_read_lock(struct qrwlock *lock)
 		return;
 
 	/* The slowpath will decrement the reader count, if necessary. */
-	queued_read_lock_slowpath(lock);
+	queued_read_lock_slowpath(lock, cnts);
 }
 
 /**
@@ -90,7 +90,7 @@ static inline void queued_read_lock(struct qrwlock *lock)
 static inline void queued_write_lock(struct qrwlock *lock)
 {
 	int cnts = 0;
-	/* Optimize for the unfair lock case where the fair flag is 0. */
+
 	if (likely(atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED)))
 		return;
 
diff --git a/kernel/locking/qrwlock.c b/kernel/locking/qrwlock.c
index 2e1600906c9f..d52d13e95600 100644
--- a/kernel/locking/qrwlock.c
+++ b/kernel/locking/qrwlock.c
@@ -18,21 +18,16 @@
  * queued_read_lock_slowpath - acquire read lock of a queued rwlock
  * @lock: Pointer to queued rwlock structure
  */
-void queued_read_lock_slowpath(struct qrwlock *lock)
+void queued_read_lock_slowpath(struct qrwlock *lock, int cnts)
 {
 	/*
-	 * Readers come here when they cannot get the lock without waiting
+	 * Readers come here when they cannot get the lock without waiting.
+	 * Readers in interrupt context can steal the lock immediately
+	 * if the writer is just waiting (not holding the lock yet).
 	 */
-	if (unlikely(in_interrupt())) {
-		/*
-		 * Readers in interrupt context will get the lock immediately
-		 * if the writer is just waiting (not holding the lock yet),
-		 * so spin with ACQUIRE semantics until the lock is available
-		 * without waiting in the queue.
-		 */
-		atomic_cond_read_acquire(&lock->cnts, !(VAL & _QW_LOCKED));
+	if (unlikely(!(cnts & _QW_LOCKED) && in_interrupt()))
 		return;
-	}
+
 	atomic_sub(_QR_BIAS, &lock->cnts);
 
 	trace_contention_begin(lock, LCB_F_SPIN | LCB_F_READ);
-- 
2.27.0


  reply	other threads:[~2022-05-10 19:22 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-10 19:21 [PATCH 1/2] locking/qrwlock: Change "queue rwlock" to "queued rwlock" Waiman Long
2022-05-10 19:21 ` Waiman Long [this message]
2022-05-11  7:20   ` [PATCH 2/2] locking/qrwlock: Reduce cacheline contention for rwlocks used in interrupt context Arnd Bergmann
2022-05-11 12:01     ` Waiman Long
2022-05-11  8:30   ` Peter Zijlstra
2022-05-11 12:44     ` Waiman Long
2022-05-11 13:34       ` Peter Zijlstra
2022-05-11 16:00         ` Waiman Long
2022-05-11 19:39 ` [tip: locking/core] locking/qrwlock: Change "queue rwlock" to "queued rwlock" tip-bot2 for Waiman Long

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220510192134.434753-2-longman@redhat.com \
    --to=longman@redhat.com \
    --cc=arnd@arndb.de \
    --cc=boqun.feng@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.