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 v8 15/33] sched/deadline: Keep a task in the root domain that counts its bandwidth
Date: Thu, 19 Nov 2026 20:55:10 +0000	[thread overview]
Message-ID: <sashiko-outbox-224072@kernel.org> (raw)
In-Reply-To: <35ed0190c119025d684ab89cd51e4805c7f7dbad.1795116791.git.mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Medium] sched/deadline: missing seqlock memory barriers in
  dl_task_bw_span()
- [Medium] sched/deadline: race on rd->span returned by
  dl_task_bw_span()
- [Medium] sched: select_fallback_rq() overwrites fallback mask when
  state is fail
- [Medium] sched/deadline: dl_task_offline_migration() ignores span when
  cpus_ptr inactive
- [Medium] sched/deadline: lost bandwidth in dl_task_offline_migration()
  during rebuild
- [Medium] sched/deadline: dl_task_bw_span() returns NULL for
  runtime-isolated CPUs
--

commit 13af0f791d03e3d95add27f78ad7796e24778c3d
Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

sched/deadline: Keep a task in the root domain that counts its bandwidth

This commit adds dl_task_bw_span() and updates CPU selection and offline
migration paths to keep SCHED_DEADLINE tasks in the root domain that
accounts for their bandwidth.

> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 7615ba02d52b3..fe2919be3775b 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c

[ ... ]

