* [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes
@ 2026-11-21 16:02 Lorenzo Stoakes (ARM)
2026-11-21 16:02 ` [PATCH 1/7] mm/vma: error out on invalid bounds in unmapped_area_topdown() Lorenzo Stoakes (ARM)
` (6 more replies)
0 siblings, 7 replies; 22+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-21 16:02 UTC (permalink / raw)
To: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas
Cc: linux-mm, Chris Mason, Josef Bacik, Lorenzo Stoakes (ARM), stable
I have reordered the patches here by priority, David - the first 2 should
be prioritised as hotfixes, the rest can go in 7.5:
== Critical ==
1. unmapped_area_topdown() invalid bounds - possible memory corruption,
triggerable by any local user.
2. Lock VMAs before mm_all_locks_mutex - system-wide unkillable hang,
triggerable by a local user with FUSE.
== Need fixing ==
3. KSM unmerge only interrupted by fatal signal - silent data loss on
mremap() of KSM memory.
4. Uncharge after failed stack relocation - strict overcommit limit
defeated system-wide.
5. mmap() offset limits on mremap() expansion - invalid file offset
reaches the DAX fault handler.
== Hardening (no need for Cc: stable) ==
6. Guard gaps on in-place mremap() expansion.
7. Non-executable brk mappings under MDWE.
As usual, please go through and check these carefully for correctness,
sanity.
Note that one of the original patches was punted over to the BPF
guys ([0]), as it doesn't feel that it'd make sense to change core mm to
address it.
[0]:https://lore.kernel.org/all/awGZWsCp6v63H_py@gremlin/
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
Chris Mason (4):
mm/vma: fix uncharge after failed stack relocation
mm/mremap: apply the mmap() offset limits on expansion
mm/mremap: respect guard gaps on in-place expansion
mm/vma: make brk mappings non-executable under MDWE
Lorenzo Stoakes (ARM) (3):
mm/vma: error out on invalid bounds in unmapped_area_topdown()
mm/vma: lock VMAs before taking mm_all_locks_mutex
mm/ksm: only interrupt unmerging on a fatal signal
mm/internal.h | 2 ++
mm/ksm.c | 4 ++--
mm/mmap.c | 4 ++--
mm/mremap.c | 10 +++++++-
mm/vma.c | 75 ++++++++++++++++++++++++++++++-----------------------------
mm/vma_exec.c | 22 +++++++++++++++---
6 files changed, 72 insertions(+), 45 deletions(-)
---
base-commit: d12eccbcb79a4fa35c3aa588eb79056af62eb859
change-id: 20261120-some-more-ai-fixes-aed3c4b5ef5c
Cheers,
--
Lorenzo Stoakes (ARM) <ljs@kernel.org>
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 1/7] mm/vma: error out on invalid bounds in unmapped_area_topdown()
2026-11-21 16:02 [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes Lorenzo Stoakes (ARM)
@ 2026-11-21 16:02 ` Lorenzo Stoakes (ARM)
2026-11-22 3:20 ` Suren Baghdasaryan
` (2 more replies)
2026-11-21 16:02 ` [PATCH 2/7] mm/vma: lock VMAs before taking mm_all_locks_mutex Lorenzo Stoakes (ARM)
` (5 subsequent siblings)
6 siblings, 3 replies; 22+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-21 16:02 UTC (permalink / raw)
To: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas
Cc: linux-mm, Chris Mason, Josef Bacik, Lorenzo Stoakes (ARM), stable
unmapped_area_topdown() suffers from a class of bug arising from a mismatch
between how maple tree functions and VMA iterator functions express ranges
- the former inclusively, the latter exclusively.
This is problematic with an exclusive upper bound of 0, as it translates
to an inclusive upper bound of ULONG_MAX, that is a range which extends to
the end of the address space, and start < end or equivalent comparisons
evaluate false in a situation where in fact the expression should evaluate
true.
unmapped_area_topdown() calls vma_iter_area_highest() to position the VMA
iterator into the next highest empty range spanning [low_limit,
high_limit), iteratively reducing high_limit without checking whether it
has reached 0.
This can result in a [low_limit, 0) pair being passed to
vma_iter_area_highest() and thus unexpected underflow. Consider:
vm_start = 0x30000
stack_guard_gap = 0x100000
info->length = 0x1000
high_limit = vm_start_gap() = 0
high_limit - 1 = ULONG_MAX
window = [low_limit, ULONG_MAX]
vma_iter_end() = ULONG_MAX + 1 = 0
gap = 0 - info->length = 0xfffffffffffff000
The result is page aligned and below the IS_ERR_VALUE() range, so it
does not look like an error. What follows depends on the caller:
- mmap() with no hint can fail with -ENOMEM while space is free. The
bottom-up fallback is skipped, and __get_unmapped_area() refuses the
address as above TASK_SIZE.
- On x86-64, find_nearest_trampoline() can pick the address for the
uprobe trampoline. _install_special_mapping() then inserts a VMA
whose vm_end is 0, and exit_mmap() frees kernel page tables that
every mm shares.
- On powerpc64 with the hash MMU, slice_get_unmapped_area() can pass
the address to alloc_extended_context(), which then writes out of
bounds of mm->context.extended_id[].
mmap() can succeed with an address from the wrapped window where the
range above TASK_SIZE is too short for the request and its alignment
padding. On s390 that range is one page, so this takes no more than the
default stack_guard_gap.
Fix the issue by converting the loop into one that explicitly tests
low_limit < high_limit.
Additionally, simplify the logic by declaring the VMA iterator in the
loop (it is reset on each iteration anyway) and exiting with the correct
output value on success rather than performing a goto when an iteration is
required.
Reported-by: Chris Mason <mason@kernel.org>
Fixes: 58c5d0d6d522 ("mm/mmap: regression fix for unmapped_area{_topdown}")
Cc: <stable@vger.kernel.org>
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
mm/vma.c | 42 ++++++++++++++++++++----------------------
1 file changed, 20 insertions(+), 22 deletions(-)
diff --git a/mm/vma.c b/mm/vma.c
index c82e9306b11a..09bbcdb4971b 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -3369,8 +3369,6 @@ unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
{
unsigned long length, gap, gap_end;
unsigned long low_limit, high_limit;
- struct vm_area_struct *tmp;
- VMA_ITERATOR(vmi, current->mm, 0);
/* Adjust search length to account for worst case alignment overhead */
length = info->length + info->align_mask + info->start_gap;
@@ -3381,31 +3379,31 @@ unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
if (low_limit < mmap_min_addr)
low_limit = mmap_min_addr;
high_limit = info->high_limit;
-retry:
- if (vma_iter_area_highest(&vmi, low_limit, high_limit, length))
- return -ENOMEM;
- gap = vma_iter_end(&vmi) - info->length;
- gap -= (gap - info->align_offset) & info->align_mask;
- gap_end = vma_iter_end(&vmi);
- tmp = vma_next(&vmi);
- /* Avoid prev check if possible */
- if (tmp && vma_test_any_mask(tmp, VMA_STARTGAP_FLAGS)) {
- if (vm_start_gap(tmp) < gap_end) {
+ while (low_limit < high_limit) {
+ VMA_ITERATOR(vmi, current->mm, 0);
+ struct vm_area_struct *tmp;
+
+ if (vma_iter_area_highest(&vmi, low_limit, high_limit, length))
+ break;
+
+ gap = vma_iter_end(&vmi) - info->length;
+ gap -= (gap - info->align_offset) & info->align_mask;
+ gap_end = vma_iter_end(&vmi);
+ tmp = vma_next(&vmi);
+ /* Avoid prev check if possible */
+ if (tmp && vma_test_any_mask(tmp, VMA_STARTGAP_FLAGS)) {
+ if (vm_start_gap(tmp) >= gap_end)
+ return gap;
high_limit = vm_start_gap(tmp);
- vma_iter_reset(&vmi);
- goto retry;
- }
- } else {
- tmp = vma_prev(&vmi);
- if (tmp && vm_end_gap(tmp) > gap) {
+ } else {
+ tmp = vma_prev(&vmi);
+ if (!tmp || vm_end_gap(tmp) <= gap)
+ return gap;
high_limit = tmp->vm_start;
- vma_iter_reset(&vmi);
- goto retry;
}
}
-
- return gap;
+ return -ENOMEM;
}
/*
--
2.56.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH 2/7] mm/vma: lock VMAs before taking mm_all_locks_mutex
2026-11-21 16:02 [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes Lorenzo Stoakes (ARM)
2026-11-21 16:02 ` [PATCH 1/7] mm/vma: error out on invalid bounds in unmapped_area_topdown() Lorenzo Stoakes (ARM)
@ 2026-11-21 16:02 ` Lorenzo Stoakes (ARM)
2026-11-22 3:23 ` Suren Baghdasaryan
` (2 more replies)
2026-11-21 16:02 ` [PATCH 3/7] mm/ksm: only interrupt unmerging on a fatal signal Lorenzo Stoakes (ARM)
` (4 subsequent siblings)
6 siblings, 3 replies; 22+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-21 16:02 UTC (permalink / raw)
To: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas
Cc: linux-mm, Chris Mason, Josef Bacik, Lorenzo Stoakes (ARM), stable
If an mmu notifier is registered for an mm while one of its page faults
sleeps indefinitely under the VMA lock, the registration of mmu
notifiers for every other mm can hang un-killably.
mm_all_locks_mutex is shared by all mms, so its holder must not wait for
something that has no bound.
However, mm_take_all_locks() calls vma_start_write() on each VMA with the
mutex held, and thus waits for the readers of the VMA.
Since commit 4ed4379881aa ("mm: handle shared faults under the VMA lock"),
such a reader can sleep in ->page_mkwrite().
Consider fuse_page_mkwrite(), which sleeps until the FUSE server replies to
a write request:
mm A, thread 1 mm A, thread 2 mm B
============== ============== ====
lock_vma_under_rcu()
fuse_page_mkwrite()
folio_wait_writeback()
mm_take_all_locks()
mutex_lock()
vma_start_write()
mm_take_all_locks()
mutex_lock()
Fix the issue by locking the VMAs before the mutex is taken.
Additionally, make the VMA write lock killable so a fatal signal returns as
quickly as possible.
Finally, update the comments to reflect the change, dropping the lock
ordering list which begs for bitrot.
Note that the lock order at the top of mm/rmap.c doesn't appear to be right
- the hugetlb locks are taken after the VMA write locks. However, this was
already the case, and can be addressed in a follow-up in any case.
Target commit 4ed4379881aa ("mm: handle shared faults under the VMA lock")
as this is where it became possible for VMA lock readers to sleep in
->page_mkwrite().
Reported-by: Chris Mason <mason@kernel.org>
Fixes: 4ed4379881aa ("mm: handle shared faults under the VMA lock")
Cc: <stable@vger.kernel.org>
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
mm/vma.c | 29 ++++++++++++++---------------
1 file changed, 14 insertions(+), 15 deletions(-)
diff --git a/mm/vma.c b/mm/vma.c
index 09bbcdb4971b..f8cf319ff9cf 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -2375,15 +2375,10 @@ static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
* mapping->flags avoid to take the same lock twice, if more than one
* vma in this mm is backed by the same anon rmap or address_space.
*
- * We take locks in following order, accordingly to comment at beginning
- * of mm/rmap.c:
- * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
- * hugetlb mapping);
- * - all vmas marked locked
- * - all i_mmap_rwsem locks;
- * - all anon_vma->rwseml
- *
- * We can take all locks within these types randomly because the VM code
+ * Mark all vmas locked, before taking mm_all_locks_mutex. Then take remaining
+ * locks in the order mandated by the comment at the top of mm/rmap.c.
+ *
+ * We can take locks of the same type in arbitrary order because the VM code
* doesn't nest them and we protected from parallel mm_take_all_locks() by
* mm_all_locks_mutex.
*
@@ -2400,20 +2395,24 @@ int mm_take_all_locks(struct mm_struct *mm)
mmap_assert_write_locked(mm);
- mutex_lock(&mm_all_locks_mutex);
-
/*
- * vma_start_write() does not have a complement in mm_drop_all_locks()
- * because vma_start_write() is always asymmetrical; it marks a VMA as
+ * VMA write locks do not have a complement in mm_drop_all_locks(),
+ * because VMA write locking is always asymmetrical; it marks a VMA as
* being written to until mmap_write_unlock() or mmap_write_downgrade()
* is reached.
*/
for_each_vma(vmi, vma) {
+ int err;
+
if (signal_pending(current))
- goto out_unlock;
- vma_start_write(vma);
+ return -EINTR;
+ err = vma_start_write_killable(vma);
+ if (err)
+ return err;
}
+ mutex_lock(&mm_all_locks_mutex);
+
vma_iter_init(&vmi, mm, 0);
for_each_vma(vmi, vma) {
if (signal_pending(current))
--
2.56.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH 3/7] mm/ksm: only interrupt unmerging on a fatal signal
2026-11-21 16:02 [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes Lorenzo Stoakes (ARM)
2026-11-21 16:02 ` [PATCH 1/7] mm/vma: error out on invalid bounds in unmapped_area_topdown() Lorenzo Stoakes (ARM)
2026-11-21 16:02 ` [PATCH 2/7] mm/vma: lock VMAs before taking mm_all_locks_mutex Lorenzo Stoakes (ARM)
@ 2026-11-21 16:02 ` Lorenzo Stoakes (ARM)
2026-11-22 3:27 ` Suren Baghdasaryan
` (2 more replies)
2026-11-21 16:02 ` [PATCH 4/7] mm/vma: fix uncharge after failed stack relocation Lorenzo Stoakes (ARM)
` (3 subsequent siblings)
6 siblings, 3 replies; 22+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-21 16:02 UTC (permalink / raw)
To: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas
Cc: linux-mm, Chris Mason, Josef Bacik, Lorenzo Stoakes (ARM), stable
Commit d952b79136a6 ("ksm: fix endless loop on oom") added a check for
pending signals on KSM break, so that a lengthy unmerge could be
interrupted, with the system call then restarted via -ERESTARTSYS.
However, it assumes that the callers can always restart whatever system
call started it in the first place (the commit message questions whether
this was wise).
Well, it turns out that it was not wise, as this baked in an implicit
assumption about system call restartability, and it was and is not safe for
mremap().
mremap() holds an mmap write lock over the operation, which an -ERESTARTSYS
drops, meaning that the restart is not in a consistent state.
This was made considerably worse with commit d23cb648e365 ("mm/mremap:
permit mremap() move of multiple VMAs"), which can result in data loss if
you encounter this issue.
Consider an mremap() specifying MREMAP_FIXED | MREMAP_DONTUNMAP of two
KSM-mergeable VMAs, where a signal becomes pending part way through:
first pass restart
========== =======
unmap tgt1 range
copy src1 to tgt1, move page tables
unmap tgt2 range
break_ksm(src2) = -ERESTARTSYS
unmap tgt1 range, which now
holds the data from src1
copy the now-empty src1 to tgt1
This results in the user observing a successful mremap() but with src1's
data missing.
Without MREMAP_DONTUNMAP, remap_move() would find a gap at the start of the
range on restart and fail with -EFAULT there instead.
Fix the issue by only breaking out of an unmerge if a fatal signal is
pending, returning -EINTR when we do. This makes KSM unmerging consistent
with many other signal checks in mm.
Target commit d23cb648e365 ("mm/mremap: permit mremap() move of multiple
VMAs") for the fix as this made the issue significantly more impactful.
Reported-by: Chris Mason <mason@kernel.org>
Fixes: d23cb648e365 ("mm/mremap: permit mremap() move of multiple VMAs")
Cc: <stable@vger.kernel.org>
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
mm/ksm.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/mm/ksm.c b/mm/ksm.c
index 623a2cdb3924..5d320a200951 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -627,8 +627,8 @@ static int break_ksm_pmd_entry(pmd_t *pmdp, unsigned long addr, unsigned long en
if (ksm_test_exit(walk->mm))
return 0;
- if (signal_pending(current))
- return -ERESTARTSYS;
+ if (fatal_signal_pending(current))
+ return -EINTR;
start_ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
if (!start_ptep)
--
2.56.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH 4/7] mm/vma: fix uncharge after failed stack relocation
2026-11-21 16:02 [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes Lorenzo Stoakes (ARM)
` (2 preceding siblings ...)
2026-11-21 16:02 ` [PATCH 3/7] mm/ksm: only interrupt unmerging on a fatal signal Lorenzo Stoakes (ARM)
@ 2026-11-21 16:02 ` Lorenzo Stoakes (ARM)
2026-11-22 3:34 ` Suren Baghdasaryan
2026-11-22 15:50 ` Gregory Price (Meta)
2026-11-21 16:02 ` [PATCH 5/7] mm/mremap: apply the mmap() offset limits on expansion Lorenzo Stoakes (ARM)
` (2 subsequent siblings)
6 siblings, 2 replies; 22+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-21 16:02 UTC (permalink / raw)
To: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas
Cc: linux-mm, Chris Mason, Josef Bacik, Lorenzo Stoakes (ARM), stable
From: Chris Mason <mason@kernel.org>
An execve() that fails to relocate its stack can underflow
vm_committed_as, and the commit limit of vm.overcommit_memory=2 then no
longer holds.
exit_mmap() uncharges vma_pages() of each VMA with VMA_ACCOUNT_BIT, so
the charge has to match the size of the VMA. relocate_vma_down() expands
the stack VMA by the relocation distance without a charge, and relies on
its vma_shrink() to make the two match again. But it returns with the
VMA still expanded if move_page_tables() can't allocate a page table,
for example at the memory cgroup limit, or if vma_shrink() fails:
relocate_vma_down() charged vma_pages()
=================== ======= ===========
L L
vma_expand() L L + shift
move_page_tables() fails
return -ENOMEM
exit_mmap()
tear_down_vmas()
vm_unacct_memory(L + shift) -shift
On x86-64 with 4-level page tables, the distance is up to 16 GiB for a
64-bit program, and about 128 TiB for a 32-bit one.
Fix the issue by giving back the charge of the stack VMA where its
relocation fails, and excluding the VMA from what exit_mmap() uncharges.
The argument pages of the dying task are then outside the commit charge
until exit_mmap() frees them.
Fixes: b6a2fea39318 ("mm: variable length argument support")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
mm/vma_exec.c | 22 +++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)
diff --git a/mm/vma_exec.c b/mm/vma_exec.c
index 586c52155942..84b98f1a201d 100644
--- a/mm/vma_exec.c
+++ b/mm/vma_exec.c
@@ -45,6 +45,7 @@ int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
struct vm_area_struct *next;
struct mmu_gather tlb;
PAGETABLE_MOVE(pmc, vma, vma, old_start, new_start, length);
+ int err;
BUG_ON(new_start > new_end);
@@ -68,8 +69,10 @@ int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
* process cleanup to remove whatever mess we made.
*/
pmc.for_stack = true;
- if (length != move_page_tables(&pmc))
- return -ENOMEM;
+ if (length != move_page_tables(&pmc)) {
+ err = -ENOMEM;
+ goto err_unacct;
+ }
tlb_gather_mmu(&tlb, mm);
next = vma_next(&vmi);
@@ -93,7 +96,20 @@ int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
vma_prev(&vmi);
/* Shrink the vma to just the new range */
- return vma_shrink(&vmi, vma, new_end);
+ err = vma_shrink(&vmi, vma, new_end);
+ if (err)
+ goto err_unacct;
+
+ return 0;
+
+err_unacct:
+ /*
+ * The vma now spans more than was charged, so exit_mmap() must not
+ * derive the uncharge from its size.
+ */
+ vm_unacct_memory(length >> PAGE_SHIFT);
+ vma_clear_flags(vma, VMA_ACCOUNT_BIT);
+ return err;
}
/*
--
2.56.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH 5/7] mm/mremap: apply the mmap() offset limits on expansion
2026-11-21 16:02 [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes Lorenzo Stoakes (ARM)
` (3 preceding siblings ...)
2026-11-21 16:02 ` [PATCH 4/7] mm/vma: fix uncharge after failed stack relocation Lorenzo Stoakes (ARM)
@ 2026-11-21 16:02 ` Lorenzo Stoakes (ARM)
2026-11-22 3:37 ` Suren Baghdasaryan
` (2 more replies)
2026-11-22 15:55 ` (subset) [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes David Hildenbrand
2026-11-22 15:57 ` Gregory Price (Meta)
6 siblings, 3 replies; 22+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-21 16:02 UTC (permalink / raw)
To: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas
Cc: linux-mm, Chris Mason, Josef Bacik, Lorenzo Stoakes (ARM), stable
From: Chris Mason <mason@kernel.org>
Commit be83bbf80682 ("mmap: introduce sane default mmap limits") made
do_mmap() reject file mappings which extend beyond the maximum offset the
file can support, via file_mmap_ok().
However mremap() can expand a file mapping and only checks that the page
offset does not wrap, so a mapping can be grown past the limit that mmap()
would have enforced.
Fault handlers rely on that limit. For instance dax_iomap_pte_fault()
converts vmf->pgoff into a loff_t, which overflows for such an offset and
defeats its i_size check.
Fix the issue by applying file_mmap_ok() to the expanded range in
check_prep_vma(), failing the mremap() with -EINVAL if it exceeds the
limit.
This means that mremap() now refuses expansions it previously permitted,
however no file can have data at these offsets, and mmap() already refuses
the equivalent mapping.
Fixes: be83bbf80682 ("mmap: introduce sane default mmap limits")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chris Mason <mason@kernel.org>
[ ljs: fixed up commit msg ]
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
mm/internal.h | 2 ++
mm/mmap.c | 4 ++--
mm/mremap.c | 6 ++++++
3 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/mm/internal.h b/mm/internal.h
index a09dbdfd615d..aa7fba0a1c41 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -941,6 +941,8 @@ extern long faultin_page_range(struct mm_struct *mm, unsigned long start,
unsigned long end, bool write, int *locked);
bool mlock_future_ok(const struct mm_struct *mm, bool is_vma_locked,
unsigned long bytes);
+bool file_mmap_ok(struct file *file, struct inode *inode, unsigned long pgoff,
+ unsigned long len);
/*
* NOTE: This function can't tell whether the folio is "fully mapped" in the
diff --git a/mm/mmap.c b/mm/mmap.c
index 7618e72766a5..79702841c06c 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -264,8 +264,8 @@ static inline u64 file_mmap_size_max(struct file *file, struct inode *inode)
return ULONG_MAX;
}
-static inline bool file_mmap_ok(struct file *file, struct inode *inode,
- unsigned long pgoff, unsigned long len)
+bool file_mmap_ok(struct file *file, struct inode *inode, unsigned long pgoff,
+ unsigned long len)
{
u64 maxsize = file_mmap_size_max(file, inode);
diff --git a/mm/mremap.c b/mm/mremap.c
index fff0465f5e06..8043c425b5df 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -1791,6 +1791,7 @@ static int check_prep_vma(struct vma_remap_struct *vrm)
struct mm_struct *mm = current->mm;
unsigned long addr = vrm->addr;
unsigned long old_len, new_len, pgoff;
+ struct file *file;
if (!vma)
return -EFAULT;
@@ -1870,6 +1871,11 @@ static int check_prep_vma(struct vma_remap_struct *vrm)
if (vma_is_fixed_mapping(vma))
return -EFAULT;
+ /* The expanded range must obey the same offset limits as mmap(). */
+ file = vma->vm_file;
+ if (file && !file_mmap_ok(file, file_inode(file), pgoff, new_len))
+ return -EINVAL;
+
if (!mlock_future_ok(mm, vma_test(vma, VMA_LOCKED_BIT), vrm->delta))
return -EAGAIN;
--
2.56.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* Re: [PATCH 1/7] mm/vma: error out on invalid bounds in unmapped_area_topdown()
2026-11-21 16:02 ` [PATCH 1/7] mm/vma: error out on invalid bounds in unmapped_area_topdown() Lorenzo Stoakes (ARM)
@ 2026-11-22 3:20 ` Suren Baghdasaryan
2026-11-22 15:55 ` Gregory Price (Meta)
2026-11-22 18:03 ` Jose A. Perez de Azpillaga
2 siblings, 0 replies; 22+ messages in thread
From: Suren Baghdasaryan @ 2026-11-22 3:20 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Liam R. Howlett, Vlastimil Babka, Shakeel Butt, Pedro Falcato,
David Hildenbrand, Jann Horn, Andrew Morton,
Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra, Ollie Wild,
Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport, Michal Hocko,
Hugh Dickins, Joey Gouly, Catalin Marinas, linux-mm, Chris Mason,
Josef Bacik, stable
On Sat, Nov 21, 2026 at 8:03 AM Lorenzo Stoakes (ARM) <ljs@kernel.org> wrote:
>
> unmapped_area_topdown() suffers from a class of bug arising from a mismatch
> between how maple tree functions and VMA iterator functions express ranges
> - the former inclusively, the latter exclusively.
>
> This is problematic with an exclusive upper bound of 0, as it translates
> to an inclusive upper bound of ULONG_MAX, that is a range which extends to
> the end of the address space, and start < end or equivalent comparisons
> evaluate false in a situation where in fact the expression should evaluate
> true.
>
> unmapped_area_topdown() calls vma_iter_area_highest() to position the VMA
> iterator into the next highest empty range spanning [low_limit,
> high_limit), iteratively reducing high_limit without checking whether it
> has reached 0.
>
> This can result in a [low_limit, 0) pair being passed to
> vma_iter_area_highest() and thus unexpected underflow. Consider:
>
> vm_start = 0x30000
> stack_guard_gap = 0x100000
> info->length = 0x1000
> high_limit = vm_start_gap() = 0
> high_limit - 1 = ULONG_MAX
> window = [low_limit, ULONG_MAX]
> vma_iter_end() = ULONG_MAX + 1 = 0
> gap = 0 - info->length = 0xfffffffffffff000
>
> The result is page aligned and below the IS_ERR_VALUE() range, so it
> does not look like an error. What follows depends on the caller:
>
> - mmap() with no hint can fail with -ENOMEM while space is free. The
> bottom-up fallback is skipped, and __get_unmapped_area() refuses the
> address as above TASK_SIZE.
> - On x86-64, find_nearest_trampoline() can pick the address for the
> uprobe trampoline. _install_special_mapping() then inserts a VMA
> whose vm_end is 0, and exit_mmap() frees kernel page tables that
> every mm shares.
> - On powerpc64 with the hash MMU, slice_get_unmapped_area() can pass
> the address to alloc_extended_context(), which then writes out of
> bounds of mm->context.extended_id[].
>
> mmap() can succeed with an address from the wrapped window where the
> range above TASK_SIZE is too short for the request and its alignment
> padding. On s390 that range is one page, so this takes no more than the
> default stack_guard_gap.
>
> Fix the issue by converting the loop into one that explicitly tests
> low_limit < high_limit.
>
> Additionally, simplify the logic by declaring the VMA iterator in the
> loop (it is reset on each iteration anyway) and exiting with the correct
> output value on success rather than performing a goto when an iteration is
> required.
>
> Reported-by: Chris Mason <mason@kernel.org>
> Fixes: 58c5d0d6d522 ("mm/mmap: regression fix for unmapped_area{_topdown}")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
> ---
> mm/vma.c | 42 ++++++++++++++++++++----------------------
> 1 file changed, 20 insertions(+), 22 deletions(-)
>
> diff --git a/mm/vma.c b/mm/vma.c
> index c82e9306b11a..09bbcdb4971b 100644
> --- a/mm/vma.c
> +++ b/mm/vma.c
> @@ -3369,8 +3369,6 @@ unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
> {
> unsigned long length, gap, gap_end;
> unsigned long low_limit, high_limit;
> - struct vm_area_struct *tmp;
> - VMA_ITERATOR(vmi, current->mm, 0);
>
> /* Adjust search length to account for worst case alignment overhead */
> length = info->length + info->align_mask + info->start_gap;
> @@ -3381,31 +3379,31 @@ unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
> if (low_limit < mmap_min_addr)
> low_limit = mmap_min_addr;
> high_limit = info->high_limit;
> -retry:
> - if (vma_iter_area_highest(&vmi, low_limit, high_limit, length))
> - return -ENOMEM;
>
> - gap = vma_iter_end(&vmi) - info->length;
> - gap -= (gap - info->align_offset) & info->align_mask;
> - gap_end = vma_iter_end(&vmi);
> - tmp = vma_next(&vmi);
> - /* Avoid prev check if possible */
> - if (tmp && vma_test_any_mask(tmp, VMA_STARTGAP_FLAGS)) {
> - if (vm_start_gap(tmp) < gap_end) {
> + while (low_limit < high_limit) {
> + VMA_ITERATOR(vmi, current->mm, 0);
> + struct vm_area_struct *tmp;
> +
> + if (vma_iter_area_highest(&vmi, low_limit, high_limit, length))
> + break;
> +
> + gap = vma_iter_end(&vmi) - info->length;
> + gap -= (gap - info->align_offset) & info->align_mask;
> + gap_end = vma_iter_end(&vmi);
> + tmp = vma_next(&vmi);
> + /* Avoid prev check if possible */
> + if (tmp && vma_test_any_mask(tmp, VMA_STARTGAP_FLAGS)) {
> + if (vm_start_gap(tmp) >= gap_end)
> + return gap;
> high_limit = vm_start_gap(tmp);
> - vma_iter_reset(&vmi);
> - goto retry;
> - }
> - } else {
> - tmp = vma_prev(&vmi);
> - if (tmp && vm_end_gap(tmp) > gap) {
> + } else {
> + tmp = vma_prev(&vmi);
> + if (!tmp || vm_end_gap(tmp) <= gap)
> + return gap;
> high_limit = tmp->vm_start;
> - vma_iter_reset(&vmi);
> - goto retry;
> }
> }
> -
> - return gap;
> + return -ENOMEM;
> }
>
> /*
>
> --
> 2.56.0
>
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 2/7] mm/vma: lock VMAs before taking mm_all_locks_mutex
2026-11-21 16:02 ` [PATCH 2/7] mm/vma: lock VMAs before taking mm_all_locks_mutex Lorenzo Stoakes (ARM)
@ 2026-11-22 3:23 ` Suren Baghdasaryan
2026-11-22 15:53 ` Gregory Price (Meta)
2026-11-22 18:03 ` Jose A. Perez de Azpillaga
2 siblings, 0 replies; 22+ messages in thread
From: Suren Baghdasaryan @ 2026-11-22 3:23 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Liam R. Howlett, Vlastimil Babka, Shakeel Butt, Pedro Falcato,
David Hildenbrand, Jann Horn, Andrew Morton,
Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra, Ollie Wild,
Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport, Michal Hocko,
Hugh Dickins, Joey Gouly, Catalin Marinas, linux-mm, Chris Mason,
Josef Bacik, stable
On Sat, Nov 21, 2026 at 8:03 AM Lorenzo Stoakes (ARM) <ljs@kernel.org> wrote:
>
> If an mmu notifier is registered for an mm while one of its page faults
> sleeps indefinitely under the VMA lock, the registration of mmu
> notifiers for every other mm can hang un-killably.
>
> mm_all_locks_mutex is shared by all mms, so its holder must not wait for
> something that has no bound.
>
> However, mm_take_all_locks() calls vma_start_write() on each VMA with the
> mutex held, and thus waits for the readers of the VMA.
>
> Since commit 4ed4379881aa ("mm: handle shared faults under the VMA lock"),
> such a reader can sleep in ->page_mkwrite().
>
> Consider fuse_page_mkwrite(), which sleeps until the FUSE server replies to
> a write request:
>
> mm A, thread 1 mm A, thread 2 mm B
> ============== ============== ====
> lock_vma_under_rcu()
> fuse_page_mkwrite()
> folio_wait_writeback()
> mm_take_all_locks()
> mutex_lock()
> vma_start_write()
> mm_take_all_locks()
> mutex_lock()
>
> Fix the issue by locking the VMAs before the mutex is taken.
>
> Additionally, make the VMA write lock killable so a fatal signal returns as
> quickly as possible.
>
> Finally, update the comments to reflect the change, dropping the lock
> ordering list which begs for bitrot.
>
> Note that the lock order at the top of mm/rmap.c doesn't appear to be right
> - the hugetlb locks are taken after the VMA write locks. However, this was
> already the case, and can be addressed in a follow-up in any case.
>
> Target commit 4ed4379881aa ("mm: handle shared faults under the VMA lock")
> as this is where it became possible for VMA lock readers to sleep in
> ->page_mkwrite().
>
> Reported-by: Chris Mason <mason@kernel.org>
> Fixes: 4ed4379881aa ("mm: handle shared faults under the VMA lock")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
> ---
> mm/vma.c | 29 ++++++++++++++---------------
> 1 file changed, 14 insertions(+), 15 deletions(-)
>
> diff --git a/mm/vma.c b/mm/vma.c
> index 09bbcdb4971b..f8cf319ff9cf 100644
> --- a/mm/vma.c
> +++ b/mm/vma.c
> @@ -2375,15 +2375,10 @@ static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
> * mapping->flags avoid to take the same lock twice, if more than one
> * vma in this mm is backed by the same anon rmap or address_space.
> *
> - * We take locks in following order, accordingly to comment at beginning
> - * of mm/rmap.c:
> - * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
> - * hugetlb mapping);
> - * - all vmas marked locked
> - * - all i_mmap_rwsem locks;
> - * - all anon_vma->rwseml
> - *
> - * We can take all locks within these types randomly because the VM code
> + * Mark all vmas locked, before taking mm_all_locks_mutex. Then take remaining
> + * locks in the order mandated by the comment at the top of mm/rmap.c.
> + *
> + * We can take locks of the same type in arbitrary order because the VM code
> * doesn't nest them and we protected from parallel mm_take_all_locks() by
> * mm_all_locks_mutex.
> *
> @@ -2400,20 +2395,24 @@ int mm_take_all_locks(struct mm_struct *mm)
>
> mmap_assert_write_locked(mm);
>
> - mutex_lock(&mm_all_locks_mutex);
> -
> /*
> - * vma_start_write() does not have a complement in mm_drop_all_locks()
> - * because vma_start_write() is always asymmetrical; it marks a VMA as
> + * VMA write locks do not have a complement in mm_drop_all_locks(),
> + * because VMA write locking is always asymmetrical; it marks a VMA as
> * being written to until mmap_write_unlock() or mmap_write_downgrade()
> * is reached.
> */
> for_each_vma(vmi, vma) {
> + int err;
> +
> if (signal_pending(current))
> - goto out_unlock;
> - vma_start_write(vma);
> + return -EINTR;
> + err = vma_start_write_killable(vma);
> + if (err)
> + return err;
> }
>
> + mutex_lock(&mm_all_locks_mutex);
> +
> vma_iter_init(&vmi, mm, 0);
> for_each_vma(vmi, vma) {
> if (signal_pending(current))
>
> --
> 2.56.0
>
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 3/7] mm/ksm: only interrupt unmerging on a fatal signal
2026-11-21 16:02 ` [PATCH 3/7] mm/ksm: only interrupt unmerging on a fatal signal Lorenzo Stoakes (ARM)
@ 2026-11-22 3:27 ` Suren Baghdasaryan
2026-11-22 15:54 ` David Hildenbrand (Arm)
2026-11-22 15:56 ` Gregory Price (Meta)
2 siblings, 0 replies; 22+ messages in thread
From: Suren Baghdasaryan @ 2026-11-22 3:27 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Liam R. Howlett, Vlastimil Babka, Shakeel Butt, Pedro Falcato,
David Hildenbrand, Jann Horn, Andrew Morton,
Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra, Ollie Wild,
Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport, Michal Hocko,
Hugh Dickins, Joey Gouly, Catalin Marinas, linux-mm, Chris Mason,
Josef Bacik, stable
On Sat, Nov 21, 2026 at 8:03 AM Lorenzo Stoakes (ARM) <ljs@kernel.org> wrote:
>
> Commit d952b79136a6 ("ksm: fix endless loop on oom") added a check for
> pending signals on KSM break, so that a lengthy unmerge could be
> interrupted, with the system call then restarted via -ERESTARTSYS.
>
> However, it assumes that the callers can always restart whatever system
> call started it in the first place (the commit message questions whether
> this was wise).
>
> Well, it turns out that it was not wise, as this baked in an implicit
> assumption about system call restartability, and it was and is not safe for
> mremap().
>
> mremap() holds an mmap write lock over the operation, which an -ERESTARTSYS
> drops, meaning that the restart is not in a consistent state.
>
> This was made considerably worse with commit d23cb648e365 ("mm/mremap:
> permit mremap() move of multiple VMAs"), which can result in data loss if
> you encounter this issue.
>
> Consider an mremap() specifying MREMAP_FIXED | MREMAP_DONTUNMAP of two
> KSM-mergeable VMAs, where a signal becomes pending part way through:
>
> first pass restart
> ========== =======
> unmap tgt1 range
> copy src1 to tgt1, move page tables
> unmap tgt2 range
> break_ksm(src2) = -ERESTARTSYS
> unmap tgt1 range, which now
> holds the data from src1
> copy the now-empty src1 to tgt1
>
> This results in the user observing a successful mremap() but with src1's
> data missing.
>
> Without MREMAP_DONTUNMAP, remap_move() would find a gap at the start of the
> range on restart and fail with -EFAULT there instead.
>
> Fix the issue by only breaking out of an unmerge if a fatal signal is
> pending, returning -EINTR when we do. This makes KSM unmerging consistent
> with many other signal checks in mm.
>
> Target commit d23cb648e365 ("mm/mremap: permit mremap() move of multiple
> VMAs") for the fix as this made the issue significantly more impactful.
>
> Reported-by: Chris Mason <mason@kernel.org>
> Fixes: d23cb648e365 ("mm/mremap: permit mremap() move of multiple VMAs")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
> ---
> mm/ksm.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/mm/ksm.c b/mm/ksm.c
> index 623a2cdb3924..5d320a200951 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -627,8 +627,8 @@ static int break_ksm_pmd_entry(pmd_t *pmdp, unsigned long addr, unsigned long en
>
> if (ksm_test_exit(walk->mm))
> return 0;
> - if (signal_pending(current))
> - return -ERESTARTSYS;
> + if (fatal_signal_pending(current))
> + return -EINTR;
>
> start_ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
> if (!start_ptep)
>
> --
> 2.56.0
>
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 4/7] mm/vma: fix uncharge after failed stack relocation
2026-11-21 16:02 ` [PATCH 4/7] mm/vma: fix uncharge after failed stack relocation Lorenzo Stoakes (ARM)
@ 2026-11-22 3:34 ` Suren Baghdasaryan
2026-11-22 15:50 ` Gregory Price (Meta)
1 sibling, 0 replies; 22+ messages in thread
From: Suren Baghdasaryan @ 2026-11-22 3:34 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Liam R. Howlett, Vlastimil Babka, Shakeel Butt, Pedro Falcato,
David Hildenbrand, Jann Horn, Andrew Morton,
Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra, Ollie Wild,
Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport, Michal Hocko,
Hugh Dickins, Joey Gouly, Catalin Marinas, linux-mm, Chris Mason,
Josef Bacik, stable
On Sat, Nov 21, 2026 at 8:03 AM Lorenzo Stoakes (ARM) <ljs@kernel.org> wrote:
>
> From: Chris Mason <mason@kernel.org>
>
> An execve() that fails to relocate its stack can underflow
> vm_committed_as, and the commit limit of vm.overcommit_memory=2 then no
> longer holds.
>
> exit_mmap() uncharges vma_pages() of each VMA with VMA_ACCOUNT_BIT, so
> the charge has to match the size of the VMA. relocate_vma_down() expands
> the stack VMA by the relocation distance without a charge, and relies on
> its vma_shrink() to make the two match again. But it returns with the
> VMA still expanded if move_page_tables() can't allocate a page table,
> for example at the memory cgroup limit, or if vma_shrink() fails:
>
> relocate_vma_down() charged vma_pages()
> =================== ======= ===========
> L L
> vma_expand() L L + shift
> move_page_tables() fails
> return -ENOMEM
>
> exit_mmap()
> tear_down_vmas()
> vm_unacct_memory(L + shift) -shift
>
> On x86-64 with 4-level page tables, the distance is up to 16 GiB for a
> 64-bit program, and about 128 TiB for a 32-bit one.
>
> Fix the issue by giving back the charge of the stack VMA where its
> relocation fails, and excluding the VMA from what exit_mmap() uncharges.
> The argument pages of the dying task are then outside the commit charge
> until exit_mmap() frees them.
>
> Fixes: b6a2fea39318 ("mm: variable length argument support")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Chris Mason <mason@kernel.org>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
> ---
> mm/vma_exec.c | 22 +++++++++++++++++++---
> 1 file changed, 19 insertions(+), 3 deletions(-)
>
> diff --git a/mm/vma_exec.c b/mm/vma_exec.c
> index 586c52155942..84b98f1a201d 100644
> --- a/mm/vma_exec.c
> +++ b/mm/vma_exec.c
> @@ -45,6 +45,7 @@ int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
> struct vm_area_struct *next;
> struct mmu_gather tlb;
> PAGETABLE_MOVE(pmc, vma, vma, old_start, new_start, length);
> + int err;
>
> BUG_ON(new_start > new_end);
>
> @@ -68,8 +69,10 @@ int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
> * process cleanup to remove whatever mess we made.
> */
> pmc.for_stack = true;
> - if (length != move_page_tables(&pmc))
> - return -ENOMEM;
> + if (length != move_page_tables(&pmc)) {
> + err = -ENOMEM;
> + goto err_unacct;
> + }
>
> tlb_gather_mmu(&tlb, mm);
> next = vma_next(&vmi);
> @@ -93,7 +96,20 @@ int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
>
> vma_prev(&vmi);
> /* Shrink the vma to just the new range */
> - return vma_shrink(&vmi, vma, new_end);
> + err = vma_shrink(&vmi, vma, new_end);
> + if (err)
> + goto err_unacct;
> +
> + return 0;
> +
> +err_unacct:
> + /*
> + * The vma now spans more than was charged, so exit_mmap() must not
> + * derive the uncharge from its size.
> + */
> + vm_unacct_memory(length >> PAGE_SHIFT);
> + vma_clear_flags(vma, VMA_ACCOUNT_BIT);
> + return err;
> }
>
> /*
>
> --
> 2.56.0
>
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 5/7] mm/mremap: apply the mmap() offset limits on expansion
2026-11-21 16:02 ` [PATCH 5/7] mm/mremap: apply the mmap() offset limits on expansion Lorenzo Stoakes (ARM)
@ 2026-11-22 3:37 ` Suren Baghdasaryan
2026-11-22 15:50 ` Gregory Price (Meta)
2026-11-22 18:03 ` Jose A. Perez de Azpillaga
2 siblings, 0 replies; 22+ messages in thread
From: Suren Baghdasaryan @ 2026-11-22 3:37 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Liam R. Howlett, Vlastimil Babka, Shakeel Butt, Pedro Falcato,
David Hildenbrand, Jann Horn, Andrew Morton,
Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra, Ollie Wild,
Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport, Michal Hocko,
Hugh Dickins, Joey Gouly, Catalin Marinas, linux-mm, Chris Mason,
Josef Bacik, stable
On Sat, Nov 21, 2026 at 8:03 AM Lorenzo Stoakes (ARM) <ljs@kernel.org> wrote:
>
> From: Chris Mason <mason@kernel.org>
>
> Commit be83bbf80682 ("mmap: introduce sane default mmap limits") made
> do_mmap() reject file mappings which extend beyond the maximum offset the
> file can support, via file_mmap_ok().
>
> However mremap() can expand a file mapping and only checks that the page
> offset does not wrap, so a mapping can be grown past the limit that mmap()
> would have enforced.
>
> Fault handlers rely on that limit. For instance dax_iomap_pte_fault()
> converts vmf->pgoff into a loff_t, which overflows for such an offset and
> defeats its i_size check.
>
> Fix the issue by applying file_mmap_ok() to the expanded range in
> check_prep_vma(), failing the mremap() with -EINVAL if it exceeds the
> limit.
>
> This means that mremap() now refuses expansions it previously permitted,
> however no file can have data at these offsets, and mmap() already refuses
> the equivalent mapping.
>
> Fixes: be83bbf80682 ("mmap: introduce sane default mmap limits")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Chris Mason <mason@kernel.org>
> [ ljs: fixed up commit msg ]
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
> ---
> mm/internal.h | 2 ++
> mm/mmap.c | 4 ++--
> mm/mremap.c | 6 ++++++
> 3 files changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/mm/internal.h b/mm/internal.h
> index a09dbdfd615d..aa7fba0a1c41 100644
> --- a/mm/internal.h
> +++ b/mm/internal.h
> @@ -941,6 +941,8 @@ extern long faultin_page_range(struct mm_struct *mm, unsigned long start,
> unsigned long end, bool write, int *locked);
> bool mlock_future_ok(const struct mm_struct *mm, bool is_vma_locked,
> unsigned long bytes);
> +bool file_mmap_ok(struct file *file, struct inode *inode, unsigned long pgoff,
> + unsigned long len);
>
> /*
> * NOTE: This function can't tell whether the folio is "fully mapped" in the
> diff --git a/mm/mmap.c b/mm/mmap.c
> index 7618e72766a5..79702841c06c 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -264,8 +264,8 @@ static inline u64 file_mmap_size_max(struct file *file, struct inode *inode)
> return ULONG_MAX;
> }
>
> -static inline bool file_mmap_ok(struct file *file, struct inode *inode,
> - unsigned long pgoff, unsigned long len)
> +bool file_mmap_ok(struct file *file, struct inode *inode, unsigned long pgoff,
> + unsigned long len)
> {
> u64 maxsize = file_mmap_size_max(file, inode);
>
> diff --git a/mm/mremap.c b/mm/mremap.c
> index fff0465f5e06..8043c425b5df 100644
> --- a/mm/mremap.c
> +++ b/mm/mremap.c
> @@ -1791,6 +1791,7 @@ static int check_prep_vma(struct vma_remap_struct *vrm)
> struct mm_struct *mm = current->mm;
> unsigned long addr = vrm->addr;
> unsigned long old_len, new_len, pgoff;
> + struct file *file;
>
> if (!vma)
> return -EFAULT;
> @@ -1870,6 +1871,11 @@ static int check_prep_vma(struct vma_remap_struct *vrm)
> if (vma_is_fixed_mapping(vma))
> return -EFAULT;
>
> + /* The expanded range must obey the same offset limits as mmap(). */
> + file = vma->vm_file;
> + if (file && !file_mmap_ok(file, file_inode(file), pgoff, new_len))
> + return -EINVAL;
> +
> if (!mlock_future_ok(mm, vma_test(vma, VMA_LOCKED_BIT), vrm->delta))
> return -EAGAIN;
>
>
> --
> 2.56.0
>
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 5/7] mm/mremap: apply the mmap() offset limits on expansion
2026-11-21 16:02 ` [PATCH 5/7] mm/mremap: apply the mmap() offset limits on expansion Lorenzo Stoakes (ARM)
2026-11-22 3:37 ` Suren Baghdasaryan
@ 2026-11-22 15:50 ` Gregory Price (Meta)
2026-11-22 18:03 ` Jose A. Perez de Azpillaga
2 siblings, 0 replies; 22+ messages in thread
From: Gregory Price (Meta) @ 2026-11-22 15:50 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas, linux-mm,
Chris Mason, Josef Bacik, stable
On 2026-11-21 16:02 +0000, Lorenzo Stoakes (ARM) wrote:
> From: Chris Mason <mason@kernel.org>
>
> Commit be83bbf80682 ("mmap: introduce sane default mmap limits") made
> do_mmap() reject file mappings which extend beyond the maximum offset the
> file can support, via file_mmap_ok().
>
> However mremap() can expand a file mapping and only checks that the page
> offset does not wrap, so a mapping can be grown past the limit that mmap()
> would have enforced.
>
> Fault handlers rely on that limit. For instance dax_iomap_pte_fault()
> converts vmf->pgoff into a loff_t, which overflows for such an offset and
> defeats its i_size check.
>
> Fix the issue by applying file_mmap_ok() to the expanded range in
> check_prep_vma(), failing the mremap() with -EINVAL if it exceeds the
> limit.
>
> This means that mremap() now refuses expansions it previously permitted,
> however no file can have data at these offsets, and mmap() already refuses
> the equivalent mapping.
>
> Fixes: be83bbf80682 ("mmap: introduce sane default mmap limits")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Chris Mason <mason@kernel.org>
> [ ljs: fixed up commit msg ]
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
--
~Gregory
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 4/7] mm/vma: fix uncharge after failed stack relocation
2026-11-21 16:02 ` [PATCH 4/7] mm/vma: fix uncharge after failed stack relocation Lorenzo Stoakes (ARM)
2026-11-22 3:34 ` Suren Baghdasaryan
@ 2026-11-22 15:50 ` Gregory Price (Meta)
1 sibling, 0 replies; 22+ messages in thread
From: Gregory Price (Meta) @ 2026-11-22 15:50 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas, linux-mm,
Chris Mason, Josef Bacik, stable
On 2026-11-21 16:02 +0000, Lorenzo Stoakes (ARM) wrote:
> From: Chris Mason <mason@kernel.org>
>
> An execve() that fails to relocate its stack can underflow
> vm_committed_as, and the commit limit of vm.overcommit_memory=2 then no
> longer holds.
>
> exit_mmap() uncharges vma_pages() of each VMA with VMA_ACCOUNT_BIT, so
> the charge has to match the size of the VMA. relocate_vma_down() expands
> the stack VMA by the relocation distance without a charge, and relies on
> its vma_shrink() to make the two match again. But it returns with the
> VMA still expanded if move_page_tables() can't allocate a page table,
> for example at the memory cgroup limit, or if vma_shrink() fails:
>
> relocate_vma_down() charged vma_pages()
> =================== ======= ===========
> L L
> vma_expand() L L + shift
> move_page_tables() fails
> return -ENOMEM
>
> exit_mmap()
> tear_down_vmas()
> vm_unacct_memory(L + shift) -shift
>
> On x86-64 with 4-level page tables, the distance is up to 16 GiB for a
> 64-bit program, and about 128 TiB for a 32-bit one.
>
> Fix the issue by giving back the charge of the stack VMA where its
> relocation fails, and excluding the VMA from what exit_mmap() uncharges.
> The argument pages of the dying task are then outside the commit charge
> until exit_mmap() frees them.
>
> Fixes: b6a2fea39318 ("mm: variable length argument support")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Chris Mason <mason@kernel.org>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
--
~Gregory
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 2/7] mm/vma: lock VMAs before taking mm_all_locks_mutex
2026-11-21 16:02 ` [PATCH 2/7] mm/vma: lock VMAs before taking mm_all_locks_mutex Lorenzo Stoakes (ARM)
2026-11-22 3:23 ` Suren Baghdasaryan
@ 2026-11-22 15:53 ` Gregory Price (Meta)
2026-11-22 18:03 ` Jose A. Perez de Azpillaga
2 siblings, 0 replies; 22+ messages in thread
From: Gregory Price (Meta) @ 2026-11-22 15:53 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas, linux-mm,
Chris Mason, Josef Bacik, stable
On 2026-11-21 16:02 +0000, Lorenzo Stoakes (ARM) wrote:
> If an mmu notifier is registered for an mm while one of its page faults
> sleeps indefinitely under the VMA lock, the registration of mmu
> notifiers for every other mm can hang un-killably.
>
> mm_all_locks_mutex is shared by all mms, so its holder must not wait for
> something that has no bound.
>
> However, mm_take_all_locks() calls vma_start_write() on each VMA with the
> mutex held, and thus waits for the readers of the VMA.
>
> Since commit 4ed4379881aa ("mm: handle shared faults under the VMA lock"),
> such a reader can sleep in ->page_mkwrite().
>
> Consider fuse_page_mkwrite(), which sleeps until the FUSE server replies to
> a write request:
>
> mm A, thread 1 mm A, thread 2 mm B
> ============== ============== ====
> lock_vma_under_rcu()
> fuse_page_mkwrite()
> folio_wait_writeback()
> mm_take_all_locks()
> mutex_lock()
> vma_start_write()
> mm_take_all_locks()
> mutex_lock()
>
> Fix the issue by locking the VMAs before the mutex is taken.
>
> Additionally, make the VMA write lock killable so a fatal signal returns as
> quickly as possible.
>
> Finally, update the comments to reflect the change, dropping the lock
> ordering list which begs for bitrot.
>
> Note that the lock order at the top of mm/rmap.c doesn't appear to be right
> - the hugetlb locks are taken after the VMA write locks. However, this was
> already the case, and can be addressed in a follow-up in any case.
>
> Target commit 4ed4379881aa ("mm: handle shared faults under the VMA lock")
> as this is where it became possible for VMA lock readers to sleep in
> ->page_mkwrite().
>
> Reported-by: Chris Mason <mason@kernel.org>
> Fixes: 4ed4379881aa ("mm: handle shared faults under the VMA lock")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
--
~Gregory
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 3/7] mm/ksm: only interrupt unmerging on a fatal signal
2026-11-21 16:02 ` [PATCH 3/7] mm/ksm: only interrupt unmerging on a fatal signal Lorenzo Stoakes (ARM)
2026-11-22 3:27 ` Suren Baghdasaryan
@ 2026-11-22 15:54 ` David Hildenbrand (Arm)
2026-11-22 15:56 ` Gregory Price (Meta)
2 siblings, 0 replies; 22+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-22 15:54 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM), Liam R. Howlett, Suren Baghdasaryan,
Vlastimil Babka, Shakeel Butt, Pedro Falcato, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas
Cc: linux-mm, Chris Mason, Josef Bacik, stable
On 11/21/26 17:02, Lorenzo Stoakes (ARM) wrote:
> Commit d952b79136a6 ("ksm: fix endless loop on oom") added a check for
> pending signals on KSM break, so that a lengthy unmerge could be
> interrupted, with the system call then restarted via -ERESTARTSYS.
>
> However, it assumes that the callers can always restart whatever system
> call started it in the first place (the commit message questions whether
> this was wise).
>
> Well, it turns out that it was not wise, as this baked in an implicit
> assumption about system call restartability, and it was and is not safe for
> mremap().
>
> mremap() holds an mmap write lock over the operation, which an -ERESTARTSYS
> drops, meaning that the restart is not in a consistent state.
>
> This was made considerably worse with commit d23cb648e365 ("mm/mremap:
> permit mremap() move of multiple VMAs"), which can result in data loss if
> you encounter this issue.
>
> Consider an mremap() specifying MREMAP_FIXED | MREMAP_DONTUNMAP of two
> KSM-mergeable VMAs, where a signal becomes pending part way through:
>
> first pass restart
> ========== =======
> unmap tgt1 range
> copy src1 to tgt1, move page tables
> unmap tgt2 range
> break_ksm(src2) = -ERESTARTSYS
> unmap tgt1 range, which now
> holds the data from src1
> copy the now-empty src1 to tgt1
>
> This results in the user observing a successful mremap() but with src1's
> data missing.
>
> Without MREMAP_DONTUNMAP, remap_move() would find a gap at the start of the
> range on restart and fail with -EFAULT there instead.
>
> Fix the issue by only breaking out of an unmerge if a fatal signal is
> pending, returning -EINTR when we do. This makes KSM unmerging consistent
> with many other signal checks in mm.
>
> Target commit d23cb648e365 ("mm/mremap: permit mremap() move of multiple
> VMAs") for the fix as this made the issue significantly more impactful.
>
> Reported-by: Chris Mason <mason@kernel.org>
> Fixes: d23cb648e365 ("mm/mremap: permit mremap() move of multiple VMAs")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> ---
> mm/ksm.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/mm/ksm.c b/mm/ksm.c
> index 623a2cdb3924..5d320a200951 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -627,8 +627,8 @@ static int break_ksm_pmd_entry(pmd_t *pmdp, unsigned long addr, unsigned long en
>
> if (ksm_test_exit(walk->mm))
> return 0;
> - if (signal_pending(current))
> - return -ERESTARTSYS;
> + if (fatal_signal_pending(current))
> + return -EINTR;
>
> start_ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
> if (!start_ptep)
>
Thanks, I'll queue this in mm-core for the next merge window.
Everything else can be merged by Andrew.
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 1/7] mm/vma: error out on invalid bounds in unmapped_area_topdown()
2026-11-21 16:02 ` [PATCH 1/7] mm/vma: error out on invalid bounds in unmapped_area_topdown() Lorenzo Stoakes (ARM)
2026-11-22 3:20 ` Suren Baghdasaryan
@ 2026-11-22 15:55 ` Gregory Price (Meta)
2026-11-22 18:03 ` Jose A. Perez de Azpillaga
2 siblings, 0 replies; 22+ messages in thread
From: Gregory Price (Meta) @ 2026-11-22 15:55 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas, linux-mm,
Chris Mason, Josef Bacik, stable
On 2026-11-21 16:02 +0000, Lorenzo Stoakes (ARM) wrote:
> unmapped_area_topdown() suffers from a class of bug arising from a mismatch
> between how maple tree functions and VMA iterator functions express ranges
> - the former inclusively, the latter exclusively.
>
> This is problematic with an exclusive upper bound of 0, as it translates
> to an inclusive upper bound of ULONG_MAX, that is a range which extends to
> the end of the address space, and start < end or equivalent comparisons
> evaluate false in a situation where in fact the expression should evaluate
> true.
>
> unmapped_area_topdown() calls vma_iter_area_highest() to position the VMA
> iterator into the next highest empty range spanning [low_limit,
> high_limit), iteratively reducing high_limit without checking whether it
> has reached 0.
>
> This can result in a [low_limit, 0) pair being passed to
> vma_iter_area_highest() and thus unexpected underflow. Consider:
>
> vm_start = 0x30000
> stack_guard_gap = 0x100000
> info->length = 0x1000
> high_limit = vm_start_gap() = 0
> high_limit - 1 = ULONG_MAX
> window = [low_limit, ULONG_MAX]
> vma_iter_end() = ULONG_MAX + 1 = 0
> gap = 0 - info->length = 0xfffffffffffff000
>
> The result is page aligned and below the IS_ERR_VALUE() range, so it
> does not look like an error. What follows depends on the caller:
>
> - mmap() with no hint can fail with -ENOMEM while space is free. The
> bottom-up fallback is skipped, and __get_unmapped_area() refuses the
> address as above TASK_SIZE.
> - On x86-64, find_nearest_trampoline() can pick the address for the
> uprobe trampoline. _install_special_mapping() then inserts a VMA
> whose vm_end is 0, and exit_mmap() frees kernel page tables that
> every mm shares.
> - On powerpc64 with the hash MMU, slice_get_unmapped_area() can pass
> the address to alloc_extended_context(), which then writes out of
> bounds of mm->context.extended_id[].
>
> mmap() can succeed with an address from the wrapped window where the
> range above TASK_SIZE is too short for the request and its alignment
> padding. On s390 that range is one page, so this takes no more than the
> default stack_guard_gap.
>
> Fix the issue by converting the loop into one that explicitly tests
> low_limit < high_limit.
>
> Additionally, simplify the logic by declaring the VMA iterator in the
> loop (it is reset on each iteration anyway) and exiting with the correct
> output value on success rather than performing a goto when an iteration is
> required.
>
> Reported-by: Chris Mason <mason@kernel.org>
> Fixes: 58c5d0d6d522 ("mm/mmap: regression fix for unmapped_area{_topdown}")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
--
~Gregory
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: (subset) [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes
2026-11-21 16:02 [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes Lorenzo Stoakes (ARM)
` (4 preceding siblings ...)
2026-11-21 16:02 ` [PATCH 5/7] mm/mremap: apply the mmap() offset limits on expansion Lorenzo Stoakes (ARM)
@ 2026-11-22 15:55 ` David Hildenbrand
2026-11-22 15:57 ` Gregory Price (Meta)
6 siblings, 0 replies; 22+ messages in thread
From: David Hildenbrand @ 2026-11-22 15:55 UTC (permalink / raw)
To: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas,
Lorenzo Stoakes (ARM)
Cc: linux-mm, Chris Mason, Josef Bacik, stable
On Sat, 21 Nov 2026 16:02:26 +0000, Lorenzo Stoakes (ARM) wrote:
> I have reordered the patches here by priority, David - the first 2 should
> be prioritised as hotfixes, the rest can go in 7.5:
>
> == Critical ==
>
> 1. unmapped_area_topdown() invalid bounds - possible memory corruption,
> triggerable by any local user.
> 2. Lock VMAs before mm_all_locks_mutex - system-wide unkillable hang,
> triggerable by a local user with FUSE.
>
> [...]
Applied, thanks!
tree: mm/core.git
branch: ksm-7.5.misc
Applied patches will appear soon in mm-next, followed by linux-next.
Please report any outstanding bugs that were missed during review, so we
can either drop the patch series or decide how to fix it up.
It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even
though the patches have already been applied. If possible, patch trailers
will be updated.
Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. Further, this is not a guarantee that the
patches will go upstream. If in doubt, please check the listed branch.
[3/7] mm/ksm: only interrupt unmerging on a fatal signal
https://git.kernel.org/mm/core/c/d90efc5d
--
Cheers,
David
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 3/7] mm/ksm: only interrupt unmerging on a fatal signal
2026-11-21 16:02 ` [PATCH 3/7] mm/ksm: only interrupt unmerging on a fatal signal Lorenzo Stoakes (ARM)
2026-11-22 3:27 ` Suren Baghdasaryan
2026-11-22 15:54 ` David Hildenbrand (Arm)
@ 2026-11-22 15:56 ` Gregory Price (Meta)
2 siblings, 0 replies; 22+ messages in thread
From: Gregory Price (Meta) @ 2026-11-22 15:56 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas, linux-mm,
Chris Mason, Josef Bacik, stable
On 2026-11-21 16:02 +0000, Lorenzo Stoakes (ARM) wrote:
> Commit d952b79136a6 ("ksm: fix endless loop on oom") added a check for
> pending signals on KSM break, so that a lengthy unmerge could be
> interrupted, with the system call then restarted via -ERESTARTSYS.
>
> However, it assumes that the callers can always restart whatever system
> call started it in the first place (the commit message questions whether
> this was wise).
>
> Well, it turns out that it was not wise, as this baked in an implicit
> assumption about system call restartability, and it was and is not safe for
> mremap().
>
> mremap() holds an mmap write lock over the operation, which an -ERESTARTSYS
> drops, meaning that the restart is not in a consistent state.
>
> This was made considerably worse with commit d23cb648e365 ("mm/mremap:
> permit mremap() move of multiple VMAs"), which can result in data loss if
> you encounter this issue.
>
> Consider an mremap() specifying MREMAP_FIXED | MREMAP_DONTUNMAP of two
> KSM-mergeable VMAs, where a signal becomes pending part way through:
>
> first pass restart
> ========== =======
> unmap tgt1 range
> copy src1 to tgt1, move page tables
> unmap tgt2 range
> break_ksm(src2) = -ERESTARTSYS
> unmap tgt1 range, which now
> holds the data from src1
> copy the now-empty src1 to tgt1
>
> This results in the user observing a successful mremap() but with src1's
> data missing.
>
> Without MREMAP_DONTUNMAP, remap_move() would find a gap at the start of the
> range on restart and fail with -EFAULT there instead.
>
> Fix the issue by only breaking out of an unmerge if a fatal signal is
> pending, returning -EINTR when we do. This makes KSM unmerging consistent
> with many other signal checks in mm.
>
> Target commit d23cb648e365 ("mm/mremap: permit mremap() move of multiple
> VMAs") for the fix as this made the issue significantly more impactful.
>
> Reported-by: Chris Mason <mason@kernel.org>
> Fixes: d23cb648e365 ("mm/mremap: permit mremap() move of multiple VMAs")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
--
~Gregory
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes
2026-11-21 16:02 [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes Lorenzo Stoakes (ARM)
` (5 preceding siblings ...)
2026-11-22 15:55 ` (subset) [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes David Hildenbrand
@ 2026-11-22 15:57 ` Gregory Price (Meta)
6 siblings, 0 replies; 22+ messages in thread
From: Gregory Price (Meta) @ 2026-11-22 15:57 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Liam R. Howlett, Suren Baghdasaryan, Vlastimil Babka,
Shakeel Butt, Pedro Falcato, David Hildenbrand, Jann Horn,
Andrew Morton, Matthew Wilcox (Oracle), Kees Cook, Peter Zijlstra,
Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou, Mike Rapoport,
Michal Hocko, Hugh Dickins, Joey Gouly, Catalin Marinas, linux-mm,
Chris Mason, Josef Bacik, stable
On 2026-11-21 16:02 +0000, Lorenzo Stoakes (ARM) wrote:
> I have reordered the patches here by priority, David - the first 2 should
> be prioritised as hotfixes, the rest can go in 7.5:
>
> == Critical ==
>
> 1. unmapped_area_topdown() invalid bounds - possible memory corruption,
> triggerable by any local user.
> 2. Lock VMAs before mm_all_locks_mutex - system-wide unkillable hang,
> triggerable by a local user with FUSE.
>
> == Need fixing ==
>
> 3. KSM unmerge only interrupted by fatal signal - silent data loss on
> mremap() of KSM memory.
> 4. Uncharge after failed stack relocation - strict overcommit limit
> defeated system-wide.
> 5. mmap() offset limits on mremap() expansion - invalid file offset
> reaches the DAX fault handler.
>
> == Hardening (no need for Cc: stable) ==
>
> 6. Guard gaps on in-place mremap() expansion.
> 7. Non-executable brk mappings under MDWE.
>
> As usual, please go through and check these carefully for correctness,
> sanity.
>
> Note that one of the original patches was punted over to the BPF
> guys ([0]), as it doesn't feel that it'd make sense to change core mm to
> address it.
>
> [0]:https://lore.kernel.org/all/awGZWsCp6v63H_py@gremlin/
>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Was able to reproduce every issue and validate the fix.
Not sure I agree with your assessment of NX brk mappings not needing
stable, that's a pretty bad bug in my opinion.
--
~Gregory
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 1/7] mm/vma: error out on invalid bounds in unmapped_area_topdown()
2026-11-21 16:02 ` [PATCH 1/7] mm/vma: error out on invalid bounds in unmapped_area_topdown() Lorenzo Stoakes (ARM)
2026-11-22 3:20 ` Suren Baghdasaryan
2026-11-22 15:55 ` Gregory Price (Meta)
@ 2026-11-22 18:03 ` Jose A. Perez de Azpillaga
2 siblings, 0 replies; 22+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-22 18:03 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM), Liam R. Howlett, Suren Baghdasaryan,
Vlastimil Babka, Shakeel Butt, Pedro Falcato, David Hildenbrand,
Jann Horn, Andrew Morton, Matthew Wilcox (Oracle), Kees Cook,
Peter Zijlstra, Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou,
Mike Rapoport, Michal Hocko, Hugh Dickins, Joey Gouly,
Catalin Marinas
Cc: linux-mm, Chris Mason, Josef Bacik, stable
On 11/21/26 5:02 PM, Lorenzo Stoakes (ARM) wrote:
> unmapped_area_topdown() suffers from a class of bug arising from a mismatch
> between how maple tree functions and VMA iterator functions express ranges
> - the former inclusively, the latter exclusively.
>
> This is problematic with an exclusive upper bound of 0, as it translates
> to an inclusive upper bound of ULONG_MAX, that is a range which extends to
> the end of the address space, and start < end or equivalent comparisons
> evaluate false in a situation where in fact the expression should evaluate
> true.
>
> unmapped_area_topdown() calls vma_iter_area_highest() to position the VMA
> iterator into the next highest empty range spanning [low_limit,
> high_limit), iteratively reducing high_limit without checking whether it
> has reached 0.
>
> This can result in a [low_limit, 0) pair being passed to
> vma_iter_area_highest() and thus unexpected underflow. Consider:
>
> vm_start = 0x30000
> stack_guard_gap = 0x100000
> info->length = 0x1000
> high_limit = vm_start_gap() = 0
> high_limit - 1 = ULONG_MAX
> window = [low_limit, ULONG_MAX]
> vma_iter_end() = ULONG_MAX + 1 = 0
> gap = 0 - info->length = 0xfffffffffffff000
>
> The result is page aligned and below the IS_ERR_VALUE() range, so it
> does not look like an error. What follows depends on the caller:
>
> - mmap() with no hint can fail with -ENOMEM while space is free. The
> bottom-up fallback is skipped, and __get_unmapped_area() refuses the
> address as above TASK_SIZE.
> - On x86-64, find_nearest_trampoline() can pick the address for the
> uprobe trampoline. _install_special_mapping() then inserts a VMA
> whose vm_end is 0, and exit_mmap() frees kernel page tables that
> every mm shares.
> - On powerpc64 with the hash MMU, slice_get_unmapped_area() can pass
> the address to alloc_extended_context(), which then writes out of
> bounds of mm->context.extended_id[].
>
> mmap() can succeed with an address from the wrapped window where the
> range above TASK_SIZE is too short for the request and its alignment
> padding. On s390 that range is one page, so this takes no more than the
> default stack_guard_gap.
>
> Fix the issue by converting the loop into one that explicitly tests
> low_limit < high_limit.
>
> Additionally, simplify the logic by declaring the VMA iterator in the
> loop (it is reset on each iteration anyway) and exiting with the correct
> output value on success rather than performing a goto when an iteration is
> required.
>
> Reported-by: Chris Mason <mason@kernel.org>
> Fixes: 58c5d0d6d522 ("mm/mmap: regression fix for unmapped_area{_topdown}")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
LGTM:
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
--
cheers,
jose a. p-a
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 2/7] mm/vma: lock VMAs before taking mm_all_locks_mutex
2026-11-21 16:02 ` [PATCH 2/7] mm/vma: lock VMAs before taking mm_all_locks_mutex Lorenzo Stoakes (ARM)
2026-11-22 3:23 ` Suren Baghdasaryan
2026-11-22 15:53 ` Gregory Price (Meta)
@ 2026-11-22 18:03 ` Jose A. Perez de Azpillaga
2 siblings, 0 replies; 22+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-22 18:03 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM), Liam R. Howlett, Suren Baghdasaryan,
Vlastimil Babka, Shakeel Butt, Pedro Falcato, David Hildenbrand,
Jann Horn, Andrew Morton, Matthew Wilcox (Oracle), Kees Cook,
Peter Zijlstra, Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou,
Mike Rapoport, Michal Hocko, Hugh Dickins, Joey Gouly,
Catalin Marinas
Cc: linux-mm, Chris Mason, Josef Bacik, stable
On 11/21/26 5:02 PM, Lorenzo Stoakes (ARM) wrote:
> If an mmu notifier is registered for an mm while one of its page faults
> sleeps indefinitely under the VMA lock, the registration of mmu
> notifiers for every other mm can hang un-killably.
>
> mm_all_locks_mutex is shared by all mms, so its holder must not wait for
> something that has no bound.
>
> However, mm_take_all_locks() calls vma_start_write() on each VMA with the
> mutex held, and thus waits for the readers of the VMA.
>
> Since commit 4ed4379881aa ("mm: handle shared faults under the VMA lock"),
> such a reader can sleep in ->page_mkwrite().
>
> Consider fuse_page_mkwrite(), which sleeps until the FUSE server replies to
> a write request:
>
> mm A, thread 1 mm A, thread 2 mm B
> ============== ============== ====
> lock_vma_under_rcu()
> fuse_page_mkwrite()
> folio_wait_writeback()
> mm_take_all_locks()
> mutex_lock()
> vma_start_write()
> mm_take_all_locks()
> mutex_lock()
>
> Fix the issue by locking the VMAs before the mutex is taken.
>
> Additionally, make the VMA write lock killable so a fatal signal returns as
> quickly as possible.
>
> Finally, update the comments to reflect the change, dropping the lock
> ordering list which begs for bitrot.
>
> Note that the lock order at the top of mm/rmap.c doesn't appear to be right
> - the hugetlb locks are taken after the VMA write locks. However, this was
> already the case, and can be addressed in a follow-up in any case.
>
> Target commit 4ed4379881aa ("mm: handle shared faults under the VMA lock")
> as this is where it became possible for VMA lock readers to sleep in
> ->page_mkwrite().
>
> Reported-by: Chris Mason <mason@kernel.org>
> Fixes: 4ed4379881aa ("mm: handle shared faults under the VMA lock")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
LGTM:
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
--
cheers,
jose a. p-a
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH 5/7] mm/mremap: apply the mmap() offset limits on expansion
2026-11-21 16:02 ` [PATCH 5/7] mm/mremap: apply the mmap() offset limits on expansion Lorenzo Stoakes (ARM)
2026-11-22 3:37 ` Suren Baghdasaryan
2026-11-22 15:50 ` Gregory Price (Meta)
@ 2026-11-22 18:03 ` Jose A. Perez de Azpillaga
2 siblings, 0 replies; 22+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-22 18:03 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM), Liam R. Howlett, Suren Baghdasaryan,
Vlastimil Babka, Shakeel Butt, Pedro Falcato, David Hildenbrand,
Jann Horn, Andrew Morton, Matthew Wilcox (Oracle), Kees Cook,
Peter Zijlstra, Ollie Wild, Adrian Bunk, Xu Xin, Chengming Zhou,
Mike Rapoport, Michal Hocko, Hugh Dickins, Joey Gouly,
Catalin Marinas
Cc: linux-mm, Chris Mason, Josef Bacik, stable
On 11/21/26 5:02 PM, Lorenzo Stoakes (ARM) wrote:
> From: Chris Mason <mason@kernel.org>
>
> Commit be83bbf80682 ("mmap: introduce sane default mmap limits") made
> do_mmap() reject file mappings which extend beyond the maximum offset the
> file can support, via file_mmap_ok().
>
> However mremap() can expand a file mapping and only checks that the page
> offset does not wrap, so a mapping can be grown past the limit that mmap()
> would have enforced.
>
> Fault handlers rely on that limit. For instance dax_iomap_pte_fault()
> converts vmf->pgoff into a loff_t, which overflows for such an offset and
> defeats its i_size check.
>
> Fix the issue by applying file_mmap_ok() to the expanded range in
> check_prep_vma(), failing the mremap() with -EINVAL if it exceeds the
> limit.
>
> This means that mremap() now refuses expansions it previously permitted,
> however no file can have data at these offsets, and mmap() already refuses
> the equivalent mapping.
>
> Fixes: be83bbf80682 ("mmap: introduce sane default mmap limits")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Chris Mason <mason@kernel.org>
> [ ljs: fixed up commit msg ]
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
LGTM:
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
--
cheers,
jose a. p-a
^ permalink raw reply [flat|nested] 22+ messages in thread
end of thread, other threads:[~2026-11-22 18:03 UTC | newest]
Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 16:02 [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes Lorenzo Stoakes (ARM)
2026-11-21 16:02 ` [PATCH 1/7] mm/vma: error out on invalid bounds in unmapped_area_topdown() Lorenzo Stoakes (ARM)
2026-11-22 3:20 ` Suren Baghdasaryan
2026-11-22 15:55 ` Gregory Price (Meta)
2026-11-22 18:03 ` Jose A. Perez de Azpillaga
2026-11-21 16:02 ` [PATCH 2/7] mm/vma: lock VMAs before taking mm_all_locks_mutex Lorenzo Stoakes (ARM)
2026-11-22 3:23 ` Suren Baghdasaryan
2026-11-22 15:53 ` Gregory Price (Meta)
2026-11-22 18:03 ` Jose A. Perez de Azpillaga
2026-11-21 16:02 ` [PATCH 3/7] mm/ksm: only interrupt unmerging on a fatal signal Lorenzo Stoakes (ARM)
2026-11-22 3:27 ` Suren Baghdasaryan
2026-11-22 15:54 ` David Hildenbrand (Arm)
2026-11-22 15:56 ` Gregory Price (Meta)
2026-11-21 16:02 ` [PATCH 4/7] mm/vma: fix uncharge after failed stack relocation Lorenzo Stoakes (ARM)
2026-11-22 3:34 ` Suren Baghdasaryan
2026-11-22 15:50 ` Gregory Price (Meta)
2026-11-21 16:02 ` [PATCH 5/7] mm/mremap: apply the mmap() offset limits on expansion Lorenzo Stoakes (ARM)
2026-11-22 3:37 ` Suren Baghdasaryan
2026-11-22 15:50 ` Gregory Price (Meta)
2026-11-22 18:03 ` Jose A. Perez de Azpillaga
2026-11-22 15:55 ` (subset) [PATCH 0/7] mm: reworked AI-generated VMA and mremap fixes David Hildenbrand
2026-11-22 15:57 ` Gregory Price (Meta)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox