[PATCH v25 8/9] sched: Move attach_one_task and attach_task helpers to sched.h

John Stultz posted 9 patches 3 weeks, 4 days ago
There is a newer version of this series
[PATCH v25 8/9] sched: Move attach_one_task and attach_task helpers to sched.h
Posted by John Stultz 3 weeks, 4 days ago
The fair scheduler locally introduced attach_one_task() and
attach_task() helpers, but these could be generically useful
so move this code to sched.h so we can use them elsewhere.

Suggested-by: K Prateek Nayak <kprateek.nayak@amd.com>
Signed-off-by: John Stultz <jstultz@google.com>
---
Cc: Joel Fernandes <joelagnelf@nvidia.com>
Cc: Qais Yousef <qyousef@layalina.io>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Juri Lelli <juri.lelli@redhat.com>
Cc: Vincent Guittot <vincent.guittot@linaro.org>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>
Cc: Valentin Schneider <vschneid@redhat.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ben Segall <bsegall@google.com>
Cc: Zimuzo Ezeozue <zezeozue@google.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Will Deacon <will@kernel.org>
Cc: Waiman Long <longman@redhat.com>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Metin Kaya <Metin.Kaya@arm.com>
Cc: Xuewen Yan <xuewen.yan94@gmail.com>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Daniel Lezcano <daniel.lezcano@linaro.org>
Cc: Suleiman Souhlal <suleiman@google.com>
Cc: kuyo chang <kuyo.chang@mediatek.com>
Cc: hupu <hupu.gm@gmail.com>
Cc: kernel-team@android.com
---
 kernel/sched/fair.c  | 26 --------------------------
 kernel/sched/sched.h | 26 ++++++++++++++++++++++++++
 2 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index bf948db905ed1..53da01a251487 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -9784,32 +9784,6 @@ static int detach_tasks(struct lb_env *env)
 	return detached;
 }
 
-/*
- * attach_task() -- attach the task detached by detach_task() to its new rq.
- */
-static void attach_task(struct rq *rq, struct task_struct *p)
-{
-	lockdep_assert_rq_held(rq);
-
-	WARN_ON_ONCE(task_rq(p) != rq);
-	activate_task(rq, p, ENQUEUE_NOCLOCK);
-	wakeup_preempt(rq, p, 0);
-}
-
-/*
- * attach_one_task() -- attaches the task returned from detach_one_task() to
- * its new rq.
- */
-static void attach_one_task(struct rq *rq, struct task_struct *p)
-{
-	struct rq_flags rf;
-
-	rq_lock(rq, &rf);
-	update_rq_clock(rq);
-	attach_task(rq, p);
-	rq_unlock(rq, &rf);
-}
-
 /*
  * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
  * new rq.
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 2a0236d745832..d4a73c3db03d4 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -3008,6 +3008,32 @@ extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
 
 extern void wakeup_preempt(struct rq *rq, struct task_struct *p, int flags);
 
+/*
+ * attach_task() -- attach the task detached by detach_task() to its new rq.
+ */
+static inline void attach_task(struct rq *rq, struct task_struct *p)
+{
+	lockdep_assert_rq_held(rq);
+
+	WARN_ON_ONCE(task_rq(p) != rq);
+	activate_task(rq, p, ENQUEUE_NOCLOCK);
+	wakeup_preempt(rq, p, 0);
+}
+
+/*
+ * attach_one_task() -- attaches the task returned from detach_one_task() to
+ * its new rq.
+ */
+static inline void attach_one_task(struct rq *rq, struct task_struct *p)
+{
+	struct rq_flags rf;
+
+	rq_lock(rq, &rf);
+	update_rq_clock(rq);
+	attach_task(rq, p);
+	rq_unlock(rq, &rf);
+}
+
 #ifdef CONFIG_PREEMPT_RT
 # define SCHED_NR_MIGRATE_BREAK 8
 #else
-- 
2.53.0.880.g73c4285caa-goog
Re: [PATCH v25 8/9] sched: Move attach_one_task and attach_task helpers to sched.h
Posted by K Prateek Nayak 3 weeks, 1 day ago
Hello John,

On 3/13/2026 8:00 AM, John Stultz wrote:
> +/*
> + * attach_one_task() -- attaches the task returned from detach_one_task() to
> + * its new rq.
> + */
> +static inline void attach_one_task(struct rq *rq, struct task_struct *p)
> +{
> +	struct rq_flags rf;
> +
> +	rq_lock(rq, &rf);

nit. We can now use guard(rq_lock)(rq) and save on needing to declare a
"rf". Apart from that, feel free to include:

Reviewed-by: K Prateek Nayak <kprateek.nayak@amd.com>

> +	update_rq_clock(rq);
> +	attach_task(rq, p);
> +	rq_unlock(rq, &rf);
> +}
> +
-- 
Thanks and Regards,
Prateek
Re: [PATCH v25 8/9] sched: Move attach_one_task and attach_task helpers to sched.h
Posted by John Stultz 3 weeks ago
On Sun, Mar 15, 2026 at 9:34 AM K Prateek Nayak <kprateek.nayak@amd.com> wrote:
> On 3/13/2026 8:00 AM, John Stultz wrote:
> > +/*
> > + * attach_one_task() -- attaches the task returned from detach_one_task() to
> > + * its new rq.
> > + */
> > +static inline void attach_one_task(struct rq *rq, struct task_struct *p)
> > +{
> > +     struct rq_flags rf;
> > +
> > +     rq_lock(rq, &rf);
>
> nit. We can now use guard(rq_lock)(rq) and save on needing to declare a
> "rf". Apart from that, feel free to include:

I actually did this in a later patch in the full series, as it seemed
more clear I wasn't modifying logic when moving the code:
  https://github.com/johnstultz-work/linux-dev/commit/8d8a12278d81ce81af6b0dfd051750f4ce2ec0e5

But, given your feedback, I'll go ahead and fold that fix down to this
change and add a note in the commit message.

Thanks as always for the review and feedback!
-john
Re: [PATCH v25 8/9] sched: Move attach_one_task and attach_task helpers to sched.h
Posted by K Prateek Nayak 3 weeks ago
Hello John,

On 3/17/2026 5:04 AM, John Stultz wrote:
> On Sun, Mar 15, 2026 at 9:34 AM K Prateek Nayak <kprateek.nayak@amd.com> wrote:
>> On 3/13/2026 8:00 AM, John Stultz wrote:
>>> +/*
>>> + * attach_one_task() -- attaches the task returned from detach_one_task() to
>>> + * its new rq.
>>> + */
>>> +static inline void attach_one_task(struct rq *rq, struct task_struct *p)
>>> +{
>>> +     struct rq_flags rf;
>>> +
>>> +     rq_lock(rq, &rf);
>>
>> nit. We can now use guard(rq_lock)(rq) and save on needing to declare a
>> "rf". Apart from that, feel free to include:
> 
> I actually did this in a later patch in the full series, as it seemed
> more clear I wasn't modifying logic when moving the code:
>   https://github.com/johnstultz-work/linux-dev/commit/8d8a12278d81ce81af6b0dfd051750f4ce2ec0e5
> 
> But, given your feedback, I'll go ahead and fold that fix down to this
> change and add a note in the commit message.

Thanks a ton! That reminds me that I should go look at the full proxy
series once again. I've queued some usual benchmark runs for these bits
on top of tip - I've hopeful I've sorted the stuff with longer running
benchmarks this time around; will report back once they are done.

Again, thank you for incorporating the suggestions and reworking the
series. Much appreciated _/\_

-- 
Thanks and Regards,
Prateek