Linux real-time development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>
Cc: linux-rt-devel@lists.linux.dev, tj@kernel.org,
	hannes@cmpxchg.org, mkoutny@suse.com,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 16/16] selftests/sched: Add a test of SCHED_DEADLINE bandwidth accounting
Date: Wed, 18 Nov 2026 22:07:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-222083@kernel.org> (raw)
In-Reply-To: <a94584f87598e48fece3c4eb9ec5f65e2a442abb.1795037078.git.mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] selftests/sched: unhandled SCHED_FIFO EPERM in
  server_client()
- [Medium] selftests/sched: pi_chain_donor_leaves() fails due to
  unpropagated dl.pi_se
- [Medium] selftests/sched: zero waiter deadline in
  deboost_new_params_reboost()
- [Low] selftests/sched: process-private ready flag in shm_lock_init()
- [Low] selftests/sched: write-only fair_tid, unused isol, and dead mode
  branches
--

commit 027ae545afaed0101b592ecd773abaab9b25aeca
Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

selftests/sched: Add a test of SCHED_DEADLINE bandwidth accounting

This commit adds the deadline_bw_test selftest covering SCHED_DEADLINE
bandwidth accounting across priority inheritance, parameter changes,
cpuset moves, task exits, bandwidth reclaiming, fair servers, and CPU
hotplug.

Link: https://github.com/jlelli/sched-deadline-tests

> diff --git a/tools/testing/selftests/sched/deadline/deadline_bw_test.c b/tools/testing/selftests/sched/deadline/deadline_bw_test.c
> new file mode 100644
> index 0000000000000..d015e743c941f
> --- /dev/null
> +++ b/tools/testing/selftests/sched/deadline/deadline_bw_test.c

[ ... ]

> +static void *reboost_owner(void *arg)
> +{
> +	uint64_t t0;
> +	int err;
> +
> +	owner_tid = gettid_();
> +	if (take_pi_lock())
> +		return (void *)1;
> +	owner_holds = 2;
> +	sem_wait_sig(&owner_go);
> +	if (rb_abort) {
> +		drop_pi_lock();
> +		return NULL;
> +	}
> +	err = set_deadline(0, 50 * NSEC_PER_MSEC, PERIOD);
> +	if (err) {
> +		ksft_print_msg("the owner refused as a deadline task: %s\n",
> +			       strerror(-err));
> +		goto out;
> +	}
> +	sem_post(&rb_donor_go);
> +	t0 = now_ns();
> +	while (!rb_donor_locking || proc_state_of(donor_tid) != 'S') {
> +		if (now_ns() - t0 > NSEC_PER_SEC) {
> +			ksft_print_msg("the donor did not block on the lock\n");
> +			err = -1;
> +			goto out;
> +		}
> +	}
> +	err = set_deadline(rb_waiter_tid, 2 * NSEC_PER_MSEC, PERIOD);
> +	if (err) {
> +		ksft_print_msg("the waiter refused as a deadline task: %s\n",
> +			       strerror(-err));
> +		goto out;
> +	}
> +	err = set_deadline(0, 5 * NSEC_PER_MSEC, PERIOD);

[Severity: Medium]
Does calling set_deadline() on rb_waiter_tid while W is already blocked on
pi_lock give W an absolute deadline of 0 instead of a deadline later than O
and D?

Because W is not queued on a runqueue, __setparam_dl() sets its static
parameters while leaving W->dl.deadline at 0, and switched_to_dl() returns
early without enqueuing W.  rt_mutex_adjust_pi() then updates W's waiter
node on pi_lock with tree.deadline = 0, which sorts ahead of D
(dl_time_before(0, D->dl.deadline) is true) and makes W the top waiter
boosting O right away.  When D times out 20ms later, D is no longer the top
waiter, so remove_waiter() returns without deboosting or reboosting O.

[ ... ]

> +static int pi_init_lock2(void)
> +{

[ ... ]

> +	donor_tid = 0;
> +	x_tid = 0;
> +	fair_tid = 0;
> +	return 0;
> +}

[Severity: Low]
This isn't a bug, but fair_tid is only written here and never read (which
triggers -Wunused-but-set-variable under Clang).  In addition, main()
populates isol via read_online(&isol, 1) without using or freeing it,
wait_spinner(), hold() and release_held() have unused forward declarations,
and several helpers retain unreachable branches with no callers in
p_cases[] or hp_cases[] (sleeper_cpu >= 0 in dl_sleeper_tid(), was_dl != 0
in sleeping_owner() and pi_asleep(), modes 1 and 2 in deboost_case(),
spin_ms == 8 in refill_case(), modes 0 and 2-5 in return_case() along with
set_constrained(), MV_YIELDED in mover(), and hp_case::no_hp).

