Message ID | 20230803143208.383663-7-david@redhat.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | smaps / mm/gup: fix gup_can_follow_protnone fallout | expand |
On Thu, Aug 03, 2023 at 04:32:07PM +0200, David Hildenbrand wrote: > Let's extend mmap_and_merge_range() to test if anything in the current > process was merged. range_maps_duplicates() is too unreliable for that > use case, so instead look at KSM stats. > > Trigger a complete unmerge first, to cleanup the stable tree and > stabilize accounting of merged pages. > > Note that we're using /proc/self/ksm_merging_pages instead of > /proc/self/ksm_stat, because that one is available in more existing > kernels. > > If /proc/self/ksm_merging_pages can't be opened, we can't perform any > checks and simply skip them. > > We have to special-case the shared zeropage for now. But the only user > -- test_unmerge_zero_pages() -- performs its own merge checks. > > Signed-off-by: David Hildenbrand <david@redhat.com> Acked-by: Peter Xu <peterx@redhat.com> One nitpick: > --- > .../selftests/mm/ksm_functional_tests.c | 47 +++++++++++++++++++ > 1 file changed, 47 insertions(+) > > diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c > index 0de9d33cd565..cb63b600cb4f 100644 > --- a/tools/testing/selftests/mm/ksm_functional_tests.c > +++ b/tools/testing/selftests/mm/ksm_functional_tests.c > @@ -30,6 +30,7 @@ > static int ksm_fd; > static int ksm_full_scans_fd; > static int proc_self_ksm_stat_fd; > +static int proc_self_ksm_merging_pages_fd; > static int ksm_use_zero_pages_fd; > static int pagemap_fd; > static size_t pagesize; > @@ -88,6 +89,22 @@ static long get_my_ksm_zero_pages(void) > 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; Better do the fds check all in main(), e.g. not all callers below considers negative values, so -1 can pass "if (get_my_merging_pages())" etc. > + > + 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); > +}
On 03.08.23 21:05, Peter Xu wrote: > On Thu, Aug 03, 2023 at 04:32:07PM +0200, David Hildenbrand wrote: >> Let's extend mmap_and_merge_range() to test if anything in the current >> process was merged. range_maps_duplicates() is too unreliable for that >> use case, so instead look at KSM stats. >> >> Trigger a complete unmerge first, to cleanup the stable tree and >> stabilize accounting of merged pages. >> >> Note that we're using /proc/self/ksm_merging_pages instead of >> /proc/self/ksm_stat, because that one is available in more existing >> kernels. >> >> If /proc/self/ksm_merging_pages can't be opened, we can't perform any >> checks and simply skip them. >> >> We have to special-case the shared zeropage for now. But the only user >> -- test_unmerge_zero_pages() -- performs its own merge checks. >> >> Signed-off-by: David Hildenbrand <david@redhat.com> > Hi Peter, thanks for the review! > Acked-by: Peter Xu <peterx@redhat.com> > > One nitpick: > >> --- >> .../selftests/mm/ksm_functional_tests.c | 47 +++++++++++++++++++ >> 1 file changed, 47 insertions(+) >> >> diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c >> index 0de9d33cd565..cb63b600cb4f 100644 >> --- a/tools/testing/selftests/mm/ksm_functional_tests.c >> +++ b/tools/testing/selftests/mm/ksm_functional_tests.c >> @@ -30,6 +30,7 @@ >> static int ksm_fd; >> static int ksm_full_scans_fd; >> static int proc_self_ksm_stat_fd; >> +static int proc_self_ksm_merging_pages_fd; >> static int ksm_use_zero_pages_fd; >> static int pagemap_fd; >> static size_t pagesize; >> @@ -88,6 +89,22 @@ static long get_my_ksm_zero_pages(void) >> 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; > > Better do the fds check all in main(), e.g. not all callers below considers > negative values, so -1 can pass "if (get_my_merging_pages())" etc. The two existing callers should be handling it correctly: if (get_my_merging_pages() > 0) -> fail if (val && !get_my_merging_pages() -> fail Both will pass on negative values, unless I am missing something. I tried to keep the test working also on older kernels where ksm_merging_pages does not exist yet (it's ~1 year old). Anyhow, if you think it's better to make the test fail on these setups, I can change it.
diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index 0de9d33cd565..cb63b600cb4f 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -30,6 +30,7 @@ static int ksm_fd; static int ksm_full_scans_fd; static int proc_self_ksm_stat_fd; +static int proc_self_ksm_merging_pages_fd; static int ksm_use_zero_pages_fd; static int pagemap_fd; static size_t pagesize; @@ -88,6 +89,22 @@ static long get_my_ksm_zero_pages(void) 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]; @@ -120,11 +137,29 @@ static int ksm_merge(void) return 0; } +static int ksm_unmerge(void) +{ + if (write(ksm_fd, "2", 1) != 1) + return -errno; + return 0; +} + static char *mmap_and_merge_range(char val, unsigned long size, bool use_prctl) { char *map; int ret; + /* Stabilize accounting by disabling KSM completely. */ + if (ksm_unmerge()) { + ksft_test_result_fail("Disabling (unmerging) KSM failed\n"); + goto unmap; + } + + if (get_my_merging_pages() > 0) { + ksft_test_result_fail("Still pages merged\n"); + goto unmap; + } + map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); if (map == MAP_FAILED) { @@ -160,6 +195,16 @@ static char *mmap_and_merge_range(char val, unsigned long size, bool use_prctl) ksft_test_result_fail("Running KSM failed\n"); goto unmap; } + + /* + * 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()) { + ksft_test_result_fail("No pages got merged\n"); + goto unmap; + } + return map; unmap: munmap(map, size); @@ -473,6 +518,8 @@ int main(int argc, char **argv) if (pagemap_fd < 0) ksft_exit_skip("open(\"/proc/self/pagemap\") failed\n"); proc_self_ksm_stat_fd = open("/proc/self/ksm_stat", O_RDONLY); + proc_self_ksm_merging_pages_fd = open("/proc/self/ksm_merging_pages", + O_RDONLY); ksm_use_zero_pages_fd = open("/sys/kernel/mm/ksm/use_zero_pages", O_RDWR); test_unmerge();
Let's extend mmap_and_merge_range() to test if anything in the current process was merged. range_maps_duplicates() is too unreliable for that use case, so instead look at KSM stats. Trigger a complete unmerge first, to cleanup the stable tree and stabilize accounting of merged pages. Note that we're using /proc/self/ksm_merging_pages instead of /proc/self/ksm_stat, because that one is available in more existing kernels. If /proc/self/ksm_merging_pages can't be opened, we can't perform any checks and simply skip them. We have to special-case the shared zeropage for now. But the only user -- test_unmerge_zero_pages() -- performs its own merge checks. Signed-off-by: David Hildenbrand <david@redhat.com> --- .../selftests/mm/ksm_functional_tests.c | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+)