From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=213.97.179.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796273829; b=pO3r9TrzvqEOLB1Zbs0mlWFAHNxcDM31U8E9efHcRum73QlmUN7IgcE16HnSIBZB6lws rBUDUL5LYqutV5ynrqyc9ElXvrjnTG2Tqp8fYHUWwXF0HyYcKwC5/4A6xkzrmZMEx4YBX 6Fq3TsvmPRjExJb92Xz+y4GsP9pbJP3/9trr3cxIRJjuZdUUzSszT9b4pw5juWFGNq8Lh OeAkA5hWa/Al9P65jJ3eBl2R9WbPtKz0RIsrROBTCWLtgmZcqAAwXdwox91t4bIPNODqs X330+ze9l3Eydqaooh0X+2jCtn7X39IYpYvezt/oZCeglM3Z7/BT68gfRSfiOJrRhgg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796273829; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=4ZsQA6lYT1TN0QXIyT66AgLVHthzVTLpwgHJoOuYnVw=; b=maxt9Hj1VF/RT7FkEVTsjIQzWuyBFyU2FnO2l07IRvsjkomlE57cPOMuhzQ5bT1CXZst l8uvlIBDXoXyjG2t1ZseZ9a1D+kZIiVZYByUgyk363jcAl1sJBZWDZwklq7qLEC38Z+Je cTtvKv8/OL9rT2W/rPT7vmwiTgSeJefH9QVOFt67+d/U3v6qPBJqsg1Arud+M+4mWU9N2 uOzfxY5ZsR4YhEtV2q6G1CSOUPOO38BCHVLAz/Wrvw7al4GIGwXOwRDrzDqScqrq5BqM7 tjVE0s+vQdUJkKugwEwq6ugs/GhrxQRS2/6wM1doy5O3FAMjtn/nf3/R9QM98yykvMg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=X0rxH1gD; arc=none smtp.remote-ip=213.97.179.56 Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38DD8606A32D for ; Thu, 3 Dec 2026 04:57:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 38DD8606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=X0rxH1gD DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:From:Reply-To; bh=4ZsQA6lYT1TN0QXIyT66AgLVHthzVTLpwgHJoOuYnVw=; t=1796273828; x=2111633828; b=X0rxH1gDQrxaujIKcK9z1WmkGQxighGUKijFIXFMPxjwzeX RVLUsN4ABNZThzdAwad3IZ4+i7lfp8OH/G2U3265zuT8CWFPqaukPXletJ7ABfF5xRP4coKZvyocy UZxM8Z31tmbuXv3sP5hZ74HFljVadAyHknhWA6ftljV9KHu6lk8zz8IlrU7AKxJtxWeAjxhzBcr8i EKJfbgGSrUVZcSwiUBdgDpyt5JlPUxhIABQ789PJsdVCg9a1oML8Cb2VCyFhs1znkSMJf4jmiFWdS uqSGGJXY+0+8Io81T/EpIT882RZgmuyvNbC+g9cy3F6PjGZiiM+WnI7IJOClr7/w==; Received: from [58.29.145.179] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) (envelope-from ) id 1xYysS-00000001TzD-1aHu; Thu, 03 Dec 2026 05:57:04 +0100 From: Changwoo Min To: tj@kernel.org, void@manifault.com, arighi@nvidia.com, changwoo@igalia.com Cc: david.dai@linux.dev, Gavin Guo , kernel-dev@igalia.com, sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2 8/8] selftests/sched_ext: Test task field reset across sub-scheduler handoffs Date: Thu, 3 Dec 2026 13:56:18 +0900 Message-ID: <20261203045618.982539-9-changwoo@igalia.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203045618.982539-1-changwoo@igalia.com> References: <20261203045618.982539-1-changwoo@igalia.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 Content-Transfer-Encoding: 8bit A task handed between a root scheduler and a sub-scheduler must not carry the bpf_priv and dsq_vtime of the scheduler it left, even when that scheduler writes them from ops.exit_task(). Load one scheduler twice, as the root and as a sub-scheduler on a cgroup. A task in that cgroup goes to the sub-scheduler when it attaches and back to the root when it detaches. Both schedulers set bpf_priv from ops.enable(), and from ops.exit_task() they try to set bpf_priv and set dsq_vtime. Setting bpf_priv from ops.exit_task() must fail, and every ops.enable() must find both fields cleared. The test skips without CONFIG_EXT_SUB_SCHED. Signed-off-by: Changwoo Min --- tools/testing/selftests/sched_ext/Makefile | 1 + .../selftests/sched_ext/sub_handoff.bpf.c | 102 +++++++ .../testing/selftests/sched_ext/sub_handoff.c | 265 ++++++++++++++++++ 3 files changed, 368 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/sub_handoff.bpf.c create mode 100644 tools/testing/selftests/sched_ext/sub_handoff.c diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile index 1ab274250403..1dcaf60c5b33 100644 --- a/tools/testing/selftests/sched_ext/Makefile +++ b/tools/testing/selftests/sched_ext/Makefile @@ -201,6 +201,7 @@ auto-test-targets := \ sched_id \ sub_caps_kfunc \ sub_core_caps \ + sub_handoff \ task_bpf_priv \ test_example \ total_bw \ diff --git a/tools/testing/selftests/sched_ext/sub_handoff.bpf.c b/tools/testing/selftests/sched_ext/sub_handoff.bpf.c new file mode 100644 index 000000000000..d7ce3f4e150c --- /dev/null +++ b/tools/testing/selftests/sched_ext/sub_handoff.bpf.c @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A cid-form scheduler for the sub_handoff selftest, loaded twice: as the root + * and as a sub-scheduler on a cgroup. + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#include + +char _license[] SEC("license") = "GPL"; + +#define PRIV_MAGIC 0x5c7da7a000000000ULL +#define VTIME_MAGIC 0x5c7d7173e0000000ULL + +struct { + __uint(type, BPF_MAP_TYPE_ARENA); + __uint(map_flags, BPF_F_MMAPABLE); + __uint(max_entries, 1 << 4); +} arena SEC(".maps"); + +u64 __arena_global arena_touch; + +/* The task handed between the schedulers, set by user space */ +s32 target_pid; + +/* ops.enable() calls on the target, and those that saw a value left behind */ +u64 nr_enable, nr_stale_priv, nr_stale_vtime; + +/* ops.exit_task() calls on the target in which setting bpf_priv succeeded */ +u64 nr_exit_set; + +UEI_DEFINE(uei); + +s32 BPF_STRUCT_OPS(sub_handoff_select_cid, struct task_struct *p, s32 prev_cid, + u64 wake_flags) +{ + return prev_cid; +} + +void BPF_STRUCT_OPS(sub_handoff_enqueue, struct task_struct *p, u64 enq_flags) +{ + scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, SCX_SLICE_DFL, enq_flags); +} + +void BPF_STRUCT_OPS(sub_handoff_enable, struct task_struct *p, + struct scx_enable_args *args) +{ + if (p->pid != target_pid) + return; + + __sync_fetch_and_add(&nr_enable, 1); + if (p->scx.bpf_priv) + __sync_fetch_and_add(&nr_stale_priv, 1); + if (p->scx.dsq_vtime) + __sync_fetch_and_add(&nr_stale_vtime, 1); + + scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid); +} + +/* + * @p is still on this scheduler but no longer enabled. Neither write may reach + * the scheduler @p moves to. + */ +void BPF_STRUCT_OPS(sub_handoff_exit_task, struct task_struct *p, + struct scx_exit_task_args *args) +{ + if (p->pid != target_pid) + return; + + if (scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid)) + __sync_fetch_and_add(&nr_exit_set, 1); + scx_bpf_task_set_dsq_vtime(p, VTIME_MAGIC); +} + +s32 BPF_STRUCT_OPS_SLEEPABLE(sub_handoff_sub_attach, + struct scx_sub_attach_args *args) +{ + return 0; +} + +s32 BPF_STRUCT_OPS_SLEEPABLE(sub_handoff_init) +{ + arena_touch++; + return 0; +} + +void BPF_STRUCT_OPS(sub_handoff_exit, struct scx_exit_info *ei) +{ + UEI_RECORD(uei, ei); +} + +SCX_OPS_CID_DEFINE(sub_handoff_ops, + .select_cid = (void *)sub_handoff_select_cid, + .enqueue = (void *)sub_handoff_enqueue, + .enable = (void *)sub_handoff_enable, + .exit_task = (void *)sub_handoff_exit_task, + .sub_attach = (void *)sub_handoff_sub_attach, + .init = (void *)sub_handoff_init, + .exit = (void *)sub_handoff_exit, + .flags = SCX_OPS_SWITCH_PARTIAL, + .name = "sub_handoff"); diff --git a/tools/testing/selftests/sched_ext/sub_handoff.c b/tools/testing/selftests/sched_ext/sub_handoff.c new file mode 100644 index 000000000000..18e20416c911 --- /dev/null +++ b/tools/testing/selftests/sched_ext/sub_handoff.c @@ -0,0 +1,265 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Verify that a task handed between a root scheduler and a sub-scheduler + * arrives without the p->scx.bpf_priv and dsq_vtime of the scheduler it left, + * even when that scheduler writes them from ops.exit_task(). + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cgroup_util.h" +#include "scx_test.h" +#include "sub_handoff.bpf.skel.h" + +#ifndef SCHED_EXT +#define SCHED_EXT 7 +#endif + +#define WAIT_TIMEOUT_MS 1000 + +struct sub_handoff_ctx { + struct sub_handoff *root_skel; + struct sub_handoff *sub_skel; + char sub_cg[PATH_MAX]; + u64 sub_cgid; + bool cg_created; +}; + +static u64 get_cgroup_id(const char *path) +{ + union { + u64 id; + unsigned char bytes[8]; + } id = {}; + struct file_handle *handle; + int mount_id, ret; + + handle = calloc(1, sizeof(*handle) + sizeof(id)); + if (!handle) + return 0; + handle->handle_bytes = sizeof(id); + ret = name_to_handle_at(AT_FDCWD, path, handle, &mount_id, 0); + if (!ret && handle->handle_bytes == sizeof(id)) + memcpy(id.bytes, handle->f_handle, sizeof(id)); + free(handle); + + return ret ? 0 : id.id; +} + +static u64 now_ns(void) +{ + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + return (u64)ts.tv_sec * 1000000000ULL + ts.tv_nsec; +} + +/* Wait until *@ptr reaches @val, return its last value */ +static u64 wait_u64(u64 *ptr, u64 val) +{ + u64 deadline = now_ns() + WAIT_TIMEOUT_MS * 1000000ULL; + u64 v; + + while ((v = __atomic_load_n(ptr, __ATOMIC_ACQUIRE)) < val && + now_ns() < deadline) + usleep(1000); + return v; +} + +static void cleanup(void *ctx_ptr) +{ + struct sub_handoff_ctx *ctx = ctx_ptr; + + sub_handoff__destroy(ctx->sub_skel); + sub_handoff__destroy(ctx->root_skel); + if (ctx->cg_created) + cg_destroy(ctx->sub_cg); + free(ctx); +} + +static struct sub_handoff *open_and_load(u64 sub_cgid) +{ + struct sub_handoff *skel; + + skel = sub_handoff__open(); + if (!skel) + return NULL; + SCX_ENUM_INIT(skel); + skel->struct_ops.sub_handoff_ops->sub_cgroup_id = sub_cgid; + if (sub_handoff__load(skel)) { + sub_handoff__destroy(skel); + return NULL; + } + return skel; +} + +static enum scx_test_status setup(void **ctx_ptr) +{ + struct sub_handoff_ctx *ctx; + char root[PATH_MAX]; + + if (!__COMPAT_struct_has_field("sched_ext_ops", "sub_cgroup_id")) { + printf("SKIP: sub-schedulers not supported\n"); + return SCX_TEST_SKIP; + } + + if (cg_find_unified_root(root, sizeof(root), NULL)) { + printf("SKIP: cgroup v2 root not found\n"); + return SCX_TEST_SKIP; + } + + ctx = calloc(1, sizeof(*ctx)); + SCX_FAIL_IF(!ctx, "Failed to allocate context"); + + if (snprintf(ctx->sub_cg, sizeof(ctx->sub_cg), "%s/scx_sub_handoff_%d", + root, getpid()) >= (int)sizeof(ctx->sub_cg)) { + cleanup(ctx); + SCX_FAIL("Sub cgroup path too long"); + } + if (cg_create(ctx->sub_cg)) { + cleanup(ctx); + SCX_FAIL("Failed to create sub cgroup (%d)", errno); + } + ctx->cg_created = true; + + ctx->sub_cgid = get_cgroup_id(ctx->sub_cg); + if (!ctx->sub_cgid) { + cleanup(ctx); + SCX_FAIL("Failed to read sub cgroup ID"); + } + + ctx->root_skel = open_and_load(0); + if (!ctx->root_skel) { + cleanup(ctx); + SCX_FAIL("Failed to load root skel"); + } + + ctx->sub_skel = open_and_load(ctx->sub_cgid); + if (!ctx->sub_skel) { + cleanup(ctx); + SCX_FAIL("Failed to load sub skel"); + } + + *ctx_ptr = ctx; + return SCX_TEST_PASS; +} + +static enum scx_test_status handoffs(struct sub_handoff_ctx *ctx, pid_t pid, + struct bpf_link **root_link, + struct bpf_link **sub_link) +{ + struct sub_handoff__bss *root = ctx->root_skel->bss; + struct sub_handoff__bss *sub = ctx->sub_skel->bss; + struct sched_param param = {}; + + root->target_pid = pid; + sub->target_pid = pid; + + *root_link = bpf_map__attach_struct_ops(ctx->root_skel->maps.sub_handoff_ops); + SCX_FAIL_IF(!*root_link, "Failed to attach root scheduler"); + + /* Step 1: Move the target to the sub-cgroup */ + SCX_FAIL_IF(cg_enter(ctx->sub_cg, pid), "Failed to move target into sub cgroup"); + + /* + * %SCX_OPS_SWITCH_PARTIAL takes only SCHED_EXT tasks. Switching the + * target makes the root, the only scheduler yet, enable it and set its + * bpf_priv. + */ + SCX_EQ(sched_setscheduler(pid, SCHED_EXT, ¶m), 0); + SCX_EQ(wait_u64(&root->nr_enable, 1), 1); + + /* Step 2: Attaching the sub-scheduler hands the target over */ + *sub_link = bpf_map__attach_struct_ops(ctx->sub_skel->maps.sub_handoff_ops); + if (!*sub_link && errno == EOPNOTSUPP) { + printf("SKIP: CONFIG_EXT_SUB_SCHED not enabled\n"); + return SCX_TEST_SKIP; + } + SCX_FAIL_IF(!*sub_link, "Failed to attach sub scheduler (%d)", errno); + + /* + * The root exits the target before the sub-scheduler enables it. The + * root's ops.exit_task() tries to set bpf_priv, which must fail because + * the target is no longer enabled. It also sets dsq_vtime, which the + * kernel must clear afterwards. Both fields start at 0 on a clean + * handoff, so the sub-scheduler's ops.enable() must see neither. + */ + SCX_EQ(wait_u64(&sub->nr_enable, 1), 1); + SCX_EQ(root->nr_exit_set, 0); + SCX_EQ(sub->nr_stale_priv, 0); + SCX_EQ(sub->nr_stale_vtime, 0); + + /* Step 3: Detaching the sub-scheduler hands the target back */ + bpf_link__destroy(*sub_link); + *sub_link = NULL; + + /* + * The same checks in the other direction, from the sub-scheduler's + * ops.exit_task() to the root's second ops.enable(). The root's stale + * counters also cover its first ops.enable() in step 1. + */ + SCX_EQ(wait_u64(&root->nr_enable, 2), 2); + SCX_EQ(sub->nr_exit_set, 0); + SCX_EQ(root->nr_stale_priv, 0); + SCX_EQ(root->nr_stale_vtime, 0); + + /* Neither handoff may have made the root exit with an error */ + SCX_ASSERT_ALIVE(ctx->root_skel, uei); + return SCX_TEST_PASS; +} + +static enum scx_test_status run(void *ctx_ptr) +{ + struct sub_handoff_ctx *ctx = ctx_ptr; + struct bpf_link *root_link = NULL, *sub_link = NULL; + enum scx_test_status status; + int pfd[2]; + pid_t pid; + char c; + + SCX_FAIL_IF(pipe(pfd), "Failed to create pipe (%d)", errno); + + /* The target sleeps through the handoffs and exits when the pipe closes */ + pid = fork(); + if (pid < 0) { + close(pfd[0]); + close(pfd[1]); + SCX_FAIL("Failed to fork target (%d)", errno); + } + if (pid == 0) { + close(pfd[1]); + _exit(read(pfd[0], &c, 1) == 0 ? 0 : 1); + } + close(pfd[0]); + + status = handoffs(ctx, pid, &root_link, &sub_link); + + bpf_link__destroy(sub_link); + bpf_link__destroy(root_link); + close(pfd[1]); + waitpid(pid, NULL, 0); + return status; +} + +struct scx_test sub_handoff = { + .name = "sub_handoff", + .description = "Verify that sub-scheduler handoffs reset the task fields", + .setup = setup, + .run = run, + .cleanup = cleanup, +}; +REGISTER_SCX_TEST(&sub_handoff) -- 2.56.1