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 4/6] sched: Apply rwsem holder boost to the fair load weight
Date: Mon, 30 Nov 2026 22:27:31 -0800	[thread overview]
Message-ID: <20261201062738.1047159-5-penghuang@google.com> (raw)
In-Reply-To: <20261201062738.1047159-1-penghuang@google.com>

The previous patch publishes how urgent an rwsem's waiters are, but a
preempted holder still runs at its own weight and keeps them blocked.

Add p->rwsem_boost.prio (MAX_PRIO when none) and feed it into
set_load_weight().  p->prio, static_prio and rt_mutex PI are never
touched: a boost only raises the fair load weight (capped at nice -20)
and never leaves the fair class.

rwsem_boost_calc_prio() scans the held slots via
rwsem_top_waiter_prio() and boosts only when a waiter outranks the
holder.  On unlock, downgrade_write(), or a signal-interrupted read
wait, a boosted task recomputes without locks and takes rq->lock only
if its boost dropped, rescheduling if other fair tasks are runnable on
its CPU.  Leaving the fair class clears the boost; queued waiters also
sample their own boost so it propagates across rwsem chains.

The next patch adds the fair-class paths that apply the boost.

Signed-off-by: Peng Huang <penghuang@google.com>
---
 include/linux/rwsem.h  |  1 +
 include/linux/sched.h  |  5 +++
 init/init_task.c       |  5 +++
 kernel/locking/rwsem.c | 37 ++++++++++++++--
 kernel/sched/core.c    | 90 ++++++++++++++++++++++++++++++++++++++-
 kernel/sched/fair.c    | 97 ++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/sched.h   | 42 ++++++++++++++++++
 7 files changed, 272 insertions(+), 5 deletions(-)

diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
index d8a49170efd2..30dfa2920dfa 100644
--- a/include/linux/rwsem.h
+++ b/include/linux/rwsem.h
@@ -370,6 +370,7 @@ void up_read_non_owner(struct rw_semaphore *sem) __releases_shared(sem);
 #ifdef CONFIG_RWSEM_SCHED_BOOST
 void rwsem_boost_untrack(struct rw_semaphore *sem);
 bool rwsem_boost_set(bool enable);
+void rwsem_boost_drop(void);
 #else
 static inline void rwsem_boost_untrack(struct rw_semaphore *sem)
 {
diff --git a/include/linux/sched.h b/include/linux/sched.h
index a98be7c4537d..92e32f58876e 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -840,6 +840,7 @@ struct task_ipi_mask { };
 /**
  * struct rwsem_sched_boost - per-task rw_semaphore boost state
  * @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
  *
  * Only the task itself writes @nr_held and @held, so the lock
@@ -847,9 +848,13 @@ struct task_ipi_mask { };
  * 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.
+ *
+ * @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];
 };
 #endif
diff --git a/init/init_task.c b/init/init_task.c
index ce7c2b07d855..214d3f2ef8a0 100644
--- a/init/init_task.c
+++ b/init/init_task.c
@@ -134,6 +134,11 @@ struct task_struct init_task __aligned(L1_CACHE_BYTES) = {
 		.time_slice	= RR_TIMESLICE,
 	},
 	.tasks		= LIST_HEAD_INIT(init_task.tasks),
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+	.rwsem_boost	= {
+		.prio		= MAX_PRIO,
+	},
+#endif
 #ifdef CONFIG_SMP
 	.pushable_tasks	= PLIST_NODE_INIT(init_task.pushable_tasks, MAX_PRIO),
 #endif
diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
index 6d9b78a3d33a..33fda82c2acc 100644
--- a/kernel/locking/rwsem.c
+++ b/kernel/locking/rwsem.c
@@ -104,7 +104,9 @@ static inline void rwsem_boost_track_trylock(struct rw_semaphore *sem)
  * 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).
+ * not tracked can only end tracking early (losing boost).  Unlock
+ * paths drop the boost themselves after the wakeup;
+ * rwsem_boost_untrack() does both for callers that hand @sem over.
  */
 static inline void __rwsem_boost_untrack(struct rw_semaphore *sem)
 {
@@ -127,16 +129,32 @@ static inline void __rwsem_boost_untrack(struct rw_semaphore *sem)
 	}
 }
 
