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 
27 #define DEFAULT_LENGTH_MB 10UL
28 #define MB_TO_BYTES(x) (x * 1024 * 1024)
29 
30 #define PROTECTION (PROT_READ | PROT_WRITE | PROT_EXEC)
31 #define FLAGS (MAP_SHARED | MAP_ANONYMOUS)
32 
33 static void check_bytes(char *addr)
34 {
35 	printf("First hex is %x\n", *((unsigned int *)addr));
36 }
37 
38 static void write_bytes(char *addr, size_t len)
39 {
40 	unsigned long i;
41 
42 	for (i = 0; i < len; i++)
43 		*(addr + i) = (char)i;
44 }
45 
46 static int read_bytes(char *addr, size_t len)
47 {
48 	unsigned long i;
49 
50 	check_bytes(addr);
51 	for (i = 0; i < len; i++)
52 		if (*(addr + i) != (char)i) {
53 			printf("Mismatch at %lu\n", i);
54 			return 1;
55 		}
56 	return 0;
57 }
58 
59 static void register_region_with_uffd(char *addr, size_t len)
60 {
61 	long uffd; /* userfaultfd file descriptor */
62 	struct uffdio_api uffdio_api;
63 	struct uffdio_register uffdio_register;
64 
65 	/* Create and enable userfaultfd object. */
66 
67 	uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
68 	if (uffd == -1) {
69 		perror("userfaultfd");
70 		exit(1);
71 	}
72 
73 	uffdio_api.api = UFFD_API;
74 	uffdio_api.features = 0;
75 	if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1) {
76 		perror("ioctl-UFFDIO_API");
77 		exit(1);
78 	}
79 
80 	/* Create a private anonymous mapping. The memory will be
81 	 * demand-zero paged--that is, not yet allocated. When we
82 	 * actually touch the memory, it will be allocated via
83 	 * the userfaultfd.
84 	 */
85 
86 	addr = mmap(NULL, len, PROT_READ | PROT_WRITE,
87 		    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
88 	if (addr == MAP_FAILED) {
89 		perror("mmap");
90 		exit(1);
91 	}
92 
93 	printf("Address returned by mmap() = %p\n", addr);
94 
95 	/* Register the memory range of the mapping we just created for
96 	 * handling by the userfaultfd object. In mode, we request to track
97 	 * missing pages (i.e., pages that have not yet been faulted in).
98 	 */
99 
100 	uffdio_register.range.start = (unsigned long)addr;
101 	uffdio_register.range.len = len;
102 	uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
103 	if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1) {
104 		perror("ioctl-UFFDIO_REGISTER");
105 		exit(1);
106 	}
107 }
108 
109 int main(int argc, char *argv[])
110 {
111 	size_t length = 0;
112 	int ret = 0, fd;
113 
114 	if (argc >= 2 && !strcmp(argv[1], "-h")) {
115 		printf("Usage: %s [length_in_MB]\n", argv[0]);
116 		exit(1);
117 	}
118 
119 	/* Read memory length as the first arg if valid, otherwise fallback to
120 	 * the default length.
121 	 */
122 	if (argc >= 2)
123 		length = (size_t)atoi(argv[1]);
124 	else
125 		length = DEFAULT_LENGTH_MB;
126 
127 	length = MB_TO_BYTES(length);
128 	fd = memfd_create(argv[0], MFD_HUGETLB);
129 	if (fd < 0) {
130 		perror("Open failed");
131 		exit(1);
132 	}
133 
134 	/* mmap to a PUD aligned address to hopefully trigger pmd sharing. */
135 	unsigned long suggested_addr = 0x7eaa40000000;
136 	void *haddr = mmap((void *)suggested_addr, length, PROTECTION,
137 			   MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0);
138 	printf("Map haddr: Returned address is %p\n", haddr);
139 	if (haddr == MAP_FAILED) {
140 		perror("mmap1");
141 		exit(1);
142 	}
143 
144 	/* mmap again to a dummy address to hopefully trigger pmd sharing. */
145 	suggested_addr = 0x7daa40000000;
146 	void *daddr = mmap((void *)suggested_addr, length, PROTECTION,
147 			   MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0);
148 	printf("Map daddr: Returned address is %p\n", daddr);
149 	if (daddr == MAP_FAILED) {
150 		perror("mmap3");
151 		exit(1);
152 	}
153 
154 	suggested_addr = 0x7faa40000000;
155 	void *vaddr =
156 		mmap((void *)suggested_addr, length, PROTECTION, FLAGS, -1, 0);
157 	printf("Map vaddr: Returned address is %p\n", vaddr);
158 	if (vaddr == MAP_FAILED) {
159 		perror("mmap2");
160 		exit(1);
161 	}
162 
163 	register_region_with_uffd(haddr, length);
164 
165 	void *addr = mremap(haddr, length, length,
166 			    MREMAP_MAYMOVE | MREMAP_FIXED, vaddr);
167 	if (addr == MAP_FAILED) {
168 		perror("mremap");
169 		exit(1);
170 	}
171 
172 	printf("Mremap: Returned address is %p\n", addr);
173 	check_bytes(addr);
174 	write_bytes(addr, length);
175 	ret = read_bytes(addr, length);
176 
177 	munmap(addr, length);
178 
179 	addr = mremap(addr, length, length, 0);
180 	if (addr != MAP_FAILED) {
181 		printf("mremap: Expected failure, but call succeeded\n");
182 		exit(1);
183 	}
184 
185 	close(fd);
186 
187 	return ret;
188 }
189