Skip to contentSkip to conversation

KSM: support smart-scan feature

PATCH Superseded Stefan Roesch 3 participants 8 messages Dec 4, 2023 – Dec 5, 2023 Memory management

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover KSM: support smart-scan feature

This patch series makes two changes:
- Disable the smart-scan feature for ksm01 - ksm04
This is necessary to make sure that the volatile metrics have the expected counts.
- Add a new test for the smart-scan feature
The new test verifies that the smart-scan feature skips pages when it has been enabled for a VMA.

Versions:
- V3:
- Disable smart scan for ksm05 and ksm06
- add ksm07 to runtest/mm
- change documentation format in header of ksm07.c
- Move function create_memory_for_smartscan() to ksm07.c
- Rename create_memory_for_smartscan() to create_memory()

- V2:
- Disable smart_scan in the test setup structure
- Remove the changes in create_same_memory()
- Add the new testcase ksm07 for the smart scan test

Stefan Roesch (2):
mem: disable KSM smart scan for ksm tests
add ksm test for smart-scan feature

 runtest/mm                       |   1 +
 testcases/kernel/mem/.gitignore  |   1 +
 testcases/kernel/mem/ksm/ksm01.c |   2 +
 testcases/kernel/mem/ksm/ksm02.c |   2 +
 testcases/kernel/mem/ksm/ksm03.c |   2 +
 testcases/kernel/mem/ksm/ksm04.c |   2 +
 testcases/kernel/mem/ksm/ksm05.c |   2 +
 testcases/kernel/mem/ksm/ksm06.c |   2 +
 testcases/kernel/mem/ksm/ksm07.c | 131 +++++++++++++++++++++++++++++++
 9 files changed, 145 insertions(+)
 create mode 100644 testcases/kernel/mem/ksm/ksm07.c

base-commit: 8c89ef3d451087ed6e18750bd5eedd10e5ab3d2e

Signature · 1 line
2.39.3
#1 lore raw

PATCH v3 1/2 mem: disable KSM smart scan for ksm tests

This disables the "smart scan" KSM feature to make sure that the volatile count remains at 0.

---
 testcases/kernel/mem/ksm/ksm01.c | 2 ++
 testcases/kernel/mem/ksm/ksm02.c | 2 ++
 testcases/kernel/mem/ksm/ksm03.c | 2 ++
 testcases/kernel/mem/ksm/ksm04.c | 2 ++
 testcases/kernel/mem/ksm/ksm05.c | 2 ++
 testcases/kernel/mem/ksm/ksm06.c | 2 ++
 6 files changed, 12 insertions(+)
testcases/kernel/mem/ksm/ksm01.c +2 -0
index bcd095865..e2d3d9e00 100644
@@ -86,6 +86,8 @@ static struct tst_test test = {
 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
 		{"/sys/kernel/mm/ksm/merge_across_nodes", "1",
 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
+		{"/sys/kernel/mm/ksm/smart_scan", "0",
+			TST_SR_SKIP_MISSING | TST_SR_TBROK_RO},
 		{}
 	},
 	.needs_kconfigs = (const char *const[]){
testcases/kernel/mem/ksm/ksm02.c +2 -0
index bce639dce..3707de95d 100644
@@ -107,6 +107,8 @@ static struct tst_test test = {
 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
 		{"/sys/kernel/mm/ksm/merge_across_nodes", "1",
 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
+		{"/sys/kernel/mm/ksm/smart_scan", "0",
+			TST_SR_SKIP_MISSING | TST_SR_TBROK_RO},
 		{}
 	},
 	.needs_kconfigs = (const char *const[]){
testcases/kernel/mem/ksm/ksm03.c +2 -0
index 4a733269f..cff74700d 100644
@@ -89,6 +89,8 @@ static struct tst_test test = {
 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
 		{"/sys/kernel/mm/ksm/merge_across_nodes", "1",
 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
+		{"/sys/kernel/mm/ksm/smart_scan", "0",
+			TST_SR_SKIP_MISSING | TST_SR_TBROK_RO},
 		{}
 	},
 	.needs_kconfigs = (const char *const[]){
testcases/kernel/mem/ksm/ksm04.c +2 -0
index 4f1f2f721..9935e32d7 100644
@@ -109,6 +109,8 @@ static struct tst_test test = {
 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
 		{"/sys/kernel/mm/ksm/merge_across_nodes", "1",
 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
+		{"/sys/kernel/mm/ksm/smart_scan", "0",
+			TST_SR_SKIP_MISSING | TST_SR_TBROK_RO},
 		{}
 	},
 	.needs_kconfigs = (const char *const[]){
testcases/kernel/mem/ksm/ksm05.c +2 -0
index 1f58c8325..61e740843 100644
@@ -89,6 +89,8 @@ static struct tst_test test = {
 	.test_all = test_ksm,
 	.save_restore = (const struct tst_path_val[]) {
 		{"/sys/kernel/mm/ksm/run", "1", TST_SR_TBROK},
+		{"/sys/kernel/mm/ksm/smart_scan", "0",
+			TST_SR_SKIP_MISSING | TST_SR_TBROK_RO},
 		{}
 	},
 	.needs_kconfigs = (const char *const[]){
testcases/kernel/mem/ksm/ksm06.c +2 -0
index 0b159e5c7..80fdf1e47 100644
@@ -142,6 +142,8 @@ static struct tst_test test = {
 		{"/sys/kernel/mm/ksm/run", NULL, TST_SR_TBROK},
 		{"/sys/kernel/mm/ksm/sleep_millisecs", NULL, TST_SR_TBROK},
 		{"/sys/kernel/mm/ksm/merge_across_nodes", NULL, TST_SR_TCONF},
+		{"/sys/kernel/mm/ksm/smart_scan", "0",
+			TST_SR_SKIP_MISSING | TST_SR_TBROK_RO},
 		{}
 	},
 	.needs_kconfigs = (const char *const[]){
Signature · 1 line
2.39.3
#2 lore raw

Re: [PATCH v3 1/2] mem: disable KSM smart scan for ksm tests

Hi Stefan,

Quoted 2 lines

This disables the "smart scan" KSM feature to make sure that the volatile count remains at 0.

Merged this one, thank you!

Kind regards,
Petr

#3 lore raw

PATCH v3 2/2 add ksm test for smart-scan feature

In reply to Stefan Roesch

This adds a new ksm (kernel samepage merging) test to evaluate the new smart scan feature. It allocates a page and fills it with 'a' characters. It captures the pages_skipped counter, waits for a few iterations and captures the pages_skipped counter again. The expectation is that over 50% of the page scans are skipped (There is only one page that has KSM enabled and it gets scanned during each iteration and it cannot be de-duplicated).

---
 runtest/mm                       |   1 +
 testcases/kernel/mem/.gitignore  |   1 +
 testcases/kernel/mem/ksm/ksm07.c | 131 +++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+)
 create mode 100644 testcases/kernel/mem/ksm/ksm07.c
runtest/mm +1 -0
index f288fed36..d859b331c 100644
@@ -70,6 +70,7 @@ ksm05 ksm05 -I 10
 ksm06 ksm06
 ksm06_1 ksm06 -n 10
 ksm06_2 ksm06 -n 8000
+ksm07 ksm07
 
 cpuset01 cpuset01
 
testcases/kernel/mem/.gitignore +1 -0
index 7258489ed..c96fe8bfc 100644
@@ -53,6 +53,7 @@
 /ksm/ksm04
 /ksm/ksm05
 /ksm/ksm06
+/ksm/ksm07
 /mem/mem02
 /mmapstress/mmap-corruption01
 /mmapstress/mmapstress01
testcases/kernel/mem/ksm/ksm07.c new file +131 -0

131 lines not on this page · raw message

Signature · 1 line
2.39.3
#4 lore raw

Re: [LTP] [PATCH v3 2/2] add ksm test for smart-scan feature

Hi!

Quoted 18 lines
testcases/kernel/mem/.gitignore +1 -0
index 7258489ed..c96fe8bfc 100644
@@ -53,6 +53,7 @@
 /ksm/ksm04
 /ksm/ksm05
 /ksm/ksm06
+/ksm/ksm07
 /mem/mem02
 /mmapstress/mmap-corruption01
 /mmapstress/mmapstress01
testcases/kernel/mem/ksm/ksm07.c new file +0 -0
new file mode 100644
index 000000000..16153fdb2

testcases/kernel/mem/ksm/ksm07.c line 3

Quoted 3 lines
testcases/kernel/mem/ksm/ksm07.c new file +3 -0
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2010-2017  Red Hat, Inc.
                      ^
		      2023?
Quoted 9 lines
testcases/kernel/mem/ksm/ksm07.c new file +9 -0
+ */
+/*\
+ * [Description]
+ *
+ * Kernel Samepage Merging (KSM)
+ *
+ * This adds a new ksm (kernel samepage merging) test to evaluate the new
+ * smart scan feature. It allocates a page and fills it with 'a'
+ * characters. It captures the pages_skipped counter, waits for a few

testcases/kernel/mem/ksm/ksm07.c line 22

Quoted 10 lines
testcases/kernel/mem/ksm/ksm07.c new file +10 -0
+ * iterations and captures the pages_skipped counter again. The expectation
+ * is that over 50% of the page scans are skipped (There is only one page
+ * that has KSM enabled and it gets scanned during each iteration and it
+ * cannot be de-duplicated).
+ *
+ * Prerequisites:
+ *
+ * 1) ksm and ksmtuned daemons need to be disabled. Otherwise, it could
+ *    distrub the testing as they also change some ksm tunables depends
+ *    on current workloads.

I guess that it would be more user friendly to check if a process with such name is running and at least print a warning, or even skip the test.

I suppose that we can add a library function that would loop over /proc/*/comm and return true if we find a match.

Quoted 73 lines
testcases/kernel/mem/ksm/ksm07.c new file +73 -0
+ */
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "../include/mem.h"
+#include "ksm_common.h"
+
+/* This test allocates one page, fills the page with a's, captures the
+ * full_scan and pages_skipped counters. Then it makes sure at least 3
+ * full scans have been performed and measures the above counters again.
+ * The expectation is that at least 50% of the pages are skipped.
+ *
+ * To wait for at least 3 scans it uses the wait_ksmd_full_scan() function. In
+ * reality, it will be a lot more scans as the wait_ksmd_full_scan() function
+ * sleeps for one second.
+ */
+static void create_memory(void)
+{
+	int status;
+	int full_scans_begin;
+	int full_scans_end;
+	int pages_skipped_begin;
+	int pages_skipped_end;
+	int diff_pages;
+	int diff_scans;
+	unsigned long page_size;
+	char *memory;
+
+	/* Apply for the space for memory. */
+	page_size = sysconf(_SC_PAGE_SIZE);
+	memory = SAFE_MALLOC(page_size);
+
+	for (int i = 0; i < 1; i++) {
+		memory = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE,
+			MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+#ifdef HAVE_DECL_MADV_MERGEABLE
+		if (madvise(memory, page_size, MADV_MERGEABLE) == -1)
+			tst_brk(TBROK|TERRNO, "madvise");
+#endif
+	}
+	memset(memory, 'a', page_size);
+
+	tst_res(TINFO, "KSM merging...");
+	if (access(PATH_KSM "max_page_sharing", F_OK) == 0) {
+		SAFE_FILE_PRINTF(PATH_KSM "run", "2");
+	}
+
+	/* Set defalut ksm scan values. */
+	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
+	SAFE_FILE_PRINTF(PATH_KSM "pages_to_scan", "%ld", 100l);
+	SAFE_FILE_PRINTF(PATH_KSM "sleep_millisecs", "0");
+
+	/* Measure pages skipped aka "smart scan". */
+	SAFE_FILE_SCANF(PATH_KSM "full_scans", "%d", &full_scans_begin);
+	SAFE_FILE_SCANF(PATH_KSM "pages_skipped", "%d", &pages_skipped_begin);
+	wait_ksmd_full_scan();
+
+	tst_res(TINFO, "stop KSM.");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "0");
+
+	SAFE_FILE_SCANF(PATH_KSM "full_scans", "%d", &full_scans_end);
+	SAFE_FILE_SCANF(PATH_KSM "pages_skipped", "%d", &pages_skipped_end);
+	diff_pages = pages_skipped_end - pages_skipped_begin;
+	diff_scans = full_scans_end - full_scans_begin;
+

testcases/kernel/mem/ksm/ksm07.c line 105

Quoted 10 lines
testcases/kernel/mem/ksm/ksm07.c new file +10 -0
+	if (diff_pages < diff_scans * 50 / 100) {
+		tst_res(TFAIL, "not enough pages have been skipped by smart_scan.");
+	} else {
+		tst_res(TPASS, "smart_scan skipped more than 50%% of the pages.");
+	}
+
+	while (waitpid(-1, &status, 0) > 0)
+		if (WEXITSTATUS(status) != 0)
+			tst_res(TFAIL, "child exit status is %d",
+					WEXITSTATUS(status));

Does the test fork a child? I do not see any place in the code that would do so, did I miss something?

testcases/kernel/mem/ksm/ksm07.c line 110

Quoted 5 lines
testcases/kernel/mem/ksm/ksm07.c new file +5 -0
+}
+
+static void verify_ksm(void)
+{
+	create_memory();

This is useless indirection.

Quoted 27 lines
testcases/kernel/mem/ksm/ksm07.c new file +21 -0
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.forks_child = 1,
+	.options = (struct tst_option[]) {
+		{}
+	},
+	.save_restore = (const struct tst_path_val[]) {
+		{"/sys/kernel/mm/ksm/run", NULL, TST_SR_TCONF},
+		{"/sys/kernel/mm/ksm/sleep_millisecs", NULL, TST_SR_TCONF},
+		{"/sys/kernel/mm/ksm/smart_scan", "1",
+			TST_SR_SKIP_MISSING | TST_SR_TCONF},
+		{}
+	},
+	.needs_kconfigs = (const char *const[]){
+		"CONFIG_KSM=y",
+		NULL
+	},
+	.test_all = verify_ksm,
+};
Signature · 5 lines
2.39.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
#5 lore raw

Re: [LTP] [PATCH v3 2/2] add ksm test for smart-scan feature

Hi all,

...

Quoted 8 lines

testcases/kernel/mem/ksm/ksm07.c line 105

Quoted 5 lines
testcases/kernel/mem/ksm/ksm07.c new file +5 -0
+
+	while (waitpid(-1, &status, 0) > 0)
+		if (WEXITSTATUS(status) != 0)
+			tst_res(TFAIL, "child exit status is %d",
+					WEXITSTATUS(status));

Does the test fork a child? I do not see any place in the code that would do so, did I miss something?

Ah, I also pointed out this is useless, but forget to point out that .forks_child = 1 should be also removed.

Kind regards,
Petr

#6 lore raw

Re: [PATCH v3 2/2] add ksm test for smart-scan feature

In reply to Stefan Roesch

Hi Stefan,

Quoted 121 lines

This adds a new ksm (kernel samepage merging) test to evaluate the new smart scan feature. It allocates a page and fills it with 'a' characters. It captures the pages_skipped counter, waits for a few iterations and captures the pages_skipped counter again. The expectation is that over 50% of the page scans are skipped (There is only one page that has KSM enabled and it gets scanned during each iteration and it cannot be de-duplicated).

---
 runtest/mm                       |   1 +
 testcases/kernel/mem/.gitignore  |   1 +
 testcases/kernel/mem/ksm/ksm07.c | 131 +++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+)
 create mode 100644 testcases/kernel/mem/ksm/ksm07.c
runtest/mm +1 -0
index f288fed36..d859b331c 100644
@@ -70,6 +70,7 @@ ksm05 ksm05 -I 10
 ksm06 ksm06
 ksm06_1 ksm06 -n 10
 ksm06_2 ksm06 -n 8000
+ksm07 ksm07
 
 cpuset01 cpuset01
 
testcases/kernel/mem/.gitignore +1 -0
index 7258489ed..c96fe8bfc 100644
@@ -53,6 +53,7 @@
 /ksm/ksm04
 /ksm/ksm05
 /ksm/ksm06
+/ksm/ksm07
 /mem/mem02
 /mmapstress/mmap-corruption01
 /mmapstress/mmapstress01
testcases/kernel/mem/ksm/ksm07.c new file +75 -0
new file mode 100644
index 000000000..16153fdb2
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2010-2017  Red Hat, Inc.
+ */
+/*\
+ * [Description]
+ *
+ * Kernel Samepage Merging (KSM)
+ *
+ * This adds a new ksm (kernel samepage merging) test to evaluate the new
+ * smart scan feature. It allocates a page and fills it with 'a'
+ * characters. It captures the pages_skipped counter, waits for a few
+ * iterations and captures the pages_skipped counter again. The expectation
+ * is that over 50% of the page scans are skipped (There is only one page
+ * that has KSM enabled and it gets scanned during each iteration and it
+ * cannot be de-duplicated).
+ *
+ * Prerequisites:
+ *
+ * 1) ksm and ksmtuned daemons need to be disabled. Otherwise, it could
+ *    distrub the testing as they also change some ksm tunables depends
+ *    on current workloads.
+ */
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "../include/mem.h"
+#include "ksm_common.h"
+
+/* This test allocates one page, fills the page with a's, captures the
+ * full_scan and pages_skipped counters. Then it makes sure at least 3
+ * full scans have been performed and measures the above counters again.
+ * The expectation is that at least 50% of the pages are skipped.
+ *
+ * To wait for at least 3 scans it uses the wait_ksmd_full_scan() function. In
+ * reality, it will be a lot more scans as the wait_ksmd_full_scan() function
+ * sleeps for one second.
+ */
+static void create_memory(void)
+{
+	int status;
+	int full_scans_begin;
+	int full_scans_end;
+	int pages_skipped_begin;
+	int pages_skipped_end;
+	int diff_pages;
+	int diff_scans;
+	unsigned long page_size;
+	char *memory;
+
+	/* Apply for the space for memory. */
+	page_size = sysconf(_SC_PAGE_SIZE);
+	memory = SAFE_MALLOC(page_size);
+
+	for (int i = 0; i < 1; i++) {
+		memory = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE,
+			MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+#ifdef HAVE_DECL_MADV_MERGEABLE
+		if (madvise(memory, page_size, MADV_MERGEABLE) == -1)
+			tst_brk(TBROK|TERRNO, "madvise");
+#endif
+	}
+	memset(memory, 'a', page_size);
+
+	tst_res(TINFO, "KSM merging...");
+	if (access(PATH_KSM "max_page_sharing", F_OK) == 0) {
+		SAFE_FILE_PRINTF(PATH_KSM "run", "2");

testcases/kernel/mem/ksm/ksm07.c line 85

Quoted 10 lines
testcases/kernel/mem/ksm/ksm07.c new file +10 -0
+	}
+
+	/* Set defalut ksm scan values. */
+	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
+	SAFE_FILE_PRINTF(PATH_KSM "pages_to_scan", "%ld", 100l);
+	SAFE_FILE_PRINTF(PATH_KSM "sleep_millisecs", "0");
+
+	/* Measure pages skipped aka "smart scan". */
+	SAFE_FILE_SCANF(PATH_KSM "full_scans", "%d", &full_scans_begin);
+	SAFE_FILE_SCANF(PATH_KSM "pages_skipped", "%d", &pages_skipped_begin);

Unfortunately SAFE_FILE_SCANF quits on missing file and /sys/kernel/mm/ksm/pages_skipped is not on kernel < 6.7.

safe_file_vprintf() which SAFE_FILE_SCANF() internally uses does not support any flag to quit with TCONF instead TBROK when /sys/kernel/mm/ksm/pages_skipped does not exists.

We could use access() in setup function, but another line in .save_restore will help:

{PATH_KSM "pages_skipped", NULL, TST_SR_TCONF}

testcases/kernel/mem/ksm/ksm07.c line 94

Quoted 9 lines
testcases/kernel/mem/ksm/ksm07.c new file +9 -0
+	wait_ksmd_full_scan();
+
+	tst_res(TINFO, "stop KSM.");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "0");
+
+	SAFE_FILE_SCANF(PATH_KSM "full_scans", "%d", &full_scans_end);
+	SAFE_FILE_SCANF(PATH_KSM "pages_skipped", "%d", &pages_skipped_end);
+	diff_pages = pages_skipped_end - pages_skipped_begin;
+	diff_scans = full_scans_end - full_scans_begin;

I was going to merge this (with minor cleanup), but the only remaining issue is that we allow to test run repeatedly via -iN:

# ./ksm07 -i2
tst_test.c:1574: TINFO: Timeout per run is 0h 00m 30s
ksm07.c:73: TINFO: KSM merging...
ksm_helper.c:36: TINFO: ksm daemon takes 1s to run two full scans
ksm07.c:88: TINFO: stop KSM.
ksm07.c:99: TPASS: smart_scan skipped more than 50% of the pages.
ksm07.c:73: TINFO: KSM merging...
ksm_helper.c:36: TINFO: ksm daemon takes 1s to run two full scans
ksm07.c:88: TINFO: stop KSM.
ksm07.c:97: TFAIL: not enough pages have been skipped by smart_scan.

I'm confused, how to reset KSM to be able to run the test more times?

testcases/kernel/mem/ksm/ksm07.c line 106

Quoted 12 lines
testcases/kernel/mem/ksm/ksm07.c new file +12 -0
+
+	if (diff_pages < diff_scans * 50 / 100) {
+		tst_res(TFAIL, "not enough pages have been skipped by smart_scan.");
+	} else {
+		tst_res(TPASS, "smart_scan skipped more than 50%% of the pages.");
+	}
+
+	while (waitpid(-1, &status, 0) > 0)
+		if (WEXITSTATUS(status) != 0)
+			tst_res(TFAIL, "child exit status is %d",
+					WEXITSTATUS(status));
+}

This is not needed, done by check_child_status() in lib/tst_test.c.

testcases/kernel/mem/ksm/ksm07.c line 111

Quoted 5 lines
testcases/kernel/mem/ksm/ksm07.c new file +5 -0
+
+static void verify_ksm(void)
+{
+	create_memory();
+}

Why this wrapper? "verify_" as a pattern name is not a must.

testcases/kernel/mem/ksm/ksm07.c line 122

Quoted 11 lines
testcases/kernel/mem/ksm/ksm07.c new file +11 -0
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.forks_child = 1,
+	.options = (struct tst_option[]) {
+		{}
+	},
+	.save_restore = (const struct tst_path_val[]) {
+		{"/sys/kernel/mm/ksm/run", NULL, TST_SR_TCONF},
+		{"/sys/kernel/mm/ksm/sleep_millisecs", NULL, TST_SR_TCONF},
+		{"/sys/kernel/mm/ksm/smart_scan", "1",

We have PATH_KSM definition, lets use it.

testcases/kernel/mem/ksm/ksm07.c line 131

Quoted 9 lines
testcases/kernel/mem/ksm/ksm07.c new file +9 -0
+			TST_SR_SKIP_MISSING | TST_SR_TCONF},
+		{}
+	},
+	.needs_kconfigs = (const char *const[]){
+		"CONFIG_KSM=y",
+		NULL
+	},
+	.test_all = verify_ksm,
+};

I was going to merge your patch with following changes. We just need solve -i2 issue.

Kind regards,
Petr

kernel/mem/ksm/ksm07.c +27 -48
index 16153fdb2..e5c31775b 100644
@@ -5,35 +5,25 @@
 /*\
  * [Description]
  *
- * Kernel Samepage Merging (KSM)
+ * Kernel Samepage Merging (KSM) for smart scan feature
  *
- * This adds a new ksm (kernel samepage merging) test to evaluate the new
- * smart scan feature. It allocates a page and fills it with 'a'
- * characters. It captures the pages_skipped counter, waits for a few
- * iterations and captures the pages_skipped counter again. The expectation
- * is that over 50% of the page scans are skipped (There is only one page
- * that has KSM enabled and it gets scanned during each iteration and it
- * cannot be de-duplicated).
+ * Test allocates a page and fills it with 'a' characters. It captures the
+ * pages_skipped counter, waits for a few  iterations and captures the
+ * pages_skipped counter again. The expectation  is that over 50% of the page
+ * scans are skipped. (There is only one page that has KSM enabled and it gets
+ * scanned during each iteration and it cannot be de-duplicated.)
  *
- * Prerequisites:
+ * Smart scan feature was added in kernel v6.7.
  *
- * 1) ksm and ksmtuned daemons need to be disabled. Otherwise, it could
- *    distrub the testing as they also change some ksm tunables depends
- *    on current workloads.
+ * [Prerequisites]
+ *
+ * ksm and ksmtuned daemons need to be disabled. Otherwise, it could
+ * distrub the testing as they also change some ksm tunables depends
+ * on current workloads.
  */
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
+
 #include <sys/wait.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include "../include/mem.h"
-#include "ksm_common.h"
+#include "mem.h"
 
 /* This test allocates one page, fills the page with a's, captures the
  * full_scan and pages_skipped counters. Then it makes sure at least 3
@@ -46,7 +36,6 @@
  */
 static void create_memory(void)
 {
-	int status;
 	int full_scans_begin;
 	int full_scans_end;
 	int pages_skipped_begin;
@@ -70,10 +59,10 @@ static void create_memory(void)
 	}
 	memset(memory, 'a', page_size);
 
-	tst_res(TINFO, "KSM merging...");
-	if (access(PATH_KSM "max_page_sharing", F_OK) == 0) {
+	tst_res(TINFO, "KSM merging");
+
+	if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
 		SAFE_FILE_PRINTF(PATH_KSM "run", "2");
-	}
 
 	/* Set defalut ksm scan values. */
 	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
@@ -85,7 +74,7 @@ static void create_memory(void)
 	SAFE_FILE_SCANF(PATH_KSM "pages_skipped", "%d", &pages_skipped_begin);
 	wait_ksmd_full_scan();
 
-	tst_res(TINFO, "stop KSM.");
+	tst_res(TINFO, "stop KSM");
 	SAFE_FILE_PRINTF(PATH_KSM "run", "0");
 
 	SAFE_FILE_SCANF(PATH_KSM "full_scans", "%d", &full_scans_end);
@@ -93,21 +82,10 @@ static void create_memory(void)
 	diff_pages = pages_skipped_end - pages_skipped_begin;
 	diff_scans = full_scans_end - full_scans_begin;
 
-	if (diff_pages < diff_scans * 50 / 100) {
-		tst_res(TFAIL, "not enough pages have been skipped by smart_scan.");
-	} else {
-		tst_res(TPASS, "smart_scan skipped more than 50%% of the pages.");
-	}
-
-	while (waitpid(-1, &status, 0) > 0)
-		if (WEXITSTATUS(status) != 0)
-			tst_res(TFAIL, "child exit status is %d",
-					WEXITSTATUS(status));
-}
-
-static void verify_ksm(void)
-{
-	create_memory();
+	if (diff_pages < diff_scans * 50 / 100)
+		tst_res(TFAIL, "not enough pages have been skipped by smart_scan");
+	else
+		tst_res(TPASS, "smart_scan skipped more than 50%% of the pages");
 }
 
 static struct tst_test test = {
@@ -117,9 +95,10 @@ static struct tst_test test = {
 		{}
 	},
 	.save_restore = (const struct tst_path_val[]) {
-		{"/sys/kernel/mm/ksm/run", NULL, TST_SR_TCONF},
-		{"/sys/kernel/mm/ksm/sleep_millisecs", NULL, TST_SR_TCONF},
-		{"/sys/kernel/mm/ksm/smart_scan", "1",
+		{PATH_KSM "pages_skipped", NULL, TST_SR_TCONF},
+		{PATH_KSM "run", NULL, TST_SR_TCONF},
+		{PATH_KSM "sleep_millisecs", NULL, TST_SR_TCONF},
+		{PATH_KSM "smart_scan", "1",
 			TST_SR_SKIP_MISSING | TST_SR_TCONF},
 		{}
 	},
@@ -127,5 +106,5 @@ static struct tst_test test = {
 		"CONFIG_KSM=y",
 		NULL
 	},
-	.test_all = verify_ksm,
+	.test_all = create_memory,
 };
#7 lore raw

Re: [PATCH v3 2/2] add ksm test for smart-scan feature

Quoted 136 lines · Petr Vorel

Hi Stefan,

Quoted 121 lines

This adds a new ksm (kernel samepage merging) test to evaluate the new smart scan feature. It allocates a page and fills it with 'a' characters. It captures the pages_skipped counter, waits for a few iterations and captures the pages_skipped counter again. The expectation is that over 50% of the page scans are skipped (There is only one page that has KSM enabled and it gets scanned during each iteration and it cannot be de-duplicated).

---
 runtest/mm                       |   1 +
 testcases/kernel/mem/.gitignore  |   1 +
 testcases/kernel/mem/ksm/ksm07.c | 131 +++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+)
 create mode 100644 testcases/kernel/mem/ksm/ksm07.c
runtest/mm +1 -0
index f288fed36..d859b331c 100644
@@ -70,6 +70,7 @@ ksm05 ksm05 -I 10
 ksm06 ksm06
 ksm06_1 ksm06 -n 10
 ksm06_2 ksm06 -n 8000
+ksm07 ksm07
 
 cpuset01 cpuset01
 
testcases/kernel/mem/.gitignore +1 -0
index 7258489ed..c96fe8bfc 100644
@@ -53,6 +53,7 @@
 /ksm/ksm04
 /ksm/ksm05
 /ksm/ksm06
+/ksm/ksm07
 /mem/mem02
 /mmapstress/mmap-corruption01
 /mmapstress/mmapstress01
testcases/kernel/mem/ksm/ksm07.c new file +75 -0
new file mode 100644
index 000000000..16153fdb2
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2010-2017  Red Hat, Inc.
+ */
+/*\
+ * [Description]
+ *
+ * Kernel Samepage Merging (KSM)
+ *
+ * This adds a new ksm (kernel samepage merging) test to evaluate the new
+ * smart scan feature. It allocates a page and fills it with 'a'
+ * characters. It captures the pages_skipped counter, waits for a few
+ * iterations and captures the pages_skipped counter again. The expectation
+ * is that over 50% of the page scans are skipped (There is only one page
+ * that has KSM enabled and it gets scanned during each iteration and it
+ * cannot be de-duplicated).
+ *
+ * Prerequisites:
+ *
+ * 1) ksm and ksmtuned daemons need to be disabled. Otherwise, it could
+ *    distrub the testing as they also change some ksm tunables depends
+ *    on current workloads.
+ */
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "../include/mem.h"
+#include "ksm_common.h"
+
+/* This test allocates one page, fills the page with a's, captures the
+ * full_scan and pages_skipped counters. Then it makes sure at least 3
+ * full scans have been performed and measures the above counters again.
+ * The expectation is that at least 50% of the pages are skipped.
+ *
+ * To wait for at least 3 scans it uses the wait_ksmd_full_scan() function. In
+ * reality, it will be a lot more scans as the wait_ksmd_full_scan() function
+ * sleeps for one second.
+ */
+static void create_memory(void)
+{
+	int status;
+	int full_scans_begin;
+	int full_scans_end;
+	int pages_skipped_begin;
+	int pages_skipped_end;
+	int diff_pages;
+	int diff_scans;
+	unsigned long page_size;
+	char *memory;
+
+	/* Apply for the space for memory. */
+	page_size = sysconf(_SC_PAGE_SIZE);
+	memory = SAFE_MALLOC(page_size);
+
+	for (int i = 0; i < 1; i++) {
+		memory = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE,
+			MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+#ifdef HAVE_DECL_MADV_MERGEABLE
+		if (madvise(memory, page_size, MADV_MERGEABLE) == -1)
+			tst_brk(TBROK|TERRNO, "madvise");
+#endif
+	}
+	memset(memory, 'a', page_size);
+
+	tst_res(TINFO, "KSM merging...");
+	if (access(PATH_KSM "max_page_sharing", F_OK) == 0) {
+		SAFE_FILE_PRINTF(PATH_KSM "run", "2");

testcases/kernel/mem/ksm/ksm07.c line 85

Quoted 10 lines
testcases/kernel/mem/ksm/ksm07.c new file +10 -0
+	}
+
+	/* Set defalut ksm scan values. */
+	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
+	SAFE_FILE_PRINTF(PATH_KSM "pages_to_scan", "%ld", 100l);
+	SAFE_FILE_PRINTF(PATH_KSM "sleep_millisecs", "0");
+
+	/* Measure pages skipped aka "smart scan". */
+	SAFE_FILE_SCANF(PATH_KSM "full_scans", "%d", &full_scans_begin);
+	SAFE_FILE_SCANF(PATH_KSM "pages_skipped", "%d", &pages_skipped_begin);

Unfortunately SAFE_FILE_SCANF quits on missing file and /sys/kernel/mm/ksm/pages_skipped is not on kernel < 6.7.

Quoted 9 lines

safe_file_vprintf() which SAFE_FILE_SCANF() internally uses does not support any flag to quit with TCONF instead TBROK when /sys/kernel/mm/ksm/pages_skipped does not exists.

We could use access() in setup function, but another line in .save_restore will help:

{PATH_KSM "pages_skipped", NULL, TST_SR_TCONF}

I don't think this works correctly. I see a warning:

tst_sys_conf.c:144: TINFO: Failed to open FILE '/sys/kernel/mm/ksm/pages_skipped'

The difference to the other ksm files is, that this is not writeable.

Quoted 60 lines

testcases/kernel/mem/ksm/ksm07.c line 94

Quoted 9 lines
testcases/kernel/mem/ksm/ksm07.c new file +9 -0
+	wait_ksmd_full_scan();
+
+	tst_res(TINFO, "stop KSM.");
+	SAFE_FILE_PRINTF(PATH_KSM "run", "0");
+
+	SAFE_FILE_SCANF(PATH_KSM "full_scans", "%d", &full_scans_end);
+	SAFE_FILE_SCANF(PATH_KSM "pages_skipped", "%d", &pages_skipped_end);
+	diff_pages = pages_skipped_end - pages_skipped_begin;
+	diff_scans = full_scans_end - full_scans_begin;

I was going to merge this (with minor cleanup), but the only remaining issue is that we allow to test run repeatedly via -iN:

# ./ksm07 -i2
tst_test.c:1574: TINFO: Timeout per run is 0h 00m 30s
ksm07.c:73: TINFO: KSM merging...
ksm_helper.c:36: TINFO: ksm daemon takes 1s to run two full scans
ksm07.c:88: TINFO: stop KSM.
ksm07.c:99: TPASS: smart_scan skipped more than 50% of the pages.
ksm07.c:73: TINFO: KSM merging...
ksm_helper.c:36: TINFO: ksm daemon takes 1s to run two full scans
ksm07.c:88: TINFO: stop KSM.
ksm07.c:97: TFAIL: not enough pages have been skipped by smart_scan.

I'm confused, how to reset KSM to be able to run the test more times?

testcases/kernel/mem/ksm/ksm07.c line 106

Quoted 12 lines
testcases/kernel/mem/ksm/ksm07.c new file +12 -0
+
+	if (diff_pages < diff_scans * 50 / 100) {
+		tst_res(TFAIL, "not enough pages have been skipped by smart_scan.");
+	} else {
+		tst_res(TPASS, "smart_scan skipped more than 50%% of the pages.");
+	}
+
+	while (waitpid(-1, &status, 0) > 0)
+		if (WEXITSTATUS(status) != 0)
+			tst_res(TFAIL, "child exit status is %d",
+					WEXITSTATUS(status));
+}

This is not needed, done by check_child_status() in lib/tst_test.c.

testcases/kernel/mem/ksm/ksm07.c line 111

Quoted 5 lines
testcases/kernel/mem/ksm/ksm07.c new file +5 -0
+
+static void verify_ksm(void)
+{
+	create_memory();
+}

Why this wrapper? "verify_" as a pattern name is not a must.

testcases/kernel/mem/ksm/ksm07.c line 122

Quoted 11 lines
testcases/kernel/mem/ksm/ksm07.c new file +11 -0
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.forks_child = 1,
+	.options = (struct tst_option[]) {
+		{}
+	},
+	.save_restore = (const struct tst_path_val[]) {
+		{"/sys/kernel/mm/ksm/run", NULL, TST_SR_TCONF},
+		{"/sys/kernel/mm/ksm/sleep_millisecs", NULL, TST_SR_TCONF},
+		{"/sys/kernel/mm/ksm/smart_scan", "1",

We have PATH_KSM definition, lets use it.

Quoted 13 lines

testcases/kernel/mem/ksm/ksm07.c line 131

Quoted 9 lines
testcases/kernel/mem/ksm/ksm07.c new file +9 -0
+			TST_SR_SKIP_MISSING | TST_SR_TCONF},
+		{}
+	},
+	.needs_kconfigs = (const char *const[]){
+		"CONFIG_KSM=y",
+		NULL
+	},
+	.test_all = verify_ksm,
+};

I was going to merge your patch with following changes. We just need solve -i2 issue.

I have a solution for this: we need another madvise call at the end. I'll send it with the next version.

Quoted 135 lines

Kind regards,
Petr

kernel/mem/ksm/ksm07.c +27 -48
index 16153fdb2..e5c31775b 100644
@@ -5,35 +5,25 @@
 /*\
  * [Description]
  *
- * Kernel Samepage Merging (KSM)
+ * Kernel Samepage Merging (KSM) for smart scan feature
  *
- * This adds a new ksm (kernel samepage merging) test to evaluate the new
- * smart scan feature. It allocates a page and fills it with 'a'
- * characters. It captures the pages_skipped counter, waits for a few
- * iterations and captures the pages_skipped counter again. The expectation
- * is that over 50% of the page scans are skipped (There is only one page
- * that has KSM enabled and it gets scanned during each iteration and it
- * cannot be de-duplicated).
+ * Test allocates a page and fills it with 'a' characters. It captures the
+ * pages_skipped counter, waits for a few  iterations and captures the
+ * pages_skipped counter again. The expectation  is that over 50% of the page
+ * scans are skipped. (There is only one page that has KSM enabled and it gets
+ * scanned during each iteration and it cannot be de-duplicated.)
  *
- * Prerequisites:
+ * Smart scan feature was added in kernel v6.7.
  *
- * 1) ksm and ksmtuned daemons need to be disabled. Otherwise, it could
- *    distrub the testing as they also change some ksm tunables depends
- *    on current workloads.
+ * [Prerequisites]
+ *
+ * ksm and ksmtuned daemons need to be disabled. Otherwise, it could
+ * distrub the testing as they also change some ksm tunables depends
+ * on current workloads.
  */
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
+
 #include <sys/wait.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include "../include/mem.h"
-#include "ksm_common.h"
+#include "mem.h"
 
 /* This test allocates one page, fills the page with a's, captures the
  * full_scan and pages_skipped counters. Then it makes sure at least 3
@@ -46,7 +36,6 @@
  */
 static void create_memory(void)
 {
-	int status;
 	int full_scans_begin;
 	int full_scans_end;
 	int pages_skipped_begin;
@@ -70,10 +59,10 @@ static void create_memory(void)
 	}
 	memset(memory, 'a', page_size);
 
-	tst_res(TINFO, "KSM merging...");
-	if (access(PATH_KSM "max_page_sharing", F_OK) == 0) {
+	tst_res(TINFO, "KSM merging");
+
+	if (access(PATH_KSM "max_page_sharing", F_OK) == 0)
 		SAFE_FILE_PRINTF(PATH_KSM "run", "2");
-	}
 
 	/* Set defalut ksm scan values. */
 	SAFE_FILE_PRINTF(PATH_KSM "run", "1");
@@ -85,7 +74,7 @@ static void create_memory(void)
 	SAFE_FILE_SCANF(PATH_KSM "pages_skipped", "%d", &pages_skipped_begin);
 	wait_ksmd_full_scan();
 
-	tst_res(TINFO, "stop KSM.");
+	tst_res(TINFO, "stop KSM");
 	SAFE_FILE_PRINTF(PATH_KSM "run", "0");
 
 	SAFE_FILE_SCANF(PATH_KSM "full_scans", "%d", &full_scans_end);
@@ -93,21 +82,10 @@ static void create_memory(void)
 	diff_pages = pages_skipped_end - pages_skipped_begin;
 	diff_scans = full_scans_end - full_scans_begin;
 
-	if (diff_pages < diff_scans * 50 / 100) {
-		tst_res(TFAIL, "not enough pages have been skipped by smart_scan.");
-	} else {
-		tst_res(TPASS, "smart_scan skipped more than 50%% of the pages.");
-	}
-
-	while (waitpid(-1, &status, 0) > 0)
-		if (WEXITSTATUS(status) != 0)
-			tst_res(TFAIL, "child exit status is %d",
-					WEXITSTATUS(status));
-}
-
-static void verify_ksm(void)
-{
-	create_memory();
+	if (diff_pages < diff_scans * 50 / 100)
+		tst_res(TFAIL, "not enough pages have been skipped by smart_scan");
+	else
+		tst_res(TPASS, "smart_scan skipped more than 50%% of the pages");
 }
 
 static struct tst_test test = {
@@ -117,9 +95,10 @@ static struct tst_test test = {
 		{}
 	},
 	.save_restore = (const struct tst_path_val[]) {
-		{"/sys/kernel/mm/ksm/run", NULL, TST_SR_TCONF},
-		{"/sys/kernel/mm/ksm/sleep_millisecs", NULL, TST_SR_TCONF},
-		{"/sys/kernel/mm/ksm/smart_scan", "1",
+		{PATH_KSM "pages_skipped", NULL, TST_SR_TCONF},
+		{PATH_KSM "run", NULL, TST_SR_TCONF},
+		{PATH_KSM "sleep_millisecs", NULL, TST_SR_TCONF},
+		{PATH_KSM "smart_scan", "1",
 			TST_SR_SKIP_MISSING | TST_SR_TCONF},
 		{}
 	},
@@ -127,5 +106,5 @@ static struct tst_test test = {
 		"CONFIG_KSM=y",
 		NULL
 	},
-	.test_all = verify_ksm,
+	.test_all = create_memory,
 };
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp