[PATCH next 2/2] sched: Fix sched_child_runs_first

From: Zhang Qiao
Date: Mon Oct 31 2022 - 08:22:56 EST


There are two cases that the sched_child_runs_first maybe not
work fine:
1) when call clone3() with CLONE_INTO_CGROUP flags, will creating
the child task into a cgroup different from the parent's cgroup,
so that child and parent's cfs_rq is diffent.
2) Assign a different cpu to the new task when fork balancing.

Above two case, the child and the parent will attach to different
cpu and cfs_rq. At this time, we can't swap the child and parent's
vruntime, and i think only do swap vruntime when the parent and
child in the same cfs_rq. This patch will add the cfs_rq check
before swap vruntime.

Signed-off-by: Zhang Qiao <zhangqiao22@xxxxxxxxxx>
---
kernel/sched/fair.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 34845d425180..6061ceb1b7cb 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -11612,7 +11612,9 @@ static void task_fork_fair(struct task_struct *p)
}
place_entity(cfs_rq, se, 1);

- if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
+ if (sysctl_sched_child_runs_first &&
+ cfs_rq == task_cfs_rq(current) &&
+ curr && entity_before(curr, se)) {
/*
* Upon rescheduling, sched_class::put_prev_task() will place
* 'current' within the tree based on its new key value.
--
2.17.1