1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2013-2017 Red Hat, Inc.
4 */
5 /*\
6 * [Description]
7 *
8 * The case is designed to test sysfs boolean knob
9 * /sys/kernel/mm/ksm/merge_across_nodes.
10 *
11 * When merge_across_nodes is set to zero only pages from the same
12 * node are merged, otherwise pages from all nodes can be merged
13 * together.
14 *
15 * Introduced in commit:
16 *
17 * commit 90bd6fd31c8097ee4ddcb74b7e08363134863de5
18 * Author: Petr Holasek <pholasek@redhat.com>
19 * Date: Fri Feb 22 16:35:00 2013 -0800
20 *
21 * ksm: allow trees per NUMA node
22 */
23
24 #include "config.h"
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 #include <limits.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <limits.h>
35
36 #include "mem.h"
37 #include "tst_numa.h"
38
39 #ifdef HAVE_NUMA_V2
40 # include <numa.h>
41 # include <numaif.h>
42
43 static unsigned long nr_pages = 100;
44 static char *n_opt;
45
46 static size_t page_size;
47 static struct tst_nodemap *nodes;
48
test_ksm(void)49 static void test_ksm(void)
50 {
51 char **memory;
52 unsigned int i;
53 int ret;
54 unsigned long length;
55 struct bitmask *bm = numa_allocate_nodemask();
56
57 length = nr_pages * page_size;
58
59 memory = SAFE_MALLOC(nodes->cnt * sizeof(char *));
60 for (i = 0; i < nodes->cnt; i++) {
61 memory[i] = SAFE_MMAP(NULL, length, PROT_READ|PROT_WRITE,
62 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
63 #ifdef HAVE_DECL_MADV_MERGEABLE
64 if (madvise(memory[i], length, MADV_MERGEABLE) == -1)
65 tst_brk(TBROK|TERRNO, "madvise");
66 #endif
67
68 #ifdef HAVE_NUMA_V2
69 numa_bitmask_setbit(bm, nodes->map[i]);
70
71 ret = mbind(memory[i], length, MPOL_BIND, bm->maskp, bm->size+1, 0);
72 if (ret == -1)
73 tst_brk(TBROK|TERRNO, "mbind");
74
75 numa_bitmask_clearbit(bm, nodes->map[i]);
76 #endif
77
78 memset(memory[i], 10, length);
79
80 if (mlock(memory[i], length))
81 tst_res(TWARN | TERRNO, "mlock() failed");
82 }
83
84 numa_free_nodemask(bm);
85
86 SAFE_FILE_PRINTF(PATH_KSM "sleep_millisecs", "0");
87 SAFE_FILE_PRINTF(PATH_KSM "pages_to_scan", "%ld",
88 nr_pages * nodes->cnt);
89 /*
90 * merge_across_nodes and max_page_sharing setting can be changed
91 * only when there are no ksm shared pages in system, so set run 2
92 * to unmerge pages first, then to 1 after changing merge_across_nodes,
93 * to remerge according to the new setting.
94 */
95 SAFE_FILE_PRINTF(PATH_KSM "run", "2");
96 if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
97 SAFE_FILE_PRINTF(PATH_KSM "max_page_sharing",
98 "%ld", nr_pages * nodes->cnt);
99 tst_res(TINFO, "Start to test KSM with merge_across_nodes=1");
100 SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
101 SAFE_FILE_PRINTF(PATH_KSM "run", "1");
102 ksm_group_check(1, 1, nr_pages * nodes->cnt - 1, 0, 0, 0,
103 nr_pages * nodes->cnt);
104
105 SAFE_FILE_PRINTF(PATH_KSM "run", "2");
106 tst_res(TINFO, "Start to test KSM with merge_across_nodes=0");
107 SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "0");
108 SAFE_FILE_PRINTF(PATH_KSM "run", "1");
109 ksm_group_check(1, nodes->cnt, nr_pages * nodes->cnt - nodes->cnt,
110 0, 0, 0, nr_pages * nodes->cnt);
111
112 SAFE_FILE_PRINTF(PATH_KSM "run", "2");
113
114 for (i = 0; i < nodes->cnt; i++)
115 SAFE_MUNMAP(memory[i], length);
116
117 free(memory);
118 }
119
setup(void)120 static void setup(void)
121 {
122 if (n_opt)
123 nr_pages = SAFE_STRTOUL(n_opt, 0, ULONG_MAX);
124
125 page_size = getpagesize();
126
127 nodes = tst_get_nodemap(TST_NUMA_MEM, nr_pages * page_size / 1024);
128 if (nodes->cnt <= 1)
129 tst_brk(TCONF, "Test requires at least two NUMA memory nodes");
130 }
131
132 static struct tst_test test = {
133 .needs_root = 1,
134 .options = (struct tst_option[]) {
135 {"n:", &n_opt, "Allocate x pages memory per node"},
136 {}
137 },
138 .setup = setup,
139 .save_restore = (const struct tst_path_val[]) {
140 {"/sys/kernel/mm/ksm/max_page_sharing", NULL,
141 TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
142 {"/sys/kernel/mm/ksm/run", NULL, TST_SR_TBROK},
143 {"/sys/kernel/mm/ksm/sleep_millisecs", NULL, TST_SR_TBROK},
144 {"/sys/kernel/mm/ksm/merge_across_nodes", NULL, TST_SR_TCONF},
145 {}
146 },
147 .needs_kconfigs = (const char *const[]){
148 "CONFIG_KSM=y",
149 "CONFIG_NUMA=y",
150 NULL
151 },
152 .test_all = test_ksm,
153 };
154
155 #else
156 TST_TEST_TCONF(NUMA_ERROR_MSG);
157 #endif
158