Skip to content

Commit 829c165

Browse files
Zhang QiaoPeter Zijlstra
authored andcommitted
sched/fair: sanitize vruntime of entity being placed
When a scheduling entity is placed onto cfs_rq, its vruntime is pulled to the base level (around cfs_rq->min_vruntime), so that the entity doesn't gain extra boost when placed backwards. However, if the entity being placed wasn't executed for a long time, its vruntime may get too far behind (e.g. while cfs_rq was executing a low-weight hog), which can inverse the vruntime comparison due to s64 overflow. This results in the entity being placed with its original vruntime way forwards, so that it will effectively never get to the cpu. To prevent that, ignore the vruntime of the entity being placed if it didn't execute for much longer than the characteristic sheduler time scale. [rkagan: formatted, adjusted commit log, comments, cutoff value] Signed-off-by: Zhang Qiao <[email protected]> Co-developed-by: Roman Kagan <[email protected]> Signed-off-by: Roman Kagan <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent a2e9061 commit 829c165

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

kernel/sched/fair.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4652,6 +4652,7 @@ static void
46524652
place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
46534653
{
46544654
u64 vruntime = cfs_rq->min_vruntime;
4655+
u64 sleep_time;
46554656

46564657
/*
46574658
* The 'current' period is already promised to the current tasks,
@@ -4681,8 +4682,18 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
46814682
vruntime -= thresh;
46824683
}
46834684

4684-
/* ensure we never gain time by being placed backwards. */
4685-
se->vruntime = max_vruntime(se->vruntime, vruntime);
4685+
/*
4686+
* Pull vruntime of the entity being placed to the base level of
4687+
* cfs_rq, to prevent boosting it if placed backwards. If the entity
4688+
* slept for a long time, don't even try to compare its vruntime with
4689+
* the base as it may be too far off and the comparison may get
4690+
* inversed due to s64 overflow.
4691+
*/
4692+
sleep_time = rq_clock_task(rq_of(cfs_rq)) - se->exec_start;
4693+
if ((s64)sleep_time > 60LL * NSEC_PER_SEC)
4694+
se->vruntime = vruntime;
4695+
else
4696+
se->vruntime = max_vruntime(se->vruntime, vruntime);
46864697
}
46874698

46884699
static void check_enqueue_throttle(struct cfs_rq *cfs_rq);

0 commit comments

Comments
 (0)