linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] lib/atomic64_test.c: convert to use KUnit
@ 2022-05-02 19:23 Daniel Latypov
  2022-05-03  6:13 ` David Gow
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Daniel Latypov @ 2022-05-02 19:23 UTC (permalink / raw)
  To: brendanhiggins, davidgow
  Cc: linux-kernel, kunit-dev, linux-kselftest, skhan, mpe, Daniel Latypov

The test currently is a bunch of checks (implemented using BUG_ON())
that can be built into the kernel or as a module.

Convert it to a KUnit test, which can also run in both modes.
From a user's perspective, this change adds a CONFIG_KUNIT=y dep and
changes the output format of the test [1]. The test itself is the same.

This hopefully makes the test easier to run and more consistent with
similar tests in lib/.
Since it has no dependencies, it can be run without explicitly setting
up a .kunitconfig via
$ ./tools/testing/kunit/kunit.py run atomic
...
[13:53:44] Starting KUnit Kernel (1/1)...
[13:53:44] ============================================================
[13:53:47] =================== atomic (2 subtests) ====================
[13:53:47] [PASSED] test_atomic
[13:53:47] [PASSED] test_atomic64
[13:53:47] ===================== [PASSED] atomic ======================
[13:53:47] ============================================================
[13:53:47] Testing complete. Passed: 2, Failed: 0, Crashed: 0, Skipped: 0, Errors: 0
[13:53:47] Elapsed time: 13.902s total, 1.629s configuring, 9.331s building, 2.852s running

It can be run on ARCH=x86_64 (and others) via:
$ ./tools/testing/kunit/kunit.py run --arch=x86_64 atomic

The message about which platform the test ran on won't show up in
kunit.py, but still gets printed out in dmesg, e.g.
> TAP version 14
> 1..1
>     # Subtest: atomic
>     1..2
>     ok 1 - test_atomic
>     ok 2 - test_atomic64
>     # atomic: ran on x86-64 platform with CX8 and with SSE
> # atomic: pass:2 fail:0 skip:0 total:2
> # Totals: pass:2 fail:0 skip:0 total:2
> ok 1 - atomic

Signed-off-by: Daniel Latypov <dlatypov@google.com>

[1] https://www.kernel.org/doc/html/latest/dev-tools/ktap.html
---
Meta:
1. this patch applies on top of the kunit branch,
https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git/?h=kunit

2. checkpatch complains about aligning with parens, but it wants me to
indent the `#ifdef CONFIG_X86_64` which seems inappropriate in context.

3. this file doesn't seem to have a clear maintainer, so I assume this
conversion is fine to go through the kunit branch. The only observable
differences are the new CONFIG_KUNIT=y dep and more standardized (KTAP)
output format.
---
 lib/Kconfig.debug   |   4 +-
 lib/atomic64_test.c | 107 +++++++++++++++++++++-----------------------
 2 files changed, 55 insertions(+), 56 deletions(-)

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 075cd25363ac..4cf8d5feda0a 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2223,7 +2223,9 @@ config PERCPU_TEST
 	  If unsure, say N.
 
 config ATOMIC64_SELFTEST
-	tristate "Perform an atomic64_t self-test"
+	tristate "Perform an atomic64_t self-test" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
 	help
 	  Enable this option to test the atomic64_t functions at boot or
 	  at module load time.
diff --git a/lib/atomic64_test.c b/lib/atomic64_test.c
index d9d170238165..46cb0130f8d0 100644
--- a/lib/atomic64_test.c
+++ b/lib/atomic64_test.c
@@ -5,13 +5,9 @@
  * Copyright © 2010  Luca Barbieri
  */
 
-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#include <kunit/test.h>
 
-#include <linux/init.h>
-#include <linux/bug.h>
-#include <linux/kernel.h>
 #include <linux/atomic.h>
-#include <linux/module.h>
 
 #ifdef CONFIG_X86
 #include <asm/cpufeature.h>	/* for boot_cpu_has below */
@@ -23,9 +19,7 @@ do {								\
 	r = v0;							\
 	atomic##bit##_##op(val, &v);				\
 	r c_op val;						\
-	WARN(atomic##bit##_read(&v) != r, "%Lx != %Lx\n",	\
-		(unsigned long long)atomic##bit##_read(&v),	\
-		(unsigned long long)r);				\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_read(&v), r);	\
 } while (0)
 
 /*
@@ -46,8 +40,8 @@ do {								\
 	atomic##bit##_set(&v, v0);				\
 	r = v0;							\
 	r c_op val;						\
-	BUG_ON(atomic##bit##_##op(val, &v) != r);		\
-	BUG_ON(atomic##bit##_read(&v) != r);			\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_##op(val, &v), r);	\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_read(&v), r);	\
 } while (0)
 
 #define TEST_FETCH(bit, op, c_op, val)				\
@@ -55,8 +49,8 @@ do {								\
 	atomic##bit##_set(&v, v0);				\
 	r = v0;							\
 	r c_op val;						\
-	BUG_ON(atomic##bit##_##op(val, &v) != v0);		\
-	BUG_ON(atomic##bit##_read(&v) != r);			\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_##op(val, &v), v0);	\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_read(&v), r);	\
 } while (0)
 
 #define RETURN_FAMILY_TEST(bit, op, c_op, val)			\
@@ -72,8 +66,8 @@ do {								\
 #define TEST_ARGS(bit, op, init, ret, expect, args...)		\
 do {								\
 	atomic##bit##_set(&v, init);				\
-	BUG_ON(atomic##bit##_##op(&v, ##args) != ret);		\
-	BUG_ON(atomic##bit##_read(&v) != expect);		\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_##op(&v, ##args), ret);\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_read(&v), expect);	\
 } while (0)
 
 #define XCHG_FAMILY_TEST(bit, init, new)				\
@@ -101,7 +95,7 @@ do {							\
 			i, (i) - one, (i) - one);	\
 } while (0)
 
-static __init void test_atomic(void)
+static void test_atomic(struct kunit *test)
 {
 	int v0 = 0xaaa31337;
 	int v1 = 0xdeadbeef;
@@ -144,7 +138,7 @@ static __init void test_atomic(void)
 }
 
 #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
-static __init void test_atomic64(void)
+static void test_atomic64(struct kunit *test)
 {
 	long long v0 = 0xaaa31337c001d00dLL;
 	long long v1 = 0xdeadbeefdeafcafeLL;
@@ -156,12 +150,12 @@ static __init void test_atomic64(void)
 
 	atomic64_t v = ATOMIC64_INIT(v0);
 	long long r = v0;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	atomic64_set(&v, v1);
 	r = v1;
-	BUG_ON(v.counter != r);
-	BUG_ON(atomic64_read(&v) != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
+	KUNIT_ASSERT_EQ(test, atomic64_read(&v), r);
 
 	TEST(64, add, +=, onestwos);
 	TEST(64, add, +=, -one);
@@ -190,12 +184,12 @@ static __init void test_atomic64(void)
 	INIT(v0);
 	atomic64_inc(&v);
 	r += one;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(v0);
 	atomic64_dec(&v);
 	r -= one;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INC_RETURN_FAMILY_TEST(64, v0);
 	DEC_RETURN_FAMILY_TEST(64, v0);
@@ -204,73 +198,76 @@ static __init void test_atomic64(void)
 	CMPXCHG_FAMILY_TEST(64, v0, v1, v2);
 
 	INIT(v0);
-	BUG_ON(atomic64_add_unless(&v, one, v0));
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_FALSE(test, atomic64_add_unless(&v, one, v0));
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(v0);
-	BUG_ON(!atomic64_add_unless(&v, one, v1));
+	KUNIT_ASSERT_TRUE(test, atomic64_add_unless(&v, one, v1));
 	r += one;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(onestwos);
-	BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
+	KUNIT_ASSERT_EQ(test, atomic64_dec_if_positive(&v), (onestwos - 1));
 	r -= one;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(0);
-	BUG_ON(atomic64_dec_if_positive(&v) != -one);
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, atomic64_dec_if_positive(&v), -one);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(-one);
-	BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, atomic64_dec_if_positive(&v), (-one - one));
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(onestwos);
-	BUG_ON(!atomic64_inc_not_zero(&v));
+	KUNIT_ASSERT_TRUE(test, atomic64_inc_not_zero(&v));
 	r += one;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(0);
-	BUG_ON(atomic64_inc_not_zero(&v));
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_FALSE(test, atomic64_inc_not_zero(&v));
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(-one);
-	BUG_ON(!atomic64_inc_not_zero(&v));
+	KUNIT_ASSERT_TRUE(test, atomic64_inc_not_zero(&v));
 	r += one;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	/* Confirm the return value fits in an int, even if the value doesn't */
 	INIT(v3);
+
 	r_int = atomic64_inc_not_zero(&v);
-	BUG_ON(!r_int);
+	KUNIT_ASSERT_NE(test, r_int, 0);
 }
 
-static __init int test_atomics_init(void)
-{
-	test_atomic();
-	test_atomic64();
+static struct kunit_case atomic_test_cases[] = {
+	KUNIT_CASE(test_atomic),
+	KUNIT_CASE(test_atomic64),
+	{},
+};
 
+static void atomic_suite_exit(struct kunit_suite *suite)
+{
 #ifdef CONFIG_X86
-	pr_info("passed for %s platform %s CX8 and %s SSE\n",
+	kunit_info(suite, "ran on %s platform %s CX8 and %s SSE\n",
 #ifdef CONFIG_X86_64
-		"x86-64",
+		   "x86-64",
 #elif defined(CONFIG_X86_CMPXCHG64)
-		"i586+",
+		   "i586+",
 #else
-		"i386+",
+		   "i386+",
 #endif
-	       boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
-	       boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
-#else
-	pr_info("passed\n");
+		   boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
+		   boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
 #endif
-
-	return 0;
 }
 
-static __exit void test_atomics_exit(void) {}
+static struct kunit_suite atomic_test_suite = {
+	.name = "atomic",
+	.test_cases = atomic_test_cases,
+	.suite_exit = atomic_suite_exit,
+};
 
-module_init(test_atomics_init);
-module_exit(test_atomics_exit);
+kunit_test_suites(&atomic_test_suite);
 
 MODULE_LICENSE("GPL");

base-commit: 38289a26e1b8a37755f3e07056ca416c1ee2a2e8
-- 
2.36.0.464.gb9c8b46e94-goog


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

* Re: [PATCH] lib/atomic64_test.c: convert to use KUnit
  2022-05-02 19:23 [PATCH] lib/atomic64_test.c: convert to use KUnit Daniel Latypov
@ 2022-05-03  6:13 ` David Gow
  2022-05-12 18:52 ` Brendan Higgins
  2022-05-13  3:05 ` Michael Ellerman
  2 siblings, 0 replies; 8+ messages in thread
From: David Gow @ 2022-05-03  6:13 UTC (permalink / raw)
  To: Daniel Latypov
  Cc: brendanhiggins, linux-kernel, kunit-dev, linux-kselftest, skhan, mpe

On Tue, May 3, 2022 at 3:23 AM Daniel Latypov <dlatypov@google.com> wrote:
>
> The test currently is a bunch of checks (implemented using BUG_ON())
> that can be built into the kernel or as a module.
>
> Convert it to a KUnit test, which can also run in both modes.
> From a user's perspective, this change adds a CONFIG_KUNIT=y dep and
> changes the output format of the test [1]. The test itself is the same.
>
> This hopefully makes the test easier to run and more consistent with
> similar tests in lib/.
> Since it has no dependencies, it can be run without explicitly setting
> up a .kunitconfig via
> $ ./tools/testing/kunit/kunit.py run atomic
> ...
> [13:53:44] Starting KUnit Kernel (1/1)...
> [13:53:44] ============================================================
> [13:53:47] =================== atomic (2 subtests) ====================
> [13:53:47] [PASSED] test_atomic
> [13:53:47] [PASSED] test_atomic64
> [13:53:47] ===================== [PASSED] atomic ======================
> [13:53:47] ============================================================
> [13:53:47] Testing complete. Passed: 2, Failed: 0, Crashed: 0, Skipped: 0, Errors: 0
> [13:53:47] Elapsed time: 13.902s total, 1.629s configuring, 9.331s building, 2.852s running
>
> It can be run on ARCH=x86_64 (and others) via:
> $ ./tools/testing/kunit/kunit.py run --arch=x86_64 atomic
>
> The message about which platform the test ran on won't show up in
> kunit.py, but still gets printed out in dmesg, e.g.
> > TAP version 14
> > 1..1
> >     # Subtest: atomic
> >     1..2
> >     ok 1 - test_atomic
> >     ok 2 - test_atomic64
> >     # atomic: ran on x86-64 platform with CX8 and with SSE
> > # atomic: pass:2 fail:0 skip:0 total:2
> > # Totals: pass:2 fail:0 skip:0 total:2
> > ok 1 - atomic
>
> Signed-off-by: Daniel Latypov <dlatypov@google.com>
>
> [1] https://www.kernel.org/doc/html/latest/dev-tools/ktap.html
> ---

This looks good to me. It's nice to see these tests being standardised.

> Meta:
> 1. this patch applies on top of the kunit branch,
> https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git/?h=kunit

(To anyone else trying it: make sure you update this first, as the
prerequisites only just landed.)

> 2. checkpatch complains about aligning with parens, but it wants me to
> indent the `#ifdef CONFIG_X86_64` which seems inappropriate in context.

Huh... checkpatch isn't showing any such warning on my system.

> 3. this file doesn't seem to have a clear maintainer, so I assume this
> conversion is fine to go through the kunit branch. The only observable
> differences are the new CONFIG_KUNIT=y dep and more standardized (KTAP)
> output format.
> ---

I'm happy to vouch for this not breaking anything, even though I'm
definitely not an expert on the atomics code.

Reviewed-by: David Gow <davidgow@google.com>

-- David

>  lib/Kconfig.debug   |   4 +-
>  lib/atomic64_test.c | 107 +++++++++++++++++++++-----------------------
>  2 files changed, 55 insertions(+), 56 deletions(-)
>
> diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> index 075cd25363ac..4cf8d5feda0a 100644
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -2223,7 +2223,9 @@ config PERCPU_TEST
>           If unsure, say N.
>
>  config ATOMIC64_SELFTEST
> -       tristate "Perform an atomic64_t self-test"
> +       tristate "Perform an atomic64_t self-test" if !KUNIT_ALL_TESTS
> +       depends on KUNIT
> +       default KUNIT_ALL_TESTS
>         help
>           Enable this option to test the atomic64_t functions at boot or
>           at module load time.
> diff --git a/lib/atomic64_test.c b/lib/atomic64_test.c
> index d9d170238165..46cb0130f8d0 100644
> --- a/lib/atomic64_test.c
> +++ b/lib/atomic64_test.c
> @@ -5,13 +5,9 @@
>   * Copyright © 2010  Luca Barbieri
>   */
>
> -#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
> +#include <kunit/test.h>
>
> -#include <linux/init.h>
> -#include <linux/bug.h>
> -#include <linux/kernel.h>
>  #include <linux/atomic.h>
> -#include <linux/module.h>
>
>  #ifdef CONFIG_X86
>  #include <asm/cpufeature.h>    /* for boot_cpu_has below */
> @@ -23,9 +19,7 @@ do {                                                          \
>         r = v0;                                                 \
>         atomic##bit##_##op(val, &v);                            \
>         r c_op val;                                             \
> -       WARN(atomic##bit##_read(&v) != r, "%Lx != %Lx\n",       \
> -               (unsigned long long)atomic##bit##_read(&v),     \
> -               (unsigned long long)r);                         \
> +       KUNIT_ASSERT_EQ(test, atomic##bit##_read(&v), r);       \
>  } while (0)
>
>  /*
> @@ -46,8 +40,8 @@ do {                                                          \
>         atomic##bit##_set(&v, v0);                              \
>         r = v0;                                                 \
>         r c_op val;                                             \
> -       BUG_ON(atomic##bit##_##op(val, &v) != r);               \
> -       BUG_ON(atomic##bit##_read(&v) != r);                    \
> +       KUNIT_ASSERT_EQ(test, atomic##bit##_##op(val, &v), r);  \
> +       KUNIT_ASSERT_EQ(test, atomic##bit##_read(&v), r);       \
>  } while (0)
>
>  #define TEST_FETCH(bit, op, c_op, val)                         \
> @@ -55,8 +49,8 @@ do {                                                          \
>         atomic##bit##_set(&v, v0);                              \
>         r = v0;                                                 \
>         r c_op val;                                             \
> -       BUG_ON(atomic##bit##_##op(val, &v) != v0);              \
> -       BUG_ON(atomic##bit##_read(&v) != r);                    \
> +       KUNIT_ASSERT_EQ(test, atomic##bit##_##op(val, &v), v0); \
> +       KUNIT_ASSERT_EQ(test, atomic##bit##_read(&v), r);       \
>  } while (0)
>
>  #define RETURN_FAMILY_TEST(bit, op, c_op, val)                 \
> @@ -72,8 +66,8 @@ do {                                                          \
>  #define TEST_ARGS(bit, op, init, ret, expect, args...)         \
>  do {                                                           \
>         atomic##bit##_set(&v, init);                            \
> -       BUG_ON(atomic##bit##_##op(&v, ##args) != ret);          \
> -       BUG_ON(atomic##bit##_read(&v) != expect);               \
> +       KUNIT_ASSERT_EQ(test, atomic##bit##_##op(&v, ##args), ret);\
> +       KUNIT_ASSERT_EQ(test, atomic##bit##_read(&v), expect);  \
>  } while (0)
>
>  #define XCHG_FAMILY_TEST(bit, init, new)                               \
> @@ -101,7 +95,7 @@ do {                                                 \
>                         i, (i) - one, (i) - one);       \
>  } while (0)
>
> -static __init void test_atomic(void)
> +static void test_atomic(struct kunit *test)
>  {
>         int v0 = 0xaaa31337;
>         int v1 = 0xdeadbeef;
> @@ -144,7 +138,7 @@ static __init void test_atomic(void)
>  }
>
>  #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
> -static __init void test_atomic64(void)
> +static void test_atomic64(struct kunit *test)
>  {
>         long long v0 = 0xaaa31337c001d00dLL;
>         long long v1 = 0xdeadbeefdeafcafeLL;
> @@ -156,12 +150,12 @@ static __init void test_atomic64(void)
>
>         atomic64_t v = ATOMIC64_INIT(v0);
>         long long r = v0;
> -       BUG_ON(v.counter != r);
> +       KUNIT_ASSERT_EQ(test, v.counter, r);
>
>         atomic64_set(&v, v1);
>         r = v1;
> -       BUG_ON(v.counter != r);
> -       BUG_ON(atomic64_read(&v) != r);
> +       KUNIT_ASSERT_EQ(test, v.counter, r);
> +       KUNIT_ASSERT_EQ(test, atomic64_read(&v), r);
>
>         TEST(64, add, +=, onestwos);
>         TEST(64, add, +=, -one);
> @@ -190,12 +184,12 @@ static __init void test_atomic64(void)
>         INIT(v0);
>         atomic64_inc(&v);
>         r += one;
> -       BUG_ON(v.counter != r);
> +       KUNIT_ASSERT_EQ(test, v.counter, r);
>
>         INIT(v0);
>         atomic64_dec(&v);
>         r -= one;
> -       BUG_ON(v.counter != r);
> +       KUNIT_ASSERT_EQ(test, v.counter, r);
>
>         INC_RETURN_FAMILY_TEST(64, v0);
>         DEC_RETURN_FAMILY_TEST(64, v0);
> @@ -204,73 +198,76 @@ static __init void test_atomic64(void)
>         CMPXCHG_FAMILY_TEST(64, v0, v1, v2);
>
>         INIT(v0);
> -       BUG_ON(atomic64_add_unless(&v, one, v0));
> -       BUG_ON(v.counter != r);
> +       KUNIT_ASSERT_FALSE(test, atomic64_add_unless(&v, one, v0));
> +       KUNIT_ASSERT_EQ(test, v.counter, r);
>
>         INIT(v0);
> -       BUG_ON(!atomic64_add_unless(&v, one, v1));
> +       KUNIT_ASSERT_TRUE(test, atomic64_add_unless(&v, one, v1));
>         r += one;
> -       BUG_ON(v.counter != r);
> +       KUNIT_ASSERT_EQ(test, v.counter, r);
>
>         INIT(onestwos);
> -       BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
> +       KUNIT_ASSERT_EQ(test, atomic64_dec_if_positive(&v), (onestwos - 1));
>         r -= one;
> -       BUG_ON(v.counter != r);
> +       KUNIT_ASSERT_EQ(test, v.counter, r);
>
>         INIT(0);
> -       BUG_ON(atomic64_dec_if_positive(&v) != -one);
> -       BUG_ON(v.counter != r);
> +       KUNIT_ASSERT_EQ(test, atomic64_dec_if_positive(&v), -one);
> +       KUNIT_ASSERT_EQ(test, v.counter, r);
>
>         INIT(-one);
> -       BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
> -       BUG_ON(v.counter != r);
> +       KUNIT_ASSERT_EQ(test, atomic64_dec_if_positive(&v), (-one - one));
> +       KUNIT_ASSERT_EQ(test, v.counter, r);
>
>         INIT(onestwos);
> -       BUG_ON(!atomic64_inc_not_zero(&v));
> +       KUNIT_ASSERT_TRUE(test, atomic64_inc_not_zero(&v));
>         r += one;
> -       BUG_ON(v.counter != r);
> +       KUNIT_ASSERT_EQ(test, v.counter, r);
>
>         INIT(0);
> -       BUG_ON(atomic64_inc_not_zero(&v));
> -       BUG_ON(v.counter != r);
> +       KUNIT_ASSERT_FALSE(test, atomic64_inc_not_zero(&v));
> +       KUNIT_ASSERT_EQ(test, v.counter, r);
>
>         INIT(-one);
> -       BUG_ON(!atomic64_inc_not_zero(&v));
> +       KUNIT_ASSERT_TRUE(test, atomic64_inc_not_zero(&v));
>         r += one;
> -       BUG_ON(v.counter != r);
> +       KUNIT_ASSERT_EQ(test, v.counter, r);
>
>         /* Confirm the return value fits in an int, even if the value doesn't */
>         INIT(v3);
> +
>         r_int = atomic64_inc_not_zero(&v);
> -       BUG_ON(!r_int);
> +       KUNIT_ASSERT_NE(test, r_int, 0);
>  }
>
> -static __init int test_atomics_init(void)
> -{
> -       test_atomic();
> -       test_atomic64();
> +static struct kunit_case atomic_test_cases[] = {
> +       KUNIT_CASE(test_atomic),
> +       KUNIT_CASE(test_atomic64),
> +       {},
> +};
>
> +static void atomic_suite_exit(struct kunit_suite *suite)
> +{
>  #ifdef CONFIG_X86
> -       pr_info("passed for %s platform %s CX8 and %s SSE\n",
> +       kunit_info(suite, "ran on %s platform %s CX8 and %s SSE\n",
>  #ifdef CONFIG_X86_64
> -               "x86-64",
> +                  "x86-64",
>  #elif defined(CONFIG_X86_CMPXCHG64)
> -               "i586+",
> +                  "i586+",
>  #else
> -               "i386+",
> +                  "i386+",
>  #endif
> -              boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
> -              boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
> -#else
> -       pr_info("passed\n");
> +                  boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
> +                  boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
>  #endif
> -
> -       return 0;
>  }
>
> -static __exit void test_atomics_exit(void) {}
> +static struct kunit_suite atomic_test_suite = {
> +       .name = "atomic",
> +       .test_cases = atomic_test_cases,
> +       .suite_exit = atomic_suite_exit,
> +};
>
> -module_init(test_atomics_init);
> -module_exit(test_atomics_exit);
> +kunit_test_suites(&atomic_test_suite);
>
>  MODULE_LICENSE("GPL");
>
> base-commit: 38289a26e1b8a37755f3e07056ca416c1ee2a2e8
> --
> 2.36.0.464.gb9c8b46e94-goog
>

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

* Re: [PATCH] lib/atomic64_test.c: convert to use KUnit
  2022-05-02 19:23 [PATCH] lib/atomic64_test.c: convert to use KUnit Daniel Latypov
  2022-05-03  6:13 ` David Gow
@ 2022-05-12 18:52 ` Brendan Higgins
  2022-05-13  3:05 ` Michael Ellerman
  2 siblings, 0 replies; 8+ messages in thread
From: Brendan Higgins @ 2022-05-12 18:52 UTC (permalink / raw)
  To: Daniel Latypov
  Cc: davidgow, linux-kernel, kunit-dev, linux-kselftest, skhan, mpe

On Mon, May 2, 2022 at 3:23 PM Daniel Latypov <dlatypov@google.com> wrote:
>
> The test currently is a bunch of checks (implemented using BUG_ON())
> that can be built into the kernel or as a module.
>
> Convert it to a KUnit test, which can also run in both modes.
> From a user's perspective, this change adds a CONFIG_KUNIT=y dep and
> changes the output format of the test [1]. The test itself is the same.
>
> This hopefully makes the test easier to run and more consistent with
> similar tests in lib/.
> Since it has no dependencies, it can be run without explicitly setting
> up a .kunitconfig via
> $ ./tools/testing/kunit/kunit.py run atomic
> ...
> [13:53:44] Starting KUnit Kernel (1/1)...
> [13:53:44] ============================================================
> [13:53:47] =================== atomic (2 subtests) ====================
> [13:53:47] [PASSED] test_atomic
> [13:53:47] [PASSED] test_atomic64
> [13:53:47] ===================== [PASSED] atomic ======================
> [13:53:47] ============================================================
> [13:53:47] Testing complete. Passed: 2, Failed: 0, Crashed: 0, Skipped: 0, Errors: 0
> [13:53:47] Elapsed time: 13.902s total, 1.629s configuring, 9.331s building, 2.852s running
>
> It can be run on ARCH=x86_64 (and others) via:
> $ ./tools/testing/kunit/kunit.py run --arch=x86_64 atomic
>
> The message about which platform the test ran on won't show up in
> kunit.py, but still gets printed out in dmesg, e.g.
> > TAP version 14
> > 1..1
> >     # Subtest: atomic
> >     1..2
> >     ok 1 - test_atomic
> >     ok 2 - test_atomic64
> >     # atomic: ran on x86-64 platform with CX8 and with SSE
> > # atomic: pass:2 fail:0 skip:0 total:2
> > # Totals: pass:2 fail:0 skip:0 total:2
> > ok 1 - atomic
>
> Signed-off-by: Daniel Latypov <dlatypov@google.com>

I am also not an expert, but it looks good to me.

Reviewed-by: Brendan Higgins <brendanhiggins@google.com>

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

* Re: [PATCH] lib/atomic64_test.c: convert to use KUnit
  2022-05-02 19:23 [PATCH] lib/atomic64_test.c: convert to use KUnit Daniel Latypov
  2022-05-03  6:13 ` David Gow
  2022-05-12 18:52 ` Brendan Higgins
@ 2022-05-13  3:05 ` Michael Ellerman
  2022-05-13 16:25   ` Daniel Latypov
  2 siblings, 1 reply; 8+ messages in thread
From: Michael Ellerman @ 2022-05-13  3:05 UTC (permalink / raw)
  To: Daniel Latypov, brendanhiggins, davidgow
  Cc: linux-kernel, kunit-dev, linux-kselftest, skhan, Daniel Latypov

Daniel Latypov <dlatypov@google.com> writes:
> The test currently is a bunch of checks (implemented using BUG_ON())
> that can be built into the kernel or as a module.
>
> Convert it to a KUnit test, which can also run in both modes.
> From a user's perspective, this change adds a CONFIG_KUNIT=y dep and
> changes the output format of the test [1]. The test itself is the same.
...

I don't know why I got Cc'ed on this :), but I gave it a quick test anyway.

Seems to work fine on a Power9.
I also flipped some of the conditionals to make sure failure is detected
correctly.

Tested-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc)


> Meta:
> 1. this patch applies on top of the kunit branch,
> https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git/?h=kunit
>
> 2. checkpatch complains about aligning with parens, but it wants me to
> indent the `#ifdef CONFIG_X86_64` which seems inappropriate in context.
>
> 3. this file doesn't seem to have a clear maintainer, so I assume this
> conversion is fine to go through the kunit branch.

I think you want to at least Cc the atomic folks:

ATOMIC INFRASTRUCTURE
M:	Will Deacon <will@kernel.org>
M:	Peter Zijlstra <peterz@infradead.org>
R:	Boqun Feng <boqun.feng@gmail.com>
R:	Mark Rutland <mark.rutland@arm.com>
L:	linux-kernel@vger.kernel.org
S:	Maintained


cheers

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

* Re: [PATCH] lib/atomic64_test.c: convert to use KUnit
  2022-05-13  3:05 ` Michael Ellerman
@ 2022-05-13 16:25   ` Daniel Latypov
  0 siblings, 0 replies; 8+ messages in thread
From: Daniel Latypov @ 2022-05-13 16:25 UTC (permalink / raw)
  To: Michael Ellerman
  Cc: brendanhiggins, davidgow, linux-kernel, kunit-dev,
	linux-kselftest, skhan

On Thu, May 12, 2022 at 8:06 PM Michael Ellerman <mpe@ellerman.id.au> wrote:
>
> Daniel Latypov <dlatypov@google.com> writes:
> > The test currently is a bunch of checks (implemented using BUG_ON())
> > that can be built into the kernel or as a module.
> >
> > Convert it to a KUnit test, which can also run in both modes.
> > From a user's perspective, this change adds a CONFIG_KUNIT=y dep and
> > changes the output format of the test [1]. The test itself is the same.
> ...
>
> I don't know why I got Cc'ed on this :), but I gave it a quick test anyway.
>
> Seems to work fine on a Power9.
> I also flipped some of the conditionals to make sure failure is detected
> correctly.
>
> Tested-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc)

Thanks!

You were just the last person to have made a real change to this file [1].
The people signing off no commits seemed inconsistent and
get_maintainers didn't give anything back.

commit ffba19ccae8d98beb0a17345a0b1ee9e415b23b8
Author: Michael Ellerman <mpe@ellerman.id.au>
Date:   Fri Jul 14 14:49:41 2017 -0700

>
>
> > Meta:
> > 1. this patch applies on top of the kunit branch,
> > https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git/?h=kunit
> >
> > 2. checkpatch complains about aligning with parens, but it wants me to
> > indent the `#ifdef CONFIG_X86_64` which seems inappropriate in context.
> >
> > 3. this file doesn't seem to have a clear maintainer, so I assume this
> > conversion is fine to go through the kunit branch.
>
> I think you want to at least Cc the atomic folks:
>
> ATOMIC INFRASTRUCTURE
> M:      Will Deacon <will@kernel.org>
> M:      Peter Zijlstra <peterz@infradead.org>
> R:      Boqun Feng <boqun.feng@gmail.com>
> R:      Mark Rutland <mark.rutland@arm.com>
> L:      linux-kernel@vger.kernel.org
> S:      Maintained

Ah, thanks for the pointer.

I'll see if I can add
  F:      lib/atomic64_test.c
to that entry so this files owners get tracked properly.

Daniel

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

* Re: [PATCH] lib/atomic64_test.c: convert to use KUnit
  2022-06-16 18:04 Daniel Latypov
  2022-06-17  8:02 ` David Gow
@ 2022-06-18  8:16 ` kernel test robot
  1 sibling, 0 replies; 8+ messages in thread
From: kernel test robot @ 2022-06-18  8:16 UTC (permalink / raw)
  To: Daniel Latypov, mark.rutland, boqun.feng
  Cc: kbuild-all, linux-kernel, kunit-dev, peterz, Daniel Latypov,
	David Gow, Brendan Higgins, Michael Ellerman

Hi Daniel,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on 8ab2afa23bd197df47819a87f0265c0ac95c5b6a]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Latypov/lib-atomic64_test-c-convert-to-use-KUnit/20220617-020546
base:   8ab2afa23bd197df47819a87f0265c0ac95c5b6a
config: mips-allyesconfig (https://download.01.org/0day-ci/archive/20220618/202206181607.somVaD8p-lkp@intel.com/config)
compiler: mips-linux-gcc (GCC) 11.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/acffbe860bc2206b4cef16408809b9f558a24465
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Daniel-Latypov/lib-atomic64_test-c-convert-to-use-KUnit/20220617-020546
        git checkout acffbe860bc2206b4cef16408809b9f558a24465
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross W=1 O=build_dir ARCH=mips SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   lib/atomic64_test.c: In function 'test_atomic64':
>> lib/atomic64_test.c:241:1: warning: the frame size of 3920 bytes is larger than 2048 bytes [-Wframe-larger-than=]
     241 | }
         | ^
   lib/atomic64_test.c: In function 'test_atomic':
   lib/atomic64_test.c:138:1: warning: the frame size of 3520 bytes is larger than 2048 bytes [-Wframe-larger-than=]
     138 | }
         | ^


vim +241 lib/atomic64_test.c

41b9e9fcc1c44b Peter Zijlstra   2015-07-13  139  
86a8938078a8bb Luca Barbieri    2010-02-24  140  #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
acffbe860bc220 Daniel Latypov   2022-06-16  141  static void test_atomic64(struct kunit *test)
86a8938078a8bb Luca Barbieri    2010-02-24  142  {
86a8938078a8bb Luca Barbieri    2010-02-24  143  	long long v0 = 0xaaa31337c001d00dLL;
86a8938078a8bb Luca Barbieri    2010-02-24  144  	long long v1 = 0xdeadbeefdeafcafeLL;
86a8938078a8bb Luca Barbieri    2010-02-24  145  	long long v2 = 0xfaceabadf00df001LL;
ffba19ccae8d98 Michael Ellerman 2017-07-14  146  	long long v3 = 0x8000000000000000LL;
86a8938078a8bb Luca Barbieri    2010-02-24  147  	long long onestwos = 0x1111111122222222LL;
86a8938078a8bb Luca Barbieri    2010-02-24  148  	long long one = 1LL;
ffba19ccae8d98 Michael Ellerman 2017-07-14  149  	int r_int;
86a8938078a8bb Luca Barbieri    2010-02-24  150  
86a8938078a8bb Luca Barbieri    2010-02-24  151  	atomic64_t v = ATOMIC64_INIT(v0);
86a8938078a8bb Luca Barbieri    2010-02-24  152  	long long r = v0;
acffbe860bc220 Daniel Latypov   2022-06-16  153  	KUNIT_ASSERT_EQ(test, v.counter, r);
86a8938078a8bb Luca Barbieri    2010-02-24  154  
86a8938078a8bb Luca Barbieri    2010-02-24  155  	atomic64_set(&v, v1);
86a8938078a8bb Luca Barbieri    2010-02-24  156  	r = v1;
acffbe860bc220 Daniel Latypov   2022-06-16  157  	KUNIT_ASSERT_EQ(test, v.counter, r);
acffbe860bc220 Daniel Latypov   2022-06-16  158  	KUNIT_ASSERT_EQ(test, atomic64_read(&v), r);
86a8938078a8bb Luca Barbieri    2010-02-24  159  
41b9e9fcc1c44b Peter Zijlstra   2015-07-13  160  	TEST(64, add, +=, onestwos);
41b9e9fcc1c44b Peter Zijlstra   2015-07-13  161  	TEST(64, add, +=, -one);
41b9e9fcc1c44b Peter Zijlstra   2015-07-13  162  	TEST(64, sub, -=, onestwos);
41b9e9fcc1c44b Peter Zijlstra   2015-07-13  163  	TEST(64, sub, -=, -one);
41b9e9fcc1c44b Peter Zijlstra   2015-07-13  164  	TEST(64, or, |=, v1);
41b9e9fcc1c44b Peter Zijlstra   2015-07-13  165  	TEST(64, and, &=, v1);
41b9e9fcc1c44b Peter Zijlstra   2015-07-13  166  	TEST(64, xor, ^=, v1);
41b9e9fcc1c44b Peter Zijlstra   2015-07-13  167  	TEST(64, andnot, &= ~, v1);
86a8938078a8bb Luca Barbieri    2010-02-24  168  
978e5a3692c3b6 Boqun Feng       2015-11-04  169  	RETURN_FAMILY_TEST(64, add_return, +=, onestwos);
978e5a3692c3b6 Boqun Feng       2015-11-04  170  	RETURN_FAMILY_TEST(64, add_return, +=, -one);
978e5a3692c3b6 Boqun Feng       2015-11-04  171  	RETURN_FAMILY_TEST(64, sub_return, -=, onestwos);
978e5a3692c3b6 Boqun Feng       2015-11-04  172  	RETURN_FAMILY_TEST(64, sub_return, -=, -one);
86a8938078a8bb Luca Barbieri    2010-02-24  173  
28aa2bda2211f4 Peter Zijlstra   2016-04-18  174  	FETCH_FAMILY_TEST(64, fetch_add, +=, onestwos);
28aa2bda2211f4 Peter Zijlstra   2016-04-18  175  	FETCH_FAMILY_TEST(64, fetch_add, +=, -one);
28aa2bda2211f4 Peter Zijlstra   2016-04-18  176  	FETCH_FAMILY_TEST(64, fetch_sub, -=, onestwos);
28aa2bda2211f4 Peter Zijlstra   2016-04-18  177  	FETCH_FAMILY_TEST(64, fetch_sub, -=, -one);
28aa2bda2211f4 Peter Zijlstra   2016-04-18  178  
28aa2bda2211f4 Peter Zijlstra   2016-04-18  179  	FETCH_FAMILY_TEST(64, fetch_or,  |=, v1);
28aa2bda2211f4 Peter Zijlstra   2016-04-18  180  	FETCH_FAMILY_TEST(64, fetch_and, &=, v1);
28aa2bda2211f4 Peter Zijlstra   2016-04-18  181  	FETCH_FAMILY_TEST(64, fetch_andnot, &= ~, v1);
28aa2bda2211f4 Peter Zijlstra   2016-04-18  182  	FETCH_FAMILY_TEST(64, fetch_xor, ^=, v1);
28aa2bda2211f4 Peter Zijlstra   2016-04-18  183  
86a8938078a8bb Luca Barbieri    2010-02-24  184  	INIT(v0);
86a8938078a8bb Luca Barbieri    2010-02-24  185  	atomic64_inc(&v);
86a8938078a8bb Luca Barbieri    2010-02-24  186  	r += one;
acffbe860bc220 Daniel Latypov   2022-06-16  187  	KUNIT_ASSERT_EQ(test, v.counter, r);
86a8938078a8bb Luca Barbieri    2010-02-24  188  
86a8938078a8bb Luca Barbieri    2010-02-24  189  	INIT(v0);
86a8938078a8bb Luca Barbieri    2010-02-24  190  	atomic64_dec(&v);
86a8938078a8bb Luca Barbieri    2010-02-24  191  	r -= one;
acffbe860bc220 Daniel Latypov   2022-06-16  192  	KUNIT_ASSERT_EQ(test, v.counter, r);
86a8938078a8bb Luca Barbieri    2010-02-24  193  
978e5a3692c3b6 Boqun Feng       2015-11-04  194  	INC_RETURN_FAMILY_TEST(64, v0);
978e5a3692c3b6 Boqun Feng       2015-11-04  195  	DEC_RETURN_FAMILY_TEST(64, v0);
86a8938078a8bb Luca Barbieri    2010-02-24  196  
978e5a3692c3b6 Boqun Feng       2015-11-04  197  	XCHG_FAMILY_TEST(64, v0, v1);
978e5a3692c3b6 Boqun Feng       2015-11-04  198  	CMPXCHG_FAMILY_TEST(64, v0, v1, v2);
86a8938078a8bb Luca Barbieri    2010-02-24  199  
86a8938078a8bb Luca Barbieri    2010-02-24  200  	INIT(v0);
acffbe860bc220 Daniel Latypov   2022-06-16  201  	KUNIT_ASSERT_FALSE(test, atomic64_add_unless(&v, one, v0));
acffbe860bc220 Daniel Latypov   2022-06-16  202  	KUNIT_ASSERT_EQ(test, v.counter, r);
86a8938078a8bb Luca Barbieri    2010-02-24  203  
86a8938078a8bb Luca Barbieri    2010-02-24  204  	INIT(v0);
acffbe860bc220 Daniel Latypov   2022-06-16  205  	KUNIT_ASSERT_TRUE(test, atomic64_add_unless(&v, one, v1));
86a8938078a8bb Luca Barbieri    2010-02-24  206  	r += one;
acffbe860bc220 Daniel Latypov   2022-06-16  207  	KUNIT_ASSERT_EQ(test, v.counter, r);
86a8938078a8bb Luca Barbieri    2010-02-24  208  
86a8938078a8bb Luca Barbieri    2010-02-24  209  	INIT(onestwos);
acffbe860bc220 Daniel Latypov   2022-06-16  210  	KUNIT_ASSERT_EQ(test, atomic64_dec_if_positive(&v), (onestwos - 1));
86a8938078a8bb Luca Barbieri    2010-02-24  211  	r -= one;
acffbe860bc220 Daniel Latypov   2022-06-16  212  	KUNIT_ASSERT_EQ(test, v.counter, r);
86a8938078a8bb Luca Barbieri    2010-02-24  213  
86a8938078a8bb Luca Barbieri    2010-02-24  214  	INIT(0);
acffbe860bc220 Daniel Latypov   2022-06-16  215  	KUNIT_ASSERT_EQ(test, atomic64_dec_if_positive(&v), -one);
acffbe860bc220 Daniel Latypov   2022-06-16  216  	KUNIT_ASSERT_EQ(test, v.counter, r);
86a8938078a8bb Luca Barbieri    2010-02-24  217  
86a8938078a8bb Luca Barbieri    2010-02-24  218  	INIT(-one);
acffbe860bc220 Daniel Latypov   2022-06-16  219  	KUNIT_ASSERT_EQ(test, atomic64_dec_if_positive(&v), (-one - one));
acffbe860bc220 Daniel Latypov   2022-06-16  220  	KUNIT_ASSERT_EQ(test, v.counter, r);
86a8938078a8bb Luca Barbieri    2010-02-24  221  
86a8938078a8bb Luca Barbieri    2010-02-24  222  	INIT(onestwos);
acffbe860bc220 Daniel Latypov   2022-06-16  223  	KUNIT_ASSERT_TRUE(test, atomic64_inc_not_zero(&v));
86a8938078a8bb Luca Barbieri    2010-02-24  224  	r += one;
acffbe860bc220 Daniel Latypov   2022-06-16  225  	KUNIT_ASSERT_EQ(test, v.counter, r);
86a8938078a8bb Luca Barbieri    2010-02-24  226  
86a8938078a8bb Luca Barbieri    2010-02-24  227  	INIT(0);
acffbe860bc220 Daniel Latypov   2022-06-16  228  	KUNIT_ASSERT_FALSE(test, atomic64_inc_not_zero(&v));
acffbe860bc220 Daniel Latypov   2022-06-16  229  	KUNIT_ASSERT_EQ(test, v.counter, r);
86a8938078a8bb Luca Barbieri    2010-02-24  230  
86a8938078a8bb Luca Barbieri    2010-02-24  231  	INIT(-one);
acffbe860bc220 Daniel Latypov   2022-06-16  232  	KUNIT_ASSERT_TRUE(test, atomic64_inc_not_zero(&v));
86a8938078a8bb Luca Barbieri    2010-02-24  233  	r += one;
acffbe860bc220 Daniel Latypov   2022-06-16  234  	KUNIT_ASSERT_EQ(test, v.counter, r);
ffba19ccae8d98 Michael Ellerman 2017-07-14  235  
ffba19ccae8d98 Michael Ellerman 2017-07-14  236  	/* Confirm the return value fits in an int, even if the value doesn't */
ffba19ccae8d98 Michael Ellerman 2017-07-14  237  	INIT(v3);
acffbe860bc220 Daniel Latypov   2022-06-16  238  
ffba19ccae8d98 Michael Ellerman 2017-07-14  239  	r_int = atomic64_inc_not_zero(&v);
acffbe860bc220 Daniel Latypov   2022-06-16  240  	KUNIT_ASSERT_NE(test, r_int, 0);
41b9e9fcc1c44b Peter Zijlstra   2015-07-13 @241  }
41b9e9fcc1c44b Peter Zijlstra   2015-07-13  242  

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp

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

* Re: [PATCH] lib/atomic64_test.c: convert to use KUnit
  2022-06-16 18:04 Daniel Latypov
@ 2022-06-17  8:02 ` David Gow
  2022-06-18  8:16 ` kernel test robot
  1 sibling, 0 replies; 8+ messages in thread
From: David Gow @ 2022-06-17  8:02 UTC (permalink / raw)
  To: Daniel Latypov
  Cc: mark.rutland, boqun.feng, Linux Kernel Mailing List,
	KUnit Development, Peter Zijlstra, Brendan Higgins,
	Michael Ellerman

On Fri, Jun 17, 2022 at 2:04 AM Daniel Latypov <dlatypov@google.com> wrote:
>
> The test currently is a bunch of checks (implemented using BUG_ON())
> that can be built into the kernel or as a module.
>
> Convert it to a KUnit test, which can also run in both modes.
> From a user's perspective, this change adds a CONFIG_KUNIT=y dep and
> changes the output format of the test [1] and makes it less destructive
> on failure. The test itself is the same.
>
> This hopefully makes the test easier to run and more consistent with
> similar tests in lib/.
> Since it has no dependencies, it can be run without explicitly setting
> up a .kunitconfig via
> $ ./tools/testing/kunit/kunit.py run atomic
> ...
> [13:53:44] Starting KUnit Kernel (1/1)...
> [13:53:44] ============================================================
> [13:53:47] =================== atomic (2 subtests) ====================
> [13:53:47] [PASSED] test_atomic
> [13:53:47] [PASSED] test_atomic64
> [13:53:47] ===================== [PASSED] atomic ======================
> [13:53:47] ============================================================
> [13:53:47] Testing complete. Passed: 2, Failed: 0, Crashed: 0, Skipped: 0, Errors: 0
> [13:53:47] Elapsed time: 13.902s total, 1.629s configuring, 9.331s building, 2.852s running
>
> It can be run on ARCH=x86_64 (and others) via:
> $ ./tools/testing/kunit/kunit.py run --arch=x86_64 atomic
>
> The message about which platform the test ran on won't show up in
> kunit.py, but still gets printed out in dmesg, e.g.
> > TAP version 14
> > 1..1
> >     # Subtest: atomic
> >     1..2
> >     ok 1 - test_atomic
> >     ok 2 - test_atomic64
> >     # atomic: ran on x86-64 platform with CX8 and with SSE
> > # atomic: pass:2 fail:0 skip:0 total:2
> > # Totals: pass:2 fail:0 skip:0 total:2
> > ok 1 - atomic
>
> [1] https://www.kernel.org/doc/html/latest/dev-tools/ktap.html
>
> Signed-off-by: Daniel Latypov <dlatypov@google.com>
> Reviewed-by: David Gow <davidgow@google.com>
> Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
> Tested-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc)
> ---
> Meta: this is a resend of https://lore.kernel.org/linux-kselftest/20220502192327.81153-1-dlatypov@google.com/
> Michael kindly pointed me to the right MAINTAINERS entry (this test file
> isn't covered by it, and it slipped my mind to check the non-test code).
>
> I've waited until 5.19-rc1 so that the relevant KUnit patches this
> depended on have been merged.
> Rebasing and tweaking the commit message a bit are the only changes.
> ---

Tested this again just to be sure, and it still builds, runs, and
passes for me on:

- UML (x86_64)
- x86_64 + qemu
- i386 + qemu
- arm64 + qemu + clang/LLVM

Cheers,
-- David

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

* [PATCH] lib/atomic64_test.c: convert to use KUnit
@ 2022-06-16 18:04 Daniel Latypov
  2022-06-17  8:02 ` David Gow
  2022-06-18  8:16 ` kernel test robot
  0 siblings, 2 replies; 8+ messages in thread
From: Daniel Latypov @ 2022-06-16 18:04 UTC (permalink / raw)
  To: mark.rutland, boqun.feng
  Cc: linux-kernel, kunit-dev, peterz, Daniel Latypov, David Gow,
	Brendan Higgins, Michael Ellerman

The test currently is a bunch of checks (implemented using BUG_ON())
that can be built into the kernel or as a module.

Convert it to a KUnit test, which can also run in both modes.
From a user's perspective, this change adds a CONFIG_KUNIT=y dep and
changes the output format of the test [1] and makes it less destructive
on failure. The test itself is the same.

This hopefully makes the test easier to run and more consistent with
similar tests in lib/.
Since it has no dependencies, it can be run without explicitly setting
up a .kunitconfig via
$ ./tools/testing/kunit/kunit.py run atomic
...
[13:53:44] Starting KUnit Kernel (1/1)...
[13:53:44] ============================================================
[13:53:47] =================== atomic (2 subtests) ====================
[13:53:47] [PASSED] test_atomic
[13:53:47] [PASSED] test_atomic64
[13:53:47] ===================== [PASSED] atomic ======================
[13:53:47] ============================================================
[13:53:47] Testing complete. Passed: 2, Failed: 0, Crashed: 0, Skipped: 0, Errors: 0
[13:53:47] Elapsed time: 13.902s total, 1.629s configuring, 9.331s building, 2.852s running

It can be run on ARCH=x86_64 (and others) via:
$ ./tools/testing/kunit/kunit.py run --arch=x86_64 atomic

The message about which platform the test ran on won't show up in
kunit.py, but still gets printed out in dmesg, e.g.
> TAP version 14
> 1..1
>     # Subtest: atomic
>     1..2
>     ok 1 - test_atomic
>     ok 2 - test_atomic64
>     # atomic: ran on x86-64 platform with CX8 and with SSE
> # atomic: pass:2 fail:0 skip:0 total:2
> # Totals: pass:2 fail:0 skip:0 total:2
> ok 1 - atomic

[1] https://www.kernel.org/doc/html/latest/dev-tools/ktap.html

Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Tested-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc)
---
Meta: this is a resend of https://lore.kernel.org/linux-kselftest/20220502192327.81153-1-dlatypov@google.com/
Michael kindly pointed me to the right MAINTAINERS entry (this test file
isn't covered by it, and it slipped my mind to check the non-test code).

I've waited until 5.19-rc1 so that the relevant KUnit patches this
depended on have been merged.
Rebasing and tweaking the commit message a bit are the only changes.
---
 lib/Kconfig.debug   |   4 +-
 lib/atomic64_test.c | 107 +++++++++++++++++++++-----------------------
 2 files changed, 55 insertions(+), 56 deletions(-)

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 2e24db4bff19..4cf1ce8910f6 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2147,7 +2147,9 @@ config PERCPU_TEST
 	  If unsure, say N.
 
 config ATOMIC64_SELFTEST
-	tristate "Perform an atomic64_t self-test"
+	tristate "Perform an atomic64_t self-test" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
 	help
 	  Enable this option to test the atomic64_t functions at boot or
 	  at module load time.
diff --git a/lib/atomic64_test.c b/lib/atomic64_test.c
index d9d170238165..46cb0130f8d0 100644
--- a/lib/atomic64_test.c
+++ b/lib/atomic64_test.c
@@ -5,13 +5,9 @@
  * Copyright © 2010  Luca Barbieri
  */
 
-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#include <kunit/test.h>
 
-#include <linux/init.h>
-#include <linux/bug.h>
-#include <linux/kernel.h>
 #include <linux/atomic.h>
-#include <linux/module.h>
 
 #ifdef CONFIG_X86
 #include <asm/cpufeature.h>	/* for boot_cpu_has below */
@@ -23,9 +19,7 @@ do {								\
 	r = v0;							\
 	atomic##bit##_##op(val, &v);				\
 	r c_op val;						\
-	WARN(atomic##bit##_read(&v) != r, "%Lx != %Lx\n",	\
-		(unsigned long long)atomic##bit##_read(&v),	\
-		(unsigned long long)r);				\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_read(&v), r);	\
 } while (0)
 
 /*
@@ -46,8 +40,8 @@ do {								\
 	atomic##bit##_set(&v, v0);				\
 	r = v0;							\
 	r c_op val;						\
-	BUG_ON(atomic##bit##_##op(val, &v) != r);		\
-	BUG_ON(atomic##bit##_read(&v) != r);			\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_##op(val, &v), r);	\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_read(&v), r);	\
 } while (0)
 
 #define TEST_FETCH(bit, op, c_op, val)				\
@@ -55,8 +49,8 @@ do {								\
 	atomic##bit##_set(&v, v0);				\
 	r = v0;							\
 	r c_op val;						\
-	BUG_ON(atomic##bit##_##op(val, &v) != v0);		\
-	BUG_ON(atomic##bit##_read(&v) != r);			\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_##op(val, &v), v0);	\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_read(&v), r);	\
 } while (0)
 
 #define RETURN_FAMILY_TEST(bit, op, c_op, val)			\
@@ -72,8 +66,8 @@ do {								\
 #define TEST_ARGS(bit, op, init, ret, expect, args...)		\
 do {								\
 	atomic##bit##_set(&v, init);				\
-	BUG_ON(atomic##bit##_##op(&v, ##args) != ret);		\
-	BUG_ON(atomic##bit##_read(&v) != expect);		\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_##op(&v, ##args), ret);\
+	KUNIT_ASSERT_EQ(test, atomic##bit##_read(&v), expect);	\
 } while (0)
 
 #define XCHG_FAMILY_TEST(bit, init, new)				\
@@ -101,7 +95,7 @@ do {							\
 			i, (i) - one, (i) - one);	\
 } while (0)
 
-static __init void test_atomic(void)
+static void test_atomic(struct kunit *test)
 {
 	int v0 = 0xaaa31337;
 	int v1 = 0xdeadbeef;
@@ -144,7 +138,7 @@ static __init void test_atomic(void)
 }
 
 #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
-static __init void test_atomic64(void)
+static void test_atomic64(struct kunit *test)
 {
 	long long v0 = 0xaaa31337c001d00dLL;
 	long long v1 = 0xdeadbeefdeafcafeLL;
@@ -156,12 +150,12 @@ static __init void test_atomic64(void)
 
 	atomic64_t v = ATOMIC64_INIT(v0);
 	long long r = v0;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	atomic64_set(&v, v1);
 	r = v1;
-	BUG_ON(v.counter != r);
-	BUG_ON(atomic64_read(&v) != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
+	KUNIT_ASSERT_EQ(test, atomic64_read(&v), r);
 
 	TEST(64, add, +=, onestwos);
 	TEST(64, add, +=, -one);
@@ -190,12 +184,12 @@ static __init void test_atomic64(void)
 	INIT(v0);
 	atomic64_inc(&v);
 	r += one;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(v0);
 	atomic64_dec(&v);
 	r -= one;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INC_RETURN_FAMILY_TEST(64, v0);
 	DEC_RETURN_FAMILY_TEST(64, v0);
@@ -204,73 +198,76 @@ static __init void test_atomic64(void)
 	CMPXCHG_FAMILY_TEST(64, v0, v1, v2);
 
 	INIT(v0);
-	BUG_ON(atomic64_add_unless(&v, one, v0));
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_FALSE(test, atomic64_add_unless(&v, one, v0));
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(v0);
-	BUG_ON(!atomic64_add_unless(&v, one, v1));
+	KUNIT_ASSERT_TRUE(test, atomic64_add_unless(&v, one, v1));
 	r += one;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(onestwos);
-	BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
+	KUNIT_ASSERT_EQ(test, atomic64_dec_if_positive(&v), (onestwos - 1));
 	r -= one;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(0);
-	BUG_ON(atomic64_dec_if_positive(&v) != -one);
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, atomic64_dec_if_positive(&v), -one);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(-one);
-	BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, atomic64_dec_if_positive(&v), (-one - one));
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(onestwos);
-	BUG_ON(!atomic64_inc_not_zero(&v));
+	KUNIT_ASSERT_TRUE(test, atomic64_inc_not_zero(&v));
 	r += one;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(0);
-	BUG_ON(atomic64_inc_not_zero(&v));
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_FALSE(test, atomic64_inc_not_zero(&v));
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	INIT(-one);
-	BUG_ON(!atomic64_inc_not_zero(&v));
+	KUNIT_ASSERT_TRUE(test, atomic64_inc_not_zero(&v));
 	r += one;
-	BUG_ON(v.counter != r);
+	KUNIT_ASSERT_EQ(test, v.counter, r);
 
 	/* Confirm the return value fits in an int, even if the value doesn't */
 	INIT(v3);
+
 	r_int = atomic64_inc_not_zero(&v);
-	BUG_ON(!r_int);
+	KUNIT_ASSERT_NE(test, r_int, 0);
 }
 
