Linux-Next discussions
 help / color / mirror / Atom feed
* [PATCH v11 1/2] kunit: add tests for smp_cond_load_*_timeout()
       [not found] <20260408122538.3610871-15-ankur.a.arora@oracle.com>
@ 2026-05-21  8:30 ` Ankur Arora
  2026-05-21  8:30   ` [PATCH v11 2/2] kunit: add clock tests for smp_cond_load_relaxed_timeout() Ankur Arora
  2026-05-21 21:46   ` [PATCH v11 1/2] kunit: add tests for smp_cond_load_*_timeout() Andrew Morton
  0 siblings, 2 replies; 5+ messages in thread
From: Ankur Arora @ 2026-05-21  8:30 UTC (permalink / raw)
  To: linux-kernel, linux-arch, linux-arm-kernel, linux-pm, linux-next,
	bpf
  Cc: arnd, catalin.marinas, will, peterz, akpm, mark.rutland, harisokn,
	cl, ast, rafael, daniel.lezcano, memxor, zhenglifeng1, xueshuai,
	rdunlap, david.laight.linux, joao.m.martins, boris.ostrovsky,
	konrad.wilk, ashok.bhat, Ankur Arora, Boqun Feng, Boqun Feng,
	Christoph Lameter, Ingo Molnar, Konrad Dybcio, Mark Brown

Add success and failure case tests for smp_cond_load_*_timeout().

All of the test cases wait on some state in smp_cond_load_*_timeout().
In the success case we spawn a kthread that pokes the bit.

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.

Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Boqun Feng <boqun@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: Haris Okanovic <harisokn@amazon.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Konrad Dybcio <konradybcio@kernel.org>
Cc: Mark Brown <broonie@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Will Deacon <will@kernel.org>

---
Note: This fixes an error in the test case reported by Mark Brown
in https://lore.kernel.org/lkml/agr_RxvNtfASfevg@sirena.org.uk/.

There are three changes over the old version:

  - One of the test conditions used in the test was much too strict.

    The test was treating success as implying that the
    smp_cond_load_relaxed_timeout() call has to return before timeout.
    (runtime <= timeout_ns).

    However, all that we know is that on failure (!success), the 
    smp_cond_load_relaxed_timeout() *cannot* return before timeout.
    (runtime >= timeout_ns).

  - The test can run in a wide variety of environments including in
    emulation. So to reduce the chance of failure due to timing,
    remove the kthreaded case.

  - Parametrize the test cases.

---
 lib/Kconfig.debug                |  10 +++
 lib/tests/Makefile               |   1 +
 lib/tests/barrier-timeout-test.c | 128 +++++++++++++++++++++++++++++++
 3 files changed, 139 insertions(+)
 create mode 100644 lib/tests/barrier-timeout-test.c

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index ce25724be414..9d54c48d7d98 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2413,6 +2413,16 @@ config FPROBE_SANITY_TEST
 
 	  Say N if you are unsure.
 
+config BARRIER_TIMEOUT_TEST
+	tristate "KUnit tests for smp_cond_load_relaxed_timeout()"
+	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().
+
+	  Say N if you are unsure.
+
 config BACKTRACE_SELF_TEST
 	tristate "Self test for the backtrace code"
 	depends on DEBUG_KERNEL
diff --git a/lib/tests/Makefile b/lib/tests/Makefile
index 7e9c2fa52e35..19c1d6b17856 100644
--- a/lib/tests/Makefile
+++ b/lib/tests/Makefile
@@ -20,6 +20,7 @@ CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN)
 obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
 CFLAGS_test_fprobe.o += $(CC_FLAGS_FTRACE)
 obj-$(CONFIG_FPROBE_SANITY_TEST) += test_fprobe.o
+obj-$(CONFIG_BARRIER_TIMEOUT_TEST) += barrier-timeout-test.o
 obj-$(CONFIG_GLOB_KUNIT_TEST) += glob_kunit.o
 obj-$(CONFIG_HASHTABLE_KUNIT_TEST) += hashtable_test.o
 obj-$(CONFIG_HASH_KUNIT_TEST) += test_hash.o
diff --git a/lib/tests/barrier-timeout-test.c b/lib/tests/barrier-timeout-test.c
new file mode 100644
index 000000000000..2160844b27b8
--- /dev/null
+++ b/lib/tests/barrier-timeout-test.c
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit tests exercising smp_cond_load_relaxed_timeout().
+ *
+ * Copyright (c) 2026, Oracle Corp.
+ * Author: Ankur Arora <ankur.a.arora@oracle.com>
+ */
+
+#include <linux/bitops.h>
+#include <linux/types.h>
+#include <linux/sched/clock.h>
+#include <linux/delay.h>
+#include <asm/barrier.h>
+#include <kunit/test.h>
+#include <kunit/visibility.h>
+
+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(2)
+static unsigned int 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(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, *update;
+
+	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;
+	struct clock_state clk = {
+		.start_time = local_clock(),
+		.end_time = local_clock(),
+	};
+	s64 runtime, timeout_ns = TIMEOUT_MSEC * NSEC_PER_MSEC;
+	unsigned int result;
+
+	flag = 0;
+	if (p->acquire) {
+		result = smp_cond_load_acquire_timeout(&flag,
+						       (VAL & TEST_FLAG_VAL),
+						       p->clock(&clk),
+						       timeout_ns);
+	} else {
+		result = smp_cond_load_relaxed_timeout(&flag,
+						       (VAL & TEST_FLAG_VAL),
+						       p->clock(&clk),
+						       timeout_ns);
+	}
+
+	runtime = clk.end_time - clk.start_time;
+	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_timeout()");
+MODULE_LICENSE("GPL");
-- 
2.43.7


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v11 2/2] kunit: add clock tests for smp_cond_load_relaxed_timeout()
  2026-05-21  8:30 ` [PATCH v11 1/2] kunit: add tests for smp_cond_load_*_timeout() Ankur Arora
