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 v14 10/39] sched/deadline: Rebuild the root domains' accounting from the task list
Date: Thu, 03 Dec 2026 15:57:17 +0000	[thread overview]
Message-ID: <sashiko-outbox-248162@kernel.org> (raw)
In-Reply-To: <ddec085e0ec25b6837ef177f0de1532e017d7d89.1796296313.git.mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] sched/deadline: O(all threads) walk under tasklist_lock in
  dl_rebuild_rd_accounting()
- [Low] sched/deadline: dead src_cs_head, stale cpuset_locked name,
  leftover externs
- [Low] sched: stale cpuset_locked name and lock comments in
  __sched_setscheduler()
- [Low] sched/deadline: data race on p->dl.dl_bw in
  dl_add_task_root_domain()
--

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

sched/deadline: Rebuild the root domains' accounting from the task list

This commit replaces the cpuset-based walk in dl_rebuild_rd_accounting()
with a tasklist walk gated by a global count of bandwidth-holding
deadline tasks (dl_nr_bw_tasks), and adds dl_accounting_mutex to
serialize admissions against rebuilds when CONFIG_CPUSETS is disabled.

> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index 4159abfbbbb53..7628cf77bdfd2 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c

[ ... ]

> @@ -3181,9 +3119,7 @@ static void clear_attach_data(struct llist_head *head, bool cancel)
>  	llist_for_each_entry_safe(cs, next, lnode, attach_node) {
>  		init_llist_node(&cs->attach_node);
>  		if (cs->nr_migrate_dl_tasks) {
> -			if (!cancel)
> -				atomic_add(cs->nr_migrate_dl_tasks, &cs->nr_deadline_tasks);
> -			else if (cs->dl_bw_cpu >= 0) /* && cancel */
> +			if (cancel && cs->dl_bw_cpu >= 0)
>  				dl_bw_free(cs->dl_bw_cpu, cs->sum_migrate_dl_bw);
>  			cs->nr_migrate_dl_tasks = 0;
>  			cs->sum_migrate_dl_bw = 0;
> @@ -3272,7 +3208,6 @@ static int cpuset_can_attach(struct cgroup_taskset *tset)
>  			 * contribute to sum_migrate_dl_bw.
>  			 */
>  			cs->nr_migrate_dl_tasks++;
> -			oldcs->nr_migrate_dl_tasks--;
>  			if (dl_task_needs_bw_move(task, cs->effective_cpus))
>  				cs->sum_migrate_dl_bw += task->dl.dl_bw;
>  		}

[Severity: Low]
Can src_cs_head be removed now that nr_deadline_tasks and
oldcs->nr_migrate_dl_tasks-- are gone?  cpuset_can_attach_check() still
adds oldcs to src_cs_head, and clear_attach_data() only unlinks it
without reading or updating any source-cpuset state.

Separately, dl_add_task_root_domain(), dl_clear_root_domain(), and
dl_clear_root_domains() in include/linux/sched/deadline.h no longer have
callers outside kernel/sched/deadline.c and can be made static.

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index 7d1e305049c25..889a70314918c 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c

[ ... ]

> @@ -3712,7 +3728,13 @@ void dl_add_task_root_domain(struct task_struct *p)
>  	struct cpumask *msk;
>  
>  	raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
> -	if (!dl_task(p) || dl_entity_is_special(&p->dl)) {
> +	/*
> +	 * Count nothing for a special entity, or for a task with no
> +	 * bandwidth. The walk of the task list also finds exited tasks. An
> +	 * exited task whose bandwidth went with its parameters has a dl_bw
> +	 * of 0.
> +	 */
> +	if (!dl_task(p) || dl_entity_is_special(&p->dl) || !p->dl.dl_bw) {
>  		raw_spin_unlock_irqrestore(&p->pi_lock, rf.flags);
>  		return;
>  	}

[Severity: Low]
Can the unmarked reads of p->dl.flags and p->dl.dl_bw here race with
__dl_clear_params()?

CPU0                              CPU1
__schedule()                      dl_rebuild_rd_accounting()
  dequeue_task_dl()                 dl_add_task_root_domain(p)
    dequeue_dl_entity()               raw_spin_lock_irqsave(&p->pi_lock)
      task_non_contending()
        dl_task_release_bw()
        dl_release_params()
          __dl_clear_params()
            dl_se->flags = 0;         if (... || dl_entity_is_special(&p->dl)
            dl_se->dl_bw = 0;             || !p->dl.dl_bw)  <-- data race

When a dying deadline task past its 0-lag time runs task_non_contending(),
__dl_clear_params() writes dl_se->flags and 64-bit dl_se->dl_bw under
only rq->__lock after dl_task_release_bw() has dropped dl_departed_lock.
Meanwhile dl_add_task_root_domain() holds only p->pi_lock, so KCSAN can
flag a data race and 32-bit architectures can observe torn 64-bit reads.

Later patches in this series (commit "sched/deadline: Record the root
domain holding a task's bandwidth" and commit "sched/deadline: Leave a
dying task out of a rebuild of the accounting") prevent bandwidth
corruption here, though the unmarked accesses remain.

Using READ_ONCE()/WRITE_ONCE() on dl_bw and flags, or moving the
!p->dl.dl_bw check under dl_departed_lock, would avoid the race.

[ ... ]

> +void dl_rebuild_rd_accounting(void)
> +{
> +	struct task_struct *g, *p;
> +
> +	lockdep_assert_cpus_held();
> +	lockdep_assert_held(&sched_domains_mutex);
> +	lockdep_assert_cpuset_lock_held();
> +	lockdep_assert_dl_accounting_held();
> +
> +	dl_clear_root_domains();
> +
> +	/*
> +	 * Without a task that holds a bandwidth, there is nothing to count.
> +	 * The walk would still visit every thread of the system. A CPU
> +	 * that goes offline or comes back runs a rebuild, so a suspend and
> +	 * 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))
> +		return;
> +
> +	read_lock(&tasklist_lock);
> +	for_each_process_thread(g, p) {
> +		/*
> +		 * Test the class without the task's locks, and take them only
> +		 * for a deadline task. A task enters the class under the locks
> +		 * this rebuild holds, or by a boost. A boost brings no
> +		 * bandwidth of its own to count.
> +		 */
> +		if (dl_task(p))
> +			dl_add_task_root_domain(p);
> +	}
> +	read_unlock(&tasklist_lock);
> +}

[Severity: Medium]
Does holding read_lock(&tasklist_lock) across for_each_process_thread()
here introduce a latency and lock-contention regression when a deadline
task is active?

With CONFIG_CPUSETS, rebuilds previously skipped cpusets with no deadline
tasks and never took tasklist_lock.  Now, as long as even one task holds
deadline bandwidth (including a departed task waiting on its 0-lag
timer), every CPU hotplug or suspend/resume rebuild walks every thread in
the system with read_lock(&tasklist_lock) held.

On non-PREEMPT_RT kernels that disables preemption for milliseconds (0.7
to 1.2 ms per rebuild on 10,000 threads) and stalls
write_lock_irq(&tasklist_lock) callers such as copy_process() and exit
paths, multiplied across every CPU on suspend and resume.

Tracking bandwidth-holding sched_dl_entity objects on a dedicated list
updated in __setparam_dl() and __dl_clear_params() would keep rebuilds
O(deadline tasks) without taking tasklist_lock.

> diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
> index 595bdb9bd04f3..145940d0ce4de 100644
> --- a/kernel/sched/syscalls.c
> +++ b/kernel/sched/syscalls.c
> @@ -568,6 +568,7 @@ int __sched_setscheduler(struct task_struct *p,
>  	if (pi && (dl_policy(policy) || dl_policy(p->policy))) {
>  		cpuset_locked = true;
>  		cpuset_lock();
> +		dl_accounting_lock();
>  	}

[Severity: Low]
This isn't a bug, but cpuset_locked now tracks dl_accounting_lock() as
well (and only dl_accounting_mutex without CONFIG_CPUSETS).  Renaming it
to dl_accounting_locked and updating the cpuset_mutex-only comments in
__sched_setscheduler() would keep the locking documentation accurate.

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

  reply	other threads:[~2026-12-03 15:57 UTC|newest]

Thread overview: 61+ 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-04 17:26   ` Peter Zijlstra
2026-12-04 18:03     ` Mathieu Desnoyers
2026-12-04 18:23       ` Sebastian Andrzej Siewior
2026-12-04 18:38       ` Mathieu Desnoyers
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 [this message]
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
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-248162@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.