All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4] add KSM selftests
@ 2021-07-14  8:56 Zhansaya Bagdauletkyzy
  2021-07-14  8:56 ` [PATCH v2 1/4] selftests: vm: add KSM merge test Zhansaya Bagdauletkyzy
                   ` (4 more replies)
  0 siblings, 5 replies; 18+ messages in thread
From: Zhansaya Bagdauletkyzy @ 2021-07-14  8:56 UTC (permalink / raw)
  To: shuah, akpm
  Cc: linux-kernel, linux-kselftest, linux-mm, tyhicks, pasha.tatashin

Introduce selftests to validate the functionality of KSM. The tests are
run on private anonymous pages. Since some KSM tunables are modified,
their starting values are saved and restored after testing. At the
start, run is set to 2 to ensure that only test pages will be merged (we
assume that no applications make madvise syscalls in the background). If
KSM config not enabled, all tests will be skipped.

Zhansaya Bagdauletkyzy (4):
  selftests: vm: add KSM merge test
  selftests: vm: add KSM unmerge test
  selftests: vm: add KSM zero page merging test
  selftests: vm: add KSM merging across nodes test

v1 -> v2:
- add a test to check KSM unmerging
- add a test to check merging of zero pages
- add a test to check merging in different NUMA nodes
- include command line options for each test
- new options to specify use_zero_pages and merge_across_nodes
- run each test case in run_vmtests.sh
- add some helper functions to make the code more compact:
  allocate_memory(), ksm_do_scan(), ksm_merge_pages()

 tools/testing/selftests/vm/.gitignore     |   1 +
 tools/testing/selftests/vm/Makefile       |   3 +
 tools/testing/selftests/vm/ksm_tests.c    | 516 ++++++++++++++++++++++
 tools/testing/selftests/vm/run_vmtests.sh |  96 ++++
 4 files changed, 616 insertions(+)
 create mode 100644 tools/testing/selftests/vm/ksm_tests.c

-- 
2.25.1


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v2 1/4] selftests: vm: add KSM merge test
  2021-07-14  8:56 [PATCH v2 0/4] add KSM selftests Zhansaya Bagdauletkyzy
@ 2021-07-14  8:56 ` Zhansaya Bagdauletkyzy
  2021-08-04  1:54     ` Pavel Tatashin
  2021-08-16 13:57   ` Tyler Hicks
  2021-07-14  8:56 ` [PATCH v2 2/4] selftests: vm: add KSM unmerge test Zhansaya Bagdauletkyzy
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 18+ messages in thread
From: Zhansaya Bagdauletkyzy @ 2021-07-14  8:56 UTC (permalink / raw)
  To: shuah, akpm
  Cc: linux-kernel, linux-kselftest, linux-mm, tyhicks, pasha.tatashin

Add check_ksm_merge() function to check the basic merging feature of
KSM. First, some number of identical pages are allocated and the
MADV_MERGEABLE advice is given to merge these pages. Then, pages_shared
and pages_sharing values are compared with the expected numbers using
assert_ksm_pages_count() function. The number of pages can be changed
using -p option.

Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>
---
 tools/testing/selftests/vm/.gitignore     |   1 +
 tools/testing/selftests/vm/Makefile       |   1 +
 tools/testing/selftests/vm/ksm_tests.c    | 306 ++++++++++++++++++++++
 tools/testing/selftests/vm/run_vmtests.sh |  16 ++
 4 files changed, 324 insertions(+)
 create mode 100644 tools/testing/selftests/vm/ksm_tests.c

diff --git a/tools/testing/selftests/vm/.gitignore b/tools/testing/selftests/vm/.gitignore
index f0fd80ef17df..b02eac613fdd 100644
--- a/tools/testing/selftests/vm/.gitignore
+++ b/tools/testing/selftests/vm/.gitignore
@@ -27,3 +27,4 @@ hmm-tests
 memfd_secret
 local_config.*
 split_huge_page_test
+ksm_tests
diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile
index 521243770f26..e6f22a801b71 100644
--- a/tools/testing/selftests/vm/Makefile
+++ b/tools/testing/selftests/vm/Makefile
@@ -45,6 +45,7 @@ TEST_GEN_FILES += thuge-gen
 TEST_GEN_FILES += transhuge-stress
 TEST_GEN_FILES += userfaultfd
 TEST_GEN_FILES += split_huge_page_test
+TEST_GEN_FILES += ksm_tests
 
 ifeq ($(MACHINE),x86_64)
 CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh $(CC) ../x86/trivial_32bit_program.c -m32)
diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c
new file mode 100644
index 000000000000..d74d5aa34b16
--- /dev/null
+++ b/tools/testing/selftests/vm/ksm_tests.c
@@ -0,0 +1,306 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <sys/mman.h>
+#include <stdbool.h>
+#include <time.h>
+#include <string.h>
+
+#include "../kselftest.h"
+
+#define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"
+#define KSM_FP(s) (KSM_SYSFS_PATH s)
+#define KSM_SCAN_LIMIT_SEC_DEFAULT 120
+#define KSM_PAGE_COUNT_DEFAULT 10l
+#define KSM_PROT_STR_DEFAULT "rw"
+
+struct ksm_sysfs {
+	unsigned long max_page_sharing;
+	unsigned long merge_across_nodes;
+	unsigned long pages_to_scan;
+	unsigned long run;
+	unsigned long sleep_millisecs;
+	unsigned long stable_node_chains_prune_millisecs;
+	unsigned long use_zero_pages;
+};
+
+static int ksm_write_sysfs(const char *file_path, unsigned long val)
+{
+	FILE *f = fopen(file_path, "w");
+
+	if (!f) {
+		fprintf(stderr, "f %s\n", file_path);
+		perror("fopen");
+		return 1;
+	}
+	if (fprintf(f, "%lu", val) < 0) {
+		perror("fprintf");
+		return 1;
+	}
+	fclose(f);
+
+	return 0;
+}
+
+static int ksm_read_sysfs(const char *file_path, unsigned long *val)
+{
+	FILE *f = fopen(file_path, "r");
+
+	if (!f) {
+		fprintf(stderr, "f %s\n", file_path);
+		perror("fopen");
+		return 1;
+	}
+	if (fscanf(f, "%lu", val) != 1) {
+		perror("fscanf");
+		return 1;
+	}
+	fclose(f);
+
+	return 0;
+}
+
+static int str_to_prot(char *prot_str)
+{
+	int prot = 0;
+
+	if ((strchr(prot_str, 'r')) != NULL)
+		prot |= PROT_READ;
+	if ((strchr(prot_str, 'w')) != NULL)
+		prot |= PROT_WRITE;
+	if ((strchr(prot_str, 'x')) != NULL)
+		prot |= PROT_EXEC;
+
+	return prot;
+}
+
+static void print_help(void)
+{
+	printf("usage: ksm_tests [-h] [-a prot] [-p page_count] [-l timeout]\n");
+	printf(" -a: specify the access protections of pages.\n"
+	       "     <prot> must be of the form [rwx].\n"
+	       "     Default: %s\n", KSM_PROT_STR_DEFAULT);
+	printf(" -p: specify the number of pages to test.\n"
+	       "     Default: %ld\n", KSM_PAGE_COUNT_DEFAULT);
+	printf(" -l: limit the maximum running time (in seconds) for a test.\n"
+	       "     Default: %d seconds\n", KSM_SCAN_LIMIT_SEC_DEFAULT);
+
+	exit(0);
+}
+
+static void  *allocate_memory(void *ptr, int prot, int mapping, char data, size_t map_size)
+{
+	void *map_ptr = mmap(ptr, map_size, PROT_WRITE, mapping, -1, 0);
+
+	if (!map_ptr) {
+		perror("mmap");
+		return NULL;
+	}
+	memset(map_ptr, data, map_size);
+	if (mprotect(map_ptr, map_size, prot)) {
+		perror("mprotect");
+		munmap(map_ptr, map_size);
+		return NULL;
+	}
+
+	return map_ptr;
+}
+
+static int ksm_do_scan(int scan_count, struct timespec start_time, int timeout)
+{
+	struct timespec cur_time;
+	unsigned long cur_scan, init_scan;
+
+	if (ksm_read_sysfs(KSM_FP("full_scans"), &init_scan))
+		return 1;
+	cur_scan = init_scan;
+
+	while (cur_scan < init_scan + scan_count) {
+		if (ksm_read_sysfs(KSM_FP("full_scans"), &cur_scan))
+			return 1;
+		if (clock_gettime(CLOCK_MONOTONIC_RAW, &cur_time)) {
+			perror("clock_gettime");
+			return 1;
+		}
+		if ((cur_time.tv_sec - start_time.tv_sec) > timeout) {
+			printf("Scan time limit exceeded\n");
+			return 1;
+		}
+	}
+
+	return 0;
+}
+
+static int ksm_merge_pages(void *addr, size_t size, struct timespec start_time, int timeout)
+{
+	if (madvise(addr, size, MADV_MERGEABLE)) {
+		perror("madvise");
+		return 1;
+	}
+	if (ksm_write_sysfs(KSM_FP("run"), 1))
+		return 1;
+
+	/* Since merging occurs only after 2 scans, make sure to get at least 2 full scans */
+	if (ksm_do_scan(2, start_time, timeout))
+		return 1;
+
+	return 0;
+}
+
+static bool assert_ksm_pages_count(long dupl_page_count)
+{
+	unsigned long max_page_sharing, pages_sharing, pages_shared;
+
+	if (ksm_read_sysfs(KSM_FP("pages_shared"), &pages_shared) ||
+	    ksm_read_sysfs(KSM_FP("pages_sharing"), &pages_sharing) ||
+	    ksm_read_sysfs(KSM_FP("max_page_sharing"), &max_page_sharing))
+		return false;
+
+	/*
+	 * Since there must be at least 2 pages for merging and 1 page can be
+	 * shared with the limited number of pages (max_page_sharing), sometimes
+	 * there are 'leftover' pages that cannot be merged. For example, if there
+	 * are 11 pages and max_page_sharing = 10, then only 10 pages will be
+	 * merged and the 11th page won't be affected. As a result, when the number
+	 * of duplicate pages is divided by max_page_sharing and the remainder is 1,
+	 * pages_shared and pages_sharing values will be equal between dupl_page_count
+	 * and dupl_page_count - 1.
+	 */
+	if (dupl_page_count % max_page_sharing == 1 || dupl_page_count % max_page_sharing == 0) {
+		if (pages_shared == dupl_page_count / max_page_sharing &&
+		    pages_sharing == pages_shared * (max_page_sharing - 1))
+			return true;
+	} else {
+		if (pages_shared == (dupl_page_count / max_page_sharing + 1) &&
+		    pages_sharing == dupl_page_count - pages_shared)
+			return true;
+	}
+
+	return false;
+}
+
+static int ksm_save_def(struct ksm_sysfs *ksm_sysfs)
+{
+	if (ksm_read_sysfs(KSM_FP("max_page_sharing"), &ksm_sysfs->max_page_sharing) ||
+	    ksm_read_sysfs(KSM_FP("merge_across_nodes"), &ksm_sysfs->merge_across_nodes) ||
+	    ksm_read_sysfs(KSM_FP("sleep_millisecs"), &ksm_sysfs->sleep_millisecs) ||
+	    ksm_read_sysfs(KSM_FP("pages_to_scan"), &ksm_sysfs->pages_to_scan) ||
+	    ksm_read_sysfs(KSM_FP("run"), &ksm_sysfs->run) ||
+	    ksm_read_sysfs(KSM_FP("stable_node_chains_prune_millisecs"),
+			   &ksm_sysfs->stable_node_chains_prune_millisecs) ||
+	    ksm_read_sysfs(KSM_FP("use_zero_pages"), &ksm_sysfs->use_zero_pages))
+		return 1;
+
+	return 0;
+}
+
+static int ksm_restore(struct ksm_sysfs *ksm_sysfs)
+{
+	if (ksm_write_sysfs(KSM_FP("max_page_sharing"), ksm_sysfs->max_page_sharing) ||
+	    ksm_write_sysfs(KSM_FP("merge_across_nodes"), ksm_sysfs->merge_across_nodes) ||
+	    ksm_write_sysfs(KSM_FP("pages_to_scan"), ksm_sysfs->pages_to_scan) ||
+	    ksm_write_sysfs(KSM_FP("run"), ksm_sysfs->run) ||
+	    ksm_write_sysfs(KSM_FP("sleep_millisecs"), ksm_sysfs->sleep_millisecs) ||
+	    ksm_write_sysfs(KSM_FP("stable_node_chains_prune_millisecs"),
+			    ksm_sysfs->stable_node_chains_prune_millisecs) ||
+	    ksm_write_sysfs(KSM_FP("use_zero_pages"), ksm_sysfs->use_zero_pages))
+		return 1;
+
+	return 0;
+}
+
+static int check_ksm_merge(int mapping, int prot, long page_count, int timeout, size_t page_size)
+{
+	void *map_ptr;
+	struct timespec start_time;
+
+	if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
+		perror("clock_gettime");
+		return KSFT_FAIL;
+	}
+
+	/* fill pages with the same data and merge them */
+	map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
+	if (!map_ptr)
+		return KSFT_FAIL;
+
+	if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
+		goto err_out;
+
+	/* verify that the right number of pages are merged */
+	if (assert_ksm_pages_count(page_count)) {
+		printf("OK\n");
+		munmap(map_ptr, page_size * page_count);
+		return KSFT_PASS;
+	}
+
+err_out:
+	printf("Not OK\n");
+	munmap(map_ptr, page_size * page_count);
+	return KSFT_FAIL;
+}
+
+int main(int argc, char *argv[])
+{
+	int ret, opt;
+	int prot = 0;
+	int ksm_scan_limit_sec = KSM_SCAN_LIMIT_SEC_DEFAULT;
+	long page_count = KSM_PAGE_COUNT_DEFAULT;
+	size_t page_size = sysconf(_SC_PAGESIZE);
+	struct ksm_sysfs ksm_sysfs_old;
+
+	while ((opt = getopt(argc, argv, "ha:p:l:")) != -1) {
+		switch (opt) {
+		case 'a':
+			prot = str_to_prot(optarg);
+			break;
+		case 'p':
+			page_count = atol(optarg);
+			if (page_count <= 0) {
+				printf("The number of pages must be greater than 0\n");
+				return KSFT_FAIL;
+			}
+			break;
+		case 'l':
+			ksm_scan_limit_sec = atoi(optarg);
+			if (ksm_scan_limit_sec <= 0) {
+				printf("Timeout value must be greater than 0\n");
+				return KSFT_FAIL;
+			}
+			break;
+		case 'h':
+			print_help();
+			break;
+		default:
+			return KSFT_FAIL;
+		}
+	}
+
+	if (prot == 0)
+		prot = str_to_prot(KSM_PROT_STR_DEFAULT);
+
+	if (access(KSM_SYSFS_PATH, F_OK)) {
+		printf("Config KSM not enabled\n");
+		return KSFT_SKIP;
+	}
+
+	if (ksm_save_def(&ksm_sysfs_old)) {
+		printf("Cannot save default tunables\n");
+		return KSFT_FAIL;
+	}
+
+	if (ksm_write_sysfs(KSM_FP("run"), 2) ||
+	    ksm_write_sysfs(KSM_FP("sleep_millisecs"), 0) ||
+	    ksm_write_sysfs(KSM_FP("merge_across_nodes"), 1) ||
+	    ksm_write_sysfs(KSM_FP("pages_to_scan"), page_count))
+		return KSFT_FAIL;
+
+	ret = check_ksm_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count, ksm_scan_limit_sec,
+			      page_size);
+
+	if (ksm_restore(&ksm_sysfs_old)) {
+		printf("Cannot restore default tunables\n");
+		return KSFT_FAIL;
+	}
+
+	return ret;
+}
diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh
index d09a6b71f1e9..97b6f712134d 100755
--- a/tools/testing/selftests/vm/run_vmtests.sh
+++ b/tools/testing/selftests/vm/run_vmtests.sh
@@ -377,6 +377,22 @@ else
 	exitcode=1
 fi
 
