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