[PATCH v11 30/43] sched/deadline: Start a period for a task that enters the class blocked
From: Mathieu Desnoyers
Date: Wed Nov 25 2026 - 20:11:31 EST
Commit fb00aca47440 ("rtmutex: Turn the plist into an rb-tree") sorts
the waiters of an rt_mutex by priority. For deadline tasks, "the one
with the earliest deadline wins". A waiter takes the deadline of its
task. A task that blocks by itself is running, so its deadline is a
current one.
A task can also enter the class while it waits on an rt_mutex. Another
task can make it SCHED_DEADLINE with sched_setattr(). A priority
inheritance boost can also put it in the class, if it owns another
rt_mutex. switched_to_dl() does not set up a task that is not queued.
Its wakeup does. But the chain walk that follows the change reads the
deadline before that wakeup, and copies it into the waiter. A task new
to the class has a deadline of 0.
So the waiter goes before every other deadline waiter. It takes the
rt_mutex first, whatever their deadlines. Those waiters can then miss
their deadlines. It also boosts a deadline owner whose own deadline is
earlier.
Fix this by starting a new period while the task waits, as
setup_new_dl_entity() does at an enqueue. The period uses the
parameters the task runs on: its own, or its donor's when it is
boosted. At the wakeup, update_dl_entity() then checks that period as
for any wakeup. Two places start the period:
- switched_to_dl(), for a task that waits on an rt_mutex, if its
deadline has passed.
- rt_mutex_setprio(), with the new dl_boost_blocked(), when a blocked
task with no parameters of its own gets a new donor. This includes
the boost that puts it in the class. For a queued task, the enqueue
of the boost already does this.
switched_to_dl() leaves one case alone. A task that comes back to the
class can keep its deadline. v7.4-rc2 always keeps it. With the first
series, it keeps it only if it comes back before its 0-lag time. So a
constrained task can come back past its deadline and before its next
period. A new period would let it run before its next period. So, as
dl_check_constrained_dl() does, leave it alone unless it is boosted. Its
wakeup throttles it until its next period.
Its waiter then keeps the passed deadline, and sorts before the other
deadline waiters. At an unlock, it is woken and throttled. The lock then
stays free until its next period.
The setattr_blocked_keeps_order and boost_blocked_keeps_order cases,
added to deadline_bw_test later in this series, test this. Both fail on
v7.4-rc2 and on the series without this patch. The case
reenter_pi_blocked_in_gap tests the constrained task left alone. The
triage of the Sashiko AI review of this series found the bug.
Fixes: fb00aca47440 ("rtmutex: Turn the plist into an rb-tree")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
kernel/sched/core.c | 4 +++
kernel/sched/deadline.c | 55 +++++++++++++++++++++++++++++++++++++++++
kernel/sched/sched.h | 1 +
3 files changed, 60 insertions(+)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 40bfa1364b90..5b2ac1b3f6f6 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -7969,8 +7969,12 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
if (!dl_prio(p->normal_prio) ||
(pi_task && dl_prio(pi_task->prio) &&
dl_entity_preempt(&pi_task->dl, &p->dl))) {
+ struct sched_dl_entity *old_donor = p->dl.pi_se;
+
dl_set_donor(&p->dl, pi_task);
scope->flags |= ENQUEUE_REPLENISH;
+ if (!scope->queued && p->dl.pi_se != old_donor)
+ dl_boost_blocked(p);
} else {
dl_deboost(&p->dl);
/*
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 5ec1c5eaf764..4d46d1c9c698 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -228,6 +228,12 @@ void dl_deboost(struct sched_dl_entity *dl_se)
}
}
}
+
+/* Return true if @p waits on an rt_mutex. The caller holds its pi_lock. */
+static inline bool dl_task_pi_blocked(struct task_struct *p)
+{
+ return p->pi_blocked_on;
+}
#else /* !CONFIG_RT_MUTEXES: */
static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
{
@@ -242,6 +248,11 @@ static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
{
return false;
}
+
+static inline bool dl_task_pi_blocked(struct task_struct *p)
+{
+ return false;
+}
#endif /* !CONFIG_RT_MUTEXES */
static inline u8 dl_get_type(struct sched_dl_entity *dl_se, struct rq *rq)
@@ -1301,6 +1312,30 @@ static inline void replenish_dl_new_period(struct sched_dl_entity *dl_se,
trace_sched_dl_replenish_tp(dl_se, cpu_of(rq), dl_get_type(dl_se, rq));
}
+#ifdef CONFIG_RT_MUTEXES
+/*
+ * Start a period from the new donor of @p, if @p has no reservation of its
+ * own and waits on an rt_mutex. The enqueue does this for a queued task, in
+ * replenish_dl_entity(). A blocked task is not enqueued, but the chain walk
+ * copies its deadline into its waiter.
+ *
+ * Call it only when the donor changes. A new period changes the deadline,
+ * so the chain walk finds the waiter out of date and goes on. In a ww_mutex
+ * cycle, a new period at each step would never let the walk stop.
+ *
+ * The caller holds the rq lock and the pi_lock of @p, and has updated the
+ * rq clock.
+ */
+void dl_boost_blocked(struct task_struct *p)
+{
+ struct sched_dl_entity *dl_se = &p->dl;
+
+ if (!dl_se->dl_deadline && !dl_se->dl_throttled &&
+ dl_task_pi_blocked(p))
+ replenish_dl_new_period(dl_se, task_rq(p));
+}
+#endif
+
/*
* We are being explicitly informed that a new instance is starting,
* and this means that:
@@ -4866,6 +4901,26 @@ static void switched_to_dl(struct rq *rq, struct task_struct *p)
if (!task_on_rq_queued(p)) {
add_rq_bw(&p->dl, &rq->dl);
+ /*
+ * The chain walk sorts the waiter of a task by its deadline,
+ * before the wakeup sets the task up. A task new to the class
+ * has a deadline of 0, which goes before every real one. So
+ * start a new period now for a task that waits on an
+ * rt_mutex, as setup_new_dl_entity() does at an enqueue.
+ *
+ * A task that comes back to the class before its 0-lag time
+ * keeps its deadline. For a constrained task, that deadline
+ * can have passed before its next period. As
+ * dl_check_constrained_dl() does, leave such a task alone
+ * unless it is boosted. Its wakeup throttles it until its
+ * next period.
+ */
+ if (dl_task_pi_blocked(p) && !p->dl.dl_throttled &&
+ dl_time_before(p->dl.deadline, rq_clock(rq)) &&
+ (is_dl_boosted(&p->dl) ||
+ !dl_time_before(rq_clock(rq), dl_next_period(&p->dl))))
+ replenish_dl_new_period(&p->dl, rq);
+
return;
}
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index b53885c624fd..32560b08808e 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -413,6 +413,7 @@ static inline void dl_clear_donor(struct sched_dl_entity *dl_se)
}
extern void dl_deboost(struct sched_dl_entity *dl_se);
+extern void dl_boost_blocked(struct task_struct *p);
/*
* End the boost of @dl_se, and forget the deadline and runtime saved for
--
2.47.3