* [PATCH] mm/nommu: keep the maple tree in step when mremap() resizes in place
@ 2026-11-21 8:31 Brad Hawthorne
2026-11-23 11:41 ` Hajime Tazaki
0 siblings, 1 reply; 2+ messages in thread
From: Brad Hawthorne @ 2026-11-21 8:31 UTC (permalink / raw)
To: Andrew Morton
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Jann Horn,
Pedro Falcato, David Hildenbrand, Suren Baghdasaryan,
Shakeel Butt, linux-mm, Brad Hawthorne, Hajime Tazaki, stable
On !MMU, do_mremap() resizes a VMA by writing vma->vm_end and nothing
else, so mm->mm_mt keeps the old extent.
After a shrink, [new_end, old_end) in the tree still points at the VMA.
When the VMA is unmapped, delete_vma_from_mm() clears only
[vm_start, vm_end), so that range survives and points at the freed
vm_area_struct. Lookups in the range then read freed memory (after the
munmap(), /proc/self/maps still lists it), and exit_mmap() frees the
VMA a second time. An unprivileged process is enough:
long pg = sysconf(_SC_PAGESIZE);
char *p = mmap(NULL, 8 * pg, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
mremap(p, 8 * pg, 4 * pg, 0);
munmap(p, 4 * pg);
_exit(0);
It was first seen on an Adafruit Fruit Jam board (RP2350, Cortex-M33)
running 7.3.1. There, three rounds of the program's mmap, mremap and
munmap leave the process's vsize at 2133 KB instead of 64 KB, and the
next exec faults the kernel, in vm_area_alloc() or __kernel_read()
under do_mmap(): the double free has corrupted the slab. With this
patch on the same kernel, 11 of 11 runs keep the vsize at 64 KB and
the board runs on.
On mm-unstable at the base commit below, under QEMU mps2-an385
(mps2_defconfig plus SLUB_DEBUG_ON), run as uid 65534:
BUG vm_area_struct (Not tainted): Object already free
Allocated in vm_area_alloc+0x10/0x38 age=0 cpu=0 pid=20
Freed in do_munmap+0x2ec/0x2fc age=0 cpu=0 pid=20
kmem_cache_free from exit_mmap+0x7c/0x8c
exit_mmap from mmput+0x2a/0x74
mmput from do_exit+0x1f8/0x520
Clear the range given up on a shrink. Once a shrink clears it, a later
grow back into the region must store the VMA over its new extent, or
find_vma() would miss addresses that are mapped; so store it there on a
grow. Shared and overlay mappings are refused above, so the region is
this VMA's alone, and the grow is bounded by its size; but the range a
shrink gave up may since have been mapped again, by a direct mapping of
device memory at those addresses, so a grow into another mapping fails
with -ENOMEM. As before, a shrink keeps the region's pages, which is
what lets a grow back reuse them.
With this patch the program above exits cleanly, with and without
DEBUG_VM_MAPLE_TREE (nommu has no tree validation of its own, so that
adds only the store's warnings). A shrink, grow and partial munmap()
sequence also passes, and fails with the shrink half alone, which is
why the grow half is needed.
524e00b36e8c is where nommu find_vma() started trusting the maple tree;
the exit_mmap() double free above follows from 8220543df148 ("nommu:
remove uses of VMA linked list") in the same v6.1 series.
Not changed here: a private file mapping's place in the file's i_mmap
tree also goes stale on a resize. That predates the maple tree and is
left to a separate patch.
Sashiko's review of Hajime Tazaki's nommu series found the stale range
in July, and his fixes and RFC since then cover it [1][2]; review asked
for a minimal fix-only patch with a Fixes: tag [3]. This is a minimal
version of that fix.
The consequence was reproduced and this patch drafted with an AI coding
assistant (Claude, Anthropic): it reviewed mm/nommu.c, wrote the
reproducer and ran the board and QEMU tests; I checked the result.
Stable backports need adjusting: 6.6.y and 6.12.y lack
vma_iter_store_overwrite(), and 6.1.y nommu uses the ma_state API.
Link: https://lore.kernel.org/linux-mm/20260710021028.892645-1-thehajime@gmail.com/ [1]
Link: https://lore.kernel.org/linux-mm/20260813063401.1786548-2-thehajime@gmail.com/ [2]
Link: https://lore.kernel.org/linux-mm/an77pELeQdW-sTEi@lucifer/ [3]
Fixes: 524e00b36e8c ("mm: remove rb tree.")
Cc: Hajime Tazaki <thehajime@gmail.com>
Cc: stable@vger.kernel.org
Signed-off-by: Brad Hawthorne <brad@sim.vu>
---
Hajime, as you asked, here it is as a separate patch with you on Cc.
Earlier note, and his reply: https://lore.kernel.org/linux-mm/20261120221021.789792-1-brad@sim.vu/
mm/nommu.c | 34 ++++++++++++++++++++++++++++++++--
1 file changed, 32 insertions(+), 2 deletions(-)
diff --git a/mm/nommu.c b/mm/nommu.c
index df99495c8..9221cd241 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -1575,6 +1575,7 @@ static unsigned long do_mremap(unsigned long addr,
unsigned long flags, unsigned long new_addr)
{
struct vm_area_struct *vma;
+ unsigned long new_end;
/* insanity checks first */
old_len = PAGE_ALIGN(old_len);
@@ -1601,8 +1602,37 @@ static unsigned long do_mremap(unsigned long addr,
if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
return (unsigned long) -ENOMEM;
- /* all checks complete - do it */
- vma->vm_end = vma->vm_start + new_len;
+ /*
+ * All checks complete - do it. The maple tree must cover exactly the
+ * new extent: a range given up but left in the tree still maps to this
+ * VMA, and once the VMA is unmapped and freed, to freed memory.
+ */
+ new_end = vma->vm_start + new_len;
+ if (new_len < old_len) {
+ VMA_ITERATOR(vmi, vma->vm_mm, new_end);
+
+ if (vma_iter_clear_gfp(&vmi, new_end, vma->vm_end, GFP_KERNEL))
+ return (unsigned long) -ENOMEM;
+ vma->vm_end = new_end;
+ } else if (new_len > old_len) {
+ VMA_ITERATOR(vmi, vma->vm_mm, vma->vm_start);
+
+ /*
+ * Shared and overlay mappings were refused above, so the
+ * region is this VMA's alone and still holds its pages: the
+ * grow, bounded by the region's size, stays inside it. The
+ * range a shrink gave up may since have been mapped again,
+ * though: a direct mapping of device memory can take any
+ * address.
+ */
+ if (find_vma_intersection(vma->vm_mm, vma->vm_end, new_end))
+ return (unsigned long) -ENOMEM;
+ vma_iter_config(&vmi, vma->vm_start, new_end);
+ if (vma_iter_prealloc(&vmi, vma))
+ return (unsigned long) -ENOMEM;
+ vma->vm_end = new_end;
+ vma_iter_store_overwrite(&vmi, vma);
+ }
return vma->vm_start;
}
base-commit: d12eccbcb79a4fa35c3aa588eb79056af62eb859
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] mm/nommu: keep the maple tree in step when mremap() resizes in place
2026-11-21 8:31 [PATCH] mm/nommu: keep the maple tree in step when mremap() resizes in place Brad Hawthorne
@ 2026-11-23 11:41 ` Hajime Tazaki
0 siblings, 0 replies; 2+ messages in thread
From: Hajime Tazaki @ 2026-11-23 11:41 UTC (permalink / raw)
To: brad
Cc: akpm, ljs, liam, vbabka, jannh, pfalcato, david, surenb,
shakeel.butt, linux-mm, stable
[-- Attachment #1: Type: text/plain, Size: 1755 bytes --]
Hello Brad,
On Sat, 21 Nov 2026 17:31:39 +0900,
Brad Hawthorne wrote:
>
> On !MMU, do_mremap() resizes a VMA by writing vma->vm_end and nothing
> else, so mm->mm_mt keeps the old extent.
(snip)
> Sashiko's review of Hajime Tazaki's nommu series found the stale range
> in July, and his fixes and RFC since then cover it [1][2]; review asked
> for a minimal fix-only patch with a Fixes: tag [3]. This is a minimal
> version of that fix.
>
> The consequence was reproduced and this patch drafted with an AI coding
> assistant (Claude, Anthropic): it reviewed mm/nommu.c, wrote the
> reproducer and ran the board and QEMU tests; I checked the result.
> Stable backports need adjusting: 6.6.y and 6.12.y lack
> vma_iter_store_overwrite(), and 6.1.y nommu uses the ma_state API.
>
> Link: https://lore.kernel.org/linux-mm/20260710021028.892645-1-thehajime@gmail.com/ [1]
> Link: https://lore.kernel.org/linux-mm/20260813063401.1786548-2-thehajime@gmail.com/ [2]
> Link: https://lore.kernel.org/linux-mm/an77pELeQdW-sTEi@lucifer/ [3]
> Fixes: 524e00b36e8c ("mm: remove rb tree.")
> Cc: Hajime Tazaki <thehajime@gmail.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Brad Hawthorne <brad@sim.vu>
> ---
> Hajime, as you asked, here it is as a separate patch with you on Cc.
> Earlier note, and his reply: https://lore.kernel.org/linux-mm/20261120221021.789792-1-brad@sim.vu/
my earlier patchset contains a kselftest case, which tried to test the
issue which your patch addressed. Part of kselftest is now upstreamed
and I would add the additional one specifically for the case of this
issue (this patch addressed).
Attached is locally tested kselftest with your patch. Would be nice
to include this in your next spin as a part of patchset.
-- Hajime
[-- Attachment #2: 0001-selftests-nommu-test-mremap-grow-after-shrink.patch --]
[-- Type: text/plain, Size: 3148 bytes --]
From 608869187e388e16e4c2b2caf5a174c3a424dd7b Mon Sep 17 00:00:00 2001
From: Hajime Tazaki <thehajime@gmail.com>
Date: Mon, 23 Nov 2026 20:24:08 +0900
Subject: [PATCH] selftests/nommu: test mremap grow after shrink
The existing mremap shrink test covers dropping the old extent. Growing
a shortened VMA must also store its restored extent in the maple tree.
Shrink an anonymous mapping, grow it back, then partially unmap the
regrown tail and the remainder to exercise that tree update.
Signed-off-by: Hajime Tazaki <thehajime@gmail.com>
---
.../selftests/nommu/nommu_mremap_test.c | 58 ++++++++++++++++++-
1 file changed, 57 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/nommu/nommu_mremap_test.c b/tools/testing/selftests/nommu/nommu_mremap_test.c
index 6ee89cb25f61..550c21fadbf4 100644
--- a/tools/testing/selftests/nommu/nommu_mremap_test.c
+++ b/tools/testing/selftests/nommu/nommu_mremap_test.c
@@ -133,6 +133,61 @@ static void mremap_shrink_test(void)
ksft_test_result_pass("mremap shrink\n");
}
+/* mremap_shrink_test() covers stale entries after shrink; test regrowth here. */
+static void mremap_shrink_grow_test(void)
+{
+ void *addr, *addr2;
+ int cleanup, head, tail;
+
+ if (!nommu) {
+ ksft_test_result_skip("NOMMU only\n");
+ return;
+ }
+
+ addr = mmap(NULL, ps * 8, PROT_READ | PROT_WRITE,
+ MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+ if (addr == MAP_FAILED) {
+ ksft_test_result_error("mmap failed: %s\n", strerror(errno));
+ return;
+ }
+
+ addr2 = mremap(addr, ps * 8, ps * 4, 0);
+ if (addr2 == MAP_FAILED) {
+ ksft_test_result_fail("mremap shrink failed: %s\n", strerror(errno));
+ if (munmap(addr, ps * 8))
+ ksft_print_msg("cleanup after shrink failure failed: %s\n",
+ strerror(errno));
+ return;
+ }
+
+ addr = mremap(addr2, ps * 4, ps * 8, 0);
+ if (addr == MAP_FAILED) {
+ ksft_test_result_fail("mremap grow failed: %s\n", strerror(errno));
+ if (munmap(addr2, ps * 4))
+ ksft_print_msg("cleanup after grow failure failed: %s\n",
+ strerror(errno));
+ return;
+ }
+ if (munmap(addr + ps * 4, ps * 4)) {
+ ksft_test_result_fail("munmap grown range failed: %s\n", strerror(errno));
+ cleanup = munmap(addr, ps * 8);
+ if (cleanup) {
+ head = munmap(addr, ps * 4);
+ tail = munmap(addr + ps * 4, ps * 4);
+ cleanup = head || tail;
+ }
+ if (cleanup)
+ ksft_print_msg("cleanup after munmap failure failed: %s\n",
+ strerror(errno));
+ return;
+ }
+ if (munmap(addr, ps * 4)) {
+ ksft_test_result_fail("munmap failed: %s\n", strerror(errno));
+ return;
+ }
+ ksft_test_result_pass("mremap shrink and grow\n");
+}
+
static int get_shared_writable_file_expected_error(const char *path)
{
if (nommu && get_fs_type(path) == RAMFS_MAGIC)
@@ -344,10 +399,11 @@ int main(int argc, char **argv)
ps = sysconf(_SC_PAGESIZE);
ksft_print_header();
- ksft_set_plan(ARRAY_SIZE(mremap_cases) + 2);
+ ksft_set_plan(ARRAY_SIZE(mremap_cases) + 3);
munmap_shrink_test();
mremap_shrink_test();
+ mremap_shrink_grow_test();
for (i = 0; i < (int)ARRAY_SIZE(mremap_cases); i++)
run_mremap_test(&mremap_cases[i]);
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-23 11:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 8:31 [PATCH] mm/nommu: keep the maple tree in step when mremap() resizes in place Brad Hawthorne
2026-11-23 11:41 ` Hajime Tazaki
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox