Re: [PATCH v2 4/4] cgroup/cpuset: Skip sibling checks for v2 members without exclusive CPUs
From: Chuyi Zhou
Date: Mon Nov 30 2026 - 21:45:38 EST
On 2026-12-01 9:49 a.m., Ridong Chen wrote:
>
>
> On 11/30/2026 8:04 PM, Chuyi Zhou wrote:
>> Changing cpuset.cpus on a cgroup v2 member calls validate_change(),
>> which walks all siblings under cpuset_mutex to check CPU and memory
>> exclusivity. The cost of changing one member's CPU mask grows with
>> the number of children under its parent.
>>
>> Skip the walk when the member's cpuset.cpus.exclusive stays empty.
>> An empty exclusive mask cannot intersect a sibling's exclusive mask.
>> The check for a sibling whose allowed CPUs are all in the member's
>> exclusive mask applies only when that mask changes. Only the top
>> cpuset is memory-exclusive on v2, so the walk can also be skipped
>> when changing cpuset.mems on these members.
>>
>> On a dual-socket Xeon Platinum 8582C with 240 online CPUs, the target
>> cpuset contained one sleeping task. A writer pinned to CPU 1 alternated
>> its cpuset.cpus between 2 and 2-3. Each table entry gives the median
>> latency of 256 write() calls.
>>
>> Children Before (ns) After (ns)
>> 1 861 859
>> 64 1425 856
>> 512 5184 855
>> 2048 24056 853
>>
>> Signed-off-by: Chuyi Zhou <zhouchuyi@xxxxxxxxxxxxx>
>> ---
>> kernel/cgroup/cpuset.c | 12 +++++++++++-
>> 1 file changed, 11 insertions(+), 1 deletion(-)
>>
>> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
>> index fb2c08b31b53..fb6359d72b68 100644
>> --- a/kernel/cgroup/cpuset.c
>> +++ b/kernel/cgroup/cpuset.c
>> @@ -805,12 +805,21 @@ int validate_change(struct cpuset *cur, struct
>> cpuset *trial)
>> !cpuset_cpumask_can_shrink(cur->effective_cpus,
>> user_xcpus(trial)))
>> goto out;
>> + xcpus_changed = !cpumask_equal(cur->exclusive_cpus, trial-
>> >exclusive_cpus);
>> +
>> + /*
>> + * On v2 an unchanged, empty exclusive CPU mask cannot conflict with
>> + * a sibling's CPU masks. Only the top cpuset is memory-exclusive.
>> + */
>> + if (cpuset_v2() && cs_is_member(cur) && !xcpus_changed &&
>> + cpumask_empty(trial->exclusive_cpus))
>> + goto valid;
>> +
>> /*
>> * If either I or some sibling (!= me) is exclusive, we can't
>> * overlap. exclusive_cpus cannot overlap with each other if set.
>> */
>> ret = -EINVAL;
>> - xcpus_changed = !cpumask_equal(cur->exclusive_cpus, trial-
>> >exclusive_cpus);
>> cpuset_for_each_child(c, css, par) {
>> if (c == cur)
>> continue;
>> @@ -820,6 +829,7 @@ int validate_change(struct cpuset *cur, struct
>> cpuset *trial)
>> goto out;
>> }
>> +valid:
>> ret = 0;
>> out:
>> rcu_read_unlock();
>
> Hi Chuyi,
>
> Thanks for the patch. I think the condition can be simplified, and
> the sibling walk could be restructured a bit.
>
> Since commit 85ad3963de96 ("cgroup/cpuset: Remove CS_CPU_EXCLUSIVE
> handling code from v2"), the v2 part of cpus_excl_conflict() only
> looks at trial->exclusive_cpus:
>
> xcpus_changed && cpumask_subset(sibling->cpus_allowed, trial-
> >exclusive_cpus)
> cpumask_intersects(trial->exclusive_cpus, sibling->exclusive_cpus)
>
> If trial->exclusive_cpus is empty, neither of them can be true,
> whether or not exclusive_cpus has changed. The subset check also
> requires a non-empty sibling->cpus_allowed, and a non-empty mask
> cannot be a subset of an empty one. So !xcpus_changed is not needed.
>
> cs_is_member(cur) is not needed either. Nothing in the loop depends
> on the partition state of @cur. For a partition root, conflicts with
> siblings are handled by compute_trialcs_excpus() and
> partition_cpus_change(), which invalidate the partition instead of
> failing the write. Dropping the check would let partition roots with
> an empty cpuset.cpus.exclusive skip the walk as well.
>
> On the memory side, mems_excl_conflict() never fires on v2: only
> top_cpuset can be CS_MEM_EXCLUSIVE there, and it has no siblings.
>
> So the v1 and v2 sibling rules no longer share anything. How about
> moving the walk into one helper that hands v1 off to cpuset-v1.c and
> does the v2 checks by default, with the empty check as an early
> return? Something like:
>
>
> In cpuset.c:
>
> ```
> static bool siblings_excl_conflict(struct cpuset *cur, struct cpuset
> *trial)
> {
> struct cgroup_subsys_state *css;
> struct cpuset *c;
> bool xcpus_changed;
>
> if (!cpuset_v2())
> return cpuset1_siblings_excl_conflict(cur, trial);
>
> /* An empty exclusive_cpus cannot conflict with any sibling */
> if (cpumask_empty(trial->exclusive_cpus))
> return false;
>
> xcpus_changed = !cpumask_equal(cur->exclusive_cpus, trial-
> >exclusive_cpus);
> cpuset_for_each_child(c, css, parent_cs(cur)) {
> if (c == cur)
> continue;
> if (xcpus_changed && !cpumask_empty(c->cpus_allowed) &&
> cpumask_subset(c->cpus_allowed, trial->exclusive_cpus))
> return true;
> if (cpumask_intersects(trial->exclusive_cpus, c->exclusive_cpus))
> return true;
> }
> return false;
> }
> ```
>
> In cpuset-v1.c:
>
> ```
> bool cpuset1_siblings_excl_conflict(struct cpuset *cur, struct cpuset
> *trial)
> {
> struct cgroup_subsys_state *css;
> struct cpuset *c;
>
> cpuset_for_each_child(c, css, parent_cs(cur)) {
> if (c == cur)
> continue;
> if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
> cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
> return true;
> if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
> nodes_intersects(trial->mems_allowed, c->mems_allowed))
> return true;
> }
> return false;
> }
> ```
Hi Ridong,
Thanks for pointing this out. I agree that an empty
trial->exclusive_cpus is enough to skip the v2 sibling walk, even when
the mask has changed or the cpuset is a partition root.
Would you mind if I picked this up and sent follow-up patches to
simplify the v2 check and move the v1 sibling walk into cpuset-v1.c as
you suggested?
Since you also sketched out the implementation, would you prefer a
Suggested-by or a Co-developed-by tag? If you prefer Co-developed-by,
could you also send your Signed-off-by?
Thanks,
Chuyi