From: Chengming Zhou <chengming.zhou@linux.dev>
To: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
"Liam R . Howlett" <Liam.Howlett@oracle.com>,
Vlastimil Babka <vbabka@suse.cz>, Jann Horn <jannh@google.com>,
Pedro Falcato <pfalcato@suse.de>,
David Hildenbrand <david@redhat.com>,
Xu Xin <xu.xin16@zte.com.cn>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH 4/4] tools/testing/selftests: add VMA merge tests for KSM merge
Date: Wed, 21 May 2025 16:07:29 +0800 [thread overview]
Message-ID: <34bd0faf-30b9-41f1-a768-0ed7165b4b98@linux.dev> (raw)
In-Reply-To: <95db1783c752fd4032fc0e81431afe7e6d128630.1747431920.git.lorenzo.stoakes@oracle.com>
On 2025/5/19 16:51, Lorenzo Stoakes wrote:
> 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!
>
> 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>
Thanks!
> ---
> tools/testing/selftests/mm/merge.c | 78 ++++++++++++++++++++++++++++++
> 1 file changed, 78 insertions(+)
>
> diff --git a/tools/testing/selftests/mm/merge.c b/tools/testing/selftests/mm/merge.c
> index c76646cdf6e6..2380a5a6a529 100644
> --- a/tools/testing/selftests/mm/merge.c
> +++ b/tools/testing/selftests/mm/merge.c
> @@ -2,10 +2,12 @@
>
> #define _GNU_SOURCE
> #include "../kselftest_harness.h"
> +#include <linux/prctl.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <sys/mman.h>
> +#include <sys/prctl.h>
> #include <sys/wait.h>
> #include "vm_util.h"
>
> @@ -31,6 +33,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)
> @@ -452,4 +459,75 @@ TEST_F(merge, forked_source_vma)
> ASSERT_EQ(procmap->query.vma_end, (unsigned long)ptr2 + 5 * page_size);
> }
>
> +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
next prev parent reply other threads:[~2025-05-21 8:07 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-19 8:51 [PATCH 0/4] mm: ksm: prevent KSM from entirely breaking VMA merging Lorenzo Stoakes
2025-05-19 8:51 ` [PATCH 1/4] mm: ksm: have KSM VMA checks not require a VMA pointer Lorenzo Stoakes
2025-05-19 17:40 ` David Hildenbrand
2025-05-20 3:14 ` Chengming Zhou
2025-05-19 8:51 ` [PATCH 2/4] mm: ksm: refer to special VMAs via VM_SPECIAL in ksm_compatible() Lorenzo Stoakes
2025-05-19 17:41 ` David Hildenbrand
2025-05-20 3:15 ` Chengming Zhou
2025-05-19 8:51 ` [PATCH 3/4] mm: prevent KSM from completely breaking VMA merging Lorenzo Stoakes
2025-05-19 13:08 ` Chengming Zhou
2025-05-19 13:13 ` Lorenzo Stoakes
2025-05-19 13:19 ` kernel test robot
2025-05-19 13:36 ` Lorenzo Stoakes
2025-05-19 18:00 ` David Hildenbrand
2025-05-19 18:04 ` David Hildenbrand
2025-05-19 19:02 ` Lorenzo Stoakes
2025-05-19 19:11 ` David Hildenbrand
2025-05-19 19:26 ` Lorenzo Stoakes
2025-05-19 19:29 ` David Hildenbrand
2025-05-19 18:52 ` Lorenzo Stoakes
2025-05-19 18:59 ` David Hildenbrand
2025-05-19 19:14 ` Lorenzo Stoakes
2025-05-19 19:18 ` Lorenzo Stoakes
2025-05-19 19:28 ` David Hildenbrand
2025-05-19 21:57 ` Andrew Morton
2025-05-20 5:25 ` Lorenzo Stoakes
2025-05-20 3:55 ` Chengming Zhou
2025-05-20 5:24 ` Lorenzo Stoakes
2025-05-19 8:51 ` [PATCH 4/4] tools/testing/selftests: add VMA merge tests for KSM merge Lorenzo Stoakes
2025-05-21 8:07 ` Chengming Zhou [this message]
2025-05-21 8:10 ` Lorenzo Stoakes
2025-05-19 11:53 ` [PATCH 0/4] mm: ksm: prevent KSM from entirely breaking VMA merging David Hildenbrand
2025-05-19 11:56 ` Lorenzo Stoakes
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=34bd0faf-30b9-41f1-a768-0ed7165b4b98@linux.dev \
--to=chengming.zhou@linux.dev \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=david@redhat.com \
--cc=jack@suse.cz \
--cc=jannh@google.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=pfalcato@suse.de \
--cc=vbabka@suse.cz \
--cc=viro@zeniv.linux.org.uk \
--cc=xu.xin16@zte.com.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox