]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/mm: introduce helper to read every page
authorKevin Brodsky <kevin.brodsky@arm.com>
Thu, 22 Jan 2026 17:02:21 +0000 (17:02 +0000)
committerAndrew Morton <akpm@linux-foundation.org>
Sat, 31 Jan 2026 22:22:41 +0000 (14:22 -0800)
FORCE_READ(*addr) ensures that the compiler will emit a load from addr.
Several tests need to trigger such a load for a range of pages, ensuring
that every page is faulted in, if it wasn't already.

Introduce a new helper force_read_pages() that does exactly that and
replace existing loops with a call to it.

The step size (regular/huge page size) is preserved for all loops, except
in split_huge_page_test.  Reading every byte is unnecessary; we now read
every huge page, matching the following call to check_huge_file().

Link: https://lkml.kernel.org/r/20260122170224.4056513-7-kevin.brodsky@arm.com
Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
Reviewed-by: Dev Jain <dev.jain@arm.com>
Reviewed-by: Muhammad Usama Anjum <usama.anjum@arm.com>
Acked-by: David Hildenbrand (Red Hat) <david@kernel.org>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Mark Brown <broonie@kernel.org>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: SeongJae Park <sj@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: wang lian <lianux.mm@gmail.com>
Cc: Yunsheng Lin <linyunsheng@huawei.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
tools/testing/selftests/mm/hugetlb-madvise.c
tools/testing/selftests/mm/pfnmap.c
tools/testing/selftests/mm/split_huge_page_test.c
tools/testing/selftests/mm/vm_util.h

index 05d9d2805ae4900f4cedbe36f5975b3ccd124abb..5b12041fa31047c4b792b7b5333fea6d029877d4 100644 (file)
@@ -47,14 +47,7 @@ void write_fault_pages(void *addr, unsigned long nr_pages)
 
 void read_fault_pages(void *addr, unsigned long nr_pages)
 {
-       unsigned long i;
-
-       for (i = 0; i < nr_pages; i++) {
-               unsigned long *addr2 =
-                       ((unsigned long *)(addr + (i * huge_page_size)));
-               /* Prevent the compiler from optimizing out the entire loop: */
-               FORCE_READ(*addr2);
-       }
+       force_read_pages(addr, nr_pages, huge_page_size);
 }
 
 int main(int argc, char **argv)
index f546dfb10caeb68bf688be42129f3d5123d63cd5..45b5f1cf60194931dd6af349d1dab4b7fb302fd0 100644 (file)
@@ -35,18 +35,15 @@ static void signal_handler(int sig)
 
 static int test_read_access(char *addr, size_t size, size_t pagesize)
 {
-       size_t offs;
        int ret;
 
        if (signal(SIGSEGV, signal_handler) == SIG_ERR)
                return -EINVAL;
 
        ret = sigsetjmp(sigjmp_buf_env, 1);
-       if (!ret) {
-               for (offs = 0; offs < size; offs += pagesize)
-                       /* Force a read that the compiler cannot optimize out. */
-                       *((volatile char *)(addr + offs));
-       }
+       if (!ret)
+               force_read_pages(addr, size/pagesize, pagesize);
+
        if (signal(SIGSEGV, SIG_DFL) == SIG_ERR)
                return -EINVAL;
 
index 40799f3f02131cf2d36d3f99906a322a7e18b1c1..e0167111bdd153bc2cd6e6f066e2a50ad1be5491 100644 (file)
@@ -652,11 +652,7 @@ static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size,
        }
        madvise(*addr, fd_size, MADV_HUGEPAGE);
 
-       for (size_t i = 0; i < fd_size; i++) {
-               char *addr2 = *addr + i;
-
-               FORCE_READ(*addr2);
-       }
+       force_read_pages(*addr, fd_size / pmd_pagesize, pmd_pagesize);
 
        if (!check_huge_file(*addr, fd_size / pmd_pagesize, pmd_pagesize)) {
                ksft_print_msg("No large pagecache folio generated, please provide a filesystem supporting large folio\n");
index 6ad32b1830f1f9a70bbbd7d6c136d04080923d34..522f7f9050f50b830218a06bd952530bf190c9c9 100644 (file)
@@ -54,6 +54,13 @@ static inline unsigned int pshift(void)
        return __page_shift;
 }
 
+static inline void force_read_pages(char *addr, unsigned int nr_pages,
+                                   size_t pagesize)
+{
+       for (unsigned int i = 0; i < nr_pages; i++)
+               FORCE_READ(addr[i * pagesize]);
+}
+
 bool detect_huge_zeropage(void);
 
 /*