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 v9 32/33] sched/deadline: Check the runtime of a task whose parameters change
Date: Fri, 20 Nov 2026 14:58:22 +0000	[thread overview]
Message-ID: <sashiko-outbox-225415@kernel.org> (raw)
In-Reply-To: <d65436da41c304678dd3d77f003f32d05583fed7.1795181240.git.mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] sched/deadline: missing resched_curr() when enqueue throttles
  curr
- [Medium] sched/deadline: missing dl_new_params handling in
  switched_to_dl()
- [Medium] sched/deadline: stale deadline comparison before dl_deboost()
- [Medium] sched/deadline: __setparam_dl() mixes old deadline with new
  period bounds
- [Medium] sched/deadline: broken PI and sleeping period start in
  dl_deboost()
- [Medium] sched/deadline: desynced rt_mutex waiter deadline via
  dl_new_params handling
- [Medium] sched/deadline: missing PI boost when update_dl_entity()
  pushes owner deadline
--

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

sched/deadline: Check the runtime of a task whose parameters change

This commit adds a dl_new_params flag to struct sched_dl_entity so that
when sched_setattr() modifies a task's runtime, deadline, or period, the
remaining runtime is checked against the new parameters via
update_dl_entity() on the next unboosted enqueue, at deboost in
dl_deboost(), or in prio_changed_dl() for tasks blocked on an rt_mutex.

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index b319173178f3a..77f37a23b808f 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c
> @@ -93,6 +93,19 @@ static inline void dl_start_own_period(struct sched_dl_entity *dl_se)
>  	dl_se->runtime = dl_se->dl_runtime;
>  }
>  
> +/*
> + * 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->deadline - dl_se->dl_deadline + dl_se->dl_period);
> +}

[Severity: Medium]
Does dl_in_constrained_gap() (as well as dl_check_constrained_dl() and
update_dl_entity()) mix the old dl_se->deadline with the newly overwritten
dl_se->dl_deadline and dl_se->dl_period?

__setparam_dl() overwrites dl_se->dl_deadline and dl_se->dl_period in
place while dl_se->deadline is still anchored to old_period_start +
old_dl_deadline.  When a constrained task increases dl_deadline to match
dl_period near the end of its runtime, dl_is_implicit(dl_se) becomes true
on the new parameters, so update_dl_entity() skips
update_dl_revised_wakeup() and calls replenish_dl_new_period(), granting a
full fresh budget within the active period.  Conversely, when an implicit
task past its old deadline shrinks dl_deadline, dl_se->deadline -
dl_se->dl_deadline + dl_se->dl_period shifts the computed period end forward
by old_dl_deadline - new_dl_deadline, causing dl_in_constrained_gap() and
dl_check_constrained_dl() to treat the expired period as an active
constrained gap and throttle the task instead of replenishing it.

> @@ -114,6 +127,8 @@ static inline void dl_note_donor_refill(struct sched_dl_entity *dl_se)
>  		dl_se->dl_donor_refill = 1;
>  }
>  
> +static void update_dl_entity(struct sched_dl_entity *dl_se);
> +
>  /*
>   * End the boost of @dl_se, whose task stays in the class. If the boost
>   * refilled its runtime from the donor's parameters, start a new period
> @@ -122,6 +137,13 @@ static inline void dl_note_donor_refill(struct sched_dl_entity *dl_se)
>   * first. Without a refill, the task keeps the runtime and the deadline
>   * of its own reservation.
>   *
> + * If sched_setattr() changed the parameters during the boost, check the
> + * runtime left against them now, for the same reason. rt_mutex_setprio()
> + * then compares the result with the top waiter. Leave that check to the
> + * next enqueue for a task with a constrained deadline, past its deadline
> + * and before its next period. The enqueue first throttles it until that
> + * period: see dl_check_constrained_dl().
> + *
>   * The caller holds the rq lock and the pi_lock of the task, and has
>   * updated the rq clock.
>   */
> @@ -130,6 +152,14 @@ void dl_deboost(struct sched_dl_entity *dl_se)
>  	if (dl_se->dl_donor_refill)
>  		dl_start_own_period(dl_se);
>  	dl_drop_boost(dl_se);
> +
> +	if (dl_se->dl_new_params && !dl_se->dl_throttled && dl_in_constrained_gap(dl_se))
> +		return;
> +	if (dl_se->dl_new_params) {
> +		dl_se->dl_new_params = 0;
> +		if (!dl_se->dl_throttled)
> +			update_dl_entity(dl_se);
> +	}
>  }