> @@ -3686,6 +3721,26 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
>  	}
>  
>  	for (;;) {
> +		/* The mask is wider now: look again in that root domain. */
> +		if (span && state != cpuset) {
> +			for_each_cpu_and(dest_cpu, span, p->cpus_ptr) {
> +				if (is_cpu_allowed(p, dest_cpu))
> +					goto out;
> +			}
> +			/*
> +			 * The wider mask has no active CPU in that root
> +			 * domain. Allow the CPUs of the root domain instead.
> +			 * At an offline, the cpuset update comes next and sets
> +			 * the real mask.
> +			 */
> +			if (!span_forced && cpumask_intersects(span, cpu_active_mask) &&
> +			    cpumask_subset(span, task_cpu_possible_mask(p))) {
> +				span_forced = true;
> +				set_cpus_allowed_force(p, span);
> +				continue;
> +			}
> +		}
> +
>  		/* Any allowed, online CPU? */
>  		for_each_cpu(dest_cpu, p->cpus_ptr) {
>  			if (!is_cpu_allowed(p, dest_cpu))

[Severity: Medium]
Can this overwrite task_cpu_fallback_mask(p) with span when state is
already fail, and hit BUG() without ever scanning the fallback mask?

When cpuset_cpus_allowed_fallback(p) returns false (such as under cgroup
v1 or CONFIG_CPUSETS=n), case cpuset falls through to case possible,
which installs task_cpu_fallback_mask(p) and sets state = fail.  On the
next iteration, state != cpuset is true, so if span has no CPU in
task_cpu_fallback_mask(p) (for example, when span is def_root_domain.span
under isolcpus=domain), this block replaces p->cpus_mask with span and
continues with state == fail.  If no CPU in span satisfies
is_cpu_allowed() on the following pass (for instance, an unbound deadline
kthread when the active CPU in span has cpu_dying() set), the loop
reaches case fail: BUG() without testing task_cpu_fallback_mask(p).

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index fb8f32a31cf91..c33e73e920ebb 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c

[ ... ]

> @@ -927,18 +929,36 @@ static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
>  
>  static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
>  {
> +	const struct cpumask *span;
>  	struct rq *later_rq = NULL;
>  	struct dl_bw *dl_b;
>  
>  	later_rq = find_lock_later_rq(p, rq);
> +	/*
> +	 * find_lock_later_rq() looks in the root domain of @rq. Once @rq is
> +	 * offline, a rebuild attaches it to def_root_domain. That root domain
> +	 * may not count @p. Do not take @p out of the root domain that counts
> +	 * it while @p has an active CPU there.
> +	 */
> +	span = dl_task_bw_span(p);
> +	if (later_rq && span && !cpumask_test_cpu(later_rq->cpu, span) &&
> +	    cpumask_first_and_and(cpu_active_mask, p->cpus_ptr, span) < nr_cpu_ids) {
> +		double_unlock_balance(rq, later_rq);
> +		later_rq = NULL;
> +	}
>  	if (!later_rq) {
>  		int cpu;
>  
>  		/*
>  		 * If we cannot preempt any rq, fall back to pick any
> -		 * online CPU:
> +		 * online CPU. Try the root domain that counts @p first.
>  		 */
> -		cpu = cpumask_any_and(cpu_active_mask, p->cpus_ptr);
> +		cpu = nr_cpu_ids;
> +		if (span)
> +			cpu = cpumask_first_and_and(cpu_active_mask,
> +						    p->cpus_ptr, span);
> +		if (cpu >= nr_cpu_ids)
> +			cpu = cpumask_any_and(cpu_active_mask, p->cpus_ptr);
>  		if (cpu >= nr_cpu_ids) {
>  			/*
>  			 * Failed to find any suitable CPU.

[Severity: Medium]
What happens here when p->cpus_ptr has no active CPUs left, but span
still does?

In sched_cpu_deactivate(), set_cpu_active(cpu, false) and
sched_set_rq_offline() run before cpuset_cpu_inactive() widens the mask
of tasks in a cpuset whose last active CPU just went offline.  If
dl_task_timer() fires for a throttled task in that window, p->cpus_ptr
contains no active CPUs, so both cpumask_first_and_and() and
cpumask_any_and() fail.  Unlike select_fallback_rq(), this path does not
fall back to an active CPU in span; instead it triggers
WARN_ON_ONCE(dl_bandwidth_enabled()) and picks
cpumask_any(cpu_active_mask), which with isolcpus=domain,0,1 moves a
housekeeping task and its bandwidth to isolated CPU 0 in def_root_domain.

> @@ -981,8 +1001,15 @@ static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p
>  	 * moved to another cpuset while it was not running. If a rebuild
>  	 * dropped the bandwidth, there is nothing to move: the next rebuild
>  	 * counts it.
> +	 *
> +	 * Move nothing while the sched domains are rebuilt, or before the
> +	 * walk that follows has counted @p, as dl_task_move_bw() does. The
> +	 * CPU picked above then had no span to prefer. The walk counts @p
> +	 * on the side of the CPU recorded.
>  	 */
>  	scoped_guard (raw_spinlock, &dl_departed_lock) {
> +		if (dl_bw_move_blocked(p))
> +			break;
>  		if (p->dl.dl_bw_gen == dl_bw_gen) {
>  			struct rq *src_rq = cpu_rq(dl_task_root_domain_cpu(p));

[Severity: Medium]
Can breaking out on dl_bw_move_blocked(p) leave p's bandwidth uncounted
after a rebuild?

If all CPUs in p->cpus_mask are inactive and dl_task_offline_migration()
moves a throttled task to an out-of-mask fallback CPU while a rebuild or
the task-list walk is in progress, skipping this block leaves
p->dl.dl_bw_cpu pointing at the offline CPU.  When dl_task_rebuild_cpu()
then visits p (or if the walk already passed p and left it uncounted with
p->dl.dl_bw_gen != dl_bw_gen while dl_bw_walking is still true), the
check

	if (cpu == recorded && cpu_active(cpu) && task_on_rq_queued(p))
		return cpu;

no longer matches, and p's bandwidth is never added to later_rq's root
domain.  This also contradicts the comment just below stating that
dl_task_offline_migration() counts the bandwidth when a rebuild left the
task out.

[ ... ]

> @@ -3757,6 +3799,61 @@ static unsigned int dl_task_root_domain_cpu(struct task_struct *p)
>  	return cpu;
>  }
>  
> +/*
> + * Return the CPUs of the root domain that counts the bandwidth of @p, or
> + * NULL if none does. The mask of @p can hold CPUs of another root domain,
> + * with isolcpus="domain". Where a CPU is picked for @p, a CPU of this
> + * root domain comes first: no other root domain admitted @p.
> + *
> + * Return NULL while the sched domains are rebuilt. The CPU recorded can
> + * then sit in def_root_domain on its way to a new root domain. The span
> + * of def_root_domain holds the isolated CPUs, and no longer the
> + * housekeeping CPUs that already left it. A caller would send a
> + * housekeeping task to an isolated CPU.
> + *
> + * Return NULL also for a task that the walk of the task list after the
> + * rebuild has not reached yet. Its dl_bw_gen is not current.
> + *
> + * The caller holds the pi_lock or the rq lock of @p. With interrupts
> + * off, the root domain is not freed.
> + */
> +const struct cpumask *dl_task_bw_span(struct task_struct *p)
> +{
> +	unsigned int seq = READ_ONCE(dl_bw_rebuild_seq);
> +	struct root_domain *rd;
> +	int cpu;
> +
> +	if (!task_has_dl_policy(p) || dl_entity_is_special(&p->dl) ||
> +	    !p->dl.dl_bw || (seq & 1))
> +		return NULL;
> +	cpu = READ_ONCE(p->dl.dl_bw_cpu);
> +	if (cpu < 0 || READ_ONCE(p->dl.dl_bw_gen) != READ_ONCE(dl_bw_gen))
> +		return NULL;
> +	/*
> +	 * The CPU recorded can be going offline. It keeps its root domain
> +	 * until the rebuild attaches it to def_root_domain. The rebuild then
> +	 * records an active CPU with @p, or leaves @p out. Until then, the
> +	 * root domain of the CPU recorded still counts @p.
> +	 *
> +	 * A CPU that isolcpus="domain" isolates is always in
> +	 * def_root_domain, which then counts @p. A housekeeping CPU that is
> +	 * not active and in def_root_domain was attached there by the
> +	 * rebuild. def_root_domain does not count @p then.
> +	 */
> +	rd = cpu_rq(cpu)->rd;
> +	/*
> +	 * A rebuild that started since writes rq->rd after it made
> +	 * dl_bw_rebuild_seq odd. Use this root domain only if none did.
> +	 */
> +	smp_rmb();
> +	if (READ_ONCE(dl_bw_rebuild_seq) != seq)
> +		return NULL;

[Severity: Medium]
Are the sequence-counter memory barriers complete between
dl_task_bw_span() and dl_clear_root_domains()?

dl_task_bw_span() has no read barrier after reading seq before reading
dl_bw_gen and cpu_rq(cpu)->rd (which is also read without READ_ONCE()),
and dl_clear_root_domains() has no write barrier before incrementing
dl_bw_rebuild_seq from odd back to even:

kernel/sched/deadline.c:dl_clear_root_domains() {
    ...
	WRITE_ONCE(dl_bw_gen, dl_bw_gen + 1);
	WARN_ON_ONCE(!(dl_bw_rebuild_seq & 1));
	WRITE_ONCE(dl_bw_rebuild_seq, dl_bw_rebuild_seq + 1);
    ...
}

On weakly-ordered architectures, the loads and stores can reorder at the
end of a rebuild:

CPU0                                  CPU1
dl_task_bw_span()                     partition_sched_domains_locked()
  // reordered loads:                   detach_destroy_domains()
  // reads old dl_bw_gen == 1             rq_attach_root()
  // reads mid-rebuild                      cpu_rq(cpu)->rd = &def_root_domain
  //   rd == &def_root_domain           ...
                                        dl_clear_root_domains()
                                          WRITE_ONCE(dl_bw_gen, 2)
                                          WRITE_ONCE(dl_bw_rebuild_seq, 2)
  seq = READ_ONCE(dl_bw_rebuild_seq) // 2
  smp_rmb()
  READ_ONCE(dl_bw_rebuild_seq) == 2
  return rd->span;  <-- returns def_root_domain.span for housekeeping task

Using smp_load_acquire(&dl_bw_rebuild_seq) at the start of
dl_task_bw_span(), READ_ONCE(cpu_rq(cpu)->rd), and
smp_store_release(&dl_bw_rebuild_seq, dl_bw_rebuild_seq + 1) in
dl_clear_root_domains() would close this race.

> +	if (!cpu_active(cpu) && rd == &def_root_domain &&
> +	    housekeeping_test_cpu(cpu, HK_TYPE_DOMAIN_BOOT))
> +		return NULL;

[Severity: Medium]
Does this return NULL when an offlining CPU belongs to def_root_domain
through runtime cpuset isolation rather than boot-time isolcpus=domain?

CPUs in a cgroup v2 isolated partition (PRS_ISOLATED) or a cgroup v1
cpuset with sched_load_balance=0 are attached to def_root_domain while
active, so def_root_domain counts their tasks' bandwidth.  However,
housekeeping_update() only updates HK_TYPE_DOMAIN, leaving runtime-
isolated CPUs set in HK_TYPE_DOMAIN_BOOT.  When such a CPU goes offline,
between set_cpu_active(cpu, false) and the sched domain rebuild in
cpuset_cpu_inactive(), this check sees housekeeping_test_cpu(cpu,
HK_TYPE_DOMAIN_BOOT) == true and returns NULL, allowing
select_fallback_rq() and __set_cpus_allowed_ptr_locked() to move the task
and its bandwidth out of def_root_domain without an admission test.

> +	return rd->span;
> +}

[Severity: Medium]
Is it safe for callers to read the live rd->span cpumask returned here
when they only hold p->pi_lock?

Checking dl_bw_rebuild_seq only verifies that no rebuild was running up
to the check.  Immediately after dl_task_bw_span() returns rd->span, a
rebuild can start on another CPU and rq_attach_root() will mutate
old_rd->span and def_root_domain.span in place:

CPU0                                  CPU1
select_fallback_rq()
  span = dl_task_bw_span(p)
    // returns rd->span
  ...
  set_cpus_allowed_force(p, span)
    ...
      set_cpus_allowed_common()
        cpumask_copy(&p->cpus_mask,
                     ctx->new_mask)
                                      partition_sched_domains_locked()
                                        dl_bw_rebuild_begin()
                                        detach_destroy_domains()
                                          rq_attach_root()
                                            cpumask_clear_cpu(..., old_rd->span)
        p->nr_cpus_allowed =
          cpumask_weight(ctx->new_mask)  <-- mismatches p->cpus_mask

Besides desynchronizing p->nr_cpus_allowed from p->cpus_mask, callers can
observe housekeeping CPUs transiently set in def_root_domain.span as
detach_destroy_domains() moves them through def_root_domain, placing an
isolated task on a housekeeping CPU without admission control.

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

  reply	other threads:[~2026-11-19 20:55 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 20:15 [PATCH v8 00/33] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 01/33] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 02/33] sched/deadline: Rebuild the root domains' accounting from the task list Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 03/33] sched/deadline: Record the root domain holding a task's bandwidth Mathieu Desnoyers
2026-11-19 20:51   ` sashiko-bot
2026-11-20 13:48     ` Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 04/33] sched/deadline: Leave a dying task out of a rebuild of the accounting Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 05/33] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-19 20:28   ` sashiko-bot
2026-11-20 13:49     ` Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 06/33] selftests/sched: Test the root domain rebuild in deadline_bw_test Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 07/33] sched/deadline: Move a task's bandwidth only where room was made Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 08/33] cgroup/cpuset: Wait for an attach before rebuilding the sched domains Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 09/33] sched: Make deadline policy and affinity changes wait for a cpuset attach Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 10/33] cgroup/cpuset: Give back the bandwidth an attach did not move Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 11/33] cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 12/33] selftests/sched: Test cpuset moves in deadline_bw_test Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 13/33] sched/deadline: Make a rebuild count a task in the root domain it runs in Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 14/33] sched/deadline: Move a task's bandwidth to " Mathieu Desnoyers
2026-11-19 20:53   ` sashiko-bot
2026-11-20 13:49     ` Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 15/33] sched/deadline: Keep a task in the root domain that counts its bandwidth Mathieu Desnoyers
2026-11-19 20:55   ` sashiko-bot [this message]
2026-11-20 13:50     ` Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 16/33] sched/deadline: Keep the last CPU a task has in its root domain Mathieu Desnoyers
2026-11-19 20:22   ` Mathieu Desnoyers
2026-11-19 20:48   ` sashiko-bot
2026-11-20 13:51     ` Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 17/33] cgroup/cpuset: Refuse to take a deadline task out of " Mathieu Desnoyers
2026-11-19 20:43   ` sashiko-bot
2026-11-20 13:51     ` Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 18/33] selftests/sched: Test the root domain of a task in deadline_bw_test Mathieu Desnoyers
2026-11-19 20:27   ` sashiko-bot
2026-11-20 13:51     ` Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 19/33] sched/deadline: Count only active CPUs when spreading extra_bw Mathieu Desnoyers
2026-11-19 20:15 ` [PATCH v8 20/33] rtmutex: Continue the chain walk when a waiter's deadline donor changes Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 21/33] rtmutex: Continue the walk when the owner's top waiter changes lock Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 22/33] selftests/sched: Test the rtmutex chain walk in deadline_bw_test Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 23/33] sched/deadline: Reset the donor's runtime at a deboost in the class Mathieu Desnoyers
2026-11-19 20:53   ` sashiko-bot
2026-11-20 13:52     ` Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 24/33] sched/deadline: Replenish a throttled task boosted while asleep Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 25/33] sched/deadline: Count the bandwidth a boosted task is given Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 26/33] sched/deadline: Start a period for a task that enters the class blocked Mathieu Desnoyers
2026-11-19 20:57   ` sashiko-bot
2026-11-20 13:52     ` Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 27/33] sched/deadline: Start a period at a deboost out of the class Mathieu Desnoyers
2026-11-19 20:47   ` sashiko-bot
2026-11-20 13:53     ` Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 28/33] selftests/sched: Test deadline boosts in deadline_bw_test Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 29/33] sched/deadline: Cancel the replenishment timer with the parameters Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 30/33] sched/deadline: Cap the runtime left of a constrained task at dl_runtime Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 31/33] sched/deadline: Check the runtime of a task that comes back runnable Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 32/33] sched/deadline: Check the runtime of a task whose parameters change Mathieu Desnoyers
2026-11-19 20:59   ` sashiko-bot
2026-11-20 13:53     ` Mathieu Desnoyers
2026-11-19 20:16 ` [PATCH v8 33/33] selftests/sched: Test parameter changes in deadline_bw_test 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-224072@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.