Re: [PATCH v34 14/14] sched/proxy: Add deactivated (sleeping) owner handling to find_proxy_task()
From: K Prateek Nayak
Date: Wed Dec 02 2026 - 03:56:51 EST
Hello John,
On 12/2/2026 9:22 AM, John Stultz wrote:
> +static void do_activate_blocked_waiter(struct rq *target_rq, struct task_struct *p, int en_flags)
> +{
> + struct rq_flags rf;
> + int target_cpu = cpu_of(target_rq);
> +
> + scoped_guard (raw_spinlock_irqsave, &p->pi_lock) {
> + scoped_guard (raw_spinlock, &p->blocked_lock) {
> + int state = READ_ONCE(p->__state);
> +
> + /* If task is no longer blocked, don't activate */
> + if (!READ_ONCE(p->is_blocked))
> + return;
> +
> + /* Avoid racing with ttwu */
> + if (state == TASK_WAKING)
> + return;
> +
> + /* Dead tasks don't activate */
> + if (state == TASK_DEAD || READ_ONCE(p->exit_state))
> + return;
> +
> + /*
> + * We raced with a non mutex handoff activation of p.
> + * That activation will also take care of activating
> + * all of the tasks after p in the blocked_head list,
> + * so we're done here.
> + */
State checks vs p->on_rq check needs to be ordered with smp_mb()
too based on __task_needs_rq_lock() and ...
> + if (READ_ONCE(p->on_rq))
> + return;
> +
> + /*
> + * Its possible this activation is very late, and
> + * we already were woken up and are running on a
> + * different cpu. If that task blocked, it could be
> + * dequeued (so on_rq == 0), but still on_cpu.
> + * Bail in this case, as we definitely don't want to
> + * activate a task when its on_cpu elsewhere.
> + *
> + * Use smp_load_acquire since we don't hold rq lock.
> + */
> + if (smp_load_acquire(&p->on_cpu))
... isn't smp_load_acquire() basically:
READ_ONCE(p->on_cpu)
smp_mb()
? I think the patten here should be:
READ_ONCE(p->on_rq)
smp_mb();
smp_cond_load_acquire(p->on_cpu, !VAL);
> + return;
> +
> + /*
> + * Again, if activation is delayed, it's possible the
> + * task has been woken and is no longer blocked_on,
> + * but went back to sleep, so double check blocked_on
> + * before activating onto the owner's rq
> + */
> + if (!task_is_blocked(p))
> + return;
> +
> + /*
> + * This was cleared right before calling
> + * do_activate_blocked_waiter(), so if it is set we've
> + * woken, and went on to queue on a different sleeping
> + * owner. They will activate us later when its
> + * appropriate, so bail now.
> + */
> + if (READ_ONCE(p->sleeping_owner))
> + return;
> + }
> +
> + if (task_cpu(p) != target_cpu) {
> + en_flags |= ENQUEUE_MIGRATED;
> + psi_ttwu_dequeue(p);
> + proxy_set_task_cpu(p, target_cpu);
> + }
> + rq_lock_irqsave(target_rq, &rf);
> + /*
> + * proxy_enqueue_on_owner() called block_task() which
> + * increments nr_uninterruptible, so we need to reverse
> + * that when we activate the blocked waiter
> + */
> + if (p->sched_contributes_to_load)
> + target_rq->nr_uninterruptible--;
> + update_rq_clock(target_rq);
> + activate_task(target_rq, p, en_flags);
> + wakeup_preempt(target_rq, p, 0);
> + rq_unlock_irqrestore(target_rq, &rf);
> + }
> +}
> +
> +static void activate_blocked_waiters(struct rq *target_rq,
> + struct task_struct *owner,
> + int wake_flags)
> +{
> + struct list_head bal_head;
> + unsigned long flags;
> + int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK;
> +
> + if (!sched_proxy_exec())
> + return;
> +
> + /*
> + * A whole bunch of waiting donor tasks back this blocked
> + * lock owner task, wake them all up to give this task its
> + * 'fair' share.
> + *
> + * This is a little unique here and the locking is messy.
> + * At this point we only hold the blocked_lock, so the
> + * owner task may be able to run and do all sorts of
> + * things while we are processing the blocked_head list,
> + * including going back to sleep, which can cause tasks
> + * to be added to the owners->blocked_head while we are
> + * processing it!
> + * Thus, we pull the entire list off the owner->blocked_head
> + * here so that we will only process a finite amount of
> + * tasks. Tasks added after this will be processed by the
> + * future wake events.
> + * Even though we have pulled the list off the blocked_head
> + * the removed list is *still* "owned" and serialized by
> + * the owner->blocked_lock! As we have to serialize against
> + * mid-chain wakeups, who may try to remove themselves from
> + * the list.
> + */
> + raw_spin_lock_irqsave(&owner->blocked_lock, flags);
> + if (!list_empty(&owner->blocked_activation_node)) {
> + raw_spin_unlock_irqrestore(&owner->blocked_lock, flags);
> + return;
> + }
> +
> + if (list_empty(&owner->blocked_head)) {
> + raw_spin_unlock_irqrestore(&owner->blocked_lock, flags);
> + return;
> + }
> +
> + get_task_struct(owner);
> + INIT_LIST_HEAD(&bal_head);
> + list_add_tail(&owner->blocked_activation_node, &bal_head);
> + raw_spin_unlock_irqrestore(&owner->blocked_lock, flags);
> +
> + while (!list_empty(&bal_head)) {
> + struct list_head tmp_head;
> +
> + INIT_LIST_HEAD(&tmp_head);
> + owner = list_first_entry(&bal_head, struct task_struct, blocked_activation_node);
> +
> + raw_spin_lock_irqsave(&owner->blocked_lock, flags);
> + list_replace_init(&owner->blocked_head, &tmp_head);
> + list_del_init(&owner->blocked_activation_node);
> + while (!list_empty(&tmp_head)) {
> + struct task_struct *p;
> +
> + p = list_first_entry(&tmp_head,
> + struct task_struct,
> + blocked_node);
> + WARN_ON(p == owner);
> + get_task_struct(p);
> + __proxy_remove_from_sleeping_owner(owner, p);
> + raw_spin_unlock_irqrestore(&owner->blocked_lock, flags);
> +
> + do_activate_blocked_waiter(target_rq, p, en_flags);
> +
> + raw_spin_lock_irqsave(&p->blocked_lock, flags);
> + if (list_empty(&p->blocked_activation_node)) {
> + get_task_struct(p);
> + list_add_tail(&p->blocked_activation_node, &bal_head);
> + }
> + raw_spin_unlock_irqrestore(&p->blocked_lock, flags);
> + put_task_struct(p);
> +
> + raw_spin_lock_irqsave(&owner->blocked_lock, flags);
> + }
> + raw_spin_unlock_irqrestore(&owner->blocked_lock, flags);
> + put_task_struct(owner); // put matches get prior to adding to local bal_head
> + }
> +}
> +
> +static inline struct task_struct *proxy_resched_idle(struct rq *rq);
> +
> /*
> * Checks to see if task p has been proxy-migrated to another rq
> * and needs to be returned. If so, we deactivate the task here
> @@ -3931,8 +4189,7 @@ static inline bool proxy_needs_return(struct rq *rq, struct task_struct *p)
> if (task_current(rq, p))
> return false;
> }
> -
> - dequeued = dequeue_block_task(rq, p, TASK_WAKING);
> + dequeued = dequeue_block_task(rq, p, TASK_WAKING, 0);
>
> /*
> * Dequeue @p from its scheduling class before resetting rq->donor.
> @@ -3954,6 +4211,10 @@ static inline bool proxy_needs_return(struct rq *rq, struct task_struct *p)
> {
> return false;
> }
> +
> +static inline void activate_blocked_waiters(struct rq *target_rq,
> + struct task_struct *owner,
> + int wake_flags) {}
> #endif /* CONFIG_SCHED_PROXY_EXEC */
>
> static void
> @@ -4023,8 +4284,10 @@ static int ttwu_runnable(struct task_struct *p, int wake_flags)
>
> update_rq_clock(rq);
> if (p->is_blocked) {
> - if (p->se.sched_delayed)
> + if (p->se.sched_delayed) {
> + proxy_remove_from_sleeping_owner(p);
> enqueue_task(rq, p, ENQUEUE_NOCLOCK | ENQUEUE_DELAYED);
> + }
> /*
> * If we're waking a blocked task and we are
> * currently proxying, force a resched in case
> @@ -4411,6 +4674,7 @@ int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
> {
> guard(preempt)();
> int cpu, success = 0;
> + bool woken = false;
>
> wake_flags |= WF_TTWU;
>
> @@ -4438,6 +4702,7 @@ int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
>
> trace_sched_waking(p);
> ttwu_do_wakeup(p);
> + woken = true;
This branch cannot have "p->is_blocked" so it probably doesn't
require sleeping owner handling right?
> goto out;
> }
>
> @@ -4453,6 +4718,7 @@ int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
> if (!ttwu_state_match(p, state, &success))
> break;
>
> + woken = true;
> trace_sched_waking(p);
>
> /*
> @@ -4568,6 +4834,8 @@ int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
> ttwu_queue(p, cpu, wake_flags);
> }
> out:
> + if (woken)
> + activate_blocked_waiters(cpu_rq(task_cpu(p)), p, wake_flags);
I think there is a very convoluted race possible here like:
/* Wakeup from task context. */
try_to_wake_up()
ttwu_queue_wakelist()
/* sends IPI */ ---------------> ttwu_do_activate(owner)
activate_task(owner)
/* stalls */ /* Owner runs */
mutex_unlock()
wake_up_process(sleeping_donor)
activate_task()
proxy_remove_from_sleeping_owner()
put_task_struct(owner)
/* &owner->usage is 1 after this */
/* Owner dies */
finish_task_switch()
if (prev_state == TASK_DEAD)
put_task_struct_rcu_user(prev = owner);
/* owner->usage turns 0 */
/* Queues RCU callback to free owner */
/*Back */
raw_spin_rq_lock_irqrestore(p->pi_lock);
/* Preempted; RCU grace period done. */
activate_blocked_waiters(owner)
/* !!! Use after free for owner !!! */
I suppose it is also possible for any remote wakeup and not necessarily
dependent on TTWU_QUEUE bit. Am I missing something that prevents this?
One way to get around this is to do a guard(irqsave)() before grabbing
the &p->pi_lock and that way, you should have an RCU-protected reference
for the entirety of activate_blocked_waiters().
IRQs will be disabled for the duration of chain wakeup but that is
also true today if the owner wakes up from an IRQ handler so it
should be okay?
> if (success)
> ttwu_stat(p, task_cpu(p), wake_flags);
>
--
Thanks and Regards,
Prateek