selftests/mm: assert rmap behave as expected
Subsystems (4)
cover selftests/mm: assert rmap behave as expected
As David suggested, currently we don't have a high level test case to verify the behavior of rmap. This patch set introduce the verification on rmap by migration.
Patch 1 is a preparation to move ksm related operation into vm_util. Patch 2 is the new test case.
Currently it covers following four scenarios:
* anonymous page
* shmem page
* pagecache page
* ksm page
Wei Yang (2):
selftests/mm: put general ksm operation into vm_util selftests/mm: assert rmap behave as expected
MAINTAINERS | 1 + tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 3 + .../selftests/mm/ksm_functional_tests.c | 76 +-- tools/testing/selftests/mm/rmap.c | 466 ++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 + tools/testing/selftests/mm/vm_util.c | 71 +++ tools/testing/selftests/mm/vm_util.h | 7 + 8 files changed, 563 insertions(+), 66 deletions(-) create mode 100644 tools/testing/selftests/mm/rmap.c
Signature · 1 line
2.34.1
RFC PATCH 1/2 selftests/mm: put general ksm operation into vm_util
There are some general ksm operations could be used by other related test case. Put them into vm_util for common use.
This is a preparation patch for later use.
--- .../selftests/mm/ksm_functional_tests.c | 76 +++---------------- tools/testing/selftests/mm/vm_util.c | 71 +++++++++++++++++ tools/testing/selftests/mm/vm_util.h | 7 ++ 3 files changed, 88 insertions(+), 66 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +10 -66
@@ -73,74 +73,18 @@ static bool range_maps_duplicates(char *addr, unsigned long size)return false; } -static long get_my_ksm_zero_pages (void ) -{ -char buf[200 ]; -char *substr_ksm_zero; -size_t value_pos; -ssize_t read_size; -unsigned long my_ksm_zero_pages; - -if (!proc_self_ksm_stat_fd) -return 0 ; - - read_size =pread (proc_self_ksm_stat_fd, buf,sizeof (buf) -1 ,0 ); -if (read_size <0 ) -return -errno; - - buf[read_size] =0 ; - - substr_ksm_zero =strstr (buf,"ksm_zero_pages" ); -if (!substr_ksm_zero) -return 0 ; - - value_pos =strcspn (substr_ksm_zero,"0123456789" ); - my_ksm_zero_pages =strtol (substr_ksm_zero + value_pos,NULL ,10 ); - -return my_ksm_zero_pages; -} - -static long get_my_merging_pages (void ) -{ -char buf[10 ]; -ssize_t ret; - -if (proc_self_ksm_merging_pages_fd <0 ) -return proc_self_ksm_merging_pages_fd; - - ret =pread (proc_self_ksm_merging_pages_fd, buf,sizeof (buf) -1 ,0 ); -if (ret <=0 ) -return -errno; - buf[ret] =0 ; - -return strtol (buf,NULL ,10 ); -} - -static long ksm_get_full_scans (void ) -{ -char buf[10 ]; -ssize_t ret; - - ret =pread (ksm_full_scans_fd, buf,sizeof (buf) -1 ,0 ); -if (ret <=0 ) -return -errno; - buf[ret] =0 ; - -return strtol (buf,NULL ,10 ); -} -static int ksm_merge (void ) {long start_scans, end_scans;/* Wait for two full scans such that any possible merging happened. */ - start_scans =ksm_get_full_scans (); + start_scans =ksm_get_full_scans (ksm_full_scans_fd);if (start_scans <0 )return start_scans; -if (write (ksm_fd,"1" ,1 ) !=1 ) +if (ksm_start_and_merge (ksm_fd) !=1 )return -errno;do { - end_scans =ksm_get_full_scans (); + end_scans =ksm_get_full_scans (ksm_full_scans_fd);if (end_scans <0 )return end_scans; }while (end_scans < start_scans +2 );
@@ -150,7 +94,7 @@ static int ksm_merge(void)static int ksm_unmerge (void ) { -if (write (ksm_fd,"2" ,1 ) !=1 ) +if (ksm_stop_and_unmerge (ksm_fd) !=1 )return -errno;return 0 ; }
@@ -168,7 +112,7 @@ static char *__mmap_and_merge_range(char val, unsigned long size, int prot,return err_map; } -if (get_my_merging_pages () >0 ) { +if (ksm_get_self_merging_pages (proc_self_ksm_merging_pages_fd) >0 ) {ksft_print_msg ("Still pages merged\n" );return err_map; }
@@ -227,7 +171,7 @@ static char *__mmap_and_merge_range(char val, unsigned long size, int prot,* Check if anything was merged at all. Ignore the zero page that is * accounted differently (depending on kernel support). */ -if (val && !get_my_merging_pages ()) { +if (val && !ksm_get_self_merging_pages (proc_self_ksm_merging_pages_fd)) {ksft_print_msg ("No pages got merged\n" ); goto unmap; }
@@ -294,7 +238,7 @@ static void test_unmerge_zero_pages(void)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 ) { +if (ksm_use_zero_pages (ksm_use_zero_pages_fd) !=1 ) {ksft_test_result_skip ("write \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n" );return ; }
@@ -306,7 +250,7 @@ static void test_unmerge_zero_pages(void)/* Check if ksm_zero_pages is updated correctly after KSM merging */ pages_expected = size / pagesize; -if (pages_expected !=get_my_ksm_zero_pages ()) { +if (pages_expected !=ksm_get_self_zero_pages (proc_self_ksm_stat_fd)) {ksft_test_result_fail ("'ksm_zero_pages' updated after merging\n" ); goto unmap; }
@@ -319,7 +263,7 @@ static void test_unmerge_zero_pages(void)/* Check if ksm_zero_pages is updated correctly after unmerging */ pages_expected /=2 ; -if (pages_expected !=get_my_ksm_zero_pages ()) { +if (pages_expected !=ksm_get_self_zero_pages (proc_self_ksm_stat_fd)) {ksft_test_result_fail ("'ksm_zero_pages' updated after unmerging\n" ); goto unmap; }
@@ -329,7 +273,7 @@ static void test_unmerge_zero_pages(void) *((unsigned int *)&map[offs]) = offs;/* Now we should have no zeropages remaining. */ -if (get_my_ksm_zero_pages ()) { +if (ksm_get_self_zero_pages (proc_self_ksm_stat_fd)) {ksft_test_result_fail ("'ksm_zero_pages' updated after write fault\n" ); goto unmap; }
tools/testing/selftests/mm/vm_util.c +71 -0
74 lines not on this page · raw message
tools/testing/selftests/mm/vm_util.h +7 -0
@@ -95,6 +95,13 @@ static inline int open_self_procmap(struct procmap_fd *procmap_out)return open_procmap (pid, procmap_out); } +int ksm_use_zero_pages (int ksm_use_zero_pages_fd); +int ksm_start_and_merge (int ksm_fd); +int ksm_stop_and_unmerge (int ksm_fd); +long ksm_get_full_scans (int ksm_full_scans_fd); +long ksm_get_self_merging_pages (int proc_self_ksm_merging_pages_fd); +long ksm_get_self_zero_pages (int proc_self_ksm_stat_fd); +/* * On ppc64 this will only work with radix 2M hugepage size */
Signature · 1 line
2.34.1
Re: [RFC Patch 1/2] selftests/mm: put general ksm operation into vm_util
tools/testing/selftests/mm/vm_util.c int close_procmap(struct procmap_fd *procmap) · line 489
Quoted 9 lines
tools/testing/selftests/mm/vm_util.c +1
-0@@ -486,3 +486,74 @@ int close_procmap(struct procmap_fd *procmap) {return close (procmap->fd); } +
I think we should just let all these functions open/close the fds. So there will not be a need to pass in the fds.
tools/testing/selftests/mm/vm_util.c int close_procmap(struct procmap_fd *procmap) · line 503
Quoted 13 lines
tools/testing/selftests/mm/vm_util.c +13
-0+int ksm_use_zero_pages (int ksm_use_zero_pages_fd) +{ +return write (ksm_use_zero_pages_fd,"1" ,1 ); +} + +int ksm_start_and_merge (int ksm_fd) +{ +return write (ksm_fd,"1" ,1 ); +}> + +int ksm_stop_and_unmerge(int ksm_fd) +{ +return write (ksm_fd,"2" ,1 ); +}
Can we make all these functions return "0" on success? This, way, the "write" will be an internal implementation detail.
E.g.,
int ksm_stop_and_unmerge(void)
{
int ksm_fd = ...
ssize_t ret;
...
ret = write(ksm_fd, "2", 1);
close(ksm_fd);
return ret == 1 ? 0 : ret;
}Quoted 61 lines
tools/testing/selftests/mm/vm_util.c +56
-0+ +long ksm_get_full_scans (int ksm_full_scans_fd) +{ +char buf[10 ]; +ssize_t ret; + + ret =pread (ksm_full_scans_fd, buf,sizeof (buf) -1 ,0 ); +if (ret <=0 ) +return -errno; + buf[ret] =0 ; + +return strtol (buf,NULL ,10 ); +} + +long ksm_get_self_merging_pages (int proc_self_ksm_merging_pages_fd) +{ +char buf[10 ]; +ssize_t ret; + +if (proc_self_ksm_merging_pages_fd <0 ) +return proc_self_ksm_merging_pages_fd; + + ret =pread (proc_self_ksm_merging_pages_fd, buf,sizeof (buf) -1 ,0 ); +if (ret <=0 ) +return -errno; + buf[ret] =0 ; + +return strtol (buf,NULL ,10 ); +} + +long ksm_get_self_zero_pages (int proc_self_ksm_stat_fd) +{ +char buf[200 ]; +char *substr_ksm_zero; +size_t value_pos; +ssize_t read_size; +unsigned long my_ksm_zero_pages; + +if (!proc_self_ksm_stat_fd) +return 0 ; + + read_size =pread (proc_self_ksm_stat_fd, buf,sizeof (buf) -1 ,0 ); +if (read_size <0 ) +return -errno; + + buf[read_size] =0 ; + + substr_ksm_zero =strstr (buf,"ksm_zero_pages" ); +if (!substr_ksm_zero) +return 0 ; + + value_pos =strcspn (substr_ksm_zero,"0123456789" ); + my_ksm_zero_pages =strtol (substr_ksm_zero + value_pos,NULL ,10 ); + +return my_ksm_zero_pages; +}tools/testing/selftests/mm/vm_util.h +0
-0
tools/testing/selftests/mm/vm_util.h static inline int open_self_procmap(struct procmap_fd *procmap_out) · line 103
Quoted 9 lines
tools/testing/selftests/mm/vm_util.h +6
-0@@ -95,6 +95,13 @@ static inline int open_self_procmap(struct procmap_fd *procmap_out)return open_procmap (pid, procmap_out); } +int ksm_use_zero_pages (int ksm_use_zero_pages_fd); +int ksm_start_and_merge (int ksm_fd); +int ksm_stop_and_unmerge (int ksm_fd); +long ksm_get_full_scans (int ksm_full_scans_fd); +long ksm_get_self_merging_pages (int proc_self_ksm_merging_pages_fd); +long ksm_get_self_zero_pages (int proc_self_ksm_stat_fd);
With the fd parameters removed, that interface will look quite neat I think.
Signature · 3 lines
Cheers, David / dhildenb
Re: [RFC Patch 1/2] selftests/mm: put general ksm operation into vm_util
Quoted 4 lines · David Hildenbrand
Quoted 4 lines
tools/testing/selftests/mm/vm_util.c +0
-0
Quoted 9 lines
tools/testing/selftests/mm/vm_util.c int close_procmap(struct procmap_fd *procmap) · line 489
Quoted 5 lines
tools/testing/selftests/mm/vm_util.c +1
-0@@ -486,3 +486,74 @@ int close_procmap(struct procmap_fd *procmap) {return close (procmap->fd); } +I think we should just let all these functions open/close the fds. So there will not be a need to pass in the fds.
Sure. Will update it.
Quoted 20 lines
tools/testing/selftests/mm/vm_util.c int close_procmap(struct procmap_fd *procmap) · line 503
Quoted 13 lines
tools/testing/selftests/mm/vm_util.c +13
-0+int ksm_use_zero_pages (int ksm_use_zero_pages_fd) +{ +return write (ksm_use_zero_pages_fd,"1" ,1 ); +} + +int ksm_start_and_merge (int ksm_fd) +{ +return write (ksm_fd,"1" ,1 ); +}> + +int ksm_stop_and_unmerge(int ksm_fd) +{ +return write (ksm_fd,"2" ,1 ); +}Can we make all these functions return "0" on success? This, way, the "write" will be an internal implementation detail.
E.g.,
int
ksm_stop_and_unmerge(void)
Quoted 11 lines
{
int ksm_fd = ...
ssize_t ret;
...
ret = write(ksm_fd, "2", 1);
close(ksm_fd);
return ret == 1 ? 0 : ret;
}Sure will do it.
Signature · 2 lines
Wei Yang Help you, Help me
RFC PATCH 2/2 selftests/mm: assert rmap behave as expected
In reply to Wei YangAs David suggested, currently we don't have a high level test case to verify the behavior of rmap. This patch introduce the verification on rmap by migration.
The general idea is if migrate one shared page between processes, this would be reflected in all related processes. Otherwise, we have problem in rmap.
Currently it covers following four scenarios:
* anonymous page
* shmem page
* pagecache page
* ksm page
--- MAINTAINERS | 1 + tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 3 + tools/testing/selftests/mm/rmap.c | 466 ++++++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 + 5 files changed, 475 insertions(+) create mode 100644 tools/testing/selftests/mm/rmap.c
MAINTAINERS +1 -0
@@ -15664,6 +15664,7 @@ L: [email protected] S: Maintained F: include/linux/rmap.h F: mm/rmap.c +F: tools/testing/selftests/mm/rmap.c MEMORY MANAGEMENT - SECRETMEM M: Andrew Morton <[email protected]>
tools/testing/selftests/mm/.gitignore +1 -0
@@ -60,3 +60,4 @@ pkey_sighandler_tests_32 pkey_sighandler_tests_64 guard-regions merge +rmap
tools/testing/selftests/mm/Makefile +3 -0
@@ -100,6 +100,7 @@ TEST_GEN_FILES += hugetlb_dio TEST_GEN_FILES += droppable TEST_GEN_FILES += guard-regions TEST_GEN_FILES += merge +TEST_GEN_FILES += rmap ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty
@@ -227,6 +228,8 @@ $(OUTPUT)/ksm_tests: LDLIBS += -lnuma $(OUTPUT)/migration: LDLIBS += -lnuma +$(OUTPUT)/rmap: LDLIBS += -lnuma + local_config.mk local_config.h: check_config.sh /bin/sh ./check_config.sh $(CC)
tools/testing/selftests/mm/rmap.c new file +466 -0
466 lines not on this page · raw message
tools/testing/selftests/mm/run_vmtests.sh +4 -0
@@ -83,6 +83,8 @@ separated by spaces: test handling of page fragment allocation and freeing - vma_merge test VMA merge cases behave as expected +- rmap + test rmap behave as expected example: ./run_vmtests.sh -t "hmm mmap ksm" EOF
@@ -521,6 +523,8 @@ CATEGORY="page_frag" run_test ./test_page_frag.sh aligned CATEGORY="page_frag" run_test ./test_page_frag.sh nonaligned +CATEGORY="rmap" run_test ./rmap + echo "SUMMARY: PASS=${count_pass} SKIP=${count_skip} FAIL=${count_fail}" | tap_prefix echo "1..${count_total}" | tap_output
Signature · 1 line
2.34.1
On Wed, Jun 04, 2025 at 08:21:45AM +0000, Wei Yang wrote:
[...]
tools/testing/selftests/mm/rmap.c line 220
Quoted 7 lines
tools/testing/selftests/mm/rmap.c new file +7
-0+int try_to_move_page (char *region) +{ +int ret; +int node; +int status =0 ; + +ksft_print_msg ("worker %d move_pages of content: %.15s\n" ,getpid (), region);
One thing confused me here.
If I don't access region here, the following move_pages() would report -ENOENT occationally. The reason is do_pages_stat_array() -> folio_walk_start() returns NULL.
Not sure which part I missed.
Quoted 23 lines
tools/testing/selftests/mm/rmap.c new file +23
-0+ + ret =move_pages (0 ,1 , (void **)®ion,NULL , &status,MPOL_MF_MOVE_ALL ); +if (ret !=0 ) +return FAIL_ON_WORK ; + +/* Pick up a different target node */ +for (node =0 ; node <=numa_max_node (); node++) { +if (numa_bitmask_isbitset (numa_all_nodes_ptr, node) && node != status) +break ; + } + +if (node >numa_max_node ()) { +ksft_print_msg ("Couldn't find available numa node for testing\n" ); +return FAIL_ON_WORK ; + } + + ret =move_pages (0 ,1 , (void **)®ion, &node, &status,MPOL_MF_MOVE_ALL ); +if (ret !=0 ) +return FAIL_ON_WORK ; + +return 0 ; +} +
Signature · 2 lines
Wei Yang Help you, Help me
Quoted 12 lines · Wei Yang
On Wed, Jun 04, 2025 at 08:21:45AM +0000, Wei Yang wrote:
[...]tools/testing/selftests/mm/rmap.c line 220
Quoted 7 lines
tools/testing/selftests/mm/rmap.c new file +7
-0+int try_to_move_page (char *region) +{ +int ret; +int node; +int status =0 ; + +ksft_print_msg ("worker %d move_pages of content: %.15s\n" ,getpid (), region);One thing confused me here.
Quoted 3 lines
If I don't access region here, the following
move_pages()would report-ENOENToccationally. The reason isdo_pages_stat_array()->folio_walk_start()returns NULL.
Right, the pages were not faulted in. The man page mentions that as
"-ENOENT: The page is not present."
Signature · 3 lines
Cheers, David / dhildenb
Quoted 13 lines · David Hildenbrand
Quoted 12 lines · Wei Yang
On Wed, Jun 04, 2025 at 08:21:45AM +0000, Wei Yang wrote:
[...]tools/testing/selftests/mm/rmap.c line 220
Quoted 7 lines
tools/testing/selftests/mm/rmap.c new file +7
-0+int try_to_move_page (char *region) +{ +int ret; +int node; +int status =0 ; + +ksft_print_msg ("worker %d move_pages of content: %.15s\n" ,getpid (), region);One thing confused me here.
Quoted 8 lines
Quoted 3 lines
If I don't access region here, the following
move_pages()would report-ENOENToccationally. The reason isdo_pages_stat_array()->folio_walk_start()returns NULL.Right, the pages were not faulted in. The man page mentions that as
"
-ENOENT: The page is not present."
Thanks I see the man page, but from the code point of view, I don't follow it.
The move_pages() return -ENOENT in a child process, and the child duplicate it memory space during fork().
dup_mmap()
for_each_vma()
copy_page_range()
...
copy_pte_range() -> copy_present_ptes()
__copy_present_ptes()
set_ptes()Even we map the range by MAP_SHARED, we don't need to wrprotect it. But we still set_ptes() in the child process page table.
So it looks the child has prepared the page table and not need to fault in to setup it.
Do I miss something?
Quoted 4 lines
Signature · 3 lines
Cheers, David / dhildenb
Signature · 2 lines
Wei Yang Help you, Help me
Quoted 36 lines · Wei Yang
Quoted 13 lines · David Hildenbrand
Quoted 12 lines · Wei Yang
On Wed, Jun 04, 2025 at 08:21:45AM +0000, Wei Yang wrote:
[...]tools/testing/selftests/mm/rmap.c line 220
Quoted 7 lines
tools/testing/selftests/mm/rmap.c new file +7
-0+int try_to_move_page (char *region) +{ +int ret; +int node; +int status =0 ; + +ksft_print_msg ("worker %d move_pages of content: %.15s\n" ,getpid (), region);One thing confused me here.
Quoted 8 lines
Quoted 3 lines
If I don't access region here, the following
move_pages()would report-ENOENToccationally. The reason isdo_pages_stat_array()->folio_walk_start()returns NULL.Right, the pages were not faulted in. The man page mentions that as
"
-ENOENT: The page is not present."Thanks I see the man page, but from the code point of view, I don't follow it.
The
move_pages()return-ENOENTin a child process, and the child duplicate it memory space duringfork().
dup_mmap()for_each_vma() copy_page_range() ... copy_pte_range() -> copy_present_ptes() __copy_present_ptes() set_ptes()
Quoted 8 lines
Even we map the range by
MAP_SHARED, we don't need to wrprotect it. But we stillset_ptes()in the child process page table.So it looks the child has prepared the page table and not need to fault in to setup it.
Do I miss something?
See copy_page_range() -> vma_needs_copy(), where we essentially optimize-out copying of page tables for most MAP_SHARED mappings (or MAP_PRIVATE mappings when we never placed private pages).
Signature · 3 lines
Cheers, David / dhildenb