Skip to content

Commit 3d0745e

Browse files
xu xinakpm00
authored andcommitted
selftest: add a testcase of ksm zero pages
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 four 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 by madvise(...MADV_UNMERGEABLE); (3) whether the count of ksm zero pages can update correctly after unmerging by triggering write fault. (4) whether ksm zero pages are really unmerged. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: xu xin <[email protected]> Acked-by: David Hildenbrand <[email protected]> Reviewed-by: Xiaokai Ran <[email protected]> Reviewed-by: Yang Yang <[email protected]> Cc: Claudio Imbrenda <[email protected]> Cc: Xuexin Jiang <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 1a8e843 commit 3d0745e

File tree

1 file changed

+97
-1
lines changed

1 file changed

+97
-1
lines changed

tools/testing/selftests/mm/ksm_functional_tests.c

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
static int ksm_fd;
3131
static int ksm_full_scans_fd;
32+
static int proc_self_ksm_stat_fd;
33+
static int ksm_use_zero_pages_fd;
3234
static int pagemap_fd;
3335
static size_t pagesize;
3436

@@ -59,6 +61,33 @@ static bool range_maps_duplicates(char *addr, unsigned long size)
5961
return false;
6062
}
6163

64+
static long get_my_ksm_zero_pages(void)
65+
{
66+
char buf[200];
67+
char *substr_ksm_zero;
68+
size_t value_pos;
69+
ssize_t read_size;
70+
unsigned long my_ksm_zero_pages;
71+
72+
if (!proc_self_ksm_stat_fd)
73+
return 0;
74+
75+
read_size = pread(proc_self_ksm_stat_fd, buf, sizeof(buf) - 1, 0);
76+
if (read_size < 0)
77+
return -errno;
78+
79+
buf[read_size] = 0;
80+
81+
substr_ksm_zero = strstr(buf, "ksm_zero_pages");
82+
if (!substr_ksm_zero)
83+
return 0;
84+
85+
value_pos = strcspn(substr_ksm_zero, "0123456789");
86+
my_ksm_zero_pages = strtol(substr_ksm_zero + value_pos, NULL, 10);
87+
88+
return my_ksm_zero_pages;
89+
}
90+
6291
static long ksm_get_full_scans(void)
6392
{
6493
char buf[10];
@@ -159,6 +188,70 @@ static void test_unmerge(void)
159188
munmap(map, size);
160189
}
161190

191+
static void test_unmerge_zero_pages(void)
192+
{
193+
const unsigned int size = 2 * MiB;
194+
char *map;
195+
unsigned int offs;
196+
unsigned long pages_expected;
197+
198+
ksft_print_msg("[RUN] %s\n", __func__);
199+
200+
if (proc_self_ksm_stat_fd < 0) {
201+
ksft_test_result_skip("open(\"/proc/self/ksm_stat\") failed\n");
202+
return;
203+
}
204+
if (ksm_use_zero_pages_fd < 0) {
205+
ksft_test_result_skip("open \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n");
206+
return;
207+
}
208+
if (write(ksm_use_zero_pages_fd, "1", 1) != 1) {
209+
ksft_test_result_skip("write \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n");
210+
return;
211+
}
212+
213+
/* Let KSM deduplicate zero pages. */
214+
map = mmap_and_merge_range(0x00, size, false);
215+
if (map == MAP_FAILED)
216+
return;
217+
218+
/* Check if ksm_zero_pages is updated correctly after KSM merging */
219+
pages_expected = size / pagesize;
220+
if (pages_expected != get_my_ksm_zero_pages()) {
221+
ksft_test_result_fail("'ksm_zero_pages' updated after merging\n");
222+
goto unmap;
223+
}
224+
225+
/* Try to unmerge half of the region */
226+
if (madvise(map, size / 2, MADV_UNMERGEABLE)) {
227+
ksft_test_result_fail("MADV_UNMERGEABLE failed\n");
228+
goto unmap;
229+
}
230+
231+
/* Check if ksm_zero_pages is updated correctly after unmerging */
232+
pages_expected /= 2;
233+
if (pages_expected != get_my_ksm_zero_pages()) {
234+
ksft_test_result_fail("'ksm_zero_pages' updated after unmerging\n");
235+
goto unmap;
236+
}
237+
238+
/* Trigger unmerging of the other half by writing to the pages. */
239+
for (offs = size / 2; offs < size; offs += pagesize)
240+
*((unsigned int *)&map[offs]) = offs;
241+
242+
/* Now we should have no zeropages remaining. */
243+
if (get_my_ksm_zero_pages()) {
244+
ksft_test_result_fail("'ksm_zero_pages' updated after write fault\n");
245+
goto unmap;
246+
}
247+
248+
/* Check if ksm zero pages are really unmerged */
249+
ksft_test_result(!range_maps_duplicates(map, size),
250+
"KSM zero pages were unmerged\n");
251+
unmap:
252+
munmap(map, size);
253+
}
254+
162255
static void test_unmerge_discarded(void)
163256
{
164257
const unsigned int size = 2 * MiB;
@@ -358,7 +451,7 @@ static void test_prctl_unmerge(void)
358451

359452
int main(int argc, char **argv)
360453
{
361-
unsigned int tests = 5;
454+
unsigned int tests = 6;
362455
int err;
363456

364457
#ifdef __NR_userfaultfd
@@ -379,8 +472,11 @@ int main(int argc, char **argv)
379472
pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
380473
if (pagemap_fd < 0)
381474
ksft_exit_skip("open(\"/proc/self/pagemap\") failed\n");
475+
proc_self_ksm_stat_fd = open("/proc/self/ksm_stat", O_RDONLY);
476+
ksm_use_zero_pages_fd = open("/sys/kernel/mm/ksm/use_zero_pages", O_RDWR);
382477

383478
test_unmerge();
479+
test_unmerge_zero_pages();
384480
test_unmerge_discarded();
385481
#ifdef __NR_userfaultfd
386482
test_unmerge_uffd_wp();

0 commit comments

Comments
 (0)