mm: various (urgent?) fixes
Subsystems (4)
David Hildenbrand (Arm)
reviewercover mm: various (urgent?) fixes
Hi,
here are various MM fixes. The majority was reported by Josef at Anthrophic, the remainder was found while reviewing/overhauling them.
Two issues/fixes reported are not included, as we are discussing (better) alternatives on the list already.
I left most of the original commit messages as schloppy as they are: I don't particularly care as long as the code is in good shape and as long as they are not actively wrong. But I (and Mike) could not be stopped from overhauling at least some of them.
While I don't think there is anything too critical in here, I think this is a good opportunity to see how fast we can get pre-screened/pre-reviewed fixes upstream that might be security-related.
The majority are hugetlb + uffd issues, that are not actually that relevant for most installations. But all things that should just get fixed. Some uffd fixes need proper follow ups to fix some more of the root causes of leftover UFFD PTE bits (or proof that there are none remaining).
I ran some MM selftests and nothing popped up.
So let's try to get these upstream in a timely fashion.
---
David Hildenbrand (Arm) (4):
mm/hugetlb: fix use-after-free in hugetlb_mfill_atomic_pte() mm/hugetlb: tolerate uffd bits set without uffd for PROT_NONE PTEs mm/hugetlb: tolerate UFFD_WP markers outside uffd-wp VMAs mm/mremap: fix calling fixup_hugetlb_reservations() on the wrong VMA
Josef Bacik (6):
mm/vma: don't leak the commit charge on mmap failure mm/hugetlb: fix use-after-free of a new anon folio mm/hugetlb: unmap the page where the other VMA maps it mm/hugetlb: don't clear the resv_map of a VMA whose lock is held mm/hugetlb: fix endless write fault on a leftover uffd bit mm/hugetlb: don't leave a uffd-wp marker when zapping an RWP entry
mm/hugetlb.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++-------------- mm/mremap.c | 2 +- mm/vma.c | 11 ++++++---- 3 files changed, 62 insertions(+), 21 deletions(-)
---
base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053
change-id: 20261015-urgent_fixes-7112629346eb
--
Cheers,
David
David Hildenbrand (Arm)
reviewerPATCH 1/10 mm/vma: don't leak the commit charge on mmap failure
From: Josef Bacik <[email protected]>
When an accountable mapping replaces existing mappings (MAP_FIXED), __mmap_setup() only charges the difference between the size of the new mapping and the VM_ACCOUNT pages it replaces. It then zeroes vms->nr_accounted so that unmapping the old VMAs does not return their charge, which now backs the new mapping.
If the operation fails after that point, the error path returns map->charged, i.e. only the difference. By then vms_clean_up_area() has zapped the page tables and closed the old VMAs, so vms_abort_munmap_vmas() cannot reattach them. It leaves a gap and finishes the unmap with vms_complete_munmap_vmas(), which uncharges vms->nr_accounted == 0 pages. The old VMAs are gone, the new one was never created, and the charge of the old ones is never returned, not even when the process exits.
No privilege is needed to hit this, only a file whose mmap hook fails, for example a socket (sock_no_mmap() returns -ENODEV):
p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); mmap(p, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, socket_fd, 0);
Every iteration adds len to Committed_AS for good. With vm.overcommit_memory=2 this lets any user make all further accountable allocations in the system fail with -ENOMEM.
Once the new mapping has taken over the charge of the mappings it replaces, it owns the charge for all of its pages. Record that in map->charged, so that the error path returns all of it.
A failure in __mmap_setup() itself is not affected: it happens before the charge is taken over and the old VMAs are reattached with their charge intact.
Original fix generated by an LLM and sent in private by Josef.
--- mm/vma.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
mm/vma.c +7 -4
@@ -2577,14 +2577,17 @@ static int __mmap_setup(struct mmap_state *map, struct vm_area_desc *desc,/* Private writable mapping: check memory availability. */ if (accountable_mapping (map)) { - map->charged = map->pglen; - map->charged -= vms->nr_accounted; -if (map->charged) { - error =security_vm_enough_memory_mm (map->mm, map->charged); +const unsigned long uncharged = map->pglen - vms->nr_accounted; + +/* Charge only what's not already charged by mappings we replace. */ +if (uncharged) { + error =security_vm_enough_memory_mm (map->mm, uncharged);if (error)return error; } +/* Our new mapping owns the entire charge now. */ + map->charged = map->pglen; vms->nr_accounted =0 ;vma_flags_set (&map->vma_flags,VMA_ACCOUNT_BIT ); }
Signature · 1 line
2.43.0
Re: [PATCH 01/10] mm/vma: don't leak the commit charge on mmap failure
Quoted 20 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
When an accountable mapping replaces existing mappings (
MAP_FIXED),__mmap_setup()only charges the difference between the size of the new mapping and theVM_ACCOUNTpages it replaces. It then zeroesvms->nr_accountedso that unmapping the old VMAs does not return their charge, which now backs the new mapping.If the operation fails after that point, the error path returns
map->charged, i.e. only the difference. By thenvms_clean_up_area()has zapped the page tables and closed the old VMAs, sovms_abort_munmap_vmas()cannot reattach them. It leaves a gap and finishes the unmap withvms_complete_munmap_vmas(), which unchargesvms->nr_accounted== 0 pages. The old VMAs are gone, the new one was never created, and the charge of the old ones is never returned, not even when the process exits.No privilege is needed to hit this, only a file whose mmap hook fails, for example a socket (
sock_no_mmap()returns-ENODEV):
Quoted 8 lines
p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); mmap(p, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, socket_fd, 0);Every iteration adds len to
Committed_ASfor good. With vm.overcommit_memory=2 this lets any user make all further accountable allocations in the system fail with-ENOMEM.
I don't think is urgent nor does it have security implications. You can already broadly screw up the system from any user when overcommit_memory=2. This just goes around the single-process user_reserve_kbytes AFAICT. Am I missing something?
Just looks like a good old bug to me.
Quoted 9 lines
Once the new mapping has taken over the charge of the mappings it replaces, it owns the charge for all of its pages. Record that in
map->charged, so that the error path returns all of it.A failure in
__mmap_setup()itself is not affected: it happens before the charge is taken over and the old VMAs are reattached with their charge intact.
Quoted 8 lines
Original fix generated by an LLM and sent in private by Josef.
Fixes 4f87153e82c4 ("mm: change failure of MAP_FIXED to restoring the gap on failure")Signed-off-by Josef Bacik <[email protected]>Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>[ david: call variable "uncharged" and make it const, adjust/simplify comments ]Signed-off-by David Hildenbrand (Arm) <[email protected]>
Signature · 1 line
Pedro
David Hildenbrand (Arm)
reviewerRe: [PATCH 01/10] mm/vma: don't leak the commit charge on mmap failure
Quoted 30 lines · Pedro Falcato
Quoted 20 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
When an accountable mapping replaces existing mappings (
MAP_FIXED),__mmap_setup()only charges the difference between the size of the new mapping and theVM_ACCOUNTpages it replaces. It then zeroesvms->nr_accountedso that unmapping the old VMAs does not return their charge, which now backs the new mapping.If the operation fails after that point, the error path returns
map->charged, i.e. only the difference. By thenvms_clean_up_area()has zapped the page tables and closed the old VMAs, sovms_abort_munmap_vmas()cannot reattach them. It leaves a gap and finishes the unmap withvms_complete_munmap_vmas(), which unchargesvms->nr_accounted== 0 pages. The old VMAs are gone, the new one was never created, and the charge of the old ones is never returned, not even when the process exits.No privilege is needed to hit this, only a file whose mmap hook fails, for example a socket (
sock_no_mmap()returns-ENODEV):Quoted 8 lines
p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); mmap(p, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, socket_fd, 0);Every iteration adds len to
Committed_ASfor good. With vm.overcommit_memory=2 this lets any user make all further accountable allocations in the system fail with-ENOMEM.
Quoted 6 lines
I don't think is urgent nor does it have security implications. You can already broadly screw up the system from any user when overcommit_memory=2. This just goes around the single-process
user_reserve_kbytesAFAICT. Am I missing something?Just looks like a good old bug to me.
Yes, most of the things here are just good old bugs with limited criticality (as I expressed in the cover letter and the "urgent?" highlight in the subject).
Things like use-after-free or deadlocks are rather bad, but they are mostly hugetlb specific, requiring actual allocated hugetlb pages in the pool. So there is limited attack surface in practice.
All valid things we should be fixing, though.
I suspect we have a flood of MM fixes coming our way with differing criticality, so this here is one attempt to figure out how well we can deal with the mostly-trivial ones in a fairly controlled (e.g., proper human review) yet efficient way.
Anything less trivial or shaky needs proper time to settle, as always.
Signature · 3 lines
Cheers, David
Re: [PATCH 01/10] mm/vma: don't leak the commit charge on mmap failure
In reply to David Hildenbrand (Arm)Quoted 37 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
When an accountable mapping replaces existing mappings (
MAP_FIXED),__mmap_setup()only charges the difference between the size of the new mapping and theVM_ACCOUNTpages it replaces. It then zeroesvms->nr_accountedso that unmapping the old VMAs does not return their charge, which now backs the new mapping.If the operation fails after that point, the error path returns
map->charged, i.e. only the difference. By thenvms_clean_up_area()has zapped the page tables and closed the old VMAs, sovms_abort_munmap_vmas()cannot reattach them. It leaves a gap and finishes the unmap withvms_complete_munmap_vmas(), which unchargesvms->nr_accounted== 0 pages. The old VMAs are gone, the new one was never created, and the charge of the old ones is never returned, not even when the process exits.No privilege is needed to hit this, only a file whose mmap hook fails, for example a socket (
sock_no_mmap()returns-ENODEV):p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); mmap(p, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, socket_fd, 0);Every iteration adds len to
Committed_ASfor good. With vm.overcommit_memory=2 this lets any user make all further accountable allocations in the system fail with-ENOMEM.Once the new mapping has taken over the charge of the mappings it replaces, it owns the charge for all of its pages. Record that in
map->charged, so that the error path returns all of it.A failure in
__mmap_setup()itself is not affected: it happens before the charge is taken over and the old VMAs are reattached with their charge intact.
Quoted 8 lines
Original fix generated by an LLM and sent in private by Josef.
Fixes 4f87153e82c4 ("mm: change failure of MAP_FIXED to restoring the gap on failure")Signed-off-by Josef Bacik <[email protected]>Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>[ david: call variable "uncharged" and make it const, adjust/simplify comments ]Signed-off-by David Hildenbrand (Arm) <[email protected]>
this LGTM.
Signature · 2 lines
cheers, jose a. p-a
Re: [PATCH 01/10] mm/vma: don't leak the commit charge on mmap failure
In reply to David Hildenbrand (Arm)Quoted 39 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
When an accountable mapping replaces existing mappings (
MAP_FIXED),__mmap_setup()only charges the difference between the size of the new mapping and theVM_ACCOUNTpages it replaces. It then zeroesvms->nr_accountedso that unmapping the old VMAs does not return their charge, which now backs the new mapping.If the operation fails after that point, the error path returns
map->charged, i.e. only the difference. By thenvms_clean_up_area()has zapped the page tables and closed the old VMAs, sovms_abort_munmap_vmas()cannot reattach them. It leaves a gap and finishes the unmap withvms_complete_munmap_vmas(), which unchargesvms->nr_accounted== 0 pages. The old VMAs are gone, the new one was never created, and the charge of the old ones is never returned, not even when the process exits.No privilege is needed to hit this, only a file whose mmap hook fails, for example a socket (
sock_no_mmap()returns-ENODEV):p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); mmap(p, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, socket_fd, 0);Every iteration adds len to
Committed_ASfor good. With vm.overcommit_memory=2 this lets any user make all further accountable allocations in the system fail with-ENOMEM.Once the new mapping has taken over the charge of the mappings it replaces, it owns the charge for all of its pages. Record that in
map->charged, so that the error path returns all of it.A failure in
__mmap_setup()itself is not affected: it happens before the charge is taken over and the old VMAs are reattached with their charge intact.Original fix generated by an LLM and sent in private by Josef.
Quoted 7 lines
Fixes 4f87153e82c4 ("mm: change failure of MAP_FIXED to restoring the gap on failure")Signed-off-by Josef Bacik <[email protected]>Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>[ david: call variable "uncharged" and make it const, adjust/simplify comments ]Signed-off-by David Hildenbrand (Arm) <[email protected]>---
David Hildenbrand (Arm)
reviewerPATCH 2/10 mm/hugetlb: fix use-after-free of a new anon folio
In reply to David Hildenbrand (Arm)From: Josef Bacik <[email protected]>
For a fault in a private mapping with nothing in the page cache, hugetlb_no_page() allocates a folio, locks it and maps it. The folio has one reference, which belongs to the page table entry from then on. After dropping the page table lock the function still sets the migratable flag of the folio and unlocks it.
Whoever zaps the entry in between frees the folio. The fault holds the fault mutex and the lock of the vma. MADV_DONTNEED and the first unmap of a hole punch, hugetlb_vmdelete_list(), do not take the fault mutex, and a private vma only has a lock if it owns a reservation map: not with MAP_NORESERVE, and not in the child after fork().
Then the flag is set in, and folio_unlock() is called on, a folio that is free or has a new owner. A surplus page goes back to the page allocator while it is locked:
BUG: Bad page state in process init pfn:108e00 flags: 0x200000000000059(locked|uptodate|dirty|head|node=0|zone=2) page dumped because: PAGE_FLAGS_CHECK_AT_FREE flag(s) set
A page of the pool can be given to the next user, of any file, who locks it and loses the lock to the late folio_unlock():
kernel BUG at mm/filemap.c:1532!
hugetlb_no_page+0x111b/0x23e0 hugetlb_fault+0x3a9/0x14a0
An unprivileged process is enough: threads that write to a MAP_PRIVATE | MAP_NORESERVE hugetlb mapping, threads that call MADV_DONTNEED on it, and one that takes the freed pages for another file.
Until commit 3a47c54f09c4 ("hugetlbfs: revert use i_mmap_rwsem for more pmd sharing synchronization") the fault held i_mmap_rwsem for reading, which everybody who zaps takes for writing. The lock that commit bf4916922c60 ("hugetlbfs: extend hugetlb_vma_lock to private VMAs") gave to private vmas closed the window again, but only for those with a reservation map.
Be done with the folio before the page table lock is dropped, as hugetlb_wp() is with its copy. The function already unlocks page cache folios under that lock.
Original fix generated by an LLM and sent in private by Josef.
--- mm/hugetlb.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
mm/hugetlb.c +6 -2
@@ -5949,8 +5949,6 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, ret =hugetlb_wp (vmf); } -spin_unlock (vmf->ptl); -/* * Only set hugetlb_migratable in newly allocated pages. Existing pages * found in the pagecache may not have hugetlb_migratable if they have
@@ -5961,6 +5959,12 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,if (folio_locked)folio_unlock (folio); + +/* +* Hold the PTL until we're done with the folio: the page table owns the +* folio reference now. +*/ +spin_unlock (vmf->ptl); out:hugetlb_vma_unlock_read (vma);
Signature · 1 line
2.43.0
Re: [PATCH 02/10] mm/hugetlb: fix use-after-free of a new anon folio
Quoted 48 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
For a fault in a private mapping with nothing in the page cache,
hugetlb_no_page()allocates a folio, locks it and maps it. The folio has one reference, which belongs to the page table entry from then on. After dropping the page table lock the function still sets the migratable flag of the folio and unlocks it.Whoever zaps the entry in between frees the folio. The fault holds the fault mutex and the lock of the vma.
MADV_DONTNEEDand the first unmap of a hole punch,hugetlb_vmdelete_list(), do not take the fault mutex, and a private vma only has a lock if it owns a reservation map: not withMAP_NORESERVE, and not in the child afterfork().Then the flag is set in, and
folio_unlock()is called on, a folio that is free or has a new owner. A surplus page goes back to the page allocator while it is locked:BUG: Bad page state in process init pfn:108e00 flags: 0x200000000000059(locked|uptodate|dirty|head|node=0|zone=2) page dumped because: PAGE_FLAGS_CHECK_AT_FREE flag(s) setA page of the pool can be given to the next user, of any file, who locks it and loses the lock to the late
folio_unlock():kernel BUG at
mm/filemap.c:1532!hugetlb_no_page+0x111b/0x23e0 hugetlb_fault+0x3a9/0x14a0An unprivileged process is enough: threads that write to a
MAP_PRIVATE|MAP_NORESERVEhugetlb mapping, threads that callMADV_DONTNEEDon it, and one that takes the freed pages for another file.Until commit
3a47c54f09c4("hugetlbfs: revert usei_mmap_rwsemfor more pmd sharing synchronization") the fault heldi_mmap_rwsemfor reading, which everybody who zaps takes for writing. The lock that commitbf4916922c60("hugetlbfs: extendhugetlb_vma_lockto private VMAs") gave to private vmas closed the window again, but only for those with a reservation map.Be done with the folio before the page table lock is dropped, as
hugetlb_wp()is with its copy. The function already unlocks page cache folios under that lock.
Quoted 8 lines
Original fix generated by an LLM and sent in private by Josef.
Fixes 3a47c54f09c4 ("hugetlbfs: revert use i_mmap_rwsem for more pmd sharing synchronization")Signed-off-by Josef Bacik <[email protected]>Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>[ david: simplify+move comment ]Signed-off-by David Hildenbrand (Arm) <[email protected]>
Thanks.
Re: [PATCH 02/10] mm/hugetlb: fix use-after-free of a new anon folio
In reply to David Hildenbrand (Arm)Quoted 46 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
For a fault in a private mapping with nothing in the page cache,
hugetlb_no_page()allocates a folio, locks it and maps it. The folio has one reference, which belongs to the page table entry from then on. After dropping the page table lock the function still sets the migratable flag of the folio and unlocks it.Whoever zaps the entry in between frees the folio. The fault holds the fault mutex and the lock of the vma.
MADV_DONTNEEDand the first unmap of a hole punch,hugetlb_vmdelete_list(), do not take the fault mutex, and a private vma only has a lock if it owns a reservation map: not withMAP_NORESERVE, and not in the child afterfork().Then the flag is set in, and
folio_unlock()is called on, a folio that is free or has a new owner. A surplus page goes back to the page allocator while it is locked:BUG: Bad page state in process init pfn:108e00 flags: 0x200000000000059(locked|uptodate|dirty|head|node=0|zone=2) page dumped because: PAGE_FLAGS_CHECK_AT_FREE flag(s) setA page of the pool can be given to the next user, of any file, who locks it and loses the lock to the late
folio_unlock():kernel BUG at
mm/filemap.c:1532!hugetlb_no_page+0x111b/0x23e0 hugetlb_fault+0x3a9/0x14a0An unprivileged process is enough: threads that write to a
MAP_PRIVATE|MAP_NORESERVEhugetlb mapping, threads that callMADV_DONTNEEDon it, and one that takes the freed pages for another file.Until commit
3a47c54f09c4("hugetlbfs: revert usei_mmap_rwsemfor more pmd sharing synchronization") the fault heldi_mmap_rwsemfor reading, which everybody who zaps takes for writing. The lock that commitbf4916922c60("hugetlbfs: extendhugetlb_vma_lockto private VMAs") gave to private vmas closed the window again, but only for those with a reservation map.Be done with the folio before the page table lock is dropped, as
hugetlb_wp()is with its copy. The function already unlocks page cache folios under that lock.Original fix generated by an LLM and sent in private by Josef.
Quoted 7 lines
Fixes 3a47c54f09c4 ("hugetlbfs: revert use i_mmap_rwsem for more pmd sharing synchronization")Signed-off-by Josef Bacik <[email protected]>Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>[ david: simplify+move comment ]Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Nice work! Nothing jumped out at me, so:
David Hildenbrand (Arm)
reviewerPATCH 3/10 mm/hugetlb: fix use-after-free in hugetlb_mfill_atomic_pte()
In reply to David Hildenbrand (Arm)Let's fix another instance where we use a folio after dropping the PTL, where someone else could e.g., MADV_DONTNEED it once we drop the PTL, making us trigger a use-after-free on the folio.
--- mm/hugetlb.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
mm/hugetlb.c +6 -1
@@ -6478,11 +6478,16 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,/* No need to invalidate - it was non-present before */ update_mmu_cache (dst_vma, dst_addr, dst_pte); -spin_unlock (ptl);if (!is_continue)folio_set_hugetlb_migratable (folio);if (vm_shared || is_continue)folio_unlock (folio); + +/* +* Hold the PTL until we're done with the folio: the page table owns the +* folio reference now. +*/ +spin_unlock (ptl); ret =0 ; out:return ret;
Signature · 1 line
2.43.0
Re: [PATCH 03/10] mm/hugetlb: fix use-after-free in hugetlb_mfill_atomic_pte()
Quoted 8 lines · David Hildenbrand (Arm)
Let's fix another instance where we use a folio after dropping the PTL, where someone else could e.g.,
MADV_DONTNEEDit once we drop the PTL, making us trigger a use-after-free on the folio.
Quoted 6 lines
Fixes cb6acd01e2e4 ("hugetlbfs: fix races and page leaks during migration")Fixes 1c9e8def43a3 ("userfaultfd: hugetlbfs: add UFFDIO_COPY support for shared mappings")Reported-by Lorenzo Stoakes (ARM) <[email protected]>Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
Thanks.
Re: [PATCH 03/10] mm/hugetlb: fix use-after-free in hugetlb_mfill_atomic_pte()
In reply to David Hildenbrand (Arm)Quoted 11 lines · David Hildenbrand (Arm)
Let's fix another instance where we use a folio after dropping the PTL, where someone else could e.g.,
MADV_DONTNEEDit once we drop the PTL, making us trigger a use-after-free on the folio.Fixes cb6acd01e2e4 ("hugetlbfs: fix races and page leaks during migration")Fixes 1c9e8def43a3 ("userfaultfd: hugetlbfs: add UFFDIO_COPY support for shared mappings")Reported-by Lorenzo Stoakes (ARM) <[email protected]>Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Nice work!
David Hildenbrand (Arm)
reviewerPATCH 4/10 mm/hugetlb: unmap the page where the other VMA maps it
In reply to David Hildenbrand (Arm)From: Josef Bacik <[email protected]>
If the owner of a MAP_PRIVATE hugetlb mapping fails to allocate a page for CoW, unmap_ref_private() unmaps the old page from all other private VMAs that have no reservation of their own. It finds them by the offset in the file, but then zaps each of them at the address of the faulting VMA. Nothing says that another VMA maps the offset at that address: a child can have moved its copy of the VMA with mremap(), and a second mapping of the file is wherever mmap() has put it.
So __unmap_hugepage_range() walks the page tables of the other mm at an address outside of iter_vma, where i_mmap_rwsem of this file protects nothing and where anything can be mapped, and the page stays mapped where it really is.
All of the following was seen on v7.3-rc6+ (g6c377d19d4a5) in KVM guests, as a user without any privilege. It takes a pool of 2 MiB pages with no free page that is not reserved, so that the allocation fails: the user can take or reserve what is free itself.
- The child moves its VMA away. The page is never unmapped, the mapcount stays 2, and the owner goes round retry_avoidcopy in hugetlb_wp() until the child is gone or a page becomes free. SIGKILL to the owner does not end it. 5 of 5 runs.
- The owner's mm has a second private mapping of the offset, made with MAP_NORESERVE. The owner zaps its own entry and reads zeroes where its data was. 5 of 5 runs.
- The child moves its VMA away and maps a 1 GiB hugetlb page at the old address, so this one takes a pool with a 1 GiB page as well. huge_pte_offset() for the 2 MiB size steps through the PUD leaf, so the "PMD table" is the 1 GiB page itself and its "page table lock" is folio->private of that page, which holds 2. Kernels with lock debugging oops in __unmap_hugepage_range(), 7 of 7 runs. Others spin on that word for ever with preemption disabled, 5 of 5 runs:
watchdog: BUG: soft lockup - CPU#0 stuck for 26s! [u_det3:130]
RIP: 0010:queued_spin_lock_slowpath+0x8a/0x290
RAX: 0000000000000002 RBX: ffffea0001000028
Call Trace:
__unmap_hugepage_range+0x154/0x6d0
unmap_hugepage_range+0xc1/0x140
hugetlb_wp+0x55c/0x700
hugetlb_fault+0x5f3/0x8f0RCU stalls follow, and killing all tasks of the user changes nothing. Four such owners, whose children share a single 1 GiB page, took down the whole guest with its four CPUs, 2 of 2 runs: one reported soft lockups on all four, the other gave no sign of life any more.
i_mmap_rwsem stays write locked. With a MAP_PRIVATE mapping of a hugetlbfs file that belongs to another user, and that the attacker can only read, the next mmap() of that file by its owner sleeps uninterruptibly in __mmap_region(), 4 of 4 runs.
Compute the address from the offset for each VMA. The tree walk returns only VMAs that cover pgoff, and vm_start, vm_end and vm_pgoff of a hugetlb VMA are aligned to the huge page size, as pgoff is, so the whole range lies inside iter_vma. Its page tables can not go away there, since the VMA is unlinked from the file under i_mmap_rwsem first.
With the right address the page is unmapped from all VMAs but the owner's, so the retry in hugetlb_wp() finds a mapcount of 1 and reuses the page.
A child that has moved its copy of the VMA now loses the page, and gets SIGBUS when it faults on a missing page of that VMA. That is what the comment above the function has always promised to those who do not own the reservation, and what already happens to a child that has left the VMA in place.
The address has been wrong from the start, and the second case needs only mmap() and fork(). mremap() of hugetlb VMAs, which the other cases use, came with commit 550a7d60bd5e ("mm, hugepages: add mremap() support for hugepage backed vma") in v5.16.
Original fix generated by an LLM and sent in private by Josef.
--- mm/hugetlb.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
mm/hugetlb.c +13 -4
@@ -5440,6 +5440,8 @@ static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,struct address_space *mapping;pgoff_t pgoff; +VM_WARN_ON_ONCE (!folio_test_anon (folio)); +/* * vm_pgoff is in PAGE_SIZE units, hence the different calculation * from page cache lookup which is in HPAGE_SIZE units.
@@ -5474,10 +5476,17 @@ static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,* could insert a zeroed page instead of the data existing * from the time of fork. This would look like data corruption */ -if (!is_vma_resv_set (iter_vma,HPAGE_RESV_OWNER )) -unmap_hugepage_range (iter_vma, address, - address +huge_page_size (h), - folio,0 ); +if (is_vma_resv_set (iter_vma,HPAGE_RESV_OWNER )) +continue ; + +/* +* The address in other VMAs can differ. Even though our old +* folio is anon, we always operate on the underlying hugetlb +* file. +*/ + address =vma_filebacked_address (iter_vma, pgoff,1 ); +unmap_hugepage_range (iter_vma, address, + address +huge_page_size (h), folio,0 ); }i_mmap_unlock_write (mapping); }
Signature · 1 line
2.43.0
Re: [PATCH 04/10] mm/hugetlb: unmap the page where the other VMA maps it
Quoted 83 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
If the owner of a
MAP_PRIVATEhugetlb mapping fails to allocate a page for CoW,unmap_ref_private()unmaps the old page from all other private VMAs that have no reservation of their own. It finds them by the offset in the file, but then zaps each of them at the address of the faulting VMA. Nothing says that another VMA maps the offset at that address: a child can have moved its copy of the VMA withmremap(), and a second mapping of the file is wherevermmap()has put it.So
__unmap_hugepage_range()walks the page tables of the other mm at an address outside ofiter_vma, wherei_mmap_rwsemof this file protects nothing and where anything can be mapped, and the page stays mapped where it really is.All of the following was seen on v7.3-rc6+ (g6c377d19d4a5) in KVM guests, as a user without any privilege. It takes a pool of 2 MiB pages with no free page that is not reserved, so that the allocation fails: the user can take or reserve what is free itself.
- The child moves its VMA away. The page is never unmapped, the mapcount stays 2, and the owner goes round
retry_avoidcopyinhugetlb_wp()until the child is gone or a page becomes free. SIGKILL to the owner does not end it. 5 of 5 runs.- The owner's mm has a second private mapping of the offset, made with
MAP_NORESERVE. The owner zaps its own entry and reads zeroes where its data was. 5 of 5 runs.- The child moves its VMA away and maps a 1 GiB hugetlb page at the old address, so this one takes a pool with a 1 GiB page as well.
huge_pte_offset()for the 2 MiB size steps through the PUD leaf, so the "PMD table" is the 1 GiB page itself and its "page table lock" isfolio->privateof that page, which holds 2. Kernels with lock debugging oops in__unmap_hugepage_range(), 7 of 7 runs. Others spin on that word for ever with preemption disabled, 5 of 5 runs:watchdog: BUG: soft lockup - CPU#0 stuck for 26s! [u_det3:130] RIP: 0010:queued_spin_lock_slowpath+0x8a/0x290 RAX: 0000000000000002 RBX: ffffea0001000028 Call Trace: __unmap_hugepage_range+0x154/0x6d0 unmap_hugepage_range+0xc1/0x140 hugetlb_wp+0x55c/0x700 hugetlb_fault+0x5f3/0x8f0RCU stalls follow, and killing all tasks of the user changes nothing. Four such owners, whose children share a single 1 GiB page, took down the whole guest with its four CPUs, 2 of 2 runs: one reported soft lockups on all four, the other gave no sign of life any more.
i_mmap_rwsemstays write locked. With aMAP_PRIVATEmapping of a hugetlbfs file that belongs to another user, and that the attacker can only read, the nextmmap()of that file by its owner sleeps uninterruptibly in__mmap_region(), 4 of 4 runs.Compute the address from the offset for each VMA. The tree walk returns only VMAs that cover pgoff, and
vm_start,vm_endandvm_pgoffof a hugetlb VMA are aligned to the huge page size, as pgoff is, so the whole range lies insideiter_vma. Its page tables can not go away there, since the VMA is unlinked from the file underi_mmap_rwsemfirst.With the right address the page is unmapped from all VMAs but the owner's, so the retry in
hugetlb_wp()finds a mapcount of 1 and reuses the page.A child that has moved its copy of the VMA now loses the page, and gets SIGBUS when it faults on a missing page of that VMA. That is what the comment above the function has always promised to those who do not own the reservation, and what already happens to a child that has left the VMA in place.
The address has been wrong from the start, and the second case needs only
mmap()andfork().mremap()of hugetlb VMAs, which the other cases use, came with commit550a7d60bd5e("mm, hugepages: addmremap()support for hugepage backed vma") in v5.16.Original fix generated by an LLM and sent in private by Josef.
Quoted 7 lines
Fixes 04f2cbe35699 ("hugetlb: guarantee that COW faults for a process that called mmap(MAP_PRIVATE) on hugetlbfs will succeed")Reported-by Chris Mason <[email protected]>Signed-off-by Josef Bacik <[email protected]>[ david: switch to vma_filebacked_address, add VM_WARN_ON_ONCE, refine comments]Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
Thanks.
Re: [PATCH 04/10] mm/hugetlb: unmap the page where the other VMA maps it
In reply to David Hildenbrand (Arm)Quoted 79 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
If the owner of a
MAP_PRIVATEhugetlb mapping fails to allocate a page for CoW,unmap_ref_private()unmaps the old page from all other private VMAs that have no reservation of their own. It finds them by the offset in the file, but then zaps each of them at the address of the faulting VMA. Nothing says that another VMA maps the offset at that address: a child can have moved its copy of the VMA withmremap(), and a second mapping of the file is wherevermmap()has put it.So
__unmap_hugepage_range()walks the page tables of the other mm at an address outside ofiter_vma, wherei_mmap_rwsemof this file protects nothing and where anything can be mapped, and the page stays mapped where it really is.All of the following was seen on v7.3-rc6+ (g6c377d19d4a5) in KVM guests, as a user without any privilege. It takes a pool of 2 MiB pages with no free page that is not reserved, so that the allocation fails: the user can take or reserve what is free itself.
- The child moves its VMA away. The page is never unmapped, the mapcount stays 2, and the owner goes round
retry_avoidcopyinhugetlb_wp()until the child is gone or a page becomes free. SIGKILL to the owner does not end it. 5 of 5 runs.- The owner's mm has a second private mapping of the offset, made with
MAP_NORESERVE. The owner zaps its own entry and reads zeroes where its data was. 5 of 5 runs.- The child moves its VMA away and maps a 1 GiB hugetlb page at the old address, so this one takes a pool with a 1 GiB page as well.
huge_pte_offset()for the 2 MiB size steps through the PUD leaf, so the "PMD table" is the 1 GiB page itself and its "page table lock" isfolio->privateof that page, which holds 2. Kernels with lock debugging oops in__unmap_hugepage_range(), 7 of 7 runs. Others spin on that word for ever with preemption disabled, 5 of 5 runs:watchdog: BUG: soft lockup - CPU#0 stuck for 26s! [u_det3:130] RIP: 0010:queued_spin_lock_slowpath+0x8a/0x290 RAX: 0000000000000002 RBX: ffffea0001000028 Call Trace: __unmap_hugepage_range+0x154/0x6d0 unmap_hugepage_range+0xc1/0x140 hugetlb_wp+0x55c/0x700 hugetlb_fault+0x5f3/0x8f0RCU stalls follow, and killing all tasks of the user changes nothing. Four such owners, whose children share a single 1 GiB page, took down the whole guest with its four CPUs, 2 of 2 runs: one reported soft lockups on all four, the other gave no sign of life any more.
i_mmap_rwsemstays write locked. With aMAP_PRIVATEmapping of a hugetlbfs file that belongs to another user, and that the attacker can only read, the nextmmap()of that file by its owner sleeps uninterruptibly in__mmap_region(), 4 of 4 runs.Compute the address from the offset for each VMA. The tree walk returns only VMAs that cover pgoff, and
vm_start,vm_endandvm_pgoffof a hugetlb VMA are aligned to the huge page size, as pgoff is, so the whole range lies insideiter_vma. Its page tables can not go away there, since the VMA is unlinked from the file underi_mmap_rwsemfirst.With the right address the page is unmapped from all VMAs but the owner's, so the retry in
hugetlb_wp()finds a mapcount of 1 and reuses the page.A child that has moved its copy of the VMA now loses the page, and gets SIGBUS when it faults on a missing page of that VMA. That is what the comment above the function has always promised to those who do not own the reservation, and what already happens to a child that has left the VMA in place.
The address has been wrong from the start, and the second case needs only
mmap()andfork().mremap()of hugetlb VMAs, which the other cases use, came with commit550a7d60bd5e("mm, hugepages: addmremap()support for hugepage backed vma") in v5.16.Original fix generated by an LLM and sent in private by Josef.
Quoted 8 lines
Fixes 04f2cbe35699 ("hugetlb: guarantee that COW faults for a process that called mmap(MAP_PRIVATE) on hugetlbfs will succeed")Reported-by Chris Mason <[email protected]>Signed-off-by Josef Bacik <[email protected]>[ david: switch to vma_filebacked_address, add VM_WARN_ON_ONCE, refine comments]Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Good catch1
David Hildenbrand (Arm)
reviewerPATCH 5/10 mm/hugetlb: don't clear the resv_map of a VMA whose lock is held
In reply to David Hildenbrand (Arm)From: Josef Bacik <[email protected]>
Since commit bf4916922c60 ("hugetlbfs: extend hugetlb_vma_lock to private VMAs") the lock of a private hugetlb VMA that owns a reservation is the rw_sema of its resv_map. The hugetlb_vma_*lock*() helpers find it through vma->vm_private_data, anew for every lock and every unlock.
At the end of mremap() the old VMA has to let go of the resv_map, which now belongs to the new VMA: clear_vma_resv_huge_pages() stores NULL into vm_private_data. mremap() holds mmap_lock for writing and has write locked the VMA, which keeps away everybody who comes through the mm. It does not keep away those who find the VMA through the file. For hole punch and truncation hugetlb_vmdelete_list() does
hugetlb_vma_trylock_write(vma); locks resv_map->rw_sema unmap_hugepage_range(vma, ...); vm_private_data = NULL hugetlb_vma_unlock_write(vma); no resv_map: does nothing
and the rw_sema stays write locked for ever:
WARNING: lock held when returning to user space! mremap_punch/116 is leaving the kernel with locks still held! locks held by mremap_punch/116: 1, last CPU#1:
#0: (&resv_map->rw_sema){+.+.}-{4:4}, at:
hugetlb_vmdelete_list+0x46/0x160
The next fault in the new VMA sleeps in hugetlb_vma_lock_read(), uninterruptibly, and it holds its entry of hugetlb_fault_mutex_table[]. That table is global, so a fault of any other process on a page that hashes to the same entry sleeps uninterruptibly as well.
All those who take the lock of a private VMA without taking the mmap/vma lock are affected: rmap walks. So hold i_mmap_rwsem around the clearing of the resv_map.
No change of behaviour, other than that mremap() of a hugetlb VMA takes i_mmap_rwsem once more.
Original fix generated by an LLM and sent in private by Josef.
--- mm/hugetlb.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
mm/hugetlb.c +11 -2
@@ -7467,6 +7467,15 @@ void hugetlb_unshare_all_pmds(struct vm_area_struct *vma)*/ void fixup_hugetlb_reservations (struct vm_area_struct *vma) { -if (is_vm_hugetlb_page (vma)) -clear_vma_resv_huge_pages (vma); +if (!is_vm_hugetlb_page (vma)) +return ; + +/* +* Rmap walks might still find this VMA and take the lock +* inside the reservation map for __vma_private_lock(). Don't clear +* the reservation map while that is possible. +*/ +i_mmap_lock_write (vma->vm_file->f_mapping); +clear_vma_resv_huge_pages (vma); +i_mmap_unlock_write (vma->vm_file->f_mapping); }
Signature · 1 line
2.43.0
Re: [PATCH 05/10] mm/hugetlb: don't clear the resv_map of a VMA whose lock is held
Quoted 45 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
Since commit
bf4916922c60("hugetlbfs: extendhugetlb_vma_lockto private VMAs") the lock of a private hugetlb VMA that owns a reservation is therw_semaof itsresv_map. The hugetlb_vma_*lock*() helpers find it throughvma->vm_private_data, anew for every lock and every unlock.At the end of
mremap()the old VMA has to let go of theresv_map, which now belongs to the new VMA:clear_vma_resv_huge_pages()stores NULL intovm_private_data.mremap()holdsmmap_lockfor writing and has write locked the VMA, which keeps away everybody who comes through the mm. It does not keep away those who find the VMA through the file. For hole punch and truncationhugetlb_vmdelete_list()does
hugetlb_vma_trylock_write(vma); locksresv_map->rw_semaunmap_hugepage_range(vma, ...);vm_private_data= NULLhugetlb_vma_unlock_write(vma); noresv_map: does nothingand the
rw_semastays write locked for ever:WARNING: lock held when returning to user space! mremap_punch/116 is leaving the kernel with locks still held! locks held by mremap_punch/116: 1, last CPU#1:
#0: (&resv_map->rw_sema){+.+.}-{4:4}, at:hugetlb_vmdelete_list+0x46/0x160The next fault in the new VMA sleeps in
hugetlb_vma_lock_read(), uninterruptibly, and it holds its entry of hugetlb_fault_mutex_table[]. That table is global, so a fault of any other process on a page that hashes to the same entry sleeps uninterruptibly as well.All those who take the lock of a private VMA without taking the mmap/vma lock are affected: rmap walks. So hold
i_mmap_rwsemaround the clearing of theresv_map.No change of behaviour, other than that
mremap()of a hugetlb VMA takesi_mmap_rwsemonce more.Original fix generated by an LLM and sent in private by Josef.
Quoted 7 lines
Fixes bf4916922c60 ("hugetlbfs: extend hugetlb_vma_lock to private VMAs")Reported-by Chris Mason <[email protected]>Signed-off-by Josef Bacik <[email protected]>Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>[ david: avoid local variable, move and refine comment, description cleanups+simplifications ]Signed-off-by David Hildenbrand (Arm) <[email protected]>
Thanks.
Re: [PATCH 05/10] mm/hugetlb: don't clear the resv_map of a VMA whose lock is held
In reply to David Hildenbrand (Arm)Quoted 41 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
Since commit
bf4916922c60("hugetlbfs: extendhugetlb_vma_lockto private VMAs") the lock of a private hugetlb VMA that owns a reservation is therw_semaof itsresv_map. The hugetlb_vma_*lock*() helpers find it throughvma->vm_private_data, anew for every lock and every unlock.At the end of
mremap()the old VMA has to let go of theresv_map, which now belongs to the new VMA:clear_vma_resv_huge_pages()stores NULL intovm_private_data.mremap()holdsmmap_lockfor writing and has write locked the VMA, which keeps away everybody who comes through the mm. It does not keep away those who find the VMA through the file. For hole punch and truncationhugetlb_vmdelete_list()doeshugetlb_vma_trylock_write(vma); locks resv_map->rw_sema unmap_hugepage_range(vma, ...); vm_private_data = NULL hugetlb_vma_unlock_write(vma); no resv_map: does nothingand the
rw_semastays write locked for ever:WARNING: lock held when returning to user space! mremap_punch/116 is leaving the kernel with locks still held! locks held by mremap_punch/116: 1, last CPU#1:
#0: (&resv_map->rw_sema){+.+.}-{4:4}, at:hugetlb_vmdelete_list+0x46/0x160The next fault in the new VMA sleeps in
hugetlb_vma_lock_read(), uninterruptibly, and it holds its entry of hugetlb_fault_mutex_table[]. That table is global, so a fault of any other process on a page that hashes to the same entry sleeps uninterruptibly as well.All those who take the lock of a private VMA without taking the mmap/vma lock are affected: rmap walks. So hold
i_mmap_rwsemaround the clearing of theresv_map.No change of behaviour, other than that
mremap()of a hugetlb VMA takesi_mmap_rwsemonce more.Original fix generated by an LLM and sent in private by Josef.
Quoted 8 lines
Fixes bf4916922c60 ("hugetlbfs: extend hugetlb_vma_lock to private VMAs")Reported-by Chris Mason <[email protected]>Signed-off-by Josef Bacik <[email protected]>Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>[ david: avoid local variable, move and refine comment, description cleanups+simplifications ]Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Nothing jumped out at me, so:
David Hildenbrand (Arm)
reviewerPATCH 6/10 mm/hugetlb: fix endless write fault on a leftover uffd bit
In reply to David Hildenbrand (Arm)From: Josef Bacik <[email protected]>
hugetlb_fault() resolves the uffd bit on a write fault only if the VMA is registered for userfaultfd write protection, and hugetlb_wp() bails out for any entry with the uffd bit set assuming hugetlb_fault() has already resolved it.
When a read-only hugetlb entry with the uffd bit set is mapped in a VMA that is not registered for uffd-wp, a write fault to it never makes progress. If the access comes from the kernel, e.g. read(2) from /dev/zero into such page, the task spins in the page fault handler, ignores SIGKILL and keeps holding the locks taken by the system call.
Such entries should not exist, but an unprivileged user can create them, for example with MADV_DONTNEED of a page protected with UFFDIO_RWPROTECT or with UFFDIO_COPY in MODE_WP racing with UFFDIO_UNREGISTER.
Clear the uffd bit when the VMA is not registered for uffd-wp and let the fault proceed to hugetlb_wp(), like do_wp_page() does for regular pages.
Note that a FOLL_FORCE write to a page protected with UFFDIO_RWPROTECT in a private VMA that was made PROT_NONE now drops the RWP protection of that page.
Original fix generated by an LLM and sent in private by Josef.
--- mm/hugetlb.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
mm/hugetlb.c +6 -2
@@ -6184,15 +6184,19 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, goto out_ptl;/* Handle userfault-wp first, before trying to lock more pages */ -if (userfaultfd_wp (vma) &&huge_pte_uffd (huge_ptep_get (mm, vmf.address, vmf.pte)) && +if (huge_pte_uffd (huge_ptep_get (mm, vmf.address, vmf.pte)) && (flags &FAULT_FLAG_WRITE ) && !huge_pte_write (vmf.orig_pte)) { -if (!userfaultfd_wp_async (vma)) { +if (userfaultfd_wp (vma) && !userfaultfd_wp_async (vma)) {spin_unlock (vmf.ptl);hugetlb_vma_unlock_read (vma);mutex_unlock (&hugetlb_fault_mutex_table[hash]);return handle_userfault (&vmf,VM_UFFD_WP ); } +/* +* TODO: fix all cases where uffd PTE bits remain in VMAs that +* are no longer registered with uffd. +*/ vmf.orig_pte =huge_pte_clear_uffd (vmf.orig_pte);set_huge_pte_at (mm, vmf.address, vmf.pte, vmf.orig_pte,huge_page_size (hstate_vma (vma)));
Signature · 1 line
2.43.0
Re: [PATCH 06/10] mm/hugetlb: fix endless write fault on a leftover uffd bit
Quoted 13 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
hugetlb_fault()resolves the uffd bit on a write fault only if the VMA is registered for userfaultfd write protection, andhugetlb_wp()bails out for any entry with the uffd bit set assuminghugetlb_fault()has already resolved it.When a read-only hugetlb entry with the uffd bit set is mapped in a VMA that is not registered for uffd-wp, a write fault to it never makes progress. If the access comes from the kernel, e.g. read(2) from
/dev/zerointo such page, the task spins in the page fault handler, ignores SIGKILL and keeps holding the locks taken by the system call.
Quoted 3 lines
Such entries should not exist, but an unprivileged user can create them, for example with
MADV_DONTNEEDof a page protected withUFFDIO_RWPROTECTor withUFFDIO_COPYinMODE_WPracing withUFFDIO_UNREGISTER.
Ugh yuck. Uffd is another gift that keeps giving...
Quoted 10 lines
Clear the uffd bit when the VMA is not registered for uffd-wp and let the fault proceed to
hugetlb_wp(), likedo_wp_page()does for regular pages.Note that a
FOLL_FORCEwrite to a page protected withUFFDIO_RWPROTECTin a private VMA that was madePROT_NONEnow drops the RWP protection of that page.Original fix generated by an LLM and sent in private by Josef.
Quoted 8 lines
Fixes 60d5b473d61b ("mm/hugetlb: fix uffd wr-protection for CoW optimization path")Reported-by Chris Mason <[email protected]>Signed-off-by Josef Bacik <[email protected]>[rppt: massage commit message]Signed-off-by Mike Rapoport (Microsoft) <[email protected]>[ david: convert comment into a TODO ]Signed-off-by David Hildenbrand (Arm) <[email protected]>
Looks sensible to me so:
Quoted 32 lines
--- mm/hugetlb.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)mm/hugetlb.c +6
-3@@ -6184,15 +6184,19 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, goto out_ptl;/* Handle userfault-wp first, before trying to lock more pages */ -if (userfaultfd_wp (vma) &&huge_pte_uffd (huge_ptep_get (mm, vmf.address, vmf.pte)) && +if (huge_pte_uffd (huge_ptep_get (mm, vmf.address, vmf.pte)) && (flags &FAULT_FLAG_WRITE ) && !huge_pte_write (vmf.orig_pte)) { -if (!userfaultfd_wp_async (vma)) { +if (userfaultfd_wp (vma) && !userfaultfd_wp_async (vma)) {spin_unlock (vmf.ptl);hugetlb_vma_unlock_read (vma);mutex_unlock (&hugetlb_fault_mutex_table[hash]);return handle_userfault (&vmf,VM_UFFD_WP ); } +/* +* TODO: fix all cases where uffd PTE bits remain in VMAs that +* are no longer registered with uffd. +*/ vmf.orig_pte =huge_pte_clear_uffd (vmf.orig_pte);set_huge_pte_at (mm, vmf.address, vmf.pte, vmf.orig_pte,huge_page_size (hstate_vma (vma))); --
Quoted 2 lines
2.43.0
--
Cheers, Lorenzo
Re: [PATCH 06/10] mm/hugetlb: fix endless write fault on a leftover uffd bit
In reply to David Hildenbrand (Arm)Quoted 34 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
hugetlb_fault()resolves the uffd bit on a write fault only if the VMA is registered for userfaultfd write protection, andhugetlb_wp()bails out for any entry with the uffd bit set assuminghugetlb_fault()has already resolved it.When a read-only hugetlb entry with the uffd bit set is mapped in a VMA that is not registered for uffd-wp, a write fault to it never makes progress. If the access comes from the kernel, e.g. read(2) from
/dev/zerointo such page, the task spins in the page fault handler, ignores SIGKILL and keeps holding the locks taken by the system call.Such entries should not exist, but an unprivileged user can create them, for example with
MADV_DONTNEEDof a page protected withUFFDIO_RWPROTECTor withUFFDIO_COPYinMODE_WPracing withUFFDIO_UNREGISTER.Clear the uffd bit when the VMA is not registered for uffd-wp and let the fault proceed to
hugetlb_wp(), likedo_wp_page()does for regular pages.Note that a
FOLL_FORCEwrite to a page protected withUFFDIO_RWPROTECTin a private VMA that was madePROT_NONEnow drops the RWP protection of that page.Original fix generated by an LLM and sent in private by Josef.
Fixes 60d5b473d61b ("mm/hugetlb: fix uffd wr-protection for CoW optimization path")Reported-by Chris Mason <[email protected]>Signed-off-by Josef Bacik <[email protected]>[rppt: massage commit message]Signed-off-by Mike Rapoport (Microsoft) <[email protected]>[ david: convert comment into a TODO ]Signed-off-by David Hildenbrand (Arm) <[email protected]>
Quoted 1 line
---
LGTM!
David Hildenbrand (Arm)
reviewerPATCH 7/10 mm/hugetlb: tolerate uffd bits set without uffd for PROT_NONE PTEs
In reply to David Hildenbrand (Arm)The whole uffd handling when a VMA is unregistered from UFFD is shaky:
there are still some scenarios where we might have stale uffd PTE bits in VMAs no longer registered for UFFD, which confuses other code.
Let's just tolerate that for now and simply clear the PTE bit. Add a TODO to really clean up any uffd leftovers when unregistering a VMA from userfaultfd.
The handling is now more similar to what we have in handle_pte_fault().
--- mm/hugetlb.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
mm/hugetlb.c +6 -2
@@ -6127,16 +6127,20 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,* NUMA hinting), so let normal hugetlb fault handling proceed. */ if (pte_protnone (vmf.orig_pte) &&vma_is_accessible (vma) && -userfaultfd_rwp (vma) &&huge_pte_uffd (vmf.orig_pte)) { +huge_pte_uffd (vmf.orig_pte)) {spinlock_t *ptl;pte_t pte;/* Sync: drop hugetlb locks before blocking in handle_userfault() */ -if (!userfaultfd_rwp_async (vma)) +if (userfaultfd_rwp (vma) && !userfaultfd_rwp_async (vma))return hugetlb_handle_userfault (&vmf, mapping,VM_UFFD_RWP ); ptl =huge_pte_lock (h, mm, vmf.pte); pte =huge_ptep_get (mm, vmf.address, vmf.pte); +/* +* TODO: fix all cases where uffd PTE bits remain in VMAs that +* are no longer registered with uffd. +*/ if (pte_protnone (pte) &&huge_pte_uffd (pte)) {unsigned int shift =huge_page_shift (h);
Signature · 1 line
2.43.0
Re: [PATCH 07/10] mm/hugetlb: tolerate uffd bits set without uffd for PROT_NONE PTEs
Quoted 8 lines · David Hildenbrand (Arm)
The whole uffd handling when a VMA is unregistered from UFFD is shaky:
there are still some scenarios where we might have stale uffd PTE bits in VMAs no longer registered for UFFD, which confuses other code.Let's just tolerate that for now and simply clear the PTE bit. Add a TODO to really clean up any uffd leftovers when unregistering a VMA from userfaultfd.
Quoted 5 lines
The handling is now more similar to what we have in
handle_pte_fault().Fixes 2d427bb01639 ("mm/userfaultfd: add RWP fault delivery and expose UFFDIO_REGISTER_MODE_RWP")Signed-off-by David Hildenbrand (Arm) <[email protected]>
Ugh what a mess. It seems like bad code makes any edits going forward problematic because of the labyrinth of bailing wire and chewing gum left behind.
Anyway seems sensible to me so:
Quoted 33 lines
--- mm/hugetlb.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)mm/hugetlb.c +6
-3@@ -6127,16 +6127,20 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,* NUMA hinting), so let normal hugetlb fault handling proceed. */ if (pte_protnone (vmf.orig_pte) &&vma_is_accessible (vma) && -userfaultfd_rwp (vma) &&huge_pte_uffd (vmf.orig_pte)) { +huge_pte_uffd (vmf.orig_pte)) {spinlock_t *ptl;pte_t pte;/* Sync: drop hugetlb locks before blocking in handle_userfault() */ -if (!userfaultfd_rwp_async (vma)) +if (userfaultfd_rwp (vma) && !userfaultfd_rwp_async (vma))return hugetlb_handle_userfault (&vmf, mapping,VM_UFFD_RWP ); ptl =huge_pte_lock (h, mm, vmf.pte); pte =huge_ptep_get (mm, vmf.address, vmf.pte); +/* +* TODO: fix all cases where uffd PTE bits remain in VMAs that +* are no longer registered with uffd. +*/ if (pte_protnone (pte) &&huge_pte_uffd (pte)) {unsigned int shift =huge_page_shift (h); --
Quoted 2 lines
2.43.0
--
Cheers, Lorenzo
Re: [PATCH 07/10] mm/hugetlb: tolerate uffd bits set without uffd for PROT_NONE PTEs
In reply to David Hildenbrand (Arm)Quoted 8 lines · David Hildenbrand (Arm)
The whole uffd handling when a VMA is unregistered from UFFD is shaky:
there are still some scenarios where we might have stale uffd PTE bits in VMAs no longer registered for UFFD, which confuses other code.Let's just tolerate that for now and simply clear the PTE bit. Add a TODO to really clean up any uffd leftovers when unregistering a VMA from userfaultfd.
Quoted 6 lines
The handling is now more similar to what we have in
handle_pte_fault().Fixes 2d427bb01639 ("mm/userfaultfd: add RWP fault delivery and expose UFFDIO_REGISTER_MODE_RWP")Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Re: [PATCH 07/10] mm/hugetlb: tolerate uffd bits set without uffd for PROT_NONE PTEs
In reply to David Hildenbrand (Arm)Quoted 8 lines · David Hildenbrand (Arm)
The whole uffd handling when a VMA is unregistered from UFFD is shaky:
there are still some scenarios where we might have stale uffd PTE bits in VMAs no longer registered for UFFD, which confuses other code.Let's just tolerate that for now and simply clear the PTE bit. Add a TODO to really clean up any uffd leftovers when unregistering a VMA from userfaultfd.
Quoted 5 lines
The handling is now more similar to what we have in
handle_pte_fault().Fixes 2d427bb01639 ("mm/userfaultfd: add RWP fault delivery and expose UFFDIO_REGISTER_MODE_RWP")Signed-off-by David Hildenbrand (Arm) <[email protected]>
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
David Hildenbrand (Arm)
reviewerPATCH 8/10 mm/hugetlb: tolerate UFFD_WP markers outside uffd-wp VMAs
In reply to David Hildenbrand (Arm)UFFD PTE bit handling is a bit like swiss cheese, in particular with recently added RWP mode. Having uffd-wp markers outside uffd-wp VMAs already confused this code in the past and resulted in uffd-pte bits confusing other code later.
Let's play safe and just ignore these markers in hugetlb_no_page(), not silently propagating the uffd-wp PTE bit.
--- mm/hugetlb.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
mm/hugetlb.c +4 -1
@@ -5939,8 +5939,11 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,/* * If this pte was previously wr-protected, keep it wr-protected even * if populated. +* +* TODO: fix all cases where uffd-wp markers remain in VMAs that are no +* longer registered with uffd. */ -if (unlikely (pte_is_uffd_wp_marker (vmf->orig_pte))) +if (unlikely (userfaultfd_wp (vma) &&pte_is_uffd_wp_marker (vmf->orig_pte))) new_pte =huge_pte_mkuffd (new_pte);set_huge_pte_at (mm, vmf->address, vmf->pte, new_pte,huge_page_size (h));
Signature · 1 line
2.43.0
Re: [PATCH 08/10] mm/hugetlb: tolerate UFFD_WP markers outside uffd-wp VMAs
Quoted 11 lines · David Hildenbrand (Arm)
UFFD PTE bit handling is a bit like swiss cheese, in particular with recently added RWP mode. Having uffd-wp markers outside uffd-wp VMAs already confused this code in the past and resulted in uffd-pte bits confusing other code later.
Let's play safe and just ignore these markers in
hugetlb_no_page(), not silently propagating the uffd-wp PTE bit.Fixes 2d427bb01639 ("mm/userfaultfd: add RWP fault delivery and expose UFFDIO_REGISTER_MODE_RWP")Signed-off-by David Hildenbrand (Arm) <[email protected]>
Seems sensible so:
Quoted 23 lines
--- mm/hugetlb.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)mm/hugetlb.c +4
-2@@ -5939,8 +5939,11 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,/* * If this pte was previously wr-protected, keep it wr-protected even * if populated. +* +* TODO: fix all cases where uffd-wp markers remain in VMAs that are no +* longer registered with uffd. */ -if (unlikely (pte_is_uffd_wp_marker (vmf->orig_pte))) +if (unlikely (userfaultfd_wp (vma) &&pte_is_uffd_wp_marker (vmf->orig_pte))) new_pte =huge_pte_mkuffd (new_pte);set_huge_pte_at (mm, vmf->address, vmf->pte, new_pte,huge_page_size (h)); --
Quoted 2 lines
2.43.0
--
Cheers, Lorenzo
Re: [PATCH 08/10] mm/hugetlb: tolerate UFFD_WP markers outside uffd-wp VMAs
In reply to David Hildenbrand (Arm)Quoted 5 lines · David Hildenbrand (Arm)
UFFD PTE bit handling is a bit like swiss cheese, in particular with recently added RWP mode. Having uffd-wp markers outside uffd-wp VMAs already confused this code in the past and resulted in uffd-pte bits confusing other code later.
Quoted 7 lines
Let's play safe and just ignore these markers in
hugetlb_no_page(), not silently propagating the uffd-wp PTE bit.Fixes 2d427bb01639 ("mm/userfaultfd: add RWP fault delivery and expose UFFDIO_REGISTER_MODE_RWP")Signed-off-by David Hildenbrand (Arm) <[email protected]>---
LGTM!
David Hildenbrand (Arm)
reviewerPATCH 9/10 mm/hugetlb: don't leave a uffd-wp marker when zapping an RWP entry
In reply to David Hildenbrand (Arm)From: Josef Bacik <[email protected]>
An entry that is protected with UFFDIO_RWPROTECT is PAGE_NONE with the uffd bit set, the bit that userfaultfd write protection uses too. When __unmap_hugepage_range() zaps an entry with that bit it leaves a PTE_MARKER_UFFD_WP behind, whatever the VMA is registered for.
In a VMA registered with UFFDIO_REGISTER_MODE_RWP that marker means nothing. The next fault has hugetlb_no_page() turn it into a present, read-only entry with the uffd bit. That is not an RWP entry, since it is not PAGE_NONE, and the VMA has no VM_UFFD_WP (the two modes can not be registered together). An earlier fix solved some side effects, now just stop the zap from wrongly installing a marker.
Leave a marker only if the VMA is registered for write protection, both for present and for non-present entries. The zap of ordinary page tables has the same test, in cond_install_uffd_wp_ptes(), and hugetlb_change_protection() says that "uffd_rwp does not install markers".
After a zap the range of an RWP VMA is simply empty, as it is for anonymous memory and shmem. Nothing changes for VMAs with VM_UFFD_WP. In a VMA that is not registered at all a leftover uffd bit or marker is now dropped by the zap and no longer kept alive.
Original fix generated by an LLM and sent in private by Josef.
--- mm/hugetlb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/hugetlb.c +2 -2
@@ -5267,7 +5267,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,* drop the uffd-wp bit in this zap, then replace the * pte with a marker. */ -if (pte_swp_uffd_any (pte) && +if (userfaultfd_wp (vma) &&pte_swp_uffd_any (pte) && !(zap_flags &ZAP_FLAG_DROP_MARKER )) set_huge_pte_at(mm, address, ptep, make_pte_marker(PTE_MARKER_UFFD_WP),
@@ -5303,7 +5303,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,if (huge_pte_dirty (pte))folio_mark_dirty (folio);/* Leave a uffd-wp pte marker if needed */ -if (huge_pte_uffd (pte) && +if (userfaultfd_wp (vma) &&huge_pte_uffd (pte) && !(zap_flags &ZAP_FLAG_DROP_MARKER )) set_huge_pte_at(mm, address, ptep, make_pte_marker(PTE_MARKER_UFFD_WP),
Signature · 1 line
2.43.0
Re: [PATCH 09/10] mm/hugetlb: don't leave a uffd-wp marker when zapping an RWP entry
Quoted 24 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
An entry that is protected with
UFFDIO_RWPROTECTisPAGE_NONEwith the uffd bit set, the bit that userfaultfd write protection uses too. When__unmap_hugepage_range()zaps an entry with that bit it leaves aPTE_MARKER_UFFD_WPbehind, whatever the VMA is registered for.In a VMA registered with
UFFDIO_REGISTER_MODE_RWPthat marker means nothing. The next fault hashugetlb_no_page()turn it into a present, read-only entry with the uffd bit. That is not an RWP entry, since it is notPAGE_NONE, and the VMA has noVM_UFFD_WP(the two modes can not be registered together). An earlier fix solved some side effects, now just stop the zap from wrongly installing a marker.Leave a marker only if the VMA is registered for write protection, both for present and for non-present entries. The zap of ordinary page tables has the same test, in
cond_install_uffd_wp_ptes(), andhugetlb_change_protection()says that "uffd_rwpdoes not install markers".After a zap the range of an RWP VMA is simply empty, as it is for anonymous memory and shmem. Nothing changes for VMAs with
VM_UFFD_WP. In a VMA that is not registered at all a leftover uffd bit or marker is now dropped by the zap and no longer kept alive.
Quoted 8 lines
Original fix generated by an LLM and sent in private by Josef.
Fixes 2d427bb01639 ("mm/userfaultfd: add RWP fault delivery and expose UFFDIO_REGISTER_MODE_RWP")Reported-by Chris Mason <[email protected]>Signed-off-by Josef Bacik <[email protected]>[ david: a bit of description overhaul+simplification ]Signed-off-by David Hildenbrand (Arm) <[email protected]>
Seems sensible to me so:
Quoted 28 lines
--- mm/hugetlb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)mm/hugetlb.c +2
-3@@ -5267,7 +5267,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,* drop the uffd-wp bit in this zap, then replace the * pte with a marker. */ -if (pte_swp_uffd_any (pte) && +if (userfaultfd_wp (vma) &&pte_swp_uffd_any (pte) && !(zap_flags &ZAP_FLAG_DROP_MARKER )) set_huge_pte_at(mm, address, ptep, make_pte_marker(PTE_MARKER_UFFD_WP),@@ -5303,7 +5303,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,if (huge_pte_dirty (pte))folio_mark_dirty (folio);/* Leave a uffd-wp pte marker if needed */ -if (huge_pte_uffd (pte) && +if (userfaultfd_wp (vma) &&huge_pte_uffd (pte) && !(zap_flags &ZAP_FLAG_DROP_MARKER )) set_huge_pte_at(mm, address, ptep, make_pte_marker(PTE_MARKER_UFFD_WP), --
Quoted 2 lines
2.43.0
--
Cheers, Lorenzo
Re: [PATCH 09/10] mm/hugetlb: don't leave a uffd-wp marker when zapping an RWP entry
In reply to David Hildenbrand (Arm)Quoted 26 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
An entry that is protected with
UFFDIO_RWPROTECTisPAGE_NONEwith the uffd bit set, the bit that userfaultfd write protection uses too. When__unmap_hugepage_range()zaps an entry with that bit it leaves aPTE_MARKER_UFFD_WPbehind, whatever the VMA is registered for.In a VMA registered with
UFFDIO_REGISTER_MODE_RWPthat marker means nothing. The next fault hashugetlb_no_page()turn it into a present, read-only entry with the uffd bit. That is not an RWP entry, since it is notPAGE_NONE, and the VMA has noVM_UFFD_WP(the two modes can not be registered together). An earlier fix solved some side effects, now just stop the zap from wrongly installing a marker.Leave a marker only if the VMA is registered for write protection, both for present and for non-present entries. The zap of ordinary page tables has the same test, in
cond_install_uffd_wp_ptes(), andhugetlb_change_protection()says that "uffd_rwpdoes not install markers".After a zap the range of an RWP VMA is simply empty, as it is for anonymous memory and shmem. Nothing changes for VMAs with
VM_UFFD_WP. In a VMA that is not registered at all a leftover uffd bit or marker is now dropped by the zap and no longer kept alive.Original fix generated by an LLM and sent in private by Josef.
Quoted 7 lines
Fixes 2d427bb01639 ("mm/userfaultfd: add RWP fault delivery and expose UFFDIO_REGISTER_MODE_RWP")Reported-by Chris Mason <[email protected]>Signed-off-by Josef Bacik <[email protected]>[ david: a bit of description overhaul+simplification ]Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Re: [PATCH 09/10] mm/hugetlb: don't leave a uffd-wp marker when zapping an RWP entry
In reply to David Hildenbrand (Arm)Quoted 24 lines · David Hildenbrand (Arm)
From: Josef Bacik <[email protected]>
An entry that is protected with
UFFDIO_RWPROTECTisPAGE_NONEwith the uffd bit set, the bit that userfaultfd write protection uses too. When__unmap_hugepage_range()zaps an entry with that bit it leaves aPTE_MARKER_UFFD_WPbehind, whatever the VMA is registered for.In a VMA registered with
UFFDIO_REGISTER_MODE_RWPthat marker means nothing. The next fault hashugetlb_no_page()turn it into a present, read-only entry with the uffd bit. That is not an RWP entry, since it is notPAGE_NONE, and the VMA has noVM_UFFD_WP(the two modes can not be registered together). An earlier fix solved some side effects, now just stop the zap from wrongly installing a marker.Leave a marker only if the VMA is registered for write protection, both for present and for non-present entries. The zap of ordinary page tables has the same test, in
cond_install_uffd_wp_ptes(), andhugetlb_change_protection()says that "uffd_rwpdoes not install markers".After a zap the range of an RWP VMA is simply empty, as it is for anonymous memory and shmem. Nothing changes for VMAs with
VM_UFFD_WP. In a VMA that is not registered at all a leftover uffd bit or marker is now dropped by the zap and no longer kept alive.
Quoted 8 lines
Original fix generated by an LLM and sent in private by Josef.
Fixes 2d427bb01639 ("mm/userfaultfd: add RWP fault delivery and expose UFFDIO_REGISTER_MODE_RWP")Reported-by Chris Mason <[email protected]>Signed-off-by Josef Bacik <[email protected]>[ david: a bit of description overhaul+simplification ]Signed-off-by David Hildenbrand (Arm) <[email protected]>
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
David Hildenbrand (Arm)
reviewerPATCH 10/10 mm/mremap: fix calling fixup_hugetlb_reservations() on the wrong VMA
In reply to David Hildenbrand (Arm)In case we fail to move the page tables, for example, because we fail to allocate a page table, we actually keep the old VMA.
Calling fixup_hugetlb_reservations() on the old VMA is wrong. Fix it by just calling it on the VMA that will be removed.
The user-visible impact is that the VMA loses its reservations, meaning that the existing reservations get stranded (leaked) and new hugetlb folios that are getting allocated in the VMA cannot make use of the reservations, failing these allocations.
--- mm/mremap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/mremap.c +1 -1
@@ -1345,7 +1345,7 @@ static int copy_vma_and_data(struct vma_remap_struct *vrm,mremap_userfaultfd_prep (new_vma, vrm->uf); } -fixup_hugetlb_reservations (vma); +fixup_hugetlb_reservations (vrm->vma); *new_vma_ptr = new_vma;return err;
Signature · 1 line
2.43.0
Re: [PATCH 10/10] mm/mremap: fix calling fixup_hugetlb_reservations() on the wrong VMA
Quoted 15 lines · David Hildenbrand (Arm)
In case we fail to move the page tables, for example, because we fail to allocate a page table, we actually keep the old VMA.
Calling
fixup_hugetlb_reservations()on the old VMA is wrong. Fix it by just calling it on the VMA that will be removed.The user-visible impact is that the VMA loses its reservations, meaning that the existing reservations get stranded (leaked) and new hugetlb folios that are getting allocated in the VMA cannot make use of the reservations, failing these allocations.
Quoted 4 lines
Fixes b714ccb02a76 ("mm/mremap: complete refactor of move_vma()")Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
Thanks.
Re: [PATCH 10/10] mm/mremap: fix calling fixup_hugetlb_reservations() on the wrong VMA
In reply to David Hildenbrand (Arm)Quoted 11 lines · David Hildenbrand (Arm)
In case we fail to move the page tables, for example, because we fail to allocate a page table, we actually keep the old VMA.
Calling
fixup_hugetlb_reservations()on the old VMA is wrong. Fix it by just calling it on the VMA that will be removed.The user-visible impact is that the VMA loses its reservations, meaning that the existing reservations get stranded (leaked) and new hugetlb folios that are getting allocated in the VMA cannot make use of the reservations, failing these allocations.
Quoted 5 lines
Fixes b714ccb02a76 ("mm/mremap: complete refactor of move_vma()")Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>---
David Hildenbrand (Arm)
reviewerRe: [PATCH 10/10] mm/mremap: fix calling fixup_hugetlb_reservations() on the wrong VMA
Quoted 13 lines · Lance Yang
Quoted 11 lines · David Hildenbrand (Arm)
In case we fail to move the page tables, for example, because we fail to allocate a page table, we actually keep the old VMA.
Calling
fixup_hugetlb_reservations()on the old VMA is wrong. Fix it by just calling it on the VMA that will be removed.The user-visible impact is that the VMA loses its reservations, meaning that the existing reservations get stranded (leaked) and new hugetlb folios that are getting allocated in the VMA cannot make use of the reservations, failing these allocations.
Quoted 7 lines
Quoted 5 lines
Fixes b714ccb02a76 ("mm/mremap: complete refactor of move_vma()")Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Reviewed-by Lance Yang <[email protected]>
Thanks for all the review, Lance!
Signature · 3 lines
Cheers, David
Muchun Song
maintainerQuoted 39 lines · David Hildenbrand (Arm)
Hi,
here are various MM fixes. The majority was reported by Josef at Anthrophic, the remainder was found while reviewing/overhauling them.
Two issues/fixes reported are not included, as we are discussing (better) alternatives on the list already.
I left most of the original commit messages as schloppy as they are: I don't particularly care as long as the code is in good shape and as long as they are not actively wrong. But I (and Mike) could not be stopped from overhauling at least some of them.
While I don't think there is anything too critical in here, I think this is a good opportunity to see how fast we can get pre-screened/pre-reviewed fixes upstream that might be security-related.
The majority are hugetlb + uffd issues, that are not actually that relevant for most installations. But all things that should just get fixed. Some uffd fixes need proper follow ups to fix some more of the root causes of leftover UFFD PTE bits (or proof that there are none remaining).
I ran some MM selftests and nothing popped up.
So let's try to get these upstream in a timely fashion.
Signed-off-by David Hildenbrand (Arm) <[email protected]>---
David Hildenbrand (Arm) (4):
mm/hugetlb: fix use-after-free inhugetlb_mfill_atomic_pte()mm/hugetlb: tolerate uffd bits set without uffd forPROT_NONEPTEs mm/hugetlb: tolerateUFFD_WPmarkers outside uffd-wp VMAs mm/mremap: fix callingfixup_hugetlb_reservations()on the wrong VMA
Quoted 7 lines
Josef Bacik (6):
mm/vma: don't leak the commit charge on mmap failure mm/hugetlb: fix use-after-free of a new anon folio mm/hugetlb: unmap the page where the other VMA maps it mm/hugetlb: don't clear theresv_mapof a VMA whose lock is held mm/hugetlb: fix endless write fault on a leftover uffd bit mm/hugetlb: don't leave a uffd-wp marker when zapping an RWP entry
HugeTLB is really nasty, while no new features are landing, we have a steady stream of bug fixes coming in.
Thanks,
Muchun
Quoted 18 lines
mm/hugetlb.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++-------------- mm/mremap.c | 2 +- mm/vma.c | 11 ++++++---- 3 files changed, 62 insertions(+), 21 deletions(-)---
base-commit:
0fcb05b72344f40806cbe810de8872f1698bf053change-id: 20261015-urgent_fixes-7112629346eb
--
Cheers,
David
David Hildenbrand (Arm)
reviewerQuoted 47 lines · Muchun Song
Quoted 39 lines · David Hildenbrand (Arm)
Hi,
here are various MM fixes. The majority was reported by Josef at Anthrophic, the remainder was found while reviewing/overhauling them.
Two issues/fixes reported are not included, as we are discussing (better) alternatives on the list already.
I left most of the original commit messages as schloppy as they are: I don't particularly care as long as the code is in good shape and as long as they are not actively wrong. But I (and Mike) could not be stopped from overhauling at least some of them.
While I don't think there is anything too critical in here, I think this is a good opportunity to see how fast we can get pre-screened/pre-reviewed fixes upstream that might be security-related.
The majority are hugetlb + uffd issues, that are not actually that relevant for most installations. But all things that should just get fixed. Some uffd fixes need proper follow ups to fix some more of the root causes of leftover UFFD PTE bits (or proof that there are none remaining).
I ran some MM selftests and nothing popped up.
So let's try to get these upstream in a timely fashion.
Signed-off-by David Hildenbrand (Arm) <[email protected]>---
David Hildenbrand (Arm) (4):
mm/hugetlb: fix use-after-free inhugetlb_mfill_atomic_pte()mm/hugetlb: tolerate uffd bits set without uffd forPROT_NONEPTEs mm/hugetlb: tolerateUFFD_WPmarkers outside uffd-wp VMAs mm/mremap: fix callingfixup_hugetlb_reservations()on the wrong VMAQuoted 7 lines
Josef Bacik (6):
mm/vma: don't leak the commit charge on mmap failure mm/hugetlb: fix use-after-free of a new anon folio mm/hugetlb: unmap the page where the other VMA maps it mm/hugetlb: don't clear theresv_mapof a VMA whose lock is held mm/hugetlb: fix endless write fault on a leftover uffd bit mm/hugetlb: don't leave a uffd-wp marker when zapping an RWP entry
Quoted 2 lines
HugeTLB is really nasty, while no new features are landing, we have a steady stream of bug fixes coming in.
Yes, and so far it's mostly due to:
* PMD table sharing
* Reservation handling
* Userfaultfd
Which is no surprise given how people already felt about most of these features in the past :)
Signature · 3 lines
Cheers, David