Re: [PATCH 0/4] sched/fair: Manage lag and run to parity with different slices

From: Peter Zijlstra
Date: Tue Jun 17 2025 - 05:22:24 EST


On Fri, Jun 13, 2025 at 04:05:10PM +0200, Vincent Guittot wrote:
> Vincent Guittot (3):
> sched/fair: Use protect_slice() instead of direct comparison
> sched/fair: Limit run to parity to the min slice of enqueued entities
> sched/fair: Improve NO_RUN_TO_PARITY

Ah. I wrote these here patches and then totally forgot about them :/.
They take a different approach.

The approach I took was to move decision to stick with curr after pick,
instead of before it. That way we can evaluate the tree at the time of
preemption.


Subject: sched/fair: Fix requeue_delayed_entity()
From: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Date: Fri Apr 4 11:23:00 CEST 2025

Since enqueue_task_fair() doesn't call update_curr() before calling
requeue_delayed_entity(), which means that update_entity_lag() uses a
slightly out-of-date avg_vruntime() -- which includes current.

Fixes: 54a58a787791 ("sched/fair: Implement DELAY_ZERO")
Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
---
kernel/sched/fair.c | 2 ++
1 file changed, 2 insertions(+)

--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -6893,6 +6893,8 @@ requeue_delayed_entity(struct sched_enti
{
struct cfs_rq *cfs_rq = cfs_rq_of(se);

+ update_curr(cfs_rq);
+
/*
* se->sched_delayed should imply: se->on_rq == 1.
* Because a delayed entity is one that is still on
Subject: sched/eevdf: Re-arrange current protection in pick_eevdf()
From: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Date: Fri Apr 4 10:15:15 CEST 2025

The way pick_eevdf() limits preemption is by explicitly picking
current if it is still eligible. It does this without consideration of
the best in-tree task.

Move current protection to after the tree selection such that a follow
up patch can change the conditions.

Should be an semantics no-op at this point.

Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
---
kernel/sched/fair.c | 33 +++++++++++++++++++++++++--------
1 file changed, 25 insertions(+), 8 deletions(-)

--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -903,6 +903,26 @@ static inline void cancel_protect_slice(
se->vlag = se->deadline + 1;
}

+static inline bool __pick_eevdf_curr(struct cfs_rq *cfs_rq, struct sched_entity *best)
+{
+ struct sched_entity *curr = cfs_rq->curr;
+
+ /* only called when this is already checked */
+ WARN_ON_ONCE(!curr || !curr->on_rq);
+
+ /*
+ * Strictly speaking we should allow current to run until its
+ * deadline. However allow (wakeup) preemption once it is no longer
+ * eligible.
+ */
+ if (sched_feat(RUN_TO_PARITY) &&
+ protect_slice(curr) &&
+ entity_eligible(cfs_rq, curr))
+ return true;
+
+ return entity_before(curr, best);
+}
+
/*
* Earliest Eligible Virtual Deadline First
*
@@ -929,18 +949,15 @@ static struct sched_entity *pick_eevdf(s
struct sched_entity *curr = cfs_rq->curr;
struct sched_entity *best = NULL;

+ if (curr && !curr->on_rq)
+ curr = NULL;
+
/*
* We can safely skip eligibility check if there is only one entity
* in this cfs_rq, saving some cycles.
*/
if (cfs_rq->nr_queued == 1)
- return curr && curr->on_rq ? curr : se;
-
- if (curr && (!curr->on_rq || !entity_eligible(cfs_rq, curr)))
- curr = NULL;
-
- if (sched_feat(RUN_TO_PARITY) && curr && protect_slice(curr))
- return curr;
+ return curr ?: se;

/* Pick the leftmost entity if it's eligible */
if (se && entity_eligible(cfs_rq, se)) {
@@ -977,7 +994,7 @@ static struct sched_entity *pick_eevdf(s
node = node->rb_right;
}
found:
- if (!best || (curr && entity_before(curr, best)))
+ if (!best || (curr && __pick_eevdf_curr(cfs_rq, best)))
best = curr;

return best;
Subject: sched/eevdf: Fix RUN_TO_PARITY vs PREEMPT_SHORT
From: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Date: Fri Apr 4 10:25:03 CEST 2025

Vincent noted that RUN_TO_PARITY can prevent preemption by a shorter
slice under some conditions.

Reported-by: Vincent Guittot <vincent.guittot@xxxxxxxxxx>
Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
---
kernel/sched/fair.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -918,7 +918,7 @@ static inline bool __pick_eevdf_curr(str
if (sched_feat(RUN_TO_PARITY) &&
protect_slice(curr) &&
entity_eligible(cfs_rq, curr))
- return true;
+ return !sched_feat(PREEMPT_SHORT) || curr->slice < best->slice;

return entity_before(curr, best);
}
@@ -1195,7 +1195,7 @@ static inline bool did_preempt_short(str
if (!sched_feat(PREEMPT_SHORT))
return false;

- if (curr->vlag == curr->deadline)
+ if (protect_slice(curr))
return false;

return !entity_eligible(cfs_rq, curr);