1 // SPDX-License-Identifier: GPL-2.0 2 3 /* 4 * Tests for mremap w/ MREMAP_DONTUNMAP. 5 * 6 * Copyright 2020, Brian Geffon <bgeffon@google.com> 7 */ 8 #define _GNU_SOURCE 9 #include <sys/mman.h> 10 #include <sys/syscall.h> 11 #include <linux/mman.h> 12 #include <errno.h> 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <string.h> 16 #include <unistd.h> 17 18 #include "kselftest.h" 19 #include "vm_util.h" 20 21 unsigned long page_size; 22 char *page_buffer; 23 24 static void dump_maps(void) 25 { 26 char cmd[32]; 27 28 snprintf(cmd, sizeof(cmd), "cat /proc/%d/maps", getpid()); 29 system(cmd); 30 } 31 32 #define BUG_ON(condition, description) \ 33 do { \ 34 if (condition) { \ 35 dump_maps(); \ 36 ksft_exit_fail_msg("[FAIL]\t%s:%d\t%s:%s\n", \ 37 __func__, __LINE__, (description), \ 38 strerror(errno)); \ 39 } \ 40 } while (0) 41 42 // Try a simple operation for to "test" for kernel support this prevents 43 // reporting tests as failed when it's run on an older kernel. 44 static int kernel_support_for_mremap_dontunmap() 45 { 46 int ret = 0; 47 unsigned long num_pages = 1; 48 void *source_mapping = mmap(NULL, num_pages * page_size, PROT_NONE, 49 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 50 BUG_ON(source_mapping == MAP_FAILED, "mmap"); 51 52 // This simple remap should only fail if MREMAP_DONTUNMAP isn't 53 // supported. 54 void *dest_mapping = 55 mremap(source_mapping, num_pages * page_size, num_pages * page_size, 56 MREMAP_DONTUNMAP | MREMAP_MAYMOVE, 0); 57 if (dest_mapping == MAP_FAILED) { 58 ret = errno; 59 } else { 60 BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1, 61 "unable to unmap destination mapping"); 62 } 63 64 BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, 65 "unable to unmap source mapping"); 66 return ret; 67 } 68 69 // This helper will just validate that an entire mapping contains the expected 70 // byte. 71 static int check_region_contains_byte(void *addr, unsigned long size, char byte) 72 { 73 BUG_ON(size & (page_size - 1), 74 "check_region_contains_byte expects page multiples"); 75 BUG_ON((unsigned long)addr & (page_size - 1), 76 "check_region_contains_byte expects page alignment"); 77 78 memset(page_buffer, byte, page_size); 79 80 unsigned long num_pages = size / page_size; 81 unsigned long i; 82 83 // Compare each page checking that it contains our expected byte. 84 for (i = 0; i < num_pages; ++i) { 85 int ret = 86 memcmp(addr + (i * page_size), page_buffer, page_size); 87 if (ret) { 88 return ret; 89 } 90 } 91 92 return 0; 93 } 94 95 // this test validates that MREMAP_DONTUNMAP moves the pagetables while leaving 96 // the source mapping mapped. 97 static void mremap_dontunmap_simple() 98 { 99 unsigned long num_pages = 5; 100 101 void *source_mapping = 102 mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, 103 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 104 BUG_ON(source_mapping == MAP_FAILED, "mmap"); 105 106 memset(source_mapping, 'a', num_pages * page_size); 107 108 // Try to just move the whole mapping anywhere (not fixed). 109 void *dest_mapping = 110 mremap(source_mapping, num_pages * page_size, num_pages * page_size, 111 MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL); 112 BUG_ON(dest_mapping == MAP_FAILED, "mremap"); 113 114 // Validate that the pages have been moved, we know they were moved if 115 // the dest_mapping contains a's. 116 BUG_ON(check_region_contains_byte 117 (dest_mapping, num_pages * page_size, 'a') != 0, 118 "pages did not migrate"); 119 BUG_ON(check_region_contains_byte 120 (source_mapping, num_pages * page_size, 0) != 0, 121 "source should have no ptes"); 122 123 BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1, 124 "unable to unmap destination mapping"); 125 BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, 126 "unable to unmap source mapping"); 127 ksft_test_result_pass("%s\n", __func__); 128 } 129 130 // This test validates that MREMAP_DONTUNMAP on a shared mapping works as expected. 131 static void mremap_dontunmap_simple_shmem() 132 { 133 unsigned long num_pages = 5; 134 135 int mem_fd = memfd_create("memfd", MFD_CLOEXEC); 136 BUG_ON(mem_fd < 0, "memfd_create"); 137 138 BUG_ON(ftruncate(mem_fd, num_pages * page_size) < 0, 139 "ftruncate"); 140 141 void *source_mapping = 142 mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, 143 MAP_FILE | MAP_SHARED, mem_fd, 0); 144 BUG_ON(source_mapping == MAP_FAILED, "mmap"); 145 146 BUG_ON(close(mem_fd) < 0, "close"); 147 148 memset(source_mapping, 'a', num_pages * page_size); 149 150 // Try to just move the whole mapping anywhere (not fixed). 151 void *dest_mapping = 152 mremap(source_mapping, num_pages * page_size, num_pages * page_size, 153 MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL); 154 if (dest_mapping == MAP_FAILED && errno == EINVAL) { 155 // Old kernel which doesn't support MREMAP_DONTUNMAP on shmem. 156 BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, 157 "unable to unmap source mapping"); 158 return; 159 } 160 161 BUG_ON(dest_mapping == MAP_FAILED, "mremap"); 162 163 // Validate that the pages have been moved, we know they were moved if 164 // the dest_mapping contains a's. 165 BUG_ON(check_region_contains_byte 166 (dest_mapping, num_pages * page_size, 'a') != 0, 167 "pages did not migrate"); 168 169 // Because the region is backed by shmem, we will actually see the same 170 // memory at the source location still. 171 BUG_ON(check_region_contains_byte 172 (source_mapping, num_pages * page_size, 'a') != 0, 173 "source should have no ptes"); 174 175 BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1, 176 "unable to unmap destination mapping"); 177 BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, 178 "unable to unmap source mapping"); 179 ksft_test_result_pass("%s\n", __func__); 180 } 181 182 // This test validates MREMAP_DONTUNMAP will move page tables to a specific 183 // destination using MREMAP_FIXED, also while validating that the source 184 // remains intact. 185 static void mremap_dontunmap_simple_fixed() 186 { 187 unsigned long num_pages = 5; 188 189 // Since we want to guarantee that we can remap to a point, we will 190 // create a mapping up front. 191 void *dest_mapping = 192 mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, 193 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 194 BUG_ON(dest_mapping == MAP_FAILED, "mmap"); 195 memset(dest_mapping, 'X', num_pages * page_size); 196 197 void *source_mapping = 198 mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, 199 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 200 BUG_ON(source_mapping == MAP_FAILED, "mmap"); 201 memset(source_mapping, 'a', num_pages * page_size); 202 203 void *remapped_mapping = 204 mremap(source_mapping, num_pages * page_size, num_pages * page_size, 205 MREMAP_FIXED | MREMAP_DONTUNMAP | MREMAP_MAYMOVE, 206 dest_mapping); 207 BUG_ON(remapped_mapping == MAP_FAILED, "mremap"); 208 BUG_ON(remapped_mapping != dest_mapping, 209 "mremap should have placed the remapped mapping at dest_mapping"); 210 211 // The dest mapping will have been unmap by mremap so we expect the Xs 212 // to be gone and replaced with a's. 213 BUG_ON(check_region_contains_byte 214 (dest_mapping, num_pages * page_size, 'a') != 0, 215 "pages did not migrate"); 216 217 // And the source mapping will have had its ptes dropped. 218 BUG_ON(check_region_contains_byte 219 (source_mapping, num_pages * page_size, 0) != 0, 220 "source should have no ptes"); 221 222 BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1, 223 "unable to unmap destination mapping"); 224 BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, 225 "unable to unmap source mapping"); 226 ksft_test_result_pass("%s\n", __func__); 227 } 228 229 // This test validates that we can MREMAP_DONTUNMAP for a portion of an 230 // existing mapping. 231 static void mremap_dontunmap_partial_mapping() 232 { 233 /* 234 * source mapping: 235 * -------------- 236 * | aaaaaaaaaa | 237 * -------------- 238 * to become: 239 * -------------- 240 * | aaaaa00000 | 241 * -------------- 242 * With the destination mapping containing 5 pages of As. 243 * --------- 244 * | aaaaa | 245 * --------- 246 */ 247 unsigned long num_pages = 10; 248 void *source_mapping = 249 mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, 250 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 251 BUG_ON(source_mapping == MAP_FAILED, "mmap"); 252 memset(source_mapping, 'a', num_pages * page_size); 253 254 // We will grab the last 5 pages of the source and move them. 255 void *dest_mapping = 256 mremap(source_mapping + (5 * page_size), 5 * page_size, 257 5 * page_size, 258 MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL); 259 BUG_ON(dest_mapping == MAP_FAILED, "mremap"); 260 261 // We expect the first 5 pages of the source to contain a's and the 262 // final 5 pages to contain zeros. 263 BUG_ON(check_region_contains_byte(source_mapping, 5 * page_size, 'a') != 264 0, "first 5 pages of source should have original pages"); 265 BUG_ON(check_region_contains_byte 266 (source_mapping + (5 * page_size), 5 * page_size, 0) != 0, 267 "final 5 pages of source should have no ptes"); 268 269 // Finally we expect the destination to have 5 pages worth of a's. 270 BUG_ON(check_region_contains_byte(dest_mapping, 5 * page_size, 'a') != 271 0, "dest mapping should contain ptes from the source"); 272 273 BUG_ON(munmap(dest_mapping, 5 * page_size) == -1, 274 "unable to unmap destination mapping"); 275 BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, 276 "unable to unmap source mapping"); 277 ksft_test_result_pass("%s\n", __func__); 278 } 279 280 // This test validates that we can remap over only a portion of a mapping. 281 static void mremap_dontunmap_partial_mapping_overwrite(void) 282 { 283 /* 284 * source mapping: 285 * --------- 286 * |aaaaa| 287 * --------- 288 * dest mapping initially: 289 * ----------- 290 * |XXXXXXXXXX| 291 * ------------ 292 * Source to become: 293 * --------- 294 * |00000| 295 * --------- 296 * With the destination mapping containing 5 pages of As. 297 * ------------ 298 * |aaaaaXXXXX| 299 * ------------ 300 */ 301 void *source_mapping = 302 mmap(NULL, 5 * page_size, PROT_READ | PROT_WRITE, 303 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 304 BUG_ON(source_mapping == MAP_FAILED, "mmap"); 305 memset(source_mapping, 'a', 5 * page_size); 306 307 void *dest_mapping = 308 mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, 309 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 310 BUG_ON(dest_mapping == MAP_FAILED, "mmap"); 311 memset(dest_mapping, 'X', 10 * page_size); 312 313 // We will grab the last 5 pages of the source and move them. 314 void *remapped_mapping = 315 mremap(source_mapping, 5 * page_size, 316 5 * page_size, 317 MREMAP_DONTUNMAP | MREMAP_MAYMOVE | MREMAP_FIXED, dest_mapping); 318 BUG_ON(remapped_mapping == MAP_FAILED, "mremap"); 319 BUG_ON(dest_mapping != remapped_mapping, "expected to remap to dest_mapping"); 320 321 BUG_ON(check_region_contains_byte(source_mapping, 5 * page_size, 0) != 322 0, "first 5 pages of source should have no ptes"); 323 324 // Finally we expect the destination to have 5 pages worth of a's. 325 BUG_ON(check_region_contains_byte(dest_mapping, 5 * page_size, 'a') != 0, 326 "dest mapping should contain ptes from the source"); 327 328 // Finally the last 5 pages shouldn't have been touched. 329 BUG_ON(check_region_contains_byte(dest_mapping + (5 * page_size), 330 5 * page_size, 'X') != 0, 331 "dest mapping should have retained the last 5 pages"); 332 333 BUG_ON(munmap(dest_mapping, 10 * page_size) == -1, 334 "unable to unmap destination mapping"); 335 BUG_ON(munmap(source_mapping, 5 * page_size) == -1, 336 "unable to unmap source mapping"); 337 ksft_test_result_pass("%s\n", __func__); 338 } 339 340 /* VmLck from /proc/self/status, which is mm->locked_vm in kB. */ 341 static unsigned long locked_vm_kb(void) 342 { 343 unsigned long kb; 344 char buf[256]; 345 FILE *fp; 346 347 fp = fopen("/proc/self/status", "r"); 348 BUG_ON(!fp, "unable to open /proc/self/status"); 349 BUG_ON(!check_for_pattern(fp, "VmLck:", buf, sizeof(buf)) || 350 sscanf(buf, "VmLck: %lu kB", &kb) != 1, "unable to read VmLck"); 351 fclose(fp); 352 353 return kb; 354 } 355 356 /* 357 * MREMAP_DONTUNMAP clears the mlock flags of the source VMA and sets them on 358 * the destination, and mm->locked_vm has to follow. mlock2() a source of 359 * num_pages and move its first move_pages to gap pages past its end: 360 * 361 * |------ source ------|... gap ...|-- dest --| 362 * 363 * then unmap everything and check that VmLck is back where it started. With 364 * no gap the destination is adjacent to the source and could merge with it, 365 * otherwise the gap is left PROT_NONE so that it cannot. 366 */ 367 static void mremap_dontunmap_mlock(const char *desc, unsigned long num_pages, 368 unsigned long move_pages, unsigned long gap, 369 int mlock_flags) 370 { 371 unsigned long size = (num_pages + gap + move_pages) * page_size; 372 unsigned long locked = locked_vm_kb(); 373 void *source, *dest; 374 375 source = mmap(NULL, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 376 BUG_ON(source == MAP_FAILED, "mmap"); 377 BUG_ON(mprotect(source, num_pages * page_size, 378 PROT_READ | PROT_WRITE) == -1, "mprotect"); 379 dest = source + (num_pages + gap) * page_size; 380 381 if (syscall(__NR_mlock2, source, num_pages * page_size, mlock_flags)) { 382 ksft_test_result_skip("%s: %s: mlock2: %s\n", __func__, desc, 383 strerror(errno)); 384 BUG_ON(munmap(source, size) == -1, "unable to unmap mappings"); 385 return; 386 } 387 388 BUG_ON(mremap(source, move_pages * page_size, move_pages * page_size, 389 MREMAP_DONTUNMAP | MREMAP_MAYMOVE | MREMAP_FIXED, 390 dest) != dest, "mremap"); 391 392 BUG_ON(munmap(source, size) == -1, "unable to unmap mappings"); 393 ksft_test_result(locked_vm_kb() == locked, "%s: %s\n", __func__, desc); 394 } 395 396 int main(void) 397 { 398 ksft_print_header(); 399 400 page_size = sysconf(_SC_PAGE_SIZE); 401 402 // test for kernel support for MREMAP_DONTUNMAP skipping the test if 403 // not. 404 if (kernel_support_for_mremap_dontunmap() != 0) { 405 ksft_print_msg("No kernel support for MREMAP_DONTUNMAP\n"); 406 ksft_finished(); 407 } 408 409 ksft_set_plan(8); 410 411 // Keep a page sized buffer around for when we need it. 412 page_buffer = 413 mmap(NULL, page_size, PROT_READ | PROT_WRITE, 414 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 415 BUG_ON(page_buffer == MAP_FAILED, "unable to mmap a page."); 416 417 mremap_dontunmap_simple(); 418 mremap_dontunmap_simple_shmem(); 419 mremap_dontunmap_simple_fixed(); 420 mremap_dontunmap_partial_mapping(); 421 mremap_dontunmap_partial_mapping_overwrite(); 422 mremap_dontunmap_mlock("onfault self-merge", 1, 1, 0, MLOCK_ONFAULT); 423 mremap_dontunmap_mlock("partial", 3, 2, 1, 0); 424 mremap_dontunmap_mlock("onfault partial", 3, 2, 1, MLOCK_ONFAULT); 425 426 BUG_ON(munmap(page_buffer, page_size) == -1, 427 "unable to unmap page buffer"); 428 429 ksft_finished(); 430 } 431