mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tianyi Chen <hi@tychen.cc>
To: ljs@kernel.org, "Liam R . Howlett" <liam@infradead.org>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: Tianyi Chen <hi@tychen.cc>, Vlastimil Babka <vbabka@kernel.org>,
	Jann Horn <jannh@google.com>, Pedro Falcato <pfalcato@suse.de>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: [PATCH] tools/testing/vma: cover hole filling through __mmap_region()
Date: Sun,  6 Sep 2026 22:41:00 +0800	[thread overview]
Message-ID: <20260906144100.849288-1-hi@tychen.cc> (raw)

The mmap tests extend existing mappings one neighbor at a time, while
merge tests construct merge state directly. Neither exercises filling
a hole between compatible mappings through the mmap setup and completion
path.

Fill a gap through __mmap_region() and require both neighbors to merge
into one VMA. Repeat with only the new mapping's execute permission set
and require three separate VMAs. Check boundaries, permissions, page
offsets, map_count and tree lookups across the mapped pages.

The full VMA test suite passes all 28 tests with ASan and UBSan enabled.

Assisted-by: LLM
Signed-off-by: Tianyi Chen <hi@tychen.cc>
---
 tools/testing/vma/tests/mmap.c | 75 ++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/tools/testing/vma/tests/mmap.c b/tools/testing/vma/tests/mmap.c
index c85bc000d1c..b66ec932a75 100644
--- a/tools/testing/vma/tests/mmap.c
+++ b/tools/testing/vma/tests/mmap.c
@@ -45,7 +45,82 @@ static bool test_mmap_region_basic(void)
 	return true;
 }
 
+static bool mmap_region_fill_hole(bool merge)
+{
+	const vma_flags_t vma_flags = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,
+			VMA_MAYREAD_BIT, VMA_MAYWRITE_BIT, VMA_MAYEXEC_BIT);
+	vma_flags_t hole_flags = vma_flags;
+	struct mm_struct mm = {};
+	struct vm_area_struct *vma;
+	unsigned long addr;
+	int count = 0;
+	VMA_ITERATOR(vmi, &mm, 0);
+
+	current->mm = &mm;
+	if (!merge)
+		vma_flags_set(&hole_flags, VMA_EXEC_BIT);
+
+	/* Leave a hole between two otherwise mergeable mappings. */
+	addr = __mmap_region(NULL, 0x300000, 0x3000, vma_flags, 0x300, NULL);
+	ASSERT_EQ(addr, 0x300000);
+	addr = __mmap_region(NULL, 0x306000, 0x3000, vma_flags, 0x306, NULL);
+	ASSERT_EQ(addr, 0x306000);
+	ASSERT_EQ(mm.map_count, 2);
+	vma_iter_set(&vmi, 0x303000);
+	ASSERT_EQ(vma_iter_load(&vmi), NULL);
+	vma_iter_set(&vmi, 0x305fff);
+	ASSERT_EQ(vma_iter_load(&vmi), NULL);
+
+	/* A single flag difference must prevent merging with either neighbor. */
+	addr = __mmap_region(NULL, 0x303000, 0x3000, hole_flags, 0x303, NULL);
+	ASSERT_EQ(addr, 0x303000);
+	ASSERT_EQ(mm.map_count, merge ? 1 : 3);
+
+	vma_iter_set(&vmi, 0);
+	for_each_vma(vmi, vma) {
+		unsigned long start = 0x300000 + count * 0x3000;
+		unsigned long end = merge ? 0x309000 : start + 0x3000;
+		VMA_ITERATOR(lookup, &mm, start);
+
+		ASSERT_EQ(vma->vm_start, start);
+		ASSERT_EQ(vma->vm_end, end);
+		ASSERT_EQ(vma_start_pgoff(vma), start >> PAGE_SHIFT);
+		ASSERT_EQ(vma_start_anon_pgoff(vma), start >> PAGE_SHIFT);
+		ASSERT_TRUE(vma_test_all(vma, VMA_READ_BIT, VMA_WRITE_BIT,
+					VMA_MAYREAD_BIT, VMA_MAYWRITE_BIT,
+					VMA_MAYEXEC_BIT));
+		ASSERT_EQ(vma_test(vma, VMA_EXEC_BIT), !merge && count == 1);
+		for (addr = start; addr < end; addr += PAGE_SIZE) {
+			vma_iter_set(&lookup, addr);
+			ASSERT_EQ(vma_iter_load(&lookup), vma);
+			vma_iter_set(&lookup, addr + PAGE_SIZE - 1);
+			ASSERT_EQ(vma_iter_load(&lookup), vma);
+		}
+		count++;
+	}
+	ASSERT_EQ(count, mm.map_count);
+	vma_iter_set(&vmi, 0x2fffff);
+	ASSERT_EQ(vma_iter_load(&vmi), NULL);
+	vma_iter_set(&vmi, 0x309000);
+	ASSERT_EQ(vma_iter_load(&vmi), NULL);
+
+	ASSERT_EQ(cleanup_mm(&mm, &vmi), count);
+	return true;
+}
+
+static bool test_mmap_region_fill_hole_merge(void)
+{
+	return mmap_region_fill_hole(true);
+}
+
+static bool test_mmap_region_fill_hole_flags_mismatch(void)
+{
+	return mmap_region_fill_hole(false);
+}
+
 static void run_mmap_tests(int *num_tests, int *num_fail)
 {
 	TEST(mmap_region_basic);
+	TEST(mmap_region_fill_hole_merge);
+	TEST(mmap_region_fill_hole_flags_mismatch);
 }
-- 
2.55.0


             reply	other threads:[~2026-09-06 14:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-06 14:41 Tianyi Chen [this message]
2026-09-07  7:53 ` Lorenzo Stoakes (ARM)
2026-09-08  9:55   ` Tianyi Chen
2026-09-08  9:55 ` [PATCH v2] " Tianyi Chen
2026-09-08 10:21   ` Lorenzo Stoakes (ARM)

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=20260906144100.849288-1-hi@tychen.cc \
    --to=hi@tychen.cc \
    --cc=akpm@linux-foundation.org \
    --cc=jannh@google.com \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=pfalcato@suse.de \
    --cc=vbabka@kernel.org \
    /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

all inboxes | Powered by JetHome®