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>,
	Suleiman Souhlal <suleiman@google.com>,
	Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	"Juri Lelli" <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Will Deacon <will@kernel.org>, Boqun Feng <boqun@kernel.org>,
	Andrea Righi <arighi@nvidia.com>, <linux-kernel@vger.kernel.org>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	Waiman Long <longman@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>
Subject: [RFC PATCH 13/16] sched/core: Use p->is_linked state to unlink from sleeping owner early
Date: Wed, 26 Aug 2026 06:28:57 +0000	[thread overview]
Message-ID: <20260826062901.2137-14-kprateek.nayak@amd.com> (raw)
In-Reply-To: <20260826062901.2137-1-kprateek.nayak@amd.com>

Use p->is_linked indicator to unlink the task from a sleeping owner's
blocked node early and continue to proxy_needs_return() on the wakeup
path.

proxy_needs_return() is now the single point of proxy donor handling on
the wakeup path. The cases that handle enqueue of owner is as follows:

  if (!owner->on_rq) {
    guard(raw_spinlock)(&owner->blocked_lock)	activate_task(owner)
    enqueue_on_owner()				  owner->on_rq = QUEUED / MIGRATING
    smp_mb()					  smp_mb()
    if (owner->on_rq) /* Raced */		  if (!list_empty(owner->blocked_head))
      dequeue_from_owner()			    guard(raw_spinlock)(&owner->blocked_lock)
						    activate_blocked_donor()

Case that handles concurrent activation of blocked_donor is as follows:

  try_to_wake_up(p)
    guard(raw_spinlock)(&p->pi_lock)
      /* State matches */
      if (p->on_rq || p->is_linked)	activate_blocked_donor(owner)
        ttwu_runnable()			  if (!list_empty(owner->blocked_head)
          task_rq_lock(p)		    rq_lock(cpu_rq(owner->blocked_cpu))
					    /* Same as task_rq(p) */

Either activate_blocked_donor() wins and activates task or
ttwu_runnable() wins and completes the wakeup after dequeuing from
sleeping owner.

activate_blocked_donor() changes are added in the subsequent patch.

Signed-off-by: K Prateek Nayak <kprateek.nayak@amd.com>
---
 kernel/sched/core.c | 176 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 174 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index ff499d4532ad..e6abcea97683 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2252,6 +2252,96 @@ static void __proxy_dequeue_from_owner(struct task_struct *p)
 	list_del_init(&p->blocked_node);
 	WRITE_ONCE(p->sleeping_owner, NULL);
 	WRITE_ONCE(p->is_linked, 0);
+	/*
+	 * p->blocked_cpu is intentionally left un-altered
+	 * since the wakeup path requires it to grab the
+	 * correct rq for chain wakeup if we are here from
+	 * the activation path.
+	 *
+	 * "p->blocked_cpu" only has significance for blocked
+	 * task on "blocked_head" and a stale value in the
+	 * field is harmless.
+	 */
+}
+
+static void proxy_dequeue_from_owner(struct task_struct *p)
+{
+	struct task_struct *owner = p->sleeping_owner;
+
+	scoped_guard(raw_spinlock, &owner->blocked_lock) {
+		WARN_ON_ONCE(p->sleeping_owner != owner);
+		__proxy_dequeue_from_owner(p);
+	}
+}
+
+static bool proxy_try_dequeue_from_owner(struct task_struct *p)
+{
+	struct task_struct *owner = READ_ONCE(p->sleeping_owner);
+
+	/*
+	 * Task is outside __task_rq_lock(). These fields
+	 * can race with a chain-wakeup.
+	 */
+	if (!owner || READ_ONCE(p->on_rq))
+		return false;
+
+	/*
+	 * The chain-wakeup slowpath grabs both the rq_lock() and the
+	 * owner->blocked_lock while removing the tasks on
+	 * owner->blocked_head.
+	 *
+	 * If there is no concurrent chain-wakeup for this task, it is
+	 * safe to just hold the owner->blocked_lock which serializes
+	 * the removal like:
+	 *
+	 * proxy_try_dequeue_from_owner(donor)	proxy_activate_blocked_donor(owner)
+	 *					  LOCK rq_lock(p->blocked_cpu)
+	 *   LOCK &owner->blocked_lock		  LOCK &owner->blocked_lock
+	 *
+	 *     # Defensive checks		    STORE donor->on_rq = MIGRATING
+	 *     __proxy_dequeue_from_owner()	    smp_mb()
+	 *
+	 *   UNLOCK &owner->blocked_lock	  UNLOCK &owner->blocked_lock
+	 *
+	 *   # If proxy_try_dequeue fails
+	 *   __task_rq_lock(donor)		  __proxy_dequeue_from_owner(donor)
+	 *     if (donor->on_rq == MIGRATING)	  __activate_task(donor)
+	 *       cpu_relax()			    STORE p->on_rq = 1
+	 *
+	 *   # Sees consistent state.
+	 *
+	 * Either proxy_activate_blocked_donor() manages to dequeue the donor
+	 * and stall the __task_rq_lock() until the task is actually stable
+	 * or proxy_try_dequeue_from_owner() wins and manages to remove the
+	 * task from "owner->blocked_head" first.
+	 */
+	scoped_guard(raw_spinlock, &owner->blocked_lock) {
+		/*
+		 * Chain-wakeup has raced to queue the task.
+		 * Try again with __task_rq_lock() held.
+		 */
+		if (READ_ONCE(p->on_rq))
+			return false;
+
+		/*
+		 * Something changes in the link. Try again with
+		 * __task_rq_lock() held via proxy_needs_return()
+		 * where both p->on_rq and p->is_linked is stable.
+		 */
+		if (!READ_ONCE(p->is_linked) || owner != READ_ONCE(p->sleeping_owner))
+			return false;
+
+		__proxy_dequeue_from_owner(p);
+	}
+
+	return true;
+}
+
+#else /* !CONFIG_SCHED_PROXY_EXEC */
+
+static bool proxy_try_dequeue_from_owner(struct task_struct *p)
+{
+	return false;
 }
 
 #endif /* CONFIG_SCHED_PROXY_EXEC */
@@ -3799,6 +3889,40 @@ void __proxy_block_task(struct task_struct *p)
 	if (!sched_proxy_exec())
 		return;
 
+	if (unlikely(p->is_linked && p->blocked_cpu != task_cpu(p))) {
+		WRITE_ONCE(p->on_rq, TASK_ON_RQ_MIGRATING);
+		ASSERT_EXCLUSIVE_WRITER(p->on_rq);
+
+		/* Move iowait signal to new CPU. */
+		if (p->in_iowait) {
+			atomic_dec(&task_rq(p)->nr_iowait);
+			atomic_inc(&cpu_rq(p->blocked_cpu)->nr_iowait);
+		}
+
+		/*
+		 * Moved the linked task to p->blocked_cpu which is now
+		 * resposnible for chain wakeup. TASK_ON_RQ_MIGRATING
+		 * above stalls task_rq_lock() until p->on_rq is
+		 * transitioned to 0 (See ___task_rq_lock()).
+		 */
+		proxy_set_task_cpu(p, p->blocked_cpu);
+
+		/*
+		 * Preserve p->blocked_cpu for any potential future
+		 * additions to p->blocked_head. The chain should be
+		 * re-directed to the same "blocked_cpu".
+		 *
+		 * XXX: Technically, it is possible to keep the
+		 * "blocked_cpu" for this sub-chain different
+		 * compared to our ancestors but the chain-wakeup
+		 * would need to juggle multiple locks then to
+		 * ensure it doesn't race with concurrent wakeups.
+		 * Matching "blocked_cpu" requires only a single
+		 * rq_lock which is simpler.
+		 */
+		return;
+	}
+
 	/*
 	 * This is the CPU where the __active_blocked_donor() slowpath will
 	 * resume activation of blocked donors queued on this task.
@@ -3819,6 +3943,25 @@ void __proxy_block_task(struct task_struct *p)
  */
 static inline bool proxy_needs_return(struct rq *rq, struct task_struct *p)
 {
+	if (!sched_proxy_exec())
+		return false;
+
+	/* See try_to_wake_up() for ordering guarantees. */
+	if (unlikely(p->is_linked)) {
+		/*
+		 * Task is queued on an owner's blocked_node.
+		 *
+		 * Dequeue from owner's blocked_node before going
+		 * further. Do not take the fast-path since this task is
+		 * fully blocked and it is necessary to clear
+		 * p->blocked_on indicator before waking up.
+		 */
+		WARN_ON_ONCE(p->on_rq);
+		proxy_dequeue_from_owner(p);
+		clear_task_blocked_on(p, NULL);
+		return true;
+	}
+
 	/*
 	 * Typically per __set_task_cpu(), task_cpu(p) == p->wake_cpu.
 	 *
@@ -3922,10 +4065,34 @@ ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags,
  */
 static int ttwu_runnable(struct task_struct *p, int wake_flags)
 {
+	struct rq *rq;
+
+	if (sched_proxy_exec() && READ_ONCE(p->is_linked)) {
+		/*
+		 * If p->is_linked put us on the ttwu_runnable() path,
+		 * try to de-link the blocked donor outside the
+		 * __task_rq_lock() first.
+		 *
+		 * If the task state changed, a wakeup raced with
+		 * ttwu_runnable() (see proxy_try_dequeue_from_owner()
+		 * for interleaving). Try again with rq_lock held where
+		 * state is guaranteed to be stable.
+		 */
+		if (proxy_try_dequeue_from_owner(p)) {
+			WARN_ON_ONCE(READ_ONCE(p->on_rq));
+			clear_task_blocked_on(p, NULL);
+			return 0;
+		}
+	}
+
 	ACQUIRE(__task_rq_lock, guard)(p);
-	struct rq *rq = guard.rq;
+	rq = guard.rq;
 
-	if (!task_on_rq_queued(p))
+	/*
+	 * schedule() managed to block the task and there is
+	 * no unlinking needed. Continue with wakeup.
+	 */
+	if (!p->needs_rq_sync)
 		return 0;
 
 	update_rq_clock(rq);
@@ -6972,6 +7139,11 @@ static void proxy_enqueue_on_owner(struct rq *rq, struct task_struct *owner,
 	WARN_ON(!p->on_rq);
 	WARN_ON(p->sleeping_owner);
 
+	/*
+	 * See __proxy_block_task() on how p->blocked_cpu is used
+	 * to re-direct the task to a handler CPU when
+	 */
+	WRITE_ONCE(p->blocked_cpu, READ_ONCE(owner->blocked_cpu));
 	WRITE_ONCE(p->sleeping_owner, owner);
 	list_add(&p->blocked_node, &owner->blocked_head);
 	WRITE_ONCE(p->is_linked, 1);
-- 
2.34.1


  parent reply	other threads:[~2026-08-26  6:34 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-26  6:28 [RFC PATCH 00/16][PoC] sched/core: Alternate approach to sleeping-owner handling in PROXY_EXEC K Prateek Nayak
2026-08-26  6:28 ` [RFC PATCH 01/16] sched/core: Break activation of blocked task into a separate helper K Prateek Nayak
2026-08-26  6:28 ` [RFC PATCH 02/16] sched/core: Use enqueue/dequeue flags instead of task_on_rq_migrating() K Prateek Nayak
2026-08-26  6:28 ` [RFC PATCH 03/16] sched/fair: Use enqueue flags for DO_ATTACH in update_load_avg() K Prateek Nayak
2026-08-26 15:36   ` Andrea Righi
2026-08-26  6:28 ` [RFC PATCH 04/16] sched/core: Activate blocked donor when no owner is found K Prateek Nayak
2026-08-26 15:56   ` Andrea Righi
2026-08-26 17:20     ` K Prateek Nayak
2026-08-28  6:04       ` K Prateek Nayak
2026-08-31 15:07         ` Andrea Righi
2026-09-16  4:16           ` K Prateek Nayak
2026-09-16  3:29   ` John Stultz
2026-09-16  4:16     ` K Prateek Nayak
2026-08-26  6:28 ` [RFC PATCH 05/16] sched/core: Do not queue blocked donor on a delayed owner K Prateek Nayak
2026-08-26  6:28 ` [RFC PATCH 06/16] sched/core: Queue blocked donor onto sleeping owner for chain-wakeup K Prateek Nayak
2026-08-26 16:20   ` Andrea Righi
2026-08-27  3:51     ` K Prateek Nayak
2026-08-26  6:28 ` [RFC PATCH 07/16] sched/core: Avoid delaying blocked donors queued on sleeping owner K Prateek Nayak
2026-08-26  6:28 ` [RFC PATCH 08/16] sched/deadline: Prepare for blocking and proxy activation with MIGRATING flag K Prateek Nayak
2026-08-26  6:28 ` [RFC PATCH 09/16] sched/core: Track CPU where the task was blocked on K Prateek Nayak
2026-09-16  5:52   ` John Stultz
2026-09-16  6:29     ` K Prateek Nayak
2026-08-26  6:28 ` [RFC PATCH 10/16] sched/core: Introduce p->is_linked to track if task is queued on sleeping owner K Prateek Nayak
2026-08-26  6:28 ` [RFC PATCH 11/16] sched/core: Prepare to inspect ->is_linked alongside ->on_rq during wakeup K Prateek Nayak
2026-08-26  6:28 ` [RFC PATCH 12/16] sched:core: Add MIGRATING flags when blocking and activating linked donors K Prateek Nayak
2026-08-26  6:28 ` K Prateek Nayak [this message]
2026-08-26  6:28 ` [RFC PATCH 14/16] sched/core: Introduce chain-wakeup to activate blocked donors K Prateek Nayak
2026-09-15  5:48   ` John Stultz
2026-08-26  6:28 ` [RFC PATCH 15/16] locking/mutex: Track locks owned by a task in a per-task counter K Prateek Nayak
2026-08-26  6:29 ` [RFC PATCH 16/16] sched/core: Set activation of non lock-holders to fast-path K Prateek Nayak
2026-09-16  5:22 ` [RFC PATCH 00/16][PoC] sched/core: Alternate approach to sleeping-owner handling in PROXY_EXEC John Stultz
2026-09-16  6:10   ` K Prateek Nayak
2026-09-16  6:23     ` John Stultz
2026-09-16  6:59       ` K Prateek Nayak
2026-09-16 17:57   ` John Stultz
2026-09-17  5:22     ` K Prateek Nayak

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=20260826062901.2137-14-kprateek.nayak@amd.com \
    --to=kprateek.nayak@amd.com \
    --cc=arighi@nvidia.com \
    --cc=boqun@kernel.org \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=jstultz@google.com \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=suleiman@google.com \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    --cc=will@kernel.org \
    /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®