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