From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796054002; b=tIGl+dG+g19T5KKqSvUxSeKkS2K11J5gfjZACP2bXtDSZqjcKwa+7XEOMTODtFoOWqqq PxxKIQZem2rT9rXA9egUIvEE2shh26x+pm/NRf6Y9iYT9sFOp3A8I1QOO2AT+t2PR5qZ1 H0XS/YqmS+ZmKBICO08R2ASyKT6EcRBAMjOp/Y+aH+/TENycgJKxzxT1jkbnso6JCbVbX QF3HYq/loPi1xNupXgmw421+yBHLnNjiPYgkE/FieZM2xqtrffpeQlxH2LYriBOiYUT21 MFfr0JIH6F2ELLmgEQ5LHlGt4azybrPpHE9tF7qYjkfoVMjLNBAcykON4qGcbmYd+KQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796054002; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=XMDvjemEHzp2M+VJI1NwV6WM1QyUs/Mgvn3yVJxT9z4=; b=SaIUb7qStzFKBHpVMZ+/sCA+pu66IrwTdXVc/StHLhTgpZH7ANpvjNirNDrjnwRBheJo tJ/5ekMyoDv9Fun7kHsd4CKciS75VHaXeCyoqyjx3f9KD0j6PN6Mfr1LRQ6QA9PdaICFl kqBiO+N373uuMcLRlwxdiKHM3Eyv/dfgMzMJdOSsDAJw7w9qq8IYG15NwlfDbgU5GJoTN qTCN5QDAED2xyTysMEC1Q97rAS8ogexQDjK6Hkv/cmRkzocE5INaFszkCewNiIJkixbDX lbA2l+DeW3tFWPWw0ojLrUPDF2IG6b+SU0CV1YTL9gxTU+tSNUhv0E4kUe/a5cC9bgA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jpiecuch.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=PkqWsGp0; arc=none smtp.remote-ip=209.85.221.70 Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0AE03606A682 for ; Mon, 30 Nov 2026 15:53:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0AE03606A682 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jpiecuch.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=PkqWsGp0 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-4aae099f5baso5308460f8f.1 for ; Mon, 30 Nov 2026 07:53:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796054000; x=1796658800; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=XMDvjemEHzp2M+VJI1NwV6WM1QyUs/Mgvn3yVJxT9z4=; b=PkqWsGp03esNMa+AILZXYKbXcbacp8Lj7Tho3RDYAJiBDgNv1ISZ8m1yTGd/X2Akgp cwGsI+SHBJ24/2aDzOfs4i9ufi6GR7oRa2a3B8GPGObMwOBiTqcw32gT5ovs9UD8q7eZ y0p049nPPEabhGO5jZkWUudhoSFO4IjjHESedIW7oOzuzGedQ0GTdR8G7QsgMebNWdI+ g8GfNI4GpN43FLyqOzLquaRr9cymfVh6X3EaMMThmKFARAT1avOXylxF/gSWuuy7KLIe gNvIErgEqeXattlRuKYKo0UZx0oa+OB4b1ccFafMiWK8Vit8wUIWbERs1Eub72ySy1KO LTKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796054000; x=1796658800; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=XMDvjemEHzp2M+VJI1NwV6WM1QyUs/Mgvn3yVJxT9z4=; b=SLtpfs0/zDTgritkIXPWkJbQYc4XMjaKoZ6ii3/jPrQVGjgkMbKKWBNLPaSwMbvM8b +G0RRwdlAX/FRB0UhzIPIrQyGYl08uCRiV74tMu0hALJQnCI+Pr1xL6OGKq9ImbvmtKE 37FPUefEVfMRlmRMUUpXzjc9Kjy1ZHLm6Yt+KziWdePFmK6TOUE2mCUeOHap2ee/aF2A j77Ir/n0UOv9s4ag7wLZ+9WPGWOd7SttQCqLFBpBzmv1n7BZxR3drVP3jgOsDMQUY7du LUN9xEH8p6PsDSXPbNO0Gq644dTGfIjsopeeXKT0XsBZoLedGh2hFNrdCyXvW107bhWk M6tg== X-Forwarded-Encrypted: i=1; AL+hErF+cCeIx1MQa00ZzUKZofglj4rnFZyn29G4yrudTKCpnXSk/vKCVPeE9i+AN28mztwdB+49EH3k4f0=@lists.linux.dev X-Gm-Message-State: APuU4Z83YXEeEpfkeumBCqF0m0G725I0Riv9TaXAUYyM2kq9jf3RrLs2 l3dfxH0aMv0hs91MwThgWKVcEkbG5y6bbcW1SPepdWdT629TDwl50uYF6yHsY4zb2dPmBtoamxh CGCXIDo01gHHL1w== X-Received: from wmcn11.prod.google.com ([2002:a05:600c:c0cb:b0:4a1:d8be:ba51]) (user=jpiecuch job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:a11:b0:4a2:e0c:2b0a with SMTP id 5b1f17b1804b1-4bb888120famr692569545e9.16.1796054000314; Mon, 30 Nov 2026 07:53:20 -0800 (PST) Date: Mon, 30 Nov 2026 15:53:16 +0000 In-Reply-To: <20261130155148.2948874-1-jpiecuch@google.com> X-Mailing-List: sched-ext@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261130155148.2948874-1-jpiecuch@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261130155148.2948874-3-jpiecuch@google.com> Subject: [PATCH 2/2] selftests/sched_ext: Add sub_caps_kfunc test From: Kuba Piecuch To: Tejun Heo , David Vernet , Andrea Righi , Changwoo Min Cc: Kuba Piecuch , sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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 --- 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 + +#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 +#include +#include +#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 +#include + +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