[v9,5/5] selftest: add a testcase of ksm zero pages

Message ID 20230524055906.20614-1-yang.yang29@zte.com.cn
State New
Headers
Series ksm: support tracking KSM-placed zero-pages |

Commit Message

Yang Yang May 24, 2023, 5:59 a.m. UTC
  From: xu xin <xu.xin16@zte.com.cn>

Add a function test_unmerge_zero_page() to test the functionality on
unsharing and counting ksm-placed zero pages and counting of this patch
series.

test_unmerge_zero_page() actually contains three subjct test objects:
(1) whether the count of ksm zero pages can update correctly after merging;
(2) whether the count of ksm zero pages can update correctly after
    unmerging;
(3) whether ksm zero pages are really unmerged.

Signed-off-by: xu xin <xu.xin16@zte.com.cn>
Cc: Claudio Imbrenda <imbrenda@linux.ibm.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Xuexin Jiang <jiang.xuexin@zte.com.cn>
Reviewed-by: Xiaokai Ran <ran.xiaokai@zte.com.cn>
Reviewed-by: Yang Yang <yang.yang29@zte.com.cn>
---
 tools/testing/selftests/mm/ksm_functional_tests.c | 78 ++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 1 deletion(-)
  

Comments

David Hildenbrand May 24, 2023, 9:11 a.m. UTC | #1
[...]

