Commit f4626e1c3c14 ("sched/deadline: Don't charge core-sched forced idle to the DL servers") keeps forced idle from being charged to the fair and ext servers. But it tests forced_idle_rq() when the idle time is charged, not when the idle task started running. When the sibling ends the forced idle, the test reads false by then and the forced-idle span is charged anyway. The timeline below describes the issue: Setup. CPU0 and CPU1 are SMT siblings. - CPU1 runs R, a SCHED_FIFO task with cookie X, which spins and sleeps briefly in a loop. - CPU0 has C, a CFS task with no cookie, which only runs through CPU0's fair server. t0, CPU0 is forced idle. The core cookie is X and CPU0 has no cookie-X task, so CPU0 runs idle. core_forceidle_count is set and idle->se.exec_start is stamped. t1, R blocks. CPU1 does the core-wide pick. No cookie-X task is left, so forced idle ends: the pick clears core_forceidle_count and picks C for CPU0. t2, CPU0 switches from idle to C. update_curr_idle() charges the idle time since its last update to the fair and ext servers. forced_idle_rq(rq0) reads false, since t1 cleared the count, so the forced-idle span up to t1 is charged. t3, result. The fair server spends its runtime on time in which C could not run, and C gets throttled. With R spinning 800us and sleeping 10us (HZ=1000, 2-core 2-thread KVM guest), C gets 0.8-1.8% of the CPU instead of the 5% reservation. With this patch, the core-wide pick settles the idle time of each idle sibling under the state from before the pick: at t1 it drops CPU0's forced-idle span, and a pick that starts forced idle charges the idle time before it. As only this pick changes core_forceidle_count, forced_idle_rq() then holds for the whole span that update_curr_idle() charges. With this, C gets 5.6-5.8% in the same setup. Fixes: f4626e1c3c14 ("sched/deadline: Don't charge core-sched forced idle to the DL servers") Reported-by: Karl Mehltretter Closes: https://lore.kernel.org/all/20261124080737.1988-1-kmehltretter@gmail.com/ Cc: stable@vger.kernel.org Signed-off-by: Fernand Sieber --- kernel/sched/core.c | 8 ++++++++ kernel/sched/idle.c | 25 +++++++++++++++++++------ kernel/sched/sched.h | 1 + 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index ea89471d4743c..825d9bbea3128 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6451,6 +6451,14 @@ pick_next_task(struct rq *rq, struct rq_flags *rf) if (i != cpu && (rq_i != rq->core || !core_clock_updated)) update_rq_clock(rq_i); + /* + * This pick can start or end forced idle on rq_i while its + * idle task runs on, and the old state is already reset. + * Charge the idle time so far under the old state. + */ + if (rq_i->curr == rq_i->idle) + charge_idle_to_dl_servers(rq_i, fi_before && rq_i->nr_running); + p = pick_task(rq_i, rf); if (unlikely(p == RETRY_TASK)) { /* rq lock may have been dropped, clocks invalidated */ diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c index 451b4846219d5..0449ccefba6c7 100644 --- a/kernel/sched/idle.c +++ b/kernel/sched/idle.c @@ -561,7 +561,14 @@ prio_changed_idle(struct rq *rq, struct task_struct *p, u64 oldprio) BUG(); } -static void update_curr_idle(struct rq *rq) +/* + * Charge the time the idle task of @rq ran since its se.exec_start to the + * fair and ext servers, and advance se.exec_start to now. Nothing is + * charged if @span_was_forced_idle: forced idle was not time available to + * any class on @rq, so the deferred servers must not consume their runtime + * on it. + */ +void charge_idle_to_dl_servers(struct rq *rq, bool span_was_forced_idle) { struct sched_entity *se = &rq->idle->se; u64 now = rq_clock_task(rq); @@ -573,11 +580,7 @@ static void update_curr_idle(struct rq *rq) se->exec_start = now; - /* - * Forced idle was not time available to any class on this rq, so - * don't let the deferred servers consume their runtime on it. - */ - if (forced_idle_rq(rq)) + if (span_was_forced_idle) return; dl_server_update_idle(&rq->fair_server, delta_exec); @@ -586,6 +589,16 @@ static void update_curr_idle(struct rq *rq) #endif } +/* + * Only the core-wide pick starts or ends forced idle, and it charges the + * idle time up to that point under the old state, so the current state is + * the state of the whole span here. + */ +static void update_curr_idle(struct rq *rq) +{ + charge_idle_to_dl_servers(rq, forced_idle_rq(rq)); +} + /* * Simple, special scheduling class for the per-CPU idle tasks: */ diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 32f76551a0c5e..1c61a1a9309a0 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3005,6 +3005,7 @@ static inline bool sched_fair_runnable(struct rq *rq) extern struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf); extern struct task_struct *pick_task_idle(struct rq *rq, struct rq_flags *rf); +void charge_idle_to_dl_servers(struct rq *rq, bool span_was_forced_idle); #define SCA_CHECK 0x01 #define SCA_MIGRATE_DISABLE 0x02 -- 2.47.3 Amazon Development Centre (South Africa) (Proprietary) Limited 29 Gogosoa Street, Observatory, Cape Town, Western Cape, 7925, South Africa Registration Number: 2004 / 034463 / 07