selftests/sched_ext: Add basic sub-sched functionality test
Subsystems (2)
PATCH 1/2 selftests/sched_ext: Add basic sub-sched functionality test
Implement a new selftest to verify sub-sched (nested scheduler) attachment and detachment. This test was missing despite sub-sched being a complex feature that has seen multiple bug fixes (cgroup double-put, uninitialized return value, abort path issues).
Sub-sched allows a child BPF scheduler to manage tasks within a specific cgroup subtree, coordinating with the parent scheduler via sub_attach/sub_detach operations.
Test structure:
- Parent BPF scheduler: Implements sub_attach/sub_detach ops; sub_attach returns s32 as required by the kernel ABI
- Child BPF scheduler: Minimal dispatch logic, configured with sub_cgroup_id pointing to a test cgroup
- Test driver: Creates a cgroup under /sys/fs/cgroup/ (cgroupfs), obtains the cgroup ID via stat().st_ino (which equals the kernfs node ID on cgroupfs), loads both schedulers, attaches child to the cgroup, verifies clean detachment
Note: The cgroup must be created under /sys/fs/cgroup/ rather than /tmp/, because cgroup_get_from_id() uses kernfs_find_and_get_node_by_id() in the cgroupfs kernfs root. A tmpfs inode is not a valid cgroup ID.
--- tools/testing/selftests/sched_ext/Makefile | 1 + tools/testing/selftests/sched_ext/sub_sched.c | 200 ++++++++++++++++++ .../selftests/sched_ext/sub_sched_child.bpf.c | 31 +++ .../sched_ext/sub_sched_parent.bpf.c | 35 +++ 4 files changed, 267 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/sub_sched.c create mode 100644 tools/testing/selftests/sched_ext/sub_sched_child.bpf.c create mode 100644 tools/testing/selftests/sched_ext/sub_sched_parent.bpf.c
tools/testing/selftests/sched_ext/Makefile +1 -0
@@ -190,6 +190,7 @@ auto-test-targets := \ select_cpu_dispatch_dbl_dsp \ select_cpu_vtime \ rt_stall \ + sub_sched \ test_example \ total_bw \
tools/testing/selftests/sched_ext/sub_sched.c new file +200 -0
200 lines not on this page · raw message
tools/testing/selftests/sched_ext/sub_sched_child.bpf.c new file +31 -0
@@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Child scheduler for sub-sched testing. +* +* This is a minimal scheduler that attaches to a specific cgroup via +* sub_cgroup_id. The BPF loader will populate sub_cgroup_id at load time +* before attaching. +* +* Copyright (c) 2026 Xiaomi Corporation. +*/ + +#include <scx/common.bpf.h> + +char _license[] SEC("license") = "GPL"; + +__u64 dispatch_count; + +void BPF_STRUCT_OPS (sub_sched_child_dispatch ,s32 cpu,struct task_struct *prev) +{ +/* Minimal dispatch: just return without dispatching anything. +* The kernel will use the default scheduling paths. +*/ +__sync_fetch_and_add (&dispatch_count,1 ); +} + +SEC(".struct_ops.link") +struct sched_ext_ops sub_sched_child_ops = { + .name ="sub_sched_child" , + .sub_cgroup_id =0 ,/* Will be set by user space */ + .dispatch = (void *)sub_sched_child_dispatch, +};
tools/testing/selftests/sched_ext/sub_sched_parent.bpf.c new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Parent scheduler for sub-sched testing. +* +* This scheduler demonstrates the sub_attach/sub_detach ops that allow +* a parent scheduler to manage sub-schedulers attached to specific cgroups. +* +* Copyright (c) 2026 Xiaomi Corporation. +*/ + +#include <scx/common.bpf.h> + +char _license[] SEC("license") = "GPL"; + +/* Simple counter for diagnostics */ +__u64 attach_count; +__u64 detach_count; + +s32 BPF_STRUCT_OPS (parent_sub_attach ,struct scx_sub_attach_args *args) +{ +__sync_fetch_and_add (&attach_count,1 ); +return 0 ; +} + +void BPF_STRUCT_OPS (parent_sub_detach ,struct scx_sub_detach_args *args) +{ +__sync_fetch_and_add (&detach_count,1 ); +} + +SEC(".struct_ops.link") +struct sched_ext_ops sub_sched_parent_ops = { + .name ="sub_sched_parent" , + .sub_attach = (void *)parent_sub_attach, + .sub_detach = (void *)parent_sub_detach, +};
Signature · 1 line
2.43.0
PATCH 2/2 selftests/sched_ext: Add failure-path tests for sub-sched
Sub-sched error paths have historically been fragile: a cgroup double-put bug and an uninitialized return value were both found in the abort path but not caught by automated tests because only the happy path was covered.
Add three tests targeting specific failure scenarios:
1. sub_sched_parent_reject: Tests the sub_attach rejection path. A parent scheduler returning -EPERM forces the kernel to execute the abort path (ext.c abort label), which must correctly clean up partially-initialized tasks and SCX_TASK_SUB_INIT flags. This is the path where commit 0c66b0da0064 ("sched_ext: Fix cgroup double-put on sub-sched abort path") and commit e36bc38ebfac ("sched_ext: Fix uninitialized ret in scx_alloc_and_add_sched()") occurred.
2. sub_sched_nesting: Tests cascading disable through a 3-level scheduler hierarchy (root -> level1 -> level2). When schedulers are detached in reverse order, drain_descendants() must recursively wait for all children to complete their disable sequence without deadlock or resource leaks.
3. sub_sched_race: Tests concurrent race between parent disable and child enable. A background thread destroys the parent link while the main thread attaches the child, exercising synchronization between scx_sub_enable_workfn() and the parent disable path.
All three tests create cgroups under /sys/fs/cgroup/ and are verified to pass on a kernel with sub-sched support enabled.
--- tools/testing/selftests/sched_ext/Makefile | 3 + .../sched_ext/sub_sched_nesting_child.bpf.c | 41 +++ .../sched_ext/sub_sched_nesting_test.c | 287 ++++++++++++++++++ .../sched_ext/sub_sched_parent_reject.bpf.c | 28 ++ .../sched_ext/sub_sched_parent_reject_test.c | 202 ++++++++++++ .../selftests/sched_ext/sub_sched_race_test.c | 257 ++++++++++++++++ 6 files changed, 818 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/sub_sched_nesting_child.bpf.c create mode 100644 tools/testing/selftests/sched_ext/sub_sched_nesting_test.c create mode 100644 tools/testing/selftests/sched_ext/sub_sched_parent_reject.bpf.c create mode 100644 tools/testing/selftests/sched_ext/sub_sched_parent_reject_test.c create mode 100644 tools/testing/selftests/sched_ext/sub_sched_race_test.c
tools/testing/selftests/sched_ext/Makefile +3 -0
@@ -191,6 +191,9 @@ auto-test-targets := \ select_cpu_vtime \ rt_stall \ sub_sched \ + sub_sched_parent_reject_test \ + sub_sched_nesting_test \ + sub_sched_race_test \ test_example \ total_bw \
tools/testing/selftests/sched_ext/sub_sched_nesting_child.bpf.c new file +41 -0
41 lines not on this page · raw message
tools/testing/selftests/sched_ext/sub_sched_nesting_test.c new file +287 -0
287 lines not on this page · raw message
tools/testing/selftests/sched_ext/sub_sched_parent_reject.bpf.c new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Parent scheduler that rejects sub_attach for failure path testing. +* +* This scheduler deliberately rejects sub_attach to test that the kernel +* properly cleans up partially-initialized tasks and rolls back without crashing. +* +* Copyright (c) 2026 Xiaomi Corporation. +*/ + +#include <scx/common.bpf.h> + +char _license[] SEC("license") = "GPL"; + +__u64 attach_reject_count; + +s32 BPF_STRUCT_OPS (parent_reject_sub_attach ,struct scx_sub_attach_args *args) +{ +/* Deliberately reject sub_attach to trigger rollback path */ +__sync_fetch_and_add (&attach_reject_count,1 ); +return -EPERM ; +} + +SEC(".struct_ops.link") +struct sched_ext_ops sub_sched_parent_reject_ops = { + .name ="sub_sched_parent_reject" , + .sub_attach = (void *)parent_reject_sub_attach, +};
tools/testing/selftests/sched_ext/sub_sched_parent_reject_test.c new file +202 -0
202 lines not on this page · raw message
tools/testing/selftests/sched_ext/sub_sched_race_test.c new file +257 -0
257 lines not on this page · raw message
Signature · 1 line
2.43.0
Tejun Heo
reviewerRe: [PATCH 2/2] selftests/sched_ext: Add failure-path tests for sub-sched
Quoted 22 lines · zhidao su
Sub-sched error paths have historically been fragile: a cgroup double-put bug and an uninitialized return value were both found in the abort path but not caught by automated tests because only the happy path was covered.
Add three tests targeting specific failure scenarios:
1.
sub_sched_parent_reject: Tests thesub_attachrejection path. A parent scheduler returning-EPERMforces the kernel to execute the abort path (ext.cabort label), which must correctly clean up partially-initialized tasks andSCX_TASK_SUB_INITflags. This is the path where commit0c66b0da0064("sched_ext: Fix cgroup double-put on sub-sched abort path") and commite36bc38ebfac("sched_ext: Fix uninitialized ret inscx_alloc_and_add_sched()") occurred.2.
sub_sched_nesting: Tests cascading disable through a 3-level scheduler hierarchy (root -> level1 -> level2). When schedulers are detached in reverse order,drain_descendants()must recursively wait for all children to complete their disable sequence without deadlock or resource leaks.
Quoted 7 lines
3.
sub_sched_race: Tests concurrent race between parent disable and child enable. A background thread destroys the parent link while the main thread attaches the child, exercising synchronization betweenscx_sub_enable_workfn()and the parent disable path.All three tests create cgroups under
/sys/fs/cgroup/and are verified to pass on a kernel with sub-sched support enabled.
Let's hold off on sub-sched tests for now. It's a bit too early.
Thanks.
Signature · 1 line
tejun