-static __init int test_atomics_init(void)
-{
-	test_atomic();
-	test_atomic64();
+static struct kunit_case atomic_test_cases[] = {
+	KUNIT_CASE(test_atomic),
+	KUNIT_CASE(test_atomic64),
+	{},
+};
 
+static void atomic_suite_exit(struct kunit_suite *suite)
+{
 #ifdef CONFIG_X86
-	pr_info("passed for %s platform %s CX8 and %s SSE\n",
+	kunit_info(suite, "ran on %s platform %s CX8 and %s SSE\n",
 #ifdef CONFIG_X86_64
-		"x86-64",
+		   "x86-64",
 #elif defined(CONFIG_X86_CMPXCHG64)
-		"i586+",
+		   "i586+",
 #else
-		"i386+",
+		   "i386+",
 #endif
-	       boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
-	       boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
-#else
-	pr_info("passed\n");
+		   boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
+		   boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
 #endif
-
-	return 0;
 }
 
-static __exit void test_atomics_exit(void) {}
+static struct kunit_suite atomic_test_suite = {
+	.name = "atomic",
+	.test_cases = atomic_test_cases,
+	.suite_exit = atomic_suite_exit,
+};
 
-module_init(test_atomics_init);
-module_exit(test_atomics_exit);
+kunit_test_suites(&atomic_test_suite);
 
 MODULE_LICENSE("GPL");

base-commit: 8ab2afa23bd197df47819a87f0265c0ac95c5b6a
-- 
2.36.1.476.g0c4daa206d-goog


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

end of thread, other threads:[~2022-06-18  8:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-02 19:23 [PATCH] lib/atomic64_test.c: convert to use KUnit Daniel Latypov
2022-05-03  6:13 ` David Gow
2022-05-12 18:52 ` Brendan Higgins
2022-05-13  3:05 ` Michael Ellerman
2022-05-13 16:25   ` Daniel Latypov
2022-06-16 18:04 Daniel Latypov
2022-06-17  8:02 ` David Gow
2022-06-18  8:16 ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).