The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: John Stultz <jstultz@google.com>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Juri Lelli <juri.lelli@redhat.com>,
	Valentin Schneider <valentin.schneider@arm.com>,
	Connor O'Brien <connoro@google.com>,
	Joel Fernandes <joelagnelf@nvidia.com>,
	Qais Yousef <qyousef@layalina.io>, Ingo Molnar <mingo@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Valentin Schneider <vschneid@redhat.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>,
	Zimuzo Ezeozue <zezeozue@google.com>,
	Mel Gorman <mgorman@suse.de>, Will Deacon <will@kernel.org>,
	Waiman Long <longman@redhat.com>,
	Boqun Feng <boqun.feng@gmail.com>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	Metin Kaya <Metin.Kaya@arm.com>,
	Xuewen Yan <xuewen.yan94@gmail.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Daniel Lezcano <daniel.lezcano@linaro.org>,
	Suleiman Souhlal <suleiman@google.com>,
	Andrea Righi <arighi@nvidia.com>,
	kuyo chang <kuyo.chang@mediatek.com>, hupu <hupu.gm@gmail.com>,
	kernel-team@android.com
Subject: Re: [RESEND][PATCH v31 8/9] sched: Add deactivated (sleeping) owner handling to find_proxy_task()
Date: Thu, 13 Aug 2026 15:48:23 +0200	[thread overview]
Message-ID: <20260813134823.GP776954@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20260807035232.1881495-9-jstultz@google.com>


I'm slowly working my way through this one..

On Fri, Aug 07, 2026 at 03:52:14AM +0000, John Stultz wrote:

> +static inline void proxy_remove_from_sleeping_owner(struct task_struct *p)
> +{
> +	struct task_struct *owner = READ_ONCE(p->sleeping_owner);
> +
> +	if (owner) {
> +		/*
> +		 * __proxy_remove_from_sleeping_owner() does a
> +		 * put on owner to match the get done in
> +		 * proxy_enqueue_on_owner(). If that put is the
> +		 * last one and it frees owner, we'd be freeing
> +		 * a lock we held. So get/put owner around its
> +		 * usage her to ensure that doesn't happen.
> +		 */
> +		get_task_struct(owner);
> +		raw_spin_lock(&owner->blocked_lock);
> +		__proxy_remove_from_sleeping_owner(owner, p);
> +		raw_spin_unlock(&owner->blocked_lock);
> +		put_task_struct(owner);
> +	}
> +}

I am not convinced this is correct. Notably
__proxy_remove_from_sleeping_owner() does nothing if p->sleeping_owner
!= owner.

Additionally, owner is read without serialization.

Either owner can change, and you need to:
 - verify p->sleeping_owner == owner once you've acquired
   owner->blocked_lock, and retry if it doesn't match
or
 - explain that once ->sleeping_owner is !NULL, it cannot change and
   instead of NOP-ing out when ->sleeping_owner != owner, assert that it
   is.


> @@ -6852,6 +7078,29 @@ static void proxy_migrate_task(struct rq *rq, struct rq_flags *rf,
>  	proxy_reacquire_rq_lock(rq, rf);
>  }
>  
> +static void proxy_enqueue_on_owner(struct rq *rq, struct task_struct *owner,
> +				   struct task_struct *p)
> +{
> +	lockdep_assert_rq_held(rq);
> +	lockdep_assert_held(&owner->blocked_lock);
> +	/*
> +	 * ttwu_activate() will pick them up and place them on whatever rq
> +	 * @owner will run next.
> +	 */
> +	WARN_ON(p == owner);
> +	WARN_ON(!p->on_rq);
> +	WARN_ON(p->sleeping_owner);
> +	get_task_struct(owner);
> +	WRITE_ONCE(p->sleeping_owner, owner);
> +	/*
> +	 * ttwu_do_activate must not have a chance to activate p
> +	 * elsewhere before it's fully extricated from its old rq.
> +	 */
> +	list_add(&p->blocked_node, &owner->blocked_head);
> +	proxy_resched_idle(rq);
> +	block_task(rq, p, READ_ONCE(p->__state));
> +}

Initially I thought you needed: smp_store_release(&p->sleeping_owner =
owner); after the list_add, and a corresponding smp_load_acquire() in
the function above. However since they both acquire ->blocked_lock
(eventually) this works out. But it might want to have a comment along
those lines.

Random changed that happened whilst I was going over things.

---

