mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peng Huang <penghuang@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,  Will Deacon <will@kernel.org>,
	Boqun Feng <boqun@kernel.org>, Juri Lelli <juri.lelli@redhat.com>,
	 Vincent Guittot <vincent.guittot@linaro.org>
Cc: Waiman Long <longman@redhat.com>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	 Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	 Valentin Schneider <vschneid@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	 John Stultz <jstultz@google.com>,
	Suren Baghdasaryan <surenb@google.com>,
	linux-kernel@vger.kernel.org,  linux-mm@kvack.org,
	Peng Huang <penghuang@google.com>
Subject: [RFC PATCH v2 3/6] locking/rwsem: Publish the top waiter priority
Date: Mon, 30 Nov 2026 22:27:30 -0800	[thread overview]
Message-ID: <20261201062738.1047159-4-penghuang@google.com> (raw)
In-Reply-To: <20261201062738.1047159-1-penghuang@google.com>

Holders need to know how urgent their waiters are without walking the
wait list.  Pack the top waiter priority into sem->count bits 3..8
(encoded as MAX_PRIO - prio, 0 = no demand) and expose
rwsem_top_waiter_prio(), shifting the reader count up by one bit so
struct rw_semaphore stays 32 bytes (54 reader bits on 64-bit, 22 on
32-bit).

Each waiter samples its priority before taking wait_lock: RT/DL
waiters cap at MAX_RT_PRIO (nice -20) and SCHED_IDLE waiters ask for
no boost.  The field is maintained under wait_lock alongside existing
sem->count updates; only losing a top waiter triggers a rescan, which
stops at the first remaining match (O(1) for equal-priority waiters).
Nothing reads it yet.

Signed-off-by: Peng Huang <penghuang@google.com>
---
 include/linux/rwsem.h  |  31 ++++++++
 kernel/locking/rwsem.c | 177 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 205 insertions(+), 3 deletions(-)

diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
index 2df33131bb5f..d8a49170efd2 100644
--- a/include/linux/rwsem.h
+++ b/include/linux/rwsem.h
@@ -16,6 +16,9 @@
 #include <linux/atomic.h>
 #include <linux/err.h>
 #include <linux/cleanup.h>
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+#include <linux/sched/prio.h>
+#endif
 
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 # define __RWSEM_DEP_MAP_INIT(lockname)			\
@@ -87,6 +90,34 @@ static inline void rwsem_assert_held_write_nolockdep(const struct rw_semaphore *
 	WARN_ON(!(atomic_long_read(&sem->count) & RWSEM_WRITER_LOCKED));
 }
 
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+/*
+ * Top waiter priority packed into sem->count bits 3..8: 0 = MAX_PRIO,
+ * 1..NICE_WIDTH = MAX_PRIO - prio (no extra struct rw_semaphore bytes).
+ */
+#define RWSEM_BOOST_PRIO_SHIFT	3
+#define RWSEM_BOOST_PRIO_BITS	6
+#define RWSEM_BOOST_PRIO_MASK	(((1UL << RWSEM_BOOST_PRIO_BITS) - 1) << \
+				 RWSEM_BOOST_PRIO_SHIFT)
+
+/**
+ * rwsem_top_waiter_prio - Priority of the most urgent waiter on @sem
+ * @sem: the rw_semaphore
+ *
+ * Lockless snapshot; RT and deadline waiters count as MAX_RT_PRIO.
+ *
+ * Return: the lowest waiter priority, or MAX_PRIO if no waiter asks
+ * for a boost.
+ */
+static inline int rwsem_top_waiter_prio(const struct rw_semaphore *sem)
+{
+	unsigned long count = atomic_long_read(&sem->count);
+
+	return MAX_PRIO - ((count & RWSEM_BOOST_PRIO_MASK) >>
+			   RWSEM_BOOST_PRIO_SHIFT);
+}
+#endif
+
 /* Common initializer macros and functions */
 
 #ifdef CONFIG_DEBUG_RWSEMS
diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
index 798a8db7636c..6d9b78a3d33a 100644
--- a/kernel/locking/rwsem.c
+++ b/kernel/locking/rwsem.c
@@ -221,6 +221,13 @@ static inline void __rwsem_boost_untrack(struct rw_semaphore *sem)
  * Bits 8-30 - 23-bit reader count
  * Bit  31   - read fail bit
  *
+ * With CONFIG_RWSEM_SCHED_BOOST, bits 3-8 hold the priority of the
+ * most urgent waiter (see rwsem_top_waiter_prio()) and the reader
+ * count starts at bit 9: 54 bits on 64-bit, 22 bits on 32-bit.  Like
+ * the handoff bit, the priority bits only change with wait_lock held.
+ * They are non-zero only while the waiters present bit is set and are
+ * cleared by the same atomic operation.
+ *
  * It is not likely that the most significant bit (read fail bit) will ever
  * be set. This guard bit is still checked anyway in the down_read() fastpath
  * just in case we need to use up more of the reader bits for other purpose
@@ -243,7 +250,12 @@ static inline void __rwsem_boost_untrack(struct rw_semaphore *sem)
 #define RWSEM_FLAG_HANDOFF	(1UL << 2)
 #define RWSEM_FLAG_READFAIL	(1UL << (BITS_PER_LONG - 1))
 
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+#define RWSEM_READER_SHIFT	(RWSEM_BOOST_PRIO_SHIFT + RWSEM_BOOST_PRIO_BITS)
+#else
+#define RWSEM_BOOST_PRIO_MASK	0UL
 #define RWSEM_READER_SHIFT	8
+#endif
 #define RWSEM_READER_BIAS	(1UL << RWSEM_READER_SHIFT)
 #define RWSEM_READER_MASK	(~(RWSEM_READER_BIAS - 1))
 #define RWSEM_WRITER_MASK	RWSEM_WRITER_LOCKED
