* [PATCH v6 1/2] sched/core: Minor optimize ttwu_runnable() @ 2022-12-23 10:32 Chengming Zhou 2022-12-23 10:32 ` [PATCH v6 2/2] sched/core: Reorganize ttwu_do_wakeup() and ttwu_do_activate() Chengming Zhou 2023-01-07 10:53 ` [tip: sched/core] sched/core: Micro-optimize ttwu_runnable() tip-bot2 for Chengming Zhou 0 siblings, 2 replies; 4+ messages in thread From: Chengming Zhou @ 2022-12-23 10:32 UTC (permalink / raw) To: mingo, peterz Cc: juri.lelli, vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman, bristot, vschneid, linux-kernel, Chengming Zhou ttwu_runnable() is used as a fast wakeup path when the wakee task is running on CPU or runnable on RQ, in both cases we can just set its state to TASK_RUNNING to prevent a sleep. If the wakee task is on_cpu running, we don't need to update_rq_clock() or check_preempt_curr(). But if the wakee task is on_rq && !on_cpu (e.g. an IRQ hit before the task got to schedule() and the task been preempted), we should check_preempt_curr() to see if it can preempt the current running. This also removes the class->task_woken() callback from ttwu_runnable(), which wasn't required per the RT/DL implementations: any required push operation would have been queued during class->set_next_task() when p got preempted. ttwu_runnable() also loses the update to rq->idle_stamp, as by definition the rq cannot be idle in this scenario. Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com> Suggested-by: Valentin Schneider <vschneid@redhat.com> Suggested-by: Peter Zijlstra <peterz@infradead.org> Reviewed-by: Valentin Schneider <vschneid@redhat.com> Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org> --- v6: - Improve the comment in ttwu_runnable() per Steve. Thanks! v5: - Split the reorganization and optimization into two patches per Chen Yu. Thanks! v4: - s/This patch reorg/Reorganize/ per Bagas Sanjaya. Thanks! v3: - Improve the changelog per Valentin Schneider. Thanks! v2: - keep check_preempt_curr() for on_rq && !on_cpu case in ttwu_runnable(), per Valentin Schneider. - reorg ttwu_do_wakeup() and ttwu_do_activate() code, so ttwu_do_wakeup() can be reused in ttwu_runnable(), per Peter Zijlstra. - reuse ttwu_do_wakeup() in try_to_wake_up() (p == current) fast path too, so ttwu_do_wakeup() become the only place we mark task runnable. --- kernel/sched/core.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 314c2c0219d9..607a5430b192 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3718,9 +3718,16 @@ static int ttwu_runnable(struct task_struct *p, int wake_flags) rq = __task_rq_lock(p, &rf); if (task_on_rq_queued(p)) { - /* check_preempt_curr() may use rq clock */ - update_rq_clock(rq); - ttwu_do_wakeup(rq, p, wake_flags, &rf); + if (!task_on_cpu(rq, p)) { + /* + * When on_rq && !on_cpu the task is preempted, see if + * it should preempt the task that is current now. + */ + update_rq_clock(rq); + check_preempt_curr(rq, p, wake_flags); + } + WRITE_ONCE(p->__state, TASK_RUNNING); + trace_sched_wakeup(p); ret = 1; } __task_rq_unlock(rq, &rf); -- 2.37.2 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v6 2/2] sched/core: Reorganize ttwu_do_wakeup() and ttwu_do_activate() 2022-12-23 10:32 [PATCH v6 1/2] sched/core: Minor optimize ttwu_runnable() Chengming Zhou @ 2022-12-23 10:32 ` Chengming Zhou 2023-01-07 10:53 ` [tip: sched/core] " tip-bot2 for Chengming Zhou 2023-01-07 10:53 ` [tip: sched/core] sched/core: Micro-optimize ttwu_runnable() tip-bot2 for Chengming Zhou 1 sibling, 1 reply; 4+ messages in thread From: Chengming Zhou @ 2022-12-23 10:32 UTC (permalink / raw) To: mingo, peterz Cc: juri.lelli, vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman, bristot, vschneid, linux-kernel, Chengming Zhou ttwu_do_activate() is used for a complete wakeup, in which we will activate_task() and use ttwu_do_wakeup() to mark the task runnable and perform wakeup-preemption, also call class->task_woken() callback and update the rq->idle_stamp. Since ttwu_runnable() is not a complete wakeup, don't need all those done in ttwu_do_wakeup(), so we can move those to ttwu_do_activate() to simplify ttwu_do_wakeup(), making it only mark the task runnable to be reused in ttwu_runnable() and try_to_wake_up(). This patch should not have any functional changes. Suggested-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com> --- kernel/sched/core.c | 64 ++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 607a5430b192..3936b3328500 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3623,14 +3623,39 @@ ttwu_stat(struct task_struct *p, int cpu, int wake_flags) } /* - * Mark the task runnable and perform wakeup-preemption. + * Mark the task runnable. */ -static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags, - struct rq_flags *rf) +static inline void ttwu_do_wakeup(struct task_struct *p) { - check_preempt_curr(rq, p, wake_flags); WRITE_ONCE(p->__state, TASK_RUNNING); trace_sched_wakeup(p); +} + +static void +ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, + struct rq_flags *rf) +{ + int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK; + + lockdep_assert_rq_held(rq); + + if (p->sched_contributes_to_load) + rq->nr_uninterruptible--; + +#ifdef CONFIG_SMP + if (wake_flags & WF_MIGRATED) + en_flags |= ENQUEUE_MIGRATED; + else +#endif + if (p->in_iowait) { + delayacct_blkio_end(p); + atomic_dec(&task_rq(p)->nr_iowait); + } + + activate_task(rq, p, en_flags); + check_preempt_curr(rq, p, wake_flags); + + ttwu_do_wakeup(p); #ifdef CONFIG_SMP if (p->sched_class->task_woken) { @@ -3660,31 +3685,6 @@ static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags, #endif } -static void -ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, - struct rq_flags *rf) -{ - int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK; - - lockdep_assert_rq_held(rq); - - if (p->sched_contributes_to_load) - rq->nr_uninterruptible--; - -#ifdef CONFIG_SMP - if (wake_flags & WF_MIGRATED) - en_flags |= ENQUEUE_MIGRATED; - else -#endif - if (p->in_iowait) { - delayacct_blkio_end(p); - atomic_dec(&task_rq(p)->nr_iowait); - } - - activate_task(rq, p, en_flags); - ttwu_do_wakeup(rq, p, wake_flags, rf); -} - /* * Consider @p being inside a wait loop: * @@ -3726,8 +3726,7 @@ static int ttwu_runnable(struct task_struct *p, int wake_flags) update_rq_clock(rq); check_preempt_curr(rq, p, wake_flags); } - WRITE_ONCE(p->__state, TASK_RUNNING); - trace_sched_wakeup(p); + ttwu_do_wakeup(p); ret = 1; } __task_rq_unlock(rq, &rf); @@ -4093,8 +4092,7 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) goto out; trace_sched_waking(p); - WRITE_ONCE(p->__state, TASK_RUNNING); - trace_sched_wakeup(p); + ttwu_do_wakeup(p); goto out; } -- 2.37.2 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [tip: sched/core] sched/core: Reorganize ttwu_do_wakeup() and ttwu_do_activate() 2022-12-23 10:32 ` [PATCH v6 2/2] sched/core: Reorganize ttwu_do_wakeup() and ttwu_do_activate() Chengming Zhou @ 2023-01-07 10:53 ` tip-bot2 for Chengming Zhou 0 siblings, 0 replies; 4+ messages in thread From: tip-bot2 for Chengming Zhou @ 2023-01-07 10:53 UTC (permalink / raw) To: linux-tip-commits Cc: Peter Zijlstra, Chengming Zhou, Ingo Molnar, x86, linux-kernel The following commit has been merged into the sched/core branch of tip: Commit-ID: 160fb0d83f206b3429fc495864a022110f9e4978 Gitweb: https://git.kernel.org/tip/160fb0d83f206b3429fc495864a022110f9e4978 Author: Chengming Zhou <zhouchengming@bytedance.com> AuthorDate: Fri, 23 Dec 2022 18:32:57 +08:00 Committer: Ingo Molnar <mingo@kernel.org> CommitterDate: Sat, 07 Jan 2023 10:48:38 +01:00 sched/core: Reorganize ttwu_do_wakeup() and ttwu_do_activate() ttwu_do_activate() is used for a complete wakeup, in which we will activate_task() and use ttwu_do_wakeup() to mark the task runnable and perform wakeup-preemption, also call class->task_woken() callback and update the rq->idle_stamp. Since ttwu_runnable() is not a complete wakeup, don't need all those done in ttwu_do_wakeup(), so we can move those to ttwu_do_activate() to simplify ttwu_do_wakeup(), making it only mark the task runnable to be reused in ttwu_runnable() and try_to_wake_up(). This patch should not have any functional changes. Suggested-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/20221223103257.4962-2-zhouchengming@bytedance.com --- kernel/sched/core.c | 64 +++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 255a318..03b8529 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3625,14 +3625,39 @@ ttwu_stat(struct task_struct *p, int cpu, int wake_flags) } /* - * Mark the task runnable and perform wakeup-preemption. + * Mark the task runnable. */ -static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags, - struct rq_flags *rf) +static inline void ttwu_do_wakeup(struct task_struct *p) { - check_preempt_curr(rq, p, wake_flags); WRITE_ONCE(p->__state, TASK_RUNNING); trace_sched_wakeup(p); +} + +static void +ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, + struct rq_flags *rf) +{ + int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK; + + lockdep_assert_rq_held(rq); + + if (p->sched_contributes_to_load) + rq->nr_uninterruptible--; + +#ifdef CONFIG_SMP + if (wake_flags & WF_MIGRATED) + en_flags |= ENQUEUE_MIGRATED; + else +#endif + if (p->in_iowait) { + delayacct_blkio_end(p); + atomic_dec(&task_rq(p)->nr_iowait); + } + + activate_task(rq, p, en_flags); + check_preempt_curr(rq, p, wake_flags); + + ttwu_do_wakeup(p); #ifdef CONFIG_SMP if (p->sched_class->task_woken) { @@ -3662,31 +3687,6 @@ static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags, #endif } -static void -ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, - struct rq_flags *rf) -{ - int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK; - - lockdep_assert_rq_held(rq); - - if (p->sched_contributes_to_load) - rq->nr_uninterruptible--; - -#ifdef CONFIG_SMP - if (wake_flags & WF_MIGRATED) - en_flags |= ENQUEUE_MIGRATED; - else -#endif - if (p->in_iowait) { - delayacct_blkio_end(p); - atomic_dec(&task_rq(p)->nr_iowait); - } - - activate_task(rq, p, en_flags); - ttwu_do_wakeup(rq, p, wake_flags, rf); -} - /* * Consider @p being inside a wait loop: * @@ -3728,8 +3728,7 @@ static int ttwu_runnable(struct task_struct *p, int wake_flags) update_rq_clock(rq); check_preempt_curr(rq, p, wake_flags); } - WRITE_ONCE(p->__state, TASK_RUNNING); - trace_sched_wakeup(p); + ttwu_do_wakeup(p); ret = 1; } __task_rq_unlock(rq, &rf); @@ -4095,8 +4094,7 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) goto out; trace_sched_waking(p); - WRITE_ONCE(p->__state, TASK_RUNNING); - trace_sched_wakeup(p); + ttwu_do_wakeup(p); goto out; } ^ permalink raw reply [flat|nested] 4+ messages in thread
* [tip: sched/core] sched/core: Micro-optimize ttwu_runnable() 2022-12-23 10:32 [PATCH v6 1/2] sched/core: Minor optimize ttwu_runnable() Chengming Zhou 2022-12-23 10:32 ` [PATCH v6 2/2] sched/core: Reorganize ttwu_do_wakeup() and ttwu_do_activate() Chengming Zhou @ 2023-01-07 10:53 ` tip-bot2 for Chengming Zhou 1 sibling, 0 replies; 4+ messages in thread From: tip-bot2 for Chengming Zhou @ 2023-01-07 10:53 UTC (permalink / raw) To: linux-tip-commits Cc: Valentin Schneider, Peter Zijlstra, Chengming Zhou, Ingo Molnar, Steven Rostedt (Google), x86, linux-kernel The following commit has been merged into the sched/core branch of tip: Commit-ID: efe09385864f3441c71711f91e621992f9423c01 Gitweb: https://git.kernel.org/tip/efe09385864f3441c71711f91e621992f9423c01 Author: Chengming Zhou <zhouchengming@bytedance.com> AuthorDate: Fri, 23 Dec 2022 18:32:56 +08:00 Committer: Ingo Molnar <mingo@kernel.org> CommitterDate: Sat, 07 Jan 2023 10:48:38 +01:00 sched/core: Micro-optimize ttwu_runnable() ttwu_runnable() is used as a fast wakeup path when the wakee task is running on CPU or runnable on RQ, in both cases we can just set its state to TASK_RUNNING to prevent a sleep. If the wakee task is on_cpu running, we don't need to update_rq_clock() or check_preempt_curr(). But if the wakee task is on_rq && !on_cpu (e.g. an IRQ hit before the task got to schedule() and the task been preempted), we should check_preempt_curr() to see if it can preempt the current running. This also removes the class->task_woken() callback from ttwu_runnable(), which wasn't required per the RT/DL implementations: any required push operation would have been queued during class->set_next_task() when p got preempted. ttwu_runnable() also loses the update to rq->idle_stamp, as by definition the rq cannot be idle in this scenario. Suggested-by: Valentin Schneider <vschneid@redhat.com> Suggested-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Reviewed-by: Valentin Schneider <vschneid@redhat.com> Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org> Link: https://lore.kernel.org/r/20221223103257.4962-1-zhouchengming@bytedance.com --- kernel/sched/core.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index f99ee69..255a318 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3720,9 +3720,16 @@ static int ttwu_runnable(struct task_struct *p, int wake_flags) rq = __task_rq_lock(p, &rf); if (task_on_rq_queued(p)) { - /* check_preempt_curr() may use rq clock */ - update_rq_clock(rq); - ttwu_do_wakeup(rq, p, wake_flags, &rf); + if (!task_on_cpu(rq, p)) { + /* + * When on_rq && !on_cpu the task is preempted, see if + * it should preempt the task that is current now. + */ + update_rq_clock(rq); + check_preempt_curr(rq, p, wake_flags); + } + WRITE_ONCE(p->__state, TASK_RUNNING); + trace_sched_wakeup(p); ret = 1; } __task_rq_unlock(rq, &rf); ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-01-07 10:53 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2022-12-23 10:32 [PATCH v6 1/2] sched/core: Minor optimize ttwu_runnable() Chengming Zhou 2022-12-23 10:32 ` [PATCH v6 2/2] sched/core: Reorganize ttwu_do_wakeup() and ttwu_do_activate() Chengming Zhou 2023-01-07 10:53 ` [tip: sched/core] " tip-bot2 for Chengming Zhou 2023-01-07 10:53 ` [tip: sched/core] sched/core: Micro-optimize ttwu_runnable() tip-bot2 for Chengming Zhou
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®