Skip to contentSkip to conversation

sched/deadline: bandwidth accounting fixes and selftests

PATCH New Mathieu Desnoyers 2 participants 58 messages Nov 27 Page 2 of 2

Subsystems (8)
lore
Share & tools
lore
#49 lore raw

Re: [PATCH v12 38/43] sched/deadline: Boost an owner that cannot run on its own reservation

In reply to Mathieu Desnoyers
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [Medium] sched/deadline: mismatched runtime <= 0 checks in dl_waiter_boosts() - [Medium] sched/deadline…

Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [Medium] sched/deadline: mismatched runtime <= 0 checks in dl_waiter_boosts()
- [Medium] sched/deadline: deadline inversion in dl_boost_donor_period() on resume
- [Medium] sched/deadline: lost throttle state on sleeping task in dl_deboost()
- [Medium] sched/deadline: stale past deadline kept when boosting unqueued owner
- [Medium] sched/deadline: missed deadline update on donor change in rt_mutex_setprio()
- [Medium] sched/deadline: dl_boost_start() skipped when !dl_prio(p->normal_prio)
--

commit c18a5e374403621910f4e9e5b28d6e3a7f84d1f4
Author: Mathieu Desnoyers <[email protected]>

sched/deadline: Boost an owner that cannot run on its own reservation

This commit updates deadline priority inheritance via dl_waiter_boosts(), dl_boost_start(), and dl_boost_donor_period() so that a deadline waiter boosts a lock owner that is throttled, out of runtime, or in its constrained gap, and runs it on a bounded donor period. It also reads the waiter's priority and donor once in dl_top_donor() to avoid boosting a non-deadline owner when the waiter's donor is the owner itself.

Quoted 4 lines
kernel/sched/core.c +0 -0
index f374b8a0a38b6..9fda819aa4efb 100644

[ ... ]

Quoted 30 lines
kernel/sched/core.c +17 -6
@@ -7984,15 +7990,36 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
 			 * with that deadline.
 			 */
 			if (!dl_prio(p->normal_prio) ||
-			    (pi_task && dl_prio(pi_task->prio) &&
+			    (donor &&
 			     !(p->dl.dl_new_params && p->dl.pi_se != &p->dl) &&
-			     dl_entity_preempt(&pi_task->dl, &p->dl))) {
+			     dl_waiter_boosts(p, pi_task))) {
 				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);
+				/*
+				 * donor is not NULL here. A task with no
+				 * deadline policy has a deadline prio only from
+				 * a deadline waiter, whose donor is not @p: see
+				 * __rt_effective_prio().
+				 */
+				if (!dl_set_donor(&p->dl, &donor)) {
+					/*
+					 * The donor of the top waiter is @p: @p waits
+					 * on a lock that it owns, or the waiter has a
+					 * stale donor. End the boost of a deadline
+					 * task, as a deboost does. A task with no
+					 * deadline policy does not get here:
+					 * __rt_effective_prio() did not boost it.
+					 */

kernel/sched/core.c void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task) · line 8022

Quoted 10 lines
kernel/sched/core.c +10 -0
+					if (dl_prio(p->normal_prio) && old_donor != &p->dl)
+						dl_deboost(&p->dl);
+				} else {
+					if (dl_prio(p->normal_prio) &&
+					    old_donor == &p->dl && p->dl.pi_se != &p->dl)
+						dl_boost_start(&p->dl);
+					scope->flags |= ENQUEUE_REPLENISH;
+					if (!scope->queued && p->dl.pi_se != old_donor)
+						dl_boost_blocked(p);
+				}

[Severity: Medium]
When a new top waiter with an earlier deadline replaces old_donor during a held boost (p->dl.dl_boost_held == 1 and old_donor != &p->dl), does rt_mutex_setprio() leave p->dl.deadline at the previous donor's later deadline?

Because old_donor != &p->dl, dl_boost_start() is skipped. For a deadline owner (p->dl.dl_deadline != 0) that still has positive donor runtime left and a future deadline from the old donor, replenish_dl_entity() and dl_boost_blocked() also leave p->dl.deadline and p->dl.runtime unchanged. Until the old donor's remaining runtime is exhausted, p continues to be scheduled (or ordered on an outer rt_mutex) at the old donor's later deadline rather than the new top waiter's deadline.

Quoted 4 lines
kernel/sched/deadline.c +0 -0
index ae0940838ca5a..7f839185eb381 100644

[ ... ]

Quoted 4 lines
kernel/sched/deadline.c +0 -0
@@ -285,6 +319,13 @@ void dl_deboost(struct sched_dl_entity *dl_se)
 	if (held && dl_se->runtime <= 0 && (!asleep || dl_se->dl_throttled)) {
 		struct rq *rq = task_rq(p);
 

kernel/sched/deadline.c void dl_deboost(struct sched_dl_entity *dl_se) · line 331

Quoted 10 lines
kernel/sched/deadline.c +7 -0
+		/*
+		 * The timer can still be armed. enqueue_task_dl() cancels it
+		 * when it boosts a runnable task, but a task that slept through
+		 * the boost kept it. sched_setattr() can have moved the task's
+		 * next period since. Cancel the timer before arming it again.
+		 */
+		cancel_replenish_timer(dl_se);
 		dl_se->dl_throttled = 1;
 		if (!start_dl_timer(dl_se)) {
 			dl_se->dl_throttled = 0;

[Severity: Medium]
Can dl_deboost() fail to re-throttle a sleeping task whose held boost ends before its own next period?

If the owner was throttled while asleep with dl_timer armed for its old period boundary, and sched_setattr() during the held boost moves own_period_start + dl_period further into the future, dl_setattr_period() updates own_* without canceling dl_timer:

kernel/sched/deadline.c:dl_setattr_period() {
    ...
	if (dl_se->dl_boost_held) {
		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);
		return true;
	}
    ...
}

When dl_timer fires at the old period boundary while the task is still asleep and boosted, dl_task_timer() skips the dl_next_period() check because is_dl_boosted(dl_se) is true and calls replenish_dl_entity(), which clears dl_se->dl_throttled = 0 (and can also grant a fresh donor period if the donor's deadline has passed). Similarly, if an owner with a negative runtime overrun (own_runtime < 0) was runnable when boosted, enqueue_task_dl() cleared dl_se->dl_throttled = 0 before the owner went to sleep on a non-rt_mutex waitqueue. When dl_deboost() later runs while the task is asleep before own_period_start + dl_period, dl_end_held_boost() restores own_runtime <= 0, but (!asleep || dl_se->dl_throttled) is false, so dl_deboost() neither re-throttles the task nor arms dl_timer, and a later wakeup can erase a negative overrun in update_dl_entity().

Quoted 41 lines
kernel/sched/deadline.c +37 -0
@@ -379,6 +420,130 @@ void dl_check_blocked_params(struct task_struct *p)
 		update_dl_entity(dl_se);
 	}
 }
+
+/*
+ * Return true if the deadline waiter @pi_task must boost the owner @p,
+ * which has a deadline policy of its own. Compare @pi_task with the
+ * deadline that @p would run on:
+ * - The boost of @p is held: keep it until the next period of @p
+ *   starts. After that, compare with now + dl_deadline.
+ * - @p is not boosted, and cannot run on its own reservation. It is
+ *   throttled, or past its deadline and before its next period. Boost it.
+ * - @p is not boosted, and past its deadline otherwise: its next enqueue
+ *   starts a new period. Compare with now + dl_deadline.
+ * - Else, compare with the deadline of @p.
+ * A special entity (a sugov kthread) always boosts @p.
+ *
+ * The caller holds the rq lock of @p and has updated the rq clock.
+ */
+bool dl_waiter_boosts(struct task_struct *p, struct task_struct *pi_task)
+{
+	struct sched_dl_entity *dl_se = &p->dl;
+	u64 now = rq_clock(task_rq(p));
+
+	if (dl_entity_is_special(&pi_task->dl))
+		return true;
+	if (dl_se->dl_boost_held) {
+		/*
+		 * A boost held at a wakeup refill can keep runtime of the
+		 * task's own, before its own deadline. The task can run on
+		 * it: compare with that deadline.
+		 */
+		if (dl_se->own_runtime > 0 &&
+		    dl_time_before(now, dl_se->own_deadline))
+			return dl_time_before(pi_task->dl.deadline,
+					      dl_se->own_deadline);
+		if (dl_time_before(now, dl_se->own_period_start + dl_se->dl_period))
+			return true;
+		return dl_time_before(pi_task->dl.deadline, now + dl_se->dl_deadline);
+	}

kernel/sched/deadline.c void dl_check_blocked_params(struct task_struct *p) · line 469

Quoted 10 lines
kernel/sched/deadline.c +10 -0
+	if (!is_dl_boosted(dl_se)) {
+		if (dl_se->dl_throttled || dl_in_constrained_gap(dl_se) ||
+		    (dl_se->dl_runtime && dl_se->runtime <= 0))
+			return true;
+		if (dl_time_before(dl_se->deadline, now))
+			return dl_time_before(pi_task->dl.deadline,
+					      now + dl_se->dl_deadline);
+	}
+	return dl_entity_preempt(&pi_task->dl, dl_se);
+}

[Severity: Medium]
Can an unboosted owner whose deadline has passed outside its constrained gap be left with its stale past deadline when boosted while not queued?

When p is blocked on another rt_mutex (or sleeping) after period_start + dl_period has passed, dl_waiter_boosts() boosts p if pi_task->dl.deadline < now + dl_se->dl_deadline (or unconditionally if dl_se->runtime <= 0). Because scope->queued is false in rt_mutex_setprio(), enqueue_task_dl() does not run, dl_boost_start() returns early because p is neither throttled nor in its constrained gap, and dl_boost_blocked() returns early because dl_se->dl_deadline is non-zero. That leaves p boosted (p->dl.pi_se != &p->dl) with p->dl.dl_boost_held == 0 and p->dl.deadline still in the past, so waiter_update_prio() propagates p's stale past deadline into the outer lock's waiter tree and subsequent dl_waiter_boosts() calls on p compare against that past deadline.

Quoted 40 lines
kernel/sched/deadline.c +40 -0
+
+static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t);
+
+/*
+ * Run @dl_se, whose boost is held, on a period of its donor. Resume the
+ * donor period that the last held boost ran on, unless a whole period of
+ * the current donor has passed since it started. A new period at each
+ * boost would give the task one donor runtime per boost. Nothing charges
+ * the donor for that runtime.
+ *
+ * If the deadline of the resumed period has passed, move to its next
+ * period here, with a full donor runtime. A later enqueue would
+ * otherwise start a period from now, which gives a second donor runtime
+ * too early. As in a refill by replenish_dl_entity(), the task can use
+ * that runtime at once, although its period starts in the future.
+ *
+ * Cap the runtime to the donor's density until the deadline, as
+ * update_dl_revised_wakeup() does at the wakeup of a constrained task.
+ */
+static void dl_boost_donor_period(struct sched_dl_entity *dl_se)
+{
+	struct sched_dl_entity *pi_se = pi_of(dl_se);
+	struct rq *rq = rq_of_dl_se(dl_se);
+	u64 runtime = READ_ONCE(pi_se->dl_runtime);
+	u64 period = READ_ONCE(pi_se->dl_period);
+	u64 now = rq_clock(rq);
+
+	/*
+	 * The donor can differ from the one of the last held boost. Keep the
+	 * start of the donor period and the runtime left, but take the
+	 * deadline and the cap of the runtime from this donor.
+	 */
+	if (dl_se->dl_boost_granted && runtime && period &&
+	    dl_time_before(now, dl_se->boost_period_start + period)) {
+		dl_se->period_start = dl_se->boost_period_start;
+		dl_se->deadline = dl_se->period_start +
+				  READ_ONCE(pi_se->dl_deadline);
+		dl_se->runtime = min_t(s64, dl_se->boost_runtime, runtime);
+		if (!dl_time_before(now, dl_se->deadline)) {
+			dl_se->period_start += period;

kernel/sched/deadline.c void dl_check_blocked_params(struct task_struct *p) · line 519

Quoted 10 lines
kernel/sched/deadline.c +10 -0
+			dl_se->deadline += period;
+			dl_se->runtime = runtime;
+		}
+		if (dl_entity_overflow(dl_se, now))
+			dl_se->runtime = (READ_ONCE(pi_se->dl_density) *
+					  (dl_se->deadline - now)) >> BW_SHIFT;
+		return;
+	}
+	replenish_dl_new_period(dl_se, rq);
+}

[Severity: Medium]
Can dl_boost_donor_period() place dl_se->deadline far past the new donor's deadline when resuming a period across different donors?

During a held boost under a donor with a long period P1 (for example, 1000ms), if the owner exhausts that donor's runtime, replenish_dl_entity() advances dl_se->period_start by P1 into the future (t0 + 1000ms), and dl_end_held_boost() saves that future timestamp in dl_se->boost_period_start. If a second donor with a short period and deadline (for example, 10ms) boosts the owner at now = t0 + 50ms, dl_time_before(now, dl_se->boost_period_start + period) is true, so dl_boost_donor_period() sets dl_se->deadline = t0 + 1010ms, scheduling the owner 950ms after the urgent waiter's deadline. The converse happens if a short-period donor saves boost_period_start = 0 and a constrained donor with dl_deadline = 10ms, dl_period = 100ms boosts at now = 20ms:
dl_se->deadline = 0 + 10ms is in the past, so adding period pushes dl_se->deadline to 110ms (80ms past the new donor's deadline). Also, dl_se->boost_deadline is written in dl_end_held_boost() but never read.

Quoted 17 lines
kernel/sched/deadline.c +17 -0
+
+/*
+ * Call this when @dl_se gets its first donor, after dl_set_donor(). If
+ * @dl_se cannot run on its own reservation, hold the boost from now. Save
+ * its deadline, runtime and period start in own_*, and set dl_boost_held.
+ * Then run it on a period of the donor. A refill would instead add donor
+ * periods to its own deadline. That is often later than the waiter's.
+ *
+ * A task with no parameters of its own has nothing to save. The caller
+ * holds the rq lock and has updated the rq clock.
+ */
+void dl_boost_start(struct sched_dl_entity *dl_se)
+{
+	if (!dl_se->dl_runtime ||
+	    (!dl_se->dl_throttled && !dl_in_constrained_gap(dl_se)))
+		return;
+	/*

kernel/sched/deadline.c void dl_check_blocked_params(struct task_struct *p) · line 546

Quoted 10 lines
kernel/sched/deadline.c +10 -0
+	 * A yield gave up the rest of the task's own period: save no runtime
+	 * for it. The yield does not apply to the donor period placed below.
+	 */
+	dl_se->own_deadline = dl_se->deadline;
+	dl_se->own_runtime = dl_se->dl_yielded ? 0 : dl_se->runtime;
+	dl_se->own_period_start = dl_se->period_start;
+	dl_se->dl_boost_held = 1;
+	dl_se->dl_yielded = 0;
+	dl_boost_donor_period(dl_se);
+}

[Severity: Medium]
Should dl_waiter_boosts() and dl_boost_start() use matching conditions for dl_se->runtime <= 0?

In dl_waiter_boosts(), (dl_se->dl_runtime && dl_se->runtime <= 0) is checked before testing whether dl_se->deadline is 0 or already in the past. When a sleeping owner's held boost ends after its own next period has started and the top waiter has a deadline later than now + dl_se->dl_deadline, the first dl_waiter_boosts() call in rt_mutex_setprio() returns false and calls dl_deboost(), where dl_end_held_boost() sets dl_se->runtime = 0 and dl_se->dl_throttled = 0. The second dl_waiter_boosts() call in rt_mutex_setprio() after dl_deboost() then sees !is_dl_boosted(dl_se) and dl_se->runtime <= 0, returns true, and immediately re-boosts the sleeping owner with the later-deadline waiter it just deboosted from.

Conversely, when a sleeping owner before its own deadline has dl_se->runtime <= 0 and dl_se->dl_throttled == 0 (such as after dl_setattr_place() -> dl_setattr_period() or after a deboost), dl_waiter_boosts() boosts it via dl_se->runtime <= 0, but dl_boost_start() does not check dl_se->runtime <= 0 and returns early without setting dl_boost_held or calling dl_boost_donor_period(). If that owner wakes up while boosted before dl_se->deadline, update_dl_entity() does not replenish it and enqueue_dl_entity() skips throttling because is_dl_boosted(dl_se) is true, enqueuing it with runtime == 0 and dl_boost_held == 0.

Quoted 18 lines
kernel/sched/syscalls.c +1 -2
index 4f26601f0530a..b71df4c2c13ae 100644
@@ -303,6 +303,7 @@ static bool check_same_owner(struct task_struct *p)
 #ifdef CONFIG_RT_MUTEXES
 static inline void __setscheduler_dl_pi(int newprio, int policy,
 			      struct task_struct *p,
+			      struct sched_dl_entity **donor,
 			      struct sched_change_ctx *scope)
 {
 	/*
@@ -312,14 +313,10 @@ static inline void __setscheduler_dl_pi(int newprio, int policy,
 	 * sure replenishment happens with the next enqueue.
 	 */
 
-	if (dl_prio(newprio) && !dl_policy(policy)) {
-		struct task_struct *pi_task = rt_mutex_get_top_task(p);

kernel/sched/syscalls.c static inline void __setscheduler_dl_pi(int newprio, int policy, · line 319

Quoted 10 lines
kernel/sched/syscalls.c +4 -6
-
-		if (pi_task) {
-			dl_set_donor(&p->dl, pi_task);
-			scope->flags |= ENQUEUE_REPLENISH;
-		}
-	}
+	/* *@donor chose newprio: see rt_effective_prio(). */
+	if (dl_prio(newprio) && !dl_policy(policy) && *donor &&
+	    dl_set_donor(&p->dl, donor))
+		scope->flags |= ENQUEUE_REPLENISH;

[Severity: Medium]
Does skipping dl_boost_start(&p->dl) here and when !dl_prio(p->normal_prio) in rt_mutex_setprio() miss holding the boost for a task that left SCHED_DEADLINE while throttled or in its constrained gap?

A task that switches from SCHED_DEADLINE to a non-deadline policy before its 0-lag time or next period retains p->dl.dl_runtime != 0 along with its throttled or constrained-gap state while !dl_prio(p->normal_prio). If it holds an rt_mutex with a deadline waiter during __sched_setscheduler() (or is boosted afterwards in rt_mutex_setprio()), dl_boost_start(&p->dl) is not called. If the task is queued with a negative runtime overrun, replenish_dl_entity() falls back to dl_hold_refilled_boost(), which clamps own_runtime to 0 instead of preserving the negative overrun as dl_boost_start() does; if the task is not queued, neither dl_boost_blocked() nor switched_to_dl() gives it a donor period, leaving it boosted with dl_boost_held == 0 and its own stale deadline and runtime.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=38
#50 lore raw

PATCH v12 39/43 sched/deadline: Boost an owner throttled after its waiter blocked

In reply to Mathieu Desnoyers

"sched/deadline: Boost an owner that cannot run on its own reservation" boosts a throttled owner when a waiter blocks, leaves or changes. But the owner can be throttled after its waiter blocked:
- its runtime runs out;
- an enqueue throttles it in its constrained gap;
- sched_setattr() throttles it until it repays its debt. Nothing calls rt_mutex_setprio() then. The waiter waits for the owner's next period, and can miss its own deadline.

Fix this by asking for a boost at these throttles, with the new dl_request_owner_boost(). rt_mutex_setprio() needs the owner's pi_lock, which nests outside the rq lock held there. So dl_request_owner_boost() queues a hard irq_work. The work calls the new rt_mutex_adjust_owner(), which boosts the owner from its top waiter under the pi_lock, if dl_waiter_boosts() says so. A call to rt_mutex_setprio() that boosts nothing would still dequeue and enqueue the owner.

The rtmutex code also asks, without the rq lock:
- In try_to_take_rt_mutex(): the tick can throttle a task in the slow path of a lock, before it has waiters. It can still take the lock, and get waiters.
- Where a task stops waiting on a lock without taking it. rt_mutex_adjust_owner() skips an owner that waits on another lock, since a new donor would need a chain walk.
A throttle after a yield does not ask: the owner gave up its runtime.

One case stays open. An owner throttled while it waits on another lock cannot run to take that lock. Its own waiters wait for its next period.

On a CPU going offline, the __balance_callbacks() at the end of rt_mutex_setprio() runs balance_push(). Its per-CPU work buffer expects one call per switch-in. The irq_work can interrupt the tail of a switch-in, and would queue that buffer twice. balance_push() now returns in hard interrupt context. The next __schedule() does the push.

dl_owner_boost_work() can drop the last reference on the task that holds the irq_work, before irq_work_single() is done with it. This is safe because put_task_struct() frees the task through call_rcu(), since commit 8671bad873eb ("sched: Do not call __put_task_struct() on rt if pi_blocked_on is set"), in v6.17. A backport needs that commit. RT_MUTEXES now selects IRQ_WORK, which is only built by default on SMP.

"selftests/sched: Test parameter changes in deadline_bw_test", later in this series, lists the cases that fail without this patch.

Fixes 2d3d891d3344 ("sched/deadline: Add SCHED_DEADLINE inheritance logic")
Cc [email protected] # 6.16.x and older: 8671bad873eb: sched: Do not call __put_task_struct() on rt if pi_blocked_on is set
---
 include/linux/sched.h        |  6 +++
 include/linux/sched/rt.h     |  4 ++
 init/Kconfig                 |  1 +
 kernel/locking/rtmutex.c     | 27 +++++++++++
 kernel/locking/rtmutex_api.c | 27 +++++++++++
 kernel/sched/core.c          |  9 ++++
 kernel/sched/deadline.c      | 94 ++++++++++++++++++++++++++++++++++++
 7 files changed, 168 insertions(+)
include/linux/sched.h +6 -0
index cafa26c874b0..3e14987e0177 100644
@@ -26,6 +26,7 @@
 #include <linux/mutex_types.h>
 #include <linux/plist_types.h>
 #include <linux/hrtimer_types.h>
+#include <linux/irq_work_types.h>
 #include <linux/timer_types.h>
 #include <linux/seccomp_types.h>
 #include <linux/nodemask_types.h>
@@ -851,6 +852,11 @@ struct sched_dl_entity {
 	 * to (the original one/itself).
 	 */
 	struct sched_dl_entity *pi_se;
+	/*
+	 * Queued when the task, an rt_mutex owner, can no longer run on its
+	 * own reservation: see dl_request_owner_boost().
+	 */
+	struct irq_work			boost_work;
 #endif
 	bool				dl_bw_def;
 };
include/linux/sched/rt.h +4 -0
index 922935cc3383..7be8f196a2fb 100644
@@ -66,6 +66,10 @@ static inline struct task_struct *rt_mutex_get_top_task(struct task_struct *p)
 }
 extern void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task);
 extern void rt_mutex_adjust_pi(struct task_struct *p);
+extern void rt_mutex_adjust_owner(struct task_struct *p);
+extern void dl_request_owner_boost(struct task_struct *p);
+extern bool dl_owner_needs_boost(struct task_struct *p,
+				 struct task_struct *pi_task);
 #else
 static inline struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
 {
init/Kconfig +1 -0
index 4013781e0a02..f0a700046133 100644
@@ -2115,6 +2115,7 @@ source "arch/Kconfig"
 config RT_MUTEXES
 	bool
 	default y if PREEMPT_RT
+	select IRQ_WORK
 
 config MODULE_SIG_FORMAT
 	def_bool n
kernel/locking/rtmutex.c +27 -0
index 49caf38759f3..1deed8937e41 100644
@@ -1086,6 +1086,20 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
 	return ret;
 }
 
+/*
+ * Ask for a boost of @task, a deadline task with a deadline top waiter.
+ * @task can be throttled already. Its throttle is only read under the
+ * rq lock, so leave the check to the irq_work of
+ * dl_request_owner_boost(). The caller holds the pi_lock of @task.
+ */
+static __always_inline void rt_mutex_ask_dl_boost(struct task_struct *task)
+{
+	lockdep_assert_held(&task->pi_lock);
+	if (dl_prio(task->normal_prio) && task_has_pi_waiters(task) &&
+	    dl_prio(task_top_pi_waiter(task)->pi_tree.prio))
+		dl_request_owner_boost(task);
+}
+
 /*
  * Try to take an rt-mutex
  *
@@ -1195,6 +1209,13 @@ try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
 	 */
 	if (rt_mutex_has_waiters(lock))
 		rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
+	/*
+	 * @task took the lock ahead of the remaining waiters, so none of
+	 * them has a higher priority. A deadline task can still need a
+	 * boost: it can be throttled already. Its throttle found no waiter,
+	 * or it waited on this lock then. Ask for the boost again.
+	 */
+	rt_mutex_ask_dl_boost(task);
 	raw_spin_unlock(&task->pi_lock);
 
 takeit:
@@ -1278,6 +1299,7 @@ static int __sched task_blocks_on_rt_mutex(struct rt_mutex_base *lock,
 			raw_spin_lock(&task->pi_lock);
 			rt_mutex_dequeue(lock, waiter);
 			task->pi_blocked_on = NULL;
+			rt_mutex_ask_dl_boost(task);
 			raw_spin_unlock(&task->pi_lock);
 			return res;
 		}
@@ -1582,6 +1604,11 @@ static void __sched remove_waiter(struct rt_mutex_base *lock,
 	scoped_guard(raw_spinlock, &waiter_task->pi_lock) {
 		rt_mutex_dequeue(lock, waiter);
 		waiter_task->pi_blocked_on = NULL;
+		/*
+		 * The task stops waiting. If it owns a lock with waiters and
+		 * was throttled meanwhile, no boost could reach it then.
+		 */
+		rt_mutex_ask_dl_boost(waiter_task);
 	}
 
 	/*
kernel/locking/rtmutex_api.c +27 -0
index f1246efc62d3..384a6e951515 100644
@@ -536,6 +536,33 @@ void __sched rt_mutex_adjust_pi(struct task_struct *task)
 				   next_lock, NULL, task);
 }
 
+/*
+ * Boost @task from its top waiter, as rt_mutex_adjust_prio() does when a
+ * waiter changes. dl_request_owner_boost() queues this call when @task, a
+ * deadline owner, can no longer run on its own reservation. The pi_lock
+ * of @task keeps its pi_waiters, and so its top waiter, in place.
+ *
+ * Leave @task alone if it waits on another lock, with pi_blocked_on set.
+ * A new donor would then need a chain walk from @task, and this runs in
+ * hard interrupt context. The rtmutex code asks again when @task takes
+ * that lock or stops waiting on it. If @task is throttled before then,
+ * it cannot run to take the lock, and its waiters wait for its next
+ * period.
+ */
+void rt_mutex_adjust_owner(struct task_struct *task)
+{
+	unsigned long flags;
+
+	raw_spin_lock_irqsave(&task->pi_lock, flags);
+	if (!task->pi_blocked_on && task_has_pi_waiters(task)) {
+		struct task_struct *pi_task = task_top_pi_waiter(task)->task;
+
+		if (dl_owner_needs_boost(task, pi_task))
+			rt_mutex_setprio(task, pi_task);
+	}
+	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
+}
+
 /*
  * Performs the wakeup of the top-waiter and re-enables preemption.
  */
kernel/sched/core.c +9 -0
index 9fda819aa4ef..e993054b5b76 100644
@@ -8650,6 +8650,15 @@ static void balance_push(struct rq *rq)
 	if (!cpu_dying(rq->cpu) || rq != this_rq())
 		return;
 
+	/*
+	 * push_work is per CPU: run once per switch-in, from __schedule().
+	 * A hard interrupt can call rt_mutex_setprio() in the tail of that
+	 * switch-in: see dl_request_owner_boost(). Leave the push to the
+	 * next __schedule(), which finds the callback installed above.
+	 */
+	if (in_hardirq())
+		return;
+
 	/*
 	 * Both the cpu-hotplug and stop task are in this case and are
 	 * required to complete the hotplug process.
kernel/sched/deadline.c +94 -0

136 lines not on this page · raw message

Signature · 1 line
2.47.3
#51 lore raw

Re: [PATCH v12 39/43] sched/deadline: Boost an owner throttled after its waiter blocked

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] locking/rtmutex: stale pi_tree.prio in rt_mutex_ask_dl_boost() on handover - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] locking/rtmutex: stale pi_tree.prio in rt_mutex_ask_dl_boost() on handover
- [Medium] sched/deadline: dl_boost_start() ignores runtime <= 0 boost condition
--

commit eaf42f89f7029cb15ae44695c0cca1de7ab04a1a
Author: Mathieu Desnoyers <[email protected]>

sched/deadline: Boost an owner throttled after its waiter blocked

This commit queues a hard irq_work when a SCHED_DEADLINE rt_mutex owner is throttled or finishes a blocked wait, so that rt_mutex_adjust_owner() can boost the owner from its top waiter outside the rq lock.

Quoted 29 lines
kernel/locking/rtmutex.c +14 -0
index 49caf38759f34..1deed8937e416 100644
@@ -1086,6 +1086,20 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
 	return ret;
 }
 
+/*
+ * Ask for a boost of @task, a deadline task with a deadline top waiter.
+ * @task can be throttled already. Its throttle is only read under the
+ * rq lock, so leave the check to the irq_work of
+ * dl_request_owner_boost(). The caller holds the pi_lock of @task.
+ */
+static __always_inline void rt_mutex_ask_dl_boost(struct task_struct *task)
+{
+	lockdep_assert_held(&task->pi_lock);
+	if (dl_prio(task->normal_prio) && task_has_pi_waiters(task) &&
+	    dl_prio(task_top_pi_waiter(task)->pi_tree.prio))
+		dl_request_owner_boost(task);
+}
+
 /*
  * Try to take an rt-mutex
  *
@@ -1195,6 +1209,13 @@ try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
 	 */
 	if (rt_mutex_has_waiters(lock))
 		rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));

kernel/locking/rtmutex.c try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task, · line 1221

Quoted 10 lines
kernel/locking/rtmutex.c +7 -0
+	/*
+	 * @task took the lock ahead of the remaining waiters, so none of
+	 * them has a higher priority. A deadline task can still need a
+	 * boost: it can be throttled already. Its throttle found no waiter,
+	 * or it waited on this lock then. Ask for the boost again.
+	 */
+	rt_mutex_ask_dl_boost(task);
 	raw_spin_unlock(&task->pi_lock);
 
 takeit:

[Severity: Medium]
Can rt_mutex_ask_dl_boost() observe a stale pi_tree.prio when a blocked waiter was promoted to SCHED_DEADLINE before becoming the lock's top waiter?

In rt_mutex_adjust_prio_chain(), waiter_update_prio() updates waiter->tree.{prio,deadline}, whereas waiter_clone_prio() only copies those keys to waiter->pi_tree.{prio,deadline} when the lock has an owner and the waiter is the old or new top waiter. If a second waiter becomes a deadline task while behind another waiter (or while the lock has no owner during wakeup handover), and the first waiter then takes the lock in try_to_take_rt_mutex() or aborts in remove_waiter(), rt_mutex_enqueue_pi() inserts the new top waiter into pi_waiters without calling waiter_clone_prio(). rt_mutex_ask_dl_boost() then reads the stale non-deadline pi_tree.prio and skips dl_request_owner_boost(), leaving a throttled deadline owner unboosted until its next period.

Quoted 9 lines
kernel/sched/deadline.c +1 -0
index 7f839185eb381..6bb92dfecbb89 100644
@@ -544,6 +544,82 @@ void dl_boost_start(struct sched_dl_entity *dl_se)
 	dl_se->dl_yielded = 0;
 	dl_boost_donor_period(dl_se);
 }
+

[ ... ]

Quoted 16 lines
kernel/sched/deadline.c +16 -0
+bool dl_owner_needs_boost(struct task_struct *p, struct task_struct *pi_task)
+{
+	struct rq_flags rf;
+	struct rq *rq;
+	bool ret = false;
+
+	if (!dl_prio(p->normal_prio) || !dl_prio(pi_task->prio))
+		return false;
+	rq = __task_rq_lock(p, &rf);
+	if (!is_dl_boosted(&p->dl)) {
+		update_rq_clock(rq);
+		ret = dl_waiter_boosts(p, pi_task);
+	}
+	__task_rq_unlock(rq, p, &rf);
+	return ret;
+}

[ ... ]

kernel/sched/deadline.c enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags) · line 3705

Quoted 7 lines
kernel/sched/deadline.c +2 -0
@@ -3611,6 +3700,8 @@ enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags)
 		dl_se->dl_throttled = 1;
 		if (task_current_donor(rq, dl_task_of(dl_se)))
 			resched_curr(rq);
+		/* A deadline waiter on an rt_mutex may boost it now. */
+		dl_request_owner_boost(dl_task_of(dl_se));
 	}

[Severity: Medium]
Should dl_boost_start() also hold the boost when dl_se->runtime <= 0?

dl_waiter_boosts() boosts an unboosted owner when (dl_se->dl_runtime && dl_se->runtime <= 0), even if !dl_se->dl_throttled and !dl_in_constrained_gap(dl_se):

kernel/sched/deadline.c:dl_waiter_boosts() {
    ...
	if (!is_dl_boosted(dl_se)) {
		if (dl_se->dl_throttled || dl_in_constrained_gap(dl_se) ||
		    (dl_se->dl_runtime && dl_se->runtime <= 0))
			return true;
    ...
}

However, dl_boost_start() returns early without holding the boost or switching to a donor period when !dl_se->dl_throttled && !dl_in_constrained_gap(dl_se):

kernel/sched/deadline.c:dl_boost_start() {
	if (!dl_se->dl_runtime ||
	    (!dl_se->dl_throttled && !dl_in_constrained_gap(dl_se)))
		return;
    ...
}

If an asleep owner has runtime == 0 and dl_throttled == 0 before its deadline (for instance, after sched_setattr() leaves an asleep constrained-deadline owner with runtime == 0 in dl_setattr_place()), rt_mutex_setprio() sets p->dl.pi_se to the donor while dl_boost_start() does nothing. When the owner later wakes up before its deadline, update_dl_entity() does not replenish it (dl_entity_overflow() returns false for runtime == 0) and enqueue_dl_entity() skips the runtime <= 0 throttle check because is_dl_boosted(dl_se) is true, enqueuing the owner with zero runtime and its own deadline instead of the donor's parameters.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=39
#52 lore raw

PATCH v12 40/43 sched/deadline: Reschedule a running task throttled at its enqueue

In reply to Mathieu Desnoyers

A task with a constrained deadline (dl_deadline < dl_period) can wake up after its deadline but before its next period.
Commit df8eac8cafce ("sched/deadline: Throttle a constrained deadline task activated after the deadline") throttles it then until that period. A refill would let it run for dl_runtime every dl_deadline, more than its reservation.

enqueue_dl_entity() makes this check at every enqueue, not only at a wakeup. These changes to a running task also dequeue and enqueue it:
- sched_setattr() and sched_setaffinity();
- a cgroup move;
- a deboost in rt_mutex_setprio().

A task can run past its deadline with runtime left, for example after tasks with earlier deadlines delayed it. The check then throttles it and leaves it off the dl_rq. But nothing reschedules: prio_changed_dl() sees the same deadline and returns.

The task keeps the CPU. update_curr_dl() skips a task that is off the dl_rq, so the tick neither charges it nor preempts it. It runs until it blocks, until a task with an earlier deadline wakes up, or until its next period. That is up to dl_period - dl_deadline of CPU time outside its reservation. The Sashiko AI review of this series found this.

Fix this by rescheduling when the check throttles the current task, as a throttle in update_curr_dl_se() does.

The task stopped this way can own an rt_mutex. Its deadline waiters then wait for its next period, unless they boost it. So this patch comes after "sched/deadline: Boost an owner that cannot run on its own reservation" and "sched/deadline: Boost an owner throttled after its waiter blocked".

The constrained_gap_change_running case of deadline_bw_test changes a running task with sched_setattr(). "sched/deadline: Keep the debt of a task across a change of parameters", earlier in this series, already reschedules a task that sched_setattr() throttles. So the case passes without this patch too. sched_setaffinity(), a cgroup move and a deboost have no test case. Their effect was found by reading.

Fixes df8eac8cafce ("sched/deadline: Throttle a constrained deadline task activated after the deadline")
---
 kernel/sched/deadline.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/sched/deadline.c +7 -0
index 6bb92dfecbb8..b9dd7ec10e7d 100644
@@ -2546,6 +2546,13 @@ static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
 		/* A deadline waiter on an rt_mutex may boost it now. */
 		if (!dl_server(dl_se))
 			dl_request_owner_boost(dl_task_of(dl_se));
+		/*
+		 * A running task is now off the dl_rq: the tick no longer
+		 * charges it or preempts it. Reschedule, as a throttle in
+		 * update_curr_dl_se() does.
+		 */
+		if (!dl_server(dl_se) && task_current_donor(rq, dl_task_of(dl_se)))
+			resched_curr(rq);
 	}
 }
 
Signature · 1 line
2.47.3
#53 lore raw

PATCH v12 41/43 sched/deadline: Run a boost held at its first refill on a donor period

In reply to Mathieu Desnoyers

"sched/deadline: Restore the owner's period at a deboost in the class", earlier in this series, holds a boost at its first refill from the donor. It saves the owner's own period, and puts it back at the deboost. But the refill itself is the one of replenish_dl_entity(): it adds the donor's runtime, and moves the owner's own deadline by the donor's period. That deadline is often later than the waiter's. The donor's runtime also pays for the owner's overrun.

"sched/deadline: Boost an owner that cannot run on its own reservation" runs a boost held from its start on a donor period
instead: a period with the donor's parameters, which starts at the boost. It also limits the owner to one donor runtime per donor period:
the next held boost resumes the donor period of the last one. A boost held at its first refill gets neither. A waiter that times out on the lock again and again would then give the owner one donor runtime per boost. Other deadline tasks would then miss their deadlines.

Fix this by running a boost held at its first refill on a donor period too. dl_hold_refilled_boost() calls dl_boost_donor_period(), which resumes the donor period of the last held boost, or starts a new one. dl_note_donor_refill() returns true when a donor period was placed, and replenish_dl_new_period() then keeps it. None is placed when the donor's parameters were cleared: replenish_dl_entity() adds no runtime then.

This was found in a user-space model of the CBS, not reproduced on a kernel. No case of deadline_bw_test fails without this patch.

Fixes 2d3d891d3344 ("sched/deadline: Add SCHED_DEADLINE inheritance logic")
---
 kernel/sched/deadline.c | 74 ++++++++++++++++++++++++++---------------
 1 file changed, 48 insertions(+), 26 deletions(-)
kernel/sched/deadline.c +48 -26
index b9dd7ec10e7d..f4e07aa47715 100644
@@ -196,28 +196,32 @@ static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
 	return pi_of(dl_se) != dl_se;
 }
 
-static void dl_hold_refilled_boost(struct sched_dl_entity *dl_se);
+static bool dl_hold_refilled_boost(struct sched_dl_entity *dl_se);
 
 /*
  * Record that a replenishment changes the runtime of @dl_se while
- * boosted. At the first refill, hold the boost: dl_hold_refilled_boost()
- * saves the task's own period. Call this before the replenishment moves
- * the deadline or adds runtime. A task with no parameters of its own at the
- * first refill, or no period yet, has no period to save. Later refills of
- * its boost leave it
- * as it is, even if the task gets parameters meanwhile.
+ * boosted. At the first refill of a boost that is not held, hold it. Call
+ * this before the replenishment changes the deadline and the runtime:
+ * dl_hold_refilled_boost() saves them, and places a period of the donor.
+ * Return true if it placed one: the caller must keep that period. It
+ * places none if the donor's parameters were cleared. A task with no
+ * parameters of its own at the first refill, or no period yet, has no
+ * period to save. Later
+ * refills of its boost leave it as it is, even if the task gets
+ * parameters meanwhile.
  */
-static inline void dl_note_donor_refill(struct sched_dl_entity *dl_se)
+static inline bool dl_note_donor_refill(struct sched_dl_entity *dl_se)
 {
 	bool first;
 
 	if (!is_dl_boosted(dl_se))
-		return;
+		return false;
 	first = !dl_se->dl_donor_refill;
 	dl_se->dl_donor_refill = 1;
-	if (first && dl_se->dl_runtime && dl_se->deadline &&
-	    !dl_se->dl_boost_held)
-		dl_hold_refilled_boost(dl_se);
+	if (!first || !dl_se->dl_runtime || !dl_se->deadline ||
+	    dl_se->dl_boost_held)
+		return false;
+	return dl_hold_refilled_boost(dl_se);
 }
 
 static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task);
@@ -626,8 +630,9 @@ static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
 	return dl_se;
 }
 
-static inline void dl_note_donor_refill(struct sched_dl_entity *dl_se)
+static inline bool dl_note_donor_refill(struct sched_dl_entity *dl_se)
 {
+	return false;
 }
 
 static inline void dl_request_owner_boost(struct task_struct *p)
@@ -1741,11 +1746,13 @@ static void wakeup_preempt_dl(struct rq *rq, struct task_struct *p, int flags);
 static inline void replenish_dl_new_period(struct sched_dl_entity *dl_se,
 					    struct rq *rq)
 {
-	dl_note_donor_refill(dl_se);
-	/* for non-boosted task, pi_of(dl_se) == dl_se */
-	dl_se->period_start = rq_clock(rq);
-	dl_se->deadline = dl_se->period_start + pi_of(dl_se)->dl_deadline;
-	dl_se->runtime = pi_of(dl_se)->dl_runtime;
+	/* A boost held from this refill keeps the period it placed. */
+	if (!dl_note_donor_refill(dl_se)) {
+		/* for non-boosted task, pi_of(dl_se) == dl_se */
+		dl_se->period_start = rq_clock(rq);
+		dl_se->deadline = dl_se->period_start + pi_of(dl_se)->dl_deadline;
+		dl_se->runtime = pi_of(dl_se)->dl_runtime;
+	}
 
 	/*
 	 * If it is a deferred reservation, and the server
@@ -6101,11 +6108,14 @@ static bool dl_setattr_period(struct sched_dl_entity *dl_se, u64 old_runtime,
 
 #ifdef CONFIG_RT_MUTEXES
 /*
- * A boosted task is refilled from its donor when its runtime is used up,
- * or when its deadline has passed. A wakeup also refills it when the
- * donor's density does not allow the runtime left. From then on, the task
- * runs on the donor's reservation. So save the task's own period in own_*
- * at the first refill: this holds the boost. dl_deboost() puts it back.
+ * A boost that is not held runs the task on its own reservation. The task
+ * is refilled from its donor when its runtime is used up, or when its
+ * deadline has passed. A wakeup also refills it when the donor's density
+ * does not allow the runtime left. From then on, the task runs on the
+ * donor's reservation. So hold the boost at the first refill. Save the
+ * task's own period in own_*, and run the task on a period of the donor,
+ * resumed or new: see dl_boost_donor_period(). dl_boost_start() does the
+ * same at the start of a boost.
  *
  * Keep the runtime left if the task's own deadline is still ahead.
  * Otherwise save no runtime. A negative runtime is an overrun that comes
@@ -6115,11 +6125,15 @@ static bool dl_setattr_period(struct sched_dl_entity *dl_se, u64 old_runtime,
  * If sched_setattr() changed the parameters before this refill,
  * dl_setattr_period() saved the old ones in setattr_*. Fit the period
  * saved in own_* to the new parameters with them. setattr_* share
- * storage with own_*: read them before writing own_*. The caller holds
- * the rq lock of the task, and has updated the rq clock.
+ * storage with own_*: read them before writing own_*. Return true if a
+ * period of the donor was placed. The caller holds the rq lock of the
+ * task, and has updated the rq clock.
  */
-static void dl_hold_refilled_boost(struct sched_dl_entity *dl_se)
+static bool dl_hold_refilled_boost(struct sched_dl_entity *dl_se)
 {
+	struct sched_dl_entity *pi_se = pi_of(dl_se);
+	u64 donor_runtime = READ_ONCE(pi_se->dl_runtime);
+	u64 donor_deadline = READ_ONCE(pi_se->dl_deadline);
 	u64 old_runtime = dl_se->setattr_runtime;
 	u64 old_deadline = dl_se->setattr_deadline;
 	u64 old_period = dl_se->setattr_period;
@@ -6142,6 +6156,14 @@ static void dl_hold_refilled_boost(struct sched_dl_entity *dl_se)
 				 old_runtime, old_deadline, old_period, false, false);
 	else if (!dl_time_before(now, dl_se->own_deadline))
 		dl_se->own_runtime = 0;
+	/*
+	 * replenish_dl_entity() checks the donor's parameters: it adds no
+	 * runtime if they were cleared. Start no period from them then.
+	 */
+	if (!donor_runtime || !donor_deadline)
+		return false;
+	dl_boost_donor_period(dl_se);
+	return true;
 }
 #endif /* CONFIG_RT_MUTEXES */
 
Signature · 1 line
2.47.3
#54 lore raw

Re: [PATCH v12 41/43] sched/deadline: Run a boost held at its first refill on a donor period

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] sched/deadline: corrupted donor period state when donor params are cleared - [Low] sched/deadline…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] sched/deadline: corrupted donor period state when donor params are cleared
- [Low] sched/deadline: duplicate replenishment tracepoint on first held-boost refill
--

commit 0aa5ad7879d538e8d7671934363e9bde5d35376e
Author: Mathieu Desnoyers <[email protected]>

sched/deadline: Run a boost held at its first refill on a donor period

This commit runs a boost held at its first refill on a donor period by calling dl_boost_donor_period() from dl_hold_refilled_boost() and keeping the placed period in replenish_dl_new_period().

Quoted 4 lines
kernel/sched/deadline.c +0 -0
index b9dd7ec10e7d2..f4e07aa47715d 100644

[ ... ]