@@ -251,6 +263,13 @@ static inline void __rwsem_boost_untrack(struct rw_semaphore *sem)
 #define RWSEM_READ_FAILED_MASK	(RWSEM_WRITER_MASK|RWSEM_FLAG_WAITERS|\
 				 RWSEM_FLAG_HANDOFF|RWSEM_FLAG_READFAIL)
 
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+static_assert(NICE_WIDTH < (1UL << RWSEM_BOOST_PRIO_BITS));
+static_assert((RWSEM_BOOST_PRIO_MASK &
+	       (RWSEM_WRITER_LOCKED | RWSEM_FLAG_WAITERS |
+		RWSEM_FLAG_HANDOFF | RWSEM_READER_MASK)) == 0);
+#endif
+
 /*
  * All writes to owner are protected by WRITE_ONCE() to make sure that
  * store tearing can't happen as optimistic spinners may read and use
@@ -465,6 +484,9 @@ struct rwsem_waiter {
 	enum rwsem_waiter_type type;
 	unsigned long timeout;
 	bool handoff_set;
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+	u8 boost_prio;
+#endif
 };
 
 enum rwsem_wake_type {
@@ -473,6 +495,142 @@ enum rwsem_wake_type {
 	RWSEM_WAKE_READ_OWNED	/* Waker thread holds the read lock */
 };
 
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+/*
+ * Sample current's priority for boost decisions before taking
+ * wait_lock, so recalc never chases w->task.  SCHED_IDLE ranks below
+ * nice 19 unless PI-boosted into RT, and nice -20 caps it to protect
+ * RT/DL.
+ */
+static inline void rwsem_waiter_init_prio(struct rwsem_waiter *waiter)
+{
+	int prio;
+
+	waiter->boost_prio = MAX_PRIO;
+	if (!static_branch_likely(&rwsem_boost_key))
+		return;
+
+	prio = READ_ONCE(current->prio);
+	if (READ_ONCE(current->policy) == SCHED_IDLE && prio >= MAX_RT_PRIO)
+		prio = MAX_PRIO;
+	waiter->boost_prio = max_t(int, prio, MAX_RT_PRIO);
+}
+
+static inline unsigned long
+rwsem_waiter_prio_bits(const struct rwsem_waiter *waiter)
+{
+	return (unsigned long)(MAX_PRIO - waiter->boost_prio) <<
+	       RWSEM_BOOST_PRIO_SHIFT;
+}
+
+/*
+ * Return the sem->count delta that publishes @waiter's priority, or 0
+ * if it does not outrank the queued waiters.  The published value is
+ * the maximum over the queue; wait_lock serializes all its updates.
+ */
+static inline long
+rwsem_waiter_prio_add_delta(struct rw_semaphore *sem,
+			    const struct rwsem_waiter *waiter)
+	__must_hold(&sem->wait_lock)
+{
+	unsigned long cur_bits, new_bits;
+
+	cur_bits = atomic_long_read(&sem->count) & RWSEM_BOOST_PRIO_MASK;
+	new_bits = rwsem_waiter_prio_bits(waiter);
+	if (new_bits <= cur_bits)
+		return 0;
+	return (long)(new_bits - cur_bits);
+}
+
+/*
+ * Waiters whose best encoded priority is @gone_bits left while others
+ * remain.  Only losing a top waiter can lower the published maximum,
+ * and no remaining waiter can beat it, so stop at the first one that
+ * still matches.  Return the sem->count delta relative to @cur_count.
+ */
+static long rwsem_waiter_prio_recalc_delta(struct rw_semaphore *sem,
+					   long cur_count,
+					   unsigned long gone_bits)
+	__must_hold(&sem->wait_lock)
+{
+	unsigned long top_bits = cur_count & RWSEM_BOOST_PRIO_MASK;
+	struct rwsem_waiter *first = sem->first_waiter, *w;
+	unsigned long bits;
+
+	if (top_bits == 0)
+		return 0;
+	if (gone_bits < top_bits)
+		return 0;
+
+	bits = rwsem_waiter_prio_bits(first);
+	list_for_each_entry(w, &first->list, list) {
+		if (bits >= top_bits)
+			break;
+		bits = max(bits, rwsem_waiter_prio_bits(w));
+	}
+	return (long)bits - (long)top_bits;
+}
+
+/* Publish @waiter's priority if it outranks the queued waiters. */
+static inline void rwsem_waiter_prio_add(struct rw_semaphore *sem,
+					 const struct rwsem_waiter *waiter)
+	__must_hold(&sem->wait_lock)
+{
+	long delta = rwsem_waiter_prio_add_delta(sem, waiter);
+
+	if (delta != 0)
+		atomic_long_add(delta, &sem->count);
+}
+
+/* Republish the top priority after @waiter left; others remain. */
+static inline void rwsem_waiter_prio_del(struct rw_semaphore *sem,
+					 const struct rwsem_waiter *waiter)
+	__must_hold(&sem->wait_lock)
+{
+	long delta;
+
+	delta = rwsem_waiter_prio_recalc_delta(sem,
+					       atomic_long_read(&sem->count),
+					       rwsem_waiter_prio_bits(waiter));
+	if (delta != 0)
+		atomic_long_add(delta, &sem->count);
+}
+#else
+static inline void rwsem_waiter_init_prio(struct rwsem_waiter *waiter)
+{
+}
+
+static inline unsigned long
+rwsem_waiter_prio_bits(const struct rwsem_waiter *waiter)
+{
+	return 0;
+}
+
+static inline long
+rwsem_waiter_prio_add_delta(struct rw_semaphore *sem,
+			    const struct rwsem_waiter *waiter)
+{
+	return 0;
+}
+
+static inline long rwsem_waiter_prio_recalc_delta(struct rw_semaphore *sem,
+						  long cur_count,
+						  unsigned long gone_bits)
+{
+	return 0;
+}
+
+static inline void rwsem_waiter_prio_add(struct rw_semaphore *sem,
+					 const struct rwsem_waiter *waiter)
+{
+}
+
+static inline void rwsem_waiter_prio_del(struct rw_semaphore *sem,
+					 const struct rwsem_waiter *waiter)
+{
+}
+#endif
+
 /*
  * The typical HZ value is either 250 or 1000. So set the minimum waiting
  * time to at least 4ms or 1 jiffy (if it is higher than 4ms) in the wait
@@ -502,6 +660,7 @@ bool __rwsem_del_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter)
 						     struct rwsem_waiter, list);
 	}
 	list_del(&waiter->list);
+	rwsem_waiter_prio_del(sem, waiter);
 
 	return true;
 }
@@ -520,7 +679,8 @@ rwsem_del_waiter(struct rw_semaphore *sem, struct rwsem_waiter *waiter)
 	lockdep_assert_held(&sem->wait_lock);
 	if (__rwsem_del_waiter(sem, waiter))
 		return true;
-	atomic_long_andnot(RWSEM_FLAG_HANDOFF | RWSEM_FLAG_WAITERS, &sem->count);
+	atomic_long_andnot(RWSEM_FLAG_HANDOFF | RWSEM_FLAG_WAITERS |
+			   RWSEM_BOOST_PRIO_MASK, &sem->count);
 	return false;
 }
 
@@ -556,6 +716,7 @@ static void rwsem_mark_wake(struct rw_semaphore *sem,
 {
 	struct rwsem_waiter *waiter, *next;
 	long oldcount, woken = 0, adjustment = 0;
+	unsigned long woken_bits = 0;
 	struct list_head wlist;
 
 	lockdep_assert_held(&sem->wait_lock);
@@ -655,6 +816,7 @@ static void rwsem_mark_wake(struct rw_semaphore *sem,
 			continue;
 
 		woken++;
+		woken_bits = max(woken_bits, rwsem_waiter_prio_bits(waiter));
 		list_move_tail(&waiter->list, &wlist);
 		if (sem->first_waiter == waiter)
 			sem->first_waiter = next;
@@ -678,6 +840,7 @@ static void rwsem_mark_wake(struct rw_semaphore *sem,
 		adjustment -= RWSEM_FLAG_WAITERS;
 		if (oldcount & RWSEM_FLAG_HANDOFF)
 			adjustment -= RWSEM_FLAG_HANDOFF;
+		adjustment -= oldcount & RWSEM_BOOST_PRIO_MASK;
 	} else if (woken) {
 		/*
 		 * When we've woken a reader, we no longer need to force
@@ -685,6 +848,8 @@ static void rwsem_mark_wake(struct rw_semaphore *sem,
 		 */
 		if (oldcount & RWSEM_FLAG_HANDOFF)
 			adjustment -= RWSEM_FLAG_HANDOFF;
