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 v7 27/40] sched/deadline: Move a task's bandwidth to the root domain it runs in
Date: Tue, 17 Nov 2026 15:29:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-219077@kernel.org> (raw)
In-Reply-To: <3292905cac4b511f515b4d353f02a41d747b8015.1794922146.git.mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] sched/deadline: dl_bw_rebuilding() drops bw moves during
  rebuild task walk
- [Medium] sched/deadline: migrate_task_rq_dl() drops bw move for
  mutex-blocked tasks
- [Medium] sched/deadline: dl_task_move_bw() moves departed bw of
  PI-boosted tasks
--

commit 00a2592b3e4a7ff7a78ebc503a680dacf59e846b
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 to the root domain of its
new CPU in migrate_task_rq_dl() and task_woken_dl() when it differs from
the root domain that currently counts it, while suppressing moves during
sched domain rebuilds and proxy execution migrations.

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index 9ff79141c6306..6c6a4d9cd0548 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c

[ ... ]

> @@ -2930,11 +2968,86 @@ 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.
> + */
> +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_rebuilding())
> +		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_rebuilding())
> +		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_move_bw() move the departing bandwidth of a non-deadline task
that was PI-boosted back into the deadline class?

When a task leaves SCHED_DEADLINE before its 0-lag time, switched_from_dl()
arms inactive_timer and leaves p->dl.dl_bw and p->dl.dl_bw_cpu set in the
original root domain.  As a non-deadline task (!task_has_dl_policy(p)), it
can move to another root domain without admission control, and if it is
then PI-boosted by a deadline waiter before its 0-lag time,
switched_to_dl() cancels the inactive timer and takes it off
dl_departed_list while keeping the original dl_bw_cpu recorded.

Because neither dl_task_bw_elsewhere() nor dl_task_move_bw() checks
task_has_dl_policy(p), waking or migrating the boosted task in the new root
domain subtracts p->dl.dl_bw from the original root domain before its
0-lag time and adds that unreserved bandwidth to the new root domain.

> +
> +static void migrate_task_rq_dl(struct task_struct *p, int new_cpu)
>  {
>  	struct rq_flags rf;
>  	struct rq *rq;
>  
> +	/*
> +	 * The new CPU can be in another root domain than the one that counts
> +	 * the bandwidth. A mask can hold CPUs of two root domains, and
> +	 * select_fallback_rq() can widen it. Move the bandwidth there, as
> +	 * dl_task_offline_migration() does for a throttled task.
> +	 *
> +	 * Not for a task that is blocked on a mutex and only queued for
> +	 * proxy execution. It does not run on the new CPU, so its bandwidth
> +	 * stays in its root domain. If its wake-up then picks a CPU of another
> +	 * root domain, the bandwidth moves at that time.
> +	 */
> +	if (unlikely(dl_task_bw_elsewhere(p, new_cpu)) &&
> +	    !(p->is_blocked && task_on_rq_migrating(p)))
> +		dl_task_move_bw(p, new_cpu);

[Severity: Medium]
Does skipping dl_task_move_bw() when p->is_blocked && task_on_rq_migrating(p)
also drop the bandwidth move for normal queued migrations of mutex-blocked
tasks under CONFIG_SCHED_PROXY_EXEC?

When a mutex-blocked task (p->is_blocked == 1) is migrated across root
domains by move_queued_task() or move_queued_task_locked() (for example
from __balance_push_cpu_stop() during CPU hotplug or push_dl_task()),
deactivate_task() sets p->on_rq = TASK_ON_RQ_MIGRATING and set_task_cpu()
updates both p->cpu and p->wake_cpu to new_cpu:

kernel/sched/core.c:move_queued_task() {
	...
	deactivate_task(rq, p, DEQUEUE_NOCLOCK);
	set_task_cpu(p, new_cpu);
	...
	activate_task(rq, p, 0);
}

When the mutex is unlocked, try_to_wake_up() sees p->on_rq == TASK_ON_RQ_QUEUED
and takes ttwu_runnable():

kernel/sched/core.c:ttwu_runnable() {
	...
	if (p->is_blocked) {
		if (p->se.sched_delayed)
			enqueue_task(rq, p, ENQUEUE_NOCLOCK | ENQUEUE_DELAYED);
		if (proxy_needs_return(rq, p))
			return 0;
	}
	...
	ttwu_do_wakeup(p);
	return 1;
}

Because task_cpu(p) == p->wake_cpu, proxy_needs_return() returns false and
ttwu_runnable() finishes the wakeup in place without calling
migrate_task_rq_dl() or task_woken_dl(), leaving p's bandwidth counted in
the old root domain.

[ ... ]

> @@ -4068,8 +4192,10 @@ void dl_rebuild_rd_accounting(void)
>  	 * a resume run one for each CPU. A task gets a bandwidth under the
>  	 * locks this rebuild holds, so the count cannot leave 0 meanwhile.
>  	 */
> -	if (!atomic_read(&dl_nr_bw_tasks))
> +	if (!atomic_read(&dl_nr_bw_tasks)) {
> +		dl_bw_rebuild_end();
>  		return;
> +	}
>  
>  	read_lock(&tasklist_lock);
>  	for_each_process_thread(g, p) {
> @@ -4083,6 +4209,7 @@ void dl_rebuild_rd_accounting(void)
>  			dl_add_task_root_domain(p);
>  	}
>  	read_unlock(&tasklist_lock);
> +	dl_bw_rebuild_end();
>  }