[ ... ]

> +/*
> + * A chain walk must pass a new donor on to the end of the chain. This
> + * holds even when the sort keys of the task in the middle do not change.
> + *

[ ... ]

> + * 5. Z blocks on L1. Z's deadline is earlier than W's, so Z becomes M's
> + *    donor. M sleeps, so its deadline does not move. The chain walk must
> + *    go on to O and make Z O's donor too.
> + * 6. The main thread switches W to SCHED_OTHER. W sleeps and has run
> + *    almost nothing, so its parameters are cleared at once.
> + * 7. 250ms later, the main thread wakes O up. O has no deadline ahead, so
> + *    its wakeup starts a new period from its donor. O spins 100ms.
> + *    Then it measures the CPU time Y gets over 200ms, and unlocks L2.
> + *
> + * With Z as its donor, O gets 2ms every 80ms. Y's deadline is soon the
> + * earlier one, and Y gets about 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 all the time, and Y gets close to
> + * nothing. The case fails if Y gets less than 30ms.
> + *
> + * W does not exit before O unlocks L2. It waits for L1, which M releases
> + * after it gets L2.
> + */
> +static int pi_chain_donor_leaves(void)
> +{

[ ... ]

> +	} else if (ret == KSFT_PASS && y_ns < 30 * (long long)NSEC_PER_MSEC) {
> +		ksft_print_msg("Y ran %lld ms of 200: O ran on the cleared parameters of W\n",
> +			       y_ns / (long long)NSEC_PER_MSEC);
> +		ret = KSFT_FAIL;
> +	}

[Severity: Medium]
Does pi_chain_donor_leaves() pass on this series without an rt_mutex chain
walk change to propagate M->dl.pi_se to O when M's sort keys do not move?

When Z blocks on L1 in step 5, rt_mutex_setprio(M, Z) updates M->dl.pi_se
to &Z->dl, but M is sleeping on L2, so M->prio and M->dl.deadline stay
unchanged.  In rt_mutex_adjust_prio_chain(), rt_waiter_node_equal() sees
that M's waiter node on L2 still matches task_to_waiter_node(M), skips
requeueing M on L2, and never calls rt_mutex_adjust_prio() on O:

kernel/locking/rtmutex.c:rt_mutex_adjust_prio_chain() {
    ...
	if (rt_waiter_node_equal(&waiter->tree, task_to_waiter_node(task))) {
		if (!detect_deadlock)
			goto out_unlock_pi;
		else
			requeue = false;
	}
    ...
}

O->dl.pi_se stays &W->dl after W switches to SCHED_OTHER and has its
parameters cleared.  When O wakes in step 7, replenish_dl_entity() keeps
resetting O->dl.deadline to rq_clock(rq) and O->dl.runtime to 0 from W's
cleared parameters, starving Y on P's CPU and failing this check.

[ ... ]

