sched_ext: Add scx_bpf_cgroup_nr_cpus()
Subsystems (4)
Andrea Righi
reviewerPATCH v2 1/1 sched_ext: Add scx_bpf_cgroup_nr_cpus()
Hierarchical BPF schedulers need to know how many CPUs a cgroup can run on when distributing group-wide weights. For example, fair's group share calculation scales a group's weight by the smaller of its estimated runnable task count and the number of CPUs in its effective cpuset. Without that count, a BPF scheduler can only use the machine-wide CPU count and may give a group confined to a few CPUs excessive shares.
This series adds scx_bpf_cgroup_nr_cpus(), a kfunc that returns the same count fair uses. This allows scx_eevdf to compute per-cgroup shares for groups confined to a subset of CPUs through cpuset. A selftest covers effective and inherited cpusets, changes to the allowed CPUs, and calls with and without a scheduler attached.
This series requires Waiman Long's cpuset fix to avoid a potential use-after-free in a rare cgroup filesystem rebind race:
https://lore.kernel.org/r/[email protected]
Changes in v2:
- Drop the cpuset RCU guard patch
- Link to v1:
https://lore.kernel.org/r/[email protected]
Andrea Righi (2):
sched_ext: Introduce scx_bpf_cgroup_nr_cpus() selftests/sched_ext: Test scx_bpf_cgroup_nr_cpus()
kernel/sched/ext/ext.c | 18 + tools/sched_ext/include/scx/common.bpf.h | 1 + tools/sched_ext/include/scx/compat.bpf.h | 7 + tools/testing/selftests/sched_ext/Makefile | 6 +- .../selftests/sched_ext/cgroup_nr_cpus.bpf.c | 60 +++ .../selftests/sched_ext/cgroup_nr_cpus.c | 419 ++++++++++++++++++ tools/testing/selftests/sched_ext/config | 1 + 7 files changed, 510 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/sched_ext/cgroup_nr_cpus.bpf.c create mode 100644 tools/testing/selftests/sched_ext/cgroup_nr_cpus.c
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 1/2 sched_ext: Introduce scx_bpf_cgroup_nr_cpus()
Hierarchical BPF schedulers may need to know how many CPUs a cgroup can run on when distributing group-wide weights.
For example, fair's group share calculation scales a group's weight by the smaller of its estimated runnable task count and the number of CPUs in its effective cpuset. Without that count, a BPF scheduler can only use the machine-wide CPU count and may give a group confined to a few CPUs excessive shares.
A task's effective affinity can include per-task restrictions, so it cannot reliably represent the group's cpuset. Reading the cpuset directly from BPF would require CO-RE access to private cgroup and cpuset structures, including inherited cpuset resolution, making the approach fragile across kernel versions.
Add scx_bpf_cgroup_nr_cpus(), which returns the same count fair uses through cpuset_num_cpus(). The count is independent of CPU or CID numbering, so the kfunc is available to both cpu-form and cid-form schedulers from any context. Also add a compat wrapper which falls back to nr_cpu_ids on older kernels.
--- kernel/sched/ext/ext.c | 18 ++++++++++++++++++ tools/sched_ext/include/scx/common.bpf.h | 1 + tools/sched_ext/include/scx/compat.bpf.h | 7 +++++++ 3 files changed, 26 insertions(+)
kernel/sched/ext/ext.c +18 -0
@@ -11355,6 +11355,23 @@ __bpf_kfunc struct cgroup *scx_bpf_task_cgroup(struct task_struct *p,cgroup_get (cgrp); return cgrp; } + +/** +* scx_bpf_cgroup_nr_cpus - Return the number of CPUs in a cgroup's cpuset +* @cgrp: cgroup of interest +* +* Return the number of CPUs in @cgrp's effective cpuset, which is inherited +* from the nearest ancestor with the cpuset controller enabled. This matches +* the count used by fair's group share calculation and lets hierarchical BPF +* schedulers bound a group's weight by the CPUs it can actually run on. +* +* The value is a snapshot and may change at any time through cpuset updates or +* CPU hotplug. Schedulers should re-read it when recomputing group state. +*/ +__bpf_kfunc u32 scx_bpf_cgroup_nr_cpus(struct cgroup *cgrp) +{ +return cpuset_num_cpus (cgrp); +}#endif /* CONFIG_CGROUP_SCHED */ __bpf_kfunc_end_defs ();
@@ -11402,6 +11419,7 @@ BTF_ID_FLAGS(func, scx_bpf_now) BTF_ID_FLAGS(func, scx_bpf_events, KF_IMPLICIT_ARGS)#ifdef CONFIG_CGROUP_SCHED BTF_ID_FLAGS(func, scx_bpf_task_cgroup, KF_IMPLICIT_ARGS | KF_RCU | KF_ACQUIRE) +BTF_ID_FLAGS(func, scx_bpf_cgroup_nr_cpus, KF_RCU)#endif BTF_ID_FLAGS(func, scx_bpf_sub_grant, KF_IMPLICIT_ARGS)BTF_ID_FLAGS (func, scx_bpf_sub_revoke,KF_IMPLICIT_ARGS )
tools/sched_ext/include/scx/common.bpf.h +1 -0
@@ -104,6 +104,7 @@ struct task_struct *scx_bpf_cpu_curr(s32 cpu) __ksym __weak;struct task_struct *scx_bpf_tid_to_task (u64 tid) __ksym__weak ; u64 scx_bpf_now(void) __ksym __weak; void scx_bpf_events(struct scx_event_stats *events, size_t events__sz) __ksym __weak; +u32 scx_bpf_cgroup_nr_cpus(struct cgroup *cgrp) __ksym __weak; s32 scx_bpf_cpu_to_cid(s32 cpu) __ksym __weak; s32 scx_bpf_cid_to_cpu(s32 cid) __ksym __weak; s32 scx_bpf_cid_node(s32 cid) __ksym __weak;
tools/sched_ext/include/scx/compat.bpf.h +7 -0
@@ -248,6 +248,13 @@ static inline bool __COMPAT_is_enq_cpu_selected(u64 enq_flags) (bpf_ksym_exists (scx_bpf_cid_node) ? \scx_bpf_cid_node (cid) :NUMA_NO_NODE ) +/* +* v7.4: Add scx_bpf_cgroup_nr_cpus(). + */ +#define __COMPAT_scx_bpf_cgroup_nr_cpus (cgrp) \ + (bpf_ksym_exists(scx_bpf_cgroup_nr_cpus) ? \ + scx_bpf_cgroup_nr_cpus(cgrp) : scx_bpf_nr_cpu_ids()) +/* * v6.18: Add a helper to retrieve the current task running on a CPU. *
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 2/2 selftests/sched_ext: Test scx_bpf_cgroup_nr_cpus()
In reply to Andrea RighiVerify that scx_bpf_cgroup_nr_cpus() reports the number of CPUs in a cgroup's effective cpuset.
Create a cgroup-v2 subtree where a child owns a cpuset and a leaf inherits it. Sample the kfunc from a BPF_PROG_TYPE_SYSCALL program and compare it with cpuset.cpus.effective for the root, the cpuset owner and the inheriting leaf, without a scheduler attached. Attach a scheduler and verify the value observed from ops.cgroup_init(). Then restrict the child's cpuset to a non-contiguous and a single-CPU set, and finally disable cpuset below the parent so that both cgroups inherit the parent's cpuset.
A disabled cpuset css stays attached to its cgroup until it is asynchronously offlined, so poll briefly for the last step. Skip the test when the required controllers or two CPUs are unavailable.
--- tools/testing/selftests/sched_ext/Makefile | 6 +- .../selftests/sched_ext/cgroup_nr_cpus.bpf.c | 60 +++ .../selftests/sched_ext/cgroup_nr_cpus.c | 419 ++++++++++++++++++ tools/testing/selftests/sched_ext/config | 1 + 4 files changed, 484 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/sched_ext/cgroup_nr_cpus.bpf.c create mode 100644 tools/testing/selftests/sched_ext/cgroup_nr_cpus.c
tools/testing/selftests/sched_ext/Makefile +4 -2
@@ -10,6 +10,7 @@ TEST_GEN_MODS_DIR := test_modules # override lib.mk's default rules OVERRIDE_TARGETS := 1 include ../lib.mk +include ../cgroup/lib/libcgroup.mk CURDIR := $(abspath .) REPOROOT := $(abspath ../../../..)
@@ -154,7 +155,7 @@ $(INCLUDE_DIR)/%.bpf.skel.h: $(SCXOBJ_DIR)/%.bpf.o $(INCLUDE_DIR)/vmlinux.h $(BP override define CLEAN rm -rf $(OUTPUT_DIR) - rm -f $(TEST_GEN_PROGS) + rm -f $(TEST_GEN_PROGS) $(EXTRA_CLEAN) endef # Every testcase takes all of the BPF progs are dependencies by default. This
@@ -163,6 +164,7 @@ endef all_test_bpfprogs := $(foreach prog,$(wildcard *.bpf.c),$(INCLUDE_DIR)/$(patsubst %.c,%.skel.h,$(prog))) auto-test-targets := \ + cgroup_nr_cpus \ create_dsq \ dequeue \ dequeue_iter \
@@ -216,7 +218,7 @@ $(testcase-targets): $(SCXOBJ_DIR)/%.o: %.c $(SCXOBJ_DIR)/runner.o $(all_test_bp $(SCXOBJ_DIR)/util.o: util.c | $(SCXOBJ_DIR) $(CC) $(CFLAGS) -c $< -o $@ -$(OUTPUT)/runner: $(SCXOBJ_DIR)/runner.o $(SCXOBJ_DIR)/util.o $(BPFOBJ) $(testcase-targets) +$(OUTPUT)/runner: $(SCXOBJ_DIR)/runner.o $(SCXOBJ_DIR)/util.o $(BPFOBJ) $(LIBCGROUP_O) $(testcase-targets) @echo "$(testcase-targets)" $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
tools/testing/selftests/sched_ext/cgroup_nr_cpus.bpf.c new file +60 -0
60 lines not on this page · raw message
tools/testing/selftests/sched_ext/cgroup_nr_cpus.c new file +419 -0
419 lines not on this page · raw message
tools/testing/selftests/sched_ext/config +1 -0
@@ -1,6 +1,7 @@ CONFIG_SCHED_CLASS_EXT=y CONFIG_CGROUPS=y CONFIG_CGROUP_SCHED=y +CONFIG_CPUSETS=y CONFIG_EXT_GROUP_SCHED=y CONFIG_BPF=y CONFIG_BPF_SYSCALL=y
Signature · 1 line
2.55.0
Tejun Heo
maintainerQuoted 3 lines · Andrea Righi
Andrea Righi (2):
sched_ext: Introducescx_bpf_cgroup_nr_cpus()selftests/sched_ext: Testscx_bpf_cgroup_nr_cpus()
Applied 1-2 to sched_ext/for-7.4.
Thanks.
--
tejun