1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Userfaultfd unit tests. 4 * 5 * Copyright (C) 2015-2023 Red Hat, Inc. 6 */ 7 8 #include <asm-generic/unistd.h> 9 #include "uffd-common.h" 10 11 #include "../../../../mm/gup_test.h" 12 13 /* The unit test doesn't need a large or random size, make it 32MB for now */ 14 #define UFFD_TEST_MEM_SIZE (32UL << 20) 15 16 #define MEM_ANON BIT_ULL(0) 17 #define MEM_SHMEM BIT_ULL(1) 18 #define MEM_SHMEM_PRIVATE BIT_ULL(2) 19 #define MEM_HUGETLB BIT_ULL(3) 20 #define MEM_HUGETLB_PRIVATE BIT_ULL(4) 21 22 #define MEM_ALL (MEM_ANON | MEM_SHMEM | MEM_SHMEM_PRIVATE | \ 23 MEM_HUGETLB | MEM_HUGETLB_PRIVATE) 24 25 #define ALIGN_UP(x, align_to) \ 26 ((__typeof__(x))((((unsigned long)(x)) + ((align_to)-1)) & ~((align_to)-1))) 27 28 struct mem_type { 29 const char *name; 30 unsigned int mem_flag; 31 uffd_test_ops_t *mem_ops; 32 bool shared; 33 }; 34 typedef struct mem_type mem_type_t; 35 36 mem_type_t mem_types[] = { 37 { 38 .name = "anon", 39 .mem_flag = MEM_ANON, 40 .mem_ops = &anon_uffd_test_ops, 41 .shared = false, 42 }, 43 { 44 .name = "shmem", 45 .mem_flag = MEM_SHMEM, 46 .mem_ops = &shmem_uffd_test_ops, 47 .shared = true, 48 }, 49 { 50 .name = "shmem-private", 51 .mem_flag = MEM_SHMEM_PRIVATE, 52 .mem_ops = &shmem_uffd_test_ops, 53 .shared = false, 54 }, 55 { 56 .name = "hugetlb", 57 .mem_flag = MEM_HUGETLB, 58 .mem_ops = &hugetlb_uffd_test_ops, 59 .shared = true, 60 }, 61 { 62 .name = "hugetlb-private", 63 .mem_flag = MEM_HUGETLB_PRIVATE, 64 .mem_ops = &hugetlb_uffd_test_ops, 65 .shared = false, 66 }, 67 }; 68 69 /* Arguments to be passed over to each uffd unit test */ 70 struct uffd_test_args { 71 mem_type_t *mem_type; 72 }; 73 typedef struct uffd_test_args uffd_test_args_t; 74 75 /* Returns: UFFD_TEST_* */ 76 typedef void (*uffd_test_fn)(uffd_test_args_t *); 77 78 typedef struct { 79 const char *name; 80 uffd_test_fn uffd_fn; 81 unsigned int mem_targets; 82 uint64_t uffd_feature_required; 83 uffd_test_case_ops_t *test_case_ops; 84 } uffd_test_case_t; 85 86 static void uffd_test_report(void) 87 { 88 printf("Userfaults unit tests: pass=%u, skip=%u, fail=%u (total=%u)\n", 89 ksft_get_pass_cnt(), 90 ksft_get_xskip_cnt(), 91 ksft_get_fail_cnt(), 92 ksft_test_num()); 93 } 94 95 static void uffd_test_pass(void) 96 { 97 printf("done\n"); 98 ksft_inc_pass_cnt(); 99 } 100 101 #define uffd_test_start(...) do { \ 102 printf("Testing "); \ 103 printf(__VA_ARGS__); \ 104 printf("... "); \ 105 fflush(stdout); \ 106 } while (0) 107 108 #define uffd_test_fail(...) do { \ 109 printf("failed [reason: "); \ 110 printf(__VA_ARGS__); \ 111 printf("]\n"); \ 112 ksft_inc_fail_cnt(); \ 113 } while (0) 114 115 static void uffd_test_skip(const char *message) 116 { 117 printf("skipped [reason: %s]\n", message); 118 ksft_inc_xskip_cnt(); 119 } 120 121 /* 122 * Returns 1 if specific userfaultfd supported, 0 otherwise. Note, we'll 123 * return 1 even if some test failed as long as uffd supported, because in 124 * that case we still want to proceed with the rest uffd unit tests. 125 */ 126 static int test_uffd_api(bool use_dev) 127 { 128 struct uffdio_api uffdio_api; 129 int uffd; 130 131 uffd_test_start("UFFDIO_API (with %s)", 132 use_dev ? "/dev/userfaultfd" : "syscall"); 133 134 if (use_dev) 135 uffd = uffd_open_dev(UFFD_FLAGS); 136 else 137 uffd = uffd_open_sys(UFFD_FLAGS); 138 if (uffd < 0) { 139 uffd_test_skip("cannot open userfaultfd handle"); 140 return 0; 141 } 142 143 /* Test wrong UFFD_API */ 144 uffdio_api.api = 0xab; 145 uffdio_api.features = 0; 146 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0) { 147 uffd_test_fail("UFFDIO_API should fail with wrong api but didn't"); 148 goto out; 149 } 150 151 /* Test wrong feature bit */ 152 uffdio_api.api = UFFD_API; 153 uffdio_api.features = BIT_ULL(63); 154 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0) { 155 uffd_test_fail("UFFDIO_API should fail with wrong feature but didn't"); 156 goto out; 157 } 158 159 /* Test normal UFFDIO_API */ 160 uffdio_api.api = UFFD_API; 161 uffdio_api.features = 0; 162 if (ioctl(uffd, UFFDIO_API, &uffdio_api)) { 163 uffd_test_fail("UFFDIO_API should succeed but failed"); 164 goto out; 165 } 166 167 /* Test double requests of UFFDIO_API with a random feature set */ 168 uffdio_api.features = BIT_ULL(0); 169 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0) { 170 uffd_test_fail("UFFDIO_API should reject initialized uffd"); 171 goto out; 172 } 173 174 uffd_test_pass(); 175 out: 176 close(uffd); 177 /* We have a valid uffd handle */ 178 return 1; 179 } 180 181 /* 182 * This function initializes the global variables. TODO: remove global 183 * vars and then remove this. 184 */ 185 static int 186 uffd_setup_environment(uffd_test_args_t *args, uffd_test_case_t *test, 187 mem_type_t *mem_type, const char **errmsg) 188 { 189 map_shared = mem_type->shared; 190 uffd_test_ops = mem_type->mem_ops; 191 uffd_test_case_ops = test->test_case_ops; 192 193 if (mem_type->mem_flag & (MEM_HUGETLB_PRIVATE | MEM_HUGETLB)) 194 page_size = default_huge_page_size(); 195 else 196 page_size = psize(); 197 198 nr_pages = UFFD_TEST_MEM_SIZE / page_size; 199 /* TODO: remove this global var.. it's so ugly */ 200 nr_cpus = 1; 201 202 /* Initialize test arguments */ 203 args->mem_type = mem_type; 204 205 return uffd_test_ctx_init(test->uffd_feature_required, errmsg); 206 } 207 208 static bool uffd_feature_supported(uffd_test_case_t *test) 209 { 210 uint64_t features; 211 212 if (uffd_get_features(&features)) 213 return false; 214 215 return (features & test->uffd_feature_required) == 216 test->uffd_feature_required; 217 } 218 219 static int pagemap_open(void) 220 { 221 int fd = open("/proc/self/pagemap", O_RDONLY); 222 223 if (fd < 0) 224 err("open pagemap"); 225 226 return fd; 227 } 228 229 /* This macro let __LINE__ works in err() */ 230 #define pagemap_check_wp(value, wp) do { \ 231 if (!!(value & PM_UFFD_WP) != wp) \ 232 err("pagemap uffd-wp bit error: 0x%"PRIx64, value); \ 233 } while (0) 234 235 typedef struct { 236 int parent_uffd, child_uffd; 237 } fork_event_args; 238 239 static void *fork_event_consumer(void *data) 240 { 241 fork_event_args *args = data; 242 struct uffd_msg msg = { 0 }; 243 244 /* Ready for parent thread to fork */ 245 pthread_barrier_wait(&ready_for_fork); 246 247 /* Read until a full msg received */ 248 while (uffd_read_msg(args->parent_uffd, &msg)); 249 250 if (msg.event != UFFD_EVENT_FORK) 251 err("wrong message: %u\n", msg.event); 252 253 /* Just to be properly freed later */ 254 args->child_uffd = msg.arg.fork.ufd; 255 return NULL; 256 } 257 258 typedef struct { 259 int gup_fd; 260 bool pinned; 261 } pin_args; 262 263 /* 264 * Returns 0 if succeed, <0 for errors. pin_pages() needs to be paired 265 * with unpin_pages(). Currently it needs to be RO longterm pin to satisfy 266 * all needs of the test cases (e.g., trigger unshare, trigger fork() early 267 * CoW, etc.). 268 */ 269 static int pin_pages(pin_args *args, void *buffer, size_t size) 270 { 271 struct pin_longterm_test test = { 272 .addr = (uintptr_t)buffer, 273 .size = size, 274 /* Read-only pins */ 275 .flags = 0, 276 }; 277 278 if (args->pinned) 279 err("already pinned"); 280 281 args->gup_fd = open("/sys/kernel/debug/gup_test", O_RDWR); 282 if (args->gup_fd < 0) 283 return -errno; 284 285 if (ioctl(args->gup_fd, PIN_LONGTERM_TEST_START, &test)) { 286 /* Even if gup_test existed, can be an old gup_test / kernel */ 287 close(args->gup_fd); 288 return -errno; 289 } 290 args->pinned = true; 291 return 0; 292 } 293 294 static void unpin_pages(pin_args *args) 295 { 296 if (!args->pinned) 297 err("unpin without pin first"); 298 if (ioctl(args->gup_fd, PIN_LONGTERM_TEST_STOP)) 299 err("PIN_LONGTERM_TEST_STOP"); 300 close(args->gup_fd); 301 args->pinned = false; 302 } 303 304 static int pagemap_test_fork(int uffd, bool with_event, bool test_pin) 305 { 306 fork_event_args args = { .parent_uffd = uffd, .child_uffd = -1 }; 307 pthread_t thread; 308 pid_t child; 309 uint64_t value; 310 int fd, result; 311 312 /* Prepare a thread to resolve EVENT_FORK */ 313 if (with_event) { 314 pthread_barrier_init(&ready_for_fork, NULL, 2); 315 if (pthread_create(&thread, NULL, fork_event_consumer, &args)) 316 err("pthread_create()"); 317 /* Wait for child thread to start before forking */ 318 pthread_barrier_wait(&ready_for_fork); 319 pthread_barrier_destroy(&ready_for_fork); 320 } 321 322 child = fork(); 323 if (!child) { 324 /* Open the pagemap fd of the child itself */ 325 pin_args args = {}; 326 327 fd = pagemap_open(); 328 329 if (test_pin && pin_pages(&args, area_dst, page_size)) 330 /* 331 * Normally when reach here we have pinned in 332 * previous tests, so shouldn't fail anymore 333 */ 334 err("pin page failed in child"); 335 336 value = pagemap_get_entry(fd, area_dst); 337 /* 338 * After fork(), we should handle uffd-wp bit differently: 339 * 340 * (1) when with EVENT_FORK, it should persist 341 * (2) when without EVENT_FORK, it should be dropped 342 */ 343 pagemap_check_wp(value, with_event); 344 if (test_pin) 345 unpin_pages(&args); 346 /* Succeed */ 347 exit(0); 348 } 349 waitpid(child, &result, 0); 350 351 if (with_event) { 352 if (pthread_join(thread, NULL)) 353 err("pthread_join()"); 354 if (args.child_uffd < 0) 355 err("Didn't receive child uffd"); 356 close(args.child_uffd); 357 } 358 359 return result; 360 } 361 362 static void uffd_wp_unpopulated_test(uffd_test_args_t *args) 363 { 364 uint64_t value; 365 int pagemap_fd; 366 367 if (uffd_register(uffd, area_dst, nr_pages * page_size, 368 false, true, false)) 369 err("register failed"); 370 371 pagemap_fd = pagemap_open(); 372 373 /* Test applying pte marker to anon unpopulated */ 374 wp_range(uffd, (uint64_t)area_dst, page_size, true); 375 value = pagemap_get_entry(pagemap_fd, area_dst); 376 pagemap_check_wp(value, true); 377 378 /* Test unprotect on anon pte marker */ 379 wp_range(uffd, (uint64_t)area_dst, page_size, false); 380 value = pagemap_get_entry(pagemap_fd, area_dst); 381 pagemap_check_wp(value, false); 382 383 /* Test zap on anon marker */ 384 wp_range(uffd, (uint64_t)area_dst, page_size, true); 385 if (madvise(area_dst, page_size, MADV_DONTNEED)) 386 err("madvise(MADV_DONTNEED) failed"); 387 value = pagemap_get_entry(pagemap_fd, area_dst); 388 pagemap_check_wp(value, false); 389 390 /* Test fault in after marker removed */ 391 *area_dst = 1; 392 value = pagemap_get_entry(pagemap_fd, area_dst); 393 pagemap_check_wp(value, false); 394 /* Drop it to make pte none again */ 395 if (madvise(area_dst, page_size, MADV_DONTNEED)) 396 err("madvise(MADV_DONTNEED) failed"); 397 398 /* Test read-zero-page upon pte marker */ 399 wp_range(uffd, (uint64_t)area_dst, page_size, true); 400 *(volatile char *)area_dst; 401 /* Drop it to make pte none again */ 402 if (madvise(area_dst, page_size, MADV_DONTNEED)) 403 err("madvise(MADV_DONTNEED) failed"); 404 405 uffd_test_pass(); 406 } 407 408 static void uffd_wp_fork_test_common(uffd_test_args_t *args, 409 bool with_event) 410 { 411 int pagemap_fd; 412 uint64_t value; 413 414 if (uffd_register(uffd, area_dst, nr_pages * page_size, 415 false, true, false)) 416 err("register failed"); 417 418 pagemap_fd = pagemap_open(); 419 420 /* Touch the page */ 421 *area_dst = 1; 422 wp_range(uffd, (uint64_t)area_dst, page_size, true); 423 value = pagemap_get_entry(pagemap_fd, area_dst); 424 pagemap_check_wp(value, true); 425 if (pagemap_test_fork(uffd, with_event, false)) { 426 uffd_test_fail("Detected %s uffd-wp bit in child in present pte", 427 with_event ? "missing" : "stall"); 428 goto out; 429 } 430 431 /* 432 * This is an attempt for zapping the pgtable so as to test the 433 * markers. 434 * 435 * For private mappings, PAGEOUT will only work on exclusive ptes 436 * (PM_MMAP_EXCLUSIVE) which we should satisfy. 437 * 438 * For shared, PAGEOUT may not work. Use DONTNEED instead which 439 * plays a similar role of zapping (rather than freeing the page) 440 * to expose pte markers. 441 */ 442 if (args->mem_type->shared) { 443 if (madvise(area_dst, page_size, MADV_DONTNEED)) 444 err("MADV_DONTNEED"); 445 } else { 446 /* 447 * NOTE: ignore retval because private-hugetlb doesn't yet 448 * support swapping, so it could fail. 449 */ 450 madvise(area_dst, page_size, MADV_PAGEOUT); 451 } 452 453 /* Uffd-wp should persist even swapped out */ 454 value = pagemap_get_entry(pagemap_fd, area_dst); 455 pagemap_check_wp(value, true); 456 if (pagemap_test_fork(uffd, with_event, false)) { 457 uffd_test_fail("Detected %s uffd-wp bit in child in zapped pte", 458 with_event ? "missing" : "stall"); 459 goto out; 460 } 461 462 /* Unprotect; this tests swap pte modifications */ 463 wp_range(uffd, (uint64_t)area_dst, page_size, false); 464 value = pagemap_get_entry(pagemap_fd, area_dst); 465 pagemap_check_wp(value, false); 466 467 /* Fault in the page from disk */ 468 *area_dst = 2; 469 value = pagemap_get_entry(pagemap_fd, area_dst); 470 pagemap_check_wp(value, false); 471 uffd_test_pass(); 472 out: 473 if (uffd_unregister(uffd, area_dst, nr_pages * page_size)) 474 err("unregister failed"); 475 close(pagemap_fd); 476 } 477 478 static void uffd_wp_fork_test(uffd_test_args_t *args) 479 { 480 uffd_wp_fork_test_common(args, false); 481 } 482 483 static void uffd_wp_fork_with_event_test(uffd_test_args_t *args) 484 { 485 uffd_wp_fork_test_common(args, true); 486 } 487 488 static void uffd_wp_fork_pin_test_common(uffd_test_args_t *args, 489 bool with_event) 490 { 491 int pagemap_fd; 492 pin_args pin_args = {}; 493 494 if (uffd_register(uffd, area_dst, page_size, false, true, false)) 495 err("register failed"); 496 497 pagemap_fd = pagemap_open(); 498 499 /* Touch the page */ 500 *area_dst = 1; 501 wp_range(uffd, (uint64_t)area_dst, page_size, true); 502 503 /* 504 * 1. First pin, then fork(). This tests fork() special path when 505 * doing early CoW if the page is private. 506 */ 507 if (pin_pages(&pin_args, area_dst, page_size)) { 508 uffd_test_skip("Possibly CONFIG_GUP_TEST missing " 509 "or unprivileged"); 510 close(pagemap_fd); 511 uffd_unregister(uffd, area_dst, page_size); 512 return; 513 } 514 515 if (pagemap_test_fork(uffd, with_event, false)) { 516 uffd_test_fail("Detected %s uffd-wp bit in early CoW of fork()", 517 with_event ? "missing" : "stall"); 518 unpin_pages(&pin_args); 519 goto out; 520 } 521 522 unpin_pages(&pin_args); 523 524 /* 525 * 2. First fork(), then pin (in the child, where test_pin==true). 526 * This tests COR, aka, page unsharing on private memories. 527 */ 528 if (pagemap_test_fork(uffd, with_event, true)) { 529 uffd_test_fail("Detected %s uffd-wp bit when RO pin", 530 with_event ? "missing" : "stall"); 531 goto out; 532 } 533 uffd_test_pass(); 534 out: 535 if (uffd_unregister(uffd, area_dst, page_size)) 536 err("register failed"); 537 close(pagemap_fd); 538 } 539 540 static void uffd_wp_fork_pin_test(uffd_test_args_t *args) 541 { 542 uffd_wp_fork_pin_test_common(args, false); 543 } 544 545 static void uffd_wp_fork_pin_with_event_test(uffd_test_args_t *args) 546 { 547 uffd_wp_fork_pin_test_common(args, true); 548 } 549 550 static void check_memory_contents(char *p) 551 { 552 unsigned long i, j; 553 uint8_t expected_byte; 554 555 for (i = 0; i < nr_pages; ++i) { 556 expected_byte = ~((uint8_t)(i % ((uint8_t)-1))); 557 for (j = 0; j < page_size; j++) { 558 uint8_t v = *(uint8_t *)(p + (i * page_size) + j); 559 if (v != expected_byte) 560 err("unexpected page contents"); 561 } 562 } 563 } 564 565 static void uffd_minor_test_common(bool test_collapse, bool test_wp) 566 { 567 unsigned long p; 568 pthread_t uffd_mon; 569 char c; 570 struct uffd_args args = { 0 }; 571 572 /* 573 * NOTE: MADV_COLLAPSE is not yet compatible with WP, so testing 574 * both do not make much sense. 575 */ 576 assert(!(test_collapse && test_wp)); 577 578 if (uffd_register(uffd, area_dst_alias, nr_pages * page_size, 579 /* NOTE! MADV_COLLAPSE may not work with uffd-wp */ 580 false, test_wp, true)) 581 err("register failure"); 582 583 /* 584 * After registering with UFFD, populate the non-UFFD-registered side of 585 * the shared mapping. This should *not* trigger any UFFD minor faults. 586 */ 587 for (p = 0; p < nr_pages; ++p) 588 memset(area_dst + (p * page_size), p % ((uint8_t)-1), 589 page_size); 590 591 args.apply_wp = test_wp; 592 if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args)) 593 err("uffd_poll_thread create"); 594 595 /* 596 * Read each of the pages back using the UFFD-registered mapping. We 597 * expect that the first time we touch a page, it will result in a minor 598 * fault. uffd_poll_thread will resolve the fault by bit-flipping the 599 * page's contents, and then issuing a CONTINUE ioctl. 600 */ 601 check_memory_contents(area_dst_alias); 602 603 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c)) 604 err("pipe write"); 605 if (pthread_join(uffd_mon, NULL)) 606 err("join() failed"); 607 608 if (test_collapse) { 609 if (madvise(area_dst_alias, nr_pages * page_size, 610 MADV_COLLAPSE)) { 611 /* It's fine to fail for this one... */ 612 uffd_test_skip("MADV_COLLAPSE failed"); 613 return; 614 } 615 616 uffd_test_ops->check_pmd_mapping(area_dst, 617 nr_pages * page_size / 618 read_pmd_pagesize()); 619 /* 620 * This won't cause uffd-fault - it purely just makes sure there 621 * was no corruption. 622 */ 623 check_memory_contents(area_dst_alias); 624 } 625 626 if (args.missing_faults != 0 || args.minor_faults != nr_pages) 627 uffd_test_fail("stats check error"); 628 else 629 uffd_test_pass(); 630 } 631 632 void uffd_minor_test(uffd_test_args_t *args) 633 { 634 uffd_minor_test_common(false, false); 635 } 636 637 void uffd_minor_wp_test(uffd_test_args_t *args) 638 { 639 uffd_minor_test_common(false, true); 640 } 641 642 void uffd_minor_collapse_test(uffd_test_args_t *args) 643 { 644 uffd_minor_test_common(true, false); 645 } 646 647 static sigjmp_buf jbuf, *sigbuf; 648 649 static void sighndl(int sig, siginfo_t *siginfo, void *ptr) 650 { 651 if (sig == SIGBUS) { 652 if (sigbuf) 653 siglongjmp(*sigbuf, 1); 654 abort(); 655 } 656 } 657 658 /* 659 * For non-cooperative userfaultfd test we fork() a process that will 660 * generate pagefaults, will mremap the area monitored by the 661 * userfaultfd and at last this process will release the monitored 662 * area. 663 * For the anonymous and shared memory the area is divided into two 664 * parts, the first part is accessed before mremap, and the second 665 * part is accessed after mremap. Since hugetlbfs does not support 666 * mremap, the entire monitored area is accessed in a single pass for 667 * HUGETLB_TEST. 668 * The release of the pages currently generates event for shmem and 669 * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked 670 * for hugetlb. 671 * For signal test(UFFD_FEATURE_SIGBUS), signal_test = 1, we register 672 * monitored area, generate pagefaults and test that signal is delivered. 673 * Use UFFDIO_COPY to allocate missing page and retry. For signal_test = 2 674 * test robustness use case - we release monitored area, fork a process 675 * that will generate pagefaults and verify signal is generated. 676 * This also tests UFFD_FEATURE_EVENT_FORK event along with the signal 677 * feature. Using monitor thread, verify no userfault events are generated. 678 */ 679 static int faulting_process(int signal_test, bool wp) 680 { 681 unsigned long nr, i; 682 unsigned long long count; 683 unsigned long split_nr_pages; 684 unsigned long lastnr; 685 struct sigaction act; 686 volatile unsigned long signalled = 0; 687 688 split_nr_pages = (nr_pages + 1) / 2; 689 690 if (signal_test) { 691 sigbuf = &jbuf; 692 memset(&act, 0, sizeof(act)); 693 act.sa_sigaction = sighndl; 694 act.sa_flags = SA_SIGINFO; 695 if (sigaction(SIGBUS, &act, 0)) 696 err("sigaction"); 697 lastnr = (unsigned long)-1; 698 } 699 700 for (nr = 0; nr < split_nr_pages; nr++) { 701 volatile int steps = 1; 702 unsigned long offset = nr * page_size; 703 704 if (signal_test) { 705 if (sigsetjmp(*sigbuf, 1) != 0) { 706 if (steps == 1 && nr == lastnr) 707 err("Signal repeated"); 708 709 lastnr = nr; 710 if (signal_test == 1) { 711 if (steps == 1) { 712 /* This is a MISSING request */ 713 steps++; 714 if (copy_page(uffd, offset, wp)) 715 signalled++; 716 } else { 717 /* This is a WP request */ 718 assert(steps == 2); 719 wp_range(uffd, 720 (__u64)area_dst + 721 offset, 722 page_size, false); 723 } 724 } else { 725 signalled++; 726 continue; 727 } 728 } 729 } 730 731 count = *area_count(area_dst, nr); 732 if (count != count_verify[nr]) 733 err("nr %lu memory corruption %llu %llu\n", 734 nr, count, count_verify[nr]); 735 /* 736 * Trigger write protection if there is by writing 737 * the same value back. 738 */ 739 *area_count(area_dst, nr) = count; 740 } 741 742 if (signal_test) 743 return signalled != split_nr_pages; 744 745 area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size, 746 MREMAP_MAYMOVE | MREMAP_FIXED, area_src); 747 if (area_dst == MAP_FAILED) 748 err("mremap"); 749 /* Reset area_src since we just clobbered it */ 750 area_src = NULL; 751 752 for (; nr < nr_pages; nr++) { 753 count = *area_count(area_dst, nr); 754 if (count != count_verify[nr]) { 755 err("nr %lu memory corruption %llu %llu\n", 756 nr, count, count_verify[nr]); 757 } 758 /* 759 * Trigger write protection if there is by writing 760 * the same value back. 761 */ 762 *area_count(area_dst, nr) = count; 763 } 764 765 uffd_test_ops->release_pages(area_dst); 766 767 for (nr = 0; nr < nr_pages; nr++) 768 for (i = 0; i < page_size; i++) 769 if (*(area_dst + nr * page_size + i) != 0) 770 err("page %lu offset %lu is not zero", nr, i); 771 772 return 0; 773 } 774 775 static void uffd_sigbus_test_common(bool wp) 776 { 777 unsigned long userfaults; 778 pthread_t uffd_mon; 779 pid_t pid; 780 int err; 781 char c; 782 struct uffd_args args = { 0 }; 783 784 pthread_barrier_init(&ready_for_fork, NULL, 2); 785 786 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK); 787 788 if (uffd_register(uffd, area_dst, nr_pages * page_size, 789 true, wp, false)) 790 err("register failure"); 791 792 if (faulting_process(1, wp)) 793 err("faulting process failed"); 794 795 uffd_test_ops->release_pages(area_dst); 796 797 args.apply_wp = wp; 798 if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args)) 799 err("uffd_poll_thread create"); 800 801 /* Wait for child thread to start before forking */ 802 pthread_barrier_wait(&ready_for_fork); 803 pthread_barrier_destroy(&ready_for_fork); 804 805 pid = fork(); 806 if (pid < 0) 807 err("fork"); 808 809 if (!pid) 810 exit(faulting_process(2, wp)); 811 812 waitpid(pid, &err, 0); 813 if (err) 814 err("faulting process failed"); 815 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c)) 816 err("pipe write"); 817 if (pthread_join(uffd_mon, (void **)&userfaults)) 818 err("pthread_join()"); 819 820 if (userfaults) 821 uffd_test_fail("Signal test failed, userfaults: %ld", userfaults); 822 else 823 uffd_test_pass(); 824 } 825 826 static void uffd_sigbus_test(uffd_test_args_t *args) 827 { 828 uffd_sigbus_test_common(false); 829 } 830 831 static void uffd_sigbus_wp_test(uffd_test_args_t *args) 832 { 833 uffd_sigbus_test_common(true); 834 } 835 836 static void uffd_events_test_common(bool wp) 837 { 838 pthread_t uffd_mon; 839 pid_t pid; 840 int err; 841 char c; 842 struct uffd_args args = { 0 }; 843 844 pthread_barrier_init(&ready_for_fork, NULL, 2); 845 846 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK); 847 if (uffd_register(uffd, area_dst, nr_pages * page_size, 848 true, wp, false)) 849 err("register failure"); 850 851 args.apply_wp = wp; 852 if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args)) 853 err("uffd_poll_thread create"); 854 855 /* Wait for child thread to start before forking */ 856 pthread_barrier_wait(&ready_for_fork); 857 pthread_barrier_destroy(&ready_for_fork); 858 859 pid = fork(); 860 if (pid < 0) 861 err("fork"); 862 863 if (!pid) 864 exit(faulting_process(0, wp)); 865 866 waitpid(pid, &err, 0); 867 if (err) 868 err("faulting process failed"); 869 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c)) 870 err("pipe write"); 871 if (pthread_join(uffd_mon, NULL)) 872 err("pthread_join()"); 873 874 if (args.missing_faults != nr_pages) 875 uffd_test_fail("Fault counts wrong"); 876 else 877 uffd_test_pass(); 878 } 879 880 static void uffd_events_test(uffd_test_args_t *args) 881 { 882 uffd_events_test_common(false); 883 } 884 885 static void uffd_events_wp_test(uffd_test_args_t *args) 886 { 887 uffd_events_test_common(true); 888 } 889 890 static void retry_uffdio_zeropage(int ufd, 891 struct uffdio_zeropage *uffdio_zeropage) 892 { 893 uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start, 894 uffdio_zeropage->range.len, 895 0); 896 if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) { 897 if (uffdio_zeropage->zeropage != -EEXIST) 898 err("UFFDIO_ZEROPAGE error: %"PRId64, 899 (int64_t)uffdio_zeropage->zeropage); 900 } else { 901 err("UFFDIO_ZEROPAGE error: %"PRId64, 902 (int64_t)uffdio_zeropage->zeropage); 903 } 904 } 905 906 static bool do_uffdio_zeropage(int ufd, bool has_zeropage) 907 { 908 struct uffdio_zeropage uffdio_zeropage = { 0 }; 909 int ret; 910 __s64 res; 911 912 uffdio_zeropage.range.start = (unsigned long) area_dst; 913 uffdio_zeropage.range.len = page_size; 914 uffdio_zeropage.mode = 0; 915 ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage); 916 res = uffdio_zeropage.zeropage; 917 if (ret) { 918 /* real retval in ufdio_zeropage.zeropage */ 919 if (has_zeropage) 920 err("UFFDIO_ZEROPAGE error: %"PRId64, (int64_t)res); 921 else if (res != -EINVAL) 922 err("UFFDIO_ZEROPAGE not -EINVAL"); 923 } else if (has_zeropage) { 924 if (res != page_size) 925 err("UFFDIO_ZEROPAGE unexpected size"); 926 else 927 retry_uffdio_zeropage(ufd, &uffdio_zeropage); 928 return true; 929 } else 930 err("UFFDIO_ZEROPAGE succeeded"); 931 932 return false; 933 } 934 935 /* 936 * Registers a range with MISSING mode only for zeropage test. Return true 937 * if UFFDIO_ZEROPAGE supported, false otherwise. Can't use uffd_register() 938 * because we want to detect .ioctls along the way. 939 */ 940 static bool 941 uffd_register_detect_zeropage(int uffd, void *addr, uint64_t len) 942 { 943 uint64_t ioctls = 0; 944 945 if (uffd_register_with_ioctls(uffd, addr, len, true, 946 false, false, &ioctls)) 947 err("zeropage register fail"); 948 949 return ioctls & (1 << _UFFDIO_ZEROPAGE); 950 } 951 952 /* exercise UFFDIO_ZEROPAGE */ 953 static void uffd_zeropage_test(uffd_test_args_t *args) 954 { 955 bool has_zeropage; 956 int i; 957 958 has_zeropage = uffd_register_detect_zeropage(uffd, area_dst, page_size); 959 if (area_dst_alias) 960 /* Ignore the retval; we already have it */ 961 uffd_register_detect_zeropage(uffd, area_dst_alias, page_size); 962 963 if (do_uffdio_zeropage(uffd, has_zeropage)) 964 for (i = 0; i < page_size; i++) 965 if (area_dst[i] != 0) 966 err("data non-zero at offset %d\n", i); 967 968 if (uffd_unregister(uffd, area_dst, page_size)) 969 err("unregister"); 970 971 if (area_dst_alias && uffd_unregister(uffd, area_dst_alias, page_size)) 972 err("unregister"); 973 974 uffd_test_pass(); 975 } 976 977 static void uffd_register_poison(int uffd, void *addr, uint64_t len) 978 { 979 uint64_t ioctls = 0; 980 uint64_t expected = (1 << _UFFDIO_COPY) | (1 << _UFFDIO_POISON); 981 982 if (uffd_register_with_ioctls(uffd, addr, len, true, 983 false, false, &ioctls)) 984 err("poison register fail"); 985 986 if ((ioctls & expected) != expected) 987 err("registered area doesn't support COPY and POISON ioctls"); 988 } 989 990 static void do_uffdio_poison(int uffd, unsigned long offset) 991 { 992 struct uffdio_poison uffdio_poison = { 0 }; 993 int ret; 994 __s64 res; 995 996 uffdio_poison.range.start = (unsigned long) area_dst + offset; 997 uffdio_poison.range.len = page_size; 998 uffdio_poison.mode = 0; 999 ret = ioctl(uffd, UFFDIO_POISON, &uffdio_poison); 1000 res = uffdio_poison.updated; 1001 1002 if (ret) 1003 err("UFFDIO_POISON error: %"PRId64, (int64_t)res); 1004 else if (res != page_size) 1005 err("UFFDIO_POISON unexpected size: %"PRId64, (int64_t)res); 1006 } 1007 1008 static void uffd_poison_handle_fault( 1009 struct uffd_msg *msg, struct uffd_args *args) 1010 { 1011 unsigned long offset; 1012 1013 if (msg->event != UFFD_EVENT_PAGEFAULT) 1014 err("unexpected msg event %u", msg->event); 1015 1016 if (msg->arg.pagefault.flags & 1017 (UFFD_PAGEFAULT_FLAG_WP | UFFD_PAGEFAULT_FLAG_MINOR)) 1018 err("unexpected fault type %llu", msg->arg.pagefault.flags); 1019 1020 offset = (char *)(unsigned long)msg->arg.pagefault.address - area_dst; 1021 offset &= ~(page_size-1); 1022 1023 /* Odd pages -> copy zeroed page; even pages -> poison. */ 1024 if (offset & page_size) 1025 copy_page(uffd, offset, false); 1026 else 1027 do_uffdio_poison(uffd, offset); 1028 } 1029 1030 static void uffd_poison_test(uffd_test_args_t *targs) 1031 { 1032 pthread_t uffd_mon; 1033 char c; 1034 struct uffd_args args = { 0 }; 1035 struct sigaction act = { 0 }; 1036 unsigned long nr_sigbus = 0; 1037 unsigned long nr; 1038 1039 fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK); 1040 1041 uffd_register_poison(uffd, area_dst, nr_pages * page_size); 1042 memset(area_src, 0, nr_pages * page_size); 1043 1044 args.handle_fault = uffd_poison_handle_fault; 1045 if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args)) 1046 err("uffd_poll_thread create"); 1047 1048 sigbuf = &jbuf; 1049 act.sa_sigaction = sighndl; 1050 act.sa_flags = SA_SIGINFO; 1051 if (sigaction(SIGBUS, &act, 0)) 1052 err("sigaction"); 1053 1054 for (nr = 0; nr < nr_pages; ++nr) { 1055 unsigned long offset = nr * page_size; 1056 const char *bytes = (const char *) area_dst + offset; 1057 const char *i; 1058 1059 if (sigsetjmp(*sigbuf, 1)) { 1060 /* 1061 * Access below triggered a SIGBUS, which was caught by 1062 * sighndl, which then jumped here. Count this SIGBUS, 1063 * and move on to next page. 1064 */ 1065 ++nr_sigbus; 1066 continue; 1067 } 1068 1069 for (i = bytes; i < bytes + page_size; ++i) { 1070 if (*i) 1071 err("nonzero byte in area_dst (%p) at %p: %u", 1072 area_dst, i, *i); 1073 } 1074 } 1075 1076 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c)) 1077 err("pipe write"); 1078 if (pthread_join(uffd_mon, NULL)) 1079 err("pthread_join()"); 1080 1081 if (nr_sigbus != nr_pages / 2) 1082 err("expected to receive %lu SIGBUS, actually received %lu", 1083 nr_pages / 2, nr_sigbus); 1084 1085 uffd_test_pass(); 1086 } 1087 1088 static void 1089 uffd_move_handle_fault_common(struct uffd_msg *msg, struct uffd_args *args, 1090 unsigned long len) 1091 { 1092 unsigned long offset; 1093 1094 if (msg->event != UFFD_EVENT_PAGEFAULT) 1095 err("unexpected msg event %u", msg->event); 1096 1097 if (msg->arg.pagefault.flags & 1098 (UFFD_PAGEFAULT_FLAG_WP | UFFD_PAGEFAULT_FLAG_MINOR | UFFD_PAGEFAULT_FLAG_WRITE)) 1099 err("unexpected fault type %llu", msg->arg.pagefault.flags); 1100 1101 offset = (char *)(unsigned long)msg->arg.pagefault.address - area_dst; 1102 offset &= ~(len-1); 1103 1104 if (move_page(uffd, offset, len)) 1105 args->missing_faults++; 1106 } 1107 1108 static void uffd_move_handle_fault(struct uffd_msg *msg, 1109 struct uffd_args *args) 1110 { 1111 uffd_move_handle_fault_common(msg, args, page_size); 1112 } 1113 1114 static void uffd_move_pmd_handle_fault(struct uffd_msg *msg, 1115 struct uffd_args *args) 1116 { 1117 uffd_move_handle_fault_common(msg, args, read_pmd_pagesize()); 1118 } 1119 1120 static void 1121 uffd_move_test_common(uffd_test_args_t *targs, unsigned long chunk_size, 1122 void (*handle_fault)(struct uffd_msg *msg, struct uffd_args *args)) 1123 { 1124 unsigned long nr; 1125 pthread_t uffd_mon; 1126 char c; 1127 unsigned long long count; 1128 struct uffd_args args = { 0 }; 1129 char *orig_area_src, *orig_area_dst; 1130 unsigned long step_size, step_count; 1131 unsigned long src_offs = 0; 1132 unsigned long dst_offs = 0; 1133 1134 /* Prevent source pages from being mapped more than once */ 1135 if (madvise(area_src, nr_pages * page_size, MADV_DONTFORK)) 1136 err("madvise(MADV_DONTFORK) failure"); 1137 1138 if (uffd_register(uffd, area_dst, nr_pages * page_size, 1139 true, false, false)) 1140 err("register failure"); 1141 1142 args.handle_fault = handle_fault; 1143 if (pthread_create(&uffd_mon, NULL, uffd_poll_thread, &args)) 1144 err("uffd_poll_thread create"); 1145 1146 step_size = chunk_size / page_size; 1147 step_count = nr_pages / step_size; 1148 1149 if (chunk_size > page_size) { 1150 char *aligned_src = ALIGN_UP(area_src, chunk_size); 1151 char *aligned_dst = ALIGN_UP(area_dst, chunk_size); 1152 1153 if (aligned_src != area_src || aligned_dst != area_dst) { 1154 src_offs = (aligned_src - area_src) / page_size; 1155 dst_offs = (aligned_dst - area_dst) / page_size; 1156 step_count--; 1157 } 1158 orig_area_src = area_src; 1159 orig_area_dst = area_dst; 1160 area_src = aligned_src; 1161 area_dst = aligned_dst; 1162 } 1163 1164 /* 1165 * Read each of the pages back using the UFFD-registered mapping. We 1166 * expect that the first time we touch a page, it will result in a missing 1167 * fault. uffd_poll_thread will resolve the fault by moving source 1168 * page to destination. 1169 */ 1170 for (nr = 0; nr < step_count * step_size; nr += step_size) { 1171 unsigned long i; 1172 1173 /* Check area_src content */ 1174 for (i = 0; i < step_size; i++) { 1175 count = *area_count(area_src, nr + i); 1176 if (count != count_verify[src_offs + nr + i]) 1177 err("nr %lu source memory invalid %llu %llu\n", 1178 nr + i, count, count_verify[src_offs + nr + i]); 1179 } 1180 1181 /* Faulting into area_dst should move the page or the huge page */ 1182 for (i = 0; i < step_size; i++) { 1183 count = *area_count(area_dst, nr + i); 1184 if (count != count_verify[dst_offs + nr + i]) 1185 err("nr %lu memory corruption %llu %llu\n", 1186 nr, count, count_verify[dst_offs + nr + i]); 1187 } 1188 1189 /* Re-check area_src content which should be empty */ 1190 for (i = 0; i < step_size; i++) { 1191 count = *area_count(area_src, nr + i); 1192 if (count != 0) 1193 err("nr %lu move failed %llu %llu\n", 1194 nr, count, count_verify[src_offs + nr + i]); 1195 } 1196 } 1197 if (step_size > page_size) { 1198 area_src = orig_area_src; 1199 area_dst = orig_area_dst; 1200 } 1201 1202 if (write(pipefd[1], &c, sizeof(c)) != sizeof(c)) 1203 err("pipe write"); 1204 if (pthread_join(uffd_mon, NULL)) 1205 err("join() failed"); 1206 1207 if (args.missing_faults != step_count || args.minor_faults != 0) 1208 uffd_test_fail("stats check error"); 1209 else 1210 uffd_test_pass(); 1211 } 1212 1213 static void uffd_move_test(uffd_test_args_t *targs) 1214 { 1215 uffd_move_test_common(targs, page_size, uffd_move_handle_fault); 1216 } 1217 1218 static void uffd_move_pmd_test(uffd_test_args_t *targs) 1219 { 1220 if (madvise(area_dst, nr_pages * page_size, MADV_HUGEPAGE)) 1221 err("madvise(MADV_HUGEPAGE) failure"); 1222 uffd_move_test_common(targs, read_pmd_pagesize(), 1223 uffd_move_pmd_handle_fault); 1224 } 1225 1226 static void uffd_move_pmd_split_test(uffd_test_args_t *targs) 1227 { 1228 if (madvise(area_dst, nr_pages * page_size, MADV_NOHUGEPAGE)) 1229 err("madvise(MADV_NOHUGEPAGE) failure"); 1230 uffd_move_test_common(targs, read_pmd_pagesize(), 1231 uffd_move_pmd_handle_fault); 1232 } 1233 1234 static int prevent_hugepages(const char **errmsg) 1235 { 1236 /* This should be done before source area is populated */ 1237 if (madvise(area_src, nr_pages * page_size, MADV_NOHUGEPAGE)) { 1238 /* Ignore only if CONFIG_TRANSPARENT_HUGEPAGE=n */ 1239 if (errno != EINVAL) { 1240 if (errmsg) 1241 *errmsg = "madvise(MADV_NOHUGEPAGE) failed"; 1242 return -errno; 1243 } 1244 } 1245 return 0; 1246 } 1247 1248 static int request_hugepages(const char **errmsg) 1249 { 1250 /* This should be done before source area is populated */ 1251 if (madvise(area_src, nr_pages * page_size, MADV_HUGEPAGE)) { 1252 if (errmsg) { 1253 *errmsg = (errno == EINVAL) ? 1254 "CONFIG_TRANSPARENT_HUGEPAGE is not set" : 1255 "madvise(MADV_HUGEPAGE) failed"; 1256 } 1257 return -errno; 1258 } 1259 return 0; 1260 } 1261 1262 struct uffd_test_case_ops uffd_move_test_case_ops = { 1263 .post_alloc = prevent_hugepages, 1264 }; 1265 1266 struct uffd_test_case_ops uffd_move_test_pmd_case_ops = { 1267 .post_alloc = request_hugepages, 1268 }; 1269 1270 /* 1271 * Test the returned uffdio_register.ioctls with different register modes. 1272 * Note that _UFFDIO_ZEROPAGE is tested separately in the zeropage test. 1273 */ 1274 static void 1275 do_register_ioctls_test(uffd_test_args_t *args, bool miss, bool wp, bool minor) 1276 { 1277 uint64_t ioctls = 0, expected = BIT_ULL(_UFFDIO_WAKE); 1278 mem_type_t *mem_type = args->mem_type; 1279 int ret; 1280 1281 ret = uffd_register_with_ioctls(uffd, area_dst, page_size, 1282 miss, wp, minor, &ioctls); 1283 1284 /* 1285 * Handle special cases of UFFDIO_REGISTER here where it should 1286 * just fail with -EINVAL first.. 1287 * 1288 * Case 1: register MINOR on anon 1289 * Case 2: register with no mode selected 1290 */ 1291 if ((minor && (mem_type->mem_flag == MEM_ANON)) || 1292 (!miss && !wp && !minor)) { 1293 if (ret != -EINVAL) 1294 err("register (miss=%d, wp=%d, minor=%d) failed " 1295 "with wrong errno=%d", miss, wp, minor, ret); 1296 return; 1297 } 1298 1299 /* UFFDIO_REGISTER should succeed, then check ioctls returned */ 1300 if (miss) 1301 expected |= BIT_ULL(_UFFDIO_COPY); 1302 if (wp) 1303 expected |= BIT_ULL(_UFFDIO_WRITEPROTECT); 1304 if (minor) 1305 expected |= BIT_ULL(_UFFDIO_CONTINUE); 1306 1307 if ((ioctls & expected) != expected) 1308 err("unexpected uffdio_register.ioctls " 1309 "(miss=%d, wp=%d, minor=%d): expected=0x%"PRIx64", " 1310 "returned=0x%"PRIx64, miss, wp, minor, expected, ioctls); 1311 1312 if (uffd_unregister(uffd, area_dst, page_size)) 1313 err("unregister"); 1314 } 1315 1316 static void uffd_register_ioctls_test(uffd_test_args_t *args) 1317 { 1318 int miss, wp, minor; 1319 1320 for (miss = 0; miss <= 1; miss++) 1321 for (wp = 0; wp <= 1; wp++) 1322 for (minor = 0; minor <= 1; minor++) 1323 do_register_ioctls_test(args, miss, wp, minor); 1324 1325 uffd_test_pass(); 1326 } 1327 1328 uffd_test_case_t uffd_tests[] = { 1329 { 1330 /* Test returned uffdio_register.ioctls. */ 1331 .name = "register-ioctls", 1332 .uffd_fn = uffd_register_ioctls_test, 1333 .mem_targets = MEM_ALL, 1334 .uffd_feature_required = UFFD_FEATURE_MISSING_HUGETLBFS | 1335 UFFD_FEATURE_MISSING_SHMEM | 1336 UFFD_FEATURE_PAGEFAULT_FLAG_WP | 1337 UFFD_FEATURE_WP_HUGETLBFS_SHMEM | 1338 UFFD_FEATURE_MINOR_HUGETLBFS | 1339 UFFD_FEATURE_MINOR_SHMEM, 1340 }, 1341 { 1342 .name = "zeropage", 1343 .uffd_fn = uffd_zeropage_test, 1344 .mem_targets = MEM_ALL, 1345 .uffd_feature_required = 0, 1346 }, 1347 { 1348 .name = "move", 1349 .uffd_fn = uffd_move_test, 1350 .mem_targets = MEM_ANON, 1351 .uffd_feature_required = UFFD_FEATURE_MOVE, 1352 .test_case_ops = &uffd_move_test_case_ops, 1353 }, 1354 { 1355 .name = "move-pmd", 1356 .uffd_fn = uffd_move_pmd_test, 1357 .mem_targets = MEM_ANON, 1358 .uffd_feature_required = UFFD_FEATURE_MOVE, 1359 .test_case_ops = &uffd_move_test_pmd_case_ops, 1360 }, 1361 { 1362 .name = "move-pmd-split", 1363 .uffd_fn = uffd_move_pmd_split_test, 1364 .mem_targets = MEM_ANON, 1365 .uffd_feature_required = UFFD_FEATURE_MOVE, 1366 .test_case_ops = &uffd_move_test_pmd_case_ops, 1367 }, 1368 { 1369 .name = "wp-fork", 1370 .uffd_fn = uffd_wp_fork_test, 1371 .mem_targets = MEM_ALL, 1372 .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | 1373 UFFD_FEATURE_WP_HUGETLBFS_SHMEM, 1374 }, 1375 { 1376 .name = "wp-fork-with-event", 1377 .uffd_fn = uffd_wp_fork_with_event_test, 1378 .mem_targets = MEM_ALL, 1379 .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | 1380 UFFD_FEATURE_WP_HUGETLBFS_SHMEM | 1381 /* when set, child process should inherit uffd-wp bits */ 1382 UFFD_FEATURE_EVENT_FORK, 1383 }, 1384 { 1385 .name = "wp-fork-pin", 1386 .uffd_fn = uffd_wp_fork_pin_test, 1387 .mem_targets = MEM_ALL, 1388 .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | 1389 UFFD_FEATURE_WP_HUGETLBFS_SHMEM, 1390 }, 1391 { 1392 .name = "wp-fork-pin-with-event", 1393 .uffd_fn = uffd_wp_fork_pin_with_event_test, 1394 .mem_targets = MEM_ALL, 1395 .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | 1396 UFFD_FEATURE_WP_HUGETLBFS_SHMEM | 1397 /* when set, child process should inherit uffd-wp bits */ 1398 UFFD_FEATURE_EVENT_FORK, 1399 }, 1400 { 1401 .name = "wp-unpopulated", 1402 .uffd_fn = uffd_wp_unpopulated_test, 1403 .mem_targets = MEM_ANON, 1404 .uffd_feature_required = 1405 UFFD_FEATURE_PAGEFAULT_FLAG_WP | UFFD_FEATURE_WP_UNPOPULATED, 1406 }, 1407 { 1408 .name = "minor", 1409 .uffd_fn = uffd_minor_test, 1410 .mem_targets = MEM_SHMEM | MEM_HUGETLB, 1411 .uffd_feature_required = 1412 UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM, 1413 }, 1414 { 1415 .name = "minor-wp", 1416 .uffd_fn = uffd_minor_wp_test, 1417 .mem_targets = MEM_SHMEM | MEM_HUGETLB, 1418 .uffd_feature_required = 1419 UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM | 1420 UFFD_FEATURE_PAGEFAULT_FLAG_WP | 1421 /* 1422 * HACK: here we leveraged WP_UNPOPULATED to detect whether 1423 * minor mode supports wr-protect. There's no feature flag 1424 * for it so this is the best we can test against. 1425 */ 1426 UFFD_FEATURE_WP_UNPOPULATED, 1427 }, 1428 { 1429 .name = "minor-collapse", 1430 .uffd_fn = uffd_minor_collapse_test, 1431 /* MADV_COLLAPSE only works with shmem */ 1432 .mem_targets = MEM_SHMEM, 1433 /* We can't test MADV_COLLAPSE, so try our luck */ 1434 .uffd_feature_required = UFFD_FEATURE_MINOR_SHMEM, 1435 }, 1436 { 1437 .name = "sigbus", 1438 .uffd_fn = uffd_sigbus_test, 1439 .mem_targets = MEM_ALL, 1440 .uffd_feature_required = UFFD_FEATURE_SIGBUS | 1441 UFFD_FEATURE_EVENT_FORK, 1442 }, 1443 { 1444 .name = "sigbus-wp", 1445 .uffd_fn = uffd_sigbus_wp_test, 1446 .mem_targets = MEM_ALL, 1447 .uffd_feature_required = UFFD_FEATURE_SIGBUS | 1448 UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_PAGEFAULT_FLAG_WP | 1449 UFFD_FEATURE_WP_HUGETLBFS_SHMEM, 1450 }, 1451 { 1452 .name = "events", 1453 .uffd_fn = uffd_events_test, 1454 .mem_targets = MEM_ALL, 1455 .uffd_feature_required = UFFD_FEATURE_EVENT_FORK | 1456 UFFD_FEATURE_EVENT_REMAP | UFFD_FEATURE_EVENT_REMOVE, 1457 }, 1458 { 1459 .name = "events-wp", 1460 .uffd_fn = uffd_events_wp_test, 1461 .mem_targets = MEM_ALL, 1462 .uffd_feature_required = UFFD_FEATURE_EVENT_FORK | 1463 UFFD_FEATURE_EVENT_REMAP | UFFD_FEATURE_EVENT_REMOVE | 1464 UFFD_FEATURE_PAGEFAULT_FLAG_WP | 1465 UFFD_FEATURE_WP_HUGETLBFS_SHMEM, 1466 }, 1467 { 1468 .name = "poison", 1469 .uffd_fn = uffd_poison_test, 1470 .mem_targets = MEM_ALL, 1471 .uffd_feature_required = UFFD_FEATURE_POISON, 1472 }, 1473 }; 1474 1475 static void usage(const char *prog) 1476 { 1477 printf("usage: %s [-f TESTNAME]\n", prog); 1478 puts(""); 1479 puts(" -f: test name to filter (e.g., event)"); 1480 puts(" -h: show the help msg"); 1481 puts(" -l: list tests only"); 1482 puts(""); 1483 exit(KSFT_FAIL); 1484 } 1485 1486 int main(int argc, char *argv[]) 1487 { 1488 int n_tests = sizeof(uffd_tests) / sizeof(uffd_test_case_t); 1489 int n_mems = sizeof(mem_types) / sizeof(mem_type_t); 1490 const char *test_filter = NULL; 1491 bool list_only = false; 1492 uffd_test_case_t *test; 1493 mem_type_t *mem_type; 1494 uffd_test_args_t args; 1495 const char *errmsg; 1496 int has_uffd, opt; 1497 int i, j; 1498 1499 while ((opt = getopt(argc, argv, "f:hl")) != -1) { 1500 switch (opt) { 1501 case 'f': 1502 test_filter = optarg; 1503 break; 1504 case 'l': 1505 list_only = true; 1506 break; 1507 case 'h': 1508 default: 1509 /* Unknown */ 1510 usage(argv[0]); 1511 break; 1512 } 1513 } 1514 1515 if (!test_filter && !list_only) { 1516 has_uffd = test_uffd_api(false); 1517 has_uffd |= test_uffd_api(true); 1518 1519 if (!has_uffd) { 1520 printf("Userfaultfd not supported or unprivileged, skip all tests\n"); 1521 exit(KSFT_SKIP); 1522 } 1523 } 1524 1525 for (i = 0; i < n_tests; i++) { 1526 test = &uffd_tests[i]; 1527 if (test_filter && !strstr(test->name, test_filter)) 1528 continue; 1529 if (list_only) { 1530 printf("%s\n", test->name); 1531 continue; 1532 } 1533 for (j = 0; j < n_mems; j++) { 1534 mem_type = &mem_types[j]; 1535 if (!(test->mem_targets & mem_type->mem_flag)) 1536 continue; 1537 1538 uffd_test_start("%s on %s", test->name, mem_type->name); 1539 if ((mem_type->mem_flag == MEM_HUGETLB || 1540 mem_type->mem_flag == MEM_HUGETLB_PRIVATE) && 1541 (default_huge_page_size() == 0)) { 1542 uffd_test_skip("huge page size is 0, feature missing?"); 1543 continue; 1544 } 1545 if (!uffd_feature_supported(test)) { 1546 uffd_test_skip("feature missing"); 1547 continue; 1548 } 1549 if (uffd_setup_environment(&args, test, mem_type, 1550 &errmsg)) { 1551 uffd_test_skip(errmsg); 1552 continue; 1553 } 1554 test->uffd_fn(&args); 1555 uffd_test_ctx_clear(); 1556 } 1557 } 1558 1559 if (!list_only) 1560 uffd_test_report(); 1561 1562 return ksft_get_fail_cnt() ? KSFT_FAIL : KSFT_PASS; 1563 } 1564 1565