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

tools/testing/selftests/mm: add smaps visibility guard region test

Assert that we observe guard regions appearing in /proc/$pid/smaps as
expected, and when split/merge is performed too (with expected sticky
behaviour).

Also add handling for file systems which don't sanely handle mmap() VMA
merging so we don't incorrectly encounter a test failure in this
situation.

Link: https://lkml.kernel.org/r/059e62b8c67e55e6d849878206a95ea1d7c1e885.1763460113.git.lorenzo.stoakes@oracle.com
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Andrei Vagin <avagin@gmail.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Barry Song <baohua@kernel.org>
Cc: David Hildenbrand (Red Hat) <david@kernel.org>
Cc: Dev Jain <dev.jain@arm.com>
Cc: Jann Horn <jannh@google.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Lance Yang <lance.yang@linux.dev>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Nico Pache <npache@redhat.com>
Cc: Pedro Falcato <pfalcato@suse.de>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Zi Yan <ziy@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Lorenzo Stoakes and committed by
Andrew Morton
c0ae966f 89330ec8

+126
+120
tools/testing/selftests/mm/guard-regions.c
··· 94 94 case ANON_BACKED: 95 95 flags |= MAP_PRIVATE | MAP_ANON; 96 96 fd = -1; 97 + offset = 0; 97 98 break; 98 99 case SHMEM_BACKED: 99 100 case LOCAL_FILE_BACKED: ··· 257 256 if (buf[i] != chr) 258 257 return false; 259 258 } 259 + 260 + return true; 261 + } 262 + 263 + /* 264 + * Some file systems have issues with merging due to changing merge-sensitive 265 + * parameters in the .mmap callback, and prior to .mmap_prepare being 266 + * implemented everywhere this will now result in an unexpected failure to 267 + * merge (e.g. - overlayfs). 268 + * 269 + * Perform a simple test to see if the local file system suffers from this, if 270 + * it does then we can skip test logic that assumes local file system merging is 271 + * sane. 272 + */ 273 + static bool local_fs_has_sane_mmap(FIXTURE_DATA(guard_regions) * self, 274 + const FIXTURE_VARIANT(guard_regions) * variant) 275 + { 276 + const unsigned long page_size = self->page_size; 277 + char *ptr, *ptr2; 278 + struct procmap_fd procmap; 279 + 280 + if (variant->backing != LOCAL_FILE_BACKED) 281 + return true; 282 + 283 + /* Map 10 pages. */ 284 + ptr = mmap_(self, variant, NULL, 10 * page_size, PROT_READ | PROT_WRITE, 0, 0); 285 + if (ptr == MAP_FAILED) 286 + return false; 287 + /* Unmap the middle. */ 288 + munmap(&ptr[5 * page_size], page_size); 289 + 290 + /* Map again. */ 291 + ptr2 = mmap_(self, variant, &ptr[5 * page_size], page_size, PROT_READ | PROT_WRITE, 292 + MAP_FIXED, 5 * page_size); 293 + 294 + if (ptr2 == MAP_FAILED) 295 + return false; 296 + 297 + /* Now make sure they all merged. */ 298 + if (open_self_procmap(&procmap) != 0) 299 + return false; 300 + if (!find_vma_procmap(&procmap, ptr)) 301 + return false; 302 + if (procmap.query.vma_start != (unsigned long)ptr) 303 + return false; 304 + if (procmap.query.vma_end != (unsigned long)ptr + 10 * page_size) 305 + return false; 306 + close_procmap(&procmap); 260 307 261 308 return true; 262 309 } ··· 2250 2201 /* Accesses should still fail. */ 2251 2202 ASSERT_FALSE(try_read_buf(ptr_page)); 2252 2203 } 2204 + } 2205 + 2206 + TEST_F(guard_regions, smaps) 2207 + { 2208 + const unsigned long page_size = self->page_size; 2209 + struct procmap_fd procmap; 2210 + char *ptr, *ptr2; 2211 + int i; 2212 + 2213 + /* Map a region. */ 2214 + ptr = mmap_(self, variant, NULL, 10 * page_size, PROT_READ | PROT_WRITE, 0, 0); 2215 + ASSERT_NE(ptr, MAP_FAILED); 2216 + 2217 + /* We shouldn't yet see a guard flag. */ 2218 + ASSERT_FALSE(check_vmflag_guard(ptr)); 2219 + 2220 + /* Install a single guard region. */ 2221 + ASSERT_EQ(madvise(ptr, page_size, MADV_GUARD_INSTALL), 0); 2222 + 2223 + /* Now we should see a guard flag. */ 2224 + ASSERT_TRUE(check_vmflag_guard(ptr)); 2225 + 2226 + /* 2227 + * Removing the guard region should not change things because we simply 2228 + * cannot accurately track whether a given VMA has had all of its guard 2229 + * regions removed. 2230 + */ 2231 + ASSERT_EQ(madvise(ptr, page_size, MADV_GUARD_REMOVE), 0); 2232 + ASSERT_TRUE(check_vmflag_guard(ptr)); 2233 + 2234 + /* Install guard regions throughout. */ 2235 + for (i = 0; i < 10; i++) { 2236 + ASSERT_EQ(madvise(&ptr[i * page_size], page_size, MADV_GUARD_INSTALL), 0); 2237 + /* We should always see the guard region flag. */ 2238 + ASSERT_TRUE(check_vmflag_guard(ptr)); 2239 + } 2240 + 2241 + /* Split into two VMAs. */ 2242 + ASSERT_EQ(munmap(&ptr[4 * page_size], page_size), 0); 2243 + 2244 + /* Both VMAs should have the guard flag set. */ 2245 + ASSERT_TRUE(check_vmflag_guard(ptr)); 2246 + ASSERT_TRUE(check_vmflag_guard(&ptr[5 * page_size])); 2247 + 2248 + /* 2249 + * If the local file system is unable to merge VMAs due to having 2250 + * unusual characteristics, there is no point in asserting merge 2251 + * behaviour. 2252 + */ 2253 + if (!local_fs_has_sane_mmap(self, variant)) { 2254 + TH_LOG("local filesystem does not support sane merging skipping merge test"); 2255 + return; 2256 + } 2257 + 2258 + /* Map a fresh VMA between the two split VMAs. */ 2259 + ptr2 = mmap_(self, variant, &ptr[4 * page_size], page_size, 2260 + PROT_READ | PROT_WRITE, MAP_FIXED, 4 * page_size); 2261 + ASSERT_NE(ptr2, MAP_FAILED); 2262 + 2263 + /* 2264 + * Check the procmap to ensure that this VMA merged with the adjacent 2265 + * two. The guard region flag is 'sticky' so should not preclude 2266 + * merging. 2267 + */ 2268 + ASSERT_EQ(open_self_procmap(&procmap), 0); 2269 + ASSERT_TRUE(find_vma_procmap(&procmap, ptr)); 2270 + ASSERT_EQ(procmap.query.vma_start, (unsigned long)ptr); 2271 + ASSERT_EQ(procmap.query.vma_end, (unsigned long)ptr + 10 * page_size); 2272 + ASSERT_EQ(close_procmap(&procmap), 0); 2273 + /* And, of course, this VMA should have the guard flag set. */ 2274 + ASSERT_TRUE(check_vmflag_guard(ptr)); 2253 2275 } 2254 2276 2255 2277 TEST_HARNESS_MAIN
+5
tools/testing/selftests/mm/vm_util.c
··· 449 449 return check_vmflag(addr, "pf"); 450 450 } 451 451 452 + bool check_vmflag_guard(void *addr) 453 + { 454 + return check_vmflag(addr, "gu"); 455 + } 456 + 452 457 bool softdirty_supported(void) 453 458 { 454 459 char *addr;
+1
tools/testing/selftests/mm/vm_util.h
··· 98 98 unsigned long get_free_hugepages(void); 99 99 bool check_vmflag_io(void *addr); 100 100 bool check_vmflag_pfnmap(void *addr); 101 + bool check_vmflag_guard(void *addr); 101 102 int open_procmap(pid_t pid, struct procmap_fd *procmap_out); 102 103 int query_procmap(struct procmap_fd *procmap); 103 104 bool find_vma_procmap(struct procmap_fd *procmap, void *address);