* [RFC] mm: Add KUNIT test for ptval_bytes_to_hex_str()
@ 2026-09-29 12:05 Anshuman Khandual
2026-10-01 7:45 ` David Hildenbrand (Arm)
0 siblings, 1 reply; 3+ messages in thread
From: Anshuman Khandual @ 2026-09-29 12:05 UTC (permalink / raw)
To: linux-mm
Cc: Anshuman Khandual, Andrew Morton, David Hildenbrand,
Lorenzo Stoakes, linux-kernel
Add KUnit tests for ptval_bytes_to_hex_str() to verify conversion of 32-bit
64-bit, and 128-bit (when supported) page table entry values into printable
hexadecimal strings.
Add a new PTVAL_HEX_STR_KUNIT_TEST option to enable the tests.
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: David Hildenbrand <david@kernel.org>
Cc: Lorenzo Stoakes <ljs@kernel.org>
Cc: linux-kernel@vger.kernel.org
Cc: linux-mm@kvack.org
Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
---
This patch applies on v7.3-rc5 after the following series
https://lore.kernel.org/linux-mm/20260831054331.625505-1-anshuman.khandual@arm.com/
mm/Kconfig | 11 +++
mm/Makefile | 1 +
mm/tests/ptval_hex_string_kunit.c | 111 ++++++++++++++++++++++++++++++
3 files changed, 123 insertions(+)
create mode 100644 mm/tests/ptval_hex_string_kunit.c
diff --git a/mm/Kconfig b/mm/Kconfig
index 604c58199acbf..f68bf26b98c83 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -1511,6 +1511,17 @@ config LAZY_MMU_MODE_KUNIT_TEST
If unsure, say N.
+config PTVAL_HEX_STR_KUNIT_TEST
+ bool "KUnit tests for pgtable entry string conversion" if !KUNIT_ALL_TESTS
+ depends on KUNIT
+ default KUNIT_ALL_TESTS
+ help
+ Enable this option to check that ptval_bytes_to_hex_str() is able
+ convert given pgtable entry value in bytes into hex strings which
+ could be printed.
+
+ If unsure, say N.
+
source "mm/damon/Kconfig"
endmenu
diff --git a/mm/Makefile b/mm/Makefile
index e7245cb88c665..d08b8bd01917b 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -147,4 +147,5 @@ obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
obj-$(CONFIG_EXECMEM) += execmem.o
obj-$(CONFIG_TMPFS_QUOTA) += shmem_quota.o
obj-$(CONFIG_LAZY_MMU_MODE_KUNIT_TEST) += tests/lazy_mmu_mode_kunit.o
+obj-$(CONFIG_PTVAL_HEX_STR_KUNIT_TEST) += tests/ptval_hex_string_kunit.o
obj-$(CONFIG_MEM_ALLOC_PROFILING) += alloc_tag.o
diff --git a/mm/tests/ptval_hex_string_kunit.c b/mm/tests/ptval_hex_string_kunit.c
new file mode 100644
index 0000000000000..45c203cfb49ae
--- /dev/null
+++ b/mm/tests/ptval_hex_string_kunit.c
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <kunit/test.h>
+#include <linux/pgtable.h>
+
+struct ptval_str {
+ u64 val;
+ const char *str;
+};
+
+static struct ptval_str ptval_u32_tests[] = {
+ { .val = 0x1234abcd, .str = "1234abcd"},
+ { .val = 0x4455aabb, .str = "4455aabb"},
+ { .val = 0xccddeeff, .str = "ccddeeff"},
+ { .val = 0x12345678, .str = "12345678"},
+ { .val = 0x87654321, .str = "87654321"},
+ { .val = 0xaabbccdd, .str = "aabbccdd"},
+ { .val = 0xaa44cc66, .str = "aa44cc66"},
+ { .val = 0x11442266, .str = "11442266"},
+};
+
+static struct ptval_str ptval_u64_tests[] = {
+ { .val = 0x123456789abcdef0ULL, .str = "123456789abcdef0"},
+ { .val = 0x113355779abcdef0ULL, .str = "113355779abcdef0"},
+ { .val = 0xaabbccddeeffaabbULL, .str = "aabbccddeeffaabb"},
+ { .val = 0x1234567812345678ULL, .str = "1234567812345678"},
+ { .val = 0x0000000000000000ULL, .str = "0000000000000000"},
+ { .val = 0xffffffffffffffffULL, .str = "ffffffffffffffff"},
+ { .val = 0xaa00cc00ee00aa00ULL, .str = "aa00cc00ee00aa00"},
+ { .val = 0xc0ffeec0ffeedeadULL, .str = "c0ffeec0ffeedead"},
+};
+
+static void ptval_hex_str_u32(struct kunit *test)
+{
+ char buf[PTVAL_STR_MAX];
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(ptval_u32_tests); i++) {
+ u32 val = ptval_u32_tests[i].val;
+
+ ptval_bytes_to_hex_str(buf, sizeof(buf), &val, sizeof(val));
+ KUNIT_EXPECT_STREQ(test, buf, ptval_u32_tests[i].str);
+ }
+}
+
+static void ptval_hex_str_u64(struct kunit *test)
+{
+ char buf[PTVAL_STR_MAX];
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(ptval_u64_tests); i++) {
+ u64 val = ptval_u64_tests[i].val;
+
+ ptval_bytes_to_hex_str(buf, sizeof(buf), &val, sizeof(val));
+ KUNIT_EXPECT_STREQ(test, buf, ptval_u64_tests[i].str);
+ }
+}
+
+#ifdef __SIZEOF_INT128__
+struct ptval_128_str {
+ u128 val;
+ const char *str;
+};
+
+static struct ptval_128_str ptval_u128_tests[] = {
+ { .val = (u128)0x1122334455667788 << 64 |
+ (u128)0x123456789abcdef0, .str = "1122334455667788123456789abcdef0"},
+ { .val = (u128)0xaabbccddeeffaabb << 64 |
+ (u128)0xaabbccddeeffaabb, .str = "aabbccddeeffaabbaabbccddeeffaabb"},
+ { .val = (u128)0x0000000000000000 << 64 |
+ (u128)0x0000000000000000, .str = "00000000000000000000000000000000"},
+ { .val = (u128)0xffffffffffffffff << 64 |
+ (u128)0xffffffffffffffff, .str = "ffffffffffffffffffffffffffffffff"},
+ { .val = (u128)0x1234123412341234 << 64 |
+ (u128)0xabcdabcdabcdabcd, .str = "1234123412341234abcdabcdabcdabcd"},
+ { .val = (u128)0xc0ffeec0ffeedead << 64 |
+ (u128)0xc0ffeec0ffeedead, .str = "c0ffeec0ffeedeadc0ffeec0ffeedead"},
+ { .val = (u128)0xa5a5a5a5a55a5a5a << 64 |
+ (u128)0xa5a5a5a55a5a5a5a, .str = "a5a5a5a5a55a5a5aa5a5a5a5a55a5a5a"},
+ { .val = (u128)0x4455445544554455 << 64 |
+ (u128)0x2233223322332233, .str = "44554455445544552233223322332233"},
+};
+
+static void ptval_hex_str_u128(struct kunit *test)
+{
+ char buf[PTVAL_STR_MAX];
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(ptval_u128_tests); i++) {
+ u128 val = ptval_u128_tests[i].val;
+
+ ptval_bytes_to_hex_str(buf, sizeof(buf), &val, sizeof(val));
+ KUNIT_EXPECT_STREQ(test, buf, ptval_u128_tests[i].str);
+ }
+}
+#endif
+
+static struct kunit_case ptval_hex_str_tests[] = {
+ KUNIT_CASE(ptval_hex_str_u32),
+ KUNIT_CASE(ptval_hex_str_u64),
+#ifdef __SIZEOF_INT128__
+ KUNIT_CASE(ptval_hex_str_u128),
+#endif
+ {}
+};
+
+static struct kunit_suite ptval_hex_str_test_suite = {
+ .name = "ptval-hex-str",
+ .test_cases = ptval_hex_str_tests,
+};
+kunit_test_suite(ptval_hex_str_test_suite);
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [RFC] mm: Add KUNIT test for ptval_bytes_to_hex_str()
2026-09-29 12:05 [RFC] mm: Add KUNIT test for ptval_bytes_to_hex_str() Anshuman Khandual
@ 2026-10-01 7:45 ` David Hildenbrand (Arm)
2026-10-01 8:56 ` Anshuman Khandual
0 siblings, 1 reply; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-10-01 7:45 UTC (permalink / raw)
To: Anshuman Khandual, linux-mm; +Cc: Andrew Morton, Lorenzo Stoakes, linux-kernel
On 9/29/26 14:05, Anshuman Khandual wrote:
> Add KUnit tests for ptval_bytes_to_hex_str() to verify conversion of 32-bit
> 64-bit, and 128-bit (when supported) page table entry values into printable
> hexadecimal strings.
>
> Add a new PTVAL_HEX_STR_KUNIT_TEST option to enable the tests.
>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: David Hildenbrand <david@kernel.org>
> Cc: Lorenzo Stoakes <ljs@kernel.org>
> Cc: linux-kernel@vger.kernel.org
> Cc: linux-mm@kvack.org
> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
> ---
> This patch applies on v7.3-rc5 after the following series
>
> https://lore.kernel.org/linux-mm/20260831054331.625505-1-anshuman.khandual@arm.com/
>
> mm/Kconfig | 11 +++
> mm/Makefile | 1 +
> mm/tests/ptval_hex_string_kunit.c | 111 ++++++++++++++++++++++++++++++
> 3 files changed, 123 insertions(+)
> create mode 100644 mm/tests/ptval_hex_string_kunit.c
>
> diff --git a/mm/Kconfig b/mm/Kconfig
> index 604c58199acbf..f68bf26b98c83 100644
> --- a/mm/Kconfig
> +++ b/mm/Kconfig
> @@ -1511,6 +1511,17 @@ config LAZY_MMU_MODE_KUNIT_TEST
>
> If unsure, say N.
>
> +config PTVAL_HEX_STR_KUNIT_TEST
> + bool "KUnit tests for pgtable entry string conversion" if !KUNIT_ALL_TESTS
> + depends on KUNIT
> + default KUNIT_ALL_TESTS
> + help
> + Enable this option to check that ptval_bytes_to_hex_str() is able
> + convert given pgtable entry value in bytes into hex strings which
> + could be printed.
> +
> + If unsure, say N.
That's a bit fine grained :)
Could we somehow pack this under another existing / more generic test? We don't
want one new test for every new function :)
An alternative would be to use mm/debug_vm_pgtable.c to just run 3 tests at
runtime as well.
> +
> source "mm/damon/Kconfig"
>
> endmenu
> diff --git a/mm/Makefile b/mm/Makefile
> index e7245cb88c665..d08b8bd01917b 100644
> --- a/mm/Makefile
> +++ b/mm/Makefile
> @@ -147,4 +147,5 @@ obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
> obj-$(CONFIG_EXECMEM) += execmem.o
> obj-$(CONFIG_TMPFS_QUOTA) += shmem_quota.o
> obj-$(CONFIG_LAZY_MMU_MODE_KUNIT_TEST) += tests/lazy_mmu_mode_kunit.o
> +obj-$(CONFIG_PTVAL_HEX_STR_KUNIT_TEST) += tests/ptval_hex_string_kunit.o
> obj-$(CONFIG_MEM_ALLOC_PROFILING) += alloc_tag.o
> diff --git a/mm/tests/ptval_hex_string_kunit.c b/mm/tests/ptval_hex_string_kunit.c
> new file mode 100644
> index 0000000000000..45c203cfb49ae
> --- /dev/null
> +++ b/mm/tests/ptval_hex_string_kunit.c
> @@ -0,0 +1,111 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <kunit/test.h>
> +#include <linux/pgtable.h>
> +
> +struct ptval_str {
> + u64 val;
> + const char *str;
> +};
> +
> +static struct ptval_str ptval_u32_tests[] = {
> + { .val = 0x1234abcd, .str = "1234abcd"},
Isn't a single test like this enough. Why would we need the other ones?
So I think this could all get condensed quite a bit (including the test
functions, a single one should be sufficient if we really only end up with 3 tests).
--
Cheers,
David
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [RFC] mm: Add KUNIT test for ptval_bytes_to_hex_str()
2026-10-01 7:45 ` David Hildenbrand (Arm)
@ 2026-10-01 8:56 ` Anshuman Khandual
0 siblings, 0 replies; 3+ messages in thread
From: Anshuman Khandual @ 2026-10-01 8:56 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: linux-mm, Andrew Morton, Lorenzo Stoakes, linux-kernel
On Thu, Oct 01, 2026 at 09:45:01AM +0200, David Hildenbrand (Arm) wrote:
> On 9/29/26 14:05, Anshuman Khandual wrote:
> > Add KUnit tests for ptval_bytes_to_hex_str() to verify conversion of 32-bit
> > 64-bit, and 128-bit (when supported) page table entry values into printable
> > hexadecimal strings.
> >
> > Add a new PTVAL_HEX_STR_KUNIT_TEST option to enable the tests.
> >
> > Cc: Andrew Morton <akpm@linux-foundation.org>
> > Cc: David Hildenbrand <david@kernel.org>
> > Cc: Lorenzo Stoakes <ljs@kernel.org>
> > Cc: linux-kernel@vger.kernel.org
> > Cc: linux-mm@kvack.org
> > Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
> > ---
> > This patch applies on v7.3-rc5 after the following series
> >
> > https://lore.kernel.org/linux-mm/20260831054331.625505-1-anshuman.khandual@arm.com/
> >
> > mm/Kconfig | 11 +++
> > mm/Makefile | 1 +
> > mm/tests/ptval_hex_string_kunit.c | 111 ++++++++++++++++++++++++++++++
> > 3 files changed, 123 insertions(+)
> > create mode 100644 mm/tests/ptval_hex_string_kunit.c
> >
> > diff --git a/mm/Kconfig b/mm/Kconfig
> > index 604c58199acbf..f68bf26b98c83 100644
> > --- a/mm/Kconfig
> > +++ b/mm/Kconfig
> > @@ -1511,6 +1511,17 @@ config LAZY_MMU_MODE_KUNIT_TEST
> >
> > If unsure, say N.
> >
> > +config PTVAL_HEX_STR_KUNIT_TEST
> > + bool "KUnit tests for pgtable entry string conversion" if !KUNIT_ALL_TESTS
> > + depends on KUNIT
> > + default KUNIT_ALL_TESTS
> > + help
> > + Enable this option to check that ptval_bytes_to_hex_str() is able
> > + convert given pgtable entry value in bytes into hex strings which
> > + could be printed.
> > +
> > + If unsure, say N.
>
> That's a bit fine grained :)
>
> Could we somehow pack this under another existing / more generic test? We don't
> want one new test for every new function :)
Probably right. We don't need one test for each new function.
>
> An alternative would be to use mm/debug_vm_pgtable.c to just run 3 tests at
> runtime as well.
Yes - that could work as well given its printing pgtable entries but
those tests would not be KUNIT based any more I guess. Although just
like the existing VM_WARN_ON() check, new tests should be sufficient.
>
> > +
> > source "mm/damon/Kconfig"
> >
> > endmenu
> > diff --git a/mm/Makefile b/mm/Makefile
> > index e7245cb88c665..d08b8bd01917b 100644
> > --- a/mm/Makefile
> > +++ b/mm/Makefile
> > @@ -147,4 +147,5 @@ obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
> > obj-$(CONFIG_EXECMEM) += execmem.o
> > obj-$(CONFIG_TMPFS_QUOTA) += shmem_quota.o
> > obj-$(CONFIG_LAZY_MMU_MODE_KUNIT_TEST) += tests/lazy_mmu_mode_kunit.o
> > +obj-$(CONFIG_PTVAL_HEX_STR_KUNIT_TEST) += tests/ptval_hex_string_kunit.o
> > obj-$(CONFIG_MEM_ALLOC_PROFILING) += alloc_tag.o
> > diff --git a/mm/tests/ptval_hex_string_kunit.c b/mm/tests/ptval_hex_string_kunit.c
> > new file mode 100644
> > index 0000000000000..45c203cfb49ae
> > --- /dev/null
> > +++ b/mm/tests/ptval_hex_string_kunit.c
> > @@ -0,0 +1,111 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +#include <kunit/test.h>
> > +#include <linux/pgtable.h>
> > +
> > +struct ptval_str {
> > + u64 val;
> > + const char *str;
> > +};
> > +
> > +static struct ptval_str ptval_u32_tests[] = {
> > + { .val = 0x1234abcd, .str = "1234abcd"},
>
> Isn't a single test like this enough. Why would we need the other ones?
>
> So I think this could all get condensed quite a bit (including the test
> functions, a single one should be sufficient if we really only end up with 3 tests).
If we continue with KUNIT framework, probably all three tests are still
required to distingish them but in mm/debug_vm_pgatble.c based approach
a single test can combine all these tests via VM_WARN_ON().
>
> --
> Cheers,
>
> David
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-10-01 8:57 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-29 12:05 [RFC] mm: Add KUNIT test for ptval_bytes_to_hex_str() Anshuman Khandual
2026-10-01 7:45 ` David Hildenbrand (Arm)
2026-10-01 8:56 ` Anshuman Khandual
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®