All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>
Cc: linux-rt-devel@lists.linux.dev, tj@kernel.org,
	hannes@cmpxchg.org, mkoutny@suse.com,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v6 28/37] cgroup/cpuset: Refuse to take a deadline task out of its root domain
Date: Mon, 16 Nov 2026 20:08:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-217636@kernel.org> (raw)
In-Reply-To: <20261116190549.230119-29-mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] cgroup/cpuset: excpus clobbered via tmp->new_cpus in
  remote_cpus_update()
- [High] cgroup/cpuset: partition_xcpus_del() called on invalid
  partition in update_parent_effective_cpumask()
- [Medium] cgroup/cpuset: wrong descendant effective_cpus in
  dl_tasks_keep_without()
- [Medium] sched/deadline: user_cpus_ptr ignored in
  dl_task_keeps_root_domain()
- [Medium] cgroup/cpuset: dl_tasks_keep_root_domain() skips invalidated
  partitions
--

commit 2c62aa35faa810e650ef3867961558c694f57e4c
Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

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

This commit rejects cpuset CPU mask updates and invalidates partition roots
when the resulting effective CPUs would move a SCHED_DEADLINE task out of the
root domain accounting its bandwidth without admission control.

> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index ef5ad8e38f06f..5b4f7d4ef3ae2 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
[ ... ]
> @@ -1126,6 +1127,92 @@ static void update_partition_sd_lb(struct cpuset *cs, int old_prs)
[ ... ]
> +static bool dl_tasks_keep_without(struct cpuset *root, struct cpuset *part,
> +				  const struct cpumask *xcpus, bool enable,
> +				  struct cpumask *new_cpus)
> +{
> +	struct cgroup_subsys_state *pos_css;
> +	struct cpuset *cp, *a;
> +	bool keep = true;
> +
> +	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;
> +		}
> +		if (!cpumask_intersects(cp->effective_cpus, xcpus))
> +			continue;
> +		for (a = cp; ; a = parent_cs(a)) {
> +			if (cpumask_andnot(new_cpus, a->effective_cpus, xcpus) ||
> +			    a == root)
> +				break;
> +		}
> +		keep = dl_tasks_keep_cpus(cp, new_cpus);
> +		if (!keep)
> +			break;
> +	}
> +	if (keep && enable) {
> +		cpuset_for_each_descendant_pre(cp, pos_css, part) {
> +			if (cp != part && is_partition_valid(cp)) {
> +				pos_css = css_rightmost_descendant(pos_css);
> +				continue;
> +			}
> +			if (cpumask_subset(cp->effective_cpus, xcpus))
> +				continue;
> +			if (!cpumask_and(new_cpus, cp->effective_cpus, xcpus))
> +				cpumask_copy(new_cpus, xcpus);
> +			keep = dl_tasks_keep_cpus(cp, new_cpus);
> +			if (!keep)
> +				break;
> +		}
> +	}
> +	rcu_read_unlock();
> +	return keep;
> +}

[Severity: Medium]
Can dl_tasks_keep_without() miscompute new_cpus for descendant cpusets and
let a deadline task move to another root domain without admission control?

In the first loop, when cp loses all of its effective_cpus to xcpus and
inherits from an ancestor a above parent_cs(cp), new_cpus is set to
a->effective_cpus & ~xcpus without filtering top-down through the
cpus_allowed masks of the cpusets between a and cp as
compute_effective_cpumask() does. If root has effective_cpus {0, 1, 2}
(0-1 housekeeping, 2 isolated), child P has cpus_allowed {0}, and grandchild
C has cpus_allowed {2} (so C inherits effective_cpus {0}), a partition taking
xcpus {0} tests C against root's {1, 2} and sees housekeeping CPU 1, whereas
update_cpumasks_hier() gives P {1, 2} and gives C {2}, moving a deadline
task in C into the isolated root domain.

In the second loop (when enable is true), cp's new mask is derived from
cp->effective_cpus & xcpus instead of filtering xcpus & cpu_active_mask
top-down through each descendant's cpus_allowed. Because enabling part
switches its effective CPUs from cpus_allowed to effective_xcpus, a child C
whose cpus_allowed only intersects part->exclusive_cpus previously inherited
part->cpus_allowed, passes the cpumask_subset(cp->effective_cpus, xcpus)
check if part->cpus_allowed is a subset of xcpus, and then switches to
C->cpus_allowed once part is enabled; in addition, xcpus is not masked with
cpu_active_mask, so offline CPUs in xcpus can match def_root_domain->span.

[ ... ]

> @@ -1779,6 +1868,9 @@ static void remote_cpus_update(struct cpuset *cs, struct cpumask *xcpus,
>  		else if (prstate_housekeeping_conflict(prs, PRS_ROOT,
>  						       excpus, tmp->delmask))
>  			WRITE_ONCE(cs->prs_err, PERR_HKEEPING);
> +		else if (!dl_tasks_keep_without(&top_cpuset, cs, tmp->addmask,
> +						false, tmp->new_cpus))
> +			WRITE_ONCE(cs->prs_err, PERR_DLTASK);
>  		if (cs->prs_err)
>  			goto invalidate;
>  	}

[Severity: High]
Does passing tmp->new_cpus to dl_tasks_keep_without() clobber excpus before
it is copied to cs->effective_xcpus?

update_cpumasks_hier() calls remote_cpus_update() with excpus pointing to
tmp->new_cpus:

kernel/cgroup/cpuset.c:update_cpumasks_hier() {
    ...
			compute_excpus(cp, tmp->new_cpus);
			...
			rcu_read_unlock();
			remote_cpus_update(cp, NULL, tmp->new_cpus, tmp);
			rcu_read_lock();
    ...
}

When a descendant remote partition gains exclusive CPUs during
update_cpumasks_hier(), dl_tasks_keep_without() overwrites tmp->new_cpus
with top_cpuset's remaining effective CPUs, and remote_cpus_update() then
copies that clobbered mask from excpus into cs->effective_xcpus.

[ ... ]