> +/* Set up shm_lock. Return 0, or -1 if it cannot be set up. */
> +static int shm_lock_init(void)
> +{
> +	static int ready;
> +	pthread_mutexattr_t a;
> +	int err;
> +
> +	/*
> +	 * A case can kill the owner while it holds the lock. Take the lock
> +	 * back and release it, so that it can be destroyed.
> +	 */
> +	if (ready) {
> +		err = pthread_mutex_trylock(shm_lock);
> +		if (err == EOWNERDEAD)
> +			err = pthread_mutex_consistent(shm_lock);
> +		if (!err)
> +			pthread_mutex_unlock(shm_lock);
> +		pthread_mutex_destroy(shm_lock);
> +		ready = 0;
> +	}

[Severity: Low]
Since death_pi_waiter, death_pi_owner, and death_pi_deadline_owner each run
in a separate child process forked by run_in(), doesn't the process-private
static ready variable start at 0 in every child?

In death_pi_waiter, reap(owner) kills the owner while it holds shm_lock,
leaving *shm_lock in MAP_SHARED memory in the EOWNERDEAD state.  When
death_pi_owner and death_pi_deadline_owner run in new child processes,
ready is 0, so the EOWNERDEAD recovery and pthread_mutex_destroy() block is
skipped and pthread_mutex_init() is called directly on the active mutex.
Should ready live in the MAP_SHARED mv_state buffer (or should pi_death()
clean up shm_lock before exiting)?

[ ... ]

> +static pid_t server_client(int rt)
> +{
> +	pid_t pid = fork_child(rt ? "a real-time task of P" : "a fair task of P");
> +
> +	if (!pid) {
> +		struct sched_param sp = { .sched_priority = 1 };
> +
> +		if (enter(cg_p, getpid()) ||
> +		    (rt && sched_setscheduler(0, SCHED_FIFO, &sp)))
> +			_exit(1);
> +		mv_state[0]++;
> +		for (;;) {
> +			wait_set(&mv_state[4 + rt]);
> +			spin_ns(rt ? 50 * NSEC_PER_MSEC : NSEC_PER_MSEC);
> +			if (rt)
> +				sleep_ns(NSEC_PER_MSEC);
> +		}
> +	}
> +	return pid;
> +}

[ ... ]

> +static int server_states(void)
> +{

[ ... ]

> +	fair = server_client(0);
> +	rt = server_client(1);
> +	if (fair < 0 || rt < 0 || wait_for(5000, mv_state[0] >= 2)) {
> +		if (fair >= 0 && rt >= 0)
> +			ksft_print_msg("a fair or real-time task of P could not start\n");
> +		ret = KSFT_FAIL;
> +	}

[Severity: Medium]
On a CONFIG_RT_GROUP_SCHED=y system where the cpu controller is already
enabled in root/cgroup.subtree_control, won't server_client(1) fail
sched_setscheduler(0, SCHED_FIFO, &sp) in cg_p with EPERM and cause
server_states() to time out and fail instead of skipping?

As noted in move_cancelled(), child cgroups have no RT runtime in cgroup v2
when CONFIG_RT_GROUP_SCHED is enabled with the cpu controller active.
Because move_cancelled() only disables the cpu controller on exit if it
added "+cpu" itself, sched_setscheduler() in server_client(1) fails and
calls _exit(1) without updating mv_state[0], so server_states() waits the
full 5 seconds and returns KSFT_FAIL.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795037078.git.mathieu.desnoyers@efficios.com?part=16

  reply	other threads:[~2026-11-18 22:07 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 21:24 [PATCH v2 00/16] sched/deadline: first bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-11-18 21:24 ` [PATCH v2 01/16] sched: Refuse SCHED_FLAG_KEEP_PARAMS that would change the policy or the class Mathieu Desnoyers
2026-11-18 21:24 ` [PATCH v2 02/16] sched/deadline: Do not admit bandwidth for parameters that are kept Mathieu Desnoyers
2026-11-18 21:24 ` [PATCH v2 03/16] sched: Let a task boosted by a deadline waiter change its policy Mathieu Desnoyers
2026-11-18 21:25 ` [PATCH v2 04/16] sched/deadline: Do not divide by zero active CPUs of a root domain Mathieu Desnoyers
2026-11-18 21:25 ` [PATCH v2 05/16] sched/deadline: Refuse dl_server debugfs writes while the rq is offline Mathieu Desnoyers
2026-11-18 21:25 ` [PATCH v2 06/16] sched: Take cpuset_mutex for a deadline policy change only with pi Mathieu Desnoyers
2026-11-18 21:25 ` [PATCH v2 07/16] sched/deadline: Do not lose a throttled task switched out and back Mathieu Desnoyers
2026-11-18 21:25 ` [PATCH v2 08/16] sched/deadline: Do not replenish a boost from cleared parameters Mathieu Desnoyers
2026-11-18 21:25 ` [PATCH v2 09/16] sched/deadline: Reset the deadline and the runtime with the parameters Mathieu Desnoyers
2026-11-18 21:44   ` sashiko-bot
2026-11-18 21:25 ` [PATCH v2 10/16] sched/deadline: Release the bandwidth of a task that leaves the class asleep Mathieu Desnoyers
2026-11-18 21:51   ` sashiko-bot
2026-11-18 21:25 ` [PATCH v2 11/16] sched/deadline: Reject SCHED_DEADLINE on exited tasks Mathieu Desnoyers
2026-11-18 21:25 ` [PATCH v2 12/16] sched/deadline: Fix this_bw when a task is changed during a remote wakeup Mathieu Desnoyers
2026-11-18 21:25 ` [PATCH v2 13/16] sched/deadline: Check SCHED_DEADLINE affinity under the rq lock Mathieu Desnoyers
2026-11-18 21:25 ` [PATCH v2 14/16] sched/debug: Show the deadline bandwidth of each runqueue Mathieu Desnoyers
2026-11-18 21:25 ` [PATCH v2 15/16] selftests/sched: Add a test of SCHED_DEADLINE policy round trips Mathieu Desnoyers
2026-11-18 21:25 ` [PATCH v2 16/16] selftests/sched: Add a test of SCHED_DEADLINE bandwidth accounting Mathieu Desnoyers
2026-11-18 22:07   ` sashiko-bot [this message]
2026-11-19 10:20 ` [PATCH v2 00/16] sched/deadline: first bandwidth accounting fixes and selftests Juri Lelli

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=sashiko-outbox-222083@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mkoutny@suse.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tj@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