Quoted 9 lines
kernel/sched/deadline.c +0 -5
@@ -1741,11 +1746,13 @@ static void wakeup_preempt_dl(struct rq *rq, struct task_struct *p, int flags);
 static inline void replenish_dl_new_period(struct sched_dl_entity *dl_se,
 					    struct rq *rq)
 {
-	dl_note_donor_refill(dl_se);
-	/* for non-boosted task, pi_of(dl_se) == dl_se */
-	dl_se->period_start = rq_clock(rq);
-	dl_se->deadline = dl_se->period_start + pi_of(dl_se)->dl_deadline;
-	dl_se->runtime = pi_of(dl_se)->dl_runtime;

kernel/sched/deadline.c static void wakeup_preempt_dl(struct rq *rq, struct task_struct *p, int flags); · line 1758

Quoted 10 lines
kernel/sched/deadline.c +7 -0
+	/* A boost held from this refill keeps the period it placed. */
+	if (!dl_note_donor_refill(dl_se)) {
+		/* for non-boosted task, pi_of(dl_se) == dl_se */
+		dl_se->period_start = rq_clock(rq);
+		dl_se->deadline = dl_se->period_start + pi_of(dl_se)->dl_deadline;
+		dl_se->runtime = pi_of(dl_se)->dl_runtime;
+	}
 
 	/*
 	 * If it is a deferred reservation, and the server

[Severity: Low]
Does holding a boost on its first refill emit trace_sched_dl_replenish_tp() twice for a single replenishment?

When dl_note_donor_refill() holds the boost, dl_hold_refilled_boost() calls dl_boost_donor_period(). Whenever no earlier donor period is resumed, dl_boost_donor_period() calls replenish_dl_new_period(), which fires trace_sched_dl_replenish_tp() before returning to the outer replenish_dl_new_period() or replenish_dl_entity() call that fires trace_sched_dl_replenish_tp() a second time.

[ ... ]

Quoted 23 lines
kernel/sched/deadline.c +7 -3
@@ -6115,11 +6125,15 @@ static bool dl_setattr_period(struct sched_dl_entity *dl_se, u64 old_runtime,
  * If sched_setattr() changed the parameters before this refill,
  * dl_setattr_period() saved the old ones in setattr_*. Fit the period
  * saved in own_* to the new parameters with them. setattr_* share
- * storage with own_*: read them before writing own_*. The caller holds
- * the rq lock of the task, and has updated the rq clock.
+ * storage with own_*: read them before writing own_*. Return true if a
+ * period of the donor was placed. The caller holds the rq lock of the
+ * task, and has updated the rq clock.
  */
-static void dl_hold_refilled_boost(struct sched_dl_entity *dl_se)
+static bool dl_hold_refilled_boost(struct sched_dl_entity *dl_se)
 {
+	struct sched_dl_entity *pi_se = pi_of(dl_se);
+	u64 donor_runtime = READ_ONCE(pi_se->dl_runtime);
+	u64 donor_deadline = READ_ONCE(pi_se->dl_deadline);
 	u64 old_runtime = dl_se->setattr_runtime;
 	u64 old_deadline = dl_se->setattr_deadline;
 	u64 old_period = dl_se->setattr_period;
@@ -6142,6 +6156,14 @@ static void dl_hold_refilled_boost(struct sched_dl_entity *dl_se)
 				 old_runtime, old_deadline, old_period, false, false);
 	else if (!dl_time_before(now, dl_se->own_deadline))
 		dl_se->own_runtime = 0;

kernel/sched/deadline.c static void dl_hold_refilled_boost(struct sched_dl_entity *dl_se) · line 6168

Quoted 10 lines
kernel/sched/deadline.c +8 -0
+	/*
+	 * replenish_dl_entity() checks the donor's parameters: it adds no
+	 * runtime if they were cleared. Start no period from them then.
+	 */
+	if (!donor_runtime || !donor_deadline)
+		return false;
+	dl_boost_donor_period(dl_se);
+	return true;
 }
 #endif /* CONFIG_RT_MUTEXES */

[Severity: Medium]
Can clearing the donor's parameters corrupt dl_se's period and saved boost_* state?

When the donor leaves SCHED_DEADLINE and __dl_clear_params(pi_se) zeroes its parameters before or during the first refill of an unheld boost, three things go wrong:

1. dl_hold_refilled_boost() sets dl_se->dl_boost_held = 1 before checking !donor_runtime || !donor_deadline. Returning false leaves dl_se->deadline and dl_se->period_start on the owner's own period with dl_se->runtime == 0. At deboost, dl_end_held_boost() sees dl_boost_held == 1 and saves the owner's own period and zero runtime into boost_* with dl_boost_granted = 1:

kernel/sched/deadline.c:dl_end_held_boost() {
	if (!dl_se->dl_boost_held)
		return false;
	...
	dl_se->boost_deadline = dl_se->deadline;
	dl_se->boost_runtime = dl_se->runtime;
	dl_se->boost_period_start = dl_se->period_start;
	dl_se->dl_boost_granted = 1;
}

A subsequent held boost from a new donor within own_period_start + period then resumes the owner's own_period_start with zero runtime in dl_boost_donor_period(), and replenish_dl_entity() pushes its deadline out to own_period_start + dl_deadline + dl_period (past the waiter's deadline).

2. When called from replenish_dl_new_period() -> dl_note_donor_refill(), returning false from dl_hold_refilled_boost() causes
if (!dl_note_donor_refill(dl_se)) in replenish_dl_new_period() to take the branch and overwrite period_start = rq_clock(rq), deadline = period_start + 0, and runtime = 0 from the cleared donor parameters anyway.

3. __dl_clear_params(pi_se) runs under the donor's rq lock on another CPU. replenish_dl_entity() snapshots runtime, deadline, and period once and validates all three, whereas dl_hold_refilled_boost() re-reads pi_se->dl_runtime and pi_se->dl_deadline (without dl_period), and dl_boost_donor_period() and replenish_dl_new_period() re-read pi_se fields again. If __dl_clear_params(pi_se) races between replenish_dl_entity()'s initial check and those re-reads, dl_hold_refilled_boost() can return false without placing a donor period while replenish_dl_entity() still executes its while (dl_se->runtime <= 0) loop on the owner's own deadline and negative runtime, or replenish_dl_new_period() can zero the relative deadline and runtime before that loop advances both period_start and deadline to now + period.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=41
#55 lore raw

PATCH v12 42/43 selftests/sched: Test parameter changes in deadline_bw_test

In reply to Mathieu Desnoyers

Add 39 cases to deadline_bw_test, for the fixes earlier in this series. The comment above each case gives its scenario:
- In 24 cases, a task with runtime left changes its parameters, or comes back to the class with new ones. In params_swap_overload, two tasks pass a large runtime back and forth. This is Example 1 of Tang, Anderson and Abeni (RTNS 2021).
- In 11 cases, a lock owner cannot run on its own reservation, so a deadline waiter must boost it. In pi_owner_runs_out_later, pi_owner_wakes_in_gap_later and pi_owner_handed_off, the owner loses its reservation after the waiter blocked. In pi_owner_reboost_loop and pi_owners_reboost_loop, a waiter boosts a throttled owner over and over. Another deadline task must still get its runtime.
- In 4 cases, a constrained task leaves the class in its gap and comes back. The gap is the time between its deadline and its next period.

These cases fail on v7.4-rc2, in each of two VMs of 8 CPUs. In the series, each one fails at the patch before the fix named, and passes from that fix on:
- "sched/deadline: Cap the runtime left of a constrained task at dl_runtime": wake_constrained_new_params.
- "sched/deadline: Check the runtime of a task that comes back runnable": return_running_new_params.
- "sched/deadline: Keep the debt of a task across a change of parameters": change_running_new_params, change_boosted_new_params, constrained_gap_change_running, change_constrained_new_params, setattr_blocked_new_period, period_grown_untouched, deadline_widened_asleep, period_shrunk_throttled, period_shrunk_throttled_started, runtime_raised_waited, deboost_out_new_params and params_swap_overload.
- "sched/deadline: Boost an owner that cannot run on its own reservation": pi_owner_asleep_in_gap, pi_owner_throttled, pi_owner_throttled_asleep and pi_owner_held_new_params.
- "sched/deadline: Boost an owner throttled after its waiter blocked":
pi_owner_runs_out_later, pi_owner_wakes_in_gap_later and pi_owner_handed_off.

These cases pass on v7.4-rc2 and with the whole series. A wrong version of the fix named would fail them:
- "sched/deadline: Keep the debt of a task across a change of parameters": deboost_asleep_new_params, setattr_blocked_reenter, deadline_grows_running, period_shrunk_running, period_grown_running, params_flip_no_gain, period_shrunk_no_burst, wake_after_deadline_shrunk and constrained_after_job.
- "sched/deadline: Boost an owner that cannot run on its own reservation": pi_owner_throttled_rival, pi_owner_held_long, pi_owner_reboost_loop and pi_owners_reboost_loop. In the series before that fix, pi_owner_throttled_rival failed in some runs. The last two failed on an earlier version of the fix, in which each boost started a new period of the waiter.

deboost_new_params_reboost passes on v7.4-rc2, and at each patch of this series. On v7.4-rc2, a task made SCHED_DEADLINE while it waits on a PI mutex keeps a deadline of 0. So it boosts the owner in any case. From "sched/deadline: Start a period for a task that enters the class blocked" on, that task gets a real deadline. The case then checks the deboost of "sched/deadline: Keep the debt of a task across a change of parameters". The debt moves the owner's deadline after the waiter's. The deboost must then boost the owner again.

The four reenter_*_in_gap cases pass on v7.4-rc2. On the first series, they fail from these patches on:
- "sched/deadline: Reset the deadline and the runtime with the parameters": reenter_runnable_in_gap.
- "sched/deadline: Release the bandwidth of a task that leaves the class asleep": reenter_asleep_in_gap, reenter_asleep_late_in_gap and reenter_pi_blocked_in_gap.
Three of them pass from "sched/deadline: Count the 0-lag time from the next period" on. reenter_asleep_late_in_gap passes from "sched/deadline: Keep a departing constrained task until its next period" on.

The cases run in P, a one-CPU partition root. The harness now waits after each case, for up to 10s. It waits until the total_bw of P's CPU comes back down to its value before the case. A task of a case can keep its bandwidth counted after it exits, until its 0-lag time. params_swap_overload leaves a debt that takes seconds to repay. random_mix runs right after it. Without the wait, random_mix failed in most runs: the this_bw it read dropped when that debt was repaid.

With the whole series, every case passes, PREEMPT_RT included. No kernel warning is logged.

---
 .../sched/deadline/deadline_bw_test.c         | 3671 ++++++++++++++++-
 1 file changed, 3634 insertions(+), 37 deletions(-)
tools/testing/selftests/sched/deadline/deadline_bw_test.c +3634 -37
index 83dd8b4e79a3..66955952901f 100644
@@ -32,7 +32,9 @@
  *
  * The cases of p_cases[] run with P, a partition root of the last
  * housekeeping CPU. P is made anew for each case. Making it rebuilds the
- * accounting, which clears what an earlier case left.
+ * accounting, which clears what an earlier case leaked. A task that exits
+ * stays counted until its 0-lag time: run_p_cases() waits for that after
+ * each case.
  *
  * The cases of hp_cases[] run after them, without P. These are the
  * hotplug cases, and ten other cases: mask_out_refused,
@@ -58,6 +60,7 @@
 #include <fcntl.h>
 #include <limits.h>
 #include <pthread.h>
+#include <sys/klog.h>
 #include <sched.h>
 #include <semaphore.h>
 #include <signal.h>
@@ -284,6 +287,20 @@ static int set_deadline(pid_t tid, uint64_t runtime, uint64_t period)
 	return set_deadline_flags(tid, runtime, period, 0);
 }
 
+/* Make @tid a deadline task with a relative deadline shorter than @period. */
+static int set_constrained(pid_t tid, uint64_t runtime, uint64_t deadline, uint64_t period)
+{
+	struct dl_attr attr = {
+		.size = sizeof(attr),
+		.sched_policy = SCHED_DEADLINE,
+		.sched_runtime = runtime,
+		.sched_deadline = deadline,
+		.sched_period = period,
+	};
+
+	return syscall(SYS_sched_setattr, tid, &attr, 0) ? -errno : 0;
+}
+
 static int set_other(pid_t tid)
 {
 	struct dl_attr attr = { .size = sizeof(attr), .sched_policy = SCHED_OTHER };
@@ -2602,6 +2619,163 @@ static int deboost_in_asleep_after_refill(void)
 	return ret;
 }
 
+static sem_t nps_wake;
+static _Atomic pid_t nps_o_tid;
+static _Atomic uint64_t nps_t0;
+
+/*
+ * Run as the owner O of deboost_out_new_params(). Its comment has the
+ * steps. Return NULL, or 1 on an error.
+ */
+static void *nps_owner(void *arg)
+{
+	uint64_t t0;
+	int err;
+
+	nps_o_tid = gettid_();
+	err = set_deadline(0, 5 * NSEC_PER_MSEC, PERIOD);
+	nps_t0 = now_ns();
+	if (err || take_pi_lock()) {
+		if (err)
+			ksft_print_msg("the owner refused as a deadline task: %s\n",
+				       strerror(-err));
+		sem_post(&inas_locked);
+		set_other(0);
+		return (void *)1;
+	}
+	sem_post(&inas_locked);
+	t0 = now_ns();
+	while (!inas_locking || proc_state_of(inas_d_tid) != 'S') {
+		if (now_ns() - t0 > NSEC_PER_SEC) {
+			ksft_print_msg("the donor did not block on the lock\n");
+			drop_pi_lock();
+			set_other(0);
+			return (void *)1;
+		}
+	}
+	spin_ns(4 * NSEC_PER_MSEC);
+	sem_wait_sig(&nps_wake);
+	drop_pi_lock();
+	set_other(0);
+	return NULL;
+}
+
+/*
+ * A boosted task changes its parameters before any refill, then leaves
+ * the class at its deboost. O and D share the CPU of P:
+ * 1. D becomes a deadline task of 2ms every 50ms, and waits.
+ * 2. O becomes a deadline task of 5ms every 100ms, at 0ms. It takes
+ *    pi_lock.
+ * 3. D waits 30ms for pi_lock. Its deadline is the earlier one: it boosts
+ *    O.
+ * 4. O runs 4ms, within its own runtime, and sleeps, boosted. Its 0-lag
+ *    time is at 100 - 1 * 100 / 5 = 80ms.
+ * 5. The main thread changes O to 5ms every 200ms, then switches O to
+ *    SCHED_OTHER. The boost keeps O in the class.
+ * 6. D times out at about 30ms. O leaves the class at its deboost.
+ *
+ * At the deboost, at 30ms, O has used 4ms. Its old bandwidth of 5% gave
+ * it 1.5ms by then. So O has a debt of 2.5ms. The new bandwidth of 2.5%
+ * repays that at 130ms. O's bandwidth must stay in total_bw until then.
+ * Mixing the runtime and the deadline of the old parameters with the new
+ * ones would put its 0-lag time at 200 - 1 * 200 / 5 = 160ms. The case
+ * reads total_bw at 115ms and at 145ms.
+ */
+static int deboost_out_new_params(void)
+{
+	long long base, with_o = -1, early = -1, late = -1;
+	void *o_ret = NULL, *d_ret = NULL;
+	int err, out = 0, ret = KSFT_FAIL;
+	pthread_t o, d;
+
+	if (total_bw_of(top_cpu(1)) < 0) {
+		ksft_print_msg("sched_debug tells of no total_bw\n");
+		return KSFT_SKIP;
+	}
+	if (pi_init())
+		return KSFT_SKIP;
+	if (sem_init(&inas_ready, 0, 0) || sem_init(&inas_locked, 0, 0) ||
+	    sem_init(&inas_out, 0, 0) || sem_init(&inas_read, 0, 0) ||
+	    sem_init(&nps_wake, 0, 0)) {
+		ksft_print_msg("sem_init: %s\n", strerror(errno));
+		pi_fini();
+		return KSFT_SKIP;
+	}
+	inas_locking = 0;
+	inas_d_tid = 0;
+	nps_o_tid = 0;
+	if (pthread_create(&d, NULL, inas_donor, NULL)) {
+		ksft_print_msg("the donor thread could not start\n");
+		goto out;
+	}
+	if (sem_wait_for(&inas_ready, 5000)) {
+		sem_post(&inas_locked);
+		sem_post(&inas_read);
+		join(d);
+		goto out;
+	}
+	base = total_bw_of(top_cpu(1));	/* with D, without O */
+	if (pthread_create(&o, NULL, nps_owner, NULL)) {
+		ksft_print_msg("the owner thread could not start\n");
+		sem_post(&inas_locked);
+		sem_post(&inas_read);
+		join(d);
+		goto out;
+	}
+	if (wait_for(1000, inas_locking && proc_state_of(inas_d_tid) == 'S' &&
+		     nps_o_tid && proc_state_of(nps_o_tid) == 'S')) {
+		ksft_print_msg("the owner did not sleep boosted\n");
+	} else {
+		err = set_deadline(nps_o_tid, 5 * NSEC_PER_MSEC, 2 * PERIOD);
+		if (!err)
+			with_o = total_bw_of(top_cpu(1));
+		if (!err)
+			err = set_other(nps_o_tid);
+		if (!err && sem_getvalue(&inas_out, &out))
+			out = -1;
+		if (err)
+			ksft_print_msg("the owner cannot change its parameters: %s\n",
+				       strerror(-err));
+		else if (out)
+			ksft_print_msg("the donor timed out before the change\n");
+		else if (sem_wait_for(&inas_out, 5000))
+			ksft_print_msg("the donor did not time out\n");
+		else {
+			while (now_ns() - nps_t0 < 115 * NSEC_PER_MSEC)
+				sleep_ns(NSEC_PER_MSEC);
+			early = total_bw_of(top_cpu(1));
+			while (now_ns() - nps_t0 < 145 * NSEC_PER_MSEC)
+				sleep_ns(NSEC_PER_MSEC);
+			late = total_bw_of(top_cpu(1));
+		}
+	}
+	sem_post(&inas_read);
+	pthread_join(d, &d_ret);
+	sem_post(&nps_wake);
+	pthread_join(o, &o_ret);
+	if (o_ret || d_ret || late < 0)
+		goto out;	/* a thread said why */
+	if (early != with_o) {
+		ksft_print_msg("total_bw %lld at 115 ms, %lld expected: %s\n", early,
+			       with_o, "O left before it repaid its debt");
+		goto out;
+	}
+	if (late != base) {
+		ksft_print_msg("total_bw %lld at 145 ms, %lld expected: %s\n", late,
+			       base, "O stayed after it repaid its debt");
+		goto out;
+	}
+	ret = KSFT_PASS;
+out:
+	sem_destroy(&inas_ready);
+	sem_destroy(&inas_locked);
+	sem_destroy(&inas_out);
+	sem_destroy(&inas_read);
+	sem_destroy(&nps_wake);
+	pi_fini();
+	return ret;
+}
+
 static _Atomic uint32_t rq_cond, rq_pi;
 static atomic_int rq_order, rq_w_rank, rq_x_rank, rq_x_locking;
 static _Atomic pid_t rq_w_tid, rq_x_tid;
@@ -2754,17 +2928,30 @@ static _Atomic pid_t ret_tid;
 static atomic_int ret_first_period_ms;
 
 /*
- * Run as the thread of return_asleep_new_params(). Become a deadline task
- * of 50ms every 100ms and run 20ms. 30ms of runtime are left, and the
- * 0-lag time is about 20ms ahead. Then sleep. The main thread switches
- * the thread to SCHED_OTHER and back, with 10ms every 100ms. Then spin
- * for 100ms, and store the CPU time used, in ms, in ret_first_period_ms.
- * Return NULL, or 1 on an error.
+ * Run as the thread of the return cases. Become a deadline task of 50ms
+ * every 100ms and run 20ms. 30ms of runtime are left, and the 0-lag time
+ * is about 20ms ahead. Then change to new parameters, as @arg says:
+ * - 0: the thread switches itself to SCHED_OTHER and back to
+ *   SCHED_DEADLINE, while it runs.
+ * - 1: the thread sleeps. The main thread switches it to SCHED_OTHER and
+ *   back.
+ * - 2: the thread changes its own parameters while it runs. It stays in
+ *   SCHED_DEADLINE.
+ * - 3: as 2, while a donor boosts the thread. The thread takes pi_lock and
+ *   wakes the donor up. The donor blocks on pi_lock. The thread then
+ *   changes its parameters, and unlocks.
+ * - 4: as 2, to a constrained deadline: 8ms in 20ms, every 100ms.
+ * - 5: the thread sleeps. The main thread changes it to 8ms in 20ms,
+ *   every 100ms.
+ * The new parameters are 10ms every 100ms, except in modes 4 and 5.
+ * Then spin for 100ms, and store the CPU time used, in ms, in
+ * ret_first_period_ms. Return NULL, or 1 on an error.
  */
 static void *returner(void *arg)
 {
+	long mode = (long)arg;
 	uint64_t t0, cpu0;
-	int err;
+	int err = 0;
 
 	ret_tid = gettid_();
 	err = set_deadline(0, 50 * NSEC_PER_MSEC, PERIOD);
@@ -2773,8 +2960,34 @@ static void *returner(void *arg)
 		return (void *)1;
 	}
 	spin_ns(20 * NSEC_PER_MSEC);
-	sem_post(&ret_ready);
-	sem_wait_sig(&ret_go);
+	if (mode == 1 || mode == 5) {
+		sem_post(&ret_ready);
+		sem_wait_sig(&ret_go);
+	} else if (mode == 4) {
+		err = set_constrained(0, 8 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, PERIOD);
+	} else if (mode == 3) {
+		if (take_pi_lock())
+			return (void *)1;
+		sem_post(&owner_go);
+		t0 = now_ns();
+		while (!refill_donor_locking || proc_state_of(donor_tid) != 'S') {
+			if (now_ns() - t0 > NSEC_PER_SEC) {
+				ksft_print_msg("the donor did not block on the lock\n");
+				drop_pi_lock();
+				return (void *)1;
+			}
+		}
+		err = set_deadline(0, 10 * NSEC_PER_MSEC, PERIOD);
+		drop_pi_lock();
+	} else {
+		err = mode ? 0 : set_other(0);
+		if (!err)
+			err = set_deadline(0, 10 * NSEC_PER_MSEC, PERIOD);
+	}
+	if (err) {
+		ksft_print_msg("the task could not change: %s\n", strerror(-err));
+		return (void *)1;
+	}
 	t0 = now_ns();
 	cpu0 = thread_time_ns();
 	while (now_ns() - t0 < PERIOD)
@@ -2784,46 +2997,80 @@ static void *returner(void *arg)
 }
 
 /*
- * A task leaves SCHED_DEADLINE while it sleeps, and comes back at once
- * with a smaller runtime. Its 0-lag time is ahead, so it keeps its
- * deadline and the runtime it has left. returner() runs the task. It must
- * get about 10ms of CPU time in the 100ms after the change: its new
- * runtime, not the 30ms left from the old one. The case fails above 20ms.
+ * Run a return case: returner() with @mode as its argument. The thread
+ * must get about 10ms of CPU time in the 100ms after the change, 8ms in
+ * modes 4 and 5. That is its new runtime, not the 30ms left from the old
+ * one. The case fails above 20ms.
  */
