1 // SPDX-License-Identifier: GPL-2.0
9 #include "../kselftest.h"
10 #include "../../../../include/vdso/time64.h"
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)
22 unsigned long max_page_sharing;
23 unsigned long merge_across_nodes;
24 unsigned long pages_to_scan;
26 unsigned long sleep_millisecs;
27 unsigned long stable_node_chains_prune_millisecs;
28 unsigned long use_zero_pages;
34 CHECK_KSM_ZERO_PAGE_MERGE,
39 static int ksm_write_sysfs(const char *file_path, unsigned long val)
41 FILE *f = fopen(file_path, "w");
44 fprintf(stderr, "f %s\n", file_path);
48 if (fprintf(f, "%lu", val) < 0) {
57 static int ksm_read_sysfs(const char *file_path, unsigned long *val)
59 FILE *f = fopen(file_path, "r");
62 fprintf(stderr, "f %s\n", file_path);
66 if (fscanf(f, "%lu", val) != 1) {
75 static int str_to_prot(char *prot_str)
79 if ((strchr(prot_str, 'r')) != NULL)
81 if ((strchr(prot_str, 'w')) != NULL)
83 if ((strchr(prot_str, 'x')) != NULL)
89 static void print_help(void)
91 printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n"
92 "[-z use_zero_pages] [-m merge_across_nodes] [-s size]\n");
94 printf("Supported <test type>:\n"
95 " -M (page merging)\n"
96 " -Z (zero pages merging)\n"
97 " -N (merging of pages in different NUMA nodes)\n"
98 " -U (page unmerging)\n"
99 " -P evaluate merging time and speed.\n"
100 " For this test, the size of duplicated memory area (in MiB)\n"
101 " must be provided using -s option\n\n");
103 printf(" -a: specify the access protections of pages.\n"
104 " <prot> must be of the form [rwx].\n"
105 " Default: %s\n", KSM_PROT_STR_DEFAULT);
106 printf(" -p: specify the number of pages to test.\n"
107 " Default: %ld\n", KSM_PAGE_COUNT_DEFAULT);
108 printf(" -l: limit the maximum running time (in seconds) for a test.\n"
109 " Default: %d seconds\n", KSM_SCAN_LIMIT_SEC_DEFAULT);
110 printf(" -z: change use_zero_pages tunable\n"
111 " Default: %d\n", KSM_USE_ZERO_PAGES_DEFAULT);
112 printf(" -m: change merge_across_nodes tunable\n"
113 " Default: %d\n", KSM_MERGE_ACROSS_NODES_DEFAULT);
114 printf(" -s: the size of duplicated memory area (in MiB)\n");
119 static void *allocate_memory(void *ptr, int prot, int mapping, char data, size_t map_size)
121 void *map_ptr = mmap(ptr, map_size, PROT_WRITE, mapping, -1, 0);
127 memset(map_ptr, data, map_size);
128 if (mprotect(map_ptr, map_size, prot)) {
130 munmap(map_ptr, map_size);
137 static int ksm_do_scan(int scan_count, struct timespec start_time, int timeout)
139 struct timespec cur_time;
140 unsigned long cur_scan, init_scan;
142 if (ksm_read_sysfs(KSM_FP("full_scans"), &init_scan))
144 cur_scan = init_scan;
146 while (cur_scan < init_scan + scan_count) {
147 if (ksm_read_sysfs(KSM_FP("full_scans"), &cur_scan))
149 if (clock_gettime(CLOCK_MONOTONIC_RAW, &cur_time)) {
150 perror("clock_gettime");
153 if ((cur_time.tv_sec - start_time.tv_sec) > timeout) {
154 printf("Scan time limit exceeded\n");
162 static int ksm_merge_pages(void *addr, size_t size, struct timespec start_time, int timeout)
164 if (madvise(addr, size, MADV_MERGEABLE)) {
168 if (ksm_write_sysfs(KSM_FP("run"), 1))
171 /* Since merging occurs only after 2 scans, make sure to get at least 2 full scans */
172 if (ksm_do_scan(2, start_time, timeout))
178 static bool assert_ksm_pages_count(long dupl_page_count)
180 unsigned long max_page_sharing, pages_sharing, pages_shared;
182 if (ksm_read_sysfs(KSM_FP("pages_shared"), &pages_shared) ||
183 ksm_read_sysfs(KSM_FP("pages_sharing"), &pages_sharing) ||
184 ksm_read_sysfs(KSM_FP("max_page_sharing"), &max_page_sharing))
188 * Since there must be at least 2 pages for merging and 1 page can be
189 * shared with the limited number of pages (max_page_sharing), sometimes
190 * there are 'leftover' pages that cannot be merged. For example, if there
191 * are 11 pages and max_page_sharing = 10, then only 10 pages will be
192 * merged and the 11th page won't be affected. As a result, when the number
193 * of duplicate pages is divided by max_page_sharing and the remainder is 1,
194 * pages_shared and pages_sharing values will be equal between dupl_page_count
195 * and dupl_page_count - 1.
197 if (dupl_page_count % max_page_sharing == 1 || dupl_page_count % max_page_sharing == 0) {
198 if (pages_shared == dupl_page_count / max_page_sharing &&
199 pages_sharing == pages_shared * (max_page_sharing - 1))
202 if (pages_shared == (dupl_page_count / max_page_sharing + 1) &&
203 pages_sharing == dupl_page_count - pages_shared)
210 static int ksm_save_def(struct ksm_sysfs *ksm_sysfs)
212 if (ksm_read_sysfs(KSM_FP("max_page_sharing"), &ksm_sysfs->max_page_sharing) ||
213 ksm_read_sysfs(KSM_FP("merge_across_nodes"), &ksm_sysfs->merge_across_nodes) ||
214 ksm_read_sysfs(KSM_FP("sleep_millisecs"), &ksm_sysfs->sleep_millisecs) ||
215 ksm_read_sysfs(KSM_FP("pages_to_scan"), &ksm_sysfs->pages_to_scan) ||
216 ksm_read_sysfs(KSM_FP("run"), &ksm_sysfs->run) ||
217 ksm_read_sysfs(KSM_FP("stable_node_chains_prune_millisecs"),
218 &ksm_sysfs->stable_node_chains_prune_millisecs) ||
219 ksm_read_sysfs(KSM_FP("use_zero_pages"), &ksm_sysfs->use_zero_pages))
225 static int ksm_restore(struct ksm_sysfs *ksm_sysfs)
227 if (ksm_write_sysfs(KSM_FP("max_page_sharing"), ksm_sysfs->max_page_sharing) ||
228 ksm_write_sysfs(KSM_FP("merge_across_nodes"), ksm_sysfs->merge_across_nodes) ||
229 ksm_write_sysfs(KSM_FP("pages_to_scan"), ksm_sysfs->pages_to_scan) ||
230 ksm_write_sysfs(KSM_FP("run"), ksm_sysfs->run) ||
231 ksm_write_sysfs(KSM_FP("sleep_millisecs"), ksm_sysfs->sleep_millisecs) ||
232 ksm_write_sysfs(KSM_FP("stable_node_chains_prune_millisecs"),
233 ksm_sysfs->stable_node_chains_prune_millisecs) ||
234 ksm_write_sysfs(KSM_FP("use_zero_pages"), ksm_sysfs->use_zero_pages))
240 static int check_ksm_merge(int mapping, int prot, long page_count, int timeout, size_t page_size)
243 struct timespec start_time;
245 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
246 perror("clock_gettime");
250 /* fill pages with the same data and merge them */
251 map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
255 if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
258 /* verify that the right number of pages are merged */
259 if (assert_ksm_pages_count(page_count)) {
261 munmap(map_ptr, page_size * page_count);
267 munmap(map_ptr, page_size * page_count);
271 static int check_ksm_unmerge(int mapping, int prot, int timeout, size_t page_size)
274 struct timespec start_time;
277 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
278 perror("clock_gettime");
282 /* fill pages with the same data and merge them */
283 map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
287 if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
290 /* change 1 byte in each of the 2 pages -- KSM must automatically unmerge them */
291 memset(map_ptr, '-', 1);
292 memset(map_ptr + page_size, '+', 1);
294 /* get at least 1 scan, so KSM can detect that the pages were modified */
295 if (ksm_do_scan(1, start_time, timeout))
298 /* check that unmerging was successful and 0 pages are currently merged */
299 if (assert_ksm_pages_count(0)) {
301 munmap(map_ptr, page_size * page_count);
307 munmap(map_ptr, page_size * page_count);
311 static int check_ksm_zero_page_merge(int mapping, int prot, long page_count, int timeout,
312 bool use_zero_pages, size_t page_size)
315 struct timespec start_time;
317 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
318 perror("clock_gettime");
322 if (ksm_write_sysfs(KSM_FP("use_zero_pages"), use_zero_pages))
325 /* fill pages with zero and try to merge them */
326 map_ptr = allocate_memory(NULL, prot, mapping, 0, page_size * page_count);
330 if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
334 * verify that the right number of pages are merged:
335 * 1) if use_zero_pages is set to 1, empty pages are merged
336 * with the kernel zero page instead of with each other;
337 * 2) if use_zero_pages is set to 0, empty pages are not treated specially
338 * and merged as usual.
340 if (use_zero_pages && !assert_ksm_pages_count(0))
342 else if (!use_zero_pages && !assert_ksm_pages_count(page_count))
346 munmap(map_ptr, page_size * page_count);
351 munmap(map_ptr, page_size * page_count);
355 static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_across_nodes,
358 void *numa1_map_ptr, *numa2_map_ptr;
359 struct timespec start_time;
362 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
363 perror("clock_gettime");
367 if (numa_available() < 0) {
368 perror("NUMA support not enabled");
371 if (numa_max_node() < 1) {
372 printf("At least 2 NUMA nodes must be available\n");
375 if (ksm_write_sysfs(KSM_FP("merge_across_nodes"), merge_across_nodes))
378 /* allocate 2 pages in 2 different NUMA nodes and fill them with the same data */
379 numa1_map_ptr = numa_alloc_onnode(page_size, 0);
380 numa2_map_ptr = numa_alloc_onnode(page_size, 1);
381 if (!numa1_map_ptr || !numa2_map_ptr) {
382 perror("numa_alloc_onnode");
386 memset(numa1_map_ptr, '*', page_size);
387 memset(numa2_map_ptr, '*', page_size);
389 /* try to merge the pages */
390 if (ksm_merge_pages(numa1_map_ptr, page_size, start_time, timeout) ||
391 ksm_merge_pages(numa2_map_ptr, page_size, start_time, timeout))
395 * verify that the right number of pages are merged:
396 * 1) if merge_across_nodes was enabled, 2 duplicate pages will be merged;
397 * 2) if merge_across_nodes = 0, there must be 0 merged pages, since there is
398 * only 1 unique page in each node and they can't be shared.
400 if (merge_across_nodes && !assert_ksm_pages_count(page_count))
402 else if (!merge_across_nodes && !assert_ksm_pages_count(0))
405 numa_free(numa1_map_ptr, page_size);
406 numa_free(numa2_map_ptr, page_size);
411 numa_free(numa1_map_ptr, page_size);
412 numa_free(numa2_map_ptr, page_size);
417 static int ksm_merge_time(int mapping, int prot, int timeout, size_t map_size)
420 struct timespec start_time, end_time;
421 unsigned long scan_time_ns;
425 map_ptr = allocate_memory(NULL, prot, mapping, '*', map_size);
429 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
430 perror("clock_gettime");
433 if (ksm_merge_pages(map_ptr, map_size, start_time, timeout))
435 if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) {
436 perror("clock_gettime");
440 scan_time_ns = (end_time.tv_sec - start_time.tv_sec) * NSEC_PER_SEC +
441 (end_time.tv_nsec - start_time.tv_nsec);
443 printf("Total size: %lu MiB\n", map_size / MB);
444 printf("Total time: %ld.%09ld s\n", scan_time_ns / NSEC_PER_SEC,
445 scan_time_ns % NSEC_PER_SEC);
446 printf("Average speed: %.3f MiB/s\n", (map_size / MB) /
447 ((double)scan_time_ns / NSEC_PER_SEC));
449 munmap(map_ptr, map_size);
454 munmap(map_ptr, map_size);
458 int main(int argc, char *argv[])
462 int ksm_scan_limit_sec = KSM_SCAN_LIMIT_SEC_DEFAULT;
463 long page_count = KSM_PAGE_COUNT_DEFAULT;
464 size_t page_size = sysconf(_SC_PAGESIZE);
465 struct ksm_sysfs ksm_sysfs_old;
466 int test_name = CHECK_KSM_MERGE;
467 bool use_zero_pages = KSM_USE_ZERO_PAGES_DEFAULT;
468 bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT;
471 while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:MUZNP")) != -1) {
474 prot = str_to_prot(optarg);
477 page_count = atol(optarg);
478 if (page_count <= 0) {
479 printf("The number of pages must be greater than 0\n");
484 ksm_scan_limit_sec = atoi(optarg);
485 if (ksm_scan_limit_sec <= 0) {
486 printf("Timeout value must be greater than 0\n");
494 if (strcmp(optarg, "0") == 0)
500 if (strcmp(optarg, "0") == 0)
501 merge_across_nodes = 0;
503 merge_across_nodes = 1;
506 size_MB = atoi(optarg);
508 printf("Size must be greater than 0\n");
514 test_name = CHECK_KSM_UNMERGE;
517 test_name = CHECK_KSM_ZERO_PAGE_MERGE;
520 test_name = CHECK_KSM_NUMA_MERGE;
523 test_name = KSM_MERGE_TIME;
531 prot = str_to_prot(KSM_PROT_STR_DEFAULT);
533 if (access(KSM_SYSFS_PATH, F_OK)) {
534 printf("Config KSM not enabled\n");
538 if (ksm_save_def(&ksm_sysfs_old)) {
539 printf("Cannot save default tunables\n");
543 if (ksm_write_sysfs(KSM_FP("run"), 2) ||
544 ksm_write_sysfs(KSM_FP("sleep_millisecs"), 0) ||
545 ksm_write_sysfs(KSM_FP("merge_across_nodes"), 1) ||
546 ksm_write_sysfs(KSM_FP("pages_to_scan"), page_count))
550 case CHECK_KSM_MERGE:
551 ret = check_ksm_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count,
552 ksm_scan_limit_sec, page_size);
554 case CHECK_KSM_UNMERGE:
555 ret = check_ksm_unmerge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
558 case CHECK_KSM_ZERO_PAGE_MERGE:
559 ret = check_ksm_zero_page_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count,
560 ksm_scan_limit_sec, use_zero_pages, page_size);
562 case CHECK_KSM_NUMA_MERGE:
563 ret = check_ksm_numa_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
564 merge_across_nodes, page_size);
568 printf("Option '-s' is required.\n");
571 ret = ksm_merge_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
576 if (ksm_restore(&ksm_sysfs_old)) {
577 printf("Cannot restore default tunables\n");