The Linux Kernel Mailing List
 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 v14 36/39] sched/deadline: Keep a reaped deadline task in a rebuild
Date: Thu, 03 Dec 2026 18:14:37 +0000	[thread overview]
Message-ID: <sashiko-outbox-248360@kernel.org> (raw)
In-Reply-To: <44135091542c76aa297063a36b531c9371543ff0.1796296313.git.mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] sched/deadline: unconditional task_rq_lock() in release_task()
  regresses exit
- [Medium] sched/deadline: rebuilds drop live dl_departed_list tasks
  with older dl_bw_gen
- [Medium] sched/deadline: rebuilds drop 0-lag bw on TASK_DEAD/deboost
  race before depart
- [Medium] sched/deadline: dl_clear_root_domains() skips task/CPU checks
  on active CPU
--

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

sched/deadline: Keep a reaped deadline task in a rebuild

This commit adds dl_task_unhash() to release_task() so a reaped deadline
task is placed on dl_departed_list before leaving the task list, keeping
its bandwidth accounted across root-domain rebuilds until its last
schedule or 0-lag time.  It also keeps exited tasks on dl_departed_list
when boosted back into SCHED_DEADLINE and updates dl_clear_root_domains()
and dl_add_task_root_domain() to account for live tasks on that list.

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index f2429672358c0..1eba22b9010bd 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c

[ ... ]

> @@ -440,6 +444,30 @@ static void dl_task_depart(struct task_struct *p)
>  	list_add(&dl_se->dl_departed_node, &dl_departed_list);
>  }
>  
> +/*
> + * Put @p on dl_departed_list if it is a deadline task with a bandwidth.
> + * release_task() calls this before it takes @p off the task list, so
> + * that a rebuild still counts @p until its last schedule. That schedule,
> + * or the inactive timer after it, takes @p off the list. The caller holds
> + * tasklist_lock for writing: no walk of the task list runs meanwhile.
> + */
> +void dl_task_unhash(struct task_struct *p)
> +{
> +	struct rq_flags rf;
> +	struct rq *rq;
> +
> +	lockdep_assert_held_write(&tasklist_lock);
> +	/*
> +	 * Test dl_bw under the rq lock. __sched_setscheduler() admits under
> +	 * that lock, and can still admit @p here: @p sets exit_state without
> +	 * that lock.
> +	 */
> +	rq = task_rq_lock(p, &rf);
> +	if (dl_task(p) && dl_task_holds_bw(p))
> +		dl_task_depart(p);
> +	task_rq_unlock(rq, p, &rf);
> +}

[Severity: High]
Does taking task_rq_lock() unconditionally here regress exit scalability
when SCHED_DEADLINE is not in use?

release_task() calls dl_task_unhash() on every reaped process and thread
while holding the global tasklist_lock for writing.  When a parent reaps
a child that last ran on another CPU, acquiring p->pi_lock and the remote
rq->__lock under tasklist_lock bounces the runqueue lock cacheline across
CPUs, injects contention onto remote CPUs, and extends tasklist_lock hold
time for concurrent fork, exit, wait, and /proc walks.

Elsewhere in kernel/sched/deadline.c (such as dl_rebuild_rd_accounting()),
work is skipped when dl_nr_bw_tasks is zero.  Could dl_task_unhash() avoid
task_rq_lock() on the common non-deadline fast path by gating on
dl_nr_bw_tasks together with in-flight deadline admissions?

[ ... ]

