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 2/5] selftests/sched_ext: Test scx_bpf_sched_id() and scx_bpf_task_sched_id()
Date: Tue,  1 Dec 2026 14:48:47 +0900	[thread overview]
Message-ID: <20261201054850.780664-3-changwoo@igalia.com> (raw)
In-Reply-To: <20261201054850.780664-1-changwoo@igalia.com>

Load a partial-switch scheduler and move the test task onto it. The
task's owner id must equal the scheduler's id and be nonzero, and both
must read 0 once the scheduler is unloaded. A second instance must get
a larger id.

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/sched_id.bpf.c        |  47 ++++++++
 tools/testing/selftests/sched_ext/sched_id.c  | 103 ++++++++++++++++++
 3 files changed, 151 insertions(+)
 create mode 100644 tools/testing/selftests/sched_ext/sched_id.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/sched_id.c

diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile
index fe63dbd53f56..ef59c463cd33 100644
--- a/tools/testing/selftests/sched_ext/Makefile
+++ b/tools/testing/selftests/sched_ext/Makefile
@@ -198,6 +198,7 @@ auto-test-targets :=			\
 	select_cpu_dispatch_dbl_dsp	\
 	select_cpu_vtime		\
 	rt_stall			\
+	sched_id			\
 	sub_caps_kfunc			\
 	sub_core_caps			\
 	test_example			\
diff --git a/tools/testing/selftests/sched_ext/sched_id.bpf.c b/tools/testing/selftests/sched_ext/sched_id.bpf.c
new file mode 100644
index 000000000000..8828c3ecec3e
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sched_id.bpf.c
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A BPF program for testing scx_bpf_sched_id() and scx_bpf_task_sched_id().
+ *
+ * Copyright (c) 2026 Meta Platforms, Inc. and affiliates.
+ * Author: Changwoo Min <changwoo@igalia.com>
+ */
+#include <scx/common.bpf.h>
+
+char _license[] SEC("license") = "GPL";
+
+UEI_DEFINE(uei); /* Error handling */
+
+/* The task to probe, set by user space */
+s32 target_pid;
+
+/* Results of the last probe() run, overwritten by every run */
+u64 probe_sched_id;
+u64 probe_task_sched_id;
+
+void BPF_STRUCT_OPS(sched_id_exit, struct scx_exit_info *ei)
+{
+	UEI_RECORD(uei, ei);
+}
+
+/* Read this scheduler's id and the id of the target task's scheduler */
+SEC("syscall")
+int probe(void *ctx)
+{
+	struct task_struct *p;
+
+	p = bpf_task_from_pid(target_pid);
+	if (!p)
+		return -ESRCH;
+
+	probe_sched_id = scx_bpf_sched_id();
+	probe_task_sched_id = scx_bpf_task_sched_id(p);
+	bpf_task_release(p);
+	return 0;
+}
+
+SEC(".struct_ops.link")
+struct sched_ext_ops sched_id_ops = {
+	.exit			= (void *)sched_id_exit,
+	.flags			= SCX_OPS_SWITCH_PARTIAL,
+	.name			= "sched_id",
+};
diff --git a/tools/testing/selftests/sched_ext/sched_id.c b/tools/testing/selftests/sched_ext/sched_id.c
new file mode 100644
index 000000000000..a926062e13c7
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sched_id.c
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test for scx_bpf_sched_id() and scx_bpf_task_sched_id().
+ *
+ * Copyright (c) 2026 Meta Platforms, Inc. and affiliates.
+ * Author: Changwoo Min <changwoo@igalia.com>
+ */
+#include <bpf/bpf.h>
+#include <sched.h>
+#include <scx/common.h>
+#include <unistd.h>
+#include "sched_id.bpf.skel.h"
+#include "scx_test.h"
+
+#define SCHED_EXT	7
+
+static int set_policy(int policy)
+{
+	struct sched_param param = {};
+
+	return sched_setscheduler(0, policy, &param);
+}
+
+/* Run probe() on this task and return its result */
+static int probe(struct sched_id *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+
+	return bpf_prog_test_run_opts(bpf_program__fd(skel->progs.probe),
+				      &topts) ?: topts.retval;
+}
+
+/*
+ * Load a scheduler, move this task onto it and return the scheduler's id, after
+ * checking that the task reports the same owner and that both read 0 once the
+ * scheduler is unloaded.
+ */
+static enum scx_test_status load_and_check(u64 *id)
+{
+	enum scx_test_status status = SCX_TEST_FAIL;
+	struct sched_id *skel;
+	struct bpf_link *link;
+
+	skel = sched_id__open();
+	SCX_FAIL_IF(!skel, "Failed to open skel");
+	SCX_ENUM_INIT(skel);
+	SCX_FAIL_IF(sched_id__load(skel), "Failed to load skel");
+	skel->bss->target_pid = getpid();
+
+	link = bpf_map__attach_struct_ops(skel->maps.sched_id_ops);
+	SCX_FAIL_IF(!link, "Failed to attach struct_ops");
+
+	/* Step 1: The task on SCHED_EXT reports the scheduler as its owner */
+	if (set_policy(SCHED_EXT) || probe(skel)) {
+		SCX_ERR("Failed to switch to SCHED_EXT and probe");
+		goto out;
+	}
+	*id = skel->bss->probe_sched_id;
+	if (!*id || skel->bss->probe_task_sched_id != *id) {
+		SCX_ERR("sched_id=%llu task_sched_id=%llu",
+			(unsigned long long)*id,
+			(unsigned long long)skel->bss->probe_task_sched_id);
+		goto out;
+	}
+
+	/* Step 2: Both read 0 after the scheduler is unloaded */
+	bpf_link__destroy(link);
+	link = NULL;
+	if (probe(skel) || skel->bss->probe_sched_id ||
+	    skel->bss->probe_task_sched_id) {
+		SCX_ERR("After unload: sched_id=%llu task_sched_id=%llu",
+			(unsigned long long)skel->bss->probe_sched_id,
+			(unsigned long long)skel->bss->probe_task_sched_id);
+		goto out;
+	}
+
+	status = SCX_TEST_PASS;
+out:
+	set_policy(SCHED_OTHER);
+	bpf_link__destroy(link);
+	sched_id__destroy(skel);
+	return status;
+}
+
+static enum scx_test_status run(void *ctx)
+{
+	u64 id1 = 0, id2 = 0;
+
+	SCX_EQ(load_and_check(&id1), SCX_TEST_PASS);
+
+	/* Step 3: A new instance gets a larger id */
+	SCX_EQ(load_and_check(&id2), SCX_TEST_PASS);
+	SCX_GT(id2, id1);
+
+	return SCX_TEST_PASS;
+}
+
+struct scx_test sched_id = {
+	.name = "sched_id",
+	.description = "Verify scx_bpf_sched_id() and scx_bpf_task_sched_id()",
+	.run = run,
+};
+REGISTER_SCX_TEST(&sched_id)
-- 
2.56.1


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

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  5:48 [PATCH 0/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup Changwoo Min
2026-12-01  5:48 ` [PATCH 1/5] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id() Changwoo Min
2026-12-01  5:48 ` Changwoo Min [this message]
2026-12-01  5:48 ` [PATCH 3/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup Changwoo Min
2026-12-01  7:48   ` Andrea Righi
2026-12-01 11:04     ` Changwoo Min
2026-12-01  5:48 ` [PATCH 4/5] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_data Changwoo Min
2026-12-01  5:48 ` [PATCH 5/5] selftests/sched_ext: Test p->scx.bpf_data across class changes Changwoo Min

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=20261201054850.780664-3-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®