The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH 0/2] sched/core: Don't count voluntary idle picks as forced idle
@ 2026-12-01 12:03 Kuba Piecuch
  2026-12-01 12:03 ` [PATCH 1/2] " Kuba Piecuch
  2026-12-01 12:03 ` [PATCH 2/2] selftests/sched_ext: Add core_forceidle test for voluntary idle picks Kuba Piecuch
  0 siblings, 2 replies; 4+ messages in thread
From: Kuba Piecuch @ 2026-12-01 12:03 UTC (permalink / raw)
  To: Peter Zijlstra, Tejun Heo
  Cc: Kuba Piecuch, Ingo Molnar, Juri Lelli, Vincent Guittot, Josh Don,
	David Vernet, Andrea Righi, Changwoo Min, sched-ext, linux-kernel

In pick_next_task(), core scheduling increments core_forceidle_count
whenever a sibling picks idle with rq_i->nr_running > 0. That mistakes a
voluntary idle pick -- such as a sched_ext task parked on a non-local DSQ
while pick_task() returns NULL -- for cookie-mismatch forced idle. When all
siblings voluntarily pick idle, core_forceidle_count > 0 with
core_forceidle_occupation == 0 triggers the warning in
__sched_core_account_forceidle(); when another sibling is busy, voluntary
idle is billed as forced idle and triggers forced-idle-only paths
(queue_core_balance(), forced_idle_rq(), sched_core_idle_needs_pick()).

Patch 1 moves the core_forceidle_count and core_forceidle_seq increments
into the !cookie_equals(p, cookie) fallback when sched_core_find() returns
NULL.

Patch 2 adds a core_forceidle sched_ext selftest covering voluntary idle
with an idle sibling, voluntary idle with an untagged busy sibling, and a
sanity check that genuine cookie-mismatch forced idle is still counted.
Without patch 1, the selftest triggers:

  WARNING: kernel/sched/core_sched.c:262 at __sched_core_account_forceidle+0x23e/0x260, CPU#0: swapper/0/0

and fails with:

  ERR: core_forceidle.c:449
  Phase 1: voluntary idle counted as forced idle (obs=1 fi=1 zero_occ=1)

This series is based on tip/sched/urgent because
commit d2583cb03113 ("sched/deadline: Clear stale rq->dl_server when
core-sched replaces a pick") modifies the same loop in pick_next_task().
Patch 2 touches tools/testing/selftests/sched_ext/ only; please route
through whichever tree is more convenient (or take patch 1 via tip and
patch 2 via sched_ext once patch 1 lands).

The patches were written with the help of an AI coding assistant. I have
reviewed the patches and verified the tests run by the tool.

Kuba Piecuch (2):
  sched/core: Don't count voluntary idle picks as forced idle
  selftests/sched_ext: Add core_forceidle test for voluntary idle picks

 kernel/sched/core.c                           |  15 +-
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/core_forceidle.bpf.c  | 176 +++++++
 .../selftests/sched_ext/core_forceidle.c      | 490 ++++++++++++++++++
 4 files changed, 673 insertions(+), 9 deletions(-)
 create mode 100644 tools/testing/selftests/sched_ext/core_forceidle.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/core_forceidle.c


base-commit: f4eb2ace3efdd8b6e3b0946bbd316166edb1a51e
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] sched/core: Don't count voluntary idle picks as forced idle
  2026-12-01 12:03 [PATCH 0/2] sched/core: Don't count voluntary idle picks as forced idle Kuba Piecuch
@ 2026-12-01 12:03 ` Kuba Piecuch
  2026-12-01 15:11   ` Tejun Heo
  2026-12-01 12:03 ` [PATCH 2/2] selftests/sched_ext: Add core_forceidle test for voluntary idle picks Kuba Piecuch
  1 sibling, 1 reply; 4+ messages in thread
From: Kuba Piecuch @ 2026-12-01 12:03 UTC (permalink / raw)
  To: Peter Zijlstra, Tejun Heo
  Cc: Kuba Piecuch, Ingo Molnar, Juri Lelli, Vincent Guittot, Josh Don,
	David Vernet, Andrea Righi, Changwoo Min, sched-ext, linux-kernel

In pick_next_task(), the per-sibling loop increments
rq->core->core_forceidle_count whenever p == rq_i->idle and
rq_i->nr_running > 0. That conflates a sibling forced idle by a cookie
mismatch with a sibling whose pick_task() voluntarily returned idle while
rq_i->nr_running > 0.

Voluntary idle with nr_running > 0 is legitimate whenever a scheduling
class keeps queued tasks off the CPU and returns NULL from pick_task(),
such as sched_ext tasks parked on a non-local DSQ. Counting those
voluntary idle picks as forced idle has two effects:

- If all siblings pick idle (occ == 0), core_forceidle_count becomes
  non-zero while core_forceidle_occupation is 0, triggering
  WARN_ON_ONCE(!rq->core->core_forceidle_occupation) in
  __sched_core_account_forceidle() when schedstats is enabled.
- If another sibling is running (occ > 0), the voluntary idle time is
  spuriously billed as forced idle to the running sibling's cgroup and
  triggers forced-idle-only paths (queue_core_balance(), forced_idle_rq(),
  sched_core_idle_needs_pick()).

Increment core_forceidle_count and core_forceidle_seq only inside the
!cookie_equals(p, cookie) branch when sched_core_find() returns NULL and
replaces a non-idle pick with idle. Since cookie_equals() always returns
true for rq_i->idle, voluntary idle picks are never counted as forced
idle, and entering the mismatch branch guarantees a non-idle @max so
core_forceidle_occupation is always non-zero whenever forced idle is
recorded.

Fixes: 4feee7d12603 ("sched/core: Forced idle accounting")
Cc: stable@vger.kernel.org
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
 kernel/sched/core.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index c9890ed35df7..8cd30021721f 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -6470,8 +6470,12 @@ pick_next_task(struct rq *rq, struct rq_flags *rf)
 			p = NULL;
 			if (cookie)
 				p = sched_core_find(rq_i, cookie);
-			if (!p)
+			if (!p) {
 				p = idle_sched_class.pick_task(rq_i, rf);
+				rq->core->core_forceidle_count++;
+				if (!fi_before)
+					rq->core->core_forceidle_seq++;
+			}
 
 			/*
 			 * Neither a core tree pick nor idle comes through a
@@ -6482,15 +6486,8 @@ pick_next_task(struct rq *rq, struct rq_flags *rf)
 
 		rq_i->core_pick = p;
 
-		if (p == rq_i->idle) {
-			if (rq_i->nr_running) {
-				rq->core->core_forceidle_count++;
-				if (!fi_before)
-					rq->core->core_forceidle_seq++;
-			}
-		} else {
+		if (p != rq_i->idle)
 			occ++;
-		}
 	}
 
 	if (schedstat_enabled() && rq->core->core_forceidle_count) {
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] selftests/sched_ext: Add core_forceidle test for voluntary idle picks
  2026-12-01 12:03 [PATCH 0/2] sched/core: Don't count voluntary idle picks as forced idle Kuba Piecuch
  2026-12-01 12:03 ` [PATCH 1/2] " Kuba Piecuch
