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) 2023 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 * Kernel Samepage Merging (KSM) for smart scan feature
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Test allocates a page and fills it with 'a' characters. It captures the
11*49cdfc7eSAndroid Build Coastguard Worker * pages_skipped counter, waits for a few iterations and captures the
12*49cdfc7eSAndroid Build Coastguard Worker * pages_skipped counter again. The expectation is that over 50% of the page
13*49cdfc7eSAndroid Build Coastguard Worker * scans are skipped. (There is only one page that has KSM enabled and it gets
14*49cdfc7eSAndroid Build Coastguard Worker * scanned during each iteration and it cannot be de-duplicated.)
15*49cdfc7eSAndroid Build Coastguard Worker *
16*49cdfc7eSAndroid Build Coastguard Worker * Smart scan feature was added in kernel v6.7.
17*49cdfc7eSAndroid Build Coastguard Worker *
18*49cdfc7eSAndroid Build Coastguard Worker * [Prerequisites]
19*49cdfc7eSAndroid Build Coastguard Worker *
20*49cdfc7eSAndroid Build Coastguard Worker * ksm and ksmtuned daemons need to be disabled. Otherwise, it could
21*49cdfc7eSAndroid Build Coastguard Worker * distrub the testing as they also change some ksm tunables depends
22*49cdfc7eSAndroid Build Coastguard Worker * on current workloads.
23*49cdfc7eSAndroid Build Coastguard Worker */
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include "mem.h"
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker /* This test allocates one page, fills the page with a's, captures the
29*49cdfc7eSAndroid Build Coastguard Worker * full_scan and pages_skipped counters. Then it makes sure at least 3
30*49cdfc7eSAndroid Build Coastguard Worker * full scans have been performed and measures the above counters again.
31*49cdfc7eSAndroid Build Coastguard Worker * The expectation is that at least 50% of the pages are skipped.
32*49cdfc7eSAndroid Build Coastguard Worker *
33*49cdfc7eSAndroid Build Coastguard Worker * To wait for at least 3 scans it uses the wait_ksmd_full_scan() function. In
34*49cdfc7eSAndroid Build Coastguard Worker * reality, it will be a lot more scans as the wait_ksmd_full_scan() function
35*49cdfc7eSAndroid Build Coastguard Worker * sleeps for one second.
36*49cdfc7eSAndroid Build Coastguard Worker */
verify_ksm(void)37*49cdfc7eSAndroid Build Coastguard Worker static void verify_ksm(void)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker int full_scans_begin;
40*49cdfc7eSAndroid Build Coastguard Worker int full_scans_end;
41*49cdfc7eSAndroid Build Coastguard Worker int pages_skipped_begin;
42*49cdfc7eSAndroid Build Coastguard Worker int pages_skipped_end;
43*49cdfc7eSAndroid Build Coastguard Worker int diff_pages;
44*49cdfc7eSAndroid Build Coastguard Worker int diff_scans;
45*49cdfc7eSAndroid Build Coastguard Worker unsigned long page_size;
46*49cdfc7eSAndroid Build Coastguard Worker char *memory;
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker /* Apply for the space for memory. */
49*49cdfc7eSAndroid Build Coastguard Worker page_size = sysconf(_SC_PAGE_SIZE);
50*49cdfc7eSAndroid Build Coastguard Worker memory = SAFE_MALLOC(page_size);
51*49cdfc7eSAndroid Build Coastguard Worker memory = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE,
52*49cdfc7eSAndroid Build Coastguard Worker MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
53*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_DECL_MADV_MERGEABLE
54*49cdfc7eSAndroid Build Coastguard Worker if (madvise(memory, page_size, MADV_MERGEABLE) == -1)
55*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK|TERRNO, "madvise");
56*49cdfc7eSAndroid Build Coastguard Worker #endif
57*49cdfc7eSAndroid Build Coastguard Worker memset(memory, 'a', page_size);
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "KSM merging");
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
62*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(PATH_KSM "run", "2");
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker /* Set defalut ksm scan values. */
65*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(PATH_KSM "run", "1");
66*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(PATH_KSM "pages_to_scan", "%ld", 100l);
67*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(PATH_KSM "sleep_millisecs", "0");
68*49cdfc7eSAndroid Build Coastguard Worker
69*49cdfc7eSAndroid Build Coastguard Worker /* Measure pages skipped aka "smart scan". */
70*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF(PATH_KSM "full_scans", "%d", &full_scans_begin);
71*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF(PATH_KSM "pages_skipped", "%d", &pages_skipped_begin);
72*49cdfc7eSAndroid Build Coastguard Worker wait_ksmd_full_scan();
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "stop KSM");
75*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(PATH_KSM "run", "0");
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF(PATH_KSM "full_scans", "%d", &full_scans_end);
78*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF(PATH_KSM "pages_skipped", "%d", &pages_skipped_end);
79*49cdfc7eSAndroid Build Coastguard Worker diff_pages = pages_skipped_end - pages_skipped_begin;
80*49cdfc7eSAndroid Build Coastguard Worker diff_scans = full_scans_end - full_scans_begin;
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker if (diff_pages < diff_scans * 50 / 100) {
83*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "number of pages %d", diff_pages);
84*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "number of scans %d", diff_scans);
85*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "not enough pages have been skipped by smart_scan");
86*49cdfc7eSAndroid Build Coastguard Worker } else {
87*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "smart_scan skipped more than 50%% of the pages");
88*49cdfc7eSAndroid Build Coastguard Worker }
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_DECL_MADV_MERGEABLE
91*49cdfc7eSAndroid Build Coastguard Worker if (madvise(memory, page_size, MADV_UNMERGEABLE) == -1)
92*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK|TERRNO, "madvise");
93*49cdfc7eSAndroid Build Coastguard Worker #endif
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
97*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
98*49cdfc7eSAndroid Build Coastguard Worker .options = (struct tst_option[]) {
99*49cdfc7eSAndroid Build Coastguard Worker {}
100*49cdfc7eSAndroid Build Coastguard Worker },
101*49cdfc7eSAndroid Build Coastguard Worker .save_restore = (const struct tst_path_val[]) {
102*49cdfc7eSAndroid Build Coastguard Worker {PATH_KSM "pages_skipped", NULL, TST_SR_TCONF},
103*49cdfc7eSAndroid Build Coastguard Worker {PATH_KSM "run", NULL, TST_SR_TCONF},
104*49cdfc7eSAndroid Build Coastguard Worker {PATH_KSM "sleep_millisecs", NULL, TST_SR_TCONF},
105*49cdfc7eSAndroid Build Coastguard Worker {PATH_KSM "smart_scan", "1",
106*49cdfc7eSAndroid Build Coastguard Worker TST_SR_SKIP_MISSING | TST_SR_TCONF},
107*49cdfc7eSAndroid Build Coastguard Worker {}
108*49cdfc7eSAndroid Build Coastguard Worker },
109*49cdfc7eSAndroid Build Coastguard Worker .needs_kconfigs = (const char *const[]){
110*49cdfc7eSAndroid Build Coastguard Worker "CONFIG_KSM=y",
111*49cdfc7eSAndroid Build Coastguard Worker NULL
112*49cdfc7eSAndroid Build Coastguard Worker },
113*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_ksm,
114*49cdfc7eSAndroid Build Coastguard Worker };
115