sched_ext: Add scx_bpf_cgroup_nr_cpus()
Subsystems (5)
Andrea Righi
reviewerPATCH 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 (tg_cpus() -> cpuset_num_cpus()). 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 patchset adds scx_bpf_cgroup_nr_cpus(), a kfunc that returns the same count fair uses. This allows scx_eevdf to implement proper group scheduling, computing per-cgroup shares the same way fair does, including for groups confined to a subset of CPUs through cpuset.
Andrea Righi (3):
cgroup/cpuset: Protect is_in_v2_mode() in cpuset_num_cpus()
sched_ext: Introduce scx_bpf_cgroup_nr_cpus() selftests/sched_ext: Test scx_bpf_cgroup_nr_cpus()
kernel/cgroup/cpuset.c | 6 +- kernel/sched/ext/ext.c | 22 ++ 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 +++ tools/testing/selftests/sched_ext/cgroup_nr_cpus.c | 419 +++++++++++++++++++++ tools/testing/selftests/sched_ext/config | 1 + 8 files changed, 519 insertions(+), 3 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
Andrea Righi
reviewerPATCH 1/3 cgroup/cpuset: Protect is_in_v2_mode() in cpuset_num_cpus()
cpuset_num_cpus() enters its RCU read-side section only after checking is_in_v2_mode(). When cpuset is bound to a v1 hierarchy, is_in_v2_mode() dereferences cpuset_cgrp_subsys.root, which is freed via kfree_rcu() once that hierarchy is destroyed and cpuset is rebound to the default hierarchy. A preemptible caller outside RCU can therefore read the flags of a freed root.
The only current caller, fair's group share calculation, runs under the rq lock with preemption disabled, so it can't hit this. However, the helper already means to protect itself with RCU, and upcoming sched_ext support exposes it to sleepable BPF programs.
Take the RCU read lock before is_in_v2_mode() so that the whole lookup is protected regardless of the caller's context.
--- kernel/cgroup/cpuset.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/cgroup/cpuset.c +5 -1
@@ -4292,8 +4292,12 @@ int cpuset_num_cpus(struct cgroup *cgrp)int nr =num_online_cpus ();struct cpuset *cs; +/* +* is_in_v2_mode() dereferences cpuset's hierarchy root, which can be a +* v1 root freed via kfree_rcu() on unmount. +*/ +guard (rcu)();if (is_in_v2_mode ()) { -guard (rcu)(); cs =css_cs (cgroup_e_css (cgrp, &cpuset_cgrp_subsys));if (cs) nr =cpumask_weight (cs->effective_cpus);
Signature · 1 line
2.55.0
Michal Koutný
maintainerRe: [PATCH 1/3] cgroup/cpuset: Protect is_in_v2_mode() in cpuset_num_cpus()
Hi.
Quoted 7 lines · Andrea Righi
cpuset_num_cpus()enters its RCU read-side section only after checkingis_in_v2_mode(). When cpuset is bound to a v1 hierarchy,is_in_v2_mode()dereferences cpuset_cgrp_subsys.root, which is freed viakfree_rcu()once that hierarchy is destroyed and cpuset is rebound to the default hierarchy. A preemptible caller outside RCU can therefore read the flags of a freed root.
Quoted 7 lines
The only current caller, fair's group share calculation, runs under the rq lock with preemption disabled, so it can't hit this. However, the helper already means to protect itself with RCU, and upcoming
sched_extsupport exposes it to sleepable BPF programs.Take the RCU read lock before
is_in_v2_mode()so that the whole lookup is protected regardless of the caller's context.
This feels like mere querying of the mode shouldn't require such constraints (despite it's needed anyway later down). But it could truly happen with the novel usage (CONFIG_CPUSET_V1 && unmounting cpuset hierarchy for some reason, I wonder how you noticed :)).
Then I'd welcome more structured approach with at least:
include/linux/cgroup-defs.h +1 -1
@@ -841,7 +841,7 @@ struct cgroup_subsys {const char *legacy_name;/* link to parent, protected by cgroup_lock() */ -struct cgroup_root *root; +struct cgroup_root __rcu *root;/* idr for css->id */ struct idr css_idr;
kernel/cgroup/cgroup.c +1 -1
@@ -1909,7 +1909,7 @@ int rebind_subsystems(struct cgroup_root *dst_root, u32 ss_mask)/* rebind */ RCU_INIT_POINTER (scgrp->subsys[ssid],NULL );rcu_assign_pointer (dcgrp->subsys[ssid], css); - ss->root = dst_root; +rcu_assign_pointer (ss->root, dst_root);spin_lock_irq (&css_set_lock); css->cgroup = dcgrp;
However, if I zoom out, I see that the intention of reading cpuset's nr_cpus from the scheduler is meant for setups where cpuset tree ~ cpu tree:
| * This only really works for cgroup-v2 where all the controllers are mounted | * in the same hierarchy. If not cgroup-v2 or no cpuset controller is | * configured it reverts to num_online_cpus().
Hence it may be just OK to do:
int nr = num_online_cpus();
struct cpuset *cs;- if (is_in_v2_mode()) {
+ if (cpuset_v2()) {
guard(rcu)();
cs = css_cs(cgroup_e_css(cgrp, &cpuset_cgrp_subsys));
if (cs)I hope Waiman seconds this -- if a feature depends on shared tree, there's only so much that 'cpuset_v2_mode' can guarantee.
0.02€,
Michal
Waiman Long
maintainerRe: [PATCH 1/3] cgroup/cpuset: Protect is_in_v2_mode() in cpuset_num_cpus()
Quoted 71 lines · Michal Koutný
Hi.
Quoted 7 lines · Andrea Righi
cpuset_num_cpus()enters its RCU read-side section only after checkingis_in_v2_mode(). When cpuset is bound to a v1 hierarchy,is_in_v2_mode()dereferences cpuset_cgrp_subsys.root, which is freed viakfree_rcu()once that hierarchy is destroyed and cpuset is rebound to the default hierarchy. A preemptible caller outside RCU can therefore read the flags of a freed root.Quoted 7 lines
The only current caller, fair's group share calculation, runs under the rq lock with preemption disabled, so it can't hit this. However, the helper already means to protect itself with RCU, and upcoming
sched_extsupport exposes it to sleepable BPF programs.Take the RCU read lock before
is_in_v2_mode()so that the whole lookup is protected regardless of the caller's context.This feels like mere querying of the mode shouldn't require such constraints (despite it's needed anyway later down). But it could truly happen with the novel usage (
CONFIG_CPUSET_V1&& unmounting cpuset hierarchy for some reason, I wonder how you noticed :)).Then I'd welcome more structured approach with at least:
include/linux/cgroup-defs.h +1
-1@@ -841,7 +841,7 @@ struct cgroup_subsys {const char *legacy_name;/* link to parent, protected by cgroup_lock() */ -struct cgroup_root *root; +struct cgroup_root __rcu *root;/* idr for css->id */ struct idr css_idr;kernel/cgroup/cgroup.c +1
-1@@ -1909,7 +1909,7 @@ int rebind_subsystems(struct cgroup_root *dst_root, u32 ss_mask)/* rebind */ RCU_INIT_POINTER (scgrp->subsys[ssid],NULL );rcu_assign_pointer (dcgrp->subsys[ssid], css); - ss->root = dst_root; +rcu_assign_pointer (ss->root, dst_root);spin_lock_irq (&css_set_lock); css->cgroup = dcgrp;However, if I zoom out, I see that the intention of reading cpuset's
nr_cpusfrom the scheduler is meant for setups where cpuset tree ~ cpu tree:| * This only really works for cgroup-v2 where all the controllers are mounted | * in the same hierarchy. If not cgroup-v2 or no cpuset controller is | * configured it reverts to num_online_cpus().Hence it may be just OK to do:
int nr = num_online_cpus(); struct cpuset *cs;kernel/cgroup/cgroup.c +1
-1-if (is_in_v2_mode ()) { +if (cpuset_v2 ()) {guard (rcu)(); cs =css_cs (cgroup_e_css (cgrp, &cpuset_cgrp_subsys));if (cs)
Quoted 2 lines
I hope Waiman seconds this -- if a feature depends on shared tree, there's only so much that '
cpuset_v2_mode' can guarantee.
I think it is simpler to just change is_in_v2_mode() to cpuset_v2(). Almost all the cpuset functions should either take the callback_lock with interrupt disabled (which is a RCU read-side critical section) or with rcu_read_lock() and cpuset_mutex() acquired. This cpuset_num_cpus() function is an exception. Given what is said in the comment, this function is not supposed to be used with v1 mounted. We should change it to cpuset_v2().
Cheers,
Longman
Quoted 3 lines
0.02€,
Michal
Andrea Righi
reviewerRe: [PATCH 1/3] cgroup/cpuset: Protect is_in_v2_mode() in cpuset_num_cpus()
Hi Michal and Waiman,
Quoted 72 lines · Waiman Long
Quoted 71 lines · Michal Koutný
Hi.
Quoted 7 lines · Andrea Righi
cpuset_num_cpus()enters its RCU read-side section only after checkingis_in_v2_mode(). When cpuset is bound to a v1 hierarchy,is_in_v2_mode()dereferences cpuset_cgrp_subsys.root, which is freed viakfree_rcu()once that hierarchy is destroyed and cpuset is rebound to the default hierarchy. A preemptible caller outside RCU can therefore read the flags of a freed root.Quoted 7 lines
The only current caller, fair's group share calculation, runs under the rq lock with preemption disabled, so it can't hit this. However, the helper already means to protect itself with RCU, and upcoming
sched_extsupport exposes it to sleepable BPF programs.Take the RCU read lock before
is_in_v2_mode()so that the whole lookup is protected regardless of the caller's context.This feels like mere querying of the mode shouldn't require such constraints (despite it's needed anyway later down). But it could truly happen with the novel usage (
CONFIG_CPUSET_V1&& unmounting cpuset hierarchy for some reason, I wonder how you noticed :)).Then I'd welcome more structured approach with at least:
include/linux/cgroup-defs.h +1
-1@@ -841,7 +841,7 @@ struct cgroup_subsys {const char *legacy_name;/* link to parent, protected by cgroup_lock() */ -struct cgroup_root *root; +struct cgroup_root __rcu *root;/* idr for css->id */ struct idr css_idr;kernel/cgroup/cgroup.c +1
-1@@ -1909,7 +1909,7 @@ int rebind_subsystems(struct cgroup_root *dst_root, u32 ss_mask)/* rebind */ RCU_INIT_POINTER (scgrp->subsys[ssid],NULL );rcu_assign_pointer (dcgrp->subsys[ssid], css); - ss->root = dst_root; +rcu_assign_pointer (ss->root, dst_root);spin_lock_irq (&css_set_lock); css->cgroup = dcgrp;However, if I zoom out, I see that the intention of reading cpuset's
nr_cpusfrom the scheduler is meant for setups where cpuset tree ~ cpu tree:| * This only really works for cgroup-v2 where all the controllers are mounted | * in the same hierarchy. If not cgroup-v2 or no cpuset controller is | * configured it reverts to num_online_cpus().Hence it may be just OK to do:
int nr = num_online_cpus(); struct cpuset *cs;kernel/cgroup/cgroup.c +1
-1-if (is_in_v2_mode ()) { +if (cpuset_v2 ()) {guard (rcu)(); cs =css_cs (cgroup_e_css (cgrp, &cpuset_cgrp_subsys));if (cs)
Quoted 9 lines
Quoted 2 lines
I hope Waiman seconds this -- if a feature depends on shared tree, there's only so much that '
cpuset_v2_mode' can guarantee.I think it is simpler to just change
is_in_v2_mode()tocpuset_v2(). Almost all the cpuset functions should either take thecallback_lockwith interrupt disabled (which is a RCU read-side critical section) or withrcu_read_lock()andcpuset_mutex()acquired. Thiscpuset_num_cpus()function is an exception. Given what is said in the comment, this function is not supposed to be used with v1 mounted. We should change it tocpuset_v2().
The comment says that, outside cgroup v2, cpuset_num_cpus() falls back to num_online_cpus(). However, on v1 with cpu and cpuset mounted together using cpuset_v2_mode, it returns the group's effective cpuset count and fair.c uses that count in the default "concur" group share calculation and in "max" mode.
So replacing is_in_v2_mode() with cpuset_v2() would change scheduler behavior for that setup. I guess we could either preserve the current behavior, fix the comment and protect the root lookup with RCU; or make the code follow the documented v2-only behavior. Which one would you prefer?
Thanks,
-Andrea
Waiman Long
maintainerRe: [PATCH 1/3] cgroup/cpuset: Protect is_in_v2_mode() in cpuset_num_cpus()
Quoted 88 lines · Andrea Righi
Hi Michal and Waiman,
Quoted 72 lines · Waiman Long
Quoted 71 lines · Michal Koutný
Hi.
Quoted 7 lines · Andrea Righi
cpuset_num_cpus()enters its RCU read-side section only after checkingis_in_v2_mode(). When cpuset is bound to a v1 hierarchy,is_in_v2_mode()dereferences cpuset_cgrp_subsys.root, which is freed viakfree_rcu()once that hierarchy is destroyed and cpuset is rebound to the default hierarchy. A preemptible caller outside RCU can therefore read the flags of a freed root.Quoted 7 lines
The only current caller, fair's group share calculation, runs under the rq lock with preemption disabled, so it can't hit this. However, the helper already means to protect itself with RCU, and upcoming
sched_extsupport exposes it to sleepable BPF programs.Take the RCU read lock before
is_in_v2_mode()so that the whole lookup is protected regardless of the caller's context.This feels like mere querying of the mode shouldn't require such constraints (despite it's needed anyway later down). But it could truly happen with the novel usage (
CONFIG_CPUSET_V1&& unmounting cpuset hierarchy for some reason, I wonder how you noticed :)).Then I'd welcome more structured approach with at least:
include/linux/cgroup-defs.h +1
-1@@ -841,7 +841,7 @@ struct cgroup_subsys {const char *legacy_name;/* link to parent, protected by cgroup_lock() */ -struct cgroup_root *root; +struct cgroup_root __rcu *root;/* idr for css->id */ struct idr css_idr;kernel/cgroup/cgroup.c +1
-1@@ -1909,7 +1909,7 @@ int rebind_subsystems(struct cgroup_root *dst_root, u32 ss_mask)/* rebind */ RCU_INIT_POINTER (scgrp->subsys[ssid],NULL );rcu_assign_pointer (dcgrp->subsys[ssid], css); - ss->root = dst_root; +rcu_assign_pointer (ss->root, dst_root);spin_lock_irq (&css_set_lock); css->cgroup = dcgrp;However, if I zoom out, I see that the intention of reading cpuset's
nr_cpusfrom the scheduler is meant for setups where cpuset tree ~ cpu tree:| * This only really works for cgroup-v2 where all the controllers are mounted | * in the same hierarchy. If not cgroup-v2 or no cpuset controller is | * configured it reverts to num_online_cpus().Hence it may be just OK to do:
int nr = num_online_cpus(); struct cpuset *cs;kernel/cgroup/cgroup.c +1
-1-if (is_in_v2_mode ()) { +if (cpuset_v2 ()) {guard (rcu)(); cs =css_cs (cgroup_e_css (cgrp, &cpuset_cgrp_subsys));if (cs)Quoted 8 lines
Quoted 2 lines
I hope Waiman seconds this -- if a feature depends on shared tree, there's only so much that '
cpuset_v2_mode' can guarantee.I think it is simpler to just change
is_in_v2_mode()tocpuset_v2(). Almost all the cpuset functions should either take thecallback_lockwith interrupt disabled (which is a RCU read-side critical section) or withrcu_read_lock()andcpuset_mutex()acquired. Thiscpuset_num_cpus()function is an exception. Given what is said in the comment, this function is not supposed to be used with v1 mounted. We should change it tocpuset_v2().The comment says that, outside cgroup v2,
cpuset_num_cpus()falls back tonum_online_cpus(). However, on v1 with cpu and cpuset mounted together usingcpuset_v2_mode, it returns the group's effective cpuset count andfair.cuses that count in the default "concur" group share calculation and in "max" mode.
Quoted 4 lines
So replacing
is_in_v2_mode()withcpuset_v2()would change scheduler behavior for that setup. I guess we could either preserve the current behavior, fix the comment and protect the root lookup with RCU; or make the code follow the documented v2-only behavior. Which one would you prefer?
I will let Peter decide if he wants to support the cpuset_v2_mode mount option of cgroup v1 since he is the original author of cpuset_num_cpus(). If this is supported, we have to update the function comment as well.
Cheers,
Longman
Quoted 3 lines
Thanks,
-Andrea
Peter Zijlstra
maintainerRe: [PATCH 1/3] cgroup/cpuset: Protect is_in_v2_mode() in cpuset_num_cpus()
Quoted 14 lines · Waiman Long
Quoted 12 lines · Andrea Righi
Quoted 6 lines
I think it is simpler to just change
is_in_v2_mode()tocpuset_v2(). Almost all the cpuset functions should either take thecallback_lockwith interrupt disabled (which is a RCU read-side critical section) or withrcu_read_lock()andcpuset_mutex()acquired. Thiscpuset_num_cpus()function is an exception. Given what is said in the comment, this function is not supposed to be used with v1 mounted. We should change it tocpuset_v2().The comment says that, outside cgroup v2,
cpuset_num_cpus()falls back tonum_online_cpus(). However, on v1 with cpu and cpuset mounted together usingcpuset_v2_mode, it returns the group's effective cpuset count andfair.cuses that count in the default "concur" group share calculation and in "max" mode.
Quoted 8 lines
Quoted 4 lines
So replacing
is_in_v2_mode()withcpuset_v2()would change scheduler behavior for that setup. I guess we could either preserve the current behavior, fix the comment and protect the root lookup with RCU; or make the code follow the documented v2-only behavior. Which one would you prefer?I will let Peter decide if he wants to support the
cpuset_v2_modemount option of cgroup v1 since he is the original author ofcpuset_num_cpus(). If this is supported, we have to update the function comment as well.
So I was not aware of this weird mount option at all. That said, ideally it would work in the widest possible setting.
The main constraint is going from a cpu-cgroup to a cpuset-cgroup. It was my understanding that this transition only works in v2, but if that mount option is sufficient to make that cross-cgroup transition meaningful, then yay I suppose.
Waiman Long
maintainerRe: [PATCH 1/3] cgroup/cpuset: Protect is_in_v2_mode() in cpuset_num_cpus()
Quoted 20 lines · Peter Zijlstra
Quoted 12 lines · Waiman Long
Quoted 11 lines · Andrea Righi
Quoted 6 lines
I think it is simpler to just change
is_in_v2_mode()tocpuset_v2(). Almost all the cpuset functions should either take thecallback_lockwith interrupt disabled (which is a RCU read-side critical section) or withrcu_read_lock()andcpuset_mutex()acquired. Thiscpuset_num_cpus()function is an exception. Given what is said in the comment, this function is not supposed to be used with v1 mounted. We should change it tocpuset_v2().The comment says that, outside cgroup v2,
cpuset_num_cpus()falls back tonum_online_cpus(). However, on v1 with cpu and cpuset mounted together usingcpuset_v2_mode, it returns the group's effective cpuset count andfair.cuses that count in the default "concur" group share calculation and in "max" mode.Quoted 7 lines
Quoted 4 lines
So replacing
is_in_v2_mode()withcpuset_v2()would change scheduler behavior for that setup. I guess we could either preserve the current behavior, fix the comment and protect the root lookup with RCU; or make the code follow the documented v2-only behavior. Which one would you prefer?I will let Peter decide if he wants to support the
cpuset_v2_modemount option of cgroup v1 since he is the original author ofcpuset_num_cpus(). If this is supported, we have to update the function comment as well.
Quoted 7 lines
So I was not aware of this weird mount option at all. That said, ideally it would work in the widest possible setting.
The main constraint is going from a cpu-cgroup to a cpuset-cgroup. It was my understanding that this transition only works in v2, but if that mount option is sufficient to make that cross-cgroup transition meaningful, then yay I suppose.
The cpuset_v2_mode mount option is only for making the cpuset.cpus and cpuset.mems behave like in v2. The cpu-cgroup and cpuset-cgroup can still be in separate hierarchies. If cross-cgroup transition is the main point, it won't work with the cpuset_v2_mode mount option. We should switch to use cpuset_v2().
Cheers,
Longman
Peter Zijlstra
maintainerRe: [PATCH 1/3] cgroup/cpuset: Protect is_in_v2_mode() in cpuset_num_cpus()
Quoted 5 lines · Waiman Long
The
cpuset_v2_modemount option is only for making the cpuset.cpus and cpuset.mems behave like in v2. The cpu-cgroup and cpuset-cgroup can still be in separate hierarchies. If cross-cgroup transition is the main point, it won't work with thecpuset_v2_modemount option. We should switch to usecpuset_v2().
Yes, the cross-cgroup transition is fundamental to how this works. Without that you cannot reasonably talk about the CPUs of a cpu-cgroup, you need to have cpu and cpuset aligned.
Andrea Righi
reviewerRe: [PATCH 1/3] cgroup/cpuset: Protect is_in_v2_mode() in cpuset_num_cpus()
In reply to Michal KoutnýHi Michal,
Quoted 43 lines · Michal Koutný
Hi.
Quoted 7 lines · Andrea Righi
cpuset_num_cpus()enters its RCU read-side section only after checkingis_in_v2_mode(). When cpuset is bound to a v1 hierarchy,is_in_v2_mode()dereferences cpuset_cgrp_subsys.root, which is freed viakfree_rcu()once that hierarchy is destroyed and cpuset is rebound to the default hierarchy. A preemptible caller outside RCU can therefore read the flags of a freed root.Quoted 7 lines
The only current caller, fair's group share calculation, runs under the rq lock with preemption disabled, so it can't hit this. However, the helper already means to protect itself with RCU, and upcoming
sched_extsupport exposes it to sleepable BPF programs.Take the RCU read lock before
is_in_v2_mode()so that the whole lookup is protected regardless of the caller's context.This feels like mere querying of the mode shouldn't require such constraints (despite it's needed anyway later down). But it could truly happen with the novel usage (
CONFIG_CPUSET_V1&& unmounting cpuset hierarchy for some reason, I wonder how you noticed :)).Then I'd welcome more structured approach with at least:
include/linux/cgroup-defs.h +1
-1@@ -841,7 +841,7 @@ struct cgroup_subsys {const char *legacy_name;/* link to parent, protected by cgroup_lock() */ -struct cgroup_root *root; +struct cgroup_root __rcu *root;/* idr for css->id */ struct idr css_idr;kernel/cgroup/cgroup.c +0
-0
kernel/cgroup/cgroup.c int rebind_subsystems(struct cgroup_root *dst_root, u32 ss_mask) · line 1912
Quoted 5 lines
kernel/cgroup/cgroup.c +1
-1@@ -1909,7 +1909,7 @@ int rebind_subsystems(struct cgroup_root *dst_root, u32 ss_mask)/* rebind */ RCU_INIT_POINTER (scgrp->subsys[ssid],NULL );rcu_assign_pointer (dcgrp->subsys[ssid], css); - ss->root = dst_root; +rcu_assign_pointer (ss->root, dst_root);
About the __rcu annotations of ss->root, I agree it'd be nice, but it makes sparse complain about all the other plain ss->root accesses. Maybe we could do this as a separate cgroup patch/series on top of this one?
Thanks,
-Andrea
Andrea Righi
reviewerPATCH 2/3 sched_ext: Introduce scx_bpf_cgroup_nr_cpus()
In reply to Andrea RighiHierarchical 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 the a compat wrapper which falls back to nr_cpu_ids on older kernels.
--- kernel/sched/ext/ext.c | 22 ++++++++++++++++++++++ tools/sched_ext/include/scx/common.bpf.h | 1 + tools/sched_ext/include/scx/compat.bpf.h | 7 +++++++ 3 files changed, 30 insertions(+)
kernel/sched/ext/ext.c +22 -0
@@ -11350,6 +11350,27 @@ __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. +* +* On cgroup v1 without the cpuset_v2_mode mount option, or when cpuset is not +* configured, the number of online CPUs is returned. The result may be 0 for a +* partition root which has no effective CPUs left. +* +* 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 ();
@@ -11397,6 +11418,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 3/3 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