@ 2026-12-01 12:03 ` Kuba Piecuch
  1 sibling, 0 replies; 4+ messages in thread
From: Kuba Piecuch @ 2026-12-01 12:03 UTC (permalink / raw)
  To: Peter Zijlstra, Tejun Heo
  Cc: Kuba Piecuch, Ingo Molnar, Juri Lelli, Vincent Guittot, Josh Don,
	David Vernet, Andrea Righi, Changwoo Min, sched-ext, linux-kernel

Add a core_forceidle selftest that exercises core-scheduling forced-idle
accounting when a sched_ext task with a core-scheduling cookie is parked
on a user DSQ so its CPU voluntarily picks idle with nr_running > 0:

- Phase 1: sibling is also idle (occ == 0), verifying core_forceidle_count
  stays 0 and __sched_core_account_forceidle() does not warn.
- Phase 2: sibling runs an untagged busy task (occ == 1), verifying the
  voluntary idle sibling is not counted as forced idle.
- Phase 3: unpark the cookied task against the untagged sibling, verifying
  genuine cookie-mismatch forced idle is still counted.

Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/core_forceidle.bpf.c  | 176 +++++++
 .../selftests/sched_ext/core_forceidle.c      | 490 ++++++++++++++++++
 3 files changed, 667 insertions(+)
 create mode 100644 tools/testing/selftests/sched_ext/core_forceidle.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/core_forceidle.c

diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile
index c5d3a2eaea7d..af1182e9b074 100644
--- a/tools/testing/selftests/sched_ext/Makefile
+++ b/tools/testing/selftests/sched_ext/Makefile
@@ -165,6 +165,7 @@ all_test_bpfprogs := $(foreach prog,$(wildcard *.bpf.c),$(INCLUDE_DIR)/$(patsubs
 
 auto-test-targets :=			\
 	cgroup_nr_cpus			\
+	core_forceidle			\
 	create_dsq			\
 	dequeue				\
 	dequeue_iter			\
diff --git a/tools/testing/selftests/sched_ext/core_forceidle.bpf.c b/tools/testing/selftests/sched_ext/core_forceidle.bpf.c
new file mode 100644
index 000000000000..12c8be46cc12
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/core_forceidle.bpf.c
@@ -0,0 +1,176 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2026 Google LLC.
+ */
+#include <scx/common.bpf.h>
+
+char _license[] SEC("license") = "GPL";
+
+enum {
+	PARK_DSQ = 0,
+};
+
+struct rq___core {
+	struct rq___core *core;
+	unsigned int core_forceidle_count;
+	unsigned int core_forceidle_occupation;
+} __attribute__((preserve_access_index));
+
+s32 sib0;
+s32 sib1;
+
+s32 parked_pid;
+s32 busy_pid;
+
+u32 phase;
+u32 parked_in_dsq;
+u32 busy_running;
+
+u32 phase1_obs;
+u32 phase1_spurious_fi;
+u32 phase1_zero_occ;
+
+u32 phase2_obs;
+u32 phase2_spurious_fi;
+
+u32 phase3_real_fi;
+
+UEI_DEFINE(uei);
+
+static void sample_core_fi(s32 cpu, u32 *fi_cnt, u32 *fi_occ)
+{
+	struct rq___core *rq = NULL;
+
+	*fi_cnt = 0;
+	*fi_occ = 0;
+
+	if (!bpf_core_field_exists(rq->core))
+		return;
+
+	rq = (struct rq___core *)bpf_per_cpu_ptr(&runqueues, cpu);
+	if (!rq)
+		return;
+
+	*fi_cnt = BPF_CORE_READ(rq, core, core_forceidle_count);
+	*fi_occ = BPF_CORE_READ(rq, core, core_forceidle_occupation);
+}
+
+s32 BPF_STRUCT_OPS(core_forceidle_select_cpu, struct task_struct *p,
+		   s32 prev_cpu, u64 wake_flags)
+{
+	if (p->pid == parked_pid)
+		return sib0;
+	if (p->pid == busy_pid)
+		return sib1;
+
+	return prev_cpu;
+}
+
+void BPF_STRUCT_OPS(core_forceidle_enqueue, struct task_struct *p, u64 enq_flags)
+{
+	u32 ph = phase;
+
+	if (p->pid == parked_pid) {
+		if ((ph == 1 || ph == 2) && (enq_flags & SCX_ENQ_LAST)) {
+			scx_bpf_dsq_insert(p, PARK_DSQ, 1000000ULL, enq_flags);
+			parked_in_dsq = 1;
+			return;
+		}
+		scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, 1000000ULL, enq_flags);
+		return;
+	}
+
+	if (p->pid == busy_pid) {
+		scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, SCX_SLICE_DFL, enq_flags);
+		return;
+	}
+
+	scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);
+}
+
+void BPF_STRUCT_OPS(core_forceidle_dispatch, s32 cpu, struct task_struct *prev)
+{
+	if (cpu == sib0 && phase != 1 && phase != 2)
+		scx_bpf_dsq_move_to_local(PARK_DSQ, 0);
+}
+
+void BPF_STRUCT_OPS(core_forceidle_running, struct task_struct *p)
+{
+	if (p->pid == parked_pid)
+		parked_in_dsq = 0;
+	else if (p->pid == busy_pid)
+		busy_running = 1;
+}
+
+void BPF_STRUCT_OPS(core_forceidle_stopping, struct task_struct *p, bool runnable)
+{
+	if (p->pid == busy_pid)
+		busy_running = 0;
+}
+
+void BPF_STRUCT_OPS(core_forceidle_update_idle, s32 cpu, bool idle)
+{
+	u32 fi_cnt, fi_occ;
+	u32 ph = phase;
+
+	if (!idle)
+		return;
+
+	if (cpu == sib0 && ph == 1 && parked_in_dsq) {
+		sample_core_fi(cpu, &fi_cnt, &fi_occ);
+		__sync_fetch_and_add(&phase1_obs, 1);
+		if (fi_cnt) {
+			__sync_fetch_and_add(&phase1_spurious_fi, 1);
+			if (!fi_occ)
+				__sync_fetch_and_add(&phase1_zero_occ, 1);
+		}
+	} else if ((cpu == sib0 || cpu == sib1) && ph == 3) {
+		sample_core_fi(cpu, &fi_cnt, &fi_occ);
+		if (fi_cnt)
+			__sync_fetch_and_add(&phase3_real_fi, 1);
+	}
+}
+
+void BPF_STRUCT_OPS(core_forceidle_tick, struct task_struct *p)
+{
+	u32 fi_cnt, fi_occ;
+	u32 ph = phase;
+
+	if (ph == 2 && p->pid == busy_pid && parked_in_dsq) {
+		sample_core_fi(sib1, &fi_cnt, &fi_occ);
+		__sync_fetch_and_add(&phase2_obs, 1);
+		if (fi_cnt)
+			__sync_fetch_and_add(&phase2_spurious_fi, 1);
+	} else if (ph == 3 && (p->pid == parked_pid || p->pid == busy_pid)) {
+		if (parked_in_dsq)
+			scx_bpf_kick_cpu(sib0, 0);
+		sample_core_fi(sib0, &fi_cnt, &fi_occ);
+		if (fi_cnt)
+			__sync_fetch_and_add(&phase3_real_fi, 1);
+	}
+}
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(core_forceidle_init)
+{
+	return scx_bpf_create_dsq(PARK_DSQ, -1);
+}
+
+void BPF_STRUCT_OPS(core_forceidle_exit, struct scx_exit_info *ei)
+{
+	UEI_RECORD(uei, ei);
+}
+
+SEC(".struct_ops.link")
+struct sched_ext_ops core_forceidle_ops = {
+	.select_cpu		= (void *)core_forceidle_select_cpu,
+	.enqueue		= (void *)core_forceidle_enqueue,
+	.dispatch		= (void *)core_forceidle_dispatch,
+	.running		= (void *)core_forceidle_running,
+	.stopping		= (void *)core_forceidle_stopping,
+	.update_idle		= (void *)core_forceidle_update_idle,
+	.tick			= (void *)core_forceidle_tick,
+	.init			= (void *)core_forceidle_init,
+	.exit			= (void *)core_forceidle_exit,
+	.flags			= SCX_OPS_ENQ_LAST | SCX_OPS_KEEP_BUILTIN_IDLE,
+	.name			= "core_forceidle",
+};
diff --git a/tools/testing/selftests/sched_ext/core_forceidle.c b/tools/testing/selftests/sched_ext/core_forceidle.c
new file mode 100644
index 000000000000..962863b3c0c1
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/core_forceidle.c
@@ -0,0 +1,490 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verify that core scheduling does not count voluntary idle picks as forced
+ * idle (preventing core_forceidle_count > 0 with core_forceidle_occupation == 0
+ * and spurious forced-idle accounting), while still counting genuine
+ * cookie-mismatch forced idle.
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+#define _GNU_SOURCE
+#include <bpf/bpf.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sched.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include <time.h>
+#include <unistd.h>
+#include <scx/common.h>
+
+#include "core_forceidle.bpf.skel.h"
+#include "scx_test.h"
+
+#ifndef PR_SCHED_CORE
+#define PR_SCHED_CORE		62
+#define PR_SCHED_CORE_CREATE	1
+#endif
+
+#define SCHEDSTATS_PATH		"/proc/sys/kernel/sched_schedstats"
+#define WAIT_TIMEOUT_MS		1000
+
+struct core_forceidle_ctx {
+	struct core_forceidle *skel;
+	cpu_set_t orig_mask;
+	int sib0;
+	int sib1;
+	int ctrl_cpu;
+	int orig_schedstats;
+};
+
+struct worker_state {
+	pthread_t thread;
+	bool started;
+	int init_err;
+	int wake_pipe[2];
+	int ready_pipe[2];
+	pid_t tid;
+	int stop;
+	int pause_flag;
+	int cpu;
+	int create_cookie;
+};
+
+static pid_t gettid_sys(void)
+{
+	return syscall(SYS_gettid);
+}
+
+static u64 now_ns(void)
+{
+	struct timespec ts;
+
+	clock_gettime(CLOCK_MONOTONIC, &ts);
+	return (u64)ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+}
+
+static int pin_current(int cpu)
+{
+	cpu_set_t mask;
+
+	CPU_ZERO(&mask);
+	CPU_SET(cpu, &mask);
+	return sched_setaffinity(0, sizeof(mask), &mask);
+}
+
+static int read_schedstats(void)
+{
+	char buf[16];
+	int fd, n;
+
+	fd = open(SCHEDSTATS_PATH, O_RDONLY);
+	if (fd < 0)
+		return -1;
+	n = read(fd, buf, sizeof(buf) - 1);
+	close(fd);
+	if (n <= 0)
+		return -1;
+	buf[n] = '\0';
+	return atoi(buf);
+}
+
+static void write_schedstats(int val)
+{
+	char buf[16];
+	int fd, len;
+
+	if (val < 0)
+		return;
+	fd = open(SCHEDSTATS_PATH, O_WRONLY);
+	if (fd < 0)
+		return;
+	len = snprintf(buf, sizeof(buf), "%d\n", val);
+	if (write(fd, buf, len) < 0)
+		;
+	close(fd);
+}
+
+static int find_smt_pair(const cpu_set_t *allowed, int *sib0, int *sib1,
+			 int *ctrl_cpu)
+{
+	int cpu;
+
+	*sib0 = -1;
+	*sib1 = -1;
+	*ctrl_cpu = -1;
+
+	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
+		char path[128], buf[256];
+		FILE *f;
+		char *tok, *saveptr = NULL;
+		int pair[2] = { -1, -1 };
+		int found = 0;
+
+		if (!CPU_ISSET(cpu, allowed))
+			continue;
+
+		snprintf(path, sizeof(path),
+			 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
+			 cpu);
+		f = fopen(path, "r");
+		if (!f)
+			continue;
+		if (!fgets(buf, sizeof(buf), f)) {
+			fclose(f);
+			continue;
+		}
+		fclose(f);
+
+		for (tok = strtok_r(buf, ",\n", &saveptr); tok && found < 2;
+		     tok = strtok_r(NULL, ",\n", &saveptr)) {
+			int a, b, c;
+
+			if (sscanf(tok, "%d-%d", &a, &b) == 2) {
+				for (c = a; c <= b && found < 2; c++) {
+					if (CPU_ISSET(c, allowed))
+						pair[found++] = c;
+				}
+			} else if (sscanf(tok, "%d", &a) == 1) {
+				if (CPU_ISSET(a, allowed))
+					pair[found++] = a;
+			}
+		}
+
+		if (found == 2) {
+			int c;
+
+			*sib0 = pair[0];
+			*sib1 = pair[1];
+			for (c = 0; c < CPU_SETSIZE; c++) {
+				if (CPU_ISSET(c, allowed) &&
+				    c != pair[0] && c != pair[1]) {
+					*ctrl_cpu = c;
+					return 0;
+				}
+			}
+			*ctrl_cpu = pair[0];
+			return 0;
+		}
+	}
+
+	return -ENOENT;
+}
+
+static void *worker_fn(void *arg)
+{
+	struct worker_state *st = arg;
+	char c = 1;
+
+	if (pin_current(st->cpu)) {
+		st->init_err = errno ?: EIO;
+		if (write(st->ready_pipe[1], &c, 1) < 0)
+			;
+		return NULL;
+	}
+
+	if (st->create_cookie && prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, 0, 0)) {
+		st->init_err = errno ?: EIO;
+		if (write(st->ready_pipe[1], &c, 1) < 0)
+			;
+		return NULL;
+	}
+
+	st->tid = gettid_sys();
+	if (write(st->ready_pipe[1], &c, 1) != 1)
+		return NULL;
+
+	while (!__atomic_load_n(&st->stop, __ATOMIC_ACQUIRE)) {
+		if (__atomic_load_n(&st->pause_flag, __ATOMIC_ACQUIRE)) {
+			if (read(st->wake_pipe[0], &c, 1) != 1)
+				break;
+			continue;
+		}
+		__asm__ volatile("" ::: "memory");
+	}
+
+	return NULL;
+}
+
+static void stop_worker(struct worker_state *st)
+{
+	char c = 0;
+
+	if (st->started) {
+		__atomic_store_n(&st->stop, 1, __ATOMIC_RELEASE);
+		__atomic_store_n(&st->pause_flag, 0, __ATOMIC_RELEASE);
+		if (st->wake_pipe[1] >= 0 && write(st->wake_pipe[1], &c, 1) < 0)
+			;
+		pthread_join(st->thread, NULL);
+		st->started = false;
+	}
+	if (st->wake_pipe[0] >= 0) {
+		close(st->wake_pipe[0]);
+		st->wake_pipe[0] = -1;
+	}
+	if (st->wake_pipe[1] >= 0) {
+		close(st->wake_pipe[1]);
+		st->wake_pipe[1] = -1;
+	}
+	if (st->ready_pipe[0] >= 0) {
+		close(st->ready_pipe[0]);
+		st->ready_pipe[0] = -1;
+	}
+	if (st->ready_pipe[1] >= 0) {
+		close(st->ready_pipe[1]);
+		st->ready_pipe[1] = -1;
+	}
+}
+
+static int start_worker(struct worker_state *st, int cpu, bool create_cookie,
+			bool start_paused)
+{
+	char c;
+	int err;
+
+	memset(st, 0, sizeof(*st));
+	st->wake_pipe[0] = -1;
+	st->wake_pipe[1] = -1;
+	st->ready_pipe[0] = -1;
+	st->ready_pipe[1] = -1;
+	st->cpu = cpu;
+	st->create_cookie = create_cookie;
+	st->pause_flag = start_paused ? 1 : 0;
+
+	if (pipe(st->wake_pipe)) {
+		err = errno ?: EIO;
+		goto err_out;
+	}
+	if (pipe(st->ready_pipe)) {
+		err = errno ?: EIO;
+		goto err_out;
+	}
+	err = pthread_create(&st->thread, NULL, worker_fn, st);
+	if (err)
+		goto err_out;
+	st->started = true;
+	if (read(st->ready_pipe[0], &c, 1) != 1) {
+		err = errno ?: EIO;
+		goto err_out;
+	}
+	if (st->init_err) {
+		err = st->init_err;
+		goto err_out;
+	}
+	return 0;
+
+err_out:
+	stop_worker(st);
+	return -err;
+}
+
+static bool wait_u32_nonzero(u32 *ptr, u64 timeout_ms)
+{
+	u64 deadline = now_ns() + timeout_ms * 1000000ULL;
+
+	while (now_ns() < deadline) {
+		if (__atomic_load_n(ptr, __ATOMIC_ACQUIRE))
+			return true;
+		usleep(500);
+	}
+	return __atomic_load_n(ptr, __ATOMIC_ACQUIRE) != 0;
+}
+
+static enum scx_test_status setup(void **ctx_ptr)
+{
+	struct core_forceidle_ctx *ctx;
+
+	ctx = calloc(1, sizeof(*ctx));
+	SCX_FAIL_IF(!ctx, "Failed to allocate context");
+	ctx->orig_schedstats = -1;
+
+	if (sched_getaffinity(0, sizeof(ctx->orig_mask), &ctx->orig_mask)) {
+		free(ctx);
+		SCX_FAIL("Failed to get affinity (%d)", errno);
+	}
+
+	if (find_smt_pair(&ctx->orig_mask, &ctx->sib0, &ctx->sib1, &ctx->ctrl_cpu)) {
+		printf("SKIP: SMT sibling pair required\n");
+		free(ctx);
+		return SCX_TEST_SKIP;
+	}
+
+	if (ctx->ctrl_cpu == ctx->sib0 || ctx->ctrl_cpu == ctx->sib1) {
+		printf("SKIP: a third CPU outside the SMT pair is required\n");
+		free(ctx);
+		return SCX_TEST_SKIP;
+	}
+
+	/* Probe CONFIG_SCHED_CORE support without modifying our own cookie. */
+	if (prctl(PR_SCHED_CORE, 0 /* PR_SCHED_CORE_GET */, 0, 0, (unsigned long)&(u64){0})) {
+		printf("SKIP: core scheduling not supported (%d)\n", errno);
+		free(ctx);
+		return SCX_TEST_SKIP;
+	}
+
+	if (pin_current(ctx->ctrl_cpu)) {
+		int ctrl_cpu = ctx->ctrl_cpu;
+
+		free(ctx);
+		SCX_FAIL("Failed to pin controller to CPU %d (%d)", ctrl_cpu, errno);
+	}
+
+	ctx->orig_schedstats = read_schedstats();
+	if (ctx->orig_schedstats == 0)
+		write_schedstats(1);
+
+	ctx->skel = SCX_OPS_OPEN(core_forceidle_ops, core_forceidle);
+	if (!ctx->skel) {
+		write_schedstats(ctx->orig_schedstats);
+		sched_setaffinity(0, sizeof(ctx->orig_mask), &ctx->orig_mask);
+		free(ctx);
+		SCX_FAIL("Failed to open core_forceidle skel");
+	}
+
+	SCX_OPS_LOAD(ctx->skel, core_forceidle_ops, core_forceidle, uei);
+
+	ctx->skel->bss->sib0 = ctx->sib0;
+	ctx->skel->bss->sib1 = ctx->sib1;
+
+	*ctx_ptr = ctx;
+	return SCX_TEST_PASS;
+}
+
+static enum scx_test_status run(void *ctx_ptr)
+{
+	struct core_forceidle_ctx *ctx = ctx_ptr;
+	struct core_forceidle *skel = ctx->skel;
+	struct worker_state parked_st = { .wake_pipe = { -1, -1 }, .ready_pipe = { -1, -1 } };
+	struct worker_state busy_st = { .wake_pipe = { -1, -1 }, .ready_pipe = { -1, -1 } };
+	struct bpf_link *link = NULL;
+	enum scx_test_status status = SCX_TEST_FAIL;
+	char c = 1;
+
+	if (start_worker(&parked_st, ctx->sib0, true, false)) {
+		SCX_ERR("Failed to start parked worker");
+		goto out;
+	}
+	/*
+	 * Leave busy_st untagged (cookie 0) so unrelated untagged kernel
+	 * threads on sib0 match it in Phase 2 instead of causing genuine
+	 * forced idle, while Phase 3 still sees a cookie mismatch against
+	 * parked_st.
+	 */
+	if (start_worker(&busy_st, ctx->sib1, false, true)) {
+		SCX_ERR("Failed to start busy worker");
+		goto out;
+	}
+
+	skel->bss->parked_pid = parked_st.tid;
+	skel->bss->busy_pid = busy_st.tid;
+	skel->bss->phase = 0;
+	skel->bss->parked_in_dsq = 0;
+	skel->bss->busy_running = 0;
+	skel->bss->phase1_obs = 0;
+	skel->bss->phase1_spurious_fi = 0;
+	skel->bss->phase1_zero_occ = 0;
+	skel->bss->phase2_obs = 0;
+	skel->bss->phase2_spurious_fi = 0;
+	skel->bss->phase3_real_fi = 0;
+	memset(&skel->data->uei, 0, sizeof(skel->data->uei));
+
+	link = bpf_map__attach_struct_ops(skel->maps.core_forceidle_ops);
+	if (!link) {
+		SCX_ERR("Failed to attach core_forceidle_ops");
+		goto out;
+	}
+
+	/*
+	 * Phase 1: sib0's cookied task is parked in PARK_DSQ on SCX_ENQ_LAST
+	 * while sib1 is idle (occ == 0). Neither sibling is forced idle.
+	 */
+	__atomic_store_n(&skel->bss->phase, 1, __ATOMIC_RELEASE);
+	if (!wait_u32_nonzero(&skel->bss->phase1_obs, WAIT_TIMEOUT_MS)) {
+		SCX_ERR("Phase 1: sib0 never entered voluntary idle");
+		goto out;
+	}
+	/* Let ticks fire so __sched_core_account_forceidle() runs if buggy. */
+	usleep(50000);
+
+	/*
+	 * Phase 2: sib0 stays voluntarily idle with parked_pid in PARK_DSQ
+	 * while busy_pid (different cookie) runs on sib1 (occ == 1).
+	 */
+	__atomic_store_n(&skel->bss->phase, 2, __ATOMIC_RELEASE);
+	__atomic_store_n(&busy_st.pause_flag, 0, __ATOMIC_RELEASE);
+	if (write(busy_st.wake_pipe[1], &c, 1) != 1) {
+		SCX_ERR("Phase 2: failed to wake busy worker");
+		goto out;
+	}
+	if (!wait_u32_nonzero(&skel->bss->phase2_obs, WAIT_TIMEOUT_MS)) {
+		SCX_ERR("Phase 2: busy worker never ticked while sib0 voluntarily idle");
+		goto out;
+	}
+	usleep(50000);
+
+	/*
+	 * Phase 3: unpark parked_pid so two conflicting cookies compete on
+	 * (sib0, sib1) and confirm genuine forced idle is still counted.
+	 */
+	__atomic_store_n(&skel->bss->phase, 3, __ATOMIC_RELEASE);
+	if (!wait_u32_nonzero(&skel->bss->phase3_real_fi, WAIT_TIMEOUT_MS)) {
+		SCX_ERR("Phase 3: genuine cookie-mismatch forced idle was not counted");
+		goto out;
+	}
+
+	if (UEI_EXITED(skel, uei)) {
+		SCX_ERR("Scheduler exited unexpectedly (kind=%llu code=%lld msg='%s')",
+			(unsigned long long)skel->data->uei.kind,
+			(long long)skel->data->uei.exit_code,
+			skel->data->uei.msg);
+		goto out;
+	}
+
+	if (skel->bss->phase1_spurious_fi) {
+		SCX_ERR("Phase 1: voluntary idle counted as forced idle (obs=%u fi=%u zero_occ=%u)",
+			skel->bss->phase1_obs, skel->bss->phase1_spurious_fi,
+			skel->bss->phase1_zero_occ);
+		goto out;
+	}
+
+	if (skel->bss->phase2_spurious_fi) {
+		SCX_ERR("Phase 2: voluntary idle counted as forced idle (obs=%u fi=%u)",
+			skel->bss->phase2_obs, skel->bss->phase2_spurious_fi);
+		goto out;
+	}
+
+	status = SCX_TEST_PASS;
+out:
+	__atomic_store_n(&skel->bss->phase, 0, __ATOMIC_RELEASE);
+	if (link)
+		bpf_link__destroy(link);
+	stop_worker(&busy_st);
+	stop_worker(&parked_st);
+	return status;
+}
+
+static void cleanup(void *ctx_ptr)
+{
+	struct core_forceidle_ctx *ctx = ctx_ptr;
+
+	if (ctx->skel)
+		core_forceidle__destroy(ctx->skel);
+	write_schedstats(ctx->orig_schedstats);
+	sched_setaffinity(0, sizeof(ctx->orig_mask), &ctx->orig_mask);
+	free(ctx);
+}
+
+static struct scx_test core_forceidle = {
+	.name = "core_forceidle",
+	.description = "Verify voluntary idle picks are not counted as core-sched forced idle",
+	.setup = setup,
+	.run = run,
+	.cleanup = cleanup,
+};
+
+REGISTER_SCX_TEST(&core_forceidle)
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] sched/core: Don't count voluntary idle picks as forced idle
  2026-12-01 12:03 ` [PATCH 1/2] " Kuba Piecuch
@ 2026-12-01 15:11   ` Tejun Heo
  0 siblings, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2026-12-01 15:11 UTC (permalink / raw)
  To: Kuba Piecuch
  Cc: Peter Zijlstra, Ingo Molnar, Juri Lelli, Vincent Guittot,
	Josh Don, David Vernet, Andrea Righi, Changwoo Min, sched-ext,
	linux-kernel

On Tue, Dec 01, 2026 at 12:03:03PM +0000, Kuba Piecuch wrote:
> In pick_next_task(), the per-sibling loop increments
> rq->core->core_forceidle_count whenever p == rq_i->idle and
> rq_i->nr_running > 0. That conflates a sibling forced idle by a cookie
> mismatch with a sibling whose pick_task() voluntarily returned idle while
> rq_i->nr_running > 0.
>
> Voluntary idle with nr_running > 0 is legitimate whenever a scheduling
> class keeps queued tasks off the CPU and returns NULL from pick_task(),
> such as sched_ext tasks parked on a non-local DSQ. Counting those
> voluntary idle picks as forced idle has two effects:
>
> - If all siblings pick idle (occ == 0), core_forceidle_count becomes
>   non-zero while core_forceidle_occupation is 0, triggering
>   WARN_ON_ONCE(!rq->core->core_forceidle_occupation) in
>   __sched_core_account_forceidle() when schedstats is enabled.
> - If another sibling is running (occ > 0), the voluntary idle time is
>   spuriously billed as forced idle to the running sibling's cgroup and
>   triggers forced-idle-only paths (queue_core_balance(), forced_idle_rq(),
>   sched_core_idle_needs_pick()).
>
> Increment core_forceidle_count and core_forceidle_seq only inside the
> !cookie_equals(p, cookie) branch when sched_core_find() returns NULL and
> replaces a non-idle pick with idle. Since cookie_equals() always returns
> true for rq_i->idle, voluntary idle picks are never counted as forced
> idle, and entering the mismatch branch guarantees a non-idle @max so
> core_forceidle_occupation is always non-zero whenever forced idle is
> recorded.
>
> Fixes: 4feee7d12603 ("sched/core: Forced idle accounting")
> Cc: stable@vger.kernel.org
> Signed-off-by: Kuba Piecuch <jpiecuch@google.com>

Acked-by: Tejun Heo <tj@kernel.org>

Thanks.

--
tejun

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-01 15:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 12:03 [PATCH 0/2] sched/core: Don't count voluntary idle picks as forced idle Kuba Piecuch
2026-12-01 12:03 ` [PATCH 1/2] " Kuba Piecuch
2026-12-01 15:11   ` Tejun Heo
2026-12-01 12:03 ` [PATCH 2/2] selftests/sched_ext: Add core_forceidle test for voluntary idle picks Kuba Piecuch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox