From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: juri.lelli@redhat.com, peterz@infradead.org, mingo@redhat.com,
vincent.guittot@linaro.org
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
tglx@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,
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
Subject: [PATCH v10 24/40] selftests/sched: Test the rtmutex chain walk in deadline_bw_test
Date: Fri, 20 Nov 2026 15:42:01 -0500 [thread overview]
Message-ID: <bd1d884143e64a1d5fc73e8af268af995627dc54.1795207232.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1795207232.git.mathieu.desnoyers@efficios.com>
Add pi_chain_top_moves and pi_chain_top_moves_rt to deadline_bw_test.
They test the two rtmutex fixes earlier in this series. In both, a
chain walk changes the top waiter of an owner to a waiter of another
lock it holds. The walk must go on to the next owner. That owner must
then get the new donor, or the new priority. The first case uses
deadline tasks, the second RT tasks. The comment above each
case gives its scenario.
On v7.4-rc2, in 8 VMs of 8 CPUs, pi_chain_top_moves_rt fails in every
VM.
pi_chain_top_moves passes there. It fails with the first series, until
"rtmutex: Continue the chain walk when a waiter's deadline donor
changes" and "rtmutex: Continue the walk when the owner's top waiter
changes lock". It needs both.
With the fixes up to here, both cases pass wherever they run.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
.../sched/deadline/deadline_bw_test.c | 293 ++++++++++++++++++
1 file changed, 293 insertions(+)
diff --git a/tools/testing/selftests/sched/deadline/deadline_bw_test.c b/tools/testing/selftests/sched/deadline/deadline_bw_test.c
index 7b0a3f2fb12b..cf3b49cf2be2 100644
--- a/tools/testing/selftests/sched/deadline/deadline_bw_test.c
+++ b/tools/testing/selftests/sched/deadline/deadline_bw_test.c
@@ -2522,6 +2522,297 @@ static int pi_chain_donor_leaves(void)
return ret;
}
+/*
+ * The threads of top_moves(): O, M, W, Z and Y. top_tid[] holds their
+ * tids. top_lock is L1'. top_y_ns is the CPU time Y gets in O's window,
+ * or -1.
+ */
+enum { TM_O, TM_M, TM_W, TM_Z, TM_Y, TM_N };
+static _Atomic pid_t top_tid[TM_N];
+static pthread_mutex_t top_lock;
+static atomic_int top_err, top_rt, top_m_locking, top_w_locking, top_y_stop;
+static _Atomic long long top_y_ns;
+static sem_t top_o_go, top_m_go;
+
+/* The priority /proc shows for a SCHED_FIFO task of priority @p. */
+#define FIFO_TASK_PRIO(p) (-1 - (p))
+
+/*
+ * O: take pi_lock2 (L2), and sleep until top_o_go. With Y (@arg), spin
+ * 200ms and measure the CPU time Y gets meanwhile. Then unlock L2.
+ */
+static void *top_o(void *arg)
+{
+ uint64_t t0, y0;
+
+ if (pthread_mutex_lock(&pi_lock2)) {
+ top_err = 1;
+ return arg;
+ }
+ top_tid[TM_O] = gettid_(); /* O holds L2 */
+ sem_wait_sig(&top_o_go);
+ if (arg) {
+ y0 = cpu_time_of(*(pthread_t *)arg);
+ t0 = now_ns();
+ while (now_ns() - t0 < 2 * PERIOD)
+ ;
+ top_y_ns = cpu_time_of(*(pthread_t *)arg) - y0;
+ }
+ pthread_mutex_unlock(&pi_lock2);
+ return NULL;
+}
+
+/*
+ * M: take pi_lock (L1) and top_lock (L1'). At top_m_go, block on L2,
+ * which O holds. Unlock L2, L1 and L1' once it gets L2.
+ */
+static void *top_m(void *arg)
+{
+ if (pthread_mutex_lock(&pi_lock)) {
+ top_err = 1;
+ return arg;
+ }
+ if (pthread_mutex_lock(&top_lock)) {
+ pthread_mutex_unlock(&pi_lock);
+ top_err = 1;
+ return arg;
+ }
+ top_tid[TM_M] = gettid_(); /* M holds L1 and L1' */
+ sem_wait_sig(&top_m_go);
+ top_m_locking = 1;
+ if (pthread_mutex_lock(&pi_lock2))
+ top_err = 1;
+ else
+ pthread_mutex_unlock(&pi_lock2);
+ pthread_mutex_unlock(&pi_lock);
+ pthread_mutex_unlock(&top_lock);
+ return arg;
+}
+
+/*
+ * W and Z: W blocks on L1', Z on L1. @arg is 1 for W. With top_rt, W
+ * becomes SCHED_FIFO 50 and Z SCHED_FIFO 20. Otherwise W becomes a
+ * deadline task of 2ms every 80ms and Z one of 5ms every 200ms. The main
+ * thread switches W to SCHED_OTHER while it waits.
+ */
+static void *top_waiter(void *arg)
+{
+ int w = (long)arg;
+ struct sched_param sp = { .sched_priority = w ? 50 : 20 };
+ uint64_t runtime = (w ? 2 : 5) * NSEC_PER_MSEC;
+ pthread_mutex_t *l = w ? &top_lock : &pi_lock;
+
+ if (top_rt ? sched_setscheduler(0, SCHED_FIFO, &sp) :
+ set_deadline(0, runtime, 40 * runtime)) {
+ top_err = 1;
+ return arg;
+ }
+ top_tid[w ? TM_W : TM_Z] = gettid_();
+ if (w)
+ top_w_locking = 1;
+ if (pthread_mutex_lock(l))
+ top_err = 1;
+ else
+ pthread_mutex_unlock(l);
+ if (set_other(0))
+ top_err = 1;
+ return arg;
+}
+
+/* Y: become a deadline task of 30ms every 100ms, and spin. */
+static void *top_y(void *arg)
+{
+ if (set_deadline(0, 30 * NSEC_PER_MSEC, PERIOD)) {
+ top_err = 1;
+ return arg;
+ }
+ top_tid[TM_Y] = gettid_();
+ while (!top_y_stop)
+ ;
+ return arg;
+}
+
+/*
+ * A chain walk must go on past a task whose top waiter moves to another
+ * lock. The priority of that task changes, and the next owner must
+ * follow.
+ *
+ * L1 is pi_lock, L1' is top_lock and L2 is pi_lock2. All threads run on
+ * the CPU of P. W and Z set their own policy when they start. With @rt,
+ * W is SCHED_FIFO 50 and Z SCHED_FIFO 20. Without @rt, W is a deadline
+ * task of 2ms every 80ms and Z one of 5ms every 200ms. W has the earlier
+ * deadline. O and M are SCHED_OTHER. Without @rt, Y is a deadline task
+ * of 30ms every 100ms. It spins all along.
+ *
+ * 1. O takes L2, and sleeps.
+ * 2. M takes L1 and L1', and sleeps.
+ * 3. W blocks on L1'. W boosts M.
+ * 4. M blocks on L2. M passes W on to O.
+ * 5. Z blocks on L1. W stays the top waiter of M.
+ * 6. The main thread switches W to SCHED_OTHER. The chain walk requeues
+ * W on L1'. Z on L1 becomes the top waiter of M, and Z boosts M. The
+ * walk must go on to O. Z must boost O too.
+ *
+ * With @rt, the case reads O's priority. It must be the one of Z. With
+ * the walk stopped at M, O keeps the priority of W.
+ *
+ * Without @rt, W sleeps and has run almost nothing. So its parameters
+ * are cleared at once. 250ms later, the main thread wakes O up. O
+ * measures the CPU time Y gets over the next 200ms, and unlocks L2.
+ *
+ * With Z as its donor, O gets 5ms every 200ms. Y runs at the start of
+ * each of its periods, and gets its 60ms. With W as its donor, O runs on
+ * cleared parameters. Each refill gives it no runtime and a deadline of
+ * about now. O then runs before Y until Y's deadline is past. Y runs
+ * only then, one period late, and gets about 30ms. The case fails if Y
+ * gets less than 45ms.
+ *
+ * W does not exit before O unlocks L2. It waits for L1', which M
+ * releases after it gets L2.
+ */
+static int top_moves(int rt)
+{
+ int ret = KSFT_FAIL, i, have[TM_N] = { 0 };
+ int boost = rt ? FIFO_TASK_PRIO(50) : DL_TASK_PRIO;
+ pthread_mutexattr_t a;
+ pthread_t t[TM_N];
+ long long y_ns;
+ int err, o_prio = 0;
+
+ if (pi_init())
+ return KSFT_SKIP;
+ if (pi_init_lock2()) {
+ pi_fini();
+ return KSFT_SKIP;
+ }
+ err = pthread_mutexattr_init(&a);
+ if (!err) {
+ err = pthread_mutexattr_setprotocol(&a, PTHREAD_PRIO_INHERIT);
+ if (!err)
+ err = pthread_mutex_init(&top_lock, &a);
+ pthread_mutexattr_destroy(&a);
+ }
+ if (err) {
+ ksft_print_msg("no third priority inheritance mutex: %s\n",
+ strerror(err));
+ ret = KSFT_SKIP;
+ goto out_lock2;
+ }
+ if (sem_init(&top_o_go, 0, 0) || sem_init(&top_m_go, 0, 0)) {
+ ksft_print_msg("sem_init: %s\n", strerror(errno));
+ goto out_lock3;
+ }
+ for (i = 0; i < TM_N; i++)
+ top_tid[i] = 0;
+ top_err = 0;
+ top_rt = rt;
+ top_m_locking = 0;
+ top_w_locking = 0;
+ top_y_stop = 0;
+ top_y_ns = -1;
+ if (!rt) {
+ have[TM_Y] = !pthread_create(&t[TM_Y], NULL, top_y, NULL);
+ if (!have[TM_Y] || wait_for(1000, top_tid[TM_Y] || top_err)) {
+ ksft_print_msg("Y never became a deadline task\n");
+ goto out;
+ }
+ }
+ have[TM_O] = !pthread_create(&t[TM_O], NULL, top_o,
+ rt ? NULL : &t[TM_Y]);
+ if (!have[TM_O] || wait_for(1000, top_tid[TM_O] || top_err)) {
+ ksft_print_msg("O never took L2\n");
+ goto out;
+ }
+ have[TM_M] = !pthread_create(&t[TM_M], NULL, top_m, NULL);
+ if (!have[TM_M] || wait_for(1000, top_tid[TM_M] || top_err)) {
+ ksft_print_msg("M never took L1 and L1'\n");
+ goto out;
+ }
+ have[TM_W] = !pthread_create(&t[TM_W], NULL, top_waiter, (void *)1L);
+ if (!have[TM_W] ||
+ wait_for(1000, (top_w_locking &&
+ proc_state_of(top_tid[TM_W]) == 'S') || top_err)) {
+ ksft_print_msg("W never blocked on L1'\n");
+ goto out;
+ }
+ sem_post(&top_m_go);
+ if (wait_for(1000, (top_m_locking && prio_of(top_tid[TM_O]) == boost) ||
+ top_err)) {
+ ksft_print_msg("O was not boosted through M\n");
+ goto out;
+ }
+ have[TM_Z] = !pthread_create(&t[TM_Z], NULL, top_waiter, (void *)0L);
+ if (!have[TM_Z] ||
+ wait_for(1000, (top_tid[TM_Z] &&
+ proc_state_of(top_tid[TM_Z]) == 'S') || top_err)) {
+ ksft_print_msg("Z never blocked on L1\n");
+ goto out;
+ }
+ sleep_ns(10 * NSEC_PER_MSEC); /* the chain walk of Z is done */
+ err = top_err ? -EINVAL : set_other(top_tid[TM_W]);
+ if (err) {
+ ksft_print_msg("W cannot leave its class: %s\n",
+ strerror(-err));
+ goto out;
+ }
+ if (rt) {
+ sleep_ns(10 * NSEC_PER_MSEC);
+ if (prio_of(top_tid[TM_M]) != FIFO_TASK_PRIO(20)) {
+ ksft_print_msg("Z does not boost M\n");
+ goto out;
+ }
+ o_prio = prio_of(top_tid[TM_O]);
+ } else {
+ sleep_ns(250 * NSEC_PER_MSEC);
+ }
+ ret = KSFT_PASS;
+out:
+ /* Let every thread end: O unlocks L2, then M unlocks L1 and L1'. */
+ sem_post(&top_m_go);
+ sem_post(&top_o_go);
+ for (i = TM_O; i < TM_Y; i++)
+ if (have[i])
+ join(t[i]);
+ top_y_stop = 1;
+ if (have[TM_Y])
+ join(t[TM_Y]);
+ y_ns = top_y_ns;
+ if (top_err) {
+ ksft_print_msg("a thread of the chain failed\n");
+ ret = KSFT_FAIL;
+ } else if (ret == KSFT_PASS && rt && o_prio != FIFO_TASK_PRIO(20)) {
+ ksft_print_msg("O runs at priority %d, not %d: W still boosts it\n",
+ o_prio, FIFO_TASK_PRIO(20));
+ ret = KSFT_FAIL;
+ } else if (ret == KSFT_PASS && !rt &&
+ y_ns < 45 * (long long)NSEC_PER_MSEC) {
+ ksft_print_msg("Y ran %lld ms of 200: W is still the donor of O\n",
+ y_ns / (long long)NSEC_PER_MSEC);
+ ret = KSFT_FAIL;
+ }
+ sem_destroy(&top_o_go);
+ sem_destroy(&top_m_go);
+out_lock3:
+ pthread_mutex_destroy(&top_lock);
+out_lock2:
+ pthread_mutex_destroy(&pi_lock2);
+ sem_destroy(&holder_go);
+ pi_fini();
+ return ret;
+}
+
+/* W and Z are deadline tasks. */
+static int pi_chain_top_moves(void)
+{
+ return top_moves(0);
+}
+
+/* W and Z are SCHED_FIFO tasks. */
+static int pi_chain_top_moves_rt(void)
+{
+ return top_moves(1);
+}
+
/* cpuset moves, from the main process, outside P */
/* Shared with the processes of a case: atomic in shared memory. */
@@ -10218,6 +10509,8 @@ static const struct p_case {
{ "return_asleep_new_params", return_asleep_new_params, MEASURED_IN_P },
{ "deboost_blocked_keeps_order", deboost_blocked_keeps_order, MEASURED_IN_P },
{ "pi_chain_donor_leaves", pi_chain_donor_leaves, MEASURED_IN_P },
+ { "pi_chain_top_moves", pi_chain_top_moves, MEASURED_IN_P },
+ { "pi_chain_top_moves_rt", pi_chain_top_moves_rt, MEASURED_IN_P },
{ "move_asleep", move_asleep, FROM_OUTSIDE },
{ "move_throttled", move_throttled, FROM_OUTSIDE },
{ "move_non_contending", move_non_contending, FROM_OUTSIDE },
--
2.47.3
next prev parent reply other threads:[~2026-11-20 20:42 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 20:41 [PATCH v10 00/40] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 01/40] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 02/40] sched/deadline: Rebuild the root domains' accounting from the task list Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 03/40] sched/deadline: Record the root domain holding a task's bandwidth Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 04/40] sched/deadline: Leave a dying task out of a rebuild of the accounting Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 05/40] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 06/40] selftests/sched: Test the root domain rebuild in deadline_bw_test Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 07/40] sched/deadline: Move a task's bandwidth only where room was made Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 08/40] cgroup/cpuset: Wait for an attach before rebuilding the sched domains Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 09/40] sched: Make deadline policy and affinity changes wait for a cpuset attach Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 10/40] cgroup/cpuset: Give back the bandwidth an attach did not move Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 11/40] cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 12/40] selftests/sched: Test cpuset moves in deadline_bw_test Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 13/40] sched/deadline: Make a rebuild count a task in the root domain it runs in Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 14/40] sched/deadline: Move a task's bandwidth to " Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 15/40] sched/deadline: Keep a task in the root domain that counts its bandwidth Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 16/40] sched/deadline: Keep the last CPU a task has in its root domain Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 17/40] cgroup/cpuset: Refuse to take a deadline task out of " Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 18/40] selftests/sched: Test the root domain of a task in deadline_bw_test Mathieu Desnoyers
2026-11-20 20:54 ` sashiko-bot
2026-11-20 20:41 ` [PATCH v10 19/40] sched/deadline: Count only active CPUs when spreading extra_bw Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 20/40] sched/deadline: Do not truncate a bandwidth to 32 bits in __dl_add/sub() Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 21/40] rtmutex: Continue the chain walk when a waiter's deadline donor changes Mathieu Desnoyers
2026-11-20 20:41 ` [PATCH v10 22/40] rtmutex: Do not stop the chain walk at an owner whose top waiter changed Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 23/40] sched/deadline: Hold a reference on the deadline donor of a boosted task Mathieu Desnoyers
2026-11-20 20:42 ` Mathieu Desnoyers [this message]
2026-11-20 21:06 ` [PATCH v10 24/40] selftests/sched: Test the rtmutex chain walk in deadline_bw_test sashiko-bot
2026-11-20 20:42 ` [PATCH v10 25/40] sched/deadline: Reset the donor's runtime at a deboost in the class Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 26/40] sched/deadline: Replenish a throttled task boosted while asleep Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 27/40] sched/deadline: Count the bandwidth a boosted task is given Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 28/40] sched/deadline: Start a period for a task that enters the class blocked Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 29/40] sched/deadline: Start a period at a deboost out of the class Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 30/40] sched/deadline: Start a period for a task requeued onto an rt_mutex Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 31/40] selftests/sched: Test deadline boosts in deadline_bw_test Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 32/40] sched/deadline: Cancel the replenishment timer with the parameters Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 33/40] sched/deadline: Cap the runtime left of a constrained task at dl_runtime Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 34/40] sched/deadline: Check the runtime of a task that comes back runnable Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 35/40] sched/deadline: Check the runtime of a task whose parameters change Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 36/40] sched/deadline: Reschedule a running task throttled at its enqueue Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 37/40] sched/deadline: Keep the start of the period across a change of dl_deadline Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 38/40] sched/deadline: Boost an owner that cannot run on its own reservation Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 39/40] selftests/sched: Test parameter changes in deadline_bw_test Mathieu Desnoyers
2026-11-20 20:42 ` [PATCH v10 40/40] selftests/sched: Share the helpers of the deadline tests Mathieu Desnoyers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bd1d884143e64a1d5fc73e8af268af995627dc54.1795207232.git.mathieu.desnoyers@efficios.com \
--to=mathieu.desnoyers@efficios.com \
--cc=bigeasy@linutronix.de \
--cc=boqun@kernel.org \
--cc=bsegall@google.com \
--cc=cgroups@vger.kernel.org \
--cc=clrkwllms@kernel.org \
--cc=dietmar.eggemann@arm.com \
--cc=hannes@cmpxchg.org \
--cc=juri.lelli@redhat.com \
--cc=kayracizmeci@gmail.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=longman@redhat.com \
--cc=luca.abeni@santannapisa.it \
--cc=m.szyprowski@samsung.com \
--cc=mathieu.poirier@linaro.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=mkoutny@suse.com \
--cc=parri.andrea@gmail.com \
--cc=peterz@infradead.org \
--cc=ridong.chen@linux.dev \
--cc=rostedt@goodmis.org \
--cc=shuah@kernel.org \
--cc=tglx@kernel.org \
--cc=tj@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=weihengqiuu@gmail.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®