[PATCH v15 8/9] lib/test: memcpy_kunit: add copy_page() and copy_mc_page() tests
From: Ruidong Tian <hidden>
Date: 2026-06-18 09:22:40
Also in:
linux-arm-kernel, linux-mm, lkml
Subsystem:
fortify_source, library code, the rest · Maintainers:
Kees Cook, Andrew Morton, Linus Torvalds
Add KUnit tests for copy_page() and copy_mc_page(), modeled after the existing memcpy_test() style: a static page-aligned src and a two-page dst, filled with random bytes plus non-zero edges, then verify byte-for-byte equality and that the adjacent page is untouched. Signed-off-by: Ruidong Tian <redacted> --- lib/tests/memcpy_kunit.c | 67 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-)
diff --git a/lib/tests/memcpy_kunit.c b/lib/tests/memcpy_kunit.c
index d36933554e46..812c1fa20fd9 100644
--- a/lib/tests/memcpy_kunit.c
+++ b/lib/tests/memcpy_kunit.c@@ -493,6 +493,67 @@ static void memmove_overlap_test(struct kunit *test) } } +/* --- Page-sized copy tests --- */ + +static u8 page_src[PAGE_SIZE] __aligned(PAGE_SIZE); +static u8 page_dst[PAGE_SIZE * 2] __aligned(PAGE_SIZE); +static const u8 page_pattern[PAGE_SIZE] __aligned(PAGE_SIZE); + +static void init_page(struct kunit *test) +{ + /* Get many bit patterns. */ + get_random_bytes(page_src, PAGE_SIZE); + + /* Make sure we have non-zero edges. */ + set_random_nonzero(test, &page_src[0]); + set_random_nonzero(test, &page_src[PAGE_SIZE - 1]); + + memset(page_dst, 0xA5, ARRAY_SIZE(page_dst)); + memset(page_pattern, 0xA5, PAGE_SIZE); +} + +static void copy_page_test(struct kunit *test) +{ + init_page(test); + + /* Copy. */ + copy_page(page_dst, page_src); + + /* Verify byte-for-byte exact. */ + KUNIT_ASSERT_EQ_MSG(test, + memcmp(page_dst, page_src, PAGE_SIZE), 0, + "copy_page content mismatch with random data"); + + /* Verify no overflow into second page. */ + KUNIT_ASSERT_EQ_MSG(test, + memcmp(page_dst + PAGE_SIZE, page_pattern, PAGE_SIZE), 0, + "copy_page overflow into adjacent page"); +} + +#ifdef __HAVE_ARCH_COPY_MC_PAGE +static void copy_mc_page_test(struct kunit *test) +{ + int ret; + + init_page(test); + + /* Copy and check return value. */ + ret = copy_mc_page(page_dst, page_src); + KUNIT_ASSERT_EQ_MSG(test, ret, 0, + "copy_mc_page returned %d on clean memory", ret); + + /* Verify byte-for-byte exact. */ + KUNIT_ASSERT_EQ_MSG(test, + memcmp(page_dst, page_src, PAGE_SIZE), 0, + "copy_mc_page content mismatch with random data"); + + /* Verify no overflow into second page. */ + KUNIT_ASSERT_EQ_MSG(test, + memcmp(page_dst + PAGE_SIZE, page_pattern, PAGE_SIZE), 0, + "copy_mc_page overflow into adjacent page"); +} +#endif /* __HAVE_ARCH_COPY_MC_PAGE */ + static struct kunit_case memcpy_test_cases[] = { KUNIT_CASE(memset_test), KUNIT_CASE(memcpy_test),
@@ -500,6 +561,10 @@ static struct kunit_case memcpy_test_cases[] = { KUNIT_CASE_SLOW(memmove_test), KUNIT_CASE_SLOW(memmove_large_test), KUNIT_CASE_SLOW(memmove_overlap_test), + KUNIT_CASE(copy_page_test), +#ifdef __HAVE_ARCH_COPY_MC_PAGE + KUNIT_CASE(copy_mc_page_test), +#endif {} };
@@ -510,5 +575,5 @@ static struct kunit_suite memcpy_test_suite = { kunit_test_suite(memcpy_test_suite); -MODULE_DESCRIPTION("test cases for memcpy(), memmove(), and memset()"); +MODULE_DESCRIPTION("test cases for memcpy(), memmove(), memset() and copy_page()"); MODULE_LICENSE("GPL");
--
2.39.3