Re: [RFC][PATCH 3/3] sched/eevdf: Fix sched idle task group preemption

From: Josh Don

Date: Mon Nov 09 2026 - 19:11:19 EST


Hi Peter,

On Thu, Nov 5, 2026 at 9:12 AM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> From: Vincent Guittot <vincent.guittot@xxxxxxxxxx>
>
> Even with flat hierarchy which puts all runnable tasks in the same cfs rq,
> we need to parse the hierarchy to check for sched_idle task group.
>
> Fixes: 85570f10a4c6 ("sched/eevdf: Move to a single runqueue")
> Reported-by: Jakub Okoński <jakub@xxxxxxxxxxx>
> Signed-off-by: Vincent Guittot <vincent.guittot@xxxxxxxxxx>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
> Link: https://lore.kernel.org/all/58dc8e8a-7260-43ce-aebb-8d9c29a6afc9@xxxxxxxxxxx/
> Link: https://patch.msgid.link/20261019182622.1584454-1-vincent.guittot@xxxxxxxxxx
> ---
>
> Changes since v1:
> - fix prototype and cleanup unsued variable
>
> kernel/sched/fair.c | 87 ++++++++++++++++++++++++++++++++++++++++++++--------
> 1 file changed, 74 insertions(+), 13 deletions(-)
>
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -433,6 +433,38 @@ static inline struct sched_entity *paren
> return se->parent;
> }
>
> +static void
> +find_matching_se(struct sched_entity **se, struct sched_entity **pse)

Thank you for restoring this :)

> +{
> + int se_depth, pse_depth;
> +
> + /*
> + * preemption test can be made between sibling entities who are in the
> + * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
> + * both tasks until we find their ancestors who are siblings of common
> + * parent.
> + */
> +
> + /* First walk up until both entities are at same depth */
> + se_depth = (*se)->depth;
> + pse_depth = (*pse)->depth;
> +
> + while (se_depth > pse_depth) {
> + se_depth--;
> + *se = parent_entity(*se);
> + }
> +
> + while (pse_depth > se_depth) {
> + pse_depth--;
> + *pse = parent_entity(*pse);
> + }
> +
> + while (!is_same_group(*se, *pse)) {
> + *se = parent_entity(*se);
> + *pse = parent_entity(*pse);
> + }
> +}
> +
> static int tg_is_idle(struct task_group *tg)
> {
> return tg->idle > 0;
> @@ -476,6 +508,11 @@ static inline struct sched_entity *paren
> return NULL;
> }
>
> +static inline void
> +find_matching_se(struct sched_entity **se, struct sched_entity **pse)
> +{
> +}
> +
> static inline int tg_is_idle(struct task_group *tg)
> {
> return 0;
> @@ -10130,7 +10167,40 @@ preempt_sync(struct rq *rq, int wake_fla
> }
>
> /*
> - * Preempt the current task with a newly woken task if needed:
> + * Preempt an idle entity in favor of a non-idle entity (and don't preempt
> + * in the inverse case).
> + */
> +static inline enum preempt_wakeup_action
> +wakeup_preempt_sched_idle(struct sched_entity *cse, struct sched_entity *pse)
> +{
> + int cse_is_idle, pse_is_idle;
> +
> + cse_is_idle = se_is_idle(cse);
> + pse_is_idle = se_is_idle(pse);
> +
> + if (cse_is_idle || pse_is_idle) {
> + find_matching_se(&cse, &pse);
> + WARN_ON_ONCE(!pse);
> +
> + cse_is_idle = se_is_idle(cse);
> + pse_is_idle = se_is_idle(pse);

now that se_is_idle() is checking for any ancestors with idle, this
doesn't appear to be correct. Some motivaing examples:

Ex 1:
regular task and SCHED_IDLE task both inside an idle cgroup.
se_is_idle() will be true for both se's, and so the regular task will
not end up preempting the SCHED_IDLE task, which would be the
desirable behavior (the task is not idle relative to the SCHED_IDLE
task)

cgrp A (idle)
/ \
regular_task SCHED_IDLE task

Ex2:
more complex example with a hierarchy of cgroups

root
|
cgrp A (idle)
/ \
cgrp B cgrp C (idle)
| |
task_1 task_2

task_1 should preempt task_2, and the inverse should also never
happen. However, the matching_se here are cgroups B and C, and both
show se_is_idle() as true

I can think of two simple ways to fix that, either seems fine
1) use the existing se_is_idle() check for knowing when we can
fast-path skip the whole find_matching_se() business, but when we do
find the matching se, rather than use se_is_idle, use a more targeted
helper that directly compares just those two entities without any of
their ancestors (ie. for the task case just check if it has an idle
policy, and for the group case, just check tg_is_idle())
2) once you find the matching se, just compare the ->idle values,
since if one of them is bigger (by at most 1?), then that one must be
the "idle" one relatively speaking. Handling the task vs group case
here seems a bit messier compared to the first option, so my
suggestion would be to go with the above

> +
> + /*
> + * Preempt an idle entity in favor of a non-idle entity (and
> + * don't preempt in the inverse case).
> + */
> + if (cse_is_idle && !pse_is_idle)
> + return PREEMPT_WAKEUP_RESCHED;
> +
> + if (cse_is_idle != pse_is_idle)
> + return PREEMPT_WAKEUP_NONE;
> + }
> +
> + return PREEMPT_WAKEUP_PICK;
> +}
> +
> +/*
> + * Preempt the current task with a newly woken task if needed.
> */
> static void wakeup_preempt_fair(struct rq *rq, struct task_struct *p, int wake_flags)
> {
> @@ -10138,7 +10208,6 @@ static void wakeup_preempt_fair(struct r
> struct task_struct *donor = rq->donor;
> struct sched_entity *nse, *se = &donor->se, *pse = &p->se;
> struct cfs_rq *cfs_rq = &rq->cfs;
> - int cse_is_idle, pse_is_idle;
>
> /*
> * XXX Getting preempted by higher class, try and find idle CPU?
> @@ -10175,22 +10244,14 @@ static void wakeup_preempt_fair(struct r
> if (!sched_feat(WAKEUP_PREEMPTION))
> return;
>
> - WARN_ON_ONCE(!pse);
> -
> - cse_is_idle = se_is_idle(se);
> - pse_is_idle = se_is_idle(pse);
> + preempt_action = wakeup_preempt_sched_idle(se, pse);
>
> - nse = se;
> - /*
> - * Preempt an idle entity in favor of a non-idle entity (and don't preempt
> - * in the inverse case).
> - */
> - if (cse_is_idle && !pse_is_idle)
> + if (preempt_action == PREEMPT_WAKEUP_RESCHED)
> goto preempt;
>
> update_curr_fair(rq);
>
> - if (cse_is_idle != pse_is_idle)
> + if (preempt_action == PREEMPT_WAKEUP_NONE)
> goto update;
>
> /*
>
>

Best,
Josh