]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
sched: Initialize the vruntime of a new task when it is first enqueued
authorZhang Qiao <zhangqiao22@huawei.com>
Thu, 27 Jun 2024 13:33:59 +0000 (21:33 +0800)
committerPeter Zijlstra <peterz@infradead.org>
Mon, 29 Jul 2024 10:22:34 +0000 (12:22 +0200)
When creating a new task, we initialize vruntime of the newly task at
sched_cgroup_fork(). However, the timing of executing this action is too
early and may not be accurate.

Because it uses current CPU to init the vruntime, but the new task
actually runs on the cpu which be assigned at wake_up_new_task().

To optimize this case, we pass ENQUEUE_INITIAL flag to activate_task()
in wake_up_new_task(), in this way, when place_entity is called in
enqueue_entity(), the vruntime of the new task will be initialized.

In addition, place_entity() in task_fork_fair() was introduced for two
reasons:
1. Previously, the __enqueue_entity() was in task_new_fair(),
in order to provide vruntime for enqueueing the newly task, the
vruntime assignment equation "se->vruntime = cfs_rq->min_vruntime" was
introduced by commit e9acbff6484d ("sched: introduce se->vruntime").
This is the initial state of place_entity().

2. commit 4d78e7b656aa ("sched: new task placement for vruntime") added
child_runs_first task placement feature which based on vruntime, this
also requires the new task's vruntime value.

After removing the child_runs_first and enqueue_entity() from
task_fork_fair(), this place_entity() no longer makes sense, so remove
it also.

Signed-off-by: Zhang Qiao <zhangqiao22@huawei.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20240627133359.1370598-1-zhangqiao22@huawei.com
kernel/sched/core.c
kernel/sched/fair.c

index f3951e4a55e5b6078142f2f2b44359b7560e6e4f..2c61b4fcb15e9d23e4fe8d238900c1efa358ea30 100644 (file)
@@ -4686,7 +4686,7 @@ void wake_up_new_task(struct task_struct *p)
        update_rq_clock(rq);
        post_init_entity_util_avg(p);
 
-       activate_task(rq, p, ENQUEUE_NOCLOCK);
+       activate_task(rq, p, ENQUEUE_NOCLOCK | ENQUEUE_INITIAL);
        trace_sched_wakeup_new(p);
        wakeup_preempt(rq, p, WF_FORK);
 #ifdef CONFIG_SMP
index 9057584ec06de948c0032f48d382354623cf1c6a..e8cdfeb898e2767966f85a0e13a59ee6ec626b78 100644 (file)
@@ -12702,22 +12702,7 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
  */
 static void task_fork_fair(struct task_struct *p)
 {
-       struct sched_entity *se = &p->se, *curr;
-       struct cfs_rq *cfs_rq;
-       struct rq *rq = this_rq();
-       struct rq_flags rf;
-
-       rq_lock(rq, &rf);
-       update_rq_clock(rq);
-
        set_task_max_allowed_capacity(p);
-
-       cfs_rq = task_cfs_rq(current);
-       curr = cfs_rq->curr;
-       if (curr)
-               update_curr(cfs_rq);
-       place_entity(cfs_rq, se, ENQUEUE_INITIAL);
-       rq_unlock(rq, &rf);
 }
 
 /*