Re: [PATCH v2] sched/proxy_exec: Break cyclic proxy chains by deactivating blocked tasks

From: K Prateek Nayak

Date: Tue Jul 14 2026 - 16:46:51 EST


Hello Zhidao,

On 7/14/2026 8:51 PM, soolaugust@xxxxxxxxx wrote:
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 2e7cde033a319..cff1a3b58660b 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -6724,6 +6724,8 @@ static bool try_to_block_task(struct rq *rq, struct task_struct *p,
> }
>
> #ifdef CONFIG_SCHED_PROXY_EXEC
> +#define MAX_PROXY_CHAIN_DEPTH 1024

I'm not really a fan of the arbitrary MAX_PROXY_CHAIN_DEPTH.

> +
> static inline void proxy_set_task_cpu(struct task_struct *p, int cpu)
> {
> unsigned int wake_cpu;
> @@ -6873,6 +6875,7 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)
> int this_cpu = cpu_of(rq);
> struct task_struct *p;
> int owner_cpu;
> + int chain_depth = 0;
>
> /* Follow blocked_on chain. */
> for (p = donor; p->is_blocked; p = owner) {
> @@ -6905,6 +6908,13 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)
> return NULL;
> }
>
> + if (++chain_depth > MAX_PROXY_CHAIN_DEPTH) {
> + WARN_ONCE(1, "sched/pe: proxy chain depth exceeded %d, pid %d\n",
> + MAX_PROXY_CHAIN_DEPTH, p->pid);
> + __clear_task_blocked_on(p, NULL);
> + goto deactivate;
> + }
> +
> if (task_current(rq, p))
> curr_in_chain = true;
>
> @@ -6923,6 +6933,13 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)
> goto deactivate;
> }
>
> + if (owner == donor) {

So let us take a step back. Say you have a genuine case of:

A -> B -> C -> D
^ |
| |
+---------+

donor = A
Cycle is somewhere in-between the chain

Now, we have a "p->blocked_donor" back link that we can probably
leverage to detect this cycle except we don't clear a
p->blocked_donor link when find_proxy_task() returns idle or NULL
so we can have stale p->blocked_donor links for queued tasks but
they are so far harmless.

Me goes and thinks ...

Since p->blocked_donor isn't used for chain migration yet, does
something like below help your deadlock case?

(Prepared on top of tip:sched/core; Lightly tested)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 2e7cde033a31..fa1d9443f744 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -6874,6 +6874,9 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)
struct task_struct *p;
int owner_cpu;

+ /* Increment proxy_pick_seq such that last 4 bits are always set. */
+ rq->proxy_pick_seq += 0x10;
+
/* Follow blocked_on chain. */
for (p = donor; p->is_blocked; p = owner) {
/* if its PROXY_WAKING, do return migration or run if current */
@@ -6990,12 +6993,28 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)
*/
return proxy_resched_idle(rq);
}
+
+ /* Cyclic deadlock detection. */
+ if (((u64)owner->blocked_donor) & 0xF) {
+ u64 pick_seq = (u64)owner->blocked_donor;
+
+ /*
+ * Owner was already seen during this find_proxy_task() loop.
+ * Deactivate the task since there is a loop in proxy-chain.
+ */
+ if (rq->proxy_pick_seq == pick_seq) {
+ __clear_task_blocked_on(p, NULL);
+ goto deactivate;
+ }
+ }
+
/*
* OK, now we're absolutely sure @owner is on this
* rq, therefore holding @rq->lock is sufficient to
* guarantee its existence, as per ttwu_remote().
*/
owner->blocked_donor = p;
+ p->blocked_donor = (void *)rq->proxy_pick_seq;
}
WARN_ON_ONCE(owner && !owner->on_rq);
return owner;
@@ -9057,6 +9076,9 @@ void __init sched_init(void)
raw_spin_lock_init(&rq->cpu_epoch_lock);
rq->cpu_epoch_next = jiffies;
#endif
+#ifdef CONFIG_SCHED_PROXY_EXEC
+ rq->proxy_pick_seq = 0xf;
+#endif

zalloc_cpumask_var_node(&rq->scratch_mask, GFP_KERNEL, cpu_to_node(i));
}
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 26ae13c86b69..452cbcd3a8c5 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1154,6 +1154,7 @@ struct rq {
#ifdef CONFIG_SCHED_PROXY_EXEC
struct task_struct __rcu *donor; /* Scheduling context */
struct task_struct __rcu *curr; /* Execution context */
+ u64 proxy_pick_seq; /* find_proxy_task() seq */
#else
union {
struct task_struct __rcu *donor; /* Scheduler context */
---
base-commit: 04998aa54848f15332202d0bea008d2ca1ed1713

This assumes task_struct pointers are 4-bytes aligned.

Only rq->curr->blocked_donor matters currently so it should be okay
to clobber the rest of the chain. If this is okay, perhaps we can
just stash the pick_seq copy in task_struct.

Thoughts?

> + WARN_ONCE(1, "sched/pe: deadlock cycle detected, pid %d\n",
> + p->pid);
> + __clear_task_blocked_on(p, NULL);
> + goto deactivate;
> + }
> +
> if (!READ_ONCE(owner->on_rq) || owner->se.sched_delayed) {
> /* XXX Don't handle blocked owners/delayed dequeue yet */
> if (curr_in_chain)

--
Thanks and Regards,
Prateek