mirror of https://lore.kernel.org/lkml/
 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 v12 17/43] cgroup/cpuset: Refuse to take a deadline task out of its root domain
Date: Fri, 27 Nov 2026 16:30:39 +0000	[thread overview]
Message-ID: <sashiko-outbox-237881@kernel.org> (raw)
In-Reply-To: <63398d7537bc4e331e08c6c998fa1506430cac57.1795771148.git.mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] cgroup/cpuset: stale def_root_domain span in
  dl_task_keeps_root_domain()
- [Medium] cgroup/cpuset: update_sibling_cpumasks() bypasses
  dl_tasks_keep_without()
- [Medium] cgroup/cpuset: missing dl_tasks_hold_bw() guard and subtree
  pruning in walks
--

commit c6b54021eb5d4c0abcdec9cdb08c630e44667111
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 change would move a SCHED_DEADLINE task out of the root domain
that accounts for its bandwidth without performing admission control.

> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index 9461277ed0fa3..62f0c7e6a97b0 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
[ ... ]
> @@ -1148,6 +1181,96 @@ static void effective_cpus_below(struct cpuset *cp, struct cpuset *from,
>  	}
>  }
>  
> +/*
> + * A scratch mask 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;
> +
> +/*
> + * 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 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)
> +{
> +	struct cpumask *new_cpus = dl_keep_cpus, *pending = NULL;
> +	struct cgroup_subsys_state *pos_css;
> +	struct cpuset *cp;
> +	bool keep = true;
> +
> +	lockdep_assert_cpuset_lock_held();
> +	/*
> +	 * The CPUs of @root can have changed before the rebuild of the
> +	 * sched domains, which ends the change. Then the root domain of a
> +	 * task of @root does not span them yet. The rebuild gives the CPUs
> +	 * of a root partition to one root domain. It gives those of an
> +	 * isolated partition, and of its valid children, to the default
> +	 * root domain.
> +	 */
> +	if (root->partition_root_state == PRS_ROOT ||
> +	    root->partition_root_state == PRS_ISOLATED) {
> +		pending = dl_pending_cpus;
> +		cpumask_copy(pending, root->effective_cpus);
> +		if (root == &top_cpuset)
> +			cpumask_and(pending, pending,
> +				    housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT));
> +	}
> +	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;
> +		keep = dl_tasks_keep_cpus(cp, new_cpus, pending);

[Severity: Medium]
With isolcpus=domain, can a deadline task counted in def_root_domain bypass
this check when a PRS_ISOLATED partition returns a housekeeping CPU to
top_cpuset while taking a boot-isolated CPU?

Suppose an isolated partition holding housekeeping CPU 0 swaps its CPUs in
one write to take boot-isolated CPU 1 (xcpus = {1}, gives = {0}), and a
deadline task in top_cpuset is counted in def_root_domain on CPU 1.  Before
rebuild_sched_domains_locked() runs at the end of the write,
dl_task_bw_span(p) returns def_root_domain.span, which still contains CPU
0.  Because new_cpus includes gives ({0}), cpumask_intersects_and(span,
new_mask, user) in dl_task_keeps_root_domain() matches on CPU 0 and returns
true.  The rebuild then moves CPU 0 back to top_cpuset's housekeeping root
domain while CPU 1 remains in def_root_domain under the isolated partition,
migrating the deadline task and its bandwidth into the housekeeping root
domain without admission control.

> +		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;
> +			}
> +			cpumask_and(new_cpus, xcpus, cpu_active_mask);
> +			effective_cpus_below(cp, part, new_cpus);
> +			if (cpumask_equal(cp->effective_cpus, new_cpus))
> +				continue;
> +			keep = dl_tasks_keep_cpus(cp, new_cpus,
> +				root->partition_root_state == PRS_ISOLATED ?
> +				pending : NULL);
> +			if (!keep)
> +				break;
> +		}
> +	}
> +	rcu_read_unlock();
> +	return keep;
> +}

[Severity: Medium]
Can update_sibling_cpumasks() cause dl_tasks_keep_without() to skip its
check when a growing parent partition has two invalid child partitions?

