selftests: vm: add COW time test for KSM pages
[linux-2.6-microblaze.git] / tools / testing / selftests / vm / ksm_tests.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <sys/mman.h>
4 #include <stdbool.h>
5 #include <time.h>
6 #include <string.h>
7 #include <numa.h>
8
9 #include "../kselftest.h"
10 #include "../../../../include/vdso/time64.h"
11
12 #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"
13 #define KSM_FP(s) (KSM_SYSFS_PATH s)
14 #define KSM_SCAN_LIMIT_SEC_DEFAULT 120
15 #define KSM_PAGE_COUNT_DEFAULT 10l
16 #define KSM_PROT_STR_DEFAULT "rw"
17 #define KSM_USE_ZERO_PAGES_DEFAULT false
18 #define KSM_MERGE_ACROSS_NODES_DEFAULT true
19 #define MB (1ul << 20)
20
21 struct ksm_sysfs {
22         unsigned long max_page_sharing;
23         unsigned long merge_across_nodes;
24         unsigned long pages_to_scan;
25         unsigned long run;
26         unsigned long sleep_millisecs;
27         unsigned long stable_node_chains_prune_millisecs;
28         unsigned long use_zero_pages;
29 };
30
31 enum ksm_test_name {
32         CHECK_KSM_MERGE,
33         CHECK_KSM_UNMERGE,
34         CHECK_KSM_ZERO_PAGE_MERGE,
35         CHECK_KSM_NUMA_MERGE,
36         KSM_MERGE_TIME,
37         KSM_COW_TIME
38 };
39
40 static int ksm_write_sysfs(const char *file_path, unsigned long val)
41 {
42         FILE *f = fopen(file_path, "w");
43
44         if (!f) {
45                 fprintf(stderr, "f %s\n", file_path);
46                 perror("fopen");
47                 return 1;
48         }
49         if (fprintf(f, "%lu", val) < 0) {
50                 perror("fprintf");
51                 return 1;
52         }
53         fclose(f);
54
55         return 0;
56 }
57
58 static int ksm_read_sysfs(const char *file_path, unsigned long *val)
59 {
60         FILE *f = fopen(file_path, "r");
61
62         if (!f) {
63                 fprintf(stderr, "f %s\n", file_path);
64                 perror("fopen");
65                 return 1;
66         }
67         if (fscanf(f, "%lu", val) != 1) {
68                 perror("fscanf");
69                 return 1;
70         }
71         fclose(f);
72
73         return 0;
74 }
75
76 static int str_to_prot(char *prot_str)
77 {
78         int prot = 0;
79
80         if ((strchr(prot_str, 'r')) != NULL)
81                 prot |= PROT_READ;
82         if ((strchr(prot_str, 'w')) != NULL)
83                 prot |= PROT_WRITE;
84         if ((strchr(prot_str, 'x')) != NULL)
85                 prot |= PROT_EXEC;
86
87         return prot;
88 }
89
90 static void print_help(void)
91 {
92         printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n"
93                "[-z use_zero_pages] [-m merge_across_nodes] [-s size]\n");
94
95         printf("Supported <test type>:\n"
96                " -M (page merging)\n"
97                " -Z (zero pages merging)\n"
98                " -N (merging of pages in different NUMA nodes)\n"
99                " -U (page unmerging)\n"
100                " -P evaluate merging time and speed.\n"
101                "    For this test, the size of duplicated memory area (in MiB)\n"
102                "    must be provided using -s option\n"
103                " -C evaluate the time required to break COW of merged pages.\n\n");
104
105         printf(" -a: specify the access protections of pages.\n"
106                "     <prot> must be of the form [rwx].\n"
107                "     Default: %s\n", KSM_PROT_STR_DEFAULT);
108         printf(" -p: specify the number of pages to test.\n"
109                "     Default: %ld\n", KSM_PAGE_COUNT_DEFAULT);
110         printf(" -l: limit the maximum running time (in seconds) for a test.\n"
111                "     Default: %d seconds\n", KSM_SCAN_LIMIT_SEC_DEFAULT);
112         printf(" -z: change use_zero_pages tunable\n"
113                "     Default: %d\n", KSM_USE_ZERO_PAGES_DEFAULT);
114         printf(" -m: change merge_across_nodes tunable\n"
115                "     Default: %d\n", KSM_MERGE_ACROSS_NODES_DEFAULT);
116         printf(" -s: the size of duplicated memory area (in MiB)\n");
117
118         exit(0);
119 }
120
121 static void  *allocate_memory(void *ptr, int prot, int mapping, char data, size_t map_size)
122 {
123         void *map_ptr = mmap(ptr, map_size, PROT_WRITE, mapping, -1, 0);
124
125         if (!map_ptr) {
126                 perror("mmap");
127                 return NULL;
128         }
129         memset(map_ptr, data, map_size);
130         if (mprotect(map_ptr, map_size, prot)) {
131                 perror("mprotect");
132                 munmap(map_ptr, map_size);
133                 return NULL;
134         }
135
136         return map_ptr;
137 }
138
139 static int ksm_do_scan(int scan_count, struct timespec start_time, int timeout)
140 {
141         struct timespec cur_time;
142         unsigned long cur_scan, init_scan;
143
144         if (ksm_read_sysfs(KSM_FP("full_scans"), &init_scan))
145                 return 1;
146         cur_scan = init_scan;
147
148         while (cur_scan < init_scan + scan_count) {
149                 if (ksm_read_sysfs(KSM_FP("full_scans"), &cur_scan))
150                         return 1;
151                 if (clock_gettime(CLOCK_MONOTONIC_RAW, &cur_time)) {
152                         perror("clock_gettime");
153                         return 1;
154                 }
155                 if ((cur_time.tv_sec - start_time.tv_sec) > timeout) {
156                         printf("Scan time limit exceeded\n");
157                         return 1;
158                 }
159         }
160
161         return 0;
162 }
163
164 static int ksm_merge_pages(void *addr, size_t size, struct timespec start_time, int timeout)
165 {
166         if (madvise(addr, size, MADV_MERGEABLE)) {
167                 perror("madvise");
168                 return 1;
169         }
170         if (ksm_write_sysfs(KSM_FP("run"), 1))
171                 return 1;
172
173         /* Since merging occurs only after 2 scans, make sure to get at least 2 full scans */
174         if (ksm_do_scan(2, start_time, timeout))
175                 return 1;
176
177         return 0;
178 }
179
180 static bool assert_ksm_pages_count(long dupl_page_count)
181 {
182         unsigned long max_page_sharing, pages_sharing, pages_shared;
183
184         if (ksm_read_sysfs(KSM_FP("pages_shared"), &pages_shared) ||
185             ksm_read_sysfs(KSM_FP("pages_sharing"), &pages_sharing) ||
186             ksm_read_sysfs(KSM_FP("max_page_sharing"), &max_page_sharing))
187                 return false;
188
189         /*
190          * Since there must be at least 2 pages for merging and 1 page can be
191          * shared with the limited number of pages (max_page_sharing), sometimes
192          * there are 'leftover' pages that cannot be merged. For example, if there
193          * are 11 pages and max_page_sharing = 10, then only 10 pages will be
194          * merged and the 11th page won't be affected. As a result, when the number
195          * of duplicate pages is divided by max_page_sharing and the remainder is 1,
196          * pages_shared and pages_sharing values will be equal between dupl_page_count
197          * and dupl_page_count - 1.
198          */
199         if (dupl_page_count % max_page_sharing == 1 || dupl_page_count % max_page_sharing == 0) {
200                 if (pages_shared == dupl_page_count / max_page_sharing &&
201                     pages_sharing == pages_shared * (max_page_sharing - 1))
202                         return true;
203         } else {
204                 if (pages_shared == (dupl_page_count / max_page_sharing + 1) &&
205                     pages_sharing == dupl_page_count - pages_shared)
206                         return true;
207         }
208
209         return false;
210 }
211
212 static int ksm_save_def(struct ksm_sysfs *ksm_sysfs)
213 {
214         if (ksm_read_sysfs(KSM_FP("max_page_sharing"), &ksm_sysfs->max_page_sharing) ||
215             ksm_read_sysfs(KSM_FP("merge_across_nodes"), &ksm_sysfs->merge_across_nodes) ||
216             ksm_read_sysfs(KSM_FP("sleep_millisecs"), &ksm_sysfs->sleep_millisecs) ||
217             ksm_read_sysfs(KSM_FP("pages_to_scan"), &ksm_sysfs->pages_to_scan) ||
218             ksm_read_sysfs(KSM_FP("run"), &ksm_sysfs->run) ||
219             ksm_read_sysfs(KSM_FP("stable_node_chains_prune_millisecs"),
220                            &ksm_sysfs->stable_node_chains_prune_millisecs) ||
221             ksm_read_sysfs(KSM_FP("use_zero_pages"), &ksm_sysfs->use_zero_pages))
222                 return 1;
223
224         return 0;
225 }
226
227 static int ksm_restore(struct ksm_sysfs *ksm_sysfs)
228 {
229         if (ksm_write_sysfs(KSM_FP("max_page_sharing"), ksm_sysfs->max_page_sharing) ||
230             ksm_write_sysfs(KSM_FP("merge_across_nodes"), ksm_sysfs->merge_across_nodes) ||
231             ksm_write_sysfs(KSM_FP("pages_to_scan"), ksm_sysfs->pages_to_scan) ||
232             ksm_write_sysfs(KSM_FP("run"), ksm_sysfs->run) ||
233             ksm_write_sysfs(KSM_FP("sleep_millisecs"), ksm_sysfs->sleep_millisecs) ||
234             ksm_write_sysfs(KSM_FP("stable_node_chains_prune_millisecs"),
235                             ksm_sysfs->stable_node_chains_prune_millisecs) ||
236             ksm_write_sysfs(KSM_FP("use_zero_pages"), ksm_sysfs->use_zero_pages))
237                 return 1;
238
239         return 0;
240 }
241
242 static int check_ksm_merge(int mapping, int prot, long page_count, int timeout, size_t page_size)
243 {
244         void *map_ptr;
245         struct timespec start_time;
246
247         if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
248                 perror("clock_gettime");
249                 return KSFT_FAIL;
250         }
251
252         /* fill pages with the same data and merge them */
253         map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
254         if (!map_ptr)
255                 return KSFT_FAIL;
256
257         if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
258                 goto err_out;
259
260         /* verify that the right number of pages are merged */
261         if (assert_ksm_pages_count(page_count)) {
262                 printf("OK\n");
263                 munmap(map_ptr, page_size * page_count);
264                 return KSFT_PASS;
265         }
266
267 err_out:
268         printf("Not OK\n");
269         munmap(map_ptr, page_size * page_count);
270         return KSFT_FAIL;
271 }
272
273 static int check_ksm_unmerge(int mapping, int prot, int timeout, size_t page_size)
274 {
275         void *map_ptr;
276         struct timespec start_time;
277         int page_count = 2;
278
279         if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
280                 perror("clock_gettime");
281                 return KSFT_FAIL;
282         }
283
284         /* fill pages with the same data and merge them */
285         map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
286         if (!map_ptr)
287                 return KSFT_FAIL;
288
289         if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
290                 goto err_out;
291
292         /* change 1 byte in each of the 2 pages -- KSM must automatically unmerge them */
293         memset(map_ptr, '-', 1);
294         memset(map_ptr + page_size, '+', 1);
295
296         /* get at least 1 scan, so KSM can detect that the pages were modified */
297         if (ksm_do_scan(1, start_time, timeout))
298                 goto err_out;
299
300         /* check that unmerging was successful and 0 pages are currently merged */
301         if (assert_ksm_pages_count(0)) {
302                 printf("OK\n");
303                 munmap(map_ptr, page_size * page_count);
304                 return KSFT_PASS;
305         }
306
307 err_out:
308         printf("Not OK\n");
309         munmap(map_ptr, page_size * page_count);
310         return KSFT_FAIL;
311 }
312
313 static int check_ksm_zero_page_merge(int mapping, int prot, long page_count, int timeout,
314                                      bool use_zero_pages, size_t page_size)
315 {
316         void *map_ptr;
317         struct timespec start_time;
318
319         if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
320                 perror("clock_gettime");
321                 return KSFT_FAIL;
322         }
323
324         if (ksm_write_sysfs(KSM_FP("use_zero_pages"), use_zero_pages))
325                 return KSFT_FAIL;
326
327         /* fill pages with zero and try to merge them */
328         map_ptr = allocate_memory(NULL, prot, mapping, 0, page_size * page_count);
329         if (!map_ptr)
330                 return KSFT_FAIL;
331
332         if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
333                 goto err_out;
334
335        /*
336         * verify that the right number of pages are merged:
337         * 1) if use_zero_pages is set to 1, empty pages are merged
338         *    with the kernel zero page instead of with each other;
339         * 2) if use_zero_pages is set to 0, empty pages are not treated specially
340         *    and merged as usual.
341         */
342         if (use_zero_pages && !assert_ksm_pages_count(0))
343                 goto err_out;
344         else if (!use_zero_pages && !assert_ksm_pages_count(page_count))
345                 goto err_out;
346
347         printf("OK\n");
348         munmap(map_ptr, page_size * page_count);
349         return KSFT_PASS;
350
351 err_out:
352         printf("Not OK\n");
353         munmap(map_ptr, page_size * page_count);
354         return KSFT_FAIL;
355 }
356
357 static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_across_nodes,
358                                 size_t page_size)
359 {
360         void *numa1_map_ptr, *numa2_map_ptr;
361         struct timespec start_time;
362         int page_count = 2;
363
364         if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
365                 perror("clock_gettime");
366                 return KSFT_FAIL;
367         }
368
369         if (numa_available() < 0) {
370                 perror("NUMA support not enabled");
371                 return KSFT_SKIP;
372         }
373         if (numa_max_node() < 1) {
374                 printf("At least 2 NUMA nodes must be available\n");
375                 return KSFT_SKIP;
376         }
377         if (ksm_write_sysfs(KSM_FP("merge_across_nodes"), merge_across_nodes))
378                 return KSFT_FAIL;
379
380         /* allocate 2 pages in 2 different NUMA nodes and fill them with the same data */
381         numa1_map_ptr = numa_alloc_onnode(page_size, 0);
382         numa2_map_ptr = numa_alloc_onnode(page_size, 1);
383         if (!numa1_map_ptr || !numa2_map_ptr) {
384                 perror("numa_alloc_onnode");
385                 return KSFT_FAIL;
386         }
387
388         memset(numa1_map_ptr, '*', page_size);
389         memset(numa2_map_ptr, '*', page_size);
390
391         /* try to merge the pages */
392         if (ksm_merge_pages(numa1_map_ptr, page_size, start_time, timeout) ||
393             ksm_merge_pages(numa2_map_ptr, page_size, start_time, timeout))
394                 goto err_out;
395
396        /*
397         * verify that the right number of pages are merged:
398         * 1) if merge_across_nodes was enabled, 2 duplicate pages will be merged;
399         * 2) if merge_across_nodes = 0, there must be 0 merged pages, since there is
400         *    only 1 unique page in each node and they can't be shared.
401         */
402         if (merge_across_nodes && !assert_ksm_pages_count(page_count))
403                 goto err_out;
404         else if (!merge_across_nodes && !assert_ksm_pages_count(0))
405                 goto err_out;
406
407         numa_free(numa1_map_ptr, page_size);
408         numa_free(numa2_map_ptr, page_size);
409         printf("OK\n");
410         return KSFT_PASS;
411
412 err_out:
413         numa_free(numa1_map_ptr, page_size);
414         numa_free(numa2_map_ptr, page_size);
415         printf("Not OK\n");
416         return KSFT_FAIL;
417 }
418
419 static int ksm_merge_time(int mapping, int prot, int timeout, size_t map_size)
420 {
421         void *map_ptr;
422         struct timespec start_time, end_time;
423         unsigned long scan_time_ns;
424
425         map_size *= MB;
426
427         map_ptr = allocate_memory(NULL, prot, mapping, '*', map_size);
428         if (!map_ptr)
429                 return KSFT_FAIL;
430
431         if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
432                 perror("clock_gettime");
433                 goto err_out;
434         }
435         if (ksm_merge_pages(map_ptr, map_size, start_time, timeout))
436                 goto err_out;
437         if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) {
438                 perror("clock_gettime");
439                 goto err_out;
440         }
441
442         scan_time_ns = (end_time.tv_sec - start_time.tv_sec) * NSEC_PER_SEC +
443                        (end_time.tv_nsec - start_time.tv_nsec);
444
445         printf("Total size:    %lu MiB\n", map_size / MB);
446         printf("Total time:    %ld.%09ld s\n", scan_time_ns / NSEC_PER_SEC,
447                scan_time_ns % NSEC_PER_SEC);
448         printf("Average speed:  %.3f MiB/s\n", (map_size / MB) /
449                                                ((double)scan_time_ns / NSEC_PER_SEC));
450
451         munmap(map_ptr, map_size);
452         return KSFT_PASS;
453
454 err_out:
455         printf("Not OK\n");
456         munmap(map_ptr, map_size);
457         return KSFT_FAIL;
458 }
459
460 static int ksm_cow_time(int mapping, int prot, int timeout, size_t page_size)
461 {
462         void *map_ptr;
463         struct timespec start_time, end_time;
464         unsigned long cow_time_ns;
465
466         /* page_count must be less than 2*page_size */
467         size_t page_count = 4000;
468
469         map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
470         if (!map_ptr)
471                 return KSFT_FAIL;
472
473         if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
474                 perror("clock_gettime");
475                 return KSFT_FAIL;
476         }
477         for (size_t i = 0; i < page_count - 1; i = i + 2)
478                 memset(map_ptr + page_size * i, '-', 1);
479         if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) {
480                 perror("clock_gettime");
481                 return KSFT_FAIL;
482         }
483
484         cow_time_ns = (end_time.tv_sec - start_time.tv_sec) * NSEC_PER_SEC +
485                        (end_time.tv_nsec - start_time.tv_nsec);
486
487         printf("Total size:    %lu MiB\n\n", (page_size * page_count) / MB);
488         printf("Not merged pages:\n");
489         printf("Total time:     %ld.%09ld s\n", cow_time_ns / NSEC_PER_SEC,
490                cow_time_ns % NSEC_PER_SEC);
491         printf("Average speed:  %.3f MiB/s\n\n", ((page_size * (page_count / 2)) / MB) /
492                                                ((double)cow_time_ns / NSEC_PER_SEC));
493
494         /* Create 2000 pairs of duplicate pages */
495         for (size_t i = 0; i < page_count - 1; i = i + 2) {
496                 memset(map_ptr + page_size * i, '+', i / 2 + 1);
497                 memset(map_ptr + page_size * (i + 1), '+', i / 2 + 1);
498         }
499         if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
500                 goto err_out;
501
502         if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
503                 perror("clock_gettime");
504                 goto err_out;
505         }
506         for (size_t i = 0; i < page_count - 1; i = i + 2)
507                 memset(map_ptr + page_size * i, '-', 1);
508         if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) {
509                 perror("clock_gettime");
510                 goto err_out;
511         }
512
513         cow_time_ns = (end_time.tv_sec - start_time.tv_sec) * NSEC_PER_SEC +
514                        (end_time.tv_nsec - start_time.tv_nsec);
515
516         printf("Merged pages:\n");
517         printf("Total time:     %ld.%09ld s\n", cow_time_ns / NSEC_PER_SEC,
518                cow_time_ns % NSEC_PER_SEC);
519         printf("Average speed:  %.3f MiB/s\n", ((page_size * (page_count / 2)) / MB) /
520                                                ((double)cow_time_ns / NSEC_PER_SEC));
521
522         munmap(map_ptr, page_size * page_count);
523         return KSFT_PASS;
524
525 err_out:
526         printf("Not OK\n");
527         munmap(map_ptr, page_size * page_count);
528         return KSFT_FAIL;
529 }
530
531 int main(int argc, char *argv[])
532 {
533         int ret, opt;
534         int prot = 0;
535         int ksm_scan_limit_sec = KSM_SCAN_LIMIT_SEC_DEFAULT;
536         long page_count = KSM_PAGE_COUNT_DEFAULT;
537         size_t page_size = sysconf(_SC_PAGESIZE);
538         struct ksm_sysfs ksm_sysfs_old;
539         int test_name = CHECK_KSM_MERGE;
540         bool use_zero_pages = KSM_USE_ZERO_PAGES_DEFAULT;
541         bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT;
542         long size_MB = 0;
543
544         while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:MUZNPC")) != -1) {
545                 switch (opt) {
546                 case 'a':
547                         prot = str_to_prot(optarg);
548                         break;
549                 case 'p':
550                         page_count = atol(optarg);
551                         if (page_count <= 0) {
552                                 printf("The number of pages must be greater than 0\n");
553                                 return KSFT_FAIL;
554                         }
555                         break;
556                 case 'l':
557                         ksm_scan_limit_sec = atoi(optarg);
558                         if (ksm_scan_limit_sec <= 0) {
559                                 printf("Timeout value must be greater than 0\n");
560                                 return KSFT_FAIL;
561                         }
562                         break;
563                 case 'h':
564                         print_help();
565                         break;
566                 case 'z':
567                         if (strcmp(optarg, "0") == 0)
568                                 use_zero_pages = 0;
569                         else
570                                 use_zero_pages = 1;
571                         break;
572                 case 'm':
573                         if (strcmp(optarg, "0") == 0)
574                                 merge_across_nodes = 0;
575                         else
576                                 merge_across_nodes = 1;
577                         break;
578                 case 's':
579                         size_MB = atoi(optarg);
580                         if (size_MB <= 0) {
581                                 printf("Size must be greater than 0\n");
582                                 return KSFT_FAIL;
583                         }
584                 case 'M':
585                         break;
586                 case 'U':
587                         test_name = CHECK_KSM_UNMERGE;
588                         break;
589                 case 'Z':
590                         test_name = CHECK_KSM_ZERO_PAGE_MERGE;
591                         break;
592                 case 'N':
593                         test_name = CHECK_KSM_NUMA_MERGE;
594                         break;
595                 case 'P':
596                         test_name = KSM_MERGE_TIME;
597                         break;
598                 case 'C':
599                         test_name = KSM_COW_TIME;
600                         break;
601                 default:
602                         return KSFT_FAIL;
603                 }
604         }
605
606         if (prot == 0)
607                 prot = str_to_prot(KSM_PROT_STR_DEFAULT);
608
609         if (access(KSM_SYSFS_PATH, F_OK)) {
610                 printf("Config KSM not enabled\n");
611                 return KSFT_SKIP;
612         }
613
614         if (ksm_save_def(&ksm_sysfs_old)) {
615                 printf("Cannot save default tunables\n");
616                 return KSFT_FAIL;
617         }
618
619         if (ksm_write_sysfs(KSM_FP("run"), 2) ||
620             ksm_write_sysfs(KSM_FP("sleep_millisecs"), 0) ||
621             ksm_write_sysfs(KSM_FP("merge_across_nodes"), 1) ||
622             ksm_write_sysfs(KSM_FP("pages_to_scan"), page_count))
623                 return KSFT_FAIL;
624
625         switch (test_name) {
626         case CHECK_KSM_MERGE:
627                 ret = check_ksm_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count,
628                                       ksm_scan_limit_sec, page_size);
629                 break;
630         case CHECK_KSM_UNMERGE:
631                 ret = check_ksm_unmerge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
632                                         page_size);
633                 break;
634         case CHECK_KSM_ZERO_PAGE_MERGE:
635                 ret = check_ksm_zero_page_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count,
636                                                 ksm_scan_limit_sec, use_zero_pages, page_size);
637                 break;
638         case CHECK_KSM_NUMA_MERGE:
639                 ret = check_ksm_numa_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
640                                            merge_across_nodes, page_size);
641                 break;
642         case KSM_MERGE_TIME:
643                 if (size_MB == 0) {
644                         printf("Option '-s' is required.\n");
645                         return KSFT_FAIL;
646                 }
647                 ret = ksm_merge_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
648                                      size_MB);
649                 break;
650         case KSM_COW_TIME:
651                 ret = ksm_cow_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
652                                    page_size);
653                 break;
654         }
655
656         if (ksm_restore(&ksm_sysfs_old)) {
657                 printf("Cannot restore default tunables\n");
658                 return KSFT_FAIL;
659         }
660
661         return ret;
662 }