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: [RFC PATCH 2/2] selftests/sched_ext: Test that ops.sub_caps_updated() runs unlocked
Date: Wed, 25 Nov 2026 15:34:21 +0000 [thread overview]
Message-ID: <20261125153353.1364467-3-jpiecuch@google.com> (raw)
In-Reply-To: <20261125153353.1364467-1-jpiecuch@google.com>
Add sub_caps_ctx. A cid-form root grants and revokes caps to a child
sub-scheduler from ops.enqueue() (SCX_CAP_PERF) and ops.select_cid()
(SCX_CAP_PREEMPT). The child calls scx_bpf_cidperf_set() on every cid from
ops.sub_caps_updated(), which aborts the child if the callback inherits
the parent's rq lock. The test is skipped on a single cpu, where the only
cid is the parent's rq.
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
tools/testing/selftests/sched_ext/Makefile | 1 +
.../selftests/sched_ext/sub_caps_ctx.bpf.c | 136 +++++++++++++++
.../selftests/sched_ext/sub_caps_ctx.c | 165 ++++++++++++++++++
3 files changed, 302 insertions(+)
create mode 100644 tools/testing/selftests/sched_ext/sub_caps_ctx.bpf.c
create mode 100644 tools/testing/selftests/sched_ext/sub_caps_ctx.c
diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile
index 9a776a8a36ff..caedba9dcde9 100644
--- a/tools/testing/selftests/sched_ext/Makefile
+++ b/tools/testing/selftests/sched_ext/Makefile
@@ -196,6 +196,7 @@ auto-test-targets := \
select_cpu_dispatch_bad_dsq \
select_cpu_dispatch_dbl_dsp \
select_cpu_vtime \
+ sub_caps_ctx \
rt_stall \
test_example \
total_bw \
diff --git a/tools/testing/selftests/sched_ext/sub_caps_ctx.bpf.c b/tools/testing/selftests/sched_ext/sub_caps_ctx.bpf.c
new file mode 100644
index 000000000000..e6613fc23d15
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sub_caps_ctx.bpf.c
@@ -0,0 +1,136 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A cid-form root scheduler which grants and revokes caps to a child
+ * sub-scheduler from its rq-locked ops.enqueue() and from ops.select_cid(), and
+ * a child which calls scx_bpf_cidperf_set() on every cid from
+ * ops.sub_caps_updated().
+ *
+ * ops.sub_caps_updated() must not inherit the parent's rq lock context. If it
+ * did, scx_bpf_cidperf_set() on any cid other than the parent's locked rq's
+ * would fail with "Invalid target CPU" and abort the child.
+ */
+#include <scx/common.bpf.h>
+
+char _license[] SEC("license") = "GPL";
+
+#define MAX_CIDS 1024
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARENA);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, 1 << 8);
+#if defined(__TARGET_ARCH_arm64) || defined(__aarch64__)
+ __ulong(map_extra, 0x1ull << 32);
+#else
+ __ulong(map_extra, 0x1ull << 44);
+#endif
+} arena SEC(".maps");
+
+struct sub_caps_ctx_cmask {
+ union {
+ struct scx_cmask mask;
+ u64 words[CMASK_NR_WORDS(MAX_CIDS) + 2];
+ };
+};
+
+struct sub_caps_ctx_cmask __arena_global all_cids;
+
+u64 child_cgid;
+u64 nr_enqueued, nr_selected, nr_grants, nr_revokes, nr_caps_updated, nr_perf_set;
+
+UEI_DEFINE(uei);
+
+/*
+ * Toggle @cap on all cids of the child every 8th call. ops.select_cid() and
+ * ops.enqueue() toggle different caps so that neither turns the other's grant
+ * or revoke into a no-op.
+ */
+static void toggle_child_cap(u64 cap, u64 seq)
+{
+ u64 cgid = child_cgid;
+
+ if (!cgid || (seq & 7) || nr_grants + nr_revokes >= 4096)
+ return;
+
+ if ((seq >> 3) & 1) {
+ scx_bpf_sub_revoke(cgid, cap, &all_cids.mask);
+ __sync_fetch_and_add(&nr_revokes, 1);
+ } else {
+ scx_bpf_sub_grant(cgid, cap, &all_cids.mask, NULL);
+ __sync_fetch_and_add(&nr_grants, 1);
+ }
+}
+
+/* grant / revoke from the ops.select_cid() context */
+s32 BPF_STRUCT_OPS(sub_caps_ctx_select_cid, struct task_struct *p, s32 prev_cid, u64 wake_flags)
+{
+ toggle_child_cap(SCX_CAP_PREEMPT, __sync_fetch_and_add(&nr_selected, 1));
+ return prev_cid;
+}
+
+/* grant / revoke while holding the enqueue rq lock */
+void BPF_STRUCT_OPS(sub_caps_ctx_enqueue, struct task_struct *p, u64 enq_flags)
+{
+ scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);
+ toggle_child_cap(SCX_CAP_PERF, __sync_fetch_and_add(&nr_enqueued, 1));
+}
+
+void BPF_STRUCT_OPS(sub_caps_ctx_sub_caps_updated, const struct scx_cmask *cmask, u64 caps)
+{
+ u32 nr_cids = scx_bpf_nr_cids();
+ s32 cid;
+
+ /* @cmask is an arena pointer, associate the arena */
+ asm volatile("" :: "r"(&arena));
+
+ __sync_fetch_and_add(&nr_caps_updated, 1);
+
+ /* any cid must be accepted when no rq lock is held */
+ bpf_for(cid, 0, nr_cids) {
+ if (!scx_bpf_cidperf_set(cid, SCX_CPUPERF_ONE))
+ __sync_fetch_and_add(&nr_perf_set, 1);
+ }
+}
+
+s32 BPF_STRUCT_OPS(sub_caps_ctx_sub_attach, struct scx_sub_attach_args *args)
+{
+ child_cgid = args->ops->sub_cgroup_id;
+ return 0;
+}
+
+void BPF_STRUCT_OPS(sub_caps_ctx_sub_detach, struct scx_sub_detach_args *args)
+{
+ child_cgid = 0;
+}
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(sub_caps_ctx_init)
+{
+ u32 nr_cids = scx_bpf_nr_cids();
+ s32 cid;
+
+ if (nr_cids > MAX_CIDS) {
+ scx_bpf_error("nr_cids=%u exceeds MAX_CIDS=%d", nr_cids, MAX_CIDS);
+ return -EINVAL;
+ }
+
+ cmask_init(&all_cids.mask, 0, nr_cids);
+ bpf_for(cid, 0, nr_cids)
+ cmask_set(cid, &all_cids.mask);
+
+ return 0;
+}
+
+void BPF_STRUCT_OPS(sub_caps_ctx_exit, struct scx_exit_info *ei)
+{
+ UEI_RECORD(uei, ei);
+}
+
+SCX_OPS_CID_DEFINE(sub_caps_ctx_ops,
+ .select_cid = (void *)sub_caps_ctx_select_cid,
+ .enqueue = (void *)sub_caps_ctx_enqueue,
+ .sub_caps_updated = (void *)sub_caps_ctx_sub_caps_updated,
+ .sub_attach = (void *)sub_caps_ctx_sub_attach,
+ .sub_detach = (void *)sub_caps_ctx_sub_detach,
+ .init = (void *)sub_caps_ctx_init,
+ .exit = (void *)sub_caps_ctx_exit,
+ .name = "sub_caps_ctx");
diff --git a/tools/testing/selftests/sched_ext/sub_caps_ctx.c b/tools/testing/selftests/sched_ext/sub_caps_ctx.c
new file mode 100644
index 000000000000..5881a6312cb6
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sub_caps_ctx.c
@@ -0,0 +1,165 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Grant and revoke caps to a sub-scheduler from the parent's rq-locked
+ * ops.enqueue() and verify that the child's ops.sub_caps_updated() runs in an
+ * unlocked context.
+ */
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <bpf/bpf.h>
+#include <scx/common.h>
+
+/* opaque to userspace, must match the size in sub_caps_ctx.bpf.c */
+#define MAX_CIDS 1024
+struct sub_caps_ctx_cmask {
+ __u64 words[(MAX_CIDS + 63) / 64 + 1 + 2];
+};
+
+#include "sub_caps_ctx.bpf.skel.h"
+#include "scx_test.h"
+
+#define CGRP_ROOT "/sys/fs/cgroup"
+#define CGRP_PATH CGRP_ROOT "/sub_caps_ctx"
+#define NR_WORKERS 8
+
+struct ctx {
+ struct sub_caps_ctx *parent;
+ struct sub_caps_ctx *child;
+ struct bpf_link *parent_link;
+ struct bpf_link *child_link;
+};
+
+static struct ctx test_ctx;
+
+static void write_file(const char *path, const char *buf)
+{
+ FILE *f = fopen(path, "w");
+
+ if (!f)
+ return;
+ fputs(buf, f);
+ fclose(f);
+}
+
+static enum scx_test_status setup(void **ctx)
+{
+ struct sub_caps_ctx *skel;
+ struct stat st;
+
+ if (!__COMPAT_struct_has_field("sched_ext_ops_cid", "sub_caps_updated"))
+ return SCX_TEST_SKIP;
+
+ /* a single cid is always the parent's rq, which scx_bpf_cidperf_set() accepts */
+ if (sysconf(_SC_NPROCESSORS_ONLN) < 2)
+ return SCX_TEST_SKIP;
+
+ write_file(CGRP_ROOT "/cgroup.subtree_control", "+cpu");
+ if (mkdir(CGRP_PATH, 0755) && errno != EEXIST) {
+ fprintf(stderr, "Failed to create %s (%s)\n", CGRP_PATH, strerror(errno));
+ return SCX_TEST_SKIP;
+ }
+ SCX_FAIL_IF(stat(CGRP_PATH, &st), "Failed to stat %s", CGRP_PATH);
+
+ skel = SCX_OPS_CID_OPEN(sub_caps_ctx_ops, sub_caps_ctx);
+ SCX_OPS_LOAD(skel, sub_caps_ctx_ops, sub_caps_ctx, uei);
+ test_ctx.parent = skel;
+
+ skel = SCX_OPS_CID_OPEN(sub_caps_ctx_ops, sub_caps_ctx);
+ skel->struct_ops.sub_caps_ctx_ops->sub_cgroup_id = st.st_ino;
+ SCX_OPS_LOAD(skel, sub_caps_ctx_ops, sub_caps_ctx, uei);
+ test_ctx.child = skel;
+
+ *ctx = &test_ctx;
+ return SCX_TEST_PASS;
+}
+
+/* sleep and wake repeatedly to drive the parent's ops.enqueue() */
+static void worker(void)
+{
+ struct timespec ts = { .tv_nsec = 100 * 1000 };
+ int i;
+
+ for (i = 0; i < 2000; i++)
+ nanosleep(&ts, NULL);
+ exit(0);
+}
+
+static enum scx_test_status run(void *arg)
+{
+ struct ctx *ctx = arg;
+ pid_t pids[NR_WORKERS];
+ int i, status;
+
+ ctx->parent_link = bpf_map__attach_struct_ops(ctx->parent->maps.sub_caps_ctx_ops);
+ SCX_FAIL_IF(!ctx->parent_link, "Failed to attach parent");
+
+ ctx->child_link = bpf_map__attach_struct_ops(ctx->child->maps.sub_caps_ctx_ops);
+ SCX_FAIL_IF(!ctx->child_link, "Failed to attach child");
+ SCX_GT(ctx->parent->bss->child_cgid, 0);
+
+ for (i = 0; i < NR_WORKERS; i++) {
+ pids[i] = fork();
+ SCX_FAIL_IF(pids[i] < 0, "Failed to fork");
+ if (!pids[i])
+ worker();
+ }
+ for (i = 0; i < NR_WORKERS; i++)
+ SCX_FAIL_IF(waitpid(pids[i], &status, 0) != pids[i] || status,
+ "Worker %d failed", i);
+
+ /* let deferred notifications drain */
+ usleep(100 * 1000);
+
+ printf("enqueued=%lu selected=%lu grants=%lu revokes=%lu caps_updated=%lu perf_set=%lu\n",
+ (unsigned long)ctx->parent->bss->nr_enqueued,
+ (unsigned long)ctx->parent->bss->nr_selected,
+ (unsigned long)ctx->parent->bss->nr_grants,
+ (unsigned long)ctx->parent->bss->nr_revokes,
+ (unsigned long)ctx->child->bss->nr_caps_updated,
+ (unsigned long)ctx->child->bss->nr_perf_set);
+
+ SCX_ASSERT_ALIVE(ctx->child, uei);
+ SCX_ASSERT_ALIVE(ctx->parent, uei);
+ SCX_GT(ctx->parent->bss->nr_grants, 0);
+ SCX_GT(ctx->parent->bss->nr_revokes, 0);
+ SCX_GT(ctx->child->bss->nr_caps_updated, 0);
+
+ bpf_link__destroy(ctx->child_link);
+ ctx->child_link = NULL;
+ bpf_link__destroy(ctx->parent_link);
+ ctx->parent_link = NULL;
+
+ SCX_EQ(ctx->child->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG));
+ SCX_EQ(ctx->parent->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG));
+
+ return SCX_TEST_PASS;
+}
+
+static void cleanup(void *arg)
+{
+ struct ctx *ctx = arg;
+
+ if (ctx->child_link)
+ bpf_link__destroy(ctx->child_link);
+ if (ctx->parent_link)
+ bpf_link__destroy(ctx->parent_link);
+ sub_caps_ctx__destroy(ctx->child);
+ sub_caps_ctx__destroy(ctx->parent);
+ rmdir(CGRP_PATH);
+}
+
+struct scx_test sub_caps_ctx = {
+ .name = "sub_caps_ctx",
+ .description = "Check ops.sub_caps_updated() runs unlocked after a locked grant",
+ .setup = setup,
+ .run = run,
+ .cleanup = cleanup,
+};
+
+REGISTER_SCX_TEST(&sub_caps_ctx)
--
2.56.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-11-25 15:34 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 15:34 [RFC PATCHSET sched_ext/for-7.4-fixes] sched_ext: sub: Don't run ops.sub_caps_updated() in an SCX op context Kuba Piecuch
2026-11-25 15:34 ` [RFC PATCH 1/2] sched_ext: sub: Defer ops.sub_caps_updated() out of SCX op contexts Kuba Piecuch
2026-11-25 15:34 ` Kuba Piecuch [this message]
2026-11-25 16:06 ` [RFC PATCH 2/2] selftests/sched_ext: Test that ops.sub_caps_updated() runs unlocked sashiko-bot
2026-11-27 15:43 ` [RFC PATCHSET sched_ext/for-7.4-fixes] sched_ext: sub: Don't run ops.sub_caps_updated() in an SCX op context Tejun Heo
2026-11-30 12:53 ` Kuba Piecuch
2026-11-30 14:39 ` Tejun Heo
2026-11-30 15:54 ` Kuba Piecuch
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=20261125153353.1364467-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.