1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2020 Google LLC
13 #include "../kselftest.h"
15 #define EXPECT_SUCCESS 0
16 #define EXPECT_FAILURE 1
17 #define NON_OVERLAPPING 0
19 #define NS_PER_SEC 1000000000ULL
20 #define VALIDATION_DEFAULT_THRESHOLD 4 /* 4MB */
21 #define VALIDATION_NO_THRESHOLD 0 /* Verify the entire region */
23 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
24 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
27 unsigned long long src_alignment;
28 unsigned long long dest_alignment;
29 unsigned long long region_size;
40 _1KB = 1ULL << 10, /* 1KB -> not page aligned */
53 #define MAKE_TEST(source_align, destination_align, size, \
54 overlaps, should_fail, test_name) \
58 .src_alignment = source_align, \
59 .dest_alignment = destination_align, \
60 .region_size = size, \
61 .overlapping = overlaps, \
63 .expect_failure = should_fail \
67 * Returns the start address of the mapping on success, else returns
70 static void *get_source_mapping(struct config c)
72 unsigned long long addr = 0ULL;
73 void *src_addr = NULL;
75 addr += c.src_alignment;
76 src_addr = mmap((void *) addr, c.region_size, PROT_READ | PROT_WRITE,
77 MAP_FIXED | MAP_ANONYMOUS | MAP_SHARED, -1, 0);
78 if (src_addr == MAP_FAILED) {
84 * Check that the address is aligned to the specified alignment.
85 * Addresses which have alignments that are multiples of that
86 * specified are not considered valid. For instance, 1GB address is
87 * 2MB-aligned, however it will not be considered valid for a
88 * requested alignment of 2MB. This is done to reduce coincidental
89 * alignment in the tests.
91 if (((unsigned long long) src_addr & (c.src_alignment - 1)) ||
92 !((unsigned long long) src_addr & c.src_alignment))
100 ksft_print_msg("Failed to map source region: %s\n",
105 /* Returns the time taken for the remap on success else returns -1. */
106 static long long remap_region(struct config c, unsigned int threshold_mb,
109 void *addr, *src_addr, *dest_addr;
110 unsigned long long i;
111 struct timespec t_start = {0, 0}, t_end = {0, 0};
112 long long start_ns, end_ns, align_mask, ret, offset;
113 unsigned long long threshold;
115 if (threshold_mb == VALIDATION_NO_THRESHOLD)
116 threshold = c.region_size;
118 threshold = MIN(threshold_mb * _1MB, c.region_size);
120 src_addr = get_source_mapping(c);
126 /* Set byte pattern */
128 for (i = 0; i < threshold; i++)
129 memset((char *) src_addr + i, (char) rand(), 1);
131 /* Mask to zero out lower bits of address for alignment */
132 align_mask = ~(c.dest_alignment - 1);
133 /* Offset of destination address from the end of the source region */
134 offset = (c.overlapping) ? -c.dest_alignment : c.dest_alignment;
135 addr = (void *) (((unsigned long long) src_addr + c.region_size
136 + offset) & align_mask);
138 /* See comment in get_source_mapping() */
139 if (!((unsigned long long) addr & c.dest_alignment))
140 addr = (void *) ((unsigned long long) addr | c.dest_alignment);
142 clock_gettime(CLOCK_MONOTONIC, &t_start);
143 dest_addr = mremap(src_addr, c.region_size, c.region_size,
144 MREMAP_MAYMOVE|MREMAP_FIXED, (char *) addr);
145 clock_gettime(CLOCK_MONOTONIC, &t_end);
147 if (dest_addr == MAP_FAILED) {
148 ksft_print_msg("mremap failed: %s\n", strerror(errno));
153 /* Verify byte pattern after remapping */
155 for (i = 0; i < threshold; i++) {
156 char c = (char) rand();
158 if (((char *) dest_addr)[i] != c) {
159 ksft_print_msg("Data after remap doesn't match at offset %d\n",
161 ksft_print_msg("Expected: %#x\t Got: %#x\n", c & 0xff,
162 ((char *) dest_addr)[i] & 0xff);
168 start_ns = t_start.tv_sec * NS_PER_SEC + t_start.tv_nsec;
169 end_ns = t_end.tv_sec * NS_PER_SEC + t_end.tv_nsec;
170 ret = end_ns - start_ns;
173 * Since the destination address is specified using MREMAP_FIXED, subsequent
174 * mremap will unmap any previous mapping at the address range specified by
175 * dest_addr and region_size. This significantly affects the remap time of
176 * subsequent tests. So we clean up mappings after each test.
179 munmap(dest_addr, c.region_size);
181 munmap(src_addr, c.region_size);
186 static void run_mremap_test_case(struct test test_case, int *failures,
187 unsigned int threshold_mb,
188 unsigned int pattern_seed)
190 long long remap_time = remap_region(test_case.config, threshold_mb,
193 if (remap_time < 0) {
194 if (test_case.expect_failure)
195 ksft_test_result_pass("%s\n\tExpected mremap failure\n",
198 ksft_test_result_fail("%s\n", test_case.name);
203 * Comparing mremap time is only applicable if entire region
206 if (threshold_mb == VALIDATION_NO_THRESHOLD ||
207 test_case.config.region_size <= threshold_mb * _1MB)
208 ksft_test_result_pass("%s\n\tmremap time: %12lldns\n",
209 test_case.name, remap_time);
211 ksft_test_result_pass("%s\n", test_case.name);
215 static void usage(const char *cmd)
218 "Usage: %s [[-t <threshold_mb>] [-p <pattern_seed>]]\n"
219 "-t\t only validate threshold_mb of the remapped region\n"
220 " \t if 0 is supplied no threshold is used; all tests\n"
221 " \t are run and remapped regions validated fully.\n"
222 " \t The default threshold used is 4MB.\n"
223 "-p\t provide a seed to generate the random pattern for\n"
224 " \t validating the remapped region.\n", cmd);
227 static int parse_args(int argc, char **argv, unsigned int *threshold_mb,
228 unsigned int *pattern_seed)
230 const char *optstr = "t:p:";
233 while ((opt = getopt(argc, argv, optstr)) != -1) {
236 *threshold_mb = atoi(optarg);
239 *pattern_seed = atoi(optarg);
255 int main(int argc, char **argv)
258 int i, run_perf_tests;
259 unsigned int threshold_mb = VALIDATION_DEFAULT_THRESHOLD;
260 unsigned int pattern_seed;
263 pattern_seed = (unsigned int) time(&t);
265 if (parse_args(argc, argv, &threshold_mb, &pattern_seed) < 0)
268 ksft_print_msg("Test configs:\n\tthreshold_mb=%u\n\tpattern_seed=%u\n\n",
269 threshold_mb, pattern_seed);
271 struct test test_cases[] = {
272 /* Expected mremap failures */
273 MAKE_TEST(_4KB, _4KB, _4KB, OVERLAPPING, EXPECT_FAILURE,
274 "mremap - Source and Destination Regions Overlapping"),
275 MAKE_TEST(_4KB, _1KB, _4KB, NON_OVERLAPPING, EXPECT_FAILURE,
276 "mremap - Destination Address Misaligned (1KB-aligned)"),
277 MAKE_TEST(_1KB, _4KB, _4KB, NON_OVERLAPPING, EXPECT_FAILURE,
278 "mremap - Source Address Misaligned (1KB-aligned)"),
280 /* Src addr PTE aligned */
281 MAKE_TEST(PTE, PTE, _8KB, NON_OVERLAPPING, EXPECT_SUCCESS,
282 "8KB mremap - Source PTE-aligned, Destination PTE-aligned"),
284 /* Src addr 1MB aligned */
285 MAKE_TEST(_1MB, PTE, _2MB, NON_OVERLAPPING, EXPECT_SUCCESS,
286 "2MB mremap - Source 1MB-aligned, Destination PTE-aligned"),
287 MAKE_TEST(_1MB, _1MB, _2MB, NON_OVERLAPPING, EXPECT_SUCCESS,
288 "2MB mremap - Source 1MB-aligned, Destination 1MB-aligned"),
290 /* Src addr PMD aligned */
291 MAKE_TEST(PMD, PTE, _4MB, NON_OVERLAPPING, EXPECT_SUCCESS,
292 "4MB mremap - Source PMD-aligned, Destination PTE-aligned"),
293 MAKE_TEST(PMD, _1MB, _4MB, NON_OVERLAPPING, EXPECT_SUCCESS,
294 "4MB mremap - Source PMD-aligned, Destination 1MB-aligned"),
295 MAKE_TEST(PMD, PMD, _4MB, NON_OVERLAPPING, EXPECT_SUCCESS,
296 "4MB mremap - Source PMD-aligned, Destination PMD-aligned"),
298 /* Src addr PUD aligned */
299 MAKE_TEST(PUD, PTE, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
300 "2GB mremap - Source PUD-aligned, Destination PTE-aligned"),
301 MAKE_TEST(PUD, _1MB, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
302 "2GB mremap - Source PUD-aligned, Destination 1MB-aligned"),
303 MAKE_TEST(PUD, PMD, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
304 "2GB mremap - Source PUD-aligned, Destination PMD-aligned"),
305 MAKE_TEST(PUD, PUD, _2GB, NON_OVERLAPPING, EXPECT_SUCCESS,
306 "2GB mremap - Source PUD-aligned, Destination PUD-aligned"),
309 struct test perf_test_cases[] = {
311 * mremap 1GB region - Page table level aligned time
314 MAKE_TEST(PTE, PTE, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS,
315 "1GB mremap - Source PTE-aligned, Destination PTE-aligned"),
316 MAKE_TEST(PMD, PMD, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS,
317 "1GB mremap - Source PMD-aligned, Destination PMD-aligned"),
318 MAKE_TEST(PUD, PUD, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS,
319 "1GB mremap - Source PUD-aligned, Destination PUD-aligned"),
322 run_perf_tests = (threshold_mb == VALIDATION_NO_THRESHOLD) ||
323 (threshold_mb * _1MB >= _1GB);
325 ksft_set_plan(ARRAY_SIZE(test_cases) + (run_perf_tests ?
326 ARRAY_SIZE(perf_test_cases) : 0));
328 for (i = 0; i < ARRAY_SIZE(test_cases); i++)
329 run_mremap_test_case(test_cases[i], &failures, threshold_mb,
332 if (run_perf_tests) {
333 ksft_print_msg("\n%s\n",
334 "mremap HAVE_MOVE_PMD/PUD optimization time comparison for 1GB region:");
335 for (i = 0; i < ARRAY_SIZE(perf_test_cases); i++)
336 run_mremap_test_case(perf_test_cases[i], &failures,
337 threshold_mb, pattern_seed);