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 v11 37/43] sched/deadline: Keep the debt of a task across a change of parameters
Date: Thu, 26 Nov 2026 02:16:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-234632@kernel.org> (raw)
In-Reply-To: <33e066447added164bbf6c39e0e4de022d7ecc7e.1795646914.git.mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [Medium] sched/deadline: unthrottled zero-runtime wakeup in
  dl_setattr_place()
- [Medium] sched/deadline: deferred boost debt and timer race in
  dl_setattr_period()
- [Medium] sched/deadline: missing zero-deadline check in
  dl_in_constrained_gap()
- [Medium] sched/deadline: redundant update_dl_entity() check after
  dl_setattr_period()
- [Medium] sched/deadline: mismatched period fitting in
  dl_drop_boost_out()
- [Medium] sched/deadline: own_runtime zeroed before debt calc in
  dl_hold_refilled_boost()
- [Medium] sched/deadline: extra period bw retention when
  dl_task_timer() refills departed task
- [Medium] sched/deadline: stale inactive_timer when sched_setattr()
  keeps same dl_bw
--

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

sched/deadline: Keep the debt of a task across a change of parameters

This commit fits the period in progress of a SCHED_DEADLINE task to new
parameters across sched_setattr() so that any accumulated runtime debt is
preserved and repaid at the new bandwidth.

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index 23186eb9968e8..1ec7789e81b86 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c
[ ... ]
> @@ -130,6 +132,19 @@ static inline bool dl_end_held_boost(struct sched_dl_entity *dl_se)
>  	return true;
>  }
>  
> +/*
> + * Return true if @dl_se has a constrained deadline, and the rq clock is
> + * past its deadline and before its next period. An enqueue then throttles
> + * it until that period.
> + */
> +static inline bool dl_in_constrained_gap(struct sched_dl_entity *dl_se)
> +{
> +	u64 now = rq_clock(rq_of_dl_se(dl_se));
> +
> +	return !dl_is_implicit(dl_se) && dl_time_before(dl_se->deadline, now) &&
> +	       dl_time_before(now, dl_se->period_start + dl_se->dl_period);
> +}

[Severity: Medium]
Should dl_in_constrained_gap() check that dl_se->deadline is non-zero
first, as dl_proxy_block() does?

For a task new to SCHED_DEADLINE (or after __dl_clear_params()), both
deadline and period_start are 0.  If the task is blocked on an rt_mutex
and given a constrained deadline while rq_clock(rq) < dl_se->dl_period,
dl_in_constrained_gap() returns true, so both dl_check_blocked_params()
and switched_to_dl() skip starting a period and leave deadline == 0 in
the task's rt_mutex waiter node.

[ ... ]

> @@ -227,12 +272,80 @@ void dl_deboost(struct sched_dl_entity *dl_se)
[ ... ]
> +void dl_drop_boost_out(struct sched_dl_entity *dl_se)
> +{
> +	bool saved = dl_se->dl_setattr_saved && !dl_se->dl_donor_refill;
> +
> +	dl_drop_boost(dl_se);
> +	if (saved)
> +		dl_setattr_period(dl_se, dl_se->setattr_runtime,
> +				  dl_se->setattr_deadline, dl_se->setattr_period);
> +}

[Severity: Medium]
Does dl_drop_boost_out() fit the period with different rules from the
ones switched_from_dl() just used to arm inactive_timer?

When a boosted task with dl_setattr_saved leaves SCHED_DEADLINE,
switched_from_dl() runs first and computes the 0-lag time via
dl_zerolag_period(), which calls dl_setattr_place() with run = false
(so keep = 0).  dl_drop_boost_out() then calls dl_setattr_period(),
which passes run = !dl_se->dl_throttled && !asleep.  For a queued or
pi-blocked task with runtime > 0, dl_setattr_place() keeps up to
dl_runtime, pushes period_start and deadline past the 0-lag time that
inactive_timer was armed for, and leaves runtime = keep unthrottled (or,
when keep == 0 and debt > 0, sets dl_throttled = 1 and arms dl_timer on
a task that has already left SCHED_DEADLINE).

[ ... ]

> @@ -1934,6 +2108,10 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
>  		if (dl_se->dl_throttled && !is_dl_boosted(dl_se)) {
>  			sched_clock_tick();
>  			update_rq_clock(rq);
> +			/* Not before the next period: see below. */
> +			if (dl_time_before(rq_clock(rq), dl_next_period(dl_se)) &&
> +			    start_dl_timer(dl_se))
> +				goto unlock;
>  			replenish_dl_entity(dl_se);
>  		}
>  		goto unlock;

[Severity: Medium]
When a throttled constrained-deadline task leaves SCHED_DEADLINE, can
replenishing it here in dl_task_timer() cause inactive_task_timer() to
hold its bandwidth for an extra period?

Both dl_timer and inactive_timer are armed for dl_next_period(dl_se)
(or dl_timer fires first when an overrun places the 0-lag time after
dl_next_period(dl_se)).  When dl_task_timer() runs replenish_dl_entity(),
it advances dl_se->period_start by dl_period, so inactive_task_timer()
then sees dl_next_period(dl_se) - rq_clock(rq) > 0 and restarts itself
for another full period before releasing total_bw:

kernel/sched/deadline.c:inactive_task_timer() {
    ...
	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;
		}
	}
    ...
}

[ ... ]

> @@ -3159,12 +3361,13 @@ enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags)
>  		update_dl_entity(dl_se);
>  	} else if (flags & ENQUEUE_REPLENISH) {
>  		replenish_dl_entity(dl_se);
> -	} else if ((flags & ENQUEUE_CLASS) && !is_dl_boosted(dl_se)) {
> +	} else if (((flags & ENQUEUE_CLASS) || new_params) &&
> +		   !is_dl_boosted(dl_se)) {
>  		/*
> -		 * The task enters the class, or its parameters change, while
> -		 * it is runnable. It may come back before
> -		 * its deadline, with the runtime left from older parameters.
> -		 * Check that runtime against the new parameters,
> -		 * as a wakeup does.
> +		 * The task enters the class, or its parameters change, while
> +		 * it is runnable. It may have runtime left from older
> +		 * parameters. Check that runtime against the new parameters,
> +		 * as a wakeup does.
>  		 */
>  		update_dl_entity(dl_se);
>  	} else if ((flags & ENQUEUE_MOVE) &&

[Severity: Medium]
Does testing (flags & ENQUEUE_CLASS) here run update_dl_entity() on a
period that dl_setattr_period() has already fitted?

When a runnable task leaves SCHED_DEADLINE and re-enters before its
0-lag time with modified parameters, __setparam_dl() fits the period
via dl_setattr_period() and clears dl_new_params.  As noted in the
comment on dl_setattr_period(), running update_dl_entity() afterward
checks dl_entity_overflow() at microsecond granularity, which can see a
rounding overflow on the just-fitted period and call
replenish_dl_new_period(), wiping out the debt.  In __setparam_dl(),
the !pending guard has the same effect when dl_new_params was already
set before a change that dl_setattr_period() fits.

[ ... ]

> @@ -5325,47 +5529,298 @@ int sched_dl_overflow(struct task_struct *p, int policy,
[ ... ]
> +static bool dl_setattr_place(struct sched_dl_entity *dl_se, u64 now,
> +			     u64 *start, u64 *deadline, s64 *runtime,
> +			     u64 old_runtime, u64 old_deadline, u64 old_period,
> +			     bool run, bool asleep)
> +{
[ ... ]
> +	*runtime = 0;
> +	if (asleep && dl_deadline < dl_period) {
> +		if (dl_time_before(next, now))
> +			next = now;
> +		*start = next - dl_period;
> +		*deadline = *start + dl_deadline;
> +		return false;
> +	}

[Severity: Medium]
What happens if a sleeping task with a new constrained deadline wakes up
before *deadline?

When next - now >= dl_period - dl_deadline (for instance, when debt
repayment exceeds the constrained gap, or when now < release + dl_deadline
forces next >= release + dl_period), *deadline = next - dl_period +
dl_deadline is in the future while *runtime is set to 0 and
dl_setattr_place() returns false (clearing dl_throttled).  If the task
wakes up before *deadline, dl_check_constrained_dl() does not throttle
it because deadline >= now, and dl_entity_overflow() returns false
because left = (dl_deadline >> DL_SCALE) * (0 >> DL_SCALE) = 0 is not
greater than right >= 0.  The task is then enqueued unthrottled with
runtime == 0 before its next period at next.

> +	if (!dl_time_before(now, next)) {
> +		*start = now;
> +		*deadline = now + dl_deadline;
> +		*runtime = dl_runtime;
> +		return false;
> +	}
> +	*start = next - dl_period;
> +	*deadline = *start + dl_deadline;
> +	return true;
> +}
> +
[ ... ]
> +static bool dl_setattr_period(struct sched_dl_entity *dl_se, u64 old_runtime,
> +			      u64 old_deadline, u64 old_period)
> +{
[ ... ]
> +	if (is_dl_boosted(dl_se)) {
> +		if (!dl_se->dl_setattr_saved && !dl_se->dl_donor_refill) {
> +			dl_se->setattr_runtime = old_runtime;
> +			dl_se->setattr_deadline = old_deadline;
> +			dl_se->setattr_period = old_period;
> +			dl_se->dl_setattr_saved = 1;
> +		}
> +		return true;
> +	}

[Severity: Medium]
When a boosted task's bandwidth is reduced before any donor refill, does
deferring the fit until deboost or refill repay the debt at the old
bandwidth instead of the new one?

sched_dl_overflow() immediately releases the old bandwidth in total_bw
at sched_setattr(), but dl_setattr_place() is only called later at the
deboost or first donor refill with that later timestamp as now.
Computing debt = (zerolag - now) * old_runtime / old_period at the later
timestamp credits the entire interval since sched_setattr() at
old_runtime / old_period rather than the new bandwidth.  Also, because
setattr_* are only recorded when !dl_setattr_saved, a second
sched_setattr() back to the original parameters before deboost leaves
dl_setattr_saved set and still fits against the first period's state.

> +
> +	/*
> +	 * start_dl_timer() does not move a queued timer: cancel it first. If
> +	 * its handler is already running, the cancel fails. The handler then
> +	 * waits for the rq lock. It finds the task no longer throttled, or
> +	 * arms the timer again at the next period set here.
> +	 */
> +	cancel_replenish_timer(dl_se);

[Severity: Medium]
If a sleeping task has dl_non_contending set and sched_setattr() changes
its period without changing dl_bw, can the old inactive_timer fire too
early if the task then leaves SCHED_DEADLINE?

When new_bw == dl_se->dl_bw (for example, when changing a constrained
dl_deadline to implicit while keeping dl_runtime and dl_period, or
scaling both proportionally), dl_change_utilization() returns early and
leaves inactive_timer armed for the old 0-lag time.  dl_setattr_period()
then updates period_start, deadline and runtime, and if the sleeping
task subsequently leaves SCHED_DEADLINE, switched_from_dl() sees
p->dl.dl_non_contending == 1 and skips task_non_contending(), so
inactive_task_timer() fires at the stale pre-setattr 0-lag time and
releases the bandwidth and clears the parameters early.

[ ... ]

> +static void dl_hold_refilled_boost(struct sched_dl_entity *dl_se)
> +{
> +	u64 old_runtime = dl_se->setattr_runtime;
> +	u64 old_deadline = dl_se->setattr_deadline;
> +	u64 old_period = dl_se->setattr_period;
> +	bool saved = dl_se->dl_setattr_saved;
> +	u64 now = rq_clock(rq_of_dl_se(dl_se));
> +
> +	dl_se->own_deadline = dl_se->deadline;
> +	dl_se->own_runtime = dl_time_before(now, dl_se->deadline) ?
> +			     max_t(s64, dl_se->runtime, 0) : 0;
> +	dl_se->own_period_start = dl_se->period_start;
> +	dl_se->dl_boost_held = 1;
> +	dl_se->dl_setattr_saved = 0;
> +	if (saved && dl_se->own_deadline && old_runtime && old_period)
> +		dl_setattr_place(dl_se, now, &dl_se->own_period_start,
> +				 &dl_se->own_deadline, &dl_se->own_runtime,
> +				 old_runtime, old_deadline, old_period, false, false);
> +}

[Severity: Medium]
Does zeroing or clamping dl_se->own_runtime before calling
dl_setattr_place() corrupt the debt calculation?

dl_setattr_place() uses the incoming *runtime to compute how much of the
old period's runtime was used (adjusting zerolag by *runtime *
old_period / old_runtime) before setting *runtime = 0 when run is false.
When a boosted constrained task has unused runtime (dl_se->runtime > 0)
and its first donor refill happens after dl_se->deadline (or when
sched_setattr() is called later while dl_boost_held is set), zeroing
own_runtime first makes dl_setattr_place() treat the entire old_runtime
as consumed and charge debt for runtime the task never used.

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

  reply	other threads:[~2026-11-26  2:16 UTC|newest]

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