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) 2010-2017 Red Hat, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * Prerequisites:
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * ksm and ksmtuned daemons need to be disabled. Otherwise, it could
12*49cdfc7eSAndroid Build Coastguard Worker * distrub the testing as they also change some ksm tunables depends
13*49cdfc7eSAndroid Build Coastguard Worker * on current workloads.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * [Algorithm]
16*49cdfc7eSAndroid Build Coastguard Worker *
17*49cdfc7eSAndroid Build Coastguard Worker * - Check ksm feature and backup current run setting.
18*49cdfc7eSAndroid Build Coastguard Worker * - Change run setting to 1 - merging.
19*49cdfc7eSAndroid Build Coastguard Worker * - 3 memory allocation programs have the memory contents that 2 of
20*49cdfc7eSAndroid Build Coastguard Worker * them are all 'a' and one is all 'b'.
21*49cdfc7eSAndroid Build Coastguard Worker * - Check ksm statistics and verify the content.
22*49cdfc7eSAndroid Build Coastguard Worker * - 1 program changes the memory content from all 'a' to all 'b'.
23*49cdfc7eSAndroid Build Coastguard Worker * - Check ksm statistics and verify the content.
24*49cdfc7eSAndroid Build Coastguard Worker * - All programs change the memory content to all 'd'.
25*49cdfc7eSAndroid Build Coastguard Worker * - Check ksm statistics and verify the content.
26*49cdfc7eSAndroid Build Coastguard Worker * - Change one page of a process.
27*49cdfc7eSAndroid Build Coastguard Worker * - Check ksm statistics and verify the content.
28*49cdfc7eSAndroid Build Coastguard Worker * - Change run setting to 2 - unmerging.
29*49cdfc7eSAndroid Build Coastguard Worker * - Check ksm statistics and verify the content.
30*49cdfc7eSAndroid Build Coastguard Worker * - Change run setting to 0 - stop.
31*49cdfc7eSAndroid Build Coastguard Worker */
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
34*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include "mem.h"
42*49cdfc7eSAndroid Build Coastguard Worker #include "ksm_common.h"
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_NUMA_V2
45*49cdfc7eSAndroid Build Coastguard Worker #include <numaif.h>
46*49cdfc7eSAndroid Build Coastguard Worker
verify_ksm(void)47*49cdfc7eSAndroid Build Coastguard Worker static void verify_ksm(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
50*49cdfc7eSAndroid Build Coastguard Worker unsigned int node;
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker node = get_a_numa_node();
53*49cdfc7eSAndroid Build Coastguard Worker set_node(nmask, node);
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) {
58*49cdfc7eSAndroid Build Coastguard Worker if (errno != ENOSYS)
59*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "set_mempolicy");
60*49cdfc7eSAndroid Build Coastguard Worker else
61*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "set_mempolicy syscall is not "
62*49cdfc7eSAndroid Build Coastguard Worker "implemented on your system.");
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker create_same_memory(size, num, unit);
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker write_cpusets(tst_cg, node);
67*49cdfc7eSAndroid Build Coastguard Worker create_same_memory(size, num, unit);
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker
setup(void)70*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker if (opt_sizestr && size > DEFAULT_MEMSIZE)
77*49cdfc7eSAndroid Build Coastguard Worker tst_set_max_runtime(32 * (size / DEFAULT_MEMSIZE));
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
81*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
82*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
83*49cdfc7eSAndroid Build Coastguard Worker .options = (struct tst_option[]) {
84*49cdfc7eSAndroid Build Coastguard Worker {"n:", &opt_numstr, "Number of processes"},
85*49cdfc7eSAndroid Build Coastguard Worker {"s:", &opt_sizestr, "Memory allocation size in MB"},
86*49cdfc7eSAndroid Build Coastguard Worker {"u:", &opt_unitstr, "Memory allocation unit in MB"},
87*49cdfc7eSAndroid Build Coastguard Worker {}
88*49cdfc7eSAndroid Build Coastguard Worker },
89*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
90*49cdfc7eSAndroid Build Coastguard Worker .save_restore = (const struct tst_path_val[]) {
91*49cdfc7eSAndroid Build Coastguard Worker {"/sys/kernel/mm/ksm/run", NULL, TST_SR_TBROK},
92*49cdfc7eSAndroid Build Coastguard Worker {"/sys/kernel/mm/ksm/sleep_millisecs", NULL, TST_SR_TBROK},
93*49cdfc7eSAndroid Build Coastguard Worker {"/sys/kernel/mm/ksm/max_page_sharing", NULL,
94*49cdfc7eSAndroid Build Coastguard Worker TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
95*49cdfc7eSAndroid Build Coastguard Worker {"/sys/kernel/mm/ksm/merge_across_nodes", "1",
96*49cdfc7eSAndroid Build Coastguard Worker TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
97*49cdfc7eSAndroid Build Coastguard Worker {"/sys/kernel/mm/ksm/smart_scan", "0",
98*49cdfc7eSAndroid Build Coastguard Worker TST_SR_SKIP_MISSING | TST_SR_TBROK_RO},
99*49cdfc7eSAndroid Build Coastguard Worker {}
100*49cdfc7eSAndroid Build Coastguard Worker },
101*49cdfc7eSAndroid Build Coastguard Worker .needs_kconfigs = (const char *const[]){
102*49cdfc7eSAndroid Build Coastguard Worker "CONFIG_KSM=y",
103*49cdfc7eSAndroid Build Coastguard Worker NULL
104*49cdfc7eSAndroid Build Coastguard Worker },
105*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_ksm,
106*49cdfc7eSAndroid Build Coastguard Worker .max_runtime = 32,
107*49cdfc7eSAndroid Build Coastguard Worker .needs_cgroup_ctrls = (const char *const []){
108*49cdfc7eSAndroid Build Coastguard Worker "memory", "cpuset", NULL
109*49cdfc7eSAndroid Build Coastguard Worker },
110*49cdfc7eSAndroid Build Coastguard Worker };
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker #else
113*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF(NUMA_ERROR_MSG);
114*49cdfc7eSAndroid Build Coastguard Worker #endif
115