> @@ -4576,19 +4617,20 @@ void dl_add_task_root_domain(struct task_struct *p)
>  	if (p->dl.dl_bw_gen == dl_bw_gen)
>  		goto unlock;
>  	if (!list_empty(&p->dl.dl_departed_node)) {
> -		int recorded = p->dl.dl_bw_cpu;
> -
>  		/*
>  		 * The walk also finds a dead task that departed before an
>  		 * earlier rebuild, if it is not reaped yet. If that rebuild
>  		 * dropped its bandwidth, dl_clear_root_domains() skipped it,
>  		 * and its release removes nothing. Skip it here too.
> +		 *
> +		 * The clear counted the other tasks on the list, or dropped a
> +		 * dead task whose CPU is inactive. It left out a task in the
> +		 * class with no active CPU in its mask, since it cannot ask
> +		 * the cpuset. Count that task here.
>  		 */
> -		if (p->dl.dl_bw_gen != dl_bw_gen - 1)
> +		if (p->dl.dl_bw_gen != dl_bw_gen - 1 || cpu >= nr_cpu_ids ||
> +		    READ_ONCE(p->__state) == TASK_DEAD)
>  			goto unlock;

[Severity: Medium]
Can this generation check drop the bandwidth of a live task on
dl_departed_list whose dl_bw_gen is older than dl_bw_gen - 1?

Suppose an exited departed task had its bandwidth dropped by rebuild G + 1
(leaving p->dl.dl_bw_gen at G), and a boost brings it back into
SCHED_DEADLINE during rebuild G + 2 so dl_task_undepart() keeps it on
dl_departed_list and sets dl_bw_skip_gen to G + 2.  If no CPU in
p->cpus_mask is active, dl_clear_root_domains() leaves it out with
dl_bw_gen still G, and when dl_add_task_root_domain() later finds an
active CPU via the cpuset, p->dl.dl_bw_gen != dl_bw_gen - 1 skips it and
leaves total_bw undercounted.

dl_clear_root_domains() has the same problem across consecutive rebuilds:
if a live reaped or boosted task on dl_departed_list is left out by
rebuild G + 1 for lack of an active CPU in its mask, rebuild G + 2 skips
it at dl_bw_gen != dl_bw_gen - 1 && dl_bw_skip_gen != dl_bw_gen.

Similarly, if an exited departed task is boosted back outside
dl_bw_rebuilding() while dl_task_count_cpu() finds no active CPU,
dl_task_undepart() does not set dl_bw_skip_gen for the next rebuild, so
the next rebuild also drops it.

Checking for live in-class tasks (dl_task(p) && READ_ONCE(p->__state) !=
TASK_DEAD) before skipping older generations in dl_clear_root_domains()
and dl_add_task_root_domain() would cover these cases.

[ ... ]

> @@ -4778,20 +4821,40 @@ void dl_clear_root_domains(void)
>  		/*
>  		 * Skip bandwidth that an earlier rebuild dropped. It is not
>  		 * counted now, and its release removes nothing
> -		 * (dl_task_release_bw()).
> +		 * (dl_task_release_bw()). Count it anyway if
> +		 * dl_task_undepart() asked for it through dl_bw_skip_gen: a
> +		 * boost brought an exited task back into the class.
>  		 */
> -		if (dl_se->dl_bw_gen != dl_bw_gen - 1)
> +		if (dl_se->dl_bw_gen != dl_bw_gen - 1 &&
> +		    dl_se->dl_bw_skip_gen != dl_bw_gen)
> +			goto left_out;
> +		if (cpu < 0)
>  			goto left_out;
>  		/*
> -		 * Drop the bandwidth of a task whose CPU is inactive. A CPU
> -		 * that goes offline is in the default root domain by then.
> -		 * The root domain that counted the task cannot be found
> -		 * from the recorded number. The release then removes
> -		 * nothing. A boost of the task back into the class counts
> -		 * it again (dl_task_undepart()).
> +		 * Drop the bandwidth of a departed task whose CPU is inactive.
> +		 * A CPU that goes offline is in the default root domain by
> +		 * then. The root domain that counted the task cannot be found
> +		 * from the recorded number. The release then removes nothing.
> +		 * A boost of the task back into the class counts it again
> +		 * (dl_task_undepart()).
> +		 *
> +		 * A task on the list can also still run in the class. It is a
> +		 * reaped task before its last schedule, or an exited task that
> +		 * a boost brought back. The walk leaves it to the clear. Count
> +		 * it on the CPU the walk would pick. This reads the task
> +		 * without its locks, so the CPU can be wrong. The bandwidth
> +		 * then stays in that root domain until the release, or the
> +		 * next rebuild.
>  		 */
> -		if (cpu < 0 || !cpu_active(cpu))
> -			goto left_out;
> +		if (!cpu_active(cpu)) {

[Severity: Medium]
When the recorded dl_bw_cpu is still active, can dl_clear_root_domains()
charge a live task on dl_departed_list to the wrong root domain?

Because dl_task_count_cpu(p) is only called inside if (!cpu_active(cpu)),
a reaped deadline task put on dl_departed_list with dl_bw_cpu = 0 that
migrates to CPU1 before a cpuset partition splits {0, 1} into two root
domains still has cpu_active(0) true.  dl_clear_root_domains() then
charges its bandwidth to CPU0's root domain instead of CPU1's,
over-accounting one root domain and under-accounting the other until
inactive_task_timer() fires.

The same mismatch happens when an exited departed task with
dl_bw_skip_gen == dl_bw_gen is boosted on CPU1 while its stale dl_bw_cpu
is active in another root domain.

If that exited task is also deboosted before dl_clear_root_domains() runs,
dl_task_depart() returns early without clearing dl_bw_skip_gen, and
skipping the !dl_task(p) / TASK_DEAD check when cpu_active(cpu) is true
resurrects its dropped bandwidth for the rest of its 0-lag period.

> +			struct task_struct *p = dl_task_of(dl_se);
> +
> +			if (!dl_task(p) || READ_ONCE(p->__state) == TASK_DEAD)
> +				goto left_out;
> +			cpu = dl_task_count_cpu(p);
> +			if (cpu >= nr_cpu_ids)
> +				goto left_out;
> +		}

[Severity: Medium]
Can this check drop a task's 0-lag bandwidth if a rebuild races between
do_task_dead() setting TASK_DEAD and task_non_contending() calling
dl_task_depart()?

CPU0 (rebuild)                    CPU2 (task p, dl_bw_cpu = 1)
                                  release_task() -> dl_task_unhash()
                                  /* on dl_departed_list, moves to CPU2 */
                                  do_task_dead()
                                    set_special_state(TASK_DEAD)
/* CPU1 goes offline */
dl_clear_root_domains()
  !cpu_active(1)
  p->__state == TASK_DEAD
  goto left_out  <-- bw dropped
                                    __schedule() -> dequeue_task_dl()
                                      task_non_contending()
                                        dl_task_depart(p)
                                          !list_empty -> returns early

Once dl_clear_root_domains() leaves the task out with a stale dl_bw_gen,
dl_task_depart() on active CPU2 sees !list_empty(&dl_se->dl_departed_node)
and returns without updating dl_bw_cpu or recounting the bandwidth,
leaving CPU2's root domain undercounted for the entire 0-lag interval.

A symmetric race happens when rt_mutex_setprio() updates p->prio out of
SCHED_DEADLINE before switched_from_dl() calls dl_task_depart(), and
dl_add_task_root_domain() has the same TASK_DEAD check for a task on
dl_departed_list that has not yet reached its last schedule.

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

  reply	other threads:[~2026-12-03 18:14 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 14:58 [PATCH v14 00/39] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 01/39] sched/deadline: Hold a reference on the deadline donor of a boosted task Mathieu Desnoyers
2026-12-03 15:44   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 02/39] sched/deadline: Replenish a throttled task boosted while asleep Mathieu Desnoyers
2026-12-03 15:29   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 03/39] sched/deadline: Do not truncate a bandwidth to 32 bits in __dl_add/sub() Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 04/39] sched/deadline: Count the 0-lag time from the next period Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 05/39] sched/deadline: Clear dl_overrun under the rq lock Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 06/39] sched/deadline: Admit a returning task without the bandwidth it holds Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 07/39] sched/deadline: Leave an active task alone in the inactive timer Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 08/39] sched/deadline: Keep the deadline of a departed constrained task Mathieu Desnoyers
2026-12-03 15:45   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 09/39] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-12-03 15:49   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 10/39] sched/deadline: Rebuild the root domains' accounting from the task list Mathieu Desnoyers
2026-12-03 15:57   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 11/39] sched/deadline: Record the root domain holding a task's bandwidth Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 12/39] sched/deadline: Leave a dying task out of a rebuild of the accounting Mathieu Desnoyers
2026-12-03 16:20   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 13/39] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild Mathieu Desnoyers
2026-12-03 16:10   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 14/39] selftests/sched: Test the root domain rebuild in deadline_bw_test Mathieu Desnoyers
2026-12-03 16:10   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 15/39] sched/deadline: Move a task's bandwidth only where room was made Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 16/39] cgroup/cpuset: Wait for an attach before rebuilding the sched domains Mathieu Desnoyers
2026-12-03 16:22   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 17/39] sched: Make deadline policy and affinity changes wait for a cpuset attach Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 18/39] cgroup/cpuset: Give back the bandwidth an attach did not move Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 19/39] cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 20/39] selftests/sched: Test cpuset moves in deadline_bw_test Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 21/39] sched/deadline: Make a rebuild count a task in the root domain it runs in Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 22/39] sched/deadline: Move a task's bandwidth to " Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 23/39] sched/deadline: Keep a task in the root domain that counts its bandwidth Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 24/39] sched/deadline: Keep the last CPU a task has in its root domain Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 25/39] cgroup/cpuset: Refuse to take a deadline task out of " Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 26/39] selftests/sched: Test the root domain of a task in deadline_bw_test Mathieu Desnoyers
2026-12-03 17:25   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 27/39] sched/deadline: Count only active CPUs when spreading extra_bw Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 28/39] sched/deadline: Cancel the replenishment timer with the parameters Mathieu Desnoyers
2026-12-03 17:39   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 29/39] sched/deadline: Cap the runtime left of a constrained task at dl_runtime Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 30/39] sched/deadline: Check the runtime of a task that comes back runnable Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 31/39] sched/deadline: Record the start of the period Mathieu Desnoyers
2026-12-03 17:42   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 32/39] sched/deadline: Keep the debt of a task across a change of parameters Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 33/39] selftests/sched: Test parameter changes in deadline_bw_test Mathieu Desnoyers
2026-12-03 17:56   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 34/39] sched/deadline: Count a task that departs while a rebuild walks the tasks Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 35/39] sched/deadline: Count again a departed task that a boost brings back Mathieu Desnoyers
2026-12-03 18:01   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 36/39] sched/deadline: Keep a reaped deadline task in a rebuild Mathieu Desnoyers
2026-12-03 18:14   ` sashiko-bot [this message]
2026-12-03 14:58 ` [PATCH v14 37/39] sched/deadline: Warn when a runqueue keeps the bandwidth of a task Mathieu Desnoyers
2026-12-03 18:05   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 38/39] selftests/sched: Test PI against hotplug and rebuilds in deadline_bw_test Mathieu Desnoyers
2026-12-03 18:07   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 39/39] 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-248360@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