> +static void test_unmerge_zero_pages(void)
> +{
> +	const unsigned int size = 2 * MiB;
> +	char *map;
> +	unsigned long pages_expected;
> +
> +	ksft_print_msg("[RUN] %s\n", __func__);
> +
> +	if (ksm_zero_pages_fd < 0) {
> +		ksft_test_result_skip("open(\"/sys/kernel/mm/ksm/ksm_zero_pages\") failed\n");
> +		return;
> +	}
> +	if (ksm_use_zero_pages_fd < 0) {
> +		ksft_test_result_skip("open \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n");
> +		return;
> +	}
> +	if (write(ksm_use_zero_pages_fd, "1", 1) != 1) {
> +		ksft_test_result_skip("write \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n");
> +		return;
> +	}

I realize that this test will fail if there is any other process in the system that has KSM
enabled with a suitable page filled with zeroes ... maybe instead of checking the global
KSM zeropages, check the per-mm ones instead? That should be better.

> +
> +	/* Mmap zero pages */

Maybe better:

/* Let KSM deduplicate zero pages. */

> +	map = mmap_and_merge_range(0x00, size, false);
> +	if (map == MAP_FAILED)
> +		return;
> +
> +	/* Check if ksm_zero_pages can be update correctly after merging */

s/can be updated/is updated/

> +	pages_expected = size / pagesize;
> +	if (pages_expected != get_ksm_zero_pages()) {
> +		ksft_test_result_fail("'ksm_zero_pages' updated after merging\n");
> +		goto unmap;
> +	}
> +
> +	/* try to unmerge half of the region */
> +	if (madvise(map, size / 2, MADV_UNMERGEABLE)) {
> +		ksft_test_result_fail("MADV_UNMERGEABLE failed\n");
> +		goto unmap;
> +	}
> +
> +	/* Check if ksm_zero_pages can be update correctly after unmerging */
> +	pages_expected /= 2;
> +	if (pages_expected != get_ksm_zero_pages()) {
> +		ksft_test_result_fail("'ksm_zero_pages' updated after unmerging\n");
> +		goto unmap;
> +	}

You could do something like the following on top (I recall you had some kind of COW
tests previously, this should be a simplified version of it):

diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
index ea060c683c80..160675a4e3d2 100644
--- a/tools/testing/selftests/mm/ksm_functional_tests.c
+++ b/tools/testing/selftests/mm/ksm_functional_tests.c
@@ -182,6 +182,7 @@ static void test_unmerge(void)
  static void test_unmerge_zero_pages(void)
  {
  	const unsigned int size = 2 * MiB;
+	unsigned int offs;
  	char *map;
  	unsigned long pages_expected;
  
@@ -225,8 +226,18 @@ static void test_unmerge_zero_pages(void)
  		goto unmap;
  	}
  
+	/* Trigger unmerging of the other half by writing to the pages. */
+	for (offs = size / 2; offs < size; offs += pagesize)
+		*((unsigned int *)&map[offs]) = offs;
+
+	/* We should have no zeropages remaining. */
+	if (get_ksm_zero_pages()) {
+		ksft_test_result_fail("'ksm_zero_pages' updated after write fault\n");
+		goto unmap;
+	}
+
  	/* Check if ksm zero pages are really unmerged */
-	ksft_test_result(!range_maps_duplicates(map, size / 2),
+	ksft_test_result(!range_maps_duplicates(map, size),
  				"KSM zero pages were unmerged\n");
  unmap:
  	munmap(map, size);
  
xu May 24, 2023, 9:38 a.m. UTC | #2
>> +	if (write(ksm_use_zero_pages_fd, "1", 1) != 1) {
>> +		ksft_test_result_skip("write \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n");
>> +		return;
>> +	}
>
>I realize that this test will fail if there is any other process in the system that has KSM
>Enabled with a suitable page filled with zeroes ... maybe instead of checking the global
>KSM zeropages, check the per-mm ones instead? That should be better

Emmm, yes. It makes sense. I'll do it per-mm in next-version.

>You could do something like the following on top (I recall you had some kind of COW
>tests previously, this should be a simplified version of it):
>

Ok. Thank you.

>diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
>index ea060c683c80..160675a4e3d2 100644
>--- a/tools/testing/selftests/mm/ksm_functional_tests.c
>+++ b/tools/testing/selftests/mm/ksm_functional_tests.c
>@@ -182,6 +182,7 @@ static void test_unmerge(void)
>  static void test_unmerge_zero_pages(void)
>  {
>  	const unsigned int size = 2 * MiB;
>+	unsigned int offs;
>  	char *map;
>  	unsigned long pages_expected;
>  
>@@ -225,8 +226,18 @@ static void test_unmerge_zero_pages(void)
>  		goto unmap;
>  	}
>  
>+	/* Trigger unmerging of the other half by writing to the pages. */
>+	for (offs = size / 2; offs < size; offs += pagesize)
>+		*((unsigned int *)&map[offs]) = offs;
>+
>+	/* We should have no zeropages remaining. */
>+	if (get_ksm_zero_pages()) {
>+		ksft_test_result_fail("'ksm_zero_pages' updated after write fault\n");
>+		goto unmap;
>+	}
>+
>  	/* Check if ksm zero pages are really unmerged */
>-	ksft_test_result(!range_maps_duplicates(map, size / 2),
>+	ksft_test_result(!range_maps_duplicates(map, size),
>  				"KSM zero pages were unmerged\n");
>  unmap:
>  	munmap(map, size);
  

Patch

diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
index 26853badae70..ea060c683c80 100644
--- a/tools/testing/selftests/mm/ksm_functional_tests.c
+++ b/tools/testing/selftests/mm/ksm_functional_tests.c
@@ -29,6 +29,8 @@ 
 
 static int ksm_fd;
 static int ksm_full_scans_fd;
+static int ksm_zero_pages_fd;
+static int ksm_use_zero_pages_fd;
 static int pagemap_fd;
 static size_t pagesize;
 
@@ -59,6 +61,24 @@  static bool range_maps_duplicates(char *addr, unsigned long size)
 	return false;
 }
 
+static long get_ksm_zero_pages(void)
+{
+	char buf[20];
+	ssize_t read_size;
+	unsigned long ksm_zero_pages;
+
+	if (!ksm_zero_pages_fd)
+		return 0;
+
+	read_size = pread(ksm_zero_pages_fd, buf, sizeof(buf) - 1, 0);
+	if (read_size < 0)
+		return -errno;
+	buf[read_size] = 0;
+	ksm_zero_pages = strtol(buf, NULL, 10);
+
+	return ksm_zero_pages;
+}
+
 static long ksm_get_full_scans(void)
 {
 	char buf[10];
@@ -159,6 +179,59 @@  static void test_unmerge(void)
 	munmap(map, size);
 }
 
+static void test_unmerge_zero_pages(void)
+{
+	const unsigned int size = 2 * MiB;
+	char *map;
+	unsigned long pages_expected;
+
+	ksft_print_msg("[RUN] %s\n", __func__);
+
+	if (ksm_zero_pages_fd < 0) {
+		ksft_test_result_skip("open(\"/sys/kernel/mm/ksm/ksm_zero_pages\") failed\n");
+		return;
+	}
+	if (ksm_use_zero_pages_fd < 0) {
+		ksft_test_result_skip("open \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n");
+		return;
+	}
+	if (write(ksm_use_zero_pages_fd, "1", 1) != 1) {
+		ksft_test_result_skip("write \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n");
+		return;
+	}
+
+	/* Mmap zero pages */
+	map = mmap_and_merge_range(0x00, size, false);
+	if (map == MAP_FAILED)
+		return;
+
+	/* Check if ksm_zero_pages can be update correctly after merging */
+	pages_expected = size / pagesize;
+	if (pages_expected != get_ksm_zero_pages()) {
+		ksft_test_result_fail("'ksm_zero_pages' updated after merging\n");
+		goto unmap;
+	}
+
+	/* try to unmerge half of the region */
+	if (madvise(map, size / 2, MADV_UNMERGEABLE)) {
+		ksft_test_result_fail("MADV_UNMERGEABLE failed\n");
+		goto unmap;
+	}
+
+	/* Check if ksm_zero_pages can be update correctly after unmerging */
+	pages_expected /= 2;
+	if (pages_expected != get_ksm_zero_pages()) {
+		ksft_test_result_fail("'ksm_zero_pages' updated after unmerging\n");
+		goto unmap;
+	}
+
+	/* Check if ksm zero pages are really unmerged */
+	ksft_test_result(!range_maps_duplicates(map, size / 2),
+				"KSM zero pages were unmerged\n");
+unmap:
+	munmap(map, size);
+}
+
 static void test_unmerge_discarded(void)
 {
 	const unsigned int size = 2 * MiB;
@@ -358,7 +431,7 @@  static void test_prctl_unmerge(void)
 
 int main(int argc, char **argv)
 {
-	unsigned int tests = 5;
+	unsigned int tests = 6;
 	int err;
 
 #ifdef __NR_userfaultfd
@@ -379,8 +452,11 @@  int main(int argc, char **argv)
 	pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
 	if (pagemap_fd < 0)
 		ksft_exit_skip("open(\"/proc/self/pagemap\") failed\n");
+	ksm_zero_pages_fd = open("/sys/kernel/mm/ksm/ksm_zero_pages", O_RDONLY);
+	ksm_use_zero_pages_fd = open("/sys/kernel/mm/ksm/use_zero_pages", O_RDWR);
 
 	test_unmerge();
+	test_unmerge_zero_pages();
 	test_unmerge_discarded();
 #ifdef __NR_userfaultfd
 	test_unmerge_uffd_wp();