@ 2026-05-21  8:30   ` Ankur Arora
  2026-05-21 21:46   ` [PATCH v11 1/2] kunit: add tests for smp_cond_load_*_timeout() Andrew Morton
  1 sibling, 0 replies; 5+ messages in thread
From: Ankur Arora @ 2026-05-21  8:30 UTC (permalink / raw)
  To: linux-kernel, linux-arch, linux-arm-kernel, linux-pm, linux-next,
	bpf
  Cc: arnd, catalin.marinas, will, peterz, akpm, mark.rutland, harisokn,
	cl, ast, rafael, daniel.lezcano, memxor, zhenglifeng1, xueshuai,
	rdunlap, david.laight.linux, joao.m.martins, boris.ostrovsky,
	konrad.wilk, ashok.bhat, Ankur Arora, Boqun Feng, Boqun Feng,
	Christoph Lameter, Ingo Molnar, Konrad Dybcio, Mark Brown

Add a few clock tests for smp_cond_load_relaxed_timeout(). These
ensure that the implementation doesn't do any funny stuff with the
clock (like multiple accesses per iteration.)

Also ensure that edge cases are handled sanely. Note that two edge
cases fail: S64_MAX and U64_MAX. However, both of those are quite
far out and if needed, can be addressed in the implementation of
the interface.

Also, this tests only smp_cond_load_relaxed_timeout(). The acquire
variant uses an identical clock path and testing wouldn't add anything.

Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Boqun Feng <boqun@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: Haris Okanovic <harisokn@amazon.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Konrad Dybcio <konradybcio@kernel.org>
Cc: Mark Brown <broonie@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Will Deacon <will@kernel.org>
---
 lib/tests/barrier-timeout-test.c | 57 ++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/lib/tests/barrier-timeout-test.c b/lib/tests/barrier-timeout-test.c