--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1261,11 +1261,11 @@ struct task_struct {
 	struct mutex			*blocked_on;	/* lock we're blocked on */
 	raw_spinlock_t			blocked_lock;
 #ifdef CONFIG_SCHED_PROXY_EXEC
-	struct list_head		blocked_head;  /* tasks blocked on this task */
+	struct list_head		blocked_head __guarded_by(&blocked_lock); /* tasks blocked on this task */
 	struct list_head		blocked_node;  /* our entry on someone elses blocked_head */
 	/* Node for list of tasks to process blocked_head list for blocked entitiy activations */
 	struct list_head		blocked_activation_node;
-	struct task_struct		*sleeping_owner; /* task our blocked_node is enqueued on */
+	struct task_struct		*sleeping_owner __guarded_by(&blocked_lock); /* task our blocked_node is enqueued on */
 #endif
 
 	/*
--- a/include/linux/sched/task.h
+++ b/include/linux/sched/task.h
@@ -160,6 +160,7 @@ static inline void put_task_struct(struc
 }
 
 DEFINE_FREE(put_task, struct task_struct *, if (_T) put_task_struct(_T))
+DEFINE_GUARD(get_task, struct task_struct *, get_task_struct(_T), put_task_struct(_T))
 
 static inline void put_task_struct_many(struct task_struct *t, int nr)
 {
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2242,33 +2242,33 @@ void __proxy_remove_from_sleeping_owner(
 
 static inline void proxy_remove_from_sleeping_owner(struct task_struct *p)
 {
-	struct task_struct *owner = READ_ONCE(p->sleeping_owner);
+	struct task_struct *owner = data_race(READ_ONCE(p->sleeping_owner));
 
-	if (owner) {
-		/*
-		 * __proxy_remove_from_sleeping_owner() does a
-		 * put on owner to match the get done in
-		 * proxy_enqueue_on_owner(). If that put is the
-		 * last one and it frees owner, we'd be freeing
-		 * a lock we held. So get/put owner around its
-		 * usage her to ensure that doesn't happen.
-		 */
-		get_task_struct(owner);
-		raw_spin_lock(&owner->blocked_lock);
-		__proxy_remove_from_sleeping_owner(owner, p);
-		raw_spin_unlock(&owner->blocked_lock);
-		put_task_struct(owner);
-	}
+	if (!owner)
+		return;
+
+	/*
+	 * __proxy_remove_from_sleeping_owner() does a
+	 * put on owner to match the get done in
+	 * proxy_enqueue_on_owner(). If that put is the
+	 * last one and it frees owner, we'd be freeing
+	 * a lock we held. So get/put owner around its
+	 * usage her to ensure that doesn't happen.
+	 */
+	guard(get_task)(owner);
+	guard(raw_spinlock)(&owner->blocked_lock);
+	__proxy_remove_from_sleeping_owner(owner, p);
 }
 
 void activate_task(struct rq *rq, struct task_struct *p, int en_flags)
 {
+	lockdep_assert_rq_held(rq);
+
 	if (!sched_proxy_exec()) {
 		__activate_task(rq, p, en_flags);
 		return;
 	}
 
-	lockdep_assert_rq_held(rq);
 	proxy_remove_from_sleeping_owner(p);
 	/*
 	 * By calling __activate_task() with blocked_lock held, we
@@ -2276,10 +2276,9 @@ void activate_task(struct rq *rq, struct
 	 * such that no more blocked tasks will be enqueued on p
 	 * once we release p->blocked_lock.
 	 */
-	raw_spin_lock(&p->blocked_lock);
+	guard(raw_spinlock)(&p->blocked_lock);
 	WARN_ON(task_cpu(p) != cpu_of(rq));
 	__activate_task(rq, p, en_flags);
-	raw_spin_unlock(&p->blocked_lock);
 }
 #else
 static inline void proxy_remove_from_sleeping_owner(struct task_struct *p)

  parent reply	other threads:[~2026-08-13 13:48 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-07  3:52 [RESEND][PATCH v31 0/9] Sleeping Owner Handling for Proxy Execution (v31) John Stultz
2026-08-07  3:52 ` [RESEND][PATCH v31 1/9] sched/deadline: Ignore proxy-exec sched_yield() John Stultz
2026-08-10 15:56   ` Peter Zijlstra
2026-08-10 19:25     ` John Stultz
2026-08-11  3:55       ` K Prateek Nayak
2026-08-11  6:10         ` K Prateek Nayak
2026-08-11  8:33         ` Juri Lelli
2026-08-07  3:52 ` [RESEND][PATCH v31 2/9] sched/core: Don't steal a proxy-exec donor John Stultz
2026-08-07  3:52 ` [RESEND][PATCH v31 3/9] sched/core: Avoid migrating blocked_on tasks John Stultz
2026-08-07  3:52 ` [RESEND][PATCH v31 4/9] sched/core: Don't proxy-exec unmatched cookie lock owners John Stultz
2026-08-07  3:52 ` [RESEND][PATCH v31 5/9] sched: Switch rq->next_class in proxy_reset_donor() John Stultz
2026-08-07  3:52 ` [RESEND][PATCH v31 6/9] sched: Break out core of attach_tasks() helper into sched.h John Stultz
2026-08-07  3:52 ` [RESEND][PATCH v31 7/9] sched: Migrate whole chain in proxy_migrate_task() John Stultz
2026-08-07  3:52 ` [RESEND][PATCH v31 8/9] sched: Add deactivated (sleeping) owner handling to find_proxy_task() John Stultz
2026-08-11 19:08   ` Atul Kumar Pant
2026-08-11 19:56     ` John Stultz
2026-08-12  2:21       ` Atul Kumar Pant
2026-08-13 13:48   ` Peter Zijlstra [this message]
2026-08-13 14:24     ` Peter Zijlstra
2026-08-13 14:40   ` Peter Zijlstra
2026-08-07  3:52 ` [RESEND][PATCH v31 9/9] sched: Distinguish proxy activations from wakeups John Stultz

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=20260813134823.GP776954@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=Metin.Kaya@arm.com \
    --cc=arighi@nvidia.com \
    --cc=boqun.feng@gmail.com \
    --cc=bsegall@google.com \
    --cc=connoro@google.com \
    --cc=daniel.lezcano@linaro.org \
    --cc=dietmar.eggemann@arm.com \
    --cc=hupu.gm@gmail.com \
    --cc=joelagnelf@nvidia.com \
    --cc=jstultz@google.com \
    --cc=juri.lelli@redhat.com \
    --cc=kernel-team@android.com \
    --cc=kprateek.nayak@amd.com \
    --cc=kuyo.chang@mediatek.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=qyousef@layalina.io \
    --cc=rostedt@goodmis.org \
    --cc=suleiman@google.com \
    --cc=tglx@linutronix.de \
    --cc=valentin.schneider@arm.com \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    --cc=will@kernel.org \
    --cc=xuewen.yan94@gmail.com \
    --cc=zezeozue@google.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