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