Re: [PATCH v13 22/36] cgroup/cpuset: Refuse to take a deadline task out of its root domain

From: sashiko-bot

Date: Tue Dec 01 2026 - 14:29:08 EST


Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] cgroup/cpuset: unchecked CPU return on isolated partition
invalidation
- [Medium] cgroup/cpuset: missing root-domain check when shrinking
partitions
- [Low] cgroup/cpuset: missing early dl_tasks_hold_bw() guard before
cgroup walks
- [Low] cgroup/cpuset: missing subtree pruning on unchanged
effective_cpus
--

commit 024194fef53c8203e8e662dd5e236d8d4627f15b
Author: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>

cgroup/cpuset: Refuse to take a deadline task out of its root domain

This commit refuses cpuset cpumask updates and invalidates or rejects
partition updates that would leave a SCHED_DEADLINE task with no CPUs in
the root domain accounting for its bandwidth.

> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index 9461277ed0fa3..c811d7de35a59 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
[ ... ]
> @@ -1126,6 +1127,42 @@ static void update_partition_sd_lb(struct cpuset *cs, int old_prs)
> cpuset_force_rebuild();
> }
>
> +/*
> + * Return false if the CPUs of @new_cpus make a deadline task of @cp leave
> + * the root domain that counts its bandwidth. @pending, if not NULL, holds
> + * the CPUs that the rebuild at the end of the change gives to that root
> + * domain. @leaving, if not NULL, holds CPUs that the rebuild takes out of
> + * the default root domain. Called under rcu_read_lock(), which it drops
> + * and takes again.
> + */
> +static bool dl_tasks_keep_cpus(struct cpuset *cp, const struct cpumask *new_cpus,
> + const struct cpumask *pending,
> + const struct cpumask *leaving)
> +{
> + struct css_task_iter it;
> + struct task_struct *task;
> + bool keep = true;
> +
> + if (!dl_tasks_hold_bw() || !css_tryget_online(&cp->css))
> + return true;

[Severity: Low]
Could dl_tasks_hold_bw() be checked on entry to dl_tasks_keep_without()
and dl_tasks_keep_root_domain() instead of only inside
dl_tasks_keep_cpus()? Because cpuset_mutex prevents any task from
acquiring deadline bandwidth during the check, testing it only at the leaf
causes every cpuset mask and partition update to walk all descendant
cpusets and recompute effective_cpus_below() even when no task in the
system holds deadline bandwidth.

> + rcu_read_unlock();
> +
> + css_task_iter_start(&cp->css, 0, &it);
> + while (keep && (task = css_task_iter_next(&it))) {
> + /* The top cpuset leaves the mask of these tasks alone. */
> + if (cp == &top_cpuset &&
> + (task->flags & (PF_KTHREAD | PF_NO_SETAFFINITY)))
> + continue;
> + keep = dl_task_keeps_root_domain(task, new_cpus, pending,
> + leaving);
> + }
> + css_task_iter_end(&it);
> +
> + rcu_read_lock();
> + css_put(&cp->css);
> + return keep;
> +}
> +
> /*
> * Set @new_cpus to the effective CPUs that @cp gets. On entry, @new_cpus
> * holds those that @from gets. @from is @cp or one of its ancestors. Work
> @@ -1148,6 +1185,119 @@ static void effective_cpus_below(struct cpuset *cp, struct cpuset *from,
> }
> }
>
> +/*
> + * Scratch masks for dl_tasks_keep_without(). Its callers still need
> + * each mask of their struct tmpmasks after the call. Protected by
> + * cpuset_mutex.
> + */
> +static cpumask_var_t dl_keep_cpus, dl_pending_cpus, dl_leaving_cpus;
> +
> +/*
> + * Return false if a partition that takes the CPUs of @xcpus makes a
> + * deadline task leave the root domain that counts its bandwidth. A cpuset
> + * can hold CPUs of two root domains: with isolcpus="domain", or on cgroup
> + * v1 with sched_load_balance off. Such a task would run in a root domain
> + * that did not admit it.
> + *
> + * @root is the partition root that gives the CPUs. @part is the cpuset
> + * that takes them. In the same change, @part can give the CPUs of @gives
> + * back to @root. @gives is NULL if it gives none back.
> + *
> + * Work out the new CPUs of each cpuset of @root from the top down, as
> + * update_cpumasks_hier() does, down to the next partition roots. If
> + * @enable, @part becomes a valid partition root. Then do the same for its own
> + * cpusets, starting from the active CPUs of @xcpus.
> + */
> +static bool dl_tasks_keep_without(struct cpuset *root, struct cpuset *part,
> + const struct cpumask *xcpus,
> + const struct cpumask *gives, bool enable)
> +{
[ ... ]
> + rcu_read_lock();
> + cpuset_for_each_descendant_pre(cp, pos_css, root) {
> + if (cp == part || (cp != root && is_partition_valid(cp))) {
> + pos_css = css_rightmost_descendant(pos_css);
> + continue;
> + }
> + /* The CPUs that @root has after the change. */
> + cpumask_copy(new_cpus, root->effective_cpus);
> + if (gives) {
> + cpumask_or(new_cpus, new_cpus, gives);
> + cpumask_and(new_cpus, new_cpus, cpu_active_mask);
> + }
> + cpumask_andnot(new_cpus, new_cpus, xcpus);
> + effective_cpus_below(cp, root, new_cpus);
> + if (cpumask_equal(cp->effective_cpus, new_cpus))
> + continue;

[Severity: Low]
When cpumask_equal(cp->effective_cpus, new_cpus) is true here, in the
second loop below, and in dl_tasks_keep_root_domain(), should the loop
advance pos_css = css_rightmost_descendant(pos_css) before continuing?
Since valid child partitions are already skipped and non-partition
descendants derive their effective_cpus only from their parent's
effective_cpus and their own unchanged cpus_allowed, no descendant in cp's
subtree can change effective_cpus, yet a plain continue walks every
descendant in unaffected subtrees and runs effective_cpus_below() on each
of them.

[ ... ]
> @@ -2035,6 +2194,21 @@ static int update_parent_effective_cpumask(struct cpuset *cs, int cmd,
> deleting = false;
> adding = cpumask_and(tmp->addmask,
> cs->effective_xcpus, parent->effective_xcpus);
> + } else if (deleting &&
> + !dl_tasks_keep_without(parent, cs, tmp->delmask,
> + adding ? tmp->addmask : NULL,
> + is_partition_invalid(cs))) {
> + /*
> + * Make the partition invalid, as for PERR_NOCPUS above.
> + * Only a valid partition holds CPUs of its parent to
> + * give back.
> + */
> + part_error = PERR_DLTASK;
> + deleting = false;
> + if (is_partition_valid(cs))
> + adding = cpumask_and(tmp->addmask,
> + cs->effective_xcpus,
> + parent->effective_xcpus);
> }

[Severity: Medium]
When an update to a valid PRS_ISOLATED partition invalidates it, can
returning all of cs->effective_xcpus to a non-isolated parent move a
deadline task out of the default root domain without being caught by
dl_tasks_keep_given_back()?

dl_tasks_keep_given_back() only checks the CPUs in
cs->effective_xcpus & ~trialcs->effective_xcpus before
partition_cpus_change() runs. If a valid isolated partition holding a
housekeeping CPU is grown (or otherwise updated without dropping that
housekeeping CPU in trialcs), dl_tasks_keep_given_back() returns true,
and if partition_cpus_change() then invalidates cs (for example via
PERR_DLTASK or PERR_NOCPUS here or in remote_cpus_update(), or via
validate_partition() or prstate_housekeeping_conflict()), all of
cs->effective_xcpus are returned to the parent and taken out of the
default root domain on the sched domain rebuild, allowing a deadline task
counted in the default root domain to move to the parent's root domain
without admission control.

[ ... ]
> +/*
> + * Return false if @cs gives housekeeping CPUs back to its parent, and a
> + * deadline task would then leave the default root domain: see
> + * dl_tasks_keep_without(). @cs is checked only if it is a valid isolated
> + * partition root, and its parent is not isolated. Making @cs invalid
> + * would not help: it would give these CPUs back all the same, and the
> + * task would still leave. So the caller refuses the write to cpuset.cpus
> + * or cpuset.cpus.exclusive. The CPUs that @cs takes in the same change
> + * count too. If @cs gives back only CPUs that isolcpus="domain" isolates,
> + * they stay in the default root domain. update_parent_effective_cpumask()
> + * then checks the CPUs that @cs takes.
> + */
> +static bool dl_tasks_keep_given_back(struct cpuset *cs, struct cpuset *trialcs,
> + struct tmpmasks *tmp)
> +{
> + struct cpuset *parent = is_remote_partition(cs) ? &top_cpuset : parent_cs(cs);
> +
> + if (cs->partition_root_state != PRS_ISOLATED ||
> + parent->partition_root_state == PRS_ISOLATED ||
> + !cpumask_andnot(tmp->delmask, cs->effective_xcpus, trialcs->effective_xcpus) ||
> + !cpumask_intersects(tmp->delmask, housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT)))
> + return true;
> + cpumask_andnot(tmp->addmask, trialcs->effective_xcpus, cs->effective_xcpus);
> + return dl_tasks_keep_without(parent, cs, tmp->addmask, tmp->delmask, false);
> +}

[Severity: Medium]
Can shrinking a partition still move a deadline task in the parent's
hierarchy out of its root domain in cases that dl_tasks_keep_given_back()
skips?

When a partition only gives CPUs back without taking new ones,
update_parent_effective_cpumask() and remote_cpus_update() do not call
dl_tasks_keep_without() because deleting (or adding in
remote_cpus_update()) is false. As a result, three shrink paths bypass
the root-domain check:

1. A valid PRS_ROOT partition shrinks and returns a housekeeping CPU to
top_cpuset: a deadline task in top_cpuset (or a member descendant)
counted in the default root domain on an isolcpus="domain" CPU, whose
user_cpus_ptr or child cpuset.cpus contains only the returned
housekeeping CPU, becomes restricted to that CPU and moves to
top_cpuset's root domain.

2. Symmetrically, a valid PRS_ISOLATED partition shrinks and returns only
an isolcpus="domain" CPU to top_cpuset: a deadline task counted in
top_cpuset's housekeeping root domain whose user_cpus_ptr or child
cpuset.cpus contains only the returned isolated CPU moves to the
default root domain.

3. Shrinking cpuset.cpus or cpuset.cpus.exclusive on an ancestor cpuset
shrinks a descendant PRS_ISOLATED partition during
update_cpumasks_hier(), returning housekeeping CPUs to its non-isolated
parent without dl_tasks_keep_given_back() checking the descendant
partition.

--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796146461.git.mathieu.desnoyers@xxxxxxxxxxxx?part=22