selftests/mm: reuse read_pmd_pagesize() in COW selftest
authorDavid Hildenbrand <david@redhat.com>
Tue, 11 Apr 2023 14:25:07 +0000 (16:25 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Tue, 18 Apr 2023 23:30:00 +0000 (16:30 -0700)
Patch series "mm: (pte|pmd)_mkdirty() should not unconditionally allow for
write access".

This is the follow-up on [1], adding selftests (testing for known issues
we added workarounds for and other issues that haven't been fixed yet),
fixing sparc64, reverting the workarounds, and perform one cleanup.

The patch from [1] was modified slightly (updated/extended patch
description, dropped one unnecessary NOP instruction from the ASM in
__pte_mkhwwrite()).

Retested on x86_64 and sparc64 (sun4u in QEMU).

I scanned most architectures to make sure their (pte|pmd)_mkdirty()
handling is correct.  To be sure, we can run the selftests and find out if
other architectures are still affectes (loongarch was fixed recently as
well).

Based on master for now. I don't expect surprises regarding mm-tress, but
I can rebase if there are any problems.

This patch (of 6):

The COW selftest can deal with THP not being configured.  So move error
handling of read_pmd_pagesize() into the callers such that we can reuse it
in the COW selftest.

Link: https://lkml.kernel.org/r/20230411142512.438404-1-david@redhat.com
Link: https://lkml.kernel.org/r/20221212130213.136267-1-david@redhat.com
Link: https://lkml.kernel.org/r/20230411142512.438404-2-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Cc: Anshuman Khandual <anshuman.khandual@arm.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Hugh Dickins <hughd@google.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Sam Ravnborg <sam@ravnborg.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Yu Zhao <yuzhao@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
tools/testing/selftests/mm/cow.c
tools/testing/selftests/mm/khugepaged.c
tools/testing/selftests/mm/soft-dirty.c
tools/testing/selftests/mm/split_huge_page_test.c
tools/testing/selftests/mm/vm_util.c

index 0eb2e81..dc9d6fe 100644 (file)
@@ -45,34 +45,6 @@ static size_t hugetlbsizes[10];
 static int gup_fd;
 static bool has_huge_zeropage;
 
-static void detect_thpsize(void)
-{
-       int fd = open("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size",
-                     O_RDONLY);
-       size_t size = 0;
-       char buf[15];
-       int ret;
-
-       if (fd < 0)
-               return;
-
-       ret = pread(fd, buf, sizeof(buf), 0);
-       if (ret > 0 && ret < sizeof(buf)) {
-               buf[ret] = 0;
-
-               size = strtoul(buf, NULL, 10);
-               if (size < pagesize)
-                       size = 0;
-               if (size > 0) {
-                       thpsize = size;
-                       ksft_print_msg("[INFO] detected THP size: %zu KiB\n",
-                                      thpsize / 1024);
-               }
-       }
-
-       close(fd);
-}
-
 static void detect_huge_zeropage(void)
 {
        int fd = open("/sys/kernel/mm/transparent_hugepage/use_zero_page",
@@ -1741,7 +1713,10 @@ int main(int argc, char **argv)
        int err;
 
        pagesize = getpagesize();
-       detect_thpsize();
+       thpsize = read_pmd_pagesize();
+       if (thpsize)
+               ksft_print_msg("[INFO] detected THP size: %zu KiB\n",
+                              thpsize / 1024);
        detect_hugetlbsizes();
        detect_huge_zeropage();
 
index 64126c8..97adc0f 100644 (file)
@@ -1476,6 +1476,10 @@ int main(int argc, const char **argv)
 
        page_size = getpagesize();
        hpage_pmd_size = read_pmd_pagesize();
+       if (!hpage_pmd_size) {
+               printf("Reading PMD pagesize failed");
+               exit(EXIT_FAILURE);
+       }
        hpage_pmd_nr = hpage_pmd_size / page_size;
 
        default_settings.khugepaged.max_ptes_none = hpage_pmd_nr - 1;
index 21d8830..cc5f144 100644 (file)
@@ -80,6 +80,9 @@ static void test_hugepage(int pagemap_fd, int pagesize)
        int i, ret;
        size_t hpage_len = read_pmd_pagesize();
 
+       if (!hpage_len)
+               ksft_exit_fail_msg("Reading PMD pagesize failed");
+
        map = memalign(hpage_len, hpage_len);
        if (!map)
                ksft_exit_fail_msg("memalign failed\n");
index b8558c7..0e74635 100644 (file)
@@ -300,6 +300,10 @@ int main(int argc, char **argv)
        pagesize = getpagesize();
        pageshift = ffs(pagesize) - 1;
        pmd_pagesize = read_pmd_pagesize();
+       if (!pmd_pagesize) {
+               printf("Reading PMD pagesize failed\n");
+               exit(EXIT_FAILURE);
+       }
 
        split_pmd_thp();
        split_pte_mapped_thp();
index 40e7956..8dc74dd 100644 (file)
@@ -84,12 +84,12 @@ uint64_t read_pmd_pagesize(void)
 
        fd = open(PMD_SIZE_FILE_PATH, O_RDONLY);
        if (fd == -1)
-               ksft_exit_fail_msg("Open hpage_pmd_size failed\n");
+               return 0;
 
        num_read = read(fd, buf, 19);
        if (num_read < 1) {
                close(fd);
-               ksft_exit_fail_msg("Read hpage_pmd_size failed\n");
+               return 0;
        }
        buf[num_read] = '\0';
        close(fd);