1 // SPDX-License-Identifier: GPL-2.0
4 * Tests for mremap w/ MREMAP_DONTUNMAP.
6 * Copyright 2020, Brian Geffon <bgeffon@google.com>
16 #include "../kselftest.h"
18 #ifndef MREMAP_DONTUNMAP
19 #define MREMAP_DONTUNMAP 4
22 unsigned long page_size
;
25 static void dump_maps(void)
29 snprintf(cmd
, sizeof(cmd
), "cat /proc/%d/maps", getpid());
33 #define BUG_ON(condition, description) \
36 fprintf(stderr, "[FAIL]\t%s():%d\t%s:%s\n", __func__, \
37 __LINE__, (description), strerror(errno)); \
43 // Try a simple operation for to "test" for kernel support this prevents
44 // reporting tests as failed when it's run on an older kernel.
45 static int kernel_support_for_mremap_dontunmap()
48 unsigned long num_pages
= 1;
49 void *source_mapping
= mmap(NULL
, num_pages
* page_size
, PROT_NONE
,
50 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
51 BUG_ON(source_mapping
== MAP_FAILED
, "mmap");
53 // This simple remap should only fail if MREMAP_DONTUNMAP isn't
56 mremap(source_mapping
, num_pages
* page_size
, num_pages
* page_size
,
57 MREMAP_DONTUNMAP
| MREMAP_MAYMOVE
, 0);
58 if (dest_mapping
== MAP_FAILED
) {
61 BUG_ON(munmap(dest_mapping
, num_pages
* page_size
) == -1,
62 "unable to unmap destination mapping");
65 BUG_ON(munmap(source_mapping
, num_pages
* page_size
) == -1,
66 "unable to unmap source mapping");
70 // This helper will just validate that an entire mapping contains the expected
72 static int check_region_contains_byte(void *addr
, unsigned long size
, char byte
)
74 BUG_ON(size
& (page_size
- 1),
75 "check_region_contains_byte expects page multiples");
76 BUG_ON((unsigned long)addr
& (page_size
- 1),
77 "check_region_contains_byte expects page alignment");
79 memset(page_buffer
, byte
, page_size
);
81 unsigned long num_pages
= size
/ page_size
;
84 // Compare each page checking that it contains our expected byte.
85 for (i
= 0; i
< num_pages
; ++i
) {
87 memcmp(addr
+ (i
* page_size
), page_buffer
, page_size
);
96 // this test validates that MREMAP_DONTUNMAP moves the pagetables while leaving
97 // the source mapping mapped.
98 static void mremap_dontunmap_simple()
100 unsigned long num_pages
= 5;
102 void *source_mapping
=
103 mmap(NULL
, num_pages
* page_size
, PROT_READ
| PROT_WRITE
,
104 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
105 BUG_ON(source_mapping
== MAP_FAILED
, "mmap");
107 memset(source_mapping
, 'a', num_pages
* page_size
);
109 // Try to just move the whole mapping anywhere (not fixed).
111 mremap(source_mapping
, num_pages
* page_size
, num_pages
* page_size
,
112 MREMAP_DONTUNMAP
| MREMAP_MAYMOVE
, NULL
);
113 BUG_ON(dest_mapping
== MAP_FAILED
, "mremap");
115 // Validate that the pages have been moved, we know they were moved if
116 // the dest_mapping contains a's.
117 BUG_ON(check_region_contains_byte
118 (dest_mapping
, num_pages
* page_size
, 'a') != 0,
119 "pages did not migrate");
120 BUG_ON(check_region_contains_byte
121 (source_mapping
, num_pages
* page_size
, 0) != 0,
122 "source should have no ptes");
124 BUG_ON(munmap(dest_mapping
, num_pages
* page_size
) == -1,
125 "unable to unmap destination mapping");
126 BUG_ON(munmap(source_mapping
, num_pages
* page_size
) == -1,
127 "unable to unmap source mapping");
130 // This test validates MREMAP_DONTUNMAP will move page tables to a specific
131 // destination using MREMAP_FIXED, also while validating that the source
133 static void mremap_dontunmap_simple_fixed()
135 unsigned long num_pages
= 5;
137 // Since we want to guarantee that we can remap to a point, we will
138 // create a mapping up front.
140 mmap(NULL
, num_pages
* page_size
, PROT_READ
| PROT_WRITE
,
141 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
142 BUG_ON(dest_mapping
== MAP_FAILED
, "mmap");
143 memset(dest_mapping
, 'X', num_pages
* page_size
);
145 void *source_mapping
=
146 mmap(NULL
, num_pages
* page_size
, PROT_READ
| PROT_WRITE
,
147 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
148 BUG_ON(source_mapping
== MAP_FAILED
, "mmap");
149 memset(source_mapping
, 'a', num_pages
* page_size
);
151 void *remapped_mapping
=
152 mremap(source_mapping
, num_pages
* page_size
, num_pages
* page_size
,
153 MREMAP_FIXED
| MREMAP_DONTUNMAP
| MREMAP_MAYMOVE
,
155 BUG_ON(remapped_mapping
== MAP_FAILED
, "mremap");
156 BUG_ON(remapped_mapping
!= dest_mapping
,
157 "mremap should have placed the remapped mapping at dest_mapping");
159 // The dest mapping will have been unmap by mremap so we expect the Xs
160 // to be gone and replaced with a's.
161 BUG_ON(check_region_contains_byte
162 (dest_mapping
, num_pages
* page_size
, 'a') != 0,
163 "pages did not migrate");
165 // And the source mapping will have had its ptes dropped.
166 BUG_ON(check_region_contains_byte
167 (source_mapping
, num_pages
* page_size
, 0) != 0,
168 "source should have no ptes");
170 BUG_ON(munmap(dest_mapping
, num_pages
* page_size
) == -1,
171 "unable to unmap destination mapping");
172 BUG_ON(munmap(source_mapping
, num_pages
* page_size
) == -1,
173 "unable to unmap source mapping");
176 // This test validates that we can MREMAP_DONTUNMAP for a portion of an
178 static void mremap_dontunmap_partial_mapping()
189 * With the destination mapping containing 5 pages of As.
194 unsigned long num_pages
= 10;
195 void *source_mapping
=
196 mmap(NULL
, num_pages
* page_size
, PROT_READ
| PROT_WRITE
,
197 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
198 BUG_ON(source_mapping
== MAP_FAILED
, "mmap");
199 memset(source_mapping
, 'a', num_pages
* page_size
);
201 // We will grab the last 5 pages of the source and move them.
203 mremap(source_mapping
+ (5 * page_size
), 5 * page_size
,
205 MREMAP_DONTUNMAP
| MREMAP_MAYMOVE
, NULL
);
206 BUG_ON(dest_mapping
== MAP_FAILED
, "mremap");
208 // We expect the first 5 pages of the source to contain a's and the
209 // final 5 pages to contain zeros.
210 BUG_ON(check_region_contains_byte(source_mapping
, 5 * page_size
, 'a') !=
211 0, "first 5 pages of source should have original pages");
212 BUG_ON(check_region_contains_byte
213 (source_mapping
+ (5 * page_size
), 5 * page_size
, 0) != 0,
214 "final 5 pages of source should have no ptes");
216 // Finally we expect the destination to have 5 pages worth of a's.
217 BUG_ON(check_region_contains_byte(dest_mapping
, 5 * page_size
, 'a') !=
218 0, "dest mapping should contain ptes from the source");
220 BUG_ON(munmap(dest_mapping
, 5 * page_size
) == -1,
221 "unable to unmap destination mapping");
222 BUG_ON(munmap(source_mapping
, num_pages
* page_size
) == -1,
223 "unable to unmap source mapping");
226 // This test validates that we can remap over only a portion of a mapping.
227 static void mremap_dontunmap_partial_mapping_overwrite(void)
234 * dest mapping initially:
242 * With the destination mapping containing 5 pages of As.
247 void *source_mapping
=
248 mmap(NULL
, 5 * page_size
, PROT_READ
| PROT_WRITE
,
249 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
250 BUG_ON(source_mapping
== MAP_FAILED
, "mmap");
251 memset(source_mapping
, 'a', 5 * page_size
);
254 mmap(NULL
, 10 * page_size
, PROT_READ
| PROT_WRITE
,
255 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
256 BUG_ON(dest_mapping
== MAP_FAILED
, "mmap");
257 memset(dest_mapping
, 'X', 10 * page_size
);
259 // We will grab the last 5 pages of the source and move them.
260 void *remapped_mapping
=
261 mremap(source_mapping
, 5 * page_size
,
263 MREMAP_DONTUNMAP
| MREMAP_MAYMOVE
| MREMAP_FIXED
, dest_mapping
);
264 BUG_ON(dest_mapping
== MAP_FAILED
, "mremap");
265 BUG_ON(dest_mapping
!= remapped_mapping
, "expected to remap to dest_mapping");
267 BUG_ON(check_region_contains_byte(source_mapping
, 5 * page_size
, 0) !=
268 0, "first 5 pages of source should have no ptes");
270 // Finally we expect the destination to have 5 pages worth of a's.
271 BUG_ON(check_region_contains_byte(dest_mapping
, 5 * page_size
, 'a') != 0,
272 "dest mapping should contain ptes from the source");
274 // Finally the last 5 pages shouldn't have been touched.
275 BUG_ON(check_region_contains_byte(dest_mapping
+ (5 * page_size
),
276 5 * page_size
, 'X') != 0,
277 "dest mapping should have retained the last 5 pages");
279 BUG_ON(munmap(dest_mapping
, 10 * page_size
) == -1,
280 "unable to unmap destination mapping");
281 BUG_ON(munmap(source_mapping
, 5 * page_size
) == -1,
282 "unable to unmap source mapping");
287 page_size
= sysconf(_SC_PAGE_SIZE
);
289 // test for kernel support for MREMAP_DONTUNMAP skipping the test if
291 if (kernel_support_for_mremap_dontunmap() != 0) {
292 printf("No kernel support for MREMAP_DONTUNMAP\n");
296 // Keep a page sized buffer around for when we need it.
298 mmap(NULL
, page_size
, PROT_READ
| PROT_WRITE
,
299 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
300 BUG_ON(page_buffer
== MAP_FAILED
, "unable to mmap a page.");
302 mremap_dontunmap_simple();
303 mremap_dontunmap_simple_fixed();
304 mremap_dontunmap_partial_mapping();
305 mremap_dontunmap_partial_mapping_overwrite();
307 BUG_ON(munmap(page_buffer
, page_size
) == -1,
308 "unable to unmap page buffer");