From: Hajime Tazaki <thehajime@gmail.com>
To: akpm@linux-foundation.org, liam@infradead.org, ljs@kernel.org,
vbabka@kernel.org, jannh@google.com, pfalcato@suse.de,
linux-mm@kvack.org, geert@linux-m68k.org
Cc: linux-kernel@vger.kernel.org, Hajime Tazaki <thehajime@gmail.com>
Subject: [PATCH] mm: nommu: free unused resources when mremap shrinks the vma
Date: Fri, 10 Jul 2026 11:10:28 +0900 [thread overview]
Message-ID: <20260710021028.892645-1-thehajime@gmail.com> (raw)
WIP:
When shrinking a VMA via mremap, the bounds are modified directly:
mm/nommu.c:do_mremap() {
...
vma->vm_end = vma->vm_start + new_len;
...
}
This shrink the VMA without updating its bounds in the maple tree.
If the maple tree (mm->mm_mt) still contains the old bounds, a user
process could access the freed portion. The stale maple tree would
incorrectly return the shrunk VMA for an address past its new vm_end.
This commit fixes this issue by calling vmi_shrink_vma() when shrink
happens.
Link: https://sashiko.dev/#/patchset/20260702012546.665383-1-thehajime@gmail.com
Signed-off-by: Hajime Tazaki <thehajime@gmail.com>
---
mm/nommu.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/mm/nommu.c b/mm/nommu.c
index 852ec9bd0505..10794e3b6be9 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -1598,7 +1598,17 @@ static unsigned long do_mremap(unsigned long addr,
return (unsigned long) -ENOMEM;
/* all checks complete - do it */
- vma->vm_end = vma->vm_start + new_len;
+ if (new_len < old_len) {
+ /* shrink only happens addr + new_len and old_len are in different pages */
+ VMA_ITERATOR(vmi, current->mm, addr);
+ /* vmi_shrink_vma() needs from/to pointers to be removed,
+ * (mainly used in munmap) so, specify them.
+ */
+ vmi_shrink_vma(&vmi, vma, addr + new_len, addr + old_len);
+ } else {
+ /* when there are no shrink, update vma. */
+ vma->vm_end = vma->vm_start + new_len;
+ }
return vma->vm_start;
}
--
2.43.0
next reply other threads:[~2026-07-10 2:10 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-10 2:10 Hajime Tazaki [this message]
2026-07-11 0:43 ` [PATCH] mm: nommu: free unused resources when mremap shrinks the vma Andrew Morton
2026-07-11 3:03 ` Hajime Tazaki
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=20260710021028.892645-1-thehajime@gmail.com \
--to=thehajime@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=geert@linux-m68k.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