Linux real-time development
 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 v12 26/43] sched/deadline: Keep a departing constrained task until its next period
Date: Fri, 27 Nov 2026 17:00:22 +0000	[thread overview]
Message-ID: <sashiko-outbox-237957@kernel.org> (raw)
In-Reply-To: <54e41332aec6ddeb1d060c892cbac401c936df86.1795771148.git.mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] sched/deadline: premature release on active timer in
  task_non_contending()
- [Medium] sched/deadline: uncanceled restarted timer after
  inactive_task_timer()
- [Medium] sched/deadline: rq bw leak if restarted inactive_task_timer()
  races task exit
--

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

sched/deadline: Keep a departing constrained task until its next period

This commit keeps the bandwidth and deadline of a constrained task that
leaves SCHED_DEADLINE until the later of its 0-lag time and its next
period, and cancels any restarted inactive timer in task_non_contending().

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index 7dc0b71e7cce7..adcd9c9f8a9d3 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c
> @@ -761,8 +761,35 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)
>  
>  	WARN_ON(dl_se->dl_non_contending);
>  
> +	/*
> +	 * inactive_task_timer() restarts itself for a constrained task that
> +	 * has left the class. If the task came back while the handler ran,
> +	 * switched_to_dl() could not cancel the timer, and it is still
> +	 * queued. Cancel it here, so that the test below sees only a handler
> +	 * that is running.
> +	 */
> +	if (!dl_server(dl_se) && hrtimer_try_to_cancel(timer) == 1)
> +		put_task_struct(dl_task_of(dl_se));
> +
>  	zerolag_time = dl_time_to_zerolag(dl_se, rq);
>  
> +	/*
> +	 * A constrained task that leaves the class keeps its bandwidth and its
> +	 * deadline until its next period, if that comes after its 0-lag time.
> +	 * Back in the class before then, it goes on with its current period,
> +	 * as if it had stayed. A new period at once would give it more than
> +	 * its runtime in this period. A dying task does not come back, so it
> +	 * releases its bandwidth at its 0-lag time.
> +	 */
> +	if (!dl_task && !dl_server(dl_se) && !dl_is_implicit(dl_se) &&
> +	    dl_se->deadline &&
> +	    READ_ONCE(dl_task_of(dl_se)->__state) != TASK_DEAD) {
> +		s64 next = dl_next_period(dl_se) - rq_clock(rq);
> +
> +		if (next > zerolag_time)
> +			zerolag_time = next;
> +	}
> +
>  	/*
>  	 * If the "0-lag time" already passed, decrease the active
>  	 * utilization now, instead of starting a timer

[Severity: Medium]
Can a departing constrained task still release its bandwidth and lose its
deadline at the 0-lag time when task_non_contending() races with
inactive_task_timer()?

If the task was still in SCHED_DEADLINE when its 0-lag timer fired on
another CPU, inactive_task_timer() clears dl_non_contending, drops the rq
lock, and returns HRTIMER_NORESTART.  Until __run_hrtimer() finishes,
hrtimer_try_to_cancel() returns -1 and hrtimer_active() stays true:

CPU0                                  CPU1
__run_hrtimer()
  inactive_task_timer()
    /* dl_task(p) is true */
    sub_running_bw()
    dl_se->dl_non_contending = 0
    task_rq_unlock()
                                      switched_from_dl()
                                        task_non_contending(dl_se, false)
                                          hrtimer_try_to_cancel() -> -1
                                          zerolag_time = next (> 0)
                                          if (zerolag_time < 0 ||
                                              hrtimer_active(timer))
                                            dl_task_release_bw(p)
                                            __dl_clear_params(dl_se)
                                              <-- released before next period
  base->running = NULL

Because the subsequent check in task_non_contending() tests
hrtimer_active(&dl_se->inactive_timer), it overrides zerolag_time > 0 and
immediately clears the task's deadline and bandwidth instead of arming the
timer until dl_next_period(dl_se).  If the task re-enters SCHED_DEADLINE
before its next period, it gets a new period right away and can exceed its
reserved bandwidth.

