sched/fair: Update zero_vruntime after clearing on_rq in dequeue_entity()
PATCH 1/1 sched/fair: Update zero_vruntime after clearing on_rq in dequeue_entity()
When dequeuing the current entity (cfs_rq->curr) in dequeue_entity(), the cfs_rq->zero_vruntime is updated via update_entity_lag() -> avg_vruntime() -> update_zero_vruntime() while curr->on_rq is still 1. This means the current entity is still included in the zero_vruntime calculation.
However, immediately after this, curr->on_rq is set to 0, which should change the avg_vruntime() result. Without re-updating zero_vruntime, the stale value may be used in subsequent task selection paths:
schedule() -> ... -> pick_task_fair() -> pick_next_entity() -> pick_eevdf() -> vruntime_eligible()
If entity_tick() -> avg_vruntime() -> update_zero_vruntime() is not triggered in time between dequeue and the next pick, vruntime_eligible() may use an inaccurate cfs_rq->zero_vruntime. This can potentially cause all tasks to appear ineligible, leading to NULL pointer dereference.
Add an explicit avg_vruntime(cfs_rq) call after clearing curr->on_rq to ensure cfs_rq->zero_vruntime is properly updated before the next pick.
--- kernel/sched/fair.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/sched/fair.c +3 -0
@@ -5461,6 +5461,9 @@ dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)if (se != cfs_rq->curr)__dequeue_entity (cfs_rq, se); se->on_rq =0 ; +/* update the cfs_rq->zero_vruntime again after curr->on_rq = 0 */ +if (se == cfs_rq->curr) +avg_vruntime (cfs_rq);account_entity_dequeue (cfs_rq, se);/* return excess runtime on last dequeue */
Signature · 1 line
2.34.1
Vincent Guittot
maintainerQuoted 6 lines · Zicheng Qu
When dequeuing the current entity (
cfs_rq->curr) indequeue_entity(), thecfs_rq->zero_vruntimeis updated viaupdate_entity_lag()->avg_vruntime()->update_zero_vruntime()whilecurr->on_rqis still 1. This means the current entity is still included in thezero_vruntimecalculation.
curr is not included in zero_vruntime but added when computing avg_vruntime so zero_vruntime is not impacted when curr is dequeued
Quoted 40 lines
However, immediately after this,
curr->on_rqis set to 0, which should change theavg_vruntime()result. Without re-updatingzero_vruntime, the stale value may be used in subsequent task selection paths:
schedule()-> ... ->pick_task_fair()->pick_next_entity()->pick_eevdf()->vruntime_eligible()If
entity_tick()->avg_vruntime()->update_zero_vruntime()is not triggered in time between dequeue and the next pick,vruntime_eligible()may use an inaccuratecfs_rq->zero_vruntime. This can potentially cause all tasks to appear ineligible, leading to NULL pointer dereference.Add an explicit
avg_vruntime(cfs_rq)call after clearingcurr->on_rqto ensurecfs_rq->zero_vruntimeis properly updated before the next pick.Fixes 147f3efaa241 ("sched/fair: Implement an EEVDF-like scheduling policy")Signed-off-by Zicheng Qu <[email protected]>Signed-off-by Zhang Qiao <[email protected]>--- kernel/sched/fair.c | 3 +++ 1 file changed, 3 insertions(+)kernel/sched/fair.c +3
-1@@ -5461,6 +5461,9 @@ dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)if (se != cfs_rq->curr)__dequeue_entity (cfs_rq, se); se->on_rq =0 ; +/* update the cfs_rq->zero_vruntime again after curr->on_rq = 0 */ +if (se == cfs_rq->curr) +avg_vruntime (cfs_rq);account_entity_dequeue (cfs_rq, se);/* return excess runtime on last dequeue */ --2.34.1
Peter Zijlstra
maintainerQuoted 10 lines · Vincent Guittot
Quoted 6 lines · Zicheng Qu
When dequeuing the current entity (
cfs_rq->curr) indequeue_entity(), thecfs_rq->zero_vruntimeis updated viaupdate_entity_lag()->avg_vruntime()->update_zero_vruntime()whilecurr->on_rqis still 1. This means the current entity is still included in thezero_vruntimecalculation.curr is not included in
zero_vruntimebut added when computingavg_vruntimesozero_vruntimeis not impacted when curr is dequeued
It is, we explicitly add curr back in.
Quoted 11 lines
Quoted 11 lines
However, immediately after this,
curr->on_rqis set to 0, which should change theavg_vruntime()result. Without re-updatingzero_vruntime, the stale value may be used in subsequent task selection paths:
schedule()-> ... ->pick_task_fair()->pick_next_entity()->pick_eevdf()->vruntime_eligible()If
entity_tick()->avg_vruntime()->update_zero_vruntime()is not triggered in time between dequeue and the next pick,vruntime_eligible()may use an inaccuratecfs_rq->zero_vruntime. This can potentially cause all tasks to appear ineligible, leading to NULL pointer dereference.
This makes no sense.
One entity worth of vruntime should not affect things to the point of overrun. Yes, it is true that zero_vruntime != avg_vruntime() right after a dequeue, but that doesn't matter.
vruntime_eligible() does the same math that avg_vruntime() does and takes this difference into account.
As long as zero_vruntime is close 'enough' to avg_vruntime, all the deltas are small and nothing overflows.
Vincent Guittot
maintainerQuoted 10 lines · Peter Zijlstra
Quoted 8 lines · Vincent Guittot
Quoted 6 lines · Zicheng Qu
When dequeuing the current entity (
cfs_rq->curr) indequeue_entity(), thecfs_rq->zero_vruntimeis updated viaupdate_entity_lag()->avg_vruntime()->update_zero_vruntime()whilecurr->on_rqis still 1. This means the current entity is still included in thezero_vruntimecalculation.
Quoted 4 lines
Quoted 2 lines
curr is not included in
zero_vruntimebut added when computingavg_vruntimesozero_vruntimeis not impacted when curr is dequeuedIt is, we explicitly add curr back in.
yeah, i should have taken one more coffee before replying
Quoted 25 lines
Quoted 5 lines
Quoted 5 lines
However, immediately after this,
curr->on_rqis set to 0, which should change theavg_vruntime()result. Without re-updatingzero_vruntime, the stale value may be used in subsequent task selection paths:Quoted 7 lines
Quoted 7 lines
schedule()-> ... ->pick_task_fair()->pick_next_entity()->pick_eevdf()->vruntime_eligible()If
entity_tick()->avg_vruntime()->update_zero_vruntime()is not triggered in time between dequeue and the next pick,vruntime_eligible()may use an inaccuratecfs_rq->zero_vruntime. This can potentially cause all tasks to appear ineligible, leading to NULL pointer dereference.This makes no sense.
One entity worth of vruntime should not affect things to the point of overrun. Yes, it is true that
zero_vruntime!=avg_vruntime()right after a dequeue, but that doesn't matter.
vruntime_eligible()does the same math thatavg_vruntime()does and takes this difference into account.As long as
zero_vruntimeis close 'enough' toavg_vruntime, all the deltas are small and nothing overflows.