+echo "-------------------------------------------------------"
+echo "running KSM MADV_MERGEABLE test with 10 identical pages"
+echo "-------------------------------------------------------"
+./ksm_tests -p 10
+ret_val=$?
+
+if [ $ret_val -eq 0 ]; then
+	echo "[PASS]"
+elif [ $ret_val -eq $ksft_skip ]; then
+	 echo "[SKIP]"
+	 exitcode=$ksft_skip
+else
+	echo "[FAIL]"
+	exitcode=1
+fi
+
 exit $exitcode
 
 exit $exitcode
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 2/4] selftests: vm: add KSM unmerge test
  2021-07-14  8:56 [PATCH v2 0/4] add KSM selftests Zhansaya Bagdauletkyzy
  2021-07-14  8:56 ` [PATCH v2 1/4] selftests: vm: add KSM merge test Zhansaya Bagdauletkyzy
@ 2021-07-14  8:56 ` Zhansaya Bagdauletkyzy
  2021-08-04  1:58     ` Pavel Tatashin
  2021-08-16 13:57   ` Tyler Hicks
  2021-07-14  8:56 ` [PATCH v2 3/4] selftests: vm: add KSM zero page merging test Zhansaya Bagdauletkyzy
                   ` (2 subsequent siblings)
  4 siblings, 2 replies; 18+ messages in thread
From: Zhansaya Bagdauletkyzy @ 2021-07-14  8:56 UTC (permalink / raw)
  To: shuah, akpm
  Cc: linux-kernel, linux-kselftest, linux-mm, tyhicks, pasha.tatashin

Add check_ksm_unmerge() function to verify that KSM is properly
unmerging shared pages. For this, two duplicate pages are merged first
and then their contents are modified. Since they are not identical
anymore, the pages must be unmerged and the number of merged pages has
to be 0. The test is run as follows: ./ksm_tests -U

Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>
---
 tools/testing/selftests/vm/ksm_tests.c    | 72 +++++++++++++++++++++--
 tools/testing/selftests/vm/run_vmtests.sh | 18 +++++-
 2 files changed, 85 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c
index d74d5aa34b16..80302bb8f64c 100644
--- a/tools/testing/selftests/vm/ksm_tests.c
+++ b/tools/testing/selftests/vm/ksm_tests.c
@@ -23,6 +23,11 @@ struct ksm_sysfs {
 	unsigned long use_zero_pages;
 };
 
+enum ksm_test_name {
+	CHECK_KSM_MERGE,
+	CHECK_KSM_UNMERGE
+};
+
 static int ksm_write_sysfs(const char *file_path, unsigned long val)
 {
 	FILE *f = fopen(file_path, "w");
@@ -75,7 +80,12 @@ static int str_to_prot(char *prot_str)
 
 static void print_help(void)
 {
-	printf("usage: ksm_tests [-h] [-a prot] [-p page_count] [-l timeout]\n");
+	printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n");
+
+	printf("Supported <test type>:\n"
+	       " -M (page merging)\n"
+	       " -U (page unmerging)\n\n");
+
 	printf(" -a: specify the access protections of pages.\n"
 	       "     <prot> must be of the form [rwx].\n"
 	       "     Default: %s\n", KSM_PROT_STR_DEFAULT);
@@ -239,6 +249,46 @@ static int check_ksm_merge(int mapping, int prot, long page_count, int timeout,
 	return KSFT_FAIL;
 }
 
+static int check_ksm_unmerge(int mapping, int prot, int timeout, size_t page_size)
+{
+	void *map_ptr;
+	struct timespec start_time;
+	int page_count = 2;
+
+	if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
+		perror("clock_gettime");
+		return KSFT_FAIL;
+	}
+
+	/* fill pages with the same data and merge them */
+	map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
+	if (!map_ptr)
+		return KSFT_FAIL;
+
+	if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
+		goto err_out;
+
+	/* change 1 byte in each of the 2 pages -- KSM must automatically unmerge them */
+	memset(map_ptr, '-', 1);
+	memset(map_ptr + page_size, '+', 1);
+
+	/* get at least 1 scan, so KSM can detect that the pages were modified */
+	if (ksm_do_scan(1, start_time, timeout))
+		goto err_out;
+
+	/* check that unmerging was successful and 0 pages are currently merged */
+	if (assert_ksm_pages_count(0)) {
+		printf("OK\n");
+		munmap(map_ptr, page_size * page_count);
+		return KSFT_PASS;
+	}
+
+err_out:
+	printf("Not OK\n");
+	munmap(map_ptr, page_size * page_count);
+	return KSFT_FAIL;
+}
+
 int main(int argc, char *argv[])
 {
 	int ret, opt;
@@ -247,8 +297,9 @@ int main(int argc, char *argv[])
 	long page_count = KSM_PAGE_COUNT_DEFAULT;
 	size_t page_size = sysconf(_SC_PAGESIZE);
 	struct ksm_sysfs ksm_sysfs_old;
+	int test_name = CHECK_KSM_MERGE;
 
-	while ((opt = getopt(argc, argv, "ha:p:l:")) != -1) {
+	while ((opt = getopt(argc, argv, "ha:p:l:MU")) != -1) {
 		switch (opt) {
 		case 'a':
 			prot = str_to_prot(optarg);
@@ -270,6 +321,11 @@ int main(int argc, char *argv[])
 		case 'h':
 			print_help();
 			break;
+		case 'M':
+			break;
+		case 'U':
+			test_name = CHECK_KSM_UNMERGE;
+			break;
 		default:
 			return KSFT_FAIL;
 		}
@@ -294,8 +350,16 @@ int main(int argc, char *argv[])
 	    ksm_write_sysfs(KSM_FP("pages_to_scan"), page_count))
 		return KSFT_FAIL;
 
-	ret = check_ksm_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count, ksm_scan_limit_sec,
-			      page_size);
+	switch (test_name) {
+	case CHECK_KSM_MERGE:
+		ret = check_ksm_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count,
+				      ksm_scan_limit_sec, page_size);
+		break;
+	case CHECK_KSM_UNMERGE:
+		ret = check_ksm_unmerge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
+					page_size);
+		break;
+	}
 
 	if (ksm_restore(&ksm_sysfs_old)) {
 		printf("Cannot restore default tunables\n");
diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh
index 97b6f712134d..3a23c6b47da2 100755
--- a/tools/testing/selftests/vm/run_vmtests.sh
+++ b/tools/testing/selftests/vm/run_vmtests.sh
@@ -380,7 +380,23 @@ fi
 echo "-------------------------------------------------------"
 echo "running KSM MADV_MERGEABLE test with 10 identical pages"
 echo "-------------------------------------------------------"
-./ksm_tests -p 10
+./ksm_tests -M -p 10
+ret_val=$?
+
+if [ $ret_val -eq 0 ]; then
+	echo "[PASS]"
+elif [ $ret_val -eq $ksft_skip ]; then
+	 echo "[SKIP]"
+	 exitcode=$ksft_skip
+else
+	echo "[FAIL]"
+	exitcode=1
+fi
+
+echo "------------------------"
+echo "running KSM unmerge test"
+echo "------------------------"
+./ksm_tests -U
 ret_val=$?
 
 if [ $ret_val -eq 0 ]; then
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 3/4] selftests: vm: add KSM zero page merging test
  2021-07-14  8:56 [PATCH v2 0/4] add KSM selftests Zhansaya Bagdauletkyzy
  2021-07-14  8:56 ` [PATCH v2 1/4] selftests: vm: add KSM merge test Zhansaya Bagdauletkyzy
  2021-07-14  8:56 ` [PATCH v2 2/4] selftests: vm: add KSM unmerge test Zhansaya Bagdauletkyzy
@ 2021-07-14  8:56 ` Zhansaya Bagdauletkyzy
  2021-08-04  1:59     ` Pavel Tatashin
  2021-08-16 14:00   ` Tyler Hicks
  2021-07-14  8:56 ` [PATCH v2 4/4] selftests: vm: add KSM merging across nodes test Zhansaya Bagdauletkyzy
  2021-07-14 22:51 ` [PATCH v2 0/4] add KSM selftests Andrew Morton
  4 siblings, 2 replies; 18+ messages in thread
From: Zhansaya Bagdauletkyzy @ 2021-07-14  8:56 UTC (permalink / raw)
  To: shuah, akpm
  Cc: linux-kernel, linux-kselftest, linux-mm, tyhicks, pasha.tatashin

Add check_ksm_zero_page_merge() function to test that empty pages are
being handled properly. For this, several zero pages are allocated and
merged using madvise. If use_zero_pages is enabled, the pages must be
shared with the special kernel zero pages; otherwise, they  are merged
as usual duplicate pages. The test is run as follows: ./ksm_tests -Z

Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>
---
 tools/testing/selftests/vm/ksm_tests.c    | 70 ++++++++++++++++++++++-
 tools/testing/selftests/vm/run_vmtests.sh | 32 +++++++++++
 2 files changed, 99 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c
index 80302bb8f64c..5843526471e1 100644
--- a/tools/testing/selftests/vm/ksm_tests.c
+++ b/tools/testing/selftests/vm/ksm_tests.c
@@ -12,6 +12,7 @@
 #define KSM_SCAN_LIMIT_SEC_DEFAULT 120
 #define KSM_PAGE_COUNT_DEFAULT 10l
 #define KSM_PROT_STR_DEFAULT "rw"
+#define KSM_USE_ZERO_PAGES_DEFAULT false
 
 struct ksm_sysfs {
 	unsigned long max_page_sharing;
@@ -25,7 +26,8 @@ struct ksm_sysfs {
 
 enum ksm_test_name {
 	CHECK_KSM_MERGE,
-	CHECK_KSM_UNMERGE
+	CHECK_KSM_UNMERGE,
+	CHECK_KSM_ZERO_PAGE_MERGE
 };
 
 static int ksm_write_sysfs(const char *file_path, unsigned long val)
@@ -80,10 +82,12 @@ static int str_to_prot(char *prot_str)
 
 static void print_help(void)
 {
-	printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n");
+	printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n"
+	       "[-z use_zero_pages]\n");
 
 	printf("Supported <test type>:\n"
 	       " -M (page merging)\n"
+	       " -Z (zero pages merging)\n"
 	       " -U (page unmerging)\n\n");
 
 	printf(" -a: specify the access protections of pages.\n"
@@ -93,6 +97,8 @@ static void print_help(void)
 	       "     Default: %ld\n", KSM_PAGE_COUNT_DEFAULT);
 	printf(" -l: limit the maximum running time (in seconds) for a test.\n"
 	       "     Default: %d seconds\n", KSM_SCAN_LIMIT_SEC_DEFAULT);
+	printf(" -z: change use_zero_pages tunable\n"
+	       "     Default: %d\n", KSM_USE_ZERO_PAGES_DEFAULT);
 
 	exit(0);
 }
@@ -289,6 +295,50 @@ static int check_ksm_unmerge(int mapping, int prot, int timeout, size_t page_siz
 	return KSFT_FAIL;
 }
 
+static int check_ksm_zero_page_merge(int mapping, int prot, long page_count, int timeout,
+				     bool use_zero_pages, size_t page_size)
+{
+	void *map_ptr;
+	struct timespec start_time;
+
+	if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
+		perror("clock_gettime");
+		return KSFT_FAIL;
+	}
+
+	if (ksm_write_sysfs(KSM_FP("use_zero_pages"), use_zero_pages))
+		return KSFT_FAIL;
+
+	/* fill pages with zero and try to merge them */
+	map_ptr = allocate_memory(NULL, prot, mapping, 0, page_size * page_count);
+	if (!map_ptr)
+		return KSFT_FAIL;
+
+	if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
+		goto err_out;
+
+       /*
+	* verify that the right number of pages are merged:
+	* 1) if use_zero_pages is set to 1, empty pages are merged
+	*    with the kernel zero page instead of with each other;
+	* 2) if use_zero_pages is set to 0, empty pages are not treated specially
+	*    and merged as usual.
+	*/
+	if (use_zero_pages && !assert_ksm_pages_count(0))
+		goto err_out;
+	else if (!use_zero_pages && !assert_ksm_pages_count(page_count))
+		goto err_out;
+
+	printf("OK\n");
+	munmap(map_ptr, page_size * page_count);
+	return KSFT_PASS;
+
+err_out:
+	printf("Not OK\n");
+	munmap(map_ptr, page_size * page_count);
+	return KSFT_FAIL;
+}
+
 int main(int argc, char *argv[])
 {
 	int ret, opt;
@@ -298,8 +348,9 @@ int main(int argc, char *argv[])
 	size_t page_size = sysconf(_SC_PAGESIZE);
 	struct ksm_sysfs ksm_sysfs_old;
 	int test_name = CHECK_KSM_MERGE;
+	bool use_zero_pages = KSM_USE_ZERO_PAGES_DEFAULT;
 
-	while ((opt = getopt(argc, argv, "ha:p:l:MU")) != -1) {
+	while ((opt = getopt(argc, argv, "ha:p:l:z:MUZ")) != -1) {
 		switch (opt) {
 		case 'a':
 			prot = str_to_prot(optarg);
@@ -321,11 +372,20 @@ int main(int argc, char *argv[])
 		case 'h':
 			print_help();
 			break;
+		case 'z':
+			if (strcmp(optarg, "0") == 0)
+				use_zero_pages = 0;
+			else
+				use_zero_pages = 1;
+			break;
 		case 'M':
 			break;
 		case 'U':
 			test_name = CHECK_KSM_UNMERGE;
 			break;
+		case 'Z':
+			test_name = CHECK_KSM_ZERO_PAGE_MERGE;
+			break;
 		default:
 			return KSFT_FAIL;
 		}
@@ -359,6 +419,10 @@ int main(int argc, char *argv[])
 		ret = check_ksm_unmerge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
 					page_size);
 		break;
+	case CHECK_KSM_ZERO_PAGE_MERGE:
+		ret = check_ksm_zero_page_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count,
+						ksm_scan_limit_sec, use_zero_pages, page_size);
+		break;
 	}
 
 	if (ksm_restore(&ksm_sysfs_old)) {
diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh
index 3a23c6b47da2..9b4e444fc4ed 100755
--- a/tools/testing/selftests/vm/run_vmtests.sh
+++ b/tools/testing/selftests/vm/run_vmtests.sh
@@ -409,6 +409,38 @@ else
 	exitcode=1
 fi
 
+echo "----------------------------------------------------------"
+echo "running KSM test with 10 zero pages and use_zero_pages = 0"
+echo "----------------------------------------------------------"
+./ksm_tests -Z -p 10 -z 0
+ret_val=$?
+
+if [ $ret_val -eq 0 ]; then
+	echo "[PASS]"
+elif [ $ret_val -eq $ksft_skip ]; then
+	 echo "[SKIP]"
+	 exitcode=$ksft_skip
+else
+	echo "[FAIL]"
+	exitcode=1
+fi
+
+echo "----------------------------------------------------------"
+echo "running KSM test with 10 zero pages and use_zero_pages = 1"
+echo "----------------------------------------------------------"
+./ksm_tests -Z -p 10 -z 1
+ret_val=$?
+
+if [ $ret_val -eq 0 ]; then
+	echo "[PASS]"
+elif [ $ret_val -eq $ksft_skip ]; then
+	 echo "[SKIP]"
+	 exitcode=$ksft_skip
+else
+	echo "[FAIL]"
+	exitcode=1
+fi
+
 exit $exitcode
 
 exit $exitcode
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 4/4] selftests: vm: add KSM merging across nodes test
  2021-07-14  8:56 [PATCH v2 0/4] add KSM selftests Zhansaya Bagdauletkyzy
                   ` (2 preceding siblings ...)
  2021-07-14  8:56 ` [PATCH v2 3/4] selftests: vm: add KSM zero page merging test Zhansaya Bagdauletkyzy
@ 2021-07-14  8:56 ` Zhansaya Bagdauletkyzy
  2021-08-04  2:01     ` Pavel Tatashin
  2021-08-16 14:28   ` Tyler Hicks
  2021-07-14 22:51 ` [PATCH v2 0/4] add KSM selftests Andrew Morton
  4 siblings, 2 replies; 18+ messages in thread
From: Zhansaya Bagdauletkyzy @ 2021-07-14  8:56 UTC (permalink / raw)
  To: shuah, akpm
  Cc: linux-kernel, linux-kselftest, linux-mm, tyhicks, pasha.tatashin

Add check_ksm_numa_merge() function  to test that pages in different NUMA
nodes are being handled properly. First, two duplicate pages are allocated
in two separate NUMA nodes using the libnuma library. Since there is one
unique page in each node, with merge_across_nodes = 0, there won't be any
shared pages. If merge_across_nodes is set to 1, the pages will be
treated as usual duplicate pages and will be merged. If NUMA config is
not enabled or the number of NUMA nodes is less than two, then the test
is skipped. The test is run as follows: ./ksm_tests -N

Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>
---
 tools/testing/selftests/vm/Makefile       |  2 +
 tools/testing/selftests/vm/ksm_tests.c    | 88 ++++++++++++++++++++++-
 tools/testing/selftests/vm/run_vmtests.sh | 32 +++++++++
 3 files changed, 119 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile
index e6f22a801b71..d9605bd10f2d 100644
--- a/tools/testing/selftests/vm/Makefile
+++ b/tools/testing/selftests/vm/Makefile
@@ -146,6 +146,8 @@ $(OUTPUT)/hmm-tests: local_config.h
 # HMM_EXTRA_LIBS may get set in local_config.mk, or it may be left empty.
 $(OUTPUT)/hmm-tests: LDLIBS += $(HMM_EXTRA_LIBS)
 
+$(OUTPUT)/ksm_tests: LDLIBS += -lnuma
+
 local_config.mk local_config.h: check_config.sh
 	/bin/sh ./check_config.sh $(CC)
 
diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c
index 5843526471e1..cdeb4a028538 100644
--- a/tools/testing/selftests/vm/ksm_tests.c
+++ b/tools/testing/selftests/vm/ksm_tests.c
@@ -4,6 +4,7 @@
 #include <stdbool.h>
 #include <time.h>
 #include <string.h>
+#include <numa.h>
 
 #include "../kselftest.h"
 
@@ -13,6 +14,7 @@
 #define KSM_PAGE_COUNT_DEFAULT 10l
 #define KSM_PROT_STR_DEFAULT "rw"
 #define KSM_USE_ZERO_PAGES_DEFAULT false
+#define KSM_MERGE_ACROSS_NODES_DEFAULT true
 
 struct ksm_sysfs {
 	unsigned long max_page_sharing;
@@ -27,7 +29,8 @@ struct ksm_sysfs {
 enum ksm_test_name {
 	CHECK_KSM_MERGE,
 	CHECK_KSM_UNMERGE,
-	CHECK_KSM_ZERO_PAGE_MERGE
+	CHECK_KSM_ZERO_PAGE_MERGE,
+	CHECK_KSM_NUMA_MERGE
 };
 
 static int ksm_write_sysfs(const char *file_path, unsigned long val)
@@ -83,11 +86,12 @@ static int str_to_prot(char *prot_str)
 static void print_help(void)
 {
 	printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n"
-	       "[-z use_zero_pages]\n");
+	       "[-z use_zero_pages] [-m merge_across_nodes]\n");
 
 	printf("Supported <test type>:\n"
 	       " -M (page merging)\n"
 	       " -Z (zero pages merging)\n"
+	       " -N (merging of pages in different NUMA nodes)\n"
 	       " -U (page unmerging)\n\n");
 
 	printf(" -a: specify the access protections of pages.\n"
@@ -99,6 +103,8 @@ static void print_help(void)
 	       "     Default: %d seconds\n", KSM_SCAN_LIMIT_SEC_DEFAULT);
 	printf(" -z: change use_zero_pages tunable\n"
 	       "     Default: %d\n", KSM_USE_ZERO_PAGES_DEFAULT);
+	printf(" -m: change merge_across_nodes tunable\n"
+	       "     Default: %d\n", KSM_MERGE_ACROSS_NODES_DEFAULT);
 
 	exit(0);
 }
@@ -339,6 +345,68 @@ static int check_ksm_zero_page_merge(int mapping, int prot, long page_count, int
 	return KSFT_FAIL;
 }
 
+static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_across_nodes,
+				size_t page_size)
+{
+	void *numa1_map_ptr, *numa2_map_ptr;
+	struct timespec start_time;
+	int page_count = 2;
+
+	if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
+		perror("clock_gettime");
+		return KSFT_FAIL;
+	}
+
+	if (numa_available() < 0) {
+		perror("NUMA support not enabled");
+		return KSFT_SKIP;
+	}
+	if (numa_max_node() < 1) {
+		printf("At least 2 NUMA nodes must be available\n");
+		return KSFT_SKIP;
+	}
+	if (ksm_write_sysfs(KSM_FP("merge_across_nodes"), merge_across_nodes))
+		return KSFT_FAIL;
+
+	/* allocate 2 pages in 2 different NUMA nodes and fill them with the same data */
+	numa1_map_ptr = numa_alloc_onnode(page_size, 0);
+	numa2_map_ptr = numa_alloc_onnode(page_size, 1);
+	if (!numa1_map_ptr || !numa2_map_ptr) {
+		perror("numa_alloc_onnode");
+		return KSFT_FAIL;
+	}
+
+	memset(numa1_map_ptr, '*', page_size);
+	memset(numa2_map_ptr, '*', page_size);
+
+	/* try to merge the pages */
+	if (ksm_merge_pages(numa1_map_ptr, page_size, start_time, timeout) ||
+	    ksm_merge_pages(numa2_map_ptr, page_size, start_time, timeout))
+		goto err_out;
+
+       /*
+	* verify that the right number of pages are merged:
+	* 1) if merge_across_nodes was enabled, 2 duplicate pages will be merged;
+	* 2) if merge_across_nodes = 0, there must be 0 merged pages, since there is
+	*    only 1 unique page in each node and they can't be shared.
+	*/
+	if (merge_across_nodes && !assert_ksm_pages_count(page_count))
+		goto err_out;
+	else if (!merge_across_nodes && !assert_ksm_pages_count(0))
+		goto err_out;
+
+	numa_free(numa1_map_ptr, page_size);
+	numa_free(numa2_map_ptr, page_size);
+	printf("OK\n");
+	return KSFT_PASS;
+
+err_out:
+	numa_free(numa1_map_ptr, page_size);
+	numa_free(numa2_map_ptr, page_size);
+	printf("Not OK\n");
+	return KSFT_FAIL;
+}
+
 int main(int argc, char *argv[])
 {
 	int ret, opt;
@@ -349,8 +417,9 @@ int main(int argc, char *argv[])
 	struct ksm_sysfs ksm_sysfs_old;
 	int test_name = CHECK_KSM_MERGE;
 	bool use_zero_pages = KSM_USE_ZERO_PAGES_DEFAULT;
+	bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT;
 
-	while ((opt = getopt(argc, argv, "ha:p:l:z:MUZ")) != -1) {
+	while ((opt = getopt(argc, argv, "ha:p:l:z:m:MUZN")) != -1) {
 		switch (opt) {
 		case 'a':
 			prot = str_to_prot(optarg);
@@ -378,6 +447,12 @@ int main(int argc, char *argv[])
 			else
 				use_zero_pages = 1;
 			break;
+		case 'm':
+			if (strcmp(optarg, "0") == 0)
+				merge_across_nodes = 0;
+			else
+				merge_across_nodes = 1;
+			break;
 		case 'M':
 			break;
 		case 'U':
@@ -386,6 +461,9 @@ int main(int argc, char *argv[])
 		case 'Z':
 			test_name = CHECK_KSM_ZERO_PAGE_MERGE;
 			break;
+		case 'N':
+			test_name = CHECK_KSM_NUMA_MERGE;
+			break;
 		default:
 			return KSFT_FAIL;
 		}
@@ -423,6 +501,10 @@ int main(int argc, char *argv[])
 		ret = check_ksm_zero_page_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count,
 						ksm_scan_limit_sec, use_zero_pages, page_size);
 		break;
+	case CHECK_KSM_NUMA_MERGE:
+		ret = check_ksm_numa_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
+					   merge_across_nodes, page_size);
+		break;
 	}
 
 	if (ksm_restore(&ksm_sysfs_old)) {
diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh
index 9b4e444fc4ed..45e803af7c77 100755
--- a/tools/testing/selftests/vm/run_vmtests.sh
+++ b/tools/testing/selftests/vm/run_vmtests.sh
@@ -441,6 +441,38 @@ else
 	exitcode=1
 fi
 
+echo "-------------------------------------------------------------"
+echo "running KSM test with 2 NUMA nodes and merge_across_nodes = 1"
+echo "-------------------------------------------------------------"
+./ksm_tests -N -m 1
+ret_val=$?
+
+if [ $ret_val -eq 0 ]; then
+	echo "[PASS]"
+elif [ $ret_val -eq $ksft_skip ]; then
+	 echo "[SKIP]"
+	 exitcode=$ksft_skip
+else
+	echo "[FAIL]"
+	exitcode=1
+fi
+
+echo "-------------------------------------------------------------"
+echo "running KSM test with 2 NUMA nodes and merge_across_nodes = 0"
+echo "-------------------------------------------------------------"
+./ksm_tests -N -m 0
+ret_val=$?
+
+if [ $ret_val -eq 0 ]; then
+	echo "[PASS]"
+elif [ $ret_val -eq $ksft_skip ]; then
+	 echo "[SKIP]"
+	 exitcode=$ksft_skip
+else
+	echo "[FAIL]"
+	exitcode=1
+fi
+
 exit $exitcode
 
 exit $exitcode
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 0/4] add KSM selftests
  2021-07-14  8:56 [PATCH v2 0/4] add KSM selftests Zhansaya Bagdauletkyzy
                   ` (3 preceding siblings ...)
  2021-07-14  8:56 ` [PATCH v2 4/4] selftests: vm: add KSM merging across nodes test Zhansaya Bagdauletkyzy
@ 2021-07-14 22:51 ` Andrew Morton
  4 siblings, 0 replies; 18+ messages in thread
From: Andrew Morton @ 2021-07-14 22:51 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, linux-kernel, linux-kselftest, linux-mm, tyhicks, pasha.tatashin

On Wed, 14 Jul 2021 14:56:01 +0600 Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com> wrote:

> Introduce selftests to validate the functionality of KSM.

ooh, nice, thanks!  Now I'm wondering why I didn't ask for this when
KSM was initially merged...


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 1/4] selftests: vm: add KSM merge test
  2021-07-14  8:56 ` [PATCH v2 1/4] selftests: vm: add KSM merge test Zhansaya Bagdauletkyzy
@ 2021-08-04  1:54     ` Pavel Tatashin
  2021-08-16 13:57   ` Tyler Hicks
  1 sibling, 0 replies; 18+ messages in thread
From: Pavel Tatashin @ 2021-08-04  1:54 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, Andrew Morton, LKML, linux-kselftest, linux-mm, Tyler Hicks

On Wed, Jul 14, 2021 at 4:56 AM Zhansaya Bagdauletkyzy
<zhansayabagdaulet@gmail.com> wrote:
>
> Add check_ksm_merge() function to check the basic merging feature of
> KSM. First, some number of identical pages are allocated and the
> MADV_MERGEABLE advice is given to merge these pages. Then, pages_shared
> and pages_sharing values are compared with the expected numbers using
> assert_ksm_pages_count() function. The number of pages can be changed
> using -p option.
>
> Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>

No comments, because I have already reviewed this series for Zhansaya
as part of the Outreachy internship, so:

Reviewed-by: Pavel Tatashin <pasha.tatashin@soleen.com>

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 1/4] selftests: vm: add KSM merge test
@ 2021-08-04  1:54     ` Pavel Tatashin
  0 siblings, 0 replies; 18+ messages in thread
From: Pavel Tatashin @ 2021-08-04  1:54 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, Andrew Morton, LKML, linux-kselftest, linux-mm, Tyler Hicks

On Wed, Jul 14, 2021 at 4:56 AM Zhansaya Bagdauletkyzy
<zhansayabagdaulet@gmail.com> wrote:
>
> Add check_ksm_merge() function to check the basic merging feature of
> KSM. First, some number of identical pages are allocated and the
> MADV_MERGEABLE advice is given to merge these pages. Then, pages_shared
> and pages_sharing values are compared with the expected numbers using
> assert_ksm_pages_count() function. The number of pages can be changed
> using -p option.
>
> Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>

No comments, because I have already reviewed this series for Zhansaya
as part of the Outreachy internship, so:

Reviewed-by: Pavel Tatashin <pasha.tatashin@soleen.com>


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 2/4] selftests: vm: add KSM unmerge test
  2021-07-14  8:56 ` [PATCH v2 2/4] selftests: vm: add KSM unmerge test Zhansaya Bagdauletkyzy
@ 2021-08-04  1:58     ` Pavel Tatashin
  2021-08-16 13:57   ` Tyler Hicks
  1 sibling, 0 replies; 18+ messages in thread
From: Pavel Tatashin @ 2021-08-04  1:58 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, Andrew Morton, LKML, linux-kselftest, linux-mm, Tyler Hicks

On Wed, Jul 14, 2021 at 4:56 AM Zhansaya Bagdauletkyzy
<zhansayabagdaulet@gmail.com> wrote:
>
> Add check_ksm_unmerge() function to verify that KSM is properly
> unmerging shared pages. For this, two duplicate pages are merged first
> and then their contents are modified. Since they are not identical
> anymore, the pages must be unmerged and the number of merged pages has
> to be 0. The test is run as follows: ./ksm_tests -U
>
> Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>

Reviewed-by: Pavel Tatashin <pasha.tatashin@soleen.com>

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 2/4] selftests: vm: add KSM unmerge test
@ 2021-08-04  1:58     ` Pavel Tatashin
  0 siblings, 0 replies; 18+ messages in thread
From: Pavel Tatashin @ 2021-08-04  1:58 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, Andrew Morton, LKML, linux-kselftest, linux-mm, Tyler Hicks

On Wed, Jul 14, 2021 at 4:56 AM Zhansaya Bagdauletkyzy
<zhansayabagdaulet@gmail.com> wrote:
>
> Add check_ksm_unmerge() function to verify that KSM is properly
> unmerging shared pages. For this, two duplicate pages are merged first
> and then their contents are modified. Since they are not identical
> anymore, the pages must be unmerged and the number of merged pages has
> to be 0. The test is run as follows: ./ksm_tests -U
>
> Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>

Reviewed-by: Pavel Tatashin <pasha.tatashin@soleen.com>


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 3/4] selftests: vm: add KSM zero page merging test
  2021-07-14  8:56 ` [PATCH v2 3/4] selftests: vm: add KSM zero page merging test Zhansaya Bagdauletkyzy
@ 2021-08-04  1:59     ` Pavel Tatashin
  2021-08-16 14:00   ` Tyler Hicks
  1 sibling, 0 replies; 18+ messages in thread
From: Pavel Tatashin @ 2021-08-04  1:59 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, Andrew Morton, LKML, linux-kselftest, linux-mm, Tyler Hicks

On Wed, Jul 14, 2021 at 4:56 AM Zhansaya Bagdauletkyzy
<zhansayabagdaulet@gmail.com> wrote:
>
> Add check_ksm_zero_page_merge() function to test that empty pages are
> being handled properly. For this, several zero pages are allocated and
> merged using madvise. If use_zero_pages is enabled, the pages must be
> shared with the special kernel zero pages; otherwise, they  are merged
> as usual duplicate pages. The test is run as follows: ./ksm_tests -Z
>
> Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>

Reviewed-by: Pavel Tatashin <pasha.tatashin@soleen.com>

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 3/4] selftests: vm: add KSM zero page merging test
@ 2021-08-04  1:59     ` Pavel Tatashin
  0 siblings, 0 replies; 18+ messages in thread
From: Pavel Tatashin @ 2021-08-04  1:59 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, Andrew Morton, LKML, linux-kselftest, linux-mm, Tyler Hicks

On Wed, Jul 14, 2021 at 4:56 AM Zhansaya Bagdauletkyzy
<zhansayabagdaulet@gmail.com> wrote:
>
> Add check_ksm_zero_page_merge() function to test that empty pages are
> being handled properly. For this, several zero pages are allocated and
> merged using madvise. If use_zero_pages is enabled, the pages must be
> shared with the special kernel zero pages; otherwise, they  are merged
> as usual duplicate pages. The test is run as follows: ./ksm_tests -Z
>
> Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>

Reviewed-by: Pavel Tatashin <pasha.tatashin@soleen.com>


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 4/4] selftests: vm: add KSM merging across nodes test
  2021-07-14  8:56 ` [PATCH v2 4/4] selftests: vm: add KSM merging across nodes test Zhansaya Bagdauletkyzy
@ 2021-08-04  2:01     ` Pavel Tatashin
  2021-08-16 14:28   ` Tyler Hicks
  1 sibling, 0 replies; 18+ messages in thread
From: Pavel Tatashin @ 2021-08-04  2:01 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, Andrew Morton, LKML, linux-kselftest, linux-mm, Tyler Hicks

On Wed, Jul 14, 2021 at 4:56 AM Zhansaya Bagdauletkyzy
<zhansayabagdaulet@gmail.com> wrote:
>
> Add check_ksm_numa_merge() function  to test that pages in different NUMA
> nodes are being handled properly. First, two duplicate pages are allocated
> in two separate NUMA nodes using the libnuma library. Since there is one
> unique page in each node, with merge_across_nodes = 0, there won't be any
> shared pages. If merge_across_nodes is set to 1, the pages will be
> treated as usual duplicate pages and will be merged. If NUMA config is
> not enabled or the number of NUMA nodes is less than two, then the test
> is skipped. The test is run as follows: ./ksm_tests -N
>
> Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>

Reviewed-by: Pavel Tatashin <pasha.tatashin@soleen.com>

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 4/4] selftests: vm: add KSM merging across nodes test
@ 2021-08-04  2:01     ` Pavel Tatashin
  0 siblings, 0 replies; 18+ messages in thread
From: Pavel Tatashin @ 2021-08-04  2:01 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, Andrew Morton, LKML, linux-kselftest, linux-mm, Tyler Hicks

On Wed, Jul 14, 2021 at 4:56 AM Zhansaya Bagdauletkyzy
<zhansayabagdaulet@gmail.com> wrote:
>
> Add check_ksm_numa_merge() function  to test that pages in different NUMA
> nodes are being handled properly. First, two duplicate pages are allocated
> in two separate NUMA nodes using the libnuma library. Since there is one
> unique page in each node, with merge_across_nodes = 0, there won't be any
> shared pages. If merge_across_nodes is set to 1, the pages will be
> treated as usual duplicate pages and will be merged. If NUMA config is
> not enabled or the number of NUMA nodes is less than two, then the test
> is skipped. The test is run as follows: ./ksm_tests -N
>
> Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>

Reviewed-by: Pavel Tatashin <pasha.tatashin@soleen.com>


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 1/4] selftests: vm: add KSM merge test
  2021-07-14  8:56 ` [PATCH v2 1/4] selftests: vm: add KSM merge test Zhansaya Bagdauletkyzy
  2021-08-04  1:54     ` Pavel Tatashin
@ 2021-08-16 13:57   ` Tyler Hicks
  1 sibling, 0 replies; 18+ messages in thread
From: Tyler Hicks @ 2021-08-16 13:57 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, akpm, linux-kernel, linux-kselftest, linux-mm, pasha.tatashin

On 2021-07-14 14:56:06, Zhansaya Bagdauletkyzy wrote:
> Add check_ksm_merge() function to check the basic merging feature of
> KSM. First, some number of identical pages are allocated and the
> MADV_MERGEABLE advice is given to merge these pages. Then, pages_shared
> and pages_sharing values are compared with the expected numbers using
> assert_ksm_pages_count() function. The number of pages can be changed
> using -p option.
> 
> Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>

Reviewed-by: Tyler Hicks <tyhicks@linux.microsoft.com>

As Pavel mentioned, we went through some private iterations on these
patches with Zhansaya so my review comments have already been addressed.

Tyler

> ---
>  tools/testing/selftests/vm/.gitignore     |   1 +
>  tools/testing/selftests/vm/Makefile       |   1 +
>  tools/testing/selftests/vm/ksm_tests.c    | 306 ++++++++++++++++++++++
>  tools/testing/selftests/vm/run_vmtests.sh |  16 ++
>  4 files changed, 324 insertions(+)
>  create mode 100644 tools/testing/selftests/vm/ksm_tests.c
> 
> diff --git a/tools/testing/selftests/vm/.gitignore b/tools/testing/selftests/vm/.gitignore
> index f0fd80ef17df..b02eac613fdd 100644
> --- a/tools/testing/selftests/vm/.gitignore
> +++ b/tools/testing/selftests/vm/.gitignore
> @@ -27,3 +27,4 @@ hmm-tests
>  memfd_secret
>  local_config.*
>  split_huge_page_test
> +ksm_tests
> diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile
> index 521243770f26..e6f22a801b71 100644
> --- a/tools/testing/selftests/vm/Makefile
> +++ b/tools/testing/selftests/vm/Makefile
> @@ -45,6 +45,7 @@ TEST_GEN_FILES += thuge-gen
>  TEST_GEN_FILES += transhuge-stress
>  TEST_GEN_FILES += userfaultfd
>  TEST_GEN_FILES += split_huge_page_test
> +TEST_GEN_FILES += ksm_tests
>  
>  ifeq ($(MACHINE),x86_64)
>  CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh $(CC) ../x86/trivial_32bit_program.c -m32)
> diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c
> new file mode 100644
> index 000000000000..d74d5aa34b16
> --- /dev/null
> +++ b/tools/testing/selftests/vm/ksm_tests.c
> @@ -0,0 +1,306 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <sys/mman.h>
> +#include <stdbool.h>
> +#include <time.h>
> +#include <string.h>
> +
> +#include "../kselftest.h"
> +
> +#define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"
> +#define KSM_FP(s) (KSM_SYSFS_PATH s)
> +#define KSM_SCAN_LIMIT_SEC_DEFAULT 120
> +#define KSM_PAGE_COUNT_DEFAULT 10l
> +#define KSM_PROT_STR_DEFAULT "rw"
> +
> +struct ksm_sysfs {
> +	unsigned long max_page_sharing;
> +	unsigned long merge_across_nodes;
> +	unsigned long pages_to_scan;
> +	unsigned long run;
> +	unsigned long sleep_millisecs;
> +	unsigned long stable_node_chains_prune_millisecs;
> +	unsigned long use_zero_pages;
> +};
> +
> +static int ksm_write_sysfs(const char *file_path, unsigned long val)
> +{
> +	FILE *f = fopen(file_path, "w");
> +
> +	if (!f) {
> +		fprintf(stderr, "f %s\n", file_path);
> +		perror("fopen");
> +		return 1;
> +	}
> +	if (fprintf(f, "%lu", val) < 0) {
> +		perror("fprintf");
> +		return 1;
> +	}
> +	fclose(f);
> +
> +	return 0;
> +}
> +
> +static int ksm_read_sysfs(const char *file_path, unsigned long *val)
> +{
> +	FILE *f = fopen(file_path, "r");
> +
> +	if (!f) {
> +		fprintf(stderr, "f %s\n", file_path);
> +		perror("fopen");
> +		return 1;
> +	}
> +	if (fscanf(f, "%lu", val) != 1) {
> +		perror("fscanf");
> +		return 1;
> +	}
> +	fclose(f);
> +
> +	return 0;
> +}
> +
> +static int str_to_prot(char *prot_str)
> +{
> +	int prot = 0;
> +
> +	if ((strchr(prot_str, 'r')) != NULL)
> +		prot |= PROT_READ;
> +	if ((strchr(prot_str, 'w')) != NULL)
> +		prot |= PROT_WRITE;
> +	if ((strchr(prot_str, 'x')) != NULL)
> +		prot |= PROT_EXEC;
> +
> +	return prot;
> +}
> +
> +static void print_help(void)
> +{
> +	printf("usage: ksm_tests [-h] [-a prot] [-p page_count] [-l timeout]\n");
> +	printf(" -a: specify the access protections of pages.\n"
> +	       "     <prot> must be of the form [rwx].\n"
> +	       "     Default: %s\n", KSM_PROT_STR_DEFAULT);
> +	printf(" -p: specify the number of pages to test.\n"
> +	       "     Default: %ld\n", KSM_PAGE_COUNT_DEFAULT);
> +	printf(" -l: limit the maximum running time (in seconds) for a test.\n"
> +	       "     Default: %d seconds\n", KSM_SCAN_LIMIT_SEC_DEFAULT);
> +
> +	exit(0);
> +}
> +
> +static void  *allocate_memory(void *ptr, int prot, int mapping, char data, size_t map_size)
> +{
> +	void *map_ptr = mmap(ptr, map_size, PROT_WRITE, mapping, -1, 0);
> +
> +	if (!map_ptr) {
> +		perror("mmap");
> +		return NULL;
> +	}
> +	memset(map_ptr, data, map_size);
> +	if (mprotect(map_ptr, map_size, prot)) {
> +		perror("mprotect");
> +		munmap(map_ptr, map_size);
> +		return NULL;
> +	}
> +
> +	return map_ptr;
> +}
> +
> +static int ksm_do_scan(int scan_count, struct timespec start_time, int timeout)
> +{
> +	struct timespec cur_time;
> +	unsigned long cur_scan, init_scan;
> +
> +	if (ksm_read_sysfs(KSM_FP("full_scans"), &init_scan))
> +		return 1;
> +	cur_scan = init_scan;
> +
> +	while (cur_scan < init_scan + scan_count) {
> +		if (ksm_read_sysfs(KSM_FP("full_scans"), &cur_scan))
> +			return 1;
> +		if (clock_gettime(CLOCK_MONOTONIC_RAW, &cur_time)) {
> +			perror("clock_gettime");
> +			return 1;
> +		}
> +		if ((cur_time.tv_sec - start_time.tv_sec) > timeout) {
> +			printf("Scan time limit exceeded\n");
> +			return 1;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +static int ksm_merge_pages(void *addr, size_t size, struct timespec start_time, int timeout)
> +{
> +	if (madvise(addr, size, MADV_MERGEABLE)) {
> +		perror("madvise");
> +		return 1;
> +	}
> +	if (ksm_write_sysfs(KSM_FP("run"), 1))
> +		return 1;
> +
> +	/* Since merging occurs only after 2 scans, make sure to get at least 2 full scans */
> +	if (ksm_do_scan(2, start_time, timeout))
> +		return 1;
> +
> +	return 0;
> +}
> +
> +static bool assert_ksm_pages_count(long dupl_page_count)
> +{
> +	unsigned long max_page_sharing, pages_sharing, pages_shared;
> +
> +	if (ksm_read_sysfs(KSM_FP("pages_shared"), &pages_shared) ||
> +	    ksm_read_sysfs(KSM_FP("pages_sharing"), &pages_sharing) ||
> +	    ksm_read_sysfs(KSM_FP("max_page_sharing"), &max_page_sharing))
> +		return false;
> +
> +	/*
> +	 * Since there must be at least 2 pages for merging and 1 page can be
> +	 * shared with the limited number of pages (max_page_sharing), sometimes
> +	 * there are 'leftover' pages that cannot be merged. For example, if there
> +	 * are 11 pages and max_page_sharing = 10, then only 10 pages will be
> +	 * merged and the 11th page won't be affected. As a result, when the number
> +	 * of duplicate pages is divided by max_page_sharing and the remainder is 1,
> +	 * pages_shared and pages_sharing values will be equal between dupl_page_count
> +	 * and dupl_page_count - 1.
> +	 */
> +	if (dupl_page_count % max_page_sharing == 1 || dupl_page_count % max_page_sharing == 0) {
> +		if (pages_shared == dupl_page_count / max_page_sharing &&
> +		    pages_sharing == pages_shared * (max_page_sharing - 1))
> +			return true;
> +	} else {
> +		if (pages_shared == (dupl_page_count / max_page_sharing + 1) &&
> +		    pages_sharing == dupl_page_count - pages_shared)
> +			return true;
> +	}
> +
> +	return false;
> +}
> +
> +static int ksm_save_def(struct ksm_sysfs *ksm_sysfs)
> +{
> +	if (ksm_read_sysfs(KSM_FP("max_page_sharing"), &ksm_sysfs->max_page_sharing) ||
> +	    ksm_read_sysfs(KSM_FP("merge_across_nodes"), &ksm_sysfs->merge_across_nodes) ||
> +	    ksm_read_sysfs(KSM_FP("sleep_millisecs"), &ksm_sysfs->sleep_millisecs) ||
> +	    ksm_read_sysfs(KSM_FP("pages_to_scan"), &ksm_sysfs->pages_to_scan) ||
> +	    ksm_read_sysfs(KSM_FP("run"), &ksm_sysfs->run) ||
> +	    ksm_read_sysfs(KSM_FP("stable_node_chains_prune_millisecs"),
> +			   &ksm_sysfs->stable_node_chains_prune_millisecs) ||
> +	    ksm_read_sysfs(KSM_FP("use_zero_pages"), &ksm_sysfs->use_zero_pages))
> +		return 1;
> +
> +	return 0;
> +}
> +
> +static int ksm_restore(struct ksm_sysfs *ksm_sysfs)
> +{
> +	if (ksm_write_sysfs(KSM_FP("max_page_sharing"), ksm_sysfs->max_page_sharing) ||
> +	    ksm_write_sysfs(KSM_FP("merge_across_nodes"), ksm_sysfs->merge_across_nodes) ||
> +	    ksm_write_sysfs(KSM_FP("pages_to_scan"), ksm_sysfs->pages_to_scan) ||
> +	    ksm_write_sysfs(KSM_FP("run"), ksm_sysfs->run) ||
> +	    ksm_write_sysfs(KSM_FP("sleep_millisecs"), ksm_sysfs->sleep_millisecs) ||
> +	    ksm_write_sysfs(KSM_FP("stable_node_chains_prune_millisecs"),
> +			    ksm_sysfs->stable_node_chains_prune_millisecs) ||
> +	    ksm_write_sysfs(KSM_FP("use_zero_pages"), ksm_sysfs->use_zero_pages))
> +		return 1;
> +
> +	return 0;
> +}
> +
> +static int check_ksm_merge(int mapping, int prot, long page_count, int timeout, size_t page_size)
> +{
> +	void *map_ptr;
> +	struct timespec start_time;
> +
> +	if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
> +		perror("clock_gettime");
> +		return KSFT_FAIL;
> +	}
> +
> +	/* fill pages with the same data and merge them */
> +	map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
> +	if (!map_ptr)
> +		return KSFT_FAIL;
> +
> +	if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
> +		goto err_out;
> +
> +	/* verify that the right number of pages are merged */
> +	if (assert_ksm_pages_count(page_count)) {
> +		printf("OK\n");
> +		munmap(map_ptr, page_size * page_count);
> +		return KSFT_PASS;
> +	}
> +
> +err_out:
> +	printf("Not OK\n");
> +	munmap(map_ptr, page_size * page_count);
> +	return KSFT_FAIL;
> +}
> +
> +int main(int argc, char *argv[])
> +{
> +	int ret, opt;
> +	int prot = 0;
> +	int ksm_scan_limit_sec = KSM_SCAN_LIMIT_SEC_DEFAULT;
> +	long page_count = KSM_PAGE_COUNT_DEFAULT;
> +	size_t page_size = sysconf(_SC_PAGESIZE);
> +	struct ksm_sysfs ksm_sysfs_old;
> +
> +	while ((opt = getopt(argc, argv, "ha:p:l:")) != -1) {
> +		switch (opt) {
> +		case 'a':
> +			prot = str_to_prot(optarg);
> +			break;
> +		case 'p':
> +			page_count = atol(optarg);
> +			if (page_count <= 0) {
> +				printf("The number of pages must be greater than 0\n");
> +				return KSFT_FAIL;
> +			}
> +			break;
> +		case 'l':
> +			ksm_scan_limit_sec = atoi(optarg);
> +			if (ksm_scan_limit_sec <= 0) {
> +				printf("Timeout value must be greater than 0\n");
> +				return KSFT_FAIL;
> +			}
> +			break;
> +		case 'h':
> +			print_help();
> +			break;
> +		default:
> +			return KSFT_FAIL;
> +		}
> +	}
> +
> +	if (prot == 0)
> +		prot = str_to_prot(KSM_PROT_STR_DEFAULT);
> +
> +	if (access(KSM_SYSFS_PATH, F_OK)) {
> +		printf("Config KSM not enabled\n");
> +		return KSFT_SKIP;
> +	}
> +
> +	if (ksm_save_def(&ksm_sysfs_old)) {
> +		printf("Cannot save default tunables\n");
> +		return KSFT_FAIL;
> +	}
> +
> +	if (ksm_write_sysfs(KSM_FP("run"), 2) ||
> +	    ksm_write_sysfs(KSM_FP("sleep_millisecs"), 0) ||
> +	    ksm_write_sysfs(KSM_FP("merge_across_nodes"), 1) ||
> +	    ksm_write_sysfs(KSM_FP("pages_to_scan"), page_count))
> +		return KSFT_FAIL;
> +
> +	ret = check_ksm_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count, ksm_scan_limit_sec,
> +			      page_size);
> +
> +	if (ksm_restore(&ksm_sysfs_old)) {
> +		printf("Cannot restore default tunables\n");
> +		return KSFT_FAIL;
> +	}
> +
> +	return ret;
> +}
> diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh
> index d09a6b71f1e9..97b6f712134d 100755
> --- a/tools/testing/selftests/vm/run_vmtests.sh
> +++ b/tools/testing/selftests/vm/run_vmtests.sh
> @@ -377,6 +377,22 @@ else
>  	exitcode=1
>  fi
>  
> +echo "-------------------------------------------------------"
> +echo "running KSM MADV_MERGEABLE test with 10 identical pages"
> +echo "-------------------------------------------------------"
> +./ksm_tests -p 10
> +ret_val=$?
> +
> +if [ $ret_val -eq 0 ]; then
> +	echo "[PASS]"
> +elif [ $ret_val -eq $ksft_skip ]; then
> +	 echo "[SKIP]"
> +	 exitcode=$ksft_skip
> +else
> +	echo "[FAIL]"
> +	exitcode=1
> +fi
> +
>  exit $exitcode
>  
>  exit $exitcode
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 2/4] selftests: vm: add KSM unmerge test
  2021-07-14  8:56 ` [PATCH v2 2/4] selftests: vm: add KSM unmerge test Zhansaya Bagdauletkyzy
  2021-08-04  1:58     ` Pavel Tatashin
@ 2021-08-16 13:57   ` Tyler Hicks
  1 sibling, 0 replies; 18+ messages in thread
From: Tyler Hicks @ 2021-08-16 13:57 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, akpm, linux-kernel, linux-kselftest, linux-mm, pasha.tatashin

On 2021-07-14 14:56:09, Zhansaya Bagdauletkyzy wrote:
> Add check_ksm_unmerge() function to verify that KSM is properly
> unmerging shared pages. For this, two duplicate pages are merged first
> and then their contents are modified. Since they are not identical
> anymore, the pages must be unmerged and the number of merged pages has
> to be 0. The test is run as follows: ./ksm_tests -U
> 
> Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>

Reviewed-by: Tyler Hicks <tyhicks@linux.microsoft.com>

Tyler

> ---
>  tools/testing/selftests/vm/ksm_tests.c    | 72 +++++++++++++++++++++--
>  tools/testing/selftests/vm/run_vmtests.sh | 18 +++++-
>  2 files changed, 85 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c
> index d74d5aa34b16..80302bb8f64c 100644
> --- a/tools/testing/selftests/vm/ksm_tests.c
> +++ b/tools/testing/selftests/vm/ksm_tests.c
> @@ -23,6 +23,11 @@ struct ksm_sysfs {
>  	unsigned long use_zero_pages;
>  };
>  
> +enum ksm_test_name {
> +	CHECK_KSM_MERGE,
> +	CHECK_KSM_UNMERGE
> +};
> +
>  static int ksm_write_sysfs(const char *file_path, unsigned long val)
>  {
>  	FILE *f = fopen(file_path, "w");
> @@ -75,7 +80,12 @@ static int str_to_prot(char *prot_str)
>  
>  static void print_help(void)
>  {
> -	printf("usage: ksm_tests [-h] [-a prot] [-p page_count] [-l timeout]\n");
> +	printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n");
> +
> +	printf("Supported <test type>:\n"
> +	       " -M (page merging)\n"
> +	       " -U (page unmerging)\n\n");
> +
>  	printf(" -a: specify the access protections of pages.\n"
>  	       "     <prot> must be of the form [rwx].\n"
>  	       "     Default: %s\n", KSM_PROT_STR_DEFAULT);
> @@ -239,6 +249,46 @@ static int check_ksm_merge(int mapping, int prot, long page_count, int timeout,
>  	return KSFT_FAIL;
>  }
>  
> +static int check_ksm_unmerge(int mapping, int prot, int timeout, size_t page_size)
> +{
> +	void *map_ptr;
> +	struct timespec start_time;
> +	int page_count = 2;
> +
> +	if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
> +		perror("clock_gettime");
> +		return KSFT_FAIL;
> +	}
> +
> +	/* fill pages with the same data and merge them */
> +	map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
> +	if (!map_ptr)
> +		return KSFT_FAIL;
> +
> +	if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
> +		goto err_out;
> +
> +	/* change 1 byte in each of the 2 pages -- KSM must automatically unmerge them */
> +	memset(map_ptr, '-', 1);
> +	memset(map_ptr + page_size, '+', 1);
> +
> +	/* get at least 1 scan, so KSM can detect that the pages were modified */
> +	if (ksm_do_scan(1, start_time, timeout))
> +		goto err_out;
> +
> +	/* check that unmerging was successful and 0 pages are currently merged */
> +	if (assert_ksm_pages_count(0)) {
> +		printf("OK\n");
> +		munmap(map_ptr, page_size * page_count);
> +		return KSFT_PASS;
> +	}
> +
> +err_out:
> +	printf("Not OK\n");
> +	munmap(map_ptr, page_size * page_count);
> +	return KSFT_FAIL;
> +}
> +
>  int main(int argc, char *argv[])
>  {
>  	int ret, opt;
> @@ -247,8 +297,9 @@ int main(int argc, char *argv[])
>  	long page_count = KSM_PAGE_COUNT_DEFAULT;
>  	size_t page_size = sysconf(_SC_PAGESIZE);
>  	struct ksm_sysfs ksm_sysfs_old;
> +	int test_name = CHECK_KSM_MERGE;
>  
> -	while ((opt = getopt(argc, argv, "ha:p:l:")) != -1) {
> +	while ((opt = getopt(argc, argv, "ha:p:l:MU")) != -1) {
>  		switch (opt) {
>  		case 'a':
>  			prot = str_to_prot(optarg);
> @@ -270,6 +321,11 @@ int main(int argc, char *argv[])
>  		case 'h':
>  			print_help();
>  			break;
> +		case 'M':
> +			break;
> +		case 'U':
> +			test_name = CHECK_KSM_UNMERGE;
> +			break;
>  		default:
>  			return KSFT_FAIL;
>  		}
> @@ -294,8 +350,16 @@ int main(int argc, char *argv[])
>  	    ksm_write_sysfs(KSM_FP("pages_to_scan"), page_count))
>  		return KSFT_FAIL;
>  
> -	ret = check_ksm_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count, ksm_scan_limit_sec,
> -			      page_size);
> +	switch (test_name) {
> +	case CHECK_KSM_MERGE:
> +		ret = check_ksm_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count,
> +				      ksm_scan_limit_sec, page_size);
> +		break;
> +	case CHECK_KSM_UNMERGE:
> +		ret = check_ksm_unmerge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
> +					page_size);
> +		break;
> +	}
>  
>  	if (ksm_restore(&ksm_sysfs_old)) {
>  		printf("Cannot restore default tunables\n");
> diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh
> index 97b6f712134d..3a23c6b47da2 100755
> --- a/tools/testing/selftests/vm/run_vmtests.sh
> +++ b/tools/testing/selftests/vm/run_vmtests.sh
> @@ -380,7 +380,23 @@ fi
>  echo "-------------------------------------------------------"
>  echo "running KSM MADV_MERGEABLE test with 10 identical pages"
>  echo "-------------------------------------------------------"
> -./ksm_tests -p 10
> +./ksm_tests -M -p 10
> +ret_val=$?
> +
> +if [ $ret_val -eq 0 ]; then
> +	echo "[PASS]"
> +elif [ $ret_val -eq $ksft_skip ]; then
> +	 echo "[SKIP]"
> +	 exitcode=$ksft_skip
> +else
> +	echo "[FAIL]"
> +	exitcode=1
> +fi
> +
> +echo "------------------------"
> +echo "running KSM unmerge test"
> +echo "------------------------"
> +./ksm_tests -U
>  ret_val=$?
>  
>  if [ $ret_val -eq 0 ]; then
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 3/4] selftests: vm: add KSM zero page merging test
  2021-07-14  8:56 ` [PATCH v2 3/4] selftests: vm: add KSM zero page merging test Zhansaya Bagdauletkyzy
  2021-08-04  1:59     ` Pavel Tatashin
@ 2021-08-16 14:00   ` Tyler Hicks
  1 sibling, 0 replies; 18+ messages in thread
From: Tyler Hicks @ 2021-08-16 14:00 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, akpm, linux-kernel, linux-kselftest, linux-mm, pasha.tatashin

On 2021-07-14 14:56:14, Zhansaya Bagdauletkyzy wrote:
> Add check_ksm_zero_page_merge() function to test that empty pages are
> being handled properly. For this, several zero pages are allocated and
> merged using madvise. If use_zero_pages is enabled, the pages must be
> shared with the special kernel zero pages; otherwise, they  are merged
> as usual duplicate pages. The test is run as follows: ./ksm_tests -Z
> 
> Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>

Reviewed-by: Tyler Hicks <tyhicks@linux.microsoft.com>

Tyler

> ---
>  tools/testing/selftests/vm/ksm_tests.c    | 70 ++++++++++++++++++++++-
>  tools/testing/selftests/vm/run_vmtests.sh | 32 +++++++++++
>  2 files changed, 99 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c
> index 80302bb8f64c..5843526471e1 100644
> --- a/tools/testing/selftests/vm/ksm_tests.c
> +++ b/tools/testing/selftests/vm/ksm_tests.c
> @@ -12,6 +12,7 @@
>  #define KSM_SCAN_LIMIT_SEC_DEFAULT 120
>  #define KSM_PAGE_COUNT_DEFAULT 10l
>  #define KSM_PROT_STR_DEFAULT "rw"
> +#define KSM_USE_ZERO_PAGES_DEFAULT false
>  
>  struct ksm_sysfs {
>  	unsigned long max_page_sharing;
> @@ -25,7 +26,8 @@ struct ksm_sysfs {
>  
>  enum ksm_test_name {
>  	CHECK_KSM_MERGE,
> -	CHECK_KSM_UNMERGE
> +	CHECK_KSM_UNMERGE,
> +	CHECK_KSM_ZERO_PAGE_MERGE
>  };
>  
>  static int ksm_write_sysfs(const char *file_path, unsigned long val)
> @@ -80,10 +82,12 @@ static int str_to_prot(char *prot_str)
>  
>  static void print_help(void)
>  {
> -	printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n");
> +	printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n"
> +	       "[-z use_zero_pages]\n");
>  
>  	printf("Supported <test type>:\n"
>  	       " -M (page merging)\n"
> +	       " -Z (zero pages merging)\n"
>  	       " -U (page unmerging)\n\n");
>  
>  	printf(" -a: specify the access protections of pages.\n"
> @@ -93,6 +97,8 @@ static void print_help(void)
>  	       "     Default: %ld\n", KSM_PAGE_COUNT_DEFAULT);
>  	printf(" -l: limit the maximum running time (in seconds) for a test.\n"
>  	       "     Default: %d seconds\n", KSM_SCAN_LIMIT_SEC_DEFAULT);
> +	printf(" -z: change use_zero_pages tunable\n"
> +	       "     Default: %d\n", KSM_USE_ZERO_PAGES_DEFAULT);
>  
>  	exit(0);
>  }
> @@ -289,6 +295,50 @@ static int check_ksm_unmerge(int mapping, int prot, int timeout, size_t page_siz
>  	return KSFT_FAIL;
>  }
>  
> +static int check_ksm_zero_page_merge(int mapping, int prot, long page_count, int timeout,
> +				     bool use_zero_pages, size_t page_size)
> +{
> +	void *map_ptr;
> +	struct timespec start_time;
> +
> +	if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
> +		perror("clock_gettime");
> +		return KSFT_FAIL;
> +	}
> +
> +	if (ksm_write_sysfs(KSM_FP("use_zero_pages"), use_zero_pages))
> +		return KSFT_FAIL;
> +
> +	/* fill pages with zero and try to merge them */
> +	map_ptr = allocate_memory(NULL, prot, mapping, 0, page_size * page_count);
> +	if (!map_ptr)
> +		return KSFT_FAIL;
> +
> +	if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
> +		goto err_out;
> +
> +       /*
> +	* verify that the right number of pages are merged:
> +	* 1) if use_zero_pages is set to 1, empty pages are merged
> +	*    with the kernel zero page instead of with each other;
> +	* 2) if use_zero_pages is set to 0, empty pages are not treated specially
> +	*    and merged as usual.
> +	*/
> +	if (use_zero_pages && !assert_ksm_pages_count(0))
> +		goto err_out;
> +	else if (!use_zero_pages && !assert_ksm_pages_count(page_count))
> +		goto err_out;
> +
> +	printf("OK\n");
> +	munmap(map_ptr, page_size * page_count);
> +	return KSFT_PASS;
> +
> +err_out:
> +	printf("Not OK\n");
> +	munmap(map_ptr, page_size * page_count);
> +	return KSFT_FAIL;
> +}
> +
>  int main(int argc, char *argv[])
>  {
>  	int ret, opt;
> @@ -298,8 +348,9 @@ int main(int argc, char *argv[])
>  	size_t page_size = sysconf(_SC_PAGESIZE);
>  	struct ksm_sysfs ksm_sysfs_old;
>  	int test_name = CHECK_KSM_MERGE;
> +	bool use_zero_pages = KSM_USE_ZERO_PAGES_DEFAULT;
>  
> -	while ((opt = getopt(argc, argv, "ha:p:l:MU")) != -1) {
> +	while ((opt = getopt(argc, argv, "ha:p:l:z:MUZ")) != -1) {
>  		switch (opt) {
>  		case 'a':
>  			prot = str_to_prot(optarg);
> @@ -321,11 +372,20 @@ int main(int argc, char *argv[])
>  		case 'h':
>  			print_help();
>  			break;
> +		case 'z':
> +			if (strcmp(optarg, "0") == 0)
> +				use_zero_pages = 0;
> +			else
> +				use_zero_pages = 1;
> +			break;
>  		case 'M':
>  			break;
>  		case 'U':
>  			test_name = CHECK_KSM_UNMERGE;
>  			break;
> +		case 'Z':
> +			test_name = CHECK_KSM_ZERO_PAGE_MERGE;
> +			break;
>  		default:
>  			return KSFT_FAIL;
>  		}
> @@ -359,6 +419,10 @@ int main(int argc, char *argv[])
>  		ret = check_ksm_unmerge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
>  					page_size);
>  		break;
> +	case CHECK_KSM_ZERO_PAGE_MERGE:
> +		ret = check_ksm_zero_page_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count,
> +						ksm_scan_limit_sec, use_zero_pages, page_size);
> +		break;
>  	}
>  
>  	if (ksm_restore(&ksm_sysfs_old)) {
> diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh
> index 3a23c6b47da2..9b4e444fc4ed 100755
> --- a/tools/testing/selftests/vm/run_vmtests.sh
> +++ b/tools/testing/selftests/vm/run_vmtests.sh
> @@ -409,6 +409,38 @@ else
>  	exitcode=1
>  fi
>  
> +echo "----------------------------------------------------------"
> +echo "running KSM test with 10 zero pages and use_zero_pages = 0"
> +echo "----------------------------------------------------------"
> +./ksm_tests -Z -p 10 -z 0
> +ret_val=$?
> +
> +if [ $ret_val -eq 0 ]; then
> +	echo "[PASS]"
> +elif [ $ret_val -eq $ksft_skip ]; then
> +	 echo "[SKIP]"
> +	 exitcode=$ksft_skip
> +else
> +	echo "[FAIL]"
> +	exitcode=1
> +fi
> +
> +echo "----------------------------------------------------------"
> +echo "running KSM test with 10 zero pages and use_zero_pages = 1"
> +echo "----------------------------------------------------------"
> +./ksm_tests -Z -p 10 -z 1
> +ret_val=$?
> +
> +if [ $ret_val -eq 0 ]; then
> +	echo "[PASS]"
> +elif [ $ret_val -eq $ksft_skip ]; then
> +	 echo "[SKIP]"
> +	 exitcode=$ksft_skip
> +else
> +	echo "[FAIL]"
> +	exitcode=1
> +fi
> +
>  exit $exitcode
>  
>  exit $exitcode
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 4/4] selftests: vm: add KSM merging across nodes test
  2021-07-14  8:56 ` [PATCH v2 4/4] selftests: vm: add KSM merging across nodes test Zhansaya Bagdauletkyzy
  2021-08-04  2:01     ` Pavel Tatashin
@ 2021-08-16 14:28   ` Tyler Hicks
  1 sibling, 0 replies; 18+ messages in thread
From: Tyler Hicks @ 2021-08-16 14:28 UTC (permalink / raw)
  To: Zhansaya Bagdauletkyzy
  Cc: shuah, akpm, linux-kernel, linux-kselftest, linux-mm, pasha.tatashin

On 2021-07-14 14:56:18, Zhansaya Bagdauletkyzy wrote:
> Add check_ksm_numa_merge() function  to test that pages in different NUMA
> nodes are being handled properly. First, two duplicate pages are allocated
> in two separate NUMA nodes using the libnuma library. Since there is one
> unique page in each node, with merge_across_nodes = 0, there won't be any
> shared pages. If merge_across_nodes is set to 1, the pages will be
> treated as usual duplicate pages and will be merged. If NUMA config is
> not enabled or the number of NUMA nodes is less than two, then the test
> is skipped. The test is run as follows: ./ksm_tests -N
> 
> Signed-off-by: Zhansaya Bagdauletkyzy <zhansayabagdaulet@gmail.com>

Reviewed-by: Tyler Hicks <tyhicks@linux.microsoft.com>

Tyler

> ---
>  tools/testing/selftests/vm/Makefile       |  2 +
>  tools/testing/selftests/vm/ksm_tests.c    | 88 ++++++++++++++++++++++-
>  tools/testing/selftests/vm/run_vmtests.sh | 32 +++++++++
>  3 files changed, 119 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile
> index e6f22a801b71..d9605bd10f2d 100644
> --- a/tools/testing/selftests/vm/Makefile
> +++ b/tools/testing/selftests/vm/Makefile
> @@ -146,6 +146,8 @@ $(OUTPUT)/hmm-tests: local_config.h
>  # HMM_EXTRA_LIBS may get set in local_config.mk, or it may be left empty.
>  $(OUTPUT)/hmm-tests: LDLIBS += $(HMM_EXTRA_LIBS)
>  
> +$(OUTPUT)/ksm_tests: LDLIBS += -lnuma
> +
>  local_config.mk local_config.h: check_config.sh
>  	/bin/sh ./check_config.sh $(CC)
>  
> diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c
> index 5843526471e1..cdeb4a028538 100644
> --- a/tools/testing/selftests/vm/ksm_tests.c
> +++ b/tools/testing/selftests/vm/ksm_tests.c
> @@ -4,6 +4,7 @@
>  #include <stdbool.h>
>  #include <time.h>
>  #include <string.h>
> +#include <numa.h>
>  
>  #include "../kselftest.h"
>  
> @@ -13,6 +14,7 @@
>  #define KSM_PAGE_COUNT_DEFAULT 10l
>  #define KSM_PROT_STR_DEFAULT "rw"
>  #define KSM_USE_ZERO_PAGES_DEFAULT false
> +#define KSM_MERGE_ACROSS_NODES_DEFAULT true
>  
>  struct ksm_sysfs {
>  	unsigned long max_page_sharing;
> @@ -27,7 +29,8 @@ struct ksm_sysfs {
>  enum ksm_test_name {
>  	CHECK_KSM_MERGE,
>  	CHECK_KSM_UNMERGE,
> -	CHECK_KSM_ZERO_PAGE_MERGE
> +	CHECK_KSM_ZERO_PAGE_MERGE,
> +	CHECK_KSM_NUMA_MERGE
>  };
>  
>  static int ksm_write_sysfs(const char *file_path, unsigned long val)
> @@ -83,11 +86,12 @@ static int str_to_prot(char *prot_str)
>  static void print_help(void)
>  {
>  	printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n"
> -	       "[-z use_zero_pages]\n");
> +	       "[-z use_zero_pages] [-m merge_across_nodes]\n");
>  
>  	printf("Supported <test type>:\n"
>  	       " -M (page merging)\n"
>  	       " -Z (zero pages merging)\n"
> +	       " -N (merging of pages in different NUMA nodes)\n"
>  	       " -U (page unmerging)\n\n");
>  
>  	printf(" -a: specify the access protections of pages.\n"
> @@ -99,6 +103,8 @@ static void print_help(void)
>  	       "     Default: %d seconds\n", KSM_SCAN_LIMIT_SEC_DEFAULT);
>  	printf(" -z: change use_zero_pages tunable\n"
>  	       "     Default: %d\n", KSM_USE_ZERO_PAGES_DEFAULT);
> +	printf(" -m: change merge_across_nodes tunable\n"
> +	       "     Default: %d\n", KSM_MERGE_ACROSS_NODES_DEFAULT);
>  
>  	exit(0);
>  }
> @@ -339,6 +345,68 @@ static int check_ksm_zero_page_merge(int mapping, int prot, long page_count, int
>  	return KSFT_FAIL;
>  }
>  
> +static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_across_nodes,
> +				size_t page_size)
> +{
> +	void *numa1_map_ptr, *numa2_map_ptr;
> +	struct timespec start_time;
> +	int page_count = 2;
> +
> +	if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
> +		perror("clock_gettime");
> +		return KSFT_FAIL;
> +	}
> +
> +	if (numa_available() < 0) {
> +		perror("NUMA support not enabled");
> +		return KSFT_SKIP;
> +	}
> +	if (numa_max_node() < 1) {
> +		printf("At least 2 NUMA nodes must be available\n");
> +		return KSFT_SKIP;
> +	}
> +	if (ksm_write_sysfs(KSM_FP("merge_across_nodes"), merge_across_nodes))
> +		return KSFT_FAIL;
> +
> +	/* allocate 2 pages in 2 different NUMA nodes and fill them with the same data */
> +	numa1_map_ptr = numa_alloc_onnode(page_size, 0);
> +	numa2_map_ptr = numa_alloc_onnode(page_size, 1);
> +	if (!numa1_map_ptr || !numa2_map_ptr) {
> +		perror("numa_alloc_onnode");
> +		return KSFT_FAIL;
> +	}
> +
> +	memset(numa1_map_ptr, '*', page_size);
> +	memset(numa2_map_ptr, '*', page_size);
> +
> +	/* try to merge the pages */
> +	if (ksm_merge_pages(numa1_map_ptr, page_size, start_time, timeout) ||
> +	    ksm_merge_pages(numa2_map_ptr, page_size, start_time, timeout))
> +		goto err_out;
> +
> +       /*
> +	* verify that the right number of pages are merged:
> +	* 1) if merge_across_nodes was enabled, 2 duplicate pages will be merged;
> +	* 2) if merge_across_nodes = 0, there must be 0 merged pages, since there is
> +	*    only 1 unique page in each node and they can't be shared.
> +	*/
> +	if (merge_across_nodes && !assert_ksm_pages_count(page_count))
> +		goto err_out;
> +	else if (!merge_across_nodes && !assert_ksm_pages_count(0))
> +		goto err_out;
> +
> +	numa_free(numa1_map_ptr, page_size);
> +	numa_free(numa2_map_ptr, page_size);
> +	printf("OK\n");
> +	return KSFT_PASS;
> +
> +err_out:
> +	numa_free(numa1_map_ptr, page_size);
> +	numa_free(numa2_map_ptr, page_size);
> +	printf("Not OK\n");
> +	return KSFT_FAIL;
> +}
> +
>  int main(int argc, char *argv[])
>  {
>  	int ret, opt;
> @@ -349,8 +417,9 @@ int main(int argc, char *argv[])
>  	struct ksm_sysfs ksm_sysfs_old;
>  	int test_name = CHECK_KSM_MERGE;
>  	bool use_zero_pages = KSM_USE_ZERO_PAGES_DEFAULT;
> +	bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT;
>  
> -	while ((opt = getopt(argc, argv, "ha:p:l:z:MUZ")) != -1) {
> +	while ((opt = getopt(argc, argv, "ha:p:l:z:m:MUZN")) != -1) {
>  		switch (opt) {
>  		case 'a':
>  			prot = str_to_prot(optarg);
> @@ -378,6 +447,12 @@ int main(int argc, char *argv[])
>  			else
>  				use_zero_pages = 1;
>  			break;
> +		case 'm':
> +			if (strcmp(optarg, "0") == 0)
> +				merge_across_nodes = 0;
> +			else
> +				merge_across_nodes = 1;
> +			break;
>  		case 'M':
>  			break;
>  		case 'U':
> @@ -386,6 +461,9 @@ int main(int argc, char *argv[])
>  		case 'Z':
>  			test_name = CHECK_KSM_ZERO_PAGE_MERGE;
>  			break;
> +		case 'N':
> +			test_name = CHECK_KSM_NUMA_MERGE;
> +			break;
>  		default:
>  			return KSFT_FAIL;
>  		}
> @@ -423,6 +501,10 @@ int main(int argc, char *argv[])
>  		ret = check_ksm_zero_page_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count,
>  						ksm_scan_limit_sec, use_zero_pages, page_size);
>  		break;
> +	case CHECK_KSM_NUMA_MERGE:
> +		ret = check_ksm_numa_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
> +					   merge_across_nodes, page_size);
> +		break;
>  	}
>  
>  	if (ksm_restore(&ksm_sysfs_old)) {
> diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh
> index 9b4e444fc4ed..45e803af7c77 100755
> --- a/tools/testing/selftests/vm/run_vmtests.sh
> +++ b/tools/testing/selftests/vm/run_vmtests.sh
> @@ -441,6 +441,38 @@ else
>  	exitcode=1
>  fi
>  
> +echo "-------------------------------------------------------------"
> +echo "running KSM test with 2 NUMA nodes and merge_across_nodes = 1"
> +echo "-------------------------------------------------------------"
> +./ksm_tests -N -m 1
> +ret_val=$?
> +
> +if [ $ret_val -eq 0 ]; then
> +	echo "[PASS]"
> +elif [ $ret_val -eq $ksft_skip ]; then
> +	 echo "[SKIP]"
> +	 exitcode=$ksft_skip
> +else
> +	echo "[FAIL]"
> +	exitcode=1
> +fi
> +
> +echo "-------------------------------------------------------------"
> +echo "running KSM test with 2 NUMA nodes and merge_across_nodes = 0"
> +echo "-------------------------------------------------------------"
> +./ksm_tests -N -m 0
> +ret_val=$?
> +
> +if [ $ret_val -eq 0 ]; then
> +	echo "[PASS]"
> +elif [ $ret_val -eq $ksft_skip ]; then
> +	 echo "[SKIP]"
> +	 exitcode=$ksft_skip
> +else
> +	echo "[FAIL]"
> +	exitcode=1
> +fi
> +
>  exit $exitcode
>  
>  exit $exitcode
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2021-08-16 14:28 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-14  8:56 [PATCH v2 0/4] add KSM selftests Zhansaya Bagdauletkyzy
2021-07-14  8:56 ` [PATCH v2 1/4] selftests: vm: add KSM merge test Zhansaya Bagdauletkyzy
2021-08-04  1:54   ` Pavel Tatashin
2021-08-04  1:54     ` Pavel Tatashin
2021-08-16 13:57   ` Tyler Hicks
2021-07-14  8:56 ` [PATCH v2 2/4] selftests: vm: add KSM unmerge test Zhansaya Bagdauletkyzy
2021-08-04  1:58   ` Pavel Tatashin
2021-08-04  1:58     ` Pavel Tatashin
2021-08-16 13:57   ` Tyler Hicks
2021-07-14  8:56 ` [PATCH v2 3/4] selftests: vm: add KSM zero page merging test Zhansaya Bagdauletkyzy
2021-08-04  1:59   ` Pavel Tatashin
2021-08-04  1:59     ` Pavel Tatashin
2021-08-16 14:00   ` Tyler Hicks
2021-07-14  8:56 ` [PATCH v2 4/4] selftests: vm: add KSM merging across nodes test Zhansaya Bagdauletkyzy
2021-08-04  2:01   ` Pavel Tatashin
2021-08-04  2:01     ` Pavel Tatashin
2021-08-16 14:28   ` Tyler Hicks
2021-07-14 22:51 ` [PATCH v2 0/4] add KSM selftests Andrew Morton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.