> @@ -2013,6 +2109,15 @@ 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,
> +						  is_partition_invalid(cs),
> +						  tmp->new_cpus)) {
> +			/* As above: the partition becomes invalid. */
> +			part_error = PERR_DLTASK;
> +			deleting = false;
> +			adding = cpumask_and(tmp->addmask,
> +					     cs->effective_xcpus, parent->effective_xcpus);
>  		}
>  	} else {

[Severity: High]
Can setting adding here when is_partition_invalid(cs) is true call
partition_xcpus_del() on an already-invalid partition?

An invalid local partition root (old_prs < 0) with cpuset.cpus.exclusive set
keeps a non-empty cs->effective_xcpus even though those CPUs were not removed
from parent->effective_cpus. When updating cpuset.cpus or
cpuset.cpus.exclusive on that invalid partition takes the newmask branch and
dl_tasks_keep_without() returns false, adding becomes true while old_prs < 0,
so partition_xcpus_del(old_prs, parent, tmp->addmask) triggers
WARN_ON_ONCE(old_prs < 0) and corrupts subpartitions_cpus, isolated_cpus, and
parent->effective_cpus by releasing CPUs that cs never held. Guarding adding
with is_partition_valid(cs) avoids releasing unowned CPUs.

[ ... ]

> @@ -2496,6 +2604,66 @@ static void partition_cpus_change(struct cpuset *cs, struct cpuset *trialcs,
[ ... ]
> +static bool dl_tasks_keep_root_domain(struct cpuset *cs, struct cpuset *trialcs,
> +				      struct cpumask *new_cpus)
> +{
> +	struct cgroup_subsys_state *pos_css;
> +	struct cpuset *cp;
> +	bool keep = true;
> +
> +	if (is_partition_valid(cs))
> +		return true;
> +	rcu_read_lock();
> +	cpuset_for_each_descendant_pre(cp, pos_css, cs) {
> +		if (is_partition_valid(cp)) {
> +			pos_css = css_rightmost_descendant(pos_css);
> +			continue;
> +		}
> +		trial_effective_cpus(cp, cs, trialcs, new_cpus);
> +		if (cpumask_subset(cp->effective_cpus, new_cpus))
> +			continue;
> +		keep = dl_tasks_keep_cpus(cp, new_cpus);
> +		if (!keep)
> +			break;
> +	}
> +	rcu_read_unlock();
> +	return keep;
> +}
> +
>  /**
>   * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
>   * @cs: the cpuset to consider
> @@ -2528,6 +2696,11 @@ static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
>  	if (alloc_tmpmasks(&tmp))
>  		return -ENOMEM;
>  
> +	if (!dl_tasks_keep_root_domain(cs, trialcs, tmp.new_cpus)) {
> +		free_tmpmasks(&tmp);
> +		return -EBUSY;
> +	}
> +
>  	/*
>  	 * Check all the descendants in update_cpumasks_hier() if
>  	 * effective_xcpus is to be changed.

[Severity: Medium]
What happens when a write to cpuset.cpus (or cpuset.cpus.exclusive)
invalidates a valid partition root?

dl_tasks_keep_root_domain() runs before partition_cpus_change() and returns
true immediately when is_partition_valid(cs) is true, and
update_exclusive_cpumask() does not check dl_tasks_keep_root_domain() at all.
If cs is a valid root partition on a housekeeping CPU containing a deadline
task and a user writes a boot-isolated CPU to cs/cpuset.cpus,
partition_cpus_change() invalidates cs (PERR_HKEEPING), and
update_cpumasks_hier() recomputes cs->effective_cpus as a non-partition
cpuset on the isolated CPU; dl_rebuild_rd_accounting() then adds the task's
bandwidth to def_root_domain via dl_add_task_root_domain() without admission
control.

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index 096c0ec55cdc2..61b50266468f9 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c
> @@ -4955,6 +4955,25 @@ int dl_bw_deactivate(int cpu)
>  	return ret;
>  }
>  
> +/*
> + * Return false if @p has CPUs in the root domain that counts its
> + * bandwidth and @new_mask holds none of them. A cpuset refuses such a
> + * mask: only a move to another cpuset makes room in another root domain.
> + */
> +bool dl_task_keeps_root_domain(struct task_struct *p,
> +			       const struct cpumask *new_mask)
> +{
> +	const struct cpumask *span;
> +
> +	if (!dl_bandwidth_enabled() || !task_has_dl_policy(p))
> +		return true;
> +	guard(raw_spinlock_irqsave)(&p->pi_lock);
> +	guard(rcu)();
> +	span = dl_task_bw_span(p);
> +	return !span || !cpumask_intersects(span, p->cpus_ptr) ||
> +	       cpumask_intersects(span, new_mask);
> +}

[Severity: Medium]
Should dl_task_keeps_root_domain() account for p->user_cpus_ptr the way
__set_cpus_allowed_ptr() and dl_task_bw_reserve_cpu() do?

When cpuset_update_tasks_cpumask() calls set_cpus_allowed_ptr(task, new_cpus),
__set_cpus_allowed_ptr() restricts new_cpus to new_cpus & p->user_cpus_ptr
whenever their intersection is non-empty. Because dl_task_keeps_root_domain()
tests span against new_mask directly (and dl_tasks_keep_root_domain() and
dl_tasks_keep_without() skip cpusets where cpumask_subset(cp->effective_cpus,
new_cpus) holds), a deadline task counted in housekeeping span {0, 1} whose
p->user_cpus_ptr only intersects new_mask on isolated CPU {2} passes this
check (or the subset check when widening effective_cpus from {0, 1} to
{0, 1, 2}) and has its affinity restricted to {2} in the isolated root domain
without admission control. Intersecting new_mask with p->user_cpus_ptr when
cpumask_intersects(new_mask, p->user_cpus_ptr) is true and checking tasks even
when new_cpus grows would match __set_cpus_allowed_ptr().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116190549.230119-1-mathieu.desnoyers@efficios.com?part=28

  reply	other threads:[~2026-11-16 20:08 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 19:05 [PATCH v6 00/37] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 01/37] sched: Refuse SCHED_FLAG_KEEP_PARAMS that would change the policy or the class Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 02/37] sched/deadline: Do not admit bandwidth for parameters that are kept Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 03/37] sched: Let a task boosted by a deadline waiter change its policy Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 04/37] sched/deadline: Do not divide by zero active CPUs of a root domain Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 05/37] sched/deadline: Refuse dl_server debugfs writes while the rq is offline Mathieu Desnoyers
