Some MM tests use memalign() to allocate memory. However, the resulting
memory area may unexpectedly merge with an adjacent VMA, causing tests
that inspect it through /proc/self/smaps to fail.

Eliminate this potential source of test flakiness by introducing
alloc_isolated_mem(), which places guard regions before and after the
allocated area to prevent unexpected VMA merging.

Replace memalign() with this helper and use the helper in
softdirty_supported() instead of mmap().

Link: https://lore.kernel.org/all/apHDdRSr59mva3ey@lucifer/ [0]
Suggested-by: Lorenzo Stoakes (ARM) <[email protected]>
Signed-off-by: Yeoreum Yun <[email protected]>
---
 tools/testing/selftests/mm/pagemap_ioctl.c        | 18 +++---
 tools/testing/selftests/mm/soft-dirty.c           |  6 +-
 tools/testing/selftests/mm/split_huge_page_test.c |  8 +--
 tools/testing/selftests/mm/vm_util.c              | 67 +++++++++++++++++++++--
 tools/testing/selftests/mm/vm_util.h              |  2 +
 5 files changed, 81 insertions(+), 20 deletions(-)

diff --git a/tools/testing/selftests/mm/pagemap_ioctl.c 
b/tools/testing/selftests/mm/pagemap_ioctl.c
index eadc7159ca5b..7ed5b8fb3b25 100644
--- a/tools/testing/selftests/mm/pagemap_ioctl.c
+++ b/tools/testing/selftests/mm/pagemap_ioctl.c
@@ -792,9 +792,9 @@ void *gethugepage(int map_size)
        int ret;
        char *map;
 
-       map = memalign(hpage_size, map_size);
+       map = alloc_isolated_mem(hpage_size, map_size);
        if (!map)
-               ksft_exit_fail_msg("memalign failed %d %s\n", errno, 
strerror(errno));
+               ksft_exit_fail_msg("alloc_isolated_mem failed %d %s\n", errno, 
strerror(errno));
 
        ret = madvise(map, map_size, MADV_HUGEPAGE);
        if (ret)
@@ -856,7 +856,7 @@ int hpage_unit_tests(void)
 
                /* 4. only middle page written */
                wp_free(map, map_size);
-               free(map);
+               free_isolated_mem(map, map_size);
                map = gethugepage(map_size);
                wp_init(map, map_size);
                wp_addr_range(map, map_size);
@@ -871,7 +871,7 @@ int hpage_unit_tests(void)
                                 "%s only middle page written\n", __func__);
 
                wp_free(map, map_size);
-               free(map);
+               free_isolated_mem(map, map_size);
        } else {
                ksft_test_result_skip("%s all new huge page must be written\n", 
__func__);
                ksft_test_result_skip("%s all the huge page must not be 
written\n", __func__);
@@ -898,7 +898,7 @@ int hpage_unit_tests(void)
                                 vec[0].start == (uintptr_t)(map + map_size/2),
                                 "%s clear first half of huge page\n", 
__func__);
                wp_free(map, map_size);
-               free(map);
+               free_isolated_mem(map, map_size);
        } else {
                ksft_test_result_skip("%s clear first half of huge page\n", 
__func__);
        }
@@ -927,7 +927,7 @@ int hpage_unit_tests(void)
                                 "%s clear first half of huge page with limited 
buffer\n",
                                 __func__);
                wp_free(map, map_size);
-               free(map);
+               free_isolated_mem(map, map_size);
        } else {
                ksft_test_result_skip("%s clear first half of huge page with 
limited buffer\n",
                                      __func__);
@@ -955,7 +955,7 @@ int hpage_unit_tests(void)
                ksft_test_result(ret == 1 && LEN(vec[0]) == vec_size/2,
                                 "%s clear second half huge page\n", __func__);
                wp_free(map, map_size);
-               free(map);
+               free_isolated_mem(map, map_size);
        } else {
                ksft_test_result_skip("%s clear second half huge page\n", 
__func__);
        }
@@ -988,7 +988,7 @@ int hpage_unit_tests(void)
                                 "%s get half huge page\n", __func__);
 
                wp_free(map, map_size);
