mm/vmalloc: fix up vmap error handling
Subsystems (6)
v6 cover mm/vmalloc: fix up vmap error handling
__vmap_pages_range_noflush() and friends can install some PTEs before failing and leave them mapped, and the kernel callers do not agree on who cleans them up. pcpu_map_pages() and kmsan_ioremap_page_range() roll back what they mapped before the failure, while vm_module_tags_populate() and the __GFP_NOFAIL retry loop in __vmalloc_area_node() rely on the mapping functions cleaning up and do not call anything like vunmap_range() themselves. When the same range is mapped again, the attempt hits the leftovers and fails with a warning in vmap_pages_pte_range(). A leftover huge pmd is worse. During the retry, vmap_try_huge_pmd() calls pmd_free_pte_page() on it, and the x86 implementation cannot tell a leaf entry from a page table and frees the mapped page.
After discussing with Suren and Ulad [1], we move the rollback into the mapping functions. A failed vmap leaves nothing mapped, and the callers no longer clean up after it.
Patch 1 gives __vunmap_range_noflush() a can_resched argument, the failure paths can be reached with a non-blocking gfp mask and the undo must not schedule there. Patch 2 is the KMSAN part, the shadow and the origin of a range are mapped by two separate calls, so the rollback there has to cover both. Patch 3 fixes a stale name in a comment, patch 4 the order of the cache flushes around the KMSAN metadata. Patches 5 and 6 reject an unaligned range at the entries of vmap_page_range() and __vunmap_range_noflush(). Patch 7 adds the rollback to vmap_page_range(), patch 8 to __vmap_pages_range_noflush(), the KMSAN metadata and the batched vmap() path. Patches 9 and 10 drop the powerpc and percpu cleanups that are not needed anymore.
Changes since v5:
- new patch 3, the comment of __vunmap_range_noflush() asks the caller for flush_cache_vmap(), it should be flush_cache_vunmap()
- new patch 4, the KMSAN metadata teardown flushed the cache after the page tables were cleared, the order is the other way round, and the ioremap metadata pages are now flushed before they are freed
- v5 patch 1 is renamed and its Fixes: tag is dropped, so it does not travel to stable together with patch 2 anymore
- v5 patch 2 flushes the cache and the TLB of the shadow range now, as Documentation/core-api/cachetlb.rst requires
- v5 patch 5 is folded into patch 7, the cache is only flushed after the vmap succeeded
- v5 patches 8 and 9 are folded into patch 8, together with the rollback of the KMSAN metadata and of the batched vmap() path
- the rollback uses the new vunmap_range_nokmsan(), it flushes the cache and the TLB of the range it undoes
- the rollback of the KMSAN metadata uses the new kmsan_vunmap_range(), it flushes the TLB of the metadata windows
- mapped_pages is an unsigned long now, the byte count of the undone range overflowed an unsigned int on large mappings
- the range that the walk mapped is tracked per PMD now, v5 tracked it per PTE chunk
[1] https://lore.kernel.org/all/[email protected]/
v5 link: https://lore.kernel.org/all/[email protected]/
v4 link: https://lore.kernel.org/all/[email protected]/
v3 link: https://lore.kernel.org/all/[email protected]/
v2 link: https://lore.kernel.org/all/[email protected]/
v1 link: https://lore.kernel.org/all/[email protected]/
Hao Ge (10):
mm/vmalloc: allow the vmap unmap paths to run in atomic context mm/kmsan: undo the shadow mapping when the origin mapping fails mm/vmalloc: fix a typo in the __vunmap_range_noflush() comment mm/kmsan: fix the metadata cache flush ordering mm/vmalloc: reject an unaligned range in vmap_page_range() mm/vmalloc: reject an unaligned range in __vunmap_range_noflush() mm/vmalloc: undo what a failed vmap_page_range() installed mm/vmalloc: undo what a failed __vmap_pages_range_noflush() installed
powerpc: drop redundant unmaps of failed vmap mappings mm/percpu: stop unmapping the CPU that failed to map
arch/powerpc/kernel/isa-bridge.c | 5 +- arch/powerpc/kernel/pci_64.c | 4 +- include/linux/kmsan.h | 27 ++++- mm/kmsan/hooks.c | 42 +++++--- mm/kmsan/shadow.c | 11 +- mm/percpu-vm.c | 5 +- mm/vmalloc.c | 175 ++++++++++++++++++++++++------- mm/vmalloc.h | 9 +- 8 files changed, 214 insertions(+), 64 deletions(-)
Signature · 1 line
2.25.1
PATCH v6 1/10 mm/vmalloc: allow the vmap unmap paths to run in atomic context
The vmap failure paths can be reached with GFP_ATOMIC or GFP_NOWAIT since commit b186a94227b7 ("kmsan: remove hard-coded GFP_KERNEL flags") and commit 8da89ba18ed4 ("mm/vmalloc: handle non-blocking GFP in __vmalloc_area_node()"). An undo of a partially installed mapping has to run in atomic context there, and the cond_resched() in vunmap_pmd_range() cannot schedule.
Give __vunmap_range_noflush() and kmsan_vunmap_range_noflush() a can_resched argument and skip the cond_resched() when it is false. The existing callers are on sleepable paths and pass true, so nothing changes for them.
--- include/linux/kmsan.h | 8 ++++++-- mm/kmsan/hooks.c | 14 +++++++++----- mm/vmalloc.c | 24 ++++++++++++++---------- mm/vmalloc.h | 3 ++- 4 files changed, 31 insertions(+), 18 deletions(-)
include/linux/kmsan.h +6 -2
@@ -152,11 +152,14 @@ int __must_check kmsan_vmap_pages_range_noflush(unsigned long start,* kmsan_vunmap_kernel_range_noflush() - Notify KMSAN about a vunmap. * @start: start of vunmapped range. * @end: end of vunmapped range. +* @can_resched: whether the unmap walk may call cond_resched(); pass +* false from atomic context. * * KMSAN unmaps the contiguous metadata ranges created by * kmsan_map_kernel_range_noflush(). */ -void kmsan_vunmap_range_noflush (unsigned long start,unsigned long end); +void kmsan_vunmap_range_noflush (unsigned long start,unsigned long end, +bool can_resched);/** * kmsan_ioremap_page_range() - Notify KMSAN about a ioremap_page_range() call.
@@ -357,7 +360,8 @@ static inline int __must_check kmsan_vmap_pages_range_noflush( }static inline void kmsan_vunmap_range_noflush (unsigned long start, -unsigned long end) +unsigned long end, +bool can_resched) { }
mm/kmsan/hooks.c +9 -5
@@ -136,10 +136,13 @@ static unsigned long vmalloc_origin(unsigned long addr) KMSAN_META_ORIGIN); } -void kmsan_vunmap_range_noflush (unsigned long start,unsigned long end) +void kmsan_vunmap_range_noflush (unsigned long start,unsigned long end, +bool can_resched) { -__vunmap_range_noflush (vmalloc_shadow (start),vmalloc_shadow (end)); -__vunmap_range_noflush (vmalloc_origin (start),vmalloc_origin (end)); +__vunmap_range_noflush (vmalloc_shadow (start),vmalloc_shadow (end), + can_resched); +__vunmap_range_noflush (vmalloc_origin (start),vmalloc_origin (end), + can_resched);flush_cache_vmap (vmalloc_shadow (start),vmalloc_shadow (end));flush_cache_vmap (vmalloc_origin (start),vmalloc_origin (end)); }
@@ -168,8 +171,9 @@ static void kmsan_iounmap_pages(unsigned long start, unsigned long end)if (origin)__free_pages (origin,KMSAN_IOREMAP_META_ORDER ); } -__vunmap_range_noflush (shadow_start, shadow_end); -__vunmap_range_noflush (origin_start, origin_end); +/* ioremap metadata mappings are always GFP_KERNEL, so resched is safe. */ +__vunmap_range_noflush (shadow_start, shadow_end, true); +__vunmap_range_noflush (origin_start, origin_end, true);flush_tlb_kernel_range (shadow_start, shadow_end);flush_tlb_kernel_range (origin_start, origin_end); }
mm/vmalloc.c +14 -10
@@ -429,7 +429,7 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, }static void vunmap_pmd_range (pud_t *pud,unsigned long addr,unsigned long end, -pgtbl_mod_mask *mask) +pgtbl_mod_mask *mask,bool can_resched) {pmd_t *pmd;unsigned long next;
@@ -451,12 +451,14 @@ static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,continue ;vunmap_pte_range (pmd, addr, next, mask); -cond_resched (); +/* The undo path may run in atomic context. */ +if (can_resched) +cond_resched (); }while (pmd++, addr = next, addr != end); }static void vunmap_pud_range (p4d_t *p4d,unsigned long addr,unsigned long end, -pgtbl_mod_mask *mask) +pgtbl_mod_mask *mask,bool can_resched) {pud_t *pud;unsigned long next;
@@ -476,12 +478,12 @@ static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, }if (pud_none_or_clear_bad (pud))continue ; -vunmap_pmd_range (pud, addr, next, mask); +vunmap_pmd_range (pud, addr, next, mask, can_resched); }while (pud++, addr = next, addr != end); }static void vunmap_p4d_range (pgd_t *pgd,unsigned long addr,unsigned long end, -pgtbl_mod_mask *mask) +pgtbl_mod_mask *mask,bool can_resched) {p4d_t *p4d;unsigned long next;
@@ -496,7 +498,7 @@ static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,if (p4d_none_or_clear_bad (p4d))continue ; -vunmap_pud_range (p4d, addr, next, mask); +vunmap_pud_range (p4d, addr, next, mask, can_resched); }while (p4d++, addr = next, addr != end); }
@@ -512,7 +514,8 @@ static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,* * This is an internal function only. Do not use outside mm/. */ -void __vunmap_range_noflush (unsigned long start,unsigned long end) +void __vunmap_range_noflush (unsigned long start,unsigned long end, +bool can_resched) {unsigned long next;pgd_t *pgd;
@@ -527,7 +530,7 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end) mask |=PGTBL_PGD_MODIFIED ;if (pgd_none_or_clear_bad (pgd))continue ; -vunmap_p4d_range (pgd, addr, next, &mask); +vunmap_p4d_range (pgd, addr, next, &mask, can_resched); }while (pgd++, addr = next, addr != end);if (mask &ARCH_PAGE_TABLE_SYNC_MASK )
@@ -536,8 +539,9 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end)void vunmap_range_noflush (unsigned long start,unsigned long end) { -kmsan_vunmap_range_noflush (start, end); -__vunmap_range_noflush (start, end); +/* The unmap paths are sleepable, keep the resched point. */ +kmsan_vunmap_range_noflush (start, end, true); +__vunmap_range_noflush (start, end, true); }/**
mm/vmalloc.h +2 -1
@@ -44,6 +44,7 @@ int __must_check __vmap_pages_range_noflush(unsigned long addr,void vunmap_range_noflush (unsigned long start,unsigned long end); -void __vunmap_range_noflush (unsigned long start,unsigned long end); +void __vunmap_range_noflush (unsigned long start,unsigned long end, +bool can_resched);#endif /* __MM_VMALLOC_H */
Signature · 1 line
2.25.1
PATCH v6 2/10 mm/kmsan: undo the shadow mapping when the origin mapping fails
In reply to Hao Gekmsan_vmap_pages_range_noflush() first maps the shadow pages and then the origin pages. If the second mapping fails, the first one is left mapped, and the callers do not roll it back. The next vmap of the same metadata range hits the stale PTEs again and fails with a WARN_ON() and -EBUSY in vmap_pages_pte_range().
Undo the shadow mapping on that error path and flush the cache and the TLB for the shadow range. The mapping may still be cached even though nothing accessed it, and Documentation/core-api/cachetlb.rst requires the cache flush to happen before the page table change and the TLB flush after it.
--- mm/kmsan/shadow.c | 7 +++++++ 1 file changed, 7 insertions(+)
mm/kmsan/shadow.c +7 -0
@@ -343,6 +343,13 @@ int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, o_pages, page_shift);kmsan_leave_runtime ();if (mapped) { +/* Undo the shadow mapping set up above. */ +flush_cache_vunmap (shadow_start, shadow_end); +kmsan_enter_runtime (); +__vunmap_range_noflush (shadow_start, shadow_end, +gfpflags_allow_blocking (gfp_mask)); +kmsan_leave_runtime (); +flush_tlb_kernel_range (shadow_start, shadow_end); err = mapped; goto ret; }
Signature · 1 line
2.25.1
PATCH v6 3/10 mm/vmalloc: fix a typo in the __vunmap_range_noflush() comment
In reply to Hao GeThe comment asks the caller to call flush_cache_vmap() before calling this function. flush_cache_vunmap() is meant here, the old comment that commit 4ad0ae8c64ac ("mm/vmalloc: remove unmap_kernel_range") replaced said flush_cache_vunmap(), and Documentation/core-api/cachetlb.rst and vunmap_range() both use flush_cache_vunmap() before the page tables are torn down.
--- mm/vmalloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmalloc.c +1 -1
@@ -506,7 +506,7 @@ static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,* vunmap_range_noflush is similar to vunmap_range, but does not * flush caches or TLBs. * -* The caller is responsible for calling flush_cache_vmap() before calling +* The caller is responsible for calling flush_cache_vunmap() before calling * this function, and flush_tlb_kernel_range after it has returned * successfully (and before the addresses are expected to cause a page fault * or be re-mapped for something else, if TLB flushes are being delayed or
Signature · 1 line
2.25.1
PATCH v6 4/10 mm/kmsan: fix the metadata cache flush ordering
In reply to Hao Gekmsan_vunmap_range_noflush(), kmsan_iounmap_page_range() and the error cleanup of kmsan_ioremap_page_range() all call flush_cache_vmap() after the page tables were cleared. Documentation/core-api/cachetlb.rst is explicit about the order.
6) void flush_cache_vmap(unsigned long start, unsigned long end) void flush_cache_vunmap(unsigned long start, unsigned long end)
The first of these two routines is invoked after vmap_range() has installed the page table entries. The second is invoked before vunmap_range() deletes the page table entries.
Switch the teardown paths to flush_cache_vunmap() before clearing the PTEs, and before the metadata pages are freed: a dirty cache line of a freed page may be written back after the page has been handed out to somebody else. The iounmap side does the flush inside kmsan_iounmap_pages(). The successful install path of kmsan_ioremap_page_range() keeps its flush_cache_vmap(), as it runs after the install.
--- mm/kmsan/hooks.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
mm/kmsan/hooks.c +10 -6
@@ -139,12 +139,12 @@ static unsigned long vmalloc_origin(unsigned long addr)void kmsan_vunmap_range_noflush (unsigned long start,unsigned long end,bool can_resched) { +flush_cache_vunmap (vmalloc_shadow (start),vmalloc_shadow (end)); +flush_cache_vunmap (vmalloc_origin (start),vmalloc_origin (end));__vunmap_range_noflush (vmalloc_shadow (start),vmalloc_shadow (end), can_resched);__vunmap_range_noflush (vmalloc_origin (start),vmalloc_origin (end), can_resched); -flush_cache_vmap (vmalloc_shadow (start),vmalloc_shadow (end)); -flush_cache_vmap (vmalloc_origin (start),vmalloc_origin (end)); }#define KMSAN_IOREMAP_META_ORDER 1
@@ -162,6 +162,10 @@ static void kmsan_iounmap_pages(unsigned long start, unsigned long end) nr = (end - start) /PAGE_SIZE ; v_shadow = shadow_start; v_origin = origin_start; + +flush_cache_vunmap (shadow_start, shadow_end); +flush_cache_vunmap (origin_start, origin_end); +for (int i =0 ; i < nr; i++, v_shadow +=PAGE_SIZE , v_origin +=PAGE_SIZE ) { shadow =kmsan_vmalloc_to_page_or_null ((void *)v_shadow);
@@ -171,6 +175,7 @@ static void kmsan_iounmap_pages(unsigned long start, unsigned long end)if (origin)__free_pages (origin,KMSAN_IOREMAP_META_ORDER ); } +/* ioremap metadata mappings are always GFP_KERNEL, so resched is safe. */ __vunmap_range_noflush (shadow_start, shadow_end, true);__vunmap_range_noflush (origin_start, origin_end, true);
@@ -235,9 +240,10 @@ int kmsan_ioremap_page_range(unsigned long start, unsigned long end,if (mapped_end > start)kmsan_iounmap_pages (start, mapped_end); + }else { +flush_cache_vmap (vmalloc_shadow (start),vmalloc_shadow (end)); +flush_cache_vmap (vmalloc_origin (start),vmalloc_origin (end)); } -flush_cache_vmap (vmalloc_shadow (start),vmalloc_shadow (end)); -flush_cache_vmap (vmalloc_origin (start),vmalloc_origin (end));kmsan_leave_runtime ();return err; }
@@ -249,8 +255,6 @@ void kmsan_iounmap_page_range(unsigned long start, unsigned long end)kmsan_enter_runtime ();kmsan_iounmap_pages (start, end); -flush_cache_vmap (vmalloc_shadow (start),vmalloc_shadow (end)); -flush_cache_vmap (vmalloc_origin (start),vmalloc_origin (end));kmsan_leave_runtime (); }
Signature · 1 line
2.25.1
PATCH v6 5/10 mm/vmalloc: reject an unaligned range in vmap_page_range()
In reply to Hao Gecommit a739e6b557af ("mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned") put the check in vmap_pte_range(), which is too late, because the upper levels may already have installed page tables for a range that is rejected anyway.
The pte loop maps whole pages and stops only when addr lands exactly on end. An unaligned end lets addr jump over it and the loop keeps mapping past the range. An unaligned start maps the page below the range and usually misses end too. Such a range is never a legitimate request.
vmap_range_noflush() has a single caller, vmap_page_range(). Warn on addr and end there and bail out with -EINVAL before any page table is installed, and drop the check from vmap_pte_range().
--- mm/vmalloc.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
mm/vmalloc.c +5 -3
@@ -125,9 +125,6 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,unsigned long size;unsigned int nr_pages; -if (WARN_ON_ONCE (!PAGE_ALIGNED (end - addr))) -return -EINVAL ; - pfn = phys_addr >>PAGE_SHIFT ; pte =pte_alloc_kernel_track (pmd, addr, mask);if (!pte)
@@ -369,6 +366,11 @@ int vmap_page_range(unsigned long addr, unsigned long end, {int err; +if (WARN_ON_ONCE (!PAGE_ALIGNED (addr))) +return -EINVAL ; +if (WARN_ON_ONCE (!PAGE_ALIGNED (end))) +return -EINVAL ; + err =vmap_range_noflush (addr, end, phys_addr,pgprot_nx (prot), ioremap_max_page_shift);flush_cache_vmap (addr, end);
Signature · 1 line
2.25.1
PATCH v6 6/10 mm/vmalloc: reject an unaligned range in __vunmap_range_noflush()
In reply to Hao GeThe PTE loop stops only at addr == end. An unaligned end is never reached, so the loop keeps clearing past the range. An unaligned start clears the page below the range first. Both are caller bugs, and the damage is hard to attribute afterwards.
vmap only installs page aligned mappings, so an unaligned unmap request is never a legitimate teardown. Warn and bail out at the entry of __vunmap_range_noflush(), before any PTE is cleared.
The adjacent BUG_ON(addr >= end) is converted to the same WARN_ON_ONCE() + return, since an empty range is a caller bug too.
--- mm/vmalloc.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
mm/vmalloc.c +7 -1
@@ -524,7 +524,13 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end,unsigned long addr = start;pgtbl_mod_mask mask =0 ; -BUG_ON (addr >= end); +if (WARN_ON_ONCE (addr >= end)) +return ; +if (WARN_ON_ONCE (!PAGE_ALIGNED (addr))) +return ; +if (WARN_ON_ONCE (!PAGE_ALIGNED (end))) +return ; + pgd =pgd_offset_k (addr); do { next = pgd_addr_end(addr, end);
Signature · 1 line
2.25.1
PATCH v6 7/10 mm/vmalloc: undo what a failed vmap_page_range() installed
In reply to Hao Gevmap_page_range() maps a physical range for ioremap and arch setup code. The walk may fail halfway, or the KMSAN metadata may fail after the walk, and the PTEs installed so far stay behind. The callers do not agree on who cleans it up. The ioremap implementations free the vm_area, which unmaps the whole range, and powerpc calls vunmap_range() itself. The arch setup code that maps a fixed PCI_IOBASE range either ignores the return value or has no vm_area to free, and leaves the PTEs in place.
vmap_range_noflush() now takes a mapped_end argument that tracks how far the walk got, and the error path undoes [addr, mapped_end), never past the failure point. The PTEs above it were not installed by this call and may be in use by somebody else, clearing them is not up to the undo. If the walk completed and only the KMSAN metadata failed, the whole range was installed by this call, and all of it is undone.
The flush_cache_vmap() also moves after the error check. Flushing a half-installed mapping is pointless, and on UML it is worse, flush_cache_vmap() there is flush_tlb_kernel_range(), which syncs the PTEs into the host OS and would leave host mappings behind for a range that was just freed again.
The undo paths use the new vunmap_range_nokmsan() helper, which flushes the cache, clears the PTEs and flushes the TLB, and leaves the KMSAN metadata to the caller.
All callers are sleepable, the teardown passes can_resched = true.
--- mm/vmalloc.c | 72 ++++++++++++++++++++++++++++++++++++++++++++-------- mm/vmalloc.h | 3 +++ 2 files changed, 64 insertions(+), 11 deletions(-)
mm/vmalloc.c +61 -11
@@ -188,7 +188,8 @@ static int vmap_try_huge_pmd(pmd_t *pmd, unsigned long addr, unsigned long end,static int vmap_pmd_range (pud_t *pud,unsigned long addr,unsigned long end,phys_addr_t phys_addr,pgprot_t prot, -unsigned int max_page_shift,pgtbl_mod_mask *mask) +unsigned int max_page_shift,pgtbl_mod_mask *mask, +unsigned long *mapped_end) {pmd_t *pmd;unsigned long next;
@@ -203,12 +204,15 @@ static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,if (vmap_try_huge_pmd (pmd, addr, next, phys_addr, prot, max_page_shift)) { *mask |=PGTBL_PMD_MODIFIED ; + *mapped_end = next;continue ; } - err =vmap_pte_range (pmd, addr, next, phys_addr, prot, max_page_shift, mask); + err =vmap_pte_range (pmd, addr, next, phys_addr, prot, + max_page_shift, mask);if (err)break ; + *mapped_end = next; }while (pmd++, phys_addr += (next - addr), addr = next, addr != end);return err; }
@@ -245,7 +249,8 @@ static int vmap_try_huge_pud(pud_t *pud, unsigned long addr, unsigned long end,static int vmap_pud_range (p4d_t *p4d,unsigned long addr,unsigned long end,phys_addr_t phys_addr,pgprot_t prot, -unsigned int max_page_shift,pgtbl_mod_mask *mask) +unsigned int max_page_shift,pgtbl_mod_mask *mask, +unsigned long *mapped_end) {pud_t *pud;unsigned long next;
@@ -260,10 +265,12 @@ static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,if (vmap_try_huge_pud (pud, addr, next, phys_addr, prot, max_page_shift)) { *mask |=PGTBL_PUD_MODIFIED ; + *mapped_end = next;continue ; } - err =vmap_pmd_range (pud, addr, next, phys_addr, prot, max_page_shift, mask); + err =vmap_pmd_range (pud, addr, next, phys_addr, prot, + max_page_shift, mask, mapped_end);if (err)break ; }while (pud++, phys_addr += (next - addr), addr = next, addr != end);
@@ -302,7 +309,8 @@ static int vmap_try_huge_p4d(p4d_t *p4d, unsigned long addr, unsigned long end,static int vmap_p4d_range (pgd_t *pgd,unsigned long addr,unsigned long end,phys_addr_t phys_addr,pgprot_t prot, -unsigned int max_page_shift,pgtbl_mod_mask *mask) +unsigned int max_page_shift,pgtbl_mod_mask *mask, +unsigned long *mapped_end) {p4d_t *p4d;unsigned long next;
@@ -317,10 +325,12 @@ static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,if (vmap_try_huge_p4d (p4d, addr, next, phys_addr, prot, max_page_shift)) { *mask |=PGTBL_P4D_MODIFIED ; + *mapped_end = next;continue ; } - err =vmap_pud_range (p4d, addr, next, phys_addr, prot, max_page_shift, mask); + err =vmap_pud_range (p4d, addr, next, phys_addr, prot, + max_page_shift, mask, mapped_end);if (err)break ; }while (p4d++, phys_addr += (next - addr), addr = next, addr != end);
@@ -329,7 +339,7 @@ static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,static int vmap_range_noflush (unsigned long addr,unsigned long end,phys_addr_t phys_addr,pgprot_t prot, -unsigned int max_page_shift) +unsigned int max_page_shift,unsigned long *mapped_end) {pgd_t *pgd;unsigned long start;
@@ -346,11 +356,12 @@ static int vmap_range_noflush(unsigned long addr, unsigned long end,BUG_ON (addr >= end); start = addr; + *mapped_end = addr; pgd =pgd_offset_k (addr);do { next =pgd_addr_end (addr, end); err =vmap_p4d_range (pgd, addr, next, phys_addr, prot, - max_page_shift, &mask); + max_page_shift, &mask, mapped_end);if (err)break ; }while (pgd++, phys_addr += (next - addr), addr = next, addr != end);
@@ -364,6 +375,7 @@ static int vmap_range_noflush(unsigned long addr, unsigned long end,int vmap_page_range (unsigned long addr,unsigned long end,phys_addr_t phys_addr,pgprot_t prot) { +unsigned long mapped_end = addr;int err;if (WARN_ON_ONCE (!PAGE_ALIGNED (addr)))
@@ -372,11 +384,26 @@ int vmap_page_range(unsigned long addr, unsigned long end,return -EINVAL ; err =vmap_range_noflush (addr, end, phys_addr,pgprot_nx (prot), - ioremap_max_page_shift); -flush_cache_vmap (addr, end); -if (!err) + ioremap_max_page_shift, &mapped_end); +if (err) { +/* Undo the part the walk installed before it failed. */ +if (mapped_end > addr) +vunmap_range_nokmsan (addr, mapped_end, true); + }else { err =kmsan_ioremap_page_range (addr, end, phys_addr, prot, ioremap_max_page_shift); +if (err) { +/* +* Only the KMSAN metadata failed, the walk mapped +* the whole range. Undo it all. The callers are +* ioremap and arch setup code, always sleepable. +*/ +vunmap_range_nokmsan (addr, end, true); + }else { +flush_cache_vmap (addr, end); + } + } +return err; }
@@ -552,6 +579,29 @@ void vunmap_range_noflush(unsigned long start, unsigned long end)__vunmap_range_noflush (start, end, true); } +/** +* vunmap_range_nokmsan - unmap kernel virtual addresses without the +* KMSAN part +* @addr: start of the VM area to unmap +* @end: end of the VM area to unmap (non-inclusive) +* @can_resched: whether the unmap walk may call cond_resched(); pass +* false from atomic context. +* +* Clears any present PTEs in the virtual address range, flushes TLBs and +* caches, but leaves the KMSAN metadata to the caller. The undo paths +* use this for a range that is not going back to vmalloc, which would +* flush the TLB lazily, so nobody else flushes it. +* +* This is an internal function only. Do not use outside mm/. +*/ +void vunmap_range_nokmsan (unsigned long addr,unsigned long end, +bool can_resched) +{ +flush_cache_vunmap (addr, end); +__vunmap_range_noflush (addr, end, can_resched); +flush_tlb_kernel_range (addr, end); +} +/** * vunmap_range - unmap kernel virtual addresses * @addr: start of the VM area to unmap
mm/vmalloc.h +3 -0
@@ -47,4 +47,7 @@ void vunmap_range_noflush(unsigned long start, unsigned long end);void __vunmap_range_noflush (unsigned long start,unsigned long end,bool can_resched); +void vunmap_range_nokmsan (unsigned long start,unsigned long end, +bool can_resched); +#endif /* __MM_VMALLOC_H */
Signature · 1 line
2.25.1
PATCH v6 8/10 mm/vmalloc: undo what a failed __vmap_pages_range_noflush() installed
In reply to Hao GeA page array is mapped into a contiguous virtual range by __vmap_pages_range_noflush(), which serves vmap_pages_range() and the vmalloc paths, and by vmap_pages_range_batched(), which serves vmap(). The walk may fail halfway, and the PTEs installed so far stay behind.
The callers do not agree on who cleans it up. pcpu_map_pages() and kmsan_ioremap_page_range() roll back what they mapped, while vm_module_tags_populate() and the __GFP_NOFAIL retry loop in __vmalloc_area_node() expect a clean range from the mapping function. For them the leftovers stay mapped, and the next attempt to map the same range trips the WARN_ON(!pte_none()) in vmap_pages_pte_range().
Both __vmap_pages_range_noflush() and vmap_pages_range_batched() now undo what they installed, up to the point where the walk failed. vmap_pages_range_noflush_walk() reports how many pages it mapped through its index into the pages array, and the batched path uses the end of the last installed batch.
Going further would clear PTEs that are not ours. vmap_pages_pte_range() fails with -EBUSY instead of installing over a PTE that is already set, so a PTE below the failure point can only be ours, and a PTE above it belongs to somebody else, for example when the caller's range overlaps an existing mapping.
The flush_cache_vmap() now only runs when the mapping succeeded, as flushing a half-installed mapping is pointless. The undo flushes the cache and the TLB itself with vunmap_range_nokmsan(), which leaves the KMSAN metadata alone.
The KMSAN metadata is set up for the whole range before the mapping starts, so the undo tears it down as a whole and flushes its TLB right away, since the undo paths do not go through the lazy purge and vmap() may retry the range right after. The undo may run with a non-blocking gfp mask, so it passes gfpflags_allow_blocking() as can_resched.
The undo calls a new kmsan_vunmap_range(), which tears the metadata down and flushes its TLB in one go.
--- include/linux/kmsan.h | 19 ++++++++++++++ mm/kmsan/hooks.c | 12 +++++++-- mm/kmsan/shadow.c | 4 +-- mm/vmalloc.c | 61 +++++++++++++++++++++++++++++++++++-------- mm/vmalloc.h | 3 ++- 5 files changed, 83 insertions(+), 16 deletions(-)
include/linux/kmsan.h +19 -0
@@ -161,6 +161,19 @@ int __must_check kmsan_vmap_pages_range_noflush(unsigned long start,void kmsan_vunmap_range_noflush (unsigned long start,unsigned long end,bool can_resched); +/** +* kmsan_vunmap_range() - Notify KMSAN about a vunmap. +* @start: start of vunmapped range. +* @end: end of vunmapped range. +* @can_resched: whether the unmap walk may call cond_resched(); pass +* false from atomic context. +* +* KMSAN clears any present PTEs of the shadow and the origin of the range, +* flushes TLBs and caches. +*/ +void kmsan_vunmap_range (unsigned long start,unsigned long end, +bool can_resched); +/** * kmsan_ioremap_page_range() - Notify KMSAN about a ioremap_page_range() call. * @addr: range start.
@@ -365,6 +378,12 @@ static inline void kmsan_vunmap_range_noflush(unsigned long start, { } +static inline void kmsan_vunmap_range (unsigned long start, +unsigned long end, +bool can_resched) +{ +} + static inline int __must_check kmsan_ioremap_page_range(unsigned long start, unsigned long end, phys_addr_t phys_addr,
mm/kmsan/hooks.c +10 -2
@@ -147,6 +147,14 @@ void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end, can_resched); } +void kmsan_vunmap_range (unsigned long start,unsigned long end, +bool can_resched) +{ +kmsan_vunmap_range_noflush (start, end, can_resched); +flush_tlb_kernel_range (vmalloc_shadow (start),vmalloc_shadow (end)); +flush_tlb_kernel_range (vmalloc_origin (start),vmalloc_origin (end)); +} +#define KMSAN_IOREMAP_META_ORDER 1static void kmsan_iounmap_pages (unsigned long start,unsigned long end)
@@ -213,7 +221,7 @@ int kmsan_ioremap_page_range(unsigned long start, unsigned long end, mapped =__vmap_pages_range_noflush (vmalloc_shadow (start + off),vmalloc_shadow (start + off +PAGE_SIZE ), prot, &shadow, -PAGE_SHIFT ); +PAGE_SHIFT , gfp_mask);if (mapped) { err = mapped; goto ret;
@@ -223,7 +231,7 @@ int kmsan_ioremap_page_range(unsigned long start, unsigned long end, mapped =__vmap_pages_range_noflush (vmalloc_origin (start + off),vmalloc_origin (start + off +PAGE_SIZE ), prot, &origin, -PAGE_SHIFT ); +PAGE_SHIFT , gfp_mask);if (mapped) { err = mapped; goto ret;
mm/kmsan/shadow.c +2 -2
@@ -332,7 +332,7 @@ int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, origin_end =vmalloc_meta ((void *)end,KMSAN_META_ORIGIN );kmsan_enter_runtime (); mapped =__vmap_pages_range_noflush (shadow_start, shadow_end, prot, - s_pages, page_shift); + s_pages, page_shift, gfp_mask);kmsan_leave_runtime ();if (mapped) { err = mapped;
@@ -340,7 +340,7 @@ int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, }kmsan_enter_runtime (); mapped =__vmap_pages_range_noflush (origin_start, origin_end, prot, - o_pages, page_shift); + o_pages, page_shift, gfp_mask);kmsan_leave_runtime ();if (mapped) {/* Undo the shadow mapping set up above. */
mm/vmalloc.c +50 -11
128 lines not on this page · raw message
mm/vmalloc.h +2 -1
@@ -40,7 +40,8 @@ void clear_vm_uninitialized_flag(struct vm_struct *vm);int __must_check __vmap_pages_range_noflush (unsigned long addr,unsigned long end,pgprot_t prot, -struct page **pages,unsigned int page_shift); +struct page **pages,unsigned int page_shift, +gfp_t gfp_mask);void vunmap_range_noflush (unsigned long start,unsigned long end);
Signature · 1 line
2.25.1
PATCH v6 9/10 powerpc: drop redundant unmaps of failed vmap mappings
In reply to Hao Gevmap_page_range() and ioremap_page_range() clear the PTEs they installed when they fail, so the vunmap_range() in remap_isa_base() and ioremap_phb() has nothing left to remove.
--- arch/powerpc/kernel/isa-bridge.c | 5 ++--- arch/powerpc/kernel/pci_64.c | 4 +--- 2 files changed, 3 insertions(+), 6 deletions(-)
arch/powerpc/kernel/isa-bridge.c +2 -3
@@ -46,9 +46,8 @@ static void remap_isa_base(phys_addr_t pa, unsigned long size)WARN_ON_ONCE (size & ~PAGE_MASK );if (slab_is_available ()) { -if (vmap_page_range (ISA_IO_BASE ,ISA_IO_BASE + size, pa, -pgprot_noncached (PAGE_KERNEL ))) -vunmap_range (ISA_IO_BASE ,ISA_IO_BASE + size); +vmap_page_range (ISA_IO_BASE ,ISA_IO_BASE + size, pa, +pgprot_noncached (PAGE_KERNEL )); }else {early_ioremap_range (ISA_IO_BASE , pa, size,pgprot_noncached (PAGE_KERNEL ));
arch/powerpc/kernel/pci_64.c +1 -3
@@ -139,10 +139,8 @@ void __iomem *ioremap_phb(phys_addr_t paddr, unsigned long size) addr = (unsigned long )area->addr;if (ioremap_page_range (addr, addr + size, paddr, -pgprot_noncached (PAGE_KERNEL ))) { -vunmap_range (addr, addr + size); +pgprot_noncached (PAGE_KERNEL )))return NULL ; - }return (void __iomem *)addr; }
Signature · 1 line
2.25.1
PATCH v6 10/10 mm/percpu: stop unmapping the CPU that failed to map
In reply to Hao Ge__pcpu_map_pages() clears the PTEs it installed when it fails, so pcpu_map_pages() only has to tear down the CPUs it mapped before the failing one.
--- mm/percpu-vm.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
mm/percpu-vm.c +3 -2
@@ -252,10 +252,11 @@ static int pcpu_map_pages(struct pcpu_chunk *chunk, struct page **pages,return 0 ; err:for_each_possible_cpu (tcpu) { -__pcpu_unmap_pages (pcpu_chunk_addr (chunk, tcpu, page_start), - page_end - page_start); +/* The failing CPU's partial mapping undoes itself */ if (tcpu == cpu)break ; +__pcpu_unmap_pages (pcpu_chunk_addr (chunk, tcpu, page_start), + page_end - page_start); }pcpu_post_unmap_tlb_flush (chunk, page_start, page_end);return err;
Signature · 1 line
2.25.1
[email protected]
AI reviewRe: [PATCH v6 01/10] mm/vmalloc: allow the vmap unmap paths to run in atomic context
In reply to Hao GeThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] mm/vmalloc: missing Cc: stable tag breaks stable backport of patch 2 --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mm/vmalloc: missing Cc: stable tag breaks stable backport of patch 2
--
commit 44c3c42d46b29f727590eb43e59883360aed2f9b
Author: Hao Ge <[email protected]>
Quoted 9 lines
mm/vmalloc: allow the vmap unmap paths to run in atomic context
The vmap failure paths can be reached with
GFP_ATOMICorGFP_NOWAITsince commitb186a94227b7("kmsan: remove hard-codedGFP_KERNELflags") and commit8da89ba18ed4("mm/vmalloc: handle non-blocking GFP in__vmalloc_area_node()"). An undo of a partially installed mapping has to run in atomic context there, and thecond_resched()invunmap_pmd_range()cannot schedule.
Quoted 8 lines
Give
__vunmap_range_noflush()andkmsan_vunmap_range_noflush()acan_reschedargument and skip thecond_resched()when it is false. The existing callers are on sleepable paths and pass true, so nothing changes for them.Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>
[Severity: Low]
Should this commit also carry Cc: [email protected]?
This patch adds the can_resched parameter to __vunmap_range_noflush(), which commit "mm/kmsan: undo the shadow mapping when the origin mapping fails" later in this series calls while carrying Cc: [email protected] without listing this patch as a prerequisite.
Cherry-picking that fix to stable trees without this commit will fail to compile against the two-argument signature, and adapting it to the two-argument version would call cond_resched() in atomic context.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v6 05/10] mm/vmalloc: reject an unaligned range in vmap_page_range()
In reply to Hao GeThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm: vmap_page_range() alignment check breaks omap_cf_probe() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm: vmap_page_range() alignment check breaks omap_cf_probe() --
commit 875f66737f08161fc6c689e08e79f67012524a9b
Author: Hao Ge <[email protected]>
mm/vmalloc: reject an unaligned range in vmap_page_range()
This commit moves the PAGE_SIZE alignment check from vmap_pte_range() to vmap_page_range(), checking both addr and end before installing any page tables.
Quoted 4 lines
mm/vmalloc.c +0
-0
[ ... ]
mm/vmalloc.c int vmap_page_range(unsigned long addr, unsigned long end, · line 376
Quoted 12 lines
mm/vmalloc.c +5
-0@@ -369,6 +366,11 @@ int vmap_page_range(unsigned long addr, unsigned long end, {int err; +if (WARN_ON_ONCE (!PAGE_ALIGNED (addr))) +return -EINVAL ; +if (WARN_ON_ONCE (!PAGE_ALIGNED (end))) +return -EINVAL ; + err =vmap_range_noflush (addr, end, phys_addr,pgprot_nx (prot), ioremap_max_page_shift);flush_cache_vmap (addr, end);
[Severity: Medium]
Does this alignment check break omap_cf_probe() on ARM?
In drivers/pcmcia/omap_cf.c, omap_cf_probe() declares an I/O resource with DEFINE_RES_IO(SZ_64, SZ_4K) and passes it to pci_remap_iospace(). On ARM, arch/arm/mm/ioremap.c:pci_remap_iospace() adds res->start to PCI_IOBASE and forwards the unaligned vaddr and end directly to vmap_page_range().
Previously vmap_pte_range() only checked PAGE_ALIGNED(end - addr), which passed because the size is SZ_4K. With the new checks on addr and end, vmap_page_range() warns and returns -EINVAL, causing omap_cf_probe() to fail with -ENOMEM and breaking CompactFlash initialization on OMAP16xx boards.
drivers/pcmcia/omap_cf.c should be updated to use a PAGE_SIZE-aligned I/O resource start before or alongside enforcing this check.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH v6 07/10] mm/vmalloc: undo what a failed vmap_page_range() installed
In reply to Hao GeThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm: vunmap_range_nokmsan() sends SMP IPIs despite atomic-context contract --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm: vunmap_range_nokmsan() sends SMP IPIs despite atomic-context contract
--
commit a6c5a3931affbc3848f78bee8cb75dc56e46f428
Author: Hao Ge <[email protected]>
mm/vmalloc: undo what a failed vmap_page_range() installed
This commit tracks how far vmap_range_noflush() walked and introduces vunmap_range_nokmsan() so vmap_page_range() can unmap partially installed PTEs on walk or KMSAN failures.
Quoted 4 lines
mm/vmalloc.c +0
-0
[ ... ]
Quoted 16 lines
mm/vmalloc.c +12
-0@@ -552,6 +579,29 @@ void vunmap_range_noflush(unsigned long start, unsigned long end)__vunmap_range_noflush (start, end, true); } +/** +* vunmap_range_nokmsan - unmap kernel virtual addresses without the +* KMSAN part +* @addr: start of the VM area to unmap +* @end: end of the VM area to unmap (non-inclusive) +* @can_resched: whether the unmap walk may call cond_resched(); pass +* false from atomic context. +* +* Clears any present PTEs in the virtual address range, flushes TLBs and +* caches, but leaves the KMSAN metadata to the caller. The undo paths +* use this for a range that is not going back to vmalloc, which would +* flush the TLB lazily, so nobody else flushes it.
mm/vmalloc.c void vunmap_range_noflush(unsigned long start, unsigned long end) · line 603
Quoted 10 lines
mm/vmalloc.c +10
-0+* +* This is an internal function only. Do not use outside mm/. +*/ +void vunmap_range_nokmsan (unsigned long addr,unsigned long end, +bool can_resched) +{ +flush_cache_vunmap (addr, end); +__vunmap_range_noflush (addr, end, can_resched); +flush_tlb_kernel_range (addr, end); +}
[Severity: Medium]
Is vunmap_range_nokmsan() actually safe to call from atomic context when can_resched is false?
flush_tlb_kernel_range() (and flush_cache_vunmap() on PA-RISC) sends synchronous IPIs via smp_call_function_many_cond() when there is no hardware broadcast TLB invalidation. That asserts IRQs are enabled and in_task(), and can deadlock if two CPUs wait with IRQs disabled.
This commit only passes can_resched = true, but commit "mm/vmalloc: undo what a failed __vmap_pages_range_noflush() installed" calls vunmap_range_nokmsan(..., false) on GFP_ATOMIC vmalloc failures.
__get_vm_area_node() only rejects NMI, hardirq and softirq contexts, so an IRQ-disabled task (e.g. a kprobe BPF program in rhtab_map_update_elem() -> rhashtable_insert_rehash() falling back to vmalloc) can reach this on a mid-walk page-table allocation failure.
Either defer cleanup for non-blocking contexts instead of issuing synchronous IPI flushes, or reject IRQ-disabled contexts and document that IRQs must be enabled.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
[email protected]
AI reviewRe: [PATCH v6 03/10] mm/vmalloc: fix a typo in the __vunmap_range_noflush() comment
In reply to Hao GeThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] mm/vmalloc: drop Cc: stable tag from comment-only typo fix --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mm/vmalloc: drop Cc: stable tag from comment-only typo fix --
commit 7d18d3e5f3f5716159820cfe868691463b593fe8
Author: Hao Ge <[email protected]>
Quoted 9 lines
mm/vmalloc: fix a typo in the
__vunmap_range_noflush()commentThe comment asks the caller to call
flush_cache_vmap()before calling this function.flush_cache_vunmap()is meant here, the old comment that commit4ad0ae8c64ac("mm/vmalloc: removeunmap_kernel_range") replaced saidflush_cache_vunmap(), andDocumentation/core-api/cachetlb.rstandvunmap_range()both useflush_cache_vunmap()before the page tables are torn down.
Quoted 3 lines
Fixes 4ad0ae8c64ac ("mm/vmalloc: remove unmap_kernel_range")Signed-off-by Hao Ge <[email protected]>
[Severity: Low]
Should the Cc: [email protected] tag be dropped? This commit only fixes a typo in a comment above __vunmap_range_noflush() with no functional change, and Documentation/process/stable-kernel-rules.rst excludes trivial spelling and comment fixes from stable trees.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3