* [PATCH 0/2] sched/deadline: Fix forced idle charged when a sibling ends it
[not found] <20261124080737.1988-1-kmehltretter@gmail.com>
@ 2026-11-24 9:40 ` Fernand Sieber
2026-11-24 9:40 ` [PATCH 1/2] sched/deadline: Keep forced idle uncharged " Fernand Sieber
2026-11-24 9:40 ` [PATCH 2/2] selftests/sched: Let the FIFO task block in cs_dl_server_test Fernand Sieber
0 siblings, 2 replies; 3+ messages in thread
From: Fernand Sieber @ 2026-11-24 9:40 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Fernand Sieber, Karl Mehltretter, stable, Ingo Molnar, Juri Lelli,
Vincent Guittot, Shuah Khan, Dietmar Eggemann, Steven Rostedt,
Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak,
Daniel Bristot de Oliveira, Joel Fernandes, Josh Don,
linux-kernel, linux-kselftest, nh-open-source, Fahad Mubeen,
David Woodhouse, Misha Karataiev, Hendrik Borghorst,
Ibrahim Elshahed
Karl reported that "sched/deadline: Don't charge core-sched forced idle
to the DL servers" (f4626e1c3c14, in sched/urgent) still charges forced
idle to the DL servers when the sibling's cookied task blocks: the core
pick clears core_forceidle_count before the idle CPU charges the time.
With his 800 us spin / 10 us sleep FIFO pattern the CFS task gets
0.8-1.8% instead of its 5% reservation.
Patch 1 makes the core-wide pick charge each idle sibling's idle time
under the forced-idle state from before the pick, so that no charged
span crosses a change of that state. It adds no new state. Patch 2 adds
Karl's pattern to cs_dl_server_test.
Tested on sched/core. QEMU, 2 cores x 2 threads, 8 runs each:
tip/sched/core fails Karl's pattern 8/8; with these patches both tests
pass 8/8 (5.0% and 5.6-5.8%). Both patches apply to sched/urgent, where
the existing selftest already needs 87a825f3c680 ("sched/core: Rank DL
server picks as deadline in the core-wide pick"), as in my earlier mail.
This series was prepared with the help of an AI coding assistant. I have
reviewed and tested all of it.
Fernand Sieber (2):
sched/deadline: Keep forced idle uncharged when a sibling ends it
selftests/sched: Let the FIFO task block in cs_dl_server_test
kernel/sched/core.c | 8 ++
kernel/sched/idle.c | 25 +++-
kernel/sched/sched.h | 1 +
.../selftests/sched/cs_dl_server_test.c | 107 +++++++++++-------
4 files changed, 96 insertions(+), 45 deletions(-)
Amazon Development Centre (South Africa) (Proprietary) Limited
29 Gogosoa Street, Observatory, Cape Town, Western Cape, 7925, South Africa
Registration Number: 2004 / 034463 / 07
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 1/2] sched/deadline: Keep forced idle uncharged when a sibling ends it
2026-11-24 9:40 ` [PATCH 0/2] sched/deadline: Fix forced idle charged when a sibling ends it Fernand Sieber
@ 2026-11-24 9:40 ` Fernand Sieber
2026-11-24 9:40 ` [PATCH 2/2] selftests/sched: Let the FIFO task block in cs_dl_server_test Fernand Sieber
1 sibling, 0 replies; 3+ messages in thread
From: Fernand Sieber @ 2026-11-24 9:40 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Fernand Sieber, Karl Mehltretter, stable, Ingo Molnar, Juri Lelli,
Vincent Guittot, Shuah Khan, Dietmar Eggemann, Steven Rostedt,
Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak,
Daniel Bristot de Oliveira, Joel Fernandes, Josh Don,
linux-kernel, linux-kselftest, nh-open-source, Fahad Mubeen,
David Woodhouse, Misha Karataiev, Hendrik Borghorst,
Ibrahim Elshahed
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 <kmehltretter@gmail.com>
Closes: https://lore.kernel.org/all/20261124080737.1988-1-kmehltretter@gmail.com/
Cc: stable@vger.kernel.org
Signed-off-by: Fernand Sieber <sieberf@amazon.com>
---
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] selftests/sched: Let the FIFO task block in cs_dl_server_test
2026-11-24 9:40 ` [PATCH 0/2] sched/deadline: Fix forced idle charged when a sibling ends it Fernand Sieber
2026-11-24 9:40 ` [PATCH 1/2] sched/deadline: Keep forced idle uncharged " Fernand Sieber
@ 2026-11-24 9:40 ` Fernand Sieber
1 sibling, 0 replies; 3+ messages in thread
From: Fernand Sieber @ 2026-11-24 9:40 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Fernand Sieber, Karl Mehltretter, stable, Ingo Molnar, Juri Lelli,
Vincent Guittot, Shuah Khan, Dietmar Eggemann, Steven Rostedt,
Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak,
Daniel Bristot de Oliveira, Joel Fernandes, Josh Don,
linux-kernel, linux-kselftest, nh-open-source, Fahad Mubeen,
David Woodhouse, Misha Karataiev, Hendrik Borghorst,
Ibrahim Elshahed
The FIFO child of cs_dl_server_test never blocks, so forced idle on its
sibling only ends through the fair server's pick. The test therefore
missed forced idle charged to the fair server when the cookied task
blocks and the sibling's core-wide pick ends forced idle.
Run the test a second time with the FIFO child spinning 800us and
sleeping 10us, and hold that pass to the same floor of half the fair
server's reservation.
Reported-by: Karl Mehltretter <kmehltretter@gmail.com>
Closes: https://lore.kernel.org/all/20261124080737.1988-1-kmehltretter@gmail.com/
Signed-off-by: Fernand Sieber <sieberf@amazon.com>
---
.../selftests/sched/cs_dl_server_test.c | 107 +++++++++++-------
1 file changed, 68 insertions(+), 39 deletions(-)
diff --git a/tools/testing/selftests/sched/cs_dl_server_test.c b/tools/testing/selftests/sched/cs_dl_server_test.c
index 38aa005080f1f..c085f7059e054 100644
--- a/tools/testing/selftests/sched/cs_dl_server_test.c
+++ b/tools/testing/selftests/sched/cs_dl_server_test.c
@@ -14,6 +14,10 @@
* whenever the FIFO task runs, and checks that the CFS task still gets a
* sensible fraction of the fair server's reservation.
*
+ * A second pass lets the FIFO task sleep briefly between spins, so forced idle
+ * mostly ends through the core-wide pick made when the FIFO task blocks rather
+ * than through the fair server.
+ *
* Copyright (c) 2026 Amazon.com, Inc. or its affiliates.
*/
#define _GNU_SOURCE
@@ -21,6 +25,7 @@
#include <limits.h>
#include <sched.h>
#include <signal.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -48,6 +53,8 @@ enum pid_type {PIDTYPE_PID = 0, PIDTYPE_TGID, PIDTYPE_PGID};
#define RUN_TIME 5 /* measurement window, in seconds */
#define FIFO_PRIO 50
+#define FIFO_SPIN_US 800 /* blocking pass: spin this long ... */
+#define FIFO_SLEEP_US 10 /* ... then sleep this long */
/*
* The reservation is only approached, not reached: every slice the fair server
@@ -208,6 +215,20 @@ static double monotonic_now(void)
return ts.tv_sec + ts.tv_nsec / 1000000000.0;
}
+/* Spin for FIFO_SPIN_US, then sleep for FIFO_SLEEP_US, until killed. */
+static void spin_sleep_forever(void)
+{
+ struct timespec nap = { .tv_nsec = FIFO_SLEEP_US * 1000 };
+ double until;
+
+ while (1) {
+ until = monotonic_now() + FIFO_SPIN_US / 1000000.0;
+ while (monotonic_now() < until)
+ ;
+ nanosleep(&nap, NULL);
+ }
+}
+
/*
* Diagnostic only: how long the FIFO task's CPU was forced idle. Needs
* CONFIG_SCHEDSTATS and schedstats enabled, so it must not gate the result.
@@ -233,7 +254,7 @@ static void print_forced_idle(pid_t pid)
}
/* Cookied SCHED_FIFO spinner. Only this task gets a cookie. */
-static void fifo_child(int cpu, int ready_fd)
+static void fifo_child(int cpu, int ready_fd, bool blocks)
{
struct sched_param param = { .sched_priority = FIFO_PRIO };
@@ -252,6 +273,8 @@ static void fifo_child(int cpu, int ready_fd)
signal_status(ready_fd, CHILD_ERROR);
}
signal_status(ready_fd, CHILD_READY);
+ if (blocks)
+ spin_sleep_forever();
spin_forever();
}
@@ -287,45 +310,18 @@ static void kill_child(pid_t *pid)
*pid = -1;
}
-int main(void)
+/*
+ * Run the FIFO and CFS spinners for RUN_TIME and check the CFS task's share.
+ * With @blocks the FIFO task sleeps between spins.
+ */
+static void test_forced_idle(int sib0, int sib1, double want, bool blocks)
{
- double fifo_runtime, cfs_start, cfs_end, t_start, t_end, share, want;
- unsigned long long runtime, period;
+ double fifo_runtime, cfs_start, cfs_end, t_start, t_end, share;
pid_t fifo_pid = -1, cfs_pid = -1;
int fifo_ready[2], cfs_ready[2];
- unsigned long long cookie;
- int sib0, sib1;
+ const char *pass = blocks ? " (FIFO task sleeps)" : "";
char status;
- ksft_print_header();
- ksft_set_plan(1);
-
- if (read_sibling_pair(&sib0, &sib1)) {
- ksft_test_result_skip("CPU0 has no SMT sibling\n");
- ksft_finished();
- }
- ksft_print_msg("SMT siblings: CPU%d (FIFO) and CPU%d (CFS)\n", sib0, sib1);
-
- if (prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, 0, PIDTYPE_PID,
- (unsigned long)&cookie)) {
- ksft_test_result_skip("core scheduling unavailable: %s\n",
- strerror(errno));
- ksft_finished();
- }
-
- if (read_fair_server(sib1, &runtime, &period)) {
- ksft_test_result_skip("cannot read " FAIR_SERVER "/cpu%d, debugfs not mounted?\n",
- sib1);
- ksft_finished();
- }
- if (!runtime || !period) {
- ksft_test_result_skip("fair server on CPU%d is disabled (%llu/%llu)\n",
- sib1, runtime, period);
- ksft_finished();
- }
- ksft_print_msg("CPU%d fair server reservation: %llu/%llu ns = %.3f%%\n",
- sib1, runtime, period, 100.0 * runtime / period);
-
if (pipe(fifo_ready) || pipe(cfs_ready))
ksft_exit_fail_perror("pipe");
@@ -334,7 +330,7 @@ int main(void)
close(fifo_ready[0]);
close(cfs_ready[0]);
close(cfs_ready[1]);
- fifo_child(sib0, fifo_ready[1]);
+ fifo_child(sib0, fifo_ready[1], blocks);
} else if (fifo_pid < 0) {
ksft_exit_fail_perror("fork FIFO child");
}
@@ -348,7 +344,7 @@ int main(void)
close(cfs_ready[1]);
if (status == CHILD_EPERM) {
ksft_test_result_skip("need privileges for SCHED_FIFO\n");
- ksft_finished();
+ return;
}
ksft_exit_fail_msg("FIFO child setup failed\n");
}
@@ -388,7 +384,6 @@ int main(void)
ksft_exit_fail_msg("cannot read CLOCK_MONOTONIC\n");
share = (cfs_end - cfs_start) / (t_end - t_start);
- want = MIN_SHARE_OF_RESERVATION * runtime / period;
ksft_print_msg("FIFO task ran %.3fs, CFS task ran %.3fs over %.3fs\n",
fifo_runtime, cfs_end - cfs_start, t_end - t_start);
@@ -396,7 +391,41 @@ int main(void)
sib1, share * 100, want * 100);
ksft_test_result(share >= want,
- "CFS task gets the fair server reservation while forced idle\n");
+ "CFS task gets the fair server reservation while forced idle%s\n",
+ pass);
+}
+
+int main(void)
+{
+ unsigned long long runtime, period;
+ unsigned long long cookie;
+ int sib0, sib1;
+ double want;
+
+ ksft_print_header();
+
+ if (read_sibling_pair(&sib0, &sib1))
+ ksft_exit_skip("CPU0 has no SMT sibling\n");
+ ksft_print_msg("SMT siblings: CPU%d (FIFO) and CPU%d (CFS)\n", sib0, sib1);
+
+ if (prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, 0, PIDTYPE_PID,
+ (unsigned long)&cookie))
+ ksft_exit_skip("core scheduling unavailable: %s\n",
+ strerror(errno));
+
+ if (read_fair_server(sib1, &runtime, &period))
+ ksft_exit_skip("cannot read " FAIR_SERVER "/cpu%d, debugfs not mounted?\n",
+ sib1);
+ if (!runtime || !period)
+ ksft_exit_skip("fair server on CPU%d is disabled (%llu/%llu)\n",
+ sib1, runtime, period);
+ ksft_print_msg("CPU%d fair server reservation: %llu/%llu ns = %.3f%%\n",
+ sib1, runtime, period, 100.0 * runtime / period);
+
+ ksft_set_plan(2);
+ want = MIN_SHARE_OF_RESERVATION * runtime / period;
+ test_forced_idle(sib0, sib1, want, false);
+ test_forced_idle(sib0, sib1, want, true);
ksft_finished();
}
--
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-24 9:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261124080737.1988-1-kmehltretter@gmail.com>
2026-11-24 9:40 ` [PATCH 0/2] sched/deadline: Fix forced idle charged when a sibling ends it Fernand Sieber
2026-11-24 9:40 ` [PATCH 1/2] sched/deadline: Keep forced idle uncharged " Fernand Sieber
2026-11-24 9:40 ` [PATCH 2/2] selftests/sched: Let the FIFO task block in cs_dl_server_test Fernand Sieber
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox