selftests/ksm: ksm_functional_tests: add prctl unmerge test
authorDavid Hildenbrand <david@redhat.com>
Sat, 22 Apr 2023 20:54:19 +0000 (22:54 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Wed, 3 May 2023 00:21:49 +0000 (17:21 -0700)
Let's test whether setting PR_SET_MEMORY_MERGE to 0 after setting it to 1
will unmerge pages, similar to how setting MADV_UNMERGEABLE after setting
MADV_MERGEABLE would.

Link: https://lkml.kernel.org/r/20230422205420.30372-3-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Acked-by: Stefan Roesch <shr@devkernel.io>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Claudio Imbrenda <imbrenda@linux.ibm.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Janosch Frank <frankja@linux.ibm.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Rik van Riel <riel@surriel.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
tools/testing/selftests/mm/ksm_functional_tests.c

index 7bc9fc1..26853ba 100644 (file)
@@ -91,9 +91,10 @@ static int ksm_merge(void)
        return 0;
 }
 
        return 0;
 }
 
-static char *mmap_and_merge_range(char val, unsigned long size)
+static char *mmap_and_merge_range(char val, unsigned long size, bool use_prctl)
 {
        char *map;
 {
        char *map;
+       int ret;
 
        map = mmap(NULL, size, PROT_READ|PROT_WRITE,
                   MAP_PRIVATE|MAP_ANON, -1, 0);
 
        map = mmap(NULL, size, PROT_READ|PROT_WRITE,
                   MAP_PRIVATE|MAP_ANON, -1, 0);
@@ -110,7 +111,17 @@ static char *mmap_and_merge_range(char val, unsigned long size)
 
        /* Make sure each page contains the same values to merge them. */
        memset(map, val, size);
 
        /* Make sure each page contains the same values to merge them. */
        memset(map, val, size);
-       if (madvise(map, size, MADV_MERGEABLE)) {
+
+       if (use_prctl) {
+               ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0);
+               if (ret < 0 && errno == EINVAL) {
+                       ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n");
+                       goto unmap;
+               } else if (ret) {
+                       ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 failed\n");
+                       goto unmap;
+               }
+       } else if (madvise(map, size, MADV_MERGEABLE)) {
                ksft_test_result_fail("MADV_MERGEABLE failed\n");
                goto unmap;
        }
                ksft_test_result_fail("MADV_MERGEABLE failed\n");
                goto unmap;
        }
@@ -133,7 +144,7 @@ static void test_unmerge(void)
 
        ksft_print_msg("[RUN] %s\n", __func__);
 
 
        ksft_print_msg("[RUN] %s\n", __func__);
 
-       map = mmap_and_merge_range(0xcf, size);
+       map = mmap_and_merge_range(0xcf, size, false);
        if (map == MAP_FAILED)
                return;
 
        if (map == MAP_FAILED)
                return;
 
@@ -155,7 +166,7 @@ static void test_unmerge_discarded(void)
 
        ksft_print_msg("[RUN] %s\n", __func__);
 
 
        ksft_print_msg("[RUN] %s\n", __func__);
 
-       map = mmap_and_merge_range(0xcf, size);
+       map = mmap_and_merge_range(0xcf, size, false);
        if (map == MAP_FAILED)
                return;
 
        if (map == MAP_FAILED)
                return;
 
@@ -187,7 +198,7 @@ static void test_unmerge_uffd_wp(void)
 
        ksft_print_msg("[RUN] %s\n", __func__);
 
 
        ksft_print_msg("[RUN] %s\n", __func__);
 
-       map = mmap_and_merge_range(0xcf, size);
+       map = mmap_and_merge_range(0xcf, size, false);
        if (map == MAP_FAILED)
                return;
 
        if (map == MAP_FAILED)
                return;
 
@@ -323,9 +334,31 @@ static void test_prctl_fork(void)
        ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n");
 }
 
        ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n");
 }
 
+static void test_prctl_unmerge(void)
+{
+       const unsigned int size = 2 * MiB;
+       char *map;
+
+       ksft_print_msg("[RUN] %s\n", __func__);
+
+       map = mmap_and_merge_range(0xcf, size, true);
+       if (map == MAP_FAILED)
+               return;
+
+       if (prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0)) {
+               ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 failed\n");
+               goto unmap;
+       }
+
+       ksft_test_result(!range_maps_duplicates(map, size),
+                        "Pages were unmerged\n");
+unmap:
+       munmap(map, size);
+}
+
 int main(int argc, char **argv)
 {
 int main(int argc, char **argv)
 {
-       unsigned int tests = 4;
+       unsigned int tests = 5;
        int err;
 
 #ifdef __NR_userfaultfd
        int err;
 
 #ifdef __NR_userfaultfd
@@ -355,6 +388,7 @@ int main(int argc, char **argv)
 
        test_prctl();
        test_prctl_fork();
 
        test_prctl();
        test_prctl_fork();
+       test_prctl_unmerge();
 
        err = ksft_get_fail_cnt();
        if (err)
 
        err = ksft_get_fail_cnt();
        if (err)