Re: [PATCH v2 4/5] selftests/futex: Test priority inheritance on PI futexes
From: Sebastian Andrzej Siewior
Date: Mon Nov 16 2026 - 11:56:17 EST
On 2026-11-08 09:13:46 [-0500], Mathieu Desnoyers wrote:
> The PI futex selftests check what FUTEX_LOCK_PI returns. In these tests,
> the owner always sleeps. No test checks the priority an owner runs at.
> No test waits behind an owner that keeps running.
…
> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
I did play with this and looks it catches the errors you reported/ fixed
earlier in this series.
Reviewed-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
> --- /dev/null
> +++ b/tools/testing/selftests/futex/functional/futex_lock_pi_inherit.c
> @@ -0,0 +1,773 @@
…
> +static pid_t sys_gettid(void)
> +{
> + return syscall(SYS_gettid);
> +}
glibc-2.42+ has pthread_gettid_np(pthread_self()) but this is fine.
Debian Trixie has just 2.41 which is a bit sad.
> +static uint64_t now_ns(void)
> +{
> + struct timespec ts;
> +
> + clock_gettime(CLOCK_MONOTONIC, &ts);
> + return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
> +}
> +
> +/* An absolute CLOCK_REALTIME timeout @ns from now, for FUTEX_LOCK_PI. */
> +static struct timespec abs_timeout(uint64_t ns)
> +{
> + struct timespec ts;
> +
> + clock_gettime(CLOCK_REALTIME, &ts);
> + ts.tv_sec += ns / NSEC_PER_SEC;
> + ts.tv_nsec += ns % NSEC_PER_SEC;
> + if (ts.tv_nsec >= (long)NSEC_PER_SEC) {
> + ts.tv_nsec -= NSEC_PER_SEC;
> + ts.tv_sec++;
> + }
This breaks if your timeout is something like 1.5s and ts.nsec is
already > 0.5s. We usually do
| while (ts->tv_nsec >= NSEC_PER_SEC) {
| ts->tv_nsec -= NSEC_PER_SEC;
| ts->tv_sec++;
| }
and in worst case gcc does a div. But other than that…
> + return ts;
> +}
…
> +/*
> + * Start @t and wait until it sleeps where it blocks. Return false if it
> + * could not be created, if it failed, or if it did not block within 2s.
> + * Unless it could not be created, the caller must still join it.
> + */
Could rename it to start_pi_thread() or pi_start_thread() rather than
just start?
> +static bool start(struct pi_thread *t)
> +{
> + uint64_t end = now_ns() + 2 * NSEC_PER_SEC;
> + pthread_attr_t attr;
> + char state = 0;
> + int err;
> +
> + pthread_attr_init(&attr);
> + pthread_attr_setstacksize(&attr, 64 * 1024);
it makes me wonder what you did use as a template for this or who wrote
this. I can't imagine why one would set a stack size 64KiB here. It
shouldn't matter.
> + err = pthread_create(&t->thread, &attr, pi_thread_fn, t);
> + pthread_attr_destroy(&attr);
> + if (err) {
> + t->thread = pthread_self();
> + t->err = err;
> + atomic_store(&t->done, 1);
> + ksft_print_msg("pthread_create: %s\n", strerror(err));
> + return false;
> + }
…
Sebastian