From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
patches@lists.linux.dev, "Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Pedro Falcato <pfalcato@suse.de>,
"Kiryl Shutsemau (Meta)" <kas@kernel.org>,
"Jose A. Perez de Azpillaga" <azpijr@gmail.com>,
Anirudh Srinivasan <asrinivasan@oss.tenstorrent.com>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>, Jann Horn <jannh@google.com>,
Brian Geffon <bgeffon@google.com>,
Minchan Kim <minchan@kernel.org>, Sasha Levin <sashal@kernel.org>
Subject: [PATCH 6.18 10/52] mm/mremap: fix locked_vm leak by splitting VMA for MREMAP_DONTUNMAP
Date: Sat, 17 Oct 2026 17:54:11 +0200 [thread overview]
Message-ID: <20261017155105.186997230@linuxfoundation.org> (raw)
In-Reply-To: <20261017155104.830766652@linuxfoundation.org>
6.18-stable review patch. If anyone has any objections, please let me know.
------------------
From: Lorenzo Stoakes (ARM) <ljs@kernel.org>
[ Upstream commit 597e4b9ab48a3f841080f627722064853d3f650e ]
The MREMAP_DONTUNMAP feature is highly unusual in that it permits mremap()
operations that keep the original VMA in place.
Historically this has led to a lot of bugs where non-obvious interactions
occur between existing mremap() operations and the original VMA.
Fix another of these - partial copies.
The long-standing mremap() partial VMA logic has the baked-in assumption
that the originating VMA is unmapped and thus moved.
However MREMAP_DONTUNMAP defeats this by performing a partial copy
instead since it keeps the source VMA around.
An mremap(..., MREMAP_DONTUNMAP) operation disallows resizing of the VMA,
but the operation can be performed partially:
|-----------------|
| |
| v
<------> <------>
.new_sz. new_sz
|--.------.--| |------|
| .source. | | dest |
|--.------.--| |------|
<------------>
old_sz
The page tables in the specified range are moved, but the original VMA is
kept intact.
This interacts poorly with mlock()'d VMAs, as the VMA_LOCKED_BIT flag is
cleared for the entire source VMA and set for the entire destination VMA.
This results in an mm->locked_vm leak as the change is therefore not
accounted correctly.
The clear solution here is to make the portion of the source VMA which is
mremap()'d distinct from the rest of it, a.k.a. split it.
Therefore resolve this issue by splitting it ahead of the rest of the
mremap() operation if the VMA is mlock()'d.
This is valid, as the source VMA will lose its VMA_LOCKED_BIT flag, so if a
partial remap it will become distinct from the rest of the VMA.
In order to make this change re-expose split_vma() in vma.h for
CONFIG_MMU (nommu doesn't compile mremap.c and uses a static helper
instead).
Finally, update the sys_map_count check to account for this case.
Note that the early check does not use needs_pre_split() - this is because
the VMA has not been looked up by this point, so be conservative and assume
that the VMA is mlock()'d in this case for the purposes of the
sys_map_count check.
Link: https://lore.kernel.org/20260930-fix-dontunmap-partial-self-merge-v2-2-f388985a0f0a@kernel.org
Fixes: e346b3813067 ("mm/mremap: add MREMAP_DONTUNMAP to mremap()")
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Pedro Falcato <pfalcato@suse.de>
Acked-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
Tested-by: Anirudh Srinivasan <asrinivasan@oss.tenstorrent.com>
Cc: Liam R. Howlett <liam@infradead.org>
Cc: Vlastimil Babka <vbabka@kernel.org>
Cc: Jann Horn <jannh@google.com>
Cc: Brian Geffon <bgeffon@google.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: <stable@vger.kernel.org>
[ sashal: adapted newer VMA and map-count helpers to direct flag access and existing map-count checks. ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
mm/mremap.c | 31 +++++++++++++++++++++++--------
mm/vma.c | 4 ++--
mm/vma.h | 5 +++++
3 files changed, 30 insertions(+), 10 deletions(-)
diff --git a/mm/mremap.c b/mm/mremap.c
index e60a63a36ea0b..5d1322dca0359 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -1025,8 +1025,8 @@ static void vrm_stat_account(struct vma_remap_struct *vrm,
}
/*
- * Perform checks before attempting to write a VMA prior to it being
- * moved.
+ * Perform checks and preparation before attempting to write a VMA prior to it
+ * being moved.
*/
static unsigned long prep_move_vma(struct vma_remap_struct *vrm)
{
@@ -1035,18 +1035,17 @@ static unsigned long prep_move_vma(struct vma_remap_struct *vrm)
unsigned long old_addr = vrm->addr;
unsigned long old_len = vrm->old_len;
vm_flags_t dummy = vma->vm_flags;
+ const bool split_before = vma->vm_start != old_addr;
+ const bool split_after = vma->vm_end != old_addr + old_len;
- /*
- * We'd prefer to avoid failure later on in do_munmap:
- * which may split one vma into three before unmapping.
- */
+ /* Avoid failure later on. */
if (current->mm->map_count >= sysctl_max_map_count - 3)
return -ENOMEM;
if (vma->vm_ops && vma->vm_ops->may_split) {
- if (vma->vm_start != old_addr)
+ if (split_before)
err = vma->vm_ops->may_split(vma, old_addr);
- if (!err && vma->vm_end != old_addr + old_len)
+ if (!err && split_after)
err = vma->vm_ops->may_split(vma, old_addr + old_len);
if (err)
return err;
@@ -1064,6 +1063,22 @@ static unsigned long prep_move_vma(struct vma_remap_struct *vrm)
if (err)
return err;
+ /*
+ * To account mlock()'d pages correctly in the MREMAP_DONTUNMAP
+ * case perform any split ahead of time for an mlock()'d VMA.
+ */
+ if ((vrm->flags & MREMAP_DONTUNMAP) &&
+ (vma->vm_flags & VM_LOCKED)) {
+ VMA_ITERATOR(vmi, vma->vm_mm, old_addr);
+
+ if (split_before)
+ err = split_vma(&vmi, vma, old_addr, 1);
+ if (!err && split_after)
+ err = split_vma(&vmi, vma, old_addr + old_len, 0);
+ vrm->vmi_needs_invalidate = true;
+ return err;
+ }
+
return 0;
}
diff --git a/mm/vma.c b/mm/vma.c
index d1158fdcde287..fd7e968ad707f 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -584,8 +584,8 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
* Split a vma into two pieces at address 'addr', a new vma is allocated
* either for the first part or the tail.
*/
-static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
- unsigned long addr, int new_below)
+int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
+ unsigned long addr, int new_below)
{
if (vma->vm_mm->map_count >= sysctl_max_map_count)
return -ENOMEM;
diff --git a/mm/vma.h b/mm/vma.h
index 4c7ea8b893ab7..5534216cea4d1 100644
--- a/mm/vma.h
+++ b/mm/vma.h
@@ -409,6 +409,11 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma,
unsigned long unmapped_area(struct vm_unmapped_area_info *info);
unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info);
+#ifdef CONFIG_MMU
+int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
+ unsigned long addr, int new_below);
+#endif
+
static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma)
{
/*
--
2.53.0
next prev parent reply other threads:[~2026-10-17 15:55 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-17 15:54 [PATCH 6.18 00/52] 6.18.58-rc1 review Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 01/52] spi: mpc52xx: clean up interrupt handling Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 02/52] usb: gadget: f_hid: Add missing error code Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 03/52] RDMA/erdma: Use IRQ-safe XArray helpers for QP and CQ tables Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 04/52] PCI: dwc: Return -ENODEV from dw_pcie_wait_for_link() if device is not found Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 05/52] PCI: dwc: Return -EIO from dw_pcie_wait_for_link() if device is not active Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 06/52] ip6_gre: remove obsolete ERSPAN PMTU update Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 07/52] drm/amd: Create a device link between APU display and XHCI devices Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 08/52] iavf: iavf_virtchnl_completion: drop duplicate ether_addr_equal() test Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 09/52] iavf: send MAC change request synchronously Greg Kroah-Hartman
2026-10-17 15:54 ` Greg Kroah-Hartman [this message]
2026-10-17 15:54 ` [PATCH 6.18 11/52] mm/mremap: fix locked_vm leak from MREMAP_DONTUNMAP self-merge Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 12/52] nfsd: fix all-zero stateid in CB_OFFLOAD for async COPY Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 13/52] wifi: mt76: connac: tolerate inactive BSS deactivation Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 14/52] dm-integrity: fix NULL pointer dereference when the R flag is used Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 15/52] dm-integrity: validate the superblock on resume Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 16/52] sched_ext: Fix CPU hotplug hang when a dying CPUs tasks sit in the BPF scheduler Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 17/52] Bluetooth: RFCOMM: connect the session socket without rfcomm_mutex Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 18/52] net: xilinx: axienet: Free outstanding DMA buffers on dmaengine stop Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 19/52] net: mana: reserve RX buffer headroom to fix forwarding performance Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 20/52] riscv: patch: Avoid early phys_to_page() Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 21/52] riscv: patch: fix handling of bpf-jit execmem addresses Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 22/52] scsi: ufs: core: Decouple CQ sweep from request iterator in MCQ Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 23/52] scsi: ch: Do not keep references to data transfer element devices Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 24/52] scsi: ufs: core: Move an assignment in ufshcd_mcq_process_cqe() Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 25/52] scsi: ufs: core: Add debug log for MCQ command timeout Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 26/52] scsi: ufs: core: Add a comment block above ufshcd_mcq_compl_all_cqes_lock() Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 27/52] scsi: ufs: core: Avoid unsafe MMIO reads in ufshcd_mcq_compl_all_cqes_lock() Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 28/52] drm/xe/i2c: cancel the client work on remove Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 29/52] cifs: fix time_last_write stamp placement in setattr/truncate paths Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 30/52] fat: stop reading directory entries past the end-of-directory marker Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 31/52] ima: Access decompressed kernel module to verify appended signature Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 32/52] ima: Fix stack-out-of-bounds in is_bprm_creds_for_exec() Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 33/52] ice: remove legacy Rx and construct SKB Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 34/52] ice: drop page splitting and recycling Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 35/52] ice: fix crash in ethtool offline loopback test Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 36/52] ice: fix rxq info registering in mbuf packets Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 37/52] ice: change XDP RxQ frag_size from DMA write length to xdp.frame_sz Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 38/52] xfs: factor out xfs_attr3_node_entry_remove Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 39/52] xfs: factor out xfs_attr3_leaf_init Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 40/52] xfs: close crash window in attr dabtree inactivation Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 41/52] mm/damon/core: cancel damos_walk() before damon_ctx->kdamond reset Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 42/52] mm/damon/core: fix damos_walk() vs kdamond_fn() exit race Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 43/52] selinux: allow multiple opens of /sys/fs/selinux/policy Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 44/52] usb: iowarrior: remove inherent race with minor number Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 45/52] USB: iowarrior: fix use-after-free on disconnect race Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 46/52] ice: fix broken Rx on VFs Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 47/52] rose: hold a reference on incoming sockets queued for accept() Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 48/52] io_uring: do not charge the SQ/CQ rings to RLIMIT_MEMLOCK Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 49/52] io_uring/kbuf: dont charge provided buffer " Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 50/52] kho: free chunks using free_page() instead of kfree() Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 51/52] bpf, sockmap: Account for receive queue in FIONREAD without a verdict program Greg Kroah-Hartman
2026-10-17 15:54 ` [PATCH 6.18 52/52] wifi: atk9k: Revert "wifi: ath9k: reject short WMI command responses" Greg Kroah-Hartman
2026-10-17 17:19 ` [PATCH 6.18 00/52] 6.18.58-rc1 review Brett A C Sheffield
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=20261017155105.186997230@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=akpm@linux-foundation.org \
--cc=asrinivasan@oss.tenstorrent.com \
--cc=azpijr@gmail.com \
--cc=bgeffon@google.com \
--cc=jannh@google.com \
--cc=kas@kernel.org \
--cc=liam@infradead.org \
--cc=ljs@kernel.org \
--cc=minchan@kernel.org \
--cc=patches@lists.linux.dev \
--cc=pfalcato@suse.de \
--cc=sashal@kernel.org \
--cc=stable@vger.kernel.org \
--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