Skip to content

Commit a398059

Browse files
Zhang Qiaogregkh
authored andcommitted
sched/fair: sanitize vruntime of entity being placed
commit 829c165 upstream. 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] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 7b9f8ef commit a398059

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
@@ -3858,6 +3858,7 @@ static void
38583858
place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
38593859
{
38603860
u64 vruntime = cfs_rq->min_vruntime;
3861+
u64 sleep_time;
38613862

38623863
/*
38633864
* The 'current' period is already promised to the current tasks,
@@ -3882,8 +3883,18 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
38823883
vruntime -= thresh;
38833884
}
38843885

3885-
/* ensure we never gain time by being placed backwards. */
3886-
se->vruntime = max_vruntime(se->vruntime, vruntime);
3886+
/*
3887+
* Pull vruntime of the entity being placed to the base level of
3888+
* cfs_rq, to prevent boosting it if placed backwards. If the entity
3889+
* slept for a long time, don't even try to compare its vruntime with
3890+
* the base as it may be too far off and the comparison may get
3891+
* inversed due to s64 overflow.
3892+
*/
3893+
sleep_time = rq_clock_task(rq_of(cfs_rq)) - se->exec_start;
3894+
if ((s64)sleep_time > 60LL * NSEC_PER_SEC)
3895+
se->vruntime = vruntime;
3896+
else
3897+
se->vruntime = max_vruntime(se->vruntime, vruntime);
38873898
}
38883899

38893900
static void check_enqueue_throttle(struct cfs_rq *cfs_rq);

0 commit comments

Comments
 (0)