From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=158.69.130.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796310003; b=q+xxlgL85UWhUmlezyNwairEVvPGwj4IzsGwT0Gn878vtER8a/pqyRluQi/fMhzBJmf0 QnIu8BfY+7v7isZSXZEAzQZt2snJxVMlKl269JIh2UTbH/vm7Vqf0xKrHOWIiLJvjNykz oeJUoOWEQ3Fc0k6b/DEExgV6vZpdKObeKvlJaYXuDB6ZPjwXcI2RayDEwME6O8O3DZzWO g7bMfDr36XMhzdbASGJy82v5H8pL586RJBnqBaBL2c+0ZCmAo48l+bN41aXMKYf0YuRX4 pGS0yucKQnoES+PCNdWv2JAyDuBqQW3on+e9TRD7tRbN4EhvfOzMzP9R0JK3vhuW8Qw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796310003; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=+/a5Q58tLz2QL274WI5XCt6ZpTyNnoDAtqVfjU4T4Do=; b=NnvrJmBZLndgRm9g9RfsP1zibuG2B7KBUd1N1DfStfl+ffXXlFtbrvq/AVhKKCSDT/lq nk51iD8rh/eF0oV4bKZ063FLDcxAxNsxWH7j7EN+otDIR2QndQl6rqERNsaGGcDb3baE+ JNfH/qV4xvp2PreXLDoJv6rkNh5Qk7q5RE+MtKypUSZmQJd39cv0IIs+Zu6i8vZwECCoX UqXkjy4wt2iv6YoNghLH2tq3dpRcSBXwjczM7sJwgHESKSDrcAlD57uEzf+dl/WnD9Dw3 +ZNroNhxMQ9wAES7lkRN3IQ6PKQEPNqEtDFkai1O6fvgR0rtfT+tYLKHc0Zcm1AEwnA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=efficios.com; spf=pass smtp.mailfrom=efficios.com; dkim=pass header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b="L6NbGZ/x"; arc=none smtp.remote-ip=158.69.130.18 Received: from smtpout.efficios.com (smtpout.efficios.com [158.69.130.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77BC4606B38C; Thu, 3 Dec 2026 15:00:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 77BC4606B38C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=efficios.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=efficios.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=L6NbGZ/x DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1796310000; bh=+/a5Q58tLz2QL274WI5XCt6ZpTyNnoDAtqVfjU4T4Do=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L6NbGZ/xR+PDMFAn9ZpNFKqLDrxjm90ZaXiQu54hN5I142JYsYzWt/59bii0kwx9L TxzmO6a1OKIZrLO6iaem4dkW7r+ani9T3Cspvap4Mv+uzTK9y5/DPWPcSpUbUH4rn8 Awu3qq+wYWbfhBmR5+6VXKsTVP7XGt0+RaLukMQCE8PDQcpeP6+oar8AaA9fUviJwq 5+bDLGEZq6Gby7xPhPCnFzzeR4yqHl7NwOJpOvyDfj/1+0UjvZtVKWhxR0cMDpJaWi E5oxG3a0jS5Qfs8j8g6CSP9Y/LeiwJK1PPzfHuyqBHQ3qWmf8CliCWvoMjgXpYBwUI JIgn1FCmguwSg== Received: from thinkos.internal.efficios.com (unknown [IPv6:2605:8d80:7180:6739:ac39:5d4:4093:61c6]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4jgHq31cxNzZlL; Thu, 03 Dec 2026 09:59:59 -0500 (EST) From: Mathieu Desnoyers To: juri.lelli@redhat.com, peterz@infradead.org, mingo@redhat.com, vincent.guittot@linaro.org Cc: Mathieu Desnoyers , tglx@kernel.org, anna-maria@linutronix.de, frederic@kernel.org, will@kernel.org, boqun@kernel.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, kprateek.nayak@amd.com, luca.abeni@santannapisa.it, tommaso.cucinotta@gmail.com, mathieu.poirier@linaro.org, longman@redhat.com, ridong.chen@linux.dev, tj@kernel.org, hannes@cmpxchg.org, mkoutny@suse.com, shuah@kernel.org, bigeasy@linutronix.de, clrkwllms@kernel.org, parri.andrea@gmail.com, m.szyprowski@samsung.com, kayracizmeci@gmail.com, weihengqiuu@gmail.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-rt-devel@lists.linux.dev, oleg@redhat.com, brauner@kernel.org Subject: [PATCH v14 33/39] selftests/sched: Test parameter changes in deadline_bw_test Date: Thu, 3 Dec 2026 09:58:47 -0500 Message-ID: <79fec47f037759dc9473351f7f718bb6f570a6a0.1796296313.git.mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add 25 cases to deadline_bw_test, for the fixes earlier in this series. The comment above each case gives its scenario. The cases form three groups: - In 20 cases, a deadline task changes its parameters, or leaves the class and comes back 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 4 cases, a constrained task leaves the class in its gap, and comes back with the same parameters. The gap is the time between its deadline and its next period. - In params_return_full, a task of 60% leaves the class and comes back with the same parameters, before its 0-lag time. Its partition has no room for two tasks of 60%. The case fails without "sched/deadline: Admit a returning task without the bandwidth it holds", and passes with it. 12 of the 20 cases fail on v7.4-rc2. Each one fails at the patch before the fix named here, 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, 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 and params_swap_overload. The other 8 cases pass on v7.4-rc2 and with the whole series. They check that "sched/deadline: Keep the debt of a task across a change of parameters" starts the next period of a task neither too early nor too late. The 8 cases are 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. Three of them fail if dl_next_period() counts from period_start without the rules of that patch: wake_after_deadline_shrunk, period_shrunk_no_burst and constrained_after_job. The four reenter_*_in_gap cases pass on v7.4-rc2. Two patches of the first series make them fail: - "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. reenter_asleep_late_in_gap passes again from "sched/deadline: Keep the deadline of a departed constrained task" on. The other three pass again from "sched/deadline: Count the 0-lag time from the next period" on. The cases run in P, a one-CPU partition root. 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. It reads this_bw of P's CPU at its start, and checks that it is the same at its end. The debt was repaid in between, so random_mix failed in most runs. 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. It then waits, the same way, for this_bw of the three CPUs that random_mix checks. A task that a cpuset attach moves while it sleeps keeps its bandwidth in this_bw of its old CPU until it wakes up or dies. In reenter_pi_blocked_in_gap, reenter_asleep_in_gap and reenter_asleep_late_in_gap, T leaves the class early in its second period. It keeps its bandwidth until its 0-lag time, before its third period. Each case waits for that third period before it returns. Else measured() can probe while T is still counted. The probe then sees no change only while T fits in the room left after the probe tasks. With the whole series, every case of deadline_bw_test passes, PREEMPT_RT included. No kernel warning is logged. Signed-off-by: Mathieu Desnoyers --- .../sched/deadline/deadline_bw_test.c | 2223 ++++++++++++++++- 1 file changed, 2163 insertions(+), 60 deletions(-) diff --git a/tools/testing/selftests/sched/deadline/deadline_bw_test.c b/tools/testing/selftests/sched/deadline/deadline_bw_test.c index dbe0de739e47..58ec444cf1bb 100644 --- a/tools/testing/selftests/sched/deadline/deadline_bw_test.c +++ b/tools/testing/selftests/sched/deadline/deadline_bw_test.c @@ -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 the cases with no_hp set, which take no CPU @@ -54,6 +56,7 @@ #include #include #include +#include #include #include #include @@ -280,6 +283,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 }; @@ -877,7 +894,7 @@ static int pi_chain_two_owners(void) } /* - * Run as the owner thread: take pi_lock, and sleep 400ms while holding + * Run as the owner thread. Take pi_lock, and sleep 400ms while holding * it. Return NULL, or 1 on an error. */ static void *sleeping_owner(void *arg) @@ -893,11 +910,11 @@ static void *sleeping_owner(void *arg) /* * A SCHED_OTHER owner takes pi_lock and sleeps 400ms while holding it. - * The main thread then waits 20ms for the lock, 5 times. For each wait it - * becomes a deadline task of 5ms every 50ms. Each wait boosts the + * The main thread then waits 20ms for the lock, @rounds times. For each + * wait it becomes a deadline task of 5ms every 50ms. Each wait boosts the * sleeping owner, and its timeout deboosts it. Every wait must time out. */ -static int pi_other_owner_asleep(void) +static int pi_asleep(int rounds) { pthread_t o; int r, ret = KSFT_PASS; @@ -914,9 +931,9 @@ static int pi_other_owner_asleep(void) } if (ret == KSFT_PASS) sleep_ns(20 * NSEC_PER_MSEC); - for (r = 0; r < 5 && ret == KSFT_PASS; r++) { + for (r = 0; r < rounds && ret == KSFT_PASS; r++) { if (wait_lock_for(20, 1)) { - ksft_print_msg("round %d of 5 failed\n", r + 1); + ksft_print_msg("round %d of %d failed\n", r + 1, rounds); ret = KSFT_FAIL; } } @@ -924,6 +941,11 @@ static int pi_other_owner_asleep(void) return ret; } +static int pi_other_owner_asleep(void) +{ + return pi_asleep(5); +} + /* * Run as the owner thread. Become a deadline task of 30ms every 100ms, * and take pi_lock. Hold it for 300ms, spinning 2ms and sleeping 5ms in @@ -1455,6 +1477,82 @@ static int params_refused(void) return ret; } +static _Atomic uint64_t rt_t_slept; + +/* + * Run as T of params_return_full(). Become a deadline task of 60ms every + * 100ms. Sleep one period, so that the next wakeup starts a new period. + * Run 30ms of it, then sleep until t_exit is posted. Return NULL, or 1 + * on an error. + */ +static void *runs_half_then_sleeps(void *arg) +{ + t_tid = gettid_(); + if (set_deadline(0, 60 * NSEC_PER_MSEC, PERIOD)) { + t_state = -1; + return (void *)1; + } + sleep_ns(PERIOD); /* a fresh period */ + spin_ns(30 * NSEC_PER_MSEC); + rt_t_slept = now_ns(); + t_state = 1; + sem_wait_sig(&t_exit); + return NULL; +} + +/* + * A deadline task T of 60ms every 100ms runs 30ms of a fresh period, then + * sleeps with 30ms left. Its 0-lag time is 100 - 30 * 100 / 60 = 50ms + * into the period. Before then, the main thread switches T to + * SCHED_OTHER, then back to SCHED_DEADLINE with the same parameters. T + * still holds its 60% until its 0-lag time. P has no room for two tasks + * of 60%. The return must be admitted: its new bandwidth replaces the old + * one. + */ +static int params_return_full(void) +{ + int ret = KSFT_PASS, err; + uint64_t slept; + pthread_t t; + + t_state = 0; + rt_t_slept = 0; + if (sem_init(&t_exit, 0, 0)) { + ksft_print_msg("sem_init: %s\n", strerror(errno)); + return KSFT_FAIL; + } + if (pthread_create(&t, NULL, runs_half_then_sleeps, NULL)) { + ksft_print_msg("the T thread could not start\n"); + return KSFT_FAIL; + } + if (wait_for_step(5000, 100 * NSEC_PER_USEC, t_state) || t_state != 1 || + wait_for_step(5, 50 * NSEC_PER_USEC, proc_state_of(t_tid) == 'S')) { + ksft_print_msg("T was refused SCHED_DEADLINE, or never slept\n"); + ret = KSFT_FAIL; + goto out; + } + err = set_other(t_tid); + if (!err) + err = set_deadline(t_tid, 60 * NSEC_PER_MSEC, PERIOD); + slept = now_ns() - rt_t_slept; + if (slept > 15 * NSEC_PER_MSEC) { + /* Too late: the 0-lag time may have passed. */ + ksft_print_msg("T switched back %llums after it slept\n", + (unsigned long long)(slept / NSEC_PER_MSEC)); + ret = KSFT_SKIP; + } else if (err) { + ksft_print_msg("T refused back as a deadline task: %s\n", strerror(-err)); + ret = KSFT_FAIL; + } + if (ret == KSFT_PASS && probe(NULL) >= 6) { + ksft_print_msg("P has room for two tasks of 60%%\n"); + ret = KSFT_SKIP; + } +out: + stop_sleeper(t); + return ret; +} + /* * A SCHED_OTHER task with a slice of 40ms asks for SCHED_DEADLINE with * SCHED_FLAG_KEEP_PARAMS. That changes the class, so it must be refused @@ -1560,7 +1658,7 @@ static int params_keep_boosted(void) if (pi_init()) return KSFT_SKIP; - if (pthread_create(&o, NULL, sleeping_owner, (void *)0L)) { + if (pthread_create(&o, NULL, sleeping_owner, NULL)) { ksft_print_msg("the owner thread could not start\n"); return KSFT_FAIL; } @@ -1604,7 +1702,7 @@ static int boosted_owner_policy(void) if (pi_init()) return KSFT_SKIP; - if (pthread_create(&o, NULL, sleeping_owner, (void *)0L)) { + if (pthread_create(&o, NULL, sleeping_owner, NULL)) { ksft_print_msg("the owner thread could not start\n"); return KSFT_FAIL; } @@ -1691,8 +1789,9 @@ static void *donor_waiter(void *arg) } /* - * The owner O starts as SCHED_OTHER. The donor D is the waiter that - * boosts it: a deadline task of 40ms every 100ms. + * A deboost ends the period of the donor. The owner O starts as + * SCHED_OTHER. The donor D is the waiter that boosts it: a deadline task + * of 40ms every 100ms. * 1. O takes pi_lock and spins. * 2. D blocks on pi_lock. This boosts O, which then runs on D's runtime. * 3. O spins 5ms more and unlocks. The unlock deboosts O. @@ -1812,10 +1911,10 @@ static void *refill_donor(void *arg) } /* - * Run as the owner O of deboost_keeps_own_runtime(). @arg is the CPU - * time, in ms, that O spins with the lock once it is boosted. Store the - * runtime O has left after the unlock in refill_owner_left. Return NULL, - * 1 on an error, or 2 if the donor did not block on the lock within 1s. + * Run as the owner O of deboost_keeps_own_runtime(). Spin 1ms with the + * lock once boosted. Store in refill_owner_left the runtime O has left + * after the unlock. Return NULL, 1 on an error, or 2 if the donor did not + * block on the lock within 1s. */ static void *refill_owner(void *arg) { @@ -1841,7 +1940,7 @@ static void *refill_owner(void *arg) return (void *)2; } } - spin_ns((long)arg * NSEC_PER_MSEC); + spin_ns(NSEC_PER_MSEC); drop_pi_lock(); refill_owner_left = dl_runtime_left(owner_tid); if (set_other(0)) @@ -1850,8 +1949,8 @@ static void *refill_owner(void *arg) } /* - * Run a case on the runtime that a deadline owner has after its boost. - * O and D share the CPU of P. + * A deadline owner keeps its own runtime after a boost. O and D share + * the CPU of P. * * owner O: 5ms every 100ms donor D: 2ms every 10ms * @@ -1867,11 +1966,10 @@ static void *refill_owner(void *arg) * takes pi_lock, unlocks * reads its dl.runtime * - * A boosted task that runs out of runtime is not throttled. Its runtime - * is refilled from the parameters of its donor. Here O has used about 3ms - * of its own 5ms at the unlock, and was never refilled. It must keep what - * it has left. A new period would hand it a full runtime at every - * contended unlock. The case fails if O has 4ms or more. + * O has used about 3ms of its own 5ms at the unlock, and was never + * refilled from D. It must keep what it has left. A new period would + * hand it a full runtime at every contended unlock. The case fails if O + * has 4ms or more. */ static int deboost_keeps_own_runtime(void) { @@ -1900,7 +1998,7 @@ static int deboost_keeps_own_runtime(void) join(d); return KSFT_FAIL; } - if (pthread_create(&o, NULL, refill_owner, (void *)1L)) { + if (pthread_create(&o, NULL, refill_owner, NULL)) { ksft_print_msg("the owner thread could not start\n"); sem_post(&owner_go); join(d); @@ -1940,17 +2038,27 @@ 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, to a constrained deadline: 8ms in 20ms, every 100ms. + * - 4: the thread sleeps. The main thread changes it to 8ms in 20ms, + * every 100ms. + * The new parameters are 10ms every 100ms, except in modes 3 and 4. + * 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); @@ -1959,8 +2067,20 @@ 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 == 4) { + sem_post(&ret_ready); + sem_wait_sig(&ret_go); + } else if (mode == 3) { + err = set_constrained(0, 8 * NSEC_PER_MSEC, 20 * NSEC_PER_MSEC, PERIOD); + } 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) @@ -1970,16 +2090,15 @@ 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 3 and 4. 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; int err, ret = KSFT_PASS; + void *t_ret = NULL; pthread_t t; if (sem_init(&ret_ready, 0, 0) || sem_init(&ret_go, 0, 0)) { @@ -1987,27 +2106,33 @@ static int return_asleep_new_params(void) return KSFT_FAIL; } ret_first_period_ms = -1; - if (pthread_create(&t, NULL, returner, NULL)) { + if (pthread_create(&t, NULL, returner, (void *)mode)) { ksft_print_msg("the task thread could not start\n"); return KSFT_FAIL; } - if (sem_wait_for(&ret_ready, 5000)) { - /* The thread may get there late. Let it end. */ + if (mode == 1 || mode == 4) { + 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 == 4) + 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 (err) { @@ -2025,11 +2150,1649 @@ 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); +} + +/* + * 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(3); +} + +/* + * 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(4); +} + +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//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)) + +/* + * A task of 10ms in 20ms, every 500ms, entered the class at @start. It + * leaves the class early in its second period. It keeps its bandwidth + * until its 0-lag time, before its third period at 1000ms. Wait for that + * period, so that measured() probes after the release. + */ +static void wait_third_period(uint64_t start) +{ + uint64_t end = start + 10 * PERIOD + 50 * NSEC_PER_MSEC, now = now_ns(); + + if (start && now < end) + sleep_ns(end - now); +} + +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 */ + wait_third_period(rb2_t_start); + 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]); + wait_third_period(ra_t_start); + 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 pthread_mutex_t pi_lock2; static sem_t holder_go; static atomic_int holder_holds, chain_waits, donor_done, x_waits, l2_takes; static atomic_int chain_rank, x_rank; -static _Atomic pid_t x_tid; +static _Atomic pid_t x_tid, fair_tid; /* The holder thread: take pi_lock2, and hold it until holder_go. */ static void *l2_holder(void *arg) @@ -2071,8 +3834,8 @@ static int take_l2_ranked(atomic_int *rank) * boost refills its runtime from the donor's 40ms. Then wait on pi_lock2 * while holding pi_lock. The donor's timeout deboosts the owner during * that wait. The owner then has more runtime left than its own 10ms. - * Return NULL, 1 on an error, or 2 if the spin took over 50ms: the owner - * was throttled, so it was not boosted. + * Return NULL, or 1 on an error. Return 2 if the spin took over 50ms. + * The owner was then throttled, so it was not boosted. */ static void *chain_owner(void *arg) { @@ -2187,6 +3950,7 @@ static int pi_init_lock2(void) x_rank = -1; donor_tid = 0; x_tid = 0; + fair_tid = 0; return 0; } @@ -2285,6 +4049,137 @@ static int deboost_blocked_keeps_order(void) return ret; } +static int dl_waiter_spin_ms; + +/* + * A waiter of pi_lock2 that is a deadline task of 400ms every 1s. It + * spins dl_waiter_spin_ms of CPU time once it starts its period, then + * waits. It leaves the class at the end. + */ +static void *dl_waiter(void *arg) +{ + int err; + + fair_tid = gettid_(); + err = set_deadline(0, 400 * NSEC_PER_MSEC, NSEC_PER_SEC); + if (err) { + ksft_print_msg("a waiter refused as a deadline task: %s\n", strerror(-err)); + return (void *)1; + } + spin_ns(dl_waiter_spin_ms * NSEC_PER_MSEC); + chain_waits = 1; + err = take_l2_ranked(&chain_rank); + if (set_other(0)) + ksft_print_msg("a waiter cannot leave SCHED_DEADLINE\n"); + return err ? (void *)1 : NULL; +} + +static int setattr_blocked_case(int reenter); + +/* + * Check the order of the waiters of pi_lock2 when sched_setattr() changes + * the parameters of one of them while it waits. The holder has pi_lock2: + * 1. W, a deadline task of 400ms every 1s, waits on pi_lock2. Its + * deadline is about 1s ahead. + * 2. The other waiter, a deadline task of 5ms every 50ms, waits on + * pi_lock2. Its deadline is about 50ms ahead. + * 3. The main thread gives W 1ms every 10ms. W has not run, so it owes + * nothing: it starts a new period at once. Its deadline is 10ms ahead. + * 4. The holder drops pi_lock2. + * + * W must take pi_lock2 first. W is not queued at step 3. Unless its + * period is fitted to its new parameters then, its waiter node keeps the + * deadline 1s ahead. + */ +static int setattr_blocked_new_period(void) +{ + return setattr_blocked_case(0); +} + +/* + * The same, when W leaves the class and comes back while it waits. W + * first spins 100ms: 60ms more than its share, so its 0-lag time is about + * 250ms ahead. At step 3, the main thread switches W to SCHED_OTHER, then + * back to SCHED_DEADLINE with 3ms every 10ms. W keeps its parameters + * until its 0-lag time, so it comes back with that debt. Repaid at 30%, + * the debt puts W's next period about 190ms ahead. W must take pi_lock2 + * after the other waiter. A new period at once, as a wakeup would start, + * would let W take it first. + */ +static int setattr_blocked_reenter(void) +{ + return setattr_blocked_case(1); +} + +/* + * Run setattr_blocked_new_period(), or setattr_blocked_reenter() if + * @reenter is set. + */ +static int setattr_blocked_case(int reenter) +{ + void *w_ret = NULL, *x_ret = NULL; + int ret = KSFT_FAIL, have_w = 0, have_x = 0, err; + pthread_t h, w, x; + + if (pi_init()) + return KSFT_SKIP; + if (pi_init_lock2()) { + pi_fini(); + return KSFT_SKIP; + } + if (pthread_create(&h, NULL, l2_holder, NULL)) { + ksft_print_msg("the holder thread could not start\n"); + goto out_fini; + } + if (wait_for(5000, holder_holds) || holder_holds != 1) { + ksft_print_msg("the holder never took pi_lock2\n"); + goto out; + } + dl_waiter_spin_ms = reenter ? 100 : 0; + have_w = !pthread_create(&w, NULL, dl_waiter, NULL); + if (!have_w || wait_for(1000, chain_waits && proc_state_of(fair_tid) == 'S')) { + ksft_print_msg("the waiter W never waited on pi_lock2\n"); + goto out; + } + have_x = !pthread_create(&x, NULL, early_waiter, NULL); + if (!have_x || wait_for(1000, x_waits && proc_state_of(x_tid) == 'S')) { + ksft_print_msg("the other waiter never waited on pi_lock2\n"); + goto out; + } + err = reenter ? set_other(fair_tid) : 0; + if (err) { + ksft_print_msg("W cannot leave SCHED_DEADLINE: %s\n", strerror(-err)); + goto out; + } + err = set_deadline(fair_tid, (reenter ? 3 : 1) * NSEC_PER_MSEC, 10 * NSEC_PER_MSEC); + if (err) { + ksft_print_msg("new parameters for W refused: %s\n", strerror(-err)); + goto out; + } + ret = KSFT_PASS; +out: + sem_post(&holder_go); /* pi_lock2 goes to its top waiter */ + if (have_x && pthread_join(x, &x_ret)) + ret = KSFT_FAIL; + if (have_w && pthread_join(w, &w_ret)) + ret = KSFT_FAIL; + join(h); + if (w_ret || x_ret) { + ret = KSFT_FAIL; /* the thread said why */ + } else if (ret == KSFT_PASS && !reenter && chain_rank > x_rank) { + ksft_print_msg("W took pi_lock2 after the other waiter\n"); + ret = KSFT_FAIL; + } else if (ret == KSFT_PASS && reenter && chain_rank < x_rank) { + ksft_print_msg("W took pi_lock2 before the other waiter\n"); + ret = KSFT_FAIL; + } +out_fini: + pthread_mutex_destroy(&pi_lock2); + sem_destroy(&holder_go); + pi_fini(); + return ret; +} + /* * The threads of pi_chain_donor_leaves(): O, M, W, Z and Y. leave_tid[] * holds their tids. leave_y_ns is the CPU time Y gets in O's window, or @@ -5937,6 +7832,144 @@ static int params_waking(void) return ret; } +/* Two tasks that pass a large runtime back and forth */ + +#define SW_BIG (70 * NSEC_PER_MSEC) +#define SW_SMALL NSEC_PER_MSEC +#define SW_MARGIN (10 * NSEC_PER_MSEC) /* raise a task of 1ms below this */ +#define SW_SETTLE (500 * NSEC_PER_MSEC) +#define SW_WINDOW (2 * NSEC_PER_SEC) +#define SW_MAX_SHARE 85 /* fail at this % of P's CPU */ + +/* + * sw_state[i] is 1 once task i is in P, and -1 if it could not enter. + * sw_state[2 + i] is the CPU time it used, in us. + */ +static atomic_int *sw_state; + +/* + * Fork task @i of params_swap_overload() into P. It spins, and writes + * the CPU time it used to sw_state[2 + @i]. Return its pid, or -1. + */ +static pid_t sw_task(int i) +{ + pid_t pid = fork_child("a task of the swap"); + uint64_t c0; + + if (pid) + return pid; + if (enter(cg_p, getpid())) { + sw_state[i] = -1; + _exit(1); + } + c0 = thread_time_ns(); + sw_state[i] = 1; + for (;;) + sw_state[2 + i] = (thread_time_ns() - c0) / NSEC_PER_USEC; +} + +/* + * Pass a runtime of 70ms back and forth between two tasks, as in + * Example 1 of "On the Defectiveness of SCHED_DEADLINE w.r.t. Tardiness + * and Affinities, and a Partial Fix", Tang, Anderson and Abeni, RTNS + * 2021. P has one CPU. A and B are processes in P that spin. The main + * process, outside P, changes their parameters with sched_setattr(): + * 1. It makes A a deadline task of 70ms every 100ms. This releases a + * job of 70ms. It then cuts the runtime of A to 1ms. + * 2. It does the same with B. + * 3. For 2.5s, it polls the runtime left of A and B in /proc. It cuts a + * task of 70ms to 1ms once the task is refilled. It raises a task of + * 1ms to 70ms when less than 10ms is left, if the other one is a task + * of 1ms. The next job of the raised task then gets 70ms. + * + * Admission control sees at most 71% of the CPU, plus the fair server. + * If a change keeps the runtime of the job in progress, every job gets + * 70ms. A and B then ask for 140% of the CPU, and get the whole CPU. A + * change must cap the runtime left at the new runtime, and keep the debt + * of the task. A and B then get no more than their 71%. The case fails if + * they get 85% of the CPU or more in the last 2s. + * + * The paper uses 94ms. P cannot admit 94ms and 1ms: its limit is 95%, + * and its fair server takes 5% of it. In the paper, the tardiness also + * grows without bound. Here it does not: a change of a task past its + * deadline starts a new period. So the case measures the CPU time of A + * and B, not their deadlines. + */ +static int params_swap_overload(void) +{ + uint64_t param[2] = { SW_SMALL, SW_SMALL }, start, t, t0 = 0, t1; + int cpu0[2] = { 0, 0 }, err = 0, i, changes = 0, ret = KSFT_FAIL; + pid_t pid[2] = { -1, -1 }; + long long used[2]; + + sw_state = mv_state + 8; + for (i = 0; i < 4; i++) + sw_state[i] = 0; + pid[0] = sw_task(0); + pid[1] = sw_task(1); + if (pid[0] < 0 || pid[1] < 0) + goto out; + if (wait_for(5000, sw_state[0] && sw_state[1]) || + sw_state[0] < 0 || sw_state[1] < 0) { + ksft_print_msg("the tasks could not enter P\n"); + goto out; + } + for (i = 0; i < 2 && !err; i++) { + err = set_deadline(pid[i], SW_BIG, PERIOD); + if (!err) + err = set_deadline(pid[i], SW_SMALL, PERIOD); + } + if (err) { + ksft_print_msg("no room for a task of 70ms in P: %s\n", strerror(-err)); + ret = err == -EBUSY ? KSFT_SKIP : KSFT_FAIL; + goto out; + } + start = now_ns(); + while (!err && (t = now_ns()) < start + SW_SETTLE + SW_WINDOW) { + if (!t0 && t >= start + SW_SETTLE) { + t0 = t; + cpu0[0] = sw_state[2]; + cpu0[1] = sw_state[3]; + } + for (i = 0; i < 2 && !err; i++) { + long long left = dl_runtime_left(pid[i]); + uint64_t to; + + if (param[i] == SW_BIG && left > (long long)SW_BIG / 2) + to = SW_SMALL; + else if (param[i] == SW_SMALL && left < (long long)SW_MARGIN && + param[!i] == SW_SMALL) + to = SW_BIG; + else + continue; + err = set_deadline(pid[i], to, PERIOD); + param[i] = to; + changes++; + } + sleep_ns(100 * NSEC_PER_USEC); + } + t1 = now_ns(); + used[0] = sw_state[2] - cpu0[0]; + used[1] = sw_state[3] - cpu0[1]; + if (err) { + ksft_print_msg("a change refused: %s\n", strerror(-err)); + goto out; + } + if (100000 * (used[0] + used[1]) >= SW_MAX_SHARE * (long long)(t1 - t0)) { + ksft_print_msg("A %lld%%, B %lld%% of the CPU, %d changes\n", + 100000 * used[0] / (long long)(t1 - t0), + 100000 * used[1] / (long long)(t1 - t0), changes); + ksft_print_msg("A and B got %d%% or more, with 71%% admitted\n", + SW_MAX_SHARE); + goto out; + } + ret = KSFT_PASS; +out: + reap(pid[0]); + reap(pid[1]); + return ret; +} + /* A random mix of the transitions above */ #define MIX_WORKERS 6 @@ -10495,13 +12528,37 @@ static const struct p_case { { "params_shrunk_long_period", params_shrunk_long_period, MEASURED_IN_P }, { "params_throttled", params_throttled, MEASURED_IN_P }, { "params_refused", params_refused, IN_P }, + { "params_return_full", params_return_full, MEASURED_IN_P }, { "params_keep_class_change", params_keep_class_change, MEASURED_IN_P }, { "params_keep_boosted", params_keep_boosted, MEASURED_IN_P }, { "boosted_owner_policy", boosted_owner_policy, MEASURED_IN_P }, { "deboost_resets_period", deboost_resets_period, MEASURED_IN_P }, { "deboost_keeps_own_runtime", deboost_keeps_own_runtime, MEASURED_IN_P }, + { "return_running_new_params", return_running_new_params, MEASURED_IN_P }, { "return_asleep_new_params", return_asleep_new_params, MEASURED_IN_P }, + { "change_running_new_params", change_running_new_params, MEASURED_IN_P }, + { "change_constrained_new_params", change_constrained_new_params, MEASURED_IN_P }, + { "wake_constrained_new_params", wake_constrained_new_params, MEASURED_IN_P }, + { "constrained_gap_change_running", constrained_gap_change_running, MEASURED_IN_P }, + { "reenter_pi_blocked_in_gap", reenter_pi_blocked_in_gap, MEASURED_IN_P }, + { "reenter_asleep_in_gap", reenter_asleep_in_gap, MEASURED_IN_P }, + { "reenter_asleep_late_in_gap", reenter_asleep_late_in_gap, MEASURED_IN_P }, + { "reenter_runnable_in_gap", reenter_runnable_in_gap, MEASURED_IN_P }, + { "wake_after_deadline_shrunk", wake_after_deadline_shrunk, MEASURED_IN_P }, + { "deadline_grows_running", deadline_grows_running, MEASURED_IN_P }, + { "deadline_widened_asleep", deadline_widened_asleep, MEASURED_IN_P }, + { "period_shrunk_throttled", period_shrunk_throttled, MEASURED_IN_P }, + { "period_shrunk_throttled_started", period_shrunk_throttled_started, MEASURED_IN_P }, + { "period_shrunk_running", period_shrunk_running, MEASURED_IN_P }, + { "period_grown_running", period_grown_running, MEASURED_IN_P }, + { "period_grown_untouched", period_grown_untouched, MEASURED_IN_P }, + { "runtime_raised_waited", runtime_raised_waited, MEASURED_IN_P }, + { "params_flip_no_gain", params_flip_no_gain, MEASURED_IN_P }, + { "period_shrunk_no_burst", period_shrunk_no_burst, MEASURED_IN_P }, + { "constrained_after_job", constrained_after_job, MEASURED_IN_P }, { "deboost_blocked_keeps_order", deboost_blocked_keeps_order, MEASURED_IN_P }, + { "setattr_blocked_new_period", setattr_blocked_new_period, MEASURED_IN_P }, + { "setattr_blocked_reenter", setattr_blocked_reenter, MEASURED_IN_P }, { "pi_chain_donor_leaves", pi_chain_donor_leaves, MEASURED_IN_P }, { "move_asleep", move_asleep, FROM_OUTSIDE }, { "move_throttled", move_throttled, FROM_OUTSIDE }, @@ -10552,6 +12609,7 @@ static const struct p_case { { "server_states", server_states, FROM_OUTSIDE }, { "server_reclaim", server_reclaim, MEASURED_IN_P }, { "params_waking", params_waking, FROM_OUTSIDE }, + { "params_swap_overload", params_swap_overload, FROM_OUTSIDE }, { "random_mix", random_mix, FROM_OUTSIDE }, { "move_normalized", move_normalized, FROM_OUTSIDE }, }; @@ -11226,6 +13284,17 @@ static const struct hp_case { #define N_HP_CASES ((int)ARRAY_SIZE(hp_cases)) +/* + * Return the this_bw of the runqueue of @cpu, or -1 if sched_debug does + * not show it. + */ +static long this_bw_of(int cpu) +{ + long this_bw, running_bw; + + return rq_bw_of(cpu, &this_bw, &running_bw) ? -1 : this_bw; +} + /* Run the cases of p_cases[], each with a P made anew. */ static void run_p_cases(void) { @@ -11250,6 +13319,12 @@ static void run_p_cases(void) } for (i = 0; i < N_P_CASES; i++) { const struct p_case *c = &p_cases[i]; + long long bw0 = total_bw_of(top_cpu(1)); + long this0[3]; + int k; + + for (k = 0; k < 3; k++) + this0[k] = this_bw_of(top_cpu(k + 1)); if (ok && make_p()) { remove_p(); @@ -11269,6 +13344,34 @@ static void run_p_cases(void) ret = c->fn(); remove_p(); server_restore(); + /* + * A task of the case can keep its bandwidth counted after it + * exits: until its 0-lag time, or until its debt is repaid. Wait + * for that, so that the next case starts from the same total_bw. + * A lower total_bw also ends the wait. Bandwidth lost by a case + * would never come back, and the wait would then cost the whole + * 10s. + */ + if (bw0 >= 0 && + wait_for_step(10000, 10 * NSEC_PER_MSEC, + total_bw_of(top_cpu(1)) <= bw0)) + ksft_print_msg("%s: total_bw %lld after the case, %lld before\n", + c->name, total_bw_of(top_cpu(1)), bw0); + /* + * this_bw can stay higher than total_bw shows. A task moved to + * another cpuset while it sleeps leaves its bandwidth in the + * this_bw of its old CPU until it wakes up or dies. Wait for + * the three CPUs that random_mix() checks. + */ + for (k = 0; k < 3; k++) { + int cpu = top_cpu(k + 1); + + if (this0[k] >= 0 && + wait_for_step(10000, 10 * NSEC_PER_MSEC, + this_bw_of(cpu) <= this0[k])) + ksft_print_msg("%s: this_bw of CPU %d %ld after the case, %ld before\n", + c->name, cpu, this_bw_of(cpu), this0[k]); + } report(c->name, ret); } if (added && write_file(root, "cgroup.subtree_control", "-cpuset")) -- 2.47.3