[RFC PATCH v2 3/6] locking/rwsem: Publish the top waiter priority

From: Peng Huang

Date: Tue Dec 01 2026 - 01:28:07 EST


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@xxxxxxxxxx>
---
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