[PATCH v11 32/43] sched/deadline: Start a period for a task requeued onto an rt_mutex

From: Mathieu Desnoyers

Date: Wed Nov 25 2026 - 20:10:50 EST


The waiter node of an rt_mutex holds the deadline of its task, as a sort
key. task_blocks_on_rt_mutex() copies it when the task blocks. A task
that blocks by itself is running, so its deadline is a current one.

FUTEX_CMP_REQUEUE_PI is different. It makes a task that sleeps in
FUTEX_WAIT_REQUEUE_PI wait on the rt_mutex of a PI futex, with
rt_mutex_start_proxy_lock(). That task is not running. Its deadline can
be one that it would never run with:
- 0, if sched_setattr() made it SCHED_DEADLINE while it slept.
switched_to_dl() leaves its deadline to its next wakeup.
- one that has passed, if it slept past it.

The waiter then sorts before the waiters with a real deadline. At the
unlock, it gets the lock before waiters with earlier deadlines. Its
deadline also decides the boost of the owner.

Fix this in __rt_mutex_start_proxy_lock(), before its trylock: the
trylock reads the deadline too. Add dl_proxy_block(), which applies the
wakeup rule of update_dl_entity() to the requeued task:
- A deadline of 0, or one that has passed, gets a new period. For a
boosted task, it is a period of its donor.
- A task past its deadline and before its next period keeps its
deadline: its wakeup throttles it until that period. A new period
would start before its next period, which the constrained gap
forbids. So its waiter still sorts first.
- futex_lock_pi() also calls __rt_mutex_start_proxy_lock(), with the
current task. That task is running, so its deadline is up to date.
dl_proxy_block() is not called for it.

The Sashiko AI review of this series found this. The case
requeue_pi_new_period of deadline_bw_test, added later in this series,
tests the deadline of 0. It fails without this patch.

Fixes: 2d3d891d3344 ("sched/deadline: Add SCHED_DEADLINE inheritance logic")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
include/linux/sched/deadline.h | 1 +
kernel/locking/rtmutex_api.c | 12 +++++++
kernel/sched/deadline.c | 59 ++++++++++++++++++++++++++++++++--
3 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/include/linux/sched/deadline.h b/include/linux/sched/deadline.h
index fa66e75cbdd8..5c53b12948a7 100644
--- a/include/linux/sched/deadline.h
+++ b/include/linux/sched/deadline.h
@@ -63,6 +63,7 @@ extern int dl_task_bw_reserve_cpu(struct task_struct *p,
const struct cpumask *new_mask);
extern int dl_task_bw_reserved_cpu(struct task_struct *p);
extern void dl_task_bw_unreserve(struct task_struct *p);
+extern void dl_proxy_block(struct task_struct *p);
extern bool dl_task_keeps_root_domain(struct task_struct *p,
const struct cpumask *new_mask,
const struct cpumask *pending);
diff --git a/kernel/locking/rtmutex_api.c b/kernel/locking/rtmutex_api.c
index 6bdbd36bac94..f1246efc62d3 100644
--- a/kernel/locking/rtmutex_api.c
+++ b/kernel/locking/rtmutex_api.c
@@ -342,6 +342,18 @@ int __sched __rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,

lockdep_assert_held(&lock->wait_lock);

+ /*
+ * A requeued @task sleeps, so its deadline can be stale. The trylock
+ * below and the waiter node both read it: update it first.
+ * futex_lock_pi() passes current, which is running. Its deadline is
+ * up to date.
+ */
+ if (task != current) {
+ raw_spin_lock(&task->pi_lock);
+ dl_proxy_block(task);
+ raw_spin_unlock(&task->pi_lock);
+ }
+
if (try_to_take_rt_mutex(lock, task, NULL))
return 1;

diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 5d1d4ea28878..c59953e80b1a 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -1682,6 +1682,59 @@ static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
}

+#ifdef CONFIG_RT_MUTEXES
+/*
+ * FUTEX_CMP_REQUEUE_PI makes @p wait on an rt_mutex while @p sleeps.
+ * The waiter node of @p takes its deadline before any wakeup. So apply
+ * the wakeup rule of update_dl_entity() to @p now:
+ * - A deadline of 0, which a task that entered the class asleep has,
+ * gets a new period.
+ * - A constrained task past its deadline and before its next period
+ * keeps its deadline. Its wakeup throttles it until that period. Its
+ * waiter node keeps that passed deadline, and sorts first.
+ * - Any other task gets the check of update_dl_entity(). A deadline
+ * that has passed gets a new period.
+ *
+ * The caller holds the pi_lock of @p.
+ */
+void dl_proxy_block(struct task_struct *p)
+{
+ struct sched_dl_entity *dl_se = &p->dl;
+ struct rq_flags rf;
+ struct rq *rq;
+
+ if (!dl_task(p))
+ return;
+ rq = __task_rq_lock(p, &rf);
+ update_rq_clock(rq);
+ if (task_on_rq_queued(p) || dl_se->dl_throttled)
+ goto unlock;
+ /*
+ * A boosted task runs on the parameters of its donor. Start a period
+ * from them if its deadline is 0 or has passed. A task in the class
+ * with no runtime of its own is always boosted: the test of
+ * dl_runtime is only a guard.
+ */
+ if (is_dl_boosted(dl_se) || !dl_se->dl_runtime) {
+ if (!dl_se->deadline ||
+ dl_time_before(dl_se->deadline, rq_clock(rq)))
+ replenish_dl_new_period(dl_se, rq);
+ goto unlock;
+ }
+ if (!dl_se->deadline) {
+ replenish_dl_new_period(dl_se, rq);
+ goto unlock;
+ }
+ if (!dl_is_implicit(dl_se) &&
+ dl_time_before(dl_se->deadline, rq_clock(rq)) &&
+ dl_time_before(rq_clock(rq), dl_next_period(dl_se)))
+ goto unlock;
+ update_dl_entity(dl_se);
+unlock:
+ __task_rq_unlock(rq, p, &rf);
+}
+#endif
+
/*
* If the entity depleted all its runtime, and if we want it to sleep
* while waiting for some new execution time to become available, we
@@ -4939,9 +4992,9 @@ static void switched_to_dl(struct rq *rq, struct task_struct *p)
* 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.
+ * dl_check_constrained_dl() and dl_proxy_block() do, 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)) &&
--
2.47.3