xref: /openbmc/linux/tools/testing/selftests/mm/mremap_test.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1baa489faSSeongJae Park // SPDX-License-Identifier: GPL-2.0
2baa489faSSeongJae Park /*
3baa489faSSeongJae Park  * Copyright 2020 Google LLC
4baa489faSSeongJae Park  */
5baa489faSSeongJae Park #define _GNU_SOURCE
6baa489faSSeongJae Park 
7baa489faSSeongJae Park #include <errno.h>
8baa489faSSeongJae Park #include <stdlib.h>
9baa489faSSeongJae Park #include <stdio.h>
10baa489faSSeongJae Park #include <string.h>
11baa489faSSeongJae Park #include <sys/mman.h>
12baa489faSSeongJae Park #include <time.h>
13baa489faSSeongJae Park #include <stdbool.h>
14baa489faSSeongJae Park 
15baa489faSSeongJae Park #include "../kselftest.h"
16baa489faSSeongJae Park 
17baa489faSSeongJae Park #define EXPECT_SUCCESS 0
18baa489faSSeongJae Park #define EXPECT_FAILURE 1
19baa489faSSeongJae Park #define NON_OVERLAPPING 0
20baa489faSSeongJae Park #define OVERLAPPING 1
21baa489faSSeongJae Park #define NS_PER_SEC 1000000000ULL
22baa489faSSeongJae Park #define VALIDATION_DEFAULT_THRESHOLD 4	/* 4MB */
23baa489faSSeongJae Park #define VALIDATION_NO_THRESHOLD 0	/* Verify the entire region */
24baa489faSSeongJae Park 
25baa489faSSeongJae Park #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
26baa489faSSeongJae Park 
27baa489faSSeongJae Park struct config {
28baa489faSSeongJae Park 	unsigned long long src_alignment;
29baa489faSSeongJae Park 	unsigned long long dest_alignment;
30baa489faSSeongJae Park 	unsigned long long region_size;
31baa489faSSeongJae Park 	int overlapping;
32baa489faSSeongJae Park };
33baa489faSSeongJae Park 
34baa489faSSeongJae Park struct test {
35baa489faSSeongJae Park 	const char *name;
36baa489faSSeongJae Park 	struct config config;
37baa489faSSeongJae Park 	int expect_failure;
38baa489faSSeongJae Park };
39baa489faSSeongJae Park 
40baa489faSSeongJae Park enum {
41baa489faSSeongJae Park 	_1KB = 1ULL << 10,	/* 1KB -> not page aligned */
42baa489faSSeongJae Park 	_4KB = 4ULL << 10,
43baa489faSSeongJae Park 	_8KB = 8ULL << 10,
44baa489faSSeongJae Park 	_1MB = 1ULL << 20,
45baa489faSSeongJae Park 	_2MB = 2ULL << 20,
46baa489faSSeongJae Park 	_4MB = 4ULL << 20,
47baa489faSSeongJae Park 	_1GB = 1ULL << 30,
48baa489faSSeongJae Park 	_2GB = 2ULL << 30,
49baa489faSSeongJae Park 	PMD = _2MB,
50baa489faSSeongJae Park 	PUD = _1GB,
51baa489faSSeongJae Park };
52baa489faSSeongJae Park 
53baa489faSSeongJae Park #define PTE page_size
54baa489faSSeongJae Park 
55baa489faSSeongJae Park #define MAKE_TEST(source_align, destination_align, size,	\
56baa489faSSeongJae Park 		  overlaps, should_fail, test_name)		\
57baa489faSSeongJae Park (struct test){							\
58baa489faSSeongJae Park 	.name = test_name,					\
59baa489faSSeongJae Park 	.config = {						\
60baa489faSSeongJae Park 		.src_alignment = source_align,			\
61baa489faSSeongJae Park 		.dest_alignment = destination_align,		\
62baa489faSSeongJae Park 		.region_size = size,				\
63baa489faSSeongJae Park 		.overlapping = overlaps,			\
64baa489faSSeongJae Park 	},							\
65baa489faSSeongJae Park 	.expect_failure = should_fail				\
66baa489faSSeongJae Park }
67baa489faSSeongJae Park 
68baa489faSSeongJae Park /*
69baa489faSSeongJae Park  * Returns false if the requested remap region overlaps with an
70baa489faSSeongJae Park  * existing mapping (e.g text, stack) else returns true.
71baa489faSSeongJae Park  */
is_remap_region_valid(void * addr,unsigned long long size)72baa489faSSeongJae Park static bool is_remap_region_valid(void *addr, unsigned long long size)
73baa489faSSeongJae Park {
74baa489faSSeongJae Park 	void *remap_addr = NULL;
75baa489faSSeongJae Park 	bool ret = true;
76baa489faSSeongJae Park 
77baa489faSSeongJae Park 	/* Use MAP_FIXED_NOREPLACE flag to ensure region is not mapped */
78baa489faSSeongJae Park 	remap_addr = mmap(addr, size, PROT_READ | PROT_WRITE,
79baa489faSSeongJae Park 					 MAP_FIXED_NOREPLACE | MAP_ANONYMOUS | MAP_SHARED,
80baa489faSSeongJae Park 					 -1, 0);
81baa489faSSeongJae Park 
82baa489faSSeongJae Park 	if (remap_addr == MAP_FAILED) {
83baa489faSSeongJae Park 		if (errno == EEXIST)
84baa489faSSeongJae Park 			ret = false;
85baa489faSSeongJae Park 	} else {
86baa489faSSeongJae Park 		munmap(remap_addr, size);
87baa489faSSeongJae Park 	}
88baa489faSSeongJae Park 
89baa489faSSeongJae Park 	return ret;
90baa489faSSeongJae Park }
91baa489faSSeongJae Park 
92baa489faSSeongJae Park /* Returns mmap_min_addr sysctl tunable from procfs */
get_mmap_min_addr(void)93baa489faSSeongJae Park static unsigned long long get_mmap_min_addr(void)
94baa489faSSeongJae Park {
95baa489faSSeongJae Park 	FILE *fp;
96baa489faSSeongJae Park 	int n_matched;
97baa489faSSeongJae Park 	static unsigned long long addr;
98baa489faSSeongJae Park 
99baa489faSSeongJae Park 	if (addr)
100baa489faSSeongJae Park 		return addr;
101baa489faSSeongJae Park 
102baa489faSSeongJae Park 	fp = fopen("/proc/sys/vm/mmap_min_addr", "r");
103baa489faSSeongJae Park 	if (fp == NULL) {
104baa489faSSeongJae Park 		ksft_print_msg("Failed to open /proc/sys/vm/mmap_min_addr: %s\n",
105baa489faSSeongJae Park 			strerror(errno));
106baa489faSSeongJae Park 		exit(KSFT_SKIP);
107baa489faSSeongJae Park 	}
108baa489faSSeongJae Park 
109baa489faSSeongJae Park 	n_matched = fscanf(fp, "%llu", &addr);
110baa489faSSeongJae Park 	if (n_matched != 1) {
111baa489faSSeongJae Park 		ksft_print_msg("Failed to read /proc/sys/vm/mmap_min_addr: %s\n",
112baa489faSSeongJae Park 			strerror(errno));
113baa489faSSeongJae Park 		fclose(fp);
114baa489faSSeongJae Park 		exit(KSFT_SKIP);
115baa489faSSeongJae Park 	}
116baa489faSSeongJae Park 
117baa489faSSeongJae Park 	fclose(fp);
118baa489faSSeongJae Park 	return addr;
119baa489faSSeongJae Park }
120baa489faSSeongJae Park 
121baa489faSSeongJae Park /*
122*da0618c1SLorenzo Stoakes  * Using /proc/self/maps, assert that the specified address range is contained
123*da0618c1SLorenzo Stoakes  * within a single mapping.
124*da0618c1SLorenzo Stoakes  */
is_range_mapped(FILE * maps_fp,void * start,void * end)125*da0618c1SLorenzo Stoakes static bool is_range_mapped(FILE *maps_fp, void *start, void *end)
126*da0618c1SLorenzo Stoakes {
127*da0618c1SLorenzo Stoakes 	char *line = NULL;
128*da0618c1SLorenzo Stoakes 	size_t len = 0;
129*da0618c1SLorenzo Stoakes 	bool success = false;
130*da0618c1SLorenzo Stoakes 
131*da0618c1SLorenzo Stoakes 	rewind(maps_fp);
132*da0618c1SLorenzo Stoakes 
133*da0618c1SLorenzo Stoakes 	while (getline(&line, &len, maps_fp) != -1) {
134*da0618c1SLorenzo Stoakes 		char *first = strtok(line, "- ");
135*da0618c1SLorenzo Stoakes 		void *first_val = (void *)strtol(first, NULL, 16);
136*da0618c1SLorenzo Stoakes 		char *second = strtok(NULL, "- ");
137*da0618c1SLorenzo Stoakes 		void *second_val = (void *) strtol(second, NULL, 16);
138*da0618c1SLorenzo Stoakes 
139*da0618c1SLorenzo Stoakes 		if (first_val <= start && second_val >= end) {
140*da0618c1SLorenzo Stoakes 			success = true;
141*da0618c1SLorenzo Stoakes 			break;
142*da0618c1SLorenzo Stoakes 		}
143*da0618c1SLorenzo Stoakes 	}
144*da0618c1SLorenzo Stoakes 
145*da0618c1SLorenzo Stoakes 	return success;
146*da0618c1SLorenzo Stoakes }
147*da0618c1SLorenzo Stoakes 
148*da0618c1SLorenzo Stoakes /*
149baa489faSSeongJae Park  * This test validates that merge is called when expanding a mapping.
150baa489faSSeongJae Park  * Mapping containing three pages is created, middle page is unmapped
151baa489faSSeongJae Park  * and then the mapping containing the first page is expanded so that
152baa489faSSeongJae Park  * it fills the created hole. The two parts should merge creating
153baa489faSSeongJae Park  * single mapping with three pages.
154baa489faSSeongJae Park  */
mremap_expand_merge(FILE * maps_fp,unsigned long page_size)155*da0618c1SLorenzo Stoakes static void mremap_expand_merge(FILE *maps_fp, unsigned long page_size)
156baa489faSSeongJae Park {
157baa489faSSeongJae Park 	char *test_name = "mremap expand merge";
158baa489faSSeongJae Park 	bool success = false;
159*da0618c1SLorenzo Stoakes 	char *remap, *start;
160*da0618c1SLorenzo Stoakes 
161*da0618c1SLorenzo Stoakes 	start = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE,
162baa489faSSeongJae Park 		     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
163baa489faSSeongJae Park 
164*da0618c1SLorenzo Stoakes 	if (start == MAP_FAILED) {
165*da0618c1SLorenzo Stoakes 		ksft_print_msg("mmap failed: %s\n", strerror(errno));
166*da0618c1SLorenzo Stoakes 		goto out;
167*da0618c1SLorenzo Stoakes 	}
168*da0618c1SLorenzo Stoakes 
169baa489faSSeongJae Park 	munmap(start + page_size, page_size);
170*da0618c1SLorenzo Stoakes 	remap = mremap(start, page_size, 2 * page_size, 0);
171*da0618c1SLorenzo Stoakes 	if (remap == MAP_FAILED) {
172*da0618c1SLorenzo Stoakes 		ksft_print_msg("mremap failed: %s\n", strerror(errno));
173*da0618c1SLorenzo Stoakes 		munmap(start, page_size);
174*da0618c1SLorenzo Stoakes 		munmap(start + 2 * page_size, page_size);
175*da0618c1SLorenzo Stoakes 		goto out;
176baa489faSSeongJae Park 	}
177baa489faSSeongJae Park 
178*da0618c1SLorenzo Stoakes 	success = is_range_mapped(maps_fp, start, start + 3 * page_size);
179*da0618c1SLorenzo Stoakes 	munmap(start, 3 * page_size);
180baa489faSSeongJae Park 
181*da0618c1SLorenzo Stoakes out:
182baa489faSSeongJae Park 	if (success)
183baa489faSSeongJae Park 		ksft_test_result_pass("%s\n", test_name);
184baa489faSSeongJae Park 	else
185baa489faSSeongJae Park 		ksft_test_result_fail("%s\n", test_name);
186*da0618c1SLorenzo Stoakes }
187*da0618c1SLorenzo Stoakes 
188*da0618c1SLorenzo Stoakes /*
189*da0618c1SLorenzo Stoakes  * Similar to mremap_expand_merge() except instead of removing the middle page,
190*da0618c1SLorenzo Stoakes  * we remove the last then attempt to remap offset from the second page. This
191*da0618c1SLorenzo Stoakes  * should result in the mapping being restored to its former state.
192*da0618c1SLorenzo Stoakes  */
mremap_expand_merge_offset(FILE * maps_fp,unsigned long page_size)193*da0618c1SLorenzo Stoakes static void mremap_expand_merge_offset(FILE *maps_fp, unsigned long page_size)
194*da0618c1SLorenzo Stoakes {
195*da0618c1SLorenzo Stoakes 
196*da0618c1SLorenzo Stoakes 	char *test_name = "mremap expand merge offset";
197*da0618c1SLorenzo Stoakes 	bool success = false;
198*da0618c1SLorenzo Stoakes 	char *remap, *start;
199*da0618c1SLorenzo Stoakes 
200*da0618c1SLorenzo Stoakes 	start = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE,
201*da0618c1SLorenzo Stoakes 		     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
202*da0618c1SLorenzo Stoakes 
203*da0618c1SLorenzo Stoakes 	if (start == MAP_FAILED) {
204*da0618c1SLorenzo Stoakes 		ksft_print_msg("mmap failed: %s\n", strerror(errno));
205*da0618c1SLorenzo Stoakes 		goto out;
206*da0618c1SLorenzo Stoakes 	}
207*da0618c1SLorenzo Stoakes 
208*da0618c1SLorenzo Stoakes 	/* Unmap final page to ensure we have space to expand. */
209*da0618c1SLorenzo Stoakes 	munmap(start + 2 * page_size, page_size);
210*da0618c1SLorenzo Stoakes 	remap = mremap(start + page_size, page_size, 2 * page_size, 0);
211*da0618c1SLorenzo Stoakes 	if (remap == MAP_FAILED) {
212*da0618c1SLorenzo Stoakes 		ksft_print_msg("mremap failed: %s\n", strerror(errno));
213*da0618c1SLorenzo Stoakes 		munmap(start, 2 * page_size);
214*da0618c1SLorenzo Stoakes 		goto out;
215*da0618c1SLorenzo Stoakes 	}
216*da0618c1SLorenzo Stoakes 
217*da0618c1SLorenzo Stoakes 	success = is_range_mapped(maps_fp, start, start + 3 * page_size);
218*da0618c1SLorenzo Stoakes 	munmap(start, 3 * page_size);
219*da0618c1SLorenzo Stoakes 
220*da0618c1SLorenzo Stoakes out:
221*da0618c1SLorenzo Stoakes 	if (success)
222*da0618c1SLorenzo Stoakes 		ksft_test_result_pass("%s\n", test_name);
223*da0618c1SLorenzo Stoakes 	else
224*da0618c1SLorenzo Stoakes 		ksft_test_result_fail("%s\n", test_name);
225baa489faSSeongJae Park }
226baa489faSSeongJae Park 
227baa489faSSeongJae Park /*
228baa489faSSeongJae Park  * Returns the start address of the mapping on success, else returns
229baa489faSSeongJae Park  * NULL on failure.
230baa489faSSeongJae Park  */
get_source_mapping(struct config c)231baa489faSSeongJae Park static void *get_source_mapping(struct config c)
232baa489faSSeongJae Park {
233baa489faSSeongJae Park 	unsigned long long addr = 0ULL;
234baa489faSSeongJae Park 	void *src_addr = NULL;
235baa489faSSeongJae Park 	unsigned long long mmap_min_addr;
236baa489faSSeongJae Park 
237baa489faSSeongJae Park 	mmap_min_addr = get_mmap_min_addr();
238baa489faSSeongJae Park 
239baa489faSSeongJae Park retry:
240baa489faSSeongJae Park 	addr += c.src_alignment;
241baa489faSSeongJae Park 	if (addr < mmap_min_addr)
242baa489faSSeongJae Park 		goto retry;
243baa489faSSeongJae Park 
244baa489faSSeongJae Park 	src_addr = mmap((void *) addr, c.region_size, PROT_READ | PROT_WRITE,
245baa489faSSeongJae Park 					MAP_FIXED_NOREPLACE | MAP_ANONYMOUS | MAP_SHARED,
246baa489faSSeongJae Park 					-1, 0);
247baa489faSSeongJae Park 	if (src_addr == MAP_FAILED) {
248baa489faSSeongJae Park 		if (errno == EPERM || errno == EEXIST)
249baa489faSSeongJae Park 			goto retry;
250baa489faSSeongJae Park 		goto error;
251baa489faSSeongJae Park 	}
252baa489faSSeongJae Park 	/*
253baa489faSSeongJae Park 	 * Check that the address is aligned to the specified alignment.
254baa489faSSeongJae Park 	 * Addresses which have alignments that are multiples of that
255baa489faSSeongJae Park 	 * specified are not considered valid. For instance, 1GB address is
256baa489faSSeongJae Park 	 * 2MB-aligned, however it will not be considered valid for a
257baa489faSSeongJae Park 	 * requested alignment of 2MB. This is done to reduce coincidental
258baa489faSSeongJae Park 	 * alignment in the tests.
259baa489faSSeongJae Park 	 */
260baa489faSSeongJae Park 	if (((unsigned long long) src_addr & (c.src_alignment - 1)) ||
261baa489faSSeongJae Park 			!((unsigned long long) src_addr & c.src_alignment)) {
262baa489faSSeongJae Park 		munmap(src_addr, c.region_size);
263baa489faSSeongJae Park 		goto retry;
264baa489faSSeongJae Park 	}
265baa489faSSeongJae Park 
266baa489faSSeongJae Park 	if (!src_addr)
267baa489faSSeongJae Park 		goto error;
268baa489faSSeongJae Park 
269baa489faSSeongJae Park 	return src_addr;
270baa489faSSeongJae Park error:
271baa489faSSeongJae Park 	ksft_print_msg("Failed to map source region: %s\n",
272baa489faSSeongJae Park 			strerror(errno));
273baa489faSSeongJae Park 	return NULL;
274baa489faSSeongJae Park }
275baa489faSSeongJae Park 
276baa489faSSeongJae Park /* Returns the time taken for the remap on success else returns -1. */
remap_region(struct config c,unsigned int threshold_mb,char pattern_seed)277baa489faSSeongJae Park static long long remap_region(struct config c, unsigned int threshold_mb,
278baa489faSSeongJae Park 			      char pattern_seed)
279baa489faSSeongJae Park {
280baa489faSSeongJae Park 	void *addr, *src_addr, *dest_addr;
281baa489faSSeongJae Park 	unsigned long long i;
282baa489faSSeongJae Park 	struct timespec t_start = {0, 0}, t_end = {0, 0};
283baa489faSSeongJae Park 	long long  start_ns, end_ns, align_mask, ret, offset;
284baa489faSSeongJae Park 	unsigned long long threshold;
285baa489faSSeongJae Park 
286baa489faSSeongJae Park 	if (threshold_mb == VALIDATION_NO_THRESHOLD)
287baa489faSSeongJae Park 		threshold = c.region_size;
288baa489faSSeongJae Park 	else
289baa489faSSeongJae Park 		threshold = MIN(threshold_mb * _1MB, c.region_size);
290baa489faSSeongJae Park 
291baa489faSSeongJae Park 	src_addr = get_source_mapping(c);
292baa489faSSeongJae Park 	if (!src_addr) {
293baa489faSSeongJae Park 		ret = -1;
294baa489faSSeongJae Park 		goto out;
295baa489faSSeongJae Park 	}
296baa489faSSeongJae Park 
297baa489faSSeongJae Park 	/* Set byte pattern */
298baa489faSSeongJae Park 	srand(pattern_seed);
299baa489faSSeongJae Park 	for (i = 0; i < threshold; i++)
300baa489faSSeongJae Park 		memset((char *) src_addr + i, (char) rand(), 1);
301baa489faSSeongJae Park 
302baa489faSSeongJae Park 	/* Mask to zero out lower bits of address for alignment */
303baa489faSSeongJae Park 	align_mask = ~(c.dest_alignment - 1);
304baa489faSSeongJae Park 	/* Offset of destination address from the end of the source region */
305baa489faSSeongJae Park 	offset = (c.overlapping) ? -c.dest_alignment : c.dest_alignment;
306baa489faSSeongJae Park 	addr = (void *) (((unsigned long long) src_addr + c.region_size
307baa489faSSeongJae Park 			  + offset) & align_mask);
308baa489faSSeongJae Park 
309baa489faSSeongJae Park 	/* See comment in get_source_mapping() */
310baa489faSSeongJae Park 	if (!((unsigned long long) addr & c.dest_alignment))
311baa489faSSeongJae Park 		addr = (void *) ((unsigned long long) addr | c.dest_alignment);
312baa489faSSeongJae Park 
313baa489faSSeongJae Park 	/* Don't destroy existing mappings unless expected to overlap */
314baa489faSSeongJae Park 	while (!is_remap_region_valid(addr, c.region_size) && !c.overlapping) {
315baa489faSSeongJae Park 		/* Check for unsigned overflow */
316baa489faSSeongJae Park 		if (addr + c.dest_alignment < addr) {
317baa489faSSeongJae Park 			ksft_print_msg("Couldn't find a valid region to remap to\n");
318baa489faSSeongJae Park 			ret = -1;
319baa489faSSeongJae Park 			goto out;
320baa489faSSeongJae Park 		}
321baa489faSSeongJae Park 		addr += c.dest_alignment;
322baa489faSSeongJae Park 	}
323baa489faSSeongJae Park 
324baa489faSSeongJae Park 	clock_gettime(CLOCK_MONOTONIC, &t_start);
325baa489faSSeongJae Park 	dest_addr = mremap(src_addr, c.region_size, c.region_size,
326baa489faSSeongJae Park 					  MREMAP_MAYMOVE|MREMAP_FIXED, (char *) addr);
327baa489faSSeongJae Park 	clock_gettime(CLOCK_MONOTONIC, &t_end);
328baa489faSSeongJae Park 
329baa489faSSeongJae Park 	if (dest_addr == MAP_FAILED) {
330baa489faSSeongJae Park 		ksft_print_msg("mremap failed: %s\n", strerror(errno));
331baa489faSSeongJae Park 		ret = -1;
332baa489faSSeongJae Park 		goto clean_up_src;
333baa489faSSeongJae Park 	}
334baa489faSSeongJae Park 
335baa489faSSeongJae Park 	/* Verify byte pattern after remapping */
336baa489faSSeongJae Park 	srand(pattern_seed);
337baa489faSSeongJae Park 	for (i = 0; i < threshold; i++) {
338baa489faSSeongJae Park 		char c = (char) rand();
339baa489faSSeongJae Park 
340baa489faSSeongJae Park 		if (((char *) dest_addr)[i] != c) {
341baa489faSSeongJae Park 			ksft_print_msg("Data after remap doesn't match at offset %d\n",
342baa489faSSeongJae Park 				       i);
343baa489faSSeongJae Park 			ksft_print_msg("Expected: %#x\t Got: %#x\n", c & 0xff,
344baa489faSSeongJae Park 					((char *) dest_addr)[i] & 0xff);
345baa489faSSeongJae Park 			ret = -1;
346baa489faSSeongJae Park 			goto clean_up_dest;
347baa489faSSeongJae Park 		}
348baa489faSSeongJae Park 	}
349baa489faSSeongJae Park 
350baa489faSSeongJae Park 	start_ns = t_start.tv_sec * NS_PER_SEC + t_start.tv_nsec;
351baa489faSSeongJae Park 	end_ns = t_end.tv_sec * NS_PER_SEC + t_end.tv_nsec;
352baa489faSSeongJae Park 	ret = end_ns - start_ns;
353baa489faSSeongJae Park 
354baa489faSSeongJae Park /*
355baa489faSSeongJae Park  * Since the destination address is specified using MREMAP_FIXED, subsequent
356baa489faSSeongJae Park  * mremap will unmap any previous mapping at the address range specified by
357baa489faSSeongJae Park  * dest_addr and region_size. This significantly affects the remap time of
358baa489faSSeongJae Park  * subsequent tests. So we clean up mappings after each test.
359baa489faSSeongJae Park  */
360baa489faSSeongJae Park clean_up_dest:
361baa489faSSeongJae Park 	munmap(dest_addr, c.region_size);
362baa489faSSeongJae Park clean_up_src:
363baa489faSSeongJae Park 	munmap(src_addr, c.region_size);
364baa489faSSeongJae Park out:
365baa489faSSeongJae Park 	return ret;
366baa489faSSeongJae Park }
367baa489faSSeongJae Park 
run_mremap_test_case(struct test test_case,int * failures,unsigned int threshold_mb,unsigned int pattern_seed)368baa489faSSeongJae Park static void run_mremap_test_case(struct test test_case, int *failures,
369baa489faSSeongJae Park 				 unsigned int threshold_mb,
370baa489faSSeongJae Park 				 unsigned int pattern_seed)
371baa489faSSeongJae Park {
372baa489faSSeongJae Park 	long long remap_time = remap_region(test_case.config, threshold_mb,
373baa489faSSeongJae Park 					    pattern_seed);
374baa489faSSeongJae Park 
375baa489faSSeongJae Park 	if (remap_time < 0) {
376baa489faSSeongJae Park 		if (test_case.expect_failure)
377baa489faSSeongJae Park 			ksft_test_result_xfail("%s\n\tExpected mremap failure\n",
378baa489faSSeongJae Park 					      test_case.name);
379baa489faSSeongJae Park 		else {
380baa489faSSeongJae Park 			ksft_test_result_fail("%s\n", test_case.name);
381baa489faSSeongJae Park 			*failures += 1;
382baa489faSSeongJae Park 		}
383baa489faSSeongJae Park 	} else {
384baa489faSSeongJae Park 		/*
385baa489faSSeongJae Park 		 * Comparing mremap time is only applicable if entire region
386baa489faSSeongJae Park 		 * was faulted in.
387baa489faSSeongJae Park 		 */
388baa489faSSeongJae Park 		if (threshold_mb == VALIDATION_NO_THRESHOLD ||
389baa489faSSeongJae Park 		    test_case.config.region_size <= threshold_mb * _1MB)
390baa489faSSeongJae Park 			ksft_test_result_pass("%s\n\tmremap time: %12lldns\n",
391baa489faSSeongJae Park 					      test_case.name, remap_time);
392baa489faSSeongJae Park 		else
393baa489faSSeongJae Park 			ksft_test_result_pass("%s\n", test_case.name);
394baa489faSSeongJae Park 	}
395baa489faSSeongJae Park }
396baa489faSSeongJae Park 
usage(const char * cmd)397baa489faSSeongJae Park static void usage(const char *cmd)
398baa489faSSeongJae Park {
399baa489faSSeongJae Park 	fprintf(stderr,
400baa489faSSeongJae Park 		"Usage: %s [[-t <threshold_mb>] [-p <pattern_seed>]]\n"
401baa489faSSeongJae Park 		"-t\t only validate threshold_mb of the remapped region\n"
402baa489faSSeongJae Park 		"  \t if 0 is supplied no threshold is used; all tests\n"
403baa489faSSeongJae Park 		"  \t are run and remapped regions validated fully.\n"
404baa489faSSeongJae Park 		"  \t The default threshold used is 4MB.\n"
405baa489faSSeongJae Park 		"-p\t provide a seed to generate the random pattern for\n"
406baa489faSSeongJae Park 		"  \t validating the remapped region.\n", cmd);
407baa489faSSeongJae Park }
408baa489faSSeongJae Park 
parse_args(int argc,char ** argv,unsigned int * threshold_mb,unsigned int * pattern_seed)409baa489faSSeongJae Park static int parse_args(int argc, char **argv, unsigned int *threshold_mb,
410baa489faSSeongJae Park 		      unsigned int *pattern_seed)
411baa489faSSeongJae Park {
412baa489faSSeongJae Park 	const char *optstr = "t:p:";
413baa489faSSeongJae Park 	int opt;
414baa489faSSeongJae Park 
415baa489faSSeongJae Park 	while ((opt = getopt(argc, argv, optstr)) != -1) {
416baa489faSSeongJae Park 		switch (opt) {
417baa489faSSeongJae Park 		case 't':
418baa489faSSeongJae Park 			*threshold_mb = atoi(optarg);
419baa489faSSeongJae Park 			break;
420baa489faSSeongJae Park 		case 'p':
421baa489faSSeongJae Park 			*pattern_seed = atoi(optarg);
422baa489faSSeongJae Park 			break;
423baa489faSSeongJae Park 		default:
424baa489faSSeongJae Park 			usage(argv[0]);
425baa489faSSeongJae Park 			return -1;
426baa489faSSeongJae Park 		}
427baa489faSSeongJae Park 	}
428baa489faSSeongJae Park 
429baa489faSSeongJae Park 	if (optind < argc) {
430baa489faSSeongJae Park 		usage(argv[0]);
431baa489faSSeongJae Park 		return -1;
432baa489faSSeongJae Park 	}
433baa489faSSeongJae Park 
434baa489faSSeongJae Park 	return 0;
435baa489faSSeongJae Park }
436baa489faSSeongJae Park 
437baa489faSSeongJae Park #define MAX_TEST 13
438baa489faSSeongJae Park #define MAX_PERF_TEST 3
main(int argc,char ** argv)439baa489faSSeongJae Park int main(int argc, char **argv)
440baa489faSSeongJae Park {
441baa489faSSeongJae Park 	int failures = 0;
442baa489faSSeongJae Park 	int i, run_perf_tests;
443baa489faSSeongJae Park 	unsigned int threshold_mb = VALIDATION_DEFAULT_THRESHOLD;
444baa489faSSeongJae Park 	unsigned int pattern_seed;
445*da0618c1SLorenzo Stoakes 	int num_expand_tests = 2;
446baa489faSSeongJae Park 	struct test test_cases[MAX_TEST];
447baa489faSSeongJae Park 	struct test perf_test_cases[MAX_PERF_TEST];
448baa489faSSeongJae Park 	int page_size;
449baa489faSSeongJae Park 	time_t t;
450*da0618c1SLorenzo Stoakes 	FILE *maps_fp;
451baa489faSSeongJae Park 
452baa489faSSeongJae Park 	pattern_seed = (unsigned int) time(&t);
453baa489faSSeongJae Park 
454baa489faSSeongJae Park 	if (parse_args(argc, argv, &threshold_mb, &pattern_seed) < 0)
455baa489faSSeongJae Park 		exit(EXIT_FAILURE);
456baa489faSSeongJae Park 
457baa489faSSeongJae Park 	ksft_print_msg("Test configs:\n\tthreshold_mb=%u\n\tpattern_seed=%u\n\n",
458baa489faSSeongJae Park 		       threshold_mb, pattern_seed);
459baa489faSSeongJae Park 
460baa489faSSeongJae Park 	page_size = sysconf(_SC_PAGESIZE);
461baa489faSSeongJae Park 
462baa489faSSeongJae Park 	/* Expected mremap failures */
463baa489faSSeongJae Park 	test_cases[0] =	MAKE_TEST(page_size, page_size, page_size,
464baa489faSSeongJae Park 				  OVERLAPPING, EXPECT_FAILURE,
465baa489faSSeongJae Park 				  "mremap - Source and Destination Regions Overlapping");
466baa489faSSeongJae Park 
467baa489faSSeongJae Park 	test_cases[1] = MAKE_TEST(page_size, page_size/4, page_size,
468baa489faSSeongJae Park 				  NON_OVERLAPPING, EXPECT_FAILURE,
469baa489faSSeongJae Park 				  "mremap - Destination Address Misaligned (1KB-aligned)");
470baa489faSSeongJae Park 	test_cases[2] = MAKE_TEST(page_size/4, page_size, page_size,
471baa489faSSeongJae Park 				  NON_OVERLAPPING, EXPECT_FAILURE,
472baa489faSSeongJae Park 				  "mremap - Source Address Misaligned (1KB-aligned)");
473baa489faSSeongJae Park 
474baa489faSSeongJae Park 	/* Src addr PTE aligned */
475baa489faSSeongJae Park 	test_cases[3] = MAKE_TEST(PTE, PTE, PTE * 2,
476baa489faSSeongJae Park 				  NON_OVERLAPPING, EXPECT_SUCCESS,
477baa489faSSeongJae Park 				  "8KB mremap - Source PTE-aligned, Destination PTE-aligned");
478baa489faSSeongJae Park 
479baa489faSSeongJae Park 	/* Src addr 1MB aligned */
480baa489faSSeongJae Park 	test_cases[4] = MAKE_TEST(_1MB, PTE, _2MB, NON_OVERLAPPING, EXPECT_SUCCESS,
481baa489faSSeongJae Park 				  "2MB mremap - Source 1MB-aligned, Destination PTE-aligned");
482baa489faSSeongJae Park 	test_cases[5] = MAKE_TEST(_1MB, _1MB, _2MB, NON_OVERLAPPING, EXPECT_SUCCESS,
483baa489faSSeongJae Park 				  "2MB mremap - Source 1MB-aligned, Destination 1MB-aligned");
484baa489faSSeongJae Park 
485baa489faSSeongJae Park 	/* Src addr PMD aligned */
486baa489faSSeongJae Park 	test_cases[6] = MAKE_TEST(PMD, PTE, _4MB, NON_OVERLAPPING, EXPECT_SUCCESS,
487baa489faSSeongJae Park 				  "4MB mremap - Source PMD-aligned, Destination PTE-aligned");
488baa489faSSeongJae Park 	test_cases[7] =	MAKE_TEST(PMD, _1MB, _4MB, NON_OVERLAPPING, EXPECT_SUCCESS,
489baa489faSSeongJae Park 				  "4MB mremap - Source PMD-aligned, Destination 1MB-aligned");
490baa489faSSeongJae Park 	test_cases[8] = MAKE_TEST(PMD, PMD, _4MB, NON_OVERLAPPING, EXPECT_SUCCESS,
491baa489faSSeongJae Park 				  "4MB mremap - Source PMD-aligned, Destination PMD-aligned");
492baa489faSSeongJae Park 
493baa489faSSeongJae Park 	/* Src addr PUD aligned */
494baa489faSSeongJae Park 	test_cases[9] = MAKE_TEST(PUD, PTE, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
495baa489faSSeongJae Park 				  "2GB mremap - Source PUD-aligned, Destination PTE-aligned");
496baa489faSSeongJae Park 	test_cases[10] = MAKE_TEST(PUD, _1MB, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
497baa489faSSeongJae Park 				   "2GB mremap - Source PUD-aligned, Destination 1MB-aligned");
498baa489faSSeongJae Park 	test_cases[11] = MAKE_TEST(PUD, PMD, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
499baa489faSSeongJae Park 				   "2GB mremap - Source PUD-aligned, Destination PMD-aligned");
500baa489faSSeongJae Park 	test_cases[12] = MAKE_TEST(PUD, PUD, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
501baa489faSSeongJae Park 				   "2GB mremap - Source PUD-aligned, Destination PUD-aligned");
502baa489faSSeongJae Park 
503baa489faSSeongJae Park 	perf_test_cases[0] =  MAKE_TEST(page_size, page_size, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS,
504baa489faSSeongJae Park 					"1GB mremap - Source PTE-aligned, Destination PTE-aligned");
505baa489faSSeongJae Park 	/*
506baa489faSSeongJae Park 	 * mremap 1GB region - Page table level aligned time
507baa489faSSeongJae Park 	 * comparison.
508baa489faSSeongJae Park 	 */
509baa489faSSeongJae Park 	perf_test_cases[1] = MAKE_TEST(PMD, PMD, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS,
510baa489faSSeongJae Park 				       "1GB mremap - Source PMD-aligned, Destination PMD-aligned");
511baa489faSSeongJae Park 	perf_test_cases[2] = MAKE_TEST(PUD, PUD, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS,
512baa489faSSeongJae Park 				       "1GB mremap - Source PUD-aligned, Destination PUD-aligned");
513baa489faSSeongJae Park 
514baa489faSSeongJae Park 	run_perf_tests =  (threshold_mb == VALIDATION_NO_THRESHOLD) ||
515baa489faSSeongJae Park 				(threshold_mb * _1MB >= _1GB);
516baa489faSSeongJae Park 
517baa489faSSeongJae Park 	ksft_set_plan(ARRAY_SIZE(test_cases) + (run_perf_tests ?
518baa489faSSeongJae Park 		      ARRAY_SIZE(perf_test_cases) : 0) + num_expand_tests);
519baa489faSSeongJae Park 
520baa489faSSeongJae Park 	for (i = 0; i < ARRAY_SIZE(test_cases); i++)
521baa489faSSeongJae Park 		run_mremap_test_case(test_cases[i], &failures, threshold_mb,
522baa489faSSeongJae Park 				     pattern_seed);
523baa489faSSeongJae Park 
524*da0618c1SLorenzo Stoakes 	maps_fp = fopen("/proc/self/maps", "r");
525*da0618c1SLorenzo Stoakes 
526*da0618c1SLorenzo Stoakes 	if (maps_fp == NULL) {
527*da0618c1SLorenzo Stoakes 		ksft_print_msg("Failed to read /proc/self/maps: %s\n", strerror(errno));
528*da0618c1SLorenzo Stoakes 		exit(KSFT_FAIL);
529*da0618c1SLorenzo Stoakes 	}
530*da0618c1SLorenzo Stoakes 
531*da0618c1SLorenzo Stoakes 	mremap_expand_merge(maps_fp, page_size);
532*da0618c1SLorenzo Stoakes 	mremap_expand_merge_offset(maps_fp, page_size);
533*da0618c1SLorenzo Stoakes 
534*da0618c1SLorenzo Stoakes 	fclose(maps_fp);
535baa489faSSeongJae Park 
536baa489faSSeongJae Park 	if (run_perf_tests) {
537baa489faSSeongJae Park 		ksft_print_msg("\n%s\n",
538baa489faSSeongJae Park 		 "mremap HAVE_MOVE_PMD/PUD optimization time comparison for 1GB region:");
539baa489faSSeongJae Park 		for (i = 0; i < ARRAY_SIZE(perf_test_cases); i++)
540baa489faSSeongJae Park 			run_mremap_test_case(perf_test_cases[i], &failures,
541baa489faSSeongJae Park 					     threshold_mb, pattern_seed);
542baa489faSSeongJae Park 	}
543baa489faSSeongJae Park 
544baa489faSSeongJae Park 	if (failures > 0)
545baa489faSSeongJae Park 		ksft_exit_fail();
546baa489faSSeongJae Park 	else
547baa489faSSeongJae Park 		ksft_exit_pass();
548baa489faSSeongJae Park }
549