[Severity: Medium]
Can rt_mutex_setprio() skip dl_deboost() by comparing pi_task->dl.deadline
against p->dl.deadline before dl_deboost() updates p->dl.deadline?

kernel/sched/core.c:rt_mutex_setprio() {
	...
			if (!dl_prio(p->normal_prio) ||
			    (pi_task && dl_prio(pi_task->prio) &&
			     dl_entity_preempt(&pi_task->dl, &p->dl))) {
				bool new_donor = p->dl.pi_se != pi_task->dl.pi_se;

				p->dl.pi_se = pi_task->dl.pi_se;
				scope->flags |= ENQUEUE_REPLENISH;
				if (!scope->queued && new_donor)
					dl_boost_blocked(p);
			} else {
				dl_deboost(&p->dl);
	...
}

If a boosted owner p changes its parameters to a shorter deadline via
sched_setattr() and its current donor leaves while a second deadline waiter
pi_task remains, pi_task->dl.deadline can be earlier than p's stale
p->dl.deadline even though update_dl_entity() in dl_deboost() would give p
an earlier deadline than pi_task.  Because dl_entity_preempt() runs first,
rt_mutex_setprio() takes the boost branch, skips dl_deboost(), and leaves p
running on its stale later deadline and old runtime.

[Severity: High]
When dl_deboost() returns early on dl_in_constrained_gap(dl_se) for a
running task (or when sched_setattr() puts a running task into a
constrained gap), who calls resched_curr(rq) after enqueue_dl_entity()
throttles it?

In sched_change_end(), enqueue_dl_entity() calls dl_check_constrained_dl(),
which sets dl_se->dl_throttled = 1 and starts dl_se->dl_timer, and
enqueue_dl_entity() then returns early without calling
__enqueue_dl_entity().  Next, prio_changed_dl() sees p->dl.deadline ==
old_deadline and also returns early without calling resched_curr(rq).  The
throttled task stays on the CPU as rq->curr with !on_dl_rq(&p->dl), where
update_curr_dl() returns immediately without accounting runtime until the
replenishment timer or another wakeup triggers a reschedule.

[Severity: Medium]
Does returning early on dl_in_constrained_gap(dl_se) in dl_deboost() (and
prio_changed_dl()), or calling update_dl_entity(dl_se) unconditionally
afterward, break PI boosting and sleeping tasks?

There are three issues here:

First, when a lock owner p is deboosted while in its constrained gap and
another deadline waiter pi_task with pi_task->dl.deadline > now is waiting
on the lock, dl_deboost() drops the boost and leaves p->dl.deadline in the
past (< now).  In rt_mutex_setprio(), dl_entity_preempt(&pi_task->dl,
&p->dl) then sees p's expired deadline as earlier than pi_task->dl.deadline
and fails to reboost p, so sched_change_end() -> enqueue_dl_entity() ->
dl_check_constrained_dl() throttles the lock owner while pi_task waits on
it.

Second, for a task blocked on an rt_mutex in its constrained gap, both
dl_deboost() and prio_changed_dl() leave its expired deadline (< now) in
place, so rt_mutex_adjust_pi() / rt_mutex_adjust_prio_chain() orders its
waiter ahead of runnable waiters with future deadlines; when the lock is
released, mark_wakeup_next_waiter() wakes only that top waiter, which is
immediately throttled in dl_check_constrained_dl() without taking the lock.

Third, when !dl_in_constrained_gap(dl_se), dl_deboost() calls
update_dl_entity(dl_se) even when the task is sleeping on a non-PI
waitqueue (!task_on_rq_queued(p) && !dl_task_pi_blocked(p)), starting a new
period while the task is asleep so that its budget is reduced or throttled
by the time it wakes up.

[ ... ]

> @@ -2961,12 +3000,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 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]
When update_dl_entity() here (or in prio_changed_dl()) starts a new period
for an unboosted rt_mutex owner and pushes p->dl.deadline past its top
waiter's deadline, does anything PI-boost p with that waiter?

