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 v8 14/33] sched/deadline: Move a task's bandwidth to the root domain it runs in
Date: Thu, 19 Nov 2026 20:53:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-224061@kernel.org> (raw)
In-Reply-To: <b4bddc11bb05352bb1eca11dd5e0060479aa2e5a.1795116791.git.mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] sched/deadline: dl_bw_move_blocked() drops bw of tasks
  skipped by walk
- [Medium] sched/deadline: task_woken_dl() moves bw to old rd on pinned
  wakeup
- [Medium] sched/deadline: dl_task_move_bw() moves departing bandwidth
  of PI-boosted task
--

commit 692ba1dcff2566600d665231dbe2bb9a5d88adc8
Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

sched/deadline: Move a task's bandwidth to the root domain it runs in

This commit moves a deadline task's bandwidth in migrate_task_rq_dl() and
task_woken_dl() when the task migrates to or wakes up on an active CPU in a
root domain different from the one currently accounting its bandwidth, while
blocking moves during sched domain rebuilds and tasklist accounting walks.

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index df55aba27ddc3..fb8f32a31cf91 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c
> @@ -268,6 +268,71 @@ static LIST_HEAD(dl_departed_list);
>  static DEFINE_RAW_SPINLOCK(dl_departed_lock);
>  static unsigned int dl_bw_gen;
>  
> +/*
> + * Odd while the sched domains are rebuilt: from before the first CPU
> + * leaves its root domain until dl_clear_root_domains(). Meanwhile the
> + * CPUs change root domains one by one. Housekeeping CPUs pass through
> + * def_root_domain, next to the CPUs that isolcpus="domain" isolates. So
> + * the root domain of the CPU recorded with a task says nothing about
> + * where its bandwidth is counted. The new root domains count no task
> + * yet.
> + *
> + * Changed under dl_departed_lock. dl_task_bw_elsewhere() reads it
> + * without the lock.
> + */
> +static unsigned int dl_bw_rebuild_seq;
> +
> +/*
> + * True while dl_rebuild_rd_accounting() walks the task list: from
> + * dl_clear_root_domains() until the walk ends. The root domains are final
> + * by then. A task the walk has not reached yet has a dl_bw_gen that is
> + * not current. Changed under dl_departed_lock. dl_task_bw_elsewhere()
> + * reads it without the lock.
> + */
> +static bool dl_bw_walking;
> +
> +/* Return true while the sched domains are rebuilt. */
> +static inline bool dl_bw_rebuilding(void)
> +{
> +	return READ_ONCE(dl_bw_rebuild_seq) & 1;
> +}
> +
> +/*
> + * Return true if the bandwidth of @p must not move now. Either the sched
> + * domains are rebuilt, or the walk of the task list that follows has not
> + * reached @p yet. The walk then counts @p. A move must not count it
> + * first.
> + *
> + * The answer is exact under dl_departed_lock. Without it, the answer is a
> + * hint: dl_task_move_bw() tests again under the lock.
> + */
> +static inline bool dl_bw_move_blocked(struct task_struct *p)
> +{
> +	if (dl_bw_rebuilding())
> +		return true;
> +	return READ_ONCE(dl_bw_walking) &&
> +	       READ_ONCE(p->dl.dl_bw_gen) != READ_ONCE(dl_bw_gen);
> +}

[Severity: Medium]
Can dl_bw_move_blocked() prevent a task that dl_add_task_root_domain()
already visited and skipped from adding its bandwidth when it falls back
to an active CPU before the tasklist walk finishes?

When dl_add_task_root_domain() visits a task that currently has no active
CPU in its mask, it returns early without updating p->dl.dl_bw_gen,
relying on a later move of the task to an active CPU to count its
bandwidth.  However, p->dl.dl_bw_gen != dl_bw_gen leaves the skipped task
indistinguishable from a task that the walk has not reached yet:

CPU0                                  CPU1
dl_rebuild_rd_accounting()
  dl_clear_root_domains()
    dl_bw_gen++
    dl_bw_walking = true
  for_each_process_thread(g, p)
    dl_add_task_root_domain(p)
      dl_task_rebuild_cpu(p) >= nr_cpu_ids
      return (p->dl.dl_bw_gen != dl_bw_gen)
                                      try_to_wake_up(p)
                                        select_fallback_rq() -> new_cpu
                                        migrate_task_rq_dl(p, new_cpu)
                                          dl_task_bw_elsewhere(p, new_cpu)
                                            dl_bw_move_blocked(p) -> true
                                        ttwu_do_activate()
                                          task_woken_dl(rq, p)
                                            dl_bw_move_blocked(p) -> true
    ... walks remaining tasks ...
  dl_bw_walk_end()
    dl_bw_walking = false             <-- p runs on new_cpu with bandwidth
                                          uncounted in new_cpu's rd

Because the walk has already passed p and both migrate_task_rq_dl() and
task_woken_dl() were blocked by dl_bw_move_blocked(), p runs in new_cpu's
root domain without its bandwidth counted there, allowing that root domain
to admit deadline tasks beyond its capacity.

[ ... ]

> @@ -2920,11 +2985,82 @@ select_task_rq_dl(struct task_struct *p, int cpu, int flags)
>  	return cpu;
>  }
>  
> -static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
> +/*
> + * Return true if the root domain of @cpu may not count the bandwidth of
> + * @p. Either another root domain counts it, or a rebuild left @p out.
> + * Test without dl_departed_lock, so that a move inside a root domain
> + * takes no lock. dl_task_move_bw() tests again under it.
> + *
> + * Return false if @cpu is not active. A migration-disabled task can wake
> + * up on a CPU that goes offline, after it joined the default root domain.
> + * Keep the bandwidth where it is counted. balance_push() moves the task
> + * off that CPU once it enables migration, or its next wakeup does once
> + * the CPU is offline. That migration moves the bandwidth if needed.
> + *
> + * Return false while the sched domains are rebuilt. The CPUs then move
> + * to their new root domains one by one. That alone can make the two root
> + * domains differ. The rebuild of the accounting that follows counts @p.
> + * Return false also while that rebuild has not reached @p. Once it has
> + * counted @p, the root domains are final, and a move is right again.
> + */
> +static bool dl_task_bw_elsewhere(struct task_struct *p, int cpu)
> +{
> +	int bw_cpu = READ_ONCE(p->dl.dl_bw_cpu);
> +
> +	if (bw_cpu < 0 || !p->dl.dl_bw || !cpu_active(cpu) ||
> +	    dl_bw_move_blocked(p))
> +		return false;
> +	return READ_ONCE(p->dl.dl_bw_gen) != READ_ONCE(dl_bw_gen) ||
> +	       cpu_rq(bw_cpu)->rd != cpu_rq(cpu)->rd;
> +}
> +
> +/*
> + * Move the bandwidth of @p to the root domain of @new_cpu. Count it there
> + * also if a rebuild left @p out. Add it even without room, since @p runs
> + * there. The caller holds the rq lock of @p, or its pi_lock for a wakeup.
> + */
> +static void dl_task_move_bw(struct task_struct *p, int new_cpu)
> +{
> +	struct dl_bw *dl_b;
> +	int cpu;
> +
> +	if (dl_entity_is_special(&p->dl) || !p->dl.dl_bw)
> +		return;
> +
> +	guard(raw_spinlock)(&dl_departed_lock);
> +	cpu = p->dl.dl_bw_cpu;
> +	if (cpu < 0 || dl_bw_move_blocked(p))
> +		return;
> +	if (p->dl.dl_bw_gen == dl_bw_gen) {
> +		if (cpu_rq(cpu)->rd == cpu_rq(new_cpu)->rd)
> +			return;
> +		dl_b = dl_bw_of(cpu);
> +		raw_spin_lock(&dl_b->lock);
> +		__dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(cpu));
> +		raw_spin_unlock(&dl_b->lock);
> +	}
> +	dl_b = dl_bw_of(new_cpu);
> +	raw_spin_lock(&dl_b->lock);
> +	__dl_add(dl_b, p->dl.dl_bw, dl_bw_cpus(new_cpu));
> +	raw_spin_unlock(&dl_b->lock);
> +	p->dl.dl_bw_cpu = new_cpu;
> +	p->dl.dl_bw_gen = dl_bw_gen;
> +}

[Severity: Medium]
Can dl_task_bw_elsewhere() and dl_task_move_bw() move the departing
bandwidth of a non-deadline task that is PI-boosted back into
dl_sched_class before its 0-lag time?

When a task leaves SCHED_DEADLINE before its 0-lag time, p->dl.dl_bw
remains non-zero and p->dl.dl_bw_cpu records the original root domain
where its departing bandwidth must stay accounted until
inactive_task_timer() fires, as noted in switched_to_dl():

kernel/sched/deadline.c:switched_to_dl() {
    ...
	 * again. So take it off the list. The task keeps the CPU recorded. It
	 * takes the bandwidth from the root domain of that CPU, even if it
	 * moved to another root domain meanwhile.
    ...
}

Because neither dl_task_bw_elsewhere() nor dl_task_move_bw() checks
task_has_dl_policy(p), if that departed task moves to another root domain
under SCHED_OTHER and is PI-boosted into dl_sched_class before its 0-lag
time, waking or migrating it in the new root domain subtracts its
departing bandwidth from the original root domain early and adds it to the
new root domain without admission control.  Checking task_has_dl_policy(p)
in dl_task_bw_elsewhere() and dl_task_move_bw() would keep departing
bandwidth pinned to the original root domain.

[ ... ]

> @@ -3587,6 +3723,15 @@ static void pull_dl_task(struct rq *this_rq)
>   */
>  static void task_woken_dl(struct rq *rq, struct task_struct *p)
>  {
> +	/*
> +	 * A task can wake up on the CPU it slept on, with no migration,
> +	 * while another root domain counts its bandwidth. The bandwidth
> +	 * moved while the task slept out of its mask, and the mask then got
> +	 * that CPU back. Move the bandwidth to this root domain.
> +	 */
> +	if (unlikely(dl_task_bw_elsewhere(p, cpu_of(rq))))
> +		dl_task_move_bw(p, cpu_of(rq));

[Severity: Medium]
If a task sleeps inside a migrate_disable() section on an active CPU while
its affinity and bandwidth move to another root domain, does
task_woken_dl() move the bandwidth back to the old root domain when the
task wakes up on its pinned CPU?

When p blocks under migrate_disable() on active CPU B (in root domain B)
and cpuset_attach_task() -> set_cpus_allowed_dl() moves p->cpus_mask and
p->dl.dl_bw_cpu to root domain A while affine_move_task() waits for
migrate_enable(), waking p to finish its migrate_disable() section selects
CPU B via p->cpus_ptr without migration.  Because CPU B is still active
and dl_task_bw_elsewhere() does not check is_migration_disabled(p) or
whether the CPU is in p->cpus_mask, task_woken_dl() moves p's bandwidth
from root domain A back to root domain B.  That frees the reserved
bandwidth in root domain A before migrate_enable() migrates p to root
domain A and re-adds its bandwidth without admission control.

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

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

Thread overview: 46+ 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-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-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 [this message]
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
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-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-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-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-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-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-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-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-224061@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®