* [RFC PATCH v2 1/6] locking/rwsem: Track held rwsems per task
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 ` Peng Huang
2026-12-01 20:43 ` [RFC PATCH v2 0/6] rwsem: priority boost for holders blocking high-priority waiters Kayra Cizmeci
1 sibling, 0 replies; 8+ messages in thread
From: Peng Huang @ 2026-12-01 6:27 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Will Deacon, Boqun Feng, Juri Lelli,
Vincent Guittot
Cc: Waiman Long, Dietmar Eggemann, Steven Rostedt, Ben Segall,
Mel Gorman, Valentin Schneider, K Prateek Nayak, John Stultz,
Suren Baghdasaryan, linux-kernel, linux-mm, Peng Huang,
Jonathan Corbet, Shuah Khan, Randy Dunlap,
Sebastian Andrzej Siewior, Clark Williams, linux-doc,
linux-rt-devel
An rw_semaphore does not track its readers: sem->owner keeps at most
the last reader as a hint, because tracking all readers in the lock
was considered too expensive. Nothing can find the tasks holding an
rwsem for read.
Track the other way round: record up to RWSEM_SCHED_BOOST_SLOTS (4)
held rwsems per task in struct rwsem_sched_boost so later patches can
boost preempted holders.
Only current writes held[] and nr_held, adding no atomics to the
fastpath. Trylocks skip IRQ and irqs_disabled() callers (e.g. BPF
helpers that defer unlock to irq_work). A blocking reader takes a
slot before queueing, because rwsem_mark_wake() grants the lock before
the reader runs; an interrupted wait drops it under wait_lock. Slots
are cleared before sem->count is released, so they never outlive their
lock.
down_read_non_owner() drops its slot immediately, and callers handing
a held rwsem to another task must call rwsem_boost_untrack() first.
A v7.4-rc5 audit found one plain rwsem released cross-task (btrfs
io_uring encoded reads, handled next); all *_non_owner() releases
already pair with untracked acquisitions.
Add CONFIG_RWSEM_SCHED_BOOST (default n) and rwsem_sched_boost=.
Signed-off-by: Peng Huang <penghuang@google.com>
---
.../admin-guide/kernel-parameters.txt | 5 +
include/linux/rwsem.h | 14 ++
include/linux/sched.h | 25 +++
kernel/Kconfig.locks | 15 ++
kernel/locking/rwsem.c | 179 ++++++++++++++++--
5 files changed, 222 insertions(+), 16 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 176648941aec..2c5207dd9c53 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -6778,6 +6778,11 @@ Kernel parameters
rw [KNL] Mount root device read-write on boot
+ rwsem_sched_boost=
+ [KNL] Enable or disable rwsem scheduler priority
+ boosting when CONFIG_RWSEM_SCHED_BOOST=y.
+ Defaults to 1 (enabled). Format: <bool>
+
S [KNL] Run init in single mode
s390_iommu= [HW,S390]
diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
index 6a1a7bae5f81..2df33131bb5f 100644
--- a/include/linux/rwsem.h
+++ b/include/linux/rwsem.h
@@ -327,8 +327,22 @@ extern void up_read_non_owner(struct rw_semaphore *sem) __releases_shared(sem);
# define down_write_nest_lock(sem, nest_lock) down_write(sem)
# define down_write_nested(sem, subclass) down_write(sem)
# define down_write_killable_nested(sem, subclass) down_write_killable(sem)
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+void down_read_non_owner(struct rw_semaphore *sem) __acquires_shared(sem);
+void up_read_non_owner(struct rw_semaphore *sem) __releases_shared(sem);
+#else
# define down_read_non_owner(sem) down_read(sem)
# define up_read_non_owner(sem) up_read(sem)
#endif
+#endif
+
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+void rwsem_boost_untrack(struct rw_semaphore *sem);
+bool rwsem_boost_set(bool enable);
+#else
+static inline void rwsem_boost_untrack(struct rw_semaphore *sem)
+{
+}
+#endif
#endif /* _LINUX_RWSEM_H */
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 7cd8df8f463c..a98be7c4537d 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -78,6 +78,7 @@ struct rcu_node;
struct reclaim_state;
struct root_domain;
struct rq;
+struct rw_semaphore;
struct sched_attr;
struct sched_dl_entity;
struct seq_file;
@@ -833,6 +834,26 @@ struct task_ipi_mask {
struct task_ipi_mask { };
#endif
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+#define RWSEM_SCHED_BOOST_SLOTS 4
+
+/**
+ * struct rwsem_sched_boost - per-task rw_semaphore boost state
+ * @nr_held: number of non-NULL entries in @held
+ * @held: rw_semaphores held, one slot per tracked acquisition
+ *
+ * Only the task itself writes @nr_held and @held, so the lock
+ * fastpaths need no atomics. A slot is filled before the rwsem can
+ * be granted (a blocking reader fills it before it queues) and cleared
+ * before the rwsem is released or the wait is abandoned, so a
+ * non-NULL slot always names an rwsem the task holds or waits for.
+ */
+struct rwsem_sched_boost {
+ u8 nr_held;
+ struct rw_semaphore *held[RWSEM_SCHED_BOOST_SLOTS];
+};
+#endif
+
struct task_struct {
#ifdef CONFIG_THREAD_INFO_IN_TASK
/*
@@ -927,6 +948,10 @@ struct task_struct {
struct uclamp_se uclamp[UCLAMP_CNT];
#endif
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+ struct rwsem_sched_boost rwsem_boost;
+#endif
+
struct sched_statistics stats;
#ifdef CONFIG_PREEMPT_NOTIFIERS
diff --git a/kernel/Kconfig.locks b/kernel/Kconfig.locks
index 1c6423aafcd4..8e67fe0b6f8d 100644
--- a/kernel/Kconfig.locks
+++ b/kernel/Kconfig.locks
@@ -279,3 +279,18 @@ config ARCH_HAS_MMIOWB
config MMIOWB
def_bool y if ARCH_HAS_MMIOWB
depends on SMP
+
+config RWSEM_SCHED_BOOST
+ bool "Scheduler priority boost for contended rw_semaphores"
+ depends on !PREEMPT_RT
+ help
+ Track held rw_semaphores per task and temporarily raise the load
+ weight of fair holders that block higher-priority waiters, to
+ mitigate reader and writer priority inversion.
+
+ Every rwsem lock and unlock records or clears a per-task slot
+ (no atomic operations), task_struct grows by 56 bytes and each
+ runqueue by 16. Boot with rwsem_sched_boost=0 to turn the
+ tracking off.
+
+ If unsure, say N.
diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
index b9c180ac1eee..798a8db7636c 100644
--- a/kernel/locking/rwsem.c
+++ b/kernel/locking/rwsem.c
@@ -28,8 +28,131 @@
#include <linux/rwsem.h>
#include <linux/atomic.h>
#include <linux/hung_task.h>
+#include <linux/jump_label.h>
#include <trace/events/lock.h>
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+static DEFINE_STATIC_KEY_TRUE(rwsem_boost_key);
+
+/**
+ * rwsem_boost_set - Enable or disable rwsem holder boosting
+ * @enable: track held rwsems and publish waiter priorities if true
+ *
+ * Return: the previous state.
+ */
+bool rwsem_boost_set(bool enable)
+{
+ bool was_enabled = static_key_enabled(&rwsem_boost_key);
+
+ if (enable)
+ static_branch_enable(&rwsem_boost_key);
+ else
+ static_branch_disable(&rwsem_boost_key);
+ return was_enabled;
+}
+
+static int __init rwsem_boost_cmdline(char *str)
+{
+ bool enable;
+
+ if (kstrtobool(str, &enable) != 0)
+ return 0;
+ rwsem_boost_set(enable);
+ return 1;
+}
+__setup("rwsem_sched_boost=", rwsem_boost_cmdline);
+
+/*
+ * Record @sem in a free held[] slot so the scheduler can find the
+ * holders of a contended rwsem. Blocking acquisitions call this
+ * directly: might_sleep() guarantees task context.
+ */
+static inline void rwsem_boost_track(struct rw_semaphore *sem)
+{
+ struct rwsem_sched_boost *b = ¤t->rwsem_boost;
+ int i;
+
+ if (!static_branch_likely(&rwsem_boost_key))
+ return;
+
+ /* rwsems do not recurse; a recursive trylock takes a slot. */
+ for (i = 0; i < RWSEM_SCHED_BOOST_SLOTS; i++) {
+ if (!b->held[i]) {
+ WRITE_ONCE(b->held[i], sem);
+ WRITE_ONCE(b->nr_held, b->nr_held + 1);
+ return;
+ }
+ }
+}
+
+/*
+ * Trylocks can run in IRQ context or with IRQs disabled, e.g. BPF
+ * helpers that defer the unlock to irq_work and release the lock
+ * with up_read_non_owner(). Leave those untracked.
+ */
+static inline void rwsem_boost_track_trylock(struct rw_semaphore *sem)
+{
+ if (unlikely(!in_task()))
+ return;
+ if (unlikely(irqs_disabled()))
+ return;
+ rwsem_boost_track(sem);
+}
+
+/*
+ * Clear @sem from held[] before sem->count is released, so that once
+ * another task can acquire or free @sem, scheduler inspection no
+ * longer finds it. Unlike tracking, ignore rwsem_boost_key and
+ * irqs_disabled() so no slot outlives its lock; an acquire that was
+ * not tracked can only end tracking early (losing boost).
+ */
+static inline void __rwsem_boost_untrack(struct rw_semaphore *sem)
+{
+ struct rwsem_sched_boost *b = ¤t->rwsem_boost;
+ u8 nr = b->nr_held;
+ int i;
+
+ if (nr == 0)
+ return;
+ /* IRQ/softirq unlocks pair with untracked trylocks. */
+ if (unlikely(!in_task()))
+ return;
+
+ for (i = 0; i < RWSEM_SCHED_BOOST_SLOTS; i++) {
+ if (b->held[i] == sem) {
+ WRITE_ONCE(b->held[i], NULL);
+ WRITE_ONCE(b->nr_held, nr - 1);
+ return;
+ }
+ }
+}
+
+/**
+ * rwsem_boost_untrack - Stop tracking @sem as held by current
+ * @sem: rwsem that current holds
+ *
+ * Call before another context can release @sem, e.g. an io_uring
+ * completion that runs in a different task. Task context only.
+ */
+void rwsem_boost_untrack(struct rw_semaphore *sem)
+{
+ __rwsem_boost_untrack(sem);
+}
+EXPORT_SYMBOL_GPL(rwsem_boost_untrack);
+#else
+static inline void rwsem_boost_track(struct rw_semaphore *sem)
+{
+}
+
+static inline void rwsem_boost_track_trylock(struct rw_semaphore *sem)
+{
+}
+
+static inline void __rwsem_boost_untrack(struct rw_semaphore *sem)
+{
+}
+#endif
+
#ifndef CONFIG_PREEMPT_RT
#include "lock_events.h"
@@ -255,6 +378,7 @@ static inline bool rwsem_read_trylock(struct rw_semaphore *sem, long *cntp)
if (!(*cntp & RWSEM_READ_FAILED_MASK)) {
rwsem_set_reader_owned(sem);
+ rwsem_boost_track(sem);
return true;
}
@@ -1021,6 +1145,12 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
struct rwsem_waiter waiter, *first;
DEFINE_WAKE_Q(wake_q);
+ /*
+ * Track @sem before queueing, as rwsem_mark_wake() grants the
+ * lock to queued readers before they run; out_nolock untracks.
+ */
+ rwsem_boost_track(sem);
+
/*
* To prevent a constant stream of readers from starving a sleeping
* writer, don't attempt optimistic lock stealing if the lock is
@@ -1125,6 +1255,8 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
return sem;
out_nolock:
+ /* Under wait_lock: drop the slot before @sem may be freed. */
+ __rwsem_boost_untrack(sem);
rwsem_del_wake_waiter(sem, &waiter, &wake_q);
__set_current_state(TASK_RUNNING);
lockevent_inc(rwsem_rlock_fail);
@@ -1327,6 +1459,7 @@ static inline int __down_read_trylock(struct rw_semaphore *sem)
if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
tmp + RWSEM_READER_BIAS)) {
rwsem_set_reader_owned(sem);
+ rwsem_boost_track_trylock(sem);
ret = 1;
break;
}
@@ -1344,9 +1477,14 @@ static __always_inline int __down_write_common(struct rw_semaphore *sem, int sta
preempt_disable();
if (unlikely(!rwsem_write_trylock(sem))) {
- if (IS_ERR(rwsem_down_write_slowpath(sem, state)))
+ sem = rwsem_down_write_slowpath(sem, state);
+ if (IS_ERR(sem)) {
ret = -EINTR;
+ goto out;
+ }
}
+ rwsem_boost_track(sem);
+out:
preempt_enable();
return ret;
}
@@ -1368,6 +1506,8 @@ static inline int __down_write_trylock(struct rw_semaphore *sem)
preempt_disable();
DEBUG_RWSEMS_WARN_ON(sem->magic != sem, sem);
ret = rwsem_write_trylock(sem);
+ if (ret == 1)
+ rwsem_boost_track_trylock(sem);
preempt_enable();
return ret;
@@ -1384,6 +1524,7 @@ static inline void __up_read(struct rw_semaphore *sem)
DEBUG_RWSEMS_WARN_ON(!is_rwsem_reader_owned(sem), sem);
preempt_disable();
+ __rwsem_boost_untrack(sem);
rwsem_clear_reader_owned(sem);
tmp = atomic_long_add_return_release(-RWSEM_READER_BIAS, &sem->count);
DEBUG_RWSEMS_WARN_ON(tmp < 0, sem);
@@ -1413,6 +1554,7 @@ static inline void __up_write(struct rw_semaphore *sem)
!rwsem_test_oflags(sem, RWSEM_NONSPINNABLE), sem);
preempt_disable();
+ __rwsem_boost_untrack(sem);
rwsem_clear_owner(sem);
tmp = atomic_long_fetch_add_release(-RWSEM_WRITER_LOCKED, &sem->count);
if (unlikely(tmp & RWSEM_FLAG_WAITERS)) {
@@ -1735,21 +1877,6 @@ void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
}
EXPORT_SYMBOL(_down_write_nest_lock);
-void down_read_non_owner(struct rw_semaphore *sem)
- __no_context_analysis
-{
- might_sleep();
- __down_read(sem);
- /*
- * The owner value for a reader-owned lock is mostly for debugging
- * purpose only and is not critical to the correct functioning of
- * rwsem. So it is perfectly fine to set it in a preempt-enabled
- * context here.
- */
- __rwsem_set_reader_owned(sem, NULL);
-}
-EXPORT_SYMBOL(down_read_non_owner);
-
void down_write_nested(struct rw_semaphore *sem, int subclass)
__no_context_analysis
{
@@ -1775,6 +1902,26 @@ int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
}
EXPORT_SYMBOL(down_write_killable_nested);
+#endif
+
+#if defined(CONFIG_DEBUG_LOCK_ALLOC) || defined(CONFIG_RWSEM_SCHED_BOOST)
+void down_read_non_owner(struct rw_semaphore *sem)
+ __no_context_analysis
+{
+ might_sleep();
+ __down_read(sem);
+ /* Another context releases it: leave no slot behind. */
+ rwsem_boost_untrack(sem);
+ /*
+ * The owner value for a reader-owned lock is mostly for debugging
+ * purpose only and is not critical to the correct functioning of
+ * rwsem. So it is perfectly fine to set it in a preempt-enabled
+ * context here.
+ */
+ __rwsem_set_reader_owned(sem, NULL);
+}
+EXPORT_SYMBOL(down_read_non_owner);
+
void up_read_non_owner(struct rw_semaphore *sem)
__no_context_analysis
{
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [RFC PATCH v2 0/6] rwsem: priority boost for holders blocking high-priority waiters
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 20:43 ` Kayra Cizmeci
2026-12-01 21:10 ` Peng Huang
1 sibling, 1 reply; 8+ messages in thread
From: Kayra Cizmeci @ 2026-12-01 20:43 UTC (permalink / raw)
To: Peng Huang
Cc: Peter Zijlstra, Ingo Molnar, Will Deacon, Boqun Feng, Juri Lelli,
Vincent Guittot, Waiman Long, Dietmar Eggemann, Steven Rostedt,
Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak,
John Stultz, Suren Baghdasaryan, linux-kernel, linux-mm,
Sebastian Andrzej Siewior, Clark Williams, linux-rt-devel
Hi Peng,
On Mon, Nov 30, 2026 at 10:27:27PM -0800, Peng Huang wrote:
> Hi,
>
> High-priority threads (e.g. UI/render) can stall for tens to hundreds
> of milliseconds on shared rw_semaphores like mmap_lock or i_rwsem when
> preempted low-priority or SCHED_IDLE readers block a writer. Outside
> PREEMPT_RT, rwsems have no PI, Proxy Execution covers only mutexes,
> and read-owners are anonymous. This hits both servers (/proc/pid/
> cmdline, background MADV_COLD) and phones (nice -10 UI vs. background
> MADV_PAGEOUT on small cores, where we found it).
>
> This RFC raises the fair load weight of contended rwsem holders:
> - Patches 1-2: per-task tracking of up to 4 held rwsems (no atomics);
> btrfs untracks i_rwsem before cross-task io_uring completion.
> - Patch 3: pack top waiter priority into sem->count bits 3..8 (no
> struct rw_semaphore growth).
> - Patches 4-5: apply boost in set_load_weight() (p->prio and PI
> untouched); re-check queued holders on enqueue, preemption, and
> tick; drop on unlock.
> - Patch 6: KUnit suite (16 cases, 20 tests).
>
> Opt-in via CONFIG_RWSEM_SCHED_BOOST; disable at boot with
> rwsem_sched_boost=0.
>
> Questions for this RFC
> ----------------------
> - Is per-task tracking + weight boost acceptable for readers, or
> should this wait for Proxy Execution to cover rwsems?
> - Track readers and writers, or readers only (leaving writers to PE)?
> - Raw pointer slots (+56 B/task, explicit untrack) or a hash table?
> - Reweight via set_load_weight() (stays in cgroup share, moves PELT)
> or a floor on flattened EEVDF weight (crosses cgroups, skips PELT)?
>
> Known limits
> ------------
> - Weight is not PI; only runnable holders benefit (not I/O-blocked).
> Queued holders wait up to one tick; at most 4 rwsems/task tracked.
> - Waiter priority is sampled on queue (so rwsem-to-rwsem propagates
> when a boosted holder queues, but not across mutexes or futexes);
> concurrent readers share the boost.
> - Under FAIR_GROUP_SCHED, boost stays within the holder's cgroup
> share; a boosted SCHED_IDLE holder still yields to non-idle wakeups.
> - Under Proxy Execution, a fair task running for a non-fair donor is
> linked only on its next enqueue or preemption.
>
> Results (mean high-priority waiter latency)
> -------------------------------------------
> - KUnit (UML, nice +19 reader + 4 nice 0 hogs, nice -10 writer):
> 1813 -> 50 ms (-97.2%, median of 3; control 1.77-1.81 s).
> - QEMU x86_64 -smp 4 (TCG), mmap_lock, same setup (10 runs):
> 445.8 -> 8.7 ms within one image (-98.1%, max 14.9 ms, 0/200 > 16 ms;
> BASE 394.4 ms, TCG shifts up to 20% across images).
> - ARM64 phone (older 6.18 backport), mmap_lock, nice -10 vs. 7 hogs:
> 46.95 -> 8.89 ms (-81.1%) unpinned, 229.5 -> 19.4 ms (-91.5%) mid.
>
> Costs and non-results
> ---------------------
> - UML uncontended fastpath (boost=0 -> 1, median of 3): read 16.9 ->
> 20.2, write 17.1 -> 19.8, nested read 16.9 -> 21.8 ns/op; boosted
> holder cycling an unrelated rwsem: 28.5 ns/op.
> - Tick scan: walks up to 16 queued holders per tick under rq->lock
> (round-robin, up to 4 sem->count reads each); not measured alone.
> - Waiter prio rescan: O(1) when waiters share priority; losing a sole
> top waiter rescans remaining waiters under wait_lock (O(waiters)).
> - QEMU (boost=0 -> 1, 10 runs): madvise +2.4%, mprotect +1.4% (+0.4%
> median); equal-priority contention -4.3% mean, -5.6% median
> (196-240 ms on vs. 195-268 ms off).
> - ARM64 (same backport): madvise -2.2%, mprotect +0.2%; boot-time
> hackbench +6.6% and pipe +0.2% (settled -8.7% and -0.7%);
> equal-priority contention -10.6% mid-pinned, -2.8% mean / +12.1%
> median unpinned.
...
> - Browser warm launch from zRAM (same backport, 22 vs. 23 runs):
> +2.4% (p=0.45); holders spent 91-96% of inversion in D state (I/O).
> - Reader count loses 1 bit (22 bits on 32-bit, > PID_MAX_LIMIT).
> Memory: +56 B/task_struct, +16 B/rq.
Well if that is the case I can't see increasing the weight would help
atleast on this browser thing.
Even Proxy Exec. wouldn't help that much. You can't make a sleeping
task run earlier while it is sleeping.
But aside from that if we really want this, I think it would be better
to try to make this compatible with Proxy Exec. rather than
trying to implement a new mechanism, no :-)?
Thanks!
Kayra
^ permalink raw reply [flat|nested] 8+ messages in thread