From: Peter Zijlstra <peterz@infradead.org>
To: mingo@redhat.com, juri.lelli@redhat.com,
vincent.guittot@linaro.org, dietmar.eggemann@arm.com,
rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de,
vschneid@redhat.com, clm@meta.com
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org
Subject: [PATCH v2 12/12] sched: Add ttwu_queue support for delayed tasks
Date: Wed, 02 Jul 2025 13:49:36 +0200 [thread overview]
Message-ID: <20250702121159.652969404@infradead.org> (raw)
In-Reply-To: 20250702114924.091581796@infradead.org
One of the more expensive things to do is take a remote runqueue lock;
which is exactly what ttwu_runnable() ends up doing. However, in the
case of sched_delayed tasks it is possible to queue up an IPI instead.
Reported-by: Chris Mason <clm@meta.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20250520101727.984171377@infradead.org
---
include/linux/sched.h | 1
kernel/sched/core.c | 96 +++++++++++++++++++++++++++++++++++++++++++++---
kernel/sched/fair.c | 17 ++++++++
kernel/sched/features.h | 1
kernel/sched/sched.h | 1
5 files changed, 110 insertions(+), 6 deletions(-)
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -984,6 +984,7 @@ struct task_struct {
* ->sched_remote_wakeup gets used, so it can be in this word.
*/
unsigned sched_remote_wakeup:1;
+ unsigned sched_remote_delayed:1;
#ifdef CONFIG_RT_MUTEXES
unsigned sched_rt_mutex:1;
#endif
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -675,7 +675,12 @@ struct rq *__task_rq_lock(struct task_st
{
struct rq *rq;
- lockdep_assert_held(&p->pi_lock);
+ /*
+ * TASK_WAKING is used to serialize the remote end of wakeup, rather
+ * than p->pi_lock.
+ */
+ lockdep_assert(p->__state == TASK_WAKING ||
+ lockdep_is_held(&p->pi_lock) != LOCK_STATE_NOT_HELD);
for (;;) {
rq = task_rq(p);
@@ -3727,6 +3732,8 @@ ttwu_do_activate(struct rq *rq, struct t
}
}
+static bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags);
+
/*
* Consider @p being inside a wait loop:
*
@@ -3754,6 +3761,35 @@ ttwu_do_activate(struct rq *rq, struct t
*/
static int ttwu_runnable(struct task_struct *p, int wake_flags)
{
+ if (sched_feat(TTWU_QUEUE_DELAYED) && READ_ONCE(p->se.sched_delayed)) {
+ /*
+ * Similar to try_to_block_task():
+ *
+ * __schedule() ttwu()
+ * prev_state = prev->state if (p->sched_delayed)
+ * if (prev_state) smp_acquire__after_ctrl_dep()
+ * try_to_block_task() p->state = TASK_WAKING
+ * ... set_delayed()
+ * RELEASE p->sched_delayed = 1
+ *
+ * __schedule() and ttwu() have matching control dependencies.
+ *
+ * Notably, once we observe sched_delayed we know the task has
+ * passed try_to_block_task() and p->state is ours to modify.
+ *
+ * TASK_WAKING controls ttwu() concurrency.
+ */
+ smp_acquire__after_ctrl_dep();
+ WRITE_ONCE(p->__state, TASK_WAKING);
+ /*
+ * Bit of a hack, see select_task_rq_fair()'s WF_DELAYED case.
+ */
+ p->wake_cpu = smp_processor_id();
+
+ if (ttwu_queue_wakelist(p, task_cpu(p), wake_flags | WF_DELAYED))
+ return 1;
+ }
+
CLASS(__task_rq_lock, guard)(p);
struct rq *rq = guard.rq;
@@ -3776,6 +3812,8 @@ static int ttwu_runnable(struct task_str
return 1;
}
+static void __ttwu_queue_wakelist(struct task_struct *p, int cpu);
+
static inline bool ttwu_do_migrate(struct rq *rq, struct task_struct *p, int cpu)
{
struct rq *p_rq = rq ? : task_rq(p);
@@ -3801,6 +3839,52 @@ static inline bool ttwu_do_migrate(struc
return true;
}
+static int ttwu_delayed(struct rq *rq, struct task_struct *p, int wake_flags,
+ struct rq_flags *rf)
+{
+ struct rq *p_rq = task_rq(p);
+ int cpu;
+
+ /*
+ * Notably it is possible for on-rq entities to get migrated -- even
+ * sched_delayed ones. This should be rare though, so flip the locks
+ * rather than IPI chase after it.
+ */
+ if (unlikely(rq != p_rq)) {
+ rq_unlock(rq, rf);
+ p_rq = __task_rq_lock(p, rf);
+ update_rq_clock(p_rq);
+ }
+
+ if (task_on_rq_queued(p))
+ dequeue_task(p_rq, p, DEQUEUE_NOCLOCK | DEQUEUE_SLEEP | DEQUEUE_DELAYED);
+
+ /*
+ * NOTE: unlike the regular try_to_wake_up() path, this runs both
+ * select_task_rq() and ttwu_do_migrate() while holding rq->lock
+ * rather than p->pi_lock.
+ */
+ cpu = select_task_rq(p, p->wake_cpu, &wake_flags);
+ if (ttwu_do_migrate(rq, p, cpu))
+ wake_flags |= WF_MIGRATED;
+
+ if (unlikely(rq != p_rq)) {
+ __task_rq_unlock(p_rq, rf);
+ rq_lock(rq, rf);
+ }
+
+ p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED);
+ p->sched_remote_delayed = 0;
+
+ /* it wants to run here */
+ if (cpu_of(rq) == cpu)
+ return 0;
+
+ /* shoot it to the CPU it wants to run on */
+ __ttwu_queue_wakelist(p, cpu);
+ return 1;
+}
+
void sched_ttwu_pending(void *arg)
{
struct llist_node *llist = arg;
@@ -3819,12 +3903,13 @@ void sched_ttwu_pending(void *arg)
if (WARN_ON_ONCE(p->on_cpu))
smp_cond_load_acquire(&p->on_cpu, !VAL);
- if (WARN_ON_ONCE(task_cpu(p) != cpu_of(rq)))
- set_task_cpu(p, cpu_of(rq));
-
if (p->sched_remote_wakeup)
wake_flags |= WF_MIGRATED;
+ if (p->sched_remote_delayed &&
+ ttwu_delayed(rq, p, wake_flags | WF_DELAYED, &guard.rf))
+ continue;
+
ttwu_do_activate(rq, p, wake_flags, &guard.rf);
}
@@ -3964,12 +4049,13 @@ static inline bool ttwu_queue_cond(struc
static bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags)
{
- bool def = sched_feat(TTWU_QUEUE_DEFAULT);
+ bool def = sched_feat(TTWU_QUEUE_DEFAULT) || (wake_flags & WF_DELAYED);
if (!ttwu_queue_cond(p, cpu, def))
return false;
p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED);
+ p->sched_remote_delayed = !!(wake_flags & WF_DELAYED);
__ttwu_queue_wakelist(p, cpu);
return true;
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -5327,7 +5327,10 @@ static __always_inline void return_cfs_r
static void set_delayed(struct sched_entity *se)
{
- se->sched_delayed = 1;
+ /*
+ * See TTWU_QUEUE_DELAYED in ttwu_runnable().
+ */
+ smp_store_release(&se->sched_delayed, 1);
/*
* Delayed se of cfs_rq have no tasks queued on them.
@@ -8481,6 +8484,18 @@ select_task_rq_fair(struct task_struct *
/* SD_flags and WF_flags share the first nibble */
int sd_flag = wake_flags & 0xF;
+ if (wake_flags & WF_DELAYED) {
+ /*
+ * This is the ttwu_delayed() case; where prev_cpu is in fact
+ * the CPU that did the wakeup, while @p is running on the
+ * current CPU.
+ *
+ * Make sure to flip them the right way around, otherwise
+ * wake-affine is going to do the wrong thing.
+ */
+ swap(cpu, new_cpu);
+ }
+
/*
* required for stable ->cpus_allowed
*/
--- a/kernel/sched/features.h
+++ b/kernel/sched/features.h
@@ -82,6 +82,7 @@ SCHED_FEAT(TTWU_QUEUE, false)
SCHED_FEAT(TTWU_QUEUE, true)
#endif
SCHED_FEAT(TTWU_QUEUE_ON_CPU, true)
+SCHED_FEAT(TTWU_QUEUE_DELAYED, true)
SCHED_FEAT(TTWU_QUEUE_DEFAULT, false)
/*
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2280,6 +2280,7 @@ static inline int task_on_rq_migrating(s
#define WF_RQ_SELECTED 0x80 /* ->select_task_rq() was called */
#define WF_ON_CPU 0x0100
+#define WF_DELAYED 0x0200
static_assert(WF_EXEC == SD_BALANCE_EXEC);
static_assert(WF_FORK == SD_BALANCE_FORK);
next prev parent reply other threads:[~2025-07-02 12:13 UTC|newest]
Thread overview: 68+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-02 11:49 [PATCH v2 00/12] sched: Address schbench regression Peter Zijlstra
2025-07-02 11:49 ` [PATCH v2 01/12] sched/psi: Optimize psi_group_change() cpu_clock() usage Peter Zijlstra
2025-07-15 19:11 ` Chris Mason
2025-07-16 6:06 ` K Prateek Nayak
2025-07-16 6:53 ` Beata Michalska
2025-07-16 10:40 ` Peter Zijlstra
2025-07-16 14:54 ` Johannes Weiner
2025-07-16 16:27 ` Chris Mason
2025-07-23 4:16 ` Aithal, Srikanth
2025-07-25 5:13 ` K Prateek Nayak
2025-07-02 11:49 ` [PATCH v2 02/12] sched/deadline: Less agressive dl_server handling Peter Zijlstra
2025-07-02 16:12 ` Juri Lelli
2025-07-10 12:46 ` [tip: sched/core] " tip-bot2 for Peter Zijlstra
2025-07-14 22:56 ` [PATCH v2 02/12] " Mel Gorman
2025-07-15 14:55 ` Chris Mason
2025-07-16 18:19 ` Mel Gorman
2025-07-30 9:34 ` Geert Uytterhoeven
2025-07-30 9:46 ` Juri Lelli
2025-07-30 10:05 ` Geert Uytterhoeven
2025-08-05 22:03 ` Chris Bainbridge
2025-08-05 23:04 ` Chris Bainbridge
2025-07-02 11:49 ` [PATCH v2 03/12] sched: Optimize ttwu() / select_task_rq() Peter Zijlstra
2025-07-10 16:47 ` Vincent Guittot
2025-07-14 22:59 ` Mel Gorman
2025-07-02 11:49 ` [PATCH v2 04/12] sched: Use lock guard in ttwu_runnable() Peter Zijlstra
2025-07-10 16:48 ` Vincent Guittot
2025-07-14 23:00 ` Mel Gorman
2025-07-02 11:49 ` [PATCH v2 05/12] sched: Add ttwu_queue controls Peter Zijlstra
2025-07-10 16:51 ` Vincent Guittot
2025-07-14 23:14 ` Mel Gorman
2025-07-02 11:49 ` [PATCH v2 06/12] sched: Introduce ttwu_do_migrate() Peter Zijlstra
2025-07-10 16:51 ` Vincent Guittot
2025-07-02 11:49 ` [PATCH v2 07/12] psi: Split psi_ttwu_dequeue() Peter Zijlstra
2025-07-17 23:59 ` Chris Mason
2025-07-18 18:02 ` Steven Rostedt
2025-07-02 11:49 ` [PATCH v2 08/12] sched: Re-arrange __ttwu_queue_wakelist() Peter Zijlstra
2025-07-02 11:49 ` [PATCH v2 09/12] sched: Clean up ttwu comments Peter Zijlstra
2025-07-02 11:49 ` [PATCH v2 10/12] sched: Use lock guard in sched_ttwu_pending() Peter Zijlstra
2025-07-10 16:51 ` Vincent Guittot
2025-07-02 11:49 ` [PATCH v2 11/12] sched: Change ttwu_runnable() vs sched_delayed Peter Zijlstra
2025-07-02 11:49 ` Peter Zijlstra [this message]
2025-07-03 16:00 ` [PATCH v2 12/12] sched: Add ttwu_queue support for delayed tasks Phil Auld
2025-07-03 16:47 ` Peter Zijlstra
2025-07-03 17:11 ` Phil Auld
2025-07-14 13:57 ` Phil Auld
2025-07-04 6:13 ` K Prateek Nayak
2025-07-04 7:59 ` Peter Zijlstra
2025-07-08 12:44 ` Dietmar Eggemann
2025-07-08 18:57 ` Peter Zijlstra
2025-07-08 21:02 ` Peter Zijlstra
2025-07-23 5:42 ` Shrikanth Hegde
2025-07-02 15:27 ` [PATCH v2 00/12] sched: Address schbench regression Chris Mason
2025-07-07 9:05 ` Shrikanth Hegde
2025-07-07 9:11 ` Peter Zijlstra
2025-07-07 9:38 ` Shrikanth Hegde
2025-07-16 13:46 ` Phil Auld
2025-07-17 17:25 ` Phil Auld
2025-07-07 18:19 ` Shrikanth Hegde
2025-07-08 19:02 ` Peter Zijlstra
2025-07-09 16:46 ` Shrikanth Hegde
2025-07-14 17:54 ` Shrikanth Hegde
2025-07-21 19:37 ` Shrikanth Hegde
2025-07-22 20:20 ` Chris Mason
2025-07-24 18:23 ` Chris Mason
2025-07-08 15:09 ` Chris Mason
2025-07-08 17:29 ` Shrikanth Hegde
2025-07-17 13:04 ` Beata Michalska
2025-07-17 16:57 ` Beata Michalska
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=20250702121159.652969404@infradead.org \
--to=peterz@infradead.org \
--cc=bsegall@google.com \
--cc=clm@meta.com \
--cc=dietmar.eggemann@arm.com \
--cc=juri.lelli@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=rostedt@goodmis.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.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;
as well as URLs for NNTP newsgroup(s).