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