-               free(map);
+               free_isolated_mem(map, map_size);
        } else {
                ksft_test_result_skip("%s get half huge page\n", __func__);
                ksft_test_result_skip("%s get half huge page\n", __func__);
@@ -1702,7 +1702,7 @@ int main(int __attribute__((unused)) argc, char *argv[])
                wp_addr_range(map, hpage_size);
                base_tests("Huge page testing:", map, hpage_size, 0);
                wp_free(map, hpage_size);
-               free(map);
+               free_isolated_mem(map, hpage_size);
        } else {
                base_tests("Huge page testing:", NULL, 0, 1);
        }
diff --git a/tools/testing/selftests/mm/soft-dirty.c 
b/tools/testing/selftests/mm/soft-dirty.c
index 5f278913c4d7..b6fad38c8bee 100644
--- a/tools/testing/selftests/mm/soft-dirty.c
+++ b/tools/testing/selftests/mm/soft-dirty.c
@@ -92,9 +92,9 @@ static void test_hugepage(int pagemap_fd, int pagesize)
        if (!hpage_len)
                ksft_exit_fail_msg("Reading PMD pagesize failed");
 
-       map = memalign(hpage_len, hpage_len);
+       map = alloc_isolated_mem(hpage_len, hpage_len);
        if (!map)
-               ksft_exit_fail_msg("memalign failed\n");
+               ksft_exit_fail_msg("alloc_isolated_mem failed\n");
 
        ret = madvise(map, hpage_len, MADV_HUGEPAGE);
        if (ret)
@@ -130,7 +130,7 @@ static void test_hugepage(int pagemap_fd, int pagesize)
                ksft_test_result_skip("Test %s huge page allocation\n", 
__func__);
                ksft_test_result_skip("Test %s huge page dirty bit\n", 
__func__);
        }
-       free(map);
+       free_isolated_mem(map, hpage_len);
 }
 
 static void test_mprotect(int pagemap_fd, int pagesize, bool anon)
diff --git a/tools/testing/selftests/mm/split_huge_page_test.c 
b/tools/testing/selftests/mm/split_huge_page_test.c
index 4bbad892bc57..7a600131f7f0 100644
--- a/tools/testing/selftests/mm/split_huge_page_test.c
+++ b/tools/testing/selftests/mm/split_huge_page_test.c
@@ -154,7 +154,7 @@ static char *allocate_zero_filled_hugepage(size_t len)
        char *result;
        size_t i;
 
-       result = memalign(pmd_pagesize, len);
+       result = alloc_isolated_mem(pmd_pagesize, len);
        if (!result) {
                printf("Fail to allocate memory\n");
                exit(EXIT_FAILURE);
@@ -217,7 +217,7 @@ static void split_pmd_zero_pages(void)
        one_page = allocate_zero_filled_hugepage(len);
        verify_rss_anon_split_huge_page_all_zeroes(one_page, nr_hpages, len);
        ksft_test_result_pass("Split zero filled huge pages successful\n");
-       free(one_page);
+       free_isolated_mem(one_page, len);
 }
 
 static void split_pmd_thp_to_order(int order)
@@ -226,7 +226,7 @@ static void split_pmd_thp_to_order(int order)
        size_t len = 4 * pmd_pagesize;
        size_t i;
 
-       one_page = memalign(pmd_pagesize, len);
+       one_page = alloc_isolated_mem(pmd_pagesize, len);
        if (!one_page)
                ksft_exit_fail_msg("Fail to allocate memory: %s\n", 
strerror(errno));
 
@@ -261,7 +261,7 @@ static void split_pmd_thp_to_order(int order)
                ksft_exit_fail_msg("Still AnonHugePages not split\n");
 
        ksft_test_result_pass("Split huge pages to order %d successful\n", 
order);
-       free(one_page);
+       free_isolated_mem(one_page, len);
 }
 
 static void split_pte_mapped_thp(void)
diff --git a/tools/testing/selftests/mm/vm_util.c 
b/tools/testing/selftests/mm/vm_util.c
index dc62ce84e143..159d627ae3b0 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -19,6 +19,11 @@
 #define KPAGEFLAGS_PATH "/proc/kpageflags"
 #define MAX_NR_ORDERS 20
 
