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