On 5/18/26 4:49 PM, Ruidong Tian wrote:
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. The copy_mc_page() case additionally checks the return
value is 0 on clean memory and is gated on CONFIG_ARCH_HAS_COPY_MC.

Signed-off-by: Ruidong Tian <[email protected]>
---
  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..85df53ccfb0c 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_zero[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]);
+
+       /* Explicitly zero the entire destination. */
+       memset(page_dst, 0, ARRAY_SIZE(page_dst));

   init_page() memsets the entire two-page page_dst to zero, and the
   test then asserts the second page is still zero. A buggy copy_page()
   that overwrites the second page with bytes that happen to include a
   zero byte would still pass.

   Please fill page_dst with a non-zero sentinel (e.g. 0xA5) instead
   of zero, and check the second half against that sentinel using
   memchr_inv():

       memset(page_dst, 0xA5, ARRAY_SIZE(page_dst));
       ...
       KUNIT_ASSERT_TRUE_MSG(test,
           !memchr_inv(page_dst + PAGE_SIZE, 0xA5, PAGE_SIZE),
           "copy_page overflow into adjacent page");

   This also lets you delete page_zero[] entirely, which on
   PAGE_SIZE=64K configs (arm64) saves a non-trivial amount of BSS.


+}
+
+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_zero, PAGE_SIZE), 0,
+               "copy_page overflow into adjacent page");
+}
+
+#ifdef CONFIG_ARCH_HAS_COPY_MC
+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);

From sashiko:

Does this introduce a build regression on architectures like x86_64 or
powerpc?
The CONFIG_ARCH_HAS_COPY_MC flag is selected by multiple architectures,
but copy_mc_page() is an arm64-specific API. Other architectures use the
generic copy_mc_to_kernel() API instead.
If the kernel is built with KUnit enabled on non-arm64 architectures, it
appears copy_mc_page() would be an undeclared identifier.


Thanks.
Shuai

Reply via email to