Add success and failure case tests for smp_cond_load_*_timeout(). Success or failure cases depend on the expected bit being set (or not). Additionally in failure cases smp_cond_load_*_timeout() cannot return before timeout. Cc: Julian Braha Tested-by: Haris Okanovic Tested-by: Sahil Chandna Signed-off-by: Ankur Arora --- Notes: - rename barrier-timeout-test.c to barrier_timeout_kunit.c lib/Kconfig.debug | 10 +++ lib/tests/Makefile | 1 + lib/tests/barrier_timeout_kunit.c | 129 ++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 lib/tests/barrier_timeout_kunit.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 2d22638ec13a..dd9bcb0b539b 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2556,6 +2556,16 @@ config FFS_KUNIT_TEST For more information on KUnit and unit tests in general, please refer to Documentation/dev-tools/kunit/. +config BARRIER_TIMEOUT_KUNIT_TEST + tristate "KUnit tests for smp_cond_load_*_timeout()" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Builds KUnit tests that validate wake-up and timeout handling paths in + smp_cond_load_relaxed_timeout() and smp_cond_load_acquire_timeout(). + + If unsure, say N. + config TEST_KSTRTOX tristate "Test kstrto*() family of functions at runtime" diff --git a/lib/tests/Makefile b/lib/tests/Makefile index df0537e1b91f..a9c644cb6d78 100644 --- a/lib/tests/Makefile +++ b/lib/tests/Makefile @@ -60,3 +60,4 @@ obj-$(CONFIG_RATELIMIT_KUNIT_TEST) += test_ratelimit.o obj-$(CONFIG_UUID_KUNIT_TEST) += uuid_kunit.o obj-$(CONFIG_TEST_RUNTIME_MODULE) += module/ +obj-$(CONFIG_BARRIER_TIMEOUT_KUNIT_TEST) += barrier_timeout_kunit.o diff --git a/lib/tests/barrier_timeout_kunit.c b/lib/tests/barrier_timeout_kunit.c new file mode 100644 index 000000000000..63d82f05ca59 --- /dev/null +++ b/lib/tests/barrier_timeout_kunit.c @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests exercising smp_cond_load_{relaxed,acquire}_timeout(). + * + * Copyright (c) 2026, Oracle Corp. + * Author: Ankur Arora + */ + +#include +#include +#include +#include +#include +#include +#include + +MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); + +struct clock_state { + s64 start_time; + s64 end_time; +}; + +#define TIMEOUT_MSEC 2 +#define TEST_FLAG_VAL BIT(17) +static unsigned int scu_flag; + +static s64 basic_clock(struct clock_state *clk) +{ + clk->end_time = local_clock(); + return clk->end_time; +} + +static void update_flags(void) +{ + WRITE_ONCE(scu_flag, TEST_FLAG_VAL); +} + +static s64 mocked_clock(struct clock_state *clk) +{ + s64 clk_mid = clk->start_time + (TIMEOUT_MSEC * NSEC_PER_MSEC)/2; + + clk->end_time = local_clock(); + if (clk->end_time >= clk_mid) + update_flags(); + return clk->end_time; +} + +typedef s64 (*clkfn_t)(struct clock_state *); +struct smp_cond_update_params { + clkfn_t clock; + bool acquire; + bool succeeds; +}; + +static const struct smp_cond_update_params update_params_list[] = { + /* mocked-clock updates flag inline. */ + { .clock = &mocked_clock, .succeeds = true, .acquire = false, }, + { .clock = &mocked_clock, .succeeds = true, .acquire = true, }, + + /* basic-clock doesn't update flag. */ + { .clock = &basic_clock, .succeeds = false, .acquire = true, }, + { .clock = &basic_clock, .succeeds = false, .acquire = false, }, +}; + +static void param_to_desc(const struct smp_cond_update_params *p, char *desc) +{ + char *clk = NULL, *update = NULL; + + if (p->clock == &mocked_clock) { + clk = "mocked"; + update = "inline"; + } else if (p->clock == &basic_clock) { + clk = "basic"; + update = "none"; + } + + snprintf(desc, KUNIT_PARAM_DESC_SIZE, "smp_cond_%s_timeout: clock=%s, update=%s", + p->acquire ? "acquire" : "relaxed", clk, update); +} + +KUNIT_ARRAY_PARAM(smp_cond_update_params, update_params_list, param_to_desc); + +static void test_smp_cond_timeout(struct kunit *test) +{ + const struct smp_cond_update_params *p = test->param_value; + s64 runtime, timeout_ns = TIMEOUT_MSEC * NSEC_PER_MSEC; + struct clock_state clk; + unsigned int result; + + scu_flag = 0; + + preempt_disable(); + clk.start_time = clk.end_time = local_clock(); + + if (p->acquire) { + result = smp_cond_load_acquire_timeout(&scu_flag, + (VAL & TEST_FLAG_VAL), + p->clock(&clk), + timeout_ns); + } else { + result = smp_cond_load_relaxed_timeout(&scu_flag, + (VAL & TEST_FLAG_VAL), + p->clock(&clk), + timeout_ns); + } + preempt_enable(); + + runtime = clk.end_time - clk.start_time; + KUNIT_EXPECT_EQ(test, result, scu_flag); + KUNIT_EXPECT_EQ(test, (bool)(result & TEST_FLAG_VAL), p->succeeds); + if (!p->succeeds) + KUNIT_EXPECT_GE(test, runtime, timeout_ns); +} + +static struct kunit_case barrier_timeout_test_cases[] = { + KUNIT_CASE_PARAM(test_smp_cond_timeout, smp_cond_update_params_gen_params), + {} +}; + +static struct kunit_suite barrier_timeout_test_suite = { + .name = "smp_cond_load_timeout", + .test_cases = barrier_timeout_test_cases, +}; + +kunit_test_suite(barrier_timeout_test_suite); + +MODULE_DESCRIPTION("KUnit tests for smp_cond_load_{relaxed,acquire}_timeout()"); +MODULE_LICENSE("GPL"); -- 2.43.7