+static inline void rwsem_boost_maybe_drop(void)
+{
+	/*
+	 * Check prio first so unboosted unlocks skip preempt_count.
+	 * rwsem_boost_drop() takes rq->lock only if the boost drops,
+	 * normally right after releasing the contended rwsem that set
+	 * it, where up_*() can already take pi_lock and rq->lock via
+	 * rwsem_wake().
+	 */
+	if (unlikely(READ_ONCE(current->rwsem_boost.prio) < MAX_PRIO &&
+		     in_task()))
+		rwsem_boost_drop();
+}
+
 /**
  * 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.
+ * completion that runs in a different task.  This also drops any
+ * boost that @sem gave current.  Task context only.
  */
 void rwsem_boost_untrack(struct rw_semaphore *sem)
 {
 	__rwsem_boost_untrack(sem);
+	rwsem_boost_maybe_drop();
 }
 EXPORT_SYMBOL_GPL(rwsem_boost_untrack);
 #else
@@ -151,6 +169,10 @@ static inline void rwsem_boost_track_trylock(struct rw_semaphore *sem)
 static inline void __rwsem_boost_untrack(struct rw_semaphore *sem)
 {
 }
+
+static inline void rwsem_boost_maybe_drop(void)
+{
+}
 #endif
 
 #ifndef CONFIG_PREEMPT_RT
@@ -499,7 +521,8 @@ enum rwsem_wake_type {
 /*
  * 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
+ * nice 19 unless PI-boosted into RT, a transitive rwsem boost counts
+ * without changing current->prio, and nice -20 caps it to protect
  * RT/DL.
  */
 static inline void rwsem_waiter_init_prio(struct rwsem_waiter *waiter)
@@ -513,6 +536,7 @@ static inline void rwsem_waiter_init_prio(struct rwsem_waiter *waiter)
 	prio = READ_ONCE(current->prio);
 	if (READ_ONCE(current->policy) == SCHED_IDLE && prio >= MAX_RT_PRIO)
 		prio = MAX_PRIO;
+	prio = min_t(int, prio, READ_ONCE(current->rwsem_boost.prio));
 	waiter->boost_prio = max_t(int, prio, MAX_RT_PRIO);
 }
 
@@ -1427,6 +1451,8 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
 	__rwsem_boost_untrack(sem);
 	rwsem_del_wake_waiter(sem, &waiter, &wake_q);
 	__set_current_state(TASK_RUNNING);
+	/* Undo any boost that the signal wakeup took from the slot. */
+	rwsem_boost_maybe_drop();
 	lockevent_inc(rwsem_rlock_fail);
 	trace_contention_end(sem, -EINTR);
 	return ERR_PTR(-EINTR);
@@ -1707,6 +1733,9 @@ static inline void __up_read(struct rw_semaphore *sem)
 		rwsem_wake(sem);
 	}
 	preempt_enable();
+
+	/* Drop any boost after the wakeup, with preemption enabled. */
+	rwsem_boost_maybe_drop();
 }
 
 /*
@@ -1733,6 +1762,7 @@ static inline void __up_write(struct rw_semaphore *sem)
 		rwsem_wake(sem);
 	}
 	preempt_enable();
+	rwsem_boost_maybe_drop();
 }
 
 /*
@@ -1759,6 +1789,7 @@ static inline void __downgrade_write(struct rw_semaphore *sem)
 		rwsem_downgrade_wake(sem);
 	}
 	preempt_enable();
+	rwsem_boost_maybe_drop();
 }
 
 #else /* !CONFIG_PREEMPT_RT */
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index ea89471d4743..56e32d4ffc3c 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -39,6 +39,7 @@
 #include <linux/sched/nohz.h>
 #include <linux/sched/rseq_api.h>
 #include <linux/sched/rt.h>
+#include <linux/rwsem.h>
 
 #include <linux/context_tracking.h>
 #include <linux/cpuset.h>
@@ -1562,12 +1563,96 @@ int tg_nop(struct task_group *tg, void *data)
 }
 #endif
 
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+static inline void rwsem_boost_fork(struct task_struct *p)
+{
+	BUILD_BUG_ON(MAX_PRIO > U8_MAX);
+
+	memset(&p->rwsem_boost, 0, sizeof(p->rwsem_boost));
+	p->rwsem_boost.prio = MAX_PRIO;
+
+	/*
+	 * dup_task_struct() copied current->se.load without rq->lock;
+	 * it may be boosted or torn.  Recompute it so the child neither
+	 * inherits the boost nor seeds its PELT load_avg from it.
+	 */
+	set_load_weight(p, false);
+}
+
+/**
+ * rwsem_boost_calc_prio - Compute @p's rwsem boost priority
+ * @p: task whose held rwsems are inspected
+ *
+ * Callers must ensure @p cannot run elsewhere (see struct
+ * rwsem_sched_boost).
+ *
+ * Return: the lowest fair priority of any waiter on @p's held rwsems
+ * if it outranks @p, or MAX_PRIO otherwise.
+ */
+int rwsem_boost_calc_prio(struct task_struct *p)
+{
+	struct rwsem_sched_boost *b = &p->rwsem_boost;
+	int best = MAX_PRIO;
+	int i;
+
+	if (!rwsem_boost_has_headroom(p))
+		return MAX_PRIO;
+
+	if (READ_ONCE(b->nr_held) == 0)
+		return MAX_PRIO;
+
+	for (i = 0; i < RWSEM_SCHED_BOOST_SLOTS; i++) {
+		struct rw_semaphore *sem = READ_ONCE(b->held[i]);
+		int wprio;
+
+		if (!sem)
+			continue;
+		wprio = rwsem_top_waiter_prio(sem);
+		if (wprio < best)
+			best = wprio;
+	}
+
+	/* Cap at nice -20; boost only if the waiter outranks @p. */
+	best = max_t(int, best, MAX_RT_PRIO);
+	return best < rwsem_boost_base_prio(p) ? best : MAX_PRIO;
+}
+
+/**
+ * rwsem_boost_drop - Drop current's rwsem boost after releasing a lock
+ *
+ * Only current writes its held[] and it still holds every rwsem listed
+ * there, so the boost can be recomputed without rq->lock.  Take the
+ * lock only if the boost dropped: releasing an rwsem that did not set
+ * it changes nothing, and upgrades are left to enqueue, preemption and
+ * the tick.
+ */
+void rwsem_boost_drop(void)
+{
+	struct task_struct *p = current;
+	struct rq_flags rf;
+	struct rq *rq;
+
+	if (rwsem_boost_calc_prio(p) <= READ_ONCE(p->rwsem_boost.prio))
+		return;
+
+	rq = task_rq_lock(p, &rf);
+	if (p->sched_class == &fair_sched_class) {
+		update_rq_clock(rq);
+		rwsem_boost_drop_fair(rq, p);
+	}
+	task_rq_unlock(rq, p, &rf);
+}
+#else
+static inline void rwsem_boost_fork(struct task_struct *p) { }
+#endif
+
 void set_load_weight(struct task_struct *p, bool update_load)
 {
-	int prio = p->static_prio - MAX_RT_PRIO;
+	int prio = rwsem_boost_effective_prio(p) - MAX_RT_PRIO;
 	struct load_weight lw;
 
-	if (task_has_idle_policy(p)) {
+	/* Boosted SCHED_IDLE holders use normal nice weights. */
+	if (task_has_idle_policy(p) && !rwsem_boost_active(p)) {
 		lw.weight = scale_load(WEIGHT_IDLEPRIO);
 		lw.inv_weight = WMULT_IDLEPRIO;
 	} else {
@@ -4916,6 +5001,7 @@ int sched_fork(u64 clone_flags, struct task_struct *p)
 	 * Make sure we do not leak PI boosting priority to the child.
 	 */
 	p->prio = current->normal_prio;
+	rwsem_boost_fork(p);
 
 	uclamp_fork(p);
 
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 2d9d3d686261..f755ee810ce2 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -8199,6 +8199,102 @@ static inline void update_curr_eevdf(struct cfs_rq *cfs_rq)
 	update_curr(cfs_rq_of(cfs_rq->curr));
 }
 
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+/*
+ * Recompute fair task @p's load weight from its held rwsems without
+ * touching p->prio, so rt_mutex PI and syscall priorities stay
+ * orthogonal.  Callers hold @rq->lock with @p attached to cfs_rq->avg
+ * and not running elsewhere (see struct rwsem_sched_boost).
+ */
+static bool rwsem_boost_reweight_fair(struct rq *rq, struct task_struct *p)
+{
+	struct sched_entity *se = &p->se;
+	int new_boost;
+
+	lockdep_assert_rq_held(rq);
+
+	new_boost = rwsem_boost_calc_prio(p);
+	if (new_boost == p->rwsem_boost.prio)
+		return false;
+
+	/* Sync @se and its cfs_rq PELT before reweight_entity(). */
+	update_load_avg(cfs_rq_of(se), se, 0);
+	WRITE_ONCE(p->rwsem_boost.prio, new_boost);
+	set_load_weight(p, true);
+	return true;
+}
+
+/*
+ * Clear @p's rwsem boost when it leaves fair_sched_class, so unlocking
+ * outside CFS never leaves a stale weight behind.  This runs before
+ * detach_task_cfs_rq(), so handle PELT like detach_entity_cfs_rq().
+ */
+static void rwsem_boost_clear_fair(struct rq *rq, struct task_struct *p)
+{
+	struct sched_entity *se = &p->se;
+
+	lockdep_assert_rq_held(rq);
+
+	if (!rwsem_boost_active(p))
+		return;
+
+	/* Wakeup still pending: no cfs_rq holds this load yet. */
+	if (!se->avg.last_update_time) {
+		WRITE_ONCE(p->rwsem_boost.prio, MAX_PRIO);
+		set_load_weight(p, false);
+		return;
+	}
+	/* Sync @se and its cfs_rq PELT before reweight_entity(). */
+	update_load_avg(cfs_rq_of(se), se, 0);
+	WRITE_ONCE(p->rwsem_boost.prio, MAX_PRIO);
+	set_load_weight(p, true);
+}
+
+/*
+ * Cancel @curr's slice protection and resched it.  Not lazy: a boost
+ * change, like a priority change in prio_changed_fair(), should not
+ * wait for the next tick.
+ */
+static void rwsem_boost_resched_fair(struct rq *rq, struct task_struct *curr)
+{
+	cancel_protect_slice(&curr->se);
+	resched_curr(rq);
+}
+
+/* True if running @p lost weight while other fair tasks wait. */
+static inline bool rwsem_boost_should_resched_fair(struct rq *rq,
+						   struct task_struct *p,
+						   int old_prio)
+{
+	return rwsem_boost_effective_prio(p) > old_prio &&
+	       task_current_donor(rq, p) && rq->cfs.h_nr_queued > 1;
+}
+
+/*
+ * Refresh running @p's boost.  Return true if it lost the boost while
+ * other fair tasks wait, so the caller should reschedule.
+ */
+static bool rwsem_boost_refresh_curr_fair(struct rq *rq,
+					  struct task_struct *p)
+{
+	int old_prio = rwsem_boost_effective_prio(p);
+
+	if (!rwsem_boost_reweight_fair(rq, p))
+		return false;
+	return rwsem_boost_should_resched_fair(rq, p, old_prio);
+}
+
+/* Refresh @p's boost weight after rwsem unlock or downgrade_write(). */
+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);
+}
+#else
+static inline void rwsem_boost_clear_fair(struct rq *rq,
+					  struct task_struct *p) { }
+#endif
+
 /*
  * The enqueue_task method is called before nr_running is
  * increased. Here we update the fair scheduling stats and
@@ -15538,6 +15634,7 @@ static void switching_from_fair(struct rq *rq, struct task_struct *p)
 
 static void switched_from_fair(struct rq *rq, struct task_struct *p)
 {
+	rwsem_boost_clear_fair(rq, p);
 	detach_task_cfs_rq(p);
 }
 
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 32f76551a0c5..058311c19cc0 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2848,6 +2848,7 @@ struct sched_class {
 	/*
 	 * set_load_weight: task_rq_lock + sched_change
 	 * __setscheduler_parms: task_rq_lock + sched_change
+	 * rwsem boost (fair): rq->lock, @task may be queued or running
 	 */
 	void (*reweight_task)(struct rq *this_rq, struct task_struct *task,
 			      const struct load_weight *lw);
@@ -4420,4 +4421,45 @@ void sched_push_current_non_preferred_cpu(struct rq *rq);
 static inline void sched_push_current_non_preferred_cpu(struct rq *rq) { }
 #endif
 
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+static inline bool rwsem_boost_active(const struct task_struct *p)
+{
+	return p->rwsem_boost.prio < MAX_PRIO;
+}
+
+/*
+ * Base CFS prio for boost decisions.  SCHED_IDLE tasks keep
+ * static_prio == NICE_TO_PRIO(0) but rank below nice 19.
+ */
+static inline int rwsem_boost_base_prio(const struct task_struct *p)
+{
+	return idle_policy(p->policy) ? MAX_PRIO : p->static_prio;
+}
+
+/* True if @p is below nice -20 and can still be boosted. */
+static inline bool rwsem_boost_has_headroom(const struct task_struct *p)
+{
+	return rwsem_boost_base_prio(p) > MAX_RT_PRIO;
+}
+
+/* Effective fair prio; MAX_PRIO for an unboosted SCHED_IDLE task. */
+static inline int rwsem_boost_effective_prio(const struct task_struct *p)
+{
+	return min_t(int, rwsem_boost_base_prio(p), p->rwsem_boost.prio);
+}
+
+int rwsem_boost_calc_prio(struct task_struct *p);
+void rwsem_boost_drop_fair(struct rq *rq, struct task_struct *p);
+#else
+static inline bool rwsem_boost_active(const struct task_struct *p)
+{
+	return false;
+}
+
+static inline int rwsem_boost_effective_prio(const struct task_struct *p)
+{
+	return p->static_prio;
+}
+#endif
+
 #endif /* _KERNEL_SCHED_SCHED_H */
-- 
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 ` [RFC PATCH v2 3/6] locking/rwsem: Publish the top waiter priority Peng Huang
2026-12-01  6:27 ` Peng Huang [this message]
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-5-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®