From: Waiman Long <longman@redhat.com>
To: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
juri.lelli@redhat.com, peterz@infradead.org, mingo@redhat.com,
vincent.guittot@linaro.org
Cc: linux-kernel@vger.kernel.org, tglx@kernel.org, will@kernel.org,
boqun@kernel.org, dietmar.eggemann@arm.com, rostedt@goodmis.org,
bsegall@google.com, mgorman@suse.de, vschneid@redhat.com,
kprateek.nayak@amd.com, luca.abeni@santannapisa.it,
mathieu.poirier@linaro.org, ridong.chen@linux.dev, tj@kernel.org,
hannes@cmpxchg.org, mkoutny@suse.com, shuah@kernel.org,
bigeasy@linutronix.de, clrkwllms@kernel.org,
parri.andrea@gmail.com, m.szyprowski@samsung.com,
kayracizmeci@gmail.com, weihengqiuu@gmail.com,
cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-rt-devel@lists.linux.dev, stable@vger.kernel.org
Subject: Re: [PATCH v5 19/29] cgroup/cpuset: Wait for an attach before rebuilding the sched domains
Date: Fri, 13 Nov 2026 10:58:57 -0500 [thread overview]
Message-ID: <4cdb4452-f11f-4e56-b03a-6d751dcfb62b@redhat.com> (raw)
In-Reply-To: <20261108185406.1549770-20-mathieu.desnoyers@efficios.com>
On 11/8/26 1:53 PM, Mathieu Desnoyers wrote:
> cpuset_can_attach() reserves the bandwidth of the deadline tasks it
> moves in the destination root domain. cpuset_attach() moves their
> bandwidth later, on the strength of that reservation.
>
> Commit 75f7a25ec6bb ("cgroup/cpuset: Prevent race between task attach
> and cpuset state change") lets cpuset_attach() rely on what
> cpuset_can_attach() set up. The writes of cpus, mems and cpus.partition
> wait for an attach in progress, in wait_attach_done_lock(). CPU hotplug
> cannot run in between either: it takes cpus_write_lock, and the attach
> holds cpus_read_lock throughout.
>
> But a rebuild of the sched domains does not wait. A write of
> sched_rt_runtime_us and the energy model rebuild them through
> rebuild_sched_domains(). The cgroup v1 writes of sched_load_balance and
> sched_relax_domain_level, and hk_sd_workfn(), use cpuset_full_lock().
>
> In this example, a rebuild drops the reservation. T is a deadline task
> asleep in root domain R. Thread A moves T to a partition root P, which
> has a root domain of its own. Thread B writes sched_rt_runtime_us:
>
> A B
>
> cpuset_can_attach()
> takes cpuset_mutex
> cpuset_reserve_dl_bw()
> P's total_bw += T's bandwidth
> drops cpuset_mutex
> rebuild_sched_domains()
> takes cpuset_mutex
> dl_rebuild_rd_accounting()
> clears R's and P's total_bw
> counts T in R (not moved yet)
> drops cpuset_mutex
> cpuset_attach()
> takes cpuset_mutex
> cpuset_attach_task(T)
> set_cpus_allowed_dl(T)
> R's total_bw -= T's bandwidth
> records a CPU of P
>
> P now lacks the bandwidth of T, and admits deadline tasks beyond its
> capacity. When T leaves P, P's total_bw underflows. In testing, the
> race left a destination admitting 3 tasks of 10% too many, or P
> admitting no task at all.
>
> Fix this by waiting for an attach in progress in
> rebuild_sched_domains_cpuslocked() and in cpuset_full_lock(). The
> latter then takes its locks in the order of the writes of cpus and
> cpus.partition. A rebuild from CPU hotplug finds no attach in progress.
> The online, offline and kill of a cpuset never wait: they hold
> cgroup_mutex, which the attach holds throughout.
>
> The move_vs_rebuild case of deadline_bw_test, added later in this
> series, tests this race. Without this patch, it fails in 5 of 8 VMs with
> 8 CPUs. With it, the case passes in 8 of 8. The next patch needs this
> wait too: without it, it would give back a reservation that a rebuild
> dropped already.
>
> Found by the Sashiko AI reviews of v2 (the race) and v3 (the cgroup v1
> writes) of this series.
>
> Fixes: 2ef269ef1ac0 ("cgroup/cpuset: Free DL BW in case can_attach() fails")
> Cc: stable@vger.kernel.org
> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> ---
> kernel/cgroup/cpuset.c | 16 +++++++++++++---
> 1 file changed, 13 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index 194a438e25c3..51b6be460794 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
> @@ -296,17 +296,20 @@ void lockdep_assert_cpuset_lock_held(void)
> lockdep_assert_held(&cpuset_mutex);
> }
>
> +static void wait_attach_done_lock(void) __acquires(&cpuset_mutex);
> +
> /**
> * cpuset_full_lock - Acquire full protection for cpuset modification
> *
> * Takes both CPU hotplug read lock (cpus_read_lock()) and cpuset mutex
> - * to safely modify cpuset data.
> + * to safely modify cpuset data. Waits for an attach in progress before
> + * it takes cpuset mutex.
> */
> void cpuset_full_lock(void)
> {
> mutex_lock(&cpuset_top_mutex);
> cpus_read_lock();
> - mutex_lock(&cpuset_mutex);
> + wait_attach_done_lock();
> }
>
> void cpuset_full_unlock(void)
> @@ -1005,7 +1008,14 @@ void rebuild_sched_domains_locked(void)
>
> static void rebuild_sched_domains_cpuslocked(void)
> {
> - mutex_lock(&cpuset_mutex);
> + /*
> + * Wait for an attach in progress. cpuset_can_attach() has reserved
> + * the deadline bandwidth of the moving tasks in their new root
> + * domain, and cpuset_attach() moves it on the strength of that
> + * reservation. A rebuild of the accounting in between would drop
> + * the reservation.
> + */
> + wait_attach_done_lock();
> rebuild_sched_domains_locked();
> mutex_unlock(&cpuset_mutex);
> }
I agree that rebuild_sched_domains() should wait for an in-progress
attach operation. The change to cpuset_full_lock(), however, is not
really necessary. This cpuset_full_lock() is now mainly used for
operations like cpuset_css_online() that are initiated from the cgroup
side where cgroup_mutex should have been held. IOW, an attach operation
which requires holding cgroup_mutex cannot happen in parallel. The only
exception is hk_sd_workfn() which is a work item for updating the
housekeeping cpumasks from hotplug which has no dependency on an attach
operation.
Cheers,
Longman
next prev parent reply other threads:[~2026-11-13 15:59 UTC|newest]
Thread overview: 89+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-08 18:53 [PATCH v5 00/29] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 01/29] sched: Fix cpuset_mutex double unlock in __sched_setscheduler() Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 02/29] sched: Refuse SCHED_FLAG_KEEP_PARAMS that would change the policy or the class Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 03/29] sched/deadline: Do not admit bandwidth for parameters that are kept Mathieu Desnoyers
2026-11-09 12:42 ` Juri Lelli
2026-11-09 13:16 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 04/29] sched: Let a task boosted by a deadline waiter change its policy Mathieu Desnoyers
2026-11-09 13:38 ` Juri Lelli
2026-11-08 18:53 ` [PATCH v5 05/29] sched/deadline: Do not divide by zero active CPUs of a root domain Mathieu Desnoyers
2026-11-09 14:30 ` Juri Lelli
2026-11-09 14:57 ` Mathieu Desnoyers
2026-11-09 15:09 ` Juri Lelli
2026-11-09 15:22 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 06/29] sched: Take cpuset_mutex for a deadline policy change only with pi Mathieu Desnoyers
2026-11-09 15:06 ` Juri Lelli
2026-11-09 15:22 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 07/29] sched/deadline: Do not lose a throttled task switched out and back Mathieu Desnoyers
2026-11-09 16:05 ` Juri Lelli
2026-11-09 16:29 ` Mathieu Desnoyers
2026-11-09 18:55 ` Mathieu Desnoyers
2026-11-09 18:56 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 08/29] sched/deadline: Take a deadline boost from the waiter's rt_mutex node Mathieu Desnoyers
2026-11-10 11:14 ` Juri Lelli
2026-11-10 13:48 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 09/29] sched/deadline: Reset the deadline and the runtime with the parameters Mathieu Desnoyers
2026-11-10 13:18 ` Juri Lelli
2026-11-10 13:41 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 10/29] sched/deadline: Reset the donor's runtime at a deboost in the class Mathieu Desnoyers
2026-11-08 19:17 ` sashiko-bot
2026-11-09 2:56 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 11/29] sched/deadline: Release the bandwidth of a task that leaves the class asleep Mathieu Desnoyers
2026-11-10 19:47 ` Juri Lelli
2026-11-10 20:26 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 12/29] sched/deadline: Reject SCHED_DEADLINE on exited tasks Mathieu Desnoyers
2026-11-11 6:55 ` Juri Lelli
2026-11-08 18:53 ` [PATCH v5 13/29] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-11 12:59 ` Juri Lelli
2026-11-11 15:35 ` Mathieu Desnoyers
2026-11-11 16:19 ` Peter Zijlstra
2026-11-11 19:19 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 14/29] sched/deadline: Fix this_bw when a task is changed during a remote wakeup Mathieu Desnoyers
2026-11-11 13:49 ` Juri Lelli
2026-11-08 18:53 ` [PATCH v5 15/29] sched/deadline: Rebuild the root domains' accounting from the task list Mathieu Desnoyers
2026-11-11 14:24 ` Juri Lelli
2026-11-13 13:31 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 16/29] sched/deadline: Record the root domain holding a task's bandwidth Mathieu Desnoyers
2026-11-11 15:54 ` Juri Lelli
2026-11-13 13:33 ` Mathieu Desnoyers
2026-11-13 15:06 ` Juri Lelli
2026-11-08 18:53 ` [PATCH v5 17/29] sched/deadline: Leave a dying task out of a rebuild of the accounting Mathieu Desnoyers
2026-11-12 6:40 ` Juri Lelli
2026-11-13 13:34 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 18/29] sched/deadline: Move a task's bandwidth only where room was made Mathieu Desnoyers
2026-11-08 19:30 ` sashiko-bot
2026-11-09 2:57 ` Mathieu Desnoyers
2026-11-12 7:03 ` Juri Lelli
2026-11-13 13:35 ` Mathieu Desnoyers
2026-11-13 15:04 ` Andrea Parri
2026-11-13 15:20 ` Mathieu Desnoyers
2026-11-13 15:49 ` Andrea Parri
2026-11-13 4:37 ` Waiman Long
2026-11-13 13:51 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 19/29] cgroup/cpuset: Wait for an attach before rebuilding the sched domains Mathieu Desnoyers
2026-11-12 7:50 ` Juri Lelli
2026-11-13 15:58 ` Waiman Long [this message]
2026-11-08 18:53 ` [PATCH v5 20/29] cgroup/cpuset: Give back the bandwidth an attach did not move Mathieu Desnoyers
2026-11-12 9:19 ` Juri Lelli
2026-11-13 13:12 ` Mathieu Desnoyers
2026-11-13 15:57 ` Juri Lelli
2026-11-08 18:53 ` [PATCH v5 21/29] cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to Mathieu Desnoyers
2026-11-08 19:32 ` sashiko-bot
2026-11-09 2:57 ` Mathieu Desnoyers
2026-11-08 18:53 ` [PATCH v5 22/29] sched/deadline: Make a rebuild use the CPU recorded with a task's bandwidth Mathieu Desnoyers
2026-11-08 19:25 ` sashiko-bot
2026-11-09 2:58 ` Mathieu Desnoyers
2026-11-08 18:54 ` [PATCH v5 23/29] sched/deadline: Move a task's bandwidth when it must leave its CPU Mathieu Desnoyers
2026-11-08 19:41 ` sashiko-bot
2026-11-09 2:58 ` Mathieu Desnoyers
2026-11-08 18:54 ` [PATCH v5 24/29] sched/deadline: Cancel the replenishment timer with the parameters Mathieu Desnoyers
2026-11-12 9:37 ` Juri Lelli
2026-11-08 18:54 ` [PATCH v5 25/29] sched/deadline: Count the bandwidth a boosted task is given Mathieu Desnoyers
2026-11-12 10:29 ` Juri Lelli
2026-11-08 18:54 ` [PATCH v5 26/29] sched/debug: Show the deadline bandwidth of each runqueue Mathieu Desnoyers
2026-11-12 10:36 ` Juri Lelli
2026-11-13 13:36 ` Mathieu Desnoyers
2026-11-13 15:04 ` Juri Lelli
2026-11-08 18:54 ` [PATCH v5 27/29] selftests/sched: Add a test of SCHED_DEADLINE policy round trips Mathieu Desnoyers
2026-11-08 18:54 ` [PATCH v5 28/29] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-08 18:54 ` [PATCH v5 29/29] selftests/sched: Add a test of SCHED_DEADLINE bandwidth accounting 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=4cdb4452-f11f-4e56-b03a-6d751dcfb62b@redhat.com \
--to=longman@redhat.com \
--cc=bigeasy@linutronix.de \
--cc=boqun@kernel.org \
--cc=bsegall@google.com \
--cc=cgroups@vger.kernel.org \
--cc=clrkwllms@kernel.org \
--cc=dietmar.eggemann@arm.com \
--cc=hannes@cmpxchg.org \
--cc=juri.lelli@redhat.com \
--cc=kayracizmeci@gmail.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=luca.abeni@santannapisa.it \
--cc=m.szyprowski@samsung.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=mathieu.poirier@linaro.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=mkoutny@suse.com \
--cc=parri.andrea@gmail.com \
--cc=peterz@infradead.org \
--cc=ridong.chen@linux.dev \
--cc=rostedt@goodmis.org \
--cc=shuah@kernel.org \
--cc=stable@vger.kernel.org \
--cc=tglx@kernel.org \
--cc=tj@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=weihengqiuu@gmail.com \
--cc=will@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®