xref: /linux/tools/testing/selftests/mm/mremap_test.c (revision beb69e81724634063b9dbae4bc79e2e011fdeeb1)
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 #ifndef MIN
26 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
27 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
28 #endif
29 #define SIZE_MB(m) ((size_t)m * (1024 * 1024))
30 #define SIZE_KB(k) ((size_t)k * 1024)
31 
32 struct config {
33 	unsigned long long src_alignment;
34 	unsigned long long dest_alignment;
35 	unsigned long long region_size;
36 	int overlapping;
37 	unsigned int dest_preamble_size;
38 };
39 
40 struct test {
41 	const char *name;
42 	struct config config;
43 	int expect_failure;
44 };
45 
46 enum {
47 	_1KB = 1ULL << 10,	/* 1KB -> not page aligned */
48 	_4KB = 4ULL << 10,
49 	_8KB = 8ULL << 10,
50 	_1MB = 1ULL << 20,
51 	_2MB = 2ULL << 20,
52 	_4MB = 4ULL << 20,
53 	_5MB = 5ULL << 20,
54 	_1GB = 1ULL << 30,
55 	_2GB = 2ULL << 30,
56 	PMD = _2MB,
57 	PUD = _1GB,
58 };
59 
60 #define PTE page_size
61 
62 #define MAKE_TEST(source_align, destination_align, size,	\
63 		  overlaps, should_fail, test_name)		\
64 (struct test){							\
65 	.name = test_name,					\
66 	.config = {						\
67 		.src_alignment = source_align,			\
68 		.dest_alignment = destination_align,		\
69 		.region_size = size,				\
70 		.overlapping = overlaps,			\
71 	},							\
72 	.expect_failure = should_fail				\
73 }
74 
75 /* compute square root using binary search */
76 static unsigned long get_sqrt(unsigned long val)
77 {
78 	unsigned long low = 1;
79 
80 	/* assuming rand_size is less than 1TB */
81 	unsigned long high = (1UL << 20);
82 
83 	while (low <= high) {
84 		unsigned long mid = low + (high - low) / 2;
85 		unsigned long temp = mid * mid;
86 
87 		if (temp == val)
88 			return mid;
89 		if (temp < val)
90 			low = mid + 1;
91 		high = mid - 1;
92 	}
93 	return low;
94 }
95 
96 /*
97  * Returns false if the requested remap region overlaps with an
98  * existing mapping (e.g text, stack) else returns true.
99  */
100 static bool is_remap_region_valid(void *addr, unsigned long long size)
101 {
102 	void *remap_addr = NULL;
103 	bool ret = true;
104 
105 	/* Use MAP_FIXED_NOREPLACE flag to ensure region is not mapped */
106 	remap_addr = mmap(addr, size, PROT_READ | PROT_WRITE,
107 					 MAP_FIXED_NOREPLACE | MAP_ANONYMOUS | MAP_SHARED,
108 					 -1, 0);
109 
110 	if (remap_addr == MAP_FAILED) {
111 		if (errno == EEXIST)
112 			ret = false;
113 	} else {
114 		munmap(remap_addr, size);
115 	}
116 
117 	return ret;
118 }
119 
120 /* Returns mmap_min_addr sysctl tunable from procfs */
121 static unsigned long long get_mmap_min_addr(void)
122 {
123 	FILE *fp;
124 	int n_matched;
125 	static unsigned long long addr;
126 
127 	if (addr)
128 		return addr;
129 
130 	fp = fopen("/proc/sys/vm/mmap_min_addr", "r");
131 	if (fp == NULL) {
132 		ksft_print_msg("Failed to open /proc/sys/vm/mmap_min_addr: %s\n",
133 			strerror(errno));
134 		exit(KSFT_SKIP);
135 	}
136 
137 	n_matched = fscanf(fp, "%llu", &addr);
138 	if (n_matched != 1) {
139 		ksft_print_msg("Failed to read /proc/sys/vm/mmap_min_addr: %s\n",
140 			strerror(errno));
141 		fclose(fp);
142 		exit(KSFT_SKIP);
143 	}
144 
145 	fclose(fp);
146 	return addr;
147 }
148 
149 /*
150  * Using /proc/self/maps, assert that the specified address range is contained
151  * within a single mapping.
152  */
153 static bool is_range_mapped(FILE *maps_fp, unsigned long start,
154 			    unsigned long end)
155 {
156 	char *line = NULL;
157 	size_t len = 0;
158 	bool success = false;
159 	unsigned long first_val, second_val;
160 
161 	rewind(maps_fp);
162 
163 	while (getline(&line, &len, maps_fp) != -1) {
164 		if (sscanf(line, "%lx-%lx", &first_val, &second_val) != 2) {
165 			ksft_exit_fail_msg("cannot parse /proc/self/maps\n");
166 			break;
167 		}
168 
169 		if (first_val <= start && second_val >= end) {
170 			success = true;
171 			break;
172 		}
173 	}
174 
175 	return success;
176 }
177 
178 /*
179  * Returns the start address of the mapping on success, else returns
180  * NULL on failure.
181  */
182 static void *get_source_mapping(struct config c)
183 {
184 	unsigned long long addr = 0ULL;
185 	void *src_addr = NULL;
186 	unsigned long long mmap_min_addr;
187 
188 	mmap_min_addr = get_mmap_min_addr();
189 	/*
190 	 * For some tests, we need to not have any mappings below the
191 	 * source mapping. Add some headroom to mmap_min_addr for this.
192 	 */
193 	mmap_min_addr += 10 * _4MB;
194 
195 retry:
196 	addr += c.src_alignment;
197 	if (addr < mmap_min_addr)
198 		goto retry;
199 
200 	src_addr = mmap((void *) addr, c.region_size, PROT_READ | PROT_WRITE,
201 					MAP_FIXED_NOREPLACE | MAP_ANONYMOUS | MAP_SHARED,
202 					-1, 0);
203 	if (src_addr == MAP_FAILED) {
204 		if (errno == EPERM || errno == EEXIST)
205 			goto retry;
206 		goto error;
207 	}
208 	/*
209 	 * Check that the address is aligned to the specified alignment.
210 	 * Addresses which have alignments that are multiples of that
211 	 * specified are not considered valid. For instance, 1GB address is
212 	 * 2MB-aligned, however it will not be considered valid for a
213 	 * requested alignment of 2MB. This is done to reduce coincidental
214 	 * alignment in the tests.
215 	 */
216 	if (((unsigned long long) src_addr & (c.src_alignment - 1)) ||
217 			!((unsigned long long) src_addr & c.src_alignment)) {
218 		munmap(src_addr, c.region_size);
219 		goto retry;
220 	}
221 
222 	if (!src_addr)
223 		goto error;
224 
225 	return src_addr;
226 error:
227 	ksft_print_msg("Failed to map source region: %s\n",
228 			strerror(errno));
229 	return NULL;
230 }
231 
232 /*
233  * This test validates that merge is called when expanding a mapping.
234  * Mapping containing three pages is created, middle page is unmapped
235  * and then the mapping containing the first page is expanded so that
236  * it fills the created hole. The two parts should merge creating
237  * single mapping with three pages.
238  */
239 static void mremap_expand_merge(FILE *maps_fp, unsigned long page_size)
240 {
241 	char *test_name = "mremap expand merge";
242 	bool success = false;
243 	char *remap, *start;
244 
245 	start = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE,
246 		     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
247 
248 	if (start == MAP_FAILED) {
249 		ksft_print_msg("mmap failed: %s\n", strerror(errno));
250 		goto out;
251 	}
252 
253 	munmap(start + page_size, page_size);
254 	remap = mremap(start, page_size, 2 * page_size, 0);
255 	if (remap == MAP_FAILED) {
256 		ksft_print_msg("mremap failed: %s\n", strerror(errno));
257 		munmap(start, page_size);
258 		munmap(start + 2 * page_size, page_size);
259 		goto out;
260 	}
261 
262 	success = is_range_mapped(maps_fp, (unsigned long)start,
263 				  (unsigned long)(start + 3 * page_size));
264 	munmap(start, 3 * page_size);
265 
266 out:
267 	if (success)
268 		ksft_test_result_pass("%s\n", test_name);
269 	else
270 		ksft_test_result_fail("%s\n", test_name);
271 }
272 
273 /*
274  * Similar to mremap_expand_merge() except instead of removing the middle page,
275  * we remove the last then attempt to remap offset from the second page. This
276  * should result in the mapping being restored to its former state.
277  */
278 static void mremap_expand_merge_offset(FILE *maps_fp, unsigned long page_size)
279 {
280 
281 	char *test_name = "mremap expand merge offset";
282 	bool success = false;
283 	char *remap, *start;
284 
285 	start = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE,
286 		     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
287 
288 	if (start == MAP_FAILED) {
289 		ksft_print_msg("mmap failed: %s\n", strerror(errno));
290 		goto out;
291 	}
292 
293 	/* Unmap final page to ensure we have space to expand. */
294 	munmap(start + 2 * page_size, page_size);
295 	remap = mremap(start + page_size, page_size, 2 * page_size, 0);
296 	if (remap == MAP_FAILED) {
297 		ksft_print_msg("mremap failed: %s\n", strerror(errno));
298 		munmap(start, 2 * page_size);
299 		goto out;
300 	}
301 
302 	success = is_range_mapped(maps_fp, (unsigned long)start,
303 				  (unsigned long)(start + 3 * page_size));
304 	munmap(start, 3 * page_size);
305 
306 out:
307 	if (success)
308 		ksft_test_result_pass("%s\n", test_name);
309 	else
310 		ksft_test_result_fail("%s\n", test_name);
311 }
312 
313 /*
314  * Verify that an mremap within a range does not cause corruption
315  * of unrelated part of range.
316  *
317  * Consider the following range which is 2MB aligned and is
318  * a part of a larger 20MB range which is not shown. Each
319  * character is 256KB below making the source and destination
320  * 2MB each. The lower case letters are moved (s to d) and the
321  * upper case letters are not moved. The below test verifies
322  * that the upper case S letters are not corrupted by the
323  * adjacent mremap.
324  *
325  * |DDDDddddSSSSssss|
326  */
327 static void mremap_move_within_range(unsigned int pattern_seed, char *rand_addr)
328 {
329 	char *test_name = "mremap mremap move within range";
330 	void *src, *dest;
331 	unsigned int i, success = 1;
332 
333 	size_t size = SIZE_MB(20);
334 	void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
335 			 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
336 	if (ptr == MAP_FAILED) {
337 		perror("mmap");
338 		success = 0;
339 		goto out;
340 	}
341 	memset(ptr, 0, size);
342 
343 	src = ptr + SIZE_MB(6);
344 	src = (void *)((unsigned long)src & ~(SIZE_MB(2) - 1));
345 
346 	/* Set byte pattern for source block. */
347 	memcpy(src, rand_addr, SIZE_MB(2));
348 
349 	dest = src - SIZE_MB(2);
350 
351 	void *new_ptr = mremap(src + SIZE_MB(1), SIZE_MB(1), SIZE_MB(1),
352 						   MREMAP_MAYMOVE | MREMAP_FIXED, dest + SIZE_MB(1));
353 	if (new_ptr == MAP_FAILED) {
354 		perror("mremap");
355 		success = 0;
356 		goto out;
357 	}
358 
359 	/* Verify byte pattern after remapping */
360 	srand(pattern_seed);
361 	for (i = 0; i < SIZE_MB(1); i++) {
362 		char c = (char) rand();
363 
364 		if (((char *)src)[i] != c) {
365 			ksft_print_msg("Data at src at %d got corrupted due to unrelated mremap\n",
366 				       i);
367 			ksft_print_msg("Expected: %#x\t Got: %#x\n", c & 0xff,
368 					((char *) src)[i] & 0xff);
369 			success = 0;
370 		}
371 	}
372 
373 out:
374 	if (munmap(ptr, size) == -1)
375 		perror("munmap");
376 
377 	if (success)
378 		ksft_test_result_pass("%s\n", test_name);
379 	else
380 		ksft_test_result_fail("%s\n", test_name);
381 }
382 
383 static bool is_multiple_vma_range_ok(unsigned int pattern_seed,
384 				     char *ptr, unsigned long page_size)
385 {
386 	int i;
387 
388 	srand(pattern_seed);
389 	for (i = 0; i <= 10; i += 2) {
390 		int j;
391 		char *buf = &ptr[i * page_size];
392 		size_t size = i == 4 ? 2 * page_size : page_size;
393 
394 		for (j = 0; j < size; j++) {
395 			char chr = rand();
396 
397 			if (chr != buf[j]) {
398 				ksft_print_msg("page %d offset %d corrupted, expected %d got %d\n",
399 					       i, j, chr, buf[j]);
400 				return false;
401 			}
402 		}
403 	}
404 
405 	return true;
406 }
407 
408 static void mremap_move_multiple_vmas(unsigned int pattern_seed,
409 				      unsigned long page_size)
410 {
411 	char *test_name = "mremap move multiple vmas";
412 	const size_t size = 11 * page_size;
413 	bool success = true;
414 	char *ptr, *tgt_ptr;
415 	int i;
416 
417 	ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
418 		   MAP_PRIVATE | MAP_ANON, -1, 0);
419 	if (ptr == MAP_FAILED) {
420 		perror("mmap");
421 		success = false;
422 		goto out;
423 	}
424 
425 	tgt_ptr = mmap(NULL, 2 * size, PROT_READ | PROT_WRITE,
426 		       MAP_PRIVATE | MAP_ANON, -1, 0);
427 	if (tgt_ptr == MAP_FAILED) {
428 		perror("mmap");
429 		success = false;
430 		goto out;
431 	}
432 	if (munmap(tgt_ptr, 2 * size)) {
433 		perror("munmap");
434 		success = false;
435 		goto out_unmap;
436 	}
437 
438 	/*
439 	 * Unmap so we end up with:
440 	 *
441 	 *  0   2   4 5 6   8   10 offset in buffer
442 	 * |*| |*| |*****| |*| |*|
443 	 * |*| |*| |*****| |*| |*|
444 	 *  0   1   2 3 4   5   6  pattern offset
445 	 */
446 	for (i = 1; i < 10; i += 2) {
447 		if (i == 5)
448 			continue;
449 
450 		if (munmap(&ptr[i * page_size], page_size)) {
451 			perror("munmap");
452 			success = false;
453 			goto out_unmap;
454 		}
455 	}
456 
457 	srand(pattern_seed);
458 
459 	/* Set up random patterns. */
460 	for (i = 0; i <= 10; i += 2) {
461 		int j;
462 		size_t size = i == 4 ? 2 * page_size : page_size;
463 		char *buf = &ptr[i * page_size];
464 
465 		for (j = 0; j < size; j++)
466 			buf[j] = rand();
467 	}
468 
469 	/* First, just move the whole thing. */
470 	if (mremap(ptr, size, size,
471 		   MREMAP_MAYMOVE | MREMAP_FIXED, tgt_ptr) == MAP_FAILED) {
472 		perror("mremap");
473 		success = false;
474 		goto out_unmap;
475 	}
476 	/* Check move was ok. */
477 	if (!is_multiple_vma_range_ok(pattern_seed, tgt_ptr, page_size)) {
478 		success = false;
479 		goto out_unmap;
480 	}
481 
482 	/* Move next to itself. */
483 	if (mremap(tgt_ptr, size, size,
484 		   MREMAP_MAYMOVE | MREMAP_FIXED, &tgt_ptr[size]) == MAP_FAILED) {
485 		perror("mremap");
486 		goto out_unmap;
487 	}
488 	/* Check that the move is ok. */
489 	if (!is_multiple_vma_range_ok(pattern_seed, &tgt_ptr[size], page_size)) {
490 		success = false;
491 		goto out_unmap;
492 	}
493 
494 	/* Map a range to overwrite. */
495 	if (mmap(tgt_ptr, size, PROT_NONE,
496 		 MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0) == MAP_FAILED) {
497 		perror("mmap tgt");
498 		success = false;
499 		goto out_unmap;
500 	}
501 	/* Move and overwrite. */
502 	if (mremap(&tgt_ptr[size], size, size,
503 		   MREMAP_MAYMOVE | MREMAP_FIXED, tgt_ptr) == MAP_FAILED) {
504 		perror("mremap");
505 		goto out_unmap;
506 	}
507 	/* Check that the move is ok. */
508 	if (!is_multiple_vma_range_ok(pattern_seed, tgt_ptr, page_size)) {
509 		success = false;
510 		goto out_unmap;
511 	}
512 
513 out_unmap:
514 	if (munmap(tgt_ptr, 2 * size))
515 		perror("munmap tgt");
516 	if (munmap(ptr, size))
517 		perror("munmap src");
518 
519 out:
520 	if (success)
521 		ksft_test_result_pass("%s\n", test_name);
522 	else
523 		ksft_test_result_fail("%s\n", test_name);
524 }
525 
526 /* Returns the time taken for the remap on success else returns -1. */
527 static long long remap_region(struct config c, unsigned int threshold_mb,
528 			      char *rand_addr)
529 {
530 	void *addr, *src_addr, *dest_addr, *dest_preamble_addr = NULL;
531 	unsigned long long t, d;
532 	struct timespec t_start = {0, 0}, t_end = {0, 0};
533 	long long  start_ns, end_ns, align_mask, ret, offset;
534 	unsigned long long threshold;
535 	unsigned long num_chunks;
536 
537 	if (threshold_mb == VALIDATION_NO_THRESHOLD)
538 		threshold = c.region_size;
539 	else
540 		threshold = MIN(threshold_mb * _1MB, c.region_size);
541 
542 	src_addr = get_source_mapping(c);
543 	if (!src_addr) {
544 		ret = -1;
545 		goto out;
546 	}
547 
548 	/* Set byte pattern for source block. */
549 	memcpy(src_addr, rand_addr, threshold);
550 
551 	/* Mask to zero out lower bits of address for alignment */
552 	align_mask = ~(c.dest_alignment - 1);
553 	/* Offset of destination address from the end of the source region */
554 	offset = (c.overlapping) ? -c.dest_alignment : c.dest_alignment;
555 	addr = (void *) (((unsigned long long) src_addr + c.region_size
556 			  + offset) & align_mask);
557 
558 	/* Remap after the destination block preamble. */
559 	addr += c.dest_preamble_size;
560 
561 	/* See comment in get_source_mapping() */
562 	if (!((unsigned long long) addr & c.dest_alignment))
563 		addr = (void *) ((unsigned long long) addr | c.dest_alignment);
564 
565 	/* Don't destroy existing mappings unless expected to overlap */
566 	while (!is_remap_region_valid(addr, c.region_size) && !c.overlapping) {
567 		/* Check for unsigned overflow */
568 		if (addr + c.dest_alignment < addr) {
569 			ksft_print_msg("Couldn't find a valid region to remap to\n");
570 			ret = -1;
571 			goto clean_up_src;
572 		}
573 		addr += c.dest_alignment;
574 	}
575 
576 	if (c.dest_preamble_size) {
577 		dest_preamble_addr = mmap((void *) addr - c.dest_preamble_size, c.dest_preamble_size,
578 					  PROT_READ | PROT_WRITE,
579 					  MAP_FIXED_NOREPLACE | MAP_ANONYMOUS | MAP_SHARED,
580 							-1, 0);
581 		if (dest_preamble_addr == MAP_FAILED) {
582 			ksft_print_msg("Failed to map dest preamble region: %s\n",
583 					strerror(errno));
584 			ret = -1;
585 			goto clean_up_src;
586 		}
587 
588 		/* Set byte pattern for the dest preamble block. */
589 		memcpy(dest_preamble_addr, rand_addr, c.dest_preamble_size);
590 	}
591 
592 	clock_gettime(CLOCK_MONOTONIC, &t_start);
593 	dest_addr = mremap(src_addr, c.region_size, c.region_size,
594 					  MREMAP_MAYMOVE|MREMAP_FIXED, (char *) addr);
595 	clock_gettime(CLOCK_MONOTONIC, &t_end);
596 
597 	if (dest_addr == MAP_FAILED) {
598 		ksft_print_msg("mremap failed: %s\n", strerror(errno));
599 		ret = -1;
600 		goto clean_up_dest_preamble;
601 	}
602 
603 	/*
604 	 * Verify byte pattern after remapping. Employ an algorithm with a
605 	 * square root time complexity in threshold: divide the range into
606 	 * chunks, if memcmp() returns non-zero, only then perform an
607 	 * iteration in that chunk to find the mismatch index.
608 	 */
609 	num_chunks = get_sqrt(threshold);
610 	for (unsigned long i = 0; i < num_chunks; ++i) {
611 		size_t chunk_size = threshold / num_chunks;
612 		unsigned long shift = i * chunk_size;
613 
614 		if (!memcmp(dest_addr + shift, rand_addr + shift, chunk_size))
615 			continue;
616 
617 		/* brute force iteration only over mismatch segment */
618 		for (t = shift; t < shift + chunk_size; ++t) {
619 			if (((char *) dest_addr)[t] != rand_addr[t]) {
620 				ksft_print_msg("Data after remap doesn't match at offset %llu\n",
621 						t);
622 				ksft_print_msg("Expected: %#x\t Got: %#x\n", rand_addr[t] & 0xff,
623 						((char *) dest_addr)[t] & 0xff);
624 				ret = -1;
625 				goto clean_up_dest;
626 			}
627 		}
628 	}
629 
630 	/*
631 	 * if threshold is not divisible by num_chunks, then check the
632 	 * last chunk
633 	 */
634 	for (t = num_chunks * (threshold / num_chunks); t < threshold; ++t) {
635 		if (((char *) dest_addr)[t] != rand_addr[t]) {
636 			ksft_print_msg("Data after remap doesn't match at offset %llu\n",
637 					t);
638 			ksft_print_msg("Expected: %#x\t Got: %#x\n", rand_addr[t] & 0xff,
639 					((char *) dest_addr)[t] & 0xff);
640 			ret = -1;
641 			goto clean_up_dest;
642 		}
643 	}
644 
645 	/* Verify the dest preamble byte pattern after remapping */
646 	if (!c.dest_preamble_size)
647 		goto no_preamble;
648 
649 	num_chunks = get_sqrt(c.dest_preamble_size);
650 
651 	for (unsigned long i = 0; i < num_chunks; ++i) {
652 		size_t chunk_size = c.dest_preamble_size / num_chunks;
653 		unsigned long shift = i * chunk_size;
654 
655 		if (!memcmp(dest_preamble_addr + shift, rand_addr + shift,
656 			    chunk_size))
657 			continue;
658 
659 		/* brute force iteration only over mismatched segment */
660 		for (d = shift; d < shift + chunk_size; ++d) {
661 			if (((char *) dest_preamble_addr)[d] != rand_addr[d]) {
662 				ksft_print_msg("Preamble data after remap doesn't match at offset %llu\n",
663 						d);
664 				ksft_print_msg("Expected: %#x\t Got: %#x\n", rand_addr[d] & 0xff,
665 						((char *) dest_preamble_addr)[d] & 0xff);
666 				ret = -1;
667 				goto clean_up_dest;
668 			}
669 		}
670 	}
671 
672 	for (d = num_chunks * (c.dest_preamble_size / num_chunks); d < c.dest_preamble_size; ++d) {
673 		if (((char *) dest_preamble_addr)[d] != rand_addr[d]) {
674 			ksft_print_msg("Preamble data after remap doesn't match at offset %llu\n",
675 					d);
676 			ksft_print_msg("Expected: %#x\t Got: %#x\n", rand_addr[d] & 0xff,
677 					((char *) dest_preamble_addr)[d] & 0xff);
678 			ret = -1;
679 			goto clean_up_dest;
680 		}
681 	}
682 
683 no_preamble:
684 	start_ns = t_start.tv_sec * NS_PER_SEC + t_start.tv_nsec;
685 	end_ns = t_end.tv_sec * NS_PER_SEC + t_end.tv_nsec;
686 	ret = end_ns - start_ns;
687 
688 /*
689  * Since the destination address is specified using MREMAP_FIXED, subsequent
690  * mremap will unmap any previous mapping at the address range specified by
691  * dest_addr and region_size. This significantly affects the remap time of
692  * subsequent tests. So we clean up mappings after each test.
693  */
694 clean_up_dest:
695 	munmap(dest_addr, c.region_size);
696 clean_up_dest_preamble:
697 	if (c.dest_preamble_size && dest_preamble_addr)
698 		munmap(dest_preamble_addr, c.dest_preamble_size);
699 clean_up_src:
700 	munmap(src_addr, c.region_size);
701 out:
702 	return ret;
703 }
704 
705 /*
706  * Verify that an mremap aligning down does not destroy
707  * the beginning of the mapping just because the aligned
708  * down address landed on a mapping that maybe does not exist.
709  */
710 static void mremap_move_1mb_from_start(unsigned int pattern_seed,
711 				       char *rand_addr)
712 {
713 	char *test_name = "mremap move 1mb from start at 1MB+256KB aligned src";
714 	void *src = NULL, *dest = NULL;
715 	unsigned int i, success = 1;
716 
717 	/* Config to reuse get_source_mapping() to do an aligned mmap. */
718 	struct config c = {
719 		.src_alignment = SIZE_MB(1) + SIZE_KB(256),
720 		.region_size = SIZE_MB(6)
721 	};
722 
723 	src = get_source_mapping(c);
724 	if (!src) {
725 		success = 0;
726 		goto out;
727 	}
728 
729 	c.src_alignment = SIZE_MB(1) + SIZE_KB(256);
730 	dest = get_source_mapping(c);
731 	if (!dest) {
732 		success = 0;
733 		goto out;
734 	}
735 
736 	/* Set byte pattern for source block. */
737 	memcpy(src, rand_addr, SIZE_MB(2));
738 
739 	/*
740 	 * Unmap the beginning of dest so that the aligned address
741 	 * falls on no mapping.
742 	 */
743 	munmap(dest, SIZE_MB(1));
744 
745 	void *new_ptr = mremap(src + SIZE_MB(1), SIZE_MB(1), SIZE_MB(1),
746 						   MREMAP_MAYMOVE | MREMAP_FIXED, dest + SIZE_MB(1));
747 	if (new_ptr == MAP_FAILED) {
748 		perror("mremap");
749 		success = 0;
750 		goto out;
751 	}
752 
753 	/* Verify byte pattern after remapping */
754 	srand(pattern_seed);
755 	for (i = 0; i < SIZE_MB(1); i++) {
756 		char c = (char) rand();
757 
758 		if (((char *)src)[i] != c) {
759 			ksft_print_msg("Data at src at %d got corrupted due to unrelated mremap\n",
760 				       i);
761 			ksft_print_msg("Expected: %#x\t Got: %#x\n", c & 0xff,
762 					((char *) src)[i] & 0xff);
763 			success = 0;
764 		}
765 	}
766 
767 out:
768 	if (src && munmap(src, c.region_size) == -1)
769 		perror("munmap src");
770 
771 	if (dest && munmap(dest, c.region_size) == -1)
772 		perror("munmap dest");
773 
774 	if (success)
775 		ksft_test_result_pass("%s\n", test_name);
776 	else
777 		ksft_test_result_fail("%s\n", test_name);
778 }
779 
780 static void run_mremap_test_case(struct test test_case, int *failures,
781 				 unsigned int threshold_mb,
782 				 char *rand_addr)
783 {
784 	long long remap_time = remap_region(test_case.config, threshold_mb,
785 					    rand_addr);
786 
787 	if (remap_time < 0) {
788 		if (test_case.expect_failure)
789 			ksft_test_result_xfail("%s\n\tExpected mremap failure\n",
790 					      test_case.name);
791 		else {
792 			ksft_test_result_fail("%s\n", test_case.name);
793 			*failures += 1;
794 		}
795 	} else {
796 		/*
797 		 * Comparing mremap time is only applicable if entire region
798 		 * was faulted in.
799 		 */
800 		if (threshold_mb == VALIDATION_NO_THRESHOLD ||
801 		    test_case.config.region_size <= threshold_mb * _1MB)
802 			ksft_test_result_pass("%s\n\tmremap time: %12lldns\n",
803 					      test_case.name, remap_time);
804 		else
805 			ksft_test_result_pass("%s\n", test_case.name);
806 	}
807 }
808 
809 static void usage(const char *cmd)
810 {
811 	fprintf(stderr,
812 		"Usage: %s [[-t <threshold_mb>] [-p <pattern_seed>]]\n"
813 		"-t\t only validate threshold_mb of the remapped region\n"
814 		"  \t if 0 is supplied no threshold is used; all tests\n"
815 		"  \t are run and remapped regions validated fully.\n"
816 		"  \t The default threshold used is 4MB.\n"
817 		"-p\t provide a seed to generate the random pattern for\n"
818 		"  \t validating the remapped region.\n", cmd);
819 }
820 
821 static int parse_args(int argc, char **argv, unsigned int *threshold_mb,
822 		      unsigned int *pattern_seed)
823 {
824 	const char *optstr = "t:p:";
825 	int opt;
826 
827 	while ((opt = getopt(argc, argv, optstr)) != -1) {
828 		switch (opt) {
829 		case 't':
830 			*threshold_mb = atoi(optarg);
831 			break;
832 		case 'p':
833 			*pattern_seed = atoi(optarg);
834 			break;
835 		default:
836 			usage(argv[0]);
837 			return -1;
838 		}
839 	}
840 
841 	if (optind < argc) {
842 		usage(argv[0]);
843 		return -1;
844 	}
845 
846 	return 0;
847 }
848 
849 #define MAX_TEST 15
850 #define MAX_PERF_TEST 3
851 int main(int argc, char **argv)
852 {
853 	int failures = 0;
854 	unsigned int i;
855 	int run_perf_tests;
856 	unsigned int threshold_mb = VALIDATION_DEFAULT_THRESHOLD;
857 
858 	/* hard-coded test configs */
859 	size_t max_test_variable_region_size = _2GB;
860 	size_t max_test_constant_region_size = _2MB;
861 	size_t dest_preamble_size = 10 * _4MB;
862 
863 	unsigned int pattern_seed;
864 	char *rand_addr;
865 	size_t rand_size;
866 	int num_expand_tests = 2;
867 	int num_misc_tests = 3;
868 	struct test test_cases[MAX_TEST] = {};
869 	struct test perf_test_cases[MAX_PERF_TEST];
870 	int page_size;
871 	time_t t;
872 	FILE *maps_fp;
873 
874 	pattern_seed = (unsigned int) time(&t);
875 
876 	if (parse_args(argc, argv, &threshold_mb, &pattern_seed) < 0)
877 		exit(EXIT_FAILURE);
878 
879 	ksft_print_msg("Test configs:\n\tthreshold_mb=%u\n\tpattern_seed=%u\n\n",
880 		       threshold_mb, pattern_seed);
881 
882 	/*
883 	 * set preallocated random array according to test configs; see the
884 	 * functions for the logic of setting the size
885 	 */
886 	if (!threshold_mb)
887 		rand_size = MAX(max_test_variable_region_size,
888 				max_test_constant_region_size);
889 	else
890 		rand_size = MAX(MIN(threshold_mb * _1MB,
891 				    max_test_variable_region_size),
892 				max_test_constant_region_size);
893 	rand_size = MAX(dest_preamble_size, rand_size);
894 
895 	rand_addr = (char *)mmap(NULL, rand_size, PROT_READ | PROT_WRITE,
896 				 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
897 	if (rand_addr == MAP_FAILED) {
898 		perror("mmap");
899 		ksft_exit_fail_msg("cannot mmap rand_addr\n");
900 	}
901 
902 	/* fill stream of random bytes */
903 	srand(pattern_seed);
904 	for (unsigned long i = 0; i < rand_size; ++i)
905 		rand_addr[i] = (char) rand();
906 
907 	page_size = sysconf(_SC_PAGESIZE);
908 
909 	/* Expected mremap failures */
910 	test_cases[0] =	MAKE_TEST(page_size, page_size, page_size,
911 				  OVERLAPPING, EXPECT_FAILURE,
912 				  "mremap - Source and Destination Regions Overlapping");
913 
914 	test_cases[1] = MAKE_TEST(page_size, page_size/4, page_size,
915 				  NON_OVERLAPPING, EXPECT_FAILURE,
916 				  "mremap - Destination Address Misaligned (1KB-aligned)");
917 	test_cases[2] = MAKE_TEST(page_size/4, page_size, page_size,
918 				  NON_OVERLAPPING, EXPECT_FAILURE,
919 				  "mremap - Source Address Misaligned (1KB-aligned)");
920 
921 	/* Src addr PTE aligned */
922 	test_cases[3] = MAKE_TEST(PTE, PTE, PTE * 2,
923 				  NON_OVERLAPPING, EXPECT_SUCCESS,
924 				  "8KB mremap - Source PTE-aligned, Destination PTE-aligned");
925 
926 	/* Src addr 1MB aligned */
927 	test_cases[4] = MAKE_TEST(_1MB, PTE, _2MB, NON_OVERLAPPING, EXPECT_SUCCESS,
928 				  "2MB mremap - Source 1MB-aligned, Destination PTE-aligned");
929 	test_cases[5] = MAKE_TEST(_1MB, _1MB, _2MB, NON_OVERLAPPING, EXPECT_SUCCESS,
930 				  "2MB mremap - Source 1MB-aligned, Destination 1MB-aligned");
931 
932 	/* Src addr PMD aligned */
933 	test_cases[6] = MAKE_TEST(PMD, PTE, _4MB, NON_OVERLAPPING, EXPECT_SUCCESS,
934 				  "4MB mremap - Source PMD-aligned, Destination PTE-aligned");
935 	test_cases[7] =	MAKE_TEST(PMD, _1MB, _4MB, NON_OVERLAPPING, EXPECT_SUCCESS,
936 				  "4MB mremap - Source PMD-aligned, Destination 1MB-aligned");
937 	test_cases[8] = MAKE_TEST(PMD, PMD, _4MB, NON_OVERLAPPING, EXPECT_SUCCESS,
938 				  "4MB mremap - Source PMD-aligned, Destination PMD-aligned");
939 
940 	/* Src addr PUD aligned */
941 	test_cases[9] = MAKE_TEST(PUD, PTE, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
942 				  "2GB mremap - Source PUD-aligned, Destination PTE-aligned");
943 	test_cases[10] = MAKE_TEST(PUD, _1MB, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
944 				   "2GB mremap - Source PUD-aligned, Destination 1MB-aligned");
945 	test_cases[11] = MAKE_TEST(PUD, PMD, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
946 				   "2GB mremap - Source PUD-aligned, Destination PMD-aligned");
947 	test_cases[12] = MAKE_TEST(PUD, PUD, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
948 				   "2GB mremap - Source PUD-aligned, Destination PUD-aligned");
949 
950 	/* Src and Dest addr 1MB aligned. 5MB mremap. */
951 	test_cases[13] = MAKE_TEST(_1MB, _1MB, _5MB, NON_OVERLAPPING, EXPECT_SUCCESS,
952 				  "5MB mremap - Source 1MB-aligned, Destination 1MB-aligned");
953 
954 	/* Src and Dest addr 1MB aligned. 5MB mremap. */
955 	test_cases[14] = MAKE_TEST(_1MB, _1MB, _5MB, NON_OVERLAPPING, EXPECT_SUCCESS,
956 				  "5MB mremap - Source 1MB-aligned, Dest 1MB-aligned with 40MB Preamble");
957 	test_cases[14].config.dest_preamble_size = 10 * _4MB;
958 
959 	perf_test_cases[0] =  MAKE_TEST(page_size, page_size, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS,
960 					"1GB mremap - Source PTE-aligned, Destination PTE-aligned");
961 	/*
962 	 * mremap 1GB region - Page table level aligned time
963 	 * comparison.
964 	 */
965 	perf_test_cases[1] = MAKE_TEST(PMD, PMD, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS,
966 				       "1GB mremap - Source PMD-aligned, Destination PMD-aligned");
967 	perf_test_cases[2] = MAKE_TEST(PUD, PUD, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS,
968 				       "1GB mremap - Source PUD-aligned, Destination PUD-aligned");
969 
970 	run_perf_tests =  (threshold_mb == VALIDATION_NO_THRESHOLD) ||
971 				(threshold_mb * _1MB >= _1GB);
972 
973 	ksft_set_plan(ARRAY_SIZE(test_cases) + (run_perf_tests ?
974 		      ARRAY_SIZE(perf_test_cases) : 0) + num_expand_tests + num_misc_tests);
975 
976 	for (i = 0; i < ARRAY_SIZE(test_cases); i++)
977 		run_mremap_test_case(test_cases[i], &failures, threshold_mb,
978 				     rand_addr);
979 
980 	maps_fp = fopen("/proc/self/maps", "r");
981 
982 	if (maps_fp == NULL) {
983 		munmap(rand_addr, rand_size);
984 		ksft_exit_fail_msg("Failed to read /proc/self/maps: %s\n", strerror(errno));
985 	}
986 
987 	mremap_expand_merge(maps_fp, page_size);
988 	mremap_expand_merge_offset(maps_fp, page_size);
989 
990 	fclose(maps_fp);
991 
992 	mremap_move_within_range(pattern_seed, rand_addr);
993 	mremap_move_1mb_from_start(pattern_seed, rand_addr);
994 	mremap_move_multiple_vmas(pattern_seed, page_size);
995 
996 	if (run_perf_tests) {
997 		ksft_print_msg("\n%s\n",
998 		 "mremap HAVE_MOVE_PMD/PUD optimization time comparison for 1GB region:");
999 		for (i = 0; i < ARRAY_SIZE(perf_test_cases); i++)
1000 			run_mremap_test_case(perf_test_cases[i], &failures,
1001 					     threshold_mb,
1002 					     rand_addr);
1003 	}
1004 
1005 	munmap(rand_addr, rand_size);
1006 
1007 	if (failures > 0)
1008 		ksft_exit_fail();
1009 	else
1010 		ksft_exit_pass();
1011 }
1012