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 5/6] sched/fair: Boost rwsem holders with contended high-priority waiters
Date: Mon, 30 Nov 2026 22:27:32 -0800 [thread overview]
Message-ID: <20261201062738.1047159-6-penghuang@google.com> (raw)
In-Reply-To: <20261201062738.1047159-1-penghuang@google.com>
A preempted fair task holding an rwsem keeps higher-priority waiters
blocked until it runs again, and for readers the waiters cannot even
tell whom to boost.
Track runnable fair holders on per-CPU rq->rwsem_holders when enqueued
or preempted, and refresh their boost then and at each tick, all
within fair.c so uncontended lock paths never touch the list. A tick
that raises a queued holder's boost reschedules current if the holder
now outranks it and is eligible.
Another task's held[] is read only under its rq->lock while it is not
running elsewhere, so its rwsems cannot be released meanwhile.
Holders unlink on any dequeue (including delayed dequeue, throttling,
and class switches). Tasks entering fair, mutex-blocked Proxy
Execution donors, and remotely running tasks are linked without
reweighting so the tick boosts them once runnable. Under Proxy
Execution, put_prev_task() also links a fair execution context when
its donor is fair; under a non-fair donor, it waits for its next
enqueue or preemption.
Each tick scans at most 16 queued holders (rotating the list for
fairness) to keep rq->lock work bounded, so a queued holder normally
waits up to one tick.
Signed-off-by: Peng Huang <penghuang@google.com>
---
include/linux/sched.h | 8 +-
init/init_task.c | 1 +
kernel/sched/core.c | 2 +
kernel/sched/fair.c | 229 ++++++++++++++++++++++++++++++++++++++++++
kernel/sched/sched.h | 17 ++++
5 files changed, 255 insertions(+), 2 deletions(-)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 92e32f58876e..8b2889ce7952 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -842,6 +842,7 @@ struct task_ipi_mask { };
* @nr_held: number of non-NULL entries in @held
* @prio: fair priority lent by waiters on @held, MAX_PRIO if none
* @held: rw_semaphores held, one slot per tracked acquisition
+ * @rq_node: entry on rq->rwsem_holders, protected by rq->lock
*
* Only the task itself writes @nr_held and @held, so the lock
* fastpaths need no atomics. A slot is filled before the rwsem can
@@ -849,13 +850,16 @@ struct task_ipi_mask { };
* 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.
*
- * @prio is written under rq->lock; the task itself also reads it
- * locklessly.
+ * Other tasks read @held only under the task's rq->lock while it is
+ * not running, so none of those rwsems can be released or freed
+ * meanwhile. @prio is written under rq->lock; the task itself also
+ * reads it locklessly.
*/
struct rwsem_sched_boost {
u8 nr_held;
u8 prio;
struct rw_semaphore *held[RWSEM_SCHED_BOOST_SLOTS];
+ struct list_head rq_node;
};
#endif
diff --git a/init/init_task.c b/init/init_task.c
index 214d3f2ef8a0..507540e50657 100644
--- a/init/init_task.c
+++ b/init/init_task.c
@@ -137,6 +137,7 @@ struct task_struct init_task __aligned(L1_CACHE_BYTES) = {
#ifdef CONFIG_RWSEM_SCHED_BOOST
.rwsem_boost = {
.prio = MAX_PRIO,
+ .rq_node = LIST_HEAD_INIT(init_task.rwsem_boost.rq_node),
},
#endif
#ifdef CONFIG_SMP
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 56e32d4ffc3c..288a8efcfb9d 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1570,6 +1570,7 @@ static inline void rwsem_boost_fork(struct task_struct *p)
memset(&p->rwsem_boost, 0, sizeof(p->rwsem_boost));
p->rwsem_boost.prio = MAX_PRIO;
+ INIT_LIST_HEAD(&p->rwsem_boost.rq_node);
/*
* dup_task_struct() copied current->se.load without rq->lock;
@@ -9168,6 +9169,7 @@ void __init sched_init(void)
rq->max_idle_balance_cost = sysctl_sched_migration_cost;
INIT_LIST_HEAD(&rq->cfs_tasks);
+ rwsem_boost_rq_init(rq);
rq_attach_root(rq, &def_root_domain);
#ifdef CONFIG_NO_HZ_COMMON
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index f755ee810ce2..08fbab673893 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -8270,6 +8270,27 @@ static inline bool rwsem_boost_should_resched_fair(struct rq *rq,
task_current_donor(rq, p) && rq->cfs.h_nr_queued > 1;
}
+/* Link runnable rwsem holder @p onto rq->rwsem_holders. */
+static inline void rwsem_boost_rq_add(struct rq *rq, struct task_struct *p)
+{
+ if (list_empty(&p->rwsem_boost.rq_node))
+ list_add_tail(&p->rwsem_boost.rq_node, &rq->rwsem_holders);
+}
+
+/* Unlink @p from rq->rwsem_holders when leaving the runqueue. */
+static inline void rwsem_boost_rq_del(struct task_struct *p)
+{
+ if (!list_empty(&p->rwsem_boost.rq_node))
+ list_del_init(&p->rwsem_boost.rq_node);
+}
+
+/* True if @p holds an rwsem and could still be boosted by the tick. */
+static inline bool rwsem_boost_needs_scan(const struct task_struct *p)
+{
+ return READ_ONCE(p->rwsem_boost.nr_held) != 0 &&
+ rwsem_boost_has_headroom(p);
+}
+
/*
* Refresh running @p's boost. Return true if it lost the boost while
* other fair tasks wait, so the caller should reschedule.
@@ -8279,6 +8300,8 @@ static bool rwsem_boost_refresh_curr_fair(struct rq *rq,
{
int old_prio = rwsem_boost_effective_prio(p);
+ if (!rwsem_boost_needs_scan(p))
+ rwsem_boost_rq_del(p);
if (!rwsem_boost_reweight_fair(rq, p))
return false;
return rwsem_boost_should_resched_fair(rq, p, old_prio);
@@ -8290,9 +8313,206 @@ void rwsem_boost_drop_fair(struct rq *rq, struct task_struct *p)
if (rwsem_boost_refresh_curr_fair(rq, p))
rwsem_boost_resched_fair(rq, p);
}
+
+/* True if @p holds any rwsem or still carries an active boost. */
+static inline bool rwsem_boost_holding_or_active(const struct task_struct *p)
+{
+ return unlikely(READ_ONCE(p->rwsem_boost.nr_held) != 0 ||
+ rwsem_boost_active(p));
+}
+
+/*
+ * True if @p is runnable on_rq, not sleeping-delayed, not proxy-blocked
+ * on a mutex, and not running remotely. Test se.on_rq rather than
+ * task_on_rq_queued(): sched_change_begin() dequeues the task before
+ * put_prev_task() without clearing p->on_rq.
+ */
+static inline bool rwsem_boost_can_inspect_fair(struct rq *rq,
+ struct task_struct *p)
+{
+ if (!p->se.on_rq)
+ return false;
+ if (p->se.sched_delayed)
+ return false;
+ if (task_is_blocked(p))
+ return false;
+ if (p != current && task_on_cpu(rq, p))
+ return false;
+ return true;
+}
+
+/*
+ * Link queued holder @p onto rq->rwsem_holders without reweighting it:
+ * a class switch before attach_task_cfs_rq(), a mutex-blocked proxy
+ * donor, or a proxy execution context that may run elsewhere. Reads
+ * only @p's nr_held, never held[].
+ */
+static inline void rwsem_boost_link_fair(struct rq *rq, struct task_struct *p)
+{
+ if (!p->se.on_rq)
+ return;
+ if (p->se.sched_delayed)
+ return;
+ if (!rwsem_boost_needs_scan(p))
+ return;
+ rwsem_boost_rq_add(rq, p);
+}
+
+/* Sync @p on rq->rwsem_holders and refresh its boosted load weight. */
+static bool rwsem_boost_update_fair(struct rq *rq, struct task_struct *p)
+{
+ if (rwsem_boost_needs_scan(p))
+ rwsem_boost_rq_add(rq, p);
+ else
+ rwsem_boost_rq_del(p);
+ return rwsem_boost_reweight_fair(rq, p);
+}
+
+/* True if boosted, queued fair task @p should preempt @curr. */
+static inline bool
+rwsem_boost_should_preempt_fair(struct rq *rq, struct task_struct *p,
+ const struct task_struct *curr)
+{
+ if (p->rwsem_boost.prio >= rwsem_boost_effective_prio(curr))
+ return false;
+ /* Only task entities are on the EEVDF timeline, &rq->cfs. */
+ return entity_eligible(&rq->cfs, &p->se);
+}
+
+/*
+ * Track held-rwsem task @p on enqueue and refresh its load weight;
+ * called at the end of enqueue_task_fair(), once PELT and a delayed
+ * requeue are settled.
+ */
+static inline void rwsem_boost_enqueue_fair(struct rq *rq,
+ struct task_struct *p,
+ int flags)
+{
+ if (!rwsem_boost_holding_or_active(p))
+ return;
+
+ /*
+ * A class switch attaches PELT only in switched_to_fair(): link
+ * the task now and let the tick apply the boost.
+ */
+ if (flags & ENQUEUE_CLASS) {
+ rwsem_boost_link_fair(rq, p);
+ return;
+ }
+ /*
+ * Link a mutex-blocked proxy donor or remotely running task
+ * without reading held[], so the tick catches it once runnable.
+ */
+ if (!rwsem_boost_can_inspect_fair(rq, p)) {
+ rwsem_boost_link_fair(rq, p);
+ return;
+ }
+
+ rwsem_boost_update_fair(rq, p);
+}
+
+/*
+ * Under PROXY_EXEC with a fair donor, @prev is the donor and rq->curr
+ * ran on its behalf. Link that execution context if it took an rwsem
+ * while running, so the tick can boost it once it waits for the CPU.
+ * Non-fair donors dispatch put_prev_task() through their own class, so
+ * a fair proxy context waits for its next enqueue or own preemption.
+ */
+static inline void rwsem_boost_put_proxy_fair(struct rq *rq,
+ struct task_struct *prev)
+{
+ struct task_struct *exec = rq->curr;
+
+ if (!sched_proxy_exec())
+ return;
+ if (exec == prev)
+ return;
+ if (exec->sched_class != &fair_sched_class)
+ return;
+ rwsem_boost_link_fair(rq, exec);
+}
+
+/*
+ * Track a preempted task that acquired an rwsem while running (so
+ * enqueue_task_fair() never saw nr_held > 0) and apply any boost.
+ */
+static inline void rwsem_boost_put_prev_fair(struct rq *rq,
+ struct task_struct *prev)
+{
+ rwsem_boost_put_proxy_fair(rq, prev);
+
+ if (!rwsem_boost_holding_or_active(prev))
+ return;
+ /*
+ * A PROXY_EXEC mutex-blocked task stays on_rq: link it without
+ * reweighting, so the tick sees it after an in-place wakeup.
+ */
+ if (task_is_blocked(prev)) {
+ rwsem_boost_link_fair(rq, prev);
+ return;
+ }
+ if (rwsem_boost_can_inspect_fair(rq, prev))
+ rwsem_boost_update_fair(rq, prev);
+}
+
+#define RWSEM_BOOST_TICK_BUDGET 16
+
+/* Refresh boost for running @curr and queued rwsem holders on tick. */
+static void rwsem_boost_tick_fair(struct rq *rq, struct task_struct *curr)
+{
+ int budget = RWSEM_BOOST_TICK_BUDGET;
+ struct task_struct *p, *next;
+ bool resched = false;
+
+ /*
+ * Refresh running @curr without linking it: preemption links
+ * it if it is preempted while still holding an rwsem.
+ */
+ if (rwsem_boost_holding_or_active(curr) &&
+ rwsem_boost_can_inspect_fair(rq, curr))
+ resched = rwsem_boost_refresh_curr_fair(rq, curr);
+
+ list_for_each_entry_safe(p, next, &rq->rwsem_holders,
+ rwsem_boost.rq_node) {
+ int old_prio;
+
+ if (budget-- == 0) {
+ /* Resume from @p next tick; keeps rq->lock O(1). */
+ list_rotate_to_front(&p->rwsem_boost.rq_node,
+ &rq->rwsem_holders);
+ break;
+ }
+
+ if (p == curr) {
+ if (!rwsem_boost_needs_scan(curr))
+ rwsem_boost_rq_del(curr);
+ continue;
+ }
+ if (!rwsem_boost_can_inspect_fair(rq, p))
+ continue;
+
+ old_prio = p->rwsem_boost.prio;
+ if (rwsem_boost_update_fair(rq, p) &&
+ p->rwsem_boost.prio < old_prio &&
+ !task_current(rq, p) &&
+ rwsem_boost_should_preempt_fair(rq, p, curr))
+ resched = true;
+ }
+
+ if (resched)
+ rwsem_boost_resched_fair(rq, curr);
+}
#else
static inline void rwsem_boost_clear_fair(struct rq *rq,
struct task_struct *p) { }
+static inline void rwsem_boost_rq_del(struct task_struct *p) { }
+static inline void rwsem_boost_enqueue_fair(struct rq *rq,
+ struct task_struct *p,
+ int flags) { }
+static inline void rwsem_boost_put_prev_fair(struct rq *rq,
+ struct task_struct *prev) { }
+static inline void rwsem_boost_tick_fair(struct rq *rq,
+ struct task_struct *curr) { }
#endif
/*
@@ -8326,6 +8546,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
if (flags & ENQUEUE_DELAYED) {
requeue_delayed_entity(cfs_rq, se);
+ rwsem_boost_enqueue_fair(rq, p, flags);
return;
}
@@ -8383,6 +8604,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
assert_list_leaf_cfs_rq(rq);
hrtick_update(rq);
+ rwsem_boost_enqueue_fair(rq, p, flags);
}
static void dequeue_hierarchy(struct task_struct *p, int flags)
@@ -8514,6 +8736,9 @@ static bool __dequeue_task(struct rq *rq, struct task_struct *p, int flags)
*/
static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
{
+ /* Unlink first: throttled and delayed dequeues return early. */
+ rwsem_boost_rq_del(p);
+
if (task_is_throttled(p)) {
dequeue_throttled_task(p, flags);
return true;
@@ -10516,6 +10741,8 @@ static void put_prev_task_fair(struct rq *rq, struct task_struct *prev, struct t
cfs_rq->curr = NULL;
if (se->on_rq)
__enqueue_entity(cfs_rq, se);
+
+ rwsem_boost_put_prev_fair(rq, prev);
}
/*
@@ -15505,6 +15732,8 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int hrtick)
update_misfit_status(curr, rq);
check_update_overutilized_status(task_rq(curr));
+ rwsem_boost_tick_fair(rq, curr);
+
task_tick_core(rq, curr);
}
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 058311c19cc0..3d7bf08223bc 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1336,6 +1336,14 @@ struct rq {
int online;
struct list_head cfs_tasks;
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+ /*
+ * Fair tasks holding rwsems, protected by rq->lock. They leave
+ * on dequeue and class switch; tasks that stopped holding are
+ * dropped lazily by the tick.
+ */
+ struct list_head rwsem_holders;
+#endif
struct sched_avg avg_rt;
struct sched_avg avg_dl;
@@ -4450,6 +4458,11 @@ static inline int rwsem_boost_effective_prio(const struct task_struct *p)
int rwsem_boost_calc_prio(struct task_struct *p);
void rwsem_boost_drop_fair(struct rq *rq, struct task_struct *p);
+
+static inline void rwsem_boost_rq_init(struct rq *rq)
+{
+ INIT_LIST_HEAD(&rq->rwsem_holders);
+}
#else
static inline bool rwsem_boost_active(const struct task_struct *p)
{
@@ -4460,6 +4473,10 @@ static inline int rwsem_boost_effective_prio(const struct task_struct *p)
{
return p->static_prio;
}
+
+static inline void rwsem_boost_rq_init(struct rq *rq)
+{
+}
#endif
#endif /* _KERNEL_SCHED_SCHED_H */
--
2.56.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-12-01 6:28 UTC|newest]
Thread overview: 7+ 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 6:27 ` [RFC PATCH v2 3/6] locking/rwsem: Publish the top waiter priority Peng Huang
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 ` Peng Huang [this message]
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-6-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