Suppose parent PRS_ROOT partition P has CPUs 0,2 and two PRS_INVALID_ROOT
children: C1 (cpus 0,2) and C2 (cpus 1, currently falling back to P's
effective_cpus 0,2 and holding 160% total deadline bandwidth admitted in
P's root domain).  When P grows to 0-3, update_cpumasks_hier() visits C1
first.  update_parent_effective_cpumask() validates C1 on 0,2 (while C2 is
still an invalid member of P, which passes via pending = {0-3}) and calls
update_sibling_cpumasks(P, C1, tmp).  That call runs
update_cpumasks_hier(C2, tmp, false), which treats C2 as a member because
cp == cs, setting C2->effective_cpus and its tasks' cpus_mask to {1}:

kernel/cgroup/cpuset.c:update_cpumasks_hier() {
    ...
		if ((cp != cs) && old_prs) {
			switch (parent->partition_root_state) {
			case PRS_ROOT:
			case PRS_ISOLATED:
				update_parent = true;
				break;
    ...
		spin_lock_irq(&callback_lock);
		cpumask_copy(cp->effective_cpus, tmp->new_cpus);
    ...
		cpuset_update_tasks_cpumask(cp, tmp->new_cpus);
    ...
}

When the outer update_cpumasks_hier(P, ...) loop then reaches C2 and calls
update_parent_effective_cpumask() -> dl_tasks_keep_without(P, C2, {1},
NULL, true), cpumask_equal(cp->effective_cpus, new_cpus) is already true
(and even inside dl_task_keeps_root_domain(), p->cpus_mask is already {1},
which no longer intersects P's pre-rebuild root domain span {0, 2}).  C2 is
then promoted to a valid 1-CPU PRS_ROOT partition holding 160% deadline
bandwidth.

[ ... ]

> @@ -2518,6 +2667,62 @@ static void partition_cpus_change(struct cpuset *cs, struct cpuset *trialcs,
>  	}
>  }
>  
> +/*
> + * Set @new_cpus to the effective CPUs that @cp gets if @cs takes the CPUs
> + * of @trialcs. @cp is @cs or one of its descendants.
> + */
> +static void trial_effective_cpus(struct cpuset *cp, struct cpuset *cs,
> +				 struct cpuset *trialcs, struct cpumask *new_cpus)
> +{
> +	struct cpuset *parent = parent_cs(cs);
> +
> +	if (!cpumask_and(new_cpus, trialcs->cpus_allowed, parent->effective_cpus) &&
> +	    is_in_v2_mode())
> +		cpumask_copy(new_cpus, parent->effective_cpus);
> +	effective_cpus_below(cp, cs, new_cpus);
> +}
> +
> +/*
> + * Return false if the CPUs of @trialcs make a deadline task of @cs, or of
> + * a descendant, 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. The task would run in a root domain that
> + * did not admit it. Only a move to another cpuset can take the task
> + * there. The move asks the other root domain for room.
> + *
> + * A partition root takes its CPUs from its parent, which checks the same
> + * for its own tasks: see dl_tasks_keep_without().
> + */
> +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);
> +		/*
> +		 * Test a mask that grows too. A task whose user affinity
> +		 * holds none of the old CPUs gets only the new ones.
> +		 */
> +		if (cpumask_equal(cp->effective_cpus, new_cpus))
> +			continue;
> +		keep = dl_tasks_keep_cpus(cp, new_cpus, NULL);
> +		if (!keep)
> +			break;
> +	}
> +	rcu_read_unlock();
> +	return keep;
> +}

[Severity: Medium]
Should dl_tasks_keep_root_domain() and dl_tasks_keep_without() check
!dl_tasks_hold_bw() before walking the hierarchy, and skip unchanged
subtrees with css_rightmost_descendant(pos_css)?