-static int return_asleep_new_params(void)
+static int return_case(long mode)
 {
-	void *t_ret;
+	void *t_ret = NULL, *d_ret = NULL;
 	int err, ret = KSFT_PASS;
-	pthread_t t;
+	pthread_t t, d;
 
 	if (sem_init(&ret_ready, 0, 0) || sem_init(&ret_go, 0, 0)) {
 		ksft_print_msg("sem_init: %s\n", strerror(errno));
 		return KSFT_FAIL;
 	}
 	ret_first_period_ms = -1;
-	if (pthread_create(&t, NULL, returner, NULL)) {
+	if (mode == 3) {
+		/* The donor: a deadline task of 2ms every 10ms */
+		if (pi_init() || sem_init(&refill_donor_ready, 0, 0))
+			return KSFT_SKIP;
+		refill_donor_locking = 0;
+		donor_tid = 0;
+		if (pthread_create(&d, NULL, refill_donor, NULL)) {
+			ksft_print_msg("the donor thread could not start\n");
+			return KSFT_FAIL;
+		}
+		if (sem_wait_for(&refill_donor_ready, 5000) || donor_tid <= 0) {
+			ksft_print_msg("the donor is not ready\n");
+			sem_post(&owner_go);
+			join(d);
+			return KSFT_FAIL;
+		}
+	}
+	if (pthread_create(&t, NULL, returner, (void *)mode)) {
 		ksft_print_msg("the task thread could not start\n");
+		if (mode == 3) {
+			sem_post(&owner_go);
+			join(d);
+		}
 		return KSFT_FAIL;
 	}
-	if (sem_wait_for(&ret_ready, 5000)) {
-		/* The thread may get there late. Let it end. */
+	if (mode == 1 || mode == 5) {
+		if (sem_wait_for(&ret_ready, 5000)) {
+			/* The thread may get there late. Let it end. */
+			sem_post(&ret_go);
+			join(t);
+			return KSFT_FAIL;	/* the task said why */
+		}
+		/* Before its 0-lag time, about 20ms ahead */
+		err = wait_for(10, proc_state_of(ret_tid) == 'S') ? -EAGAIN : 0;
+		if (!err && mode == 1)
+			err = set_other(ret_tid);
+		if (!err && mode == 1)
+			err = set_deadline(ret_tid, 10 * NSEC_PER_MSEC, PERIOD);
+		if (!err && mode == 5)
+			err = set_constrained(ret_tid, 8 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC,
+					      PERIOD);
 		sem_post(&ret_go);
-		join(t);
-		return KSFT_FAIL;	/* the task said why */
-	}
-	/* Before its 0-lag time, about 20ms ahead */
-	err = wait_for(10, proc_state_of(ret_tid) == 'S') ? -EAGAIN : 0;
-	if (!err)
-		err = set_other(ret_tid);
-	if (!err)
-		err = set_deadline(ret_tid, 10 * NSEC_PER_MSEC, PERIOD);
-	sem_post(&ret_go);
-	if (err) {
-		ksft_print_msg("the task could not change asleep: %s\n", strerror(-err));
-		join(t);
-		return KSFT_FAIL;
+		if (err) {
+			ksft_print_msg("the task could not change asleep: %s\n",
+				       strerror(-err));
+			join(t);
+			return KSFT_FAIL;
+		}
 	}
 	err = pthread_join(t, &t_ret);
+	if (mode == 3) {
+		if (t_ret)
+			sem_post(&owner_go);	/* the task never woke the donor up */
+		if (pthread_join(d, &d_ret) || d_ret)
+			ret = KSFT_FAIL;
+		pi_fini();
+		sem_destroy(&refill_donor_ready);
+	}
 	if (err) {
 		ksft_print_msg("pthread_join: %s\n", strerror(err));
 		ret = KSFT_FAIL;
@@ -2839,6 +3086,3034 @@ static int return_asleep_new_params(void)
 	return ret;
 }
 
+/*
+ * A running task leaves SCHED_DEADLINE and comes back at once with a
+ * smaller runtime. Its 0-lag time is ahead, so it keeps its deadline and
+ * the runtime it has left. It must not run on more than the new runtime.
+ */
+static int return_running_new_params(void)
+{
+	return return_case(0);
+}
+
+/* The same, for a task that leaves and comes back while it sleeps. */
+static int return_asleep_new_params(void)
+{
+	return return_case(1);
+}
+
+/* The same, for a task that changes its parameters within the class. */
+static int change_running_new_params(void)
+{
+	return return_case(2);
+}
+
+/*
+ * The same, for a task that changes its parameters while a donor boosts
+ * it. The donor is a deadline task of 2ms every 10ms. Its deadline is the
+ * earlier one, so the task runs on its own runtime while boosted. The
+ * unlock ends the boost. The runtime left must then be checked against
+ * the new parameters.
+ */
+static int change_boosted_new_params(void)
+{
+	return return_case(3);
+}
+
+/*
+ * A running task of 50ms every 100ms has 30ms left. It changes itself to
+ * 8ms in 20ms, every 100ms. Its deadline stays 80ms ahead, further than
+ * the new 20ms. The check of the runtime left uses the new density over
+ * those 80ms. That allows 32ms, so the check keeps the 30ms. The task
+ * must keep no more than its new 8ms.
+ */
+static int change_constrained_new_params(void)
+{
+	return return_case(4);
+}
+
+/*
+ * The same, for a task that the main thread changes while it sleeps. The
+ * check is at its wakeup.
+ */
+static int wake_constrained_new_params(void)
+{
+	return return_case(5);
+}
+
+static sem_t shrink_ready;
+static _Atomic pid_t shrink_tid;
+static _Atomic unsigned long long shrink_t0;
+static _Atomic long long shrink_back_ms;
+
+/*
+ * Run as T of wake_after_deadline_shrunk(). Its comment has the steps.
+ * Store in shrink_back_ms the time at which T runs after its sleep.
+ * Return NULL, or 1 on an error.
+ */
+static void *shrink_t(void *arg)
+{
+	struct timespec ts;
+	uint64_t t0, end;
+	int err;
+
+	shrink_tid = gettid_();
+	err = set_deadline(0, 10 * NSEC_PER_MSEC, PERIOD);
+	t0 = now_ns();
+	shrink_t0 = t0;
+	sem_post(&shrink_ready);
+	if (err) {
+		ksft_print_msg("T refused as a deadline task: %s\n", strerror(-err));
+		return (void *)1;
+	}
+	spin_ns(NSEC_PER_MSEC);
+	end = t0 + 110 * NSEC_PER_MSEC;
+	ts.tv_sec = end / NSEC_PER_SEC;
+	ts.tv_nsec = end % NSEC_PER_SEC;
+	while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR)
+		;
+	shrink_back_ms = (now_ns() - t0) / NSEC_PER_MSEC;
+	if (set_other(0))
+		ksft_print_msg("T cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Shorten the relative deadline of a task asleep past its deadline:
+ * 1. T makes itself a deadline task of 10ms every 100ms, at 0ms. Its
+ *    deadline is 100ms. It runs 1ms, and sleeps until 110ms.
+ * 2. At 105ms, the main thread changes T to 10ms in 20ms, every 100ms.
+ * 3. T wakes up at 110ms.
+ *
+ * T's job ran. It counts as a job of the new parameters with the same
+ * deadline, released at 100 - 20 = 80ms. So T's next period starts at
+ * 80 + 100 = 180ms, and T runs then. A new period at the wakeup would
+ * start only 30ms after the release of the job that ran. That is less
+ * than a period of the new parameters. The case fails if T runs before
+ * 170ms or at 200ms or later.
+ */
+static int wake_after_deadline_shrunk(void)
+{
+	void *t_ret = NULL;
+	int err, ret = KSFT_FAIL;
+	uint64_t at;
+	pthread_t t;
+
+	if (sem_init(&shrink_ready, 0, 0)) {
+		ksft_print_msg("sem_init: %s\n", strerror(errno));
+		return KSFT_FAIL;
+	}
+	shrink_back_ms = -1;
+	if (pthread_create(&t, NULL, shrink_t, NULL)) {
+		ksft_print_msg("the T thread could not start\n");
+		goto out;
+	}
+	if (sem_wait_for(&shrink_ready, 5000)) {
+		join(t);
+		goto out;
+	}
+	at = shrink_t0 + 105 * NSEC_PER_MSEC;
+	while (now_ns() < at)
+		sleep_ns(NSEC_PER_MSEC);
+	if (proc_state_of(shrink_tid) != 'S') {
+		ksft_print_msg("T is not asleep at 105ms\n");
+		join(t);
+		goto out;
+	}
+	err = set_constrained(shrink_tid, 10 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC,
+			      PERIOD);
+	if (err) {
+		ksft_print_msg("T could not change asleep: %s\n", strerror(-err));
+		join(t);
+		goto out;
+	}
+	pthread_join(t, &t_ret);
+	if (t_ret || shrink_back_ms < 0)
+		goto out;	/* T said why */
+	if (shrink_back_ms < 170 || shrink_back_ms >= 200) {
+		ksft_print_msg("T ran again at %lld ms, not at 180\n",
+			       (long long)shrink_back_ms);
+		goto out;
+	}
+	ret = KSFT_PASS;
+out:
+	sem_destroy(&shrink_ready);
+	return ret;
+}
+
+static sem_t widen_ready;
+static _Atomic pid_t widen_tid;
+static _Atomic unsigned long long widen_t0;
+static _Atomic long long widen_used_us;
+
+/*
+ * Run as T of deadline_widened_asleep(). Its comment has the steps.
+ * Store in widen_used_us the CPU time that T gets from 30ms to 95ms.
+ * Return NULL, or 1 on an error.
+ */
+static void *widen_t(void *arg)
+{
+	struct timespec ts;
+	uint64_t t0, end, cpu0;
+	int err;
+
+	widen_tid = gettid_();
+	err = set_constrained(0, 10 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, PERIOD);
+	t0 = now_ns();
+	widen_t0 = t0;
+	sem_post(&widen_ready);
+	if (err) {
+		ksft_print_msg("T refused as a deadline task: %s\n", strerror(-err));
+		return (void *)1;
+	}
+	spin_ns(8 * NSEC_PER_MSEC);
+	end = t0 + 30 * NSEC_PER_MSEC;
+	ts.tv_sec = end / NSEC_PER_SEC;
+	ts.tv_nsec = end % NSEC_PER_SEC;
+	while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR)
+		;
+	cpu0 = thread_time_ns();
+	while (now_ns() < t0 + 95 * NSEC_PER_MSEC)
+		;
+	widen_used_us = (thread_time_ns() - cpu0) / NSEC_PER_USEC;
+	if (set_other(0))
+		ksft_print_msg("T cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Widen the relative deadline of a task asleep in its period:
+ * 1. T becomes a deadline task of 10ms in 20ms, every 100ms, at 0ms. It
+ *    spins 8ms and sleeps until 30ms.
+ * 2. At 25ms, the main thread changes T to 10ms every 100ms. T's deadline
+ *    is now 100ms. It has 2ms of runtime left.
+ * 3. T wakes up at 30ms and spins until 95ms.
+ *
+ * T must get about its 2ms left before 100ms. If its deadline stayed at
+ * 20ms, it would have passed at the wakeup. T would then get a new period
+ * of 10ms. The case fails if T gets 5ms of CPU time or more.
+ */
+static int deadline_widened_asleep(void)
+{
+	void *t_ret = NULL;
+	int err, ret = KSFT_FAIL;
+	uint64_t at;
+	pthread_t t;
+
+	if (sem_init(&widen_ready, 0, 0)) {
+		ksft_print_msg("sem_init: %s\n", strerror(errno));
+		return KSFT_FAIL;
+	}
+	widen_used_us = -1;
+	if (pthread_create(&t, NULL, widen_t, NULL)) {
+		ksft_print_msg("the T thread could not start\n");
+		goto out;
+	}
+	if (sem_wait_for(&widen_ready, 5000)) {
+		join(t);
+		goto out;
+	}
+	at = widen_t0 + 25 * NSEC_PER_MSEC;
+	while (now_ns() < at)
+		sleep_ns(NSEC_PER_MSEC);
+	if (proc_state_of(widen_tid) != 'S') {
+		ksft_print_msg("T is not asleep at 25ms\n");
+		join(t);
+		goto out;
+	}
+	err = set_deadline(widen_tid, 10 * NSEC_PER_MSEC, PERIOD);
+	if (err) {
+		ksft_print_msg("T could not change asleep: %s\n", strerror(-err));
+		join(t);
+		goto out;
+	}
+	pthread_join(t, &t_ret);
+	if (t_ret || widen_used_us < 0)
+		goto out;	/* T said why */
+	if (widen_used_us >= 5000) {
+		ksft_print_msg("T got %lld us before its deadline, with 2ms left\n",
+			       (long long)widen_used_us);
+		goto out;
+	}
+	ret = KSFT_PASS;
+out:
+	sem_destroy(&widen_ready);
+	return ret;
+}
+
+/*
+ * The kernel prints this when a refill finds a deadline long passed. It
+ * prints it once per boot, so a case can see it only if no earlier test
+ * caused it.
+ */
+#define DL_LAG_MSG	"sched: DL replenish lagged too much"
+
+/*
+ * Return 1 if the kernel log holds @s, 0 if it does not, or -1 if it
+ * cannot be read.
+ */
+static int klog_has(const char *s)
+{
+	int len = klogctl(10, NULL, 0);		/* SYSLOG_ACTION_SIZE_BUFFER */
+	char *buf;
+	int n, ret;
+
+	if (len <= 0)
+		return -1;
+	buf = malloc(len + 1);
+	if (!buf)
+		return -1;
+	n = klogctl(3, buf, len);		/* SYSLOG_ACTION_READ_ALL */
+	ret = -1;
+	if (n >= 0) {
+		buf[n] = '\0';
+		ret = !!strstr(buf, s);
+	}
+	free(buf);
+	return ret;
+}
+
+static sem_t shorten_ready;
+static _Atomic pid_t shorten_tid;
+static _Atomic unsigned long long shorten_t0;
+static _Atomic long long shorten_back_ms;
+
+/*
+ * Run as T of period_shrunk_throttled(). Its comment has the steps. Store
+ * in shorten_back_ms the time at which T runs after its yield. Return
+ * NULL, or 1 on an error.
+ */
+static void *shorten_t(void *arg)
+{
+	uint64_t t0;
+	int err;
+
+	shorten_tid = gettid_();
+	err = set_deadline(0, 10 * NSEC_PER_MSEC, PERIOD);
+	t0 = now_ns();
+	shorten_t0 = t0;
+	sem_post(&shorten_ready);
+	if (err) {
+		ksft_print_msg("T refused as a deadline task: %s\n", strerror(-err));
+		return (void *)1;
+	}
+	spin_ns(8 * NSEC_PER_MSEC);
+	sched_yield();
+	shorten_back_ms = (now_ns() - t0) / NSEC_PER_MSEC;
+	if (set_other(0))
+		ksft_print_msg("T cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Shorten the period of a task throttled until its next period:
+ * 1. T becomes a deadline task of 10ms every 100ms, at 0ms. It runs 8ms
+ *    and yields. Its replenishment timer is armed at 100ms.
+ * 2. At @at_ms, the main thread changes T to 10ms every @period_ms.
+ *
+ * T ran ahead of its share: its 0-lag time is at 80ms. The change keeps
+ * that debt, and repays it at the new bandwidth. T's next period starts
+ * once it is repaid. The timer must be armed again there. T must run
+ * again at @back_ms: the case fails 5ms before it, or 30ms after it. A
+ * refill at 100ms would also find a period that ended before, and print
+ * "sched: DL replenish lagged too much". The case fails if that message
+ * is logged. It skips if the message was logged before: the kernel prints
+ * it once.
+ */
+static int shorten_case(int at_ms, int period_ms, int back_ms)
+{
+	void *t_ret = NULL;
+	int err, had, ret = KSFT_FAIL;
+	uint64_t at;
+	pthread_t t;
+
+	had = klog_has(DL_LAG_MSG);
+	if (had) {
+		ksft_print_msg(had < 0 ? "the kernel log cannot be read\n" :
+			       "the lag message was logged before this case\n");
+		return KSFT_SKIP;
+	}
+	if (sem_init(&shorten_ready, 0, 0)) {
+		ksft_print_msg("sem_init: %s\n", strerror(errno));
+		return KSFT_FAIL;
+	}
+	shorten_back_ms = -1;
+	if (pthread_create(&t, NULL, shorten_t, NULL)) {
+		ksft_print_msg("the T thread could not start\n");
+		goto out;
+	}
+	if (sem_wait_for(&shorten_ready, 5000)) {
+		join(t);
+		goto out;
+	}
+	at = shorten_t0 + at_ms * NSEC_PER_MSEC;
+	while (now_ns() < at)
+		sleep_ns(NSEC_PER_MSEC);
+	err = set_deadline(shorten_tid, 10 * NSEC_PER_MSEC, period_ms * NSEC_PER_MSEC);
+	if (err) {
+		ksft_print_msg("T could not change throttled: %s\n", strerror(-err));
+		join(t);
+		goto out;
+	}
+	pthread_join(t, &t_ret);
+	if (t_ret || shorten_back_ms < 0)
+		goto out;	/* T said why */
+	if (shorten_back_ms < back_ms - 5 || shorten_back_ms >= back_ms + 30) {
+		ksft_print_msg("T ran again at %lld ms, not at its next period at %d\n",
+			       (long long)shorten_back_ms, back_ms);
+		goto out;
+	}
+	if (klog_has(DL_LAG_MSG) == 1) {
+		ksft_print_msg("the refill logged \"%s\"\n", DL_LAG_MSG);
+		goto out;
+	}
+	ret = KSFT_PASS;
+out:
+	sem_destroy(&shorten_ready);
+	return ret;
+}
+
+/*
+ * Shorten the period of a throttled task: shorten_case() with a change at
+ * 30ms to a period of 50ms. T then owes 5ms, repaid at 20% in 25ms. T
+ * must run again at 55ms.
+ */
+static int period_shrunk_throttled(void)
+{
+	return shorten_case(30, 50, 55);
+}
+
+/*
+ * Shorten the period of a throttled task, later in its period:
+ * shorten_case() with a change at 60ms to a period of 20ms. T then owes
+ * 2ms, repaid at 50% in 4ms. T must run again at 64ms.
+ */
+static int period_shrunk_throttled_started(void)
+{
+	return shorten_case(60, 20, 64);
+}
+
+/* The kinds of period_rule_case() */
+enum { PR_SHRUNK, PR_GROWN, PR_UNTOUCHED, PR_RAISED };
+
+static sem_t pr_h_ready, pr_h_go;
+static atomic_int pr_h_err;
+static int pr_kind;
+static _Atomic long long pr_pre_cpu_ms, pr_mark_ms, pr_done_ms;
+
+/*
+ * Run as H of period_rule_case(). Become a deadline task with a deadline
+ * earlier than T's, and wait for pr_h_go. Then keep the CPU: 5ms with
+ * PR_UNTOUCHED, 60ms with PR_RAISED. Return NULL, or 1 on an error.
+ */
+static void *pr_h(void *arg)
+{
+	int err;
+
+	if (pr_kind == PR_UNTOUCHED)
+		err = set_constrained(0, 6 * NSEC_PER_MSEC, 10 * NSEC_PER_MSEC, PERIOD);
+	else
+		err = set_constrained(0, 62 * NSEC_PER_MSEC, 65 * NSEC_PER_MSEC, PERIOD);
+	pr_h_err = err;
+	sem_post(&pr_h_ready);
+	if (err) {
+		ksft_print_msg("H refused as a deadline task: %s\n", strerror(-err));
+		return (void *)1;
+	}
+	sem_wait_sig(&pr_h_go);
+	spin_ns((pr_kind == PR_UNTOUCHED ? 5 : 60) * NSEC_PER_MSEC);
+	if (set_other(0))
+		ksft_print_msg("H cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Run as T of period_rule_case(). The comments of the cases have the
+ * steps. Store the times of T's marks in pr_mark_ms and pr_done_ms, and
+ * the CPU time T used before its change in pr_pre_cpu_ms. Return NULL, or
+ * 1 on an error.
+ */
+static void *pr_t(void *arg)
+{
+	uint64_t ms = NSEC_PER_MSEC, t0, c0;
+	int err;
+
+	if (pr_kind == PR_RAISED)
+		err = set_deadline(0, 10 * ms, PERIOD);
+	else
+		err = set_constrained(0, 10 * ms, 20 * ms,
+				      pr_kind == PR_SHRUNK ? PERIOD : PERIOD / 2);
+	if (err) {
+		ksft_print_msg("T refused as a deadline task: %s\n", strerror(-err));
+		sem_post(&pr_h_go);
+		return (void *)1;
+	}
+	t0 = now_ns();
+	c0 = thread_time_ns();
+	switch (pr_kind) {
+	case PR_SHRUNK:
+	case PR_GROWN:
+		spin_ns(3 * ms);
+		break;
+	case PR_UNTOUCHED:
+		/* H preempts T here, for 5ms. */
+		sem_post(&pr_h_go);
+		while (now_ns() - t0 < 4 * ms)
+			;
+		break;
+	case PR_RAISED:
+		spin_ns(2 * ms);
+		/* H preempts T here, for 60ms. */
+		sem_post(&pr_h_go);
+		while (now_ns() - t0 < 55 * ms)
+			;
+		break;
+	}
+	pr_pre_cpu_ms = (thread_time_ns() - c0) / ms;
+	if (pr_kind == PR_RAISED)
+		err = set_deadline(0, 15 * ms, PERIOD);
+	else
+		err = set_constrained(0, 10 * ms, 20 * ms,
+				      pr_kind == PR_SHRUNK ? PERIOD / 2 : PERIOD);
+	if (err) {
+		ksft_print_msg("T could not change: %s\n", strerror(-err));
+		set_other(0);
+		return (void *)1;
+	}
+	switch (pr_kind) {
+	case PR_SHRUNK:
+		spin_ns(9 * ms);
+		break;
+	case PR_GROWN:
+		spin_ns(4 * ms);
+		pr_mark_ms = (now_ns() - t0) / ms;
+		spin_ns(5 * ms);
+		break;
+	case PR_UNTOUCHED:
+		spin_ns(9 * ms);
+		break;
+	case PR_RAISED:
+		spin_ns(12 * ms);
+		break;
+	}
+	pr_done_ms = (now_ns() - t0) / ms;
+	if (set_other(0))
+		ksft_print_msg("T cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Run a case where T changes its own parameters while it runs. P has one
+ * CPU. With PR_UNTOUCHED and PR_RAISED, a deadline task H first keeps T
+ * off the CPU: H has the earlier deadline. So T has more runtime left than
+ * a wakeup at that time would give it. H sleeps more than its period
+ * before T wakes it: its wakeup then starts a new period.
+ */
+static int period_rule_case(int kind)
+{
+	int with_h = kind == PR_UNTOUCHED || kind == PR_RAISED;
+	void *h_ret = NULL, *t_ret = NULL;
+	int ret = KSFT_FAIL;
+	pthread_t h, t;
+
+	if (sem_init(&pr_h_ready, 0, 0) || sem_init(&pr_h_go, 0, 0)) {
+		ksft_print_msg("sem_init: %s\n", strerror(errno));
+		return KSFT_FAIL;
+	}
+	pr_kind = kind;
+	pr_pre_cpu_ms = -1;
+	pr_mark_ms = -1;
+	pr_done_ms = -1;
+	if (with_h) {
+		if (pthread_create(&h, NULL, pr_h, NULL)) {
+			ksft_print_msg("the H thread could not start\n");
+			goto out;
+		}
+		if (sem_wait_for(&pr_h_ready, 5000) || pr_h_err) {
+			sem_post(&pr_h_go);
+			join(h);
+			goto out;	/* H said why */
+		}
+		sleep_ns(PERIOD + 10 * NSEC_PER_MSEC);
+	}
+	if (pthread_create(&t, NULL, pr_t, NULL)) {
+		ksft_print_msg("the T thread could not start\n");
+		t_ret = (void *)1;
+	} else {
+		pthread_join(t, &t_ret);
+	}
+	if (with_h) {
+		sem_post(&pr_h_go);	/* if T never woke H */
+		pthread_join(h, &h_ret);
+	}
+	if (t_ret || h_ret || pr_done_ms < 0)
+		goto out;	/* the thread said why */
+	switch (kind) {
+	case PR_SHRUNK:
+		if (pr_done_ms < 45 || pr_done_ms >= 80) {
+			ksft_print_msg("T ended at %lld ms: its next period is at 50\n",
+				       (long long)pr_done_ms);
+			goto out;
+		}
+		break;
+	case PR_GROWN:
+		if (pr_mark_ms >= 30) {
+			ksft_print_msg("T used 7ms of its runtime by %lld ms\n",
+				       (long long)pr_mark_ms);
+			goto out;
+		}
+		if (pr_done_ms < 95 || pr_done_ms >= 130) {
+			ksft_print_msg("T ended at %lld ms: its next period is at 100\n",
+				       (long long)pr_done_ms);
+			goto out;
+		}
+		break;
+	case PR_UNTOUCHED:
+		if (pr_pre_cpu_ms > 2) {
+			ksft_print_msg("T used %lld ms before its change: H did not delay it\n",
+				       (long long)pr_pre_cpu_ms);
+			goto out;
+		}
+		if (pr_done_ms < 95 || pr_done_ms >= 130) {
+			ksft_print_msg("T ended at %lld ms: its next period is at 100\n",
+				       (long long)pr_done_ms);
+			goto out;
+		}
+		break;
+	case PR_RAISED:
+		if (pr_pre_cpu_ms > 6) {
+			ksft_print_msg("T used %lld ms before its change: H did not delay it\n",
+				       (long long)pr_pre_cpu_ms);
+			goto out;
+		}
+		if (pr_done_ms < 110 || pr_done_ms >= 140) {
+			ksft_print_msg("T ended at %lld ms: its next period is at about 115\n",
+				       (long long)pr_done_ms);
+			goto out;
+		}
+		break;
+	}
+	ret = KSFT_PASS;
+out:
+	sem_destroy(&pr_h_ready);
+	sem_destroy(&pr_h_go);
+	return ret;
+}
+
+/*
+ * Shorten the period of a running task:
+ * 1. T becomes a deadline task of 10ms in 20ms, every 100ms, at 0ms. It
+ *    spins 3ms.
+ * 2. T changes its period to 50ms, and spins 9ms more. It runs out of
+ *    runtime at about 10ms.
+ *
+ * The period in progress keeps its start. So T's next period starts at
+ * 50ms, and T ends its spin at about 52ms. The case fails if T ends
+ * before 45ms, or at 80ms or later.
+ */
+static int period_shrunk_running(void)
+{
+	return period_rule_case(PR_SHRUNK);
+}
+
+/*
+ * Lengthen the period of a running task:
+ * 1. T becomes a deadline task of 10ms in 20ms, every 50ms, at 0ms. It
+ *    spins 3ms.
+ * 2. T changes its period to 100ms, and spins 4ms more. It then spins
+ *    5ms more, and runs out of runtime at about 10ms.
+ *
+ * The job in progress keeps its deadline, at 20ms: T must end its first
+ * 7ms at once. T's next period starts at 100ms, and T ends at about
+ * 102ms. The case fails if T ends its first 7ms at 30ms or later, or if
+ * it ends before 95ms, or at 130ms or later.
+ */
+static int period_grown_running(void)
+{
+	return period_rule_case(PR_GROWN);
+}
+
+/*
+ * Lengthen the period of a task that has not run yet in its period:
+ * 1. H, a deadline task of 6ms in 10ms, every 100ms, sleeps.
+ * 2. T becomes a deadline task of 10ms in 20ms, every 50ms, at 0ms, and
+ *    wakes H. H has the earlier deadline. It runs for 5ms.
+ * 3. T runs from 5ms, with its 10ms of runtime and 15ms to its deadline.
+ *    It changes its period to 100ms, and spins 9ms.
+ *
+ * T keeps its job, with the runtime that the new density allows before
+ * its deadline: 10ms in 20ms over the 15ms left, 7.5ms. Its next period
+ * is its release plus the new period, at 100ms. T runs 7.5ms, waits, and
+ * ends at about 101ms. The case fails if T ends before 95ms, or at 130ms
+ * or later.
+ */
+static int period_grown_untouched(void)
+{
+	return period_rule_case(PR_UNTOUCHED);
+}
+
+/*
+ * Raise the runtime of a task late in its period:
+ * 1. H, a deadline task of 62ms in 65ms, every 100ms, sleeps.
+ * 2. T becomes a deadline task of 10ms every 100ms, at 0ms. It spins 2ms,
+ *    and wakes H. H has the earlier deadline. It runs for 60ms.
+ * 3. T runs from about 62ms, with 8ms left. It raises its runtime to
+ *    15ms, and spins 12ms.
+ *
+ * T used 2ms by 62ms, less than its share: it has no debt. It keeps its
+ * job and its 8ms. Its next period starts once those 8ms are paid at the
+ * new bandwidth, at about 115ms. T runs its 8ms, waits, and ends at about
+ * 119ms. The case fails if T ends before 110ms, or at 140ms or later.
+ */
+static int runtime_raised_waited(void)
+{
+	return period_rule_case(PR_RAISED);
+}
+
+static atomic_int flip_stop;
+static _Atomic pid_t flip_tid;
+static _Atomic long long flip_cpu_ms;
+static sem_t flip_ready;
+
+/*
+ * Run as T of params_flip_no_gain(). Become a deadline task of 10ms
+ * every 100ms, and spin until flip_stop. Store the CPU time used in
+ * flip_cpu_ms. Return NULL, or 1 on an error.
+ */
+static void *flip_t(void *arg)
+{
+	uint64_t c0;
+	int err;
+
+	flip_tid = gettid_();
+	err = set_deadline(0, 10 * NSEC_PER_MSEC, PERIOD);
+	sem_post(&flip_ready);
+	if (err) {
+		ksft_print_msg("T refused as a deadline task: %s\n", strerror(-err));
+		return (void *)1;
+	}
+	c0 = thread_time_ns();
+	while (!flip_stop)
+		;
+	flip_cpu_ms = (thread_time_ns() - c0) / NSEC_PER_MSEC;
+	if (set_other(0))
+		ksft_print_msg("T cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Flip the parameters of a task that never sleeps:
+ * 1. T becomes a deadline task of 10ms every 100ms, and spins.
+ * 2. For 500ms, the main thread changes T every 3ms: to 2ms every 20ms,
+ *    then back to 10ms every 100ms. Both are 10% of a CPU. P has one
+ *    CPU, so the main thread mostly runs while T is throttled.
+ *
+ * No change may give T runtime. T must get about 10% of the 500ms, plus
+ * its first 10ms. The case fails if T gets 90ms of CPU time or more, or
+ * if the kernel logs "sched: DL replenish lagged too much". It skips if
+ * that message was logged before: the kernel prints it once.
+ */
+static int params_flip_no_gain(void)
+{
+	void *t_ret = NULL;
+	int err = 0, had, i, ret = KSFT_FAIL;
+	uint64_t end;
+	pthread_t t;
+
+	had = klog_has(DL_LAG_MSG);
+	if (had) {
+		ksft_print_msg(had < 0 ? "the kernel log cannot be read\n" :
+			       "the lag message was logged before this case\n");
+		return KSFT_SKIP;
+	}
+	if (sem_init(&flip_ready, 0, 0)) {
+		ksft_print_msg("sem_init: %s\n", strerror(errno));
+		return KSFT_FAIL;
+	}
+	flip_stop = 0;
+	flip_cpu_ms = -1;
+	if (pthread_create(&t, NULL, flip_t, NULL)) {
+		ksft_print_msg("the T thread could not start\n");
+		goto out;
+	}
+	if (sem_wait_for(&flip_ready, 5000)) {
+		flip_stop = 1;
+		join(t);
+		goto out;
+	}
+	end = now_ns() + 500 * NSEC_PER_MSEC;
+	for (i = 0; !err && now_ns() < end; i++) {
+		sleep_ns(3 * NSEC_PER_MSEC);
+		if (i & 1)
+			err = set_deadline(flip_tid, 10 * NSEC_PER_MSEC, PERIOD);
+		else
+			err = set_deadline(flip_tid, 2 * NSEC_PER_MSEC, PERIOD / 5);
+	}
+	flip_stop = 1;
+	pthread_join(t, &t_ret);
+	if (err) {
+		ksft_print_msg("T could not change: %s\n", strerror(-err));
+		goto out;
+	}
+	if (t_ret || flip_cpu_ms < 0)
+		goto out;	/* T said why */
+	if (flip_cpu_ms >= 90) {
+		ksft_print_msg("T got %lld ms of CPU time in 500ms, with 10%% of a CPU\n",
+			       (long long)flip_cpu_ms);
+		goto out;
+	}
+	if (klog_has(DL_LAG_MSG) == 1) {
+		ksft_print_msg("a refill logged \"%s\"\n", DL_LAG_MSG);
+		goto out;
+	}
+	ret = KSFT_PASS;
+out:
+	sem_destroy(&flip_ready);
+	return ret;
+}
+
+static sem_t vic_b_ready, vic_go;
+static atomic_int vic_stop, vic_b_err;
+static _Atomic unsigned long long vic_t0;
+static _Atomic long long vic_b_done_ms;
+
+/*
+ * Run as B of period_shrunk_no_burst(). Become a deadline task of 40ms
+ * every 100ms, and wait for vic_go. Then spin 39ms, and store in
+ * vic_b_done_ms the time at which the spin ends. Return NULL, or 1 on an
+ * error.
+ */
+static void *vic_b(void *arg)
+{
+	int err = set_deadline(0, 40 * NSEC_PER_MSEC, PERIOD);
+
+	vic_b_err = err;
+	sem_post(&vic_b_ready);
+	if (err) {
+		ksft_print_msg("B refused as a deadline task: %s\n", strerror(-err));
+		return (void *)1;
+	}
+	sem_wait_sig(&vic_go);
+	if (!vic_stop) {
+		spin_ns(39 * NSEC_PER_MSEC);
+		vic_b_done_ms = (now_ns() - vic_t0) / NSEC_PER_MSEC;
+	}
+	if (set_other(0))
+		ksft_print_msg("B cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Run as A of period_shrunk_no_burst(). Its comment has the steps. Return
+ * NULL, or 1 on an error.
+ */
+static void *vic_a(void *arg)
+{
+	int err = set_deadline(0, 40 * NSEC_PER_MSEC, PERIOD);
+
+	if (err) {
+		ksft_print_msg("A refused as a deadline task: %s\n", strerror(-err));
+		vic_stop = 1;
+		sem_post(&vic_go);
+		return (void *)1;
+	}
+	vic_t0 = now_ns();
+	sem_post(&vic_go);
+	spin_ns(39 * NSEC_PER_MSEC + NSEC_PER_MSEC / 2);
+	err = set_deadline(0, 400 * NSEC_PER_USEC, NSEC_PER_MSEC);
+	if (err)
+		ksft_print_msg("A could not change: %s\n", strerror(-err));
+	while (!vic_stop)
+		;
+	if (set_other(0))
+		ksft_print_msg("A cannot leave SCHED_DEADLINE\n");
+	return err ? (void *)1 : NULL;
+}
+
+/*
+ * A task that used its runtime shortens its period, at the same
+ * bandwidth. P has one CPU:
+ * 1. B, a deadline task of 40ms every 100ms, sleeps for more than its
+ *    period.
+ * 2. A becomes a deadline task of 40ms every 100ms, at 0ms, and wakes B.
+ *    B's wakeup starts a period, with a deadline just after A's.
+ * 3. A spins 39.5ms. It then changes to 0.4ms every 1ms, and spins on.
+ * 4. B runs when A is throttled, and spins 39ms.
+ *
+ * A ran about 24ms ahead of its share. Repaid at its new bandwidth, that
+ * debt puts A's next period at about 99ms, and B ends at about 79ms. If the change gave A a
+ * period at once, A would run 0.4ms of every 1ms from 40ms, with
+ * deadlines before B's. B would end at about 105ms, after its deadline.
+ * The case fails if B ends at 95ms or later.
+ */
+static int period_shrunk_no_burst(void)
+{
+	void *a_ret = NULL, *b_ret = NULL;
+	int ret = KSFT_FAIL;
+	pthread_t a, b;
+
+	if (sem_init(&vic_b_ready, 0, 0) || sem_init(&vic_go, 0, 0)) {
+		ksft_print_msg("sem_init: %s\n", strerror(errno));
+		return KSFT_FAIL;
+	}
+	vic_stop = 0;
+	vic_b_done_ms = -1;
+	if (pthread_create(&b, NULL, vic_b, NULL)) {
+		ksft_print_msg("the B thread could not start\n");
+		goto out;
+	}
+	if (sem_wait_for(&vic_b_ready, 5000) || vic_b_err) {
+		vic_stop = 1;
+		sem_post(&vic_go);
+		join(b);
+		goto out;	/* B said why */
+	}
+	sleep_ns(PERIOD + 10 * NSEC_PER_MSEC);
+	if (pthread_create(&a, NULL, vic_a, NULL)) {
+		ksft_print_msg("the A thread could not start\n");
+		vic_stop = 1;
+		sem_post(&vic_go);
+		join(b);
+		goto out;
+	}
+	pthread_join(b, &b_ret);
+	vic_stop = 1;
+	pthread_join(a, &a_ret);
+	if (a_ret || b_ret || vic_b_done_ms < 0)
+		goto out;	/* the thread said why */
+	if (vic_b_done_ms >= 95) {
+		ksft_print_msg("B ended at %lld ms: A ran in new periods before 99ms\n",
+			       (long long)vic_b_done_ms);
+		goto out;
+	}
+	ret = KSFT_PASS;
+out:
+	sem_destroy(&vic_b_ready);
+	sem_destroy(&vic_go);
+	return ret;
+}
+
+static sem_t cmix_b_ready, cmix_go;
+static atomic_int cmix_stop, cmix_b_err;
+static _Atomic unsigned long long cmix_t0;
+static _Atomic long long cmix_b_done_ms;
+
+/*
+ * Run as B of constrained_after_job(). Become a deadline task of 150ms
+ * every 200ms, and wait for cmix_go. Then spin 148ms, and store in
+ * cmix_b_done_ms the time at which the spin ends. Return NULL, or 1 on an
+ * error.
+ */
+static void *cmix_b(void *arg)
+{
+	int err = set_deadline(0, 150 * NSEC_PER_MSEC, 200 * NSEC_PER_MSEC);
+
+	cmix_b_err = err;
+	sem_post(&cmix_b_ready);
+	if (err) {
+		ksft_print_msg("B refused as a deadline task: %s\n", strerror(-err));
+		return (void *)1;
+	}
+	sem_wait_sig(&cmix_go);
+	if (!cmix_stop) {
+		spin_ns(148 * NSEC_PER_MSEC);
+		cmix_b_done_ms = (now_ns() - cmix_t0) / NSEC_PER_MSEC;
+	}
+	if (set_other(0))
+		ksft_print_msg("B cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Run as A of constrained_after_job(). Its comment has the steps. Return
+ * NULL, or 1 on an error.
+ */
+static void *cmix_a(void *arg)
+{
+	int err = set_deadline(0, 15 * NSEC_PER_MSEC, PERIOD);
+
+	if (err) {
+		ksft_print_msg("A refused as a deadline task: %s\n", strerror(-err));
+		cmix_stop = 1;
+		sem_post(&cmix_go);
+		return (void *)1;
+	}
+	cmix_t0 = now_ns();
+	sem_post(&cmix_go);
+	spin_ns(14 * NSEC_PER_MSEC + NSEC_PER_MSEC / 2);
+	err = set_constrained(0, 18 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC,
+			      120 * NSEC_PER_MSEC);
+	if (err)
+		ksft_print_msg("A could not change: %s\n", strerror(-err));
+	while (!cmix_stop)
+		;
+	if (set_other(0))
+		ksft_print_msg("A cannot leave SCHED_DEADLINE\n");
+	return err ? (void *)1 : NULL;
+}
+
+/*
+ * A task that used most of its runtime moves to a constrained deadline,
+ * at the same bandwidth. P has one CPU:
+ * 1. B makes itself a deadline task of 150ms every 200ms. It sleeps for
+ *    more than its period.
+ * 2. A makes itself a deadline task of 15ms every 100ms, at 0ms, and
+ *    wakes B. A's deadline is 100ms. B's wakeup starts a period, with a
+ *    deadline at 200ms.
+ * 3. A spins 14.5ms. It then changes itself to 18ms in 20ms, every
+ *    120ms, and spins on.
+ * 4. A is throttled at 15ms, when its runtime runs out. B then runs, and
+ *    spins 148ms.
+ *
+ * A's job of the old parameters counts as a whole job of the new ones.
+ * It keeps its deadline of 100ms, so it counts as released at
+ * 100 - 20 = 80ms. A's next job then starts at 80 + 120 = 200ms, and B
+ * ends at about 163ms. If A's next job started at 120ms, its deadline at
+ * 140ms would come before B's. A would preempt B for 18ms, and B would
+ * end at about 181ms. The case fails if B ends at 172ms or later.
+ */
+static int constrained_after_job(void)
+{
+	void *a_ret = NULL, *b_ret = NULL;
+	int ret = KSFT_FAIL;
+	pthread_t a, b;
+
+	if (sem_init(&cmix_b_ready, 0, 0) || sem_init(&cmix_go, 0, 0)) {
+		ksft_print_msg("sem_init: %s\n", strerror(errno));
+		return KSFT_FAIL;
+	}
+	cmix_stop = 0;
+	cmix_b_done_ms = -1;
+	if (pthread_create(&b, NULL, cmix_b, NULL)) {
+		ksft_print_msg("the B thread could not start\n");
+		goto out;
+	}
+	if (sem_wait_for(&cmix_b_ready, 5000) || cmix_b_err) {
+		cmix_stop = 1;
+		sem_post(&cmix_go);
+		join(b);
+		goto out;	/* B said why */
+	}
+	sleep_ns(210 * NSEC_PER_MSEC);
+	if (pthread_create(&a, NULL, cmix_a, NULL)) {
+		ksft_print_msg("the A thread could not start\n");
+		cmix_stop = 1;
+		sem_post(&cmix_go);
+		join(b);
+		goto out;
+	}
+	pthread_join(b, &b_ret);
+	cmix_stop = 1;
+	pthread_join(a, &a_ret);
+	if (a_ret || b_ret || cmix_b_done_ms < 0)
+		goto out;	/* the thread said why */
+	if (cmix_b_done_ms >= 172) {
+		ksft_print_msg("B ended at %lld ms: A's next job came before 200ms\n",
+			       (long long)cmix_b_done_ms);
+		goto out;
+	}
+	ret = KSFT_PASS;
+out:
+	sem_destroy(&cmix_b_ready);
+	sem_destroy(&cmix_go);
+	return ret;
+}
+
+static _Atomic long long grow_back_ms;
+
+/*
+ * Run as T of deadline_grows_running(). Its comment has the steps. Store
+ * in grow_back_ms how late T runs after its sleep. Return NULL, or 1 on
+ * an error.
+ */
+static void *grow_t(void *arg)
+{
+	struct timespec ts;
+	uint64_t end;
+	int err;
+
+	err = set_constrained(0, 10 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, PERIOD);
+	if (!err) {
+		sched_yield();
+		err = set_constrained(0, 10 * NSEC_PER_MSEC, 50 * NSEC_PER_MSEC,
+				      PERIOD);
+	}
+	if (err) {
+		ksft_print_msg("T refused as a deadline task: %s\n", strerror(-err));
+		set_other(0);
+		return (void *)1;
+	}
+	sched_yield();
+	end = now_ns() + 30 * NSEC_PER_MSEC;
+	ts.tv_sec = end / NSEC_PER_SEC;
+	ts.tv_nsec = end % NSEC_PER_SEC;
+	while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR)
+		;
+	grow_back_ms = (now_ns() - end) / NSEC_PER_MSEC;
+	if (set_other(0))
+		ksft_print_msg("T cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Lengthen the relative deadline of a task that its replenishment timer
+ * refills:
+ * 1. T becomes a deadline task of 10ms in 20ms, every 100ms, and yields.
+ *    The timer refills it at the start of its next period.
+ * 2. T changes itself to 10ms in 50ms, every 100ms, and yields again.
+ * 3. The timer refills T at the start of its next period. T then sleeps
+ *    30ms. It wakes up before its new deadline, 50ms into the period.
+ *
+ * T must run at once. With the old relative deadline, its deadline would
+ * be 20ms into the period, and the wakeup would throttle T until the next
+ * period. The case fails if T runs 20ms or more after its wakeup.
+ */
+static int deadline_grows_running(void)
+{
+	void *t_ret = NULL;
+	pthread_t t;
+
+	grow_back_ms = -1;
+	if (pthread_create(&t, NULL, grow_t, NULL)) {
+		ksft_print_msg("the T thread could not start\n");
+		return KSFT_FAIL;
+	}
+	pthread_join(t, &t_ret);
+	if (t_ret || grow_back_ms < 0)
+		return KSFT_FAIL;	/* T said why */
+	if (grow_back_ms >= 20) {
+		ksft_print_msg("T ran %lld ms after its wakeup\n",
+			       (long long)grow_back_ms);
+		return KSFT_FAIL;
+	}
+	return KSFT_PASS;
+}
+
+static sem_t gap_h_ready, gap_h_go, gap_h_end;
+static atomic_int gap_h_err;
+static _Atomic long long gap_used_ms, gap_back_ms;
+
+/*
+ * Run as H of constrained_gap_change_running(). Become a deadline task of
+ * 40ms in 45ms, every 100ms, and wait for gap_h_go. Then spin for 35ms of
+ * CPU time, and wait for gap_h_end. Return NULL, or 1 on an error.
+ */
+static void *gap_h(void *arg)
+{
+	int err = set_constrained(0, 40 * NSEC_PER_MSEC, 45 * NSEC_PER_MSEC, PERIOD);
+
+	gap_h_err = err;
+	sem_post(&gap_h_ready);
+	if (err) {
+		ksft_print_msg("H refused as a deadline task: %s\n", strerror(-err));
+		return (void *)1;
+	}
+	sem_wait_sig(&gap_h_go);
+	spin_ns(35 * NSEC_PER_MSEC);
+	sem_wait_sig(&gap_h_end);
+	if (set_other(0))
+		ksft_print_msg("H cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Run as T of constrained_gap_change_running(). Its comment has the
+ * steps. Store in gap_used_ms the CPU time T used until 55ms, and in
+ * gap_back_ms the time at which T runs again after its change. Return
+ * NULL, or 1 on an error.
+ */
+static void *gap_t(void *arg)
+{
+	uint64_t t0, c0;
+	int err;
+
+	err = set_constrained(0, 40 * NSEC_PER_MSEC, 50 * NSEC_PER_MSEC, PERIOD);
+	if (err) {
+		ksft_print_msg("T refused as a deadline task: %s\n", strerror(-err));
+		return (void *)1;
+	}
+	t0 = now_ns();
+	c0 = thread_time_ns();
+	sem_post(&gap_h_go);
+	while (now_ns() - t0 < 55 * NSEC_PER_MSEC)
+		;
+	gap_used_ms = (thread_time_ns() - c0) / NSEC_PER_MSEC;
+	err = set_constrained(0, 39 * NSEC_PER_MSEC, 50 * NSEC_PER_MSEC, PERIOD);
+	gap_back_ms = (now_ns() - t0) / NSEC_PER_MSEC;
+	if (err)
+		ksft_print_msg("T could not change: %s\n", strerror(-err));
+	if (set_other(0))
+		ksft_print_msg("T cannot leave SCHED_DEADLINE\n");
+	return err ? (void *)1 : NULL;
+}
+
+/*
+ * Change the parameters of a running task past its deadline, before its
+ * next period. P has one CPU:
+ * 1. H, a deadline task of 40ms in 45ms, every 100ms, waits.
+ * 2. T becomes a deadline task of 40ms in 50ms, every 100ms, and wakes H.
+ *    H has the earlier deadline. It runs for 35ms.
+ * 3. T runs from 35ms. At 50ms it is past its deadline, with runtime
+ *    left.
+ * 4. At 55ms, T changes its runtime to 39ms. The enqueue of the change
+ *    throttles it until its next period, at 100ms.
+ *
+ * T must then leave the CPU until 100ms. Its CPU time does not show it:
+ * a throttled task that keeps running is not charged. So T reads the time
+ * when its change returns. The case fails before 90ms.
+ */
+static int constrained_gap_change_running(void)
+{
+	void *h_ret = NULL, *t_ret = NULL;
+	int ret = KSFT_FAIL;
+	pthread_t h, t;
+
+	if (sem_init(&gap_h_ready, 0, 0) || sem_init(&gap_h_go, 0, 0) ||
+	    sem_init(&gap_h_end, 0, 0)) {
+		ksft_print_msg("sem_init: %s\n", strerror(errno));
+		return KSFT_FAIL;
+	}
+	gap_used_ms = -1;
+	gap_back_ms = -1;
+	if (pthread_create(&h, NULL, gap_h, NULL)) {
+		ksft_print_msg("the H thread could not start\n");
+		goto out;
+	}
+	if (sem_wait_for(&gap_h_ready, 5000) || gap_h_err) {
+		sem_post(&gap_h_go);
+		sem_post(&gap_h_end);
+		join(h);
+		goto out;	/* H said why */
+	}
+	if (pthread_create(&t, NULL, gap_t, NULL)) {
+		ksft_print_msg("the T thread could not start\n");
+		sem_post(&gap_h_go);
+		sem_post(&gap_h_end);
+		join(h);
+		goto out;
+	}
+	pthread_join(t, &t_ret);
+	sem_post(&gap_h_go);	/* if T never woke H */
+	sem_post(&gap_h_end);
+	pthread_join(h, &h_ret);
+	if (t_ret || h_ret || gap_back_ms < 0)
+		goto out;	/* the thread said why */
+	if (gap_used_ms > 30) {
+		ksft_print_msg("T used %lld ms of its first 55: H did not delay it\n",
+			       (long long)gap_used_ms);
+		goto out;
+	}
+	if (gap_back_ms < 90) {
+		ksft_print_msg("T ran again at %lld ms, throttled until 100\n",
+			       (long long)gap_back_ms);
+		goto out;
+	}
+	ret = KSFT_PASS;
+out:
+	sem_destroy(&gap_h_ready);
+	sem_destroy(&gap_h_go);
+	sem_destroy(&gap_h_end);
+	return ret;
+}
+
+/*
+ * Return the absolute deadline of the deadline task @tid, in ns of the rq
+ * clock, from dl.deadline in /proc/<tid>/sched. Return -1 if it cannot be
+ * read.
+ */
+static long long dl_deadline_of(pid_t tid)
+{
+	char path[64], line[256];
+	long long d = -1;
+	FILE *f;
+
+	snprintf(path, sizeof(path), "/proc/%d/sched", tid);
+	f = fopen(path, "r");
+	if (!f)
+		return -1;
+	while (fgets(line, sizeof(line), f)) {
+		char *val = strchr(line, ':');
+
+		if (!strncmp(line, "dl.deadline", 11) && val) {
+			d = atoll(val + 1);
+			break;
+		}
+	}
+	fclose(f);
+	return d;
+}
+
+/* Milliseconds from @from to @to, both from now_ns(). */
+#define MS_FROM(from, to)	((long long)((int64_t)((to) - (from)) / (int64_t)NSEC_PER_MSEC))
+
+static _Atomic uint32_t rb2_pi;
+static atomic_int rb2_t_locking;
+static _Atomic pid_t rb2_t_tid;
+static _Atomic uint64_t rb2_t_start, rb2_t_got;
+
+/*
+ * Run as T of reenter_pi_blocked_in_gap(). Become a deadline task of 10ms
+ * in 20ms, every 500ms. Spin 2ms of CPU time, then take rb2_pi with
+ * FUTEX_LOCK_PI. Once T holds it, store the time and unlock. Return NULL,
+ * or 1 on an error.
+ */
+static void *rb2_t(void *arg)
+{
+	uint32_t tid = gettid_();
+	int err;
+
+	rb2_t_tid = tid;
+	err = set_constrained(0, 10 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, 5 * PERIOD);
+	rb2_t_start = now_ns();
+	if (err) {
+		ksft_print_msg("T refused as a deadline task: %s\n", strerror(-err));
+		rb2_t_locking = -1;
+		return (void *)1;
+	}
+	spin_ns(2 * NSEC_PER_MSEC);
+	rb2_t_locking = 1;
+	if (syscall(SYS_futex, &rb2_pi, FUTEX_LOCK_PI | FUTEX_PRIVATE_FLAG,
+		    0, NULL, NULL, 0)) {
+		ksft_print_msg("FUTEX_LOCK_PI: %s\n", strerror(errno));
+		set_other(0);
+		return (void *)1;
+	}
+	rb2_t_got = now_ns();
+	if (!atomic_compare_exchange_strong(&rb2_pi, &tid, 0))
+		syscall(SYS_futex, &rb2_pi, FUTEX_UNLOCK_PI | FUTEX_PRIVATE_FLAG,
+			0, NULL, NULL, 0);
+	if (set_other(0))
+		ksft_print_msg("T cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * A task blocked on an rt_mutex leaves the deadline class in its
+ * constrained gap and comes back. The main thread holds the PI futex
+ * rb2_pi. P has one CPU:
+ * 1. T becomes a deadline task of 10ms in 20ms, every 500ms, at 0ms. It
+ *    spins 2ms and blocks on rb2_pi. Its deadline is 20ms.
+ * 2. At 40ms, T is past its deadline and before its next period, at
+ *    500ms. The main thread switches T to SCHED_OTHER, then back to
+ *    SCHED_DEADLINE with the same parameters. T still waits on rb2_pi.
+ * 3. At about 45ms, the main thread unlocks rb2_pi.
+ *
+ * T used 2ms of its 10ms. Past its deadline, it gets no more runtime in
+ * that period: a wakeup would throttle it until 500ms. Leaving the class
+ * and coming back must not give it a new period before then. The case
+ * fails if T gets rb2_pi before 490ms.
+ */
+static int reenter_pi_blocked_in_gap(void)
+{
+	long long dl_gap = -1, dl_back = -1;
+	uint64_t back_at = 0, unlock_at;
+	int ret = KSFT_FAIL, err;
+	void *t_ret = NULL;
+	pthread_t t;
+
+	rb2_pi = gettid_();
+	rb2_t_locking = 0;
+	rb2_t_tid = 0;
+	rb2_t_start = 0;
+	rb2_t_got = 0;
+	if (pthread_create(&t, NULL, rb2_t, NULL)) {
+		ksft_print_msg("the T thread could not start\n");
+		return KSFT_FAIL;
+	}
+	if (wait_for(1000, rb2_t_locking &&
+		     (rb2_t_locking < 0 || (proc_state_of(rb2_t_tid) == 'S' &&
+					    (rb2_pi & FUTEX_WAITERS)))) ||
+	    rb2_t_locking < 0) {
+		ksft_print_msg("T never blocked on rb2_pi\n");
+		goto out;
+	}
+	while (now_ns() - rb2_t_start < 40 * NSEC_PER_MSEC)
+		sleep_ns(NSEC_PER_MSEC);
+	dl_gap = dl_deadline_of(rb2_t_tid);
+	err = set_other(rb2_t_tid);
+	if (err) {
+		ksft_print_msg("T cannot leave SCHED_DEADLINE: %s\n", strerror(-err));
+		goto out;
+	}
+	err = set_constrained(rb2_t_tid, 10 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, 5 * PERIOD);
+	back_at = now_ns();
+	if (err) {
+		ksft_print_msg("T refused back as a deadline task: %s\n", strerror(-err));
+		goto out;
+	}
+	dl_back = dl_deadline_of(rb2_t_tid);
+	if (proc_state_of(rb2_t_tid) != 'S' || !(rb2_pi & FUTEX_WAITERS)) {
+		ksft_print_msg("T no longer waits on rb2_pi\n");
+		goto out;
+	}
+	sleep_ns(5 * NSEC_PER_MSEC);
+	ret = KSFT_PASS;
+out:
+	unlock_at = now_ns();
+	{
+		uint32_t tid = gettid_();
+
+		if (!atomic_compare_exchange_strong(&rb2_pi, &tid, 0))
+			syscall(SYS_futex, &rb2_pi, FUTEX_UNLOCK_PI | FUTEX_PRIVATE_FLAG,
+				0, NULL, NULL, 0);
+	}
+	if (pthread_join(t, &t_ret) || t_ret || ret != KSFT_PASS)
+		return KSFT_FAIL;	/* the thread said why */
+	ksft_print_msg("T back at %lld ms, deadline moved by %lld ms; next period at 500 ms\n",
+		       MS_FROM(rb2_t_start, back_at),
+		       dl_back < 0 || dl_gap < 0 ? -1LL :
+		       (dl_back - dl_gap) / (long long)NSEC_PER_MSEC);
+	ksft_print_msg("(0: kept, %lld: new period); unlock %lld ms; T got the lock %lld ms\n",
+		       MS_FROM(rb2_t_start, back_at),
+		       MS_FROM(rb2_t_start, unlock_at), MS_FROM(rb2_t_start, rb2_t_got));
+	if (MS_FROM(rb2_t_start, rb2_t_got) < 490) {
+		ksft_print_msg("T ran before its next period\n");
+		return KSFT_FAIL;
+	}
+	return KSFT_PASS;
+}
+
+static int ra_fd[2];
+static atomic_int ra_t_asleep;
+static _Atomic pid_t ra_t_tid;
+static _Atomic uint64_t ra_t_start, ra_t_run;
+
+/*
+ * Run as T of reenter_asleep_case(). Become a deadline task of 10ms in
+ * 20ms, every 500ms. Spin 2ms of CPU time, then sleep in a read of
+ * ra_fd[0]. Once woken, store the time and leave SCHED_DEADLINE. Return
+ * NULL, or 1 on an error.
+ */
+static void *ra_t(void *arg)
+{
+	char c;
+	int err;
+
+	ra_t_tid = gettid_();
+	err = set_constrained(0, 10 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, 5 * PERIOD);
+	ra_t_start = now_ns();
+	if (err) {
+		ksft_print_msg("T refused as a deadline task: %s\n", strerror(-err));
+		ra_t_asleep = -1;
+		return (void *)1;
+	}
+	spin_ns(2 * NSEC_PER_MSEC);
+	ra_t_asleep = 1;
+	if (read(ra_fd[0], &c, 1) != 1) {
+		ksft_print_msg("T: read: %s\n", strerror(errno));
+		set_other(0);
+		return (void *)1;
+	}
+	ra_t_run = now_ns();
+	if (set_other(0))
+		ksft_print_msg("T cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * The same as reenter_pi_blocked_in_gap(), without the rt_mutex. P has
+ * one CPU:
+ * 1. T becomes a deadline task of 10ms in 20ms, every 500ms, at 0ms. It
+ *    spins 2ms and sleeps in a read of a pipe, with 8ms left. Its
+ *    deadline is 20ms. Its 0-lag time is 100ms.
+ * 2. At @at_ms, T is past its deadline and before its next period, at
+ *    500ms. The main thread switches T to SCHED_OTHER, then back to
+ *    SCHED_DEADLINE with the same parameters.
+ * 3. About 5ms later, the main thread wakes T up.
+ *
+ * Leaving the class and coming back must not give T a new period before
+ * 500ms. The case fails if T runs after its wakeup before 490ms.
+ */
+static int reenter_asleep_case(int at_ms)
+{
+	long long dl_gap = -1, dl_back = -1;
+	uint64_t back_at = 0, wake_at = 0;
+	int ret = KSFT_FAIL, err;
+	void *t_ret = NULL;
+	pthread_t t;
+
+	if (pipe(ra_fd)) {
+		ksft_print_msg("pipe: %s\n", strerror(errno));
+		return KSFT_FAIL;
+	}
+	ra_t_asleep = 0;
+	ra_t_tid = 0;
+	ra_t_start = 0;
+	ra_t_run = 0;
+	if (pthread_create(&t, NULL, ra_t, NULL)) {
+		ksft_print_msg("the T thread could not start\n");
+		goto close;
+	}
+	if (wait_for(1000, ra_t_asleep &&
+		     (ra_t_asleep < 0 || proc_state_of(ra_t_tid) == 'S')) ||
+	    ra_t_asleep < 0) {
+		ksft_print_msg("T never slept\n");
+		goto out;
+	}
+	while (now_ns() - ra_t_start < (uint64_t)at_ms * NSEC_PER_MSEC)
+		sleep_ns(NSEC_PER_MSEC);
+	dl_gap = dl_deadline_of(ra_t_tid);
+	err = set_other(ra_t_tid);
+	if (err) {
+		ksft_print_msg("T cannot leave SCHED_DEADLINE: %s\n", strerror(-err));
+		goto out;
+	}
+	err = set_constrained(ra_t_tid, 10 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, 5 * PERIOD);
+	back_at = now_ns();
+	if (err) {
+		ksft_print_msg("T refused back as a deadline task: %s\n", strerror(-err));
+		goto out;
+	}
+	dl_back = dl_deadline_of(ra_t_tid);
+	if (proc_state_of(ra_t_tid) != 'S') {
+		ksft_print_msg("T is no longer asleep\n");
+		goto out;
+	}
+	sleep_ns(5 * NSEC_PER_MSEC);
+	ret = KSFT_PASS;
+out:
+	wake_at = now_ns();
+	if (write(ra_fd[1], "x", 1) != 1)
+		ksft_print_msg("write: %s\n", strerror(errno));
+	if (pthread_join(t, &t_ret) || t_ret)
+		ret = KSFT_FAIL;	/* the thread said why */
+close:
+	close(ra_fd[0]);
+	close(ra_fd[1]);
+	if (ret != KSFT_PASS)
+		return KSFT_FAIL;
+	ksft_print_msg("T back at %lld ms, deadline %s%lld ms; next period at 500 ms\n",
+		       MS_FROM(ra_t_start, back_at),
+		       dl_back == 0 ? "is 0, moved by " : "moved by ",
+		       dl_back < 0 || dl_gap < 0 ? -1LL :
+		       (dl_back - dl_gap) / (long long)NSEC_PER_MSEC);
+	ksft_print_msg("T woken at %lld ms, ran at %lld ms\n",
+		       MS_FROM(ra_t_start, wake_at), MS_FROM(ra_t_start, ra_t_run));
+	if (MS_FROM(ra_t_start, ra_t_run) < 490) {
+		ksft_print_msg("T ran before its next period\n");
+		return KSFT_FAIL;
+	}
+	return KSFT_PASS;
+}
+
+/* Run reenter_asleep_case() before T's 0-lag time. */
+static int reenter_asleep_in_gap(void)
+{
+	return reenter_asleep_case(40);
+}
+
+/* Run reenter_asleep_case() after T's 0-lag time, before its next period. */
+static int reenter_asleep_late_in_gap(void)
+{
+	return reenter_asleep_case(150);
+}
+
+static atomic_int rr_t_ready, rr_stop;
+static _Atomic pid_t rr_t_tid;
+static _Atomic uint64_t rr_t_start, rr_back_at, rr_t_ran;
+
+/*
+ * Run as T of reenter_runnable_in_gap(). Become a deadline task of 10ms in
+ * 20ms, every 500ms, and spin until rr_stop. Store in rr_t_ran the first
+ * time T runs more than 2ms after rr_back_at. The 2ms leave out the time T
+ * may run as SCHED_OTHER before it is back. Return NULL, or 1 on an
+ * error.
+ */
+static void *rr_t(void *arg)
+{
+	int err;
+
+	rr_t_tid = gettid_();
+	err = set_constrained(0, 10 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, 5 * PERIOD);
+	rr_t_start = now_ns();
+	if (err) {
+		ksft_print_msg("T refused as a deadline task: %s\n", strerror(-err));
+		rr_t_ready = -1;
+		return (void *)1;
+	}
+	rr_t_ready = 1;
+	while (!rr_stop) {
+		uint64_t t = now_ns(), b = rr_back_at;
+
+		if (b && !rr_t_ran && t > b + 2 * NSEC_PER_MSEC)
+			rr_t_ran = t;
+	}
+	return NULL;
+}
+
+/*
+ * The same as reenter_asleep_in_gap(), for a task that stays runnable. P
+ * has one CPU:
+ * 1. T becomes a deadline task of 10ms in 20ms, every 500ms, at 0ms, and
+ *    spins. It is throttled at 10ms, until its next period at 500ms.
+ * 2. At 40ms, the main thread switches T to SCHED_OTHER, then back to
+ *    SCHED_DEADLINE with the same parameters.
+ *
+ * Leaving the class and coming back must not give T a new period before
+ * 500ms. With a new period, T would run at once. The case fails if T runs
+ * from 2ms to 50ms after rr_back_at.
+ */
+static int reenter_runnable_in_gap(void)
+{
+	int ret = KSFT_FAIL, err;
+	void *t_ret = NULL;
+	pthread_t t;
+
+	rr_t_ready = 0;
+	rr_stop = 0;
+	rr_t_tid = 0;
+	rr_t_start = 0;
+	rr_back_at = 0;
+	rr_t_ran = 0;
+	if (pthread_create(&t, NULL, rr_t, NULL)) {
+		ksft_print_msg("the T thread could not start\n");
+		return KSFT_FAIL;
+	}
+	if (wait_for(1000, rr_t_ready) || rr_t_ready < 0) {
+		ksft_print_msg("T never started\n");
+		goto out;
+	}
+	while (now_ns() - rr_t_start < 40 * NSEC_PER_MSEC)
+		sleep_ns(NSEC_PER_MSEC);
+	err = set_other(rr_t_tid);
+	if (err) {
+		ksft_print_msg("T cannot leave SCHED_DEADLINE: %s\n", strerror(-err));
+		goto out;
+	}
+	/* Store it before the switch back. A T given a new period runs at once. */
+	rr_back_at = now_ns();
+	err = set_constrained(rr_t_tid, 10 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, 5 * PERIOD);
+	if (err) {
+		ksft_print_msg("T refused back as a deadline task: %s\n", strerror(-err));
+		goto out;
+	}
+	sleep_ns(50 * NSEC_PER_MSEC);
+	ret = KSFT_PASS;
+out:
+	set_other(rr_t_tid);
+	rr_stop = 1;
+	if (pthread_join(t, &t_ret) || t_ret)
+		ret = KSFT_FAIL;
+	/*
+	 * T left the class in its constrained gap, throttled. It keeps its
+	 * bandwidth until its 0-lag time: its next period, 5 * PERIOD after
+	 * its start, plus its overrun paid back at 10ms every 5 * PERIOD. An
+	 * overrun of a 10ms tick adds 5 * PERIOD. Wait for it, so that
+	 * measured() finds the bandwidth released.
+	 */
+	if (rr_t_start) {
+		uint64_t end = rr_t_start + 10 * PERIOD + 50 * NSEC_PER_MSEC;
+		uint64_t now = now_ns();
+
+		if (now < end)
+			sleep_ns(end - now);
+	}
+	if (ret != KSFT_PASS)
+		return KSFT_FAIL;
+	ksft_print_msg("T back at %lld ms, ran again at %lld ms\n",
+		       MS_FROM(rr_t_start, rr_back_at),
+		       rr_t_ran ? MS_FROM(rr_t_start, rr_t_ran) : -1LL);
+	if (rr_t_ran && rr_t_ran < rr_back_at + 50 * NSEC_PER_MSEC) {
+		ksft_print_msg("T ran before its next period\n");
+		return KSFT_FAIL;
+	}
+	return KSFT_PASS;
+}
+
+static sem_t rb_donor_go;
+static atomic_int rb_abort, rb_donor_locking, rb_donor_done, rb_waiter_locking;
+static _Atomic pid_t rb_waiter_tid;
+static _Atomic long long rb_spin_ns;
+
+/*
+ * Run as the owner O of deboost_new_params_reboost(). Its comment has
+ * the steps. Store in rb_spin_ns the time that O's last spin, 20ms of CPU
+ * time, takes. Return NULL, or 1 on an error.
+ */
+static void *reboost_owner(void *arg)
+{
+	uint64_t t0;
+	int err;
+
+	owner_tid = gettid_();
+	if (take_pi_lock())
+		return (void *)1;
+	owner_holds = 2;
+	sem_wait_sig(&owner_go);
+	if (rb_abort) {
+		drop_pi_lock();
+		return NULL;
+	}
+	err = set_deadline(0, 50 * NSEC_PER_MSEC, PERIOD);
+	if (err) {
+		ksft_print_msg("the owner refused as a deadline task: %s\n",
+			       strerror(-err));
+		goto out;
+	}
+	sem_post(&rb_donor_go);
+	t0 = now_ns();
+	while (!rb_donor_locking || proc_state_of(donor_tid) != 'S') {
+		if (now_ns() - t0 > NSEC_PER_SEC) {
+			ksft_print_msg("the donor did not block on the lock\n");
+			err = -1;
+			goto out;
+		}
+	}
+	err = set_deadline(rb_waiter_tid, 2 * NSEC_PER_MSEC, PERIOD);
+	if (err) {
+		ksft_print_msg("the waiter refused as a deadline task: %s\n",
+			       strerror(-err));
+		goto out;
+	}
+	err = set_deadline(0, 5 * NSEC_PER_MSEC, PERIOD);
+	if (err) {
+		ksft_print_msg("the owner could not change: %s\n",
+			       strerror(-err));
+		goto out;
+	}
+	t0 = now_ns();
+	while (!rb_donor_done) {
+		if (now_ns() - t0 > NSEC_PER_SEC) {
+			ksft_print_msg("the donor did not time out\n");
+			err = -1;
+			goto out;
+		}
+	}
+	t0 = now_ns();
+	spin_ns(20 * NSEC_PER_MSEC);
+	rb_spin_ns = now_ns() - t0;
+out:
+	drop_pi_lock();
+	if (set_other(0))
+		ksft_print_msg("the owner cannot leave SCHED_DEADLINE\n");
+	return err ? (void *)1 : NULL;
+}
+
+/*
+ * Run as the donor D of deboost_new_params_reboost(). Become a deadline
+ * task of 5ms every 50ms, and sleep until rb_donor_go. Then wait 20ms for
+ * pi_lock. Return NULL if the wait timed out or the case gave up, or 1
+ * otherwise.
+ */
+static void *reboost_donor(void *arg)
+{
+	struct timespec ts;
+	int err;
+
+	err = set_deadline(0, 5 * NSEC_PER_MSEC, PERIOD / 2);
+	if (err) {
+		ksft_print_msg("the donor refused as a deadline task: %s\n",
+			       strerror(-err));
+		donor_tid = -1;
+		return (void *)1;
+	}
+	donor_tid = gettid_();
+	sem_wait_sig(&rb_donor_go);
+	if (rb_abort) {
+		set_other(0);
+		return NULL;
+	}
+	clock_gettime(CLOCK_REALTIME, &ts);
+	ts.tv_nsec += 20 * 1000000L;
+	ts.tv_sec += ts.tv_nsec / 1000000000L;
+	ts.tv_nsec %= 1000000000L;
+	rb_donor_locking = 1;
+	err = pthread_mutex_timedlock(&pi_lock, &ts);
+	rb_donor_done = 1;
+	if (!err)
+		pthread_mutex_unlock(&pi_lock);
+	if (set_other(0))
+		ksft_print_msg("the donor cannot leave SCHED_DEADLINE\n");
+	if (err != ETIMEDOUT) {
+		ksft_print_msg("the donor: %s, not a timeout\n",
+			       err ? strerror(err) : "got pi_lock");
+		return (void *)1;
+	}
+	return NULL;
+}
+
+/*
+ * Run as the waiter W of deboost_new_params_reboost(). Wait for pi_lock
+ * as a fair task. The owner makes W a deadline task during the wait.
+ * Return NULL, or 1 on an error.
+ */
+static void *reboost_waiter(void *arg)
+{
+	rb_waiter_tid = gettid_();
+	rb_waiter_locking = 1;
+	if (take_pi_lock())
+		return (void *)1;
+	drop_pi_lock();
+	if (set_other(0))
+		ksft_print_msg("the waiter cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+static int reboost_case(void);
+
+/*
+ * Check that a deboost boosts the owner again when the check of its new
+ * parameters moves its deadline after the one of its top waiter. The
+ * threads share the CPU of P:
+ *
+ *   owner O                     donor D               waiter W
+ *   fair                        5ms every 50ms        fair
+ *
+ *   takes pi_lock               sleeps
+ *                                                     blocks on pi_lock
+ *   becomes 50ms every 100ms
+ *   wakes D up
+ *                               preempts O
+ *                               waits 20ms for
+ *                                 pi_lock, boosts O
+ *   makes W 2ms every 100ms
+ *   changes itself to 5ms
+ *     every 100ms
+ *   spins
+ *                               times out: W is now
+ *                                 the top waiter
+ *   spins 20ms
+ *   unlocks
+ *                                                     takes pi_lock
+ *
+ * W gets its deadline after O does, so W's deadline is the later one.
+ * W blocks as a fair task: a deadline W could not preempt O to block.
+ *
+ * O has run about 20ms of its 50ms when D times out: 10ms more than its
+ * share. The deboost fits O's period to the new parameters. O keeps 5ms,
+ * and its next period starts once those 5ms and the 10ms of debt are
+ * paid at 5%: its deadline is about 300ms ahead. W's deadline is now the
+ * earlier one. W must boost O again. A boosted O is refilled from W, so
+ * its last spin takes about 20ms. Without the boost, O is throttled once
+ * its 5ms run out, until its new deadline. The case fails if the spin
+ * takes more than 50ms.
+ */
+static int deboost_new_params_reboost(void)
+{
+	return reboost_case();
+}
+
+/* Run deboost_new_params_reboost(). */
+static int reboost_case(void)
+{
+	void *o_ret = NULL, *d_ret = NULL, *w_ret = NULL;
+	int ret = KSFT_FAIL, have_d = 0, have_w = 0;
+	pthread_t o, d, w;
+
+	if (pi_init())
+		return KSFT_SKIP;
+	if (sem_init(&rb_donor_go, 0, 0)) {
+		ksft_print_msg("sem_init: %s\n", strerror(errno));
+		pi_fini();
+		return KSFT_FAIL;
+	}
+	donor_tid = 0;
+	rb_abort = 0;
+	rb_donor_locking = 0;
+	rb_donor_done = 0;
+	rb_waiter_locking = 0;
+	rb_waiter_tid = 0;
+	rb_spin_ns = -1;
+	if (pthread_create(&o, NULL, reboost_owner, NULL)) {
+		ksft_print_msg("the owner thread could not start\n");
+		goto out_fini;
+	}
+	if (wait_for(5000, owner_holds) || owner_holds != 2) {
+		ksft_print_msg("the owner never took pi_lock\n");
+		goto out;
+	}
+	have_d = !pthread_create(&d, NULL, reboost_donor, NULL);
+	if (!have_d || wait_for(1000, donor_tid &&
+				   proc_state_of(donor_tid) == 'S') ||
+	    donor_tid < 0) {
+		ksft_print_msg("the donor never slept\n");
+		goto out;
+	}
+	have_w = !pthread_create(&w, NULL, reboost_waiter, NULL);
+	if (!have_w || wait_for(1000, rb_waiter_locking &&
+				   proc_state_of(rb_waiter_tid) == 'S')) {
+		ksft_print_msg("the waiter never waited on pi_lock\n");
+		goto out;
+	}
+	ret = KSFT_PASS;
+out:
+	/* On a failure, the owner and the donor end without the steps. */
+	rb_abort = ret != KSFT_PASS;
+	sem_post(&owner_go);
+	if (pthread_join(o, &o_ret))
+		ret = KSFT_FAIL;
+	sem_post(&rb_donor_go);
+	if (have_d && pthread_join(d, &d_ret))
+		ret = KSFT_FAIL;
+	if (have_w && pthread_join(w, &w_ret))
+		ret = KSFT_FAIL;
+	if (o_ret || d_ret || w_ret) {
+		ret = KSFT_FAIL;	/* the thread said why */
+	} else if (ret == KSFT_PASS && rb_spin_ns > PERIOD / 2) {
+		ksft_print_msg("the spin took %lld ms: not boosted again\n",
+			       rb_spin_ns / (long long)NSEC_PER_MSEC);
+		ret = KSFT_FAIL;
+	}
+out_fini:
+	sem_destroy(&rb_donor_go);
+	pi_fini();
+	return ret;
+}
+
+static sem_t das_donor_ready, das_owner_locked;
+static atomic_int das_donor_locking;
+static _Atomic pid_t das_donor_tid;
+static _Atomic long long das_back_ms;
+
+/*
+ * Run as the donor D of deboost_asleep_new_params(). Become a deadline
+ * task of 1ms every 10ms. Once O holds pi_lock, wait 150ms for it. Return
+ * NULL if the wait timed out, or 1 otherwise.
+ */
+static void *das_donor(void *arg)
+{
+	struct timespec ts;
+	int err;
+
+	das_donor_tid = gettid_();
+	err = set_deadline(0, NSEC_PER_MSEC, 10 * NSEC_PER_MSEC);
+	sem_post(&das_donor_ready);
+	if (err) {
+		ksft_print_msg("the donor refused as a deadline task: %s\n", strerror(-err));
+		return (void *)1;
+	}
+	if (sem_wait_for(&das_owner_locked, 5000)) {
+		ksft_print_msg("the owner did not take the lock\n");
+		set_other(0);
+		return (void *)1;
+	}
+	clock_gettime(CLOCK_REALTIME, &ts);
+	ts.tv_nsec += 150 * 1000000L;
+	ts.tv_sec += ts.tv_nsec / 1000000000L;
+	ts.tv_nsec %= 1000000000L;
+	das_donor_locking = 1;
+	err = pthread_mutex_timedlock(&pi_lock, &ts);
+	if (!err)
+		pthread_mutex_unlock(&pi_lock);
+	if (set_other(0))
+		ksft_print_msg("the donor cannot leave SCHED_DEADLINE\n");
+	if (err != ETIMEDOUT) {
+		ksft_print_msg("the donor: %s, not a timeout\n",
+			       err ? strerror(err) : "got pi_lock");
+		return (void *)1;
+	}
+	return NULL;
+}
+
+/*
+ * Run as the owner O of deboost_asleep_new_params(). Its comment has the
+ * steps. Store in das_back_ms the time at which O runs after its sleep.
+ * Return NULL, or 1 on an error.
+ */
+static void *das_owner(void *arg)
+{
+	struct timespec ts;
+	uint64_t t0, end;
+	int err;
+
+	err = set_constrained(0, 10 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, PERIOD);
+	t0 = now_ns();
+	if (err || take_pi_lock()) {
+		if (err)
+			ksft_print_msg("the owner refused as a deadline task: %s\n",
+				       strerror(-err));
+		sem_post(&das_owner_locked);
+		set_other(0);
+		return (void *)1;
+	}
+	sem_post(&das_owner_locked);
+	while (!das_donor_locking || proc_state_of(das_donor_tid) != 'S') {
+		if (now_ns() - t0 > NSEC_PER_SEC) {
+			ksft_print_msg("the donor did not block on the lock\n");
+			err = -1;
+			goto out;
+		}
+	}
+	err = set_constrained(0, 9 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, PERIOD);
+	if (err) {
+		ksft_print_msg("the owner could not change: %s\n", strerror(-err));
+		goto out;
+	}
+	end = t0 + 190 * NSEC_PER_MSEC;
+	ts.tv_sec = end / NSEC_PER_SEC;
+	ts.tv_nsec = end % NSEC_PER_SEC;
+	while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR)
+		;
+	das_back_ms = (now_ns() - t0) / NSEC_PER_MSEC;
+out:
+	drop_pi_lock();
+	if (set_other(0))
+		ksft_print_msg("the owner cannot leave SCHED_DEADLINE\n");
+	return err ? (void *)1 : NULL;
+}
+
+/*
+ * Deboost a task that sleeps on a timer, after it changed its parameters
+ * while boosted. The threads share the CPU of P:
+ * 1. O becomes a deadline task of 10ms in 20ms, every 100ms, at 0ms. It
+ *    takes pi_lock.
+ * 2. D, a deadline task of 1ms every 10ms, waits 150ms for pi_lock. It
+ *    boosts O.
+ * 3. O changes itself to 9ms in 20ms, every 100ms. It sleeps until
+ *    190ms, holding pi_lock.
+ * 4. D times out at 150ms, while O sleeps.
+ * 5. O wakes up at 190ms. Its own period ended at 100ms. It must run at
+ *    once, in a new period.
+ *
+ * If the deboost checks O's new parameters while O sleeps, O starts a
+ * period at 150ms, with a deadline at 170ms. Its wakeup at 190ms is then
+ * between that deadline and the next period, at 250ms. It throttles O
+ * until 250ms. The case fails if O runs at 220ms or later.
+ */
+static int deboost_asleep_new_params(void)
+{
+	void *o_ret = NULL, *d_ret = NULL;
+	int ret = KSFT_FAIL;
+	pthread_t o, d;
+
+	if (pi_init())
+		return KSFT_SKIP;
+	if (sem_init(&das_donor_ready, 0, 0) || sem_init(&das_owner_locked, 0, 0)) {
+		ksft_print_msg("sem_init: %s\n", strerror(errno));
+		pi_fini();
+		return KSFT_FAIL;
+	}
+	das_donor_locking = 0;
+	das_donor_tid = 0;
+	das_back_ms = -1;
+	if (pthread_create(&d, NULL, das_donor, NULL)) {
+		ksft_print_msg("the donor thread could not start\n");
+		goto out;
+	}
+	if (sem_wait_for(&das_donor_ready, 5000)) {
+		sem_post(&das_owner_locked);
+		join(d);
+		goto out;
+	}
+	if (pthread_create(&o, NULL, das_owner, NULL)) {
+		ksft_print_msg("the owner thread could not start\n");
+		sem_post(&das_owner_locked);
+		join(d);
+		goto out;
+	}
+	pthread_join(o, &o_ret);
+	pthread_join(d, &d_ret);
+	if (o_ret || d_ret || das_back_ms < 0)
+		goto out;	/* the thread said why */
+	if (das_back_ms >= 220) {
+		ksft_print_msg("O ran again at %lld ms, after its wakeup at 190\n",
+			       (long long)das_back_ms);
+		goto out;
+	}
+	ret = KSFT_PASS;
+out:
+	sem_destroy(&das_donor_ready);
+	sem_destroy(&das_owner_locked);
+	pi_fini();
+	return ret;
+}
+
+/* The kinds of owner_boost_case() */
+enum {
+	OB_GAP, OB_THROTTLED, OB_NEW_PARAMS, OB_RIVAL, OB_LONG,
+	OB_LATE_THROTTLE, OB_LATE_GAP,
+};
+
+static sem_t ob_locked, ob_go, ob_rival_go, ob_rival_ready;
+static _Atomic unsigned long long ob_t0;
+static _Atomic long long ob_got_ms;
+static _Atomic pid_t ob_w_tid;
+static int ob_throttled, ob_kind;
+static atomic_int ob_w_locking;
+
+/*
+ * Run as the owner O of owner_boost_case(). Become a deadline task, take
+ * pi_lock, and act by ob_kind:
+ * - OB_GAP and OB_LATE_GAP: 10ms in 20ms, every 500ms. Sleep until 40ms.
+ *   O then wakes past its deadline, before its next period.
+ * - the other kinds: 10ms every 100ms. O runs out of runtime at 10ms,
+ *   holding the lock. Then:
+ *   - OB_THROTTLED and OB_RIVAL: spin 15ms of CPU time.
+ *   - OB_LATE_THROTTLE: sleep until W blocks on pi_lock, then spin 15ms.
+ *   - OB_NEW_PARAMS: spin 12ms. Change O to 9ms every 100ms, and W to 5ms
+ *     every 310ms. Then spin 3ms.
+ *   - OB_LONG: spin 12ms, then sleep until 250ms.
+ * Then unlock. Return NULL, or 1 on an error.
+ */
+static void *ob_owner(void *arg)
+{
+	struct timespec ts;
+	uint64_t end;
+	int err, bad = 0;
+
+	if (ob_throttled)
+		err = set_deadline(0, 10 * NSEC_PER_MSEC, PERIOD);
+	else
+		err = set_constrained(0, 10 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC,
+				      5 * PERIOD);
+	ob_t0 = now_ns();
+	if (err || take_pi_lock()) {
+		if (err)
+			ksft_print_msg("the owner refused as a deadline task: %s\n",
+				       strerror(-err));
+		sem_post(&ob_locked);
+		set_other(0);
+		return (void *)1;
+	}
+	sem_post(&ob_locked);
+	if (ob_kind == OB_NEW_PARAMS) {
+		spin_ns(12 * NSEC_PER_MSEC);
+		err = set_deadline(0, 9 * NSEC_PER_MSEC, PERIOD);
+		if (!err)
+			err = set_deadline(ob_w_tid, 5 * NSEC_PER_MSEC,
+					   3 * PERIOD + 10 * NSEC_PER_MSEC);
+		if (err) {
+			ksft_print_msg("the owner could not change the parameters: %s\n",
+				       strerror(-err));
+			bad = 1;
+		}
+		spin_ns(3 * NSEC_PER_MSEC);
+	} else if (ob_kind == OB_LONG) {
+		spin_ns(12 * NSEC_PER_MSEC);
+		end = ob_t0 + 250 * NSEC_PER_MSEC;
+		ts.tv_sec = end / NSEC_PER_SEC;
+		ts.tv_nsec = end % NSEC_PER_SEC;
+		while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR)
+			;
+	} else if (ob_kind == OB_LATE_THROTTLE) {
+		end = now_ns();
+		while (!ob_w_locking || proc_state_of(ob_w_tid) != 'S') {
+			if (now_ns() - end > NSEC_PER_SEC) {
+				ksft_print_msg("the waiter did not block on the lock\n");
+				bad = 1;
+				break;
+			}
+			sleep_ns(NSEC_PER_MSEC);
+		}
+		spin_ns(15 * NSEC_PER_MSEC);
+	} else if (ob_throttled) {
+		spin_ns(15 * NSEC_PER_MSEC);
+	} else {
+		end = ob_t0 + 40 * NSEC_PER_MSEC;
+		ts.tv_sec = end / NSEC_PER_SEC;
+		ts.tv_nsec = end % NSEC_PER_SEC;
+		while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR)
+			;
+	}
+	drop_pi_lock();
+	if (set_other(0))
+		ksft_print_msg("the owner cannot leave SCHED_DEADLINE\n");
+	return bad ? (void *)1 : NULL;
+}
+
+/*
+ * Run as the waiter W of owner_boost_case(). Wait for ob_go. Then
+ * become a deadline task of 5ms every 300ms, or of 2ms every 10ms with
+ * OB_RIVAL, and take pi_lock. Store in ob_got_ms the time at which W gets
+ * it. Return NULL, or 1 on an error.
+ */
+static void *ob_waiter(void *arg)
+{
+	int err;
+
+	ob_w_tid = gettid_();
+	sem_wait_sig(&ob_go);
+	if (ob_kind == OB_RIVAL)
+		err = set_deadline(0, 2 * NSEC_PER_MSEC, 10 * NSEC_PER_MSEC);
+	else
+		err = set_deadline(0, 5 * NSEC_PER_MSEC, 3 * PERIOD);
+	if (err) {
+		ksft_print_msg("the waiter refused as a deadline task: %s\n",
+			       strerror(-err));
+		if (ob_kind == OB_RIVAL)
+			sem_post(&ob_rival_go);	/* release the rival */
+		return (void *)1;
+	}
+	if (ob_kind == OB_RIVAL)
+		sem_post(&ob_rival_go);
+	ob_w_locking = 1;
+	if (take_pi_lock()) {
+		set_other(0);
+		return (void *)1;
+	}
+	ob_got_ms = (now_ns() - ob_t0) / NSEC_PER_MSEC;
+	drop_pi_lock();
+	if (set_other(0))
+		ksft_print_msg("the waiter cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Run as the rival C of pi_owner_throttled_rival(). Become a deadline
+ * task of 40ms in 60ms, every 100ms. Wait until W is about to block,
+ * then sleep until 13ms after O's start, and spin 40ms of CPU time.
+ * Return NULL, or 1 on an error.
+ */
+static void *ob_rival(void *arg)
+{
+	struct timespec ts;
+	uint64_t end;
+	int err;
+
+	err = set_constrained(0, 40 * NSEC_PER_MSEC, 60 * NSEC_PER_MSEC, PERIOD);
+	sem_post(&ob_rival_ready);	/* W may become a deadline task now */
+	if (err) {
+		ksft_print_msg("the rival refused as a deadline task: %s\n",
+			       strerror(-err));
+		return (void *)1;
+	}
+	sem_wait_sig(&ob_rival_go);
+	end = ob_t0 + 13 * NSEC_PER_MSEC;
+	ts.tv_sec = end / NSEC_PER_SEC;
+	ts.tv_nsec = end % NSEC_PER_SEC;
+	while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR)
+		;
+	spin_ns(40 * NSEC_PER_MSEC);
+	if (set_other(0))
+		ksft_print_msg("the rival cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Run an owner_boost case of @kind: ob_owner() and ob_waiter(), and
+ * ob_rival() with OB_RIVAL. W must get pi_lock within @limit_ms of O's
+ * start.
+ */
+static int owner_boost_case(int kind, int limit_ms)
+{
+	void *o_ret = NULL, *w_ret = NULL, *c_ret = NULL;
+	int throttled = kind != OB_GAP && kind != OB_LATE_GAP;
+	int ret = KSFT_FAIL, rival_late = 0;
+	pthread_t o, w, c;
+
+	if (pi_init())
+		return KSFT_SKIP;
+	if (kind == OB_LONG) {
+		int had = klog_has(DL_LAG_MSG);
+
+		if (had) {
+			ksft_print_msg(had < 0 ? "the kernel log cannot be read\n" :
+				       "the lag message was logged before this case\n");
+			pi_fini();
+			return KSFT_SKIP;
+		}
+	}
+	if (sem_init(&ob_locked, 0, 0) || sem_init(&ob_go, 0, 0) ||
+	    sem_init(&ob_rival_go, 0, 0) || sem_init(&ob_rival_ready, 0, 0)) {
+		ksft_print_msg("sem_init: %s\n", strerror(errno));
+		pi_fini();
+		return KSFT_FAIL;
+	}
+	ob_throttled = throttled;
+	ob_kind = kind;
+	ob_got_ms = -1;
+	ob_w_locking = 0;
+	if (pthread_create(&w, NULL, ob_waiter, NULL)) {
+		ksft_print_msg("the waiter thread could not start\n");
+		goto out;
+	}
+	if (pthread_create(&o, NULL, ob_owner, NULL)) {
+		ksft_print_msg("the owner thread could not start\n");
+		sem_post(&ob_go);
+		join(w);
+		goto out;
+	}
+	/* O holds pi_lock, and runs until it is throttled or asleep. */
+	if (sem_wait_for(&ob_locked, 5000)) {
+		sem_post(&ob_go);
+		join(o);
+		join(w);
+		goto out;
+	}
+	if (kind == OB_GAP)
+		sleep_ns(30 * NSEC_PER_MSEC);	/* O sleeps past its deadline */
+	if (kind == OB_RIVAL && pthread_create(&c, NULL, ob_rival, NULL)) {
+		ksft_print_msg("the rival thread could not start\n");
+		kind = OB_THROTTLED;
+		c_ret = (void *)1;
+	}
+	/*
+	 * Let W run only once C is a deadline task. Otherwise W could get
+	 * the lock before C competes with O, and a broken kernel would pass.
+	 */
+	if (kind == OB_RIVAL && sem_wait_for(&ob_rival_ready, 5000))
+		rival_late = 1;
+	sem_post(&ob_go);
+	pthread_join(o, &o_ret);
+	pthread_join(w, &w_ret);
+	if (kind == OB_RIVAL)
+		pthread_join(c, &c_ret);
+	if (rival_late) {
+		ksft_print_msg("the rival did not become a deadline task\n");
+		goto out;
+	}
+	if (o_ret || w_ret || c_ret || ob_got_ms < 0)
+		goto out;	/* the thread said why */
+	if (ob_got_ms >= limit_ms) {
+		ksft_print_msg("the waiter got the lock at %lld ms, not before %d\n",
+			       (long long)ob_got_ms, limit_ms);
+		goto out;
+	}
+	if (kind == OB_LONG && klog_has(DL_LAG_MSG) == 1) {
+		ksft_print_msg("the deboost logged \"%s\"\n", DL_LAG_MSG);
+		goto out;
+	}
+	ret = KSFT_PASS;
+out:
+	sem_destroy(&ob_locked);
+	sem_destroy(&ob_go);
+	sem_destroy(&ob_rival_go);
+	sem_destroy(&ob_rival_ready);
+	pi_fini();
+	return ret;
+}
+
+/*
+ * A deadline waiter blocks on a lock whose owner sleeps past its deadline:
+ * 1. O becomes a deadline task of 10ms in 20ms, every 500ms, at 0ms. It
+ *    takes pi_lock and sleeps until 40ms.
+ * 2. At 30ms, W becomes a deadline task of 5ms every 300ms and blocks on
+ *    pi_lock. O's deadline, 20ms, is the earlier one.
+ * 3. O wakes up at 40ms, between its deadline and its next period, at
+ *    500ms. The wakeup would throttle it until then.
+ *
+ * W must boost O: O cannot run on its own reservation. O then runs at
+ * 40ms and unlocks. The case fails if W gets pi_lock at 200ms or later.
+ */
+static int pi_owner_asleep_in_gap(void)
+{
+	return owner_boost_case(OB_GAP, 200);
+}
+
+/*
+ * A deadline waiter blocks on a lock whose owner ran out of runtime:
+ * 1. O becomes a deadline task of 10ms every 100ms, at 0ms. It takes
+ *    pi_lock and spins 15ms of CPU time. It is throttled at 10ms, until
+ *    100ms.
+ * 2. W then becomes a deadline task of 5ms every 300ms and blocks on
+ *    pi_lock. O's deadline, 100ms, is the earlier one.
+ *
+ * W must boost O: O cannot run on its own reservation. O then runs its
+ * last 5ms on W's runtime and unlocks. The case fails if W gets pi_lock
+ * at 60ms or later.
+ */
+static int pi_owner_throttled(void)
+{
+	return owner_boost_case(OB_THROTTLED, 60);
+}
+
+/*
+ * A deadline waiter blocks on a lock whose owner can still run on its own
+ * reservation, and the owner then runs out of runtime:
+ * 1. O becomes a deadline task of 10ms every 100ms, at 0ms. It takes
+ *    pi_lock, and sleeps until W blocks on it.
+ * 2. W becomes a deadline task of 5ms every 300ms and blocks on pi_lock.
+ *    O's deadline, 100ms, is the earlier one: W does not boost O.
+ * 3. O spins 15ms of CPU time. It runs out of runtime after 10ms, with
+ *    pi_lock.
+ *
+ * W must boost O then: O cannot run on its own reservation until 100ms.
+ * O then runs its last 5ms on W's runtime and unlocks. The case fails if
+ * W gets pi_lock at 60ms or later.
+ */
+static int pi_owner_runs_out_later(void)
+{
+	return owner_boost_case(OB_LATE_THROTTLE, 60);
+}
+
+/*
+ * A deadline waiter blocks on a lock whose owner sleeps before its
+ * deadline:
+ * 1. O becomes a deadline task of 10ms in 20ms, every 500ms, at 0ms. It
+ *    takes pi_lock and sleeps until 40ms.
+ * 2. W becomes a deadline task of 5ms every 300ms and blocks on pi_lock,
+ *    before 20ms. O's deadline, 20ms, is the earlier one: W does not
+ *    boost O.
+ * 3. O wakes up at 40ms, between its deadline and its next period, at
+ *    500ms. The wakeup would throttle it until then.
+ *
+ * W must boost O at that wakeup. O then runs at 40ms and unlocks. The
+ * case fails if W gets pi_lock at 200ms or later.
+ */
+static int pi_owner_wakes_in_gap_later(void)
+{
+	return owner_boost_case(OB_LATE_GAP, 200);
+}
+
+static _Atomic unsigned long long ho_t0;
+static _Atomic long long ho_w_got_ms;
+static _Atomic pid_t ho_o_tid, ho_w_tid;
+static atomic_int ho_o_locking, ho_w_locking, ho_w_queued;
+
+/*
+ * Run as the owner O of pi_owner_handed_off(). Become a deadline task of
+ * 10ms every 100ms, and take pi_lock, which A holds. Once O has it, set
+ * ho_w_queued if W still waits for it. Spin 15ms of CPU time and unlock.
+ * Return NULL, or 1 on an error.
+ */
+static void *ho_owner(void *arg)
+{
+	int err;
+
+	ho_o_tid = gettid_();
+	err = set_deadline(0, 10 * NSEC_PER_MSEC, PERIOD);
+	ho_t0 = now_ns();
+	if (err) {
+		ksft_print_msg("the owner refused as a deadline task: %s\n",
+			       strerror(-err));
+		return (void *)1;
+	}
+	ho_o_locking = 1;
+	if (take_pi_lock()) {
+		set_other(0);
+		return (void *)1;
+	}
+	ho_w_queued = ho_w_got_ms < 0 && proc_state_of(ho_w_tid) == 'S';
+	spin_ns(15 * NSEC_PER_MSEC);
+	drop_pi_lock();
+	if (set_other(0))
+		ksft_print_msg("the owner cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * Run as the waiter W of pi_owner_handed_off(). Become a deadline task of
+ * 5ms every 300ms and take pi_lock. Store in ho_w_got_ms the time at
+ * which W gets it. Return NULL, or 1 on an error.
+ */
+static void *ho_waiter(void *arg)
+{
+	int err;
+
+	ho_w_tid = gettid_();
+	err = set_deadline(0, 5 * NSEC_PER_MSEC, 3 * PERIOD);
+	if (err) {
+		ksft_print_msg("the waiter refused as a deadline task: %s\n",
+			       strerror(-err));
+		return (void *)1;
+	}
+	ho_w_locking = 1;
+	if (take_pi_lock()) {
+		set_other(0);
+		return (void *)1;
+	}
+	ho_w_got_ms = (now_ns() - ho_t0) / NSEC_PER_MSEC;
+	drop_pi_lock();
+	if (set_other(0))
+		ksft_print_msg("the waiter cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}
+
+/*
+ * A variant of pi_owner_runs_out_later(): the owner gets the lock by a
+ * hand-off, with the waiter already queued on it. A, the main thread of
+ * the case, is a SCHED_OTHER task:
+ * 1. A takes pi_lock.
+ * 2. O becomes a deadline task of 10ms every 100ms, at 0ms. It blocks on
+ *    pi_lock.
+ * 3. W becomes a deadline task of 5ms every 300ms. It blocks on pi_lock.
+ * 4. A unlocks. O's deadline, about 100ms, is the earlier one. So O gets
+ *    pi_lock, and W stays queued on it. O can still run on its own
+ *    reservation, so W does not boost it.
+ * 5. O spins 15ms of CPU time. It runs out of runtime after 10ms, with
+ *    pi_lock.
+ *
+ * W must boost O then: O cannot run on its own reservation until about
+ * 100ms. W blocked on pi_lock before O owned it. So the boost must find W
+ * among the waiters that O got with the lock. O then runs its last 5ms on
+ * W's runtime and unlocks. The case fails if W does not wait for pi_lock
+ * when O gets it, or if W gets pi_lock at 60ms or later.
+ */
+static int pi_owner_handed_off(void)
+{
+	void *o_ret = NULL, *w_ret = NULL;
+	int ret = KSFT_FAIL, w_blocked;
+	pthread_t o, w;
+
+	if (pi_init())
+		return KSFT_SKIP;
+	ho_o_tid = 0;
+	ho_w_tid = 0;
+	ho_o_locking = 0;
+	ho_w_locking = 0;
+	ho_w_queued = 0;
+	ho_w_got_ms = -1;
+	if (take_pi_lock())
+		goto out;
+	w_blocked = 1;
+	if (pthread_create(&o, NULL, ho_owner, NULL)) {
+		ksft_print_msg("the owner thread could not start\n");
+		drop_pi_lock();
+		goto out;
+	}
+	if (wait_for(1000, ho_o_locking && proc_state_of(ho_o_tid) == 'S')) {
+		ksft_print_msg("the owner did not block on the lock\n");
+		drop_pi_lock();
+		join(o);
+		goto out;
+	}
+	if (pthread_create(&w, NULL, ho_waiter, NULL)) {
+		ksft_print_msg("the waiter thread could not start\n");
+		drop_pi_lock();
+		join(o);
+		goto out;
+	}
+	if (wait_for(1000, ho_w_locking && proc_state_of(ho_w_tid) == 'S')) {
+		ksft_print_msg("the waiter did not block on the lock\n");
+		w_blocked = 0;
+	}
+	drop_pi_lock();
+	pthread_join(o, &o_ret);
+	pthread_join(w, &w_ret);
+	if (!w_blocked || o_ret || w_ret || ho_w_got_ms < 0)
+		goto out;	/* the thread said why */
+	if (!ho_w_queued) {
+		ksft_print_msg("the waiter did not wait when the owner got the lock\n");
+		goto out;
+	}
+	if (ho_w_got_ms >= 60) {
+		ksft_print_msg("the waiter got the lock at %lld ms, not before 60\n",
+			       (long long)ho_w_got_ms);
+		goto out;
+	}
+	ret = KSFT_PASS;
+out:
+	pi_fini();
+	return ret;
+}
+
+/*
+ * The owner of pi_owner_throttled() changes its parameters while boosted:
+ * 1. As in pi_owner_throttled(), O is throttled at 10ms, and W boosts it.
+ * 2. After 12ms of CPU time, O changes itself to 9ms every 100ms. Then it
+ *    changes W to 5ms every 310ms. This change of W walks the chain to O.
+ *    O has new parameters, so rt_mutex_setprio() deboosts O before it
+ *    compares O with W.
+ *
+ * O has used up its own runtime until 100ms, so W must boost it again.
+ * O then runs its last 3ms and unlocks. The case fails if W gets pi_lock
+ * at 60ms or later.
+ */
+static int pi_owner_held_new_params(void)
+{
+	return owner_boost_case(OB_NEW_PARAMS, 60);
+}
+
+/*
+ * A deadline task C competes with the boosted owner of
+ * pi_owner_throttled():
+ * 1. O becomes a deadline task of 10ms every 100ms, at 0ms. Its deadline
+ *    is 100ms. It takes pi_lock and spins 15ms of CPU time. It is
+ *    throttled at 10ms.
+ * 2. W then becomes a deadline task of 2ms every 10ms, and blocks on
+ *    pi_lock. Its deadline is about 20ms. W boosts O.
+ * 3. C becomes a deadline task of 40ms in 60ms, every 100ms. It wakes up
+ *    at 13ms, with a deadline of about 70ms, and spins 40ms.
+ *
+ * Boosted, O must run on W's deadlines, before C's. O then runs its last
+ * 5ms and unlocks. Refilled from its own deadline, O would get a deadline
+ * of 110ms. That is after C's, so O would wait for C. The case fails if W
+ * gets pi_lock at 35ms or later.
+ */
+static int pi_owner_throttled_rival(void)
+{
+	return owner_boost_case(OB_RIVAL, 35);
+}
+
+/*
+ * The owner of pi_owner_throttled() stays boosted past its own next
+ * period:
+ * 1. As in pi_owner_throttled(), O is throttled at 10ms, and W boosts it.
+ * 2. After 12ms of CPU time, O sleeps until 250ms with the lock. Its own
+ *    period ends at 100ms, during that sleep.
+ * 3. O unlocks at 250ms.
+ *
+ * The deboost must start a period of O's own. Putting back the period
+ * that ended at 100ms would make the next refill print "sched: DL
+ * replenish lagged too much". The case fails if that message is logged,
+ * or if W gets pi_lock at 400ms or later. It skips if the message was
+ * logged before: the kernel prints it once.
+ */
+static int pi_owner_held_long(void)
+{
+	return owner_boost_case(OB_LONG, 400);
+}
+
+/*
+ * The threads of reboost_loop_case(): rb_owner() for each owner, rb_waiter()
+ * and rb_victim(). Each owner holds its own lock in rb_lock[].
+ */
+#define RB_OWNERS	2	/* the most owners of a case */
+#define RB_JOBS		20	/* periods of the victim */
+#define RB_RUNTIME	(35 * NSEC_PER_MSEC)	/* of the victim */
+#define RB_PERIOD	(50 * NSEC_PER_MSEC)	/* of the victim */
+
+static pthread_mutex_t rb_lock[RB_OWNERS];
+static sem_t rb_locked, rb_ready, rb_go, rb_go_w;
+static atomic_int rb_stop, rb_release;
+static uint64_t rb_victim_ns, rb_waits;
+
+/*
+ * Run as an owner of reboost_loop_case(). Become a deadline task of 1ms
+ * every 3s, take rb_lock[@arg] and post rb_locked. Spin until rb_release
+ * is set, then unlock. Return NULL, or 1 on an error.
+ */
+static void *rb_owner(void *arg)
+{
+	long i = (long)arg;
+	int err = set_deadline(0, NSEC_PER_MSEC, 30 * PERIOD);
+
+	if (err) {
+		ksft_print_msg("an owner refused as a deadline task: %s\n",
+			       strerror(-err));
+		sem_post(&rb_locked);
+		return (void *)1;
+	}
+	err = pthread_mutex_lock(&rb_lock[i]);
+	sem_post(&rb_locked);
+	if (err) {
+		ksft_print_msg("an owner cannot take its lock: %s\n", strerror(err));
+		set_other(0);
+		return (void *)1;
+	}
+	while (!atomic_load(&rb_release))
+		;
+	err = pthread_mutex_unlock(&rb_lock[i]);
+	if (err)
+		ksft_print_msg("an owner cannot unlock: %s\n", strerror(err));
+	if (set_other(0))
+		ksft_print_msg("an owner cannot leave SCHED_DEADLINE\n");
+	return err ? (void *)1 : NULL;
+}
+
+/*
+ * Run as the waiter of reboost_loop_case(), with @arg owners. Become a
+ * deadline task of 1ms every 10ms, post rb_ready and wait for rb_go_w.
+ * Until rb_stop is set, wait 1ms for each lock in turn, and count the
+ * waits in rb_waits. Each wait boosts the owner, and must time out. Then
+ * set rb_release, and take each lock without a timeout: its owner runs
+ * on the boost and unlocks. Return NULL, or 1 on an error.
+ */
+static void *rb_waiter(void *arg)
+{
+	int n = (long)arg, i = 0, err, bad = 0;
+
+	err = set_deadline(0, NSEC_PER_MSEC, 10 * NSEC_PER_MSEC);
+	if (err) {
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.47.3

Part of the diff is not shown here. View the raw message.

#56 lore raw

Re: [PATCH v12 42/43] selftests/sched: Test parameter changes in deadline_bw_test

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] selftests/sched: premature probe in measured() after constrained task exit --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests/sched: premature probe in measured() after constrained task exit
--

commit 55867328ab1ab4f59bba08a621ac5a4d89b5c264
Author: Mathieu Desnoyers <[email protected]>

selftests/sched: Test parameter changes in deadline_bw_test

This commit adds 39 test cases to deadline_bw_test covering parameter changes, priority inheritance boosts of throttled or constrained owners, and constrained tasks re-entering SCHED_DEADLINE during their gap.

Quoted 4 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +0 -0
index 83dd8b4e79a3d..66955952901ff 100644

[ ... ]

Quoted 4 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +0 -0
@@ -2749,93 +2923,3194 @@ static int requeue_pi_new_period(void)
 	return ret;
 }
 

[ ... ]

Quoted 19 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +19 -0
+static void *rb2_t(void *arg)
+{
+	uint32_t tid = gettid_();
+	int err;
+
+	rb2_t_tid = tid;
+	err = set_constrained(0, 10 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, 5 * PERIOD);
+	rb2_t_start = now_ns();
+	if (err) {
+		ksft_print_msg("T refused as a deadline task: %s\n", strerror(-err));
+		rb2_t_locking = -1;
+		return (void *)1;
+	}
+	spin_ns(2 * NSEC_PER_MSEC);
+	rb2_t_locking = 1;
+	if (syscall(SYS_futex, &rb2_pi, FUTEX_LOCK_PI | FUTEX_PRIVATE_FLAG,
+		    0, NULL, NULL, 0)) {
+		ksft_print_msg("FUTEX_LOCK_PI: %s\n", strerror(errno));
+		set_other(0);

tools/testing/selftests/sched/deadline/deadline_bw_test.c static int return_asleep_new_params(void) · line 4388

Quoted 10 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +10 -0
+		return (void *)1;
+	}
+	rb2_t_got = now_ns();
+	if (!atomic_compare_exchange_strong(&rb2_pi, &tid, 0))
+		syscall(SYS_futex, &rb2_pi, FUTEX_UNLOCK_PI | FUTEX_PRIVATE_FLAG,
+			0, NULL, NULL, 0);
+	if (set_other(0))
+		ksft_print_msg("T cannot leave SCHED_DEADLINE\n");
+	return NULL;
+}

[Severity: Medium]
Does calling set_other(0) at the start of T's second 500ms constrained period cause measured() to probe before T's bandwidth is released?

T is throttled in its constrained gap until t = 500ms, when dl_task_timer() replenishes it for [500ms, 1000ms]. When T immediately calls set_other(0) while still alive, task_non_contending() keeps the departing constrained task's 2% bandwidth until the end of that period at t = 1000ms, and do_exit() does not cancel inactive_timer after T has left SCHED_DEADLINE.

Because reenter_pi_blocked_in_gap() returns right after pthread_join() at t = 500ms and measured() only waits 3 * PERIOD (300ms):

tools/testing/selftests/sched/deadline/deadline_bw_test.c:measured() {
	...
	ret = run_in(cg, fn);
	sleep_ns(3 * PERIOD);
	after = probe(cg);
	...
}

probe(cg) runs at t = 800ms while T's 2% bandwidth is still counted in total_bw, admitting 8 probe tasks of 10% instead of 9 and failing the test. The same happens in ra_t() (for reenter_asleep_in_gap() and reenter_asleep_late_in_gap()) and in ob_owner() (for pi_owner_asleep_in_gap() and pi_owner_wakes_in_gap_later(), where drop_pi_lock() deboosts and throttles O in its gap until t = 500ms before set_other(0)). Waiting for the next period before returning (as reenter_runnable_in_gap() does) or letting the thread exit without calling set_other(0) first would avoid the premature probe.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=42
#57 lore raw

PATCH v12 43/43 selftests/sched: Share the helpers of the deadline tests

In reply to Mathieu Desnoyers

deadline_bw_test, deadline_rebuild_test and deadline_switch_test each have a copy of the same helpers. Move these to deadline_util.h:
- now_ns(), thread_time_ns() and sleep_ns();
- report();
- read_file(), write_file() and enter().

deadline_rebuild_test uses thread_time_ns() in place of its own cpu_time_ns(). The helpers that differ from one test to the other stay in each test. The Makefile adds the header to LOCAL_HDRS, so that lib.mk rebuilds the tests when the header changes.

The Sashiko AI review of this series asked for this. No case changes.

---
 .../testing/selftests/sched/deadline/Makefile |   2 +
 .../sched/deadline/deadline_bw_test.c         |  86 +-------------
 .../sched/deadline/deadline_rebuild_test.c    |  94 +---------------
 .../sched/deadline/deadline_switch_test.c     |  36 +-----
 .../selftests/sched/deadline/deadline_util.h  | 106 ++++++++++++++++++
 5 files changed, 116 insertions(+), 208 deletions(-)
 create mode 100644 tools/testing/selftests/sched/deadline/deadline_util.h
tools/testing/selftests/sched/deadline/Makefile +2 -0
index 15f1e96a21e6..a81aa97c6cc6 100644
@@ -5,4 +5,6 @@ LDLIBS += -lpthread
 
 TEST_GEN_PROGS := deadline_switch_test deadline_rebuild_test deadline_bw_test
 
+LOCAL_HDRS += deadline_util.h
+
 include ../../lib.mk
tools/testing/selftests/sched/deadline/deadline_bw_test.c +2 -84

110 lines not on this page · raw message

tools/testing/selftests/sched/deadline/deadline_rebuild_test.c +4 -90

130 lines not on this page · raw message

tools/testing/selftests/sched/deadline/deadline_switch_test.c +2 -34
index 40c7c6bafc2b..a544bee5af5a 100644
@@ -51,6 +51,8 @@
 #define NSEC_PER_MSEC	1000000ULL
 #define NSEC_PER_SEC	1000000000ULL
 
+#include "deadline_util.h"
+
 struct dl_attr {
 	uint32_t size;
 	uint32_t sched_policy;
@@ -70,22 +72,6 @@ struct shared {
 	uint64_t sleeps;	/* sleeps begun */
 };
 
-static uint64_t now_ns(void)
-{
-	struct timespec ts;
-
-	clock_gettime(CLOCK_MONOTONIC, &ts);
-	return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
-}
-
-static void sleep_ns(uint64_t ns)
-{
-	struct timespec ts = { .tv_sec = ns / NSEC_PER_SEC, .tv_nsec = ns % NSEC_PER_SEC };
-
-	while (nanosleep(&ts, &ts) && errno == EINTR)
-		;
-}
-
 static void sleep_until(uint64_t t)
 {
 	struct timespec ts = { .tv_sec = t / NSEC_PER_SEC, .tv_nsec = t % NSEC_PER_SEC };
@@ -518,14 +504,6 @@ static sem_t owner_locked, owner_go;
 static pid_t owner_tid;
 static int owner_ms;		/* the owner's CPU time in its first period */
 
-static uint64_t thread_time_ns(void)
-{
-	struct timespec ts;
-
-	clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
-	return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
-}
-
 /*
  * Wait on @s for at most @ms, retrying on signals. Return 0, or -1 on a
  * timeout or an error.
@@ -1081,16 +1059,6 @@ static int test_sysrq_running_owner(int normalized)
 	return ret;
 }
 
-static void report(const char *name, int ret)
-{
-	if (ret == KSFT_PASS)
-		ksft_test_result_pass("%s\n", name);
-	else if (ret == KSFT_SKIP)
-		ksft_test_result_skip("%s\n", name);
-	else
-		ksft_test_result_fail("%s\n", name);
-}
-
 int main(void)
 {
 	int ret;
tools/testing/selftests/sched/deadline/deadline_util.h new file +106 -0

106 lines not on this page · raw message

Signature · 1 line
2.47.3