index 2160844b27b8..6ce6c7b0fc44 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
@@ -112,8 +114,63 @@ 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;
+	s64	timeout;
+	s64	clk_inc;
+	u32	niters;
+};
+
+static const struct smp_cond_expiry_params expiry_params_list[] = {
+	{ .clk_inc = (0x1ULL << 28), .timeout = -1LL,		.niters = 1,		  	.desc = "-1LL",    },
+	{ .clk_inc = (0x1ULL << 28), .timeout = (0x1ULL << 30), .niters = 1 + (1 << (30-28)),	.desc = "1<<30",   },
+	{ .clk_inc = (0x1ULL << 28), .timeout = S32_MAX,	.niters = 1 + (1 << (31-28)),	.desc = "S32_MAX", },
+	{ .clk_inc = (0x1ULL << 28), .timeout = U32_MAX,	.niters = 1 + (1 << (32-28)),	.desc = "U32_MAX", },
+	{ .clk_inc = (0x1ULL << 28), .timeout = (0x1ULL << 33), .niters = 1 + (1 << (33-28)),	.desc = "1<<33",   },
+};
+
+static void expiry_param_to_desc(const struct smp_cond_expiry_params *p, char *desc)
+{
+	snprintf(desc, KUNIT_PARAM_DESC_SIZE, "smp_cond_%s_timeout: clock-%s, timeout=%s, iterations=%u",
+		"relaxed", "synthetic", p->desc, p->niters);
+}
+
+static void test_smp_cond_expiry(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_inc,
+		.niters = 0,
+	};
+	unsigned int result;
+	s64 runtime;
+
+	flag = 0;
+	result = smp_cond_load_relaxed_timeout(&flag,
+					       0,
+					       synthetic_clock(&clk),
+					       p->timeout);
+
+	runtime = (u64)clk.end_time - (u64)clk.start_time;
+	KUNIT_EXPECT_EQ(test, clk.niters, p->niters);
+	KUNIT_EXPECT_GE(test, runtime, p->timeout);
+}
+
+
+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_expiry, smp_cond_expiry_params_gen_params),
 	{}
 };
 
-- 
2.43.7


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v11 1/2] kunit: add tests for smp_cond_load_*_timeout()
  2026-05-21  8:30 ` [PATCH v11 1/2] kunit: add tests for smp_cond_load_*_timeout() Ankur Arora
  2026-05-21  8:30   ` [PATCH v11 2/2] kunit: add clock tests for smp_cond_load_relaxed_timeout() Ankur Arora
@ 2026-05-21 21:46   ` Andrew Morton
  2026-05-21 21:57     ` Mark Brown
  2026-05-22  5:10     ` Ankur Arora
  1 sibling, 2 replies; 5+ messages in thread
From: Andrew Morton @ 2026-05-21 21:46 UTC (permalink / raw)
  To: Ankur Arora
  Cc: linux-kernel, linux-arch, linux-arm-kernel, linux-pm, linux-next,
	bpf, arnd, catalin.marinas, will, peterz, mark.rutland, harisokn,
	cl, ast, rafael, daniel.lezcano, memxor, zhenglifeng1, xueshuai,
	rdunlap, david.laight.linux, joao.m.martins, boris.ostrovsky,
	konrad.wilk, ashok.bhat, Boqun Feng, Boqun Feng,
	Christoph Lameter, Ingo Molnar, Konrad Dybcio, Mark Brown

On Thu, 21 May 2026 01:30:37 -0700 Ankur Arora <ankur.a.arora@oracle.com> wrote:

> Add success and failure case tests for smp_cond_load_*_timeout().
> 
> All of the test cases wait on some state in smp_cond_load_*_timeout().
> In the success case we spawn a kthread that pokes the bit.
> 
> 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.

Your title "kunit: ..." implies that this is a kunit patchset.  ie, and
to my mind, this implies that the patchset makes changes to kunit
infrastructure.  This isn't the case, of course.

>
> ...
>  
> +config BARRIER_TIMEOUT_TEST
> +	tristate "KUnit tests for smp_cond_load_relaxed_timeout()"

The patchset which attempted to add smp_cond_load_relaxed_timeout() was
titled "barrier: ...", which is a more appropriate identifier for this
patchset.

Anyway, I removed the series "barrier: Add
smp_cond_load_{relaxed,acquire}_timeout()" from mm.git on May 19 for
forgotten reasons.  So I suggest that the series be respun and resent,
with these two test patches included.

Again, I'm really not the guy to be handling these patches.  But a 4-6%
performance improvement in real world workloads is not to be ignored.

Not by me, anyway.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v11 1/2] kunit: add tests for smp_cond_load_*_timeout()
  2026-05-21 21:46   ` [PATCH v11 1/2] kunit: add tests for smp_cond_load_*_timeout() Andrew Morton
