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 v13 05/36] sched/deadline: Keep a departing constrained task until its next period
Date: Tue, 01 Dec 2026 18:17:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-243957@kernel.org> (raw)
In-Reply-To: <835511935d961be832cbaf24e902762d1926e611.1796146461.git.mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] sched/deadline: premature bw release in inactive_task_timer()
  on pending race
- [Medium] sched/deadline: extra period wait when dl_task_timer()
  precedes inactive timer
--

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

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

This commit retains the bandwidth and deadline of a constrained
SCHED_DEADLINE task that leaves the class until the later of its 0-lag
time and its next period, and adds dl_inactive_pending to coordinate with
an active inactive_task_timer() handler.

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index a02975eb91f63..cd7f2ada8cdf3 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c
> @@ -515,6 +515,7 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)
>  	struct rq *rq = rq_of_dl_se(dl_se);
>  	struct dl_rq *dl_rq = &rq->dl;
>  	s64 zerolag_time;
> +	bool running, pending;
>  
>  	/*
>  	 * If this is a non-deadline task that has been boosted,
> @@ -528,13 +529,50 @@ 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, which may be queued
> +	 * again. Cancel it here. A running handler cannot be canceled.
> +	 * dl_inactive_pending then tells whether it will still act on the
> +	 * task, once it gets the rq lock or after its restart.
> +	 */
> +	if (dl_server(dl_se)) {
> +		running = hrtimer_active(timer);
> +		pending = running;
> +	} else {
> +		int ret = hrtimer_try_to_cancel(timer);
> +
> +		if (ret == 1)
> +			put_task_struct(dl_task_of(dl_se));
> +		running = ret < 0;
> +		pending = running && dl_se->dl_inactive_pending;
> +	}
> +
>  	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;
> +	}

[Severity: Medium]
Can a throttled constrained task that leaves SCHED_DEADLINE end up keeping
its bandwidth and deadline for an extra unused period?

When a constrained task is throttled, start_dl_timer() arms dl_se->dl_timer
for dl_next_period(dl_se).  If the task switches out of SCHED_DEADLINE
before that period ends, task_non_contending() arms inactive_timer for at
least dl_next_period(dl_se), so dl_task_timer() expires at the period
boundary before inactive_task_timer() and replenishes the departed task:

kernel/sched/deadline.c:dl_task_timer() {
    ...
	if (!dl_task(p)) {
		if (dl_se->dl_throttled && !is_dl_boosted(dl_se)) {
			sched_clock_tick();
			update_rq_clock(rq);
			replenish_dl_entity(dl_se);
		}
		goto unlock;
	}
    ...
}

replenish_dl_entity() adds dl_period to dl_se->deadline and dl_runtime to
dl_se->runtime.  That leaves dl_time_to_zerolag() unchanged, but shifts
dl_next_period(dl_se) forward by a full period.  When inactive_task_timer()
then runs, it sees dl_next_period(dl_se) - rq_clock(rq) > 0 for the newly
replenished period and returns HRTIMER_RESTART.

The same happens here in task_non_contending() if a sleeping throttled
constrained task is replenished at the period boundary by dl_task_timer()
and then leaves SCHED_DEADLINE while still asleep before consuming any
runtime in the new period (so zerolag_time < 0, but next > 0).  In both
cases, the task holds its bandwidth in total_bw for a second period in
which it never ran as SCHED_DEADLINE, and if it returns to the class after
the replenished dl_se->deadline in that second period,
dl_check_constrained_dl() throttles it until the period ends.

>  	/*
>  	 * If the "0-lag time" already passed, decrease the active
>  	 * utilization now, instead of starting a timer
>  	 */
> -	if ((zerolag_time < 0) || hrtimer_active(&dl_se->inactive_timer)) {
> +	if ((zerolag_time < 0) || (running && (dl_task || pending))) {
>  		if (dl_server(dl_se)) {
>  			sub_running_bw(dl_se, dl_rq);
>  		} else {
> @@ -543,6 +581,14 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)
>  			if (dl_task)
>  				sub_running_bw(dl_se, dl_rq);
>  
> +			/*
> +			 * The pending handler of a task that has left the
> +			 * class releases its bandwidth, or restarts for its
> +			 * next period.
> +			 */
> +			if (pending && !dl_task &&
> +			    READ_ONCE(p->__state) != TASK_DEAD)
> +				return;

[ ... ]

> @@ -2321,6 +2369,40 @@ 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);
> +
> +			/*
> +			 * The timer is not queued: while this handler runs,
> +			 * task_non_contending() finds dl_inactive_pending set
> +			 * and does not arm it.
> +			 */
> +			WARN_ON_ONCE(hrtimer_is_queued(timer));
> +			hrtimer_set_expires(timer, at);
> +			task_rq_unlock(rq, p, &rf);
> +			return HRTIMER_RESTART;
> +		}
> +	}

[Severity: Medium]
Can inactive_task_timer() release a departing task's bandwidth before its
0-lag time when task_non_contending() takes the pending early return?

When pending is true for a departing task, task_non_contending() returns
early for both implicit and constrained tasks, relying on the running
inactive_task_timer() handler to wait until the required retention time.
However, inactive_task_timer() only checks dl_next_period(dl_se) -
rq_clock(rq) when !dl_is_implicit(dl_se), and never checks
dl_time_to_zerolag(dl_se, rq):

CPU0                                  CPU1
                                      T blocks before 0-lag time
                                        task_non_contending() arms timer
inactive_task_timer()
  task_rq_lock(T) waits
                                      T wakes up at/after 0-lag time
                                        replenish_dl_new_period()
                                        T runs and consumes runtime
                                      sched_setattr(T, SCHED_OTHER)
                                        switched_from_dl()
                                          task_non_contending()
                                            pending is true -> returns
  acquires task_rq_lock(T)
  __dl_sub() / __dl_clear_params()  <-- releases bandwidth before new 0-lag

If T has an implicit deadline, !dl_is_implicit(dl_se) is false, so
inactive_task_timer() immediately subtracts T's bandwidth from total_bw and
clears its parameters even though its new 0-lag time is in the future.  If
T has a constrained deadline and overran its runtime (dl_se->runtime < 0),
dl_time_to_zerolag(dl_se, rq) is later than dl_next_period(dl_se) -
rq_clock(rq), so inactive_task_timer() releases T's bandwidth at
dl_next_period(dl_se) before the overrun is paid back.  In either case, T
can re-enter SCHED_DEADLINE and consume more than its reserved bandwidth.

In addition, unlike task_non_contending(), this check does not guard on
dl_se->deadline != 0 before calling dl_next_period(dl_se), which can be 0
if a sleeping task enters and leaves SCHED_DEADLINE with constrained
parameters while a previously restarted inactive_task_timer() is waiting
for task_rq_lock().

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

  reply	other threads:[~2026-12-01 18:17 UTC|newest]

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