mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kuba Piecuch <jpiecuch@google.com>
To: Peter Zijlstra <peterz@infradead.org>, Tejun Heo <tj@kernel.org>
Cc: Kuba Piecuch <jpiecuch@google.com>,
	Ingo Molnar <mingo@redhat.com>,
	 Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	 Josh Don <joshdon@google.com>, David Vernet <void@manifault.com>,
	 Andrea Righi <arighi@nvidia.com>,
	Changwoo Min <changwoo@igalia.com>,
	sched-ext@lists.linux.dev,  linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] selftests/sched_ext: Add core_forceidle test for voluntary idle picks
Date: Tue,  1 Dec 2026 12:03:04 +0000	[thread overview]
Message-ID: <20261201120123.3678974-3-jpiecuch@google.com> (raw)
In-Reply-To: <20261201120123.3678974-1-jpiecuch@google.com>

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


      parent reply	other threads:[~2026-12-01 12:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201120123.3678974-3-jpiecuch@google.com \
    --to=jpiecuch@google.com \
    --cc=arighi@nvidia.com \
    --cc=changwoo@igalia.com \
    --cc=joshdon@google.com \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=tj@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=void@manifault.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®