@ 2026-05-21 21:57     ` Mark Brown
  2026-05-22  5:10     ` Ankur Arora
  1 sibling, 0 replies; 5+ messages in thread
From: Mark Brown @ 2026-05-21 21:57 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Ankur Arora, linux-kernel, linux-arch, linux-arm-kernel, linux-pm,
	linux-next, bpf, arnd, catalin.marinas, will, peterz,
	mark.rutland, harisokn, cl, ast, rafael, daniel.lezcano, memxor,
	zhenglifeng1, xueshuai, rdunlap, david.laight.linux,
	joao.m.martins, boris.ostrovsky, konrad.wilk, ashok.bhat,
	Boqun Feng, Boqun Feng, Christoph Lameter, Ingo Molnar,
	Konrad Dybcio

[-- Attachment #1: Type: text/plain, Size: 367 bytes --]

On Thu, May 21, 2026 at 02:46:29PM -0700, Andrew Morton wrote:

> Anyway, I removed the series "barrier: Add
> smp_cond_load_{relaxed,acquire}_timeout()" from mm.git on May 19 for
> forgotten reasons.  So I suggest that the series be respun and resent,
> with these two test patches included.

The tests were failing on arm64 when run on hardware (rather than qemu).

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v11 1/2] kunit: add tests for smp_cond_load_*_timeout()
  2026-05-21 21:46   ` [PATCH v11 1/2] kunit: add tests for smp_cond_load_*_timeout() Andrew Morton
  2026-05-21 21:57     ` Mark Brown
@ 2026-05-22  5:10     ` Ankur Arora
  1 sibling, 0 replies; 5+ messages in thread
From: Ankur Arora @ 2026-05-22  5:10 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Ankur Arora, linux-kernel, linux-arch, linux-arm-kernel, linux-pm,
	linux-next, bpf, arnd, catalin.marinas, will, peterz,
	mark.rutland, harisokn, cl, ast, rafael, daniel.lezcano, memxor,
	zhenglifeng1, xueshuai, rdunlap, david.laight.linux,
	joao.m.martins, boris.ostrovsky, konrad.wilk, ashok.bhat,
	Boqun Feng, Boqun Feng, Christoph Lameter, Ingo Molnar,
	Konrad Dybcio, Mark Brown


Andrew Morton <akpm@linux-foundation.org> writes:

> On Thu, 21 May 2026 01:30:37 -0700 Ankur Arora <ankur.a.arora@oracle.com> wrote:
>
>> Add success and failure case tests for smp_cond_load_*_timeout().
>>
>> All of the test cases wait on some state in smp_cond_load_*_timeout().
>> In the success case we spawn a kthread that pokes the bit.
>>
>> 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.
>
> Your title "kunit: ..." implies that this is a kunit patchset.  ie, and
> to my mind, this implies that the patchset makes changes to kunit
> infrastructure.  This isn't the case, of course.

Aah yes. I did miss that.

>> ...
>>
>> +config BARRIER_TIMEOUT_TEST
>> +	tristate "KUnit tests for smp_cond_load_relaxed_timeout()"
>
> The patchset which attempted to add smp_cond_load_relaxed_timeout() was
> titled "barrier: ...", which is a more appropriate identifier for this
> patchset.
>
> Anyway, I removed the series "barrier: Add
> smp_cond_load_{relaxed,acquire}_timeout()" from mm.git on May 19 for
> forgotten reasons.  So I suggest that the series be respun and resent,
> with these two test patches included.

Will do.

> Again, I'm really not the guy to be handling these patches.  But a 4-6%
> performance improvement in real world workloads is not to be ignored.
>
> Not by me, anyway.

:).

Thanks
--
ankur

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-05-22  5:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20260408122538.3610871-15-ankur.a.arora@oracle.com>
2026-05-21  8:30 ` [PATCH v11 1/2] kunit: add tests for smp_cond_load_*_timeout() Ankur Arora
2026-05-21  8:30   ` [PATCH v11 2/2] kunit: add clock tests for smp_cond_load_relaxed_timeout() Ankur Arora
2026-05-21 21:46   ` [PATCH v11 1/2] kunit: add tests for smp_cond_load_*_timeout() Andrew Morton
2026-05-21 21:57     ` Mark Brown
2026-05-22  5:10     ` Ankur Arora

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox