linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: <xu.xin16@zte.com.cn>
To: <hughd@google.com>, <akpm@linux-foundation.org>, <david@kernel.org>
Cc: <michel@lespinasse.org>, <ljs@kernel.org>,
	<chengming.zhou@linux.dev>, <linux-kernel@vger.kernel.org>,
	<linux-mm@kvack.org>
Subject: ksm: add mremap selftests for ksm_rmap_walk
Date: Tue, 7 Apr 2026 14:08:05 +0800 (CST)	[thread overview]
Message-ID: <20260407140805858ViqJKFhfmYSfq0FynsaEY@zte.com.cn> (raw)

From: xu xin <xu.xin16@zte.com.cn>

The existing tools/testing/selftests/mm/rmap.c has already one testcase
for ksm_rmap_walk in TEST_F(migrate, ksm), which takes use of migration
of page from one NUMA node to another NUMA node. However, it just lacks
the senario of mremapped VMAs.

Before migrating, we add the calling of mremap() to address mapped with KSM
pages, which is specailly to test a optimization which is introduced by this
patch ("ksm: Optimize rmap_walk_ksm by passing a suitable address range")
https://lore.kernel.org/all/20260212193045556CbzCX8p9gDu73tQ2nvHEI@zte.com.cn/

Result:
TAP version 13
1..5
ok 1 migrate.anon
ok 2 migrate.shm
ok 3 migrate.file # SKIP Failed in worker
ok 4 migrate.ksm
ok 5 migrate.ksm_and_mremap

Signed-off-by: xu xin <xu.xin16@zte.com.cn>
---
 tools/testing/selftests/mm/rmap.c    | 69 ++++++++++++++++++++++++++++
 tools/testing/selftests/mm/vm_util.c | 38 +++++++++++++++
 tools/testing/selftests/mm/vm_util.h |  2 +
 3 files changed, 109 insertions(+)

diff --git a/tools/testing/selftests/mm/rmap.c b/tools/testing/selftests/mm/rmap.c
index 53f2058b0ef2..65470def2bf1 100644
--- a/tools/testing/selftests/mm/rmap.c
+++ b/tools/testing/selftests/mm/rmap.c
@@ -430,4 +430,73 @@ TEST_F(migrate, ksm)
 	propagate_children(_metadata, data);
 }

+/* To test if ksm page can be migrated when it's mremapped */
+int merge_mremap_and_migrate(struct global_data *data)
+{
+	int ret = 0;
+	/* Allocate range and set the same data */
+	data->mapsize = 3*getpagesize();
+	data->region = mmap(NULL, data->mapsize, PROT_READ|PROT_WRITE,
+			   MAP_PRIVATE|MAP_ANON, -1, 0);
+	if (data->region == MAP_FAILED)
+		ksft_exit_fail_perror("mmap failed");
+
+	memset(data->region, 0x77, data->mapsize);
+
+	if (ksm_start() < 0)
+		return FAIL_ON_CHECK;
+
+	/* 1  2 expected */
+	ksft_print_msg("Shared: %ld (1 expected) Sharing: %ld (2 expected)\n",
+		ksm_get_pages_shared(), ksm_get_pages_sharing());
+
+	/*
+	 * Mremap the second pagesize address range into the third pagesize
+	 * address.
+	 */
+	data->region = mremap(data->region + getpagesize(), getpagesize(), getpagesize(),
+			 MREMAP_MAYMOVE|MREMAP_FIXED, data->region + 2*getpagesize());
+
+	if (data->region == MAP_FAILED)
+		return FAIL_ON_CHECK;
+
+	/* Check if we can migrate this region successfully */
+	ret = try_to_move_page(data->region);
+	if (ret != 0)
+		return ret;
+
+	/* Wait ksm scan two turns at least */
+	if (ksm_start() < 0)
+		return FAIL_ON_CHECK;
+
+	/* 1  1 expected */
+	ksft_print_msg("Shared: %ld (1 expected) Sharing: %ld (1 expected)\n",
+		ksm_get_pages_shared(), ksm_get_pages_sharing());
+
+	return 0;
+}
+
+TEST_F(migrate, ksm_and_mremap)
+{
+	int ret;
+	struct global_data *data = &self->data;
+
+	/* prepare KSM interface setting */
+	if (ksm_stop() < 0)
+		SKIP(return, "accessing \"/sys/kernel/mm/ksm/run\") failed");
+	if (ksm_get_full_scans() < 0)
+		SKIP(return, "accessing \"/sys/kernel/mm/ksm/full_scan\") failed");
+
+	ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0);
+	if (ret < 0 && errno == EINVAL)
+		SKIP(return, "PR_SET_MEMORY_MERGE not supported");
+	else if (ret)
+		ksft_exit_fail_perror("PR_SET_MEMORY_MERGE=1 failed");
+
+	/* Start to merge same pages and mremap one of the three page area,
+	 * and test if ksm page can be migrated when it's mremapped */
+	ASSERT_EQ(merge_mremap_and_migrate(data), 0);
+}
+
+
 TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 9428f4d7bf55..47402b771ef8 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -646,6 +646,44 @@ long ksm_get_self_merging_pages(void)
 	return strtol(buf, NULL, 10);
 }

