Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2020 Google LLC
4 */
5#define _GNU_SOURCE
6
7#include <errno.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/mman.h>
11#include <time.h>
12
13#include "../kselftest.h"
14
15#define EXPECT_SUCCESS 0
16#define EXPECT_FAILURE 1
17#define NON_OVERLAPPING 0
18#define OVERLAPPING 1
19#define NS_PER_SEC 1000000000ULL
20#define VALIDATION_DEFAULT_THRESHOLD 4 /* 4MB */
21#define VALIDATION_NO_THRESHOLD 0 /* Verify the entire region */
22
23#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
24
25struct config {
26 unsigned long long src_alignment;
27 unsigned long long dest_alignment;
28 unsigned long long region_size;
29 int overlapping;
30};
31
32struct test {
33 const char *name;
34 struct config config;
35 int expect_failure;
36};
37
38enum {
39 _1KB = 1ULL << 10, /* 1KB -> not page aligned */
40 _4KB = 4ULL << 10,
41 _8KB = 8ULL << 10,
42 _1MB = 1ULL << 20,
43 _2MB = 2ULL << 20,
44 _4MB = 4ULL << 20,
45 _1GB = 1ULL << 30,
46 _2GB = 2ULL << 30,
47 PMD = _2MB,
48 PUD = _1GB,
49};
50
51#define PTE page_size
52
53#define MAKE_TEST(source_align, destination_align, size, \
54 overlaps, should_fail, test_name) \
55(struct test){ \
56 .name = test_name, \
57 .config = { \
58 .src_alignment = source_align, \
59 .dest_alignment = destination_align, \
60 .region_size = size, \
61 .overlapping = overlaps, \
62 }, \
63 .expect_failure = should_fail \
64}
65
66/*
67 * Returns the start address of the mapping on success, else returns
68 * NULL on failure.
69 */
70static void *get_source_mapping(struct config c)
71{
72 unsigned long long addr = 0ULL;
73 void *src_addr = NULL;
74retry:
75 addr += c.src_alignment;
76 src_addr = mmap((void *) addr, c.region_size, PROT_READ | PROT_WRITE,
77 MAP_FIXED_NOREPLACE | MAP_ANONYMOUS | MAP_SHARED,
78 -1, 0);
79 if (src_addr == MAP_FAILED) {
80 if (errno == EPERM || errno == EEXIST)
81 goto retry;
82 goto error;
83 }
84 /*
85 * Check that the address is aligned to the specified alignment.
86 * Addresses which have alignments that are multiples of that
87 * specified are not considered valid. For instance, 1GB address is
88 * 2MB-aligned, however it will not be considered valid for a
89 * requested alignment of 2MB. This is done to reduce coincidental
90 * alignment in the tests.
91 */
92 if (((unsigned long long) src_addr & (c.src_alignment - 1)) ||
93 !((unsigned long long) src_addr & c.src_alignment))
94 goto retry;
95
96 if (!src_addr)
97 goto error;
98
99 return src_addr;
100error:
101 ksft_print_msg("Failed to map source region: %s\n",
102 strerror(errno));
103 return NULL;
104}
105
106/* Returns the time taken for the remap on success else returns -1. */
107static long long remap_region(struct config c, unsigned int threshold_mb,
108 char pattern_seed)
109{
110 void *addr, *src_addr, *dest_addr;
111 unsigned long long i;
112 struct timespec t_start = {0, 0}, t_end = {0, 0};
113 long long start_ns, end_ns, align_mask, ret, offset;
114 unsigned long long threshold;
115
116 if (threshold_mb == VALIDATION_NO_THRESHOLD)
117 threshold = c.region_size;
118 else
119 threshold = MIN(threshold_mb * _1MB, c.region_size);
120
121 src_addr = get_source_mapping(c);
122 if (!src_addr) {
123 ret = -1;
124 goto out;
125 }
126
127 /* Set byte pattern */
128 srand(pattern_seed);
129 for (i = 0; i < threshold; i++)
130 memset((char *) src_addr + i, (char) rand(), 1);
131
132 /* Mask to zero out lower bits of address for alignment */
133 align_mask = ~(c.dest_alignment - 1);
134 /* Offset of destination address from the end of the source region */
135 offset = (c.overlapping) ? -c.dest_alignment : c.dest_alignment;
136 addr = (void *) (((unsigned long long) src_addr + c.region_size
137 + offset) & align_mask);
138
139 /* See comment in get_source_mapping() */
140 if (!((unsigned long long) addr & c.dest_alignment))
141 addr = (void *) ((unsigned long long) addr | c.dest_alignment);
142
143 clock_gettime(CLOCK_MONOTONIC, &t_start);
144 dest_addr = mremap(src_addr, c.region_size, c.region_size,
145 MREMAP_MAYMOVE|MREMAP_FIXED, (char *) addr);
146 clock_gettime(CLOCK_MONOTONIC, &t_end);
147
148 if (dest_addr == MAP_FAILED) {
149 ksft_print_msg("mremap failed: %s\n", strerror(errno));
150 ret = -1;
151 goto clean_up_src;
152 }
153
154 /* Verify byte pattern after remapping */
155 srand(pattern_seed);
156 for (i = 0; i < threshold; i++) {
157 char c = (char) rand();
158
159 if (((char *) dest_addr)[i] != c) {
160 ksft_print_msg("Data after remap doesn't match at offset %d\n",
161 i);
162 ksft_print_msg("Expected: %#x\t Got: %#x\n", c & 0xff,
163 ((char *) dest_addr)[i] & 0xff);
164 ret = -1;
165 goto clean_up_dest;
166 }
167 }
168
169 start_ns = t_start.tv_sec * NS_PER_SEC + t_start.tv_nsec;
170 end_ns = t_end.tv_sec * NS_PER_SEC + t_end.tv_nsec;
171 ret = end_ns - start_ns;
172
173/*
174 * Since the destination address is specified using MREMAP_FIXED, subsequent
175 * mremap will unmap any previous mapping at the address range specified by
176 * dest_addr and region_size. This significantly affects the remap time of
177 * subsequent tests. So we clean up mappings after each test.
178 */
179clean_up_dest:
180 munmap(dest_addr, c.region_size);
181clean_up_src:
182 munmap(src_addr, c.region_size);
183out:
184 return ret;
185}
186
187static void run_mremap_test_case(struct test test_case, int *failures,
188 unsigned int threshold_mb,
189 unsigned int pattern_seed)
190{
191 long long remap_time = remap_region(test_case.config, threshold_mb,
192 pattern_seed);
193
194 if (remap_time < 0) {
195 if (test_case.expect_failure)
196 ksft_test_result_pass("%s\n\tExpected mremap failure\n",
197 test_case.name);
198 else {
199 ksft_test_result_fail("%s\n", test_case.name);
200 *failures += 1;
201 }
202 } else {
203 /*
204 * Comparing mremap time is only applicable if entire region
205 * was faulted in.
206 */
207 if (threshold_mb == VALIDATION_NO_THRESHOLD ||
208 test_case.config.region_size <= threshold_mb * _1MB)
209 ksft_test_result_pass("%s\n\tmremap time: %12lldns\n",
210 test_case.name, remap_time);
211 else
212 ksft_test_result_pass("%s\n", test_case.name);
213 }
214}
215
216static void usage(const char *cmd)
217{
218 fprintf(stderr,
219 "Usage: %s [[-t <threshold_mb>] [-p <pattern_seed>]]\n"
220 "-t\t only validate threshold_mb of the remapped region\n"
221 " \t if 0 is supplied no threshold is used; all tests\n"
222 " \t are run and remapped regions validated fully.\n"
223 " \t The default threshold used is 4MB.\n"
224 "-p\t provide a seed to generate the random pattern for\n"
225 " \t validating the remapped region.\n", cmd);
226}
227
228static int parse_args(int argc, char **argv, unsigned int *threshold_mb,
229 unsigned int *pattern_seed)
230{
231 const char *optstr = "t:p:";
232 int opt;
233
234 while ((opt = getopt(argc, argv, optstr)) != -1) {
235 switch (opt) {
236 case 't':
237 *threshold_mb = atoi(optarg);
238 break;
239 case 'p':
240 *pattern_seed = atoi(optarg);
241 break;
242 default:
243 usage(argv[0]);
244 return -1;
245 }
246 }
247
248 if (optind < argc) {
249 usage(argv[0]);
250 return -1;
251 }
252
253 return 0;
254}
255
256#define MAX_TEST 13
257#define MAX_PERF_TEST 3
258int main(int argc, char **argv)
259{
260 int failures = 0;
261 int i, run_perf_tests;
262 unsigned int threshold_mb = VALIDATION_DEFAULT_THRESHOLD;
263 unsigned int pattern_seed;
264 struct test test_cases[MAX_TEST];
265 struct test perf_test_cases[MAX_PERF_TEST];
266 int page_size;
267 time_t t;
268
269 pattern_seed = (unsigned int) time(&t);
270
271 if (parse_args(argc, argv, &threshold_mb, &pattern_seed) < 0)
272 exit(EXIT_FAILURE);
273
274 ksft_print_msg("Test configs:\n\tthreshold_mb=%u\n\tpattern_seed=%u\n\n",
275 threshold_mb, pattern_seed);
276
277 page_size = sysconf(_SC_PAGESIZE);
278
279 /* Expected mremap failures */
280 test_cases[0] = MAKE_TEST(page_size, page_size, page_size,
281 OVERLAPPING, EXPECT_FAILURE,
282 "mremap - Source and Destination Regions Overlapping");
283
284 test_cases[1] = MAKE_TEST(page_size, page_size/4, page_size,
285 NON_OVERLAPPING, EXPECT_FAILURE,
286 "mremap - Destination Address Misaligned (1KB-aligned)");
287 test_cases[2] = MAKE_TEST(page_size/4, page_size, page_size,
288 NON_OVERLAPPING, EXPECT_FAILURE,
289 "mremap - Source Address Misaligned (1KB-aligned)");
290
291 /* Src addr PTE aligned */
292 test_cases[3] = MAKE_TEST(PTE, PTE, PTE * 2,
293 NON_OVERLAPPING, EXPECT_SUCCESS,
294 "8KB mremap - Source PTE-aligned, Destination PTE-aligned");
295
296 /* Src addr 1MB aligned */
297 test_cases[4] = MAKE_TEST(_1MB, PTE, _2MB, NON_OVERLAPPING, EXPECT_SUCCESS,
298 "2MB mremap - Source 1MB-aligned, Destination PTE-aligned");
299 test_cases[5] = MAKE_TEST(_1MB, _1MB, _2MB, NON_OVERLAPPING, EXPECT_SUCCESS,
300 "2MB mremap - Source 1MB-aligned, Destination 1MB-aligned");
301
302 /* Src addr PMD aligned */
303 test_cases[6] = MAKE_TEST(PMD, PTE, _4MB, NON_OVERLAPPING, EXPECT_SUCCESS,
304 "4MB mremap - Source PMD-aligned, Destination PTE-aligned");
305 test_cases[7] = MAKE_TEST(PMD, _1MB, _4MB, NON_OVERLAPPING, EXPECT_SUCCESS,
306 "4MB mremap - Source PMD-aligned, Destination 1MB-aligned");
307 test_cases[8] = MAKE_TEST(PMD, PMD, _4MB, NON_OVERLAPPING, EXPECT_SUCCESS,
308 "4MB mremap - Source PMD-aligned, Destination PMD-aligned");
309
310 /* Src addr PUD aligned */
311 test_cases[9] = MAKE_TEST(PUD, PTE, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
312 "2GB mremap - Source PUD-aligned, Destination PTE-aligned");
313 test_cases[10] = MAKE_TEST(PUD, _1MB, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
314 "2GB mremap - Source PUD-aligned, Destination 1MB-aligned");
315 test_cases[11] = MAKE_TEST(PUD, PMD, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
316 "2GB mremap - Source PUD-aligned, Destination PMD-aligned");
317 test_cases[12] = MAKE_TEST(PUD, PUD, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
318 "2GB mremap - Source PUD-aligned, Destination PUD-aligned");
319
320 perf_test_cases[0] = MAKE_TEST(page_size, page_size, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS,
321 "1GB mremap - Source PTE-aligned, Destination PTE-aligned");
322 /*
323 * mremap 1GB region - Page table level aligned time
324 * comparison.
325 */
326 perf_test_cases[1] = MAKE_TEST(PMD, PMD, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS,
327 "1GB mremap - Source PMD-aligned, Destination PMD-aligned");
328 perf_test_cases[2] = MAKE_TEST(PUD, PUD, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS,
329 "1GB mremap - Source PUD-aligned, Destination PUD-aligned");
330
331 run_perf_tests = (threshold_mb == VALIDATION_NO_THRESHOLD) ||
332 (threshold_mb * _1MB >= _1GB);
333
334 ksft_set_plan(ARRAY_SIZE(test_cases) + (run_perf_tests ?
335 ARRAY_SIZE(perf_test_cases) : 0));
336
337 for (i = 0; i < ARRAY_SIZE(test_cases); i++)
338 run_mremap_test_case(test_cases[i], &failures, threshold_mb,
339 pattern_seed);
340
341 if (run_perf_tests) {
342 ksft_print_msg("\n%s\n",
343 "mremap HAVE_MOVE_PMD/PUD optimization time comparison for 1GB region:");
344 for (i = 0; i < ARRAY_SIZE(perf_test_cases); i++)
345 run_mremap_test_case(perf_test_cases[i], &failures,
346 threshold_mb, pattern_seed);
347 }
348
349 if (failures > 0)
350 ksft_exit_fail();
351 else
352 ksft_exit_pass();
353}