From: Sven Schnelle <svens@linux.ibm.com>
To: Arnd Bergmann <arnd@arndb.de>, Kees Cook <keescook@chromium.org>,
Andy Shevchenko <andy@kernel.org>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] tests: provide a word-at-a-time test implementation
Date: Mon, 9 Oct 2023 14:04:55 +0200 [thread overview]
Message-ID: <20231009120455.173862-1-svens@linux.ibm.com> (raw)
Add some basic tests to test the correctness of has_zero() and
find_zero().
Signed-off-by: Sven Schnelle <svens@linux.ibm.com>
---
lib/Kconfig.debug | 11 +++++++
lib/Makefile | 1 +
lib/test_word-at-a-time.c | 62 +++++++++++++++++++++++++++++++++++++++
3 files changed, 74 insertions(+)
create mode 100644 lib/test_word-at-a-time.c
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index fa307f93fa2e..f7a4df3054ac 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2791,6 +2791,17 @@ config SIPHASH_KUNIT_TEST
This is intended to help people writing architecture-specific
optimized versions. If unsure, say N.
+config WORDATATIME_KUNIT_TEST
+ tristate "KUnit test word-at-a-time implementation at runtime" if !KUNIT_ALL_TESTS
+ depends on KUNIT
+ default KUNIT_ALL_TESTS
+ help
+ Enable this option to test the kernel's word-at-a-time (<asm/word-at-a-time.h>)
+ functions on boot (or module load).
+
+ This is intended to help people writing architecture-specific
+ optimized versions. If unsure, say N.
+
config TEST_UDELAY
tristate "udelay test driver"
help
diff --git a/lib/Makefile b/lib/Makefile
index 740109b6e2c8..b3bc6a698896 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -402,6 +402,7 @@ obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
obj-$(CONFIG_STRCAT_KUNIT_TEST) += strcat_kunit.o
obj-$(CONFIG_STRSCPY_KUNIT_TEST) += strscpy_kunit.o
obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o
+obj-$(CONFIG_WORDATATIME_KUNIT_TEST) += test_word-at-a-time.o
obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
diff --git a/lib/test_word-at-a-time.c b/lib/test_word-at-a-time.c
new file mode 100644
index 000000000000..574303a4f913
--- /dev/null
+++ b/lib/test_word-at-a-time.c
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <linux/module.h>
+#include <linux/printk.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <kunit/test.h>
+#include <asm/word-at-a-time.h>
+
+static void test_wordatatime_has_zero(struct kunit *test)
+{
+ const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
+ unsigned long val, data;
+
+ val = -1UL;
+ KUNIT_ASSERT_FALSE(test, has_zero(val, &data, &constants));
+
+ for (int i = 0; i < BITS_PER_LONG; i += 8) {
+ val = ~(0xffUL << i);
+ KUNIT_ASSERT_TRUE(test, has_zero(val, &data, &constants));
+ }
+
+ for (int i = 0; i < BITS_PER_LONG; i++) {
+ val = ~(0x1UL << i);
+ KUNIT_ASSERT_FALSE(test, has_zero(val, &data, &constants));
+ }
+
+ for (int i = 0; i < BITS_PER_LONG; i++) {
+ val = 0x1UL << i;
+ KUNIT_ASSERT_TRUE(test, has_zero(val, &data, &constants));
+ }
+}
+
+static void test_wordatatime_find_zero(struct kunit *test)
+{
+ const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
+ unsigned long val, data;
+
+ for (int i = 0; i < BITS_PER_LONG; i += 8) {
+ val = ~(0xffUL << i);
+ KUNIT_ASSERT_TRUE(test, has_zero(val, &data, &constants));
+ data = prep_zero_mask(val, data, &constants);
+ data = create_zero_mask(data);
+#ifdef CONFIG_CPU_BIG_ENDIAN
+ KUNIT_ASSERT_EQ(test, find_zero(data), (BITS_PER_LONG / 8 - 1) - (i / 8));
+#else
+ KUNIT_ASSERT_EQ(test, find_zero(data), i / 8);
+#endif
+ }
+}
+static struct kunit_case wordatatime_test_cases[] = {
+ KUNIT_CASE(test_wordatatime_has_zero),
+ KUNIT_CASE(test_wordatatime_find_zero),
+ {}
+};
+
+static struct kunit_suite wordatatime_test_suite = {
+ .name = "wordatatime_test",
+ .test_cases = wordatatime_test_cases,
+};
+
+kunit_test_suites(&wordatatime_test_suite);
+MODULE_LICENSE("GPL");
--
2.39.2
next reply other threads:[~2023-10-09 12:05 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-09 12:04 Sven Schnelle [this message]
2023-10-09 16:49 ` [PATCH] tests: provide a word-at-a-time test implementation Kees Cook
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231009120455.173862-1-svens@linux.ibm.com \
--to=svens@linux.ibm.com \
--cc=andy@kernel.org \
--cc=arnd@arndb.de \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.