In __sched_setscheduler(), __setscheduler_dl_pi() runs before
sched_change_end() and only checks !dl_policy(policy).  If p already runs
under SCHED_DEADLINE and holds an rt_mutex with a deadline waiter whose
deadline was later than p's old deadline, shrinking p's reservation causes
update_dl_entity() to advance p->dl.deadline past the waiter's deadline
without adopting the waiter's pi_se, leaving p unboosted on its reduced
budget while holding the lock.

> @@ -4832,8 +4872,22 @@ static u64 get_prio_dl(struct rq *rq, struct task_struct *p)
>   */
>  static void prio_changed_dl(struct rq *rq, struct task_struct *p, u64 old_deadline)
>  {
> -	if (!task_on_rq_queued(p))
> +	if (!task_on_rq_queued(p)) {
> +		/*
> +		 * The deadline of a task blocked on an rt_mutex is in its
> +		 * waiter node, before any wakeup. If sched_setattr() changed
> +		 * its parameters, check its runtime against them now, as
> +		 * dl_deboost() does. rt_mutex_adjust_pi() then requeues the
> +		 * waiter with the result.
> +		 */
> +		if (p->dl.dl_new_params && !is_dl_boosted(&p->dl) &&
> +		    !p->dl.dl_throttled && dl_task_pi_blocked(p) &&
> +		    !dl_in_constrained_gap(&p->dl)) {
> +			p->dl.dl_new_params = 0;
> +			update_dl_entity(&p->dl);
> +		}
>  		return;
> +	}

[Severity: Medium]
Should switched_to_dl() also handle p->dl.dl_new_params for a task blocked
on an rt_mutex?

kernel/sched/deadline.c:switched_to_dl() {
	...
	if (!task_on_rq_queued(p)) {
		/*
		 * Give a deadline to a task that enters the class while it
		 * waits on an rt_mutex: rt_mutex_adjust_pi() reads it next.
		 * A task that comes back before its deadline keeps its old
		 * period; enqueue_dl_entity() checks the runtime left at the
		 * wakeup.
		 */
		if (dl_task_pi_blocked(p) && !p->dl.dl_throttled &&
		    dl_time_before(p->dl.deadline, rq_clock(rq)))
			replenish_dl_new_period(&p->dl, rq);

		return;
	}
	...
}

When a task enters or re-enters SCHED_DEADLINE via __sched_setscheduler()
while blocked on an rt_mutex, __setparam_dl() sets p->dl.dl_new_params = 1,
and sched_change_end() takes the ENQUEUE_CLASS branch and calls
switched_to_dl() instead of prio_changed_dl().  If the task returns to
SCHED_DEADLINE before its old deadline with a smaller reservation,
switched_to_dl() does not call update_dl_entity(&p->dl), so
rt_mutex_adjust_pi() queues the waiter with its stale old deadline; and if
p->dl.deadline was in the past, switched_to_dl() starts a new period
without clearing p->dl.dl_new_params.

[Severity: Medium]
Can consuming p->dl.dl_new_params in prio_changed_dl() desynchronize
p->dl.deadline from the task's rt_mutex waiter node when prio_changed_dl()
is reached from callers other than __sched_setscheduler()?

If p->dl.dl_new_params remains set while p is blocked on an rt_mutex (after
switched_to_dl(), or after prio_changed_dl() skipped update_dl_entity() due
to dl_in_constrained_gap(&p->dl) or is_dl_boosted(&p->dl)), a later
sched_move_task(), do_set_cpus_allowed(), or sched_setnuma() on p runs
sched_change_end() -> prio_changed_dl().  Because time has elapsed while p
was blocked, update_dl_entity(&p->dl) sees dl_entity_overflow() and pushes
p->dl.deadline to a new period, but those callers do not call
rt_mutex_adjust_pi(p) afterward, leaving p->dl.deadline out of sync with
p->pi_blocked_on->tree.deadline.

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

  reply	other threads:[~2026-11-20 14:58 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 13:55 [PATCH v9 00/33] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 01/33] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 02/33] sched/deadline: Rebuild the root domains' accounting from the task list Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 03/33] sched/deadline: Record the root domain holding a task's bandwidth Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 04/33] sched/deadline: Leave a dying task out of a rebuild of the accounting Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 05/33] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 06/33] selftests/sched: Test the root domain rebuild in deadline_bw_test Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 07/33] sched/deadline: Move a task's bandwidth only where room was made Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 08/33] cgroup/cpuset: Wait for an attach before rebuilding the sched domains Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 09/33] sched: Make deadline policy and affinity changes wait for a cpuset attach Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 10/33] cgroup/cpuset: Give back the bandwidth an attach did not move Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 11/33] cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 12/33] selftests/sched: Test cpuset moves in deadline_bw_test Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 13/33] sched/deadline: Make a rebuild count a task in the root domain it runs in Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 14/33] sched/deadline: Move a task's bandwidth to " Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 15/33] sched/deadline: Keep a task in the root domain that counts its bandwidth Mathieu Desnoyers
2026-11-20 14:12   ` sashiko-bot
2026-11-20 13:55 ` [PATCH v9 16/33] sched/deadline: Keep the last CPU a task has in its root domain Mathieu Desnoyers
2026-11-20 14:31   ` sashiko-bot
2026-11-20 13:55 ` [PATCH v9 17/33] cgroup/cpuset: Refuse to take a deadline task out of " Mathieu Desnoyers
2026-11-20 14:32   ` sashiko-bot
2026-11-20 13:55 ` [PATCH v9 18/33] selftests/sched: Test the root domain of a task in deadline_bw_test Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 19/33] sched/deadline: Count only active CPUs when spreading extra_bw Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 20/33] rtmutex: Continue the chain walk when a waiter's deadline donor changes Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 21/33] rtmutex: Continue the walk when the owner's top waiter changes lock Mathieu Desnoyers
2026-11-20 14:20   ` sashiko-bot
2026-11-20 13:55 ` [PATCH v9 22/33] selftests/sched: Test the rtmutex chain walk in deadline_bw_test Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 23/33] sched/deadline: Reset the donor's runtime at a deboost in the class Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 24/33] sched/deadline: Replenish a throttled task boosted while asleep Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 25/33] sched/deadline: Count the bandwidth a boosted task is given Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 26/33] sched/deadline: Start a period for a task that enters the class blocked Mathieu Desnoyers
2026-11-20 14:39   ` sashiko-bot
2026-11-20 13:55 ` [PATCH v9 27/33] sched/deadline: Start a period at a deboost out of the class Mathieu Desnoyers
2026-11-20 14:27   ` sashiko-bot
2026-11-20 13:55 ` [PATCH v9 28/33] selftests/sched: Test deadline boosts in deadline_bw_test Mathieu Desnoyers
2026-11-20 14:26   ` sashiko-bot
2026-11-20 13:55 ` [PATCH v9 29/33] sched/deadline: Cancel the replenishment timer with the parameters Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 30/33] sched/deadline: Cap the runtime left of a constrained task at dl_runtime Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 31/33] sched/deadline: Check the runtime of a task that comes back runnable Mathieu Desnoyers
2026-11-20 13:55 ` [PATCH v9 32/33] sched/deadline: Check the runtime of a task whose parameters change Mathieu Desnoyers
2026-11-20 14:58   ` sashiko-bot [this message]
2026-11-20 13:55 ` [PATCH v9 33/33] selftests/sched: Test parameter changes in deadline_bw_test 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-225415@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