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 11/16] sched/core: Prepare to inspect ->is_linked alongside ->on_rq during wakeup
Date: Wed, 26 Aug 2026 06:28:55 +0000	[thread overview]
Message-ID: <20260826062901.2137-12-kprateek.nayak@amd.com> (raw)
In-Reply-To: <20260826062901.2137-1-kprateek.nayak@amd.com>

To handle wakeup of blocked donors queued on owner's blocked_head via
ttwu_runnable(), prepare to inspect p->is_blocked in addition to
p->on_rq on paths that demand grabbing rq_lock to prevent "->on_rq"
transitions.

Unionize p->on_rq with p->is_linked to derive a 16-bit p->needs_rq_sync
state that can be atomically inspected on all architectures.
p->needs_rq_sync check follows the same ordering requirements as
p->on_rq check.

Subsequent tasks will guarntee ordering of p->is_linked against p->on_rq
transitions ensuring that loading p->needs_rq_sync state is enough to
cover both and the readers don't falsely observe them to be cleared if
read individually during their transition.

get_wchan() has been reflowed using guards as a pert of the change to
prevent awkward indentation.

Signed-off-by: K Prateek Nayak <kprateek.nayak@amd.com>
---
 include/linux/sched.h | 18 +++++++++++++--
 kernel/sched/core.c   | 53 +++++++++++++++++++++++++++++++++++++++----
 2 files changed, 64 insertions(+), 7 deletions(-)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index ec65abf090a4..76de348fb88b 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -852,9 +852,23 @@ struct task_struct {
 	struct alloc_tag		*alloc_tag;
 #endif
 
+	union {
+		struct {
+			u8		on_rq;
+			u8		is_linked;
+		};
+		/*
+		 * Allows inspecting on_rq and is_linked
+		 * atomically with a single read outside
+		 * the task_rq_lock().
+		 *
+		 * See the comment above the load in
+		 * try_to_wake_up() for ordering
+		 * guarantees.
+		 */
+		u16			needs_rq_sync;
+	};
 	u8				on_cpu;
-	u8				on_rq;
-	u8				is_linked;
 	u8				is_blocked;
 
 	struct __call_single_node	wake_entry;
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index d3cdfcb400d1..957f30632b82 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2171,12 +2171,16 @@ unsigned long get_wchan(struct task_struct *p)
 		return 0;
 
 	/* Only get wchan if task is blocked and we can keep it that way. */
-	raw_spin_lock_irq(&p->pi_lock);
+	guard(raw_spinlock_irq)(&p->pi_lock);
+
 	state = READ_ONCE(p->__state);
+	if (state == TASK_RUNNING || state == TASK_WAKING)
+		return 0;
+
 	smp_rmb(); /* see try_to_wake_up() */
-	if (state != TASK_RUNNING && state != TASK_WAKING && !p->on_rq)
+
+	if (!READ_ONCE(p->needs_rq_sync))
 		ip = __get_wchan(p);
-	raw_spin_unlock_irq(&p->pi_lock);
 
 	return ip;
 }
@@ -4365,10 +4369,49 @@ int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
 		 * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in
 		 * __schedule().  See the comment for smp_mb__after_spinlock().
 		 *
+		 * Additionally, this also guards against activation of blocked
+		 * donors queued on a sleeping owner when a wakeup races for
+		 * the same task with sched_proxy_exec().
+		 *
+		 * proxy_enqueue_on_owner()
+		 *   STORE p->is_linked = 1
+		 *
+		 *   block_task()			try_to_wake_up()
+		 *     smp_mb()
+		 *     STRORE p->on_rq = 0		  LOAD p->needs_rq_sync
+		 *
+		 * The read below will atomically observe either p->on_rq or
+		 * p->is_linked being set and put the task on ttwu_runnable()
+		 * path.
+		 *
+		 * On the chain-wakeup path, p->on_rq is first transitioned to
+		 * TASK_ON_RQ_MIGRATING before p->is_linked is cleared and the
+		 * task is transitioned to TASK_ON_RQ_QUEUED
+		 *
+		 * activate_blocked_task()
+		 *   task_rq_lock()			ttwu_runnable()
+		 *   STORE donor->on_rq = MIGRATING	  task_rq_lock()
+		 *					    # STALL
+		 *   smp_wmb();
+		 *
+		 *   proxy_dequeue_from_owner()
+		 *     STORE donor->is_linked = 0
+		 *
+		 *   activate_task()
+		 *     STORE donor->on_rq = QUEUED
+		 *
+		 *   rq_unlock()			    # ACQUIRED
+		 *					  LOAD donor->needs_rq_sync
+		 *
+		 * In this case too try_to_wake_up() will correctly observe
+		 * either p->on_rq != 0 or p->is_linked != 0 by atomically
+		 * reading p->needs_rq_sync when the task is being activated as
+		 * a part of chain wakeup.
+		 *
 		 * A similar smp_rmb() lives in __task_needs_rq_lock().
 		 */
 		smp_rmb();
-		if (READ_ONCE(p->on_rq) && ttwu_runnable(p, wake_flags))
+		if (READ_ONCE(p->needs_rq_sync) && ttwu_runnable(p, wake_flags))
 			break;
 
 		/*
@@ -4488,7 +4531,7 @@ static bool __task_needs_rq_lock(struct task_struct *p)
 	 * See try_to_wake_up() for a longer comment.
 	 */
 	smp_rmb();
-	if (p->on_rq)
+	if (READ_ONCE(p->needs_rq_sync))
 		return true;
 
 	/*
-- 
2.34.1


  parent reply	other threads:[~2026-08-26  6:33 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 ` K Prateek Nayak [this message]
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 ` [RFC PATCH 13/16] sched/core: Use p->is_linked state to unlink from sleeping owner early K Prateek Nayak
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-12-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®