]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/testing/selftests: add VMA merge tests for KSM merge
authorLorenzo Stoakes <lorenzo.stoakes@oracle.com>
Thu, 29 May 2025 17:15:48 +0000 (18:15 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Thu, 10 Jul 2025 05:41:54 +0000 (22:41 -0700)
Add test to assert that we have now allowed merging of VMAs when KSM
merging-by-default has been set by prctl(PR_SET_MEMORY_MERGE, ...).

We simply perform a trivial mapping of adjacent VMAs expecting a merge,
however prior to recent changes implementing this mode earlier than
before, these merges would not have succeeded.

Assert that we have fixed this!

Link: https://lkml.kernel.org/r/6dec7aabf062c6b121cfac992c9c716cefdda00c.1748537921.git.lorenzo.stoakes@oracle.com
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev>
Tested-by: Chengming Zhou <chengming.zhou@linux.dev>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christian Brauner <brauner@kernel.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Jann Horn <jannh@google.com>
Cc: Stefan Roesch <shr@devkernel.io>
Cc: Xu Xin <xu.xin16@zte.com.cn>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
tools/testing/selftests/mm/merge.c

index cc26480098ae8776e29713f2918e3466c5daf327..150dd5baed2b46027368fa4272a9cc151dbbda57 100644 (file)
@@ -2,11 +2,13 @@
 
 #define _GNU_SOURCE
 #include "../kselftest_harness.h"
+#include <linux/prctl.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/mman.h>
+#include <sys/prctl.h>
 #include <sys/syscall.h>
 #include <sys/wait.h>
 #include <linux/perf_event.h>
@@ -34,6 +36,11 @@ FIXTURE_TEARDOWN(merge)
 {
        ASSERT_EQ(munmap(self->carveout, 12 * self->page_size), 0);
        ASSERT_EQ(close_procmap(&self->procmap), 0);
+       /*
+        * Clear unconditionally, as some tests set this. It is no issue if this
+        * fails (KSM may be disabled for instance).
+        */
+       prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0);
 }
 
 TEST_F(merge, mprotect_unfaulted_left)
@@ -498,4 +505,75 @@ out:
        remove(probe_file);
 }
 
+TEST_F(merge, ksm_merge)
+{
+       unsigned int page_size = self->page_size;
+       char *carveout = self->carveout;
+       struct procmap_fd *procmap = &self->procmap;
+       char *ptr, *ptr2;
+       int err;
+
+       /*
+        * Map two R/W immediately adjacent to one another, they should
+        * trivially merge:
+        *
+        * |-----------|-----------|
+        * |    R/W    |    R/W    |
+        * |-----------|-----------|
+        *      ptr         ptr2
+        */
+
+       ptr = mmap(&carveout[page_size], page_size, PROT_READ | PROT_WRITE,
+                  MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
+       ASSERT_NE(ptr, MAP_FAILED);
+       ptr2 = mmap(&carveout[2 * page_size], page_size,
+                   PROT_READ | PROT_WRITE,
+                   MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
+       ASSERT_NE(ptr2, MAP_FAILED);
+       ASSERT_TRUE(find_vma_procmap(procmap, ptr));
+       ASSERT_EQ(procmap->query.vma_start, (unsigned long)ptr);
+       ASSERT_EQ(procmap->query.vma_end, (unsigned long)ptr + 2 * page_size);
+
+       /* Unmap the second half of this merged VMA. */
+       ASSERT_EQ(munmap(ptr2, page_size), 0);
+
+       /* OK, now enable global KSM merge. We clear this on test teardown. */
+       err = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0);
+       if (err == -1) {
+               int errnum = errno;
+
+               /* Only non-failure case... */
+               ASSERT_EQ(errnum, EINVAL);
+               /* ...but indicates we should skip. */
+               SKIP(return, "KSM memory merging not supported, skipping.");
+       }
+
+       /*
+        * Now map a VMA adjacent to the existing that was just made
+        * VM_MERGEABLE, this should merge as well.
+        */
+       ptr2 = mmap(&carveout[2 * page_size], page_size,
+                   PROT_READ | PROT_WRITE,
+                   MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
+       ASSERT_NE(ptr2, MAP_FAILED);
+       ASSERT_TRUE(find_vma_procmap(procmap, ptr));
+       ASSERT_EQ(procmap->query.vma_start, (unsigned long)ptr);
+       ASSERT_EQ(procmap->query.vma_end, (unsigned long)ptr + 2 * page_size);
+
+       /* Now this VMA altogether. */
+       ASSERT_EQ(munmap(ptr, 2 * page_size), 0);
+
+       /* Try the same operation as before, asserting this also merges fine. */
+       ptr = mmap(&carveout[page_size], page_size, PROT_READ | PROT_WRITE,
+                  MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
+       ASSERT_NE(ptr, MAP_FAILED);
+       ptr2 = mmap(&carveout[2 * page_size], page_size,
+                   PROT_READ | PROT_WRITE,
+                   MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
+       ASSERT_NE(ptr2, MAP_FAILED);
+       ASSERT_TRUE(find_vma_procmap(procmap, ptr));
+       ASSERT_EQ(procmap->query.vma_start, (unsigned long)ptr);
+       ASSERT_EQ(procmap->query.vma_end, (unsigned long)ptr + 2 * page_size);
+}
+
 TEST_HARNESS_MAIN