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