Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests/mm: add PAGEMAP_SCAN guard region test

Add a selftest to verify the PAGEMAP_SCAN ioctl correctly reports guard
regions using the newly introduced PAGE_IS_GUARD flag.

Link: https://lkml.kernel.org/r/20250324065328.107678-4-avagin@google.com
Signed-off-by: Andrei Vagin <avagin@gmail.com>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Andrei Vagin and committed by
Andrew Morton
a9562fd0 267bee0c

+57
+57
tools/testing/selftests/mm/guard-regions.c
··· 8 8 #include <fcntl.h> 9 9 #include <linux/limits.h> 10 10 #include <linux/userfaultfd.h> 11 + #include <linux/fs.h> 11 12 #include <setjmp.h> 12 13 #include <signal.h> 13 14 #include <stdbool.h> ··· 2070 2069 unsigned long masked = entry & PM_GUARD_REGION; 2071 2070 2072 2071 ASSERT_EQ(masked, i % 2 == 0 ? PM_GUARD_REGION : 0); 2072 + } 2073 + 2074 + ASSERT_EQ(close(proc_fd), 0); 2075 + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); 2076 + } 2077 + 2078 + /* 2079 + * Assert that PAGEMAP_SCAN correctly reports guard region ranges. 2080 + */ 2081 + TEST_F(guard_regions, pagemap_scan) 2082 + { 2083 + const unsigned long page_size = self->page_size; 2084 + struct page_region pm_regs[10]; 2085 + struct pm_scan_arg pm_scan_args = { 2086 + .size = sizeof(struct pm_scan_arg), 2087 + .category_anyof_mask = PAGE_IS_GUARD, 2088 + .return_mask = PAGE_IS_GUARD, 2089 + .vec = (long)&pm_regs, 2090 + .vec_len = ARRAY_SIZE(pm_regs), 2091 + }; 2092 + int proc_fd, i; 2093 + char *ptr; 2094 + 2095 + proc_fd = open("/proc/self/pagemap", O_RDONLY); 2096 + ASSERT_NE(proc_fd, -1); 2097 + 2098 + ptr = mmap_(self, variant, NULL, 10 * page_size, 2099 + PROT_READ | PROT_WRITE, 0, 0); 2100 + ASSERT_NE(ptr, MAP_FAILED); 2101 + 2102 + pm_scan_args.start = (long)ptr; 2103 + pm_scan_args.end = (long)ptr + 10 * page_size; 2104 + ASSERT_EQ(ioctl(proc_fd, PAGEMAP_SCAN, &pm_scan_args), 0); 2105 + ASSERT_EQ(pm_scan_args.walk_end, (long)ptr + 10 * page_size); 2106 + 2107 + /* Install a guard region in every other page. */ 2108 + for (i = 0; i < 10; i += 2) { 2109 + char *ptr_p = &ptr[i * page_size]; 2110 + 2111 + ASSERT_EQ(syscall(__NR_madvise, ptr_p, page_size, MADV_GUARD_INSTALL), 0); 2112 + } 2113 + 2114 + /* 2115 + * Assert ioctl() returns the count of located regions, where each 2116 + * region spans every other page within the range of 10 pages. 2117 + */ 2118 + ASSERT_EQ(ioctl(proc_fd, PAGEMAP_SCAN, &pm_scan_args), 5); 2119 + ASSERT_EQ(pm_scan_args.walk_end, (long)ptr + 10 * page_size); 2120 + 2121 + /* Re-read from pagemap, and assert guard regions are detected. */ 2122 + for (i = 0; i < 5; i++) { 2123 + long ptr_p = (long)&ptr[2 * i * page_size]; 2124 + 2125 + ASSERT_EQ(pm_regs[i].start, ptr_p); 2126 + ASSERT_EQ(pm_regs[i].end, ptr_p + page_size); 2127 + ASSERT_EQ(pm_regs[i].categories, PAGE_IS_GUARD); 2073 2128 } 2074 2129 2075 2130 ASSERT_EQ(close(proc_fd), 0);