linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Wei Yang <richard.weiyang@gmail.com>
To: akpm@linux-foundation.org
Cc: david@redhat.com, lorenzo.stoakes@oracle.com, riel@surriel.com,
	vbabka@suse.cz, harry.yoo@oracle.com, jannh@google.com,
	baohua@kernel.org, linux-mm@kvack.org,
	Wei Yang <richard.weiyang@gmail.com>
Subject: [RFC Patch 5/5] anon_vma: add test to assert no double-reuse
Date: Tue, 29 Apr 2025 09:06:39 +0000	[thread overview]
Message-ID: <20250429090639.784-6-richard.weiyang@gmail.com> (raw)
In-Reply-To: <20250429090639.784-1-richard.weiyang@gmail.com>

commit 2555283eb40d ("mm/rmap: Fix anon_vma->degree ambiguity leading to
double-reuse") fixed anon_vma double-reuse issue introduced by commit
7a3ef208e662 ("mm: prevent endless growth of anon_vma hierarchy").

Add a test case to assert no double-reuse.

Signed-off-by: Wei Yang <richard.weiyang@gmail.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Jann Horn <jannh@google.com>
Cc: Rik van Riel <riel@surriel.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Harry Yoo <harry.yoo@oracle.com>
---
 tools/testing/anon_vma/anon_vma.c | 47 ++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)

diff --git a/tools/testing/anon_vma/anon_vma.c b/tools/testing/anon_vma/anon_vma.c
index 495cd02ea661..633a62f5acba 100644
--- a/tools/testing/anon_vma/anon_vma.c
+++ b/tools/testing/anon_vma/anon_vma.c
@@ -558,7 +558,7 @@ static bool test_mergeable_vma(void)
 
 static bool test_reuse_anon_vma(void)
 {
-	struct vm_area_struct *root_vma, *vma, *vma1, *vma2;
+	struct vm_area_struct *root_vma, *vma, *vma1, *vma2, *vma3;
 	struct anon_vma *root_anon_vma, *reused_anon_vma;
 	struct anon_vma_chain *avc;
 
@@ -690,6 +690,51 @@ static bool test_reuse_anon_vma(void)
 		ASSERT_TRUE(avc->vma == vma1 || avc->vma == vma2);
 	}
 
+	/* Fork from vma2 */
+	/*
+	 * When commit 7a3ef208e662 ("mm: prevent endless growth of anon_vma
+	 * hierarchy") introduce anon_vma reuse, it embedded an issue of
+	 * double-reuse.
+	 *
+	 * It happens when vma2 reuse an existing anon_vma and we fork from
+	 * vma2 later. Before commit 2555283eb40d ("mm/rmap: Fix
+	 * anon_vma->degree ambiguity leading to double-reuse"), the forked
+	 * vma3 would reuse vma1->anon_vma, which is already in use.
+	 *
+	 * The following case assert vma1->anon_vma will not double-reuse.
+	 *
+	 *  root_anon_vma
+	 *  +-----------+
+	 *  |           |
+	 *  +-----------+
+	 *                \
+	 *                |
+	 *                \
+	 *                |\   vma1
+	 *                | \  +-----------+
+	 *                |  > |         av| != root_anon_vma
+	 *                |    +-----------+
+	 *                \
+	 *                |\   vma2
+	 *                | \  +-----------+
+	 *                |  > |         av| == reused_anon_vma
+	 *                |    +-----------+
+	 *                \
+	 *                 \   vma3
+	 *                  \  +-----------+
+	 *                   > |         av| != vma1->anon_vma
+	 *                     +-----------+
+	 */
+	/* vma1->anon_vma already has active vma */
+	ASSERT_NE(NULL, vma1->anon_vma);
+	vma3 = alloc_vma(0x3000, 0x5000, 3);
+	anon_vma_fork(vma3, vma2);
+	ASSERT_NE(NULL, vma3->anon_vma);
+	/* Root is root_vma->anon_vma */
+	ASSERT_EQ(vma3->anon_vma->root, root_anon_vma);
+	/* vma1->anon_vma is NOT reused here */
+	ASSERT_NE(vma3->anon_vma, vma1->anon_vma);
+
 	cleanup();
 
 	ASSERT_EQ(0, nr_allocated);
-- 
2.34.1



  parent reply	other threads:[~2025-04-29  9:07 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-29  9:06 [RFC Patch 0/5] Make anon_vma operations testable Wei Yang
2025-04-29  9:06 ` [RFC Patch 1/5] mm: move anon_vma manipulation functions to own file Wei Yang
2025-04-29  9:06 ` [RFC Patch 2/5] anon_vma: add skeleton code for userland testing of anon_vma logic Wei Yang
2025-05-01  1:31   ` Wei Yang
2025-05-01  9:41     ` Lorenzo Stoakes
2025-05-01 14:45       ` Wei Yang
2025-04-29  9:06 ` [RFC Patch 3/5] anon_vma: add test for mergeable anon_vma Wei Yang
2025-04-29  9:06 ` [RFC Patch 4/5] anon_vma: add test for reusable anon_vma Wei Yang
2025-04-29  9:06 ` Wei Yang [this message]
2025-04-29  9:31 ` [RFC Patch 0/5] Make anon_vma operations testable Lorenzo Stoakes
2025-04-29  9:38   ` David Hildenbrand
2025-04-29  9:41     ` Lorenzo Stoakes
2025-04-29 23:56       ` Wei Yang
2025-04-30  7:47         ` David Hildenbrand
2025-04-30 15:44           ` Wei Yang
2025-04-30 21:36             ` David Hildenbrand
2025-05-14  1:23           ` Wei Yang
2025-05-27  6:34             ` Wei Yang
2025-05-27 11:31               ` David Hildenbrand
2025-05-28  1:17                 ` Wei Yang
2025-05-30  2:11                 ` Wei Yang
2025-05-30  8:00                   ` David Hildenbrand
2025-05-30 14:05                     ` Wei Yang
2025-05-30 14:39                       ` David Hildenbrand
2025-05-30 23:23                         ` Wei Yang
2025-06-03 21:31                           ` David Hildenbrand
2025-04-29 23:15   ` Wei Yang
2025-04-30 14:38     ` Lorenzo Stoakes
2025-04-30 15:41       ` Wei Yang

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=20250429090639.784-6-richard.weiyang@gmail.com \
    --to=richard.weiyang@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=david@redhat.com \
    --cc=harry.yoo@oracle.com \
    --cc=jannh@google.com \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=riel@surriel.com \
    --cc=vbabka@suse.cz \
    /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