+long ksm_get_pages_shared(void)
+{
+	int ksm_pages_shared_fd;
+	char buf[10];
+	ssize_t ret;
+
+	ksm_pages_shared_fd = open("/sys/kernel/mm/ksm/pages_shared", O_RDONLY);
+	if (ksm_pages_shared_fd < 0)
+		return -errno;
+
+	ret = pread(ksm_pages_shared_fd, buf, sizeof(buf) - 1, 0);
+	close(ksm_pages_shared_fd);
+	if (ret <= 0)
+		return -errno;
+	buf[ret] = 0;
+
+	return strtol(buf, NULL, 10);
+}
+
+long ksm_get_pages_sharing(void)
+{
+	int ksm_pages_sharing_fd;
+	char buf[10];
+	ssize_t ret;
+
+	ksm_pages_sharing_fd = open("/sys/kernel/mm/ksm/pages_sharing", O_RDONLY);
+	if (ksm_pages_sharing_fd < 0)
+		return -errno;
+
+	ret = pread(ksm_pages_sharing_fd, buf, sizeof(buf) - 1, 0);
+	close(ksm_pages_sharing_fd);
+	if (ret <= 0)
+		return -errno;
+	buf[ret] = 0;
+
+	return strtol(buf, NULL, 10);
+}
+
 long ksm_get_full_scans(void)
 {
 	int ksm_full_scans_fd;
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index e9c4e24769c1..b0dd190f83e4 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -151,6 +151,8 @@ void *sys_mremap(void *old_address, unsigned long old_size,

 long ksm_get_self_zero_pages(void);
 long ksm_get_self_merging_pages(void);
+long ksm_get_pages_shared(void);
+long ksm_get_pages_sharing(void);
 long ksm_get_full_scans(void);
 int ksm_use_zero_pages(void);
 int ksm_start(void);
-- 
2.25.1


             reply	other threads:[~2026-04-07  6:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-07  6:08 xu.xin16 [this message]
2026-04-07  9:43 ` Lorenzo Stoakes (Oracle)
2026-04-07 15:00 ` David Hildenbrand (Arm)
2026-04-08  0:08   ` Hugh Dickins
2026-04-08  7:11     ` David Hildenbrand (Arm)
2026-04-08 13:15     ` xu.xin16
2026-04-08 13:19       ` David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260407140805858ViqJKFhfmYSfq0FynsaEY@zte.com.cn \
    --to=xu.xin16@zte.com.cn \
    --cc=akpm@linux-foundation.org \
    --cc=chengming.zhou@linux.dev \
    --cc=david@kernel.org \
    --cc=hughd@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=michel@lespinasse.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox