sched/proxy_exec: Extend PE blocked_on chain to rwsem write-side
Subsystems (4)
RFC PATCH 1/1 sched/proxy_exec: Extend PE blocked_on chain to rwsem write-side
From: zhidao su <[email protected]>
Proxy Execution currently tracks blocked_on chains only through struct mutex. This patch extends the infrastructure to support rw_semaphore write-side blocking, allowing PE to eliminate priority inversion where a high-priority writer waits for a low-priority write lock holder.
Changes:
1. include/linux/sched.h: Generalise blocked_on from struct mutex * to void *, and add a 2-bit blocked_on_type field encoding the primitive type (BLOCKED_ON_NONE/MUTEX/RWSEM). All existing mutex helpers are renamed to _mutex suffix; compatibility wrappers preserve the old names so that mutex.c requires no change. New __set/clear_task_blocked_on_rwsem() helpers are added (void * parameter avoids pulling rwsem.h into sched.h).
2. kernel/locking/rwsem.c: In rwsem_down_write_slowpath(), call __set_task_blocked_on_rwsem() after entering the wait queue (wait_lock held), re-set it after each schedule() wakeup, and clear it on lock acquisition and on signal-interrupted exit. Pattern mirrors the existing mutex slowpath.
3. kernel/sched/core.c: find_proxy_task() now dispatches on blocked_on_type. The BLOCKED_ON_RWSEM branch acquires sem->wait_lock, re-validates blocked_on, then calls rwsem_owner() to retrieve the write owner (returns NULL for reader-owned sem, which safely terminates the chain). Owner validity checks (on_rq, sched_delayed, cpu, migrating) are shared between both branches.
4. tools/testing/selftests/sched/proxy_exec_test.c: Add TC-4 (single-level rwsem write PE) and TC-5 (mixed rwsem->mutex chain). TAP plan updated from 3 to 5.
PREEMPT_RT limitation: rwsem is backed by rwbase_rt/rt_mutex under CONFIG_PREEMPT_RT. The new code paths are not compiled on RT kernels; on RT blocked_on is never set for rwsem and find_proxy_task() terminates cleanly at such nodes.
--- include/linux/sched.h | 110 ++- kernel/locking/rwsem.c | 9 + kernel/sched/core.c | 106 ++- .../testing/selftests/sched/proxy_exec_test.c | 763 ++++++++++++++++++ 4 files changed, 943 insertions(+), 45 deletions(-) create mode 100644 tools/testing/selftests/sched/proxy_exec_test.c
include/linux/sched.h +99 -11
@@ -1237,7 +1237,8 @@ struct task_struct {struct rt_mutex_waiter *pi_blocked_on;#endif -struct mutex *blocked_on;/* lock we're blocked on */ +void *blocked_on;/* lock we're blocked on */ + unsigned int blocked_on_type : 2; /* enum blocked_on_type */#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER /*
@@ -2178,8 +2179,21 @@ extern int __cond_resched_rwlock_write(rwlock_t *lock) __must_hold(lock);__cond_resched_rwlock_write (lock); \ }) +/* +* Type tag for task_struct::blocked_on. Allows PE chain traversal +* to handle different lock primitives (mutex, rwsem write-side). +*/ +enum blocked_on_type { +BLOCKED_ON_NONE =0 , +BLOCKED_ON_MUTEX =1 , +BLOCKED_ON_RWSEM =2 , +}; +#ifndef CONFIG_PREEMPT_RT -static inline struct mutex *__get_task_blocked_on (struct task_struct *p) +/* --- mutex blocked_on helpers --- */ + +static inline struct mutex * +__get_task_blocked_on_mutex (struct task_struct *p) {struct mutex *m = p->blocked_on;
@@ -2188,7 +2202,8 @@ static inline struct mutex *__get_task_blocked_on(struct task_struct *p)return m; } -static inline void __set_task_blocked_on (struct task_struct *p,struct mutex *m) +static inline void +__set_task_blocked_on_mutex (struct task_struct *p,struct mutex *m) {struct mutex *blocked_on =READ_ONCE (p->blocked_on);
@@ -2204,15 +2219,18 @@ static inline void __set_task_blocked_on(struct task_struct *p, struct mutex *m)*/ WARN_ON_ONCE (blocked_on && blocked_on != m);WRITE_ONCE (p->blocked_on, m); + p->blocked_on_type =BLOCKED_ON_MUTEX ; } -static inline void set_task_blocked_on (struct task_struct *p,struct mutex *m) +static inline void +set_task_blocked_on_mutex (struct task_struct *p,struct mutex *m) {guard (raw_spinlock_irqsave)(&m->wait_lock); -__set_task_blocked_on (p, m); +__set_task_blocked_on_mutex (p, m); } -static inline void __clear_task_blocked_on (struct task_struct *p,struct mutex *m) +static inline void +__clear_task_blocked_on_mutex (struct task_struct *p,struct mutex *m) {if (m) {struct mutex *blocked_on =READ_ONCE (p->blocked_on);
@@ -2227,21 +2245,91 @@ static inline void __clear_task_blocked_on(struct task_struct *p, struct mutex * WARN_ON_ONCE(blocked_on && blocked_on != m); }WRITE_ONCE (p->blocked_on,NULL ); + p->blocked_on_type =BLOCKED_ON_NONE ; } -static inline void clear_task_blocked_on (struct task_struct *p,struct mutex *m) +static inline void +clear_task_blocked_on_mutex (struct task_struct *p,struct mutex *m) {guard (raw_spinlock_irqsave)(&m->wait_lock); -__clear_task_blocked_on (p, m); +__clear_task_blocked_on_mutex (p, m); } -#else -static inline void __clear_task_blocked_on (struct task_struct *p,struct rt_mutex *m) + +/* Compatibility wrappers — keep mutex.c callers unchanged */ +static inline struct mutex * +__get_task_blocked_on (struct task_struct *p) +{ +return __get_task_blocked_on_mutex (p); +} + +static inline void +__set_task_blocked_on (struct task_struct *p,struct mutex *m) { +__set_task_blocked_on_mutex (p, m); } -static inline void clear_task_blocked_on (struct task_struct *p,struct rt_mutex *m) +static inline void +set_task_blocked_on (struct task_struct *p,struct mutex *m) +{ +set_task_blocked_on_mutex (p, m); +} + +static inline void +__clear_task_blocked_on (struct task_struct *p,struct mutex *m) { +__clear_task_blocked_on_mutex (p, m); } + +static inline void +clear_task_blocked_on (struct task_struct *p,struct mutex *m) +{ +clear_task_blocked_on_mutex (p, m); +} + +/* --- rwsem write-side blocked_on helpers --- */ + +/* +* __set/clear_task_blocked_on_rwsem: called with sem->wait_lock held. +* Uses void* to avoid pulling struct rw_semaphore into sched.h. +* Callers (rwsem.c) cast sem to void* before passing. +*/ +static inline void +__set_task_blocked_on_rwsem (struct task_struct *p,void *sem) +{ +void *blocked_on =READ_ONCE (p->blocked_on); + +WARN_ON_ONCE (!sem); +/* The task should only be setting itself as blocked */ +WARN_ON_ONCE (p != current); +WARN_ON_ONCE (blocked_on && blocked_on != sem); +WRITE_ONCE (p->blocked_on, sem); + p->blocked_on_type =BLOCKED_ON_RWSEM ; +} + +static inline void +__clear_task_blocked_on_rwsem (struct task_struct *p,void *sem) +{ +if (sem) { +void *blocked_on =READ_ONCE (p->blocked_on); + +WARN_ON_ONCE (blocked_on && blocked_on != sem); + } +WRITE_ONCE (p->blocked_on,NULL ); + p->blocked_on_type =BLOCKED_ON_NONE ; +} + +#else /* CONFIG_PREEMPT_RT */ + +static inline void +__clear_task_blocked_on (struct task_struct *p,struct rt_mutex *m) +{ +} + +static inline void +clear_task_blocked_on (struct task_struct *p,struct rt_mutex *m) +{ +} +#endif /* !CONFIG_PREEMPT_RT */ static __always_inline bool need_resched(void)
kernel/locking/rwsem.c +9 -0
@@ -1154,6 +1154,8 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)if (state ==TASK_UNINTERRUPTIBLE )hung_task_set_blocker (sem,BLOCKER_TYPE_RWSEM_WRITER ); +/* PE: mark this task as blocked on the rwsem write lock */ +__set_task_blocked_on_rwsem (current, sem);for (;;) {if (rwsem_try_write_lock (sem, &waiter)) {
@@ -1187,8 +1189,13 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)set_current_state (state); trylock_again:raw_spin_lock_irq (&sem->wait_lock); +/* PE: re-set blocked_on after wakeup re-acquires wait_lock */ +__set_task_blocked_on_rwsem (current, sem); } +/* PE: clear blocked_on — lock acquired, wait_lock still held */ +__clear_task_blocked_on_rwsem (current, sem); +if (state ==TASK_UNINTERRUPTIBLE )hung_task_clear_blocker ();
@@ -1201,6 +1208,8 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state) out_nolock:__set_current_state (TASK_RUNNING );raw_spin_lock_irq (&sem->wait_lock); +/* PE: clear blocked_on on signal-interrupted exit */ +__clear_task_blocked_on_rwsem (current, sem);rwsem_del_wake_waiter (sem, &waiter, &wake_q);lockevent_inc (rwsem_wlock_fail);trace_contention_end (sem, -EINTR );
kernel/sched/core.c +72 -34
153 lines not on this page · raw message
tools/testing/selftests/sched/proxy_exec_test.c new file +763 -0
763 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 8 lines · On Wed, Mar 4, 2026 at 12:23 AM <[email protected]> wrote:
From: zhidao su <[email protected]>
Proxy Execution currently tracks
blocked_onchains only through struct mutex. This patch extends the infrastructure to supportrw_semaphorewrite-side blocking, allowing PE to eliminate priority inversion where a high-priority writer waits for a low-priority write lock holder.
I'm confused. I've already mentioned Suleiman has implemented rwsem support for proxy back in September:
https://lore.kernel.org/lkml/[email protected]/
Which the latest version can be found here (though I do have fixes pending for v25):
https://github.com/johnstultz-work/linux-dev/commits/proxy-exec-v24-6.18/
Quoted 45 lines
tools/testing/selftests/sched/proxy_exec_test.c new file +39
-0@@ -0,0 +1,763 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Proxy Execution (PE) selftest +* +* Tests for the sched_proxy_exec feature. Verifies that the kernel +* correctly handles RT priority inheritance through proxy execution. +* +* TC-1: Basic PE activation — low-prio holder releases lock for high-prio +* waiter within expected time bound. +* TC-2: Three-level blocked_on chain — PE chains through B->C so that +* A eventually acquires its mutex. +* TC-3: PE deactivate path — SIGSTOP/SIGCONT on holder; high-prio thread +* must still acquire the lock within a generous timeout. +*/ + +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <pthread.h> +#include <sched.h> +#include <errno.h> +#include <signal.h> +#include <time.h> +#include <sys/syscall.h> +#include <sys/types.h> + +static int test_count; + +/* ------------------------------------------------------------------ */ +/* Helpers */ +/* ------------------------------------------------------------------ */ + +/* +* is_proxy_exec_enabled - check whether CONFIG_SCHED_PROXY_EXEC is active +* +* Try to read /proc/sys/kernel/sched_proxy_exec. If the file exists and +* contains a non-zero value the feature is considered enabled. Returns 1
tools/testing/selftests/sched/proxy_exec_test.c line 49
Quoted 10 lines
tools/testing/selftests/sched/proxy_exec_test.c new file +10
-0+* when enabled, 0 otherwise. +*/ +static int is_proxy_exec_enabled (void ) +{ +FILE *f; +int val =0 ; + + f =fopen ("/proc/sys/kernel/sched_proxy_exec" ,"r" ); +if (!f) +return 0 ;
? There is no /proc/sys/kernel/sched_proxy_exec file.
This seems a bit like its AI generated, and wasn't tested. I'd avoid sending stuff like that out to the list.
thanks
-john