selftest: add testing unsharing and counting ksm zero page
Subsystems (2)
PATCH v5 6/6 selftest: add testing unsharing and counting ksm zero page
From: xu xin <[email protected]>
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 page can react correctly to cow (copy on write);
2) whether the count of ksm zero page can react correctly to unmerge;
3) whether ksm zero pages are really unmerged.
v4->v5:
fix error of "} while (end_scans < start_scans + 20);" to "} while (end_scans < start_scans + 2);" in wait_two_full_scans().
--- tools/testing/selftests/vm/ksm_functional_tests.c | 103 +++++++++++++++++++++- 1 file changed, 99 insertions(+), 4 deletions(-)
tools/testing/selftests/vm/ksm_functional_tests.c +99 -4
@@ -27,6 +27,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;
@@ -57,6 +59,22 @@ static bool range_maps_duplicates(char *addr, unsigned long size)return false; } +static bool check_ksm_zero_pages_count (unsigned long zero_size) +{ +unsigned long pages_expected = zero_size / (4 * KiB); +char buf[20 ]; +ssize_t read_size; +unsigned long ksm_zero_pages; + + 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 == pages_expected; +} +static long ksm_get_full_scans (void ) {char buf[10 ];
@@ -70,15 +88,12 @@ static long ksm_get_full_scans(void)return strtol (buf,NULL ,10 ); } -static int ksm_merge (void ) +static int wait_two_full_scans (void ) {long start_scans, end_scans; -/* Wait for two full scans such that any possible merging happened. */ start_scans =ksm_get_full_scans ();if (start_scans <0 ) -return start_scans; -if (write (ksm_fd,"1" ,1 ) !=1 )return -errno; do { end_scans = ksm_get_full_scans();
@@ -89,6 +104,34 @@ static int ksm_merge(void)return 0 ; } +static inline int ksm_merge (void ) +{ +/* Wait for two full scans such that any possible merging happened. */ +if (write (ksm_fd,"1" ,1 ) !=1 ) +return -errno; +return wait_two_full_scans (); +} + +static inline int make_cow (char *map,char val,unsigned long size) +{ + +memset (map, val, size); +return wait_two_full_scans (); +} + +static int unmerge_zero_page (char *start,unsigned long size) +{ +int ret; + + ret =madvise (start, size,MADV_UNMERGEABLE ); +if (ret) { +ksft_test_result_fail ("MADV_UNMERGEABLE failed\n" ); +return ret; + } + +return wait_two_full_scans (); +} +static char *mmap_and_merge_range (char val,unsigned long size) {char *map;
@@ -146,6 +189,56 @@ static void test_unmerge(void)munmap (map, size); } +static void test_unmerge_zero_pages (void ) +{ +const unsigned int size =2 * MiB; +char *map; + +ksft_print_msg ("[RUN] %s\n" , __func__); + +/* Confirm the interfaces*/ + ksm_zero_pages_fd =open ("/sys/kernel/mm/ksm/zero_pages_sharing" ,O_RDONLY ); +if (ksm_zero_pages_fd <0 ) { +ksft_test_result_skip ("open(\"/sys/kernel/mm/ksm/zero_pages_sharing\") failed\n" ); +return ; + } + ksm_use_zero_pages_fd =open ("/sys/kernel/mm/ksm/use_zero_pages" ,O_RDWR ); +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); + +/* Case 1: make Writing on ksm zero pages (COW) */ +if (make_cow (map,0xcf , size /2 )) { +ksft_test_result_fail ("COW failed\n" ); + goto unmap; + } +ksft_test_result (check_ksm_zero_pages_count (size /2 ), +"zero page count react to cow\n" ); + +/* Case 2: Call madvise(xxx, MADV_UNMERGEABLE)*/ +if (unmerge_zero_page (map + size /2 , size /4 )) { +ksft_test_result_fail ("unmerge_zero_page failed\n" ); + goto unmap; + } +ksft_test_result (check_ksm_zero_pages_count (size /4 ), +"zero page count react to unmerge\n" ); + +/*Check if ksm pages are really unmerged */ +ksft_test_result (!range_maps_duplicates (map + size /2 , size /4 ), +"KSM zero pages were unmerged\n" ); + +unmap: +munmap (map, size); +} +static void test_unmerge_discarded (void ) {const unsigned int size =2 * MiB;
@@ -261,11 +354,13 @@ int main(int argc, char **argv) ksm_full_scans_fd =open ("/sys/kernel/mm/ksm/full_scans" ,O_RDONLY );if (ksm_full_scans_fd <0 )ksft_exit_skip ("open(\"/sys/kernel/mm/ksm/full_scans\") failed\n" ); + pagemap_fd =open ("/proc/self/pagemap" ,O_RDONLY );if (pagemap_fd <0 )ksft_exit_skip ("open(\"/proc/self/pagemap\") failed\n" );test_unmerge (); +test_unmerge_zero_pages ();test_unmerge_discarded ();#ifdef __NR_userfaultfdtest_unmerge_uffd_wp ();
Signature · 1 line
2.15.2
Quoted 31 lines · On 30.12.22 02:18, [email protected] wrote:
From: xu xin <[email protected]>
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 page can react correctly to cow (copy on write);
2) whether the count of ksm zero page can react correctly to unmerge;
3) whether ksm zero pages are really unmerged.Signed-off-by xu xin <[email protected]>Cc Claudio Imbrenda <[email protected]>Cc David Hildenbrand <[email protected]>Cc Xuexin Jiang <[email protected]>Reviewed-by Xiaokai Ran <[email protected]>Reviewed-by Yang Yang <[email protected]>
v4->v5:
fix error of "} while (end_scans<start_scans+ 20);" to "} while (end_scans<start_scans+ 2);" inwait_two_full_scans().--- tools/testing/selftests/vm/ksm_functional_tests.c | 103 +++++++++++++++++++++- 1 file changed, 99 insertions(+), 4 deletions(-)tools/testing/selftests/vm/ksm_functional_tests.c +0
-0
tools/testing/selftests/vm/ksm_functional_tests.c line 31
Quoted 5 lines
tools/testing/selftests/vm/ksm_functional_tests.c +2
-0@@ -27,6 +27,8 @@static int ksm_fd;static int ksm_full_scans_fd; +static int ksm_zero_pages_fd; +static int ksm_use_zero_pages_fd;
If they are global, open them from main(), not from the test case.
Quoted 11 lines
tools/testing/selftests/vm/ksm_functional_tests.c +4
-0static int pagemap_fd;static size_t pagesize;@@ -57,6 +59,22 @@ static bool range_maps_duplicates(char *addr, unsigned long size)return false; } +static bool check_ksm_zero_pages_count (unsigned long zero_size) +{ +unsigned long pages_expected = zero_size / (4 * KiB); +char buf[20 ];
tools/testing/selftests/vm/ksm_functional_tests.c static bool range_maps_duplicates(char *addr, unsigned long size) · line 75
Quoted 10 lines
tools/testing/selftests/vm/ksm_functional_tests.c +10
-0+ssize_t read_size; +unsigned long ksm_zero_pages; + + 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 == pages_expected;
Better call this function "ksm_get_zero_pages", do it similarly to ksm_get_full_scans() and do the comparison in the kernel.
Quoted 10 lines
tools/testing/selftests/vm/ksm_functional_tests.c +2
-1+} +static long ksm_get_full_scans (void ) {char buf[10 ];@@ -70,15 +88,12 @@ static long ksm_get_full_scans(void)return strtol (buf,NULL ,10 ); } -static int ksm_merge (void )
tools/testing/selftests/vm/ksm_functional_tests.c static long ksm_get_full_scans(void) · line 97
Quoted 10 lines
tools/testing/selftests/vm/ksm_functional_tests.c +1
-3+static int wait_two_full_scans (void ) {long start_scans, end_scans; -/* Wait for two full scans such that any possible merging happened. */ start_scans =ksm_get_full_scans ();if (start_scans <0 ) -return start_scans; -if (write (ksm_fd,"1" ,1 ) !=1 )return -errno;
This change might not be required.
Quoted 9 lines
tools/testing/selftests/vm/ksm_functional_tests.c +3
-0do { end_scans = ksm_get_full_scans();@@ -89,6 +104,34 @@ static int ksm_merge(void)return 0 ; } +static inline int ksm_merge (void ) +{ +/* Wait for two full scans such that any possible merging happened. */
tools/testing/selftests/vm/ksm_functional_tests.c static int ksm_merge(void) · line 119
Quoted 10 lines
tools/testing/selftests/vm/ksm_functional_tests.c +10
-0+if (write (ksm_fd,"1" ,1 ) !=1 ) +return -errno; +return wait_two_full_scans (); +} + +static inline int make_cow (char *map,char val,unsigned long size) +{ + +memset (map, val, size); +return wait_two_full_scans ();
Why is that required?
tools/testing/selftests/vm/ksm_functional_tests.c static int ksm_merge(void) · line 132
Quoted 13 lines
tools/testing/selftests/vm/ksm_functional_tests.c +13
-0+} + +static int unmerge_zero_page (char *start,unsigned long size) +{ +int ret; + + ret =madvise (start, size,MADV_UNMERGEABLE ); +if (ret) { +ksft_test_result_fail ("MADV_UNMERGEABLE failed\n" ); +return ret; + } + +return wait_two_full_scans ();
Dito, why is that required when unmerging?
Quoted 24 lines
tools/testing/selftests/vm/ksm_functional_tests.c +17
-0+} +static char *mmap_and_merge_range (char val,unsigned long size) {char *map;@@ -146,6 +189,56 @@ static void test_unmerge(void)munmap (map, size); } +static void test_unmerge_zero_pages (void ) +{ +const unsigned int size =2 * MiB; +char *map; + +ksft_print_msg ("[RUN] %s\n" , __func__); + +/* Confirm the interfaces*/ + ksm_zero_pages_fd =open ("/sys/kernel/mm/ksm/zero_pages_sharing" ,O_RDONLY ); +if (ksm_zero_pages_fd <0 ) { +ksft_test_result_skip ("open(\"/sys/kernel/mm/ksm/zero_pages_sharing\") failed\n" ); +return ; + } + ksm_use_zero_pages_fd =open ("/sys/kernel/mm/ksm/use_zero_pages" ,O_RDWR ); +if (ksm_use_zero_pages_fd <0 ) {
tools/testing/selftests/vm/ksm_functional_tests.c static void test_unmerge(void) · line 216
Quoted 10 lines
tools/testing/selftests/vm/ksm_functional_tests.c +10
-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);
Not checking for map == MAP_FAILED. Best to follow what test_unmerge() does.
tools/testing/selftests/vm/ksm_functional_tests.c static void test_unmerge(void) · line 225
Quoted 9 lines
tools/testing/selftests/vm/ksm_functional_tests.c +9
-0+ +/* Case 1: make Writing on ksm zero pages (COW) */ +if (make_cow (map,0xcf , size /2 )) { +ksft_test_result_fail ("COW failed\n" ); + goto unmap; + } +ksft_test_result (check_ksm_zero_pages_count (size /2 ), +"zero page count react to cow\n" ); +
Let's keep COW tests to cow.c and focus on explicit unmerging here. Once we add support for KSM in cow.c , we can also test that e.g., long-term R/O pinning works as expected. I have that on my todo list but would be pleasantly surprised if someone else could tackle it :)
tools/testing/selftests/vm/ksm_functional_tests.c static void test_unmerge(void) · line 230
Quoted 5 lines
tools/testing/selftests/vm/ksm_functional_tests.c +5
-0+/* Case 2: Call madvise(xxx, MADV_UNMERGEABLE)*/ +if (unmerge_zero_page (map + size /2 , size /4 )) { +ksft_test_result_fail ("unmerge_zero_page failed\n" ); + goto unmap; + }
Just avoid that helper and do it like test_unmerge(). Keep it simple.
tools/testing/selftests/vm/ksm_functional_tests.c static void test_unmerge(void) · line 234
Quoted 4 lines
tools/testing/selftests/vm/ksm_functional_tests.c +4
-0+ksft_test_result (check_ksm_zero_pages_count (size /4 ), +"zero page count react to unmerge\n" ); + +/*Check if ksm pages are really unmerged */
Missing space at the beginning. + the comment is superfluous.
Quoted 11 lines
tools/testing/selftests/vm/ksm_functional_tests.c +7
-0+ksft_test_result (!range_maps_duplicates (map + size /2 , size /4 ), +"KSM zero pages were unmerged\n" ); + +unmap: +munmap (map, size); +} +static void test_unmerge_discarded (void ) {const unsigned int size =2 * MiB;
tools/testing/selftests/vm/ksm_functional_tests.c int main(int argc, char **argv) · line 357
Quoted 4 lines
tools/testing/selftests/vm/ksm_functional_tests.c +1
-0@@ -261,11 +354,13 @@ int main(int argc, char **argv) ksm_full_scans_fd =open ("/sys/kernel/mm/ksm/full_scans" ,O_RDONLY );if (ksm_full_scans_fd <0 )ksft_exit_skip ("open(\"/sys/kernel/mm/ksm/full_scans\") failed\n" ); +
Unrelated change.
Quoted 9 lines
tools/testing/selftests/vm/ksm_functional_tests.c +1
-0pagemap_fd =open ("/proc/self/pagemap" ,O_RDONLY );if (pagemap_fd <0 )ksft_exit_skip ("open(\"/proc/self/pagemap\") failed\n" );test_unmerge (); +test_unmerge_zero_pages ();test_unmerge_discarded ();#ifdef __NR_userfaultfdtest_unmerge_uffd_wp ();
Signature · 3 lines
Thanks, David / dhildenb