Skip to contentSkip to conversation

rwsem: priority boost for holders blocking high-priority waiters

RFC PATCH New Peng Huang 1 participant 4 messages Nov 19

Subsystems (4)
lore
Share & tools
lore

Peng Huang

author
#0 lore raw

cover rwsem: priority boost for holders blocking high-priority waiters

Hi,

High-priority interactive threads (such as UI/render threads) can stall for tens to hundreds of milliseconds on shared rw_semaphores like mm->mmap_lock, inode->i_rwsem, or mapping->i_mmap_rwsem/invalidate_lock when lower-priority (or SCHED_IDLE) read-owners are preempted on busy CPUs and block an incoming higher-weight writer. Upstream CONFIG_SCHED_PROXY_EXEC currently covers mutexes, and out-of-tree rwsem proxy efforts only track a single write-owner, leaving multi-reader inversions unaddressed.

This RFC proposes a lightweight priority-inheritance mechanism for CFS rw_semaphore holders that accelerates stalled readers and writers in parallel without requiring lock-side reader tracking.

Design rationale
----------------
1. Per-task hold tracking (avoiding lock-side reader lists):
Because rw_semaphore allows unbounded concurrent readers, storing a reader list inside struct rw_semaphore would require dynamic allocation or extra locking in the down_read()/up_read() fastpath. Instead, each task records up to CONFIG_RWSEM_SCHED_BOOST_SLOTS (default 4) rwsem pointers in task_struct under current-only access. This supports an unlimited number of concurrent readers per rwsem; the slot count only caps simultaneous nested rwsems held by one task.

2. Contended waiter demand (sem->max_waiter_prio):
Waiters in rwsem_down_{read,write}_slowpath() publish the lowest normal prio (i.e. highest nice weight, capped at MAX_RT_PRIO so CFS holders never escalate into RT/DL) into sem->max_waiter_prio, recalculating without redundant atomic writes as waiters wake/leave.

3. Per-rq runqueue list and parallel boost:
Runnable CFS tasks holding at least one rwsem are linked onto a per-CPU list (rq->held_rwsem_tasks) during enqueue/dequeue/migrate under rq_lock, keeping lock/unlock fastpaths list-free. Normal short holds never touch rq->held_rwsem_tasks; tasks are only linked if preempted, enqueued, or crossing a tick while holding a lock, so tick and idle-pull typically exit immediately on an empty list with zero iteration or max_waiter_prio lookups. Across wakeup, preemption, tick, and idle-balance pull paths, the scheduler checks held locks' sem->max_waiter_prio and elevates the holder's effective priority and EEVDF weight (preserving static_prio/nice and handling SCHED_IDLE separately) down to min(p->prio, max_waiter_prio), restoring cleanly on unlock. Runtime/build disablement is provided via rwsem_sched_boost=0, CONFIG_RWSEM_SCHED_BOOST_SLOTS=0, or PREEMPT_RT.

4. Model trade-off (Parallel priority spray vs. Proxy Execution):
Proxy Execution follows task->blocked_on to boost one owner at a time on the owner's CPU (entailing cross-CPU proxy migrations and requiring a lock-side owner list for readers), while naturally propagating transitively across mutex/futex/rwsem chains. By contrast, this RFC avoids lock-side reader tracking and lets all preempted readers on any CPU boost and drain in parallel once contended, at the intentional cost of not propagating transitively through mutex/futex chains (e.g. RT -> mutex -> rwsem).

Validation & known trade-offs
-----------------------------
- Included KUnit suite (Patch 3/3, CONFIG_RWSEM_SCHED_BOOST_KUNIT_TEST):
Reproducible under ARCH=um via kunit.py (10/10 pass incl. SCHED_IDLE cap, interleaved unlock, and slot overflow):
* Preemption inversion (n=3, 4 nice=0 hogs):

    Control mean 1939 ms (max 1970 ms) -> Treatment mean 49 ms (-97.5%)
  * Uncontended fastpath (1,000,000 ops):
    read +2.75 ns/op (21.43 -> 24.18),
    write +1.99 ns/op (20.78 -> 22.77),
    nested2 read +4.31 ns/op (21.20 -> 25.51)

- ARM64 mobile hardware (6.18 backport, asymmetric SMP, user-space madvise/mprotect stress reproducer):
* mmap_lock inversion (nice -10 madvise vs 7 nice 0 mprotect hogs):
- Unpinned 5-round AB (100 runs): mean 120.22 -> 7.26 ms (-94.0%), missed 16ms deadlines 46/100 -> 8/100
- Mid-cluster pinned 4-round AB/BA (80 runs): mean 281.11 -> 7.34 ms (-97.4%), max 736.14 -> 50.76 ms, missed 16ms 80/80 -> 4/80
* Big-core pinned syscall fastpath (big core, 4 rounds x 100k iters):

    madvise 615.8 -> 604.8 ns/op (-1.8%),
    mprotect 857.5 -> 848.5 ns/op (-1.0%)
  * Contended equal-priority non-inversion (4 threads, Mid pinned):
    145.5 -> 162.5 ms (+11.7% throughput trade-off under contention;
    also +10.8% in x86_64 QEMU -smp 4 TCG)
  * Pinned hackbench / pipe ping-pong (Mid+Big):
    hackbench 263.7 -> 243.3 ms (-7.7%),
    pipe 10.81 -> 10.33 us/op (-4.4%)

Request for feedback
--------------------
Before iterating further, feedback is especially welcome on:
- Parallel priority spray (task-slot tracking) vs. extending Proxy Execution with lock-side reader tracking, vs. a slowpath/owner-only heuristic;
- Using normal prio / nice weight as the demand currency vs. EEVDF virtual deadline / lag considerations;
- Struct size impact of adding max_waiter_prio to struct rw_semaphore (+8 bytes aligned on 64-bit, 32 -> 40 bytes, embedded in inode/mm);
- Intentional RFC boundaries: 4-slot nested limit per task, boosting all concurrent readers equally, idle-pull inspecting only the busiest rq (no global CPU scan/cpumask), no cgroup/task_group share propagation, and no transitive inheritance across mutex/futex chains.

Per Documentation/process/generated-content.rst: Jetski AI coding assistance was used for drafting, refactoring discussions, and KUnit / benchmark verification; all patches and tests have been reviewed and verified by the author.

Peng Huang (3):
locking/rwsem, sched: track held rwsems and waiter priorities
sched: boost rwsem holders with contended high-priority waiters locking/rwsem: add KUnit tests for scheduler priority boost

 .../admin-guide/kernel-parameters.txt         |   5 +
 include/linux/rwsem.h                         |  28 +
 include/linux/sched.h                         |  16 +
 init/init_task.c                              |   4 +
 kernel/Kconfig.locks                          |  15 +
 kernel/locking/Makefile                       |   1 +
 kernel/locking/rwsem.c                        | 209 +++++++-
 kernel/locking/rwsem_sched_boost_test.c       | 504 ++++++++++++++++++
 kernel/sched/core.c                           | 211 +++++++-
 kernel/sched/fair.c                           |  51 ++
 kernel/sched/sched.h                          |  70 +++
 kernel/sched/syscalls.c                       |   2 +-
 lib/Kconfig.debug                             |  10 +
 13 files changed, 1101 insertions(+), 25 deletions(-)
 create mode 100644 kernel/locking/rwsem_sched_boost_test.c
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#1 lore raw

RFC PATCH 1/3 locking/rwsem, sched: track held rwsems and waiter priorities

Track per-task held rw_semaphore pointers in a small fixed-size slot array (CONFIG_RWSEM_SCHED_BOOST_SLOTS) and track the highest-priority contending waiter on each rw_semaphore (max_waiter_prio, capped at MAX_RT_PRIO).

Blocking acquisitions skip irqs/atomic guards to avoid pushf on the fastpath, while trylock paths keep IRQ/atomic context filtering for non-owner unlockers such as BPF helpers. Held slots are cleared under preempt_disable() before releasing sem->count so another CPU cannot acquire and free the semaphore while a tick/preempt check still sees it in held_rwsems[].

Also wire up core priority calculation, runtime toggle (rwsem_sched_boost=), and post-unlock priority restoration.

---
 .../admin-guide/kernel-parameters.txt         |   5 +
 include/linux/rwsem.h                         |  28 +++
 include/linux/sched.h                         |  16 ++
 init/init_task.c                              |   4 +
 kernel/Kconfig.locks                          |  15 ++
 kernel/locking/rwsem.c                        | 211 ++++++++++++++++--
 kernel/sched/core.c                           | 102 ++++++++-
 kernel/sched/sched.h                          |  43 ++++
 kernel/sched/syscalls.c                       |   2 +-
 9 files changed, 402 insertions(+), 24 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -0
index 176648941aec..2c5207dd9c53 100644
@@ -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]
include/linux/rwsem.h +28 -0
index 6a1a7bae5f81..b9188de31881 100644
@@ -16,6 +16,8 @@
 #include <linux/atomic.h>
 #include <linux/err.h>
 #include <linux/cleanup.h>