2026-11-16 19:30   ` sashiko-bot
2026-11-17 14:14     ` Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 06/37] sched: Take cpuset_mutex for a deadline policy change only with pi Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 07/37] sched/deadline: Do not lose a throttled task switched out and back Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 08/37] sched/deadline: Do not replenish a boost from cleared parameters Mathieu Desnoyers
2026-11-16 19:42   ` sashiko-bot
2026-11-16 19:05 ` [PATCH v6 09/37] sched/deadline: Reset the deadline and the runtime with the parameters Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 10/37] sched/deadline: Reset the donor's runtime at a deboost in the class Mathieu Desnoyers
2026-11-16 19:38   ` sashiko-bot
2026-11-17 14:15     ` Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 11/37] sched/deadline: Release the bandwidth of a task that leaves the class asleep Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 12/37] sched/deadline: Reject SCHED_DEADLINE on exited tasks Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 13/37] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 14/37] sched/deadline: Fix this_bw when a task is changed during a remote wakeup Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 15/37] sched/deadline: Rebuild the root domains' accounting from the task list Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 16/37] sched/deadline: Record the root domain holding a task's bandwidth Mathieu Desnoyers
2026-11-16 19:44   ` sashiko-bot
2026-11-17 14:15     ` Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 17/37] sched/deadline: Leave a dying task out of a rebuild of the accounting Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 18/37] sched/deadline: Check SCHED_DEADLINE affinity under the rq lock Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 19/37] sched/deadline: Move a task's bandwidth only where room was made Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 20/37] cgroup/cpuset: Wait for an attach before rebuilding the sched domains Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 21/37] sched: Make deadline policy and affinity changes wait for a cpuset attach Mathieu Desnoyers
2026-11-16 19:31   ` sashiko-bot
2026-11-16 19:05 ` [PATCH v6 22/37] cgroup/cpuset: Give back the bandwidth an attach did not move Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 23/37] cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to Mathieu Desnoyers
2026-11-16 19:56   ` sashiko-bot
2026-11-16 19:05 ` [PATCH v6 24/37] sched/deadline: Make a rebuild count a task in the root domain it runs in Mathieu Desnoyers
2026-11-16 20:01   ` sashiko-bot
2026-11-16 19:05 ` [PATCH v6 25/37] sched/deadline: Move a task's bandwidth to " Mathieu Desnoyers
2026-11-16 19:47   ` sashiko-bot
2026-11-16 19:05 ` [PATCH v6 26/37] sched/deadline: Keep a task in the root domain that counts its bandwidth Mathieu Desnoyers
2026-11-16 19:50   ` sashiko-bot
2026-11-17 14:16     ` Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 27/37] sched/deadline: Keep the last CPU a task has in its root domain Mathieu Desnoyers
2026-11-16 19:51   ` sashiko-bot
2026-11-16 19:05 ` [PATCH v6 28/37] cgroup/cpuset: Refuse to take a deadline task out of " Mathieu Desnoyers
2026-11-16 20:08   ` sashiko-bot [this message]
2026-11-17 14:17     ` Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 29/37] sched/deadline: Cancel the replenishment timer with the parameters Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 30/37] sched/deadline: Count the bandwidth a boosted task is given Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 31/37] sched/deadline: Start a period for a task that enters the class blocked Mathieu Desnoyers
2026-11-16 20:12   ` sashiko-bot
2026-11-17 14:18     ` Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 32/37] sched/deadline: Check the runtime of a task that comes back runnable Mathieu Desnoyers
2026-11-16 20:08   ` sashiko-bot
2026-11-16 19:05 ` [PATCH v6 33/37] sched/deadline: Check the runtime of a task whose parameters change Mathieu Desnoyers
2026-11-16 20:04   ` sashiko-bot
2026-11-16 19:05 ` [PATCH v6 34/37] sched/debug: Show the deadline bandwidth of each runqueue Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 35/37] selftests/sched: Add a test of SCHED_DEADLINE policy round trips Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 36/37] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-16 19:05 ` [PATCH v6 37/37] selftests/sched: Add a test of SCHED_DEADLINE bandwidth accounting Mathieu Desnoyers
2026-11-16 19:49   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-217636@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mkoutny@suse.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.