mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Changwoo Min <changwoo@igalia.com>
To: tj@kernel.org, void@manifault.com, arighi@nvidia.com,
	changwoo@igalia.com
Cc: david.dai@linux.dev, Gavin Guo <gavinguo@igalia.com>,
	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	[thread overview]
Message-ID: <20261203045618.982539-9-changwoo@igalia.com> (raw)
In-Reply-To: <20261203045618.982539-1-changwoo@igalia.com>

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 <changwoo@igalia.com>
---
 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 <changwoo@igalia.com>
+ */
+#include <scx/common.bpf.h>
+
+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 <changwoo@igalia.com>
+ */
+#define _GNU_SOURCE
+#include <bpf/bpf.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+#include <scx/common.h>
+
+#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, &param), 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


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

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  4:56 [PATCH v2 0/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
2026-12-03  4:56 ` [PATCH v2 1/8] sched_ext: Reset task fields after ops.exit_task() Changwoo Min
2026-12-03  4:56 ` [PATCH v2 2/8] sched_ext: Mark a task enabled before ops.enable() Changwoo Min
2026-12-03  4:56 ` [PATCH v2 3/8] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id() Changwoo Min
2026-12-03  4:56 ` [PATCH v2 4/8] selftests/sched_ext: Test " Changwoo Min
2026-12-03  4:56 ` [PATCH v2 5/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
2026-12-03  4:56 ` [PATCH v2 6/8] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv Changwoo Min
2026-12-03  4:56 ` [PATCH v2 7/8] selftests/sched_ext: Test p->scx.bpf_priv across class changes Changwoo Min
2026-12-03  4:56 ` Changwoo Min [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=20261203045618.982539-9-changwoo@igalia.com \
    --to=changwoo@igalia.com \
    --cc=arighi@nvidia.com \
    --cc=david.dai@linux.dev \
    --cc=gavinguo@igalia.com \
    --cc=kernel-dev@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=tj@kernel.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®