mm: fix UAF caused by race between ptdump and vmap pgtable freeing
Subsystems (9)
Lorenzo Stoakes (ARM)
maintainerv5 cover mm: fix UAF caused by race between ptdump and vmap pgtable freeing
Kernel page table walkers fall into two broad categories - those ranges where no exclusion is required via walk_kernel_page_table_range_lockless() and those where exclusion is required via walk_kernel_page_table_range() or walk_page_range_debug().
The former category is used only by arm64 arch code operating on ranges it both wholly owns and does not concurrently write.
The latter category consists of kernel page table walkers operating on ranges that are wholly owned (but which need exclusion against concurrent writers).
The lock used for exclusion is the mmap lock, and for kernel ranges this the mmap lock on init_mm.
ptdump is a special case being both the only user of walk_page_range_debug(), and the only case in which it walks ranges it does not own.
This presents a problem, as page tables may be freed under ptdump. And indeed there is a use-after-free bug in the kernel as a result, which this series addresses.
vmap promotes page tables to huge leaf entries where possible, freeing the lower page table when it does. It does this with no meaningful locks held against concurrent ptdump walks.
As a result, use-after-free can currently occur. This series addresses the issue by having the vmap huge promotion logic acquire the mmap read lock while both setting the huge page table entry and freeing the prior leaf page table.
The ptdump code already acquires the mmap write lock, so by doing so we ensure that the ptdump walker only ever observes either the huge page table entry or the existing page table entry, and nothing is freed underneath it.
A mitigation for this issue was already applied for arm64 in commit fa93b45fd397 ("arm64: Enable vmalloc-huge with ptdump"), which this series has to deal with carefully.
This mitigation resolves the issue by acquiring the mmap read lock on init_mm on vmap page table free if a ptdump is in progress.
However the fix in this series would cause a deadlock if we were to simply apply it for arm64 without also reverting the change.
This is because vmap may acquire the read lock before ptdump attempts to acquire the write lock, which then gets queued, and rwsem starvation rules mean that the (unacknowledged) nested mmap read lock in the arm64 code would also block, meaning the original read lock is never released and thus deadlock.
This series works around this by #ifndef CONFIG_ARM64'ing the mmap read lock in vmap logic, then partially reverting commit fa93b45fd397 ("arm64:
Enable vmalloc-huge with ptdump"), keeping the enablement of huge vmap support, and removing the ifdeffery with the partial revert patch.
There are related issues that we also address in this series:
* x86 page attribute logic, specifically Change Page Attributes (CPA), implements a feature whereby huge ranges can be collapsed into huge leaf entries. This can similarly cause a UAF when done in parallel with a ptdump walk, so similarly acquire the init_mm mmap lock to avoid this.
* The CPA logic allows concurrent page table manipulation and CPA collapse, meaning the former risks accessing a page table the latter frees. Fix this by acquiring mmap write lock on init_mm across the whole CPA collapse operation and read lock on the page table manipulation.
* x86 and arm64 permit walks of non-kernel mm's (both allowing efi mm walks, and in intel's case arbitrary mm's), so we ensure kernel mappings remain stable by locking the init_mm as well as the mm being walked.
The ordering of patches is established for both strict dependencies (the arm64 partial revert in particular has to be done after the vmap changes) and logical ones (the non-kernel mm fix only makes sense once the vmap/CPA fixes are in place).
---
v5:
* Rebased on latest master of Linus's tree.
* Accumulated tags, thanks everybody!
* Added additional commit to fix race between CPA collapse/attribute set.
* Slight commit message tweaks.
v4:
* Rebased on latest master of Linus's tree.
* Accumulated tags, thanks everybody!
* Updated commit messages further as per Kiryl.
* Took the mmap write lock across the whole CPA operation as per Will.
https://patch.msgid.link/[email protected]
v3:
* Rebased on latest master of Linus's tree.
* Accumulated tags, thanks everybody!
* Reworded commit messages as per Kiryl and Boris.
https://patch.msgid.link/[email protected]
v2:
* Rebased on latest master of Linus's tree.
* Accumulated tags, thanks everybody!
* Fixed cover letter reference to arm64 partial revert as per David C.
* Combined all patches into a 4 patch series for ease of tracking/review and updated cover letter to reflect.
* Reordered patches in series logically - fix vmap, fix CPA issue, handle mm vs. init_mm then revert arm64 mitigation.
* Reworded first patch to be consistent with x86 wording to clearly indicate the intent of the fix is to fix ptdump UAF.
* Reworded arm64 revert patch subject as per Mike, Dev to make clear this is only reverting the ptdump mitigation not the vmap huge support.
* Added Fixes: tag to arm64 revert patch so we backport this also for neatness. Not strictly necessary, but is a better fix overall applied.
* Added note about stable dependency to arm64 revert patch.
* Updated walk_page_range_debug() to remove pointless !walk.mm check - if NULL mm then the mmap lock asserts would NULL pointer deref, and of course no caller does this anyway.
* Updated walk_page_range_debug() to always assert init_mm mmap write lock is held.
* Updated walk_page_range_debug() to always check for start >= end and that it has safe walk ops, and remove the unnecessary walk_kernel_page_table_range() invocation which ultimately does the same thing.
* Typo fixups as per Mike.
* Some small commit message/comment wording fixups.
https://patch.msgid.link/[email protected]
v1:
* vmap/arm64 partial revert series:
https://patch.msgid.link/[email protected]
* CPA patch:
https://patch.msgid.link/[email protected]
* non-init_mm patch:
https://patch.msgid.link/[email protected]
To: Andrew Morton <[email protected]>
To: Suren Baghdasaryan <[email protected]>
To: "Liam R. Howlett" <[email protected]>
To: Vlastimil Babka <[email protected]>
To: Shakeel Butt <[email protected]>
To: David Hildenbrand <[email protected]>
To: Mike Rapoport <[email protected]>
To: Michal Hocko <[email protected]>
To: Uladzislau Rezki <[email protected]>
To: Toshi Kani <[email protected]>
To: Dave Hansen <[email protected]>
To: Andy Lutomirski <[email protected]>
To: Peter Zijlstra <[email protected]>
To: Thomas Gleixner <[email protected]>
To: Ingo Molnar <[email protected]>
To: Borislav Petkov <[email protected]>
To: [email protected]
To: "H. Peter Anvin" <[email protected]>
To: Kiryl Shutsemau <[email protected]>
To: Catalin Marinas <[email protected]>
To: Will Deacon <[email protected]>
To: Dev Jain <[email protected]>
To: Ryan Roberts <[email protected]>
Cc: David Carlier <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: "Denis V. Lunev" <[email protected]>
---
Lorenzo Stoakes (ARM) (5):
mm/vmalloc: acquire init_mm lock on huge vmap to avoid ptdump UAF x86/mm/pat: acquire init_mm write lock on collapse to avoid UAF x86/mm/pat: acquire init_mm read lock on attribute change to avoid UAF mm/ptdump: always stabilise against page table freeing using init_mm
arm64: remove redundant concurrent ptdump UAF mitigation
arch/arm64/include/asm/ptdump.h | 2 -- arch/arm64/mm/mmu.c | 43 ++++------------------------------------- arch/arm64/mm/ptdump.c | 11 ++--------- arch/x86/mm/pat/set_memory.c | 19 ++++++++++++++++-- include/linux/mmap_lock.h | 3 +++ mm/pagewalk.c | 36 ++++++++++++++++++++-------------- mm/ptdump.c | 7 +++++++ mm/vmalloc.c | 40 +++++++++++++++++++++++++++++--------- 8 files changed, 85 insertions(+), 76 deletions(-)
---
base-commit: fce2dfa773ced15f27dd27cd0b482a7473cdcf2a
change-id: 20260710-series-vmap-race-fix-2a4cac988938
Cheers,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
Lorenzo Stoakes (ARM)
maintainerPATCH v5 1/5 mm/vmalloc: acquire init_mm lock on huge vmap to avoid ptdump UAF
Currently there is a nasty race between ptdump and vmap when attempting to map a huge P4D, PUD or PMD entry:
* ptdump walks kernel page table ranges it doesn't own.
* When vmap maps ranges it tries to promotes existing ones to huge page tables in vmap_try_huge_[p4d,pud,pmd]() at P4D, PUD and PMD level, freeing the lower page table in [p4d,pud,pmd]_free_[pud,pmd,pte]_page() when it succeeds.
Both of these things can happen at the same time and as a result ptdump can access a freed page table, resulting in a use-after-free and memory corruption.
This is possible because while ptdump_walk_pgd() holds both the mem hotplug lock and the mmap write lock before invoking walk_page_range_debug(), vmap takes no relevant locks at all.
Fix this by holding the mmap read lock in vmap_try_huge_*() when freeing page tables.
The read lock is sufficient: ptdump is the only walker that must be excluded and it holds the mmap write lock. Other holders of the read lock may run concurrently, but each exclusively owns the range it operates on and cannot reach the page tables freed here.
We also hold the lock while assigning the huge page table entry, which means page table walkers observe only the huge or non-huge page table entry.
We use a trylock to prevent ptdump from blocking vmap making forward progress. This is fine because it's an optimisation in any case, and thus the vmap can safely proceed regardless.
All other kernel page table walkers that touch vmalloc ranges either exclusively own the memory walked or acquire the mmap lock, so this correctly excludes those walkers.
One wrinkle here is commit fa93b45fd397 ("arm64: Enable vmalloc-huge with ptdump"), which addresses the issue for arm64 only by explicitly acquiring the mmap read lock on kernel page table freeing should a concurrent ptdump be in progress.
This is problematic as vmap may acquire the mmap read lock prior to ptdump attempting to acquire an mmap write lock, leading to a deadlock when the mmap read lock is slept upon on page table freeing due to rwsem anti-starvation.
We work around this by predicating the mmap lock being taken on !CONFIG_ARM64 for the time being.
With this patch applied, a follow up will partially revert commit fa93b45fd397 ("arm64: Enable vmalloc-huge with ptdump") and at that stage remove the arm64 ifdeffery.
We also update walk_page_range_debug() to assert the mmap write lock unconditionally and update the comment here to reflect this change.
The issue has existed as long as ptdump was available and vmap freed page tables when promoting to a huge leaf entry, that is, since commit b6bdb7517c3d ("mm/vmalloc: add interfaces to free unmapped page table") for huge ioremap, and commit 121e6f3258fe ("mm/vmalloc: hugepage vmalloc mappings") for huge vmalloc.
Since the former is the earlier of the two we choose that for our Fixes tag.
We also define a guard class for mmap_read_trylock() so we can use cleanup.h to make the scope handling cleaner in the implementation.
This patch is based on work by David Carlier (linked), with gratitude!
--- include/linux/mmap_lock.h | 1 + mm/pagewalk.c | 22 +++++++++++---------- mm/vmalloc.c | 49 ++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 53 insertions(+), 19 deletions(-)
include/linux/mmap_lock.h +1 -0
@@ -621,6 +621,7 @@ static inline void mmap_read_unlock(struct mm_struct *mm) DEFINE_GUARD(mmap_read_lock, struct mm_struct *, mmap_read_lock(_T), mmap_read_unlock(_T)) +DEFINE_GUARD_COND(mmap_read_lock, _try, mmap_read_trylock(_T))static inline void mmap_read_unlock_non_owner (struct mm_struct *mm) {
mm/pagewalk.c +12 -10
@@ -678,6 +678,8 @@ int walk_kernel_page_table_range_lockless(unsigned long start, unsigned long end* will also not lock the PTEs for the pte_entry() callback. * * This is for debugging purposes ONLY. +* +* The mmap write lock must be held. */ int walk_page_range_debug(struct mm_struct *mm, unsigned long start, unsigned long end, const struct mm_walk_ops *ops,
@@ -691,6 +693,16 @@ int walk_page_range_debug(struct mm_struct *mm, unsigned long start, .no_vma = true }; +/* +* When walking userland page tables, an mmap write lock must be held to +* account for munmap() downgrading to an mmap read lock when tearing +* down page tables. +* +* When walking kernel page tables, an mmap write lock must also be held +* to account for page table freeing on vmap huge page mapping. +*/ +mmap_assert_write_locked (mm); +/* For convenience, we allow traversal of kernel mappings. */ if (mm == &init_mm) return walk_kernel_page_table_range(start, end, ops,
@@ -700,16 +712,6 @@ int walk_page_range_debug(struct mm_struct *mm, unsigned long start,if (!check_ops_safe (ops))return -EINVAL ; -/* -* The mmap lock protects the page walker from changes to the page -* tables during the walk. However a read lock is insufficient to -* protect those areas which don't have a VMA as munmap() detaches -* the VMAs before downgrading to a read lock and actually tearing -* down PTEs/page tables. In which case, the mmap write lock should -* be held. -*/ -mmap_assert_write_locked (mm); -return walk_pgd_range (start, end, &walk); }
mm/vmalloc.c +40 -9
76 lines not on this page · raw message
Signature · 1 line
2.55.0
Lorenzo Stoakes (ARM)
maintainerPATCH v5 2/5 x86/mm/pat: acquire init_mm write lock on collapse to avoid UAF
In reply to Lorenzo Stoakes (ARM)x86 implements page attribute modification using its Change Page Attributes (CPA) mechanism.
This tracks properties of ranges such as cache mode through x86 page attributes, and as part of that logic manipulates kernel page tables.
Since commit 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentation") ranges of kernel page table entries can be collapsed into huge page table entries as part of this logic.
As part of this collapse, it frees the page tables which the collapsed entries previously pointed to, and it does so without any relevant locks being held to preclude concurrent kernel page table walkers.
The only way this code can be reached is if CPA_COLLAPSE is specified, and this is only set in set_memory_rox() via:
set_memory_rox()
-> change_page_attr_set_clr()
-> cpa_flush()
-> cpa_collapse_large_pages()
Notable users of this are execmem and bpf when manipulating executable mappings.
However, this is problematic for ptdump as it walks ranges it does not own and thus runs the risk of a use-after-free on page tables freed underneath it.
In addition, concurrent CPA collapse operations are possible which can also cause races.
Resolve the issue by acquiring the mmap write lock on init_mm across the whole operation.
It is safe to acquire a sleeping lock as all the callers invoke set_memory_rox() from process context and in any case, change_page_attr_set_clr() calls vm_unmap_alias() which ultimately takes a mutex, disallowing atomic context here.
--- arch/x86/mm/pat/set_memory.c | 15 ++++++++++++++- include/linux/mmap_lock.h | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-)
arch/x86/mm/pat/set_memory.c +14 -1
@@ -22,6 +22,7 @@#include <linux/cc_platform.h> #include <linux/set_memory.h> #include <linux/memregion.h> +#include <linux/cleanup.h> #include <asm/e820/api.h> #include <asm/processor.h>
@@ -410,7 +411,7 @@ static void __cpa_flush_tlb(void *data)static int collapse_large_pages (unsigned long addr,struct list_head *pgtables); -static void cpa_collapse_large_pages (struct cpa_data *cpa) +static void __cpa_collapse_large_pages (struct cpa_data *cpa) {unsigned long start, addr, end;struct ptdesc *ptdesc, *tmp;
@@ -442,6 +443,18 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa) } } +static void cpa_collapse_large_pages (struct cpa_data *cpa) +{ +/* +* Take the mmap write lock on init_mm to: +* - Avoid a use-after-free if raced by ptdump (which takes its own +* write lock on init_mm). +* - Serialise concurrent CPA walkers. +*/ + scoped_guard(mmap_write_lock, &init_mm) +__cpa_collapse_large_pages (cpa); +} +static void cpa_flush (struct cpa_data *cpa,int cache) {unsigned int i;
include/linux/mmap_lock.h +2 -0
@@ -621,6 +621,8 @@ static inline void mmap_read_unlock(struct mm_struct *mm) DEFINE_GUARD(mmap_read_lock, struct mm_struct *, mmap_read_lock(_T), mmap_read_unlock(_T)) +DEFINE_GUARD(mmap_write_lock, struct mm_struct *, + mmap_write_lock(_T), mmap_write_unlock(_T)) DEFINE_GUARD_COND(mmap_read_lock, _try, mmap_read_trylock(_T))static inline void mmap_read_unlock_non_owner (struct mm_struct *mm)
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH mm-hotfixes v5 2/5] x86/mm/pat: acquire init_mm write lock on collapse to avoid UAF
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [Critical] TOCTOU race in _lookup_address_cpa leading to kernel memory corruption.
- [High] Incomplete Use-After-Free fix against ptdump due to mmap_lock mismatch.
- [High] Use-after-free in populate_pgd rollback path due to premature page freeing. --
--- Patch [2]: [PATCH mm-hotfixes v5 2/5] x86/mm/pat: acquire init_mm write lock on collapse to avoid UAF --- commit b25bc68391fb2bdee5625f9af3d5344a0d10d51f
Author: Lorenzo Stoakes (ARM) <[email protected]>
x86/mm/pat: acquire init_mm write lock on collapse to avoid UAF
This commit adds scoped_guard(mmap_write_lock, &init_mm) to cpa_collapse_large_pages() to avoid use-after-free issues with ptdump and to serialize concurrent CPA walkers.
Quoted 4 lines
arch/x86/mm/pat/set_memory.c +0
-0
[ ... ]
Quoted 4 lines
arch/x86/mm/pat/set_memory.c +0
-0@@ -442,6 +443,18 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa) } }
arch/x86/mm/pat/set_memory.c static void cpa_collapse_large_pages(struct cpa_data *cpa) · line 455
Quoted 10 lines
arch/x86/mm/pat/set_memory.c +10
-0+static void cpa_collapse_large_pages (struct cpa_data *cpa) +{ +/* +* Take the mmap write lock on init_mm to: +* - Avoid a use-after-free if raced by ptdump (which takes its own +* write lock on init_mm). +* - Serialise concurrent CPA walkers. +*/ + scoped_guard(mmap_write_lock, &init_mm) +__cpa_collapse_large_pages (cpa);
[Severity: Critical]
This is a pre-existing issue, but does this lock actually serialize concurrent CPA walkers like set_memory_rw() that might evaluate pmd_leaf()?
Other CPA walkers rely on cpa_lock and do not take init_mm's mmap lock. If CPU1 executes __change_page_attr() via set_memory_rw(), it takes cpa_lock, calls _lookup_address_cpa() -> lookup_address_in_pgd_attr(), and evaluates pmd_leaf(*pmd) as false.
If CPU2 concurrently executes cpa_collapse_large_pages(), taking mmap_write_lock(&init_mm) and pgd_lock, it collapses the PMD into a large page (leaf).
CPU1 then continues and calls pte_offset_kernel(pmd), which reads the new leaf PMD and incorrectly returns a pointer to the 2M data page itself. Then set_pte_atomic() writes to this data page, corrupting physical memory.
Are concurrent standard CPA walkers able to race with this collapse since they do not share the same lock?
[Severity: High]
This is a pre-existing issue, but does taking the mmap lock on init_mm fully prevent races with ptdump?
When ptdump is triggered via debugfs, it callsptdump_walk_pgd_level_debugfs() on current->mm:
ptdump_walk_pgd_level_debugfs(m, current->mm, false);
This takes mmap_write_lock(current->mm) in ptdump_walk_pgd(), which provides no mutual exclusion against mmap_write_lock(&init_mm) taken here.
Since current->mm can be different from init_mm, won't ptdump concurrently walk the shared kernel page tables while this function frees the PTE pages, resulting in the same use-after-free this patch attempts to fix?
arch/x86/mm/pat/set_memory.c static void cpa_collapse_large_pages(struct cpa_data *cpa) · line 459
Quoted 4 lines
arch/x86/mm/pat/set_memory.c +2
-0+} +static void cpa_flush (struct cpa_data *cpa,int cache) {
[ ... ]
As a related note on pre-existing issues in this file:
[Severity: High]
This is a pre-existing issue, but is there a use-after-free in the populate_pgd() rollback path due to premature page freeing?
In unmap_pte_range() during rollback (e.g., if populate_pgd() fails due to OOM during dynamic encryption attribute changes via set_memory_encrypted()):
if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) {
pmd_clear(pmd);The PTE page is returned to the allocator by try_to_free_pte_page() before pmd_clear() clears the PMD entry.
Could a concurrent ptdump or lockless walker read the PMD and access the freed memory during this window?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Lorenzo Stoakes (ARM)
maintainerPATCH v5 3/5 x86/mm/pat: acquire init_mm read lock on attribute change to avoid UAF
In reply to Lorenzo Stoakes (ARM)A previous commit protected us against races between ptdump and CPA collapse, however one still exists between attribute changes and collapse as reported by Denis V. Lunev (linked).
When an attribute change arises, a lockless page table walker obtains a PTE entry, which is later written to via set_pte_atomic():
...
-> change_page_attr_set_clr()
-> __change_page_attr_set_clr()
-> __change_page_attr()
-> _lookup_address_cpa() -> lookup_address_in_pgd_attr() -> [ lockless page table walker ]
-> set_pte_atomic()
There is nothing preventing a concurrent CPA collapse which can free the PTE that was retrieved here, resulting in a use-after-free.
With the mmap write lock taken on init_mm over CPA collapse, we can now resolve this race by acquiring an mmap read lock on init_mm over __change_page_attr_set_clr().
This locks across the whole operation over which the walk and the PTE entry write occurs, solving the race.
It is safe to do this here, as no spinlocks are held upon entry to __change_page_attr_set_clr().
The CPA_COLLAPSE flag is only set by set_memory_rox(), which exclusively operates upon vmalloc ranges, and on x86 only within the module mapping space.
This is important, because some callers directly invoke __change_page_attr_set_clr(), bypassing this lock. However, none of these operate within the module mapping space.
* cpa_process_alias() - a recursive helper called by __change_page_attr_set_clr().
* __set_memory_enc_pgtable() - operates on the direct mapping and (via __vmbus_establish_gpadl()) the vmalloc mapping space.
* __set_pages_[n]p() - called by set_direct_map_[invalid, default, valid]_noflush(), __kernel_map_pages() - operates on the direct map.
* kernel_[un]map_pages_in_pgd() - operates on EFI ranges.
This work is based upon Denis V. Lunev's excellent analysis of the bug with gratitude.
--- arch/x86/mm/pat/set_memory.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/mm/pat/set_memory.c +3 -1
@@ -2122,7 +2122,9 @@ static int change_page_attr_set_clr(unsigned long *addr, int numpages, cpa.curpage =0 ; cpa.force_split = force_split; - ret =__change_page_attr_set_clr (&cpa,1 ); +/* Avoid race with concurrent CPA collapse. */ +scoped_guard (mmap_read_lock, &init_mm) + ret = __change_page_attr_set_clr(&cpa, 1);/* * Check whether we really changed something:
Signature · 1 line
2.55.0
Mike Rapoport
reviewerRe: [PATCH mm-hotfixes v5 3/5] x86/mm/pat: acquire init_mm read lock on attribute change to avoid UAF
Quoted 61 lines · Lorenzo Stoakes (ARM)
A previous commit protected us against races between ptdump and CPA collapse, however one still exists between attribute changes and collapse as reported by Denis V. Lunev (linked).
When an attribute change arises, a lockless page table walker obtains a PTE entry, which is later written to via
set_pte_atomic():...
->change_page_attr_set_clr()
->__change_page_attr_set_clr()
->__change_page_attr()-> _lookup_address_cpa() -> lookup_address_in_pgd_attr() -> [ lockless page table walker ]->
set_pte_atomic()There is nothing preventing a concurrent CPA collapse which can free the PTE that was retrieved here, resulting in a use-after-free.
With the mmap write lock taken on
init_mmover CPA collapse, we can now resolve this race by acquiring an mmap read lock oninit_mmover__change_page_attr_set_clr().This locks across the whole operation over which the walk and the PTE entry write occurs, solving the race.
It is safe to do this here, as no spinlocks are held upon entry to
__change_page_attr_set_clr().The
CPA_COLLAPSEflag is only set byset_memory_rox(), which exclusively operates upon vmalloc ranges, and on x86 only within the module mapping space.This is important, because some callers directly invoke
__change_page_attr_set_clr(), bypassing this lock. However, none of these operate within the module mapping space.*
cpa_process_alias()- a recursive helper called by__change_page_attr_set_clr().
*__set_memory_enc_pgtable()- operates on the direct mapping and (via__vmbus_establish_gpadl()) the vmalloc mapping space.
* __set_pages_[n]p() - called by set_direct_map_[invalid, default, valid]_noflush(),__kernel_map_pages()- operates on the direct map.
* kernel_[un]map_pages_in_pgd() - operates on EFI ranges.This work is based upon Denis V. Lunev's excellent analysis of the bug with gratitude.
Fixes 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentation")Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- arch/x86/mm/pat/set_memory.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)arch/x86/mm/pat/set_memory.c +0
-0
arch/x86/mm/pat/set_memory.c static int change_page_attr_set_clr(unsigned long *addr, int numpages, · line 2127
Quoted 7 lines
arch/x86/mm/pat/set_memory.c +3
-1@@ -2122,7 +2122,9 @@ static int change_page_attr_set_clr(unsigned long *addr, int numpages, cpa.curpage =0 ; cpa.force_split = force_split; - ret =__change_page_attr_set_clr (&cpa,1 ); +/* Avoid race with concurrent CPA collapse. */ +scoped_guard (mmap_read_lock, &init_mm) + ret = __change_page_attr_set_clr(&cpa, 1);
There's a small issue with this. debug_pagealloc_unmap_pages() ends up here and since it's called from __free_pages() it can be in an atomic context.
Quoted 7 lines
arch/x86/mm/pat/set_memory.c +0
-0/* * Check whether we really changed something: Signature · 1 line
2.55.0
Signature · 2 lines
Sincerely yours, Mike.
Lorenzo Stoakes (ARM)
maintainerRe: [PATCH mm-hotfixes v5 3/5] x86/mm/pat: acquire init_mm read lock on attribute change to avoid UAF
Quoted 65 lines · Mike Rapoport
Quoted 64 lines · Lorenzo Stoakes (ARM)
A previous commit protected us against races between ptdump and CPA collapse, however one still exists between attribute changes and collapse as reported by Denis V. Lunev (linked).
When an attribute change arises, a lockless page table walker obtains a PTE entry, which is later written to via
set_pte_atomic():...
->change_page_attr_set_clr()
->__change_page_attr_set_clr()
->__change_page_attr()-> _lookup_address_cpa() -> lookup_address_in_pgd_attr() -> [ lockless page table walker ]->
set_pte_atomic()There is nothing preventing a concurrent CPA collapse which can free the PTE that was retrieved here, resulting in a use-after-free.
With the mmap write lock taken on
init_mmover CPA collapse, we can now resolve this race by acquiring an mmap read lock oninit_mmover__change_page_attr_set_clr().This locks across the whole operation over which the walk and the PTE entry write occurs, solving the race.
It is safe to do this here, as no spinlocks are held upon entry to
__change_page_attr_set_clr().The
CPA_COLLAPSEflag is only set byset_memory_rox(), which exclusively operates upon vmalloc ranges, and on x86 only within the module mapping space.This is important, because some callers directly invoke
__change_page_attr_set_clr(), bypassing this lock. However, none of these operate within the module mapping space.*
cpa_process_alias()- a recursive helper called by__change_page_attr_set_clr().
*__set_memory_enc_pgtable()- operates on the direct mapping and (via__vmbus_establish_gpadl()) the vmalloc mapping space.
* __set_pages_[n]p() - called by set_direct_map_[invalid, default, valid]_noflush(),__kernel_map_pages()- operates on the direct map.
* kernel_[un]map_pages_in_pgd() - operates on EFI ranges.This work is based upon Denis V. Lunev's excellent analysis of the bug with gratitude.
Fixes 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentation")Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- arch/x86/mm/pat/set_memory.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)arch/x86/mm/pat/set_memory.c +0
-0@@ -2122,7 +2122,9 @@ static int change_page_attr_set_clr(unsigned long *addr, int numpages, cpa.curpage =0 ; cpa.force_split = force_split;
Quoted 7 lines
arch/x86/mm/pat/set_memory.c static int change_page_attr_set_clr(unsigned long *addr, int numpages, · line 2127
Quoted 4 lines
arch/x86/mm/pat/set_memory.c +3
-1- ret =__change_page_attr_set_clr (&cpa,1 ); +/* Avoid race with concurrent CPA collapse. */ +scoped_guard (mmap_read_lock, &init_mm) + ret = __change_page_attr_set_clr(&cpa, 1);There's a small issue with this.
debug_pagealloc_unmap_pages()ends up here and since it's called from__free_pages()it can be in an atomic context.
Not the case - the callstack is:
debug_pagealloc_unmap_pages()
-> __kernel_map_pages()
-> __set_pages_[n]p()
-> __change_page_attr_set_clr()
So it calls __change_page_attr_set_clr() not change_page_addr_set_clr() and doesn't hit this code.
As per the commit msg, it (and all the other bare __change_page_attr_set_clr() callers) do not hit ranges that can be collapsed so the race doesn't exist there.
Also the CPA_NO_CHECK_ALIAS flag is set, meaning there's no weird aliasing recursion via cpa_process_alias() (which in any case, also calls __change_page_attr_set_clr() directly anyway).
Quoted 5 lines
Quoted 5 lines
arch/x86/mm/pat/set_memory.c +0
-0/* * Check whether we really changed something:
Quoted 7 lines
Quoted 3 lines
--
2.55.0--
Sincerely yours,
Mike.
Thanks, Lorenzo
[REGRESSION]: [PATCH mm-hotfixes v5 3/5] x86/mm/pat: acquire init_mm read lock on attribute change to avoid UAF
In reply to Lorenzo Stoakes (ARM)Hello Lorenzo,
Quoted 45 lines · Lorenzo Stoakes (ARM)
A previous commit protected us against races between ptdump and CPA collapse, however one still exists between attribute changes and collapse as reported by Denis V. Lunev (linked).
When an attribute change arises, a lockless page table walker obtains a PTE entry, which is later written to via
set_pte_atomic():...
->change_page_attr_set_clr()
->__change_page_attr_set_clr()
->__change_page_attr()-> _lookup_address_cpa() -> lookup_address_in_pgd_attr() -> [ lockless page table walker ]->
set_pte_atomic()There is nothing preventing a concurrent CPA collapse which can free the PTE that was retrieved here, resulting in a use-after-free.
With the mmap write lock taken on
init_mmover CPA collapse, we can now resolve this race by acquiring an mmap read lock oninit_mmover__change_page_attr_set_clr().This locks across the whole operation over which the walk and the PTE entry write occurs, solving the race.
It is safe to do this here, as no spinlocks are held upon entry to
__change_page_attr_set_clr().The
CPA_COLLAPSEflag is only set byset_memory_rox(), which exclusively operates upon vmalloc ranges, and on x86 only within the module mapping space.This is important, because some callers directly invoke
__change_page_attr_set_clr(), bypassing this lock. However, none of these operate within the module mapping space.*
cpa_process_alias()- a recursive helper called by__change_page_attr_set_clr().
*__set_memory_enc_pgtable()- operates on the direct mapping and (via__vmbus_establish_gpadl()) the vmalloc mapping space.
* __set_pages_[n]p() - called by set_direct_map_[invalid, default, valid]_noflush(),__kernel_map_pages()- operates on the direct map.
* kernel_[un]map_pages_in_pgd() - operates on EFI ranges.
Quoted 3 lines
This work is based upon Denis V. Lunev's excellent analysis of the bug with gratitude.
This seems to be causing regression in our linux-next CI [1] on some of the older machines (fi-elk-e7500, fi-ilk-650, fi-pnv-d510)
<4> [196.236867] ======================================================
<4> [196.236878] WARNING: possible circular locking dependency detected
<4> [196.236890] 7.2.0-rc4-next-20260720-next-20260720-g3fe08b9796f3+ #1 Tainted: G S U L
<4> [196.236906] ------------------------------------------------------
<4> [196.236916] core_hotunplug/5687 is trying to acquire lock:
<4> [196.236927] ffffffff839048b8 ((init_mm).mmap_lock){++++}-{4:4}, at: change_page_attr_set_clr+0x10a/0x220 <4> [196.236958]
but task is already holding lock:
<4> [196.236969] ffff88810c852238 (&vm->mutex){+.+.}-{4:4}, at: i915_ggtt_driver_release+0xab/0x260 [i915] <4> [196.237572]
which lock already depends on the new lock.
<4> [196.237585]
the existing dependency chain (in reverse order) is:
<4> [196.237597]
-> #2 (&vm->mutex){+.+.}-{4:4}:
<4> [196.237617] i915_gem_shrinker_taints_mutex+0x35/0x70 [i915] <4> [196.238267] i915_address_space_init+0x226/0x2a0 [i915] <4> [196.238961] i915_ggtt_init_hw+0x2c/0x140 [i915] <4> [196.239638] i915_driver_hw_probe+0x208/0x380 [i915] <4> [196.240260] i915_driver_probe+0x113/0x5b0 [i915] <4> [196.240883] i915_pci_probe+0xe0/0x1d0 [i915]
Detailed log can be seen found in [2]
We confirmed that reverting the patch solves the issue.
Could you please check why the patch causes this regression and provide a fix if necessary?
Regards
Chaitanya
[1] https://intel-gfx-ci.01.org/tree/linux-next/combined-alt.html?
[2] https://intel-gfx-ci.01.org/tree/linux-next/next-20260720/fi-ilk-650/igt@[email protected]
-- Bisect Logs --
git bisect start
# status: waiting for both good and bad commits
# good: [2b74f1f2024486c7a81915528b6aa7f8f5daa021] Merge patch series "software node: provide support for fw_devlink"
git bisect good 2b74f1f2024486c7a81915528b6aa7f8f5daa021
# status: waiting for bad commit, 1 good commit known
# bad: [3fe08b9796f36ef437ab9328e7dd1e5ff2d66603] Add linux-next specific files for 20260720
git bisect bad 3fe08b9796f36ef437ab9328e7dd1e5ff2d66603
# bad: [19137e3cffc35f327bd14d7b8b02f739f1e5c7bc] Merge branch 'main' of ssh://[email protected]/pub/scm/linux/kernel/git/netdev/net-next.git git bisect bad 19137e3cffc35f327bd14d7b8b02f739f1e5c7bc
# skip: [4f9e9b2e992ca79e090771e244bc2096709e13c6] Merge branch 'mm-unstable' of ssh://[email protected]/pub/scm/linux/kernel/git/akpm/mm git bisect skip 4f9e9b2e992ca79e090771e244bc2096709e13c6
# good: [1aa4e2ed7caafbbbedff89fb226a982413469baf] dt-bindings: clock: Replace bouncing emails
git bisect good 1aa4e2ed7caafbbbedff89fb226a982413469baf
# good: [daec24a5ed5da77a108e246ad77aa8b889911f93] wifi: iwlwifi: validate payload length in iwl_pnvm_complete_fn
git bisect good daec24a5ed5da77a108e246ad77aa8b889911f93
# bad: [247e42b76590a27408e5eea7009067cac5ad9feb] Merge branch 'for-next' of ssh://[email protected]/pub/scm/linux/kernel/git/peter.chen/cix.git git bisect bad 247e42b76590a27408e5eea7009067cac5ad9feb
# bad: [d623282ba80e07d3bd7608d905b143041f865f3b] Merge branch 'tty-linus' of ssh://[email protected]/pub/scm/linux/kernel/git/gregkh/tty.git git bisect bad d623282ba80e07d3bd7608d905b143041f865f3b
# good: [6f5156d7a31a8c3b0f34af4675c9299c8f877cbe] Merge tag 'pm-7.2-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
git bisect good 6f5156d7a31a8c3b0f34af4675c9299c8f877cbe
# good: [7d6ca51a444234ca436d7c19282825797507fe82] Merge tag 'drm-fixes-2026-07-18-1' of https://gitlab.freedesktop.org/drm/kernel
git bisect good 7d6ca51a444234ca436d7c19282825797507fe82
# bad: [499079fbac564b04cce6a750111bf1c36aaeb4d5] Merge branch 'mm-hotfixes-unstable' of ssh://[email protected]/pub/scm/linux/kernel/git/akpm/mm git bisect bad 499079fbac564b04cce6a750111bf1c36aaeb4d5
# good: [a2b81de43ca64832fe09844bbf97d1251115d80f] Merge tag 'io_uring-7.2-20260717' of git://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux
git bisect good a2b81de43ca64832fe09844bbf97d1251115d80f
# bad: [b55b09b68a9cd81d2c1f4004e4c258cbfa61b16f] x86/mm/pat: acquire init_mm read lock on attribute change to avoid UAF
git bisect bad b55b09b68a9cd81d2c1f4004e4c258cbfa61b16f
# good: [841587a3eaad22b288d9701f783e20a5046e76b4] mm/hugetlb: fix null nodemask in alloc_fresh_hugetlb_folio
git bisect good 841587a3eaad22b288d9701f783e20a5046e76b4
# good: [2272f397e66bd9f78c2e1f524f8e7b1e01e15005] ocfs2: fix boundary check in ocfs2_check_dir_entry() to use buffer offset git bisect good 2272f397e66bd9f78c2e1f524f8e7b1e01e15005
# good: [a5bba3c7f83071427f5f770b53ce5c59f8ed0af0] fs/proc/task_mmu: fix PAGEMAP_SCAN written state for PMD holes
git bisect good a5bba3c7f83071427f5f770b53ce5c59f8ed0af0
# good: [f539349310317df97b5f1b501c4c6dc2a73c3c37] mm/vmalloc: acquire init_mm lock on huge vmap to avoid ptdump UAF
git bisect good f539349310317df97b5f1b501c4c6dc2a73c3c37
# good: [c6d14c413d47a772a54f6aa7e545a15abc06b1d0] x86/mm/pat: acquire init_mm write lock on collapse to avoid UAF
git bisect good c6d14c413d47a772a54f6aa7e545a15abc06b1d0
# first bad commit: [b55b09b68a9cd81d2c1f4004e4c258cbfa61b16f] x86/mm/pat: acquire init_mm read lock on attribute change to avoid UAF
Quoted 24 lines
Fixes 41d88484c71c ("x86/mm/pat: restore large ROX pages after fragmentation")Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- arch/x86/mm/pat/set_memory.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)arch/x86/mm/pat/set_memory.c +3
-1@@ -2122,7 +2122,9 @@ static int change_page_attr_set_clr(unsigned long *addr, int numpages, cpa.curpage =0 ; cpa.force_split = force_split; - ret =__change_page_attr_set_clr (&cpa,1 ); +/* Avoid race with concurrent CPA collapse. */ +scoped_guard (mmap_read_lock, &init_mm) + ret = __change_page_attr_set_clr(&cpa, 1);/* * Check whether we really changed something:
Lorenzo Stoakes (ARM)
maintainerRe: [REGRESSION]: [PATCH mm-hotfixes v5 3/5] x86/mm/pat: acquire init_mm read lock on attribute change to avoid UAF
Quoted 48 lines · Chaitanya Kumar
Hello Lorenzo,
Quoted 45 lines · Lorenzo Stoakes (ARM)
A previous commit protected us against races between ptdump and CPA collapse, however one still exists between attribute changes and collapse as reported by Denis V. Lunev (linked).
When an attribute change arises, a lockless page table walker obtains a PTE entry, which is later written to via
set_pte_atomic():...
->change_page_attr_set_clr()
->__change_page_attr_set_clr()
->__change_page_attr()-> _lookup_address_cpa() -> lookup_address_in_pgd_attr() -> [ lockless page table walker ]->
set_pte_atomic()There is nothing preventing a concurrent CPA collapse which can free the PTE that was retrieved here, resulting in a use-after-free.
With the mmap write lock taken on
init_mmover CPA collapse, we can now resolve this race by acquiring an mmap read lock oninit_mmover__change_page_attr_set_clr().This locks across the whole operation over which the walk and the PTE entry write occurs, solving the race.
It is safe to do this here, as no spinlocks are held upon entry to
__change_page_attr_set_clr().The
CPA_COLLAPSEflag is only set byset_memory_rox(), which exclusively operates upon vmalloc ranges, and on x86 only within the module mapping space.This is important, because some callers directly invoke
__change_page_attr_set_clr(), bypassing this lock. However, none of these operate within the module mapping space.*
cpa_process_alias()- a recursive helper called by__change_page_attr_set_clr().
*__set_memory_enc_pgtable()- operates on the direct mapping and (via__vmbus_establish_gpadl()) the vmalloc mapping space.
* __set_pages_[n]p() - called by set_direct_map_[invalid, default, valid]_noflush(),__kernel_map_pages()- operates on the direct map.
* kernel_[un]map_pages_in_pgd() - operates on EFI ranges.
Quoted 6 lines
Quoted 3 lines
This work is based upon Denis V. Lunev's excellent analysis of the bug with gratitude.
This seems to be causing regression in our linux-next CI [1] on some of the older machines (fi-elk-e7500, fi-ilk-650, fi-pnv-d510)
Thanks for the report!
A small note - it'd be useful for you to clearly point out the revision of next to make our life a bit easier :)
I can see from below in the stack and the URI it's next-20260720. There was a broken merge in linux-next for a while but it's fixed by 20260720 so that's not it.
Quoted 9 lines
<4> [196.236867] ====================================================== <4> [196.236878] WARNING: possible circular locking dependency detected <4> [196.236890] 7.2.0-rc4-next-20260720-next-20260720-g3fe08b9796f3+ #1
Tainted: G S U L<4> [196.236906] ------------------------------------------------------ <4> [196.236916] core_hotunplug/5687 is trying to acquire lock:
<4> [196.236927]ffffffff839048b8((init_mm).mmap_lock){++++}-{4:4}, at:change_page_attr_set_clr+0x10a/0x220
This is the result of my change indeed.
Quoted 3 lines
<4> [196.236958]
but task is already holding lock:
<4> [196.236969]ffff88810c852238(&vm->mutex){+.+.}-{4:4}, at:
So this is a lock dependency issue as this is obviously an entirely different lock.
Quoted 7 lines
i915_ggtt_driver_release+0xab/0x260[i915] <4> [196.237572]
which lock already depends on the new lock. <4> [196.237585]
the existing dependency chain (in reverse order) is:
<4> [196.237597]
-> #2 (&vm->mutex){+.+.}-{4:4}:
Quoted 6 lines
<4> [196.237617] i915_gem_shrinker_taints_mutex+0x35/0x70 [i915] <4> [196.238267] i915_address_space_init+0x226/0x2a0 [i915] <4> [196.238961] i915_ggtt_init_hw+0x2c/0x140 [i915] <4> [196.239638] i915_driver_hw_probe+0x208/0x380 [i915] <4> [196.240260] i915_driver_probe+0x113/0x5b0 [i915] <4> [196.240883] i915_pci_probe+0xe0/0x1d0 [i915]
(another small note - running scripts/decode_stacktrace.sh would be hugely useful as these offsets aren't so useful unless I have the exact kernel binary you have with debug symbols :)
Quoted 5 lines
Detailed log can be seen found in [2]
We confirmed that reverting the patch solves the issue.
Quoted 8 lines
Could you please check why the patch causes this regression and provide a fix if necessary?
Regards
Chaitanya[1] https://intel-gfx-ci.01.org/tree/linux-next/combined-alt.html?
[2] https://intel-gfx-ci.01.org/tree/linux-next/next-20260720/fi-ilk-650/igt@[email protected]
<snip>
So looking into it we have deadlock potential here:
1. fs_reclaim -> &vm->mutex
< device init >
-> i915_address_space_init()
-> ...
-> i915_gem_shrinker_taints_mutex()
(mark the dependency in lockdep)
This is presumably because the shrinker needs to lock this mutex.
2. init_mm -> fs_reclaim
This was introduced by my patch and there we have:
< caller needs to set memory, e.g. BPF >
-> set_memory_rox()
-> change_page_attr_set_clr()
-> [acquire init_mm lock] <-- my change
-> __change_page_attr_set_clr()
-> __change_page_attr()
-> split_large_page()
-> pte_alloc_one_kernel()
[ allocation ]
< trigger direct reclaim >
Thus init_mm -> fs_reclaim becasue in split_large_page() there's:
if (!debug_pagealloc_enabled()) spin_unlock(&cpa_lock); pte = pte_alloc_one_kernel(&init_mm); <-- allocate if (!debug_pagealloc_enabled()) spin_lock(&cpa_lock); if (!pte) return -ENOMEM;
Note the CPA lock dance, will come back to that as that's how to solve this issue...
3. &vm->mutex -> init_mm
< device remove >
-> i915_ggtt_driver_release()
-> ggtt_cleanup_hw()
-> [acquire &vm->mutex lock]
-> ggtt->vm.cleanup == gmch_ggtt_remove()
-> intel_gmch_remove()
-> intel_gtt_teardown_scratch_page()
-> set_pages_wb()
-> set_memory_wb()
-> _set_memory_wb()
-> change_page_attr_clear()
-> change_page_attr_set_clr()
-> [acquire init_mm lock] <-- my change
This &vm->mutex -> init_mm
And so there is a circular dependency:
&vm->mutex ---> init_mm
^ |
\ v
\--- fs_reclaim(lockdep should totally do diagrams like this :)
In reality to hit this you'd need something to be doing direct reclaim on allocating a split page table during a CPA operation with a simultaneous contended init_mm lock.
But this issue is broader than that really - we can't be holding init_mm over an allocation.
split_large_page() calls __split_large_page() after dropping the CPA lock, which opens with a revalidation:
spin_lock(&pgd_lock);
/*
* Check for races, another CPU might have split this page
* up for us already:
*/
tmp = _lookup_address_cpa(cpa, address, &level, &nx, &rw);
if (tmp != kpte) {
spin_unlock(&pgd_lock);
return 1;
}There's also revalidation in __change_page_attr() on every split:
err = split_large_page(cpa, kpte, address); if (!err) goto repeat;
(Where goto repeat entails a complete rewalk)
So dropping the init_mm lock here should not result in any problems with being raced by a collapse and was something already accounted for.
So the broad strokes of the fix is to eliminate the init_mm -> fs_reclaim dependency and thus break the dependency loop.
I'll send a respin with this addressed.
Cheers, Lorenzo
Re: [REGRESSION]: [PATCH mm-hotfixes v5 3/5] x86/mm/pat: acquire init_mm read lock on attribute change to avoid UAF
Quoted 53 lines · Lorenzo Stoakes (ARM)
Quoted 52 lines · Chaitanya Kumar
Hello Lorenzo,
Quoted 48 lines · Lorenzo Stoakes (ARM)
A previous commit protected us against races between ptdump and CPA collapse, however one still exists between attribute changes and collapse as reported by Denis V. Lunev (linked).
When an attribute change arises, a lockless page table walker obtains a PTE entry, which is later written to via
set_pte_atomic():...
->change_page_attr_set_clr()
->__change_page_attr_set_clr()
->__change_page_attr()-> _lookup_address_cpa() -> lookup_address_in_pgd_attr() -> [ lockless page table walker ]->
set_pte_atomic()There is nothing preventing a concurrent CPA collapse which can free the PTE that was retrieved here, resulting in a use-after-free.
With the mmap write lock taken on
init_mmover CPA collapse, we can now resolve this race by acquiring an mmap read lock oninit_mmover__change_page_attr_set_clr().This locks across the whole operation over which the walk and the PTE entry write occurs, solving the race.
It is safe to do this here, as no spinlocks are held upon entry to
__change_page_attr_set_clr().The
CPA_COLLAPSEflag is only set byset_memory_rox(), which exclusively operates upon vmalloc ranges, and on x86 only within the module mapping space.This is important, because some callers directly invoke
__change_page_attr_set_clr(), bypassing this lock. However, none of these operate within the module mapping space.*
cpa_process_alias()- a recursive helper called by__change_page_attr_set_clr().*
__set_memory_enc_pgtable()- operates on the direct mapping and (via__vmbus_establish_gpadl()) the vmalloc mapping space.
* __set_pages_[n]p() - called by set_direct_map_[invalid, default, valid]_noflush(),__kernel_map_pages()- operates on the direct map.
* kernel_[un]map_pages_in_pgd() - operates on EFI ranges.This work is based upon Denis V. Lunev's excellent analysis of the bug with gratitude.
Quoted 8 lines
Quoted 2 lines
This seems to be causing regression in our linux-next CI [1] on some of the older machines (fi-elk-e7500, fi-ilk-650, fi-pnv-d510)
Thanks for the report!
A small note - it'd be useful for you to clearly point out the revision of next to make our life a bit easier :)
Ack.
Quoted 37 lines
I can see from below in the stack and the URI it's next-20260720. There was a broken merge in linux-next for a while but it's fixed by 20260720 so that's not it.
Quoted 9 lines
<4> [196.236867] ====================================================== <4> [196.236878] WARNING: possible circular locking dependency detected <4> [196.236890] 7.2.0-rc4-next-20260720-next-20260720-g3fe08b9796f3+ #1
Tainted: G S U L<4> [196.236906] ------------------------------------------------------ <4> [196.236916] core_hotunplug/5687 is trying to acquire lock:
<4> [196.236927]ffffffff839048b8((init_mm).mmap_lock){++++}-{4:4}, at:change_page_attr_set_clr+0x10a/0x220This is the result of my change indeed.
Quoted 3 lines
<4> [196.236958]
but task is already holding lock:
<4> [196.236969]ffff88810c852238(&vm->mutex){+.+.}-{4:4}, at:So this is a lock dependency issue as this is obviously an entirely different lock.
Quoted 7 lines
i915_ggtt_driver_release+0xab/0x260[i915] <4> [196.237572]
which lock already depends on the new lock. <4> [196.237585]
the existing dependency chain (in reverse order) is:
<4> [196.237597]
-> #2 (&vm->mutex){+.+.}-{4:4}:Quoted 6 lines
<4> [196.237617] i915_gem_shrinker_taints_mutex+0x35/0x70 [i915] <4> [196.238267] i915_address_space_init+0x226/0x2a0 [i915] <4> [196.238961] i915_ggtt_init_hw+0x2c/0x140 [i915] <4> [196.239638] i915_driver_hw_probe+0x208/0x380 [i915] <4> [196.240260] i915_driver_probe+0x113/0x5b0 [i915] <4> [196.240883] i915_pci_probe+0xe0/0x1d0 [i915]
Quoted 4 lines
(another small note - running
scripts/decode_stacktrace.shwould be hugely useful as these offsets aren't so useful unless I have the exact kernel binary you have with debug symbols :)
We have not been asked for that yet but we can surely provide it if it becomes absolutely necessary for debugging.
Quoted 112 lines
Quoted 5 lines
Detailed log can be seen found in [2]
We confirmed that reverting the patch solves the issue.
Quoted 8 lines
Could you please check why the patch causes this regression and provide a fix if necessary?
Regards
Chaitanya[1] https://intel-gfx-ci.01.org/tree/linux-next/combined-alt.html?
[2] https://intel-gfx-ci.01.org/tree/linux-next/next-20260720/fi-ilk-650/igt@[email protected]<snip>
So looking into it we have deadlock potential here:
1.
fs_reclaim->&vm->mutex< device init >
->i915_address_space_init()
-> ...
->i915_gem_shrinker_taints_mutex()
(mark the dependency in lockdep)This is presumably because the shrinker needs to lock this mutex.
2.
init_mm->fs_reclaimThis was introduced by my patch and there we have:
< caller needs to set memory, e.g. BPF >
->set_memory_rox()
->change_page_attr_set_clr()
-> [acquireinit_mmlock] <-- my change
->__change_page_attr_set_clr()
->__change_page_attr()
->split_large_page()
->pte_alloc_one_kernel()
[ allocation ]
< trigger direct reclaim >Thus
init_mm->fs_reclaimbecasue insplit_large_page()there's:if (!debug_pagealloc_enabled()) spin_unlock(&cpa_lock); pte = pte_alloc_one_kernel(&init_mm); <-- allocate if (!debug_pagealloc_enabled()) spin_lock(&cpa_lock); if (!pte) return -ENOMEM;Note the CPA lock dance, will come back to that as that's how to solve this issue...
3.
&vm->mutex->init_mm< device remove >
->i915_ggtt_driver_release()
->ggtt_cleanup_hw()
-> [acquire&vm->mutexlock]
->ggtt->vm.cleanup==gmch_ggtt_remove()
->intel_gmch_remove()
->intel_gtt_teardown_scratch_page()
->set_pages_wb()
->set_memory_wb()
->_set_memory_wb()
->change_page_attr_clear()
->change_page_attr_set_clr()
-> [acquireinit_mmlock] <-- my changeThis
&vm->mutex->init_mmAnd so there is a circular dependency:
&vm->mutex--->init_mm^ | \ v \--- fs_reclaim(lockdep should totally do diagrams like this :)
In reality to hit this you'd need something to be doing direct reclaim on allocating a split page table during a CPA operation with a simultaneous contended
init_mmlock.But this issue is broader than that really - we can't be holding
init_mmover an allocation.
split_large_page()calls__split_large_page()after dropping the CPA lock, which opens with a revalidation:spin_lock(&pgd_lock); /* * Check for races, another CPU might have split this page * up for us already: */ tmp = _lookup_address_cpa(cpa, address, &level, &nx, &rw); if (tmp != kpte) { spin_unlock(&pgd_lock); return 1; }There's also revalidation in
__change_page_attr()on every split:err = split_large_page(cpa, kpte, address); if (!err) goto repeat;(Where goto repeat entails a complete rewalk)
Quoted 8 lines
So dropping the
init_mmlock here should not result in any problems with being raced by a collapse and was something already accounted for.So the broad strokes of the fix is to eliminate the
init_mm->fs_reclaimdependency and thus break the dependency loop.I'll send a respin with this addressed.
Thank you for sending the fixes.
CI looks good now.
https://intel-gfx-ci.01.org/tree/linux-next/combined-alt.html?
==
Chaitanya
Quoted 1 line
Cheers, Lorenzo
Lorenzo Stoakes (ARM)
maintainerRe: [REGRESSION]: [PATCH mm-hotfixes v5 3/5] x86/mm/pat: acquire init_mm read lock on attribute change to avoid UAF
Quoted 11 lines · Chaitanya Kumar
Quoted 5 lines · Lorenzo Stoakes (ARM)
Thanks for the report!
A small note - it'd be useful for you to clearly point out the revision of next to make our life a bit easier :)
Ack.
Thanks! :)
Quoted 8 lines
Quoted 4 lines
(another small note - running
scripts/decode_stacktrace.shwould be hugely useful as these offsets aren't so useful unless I have the exact kernel binary you have with debug symbols :)We have not been asked for that yet but we can surely provide it if it becomes absolutely necessary for debugging.
It would be SUPER useful if it's not too difficult to implement, thanks :)
Quoted 5 lines
Thank you for sending the fixes.
CI looks good now.
https://intel-gfx-ci.01.org/tree/linux-next/combined-alt.html?
Great thanks!
Cheers, Lorenzo
Lorenzo Stoakes (ARM)
maintainerPATCH v5 4/5 mm/ptdump: always stabilise against page table freeing using init_mm
In reply to Lorenzo Stoakes (ARM)Previous commits have established the invariant that kernel page table freeing is performed while an mmap read lock on init_mm is held, which fixes races between ptdump and kernel page table freeing over init_mm.
However, x86 and arm64 can perform a ptdump over an mm other than init_mm via ptdump_walk_pgd() and since kernel memory ranges are shared across non-kernel mm's, this means that the race still exists for these cases.
Fix this by acquiring a nested mmap write lock for init_mm in ptdump_walk_pgd().
This is safe as we take this after mmap write locking the mm, and nothing acquires the init_mm lock first before locking an arbitrary mm, so no deadlock is possible.
Also update walk_page_range_debug() to assert that init_mm is write locked, add a comment explaining why and remove some redundant code, and eliminate the unnecessary and confusing invocation of walk_kernel_page_table_range().
We can safely remove the non-NULL check for walk.mm, as the mmap lock asserts would NULL pointer deref if it was (and of course no callers do this).
The first point at which ptdump can race kernel page table freeing is commit b6bdb7517c3d ("mm/vmalloc: add interfaces to free unmapped page table"), so we target this in the Fixes tag.
--- mm/pagewalk.c | 14 +++++++++----- mm/ptdump.c | 7 +++++++ 2 files changed, 16 insertions(+), 5 deletions(-)
mm/pagewalk.c +9 -5
@@ -702,12 +702,16 @@ int walk_page_range_debug(struct mm_struct *mm, unsigned long start,* to account for page table freeing on vmap huge page mapping. */ mmap_assert_write_locked (mm); +/* +* x86, arm64 ptdump allow walks of efi mm's and x86 ptdump allows walks +* of arbitrary mm's. +* +* However, they both must also hold the init_mm lock to account for +* concurrent kernel page table freeing. +*/ +mmap_assert_write_locked (&init_mm); -/* For convenience, we allow traversal of kernel mappings. */ -if (mm == &init_mm) -return walk_kernel_page_table_range (start, end, ops, - pgd, private); -if (start >= end || !walk.mm) +if (start >= end)return -EINVAL ;if (!check_ops_safe (ops))return -EINVAL ;
mm/ptdump.c +7 -0
@@ -178,11 +178,18 @@ void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm, pgd_t *pgd)get_online_mems ();mmap_write_lock (mm); +/* To stabilise kernel page tables we must hold the init_mm lock too. */ +if (mm != &init_mm) +mmap_write_lock_nested (&init_mm,SINGLE_DEPTH_NESTING ); +while (range->start != range->end) {walk_page_range_debug (mm, range->start, range->end, &ptdump_ops, pgd, st); range++; } + +if (mm != &init_mm) +mmap_write_unlock (&init_mm);mmap_write_unlock (mm);put_online_mems ();
Signature · 1 line
2.55.0
Lorenzo Stoakes (ARM)
maintainerPATCH v5 5/5 arm64: remove redundant concurrent ptdump UAF mitigation
In reply to Lorenzo Stoakes (ARM)This partially reverts commit fa93b45fd397 ("arm64: Enable vmalloc-huge with ptdump"), retaining vmalloc-huge support but eliminating the now redundant mitigation against a race between huge vmap page table freeing and ptdump, as this issue has now been fixed at core.
We also simultaneously remove the arm64 if-deffery when acquiring the mmap read lock upon vmap huge page table promotion as it is no longer required.
Note that this patch relies on the preceding vmalloc patch, and should not be backported alone.
--- arch/arm64/include/asm/ptdump.h | 2 -- arch/arm64/mm/mmu.c | 43 ++++------------------------------------- arch/arm64/mm/ptdump.c | 11 ++--------- mm/vmalloc.c | 15 +++----------- 4 files changed, 9 insertions(+), 62 deletions(-)
arch/arm64/include/asm/ptdump.h +0 -2
@@ -7,8 +7,6 @@#include <linux/ptdump.h> -DECLARE_STATIC_KEY_FALSE (arm64_ptdump_lock_key); -#ifdef CONFIG_PTDUMP #include <linux/mm_types.h>
arch/arm64/mm/mmu.c +4 -39
81 lines not on this page · raw message
arch/arm64/mm/ptdump.c +2 -9
@@ -283,13 +283,6 @@ void note_page_flush(struct ptdump_state *pt_st)note_page (pt_st,0 ,-1 ,pte_val (pte_zero)); } -static void arm64_ptdump_walk_pgd (struct ptdump_state *st,struct mm_struct *mm) -{ -static_branch_inc (&arm64_ptdump_lock_key); -ptdump_walk_pgd (st, mm,NULL ); -static_branch_dec (&arm64_ptdump_lock_key); -} -void ptdump_walk (struct seq_file *s,struct ptdump_info *info) {unsigned long end = ~0UL ;
@@ -318,7 +311,7 @@ void ptdump_walk(struct seq_file *s, struct ptdump_info *info) } }; -arm64_ptdump_walk_pgd (&st.ptdump, info->mm); +ptdump_walk_pgd (&st.ptdump, info->mm,NULL ); }static void __init ptdump_initialize (void )
@@ -360,7 +353,7 @@ bool ptdump_check_wx(void) } }; -arm64_ptdump_walk_pgd (&st.ptdump, &init_mm); +ptdump_walk_pgd (&st.ptdump, &init_mm,NULL );if (st.wx_pages || st.uxn_pages) { pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
mm/vmalloc.c +3 -12
@@ -169,10 +169,7 @@ static int vmap_try_huge_pmd(pmd_t *pmd, unsigned long addr, unsigned long end,* Concurrent read lock holders are safe: each exclusively owns * the range it operates on and cannot reach this page table. */ -#ifndef CONFIG_ARM64 - scoped_cond_guard(mmap_read_lock_try, return 0, &init_mm) -#endif - { + scoped_cond_guard(mmap_read_lock_try, return 0, &init_mm) {if (!pmd_free_pte_page (pmd, addr))return 0 ;return pmd_set_huge (pmd, phys_addr, prot);
@@ -229,10 +226,7 @@ static int vmap_try_huge_pud(pud_t *pud, unsigned long addr, unsigned long end,return pud_set_huge (pud, phys_addr, prot);/* See comment in vmap_try_huge_pmd(). */ -#ifndef CONFIG_ARM64 - scoped_cond_guard(mmap_read_lock_try, return 0, &init_mm) -#endif - { + scoped_cond_guard(mmap_read_lock_try, return 0, &init_mm) {if (!pud_free_pmd_page (pud, addr))return 0 ;return pud_set_huge (pud, phys_addr, prot);
@@ -289,10 +283,7 @@ static int vmap_try_huge_p4d(p4d_t *p4d, unsigned long addr, unsigned long end,return p4d_set_huge (p4d, phys_addr, prot);/* See comment in vmap_try_huge_pmd(). */ -#ifndef CONFIG_ARM64 - scoped_cond_guard(mmap_read_lock_try, return 0, &init_mm) -#endif - { + scoped_cond_guard(mmap_read_lock_try, return 0, &init_mm) {if (!p4d_free_pud_page (p4d, addr))return 0 ;return p4d_set_huge (p4d, phys_addr, prot);
Signature · 1 line
2.55.0
Andrew Morton
maintainerQuoted 23 lines · On Fri, 17 Jul 2026 18:30:06 +0100 "Lorenzo Stoakes (ARM)" <[email protected]>…
Kernel page table walkers fall into two broad categories - those ranges where no exclusion is required via
walk_kernel_page_table_range_lockless()and those where exclusion is required viawalk_kernel_page_table_range()orwalk_page_range_debug().The former category is used only by arm64 arch code operating on ranges it both wholly owns and does not concurrently write.
The latter category consists of kernel page table walkers operating on ranges that are wholly owned (but which need exclusion against concurrent writers).
The lock used for exclusion is the mmap lock, and for kernel ranges this the mmap lock on
init_mm.ptdump is a special case being both the only user of
walk_page_range_debug(), and the only case in which it walks ranges it does not own.This presents a problem, as page tables may be freed under ptdump. And indeed there is a use-after-free bug in the kernel as a result, which this series addresses.
Quoted 7 lines
...
This series works around this by #ifndef CONFIG_ARM64'ing the mmap read lock in vmap logic, then partially reverting commit
fa93b45fd397("arm64:
Enable vmalloc-huge with ptdump"), keeping the enablement of huge vmap support, and removing the ifdeffery with the partial revert patch.
Thanks, I updated mm-hotfixes-unstable to this version.
Quoted 5 lines
v5:
* Rebased on latest master of Linus's tree.
* Accumulated tags, thanks everybody!
* Added additional commit to fix race between CPA collapse/attribute set.
* Slight commit message tweaks.
Here's how v5 altered mm.git. This is from the addition of [3/5] - no other patches changed.
arch/x86/mm/pat/set_memory.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/mm/pat/set_memory.c renamed from arch/x86/mm/pat/set_memory.c~b +3 -1
@@ -2122,7 +2122,9 @@ static int change_page_attr_set_clr(unsi cpa.curpage =0 ; cpa.force_split = force_split; - ret =__change_page_attr_set_clr (&cpa,1 ); +/* Avoid race with concurrent CPA collapse. */ +scoped_guard (mmap_read_lock, &init_mm) + ret = __change_page_attr_set_clr(&cpa, 1);/* * Check whether we really changed something:
_