Because dl_tasks_hold_bw() is only checked inside dl_tasks_keep_cpus(),
every cpuset.cpus write and partition update walks the entire non-partition
subtree and runs effective_cpus_below() on every descendant even when no
deadline tasks exist or admission control is disabled.  In addition, when
cpumask_equal(cp->effective_cpus, new_cpus) is true in either function, a
plain continue still visits all of cp's descendants even though their
effective_cpus cannot change either.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795771147.git.mathieu.desnoyers@efficios.com?part=17

  reply	other threads:[~2026-11-27 16:30 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 14:35 [PATCH v12 00/43] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 01/43] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-27 15:22   ` sashiko-bot
2026-11-27 14:35 ` [PATCH v12 02/43] sched/deadline: Rebuild the root domains' accounting from the task list Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 03/43] sched/deadline: Record the root domain holding a task's bandwidth Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 04/43] sched/deadline: Leave a dying task out of a rebuild of the accounting Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 05/43] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-27 15:16   ` sashiko-bot
2026-11-27 14:35 ` [PATCH v12 06/43] selftests/sched: Test the root domain rebuild in deadline_bw_test Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 07/43] sched/deadline: Move a task's bandwidth only where room was made Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 08/43] cgroup/cpuset: Wait for an attach before rebuilding the sched domains Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 09/43] sched: Make deadline policy and affinity changes wait for a cpuset attach Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 10/43] cgroup/cpuset: Give back the bandwidth an attach did not move Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 11/43] cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 12/43] selftests/sched: Test cpuset moves in deadline_bw_test Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 13/43] sched/deadline: Make a rebuild count a task in the root domain it runs in Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 14/43] sched/deadline: Move a task's bandwidth to " Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 15/43] sched/deadline: Keep a task in the root domain that counts its bandwidth Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 16/43] sched/deadline: Keep the last CPU a task has in its root domain Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 17/43] cgroup/cpuset: Refuse to take a deadline task out of " Mathieu Desnoyers
2026-11-27 16:30   ` sashiko-bot [this message]
2026-11-27 14:35 ` [PATCH v12 18/43] selftests/sched: Test the root domain of a task in deadline_bw_test Mathieu Desnoyers
2026-11-27 16:10   ` sashiko-bot
2026-11-27 14:35 ` [PATCH v12 19/43] sched/deadline: Count only active CPUs when spreading extra_bw Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 20/43] sched/deadline: Do not truncate a bandwidth to 32 bits in __dl_add/sub() Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 21/43] rtmutex: Continue the chain walk when a waiter's deadline donor changes Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 22/43] rtmutex: Do not stop the chain walk at an owner whose top waiter changed Mathieu Desnoyers
2026-11-27 16:18   ` sashiko-bot
2026-11-27 14:35 ` [PATCH v12 23/43] sched/deadline: Hold a reference on the deadline donor of a boosted task Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 24/43] selftests/sched: Test the rtmutex chain walk in deadline_bw_test Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 25/43] sched/deadline: Count the 0-lag time from the next period Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 26/43] sched/deadline: Keep a departing constrained task until its " Mathieu Desnoyers
2026-11-27 17:00   ` sashiko-bot
2026-11-27 14:35 ` [PATCH v12 27/43] sched/deadline: Replenish a throttled task boosted while asleep Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 28/43] sched/deadline: Restore the owner's period at a deboost in the class Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 29/43] sched/deadline: Count the bandwidth a boosted task is given Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 30/43] sched/deadline: Start a period for a task that enters the class blocked Mathieu Desnoyers
2026-11-27 17:17   ` sashiko-bot
2026-11-27 14:35 ` [PATCH v12 31/43] sched/deadline: Restore the owner's period at a deboost out of the class Mathieu Desnoyers
2026-11-27 17:28   ` sashiko-bot
2026-11-27 14:35 ` [PATCH v12 32/43] sched/deadline: Start a period for a task requeued onto an rt_mutex Mathieu Desnoyers
2026-11-27 17:21   ` sashiko-bot
2026-11-27 14:36 ` [PATCH v12 33/43] selftests/sched: Test deadline boosts in deadline_bw_test Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 34/43] sched/deadline: Cancel the replenishment timer with the parameters Mathieu Desnoyers
2026-11-27 17:31   ` sashiko-bot
2026-11-27 14:36 ` [PATCH v12 35/43] sched/deadline: Cap the runtime left of a constrained task at dl_runtime Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 36/43] sched/deadline: Check the runtime of a task that comes back runnable Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 37/43] sched/deadline: Keep the debt of a task across a change of parameters Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 38/43] sched/deadline: Boost an owner that cannot run on its own reservation Mathieu Desnoyers
2026-11-27 18:07   ` sashiko-bot
2026-11-27 14:36 ` [PATCH v12 39/43] sched/deadline: Boost an owner throttled after its waiter blocked Mathieu Desnoyers
2026-11-27 17:58   ` sashiko-bot
2026-11-27 14:36 ` [PATCH v12 40/43] sched/deadline: Reschedule a running task throttled at its enqueue Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 41/43] sched/deadline: Run a boost held at its first refill on a donor period Mathieu Desnoyers
2026-11-27 18:07   ` sashiko-bot
2026-11-27 14:36 ` [PATCH v12 42/43] selftests/sched: Test parameter changes in deadline_bw_test Mathieu Desnoyers
2026-11-27 18:57   ` sashiko-bot
2026-11-27 14:36 ` [PATCH v12 43/43] selftests/sched: Share the helpers of the deadline tests Mathieu Desnoyers

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-237881@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®