From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796126590; b=qigpaloRPL3+GWe1tUsM+ddeRCz0ZwNtdvAG4nKE/Kp9JYGqkeF1+I+Z4mx+NjLm3j/Z bZ/kZ2zalFHXXRsw5/8K7WhaUv6cHft+zje/HfD3Ei9bzrEhEKf6TWrYp/KzXJnQabhu5 fxl1M6ykt7LVK4JN6/sQeH3qgfA7eTqtSr9HeJugZVzXgB9peallbfYY5lZ13xUNOlbyz n58JWB/uKfv9p31LVzKPsmA7jfykmSLzMNqcTcZq9zlpCVsVSy3GURkSZapiMsImmN0fm QN7mxNs9O7tdba97o22eIr3C5D3tC4g7AA4aESkqMQ7B81T+ctzMOPBfQuUSasjrJEA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796126590; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=fSfSd61L7hqiEpnu5MZWPkAPXwc67cVQQ7XrTD40xJI=; b=5H7shh0BX6g+cvma1Nuqqia7jGOc/gbplCbUfCEaZFLtHy9k6TmiN209nOlo1aF3+/X4 nX91ZYuTKnCFhgsQ4eQw6/QvqW9rHaatEj5HKySwOyeGIPqClsOo7btjNHQgkYATRdZMI FYOZ/rn0xqnJY9MPjDVY682KWGnF0Jkrzi3qpmj/D8XtrQk3dVGwcmt2B9wbIjACKrfbY liGSZFjhoAspkLV1Tpm3knC+ph4X8TAxrCnf6MFgxyUDGJ5Gjt8NwYNLMbubXYL8qT4u2 8i04GjnpFIEBXnMZe+xFVazsQaU3XyfNqYnUSVsR7Mz0dZw1yUaOMFxfO/eAzEfHrSA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jpiecuch.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=I71FdlK4; arc=none smtp.remote-ip=209.85.128.71 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 96D8C606A683 for ; Tue, 1 Dec 2026 12:03:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 96D8C606A683 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jpiecuch.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=I71FdlK4 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4ad3d35e899so95508085e9.1 for ; Tue, 01 Dec 2026 04:03:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796126588; x=1796731388; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=fSfSd61L7hqiEpnu5MZWPkAPXwc67cVQQ7XrTD40xJI=; b=I71FdlK4UbEJymEomMCxoIlZAhjOCtggP7GU1pmZ7pd/qjTyzmjOcf1HKNHdcek/8x 6k5mN+26+H5VJFm8AbhYm00rYT7POLRRPmzecbU+Zg4cguj/BRoKlzqZWiE2dMX8kBOR JXlyHI9djIJSvcuyJKe09In1LqmDRLmx8+NH6drIFmdRr6ngee+yUs4xP+/+XyXNBxky b76bexeXX3kwkGOiRFGCn+UAtVvSQsn6JPf7bELPxf+b0a+wUle8cQJECZMwLS9FZLA3 s0HJFwoxAHp124Dde/6I3aug8R8SkFdcTopszRdLIpyxtvZ/54wgAsIefBODC/jin8O1 Wr2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796126588; x=1796731388; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=fSfSd61L7hqiEpnu5MZWPkAPXwc67cVQQ7XrTD40xJI=; b=IIOAnEV/BU7OzZfDJEEdKYRq4OGfedGvzw/GBf6no2Iqe+fvuiTJyz+HSsQu2qi3xW dnZAks1qja39gAHCbVSOdDCpgrl5lm2JOlopyn1DQteKQVs6JcAd4PPcmrjYiWz3C7f8 DgJvsm1dK/eSPJqXoJOFfYkNAjOl2rfgCsiSQOELkeFQwIY8U0WrnGbSc/eR7A5lPOvI Vmi0b99V03LH4UGkg8Jw7DeMbkE115H+HIYK7bSZtfhbEmYpDJEO5jlttEedXnHxdb7G HbYq1LmRKj2gCjlQD7Gxi4kJsvLyCwy0JXHERXskLsHfzo1taQQNI9ma/lfzOdPQaVfj 15+A== X-Forwarded-Encrypted: i=1; AL+hErGWWfElqxiBO/tsq1TT0kZsRIogC7fUVgKsIZLMcN+KI92i6aW44JX4wdZJEZxq/O/ElPW6Cpz5No7X55E=@vger.kernel.org X-Gm-Message-State: APuU4Z9ewLuiHV2pRKszG0wj9OSm7S/x9GMx9V777oiSJMbYdPi5nbOH oRzTtBvy/MTJY6kUBICZS55JcBaiRn+ll4/pH13GH3iTEe/amQBsCOvptX/U3vLaHT0cdiljhmM 4iuTTxO0g9RQmEA== X-Received: from wmdp22.prod.google.com ([2002:a05:600c:5d6:b0:4a2:1824:f77d]) (user=jpiecuch job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:37c6:b0:4bb:7033:c938 with SMTP id 5b1f17b1804b1-4bb888147b7mr738906505e9.14.1796126587743; Tue, 01 Dec 2026 04:03:07 -0800 (PST) Date: Tue, 1 Dec 2026 12:03:04 +0000 In-Reply-To: <20261201120123.3678974-1-jpiecuch@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261201120123.3678974-1-jpiecuch@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261201120123.3678974-3-jpiecuch@google.com> Subject: [PATCH 2/2] selftests/sched_ext: Add core_forceidle test for voluntary idle picks From: Kuba Piecuch To: Peter Zijlstra , Tejun Heo Cc: Kuba Piecuch , Ingo Molnar , Juri Lelli , Vincent Guittot , Josh Don , David Vernet , Andrea Righi , Changwoo Min , sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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 --- 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 + +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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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