* [PATCH 0/2] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path
@ 2026-11-17 14:27 Ibrahim Elshahed
2026-11-17 14:27 ` [PATCH 1/2] 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-17 14:27 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, 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 consuming the 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 core scheduling
still has a pick or forced idle to settle.
Testing, on tip sched/core d7e670601af6, schedstats=enable:
- KVM guest, 2 cores x 2 threads, idle=halt. A test init wakes a
SCHED_FIFO task with its own cookie and cgroup on CPU1 while a
cookied task yields on CPU0, migrates it to CPU2 0-50 us later, and
checks that CPU0 is stuck forced idle. It then kills the task,
removes its cgroup, waits 100 ms and wakes a task on CPU1.
Without the series all 6 boots hit:
BUG: unable to handle page fault for address: ffffa3511fcbc018
#PF: supervisor write access in kernel mode
#PF: error_code(0x0002) - not-present page
RIP: 0010:__cgroup_account_cputime_field+0x51/0x70
Call Trace:
__sched_core_account_forceidle+0x14a/0x180
__schedule+0x105d/0x19a0
schedule_idle+0x1e/0x40
cpu_startup_entry+0x24/0x30
start_secondary+0xf3/0x100
With the series, 6 boots of 240 s (about 1M attempts) showed no
stuck forced idle and no oops.
- Bare metal, Intel Xeon Platinum 8375C, 128 CPUs, 2 threads per core.
The same churn on 32 SMT pairs for 300 s, counting a stall when the
yielding task makes no progress for 5 ms: 41 stalls in 123036
attempts without the series, 0 in 109745 with it. dmesg clean on
both, forced idle counters still increasing.
- 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/.
- Each patch builds on its own (kernel/sched/ with SCHED_CORE on and
off, W=1).
I will reply with the test programs.
Ibrahim Elshahed (2):
sched/core: Clear a stale rq->core_pick after dequeue
sched/core: Don't skip a pending core-wide pick on idle re-entry
kernel/sched/core.c | 30 ++++++++++++++++++++++++++++--
1 file changed, 28 insertions(+), 2 deletions(-)
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] sched/core: Clear a stale rq->core_pick after dequeue
2026-11-17 14:27 [PATCH 0/2] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path Ibrahim Elshahed
@ 2026-11-17 14:27 ` Ibrahim Elshahed
2026-11-17 14:27 ` [PATCH 2/2] sched/core: Don't skip a pending core-wide pick on idle re-entry Ibrahim Elshahed
2026-11-17 14:27 ` [PATCH 0/2] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path Ibrahim Elshahed
2 siblings, 0 replies; 4+ messages in thread
From: Ibrahim Elshahed @ 2026-11-17 14:27 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, 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 175b52a7ea..b1718ba5a4 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 [flat|nested] 4+ messages in thread
* [PATCH 2/2] sched/core: Don't skip a pending core-wide pick on idle re-entry
2026-11-17 14:27 [PATCH 0/2] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path Ibrahim Elshahed
2026-11-17 14:27 ` [PATCH 1/2] sched/core: Clear a stale rq->core_pick after dequeue Ibrahim Elshahed
@ 2026-11-17 14:27 ` Ibrahim Elshahed
2026-11-17 14:27 ` [PATCH 0/2] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path Ibrahim Elshahed
2 siblings, 0 replies; 4+ messages in thread
From: Ibrahim Elshahed @ 2026-11-17 14:27 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, 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 consumes the pick a sibling's core-wide
selection left in rq->core_pick and 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 without consuming its pick. 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 core scheduling has neither a
pick pending for this CPU nor forced idle to settle.
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 | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index b1718ba5a4..58b429bbe3 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -347,6 +347,17 @@ 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
+ * it has a pending core-wide pick or the core is forced idle. Only that pick
+ * consumes rq->core_pick and ends the forced idle.
+ */
+static inline bool sched_core_idle_needs_pick(struct rq *rq)
+{
+ return sched_core_enabled(rq) &&
+ (rq->core_pick || 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 +561,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 +7297,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 consume
+ * rq->core_pick or 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 [flat|nested] 4+ messages in thread
* Re: [PATCH 0/2] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path
2026-11-17 14:27 [PATCH 0/2] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path Ibrahim Elshahed
2026-11-17 14:27 ` [PATCH 1/2] sched/core: Clear a stale rq->core_pick after dequeue Ibrahim Elshahed
2026-11-17 14:27 ` [PATCH 2/2] sched/core: Don't skip a pending core-wide pick on idle re-entry Ibrahim Elshahed
@ 2026-11-17 14:27 ` Ibrahim Elshahed
2 siblings, 0 replies; 4+ messages in thread
From: Ibrahim Elshahed @ 2026-11-17 14:27 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, linux-kernel,
nh-open-source, Fernand Sieber, Roman Kagan
Here are the two test programs used for the numbers in the cover letter.
1. init.c runs as PID 1 in a KVM guest with one socket, two cores and
two threads (CPUs 0 and 1 are one core). A cookied task yields on
CPU0. Each attempt wakes a SCHED_FIFO task with another cookie, in
its own cgroup, on CPU1 and migrates it to CPU2 0-50 us later. If
the yielding task then makes no progress for 20 ms, the core is
stuck forced idle ("STALE"): the program kills the task, removes its
cgroup, waits 100 ms without touching CPUs 0 and 1, and wakes a task
on CPU1. Without the series that reschedule oopses in
__cgroup_account_cputime_field(). mode=nomigrate is a control that
never migrates the woken task.
Build and run:
gcc -O2 -static -o init init.c
mkdir -p root/dev root/proc root/sys/fs/cgroup && cp init root/
(cd root && find . | cpio -o -H newc | gzip) > initramfs.cpio.gz
qemu-system-x86_64 -enable-kvm -cpu host -m 2G -nographic -no-reboot \
-smp 4,sockets=1,cores=2,threads=2 \
-kernel bzImage -initrd initramfs.cpio.gz \
-append "schedstats=enable idle=halt console=ttyS0 panic=-1 mode=migrate secs=240"
The kernel needs CONFIG_SCHED_CORE, CONFIG_SCHEDSTATS, CONFIG_SCHED_SMT,
CONFIG_CGROUP_SCHED and CONFIG_FAIR_GROUP_SCHED.
2. cschurn.c runs the same churn on bare metal on half of the SMT cores,
using the other half as migration targets, and counts a stall when
the yielding task makes no progress for 5 ms. It frees each woken
task and its cgroup shortly after the check (2 ms by default, second
argument), so it shows the stuck forced idle but not the
use-after-free.
gcc -O2 -o cschurn cschurn.c
./cschurn 300 # as root, cgroup2 at /sys/fs/cgroup,
# kernel booted with schedstats=enable
---8<--- init.c ---8<---
// PID 1 for a 1-socket/2-core/2-thread KVM guest (CPUs 0,1 = one core).
// Reproducer for the core-sched stale rq->core_pick use-after-free:
// U (cookie A, SCHED_OTHER, CPU0) yields in a loop; each attempt wakes a
// SCHED_FIFO victim V (cookie B, own cgroup, CPU1), spins 0..50us, then
// migrates V to CPU2. If U stalls 20ms -> STALE: free V and its cgroup
// without touching CPUs 0/1, wait 100ms, then poke CPU1.
// Kernel cmdline: mode=migrate|nomigrate secs=N
#define _GNU_SOURCE
#include <errno.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/mount.h>
#include <sys/prctl.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#ifndef PR_SCHED_CORE
#define PR_SCHED_CORE 62
#endif
#define PR_SCHED_CORE_CREATE 1
static volatile uint64_t *ctr;
static uint64_t now_ns(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1000000000ull + t.tv_nsec; }
static void pin(pid_t p, int cpu) { cpu_set_t s; CPU_ZERO(&s); CPU_SET(cpu, &s); if (sched_setaffinity(p, sizeof s, &s)) printf("setaffinity(%d,%d): %s\n", p, cpu, strerror(errno)); }
static void cookie(void) { if (prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, 0 /*PIDTYPE_PID*/, 0)) printf("prctl cookie: %s\n", strerror(errno)); }
static void wr(const char *path, const char *s) { int fd = open(path, O_WRONLY); if (fd < 0 || write(fd, s, strlen(s)) < 0) printf("write %s: %s\n", path, strerror(errno)); if (fd >= 0) close(fd); }
static void die(void) { fflush(stdout); sync(); reboot(RB_POWER_OFF); for (;;) pause(); }
int main(void)
{
mount("devtmpfs", "/dev", "devtmpfs", 0, 0);
{ int c = open("/dev/console", O_RDWR); if (c >= 0) { dup2(c, 0); dup2(c, 1); dup2(c, 2); if (c > 2) close(c); } }
mount("proc", "/proc", "proc", 0, 0);
mount("sysfs", "/sys", "sysfs", 0, 0);
mount("cgroup2", "/sys/fs/cgroup", "cgroup2", 0, 0);
setvbuf(stdout, 0, _IONBF, 0);
const char *mode = getenv("mode") ? getenv("mode") : "migrate";
int secs = getenv("secs") ? atoi(getenv("secs")) : 180;
int migrate = strcmp(mode, "nomigrate") != 0;
printf("init: %ld CPUs, mode=%s secs=%d\n", sysconf(_SC_NPROCESSORS_ONLN), mode, secs);
char buf[256]; int fd = open("/sys/devices/system/cpu/cpu0/topology/thread_siblings_list", O_RDONLY);
if (fd >= 0) { int n = read(fd, buf, 255); buf[n > 0 ? n : 0] = 0; printf("init: cpu0 siblings %s", buf); close(fd); }
ctr = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
pid_t u = fork();
if (!u) { pin(0, 0); cookie(); for (;;) { sched_yield(); (*ctr)++; } }
pin(0, 3);
srand(time(0) ^ getpid());
usleep(100000);
uint64_t end = now_ns() + (uint64_t)secs * 1000000000ull;
int stale = 0, attempt;
for (attempt = 0; now_ns() < end; attempt++) {
char cg[64]; snprintf(cg, sizeof cg, "/sys/fs/cgroup/v%d", attempt);
if (mkdir(cg, 0755)) { printf("mkdir %s: %s\n", cg, strerror(errno)); break; }
int p[2], r[2]; pipe(p); pipe(r);
pid_t v = fork();
if (!v) {
char c; close(p[1]); close(r[0]);
cookie();
struct sched_param sp = { .sched_priority = 50 };
if (sched_setscheduler(0, SCHED_FIFO, &sp)) printf("fifo: %s\n", strerror(errno));
pin(0, 1);
write(r[1], "r", 1);
read(p[0], &c, 1);
/* run briefly, then block for good: a spinning cookie-B FIFO task on
* CPU2 would force-idle the parent on its sibling CPU3 */
struct timespec a, b; clock_gettime(CLOCK_MONOTONIC, &a);
do clock_gettime(CLOCK_MONOTONIC, &b); while ((b.tv_sec - a.tv_sec) * 1000000000l + b.tv_nsec - a.tv_nsec < 200000);
for (;;) pause();
}
close(p[0]); close(r[1]);
char procs[96], pidb[16], c; snprintf(procs, sizeof procs, "%s/cgroup.procs", cg);
snprintf(pidb, sizeof pidb, "%d", v); wr(procs, pidb);
read(r[0], &c, 1);
usleep(1000); /* let V block on the pipe */
write(p[1], "w", 1);
uint64_t t0 = now_ns(), spin = (rand() % 51) * 1000ull;
while (now_ns() - t0 < spin) ;
if (migrate) pin(v, 2);
/* nomigrate: V legitimately owns the core while it runs on CPU1, so the
* control kills V in place (still on CPU1) and then checks U resumes */
if (!migrate) { kill(v, SIGKILL); waitpid(v, 0, 0); }
/* oracle: U must make progress within 20 ms */
uint64_t c0 = *ctr, t1 = now_ns(); int st = 1;
while (now_ns() - t1 < 20000000ull) if (*ctr != c0) { st = 0; break; }
if (migrate) { kill(v, SIGKILL); waitpid(v, 0, 0); }
for (int i = 0; i < 1000 && rmdir(cg); i++) usleep(1000);
close(p[1]); close(r[0]);
if (st) {
stale++;
printf("STALE attempt=%d spin=%luus\n", attempt, (unsigned long)(spin / 1000));
usleep(100000); /* RCU frees task_struct and css */
pid_t k = fork();
if (!k) { pin(0, 1); _exit(0); }
waitpid(k, 0, 0);
uint64_t c1 = *ctr; usleep(20000);
printf("POKE attempt=%d done, U %s\n", attempt, *ctr != c1 ? "progressing" : "STILL STUCK");
}
if (attempt % 1000 == 0) printf("init: attempt %d, stale %d, U ctr %llu\n", attempt, stale, (unsigned long long)*ctr);
}
printf("init: DONE attempts=%d stale=%d mode=%s\n", attempt, stale, mode);
die();
}
---8<--- cschurn.c ---8<---
// Parallel core-scheduling churn for SMT hosts.
//
// For each test core (siblings A, B): a cookied task U on A that yields in a
// loop, and a controller pinned on a CPU of a different "helper" core. Each
// attempt the controller forks a victim V in its own cgroup with a different
// cookie, SCHED_FIFO, pinned to B and blocked on a pipe; it wakes V, spins
// 0-50 us, moves V to the helper CPU, then checks that U makes progress for
// STALL_US. Then V is killed, reaped, its cgroup removed, and a short task
// is woken on B so the core schedules again after the free.
//
// Usage: cschurn <seconds>
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.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>
#ifndef PR_SCHED_CORE
#define PR_SCHED_CORE 62
#define PR_SCHED_CORE_CREATE 1
#endif
#define PIDTYPE_TGID 1
#define STALL_US 5000
#define MAXCORES 512
struct slot {
volatile uint64_t u_count;
volatile uint64_t attempts, stalls;
};
static int sib[MAXCORES][2], ncores;
static uint64_t now_ns(void)
{
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec * 1000000000ull + t.tv_nsec;
}
static void pin_pid(pid_t pid, int cpu)
{
cpu_set_t s;
CPU_ZERO(&s);
CPU_SET(cpu, &s);
sched_setaffinity(pid, sizeof(s), &s);
}
static void wr(const char *path, const char *val)
{
int fd = open(path, O_WRONLY);
if (fd >= 0) {
if (write(fd, val, strlen(val)) < 0)
;
close(fd);
}
}
/* Collect (first, second) SMT siblings per core from sysfs. */
static void topo(void)
{
char p[256];
int cpu, seen[4096] = { 0 };
long n = sysconf(_SC_NPROCESSORS_ONLN);
for (cpu = 0; cpu < n && ncores < MAXCORES; cpu++) {
int a, b;
FILE *f;
if (seen[cpu])
continue;
snprintf(p, sizeof(p),
"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
f = fopen(p, "r");
if (!f)
continue;
if (fscanf(f, "%d%*[,-]%d", &a, &b) == 2) {
sib[ncores][0] = a;
sib[ncores][1] = b;
seen[a] = seen[b] = 1;
ncores++;
}
fclose(f);
}
}
static int postfree_us = 2000;
static void controller(int id, int a, int b, int helper, struct slot *sl,
uint64_t end)
{
char cg[128], buf[32];
unsigned seed = id * 7919 + 1;
int k = 0;
pin_pid(0, helper);
while (now_ns() < end) {
int p[2];
pid_t v;
uint64_t c0, t0, d;
snprintf(cg, sizeof(cg), "/sys/fs/cgroup/cs/c%d_%d", id, k++);
mkdir(cg, 0755);
if (pipe(p))
break;
v = fork();
if (v == 0) {
struct sched_param sp = { .sched_priority = 10 };
char procs[160];
snprintf(procs, sizeof(procs), "%s/cgroup.procs", cg);
snprintf(buf, sizeof(buf), "%d", getpid());
wr(procs, buf);
prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, PIDTYPE_TGID, 0);
sched_setscheduler(0, SCHED_FIFO, &sp);
pin_pid(0, b);
close(p[1]);
if (read(p[0], buf, 1) < 0)
;
for (;;)
;
}
close(p[0]);
usleep(500); /* V blocks on B */
d = rand_r(&seed) % 50000;
if (write(p[1], "x", 1) < 0)
;
t0 = now_ns();
while (now_ns() - t0 < d)
;
pin_pid(v, helper); /* migrate V off B */
c0 = sl->u_count;
usleep(STALL_US);
sl->attempts++;
if (sl->u_count == c0)
sl->stalls++;
kill(v, SIGKILL);
waitpid(v, NULL, 0);
close(p[1]);
while (rmdir(cg) && errno == EBUSY)
usleep(200);
usleep(postfree_us); /* let task and cgroup be freed */
if (fork() == 0) { /* make B schedule after the free */
pin_pid(0, b);
_exit(0);
}
wait(NULL);
}
_exit(0);
}
int main(int argc, char **argv)
{
int secs = argc > 1 ? atoi(argv[1]) : 180, i, ntest;
if (argc > 2)
postfree_us = atoi(argv[2]);
struct slot *sl;
uint64_t end, attempts = 0, stalls = 0;
pid_t *upid;
topo();
if (ncores < 2) {
fprintf(stderr, "no SMT topology\n");
return 1;
}
ntest = ncores / 2; /* first half under test, second half helpers */
sl = mmap(NULL, sizeof(*sl) * ntest, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
upid = calloc(ntest, sizeof(*upid));
mkdir("/sys/fs/cgroup/cs", 0755);
mkdir("/sys/fs/cgroup/cs/u", 0755);
for (i = 0; i < ntest; i++) {
pid_t u = fork();
if (u == 0) {
char b[32];
snprintf(b, sizeof(b), "%d", getpid());
wr("/sys/fs/cgroup/cs/u/cgroup.procs", b);
pin_pid(0, sib[i][0]);
prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, PIDTYPE_TGID, 0);
for (;;) {
sl[i].u_count++;
sched_yield();
}
}
upid[i] = u;
}
usleep(200000);
end = now_ns() + (uint64_t)secs * 1000000000ull;
printf("cschurn: %d SMT cores, %d under test, %d s, postfree %d us\n", ncores, ntest, secs, postfree_us);
fflush(stdout);
for (i = 0; i < ntest; i++)
if (fork() == 0)
controller(i, sib[i][0], sib[i][1], sib[ntest + i][0],
&sl[i], end);
for (i = 0; i < ntest; i++)
wait(NULL);
for (i = 0; i < ntest; i++) {
attempts += sl[i].attempts;
stalls += sl[i].stalls;
}
printf("cschurn: attempts=%llu stalls=%llu (U made no progress for %d us)\n",
(unsigned long long)attempts, (unsigned long long)stalls, STALL_US);
{
char p[64], line[256];
FILE *f;
snprintf(p, sizeof(p), "/proc/%d/sched", upid[0]);
f = fopen(p, "r");
while (f && fgets(line, sizeof(line), f))
if (strstr(line, "forceidle"))
printf("U[0] %s", line);
if (f)
fclose(f);
f = fopen("/sys/fs/cgroup/cs/cpu.stat", "r");
while (f && fgets(line, sizeof(line), f))
if (strstr(line, "force_idle"))
printf("cgroup cs %s", line);
if (f)
fclose(f);
}
for (i = 0; i < ntest; i++)
kill(upid[i], SIGKILL);
while (wait(NULL) > 0)
;
rmdir("/sys/fs/cgroup/cs/u");
rmdir("/sys/fs/cgroup/cs");
return 0;
}
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-17 14:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 14:27 [PATCH 0/2] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path Ibrahim Elshahed
2026-11-17 14:27 ` [PATCH 1/2] sched/core: Clear a stale rq->core_pick after dequeue Ibrahim Elshahed
2026-11-17 14:27 ` [PATCH 2/2] sched/core: Don't skip a pending core-wide pick on idle re-entry Ibrahim Elshahed
2026-11-17 14:27 ` [PATCH 0/2] sched/core: Fix stale core_pick and forced idle after SM_IDLE fast path Ibrahim Elshahed
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®