FAILED: patch "[PATCH] mm/ptdump: always stabilise against page table freeing using" failed to apply to 6.12-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkout FETCH_HEAD
git cherry-pick -x 27c32e5538344b13c1505a08861e04620c125d47
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026081755-wanting-ended-b457@gregkh' --subject-prefix 'PATCH 6.12.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 27c32e5538344b13c1505a08861e04620c125d47 Mon Sep 17 00:00:00 2001
From: "Lorenzo Stoakes (ARM)" <[email protected]>
Date: Thu, 23 Jul 2026 16:16:34 +0100
Subject: [PATCH] mm/ptdump: always stabilise against page table freeing using
init_mm
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 +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 ();
Sasha Levin
maintainerPATCH 1/3 mm/pagewalk: split walk_page_range_novma() into kernel/user parts
From: Lorenzo Stoakes <[email protected]>
[ Upstream commit 96d81e4766f9e88b66a0502b5a7f34a4c20ac754 ]
walk_page_range_novma() is rather confusing - it supports two modes, one used often, the other used only for debugging.
The first mode is the common case of traversal of kernel page tables, which is what nearly all callers use this for.
Secondly it provides an unusual debugging interface that allows for the traversal of page tables in a userland range of memory even for that memory which is not described by a VMA.
It is far from certain that such page tables should even exist, but perhaps this is precisely why it is useful as a debugging mechanism.
As a result, this is utilised by ptdump only. Historically, things were reversed - ptdump was the only user, and other parts of the kernel evolved to use the kernel page table walking here.
Since we have some complicated and confusing locking rules for the novma case, it makes sense to separate the two usages into their own functions.
Doing this also provide self-documentation as to the intent of the caller
- are they doing something rather unusual or are they simply doing a standard kernel page table walk?
We therefore establish two separate functions - walk_page_range_debug() for this single usage, and walk_kernel_page_table_range() for general kernel page table walking.
The walk_page_range_debug() function is currently used to traverse both userland and kernel mappings, so we maintain this and in the case of kernel mappings being traversed, we have walk_page_range_debug() invoke walk_kernel_page_table_range() internally.
We additionally make walk_page_range_debug() internal to mm.
Stable-dep-of: 27c32e553834 ("mm/ptdump: always stabilise against page table freeing using init_mm")
--- arch/loongarch/mm/pageattr.c | 2 +- arch/openrisc/kernel/dma.c | 4 +- arch/riscv/mm/pageattr.c | 8 ++-- include/linux/pagewalk.h | 7 ++-- mm/hugetlb_vmemmap.c | 2 +- mm/internal.h | 6 +++ mm/pagewalk.c | 77 +++++++++++++++++++++++++----------- mm/ptdump.c | 3 +- 8 files changed, 74 insertions(+), 35 deletions(-)
arch/loongarch/mm/pageattr.c +1 -1
@@ -118,7 +118,7 @@ static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask, pgpreturn 0 ;mmap_write_lock (&init_mm); - ret =walk_page_range_novma (&init_mm, start, end, &pageattr_ops,NULL , &masks); + ret =walk_kernel_page_table_range (start, end, &pageattr_ops,NULL , &masks);mmap_write_unlock (&init_mm);flush_tlb_kernel_range (start, end);
arch/openrisc/kernel/dma.c +2 -2
@@ -75,7 +75,7 @@ void *arch_dma_set_uncached(void *cpu_addr, size_t size)* them and setting the cache-inhibit bit. */ mmap_write_lock (&init_mm); - error =walk_page_range_novma (&init_mm, va, va + size, + error =walk_kernel_page_table_range (va, va + size, &set_nocache_walk_ops,NULL ,NULL );mmap_write_unlock (&init_mm);
@@ -90,7 +90,7 @@ void arch_dma_clear_uncached(void *cpu_addr, size_t size)mmap_write_lock (&init_mm);/* walk_page_range shouldn't be able to fail here */ -WARN_ON (walk_page_range_novma (&init_mm, va, va + size, +WARN_ON (walk_kernel_page_table_range (va, va + size, &clear_nocache_walk_ops,NULL ,NULL ));mmap_write_unlock (&init_mm); }
arch/riscv/mm/pageattr.c +4 -4
@@ -299,7 +299,7 @@ static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask,if (ret) goto unlock; - ret =walk_page_range_novma (&init_mm, lm_start, lm_end, + ret =walk_kernel_page_table_range (lm_start, lm_end, &pageattr_ops,NULL , &masks);if (ret) goto unlock;
@@ -317,13 +317,13 @@ static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask,if (ret) goto unlock; - ret =walk_page_range_novma (&init_mm, lm_start, lm_end, + ret =walk_kernel_page_table_range (lm_start, lm_end, &pageattr_ops,NULL , &masks);if (ret) goto unlock; } - ret =walk_page_range_novma (&init_mm, start, end, &pageattr_ops,NULL , + ret =walk_kernel_page_table_range (start, end, &pageattr_ops,NULL , &masks); unlock:
@@ -335,7 +335,7 @@ static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask,*/ flush_tlb_all ();#else - ret =walk_page_range_novma (&init_mm, start, end, &pageattr_ops,NULL , + ret =walk_kernel_page_table_range (start, end, &pageattr_ops,NULL , &masks);mmap_write_unlock (&init_mm);
include/linux/pagewalk.h +3 -4
@@ -117,10 +117,9 @@ struct mm_walk {int walk_page_range (struct mm_struct *mm,unsigned long start,unsigned long end,const struct mm_walk_ops *ops,void *private); -int walk_page_range_novma (struct mm_struct *mm,unsigned long start, -unsigned long end,const struct mm_walk_ops *ops, -pgd_t *pgd, -void *private); +int walk_kernel_page_table_range (unsigned long start, +unsigned long end,const struct mm_walk_ops *ops, +pgd_t *pgd,void *private);int walk_page_range_vma (struct vm_area_struct *vma,unsigned long start,unsigned long end,const struct mm_walk_ops *ops,void *private);
mm/hugetlb_vmemmap.c +1 -1
@@ -166,7 +166,7 @@ static int vmemmap_remap_range(unsigned long start, unsigned long end,VM_BUG_ON (!PAGE_ALIGNED (start | end));mmap_read_lock (&init_mm); - ret =walk_page_range_novma (&init_mm, start, end, &vmemmap_remap_ops, + ret =walk_kernel_page_table_range (start, end, &vmemmap_remap_ops,NULL , walk);mmap_read_unlock (&init_mm);if (ret)
mm/internal.h +6 -0
@@ -12,6 +12,7 @@#include <linux/mm.h> #include <linux/mm_inline.h> #include <linux/pagemap.h> +#include <linux/pagewalk.h> #include <linux/rmap.h> #include <linux/swap.h> #include <linux/swapops.h>
@@ -1540,4 +1541,9 @@ static inline void accept_page(struct page *page) }#endif /* CONFIG_UNACCEPTED_MEMORY */ +/* pagewalk.c */ +int walk_page_range_debug (struct mm_struct *mm,unsigned long start, +unsigned long end,const struct mm_walk_ops *ops, +pgd_t *pgd,void *private); +#endif /* __MM_INTERNAL_H */
mm/pagewalk.c +55 -22
118 lines not on this page · raw message
mm/ptdump.c +2 -1
@@ -4,6 +4,7 @@#include <linux/debugfs.h> #include <linux/ptdump.h> #include <linux/kasan.h> +#include "internal.h" #if defined(CONFIG_KASAN_GENERIC ) || defined(CONFIG_KASAN_SW_TAGS )/*
@@ -156,7 +157,7 @@ void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm, pgd_t *pgd)get_online_mems ();mmap_write_lock (mm);while (range->start != range->end) { -walk_page_range_novma (mm, range->start, range->end, +walk_page_range_debug (mm, range->start, range->end, &ptdump_ops, pgd, st); range++; }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/3 mm/vmalloc: acquire init_mm lock on huge vmap to avoid ptdump UAF
From: "Lorenzo Stoakes (ARM)" <[email protected]>
[ Upstream commit 26444eb71465c9934d9d418ef69c43f61185329b ]
Patch series "mm: fix UAF caused by race between ptdump and vmap pgtable freeing", v6.
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 is 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 are also addressed 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 x86'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).
This patch (of 3):
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!
Stable-dep-of: 27c32e553834 ("mm/ptdump: always stabilise against page table freeing using init_mm")
--- mm/pagewalk.c | 22 ++++++++++--------- mm/vmalloc.c | 60 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 66 insertions(+), 16 deletions(-)
mm/pagewalk.c +12 -10
@@ -561,6 +561,8 @@ int walk_kernel_page_table_range(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,
@@ -574,6 +576,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,
@@ -581,16 +593,6 @@ int walk_page_range_debug(struct mm_struct *mm, unsigned long start,if (start >= end || !walk.mm)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 +54 -6
102 lines not on this page · raw message
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/3 mm/ptdump: always stabilise against page table freeing using init_mm
In reply to Sasha LevinFrom: "Lorenzo Stoakes (ARM)" <[email protected]>
[ Upstream commit 27c32e5538344b13c1505a08861e04620c125d47 ]
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
@@ -585,12 +585,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 ;return walk_pgd_range (start, end, &walk);
mm/ptdump.c +7 -0
@@ -156,11 +156,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.53.0