mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: K Prateek Nayak <kprateek.nayak@amd.com>
To: John Stultz <jstultz@google.com>, LKML <linux-kernel@vger.kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Juri Lelli <juri.lelli@redhat.com>,
	Valentin Schneider <valentin.schneider@arm.com>,
	Connor O'Brien <connoro@google.com>,
	Ingo Molnar <mingo@redhat.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	"Valentin Schneider" <vschneid@redhat.com>,
	Suleiman Souhlal <suleiman@google.com>,
	Andrea Righi <arighi@nvidia.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	"Metin Kaya" <Metin.Kaya@arm.com>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Zimuzo Ezeozue <zezeozue@google.com>,
	Waiman Long <longman@redhat.com>,
	Boqun Feng <boqun.feng@gmail.com>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	Joel Fernandes <joelagnelf@nvidia.com>,
	"Qais Yousef" <qyousef@layalina.io>,
	Xuewen Yan <xuewen.yan94@gmail.com>,
	kuyo chang <kuyo.chang@mediatek.com>, hupu <hupu.gm@gmail.com>,
	Zhan Xusheng <zhanxusheng@xiaomi.com>,
	"zhidao su (Xiaomi)" <soolaugust@gmail.com>,
	Hui Su <sh_def@163.com>,
	"Shubhang Kaushik (Ampere)" <sh@gentwo.org>,
	<kernel-team@android.com>
Subject: Re: [PATCH v34 14/14] sched/proxy: Add deactivated (sleeping) owner handling to find_proxy_task()
Date: Wed, 2 Dec 2026 14:26:24 +0530	[thread overview]
Message-ID: <4b2f3354-9688-4b36-ad43-dce817cbe985@amd.com> (raw)
In-Reply-To: <20261202035248.1210026-15-jstultz@google.com>

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


      parent reply	other threads:[~2026-12-02  8:56 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  3:52 [PATCH v34 00/14] Sleeping Owner Handling for Proxy Execution (v34) John Stultz
2026-12-02  3:52 ` [PATCH v34 01/14] sched/psi: Preserve sleep-persistent PSI flags on off-CPU sleeping dequeues John Stultz
2026-12-02  3:52 ` [PATCH v34 02/14] sched/core: Use the scheduling context for remote ticks John Stultz
2026-12-02  3:52 ` [PATCH v34 03/14] sched/proxy: Reschedule when a blocked task is woken under a proxy John Stultz
2026-12-02  5:00   ` K Prateek Nayak
2026-12-02  5:06     ` K Prateek Nayak
2026-12-02  6:06       ` John Stultz
2026-12-02  6:24         ` K Prateek Nayak
2026-12-02  6:32           ` John Stultz
2026-12-02  6:49             ` K Prateek Nayak
2026-12-02  5:47     ` John Stultz
2026-12-02  3:52 ` [PATCH v34 04/14] sched/proxy: Wake blocked RT and deadline tasks through the full path John Stultz
2026-12-02  5:37   ` K Prateek Nayak
2026-12-02  3:52 ` [PATCH v34 05/14] sched/proxy: Clear blocked_donor when the donor stops donating John Stultz
2026-12-02  6:10   ` K Prateek Nayak
2026-12-02  6:59     ` K Prateek Nayak
2026-12-02  3:52 ` [PATCH v34 06/14] sched/mutex: Use READ_ONCE/WRITE_ONCE for concurrent blocked_donor accesses John Stultz
2026-12-02  3:52 ` [PATCH v34 07/14] sched/proxy: Avoid transient blocked_donor cycles John Stultz
2026-12-02  6:28   ` K Prateek Nayak
2026-12-02  6:59     ` John Stultz
2026-12-02  3:52 ` [PATCH v34 08/14] sched/proxy: Invoke migrate_task_rq() on proxy-migration John Stultz
2026-12-02  6:39   ` K Prateek Nayak
2026-12-02  3:52 ` [PATCH v34 09/14] sched/core: Activate blocked donor when no owner is found John Stultz
2026-12-02  3:52 ` [PATCH v34 10/14] sched/proxy: Break cyclic proxy chains by deactivating blocked tasks John Stultz
2026-12-02  3:52 ` [PATCH v34 11/14] sched/proxy: Detect cycles in proxy walks earlier John Stultz
2026-12-02  3:52 ` [PATCH v34 12/14] sched/deadline: Try to handle dl bandwidth properly with proxy blocked-waiter activations John Stultz
2026-12-02  3:52 ` [PATCH v34 13/14] rcu-tasks: Treat proxy-exec blocked tasks as voluntary sleeps John Stultz
2026-12-02  7:41   ` K Prateek Nayak
2026-12-02  3:52 ` [PATCH v34 14/14] sched/proxy: Add deactivated (sleeping) owner handling to find_proxy_task() John Stultz
2026-12-02  8:30   ` Zhan Xusheng
2026-12-02  8:56   ` K Prateek Nayak [this message]

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=4b2f3354-9688-4b36-ad43-dce817cbe985@amd.com \
    --to=kprateek.nayak@amd.com \
    --cc=Metin.Kaya@arm.com \
    --cc=arighi@nvidia.com \
    --cc=boqun.feng@gmail.com \
    --cc=connoro@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=hannes@cmpxchg.org \
    --cc=hupu.gm@gmail.com \
    --cc=joelagnelf@nvidia.com \
    --cc=jstultz@google.com \
    --cc=juri.lelli@redhat.com \
    --cc=kernel-team@android.com \
    --cc=kuyo.chang@mediatek.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=qyousef@layalina.io \
    --cc=rostedt@goodmis.org \
    --cc=sh@gentwo.org \
    --cc=sh_def@163.com \
    --cc=soolaugust@gmail.com \
    --cc=suleiman@google.com \
    --cc=tglx@linutronix.de \
    --cc=valentin.schneider@arm.com \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    --cc=xuewen.yan94@gmail.com \
    --cc=zezeozue@google.com \
    --cc=zhanxusheng@xiaomi.com \
    /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®