> @@ -2602,6 +2629,25 @@ static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
>  	if (dl_server(dl_se))
>  		goto no_task;
>  
> +	/*
> +	 * A constrained task which has left the class keeps its bandwidth
> +	 * and its deadline until its next period, as in
> +	 * task_non_contending(). This timer may have been armed for the
> +	 * 0-lag time while the task was still in the class.
> +	 */
> +	if (!dl_task(p) && READ_ONCE(p->__state) != TASK_DEAD &&
> +	    !dl_is_implicit(dl_se)) {
> +		s64 left = dl_next_period(dl_se) - rq_clock(rq);
> +
> +		if (left > 0) {
> +			ktime_t at = ktime_add_ns(hrtimer_cb_get_time(timer), left);
> +
> +			hrtimer_set_expires(timer, at);
> +			task_rq_unlock(rq, p, &rf);
> +			return HRTIMER_RESTART;
> +		}
> +	}

[Severity: Medium]
Does canceling the restarted inactive_timer only in task_non_contending()
miss a task that returns to SCHED_DEADLINE while inactive_task_timer() is
returning HRTIMER_RESTART?

CPU0                                  CPU1
__run_hrtimer()
  inactive_task_timer()
    hrtimer_set_expires(timer, at)
    task_rq_unlock()
                                      switched_to_dl()
                                        cancel_inactive_timer() -> -1
                                      task_non_contending()
                                        hrtimer_try_to_cancel() -> -1
                                        hrtimer_active() == true
                                          <-- premature release / sub_running_bw
    return HRTIMER_RESTART
  enqueue_hrtimer(timer)

If task_non_contending() runs before __run_hrtimer() clears base->running,
hrtimer_try_to_cancel() returns -1 and hrtimer_active() is still true, so
task_non_contending() takes the immediate-release path while CPU0 still
re-enqueues inactive_timer.

Even if the task stays asleep until after __run_hrtimer() re-enqueues the
timer, waking up in SCHED_DEADLINE calls task_contending(), which only
calls cancel_inactive_timer() when dl_se->dl_non_contending is set:

kernel/sched/deadline.c:task_contending() {
	...
	if (dl_se->dl_non_contending) {
		dl_se->dl_non_contending = 0;
		cancel_inactive_timer(dl_se);
	}
}

Because dl_non_contending is 0, the restarted inactive_timer stays queued
while the task is active or throttled in SCHED_DEADLINE.

> +
>  	if (!dl_task(p) || READ_ONCE(p->__state) == TASK_DEAD) {
>  		if (READ_ONCE(p->__state) == TASK_DEAD && dl_se->dl_non_contending) {
>  			sub_running_bw(&p->dl, dl_rq_of_se(&p->dl));

[Severity: Medium]
Can that leftover restarted inactive_timer leak running_bw and this_bw on
the runqueue if it expires while the task is exiting in do_task_dead()?

When a constrained task returns to SCHED_DEADLINE while inactive_task_timer()
is restarting (so dl_non_contending is 0 and inactive_timer remains queued
until dl_next_period(dl_se)) and then exits without blocking first,
do_task_dead() sets p->__state = TASK_DEAD before __schedule() acquires the
rq lock:

CPU0                                  CPU1
do_task_dead()
  set_special_state(TASK_DEAD)
                                      inactive_task_timer()
                                        /* p->__state == TASK_DEAD,
                                         * dl_non_contending == 0 */
                                        dl_task_release_bw(p)
                                        __dl_clear_params(dl_se)
  __schedule()
    dequeue_task_dl()
      task_non_contending()
        if (dl_se->dl_runtime == 0)
          return;  <-- skips sub_running_bw() and sub_rq_bw()

Because dl_se->dl_non_contending is 0, inactive_task_timer() skips
sub_running_bw() and sub_rq_bw(), but still calls __dl_clear_params(dl_se),
zeroing dl_runtime, dl_bw, and dl_bw_oncpu while the task is still on the
runqueue.  The final dequeue in __schedule() then sees dl_runtime == 0 and
returns early, permanently leaking the task's running_bw and this_bw on
that runqueue.

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

  reply	other threads:[~2026-11-27 17:00 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 14:35 [PATCH v12 00/43] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 01/43] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-27 15:22   ` sashiko-bot
2026-11-27 14:35 ` [PATCH v12 02/43] sched/deadline: Rebuild the root domains' accounting from the task list Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 03/43] sched/deadline: Record the root domain holding a task's bandwidth Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 04/43] sched/deadline: Leave a dying task out of a rebuild of the accounting Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 05/43] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-27 15:16   ` sashiko-bot
2026-11-27 14:35 ` [PATCH v12 06/43] selftests/sched: Test the root domain rebuild in deadline_bw_test Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 07/43] sched/deadline: Move a task's bandwidth only where room was made Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 08/43] cgroup/cpuset: Wait for an attach before rebuilding the sched domains Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 09/43] sched: Make deadline policy and affinity changes wait for a cpuset attach Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 10/43] cgroup/cpuset: Give back the bandwidth an attach did not move Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 11/43] cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 12/43] selftests/sched: Test cpuset moves in deadline_bw_test Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 13/43] sched/deadline: Make a rebuild count a task in the root domain it runs in Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 14/43] sched/deadline: Move a task's bandwidth to " Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 15/43] sched/deadline: Keep a task in the root domain that counts its bandwidth Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 16/43] sched/deadline: Keep the last CPU a task has in its root domain Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 17/43] cgroup/cpuset: Refuse to take a deadline task out of " Mathieu Desnoyers
2026-11-27 16:30   ` sashiko-bot
2026-11-27 14:35 ` [PATCH v12 18/43] selftests/sched: Test the root domain of a task in deadline_bw_test Mathieu Desnoyers
2026-11-27 16:10   ` sashiko-bot
2026-11-27 14:35 ` [PATCH v12 19/43] sched/deadline: Count only active CPUs when spreading extra_bw Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 20/43] sched/deadline: Do not truncate a bandwidth to 32 bits in __dl_add/sub() Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 21/43] rtmutex: Continue the chain walk when a waiter's deadline donor changes Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 22/43] rtmutex: Do not stop the chain walk at an owner whose top waiter changed Mathieu Desnoyers
2026-11-27 16:18   ` sashiko-bot
2026-11-27 14:35 ` [PATCH v12 23/43] sched/deadline: Hold a reference on the deadline donor of a boosted task Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 24/43] selftests/sched: Test the rtmutex chain walk in deadline_bw_test Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 25/43] sched/deadline: Count the 0-lag time from the next period Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 26/43] sched/deadline: Keep a departing constrained task until its " Mathieu Desnoyers
2026-11-27 17:00   ` sashiko-bot [this message]
2026-11-27 14:35 ` [PATCH v12 27/43] sched/deadline: Replenish a throttled task boosted while asleep Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 28/43] sched/deadline: Restore the owner's period at a deboost in the class Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 29/43] sched/deadline: Count the bandwidth a boosted task is given Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 30/43] sched/deadline: Start a period for a task that enters the class blocked Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 31/43] sched/deadline: Restore the owner's period at a deboost out of the class Mathieu Desnoyers
2026-11-27 14:35 ` [PATCH v12 32/43] sched/deadline: Start a period for a task requeued onto an rt_mutex Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 33/43] selftests/sched: Test deadline boosts in deadline_bw_test Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 34/43] sched/deadline: Cancel the replenishment timer with the parameters Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 35/43] sched/deadline: Cap the runtime left of a constrained task at dl_runtime Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 36/43] sched/deadline: Check the runtime of a task that comes back runnable Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 37/43] sched/deadline: Keep the debt of a task across a change of parameters Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 38/43] sched/deadline: Boost an owner that cannot run on its own reservation Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 39/43] sched/deadline: Boost an owner throttled after its waiter blocked Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 40/43] sched/deadline: Reschedule a running task throttled at its enqueue Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 41/43] sched/deadline: Run a boost held at its first refill on a donor period Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 42/43] selftests/sched: Test parameter changes in deadline_bw_test Mathieu Desnoyers
2026-11-27 14:36 ` [PATCH v12 43/43] 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-237957@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