From: Chengming Zhou Date: Fri, 23 Dec 2022 10:32:56 +0000 (+0800) Subject: sched/core: Micro-optimize ttwu_runnable() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=efe09385864f3441c71711f91e621992f9423c01;p=linux.git 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 Suggested-by: Peter Zijlstra Signed-off-by: Chengming Zhou Signed-off-by: Ingo Molnar Reviewed-by: Valentin Schneider Reviewed-by: Steven Rostedt (Google) Link: https://lore.kernel.org/r/20221223103257.4962-1-zhouchengming@bytedance.com --- diff --git a/kernel/sched/core.c b/kernel/sched/core.c index f99ee69867e3e..255a318cf24e4 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);