[Severity: Medium]
Can keeping dl_bw_rebuilding() true until the end of the tasklist walk
drop bandwidth moves for tasks that dl_add_task_root_domain() has already
visited?

During CPU deactivation, tasks are woken via select_fallback_rq() or pushed
by __balance_push_cpu_stop() while dl_rebuild_rd_accounting() walks
for_each_process_thread() under read_lock(&tasklist_lock):

CPU0                                    CPU1
partition_sched_domains_locked()
  dl_bw_rebuild_begin()
  ...
  dl_rebuild_rd_accounting()
    dl_clear_root_domains()
    read_lock(&tasklist_lock)
    for_each_process_thread(g, p)
      dl_add_task_root_domain(p)
        /* counts p in RD_A (or skips
         * p if cpu >= nr_cpu_ids) */
                                        try_to_wake_up(p)
                                          select_fallback_rq() -> cpu in RD_B
                                          set_task_cpu(p, new_cpu)
                                            migrate_task_rq_dl(p, new_cpu)
                                              dl_task_bw_elsewhere()
                                                dl_bw_rebuilding() == true
                                                --> returns false (bw not moved)
      /* still visiting later tasks */
    read_unlock(&tasklist_lock)
    dl_bw_rebuild_end()

Once dl_add_task_root_domain(p) releases p->pi_lock, p can wake up or
migrate to an active CPU in another root domain before the walk finishes.
Because dl_bw_rebuilding() is still true, migrate_task_rq_dl() and
task_woken_dl() ignore the move, leaving p's bandwidth counted in RD_A (or
uncounted if dl_task_rebuild_cpu(p) returned nr_cpu_ids).

In addition, when dl_task_bw_elsewhere() runs under rq->__lock without
p->pi_lock, it reads bw_cpu = READ_ONCE(p->dl.dl_bw_cpu) before checking
dl_bw_rebuilding() and READ_ONCE(dl_bw_gen), whereas
dl_add_task_root_domain() updates p->dl.dl_bw_cpu and p->dl.dl_bw_gen under
p->pi_lock and dl_departed_lock, so dl_task_bw_elsewhere() can compare
cpu_rq(bw_cpu)->rd using a stale bw_cpu read from before the rebuild ended.

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

  reply	other threads:[~2026-11-17 15:29 UTC|newest]

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