mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kuba Piecuch <jpiecuch@google.com>
To: Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
	Andrea Righi <arighi@nvidia.com>,
	 Changwoo Min <changwoo@igalia.com>
Cc: Kuba Piecuch <jpiecuch@google.com>,
	sched-ext@lists.linux.dev,  linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] selftests/sched_ext: Add unlocked_kfunc test
Date: Mon, 23 Nov 2026 13:55:25 +0000	[thread overview]
Message-ID: <20261123135343.4131146-3-jpiecuch@google.com> (raw)
In-Reply-To: <20261123135343.4131146-1-jpiecuch@google.com>

Add the unlocked_kfunc test to verify verifier-time restrictions on the
SCX_KF_ALLOW_UNLOCKED kfuncs.

The valid cpu-form and cid-form schedulers verify that ops.init() and
ops.exit() can call scx_bpf_create_dsq(), scx_bpf_dsq_move(),
scx_bpf_dsq_reenq_task(), and scx_bpf_select_cpu_dfl(). They also verify
that ops.dump() can call scx_bpf_dump() and that ops.init_cids() can
call scx_bpf_cid_override().

The two negative schedulers verify that the verifier rejects
scx_bpf_dsq_move() from ops.dump() and scx_bpf_create_dsq() from
ops.init_cids() with -EACCES.

Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
 tools/testing/selftests/sched_ext/Makefile    |  1 +
 .../selftests/sched_ext/unlocked_kfunc.bpf.c  | 69 ++++++++++++++++++
 .../selftests/sched_ext/unlocked_kfunc.c      | 73 +++++++++++++++++++
 .../sched_ext/unlocked_kfunc_cid.bpf.c        | 67 +++++++++++++++++
 .../sched_ext/unlocked_kfunc_fail_dump.bpf.c  | 26 +++++++
 .../unlocked_kfunc_fail_init_cids.bpf.c       | 22 ++++++
 6 files changed, 258 insertions(+)
 create mode 100644 tools/testing/selftests/sched_ext/unlocked_kfunc.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/unlocked_kfunc.c
 create mode 100644 tools/testing/selftests/sched_ext/unlocked_kfunc_cid.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/unlocked_kfunc_fail_dump.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/unlocked_kfunc_fail_init_cids.bpf.c

diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile
index c5d3a2eaea7d..9a776a8a36ff 100644
--- a/tools/testing/selftests/sched_ext/Makefile
+++ b/tools/testing/selftests/sched_ext/Makefile
@@ -199,6 +199,7 @@ auto-test-targets :=			\
 	rt_stall			\
 	test_example			\
 	total_bw			\
+	unlocked_kfunc			\
 	cyclic_kick_wait		\
 
 testcase-targets := $(addsuffix .o,$(addprefix $(SCXOBJ_DIR)/,$(auto-test-targets)))
diff --git a/tools/testing/selftests/sched_ext/unlocked_kfunc.bpf.c b/tools/testing/selftests/sched_ext/unlocked_kfunc.bpf.c
new file mode 100644
index 000000000000..194993a0ef07
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/unlocked_kfunc.bpf.c
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A cpu-form scheduler that calls SCX_KF_ALLOW_UNLOCKED kfuncs from ops.init()
+ * and ops.exit(). ops.dump() calls scx_bpf_dump().
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+
+#include <scx/common.bpf.h>
+
+#define SHARED_DSQ	0
+
+char _license[] SEC("license") = "GPL";
+
+static void drain_dsq(void)
+{
+	struct task_struct *p;
+
+	bpf_rcu_read_lock();
+	bpf_for_each(scx_dsq, p, SHARED_DSQ, 0) {
+		if (p->pid & 1)
+			scx_bpf_dsq_reenq_task(BPF_FOR_EACH_ITER, p);
+		else
+			scx_bpf_dsq_move(BPF_FOR_EACH_ITER, p, SCX_DSQ_GLOBAL, 0);
+	}
+	bpf_rcu_read_unlock();
+}
+
+static void select_cpu_for_current(void)
+{
+	struct task_struct *p = bpf_get_current_task_btf();
+	bool is_idle;
+
+	bpf_rcu_read_lock();
+	scx_bpf_select_cpu_dfl(p, bpf_get_smp_processor_id(), 0, &is_idle);
+	bpf_rcu_read_unlock();
+}
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(unlocked_kfunc_init)
+{
+	s32 ret;
+
+	ret = scx_bpf_create_dsq(SHARED_DSQ, -1);
+	if (ret)
+		return ret;
+
+	drain_dsq();
+	select_cpu_for_current();
+	return 0;
+}
+
+void BPF_STRUCT_OPS(unlocked_kfunc_exit, struct scx_exit_info *ei)
+{
+	drain_dsq();
+	select_cpu_for_current();
+}
+
+void BPF_STRUCT_OPS(unlocked_kfunc_dump, struct scx_dump_ctx *dctx)
+{
+	scx_bpf_dump("nr_cpu_ids=%u\n", scx_bpf_nr_cpu_ids());
+}
+
+SEC(".struct_ops.link")
+struct sched_ext_ops unlocked_kfunc_ops = {
+	.init			= (void *)unlocked_kfunc_init,
+	.exit			= (void *)unlocked_kfunc_exit,
+	.dump			= (void *)unlocked_kfunc_dump,
+	.name			= "unlocked_kfunc",
+};
diff --git a/tools/testing/selftests/sched_ext/unlocked_kfunc.c b/tools/testing/selftests/sched_ext/unlocked_kfunc.c
new file mode 100644
index 000000000000..0052c608365d
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/unlocked_kfunc.c
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verify that the BPF verifier rejects SCX_KF_ALLOW_UNLOCKED kfuncs from
+ * ops.dump() and ops.init_cids(). Unlocked ops such as ops.init() and
+ * ops.exit() must still be allowed to call them.
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+#include <bpf/bpf.h>
+#include <errno.h>
+#include <scx/common.h>
+#include "scx_test.h"
+#include "unlocked_kfunc.bpf.skel.h"
+#include "unlocked_kfunc_cid.bpf.skel.h"
+#include "unlocked_kfunc_fail_dump.bpf.skel.h"
+#include "unlocked_kfunc_fail_init_cids.bpf.skel.h"
+
+static enum scx_test_status run(void *ctx)
+{
+	struct unlocked_kfunc *skel;
+	struct unlocked_kfunc_cid *cid_skel;
+	struct unlocked_kfunc_fail_dump *fail_dump;
+	struct unlocked_kfunc_fail_init_cids *fail_init_cids;
+	int err;
+
+	if (!__COMPAT_struct_has_field("sched_ext_ops_cid", "init_cids") ||
+	    !__COMPAT_has_ksym("scx_bpf_dsq_reenq_task"))
+		return SCX_TEST_SKIP;
+
+	skel = unlocked_kfunc__open();
+	SCX_FAIL_IF(!skel, "Failed to open unlocked_kfunc");
+	SCX_ENUM_INIT(skel);
+	err = unlocked_kfunc__load(skel);
+	unlocked_kfunc__destroy(skel);
+	SCX_FAIL_IF(err, "Failed to load unlocked_kfunc (%d)", err);
+
+	cid_skel = unlocked_kfunc_cid__open();
+	SCX_FAIL_IF(!cid_skel, "Failed to open unlocked_kfunc_cid");
+	SCX_ENUM_INIT(cid_skel);
+	err = unlocked_kfunc_cid__load(cid_skel);
+	unlocked_kfunc_cid__destroy(cid_skel);
+	SCX_FAIL_IF(err, "Failed to load unlocked_kfunc_cid (%d)", err);
+
+	fail_dump = unlocked_kfunc_fail_dump__open();
+	SCX_FAIL_IF(!fail_dump, "Failed to open unlocked_kfunc_fail_dump");
+	SCX_ENUM_INIT(fail_dump);
+	err = unlocked_kfunc_fail_dump__load(fail_dump);
+	unlocked_kfunc_fail_dump__destroy(fail_dump);
+	SCX_FAIL_IF(!err,
+		    "unlocked_kfunc_fail_dump loaded when it should have been rejected");
+	SCX_FAIL_IF(err != -EACCES,
+		    "Expected -EACCES loading unlocked_kfunc_fail_dump, got %d", err);
+
+	fail_init_cids = unlocked_kfunc_fail_init_cids__open();
+	SCX_FAIL_IF(!fail_init_cids, "Failed to open unlocked_kfunc_fail_init_cids");
+	SCX_ENUM_INIT(fail_init_cids);
+	err = unlocked_kfunc_fail_init_cids__load(fail_init_cids);
+	unlocked_kfunc_fail_init_cids__destroy(fail_init_cids);
+	SCX_FAIL_IF(!err,
+		    "unlocked_kfunc_fail_init_cids loaded when it should have been rejected");
+	SCX_FAIL_IF(err != -EACCES,
+		    "Expected -EACCES loading unlocked_kfunc_fail_init_cids, got %d", err);
+
+	return SCX_TEST_PASS;
+}
+
+struct scx_test unlocked_kfunc = {
+	.name = "unlocked_kfunc",
+	.description = "Verify verifier kfunc restrictions on ops.dump() and ops.init_cids()",
+	.run = run,
+};
+
+REGISTER_SCX_TEST(&unlocked_kfunc)
diff --git a/tools/testing/selftests/sched_ext/unlocked_kfunc_cid.bpf.c b/tools/testing/selftests/sched_ext/unlocked_kfunc_cid.bpf.c
new file mode 100644
index 000000000000..ffe2f03c42e3
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/unlocked_kfunc_cid.bpf.c
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A cid-form scheduler that calls scx_bpf_cid_override() from ops.init_cids()
+ * and SCX_KF_ALLOW_UNLOCKED kfuncs from ops.init(). ops.dump() calls
+ * scx_bpf_dump().
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+
+#include <scx/common.bpf.h>
+
+#define SHARED_DSQ	0
+#define MAX_CPUS	512
+
+char _license[] SEC("license") = "GPL";
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARENA);
+	__uint(map_flags, BPF_F_MMAPABLE);
+	__uint(max_entries, 1 << 4);
+} arena SEC(".maps");
+
+s32 __arena_global cpu_to_cid[MAX_CPUS];
+s32 __arena_global shard_start[MAX_CPUS];
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(unlocked_kfunc_cid_init_cids)
+{
+	u32 nr_cpu_ids = scx_bpf_nr_cpu_ids();
+
+	if (nr_cpu_ids > MAX_CPUS)
+		return -E2BIG;
+
+	scx_bpf_cid_override(cpu_to_cid, nr_cpu_ids, shard_start, 1);
+	return 0;
+}
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(unlocked_kfunc_cid_init)
+{
+	struct task_struct *p;
+	s32 ret;
+
+	ret = scx_bpf_create_dsq(SHARED_DSQ, -1);
+	if (ret)
+		return ret;
+
+	bpf_rcu_read_lock();
+	bpf_for_each(scx_dsq, p, SHARED_DSQ, 0) {
+		if (p->pid & 1)
+			scx_bpf_dsq_reenq_task(BPF_FOR_EACH_ITER, p);
+		else
+			scx_bpf_dsq_move(BPF_FOR_EACH_ITER, p, SCX_DSQ_GLOBAL, 0);
+	}
+	bpf_rcu_read_unlock();
+
+	return 0;
+}
+
+void BPF_STRUCT_OPS(unlocked_kfunc_cid_dump, struct scx_dump_ctx *dctx)
+{
+	scx_bpf_dump("nr_cids=%u\n", scx_bpf_nr_cids());
+}
+
+SCX_OPS_CID_DEFINE(unlocked_kfunc_cid_ops,
+		   .init_cids		= (void *)unlocked_kfunc_cid_init_cids,
+		   .init		= (void *)unlocked_kfunc_cid_init,
+		   .dump		= (void *)unlocked_kfunc_cid_dump,
+		   .name		= "unlocked_kfunc_cid");
diff --git a/tools/testing/selftests/sched_ext/unlocked_kfunc_fail_dump.bpf.c b/tools/testing/selftests/sched_ext/unlocked_kfunc_fail_dump.bpf.c
new file mode 100644
index 000000000000..70218f48e93a
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/unlocked_kfunc_fail_dump.bpf.c
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verify that the BPF verifier rejects scx_bpf_dsq_move() from ops.dump().
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+
+#include <scx/common.bpf.h>
+
+#define SHARED_DSQ	0
+
+char _license[] SEC("license") = "GPL";
+
+void BPF_STRUCT_OPS(unlocked_kfunc_fail_dump_cb, struct scx_dump_ctx *dctx)
+{
+	struct task_struct *p;
+
+	bpf_for_each(scx_dsq, p, SHARED_DSQ, 0)
+		scx_bpf_dsq_move(BPF_FOR_EACH_ITER, p, SCX_DSQ_GLOBAL, 0);
+}
+
+SEC(".struct_ops.link")
+struct sched_ext_ops unlocked_kfunc_fail_dump_ops = {
+	.dump			= (void *)unlocked_kfunc_fail_dump_cb,
+	.name			= "unlocked_kfunc_fail_dump",
+};
diff --git a/tools/testing/selftests/sched_ext/unlocked_kfunc_fail_init_cids.bpf.c b/tools/testing/selftests/sched_ext/unlocked_kfunc_fail_init_cids.bpf.c
new file mode 100644
index 000000000000..2f2d277aa54f
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/unlocked_kfunc_fail_init_cids.bpf.c
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verify that the BPF verifier rejects scx_bpf_create_dsq() from
+ * ops.init_cids().
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+
+#include <scx/common.bpf.h>
+
+#define SHARED_DSQ	0
+
+char _license[] SEC("license") = "GPL";
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(unlocked_kfunc_fail_init_cids_cb)
+{
+	return scx_bpf_create_dsq(SHARED_DSQ, -1);
+}
+
+SCX_OPS_CID_DEFINE(unlocked_kfunc_fail_init_cids_ops,
+		   .init_cids		= (void *)unlocked_kfunc_fail_init_cids_cb,
+		   .name		= "unlocked_kfunc_fail_init_cids");
-- 
2.56.1.750.gf70dc682cf-goog


      parent reply	other threads:[~2026-11-23 13:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 13:55 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Don't allow unlocked kfuncs from ops.dump() and ops.init_cids() Kuba Piecuch
2026-11-23 13:55 ` [PATCH 1/2] " Kuba Piecuch
2026-11-23 13:55 ` 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=20261123135343.4131146-3-jpiecuch@google.com \
    --to=jpiecuch@google.com \
    --cc=arighi@nvidia.com \
    --cc=changwoo@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®