+#define ALIGN(x, a)            (((x) + (a - 1)) & (~((a) - 1)))
+#define ALIGN_PTR(p, a)                ((typeof(p))ALIGN((unsigned long)(p), 
a))
+#define IS_ALIGNED(x, a)       (((x) & ((typeof(x))(a) - 1)) == 0)
+#define PTR_ALIGNED(p, a)      (IS_ALIGNED((unsigned long)(p), a))
+
 unsigned int __page_size;
 unsigned int __page_shift;
 
@@ -514,6 +519,61 @@ int64_t allocate_transhuge(void *ptr, int pagemap_fd)
        return -1;
 }
 
+void *alloc_isolated_mem(size_t align, size_t size)
+{
+       char *ptr, *guard, *addr_align;
+       size_t map_size, guard_size, page_size = psize();
+       size_t padding;
+
+       if (__builtin_popcountll((unsigned long)align) > 1)
+               return NULL;
+
+       align = (align > page_size) ? align : page_size;
+       guard_size = page_size;
+       size = ALIGN(size, page_size);
+       map_size = size + align + guard_size;
+
+       ptr = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
+                  MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+       if (ptr == MAP_FAILED)
+               return NULL;
+
+       addr_align = PTR_ALIGNED(ptr, align) ? ptr + align : ALIGN_PTR(ptr, 
align);
+       padding = addr_align - guard_size - ptr;
+       if (padding) {
+               munmap(ptr, padding);
+               map_size -= padding;
+               ptr += padding;
+       }
+
+       guard = ptr;
+       if (mprotect(guard, guard_size, PROT_NONE))
+               goto err_out;
+
+       guard = addr_align + size;
+       if (mprotect(guard, guard_size, PROT_NONE))
+               goto err_out;
+
+       padding = (ptr + map_size) - (guard + guard_size);
+       if (padding)
+               munmap(guard + guard_size, padding);
+
+       return addr_align;
+
+err_out:
+       munmap(ptr, map_size);
+       return NULL;
+}
+
+void free_isolated_mem(void *addr, size_t size)
+{
+       int guard_size, page_size = psize();
+
+       guard_size = page_size;
+       munmap((char *)addr - guard_size,
+              guard_size * 2 + ALIGN(size, page_size));
+}
+
 int pageflags_get(unsigned long pfn, int kpageflags_fd, uint64_t *flags)
 {
        size_t count;
@@ -618,13 +678,12 @@ bool softdirty_supported(void)
        const size_t pagesize = getpagesize();
 
        /* New mappings are expected to be marked with VM_SOFTDIRTY (sd). */
-       addr = mmap(0, pagesize, PROT_READ | PROT_WRITE,
-                   MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
-       if (addr == MAP_FAILED)
+       addr = alloc_isolated_mem(pagesize, pagesize);
+       if (!addr)
                ksft_exit_fail_msg("mmap failed\n");
 
        supported = check_vmflag(addr, "sd");
-       munmap(addr, pagesize);
+       free_isolated_mem(addr, pagesize);
        return supported;
 }
 
diff --git a/tools/testing/selftests/mm/vm_util.h 
b/tools/testing/selftests/mm/vm_util.h
index f19bd17817e5..ce1da9a55310 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -95,6 +95,8 @@ bool check_huge_anon(void *addr, size_t len, int nr_hpages, 
uint64_t hpage_size)
 bool check_huge_file(void *addr, size_t len, int nr_hpages, uint64_t 
hpage_size);
 bool check_huge_shmem(void *addr, size_t len, int nr_hpages, uint64_t 
hpage_size);
 int64_t allocate_transhuge(void *ptr, int pagemap_fd);
+void *alloc_isolated_mem(size_t align, size_t size);
+void free_isolated_mem(void *addr, size_t size);
 int pageflags_get(unsigned long pfn, int kpageflags_fd, uint64_t *flags);
 int gather_folio_orders(char *vaddr_start, size_t len,
                int pagemap_fd, int kpageflags_fd, int orders[], int nr_orders);

-- 
2.43.0


Reply via email to