+#include <linux/jump_label.h>
+#include <linux/sched/prio.h>
 
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 # define __RWSEM_DEP_MAP_INIT(lockname)			\
@@ -58,6 +60,9 @@ context_lock_struct(rw_semaphore) {
 #endif
 	raw_spinlock_t wait_lock;
 	struct rwsem_waiter *first_waiter __guarded_by(&wait_lock);
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+	atomic_t max_waiter_prio;
+#endif
 #ifdef CONFIG_DEBUG_RWSEMS
 	void *magic;
 #endif
@@ -101,12 +106,19 @@ static inline void rwsem_assert_held_write_nolockdep(const struct rw_semaphore *
 #define __RWSEM_OPT_INIT(lockname)
 #endif
 
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+# define __RWSEM_SCHED_BOOST_INIT .max_waiter_prio = ATOMIC_INIT(MAX_PRIO),
+#else
+# define __RWSEM_SCHED_BOOST_INIT
+#endif
+
 #define __RWSEM_INITIALIZER(name)				\
 	{ __RWSEM_COUNT_INIT(name),				\
 	  .owner = ATOMIC_LONG_INIT(0),				\
 	  __RWSEM_OPT_INIT(name)				\
 	  .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock),\
 	  .first_waiter = NULL,					\
+	  __RWSEM_SCHED_BOOST_INIT				\
 	  __RWSEM_DEBUG_INIT(name)				\
 	  __RWSEM_DEP_MAP_INIT(name) }
 
@@ -327,8 +339,24 @@ 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
+extern struct static_key_true rwsem_sched_boost_key;
+static inline void rwsem_sched_boost_set(bool enable)
+{
+	if (enable)
+		static_branch_enable(&rwsem_sched_boost_key);
+	else
+		static_branch_disable(&rwsem_sched_boost_key);
+}
+#endif
 
 #endif /* _LINUX_RWSEM_H */
include/linux/sched.h +16 -0
index e4f6323d7855..9ce2c11aa500 100644
@@ -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;
@@ -1266,6 +1267,13 @@ struct task_struct {
 	struct rt_mutex_waiter		*pi_blocked_on;
 #endif
 
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+	struct rw_semaphore		*held_rwsems[CONFIG_RWSEM_SCHED_BOOST_SLOTS];
+	u8				nr_held_rwsems;
+	int				rwsem_boost_prio;
+	struct list_head		rwsem_rq_node;
+#endif
+
 	struct mutex			*blocked_on;	/* lock we're blocked on */
 	raw_spinlock_t			blocked_lock;
 
@@ -2250,6 +2258,14 @@ static inline void clear_task_blocked_on(struct task_struct *p, struct rt_mutex
 }
 #endif /* !CONFIG_PREEMPT_RT */
 
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+void sched_rwsem_recalc_boost(struct task_struct *p);
+#else
+static inline void sched_rwsem_recalc_boost(struct task_struct *p)
+{
+}
+#endif
+
 static __always_inline bool need_resched(void)
 {
 	return unlikely(tif_need_resched());
init/init_task.c +4 -0
index ce7c2b07d855..a52a08f11ffd 100644
@@ -134,6 +134,10 @@ 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,
+	.rwsem_rq_node	= LIST_HEAD_INIT(init_task.rwsem_rq_node),
+#endif
 #ifdef CONFIG_SMP
 	.pushable_tasks	= PLIST_NODE_INIT(init_task.pushable_tasks, MAX_PRIO),
 #endif
kernel/Kconfig.locks +15 -0
index 1c6423aafcd4..7487cdab4e59 100644
@@ -279,3 +279,18 @@ config ARCH_HAS_MMIOWB
 config MMIOWB
 	def_bool y if ARCH_HAS_MMIOWB
 	depends on SMP
+
+config RWSEM_SCHED_BOOST_SLOTS
+	int "Maximum held rw_semaphores tracked per task for priority boost"
+	range 0 16
+	default 4
+	depends on !PREEMPT_RT
+	help
+	  Number of rw_semaphore pointers recorded per task_struct so the
+	  scheduler can detect and boost holders blocking higher-priority
+	  waiters across wakeup, preemption, tick, and idle-balance hooks.
+	  Set to 0 to disable.
+
+config RWSEM_SCHED_BOOST
+	def_bool y
+	depends on RWSEM_SCHED_BOOST_SLOTS > 0
kernel/locking/rwsem.c +190 -21

386 lines not on this page · raw message

kernel/sched/core.c +100 -2

124 lines not on this page · raw message

kernel/sched/sched.h +43 -0

47 lines not on this page · raw message

kernel/sched/syscalls.c +1 -1
index 0ec401af80d1..d9e3c61fcfba 100644
@@ -58,7 +58,7 @@ static int effective_prio(struct task_struct *p)
 	 * to the normal priority:
 	 */
 	if (!rt_or_dl_prio(p->prio))
-		return p->normal_prio;
+		return sched_rwsem_effective_prio(p);
 	return p->prio;
 }
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#2 lore raw

RFC PATCH 2/3 sched: boost rwsem holders with contended high-priority waiters

In reply to Peng Huang

Keep queued fair tasks holding rwsems on rq->held_rwsem_tasks and check waiter demand across scheduler wakeup, preemption, tick, and idle load-balance pull paths.

Uncontended lock/unlock paths never touch rq->held_rwsem_tasks; a task is linked only if preempted, enqueued, or crossing a tick while holding an rwsem. Thus rq->held_rwsem_tasks stays empty for the vast majority of ticks and idle pulls, exiting immediately with zero iteration or max_waiter_prio lookups.

When a fair task holds a contended rwsem (max_waiter_prio < task prio), temporarily elevate its effective priority and EEVDF weight without altering user-visible static_prio/nice, and restore it on unlock or lazily unlink uncontended unlocks on tick.

---
 kernel/sched/core.c  | 109 ++++++++++++++++++++++++++++++++++++++++++-
 kernel/sched/fair.c  |  51 ++++++++++++++++++++
 kernel/sched/sched.h |  27 +++++++++++
 3 files changed, 185 insertions(+), 2 deletions(-)
kernel/sched/core.c +107 -2
index 5737fe55d52c..2ead771c703a 100644
@@ -1561,7 +1561,12 @@ static inline void sched_rwsem_fork_init(struct task_struct *p)
 	INIT_LIST_HEAD(&p->rwsem_rq_node);
 }
 
-static int sched_rwsem_compute_boost_prio(struct task_struct *p)
+static inline void sched_rwsem_rq_init(struct rq *rq)
+{
+	INIT_LIST_HEAD(&rq->held_rwsem_tasks);
+}
+
+int sched_rwsem_compute_boost_prio(struct task_struct *p)
 {
 	int best = MAX_PRIO;
 	int i;
@@ -1613,10 +1618,88 @@ bool sched_rwsem_check_boost(struct task_struct *p, bool update_load)
 	return true;
 }
 
+static inline void sched_rwsem_track_on_rq(struct rq *rq, struct task_struct *p)
+{
+	if (list_empty(&p->rwsem_rq_node))
+		list_add(&p->rwsem_rq_node, &rq->held_rwsem_tasks);
+}
+
+/*
+ * Safe to inspect p->held_rwsems[] here because ttwu() holds p->pi_lock
+ * after smp_cond_load_acquire(&p->on_cpu, !VAL), so @p is off-CPU and
+ * cannot concurrently run up_read()/kfree(sem).
+ */
+static inline void sched_rwsem_wakeup_boost(struct task_struct *p)
+{
+	if (READ_ONCE(p->nr_held_rwsems))
+		sched_rwsem_check_boost(p, false);
+}
+
+static inline void sched_rwsem_enqueue_locked(struct rq *rq,
+					      struct task_struct *p)
+{
+	if (!READ_ONCE(p->nr_held_rwsems) ||
+	    p->sched_class != &fair_sched_class)
+		return;
+
+	sched_rwsem_check_boost(p, false);
+	sched_rwsem_track_on_rq(rq, p);
+}
+
+static inline void sched_rwsem_on_preempt_locked(struct rq *rq,
+						 struct task_struct *prev)
+{
+	if (!READ_ONCE(prev->nr_held_rwsems) || task_rq(prev) != rq ||
+	    !task_on_rq_queued(prev) || prev->se.sched_delayed ||
+	    prev->sched_class != &fair_sched_class)
+		return;
+
+	sched_rwsem_track_on_rq(rq, prev);
+	sched_rwsem_check_boost(prev, true);
+}
+
+static inline void sched_rwsem_tick_locked(struct rq *rq)
+{
+	struct task_struct *curr = rq->curr;
+	struct task_struct *p, *tmp;
+	bool need_resched = false;
+
+	if (READ_ONCE(curr->nr_held_rwsems) &&
+	    curr->sched_class == &fair_sched_class &&
+	    task_rq(curr) == rq && task_on_rq_queued(curr) &&
+	    !curr->se.sched_delayed) {
+		sched_rwsem_track_on_rq(rq, curr);
+		sched_rwsem_check_boost(curr, true);
+	}
+
+	/*
+	 * Lazily unlink tasks whose unlock fastpath skipped rq->lock because
+	 * they were never boosted (!nr_held_rwsems short-circuits sem access),
+	 * or if proxy/delayed-dequeue movement left a stale rq link.
+	 */
+	list_for_each_entry_safe(p, tmp, &rq->held_rwsem_tasks, rwsem_rq_node) {
+		if (!READ_ONCE(p->nr_held_rwsems) ||
+		    task_rq(p) != rq || !task_on_rq_queued(p)) {
+			sched_rwsem_dequeue_locked(p);
+			continue;
+		}
+		if (p == curr || task_on_cpu(rq, p) || p->se.sched_delayed)
+			continue;
+		sched_rwsem_check_boost(p, true);
+		if (p->rwsem_boost_prio < curr->prio)
+			need_resched = true;
+	}
+
+	if (need_resched)
+		resched_curr(rq);
+}
+
 void sched_rwsem_recalc_boost(struct task_struct *p)
 {
 	guard(task_rq_lock)(p);
 
+	if (!READ_ONCE(p->nr_held_rwsems))
+		sched_rwsem_dequeue_locked(p);
 	if (p->sched_class == &fair_sched_class) {
 		update_rq_clock(task_rq(p));
 		sched_rwsem_check_boost(p, !p->se.sched_delayed);
@@ -1629,6 +1712,16 @@ static inline bool sched_rwsem_is_boosted(const struct task_struct *p)
 }
 
 static inline void sched_rwsem_fork_init(struct task_struct *p) { }
+static inline void sched_rwsem_rq_init(struct rq *rq) { }
+static inline void sched_rwsem_wakeup_boost(struct task_struct *p) { }
+
+static inline void sched_rwsem_enqueue_locked(struct rq *rq,
+					      struct task_struct *p) { }
+
+static inline void sched_rwsem_on_preempt_locked(struct rq *rq,
+						 struct task_struct *prev) { }
+
+static inline void sched_rwsem_tick_locked(struct rq *rq) { }
 #endif
 
 void set_load_weight(struct task_struct *p, bool update_load)
@@ -2284,6 +2377,8 @@ void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
 	if (!(flags & ENQUEUE_NOCLOCK))
 		update_rq_clock(rq);
 
+	sched_rwsem_enqueue_locked(rq, p);
+
 	/*
 	 * Can be before ->enqueue_task() because uclamp considers the
 	 * ENQUEUE_DELAYED task before its ->sched_delayed gets cleared
@@ -2323,7 +2418,11 @@ inline bool dequeue_task(struct rq *rq, struct task_struct *p, int flags)
 	 * and mark the task ->sched_delayed.
 	 */
 	uclamp_rq_dec(rq, p);
-	return p->sched_class->dequeue_task(rq, p, flags);
+	if (p->sched_class->dequeue_task(rq, p, flags)) {
+		sched_rwsem_dequeue_locked(p);
+		return true;
+	}
+	return false;
 }
 
 void activate_task(struct rq *rq, struct task_struct *p, int flags)
@@ -4566,6 +4665,8 @@ int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
 		 */
 		smp_cond_load_acquire(&p->on_cpu, !VAL);
 
+		sched_rwsem_wakeup_boost(p);
+
 		cpu = select_task_rq(p, p->wake_cpu, &wake_flags);
 		if (task_cpu(p) != cpu) {
 			wake_flags |= WF_MIGRATED;
@@ -5957,6 +6058,7 @@ void sched_tick(void)
 		resched_curr(rq);
 
 	donor->sched_class->task_tick(rq, donor, 0);
+	sched_rwsem_tick_locked(rq);
 	if (sched_feat(LATENCY_WARN))
 		resched_latency = cpu_resched_latency(rq);
 	calc_global_load_tick(rq);
@@ -7391,6 +7493,8 @@ static void __sched notrace __schedule(int sched_mode)
 		switch_count = &prev->nvcsw;
 	}
 
+	sched_rwsem_on_preempt_locked(rq, prev);
+
 pick_again:
 	assert_balance_callbacks_empty(rq);
 	next = pick_next_task(rq, &rf);
@@ -9125,6 +9229,7 @@ void __init sched_init(void)
 		rq->max_idle_balance_cost = sysctl_sched_migration_cost;
 
 		INIT_LIST_HEAD(&rq->cfs_tasks);
+		sched_rwsem_rq_init(rq);
 
 		rq_attach_root(rq, &def_root_domain);
 #ifdef CONFIG_NO_HZ_COMMON
kernel/sched/fair.c +51 -0

63 lines not on this page · raw message

kernel/sched/sched.h +27 -0
index 98bc9029f9ae..58e53980e71b 100644
@@ -1317,6 +1317,13 @@ struct rq {
 	int			online;
 
 	struct list_head cfs_tasks;
+#ifdef CONFIG_RWSEM_SCHED_BOOST
+	/*
+	 * Queued fair tasks holding an rwsem; protected by rq->lock and
+	 * unlinked on dequeue_task() (incl. migration and TASK_DEAD).
+	 */
+	struct list_head	held_rwsem_tasks;
+#endif
 
 	struct sched_avg	avg_rt;
 	struct sched_avg	avg_dl;
@@ -4368,7 +4375,18 @@ static inline int sched_rwsem_effective_prio(struct task_struct *p)
 	return sched_rwsem_prio(p, p->normal_prio);
 }
 
+int sched_rwsem_compute_boost_prio(struct task_struct *p);
 bool sched_rwsem_check_boost(struct task_struct *p, bool update_load);
+
+/*
+ * Remove @p when it leaves an rq (sleep, migration, class change,
+ * TASK_DEAD via DEQUEUE_SPECIAL, or stale cross-rq cleanup).
+ */
+static inline void sched_rwsem_dequeue_locked(struct task_struct *p)
+{
+	if (!list_empty(&p->rwsem_rq_node))
+		list_del_init(&p->rwsem_rq_node);
+}
 #else
 static inline int sched_rwsem_cfs_prio(const struct task_struct *p)
 {
@@ -4380,11 +4398,20 @@ static inline int sched_rwsem_effective_prio(const struct task_struct *p)
 	return p->normal_prio;
 }
 
+static inline int sched_rwsem_compute_boost_prio(struct task_struct *p)
+{
+	return MAX_PRIO;
+}
+
 static inline bool sched_rwsem_check_boost(struct task_struct *p,
 					   bool update_load)
 {
 	return false;
 }
+
+static inline void sched_rwsem_dequeue_locked(struct task_struct *p)
+{
+}
 #endif
 
 #endif /* _KERNEL_SCHED_SCHED_H */
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#3 lore raw

RFC PATCH 3/3 locking/rwsem: add KUnit tests for scheduler priority boost

In reply to Peng Huang

Add KUnit tests covering per-task rwsem slot tracking, interleaved unlock order, slot overflow fallback, write-lock downgrade, wakeup boost for both reader and writer holders, preemption priority-inversion A/B latency, false-promotion checks, and fastpath overhead measurement.

---
 kernel/locking/Makefile                 |   1 +
 kernel/locking/rwsem_sched_boost_test.c | 504 ++++++++++++++++++++++++
 lib/Kconfig.debug                       |  10 +
 3 files changed, 515 insertions(+)
 create mode 100644 kernel/locking/rwsem_sched_boost_test.c
kernel/locking/Makefile +1 -0
index cee1901d4cff..3df6ae1f1707 100644
@@ -37,4 +37,5 @@ obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
 obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o
 obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
 obj-$(CONFIG_WW_MUTEX_SELFTEST) += test-ww_mutex.o
+obj-$(CONFIG_RWSEM_SCHED_BOOST_KUNIT_TEST) += rwsem_sched_boost_test.o
 obj-$(CONFIG_LOCK_EVENT_COUNTS) += lock_events.o
kernel/locking/rwsem_sched_boost_test.c new file +504 -0

504 lines not on this page · raw message

lib/Kconfig.debug +10 -0
index 2d22638ec13a..b3f2b8d6633e 100644
@@ -1692,6 +1692,16 @@ config WW_MUTEX_SELFTEST
 	  Say M if you want these self tests to build as a module.
 	  Say N if you are unsure.
 
+config RWSEM_SCHED_BOOST_KUNIT_TEST
+	bool "KUnit tests for rw_semaphore scheduler priority boost"
+	depends on KUNIT && RWSEM_SCHED_BOOST
+	default KUNIT_ALL_TESTS
+	help
+	  Enable KUnit tests verifying per-task rw_semaphore hold tracking,
+	  atomic max_waiter_prio updates, contention-driven priority boosting,
+	  and zero false promotion when uncontended.
+	  If unsure, say N.
+
 config SCF_TORTURE_TEST
 	tristate "torture tests for smp_call_function*()"
 	depends on DEBUG_KERNEL
Signature · 1 line
2.56.1.750.gf70dc682cf-goog