+		adjustment += rwsem_waiter_prio_recalc_delta(sem, oldcount,
+							     woken_bits);
 	}
 
 	if (adjustment)
@@ -787,7 +952,8 @@ static inline bool rwsem_try_write_lock(struct rw_semaphore *sem,
 			new &= ~RWSEM_FLAG_HANDOFF;
 
 			if (list_empty(&first->list))
-				new &= ~RWSEM_FLAG_WAITERS;
+				new &= ~(RWSEM_FLAG_WAITERS |
+					 RWSEM_BOOST_PRIO_MASK);
 		}
 	} while (!atomic_long_try_cmpxchg_acquire(&sem->count, &count, new));
 
@@ -1187,6 +1353,7 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
 	waiter.type = RWSEM_WAITING_FOR_READ;
 	waiter.timeout = jiffies + RWSEM_WAIT_TIMEOUT;
 	waiter.handoff_set = false;
+	rwsem_waiter_init_prio(&waiter);
 
 	raw_spin_lock_irq(&sem->wait_lock);
 	first = sem->first_waiter;
@@ -1211,6 +1378,7 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
 	} else {
 		list_add_tail(&waiter.list, &first->list);
 	}
+	adjustment += rwsem_waiter_prio_add_delta(sem, &waiter);
 
 	/* we're now waiting on the lock, but no longer actively locking */
 	count = atomic_long_add_return(adjustment, &sem->count);
@@ -1287,12 +1455,14 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
 	waiter.type = RWSEM_WAITING_FOR_WRITE;
 	waiter.timeout = jiffies + RWSEM_WAIT_TIMEOUT;
 	waiter.handoff_set = false;
+	rwsem_waiter_init_prio(&waiter);
 
 	raw_spin_lock_irq(&sem->wait_lock);
 
 	first = sem->first_waiter;
 	if (first) {
 		list_add_tail(&waiter.list, &first->list);
+		rwsem_waiter_prio_add(sem, &waiter);
 		rwsem_cond_wake_waiter(sem, atomic_long_read(&sem->count),
 				       &wake_q);
 		if (!wake_q_empty(&wake_q)) {
@@ -1307,7 +1477,8 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
 	} else {
 		INIT_LIST_HEAD(&waiter.list);
 		sem->first_waiter = &waiter;
-		atomic_long_or(RWSEM_FLAG_WAITERS, &sem->count);
+		atomic_long_or(RWSEM_FLAG_WAITERS |
+			       rwsem_waiter_prio_bits(&waiter), &sem->count);
 	}
 
 	/* wait until we successfully acquire the lock */
-- 
2.56.1.839.g22a8290db9-goog


  parent reply	other threads:[~2026-12-01  6:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  6:27 [RFC PATCH v2 0/6] rwsem: priority boost for holders blocking high-priority waiters Peng Huang
2026-12-01  6:27 ` [RFC PATCH v2 1/6] locking/rwsem: Track held rwsems per task Peng Huang
2026-12-01  6:27 ` [RFC PATCH v2 2/6] btrfs: untrack i_rwsem before io_uring encoded read completion Peng Huang
2026-12-01  8:40   ` Christoph Hellwig
2026-12-01  6:27 ` Peng Huang [this message]
2026-12-01  6:27 ` [RFC PATCH v2 4/6] sched: Apply rwsem holder boost to the fair load weight Peng Huang
2026-12-01  6:27 ` [RFC PATCH v2 5/6] sched/fair: Boost rwsem holders with contended high-priority waiters Peng Huang
2026-12-01  6:27 ` [RFC PATCH v2 6/6] locking/rwsem: Add KUnit tests for scheduler priority boost Peng Huang

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=20261201062738.1047159-4-penghuang@google.com \
    --to=penghuang@google.com \
    --cc=boqun@kernel.org \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=jstultz@google.com \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=longman@redhat.com \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=surenb@google.com \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®