* [PATCH v2 0/3] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path
@ 2026-11-20 10:58 Ibrahim Elshahed
2026-11-20 10:58 ` [PATCH v2 1/3] sched/core: Clear a stale rq->core_pick after dequeue Ibrahim Elshahed
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Ibrahim Elshahed @ 2026-11-20 10:58 UTC (permalink / raw)
To: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak, Josh Don, Shuah Khan,
linux-kselftest, linux-kernel, nh-open-source, Fernand Sieber,
Roman Kagan
Under core scheduling, a core-wide selection can leave a pick for an
idle sibling in rq->core_pick and force the other sibling idle. If the
picked task is migrated away before the idle sibling schedules, and it
was that sibling's only task, the SM_IDLE fast path (v6.12+) sends the
sibling back to idle without a pick. The core then stays forced idle
with nothing running, and the forced idle accounting keeps using the
stale pointer, also after the task and its cgroup are freed.
Patch 1 clears rq->core_pick when the picked task is dequeued. Patch 2
stops idle CPUs from taking the SM_IDLE fast path while the core is
forced idle. Patch 3 adds a selftest for the stuck forced idle and the
use-after-free.
Changes in v2:
- Patch 2: drop the rq->core_pick check. With patch 1 a pending pick
is always queued on its rq, so an empty runqueue has none (Peter).
Reword the subject and changelog accordingly.
- Patch 3: new, a selftest derived from the reproducer (Peter). It
checks for stuck forced idle and, with cgroup v2 and schedstats,
frees each stalled task with its cgroup to catch the use-after-free.
- v1: https://lore.kernel.org/lkml/20261117142704.6878-1-lshahed@amazon.de/
Testing, on tip sched/core d7e670601af6, schedstats=enable:
- KVM guest, 2 cores x 2 threads, idle=halt, the v1 reproducer. Without
the series, both boots oopsed in __cgroup_account_cputime_field()
from __sched_core_account_forceidle(). With v2, 6 boots of 240 s
(972562 attempts) showed no stuck forced idle and no oops; the
no-migrate control (194596 attempts) was clean as well.
- cs_forceidle_test through run_kselftest.sh in the same guest, 30 s
runs. Without the series, all 11 runs crashed in
__cgroup_account_cputime_field() 0.4-2.2 s into the test (with
KASAN, 2 of 2 first reported a slab-use-after-free in
__account_forceidle_time()); with -n, which skips freeing the
stalled tasks, 72, 59 and 67 stalls in about 20000 attempts each.
With v2, 10 of 10 runs passed with 0 stalls and nothing in dmesg (2
with KASAN, 2 booted without schedstats). With patch 1 alone, 42, 43
and 34 stalls and no crash; with patch 2 alone, 0 stalls.
- Builds with SCHED_CORE on and off, SCHEDSTATS on and off,
SCHED_CLASS_EXT, PREEMPT_RT, tinyconfig and allnoconfig; W=1 clean in
kernel/sched/, and each patch builds on its own.
Ibrahim Elshahed (3):
sched/core: Clear a stale rq->core_pick after dequeue
sched/core: Don't take the idle fast path while the core is forced
idle
selftests/sched: Add a test for stuck forced idle under core
scheduling
kernel/sched/core.c | 28 +-
tools/testing/selftests/sched/.gitignore | 1 +
tools/testing/selftests/sched/Makefile | 1 +
tools/testing/selftests/sched/config | 2 +-
.../selftests/sched/cs_forceidle_test.c | 471 ++++++++++++++++++
5 files changed, 500 insertions(+), 3 deletions(-)
create mode 100644 tools/testing/selftests/sched/cs_forceidle_test.c
base-commit: d7e670601af6c564ccf6d3b2ddf70ee4f41a6d1c
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/3] sched/core: Clear a stale rq->core_pick after dequeue
2026-11-20 10:58 [PATCH v2 0/3] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path Ibrahim Elshahed
@ 2026-11-20 10:58 ` Ibrahim Elshahed
2026-11-20 10:58 ` [PATCH v2 2/3] sched/core: Don't take the idle fast path while the core is forced idle Ibrahim Elshahed
2026-11-20 10:58 ` [PATCH v2 3/3] selftests/sched: Add a test for stuck forced idle under core scheduling Ibrahim Elshahed
2 siblings, 0 replies; 4+ messages in thread
From: Ibrahim Elshahed @ 2026-11-20 10:58 UTC (permalink / raw)
To: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak, Josh Don, Shuah Khan,
linux-kselftest, linux-kernel, nh-open-source, Fernand Sieber,
Roman Kagan
A core-wide selection leaves the task chosen for each sibling in that
sibling's rq->core_pick until pick_next_task() on the sibling consumes
it. If the task is dequeued before the sibling consumes it, for example
migrated away, the pointer stays until the sibling's next
pick_next_task() overwrites it. Until then
__sched_core_account_forceidle() keeps charging forced idle time to a
task that no longer runs there and, with schedstats enabled, to its
cgroup, even after the task has exited.
Clear rq->core_pick and rq->core_dl_server in sched_core_dequeue() once
the pending pick is dequeued, and don't wait for pick_next_task() to
overwrite them.
Fixes: 4feee7d12603 ("sched/core: Forced idle accounting")
Cc: stable@vger.kernel.org
Co-developed-by: Roman Kagan <rkagan@amazon.de>
Signed-off-by: Roman Kagan <rkagan@amazon.de>
Signed-off-by: Ibrahim Elshahed <lshahed@amazon.de>
---
kernel/sched/core.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 175b52a7ea79..b1718ba5a4d1 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -328,6 +328,15 @@ void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags)
RB_CLEAR_NODE(&p->core_node);
}
+ /*
+ * @p left this rq, so it can no longer be the pick a sibling made.
+ * This also avoids forced idle accounting charging it once it is gone.
+ */
+ if (rq->core_pick == p) {
+ rq->core_pick = NULL;
+ rq->core_dl_server = NULL;
+ }
+
/*
* Migrating the last task off the cpu, with the cpu in forced idle
* state. Reschedule to create an accounting edge for forced idle,
--
2.47.3
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 2/3] sched/core: Don't take the idle fast path while the core is forced idle
2026-11-20 10:58 [PATCH v2 0/3] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path Ibrahim Elshahed
2026-11-20 10:58 ` [PATCH v2 1/3] sched/core: Clear a stale rq->core_pick after dequeue Ibrahim Elshahed
@ 2026-11-20 10:58 ` Ibrahim Elshahed
2026-11-20 10:58 ` [PATCH v2 3/3] selftests/sched: Add a test for stuck forced idle under core scheduling Ibrahim Elshahed
2 siblings, 0 replies; 4+ messages in thread
From: Ibrahim Elshahed @ 2026-11-20 10:58 UTC (permalink / raw)
To: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak, Josh Don, Shuah Khan,
linux-kselftest, linux-kernel, nh-open-source, Fernand Sieber,
Roman Kagan
Since v6.12, with commit 3dcac251b066 ("sched/core: Introduce SM_IDLE
and an idle re-entry fast-path in __schedule()"), an idle CPU with an
empty runqueue skips pick_next_task() when it is rescheduled. Under
core scheduling that pick also ends or re-examines the core's forced
idle.
A core-wide selection chooses a task for an idle sibling and reschedules
it. If the chosen task is migrated away before the sibling schedules
and it was the sibling's only task, the sibling goes back to idle
through the fast path. When that selection also forced the other
sibling idle, because its runnable tasks did not match the chosen
task's cookie, the core stays forced idle with nothing running on it,
and two things go wrong:
- Without the previous patch, the idle sibling's rq->core_pick keeps
pointing at the migrated task, and with schedstats enabled the forced
idle accounting keeps writing through it, even after the task and its
cgroup are freed:
BUG: unable to handle page fault for address: ffff8d6b205f7018
#PF: supervisor write access in kernel mode
#PF: error_code(0x0002) - not-present page
RIP: 0010:__cgroup_account_cputime_field+0x3c/0x70
Call Trace:
__sched_core_account_forceidle+0xd8/0x140
__schedule+0x1390/0x18d0
schedule_idle+0x1a/0x30
cpu_startup_entry+0x29/0x30
start_secondary+0x11e/0x140
- The forced-idle sibling keeps waiting with runnable tasks until some
unrelated event reschedules the core. Likewise, when the forced-idle
CPU itself loses its last task, the reschedule that
sched_core_dequeue() sends to end the forced idle also ends in the
fast path.
Take the SM_IDLE fast path only when the core is not forced idle. A
pending rq->core_pick needs no check of its own: the previous patch
clears it when the picked task is dequeued, so a CPU with an empty
runqueue has none.
Fixes: 3dcac251b066 ("sched/core: Introduce SM_IDLE and an idle re-entry fast-path in __schedule()")
Cc: stable@vger.kernel.org
Co-developed-by: Roman Kagan <rkagan@amazon.de>
Signed-off-by: Roman Kagan <rkagan@amazon.de>
Signed-off-by: Ibrahim Elshahed <lshahed@amazon.de>
---
kernel/sched/core.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index b1718ba5a4d1..d2f7532fc7cf 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -347,6 +347,15 @@ void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags)
resched_curr(rq);
}
+/*
+ * An idle CPU with an empty runqueue must still call pick_next_task() while
+ * the core is forced idle. Only that pick ends or re-examines the forced idle.
+ */
+static inline bool sched_core_idle_needs_pick(struct rq *rq)
+{
+ return sched_core_enabled(rq) && rq->core->core_forceidle_count;
+}
+
static int sched_task_is_throttled(struct task_struct *p, int cpu)
{
if (p->sched_class->task_is_throttled)
@@ -550,6 +559,7 @@ void sched_core_put(void)
static inline void sched_core_enqueue(struct rq *rq, struct task_struct *p) { }
static inline void
sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags) { }
+static inline bool sched_core_idle_needs_pick(struct rq *rq) { return false; }
#endif /* !CONFIG_SCHED_CORE */
@@ -7285,8 +7295,13 @@ static void __sched notrace __schedule(int sched_mode)
*/
prev_state = READ_ONCE(prev->__state);
if (sched_mode == SM_IDLE) {
- /* SCX must consult the BPF scheduler to tell if rq is empty */
- if (!rq->nr_running && !scx_enabled()) {
+ /*
+ * SCX must consult the BPF scheduler to tell if rq is empty,
+ * and core scheduling may need pick_next_task() to end forced
+ * idle.
+ */
+ if (!rq->nr_running && !scx_enabled() &&
+ !sched_core_idle_needs_pick(rq)) {
next = prev;
rq->next_class = &idle_sched_class;
goto picked;
--
2.47.3
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 3/3] selftests/sched: Add a test for stuck forced idle under core scheduling
2026-11-20 10:58 [PATCH v2 0/3] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path Ibrahim Elshahed
2026-11-20 10:58 ` [PATCH v2 1/3] sched/core: Clear a stale rq->core_pick after dequeue Ibrahim Elshahed
2026-11-20 10:58 ` [PATCH v2 2/3] sched/core: Don't take the idle fast path while the core is forced idle Ibrahim Elshahed
@ 2026-11-20 10:58 ` Ibrahim Elshahed
2 siblings, 0 replies; 4+ messages in thread
From: Ibrahim Elshahed @ 2026-11-20 10:58 UTC (permalink / raw)
To: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak, Josh Don, Shuah Khan,
linux-kselftest, linux-kernel, nh-open-source, Fernand Sieber,
Roman Kagan
Add cs_forceidle_test, which checks that a core does not stay forced
idle after the task that a core-wide selection picked for an idle SMT
sibling is migrated away before the sibling schedules, and that the
pick left behind is not used once that task is freed.
On one SMT core, a task with its own cookie yields in a loop on one
sibling. Each attempt wakes a SCHED_FIFO task with another cookie on
the other sibling, so the core-wide selection picks it there and forces
the first sibling idle, and migrates it to a CPU of another core 0-50 us
later. The yielding task must make progress again within 20 ms; an
attempt where it does not counts as a stall, and the first test fails if
there is any.
The woken task also runs in its own cgroup. After a stall the test kills
it, removes its cgroup, waits 100 ms for RCU to free both, and runs a
short-lived task on the sibling. A kernel that kept the freed task in
the sibling's rq->core_pick then charges forced idle time to it and its
cgroup, and crashes in __cgroup_account_cputime_field() or reports a
use-after-free with KASAN. Forced idle time is only accounted with
schedstats, so the test turns them on for its duration if they are off.
This second test is on by default and is skipped with -n, without cgroup
v2, or without schedstats.
The test runs for 30 s by default, -t sets the duration. It is skipped
when not run as root, without an SMT core and two other online CPUs, or
when core scheduling is unavailable. Also list CONFIG_SCHED_CORE in the
config fragment, which cs_prctl_test needs as well.
Signed-off-by: Ibrahim Elshahed <lshahed@amazon.de>
---
tools/testing/selftests/sched/.gitignore | 1 +
tools/testing/selftests/sched/Makefile | 1 +
tools/testing/selftests/sched/config | 2 +-
.../selftests/sched/cs_forceidle_test.c | 471 ++++++++++++++++++
4 files changed, 474 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/sched/cs_forceidle_test.c
diff --git a/tools/testing/selftests/sched/.gitignore b/tools/testing/selftests/sched/.gitignore
index 6996d4654d92..8a698db51719 100644
--- a/tools/testing/selftests/sched/.gitignore
+++ b/tools/testing/selftests/sched/.gitignore
@@ -1 +1,2 @@
cs_prctl_test
+cs_forceidle_test
diff --git a/tools/testing/selftests/sched/Makefile b/tools/testing/selftests/sched/Makefile
index 099ee9213557..43e999fa1778 100644
--- a/tools/testing/selftests/sched/Makefile
+++ b/tools/testing/selftests/sched/Makefile
@@ -10,5 +10,6 @@ LDLIBS += -lpthread
TEST_GEN_FILES := cs_prctl_test
TEST_PROGS := cs_prctl_test
+TEST_GEN_PROGS := cs_forceidle_test
include ../lib.mk
diff --git a/tools/testing/selftests/sched/config b/tools/testing/selftests/sched/config
index 1bb8bf6d7fd4..cc3f25e8f01b 100644
--- a/tools/testing/selftests/sched/config
+++ b/tools/testing/selftests/sched/config
@@ -1 +1 @@
-# empty
+CONFIG_SCHED_CORE=y
diff --git a/tools/testing/selftests/sched/cs_forceidle_test.c b/tools/testing/selftests/sched/cs_forceidle_test.c
new file mode 100644
index 000000000000..ca89a4633a13
--- /dev/null
+++ b/tools/testing/selftests/sched/cs_forceidle_test.c
@@ -0,0 +1,471 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Check that core scheduling does not leave a core forced idle after the
+ * task a core-wide selection picked for an idle SMT sibling is migrated
+ * away before the sibling schedules, and that the pick it leaves behind is
+ * not used once that task is freed.
+ *
+ * On one SMT core with siblings A and B, a task U with its own cookie
+ * yields in a loop on A. Each attempt wakes a SCHED_FIFO task V with
+ * another cookie on B, so that the core-wide selection picks V for B and
+ * forces A idle, then migrates V to a CPU of another core 0-50 us later.
+ * U must make progress again within STALL_MS; an attempt where it does not
+ * is a stall.
+ *
+ * Unless -n is given, each V also runs in its own cgroup. After a stall,
+ * V is killed and its cgroup removed, and once RCU had time to free both,
+ * a short-lived task is run on B. A kernel that kept the migrated V in
+ * B's rq->core_pick then charges forced idle time to the freed task and
+ * cgroup, which crashes it in __cgroup_account_cputime_field() or makes
+ * KASAN report a use-after-free. Forced idle time is only accounted with
+ * schedstats, so the test turns them on for its duration if they are off.
+ * This part is skipped when cgroup v2 is not mounted or schedstats are
+ * unavailable.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <limits.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "../kselftest.h"
+
+#ifndef PR_SCHED_CORE
+#define PR_SCHED_CORE 62
+#endif
+#ifndef PR_SCHED_CORE_CREATE
+#define PR_SCHED_CORE_CREATE 1
+#endif
+#define PIDTYPE_PID 0
+
+#define STALL_MS 20
+#define FREE_WAIT_MS 100
+#define DEFAULT_SECS 30
+#define SCHEDSTATS "/proc/sys/kernel/sched_schedstats"
+#define CG_PATH_MAX (2 * PATH_MAX + 64)
+
+static uint64_t *progress;
+
+/* cgroup part: parent cgroup of the per-attempt cgroups, if in use */
+static char cg_parent[CG_PATH_MAX];
+static int cg_active;
+static int schedstats_restore = -1;
+
+static uint64_t now_ns(void)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+}
+
+static int pin(pid_t pid, int cpu)
+{
+ cpu_set_t set;
+
+ CPU_ZERO(&set);
+ CPU_SET(cpu, &set);
+ return sched_setaffinity(pid, sizeof(set), &set);
+}
+
+static int create_cookie(void)
+{
+ return prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, PIDTYPE_PID, 0);
+}
+
+static int write_str(const char *path, const char *val)
+{
+ ssize_t len = strlen(val);
+ int fd, ret;
+
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ return -1;
+ ret = write(fd, val, len) == len ? 0 : -1;
+ close(fd);
+ return ret;
+}
+
+/* Find an SMT core with two online siblings and two CPUs outside it. */
+static int find_cpus(int *a, int *b, int *helper, int *ctl)
+{
+ long ncpus = sysconf(_SC_NPROCESSORS_ONLN);
+ char path[128];
+ int cpu, s0, s1;
+ FILE *f;
+
+ *a = *b = *helper = *ctl = -1;
+ for (cpu = 0; cpu < ncpus && *a < 0; cpu++) {
+ snprintf(path, sizeof(path),
+ "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
+ cpu);
+ f = fopen(path, "r");
+ if (!f)
+ continue;
+ if (fscanf(f, "%d%*[,-]%d", &s0, &s1) == 2 && s0 != s1) {
+ *a = s0;
+ *b = s1;
+ }
+ fclose(f);
+ }
+ if (*a < 0)
+ return -1;
+
+ for (cpu = 0; cpu < ncpus; cpu++) {
+ if (cpu == *a || cpu == *b)
+ continue;
+ if (*helper < 0)
+ *helper = cpu;
+ else if (*ctl < 0)
+ *ctl = cpu;
+ }
+ return *ctl < 0 ? -1 : 0;
+}
+
+/* Probe in a child so this process keeps no cookie. */
+static int core_sched_available(void)
+{
+ int status;
+ pid_t pid;
+
+ pid = fork();
+ if (pid < 0)
+ return 0;
+ if (!pid)
+ _exit(create_cookie() ? 1 : 0);
+ if (waitpid(pid, &status, 0) != pid)
+ return 0;
+ return WIFEXITED(status) && !WEXITSTATUS(status);
+}
+
+static void cg_cleanup(void)
+{
+ if (cg_active)
+ rmdir(cg_parent);
+ if (schedstats_restore >= 0)
+ write_str(SCHEDSTATS, schedstats_restore ? "1" : "0");
+}
+
+/*
+ * Set up the cgroup part: find the cgroup v2 mount and this process's
+ * cgroup in it, create a parent for the per-attempt cgroups below it, and
+ * turn on schedstats. Returns NULL on success or why it is unavailable.
+ */
+static const char *cg_setup(void)
+{
+ char line[PATH_MAX], mnt[PATH_MAX] = "", own[PATH_MAX] = "";
+ char *sep;
+ FILE *f;
+ int val;
+
+ f = fopen("/proc/self/mountinfo", "r");
+ if (!f)
+ return "cannot read /proc/self/mountinfo";
+ while (fgets(line, sizeof(line), f)) {
+ sep = strstr(line, " - ");
+ if (sep && !strncmp(sep + 3, "cgroup2 ", 8) &&
+ sscanf(line, "%*s %*s %*s %*s %s", mnt) == 1)
+ break;
+ mnt[0] = '\0';
+ }
+ fclose(f);
+ if (!mnt[0])
+ return "cgroup v2 not mounted";
+
+ f = fopen("/proc/self/cgroup", "r");
+ if (!f)
+ return "cannot read /proc/self/cgroup";
+ while (fgets(line, sizeof(line), f)) {
+ if (!strncmp(line, "0::", 3)) {
+ line[strcspn(line, "\n")] = '\0';
+ snprintf(own, sizeof(own), "%s", line + 3);
+ break;
+ }
+ }
+ fclose(f);
+ if (!own[0])
+ return "not in a cgroup v2 cgroup";
+
+ f = fopen(SCHEDSTATS, "r");
+ if (!f)
+ return "schedstats unavailable (CONFIG_SCHEDSTATS=n)";
+ if (fscanf(f, "%d", &val) != 1)
+ val = -1;
+ fclose(f);
+ if (val < 0)
+ return "cannot read " SCHEDSTATS;
+
+ snprintf(cg_parent, sizeof(cg_parent), "%s%s/cs_forceidle_test.%d",
+ mnt, strcmp(own, "/") ? own : "", getpid());
+ if (mkdir(cg_parent, 0755))
+ return "cannot create a cgroup";
+ cg_active = 1;
+ atexit(cg_cleanup);
+
+ if (!val) {
+ if (write_str(SCHEDSTATS, "1")) {
+ rmdir(cg_parent);
+ cg_active = 0;
+ return "schedstats off and cannot be turned on";
+ }
+ schedstats_restore = 0;
+ }
+ return NULL;
+}
+
+static void run_u(int cpu)
+{
+ if (pin(0, cpu) || create_cookie())
+ _exit(1);
+ for (;;) {
+ sched_yield();
+ __atomic_fetch_add(progress, 1, __ATOMIC_RELAXED);
+ }
+}
+
+static void run_v(int cpu, int ready, int go)
+{
+ struct sched_param sp = { .sched_priority = 50 };
+ uint64_t t0;
+ char c;
+
+ if (create_cookie() || sched_setscheduler(0, SCHED_FIFO, &sp) ||
+ pin(0, cpu))
+ _exit(1);
+ if (write(ready, "r", 1) != 1 || read(go, &c, 1) != 1)
+ _exit(1);
+ /*
+ * Run briefly so the migration can catch V runnable, then block: a
+ * spinning FIFO task would force idle the CPUs it shares a core with.
+ */
+ t0 = now_ns();
+ while (now_ns() - t0 < 200000)
+ ;
+ for (;;)
+ pause();
+}
+
+/* Run a task on @cpu that exits right away, so that @cpu schedules. */
+static void poke(int cpu)
+{
+ pid_t pid;
+
+ pid = fork();
+ if (!pid)
+ _exit(pin(0, cpu) ? 1 : 0);
+ if (pid > 0)
+ waitpid(pid, NULL, 0);
+}
+
+static void cg_remove(const char *cg)
+{
+ int i;
+
+ /* The cgroup can stay busy until the killed task is fully released. */
+ for (i = 0; i < 1000 && rmdir(cg) && errno == EBUSY; i++)
+ usleep(1000);
+}
+
+/*
+ * One attempt; returns 1 on a stall, 0 if U made progress, -1 on error.
+ * Increments @freed when it frees a stalled V and its cgroup.
+ */
+static int attempt(int b, int helper, unsigned int *seed, unsigned long *freed)
+{
+ static unsigned long nr;
+ int ready[2], go[2], stall = 1;
+ uint64_t t0, spin, start;
+ char cg[CG_PATH_MAX + 32], path[CG_PATH_MAX + 64];
+ char pid[16];
+ pid_t v;
+ char c;
+
+ if (pipe(ready))
+ return -1;
+ if (pipe(go)) {
+ close(ready[0]);
+ close(ready[1]);
+ return -1;
+ }
+ if (cg_active) {
+ snprintf(cg, sizeof(cg), "%s/v%lu", cg_parent, nr++);
+ if (mkdir(cg, 0755)) {
+ close(ready[0]);
+ close(ready[1]);
+ close(go[0]);
+ close(go[1]);
+ return -1;
+ }
+ }
+
+ v = fork();
+ if (v < 0) {
+ stall = -1;
+ goto out_pipes;
+ }
+ if (!v) {
+ close(ready[0]);
+ close(go[1]);
+ run_v(b, ready[1], go[0]);
+ }
+ close(ready[1]);
+ close(go[0]);
+
+ if (cg_active) {
+ snprintf(path, sizeof(path), "%s/cgroup.procs", cg);
+ snprintf(pid, sizeof(pid), "%d", v);
+ if (write_str(path, pid)) {
+ stall = -1;
+ goto out;
+ }
+ }
+
+ if (read(ready[0], &c, 1) != 1) {
+ stall = -1;
+ goto out;
+ }
+ usleep(1000); /* let V block on the pipe */
+
+ spin = (rand_r(seed) % 51) * 1000ULL;
+ if (write(go[1], "w", 1) != 1) {
+ stall = -1;
+ goto out;
+ }
+ t0 = now_ns();
+ while (now_ns() - t0 < spin)
+ ;
+ pin(v, helper);
+
+ start = __atomic_load_n(progress, __ATOMIC_RELAXED);
+ t0 = now_ns();
+ while (now_ns() - t0 < STALL_MS * 1000000ULL) {
+ if (__atomic_load_n(progress, __ATOMIC_RELAXED) != start) {
+ stall = 0;
+ break;
+ }
+ }
+out:
+ kill(v, SIGKILL);
+ waitpid(v, NULL, 0);
+ close(ready[0]);
+ close(go[1]);
+ if (cg_active)
+ cg_remove(cg);
+ if (cg_active && stall == 1) {
+ /* Let RCU free V and its cgroup, then make B schedule. */
+ usleep(FREE_WAIT_MS * 1000);
+ poke(b);
+ (*freed)++;
+ }
+ return stall;
+
+out_pipes:
+ close(ready[0]);
+ close(ready[1]);
+ close(go[0]);
+ close(go[1]);
+ if (cg_active)
+ cg_remove(cg);
+ return stall;
+}
+
+int main(int argc, char **argv)
+{
+ unsigned long attempts = 0, stalls = 0, freed = 0;
+ int a, b, helper, ctl, opt, ret = 0;
+ const char *cg_skip = NULL;
+ int secs = DEFAULT_SECS;
+ unsigned int seed;
+ uint64_t end;
+ pid_t u;
+
+ while ((opt = getopt(argc, argv, "nt:")) != -1) {
+ switch (opt) {
+ case 'n':
+ cg_skip = "disabled with -n";
+ break;
+ case 't':
+ secs = atoi(optarg);
+ break;
+ default:
+ fprintf(stderr, "usage: %s [-n] [-t seconds]\n",
+ argv[0]);
+ return KSFT_FAIL;
+ }
+ }
+
+ ksft_print_header();
+ ksft_set_plan(2);
+
+ if (geteuid())
+ ksft_exit_skip("must be run as root\n");
+ if (find_cpus(&a, &b, &helper, &ctl))
+ ksft_exit_skip("needs an SMT core and two other online CPUs\n");
+ if (!core_sched_available())
+ ksft_exit_skip("core scheduling unavailable\n");
+
+ progress = mmap(NULL, sizeof(*progress), PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ if (progress == MAP_FAILED)
+ ksft_exit_fail_msg("mmap: %s\n", strerror(errno));
+
+ if (!cg_skip)
+ cg_skip = cg_setup();
+ if (cg_skip)
+ ksft_print_msg("freeing stalled tasks with their cgroups skipped: %s\n",
+ cg_skip);
+
+ if (pin(0, ctl))
+ ksft_exit_fail_msg("sched_setaffinity: %s\n", strerror(errno));
+
+ u = fork();
+ if (u < 0)
+ ksft_exit_fail_msg("fork: %s\n", strerror(errno));
+ if (!u)
+ run_u(a);
+ usleep(100000);
+
+ ksft_print_msg("core %d,%d, migrate to %d, %d s%s\n", a, b, helper,
+ secs, cg_active ? ", one cgroup per woken task" : "");
+ seed = getpid();
+ end = now_ns() + (uint64_t)secs * 1000000000ULL;
+ while (now_ns() < end) {
+ ret = attempt(b, helper, &seed, &freed);
+ if (ret < 0)
+ break;
+ attempts++;
+ stalls += ret;
+ }
+
+ kill(u, SIGKILL);
+ waitpid(u, NULL, 0);
+
+ ksft_print_msg("attempts=%lu stalls=%lu (no progress for %d ms)\n",
+ attempts, stalls, STALL_MS);
+ if (ret < 0)
+ ksft_test_result_fail("attempt failed: %s\n", strerror(errno));
+ else
+ ksft_test_result(!stalls, "no stuck forced idle\n");
+
+ if (cg_skip)
+ ksft_test_result_skip("free a stalled task and its cgroup: %s\n",
+ cg_skip);
+ else if (ret < 0)
+ ksft_test_result_fail("free a stalled task and its cgroup: attempt failed\n");
+ else
+ ksft_test_result_pass("free a stalled task and its cgroup (%lu freed)\n",
+ freed);
+ ksft_finished();
+}
--
2.47.3
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-20 10:58 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 10:58 [PATCH v2 0/3] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path Ibrahim Elshahed
2026-11-20 10:58 ` [PATCH v2 1/3] sched/core: Clear a stale rq->core_pick after dequeue Ibrahim Elshahed
2026-11-20 10:58 ` [PATCH v2 2/3] sched/core: Don't take the idle fast path while the core is forced idle Ibrahim Elshahed
2026-11-20 10:58 ` [PATCH v2 3/3] selftests/sched: Add a test for stuck forced idle under core scheduling Ibrahim Elshahed
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.