[PATCH v4 4/5] selftests/mm: move waiting for KSM full scans into a helper
From: Qiliang Yuan
Date: Sun Nov 08 2026 - 20:33:56 EST
ksm_start() enables ksmd and then polls full_scans until two more full
scans have completed, so that any merging that can happen has happened.
There is no helper to wait for a number of full scans while ksmd is
already running, so a test that needs one has to copy that loop.
Move the loop into ksm_wait_full_scans() and make ksm_start() call it
after enabling ksmd. The count is now read after ksmd starts rather than
before, which can only make ksm_start() wait longer.
Signed-off-by: Qiliang Yuan <odys.yuan@xxxxxxxxx>
Tested-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Reviewed-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
tools/testing/selftests/mm/vm_util.c | 31 ++++++++++++++++++-------------
tools/testing/selftests/mm/vm_util.h | 1 +
2 files changed, 19 insertions(+), 13 deletions(-)
diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index caea045fa7d46..8184691211acc 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -992,35 +992,40 @@ int ksm_use_zero_pages(void)
return ret == 1 ? 0 : -EIO;
}
+int ksm_wait_full_scans(long nr)
+{
+ long start_scans, end_scans;
+
+ start_scans = ksm_get_full_scans();
+ if (start_scans < 0)
+ return start_scans;
+ do {
+ end_scans = ksm_get_full_scans();
+ if (end_scans < 0)
+ return end_scans;
+ } while (end_scans < start_scans + nr);
+
+ return 0;
+}
+
int ksm_start(void)
{
int ksm_fd;
ssize_t ret;
- long start_scans, end_scans;
ksm_fd = open("/sys/kernel/mm/ksm/run", O_RDWR);
if (ksm_fd < 0)
return -errno;
- /* Wait for two full scans such that any possible merging happened. */
- start_scans = ksm_get_full_scans();
- if (start_scans < 0) {
- close(ksm_fd);
- return start_scans;
- }
ret = write(ksm_fd, "1", 1);
close(ksm_fd);
if (ret < 0)
return -errno;
if (ret != 1)
return -EIO;
- do {
- end_scans = ksm_get_full_scans();
- if (end_scans < 0)
- return end_scans;
- } while (end_scans < start_scans + 2);
- return 0;
+ /* Wait for two full scans such that any possible merging happened. */
+ return ksm_wait_full_scans(2);
}
int ksm_stop(void)
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index b5d59729d4327..7e00030077d2f 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -162,6 +162,7 @@ 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_full_scans(void);
+int ksm_wait_full_scans(long nr);
int ksm_use_zero_pages(void);
int ksm_start(void);
int ksm_stop(void);
--
2.43.0