[PATCH v15 15/16] barrier: timeout validity checks for smp_cond_load_relaxed_timeout()
From: Ankur Arora
Date: Mon Aug 31 2026 - 16:43:25 EST
Add timeout tests for smp_cond_load_relaxed_timeout(). These check
that the implementation returns early on invalid timeout values and
handles edge cases sanely.
Signed-off-by: Ankur Arora <ankur.a.arora@xxxxxxxxxx>
---
lib/tests/barrier-timeout-test.c | 86 ++++++++++++++++++++++++++++++++
1 file changed, 86 insertions(+)
diff --git a/lib/tests/barrier-timeout-test.c b/lib/tests/barrier-timeout-test.c
index 60f121fe5472..9b16ad6f5514 100644
--- a/lib/tests/barrier-timeout-test.c
+++ b/lib/tests/barrier-timeout-test.c
@@ -19,6 +19,8 @@ MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
struct clock_state {
s64 start_time;
s64 end_time;
+ s64 extra;
+ u32 niters;
};
#define TIMEOUT_MSEC 2
@@ -110,8 +112,92 @@ static void test_smp_cond_timeout(struct kunit *test)
KUNIT_EXPECT_GE(test, runtime, timeout_ns);
}
+static s64 synthetic_clock(struct clock_state *clk)
+{
+ clk->end_time += clk->extra;
+ clk->niters++;
+
+ return clk->end_time;
+}
+
+struct smp_cond_expiry_params {
+ char *desc;
+ u64 timeout_ns;
+ s64 clk_unit;
+ s64 miniters;
+ s64 maxiters;
+};
+
+static const struct smp_cond_expiry_params expiry_params_list[] = {
+ /* timeout_ns is invalid/out-of-range */
+ { .clk_unit = 0, .timeout_ns = -1LL, .miniters = -1, .maxiters = 0, .desc = "invalid (-1LL)", },
+ { .clk_unit = 0, .timeout_ns = ~0ULL, .miniters = -1, .maxiters = 0, .desc = "invalid (~0ULL)", },
+ { .clk_unit = 0, .timeout_ns = S64_MAX+1ULL, .miniters = -1, .maxiters = 0, .desc = "out-of-range (S64_MAX+1)", },
+ { .clk_unit = 0, .timeout_ns = U64_MAX, .miniters = -1, .maxiters = 0, .desc = "out-of-range (U64_MAX)", },
+ { .clk_unit = 0, .timeout_ns = 0, .miniters = -1, .maxiters = 1, .desc = "degenerate (0)", },
+
+ /* timeout_ns is valid */
+ { .clk_unit = (0x1ULL << 28), .timeout_ns = 1, .miniters = 1, .maxiters = -1, .desc = "1", },
+ { .clk_unit = (0x1ULL << 28), .timeout_ns = (0x1ULL << 30), .miniters = 1 << (30-28), .maxiters = -1, .desc = "1<<30", },
+ { .clk_unit = (0x1ULL << 28), .timeout_ns = S32_MAX, .miniters = 1 << (31-28), .maxiters = -1, .desc = "S32_MAX", },
+ { .clk_unit = (0x1ULL << 28), .timeout_ns = U32_MAX, .miniters = 1 << (32-28), .maxiters = -1, .desc = "U32_MAX", },
+ { .clk_unit = (0x1ULL << 28), .timeout_ns = (0x1ULL << 33), .miniters = 1 << (33-28), .maxiters = -1, .desc = "1<<33", },
+ { .clk_unit = (0x1ULL << 58), .timeout_ns = S64_MAX, .miniters = 1 << (63-58), .maxiters = -1, .desc = "S64_MAX", },
+};
+
+static void expiry_param_to_desc(const struct smp_cond_expiry_params *p, char *desc)
+{
+ char iters[32] = "";
+
+ if (p->miniters != -1)
+ snprintf(iters, 32, ">= %llx", p->miniters);
+ else if (p->maxiters != -1)
+ snprintf(iters, 32, "%s %llx", p->maxiters == 0 ? "==" : "<=", p->maxiters);
+
+ snprintf(desc, KUNIT_PARAM_DESC_SIZE,
+ "smp_cond_*_timeout: clock=%s, timeout=%s, iterations %s",
+ "synthetic", p->desc, iters);
+}
+
+static void test_smp_cond_relaxed(struct kunit *test)
+{
+ const struct smp_cond_expiry_params *p = test->param_value;
+ struct clock_state clk = {
+ .start_time = 0,
+ .end_time = 0,
+ .extra = p->clk_unit,
+ .niters = 0,
+ };
+ s64 runtime;
+
+ flag = 0;
+ smp_cond_load_relaxed_timeout(&flag,
+ 0,
+ synthetic_clock(&clk),
+ p->timeout_ns);
+
+ runtime = (u64)clk.end_time - (u64)clk.start_time;
+
+ /*
+ * Check if we do the expected number of iterations.
+ */
+ if (p->miniters != -1)
+ KUNIT_EXPECT_GE(test, clk.niters, p->miniters);
+ if (p->maxiters != -1)
+ KUNIT_EXPECT_LE(test, clk.niters, p->maxiters);
+
+ /*
+ * maxiters == 0 means that the timeout is invalid/out-of-range.
+ * When not, we cannot return with runtime < timeout_ns.
+ */
+ if (p->maxiters != 0)
+ KUNIT_EXPECT_GE(test, runtime, p->timeout_ns);
+}
+
+KUNIT_ARRAY_PARAM(smp_cond_expiry_params, expiry_params_list, expiry_param_to_desc);
static struct kunit_case barrier_timeout_test_cases[] = {
KUNIT_CASE_PARAM(test_smp_cond_timeout, smp_cond_update_params_gen_params),
+ KUNIT_CASE_PARAM(test_smp_cond_relaxed, smp_cond_expiry_params_gen_params),
{}
};
--
2.43.7