1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * hugepage-mremap: 4 * 5 * Example of remapping huge page memory in a user application using the 6 * mremap system call. The path to a file in a hugetlbfs filesystem must 7 * be passed as the last argument to this test. The amount of memory used 8 * by this test in MBs can optionally be passed as an argument. If no memory 9 * amount is passed, the default amount is 10MB. 10 * 11 * To make sure the test triggers pmd sharing and goes through the 'unshare' 12 * path in the mremap code use 1GB (1024) or more. 13 */ 14 15 #define _GNU_SOURCE 16 #include <stdlib.h> 17 #include <stdio.h> 18 #include <unistd.h> 19 #include <sys/mman.h> 20 #include <errno.h> 21 #include <fcntl.h> /* Definition of O_* constants */ 22 #include <sys/syscall.h> /* Definition of SYS_* constants */ 23 #include <linux/userfaultfd.h> 24 #include <sys/ioctl.h> 25 #include <string.h> 26 #include <stdbool.h> 27 #include "../kselftest.h" 28 #include "vm_util.h" 29 30 #define DEFAULT_LENGTH_MB 10UL 31 #define MB_TO_BYTES(x) (x * 1024 * 1024) 32 33 #define PROTECTION (PROT_READ | PROT_WRITE | PROT_EXEC) 34 #define FLAGS (MAP_SHARED | MAP_ANONYMOUS) 35 36 static void check_bytes(char *addr) 37 { 38 ksft_print_msg("First hex is %x\n", *((unsigned int *)addr)); 39 } 40 41 static void write_bytes(char *addr, size_t len) 42 { 43 unsigned long i; 44 45 for (i = 0; i < len; i++) 46 *(addr + i) = (char)i; 47 } 48 49 static int read_bytes(char *addr, size_t len) 50 { 51 unsigned long i; 52 53 check_bytes(addr); 54 for (i = 0; i < len; i++) 55 if (*(addr + i) != (char)i) { 56 ksft_print_msg("Mismatch at %lu\n", i); 57 return 1; 58 } 59 return 0; 60 } 61 62 static void register_region_with_uffd(char *addr, size_t len) 63 { 64 long uffd; /* userfaultfd file descriptor */ 65 struct uffdio_api uffdio_api; 66 67 /* Create and enable userfaultfd object. */ 68 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); 69 if (uffd == -1) { 70 switch (errno) { 71 case EPERM: 72 ksft_exit_skip("Insufficient permissions, try running as root.\n"); 73 break; 74 case ENOSYS: 75 ksft_exit_skip("userfaultfd is not supported/not enabled.\n"); 76 break; 77 default: 78 ksft_exit_fail_msg("userfaultfd failed with %s\n", strerror(errno)); 79 break; 80 } 81 } 82 83 uffdio_api.api = UFFD_API; 84 uffdio_api.features = 0; 85 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1) 86 ksft_exit_fail_msg("ioctl-UFFDIO_API: %s\n", strerror(errno)); 87 88 /* Create a private anonymous mapping. The memory will be 89 * demand-zero paged--that is, not yet allocated. When we 90 * actually touch the memory, it will be allocated via 91 * the userfaultfd. 92 */ 93 94 addr = mmap(NULL, len, PROT_READ | PROT_WRITE, 95 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 96 if (addr == MAP_FAILED) 97 ksft_exit_fail_msg("mmap: %s\n", strerror(errno)); 98 99 ksft_print_msg("Address returned by mmap() = %p\n", addr); 100 101 /* Register the memory range of the mapping we just created for 102 * handling by the userfaultfd object. In mode, we request to track 103 * missing pages (i.e., pages that have not yet been faulted in). 104 */ 105 if (uffd_register(uffd, addr, len, true, false, false)) 106 ksft_exit_fail_msg("ioctl-UFFDIO_REGISTER: %s\n", strerror(errno)); 107 } 108 109 int main(int argc, char *argv[]) 110 { 111 size_t length = 0; 112 int ret = 0, fd; 113 114 ksft_print_header(); 115 ksft_set_plan(1); 116 117 if (argc >= 2 && !strcmp(argv[1], "-h")) 118 ksft_exit_fail_msg("Usage: %s [length_in_MB]\n", argv[0]); 119 120 /* Read memory length as the first arg if valid, otherwise fallback to 121 * the default length. 122 */ 123 if (argc >= 2) 124 length = (size_t)atoi(argv[1]); 125 else 126 length = DEFAULT_LENGTH_MB; 127 128 length = MB_TO_BYTES(length); 129 fd = memfd_create(argv[0], MFD_HUGETLB); 130 if (fd < 0) 131 ksft_exit_fail_msg("Open failed: %s\n", strerror(errno)); 132 133 /* mmap to a PUD aligned address to hopefully trigger pmd sharing. */ 134 unsigned long suggested_addr = 0x7eaa40000000; 135 void *haddr = mmap((void *)suggested_addr, length, PROTECTION, 136 MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0); 137 ksft_print_msg("Map haddr: Returned address is %p\n", haddr); 138 if (haddr == MAP_FAILED) 139 ksft_exit_fail_msg("mmap1: %s\n", strerror(errno)); 140 141 /* mmap again to a dummy address to hopefully trigger pmd sharing. */ 142 suggested_addr = 0x7daa40000000; 143 void *daddr = mmap((void *)suggested_addr, length, PROTECTION, 144 MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0); 145 ksft_print_msg("Map daddr: Returned address is %p\n", daddr); 146 if (daddr == MAP_FAILED) 147 ksft_exit_fail_msg("mmap3: %s\n", strerror(errno)); 148 149 suggested_addr = 0x7faa40000000; 150 void *vaddr = 151 mmap((void *)suggested_addr, length, PROTECTION, FLAGS, -1, 0); 152 ksft_print_msg("Map vaddr: Returned address is %p\n", vaddr); 153 if (vaddr == MAP_FAILED) 154 ksft_exit_fail_msg("mmap2: %s\n", strerror(errno)); 155 156 register_region_with_uffd(haddr, length); 157 158 void *addr = mremap(haddr, length, length, 159 MREMAP_MAYMOVE | MREMAP_FIXED, vaddr); 160 if (addr == MAP_FAILED) 161 ksft_exit_fail_msg("mremap: %s\n", strerror(errno)); 162 163 ksft_print_msg("Mremap: Returned address is %p\n", addr); 164 check_bytes(addr); 165 write_bytes(addr, length); 166 ret = read_bytes(addr, length); 167 168 munmap(addr, length); 169 170 addr = mremap(addr, length, length, 0); 171 if (addr != MAP_FAILED) 172 ksft_exit_fail_msg("mremap: Expected failure, but call succeeded\n"); 173 174 close(fd); 175 176 ksft_test_result(!ret, "Read same data\n"); 177 ksft_exit(!ret); 178 } 179