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 sub_caps_kfunc test
Date: Mon, 30 Nov 2026 15:53:16 +0000	[thread overview]
Message-ID: <20261130155148.2948874-3-jpiecuch@google.com> (raw)
In-Reply-To: <20261130155148.2948874-1-jpiecuch@google.com>

Add the sub_caps_kfunc selftest to verify BPF verifier restrictions on
ops.sub_caps_updated().

A positive cid-form scheduler verifies that ops.sub_caps_updated() can
still call sub-scheduler cap and context-free kfuncs (scx_bpf_sub_caps(),
scx_bpf_sub_grant(), scx_bpf_sub_revoke(), scx_bpf_sub_kill(),
scx_bpf_nr_cids(), scx_bpf_kick_cid(), scx_bpf_dsq_peek(),
scx_bpf_task_set_slice(), and scx_bpf_destroy_dsq()) and that
ops.sub_ecaps_updated() can still call scx_bpf_cidperf_set().

Seven negative test cases verify that the verifier rejects
scx_bpf_cidperf_set(), scx_bpf_locked_rq(), scx_bpf_dsq_nr_queued(),
scx_bpf_dsq_reenq(), scx_bpf_reenqueue_local___v2(),
bpf_for_each(scx_dsq, ...), and scx_bpf_dump() from ops.sub_caps_updated()
with -EACCES.

Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/sub_caps_kfunc.bpf.c  |  66 +++++++++++
 .../selftests/sched_ext/sub_caps_kfunc.c      | 107 +++++++++++++++++
 .../sched_ext/sub_caps_kfunc_fail.bpf.c       | 109 ++++++++++++++++++
 4 files changed, 283 insertions(+)
 create mode 100644 tools/testing/selftests/sched_ext/sub_caps_kfunc.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/sub_caps_kfunc.c
 create mode 100644 tools/testing/selftests/sched_ext/sub_caps_kfunc_fail.bpf.c

diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile
index fac8b26c4d70..47dafa4c08c4 100644
--- a/tools/testing/selftests/sched_ext/Makefile
+++ b/tools/testing/selftests/sched_ext/Makefile
@@ -197,6 +197,7 @@ auto-test-targets :=			\
 	select_cpu_dispatch_dbl_dsp	\
 	select_cpu_vtime		\
 	rt_stall			\
+	sub_caps_kfunc			\
 	sub_core_caps			\
 	test_example			\
 	total_bw			\
diff --git a/tools/testing/selftests/sched_ext/sub_caps_kfunc.bpf.c b/tools/testing/selftests/sched_ext/sub_caps_kfunc.bpf.c
new file mode 100644
index 000000000000..05c590c63738
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sub_caps_kfunc.bpf.c
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A cid-form scheduler that calls allowed kfuncs from ops.sub_caps_updated()
+ * and calls scx_bpf_cidperf_set() from ops.sub_ecaps_updated().
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+
+#include <scx/common.bpf.h>
+
+#define SHARED_DSQ	0
+#define MAX_CIDS	1024
+
+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");
+
+struct sub_caps_kfunc_cmask {
+	union {
+		struct scx_cmask mask;
+		u64 words[CMASK_NR_WORDS(MAX_CIDS) + 2];
+	};
+};
+
+struct sub_caps_kfunc_cmask __arena_global caps_mask;
+
+void BPF_STRUCT_OPS(sub_caps_kfunc_sub_caps_updated, const struct scx_cmask *cmask, u64 caps)
+{
+	struct task_struct *p;
+
+	/* @cmask is an arena pointer, associate the arena */
+	asm volatile("" :: "r"(&arena));
+
+	if (scx_bpf_nr_cids() > MAX_CIDS) {
+		scx_bpf_error("too many cids");
+		return;
+	}
+
+	scx_bpf_sub_caps(0, SCX_CAP_ENQ, &caps_mask.mask);
+	scx_bpf_sub_grant(0, SCX_CAP_ENQ, &caps_mask.mask, NULL);
+	scx_bpf_sub_revoke(0, SCX_CAP_ENQ, &caps_mask.mask);
+	scx_bpf_sub_kill(0, "sub_caps_kfunc");
+	scx_bpf_kick_cid(0, 0);
+
+	bpf_rcu_read_lock();
+	p = scx_bpf_dsq_peek(SHARED_DSQ);
+	if (p)
+		scx_bpf_task_set_slice(p, SCX_SLICE_DFL);
+	bpf_rcu_read_unlock();
+
+	scx_bpf_destroy_dsq(SHARED_DSQ);
+}
+
+void BPF_STRUCT_OPS(sub_caps_kfunc_sub_ecaps_updated, s32 cid, u64 before, u64 after)
+{
+	scx_bpf_cidperf_set(cid, SCX_CPUPERF_ONE);
+}
+
+SCX_OPS_CID_DEFINE(sub_caps_kfunc_ops,
+		   .sub_caps_updated	= (void *)sub_caps_kfunc_sub_caps_updated,
+		   .sub_ecaps_updated	= (void *)sub_caps_kfunc_sub_ecaps_updated,
+		   .name		= "sub_caps_kfunc");
diff --git a/tools/testing/selftests/sched_ext/sub_caps_kfunc.c b/tools/testing/selftests/sched_ext/sub_caps_kfunc.c
new file mode 100644
index 000000000000..c689538d8594
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sub_caps_kfunc.c
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verify that the BPF verifier rejects rq-lock-sensitive and dump kfuncs from
+ * ops.sub_caps_updated(), while still allowing sub-scheduler cap propagation
+ * and context-free kfuncs there (and scx_bpf_cidperf_set() from
+ * ops.sub_ecaps_updated()).
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+#include <bpf/bpf.h>
+#include <errno.h>
+#include <scx/common.h>
+#include "scx_test.h"
+
+/* opaque to userspace, must match the size in sub_caps_kfunc.bpf.c */
+#define MAX_CIDS	1024
+struct sub_caps_kfunc_cmask {
+	__u64 words[(MAX_CIDS + 63) / 64 + 1 + 2];
+};
+
+#include "sub_caps_kfunc.bpf.skel.h"
+#include "sub_caps_kfunc_fail.bpf.skel.h"
+
+struct fail_case {
+	const char *name;
+	size_t map_off;
+	size_t prog_off;
+};
+
+#define FAIL_CASE(id) {							\
+	.name = #id,							\
+	.map_off = offsetof(struct sub_caps_kfunc_fail, maps.id##_ops),	\
+	.prog_off = offsetof(struct sub_caps_kfunc_fail, progs.id##_cb), \
+}
+
+static const struct fail_case fail_cases[] = {
+	FAIL_CASE(fail_cidperf),
+	FAIL_CASE(fail_locked_rq),
+	FAIL_CASE(fail_dsq_nr_queued),
+	FAIL_CASE(fail_dsq_reenq),
+	FAIL_CASE(fail_reenq_local),
+	FAIL_CASE(fail_dsq_iter),
+	FAIL_CASE(fail_dump),
+	{},
+};
+
+static struct bpf_map *case_map(struct sub_caps_kfunc_fail *skel, size_t off)
+{
+	return *(struct bpf_map **)((char *)skel + off);
+}
+
+static struct bpf_program *case_prog(struct sub_caps_kfunc_fail *skel, size_t off)
+{
+	return *(struct bpf_program **)((char *)skel + off);
+}
+
+static enum scx_test_status run(void *ctx)
+{
+	struct sub_caps_kfunc *skel;
+	int err, i, j;
+
+	if (!__COMPAT_struct_has_field("sched_ext_ops_cid", "sub_caps_updated") ||
+	    !__COMPAT_has_ksym("scx_bpf_sub_grant"))
+		return SCX_TEST_SKIP;
+
+	skel = sub_caps_kfunc__open();
+	SCX_FAIL_IF(!skel, "Failed to open sub_caps_kfunc");
+	SCX_ENUM_INIT(skel);
+	err = sub_caps_kfunc__load(skel);
+	sub_caps_kfunc__destroy(skel);
+	SCX_FAIL_IF(err, "Failed to load sub_caps_kfunc (%d)", err);
+
+	for (i = 0; fail_cases[i].name; i++) {
+		struct sub_caps_kfunc_fail *fail_skel;
+
+		fail_skel = sub_caps_kfunc_fail__open();
+		SCX_FAIL_IF(!fail_skel, "Failed to open %s", fail_cases[i].name);
+		SCX_ENUM_INIT(fail_skel);
+
+		for (j = 0; fail_cases[j].name; j++) {
+			if (j == i)
+				continue;
+			bpf_map__set_autocreate(case_map(fail_skel, fail_cases[j].map_off),
+						false);
+			bpf_program__set_autoload(case_prog(fail_skel, fail_cases[j].prog_off),
+						  false);
+		}
+
+		err = sub_caps_kfunc_fail__load(fail_skel);
+		sub_caps_kfunc_fail__destroy(fail_skel);
+		SCX_FAIL_IF(!err, "%s loaded when it should have been rejected",
+			    fail_cases[i].name);
+		SCX_FAIL_IF(err != -EACCES,
+			    "Expected -EACCES loading %s, got %d",
+			    fail_cases[i].name, err);
+	}
+
+	return SCX_TEST_PASS;
+}
+
+struct scx_test sub_caps_kfunc = {
+	.name = "sub_caps_kfunc",
+	.description = "Verify verifier kfunc restrictions on ops.sub_caps_updated()",
+	.run = run,
+};
+
+REGISTER_SCX_TEST(&sub_caps_kfunc)
diff --git a/tools/testing/selftests/sched_ext/sub_caps_kfunc_fail.bpf.c b/tools/testing/selftests/sched_ext/sub_caps_kfunc_fail.bpf.c
new file mode 100644
index 000000000000..c33e030c62df
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sub_caps_kfunc_fail.bpf.c
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verify that the BPF verifier rejects rq-lock-sensitive and dump kfuncs from
+ * ops.sub_caps_updated().
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+
+#include <scx/common.bpf.h>
+#include <scx/compat.bpf.h>
+
+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");
+
+static __always_inline void assoc_arena(void)
+{
+	/*
+	 * ops.sub_caps_updated() takes an arena pointer (@cmask), so
+	 * check_attach_btf_id() rejects the program with -EINVAL before
+	 * scx_kfunc_context_filter() runs unless an arena map is associated.
+	 * Reference the arena so verification reaches the kfunc context check
+	 * and fails with -EACCES.
+	 */
+	asm volatile("" :: "r"(&arena));
+}
+
+void BPF_STRUCT_OPS(fail_cidperf_cb, const struct scx_cmask *cmask, u64 caps)
+{
+	assoc_arena();
+	scx_bpf_cidperf_set(0, SCX_CPUPERF_ONE);
+}
+
+SCX_OPS_CID_DEFINE(fail_cidperf_ops,
+		   .sub_caps_updated	= (void *)fail_cidperf_cb,
+		   .name		= "sub_caps_fail_cidperf");
+
+void BPF_STRUCT_OPS(fail_locked_rq_cb, const struct scx_cmask *cmask, u64 caps)
+{
+	assoc_arena();
+	if (scx_bpf_locked_rq())
+		bpf_printk("locked rq\n");
+}
+
+SCX_OPS_CID_DEFINE(fail_locked_rq_ops,
+		   .sub_caps_updated	= (void *)fail_locked_rq_cb,
+		   .name		= "sub_caps_fail_locked_rq");
+
+void BPF_STRUCT_OPS(fail_dsq_nr_queued_cb, const struct scx_cmask *cmask, u64 caps)
+{
+	assoc_arena();
+	if (scx_bpf_dsq_nr_queued(SCX_DSQ_LOCAL) > 0)
+		bpf_printk("queued\n");
+}
+
+SCX_OPS_CID_DEFINE(fail_dsq_nr_queued_ops,
+		   .sub_caps_updated	= (void *)fail_dsq_nr_queued_cb,
+		   .name		= "sub_caps_fail_dsq_nr_queued");
+
+void BPF_STRUCT_OPS(fail_dsq_reenq_cb, const struct scx_cmask *cmask, u64 caps)
+{
+	assoc_arena();
+	scx_bpf_dsq_reenq(SCX_DSQ_LOCAL, 0);
+}
+
+SCX_OPS_CID_DEFINE(fail_dsq_reenq_ops,
+		   .sub_caps_updated	= (void *)fail_dsq_reenq_cb,
+		   .name		= "sub_caps_fail_dsq_reenq");
+
+void BPF_STRUCT_OPS(fail_reenq_local_cb, const struct scx_cmask *cmask, u64 caps)
+{
+	assoc_arena();
+	scx_bpf_reenqueue_local___v2___compat();
+}
+
+SCX_OPS_CID_DEFINE(fail_reenq_local_ops,
+		   .sub_caps_updated	= (void *)fail_reenq_local_cb,
+		   .name		= "sub_caps_fail_reenq_local");
+
+void BPF_STRUCT_OPS(fail_dsq_iter_cb, const struct scx_cmask *cmask, u64 caps)
+{
+	struct task_struct *p;
+
+	assoc_arena();
+	bpf_rcu_read_lock();
+	bpf_for_each(scx_dsq, p, 0, 0) {
+		if (p)
+			break;
+	}
+	bpf_rcu_read_unlock();
+}
+
+SCX_OPS_CID_DEFINE(fail_dsq_iter_ops,
+		   .sub_caps_updated	= (void *)fail_dsq_iter_cb,
+		   .name		= "sub_caps_fail_dsq_iter");
+
+void BPF_STRUCT_OPS(fail_dump_cb, const struct scx_cmask *cmask, u64 caps)
+{
+	assoc_arena();
+	scx_bpf_dump("caps=0x%llx\n", caps);
+}
+
+SCX_OPS_CID_DEFINE(fail_dump_ops,
+		   .sub_caps_updated	= (void *)fail_dump_cb,
+		   .name		= "sub_caps_fail_dump");
-- 
2.56.1.839.g22a8290db9-goog


  parent reply	other threads:[~2026-11-30 15:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 15:53 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Reject rq-lock-sensitive kfuncs from ops.sub_caps_updated() Kuba Piecuch
2026-11-30 15:53 ` [PATCH 1/2] " Kuba Piecuch
2026-11-30 15:53 ` Kuba Piecuch [this message]
2026-11-30 16:59 ` [PATCHSET sched_ext/for-7.4-fixes] " Tejun Heo

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=20261130155148.2948874-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®