[PATCH 4/8] perf bench futex: Reject invalid -q, -f and -b values
From: Michal Pluta
Date: Sat Sep 26 2026 - 15:05:13 EST
Some values make a futex benchmark hang or are silently ignored.
'futex requeue -q 0' never finishes, because no thread is requeued.
'futex hash -f 0' never finishes either, because with no futexes the
workers never notice that the run is over. A negative -b is treated as
if it had not been given, and the kernel simply auto-sizes the hash.
Print an error for these values, except for -b -1, which still means
automatic sizing.
Fixes: 0fb298cf95c0 ("perf bench: Add futex-requeue microbenchmark")
Fixes: a043971141f1 ("perf bench: Add futex-hash microbenchmark")
Fixes: 60035a3981a7 ("tools/perf: Allow to select the number of hash buckets")
Assisted-by: LLM
Signed-off-by: Michal Pluta <michalpl2003@xxxxxxxxx>
---
tools/perf/bench/futex-hash.c | 3 +++
tools/perf/bench/futex-requeue.c | 4 ++++
tools/perf/bench/futex.c | 3 +++
3 files changed, 10 insertions(+)
diff --git a/tools/perf/bench/futex-hash.c b/tools/perf/bench/futex-hash.c
index 32b88df8ee6d..710c253cfe17 100644
--- a/tools/perf/bench/futex-hash.c
+++ b/tools/perf/bench/futex-hash.c
@@ -161,6 +161,9 @@ int bench_futex_hash(int argc, const char **argv)
if (!params.nthreads) /* default to the number of CPUs */
params.nthreads = perf_cpu_map__nr(cpu);
+ if (!params.nfutexes)
+ errx(EXIT_FAILURE, "-f/--futexes must be at least 1");
+
worker = calloc(params.nthreads, sizeof(*worker));
if (!worker)
goto errmem;
diff --git a/tools/perf/bench/futex-requeue.c b/tools/perf/bench/futex-requeue.c
index 5d4708f40c60..bc6efc2ecadb 100644
--- a/tools/perf/bench/futex-requeue.c
+++ b/tools/perf/bench/futex-requeue.c
@@ -207,6 +207,10 @@ int bench_futex_requeue(int argc, const char **argv)
if (params.broadcast)
params.nrequeue = params.nthreads;
+ /* A regular requeue that moves no threads would never finish. */
+ if (!params.pi && !params.nrequeue)
+ errx(EXIT_FAILURE, "-q/--nrequeue must be at least 1");
+
futex_set_nbuckets_param(¶ms);
printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %s%p), "
diff --git a/tools/perf/bench/futex.c b/tools/perf/bench/futex.c
index 1968c9d00b5b..5757d47f8f7c 100644
--- a/tools/perf/bench/futex.c
+++ b/tools/perf/bench/futex.c
@@ -17,6 +17,9 @@ void futex_set_nbuckets_param(struct bench_futex_parameters *params)
{
int ret;
+ if (params->nbuckets < -1)
+ errx(EXIT_FAILURE, "-b/--buckets must be -1 (auto), 0 or more");
+
if (params->nbuckets < 0)
return;
--
2.43.0