Skip to contentSkip to conversation

sched/fair: Drop min_vruntime() call from set_protect_slice()

PATCH In review Kayra Cizmeci 2 participants 3 messages Aug 10 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/fair: Drop min_vruntime() call from set_protect_slice()

The branch is only taken when slice is not equal to se->slice, and that means se->slice is bigger than slice, since after the min() call, slice is assigned the smaller one of se->slice and slice.

The min_vruntime comparison always picks the calculated value, since it is smaller.

So, skip the comparison by not calling min_vruntime and assign the calculated value directly to vprot. No functional change intended.

---
I only did a boot test on x86 (Zen 3), since the change is well below my noise floor.

 kernel/sched/fair.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/sched/fair.c +6 -1
index d78467ec6ee1..97506b2edf7d 100644
@@ -1090,8 +1090,13 @@ static inline void set_protect_slice(struct cfs_rq *cfs_rq, struct sched_entity
 		slice = cfs_rq_min_slice(cfs_rq);
 
 	slice = min(slice, se->slice);
+	/*
+	 * slice < se->slice, so the calculated value
+	 * is always before se->deadline.
+	 * No comparison needed.
+	 */
 	if (slice != se->slice)
-		vprot = min_vruntime(vprot, se->vruntime + calc_delta_fair(slice, se));
+		vprot = se->vruntime + calc_delta_fair(slice, se);
 
 	se->vprot = vprot;
 }
Signature · 1 line
2.53.0
#1 lore raw
Quoted 2 lines · Kayra Cizmeci

The min_vruntime comparison always picks the calculated value, since it is smaller.

The argument needs @vprot to be se->vruntime + calc_delta_fair(se->slice, se). It is se->deadline, which is only that much ahead just after the deadline is set. place_entity() does not even set a full slice:

	vslice = calc_delta_fair(se->slice, se);
	...
	if (sched_feat(PLACE_DEADLINE_INITIAL) && (flags & ENQUEUE_INITIAL))
		vslice /= 2;
	...
	se->deadline = se->vruntime + vslice;

PLACE_DEADLINE_INITIAL is on by default, so a new entity reaches its first pick with the deadline half a slice ahead. The branch you are simplifying runs when cfs_rq_min_slice() is below se->slice, i.e. with custom slices in play; if it is also above half of se->slice, then
se->vruntime + calc_delta_fair(slice, se) is past se->deadline and min_vruntime() is what keeps @vprot there.

The gap also shrinks while an entity runs: update_deadline() returns early below the deadline, and put_prev_entity() re-enqueues a preempted entity without renewing it, so by the next pick se->deadline - se->vruntime is only what is left of the slice.

Assigning the calculated value unconditionally puts se->vprot past se->deadline, and protect_slice() only compares se->vruntime against se->vprot, so RUN_TO_PARITY would keep protecting the entity past its own deadline.

Thanks,
Zhan Xusheng

#2 lore raw
Quoted 3 lines

The argument needs @vprot to be se->vruntime + calc_delta_fair(se->slice, se). It is se->deadline, which is only that much ahead just after the deadline is set. place_entity() does not even set a full slice

You are right. I completely overlooked that. I assumed that the deadline is always calculated with se->slice but that is wrong. Thanks for the detailed explanation.

Please drop this patch.

Thanks,
Kayra