mm/vmalloc: fix up vmap error handling
Subsystems (6)
v5 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. Patches 3 and 4 reject an unaligned range at the entries of vmap_page_range() and __vunmap_range_noflush(). Patch 5 only flushes the cache once the mapping succeeded. Patches 6 to 9 add the rollback to the mapping functions, one entry per patch, vmap_page_range(), __vmap_pages_range_noflush(), the KMSAN metadata and the batched vmap() path. Patches 10 and 11 drop the powerpc and percpu cleanups that are not needed anymore.
Changes since v4:
- new patch 1, the unmap paths can run under a non-blocking gfp mask and must skip cond_resched(); it carries a Fixes: tag so it travels to stable together with patch 2
- drop v4 patch 2, the KMSAN-disabled vunmap skip. !kmsan_enabled does not mean no metadata was installed, KMSAN_WARN_ON() can clear the flag at runtime, on Suren's review
- v4 patch 3 now warns and returns -EINVAL at vmap_page_range(), instead of BUG_ON()ing inside vmap_range_noflush()
- drop v4 patch 4, the walk check. No in-tree caller passes an unaligned range to vmap_pages_range_noflush_walk(), the vmap allocator guarantees the alignment for vmalloc()/vmap()/ vm_map_ram()
- v4 patch 5 warns and bails out instead of BUG_ON(), following Ulad's review of v4
- v4 patch 7 is split into patches 6 to 9, one mapping entry per patch. The batched vmap() path is covered now too, on Suren's request
[1] 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 (11):
mm/vmalloc: skip cond_resched() in the vmap unmap paths when atomic mm/kmsan: undo the shadow mapping when the origin mapping fails mm/vmalloc: reject an unaligned range in vmap_page_range() mm/vmalloc: reject an unaligned range in __vunmap_range_noflush() mm/vmalloc: only flush the cache after the vmap succeeded mm/vmalloc: undo what vmap_page_range() mapped on failure mm/vmalloc: undo what __vmap_pages_range_noflush() mapped on failure mm/vmalloc: drop the KMSAN metadata in vmap_pages_range_noflush() on
failure
mm/vmalloc: undo what vmap_pages_range_batched() mapped on failure
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 | 8 +- mm/kmsan/hooks.c | 18 ++-- mm/kmsan/shadow.c | 9 +- mm/percpu-vm.c | 5 +- mm/vmalloc.c | 154 +++++++++++++++++++++++-------- mm/vmalloc.h | 6 +- 8 files changed, 151 insertions(+), 58 deletions(-)
Signature · 1 line
2.25.1
PATCH v5 1/11 mm/vmalloc: skip cond_resched() in the vmap unmap paths when atomic
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()"), so an undo of a partially installed mapping may have to run in atomic context, where 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
@@ -150,11 +150,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.
@@ -355,7 +358,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
Uladzislau Rezki
maintainerRe: [PATCH v5 01/11] mm/vmalloc: skip cond_resched() in the vmap unmap paths when atomic
Quoted 188 lines · Hao Ge
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()"), so an undo of a partially installed mapping may have to run in atomic context, where thecond_resched()invunmap_pmd_range()cannot schedule.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.Fixes 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- 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@@ -150,11 +150,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. @@ -355,7 +358,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
This looks good to me. I am not sure if passing gfp_mask is better here since it is only for checking if-allow-blocking.
--
Uladzislau Rezki
Re: [PATCH v5 01/11] mm/vmalloc: skip cond_resched() in the vmap unmap paths when atomic
In reply to Hao GeQuoted 4 lines · Hao Ge
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()"), so an undo of a partially installed
You mention this commit too, implying that vunmap_range_noflush() can be called in atomic context right now, but in this patch in vunmap_range_noflush() you pass 'true' unconditionally to __vunmap_range_noflush()?
Can you give me the call chain existing in current kernel which makes this bug trigger? In case it does not exist but is only triggered when you add the rollback code later, then this patch is not fixing a bug.
Quoted 181 lines
mapping may have to run in atomic context, where the
cond_resched()invunmap_pmd_range()cannot schedule.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.Fixes 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- 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@@ -150,11 +150,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. @@ -355,7 +358,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 */
Re: [PATCH v5 01/11] mm/vmalloc: skip cond_resched() in the vmap unmap paths when atomic
Quoted 9 lines · Dev Jain
Quoted 4 lines · Hao Ge
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()"), so an undo of a partially installed
Quoted 8 lines
You mention this commit too, implying that
vunmap_range_noflush()can be called in atomic context right now, but in this patch invunmap_range_noflush()you pass 'true' unconditionally to__vunmap_range_noflush()?Can you give me the call chain existing in current kernel which makes this bug trigger? In case it does not exist but is only triggered when you add the rollback code later, then this patch is not fixing a bug.
Yeah, IIUC this is a prep patch for later changes and does not fix any existing issues, therefore should not have any Fixes tags and should not be CC'ed to stable.
Other than that LGTM.
Quoted 184 lines
Quoted 183 lines
mapping may have to run in atomic context, where the
cond_resched()invunmap_pmd_range()cannot schedule.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.Fixes 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- 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@@ -150,11 +150,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. @@ -355,7 +358,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 */
Re: [PATCH v5 01/11] mm/vmalloc: skip cond_resched() in the vmap unmap paths when atomic
Hi Suren and Dev
Thanks for the review.
Quoted 19 lines · Suren Baghdasaryan
Quoted 9 lines · Dev Jain
Quoted 4 lines · Hao Ge
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()"), so an undo of a partially installedQuoted 8 lines
You mention this commit too, implying that
vunmap_range_noflush()can be called in atomic context right now, but in this patch invunmap_range_noflush()you pass 'true' unconditionally to__vunmap_range_noflush()?Can you give me the call chain existing in current kernel which makes this bug trigger? In case it does not exist but is only triggered when you add the rollback code later, then this patch is not fixing a bug.
Quoted 5 lines
Yeah, IIUC this is a prep patch for later changes and does not fix any existing issues, therefore should not have any Fixes tags and should not be CC'ed to stable.
Other than that LGTM.
Right, this is a prep patch for later changes. I will drop the Fixes tag, this patch fixes nothing by itself.
I would keep the stable Cc though. The next patch, "mm/kmsan: undo the shadow mapping when the origin mapping fails", is the real fix and it depends on this one. Per stable-kernel-rules.rst, patch1 in a series should be marked for stable inclusion when patch2 depends on it, otherwise the fix lands in stable without its prerequisite.
Or did I misread the rules?
Thanks
Best Regards
Hao
Quoted 185 lines
Quoted 185 lines
Quoted 184 lines
mapping may have to run in atomic context, where the
cond_resched()invunmap_pmd_range()cannot schedule.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.Fixes 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- 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@@ -150,11 +150,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. @@ -355,7 +358,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 */
Re: [PATCH v5 01/11] mm/vmalloc: skip cond_resched() in the vmap unmap paths when atomic
Quoted 31 lines · Hao Ge
Hi Suren and Dev
Thanks for the review.
Quoted 19 lines · Suren Baghdasaryan
Quoted 9 lines · Dev Jain
Quoted 4 lines · Hao Ge
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()"), so an undo of a partially installedQuoted 8 lines
You mention this commit too, implying that
vunmap_range_noflush()can be called in atomic context right now, but in this patch invunmap_range_noflush()you pass 'true' unconditionally to__vunmap_range_noflush()?Can you give me the call chain existing in current kernel which makes this bug trigger? In case it does not exist but is only triggered when you add the rollback code later, then this patch is not fixing a bug.
Quoted 5 lines
Yeah, IIUC this is a prep patch for later changes and does not fix any existing issues, therefore should not have any Fixes tags and should not be CC'ed to stable.
Other than that LGTM.
Quoted 8 lines
Right, this is a prep patch for later changes. I will drop the Fixes tag, this patch fixes nothing by itself.
I would keep the stable Cc though. The next patch, "mm/kmsan: undo the shadow mapping when the origin mapping fails", is the real fix and it depends on this one. Per
stable-kernel-rules.rst, patch1 in a series should be marked for stable inclusion when patch2 depends on it, otherwise the fix lands in stable without its prerequisite.
It might be worth spelling this out in the changelog to avoid confusing Greg and Sasha.
Quoted 192 lines
Or did I misread the rules?
Thanks
Best Regards
HaoQuoted 185 lines
Quoted 185 lines
Quoted 184 lines
mapping may have to run in atomic context, where the
cond_resched()invunmap_pmd_range()cannot schedule.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.Fixes 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- 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@@ -150,11 +150,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. @@ -355,7 +358,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 */
PATCH v5 2/11 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, the same way kmsan_ioremap_page_range() cleans up after a partial failure. __vunmap_range_noflush() only clears the PTEs; nothing has touched the shadow mapping, so no TLB flush is needed.
--- mm/kmsan/shadow.c | 5 +++++ 1 file changed, 5 insertions(+)
mm/kmsan/shadow.c +5 -0
@@ -258,6 +258,11 @@ 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. */ +kmsan_enter_runtime (); +__vunmap_range_noflush (shadow_start, shadow_end, +gfpflags_allow_blocking (gfp_mask)); +kmsan_leave_runtime (); err = mapped; goto ret; }
Signature · 1 line
2.25.1
Re: [PATCH v5 02/11] mm/kmsan: undo the shadow mapping when the origin mapping fails
Quoted 12 lines · Hao Ge
kmsan_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 aWARN_ON()and-EBUSYinvmap_pages_pte_range().Undo the shadow mapping on that error path, the same way
kmsan_ioremap_page_range()cleans up after a partial failure.__vunmap_range_noflush()only clears the PTEs; nothing has touched the shadow mapping, so no TLB flush is needed.
Quoted 4 lines
Fixes 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")Suggested-by Suren Baghdasaryan <[email protected]>Signed-off-by Hao Ge <[email protected]>
Quoted 23 lines
--- mm/kmsan/shadow.c | 5 +++++ 1 file changed, 5 insertions(+)mm/kmsan/shadow.c +5
-1@@ -258,6 +258,11 @@ 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. */ +kmsan_enter_runtime (); +__vunmap_range_noflush (shadow_start, shadow_end, +gfpflags_allow_blocking (gfp_mask)); +kmsan_leave_runtime (); err = mapped; goto ret; } --2.25.1
Re: [PATCH v5 02/11] mm/kmsan: undo the shadow mapping when the origin mapping fails
Quoted 9 lines · Suren Baghdasaryan
Quoted 7 lines · Hao Ge
kmsan_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 aWARN_ON()and-EBUSYinvmap_pages_pte_range().
Quoted 4 lines
Quoted 4 lines
Undo the shadow mapping on that error path, the same way
kmsan_ioremap_page_range()cleans up after a partial failure.__vunmap_range_noflush()only clears the PTEs; nothing has touched the shadow mapping, so no TLB flush is needed.
After a second thought, kmsan_ioremap_page_range() does not skip TLB flushing. __vunmap_range_noflush() comment at
https://elixir.bootlin.com/linux/v7.4-rc1/source/mm/vmalloc.c#L497
says:
The caller is responsible for calling flush_cache_vmap() before calling this function, and flush_tlb_kernel_range after it has returned successfully ...". There are no mentions of guarantees that mapped PTEs require explicit access to be cached. Please double-check if this is safe.
Quoted 31 lines
Quoted 5 lines
Fixes 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")Suggested-by Suren Baghdasaryan <[email protected]>Signed-off-by Hao Ge <[email protected]>Reviewed-by Suren Baghdasaryan <[email protected]>Quoted 23 lines
--- mm/kmsan/shadow.c | 5 +++++ 1 file changed, 5 insertions(+)mm/kmsan/shadow.c +5
-1@@ -258,6 +258,11 @@ 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. */ +kmsan_enter_runtime (); +__vunmap_range_noflush (shadow_start, shadow_end, +gfpflags_allow_blocking (gfp_mask)); +kmsan_leave_runtime (); err = mapped; goto ret; } --2.25.1
Re: [PATCH v5 02/11] mm/kmsan: undo the shadow mapping when the origin mapping fails
Hi Suren
Quoted 20 lines · Suren Baghdasaryan
Quoted 9 lines · Suren Baghdasaryan
Quoted 7 lines · Hao Ge
kmsan_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 aWARN_ON()and-EBUSYinvmap_pages_pte_range().Quoted 4 lines
Quoted 4 lines
Undo the shadow mapping on that error path, the same way
kmsan_ioremap_page_range()cleans up after a partial failure.__vunmap_range_noflush()only clears the PTEs; nothing has touched the shadow mapping, so no TLB flush is needed.After a second thought,
kmsan_ioremap_page_range()does not skip TLB flushing.__vunmap_range_noflush()comment at
https://elixir.bootlin.com/linux/v7.4-rc1/source/mm/vmalloc.c#L497
says:
Quoted 5 lines
The caller is responsible for calling
flush_cache_vmap()before calling this function, andflush_tlb_kernel_rangeafter it has returned successfully ...". There are no mentions of guarantees that mapped PTEs require explicit access to be cached. Please double-check if this is safe.
That's a subtle detail. I've looked up the relevant documentation. You are right, and my "no TLB flush is needed" was wrong.
The Arm "Learn the architecture" AArch64 memory management guide says it in "TLBs and Changing translations":
https://support.arm.com/documentation/101811/0106/TLBs-and-Changing-translations
If you change a translation table entry, or the controls that affect how entries are interpreted, then you need to invalidate the affected entries in the TLB. If you do not invalidate those entries, then the processor might continue to use the old translation.
The undo changes the shadow PTEs, so the TLB has to be invalidated.
I also checked the Arm Architecture Reference Manual:
https://support.arm.com/documentation/ddi0487/md/-Part-D-The-AArch64-System-Level-Architecture/-Chapter-D8-The-AArch64-Virtual-Memory-System-Architecture/-D8-16-Translation-Lookaside-Buffers
rule R SQBCS:
If a stage of translation is enabled for a translation regime that is not out-of-context, any translation table entry for that stage that does not cause a Translation fault, an Address size fault, or an Access flag fault is permitted to be cached in a TLB or intermediate TLB caching structure as the result of an explicit or speculative access.
So it may already be cached by speculative access even if there was no explicit access.
For the kernel side, Documentation/core-api/cachetlb.rst
https://elixir.bootlin.com/linux/v7.4-rc1/source/Documentation/core-api/cachetlb.rst
says that The "TLB" is abstracted under Linux as something the cpu uses to cache virtual-->physical address translations obtained from the software page tables. Meaning that if the software page tables change, it is possible for stale translations to exist in this "TLB" cache. Therefore when software page table changes occur, the kernel will invoke one of the following flush methods _after_ the page table changes occur:
The condition there is the page table change, not whether the range was accessed.
Likewise, I believe flush_cache_vmap() is also required.
The sequence in cachetlb.rst isflush_cache_range()change_range_of_page_tables()flush_tlb_range()
so the cache flush has to happen before the unmap.
This is exactly the contract that the __vunmap_range_noflush() comment you quoted spells out, the caller is responsible for calling flush_cache_vmap() before the call and flush_tlb_kernel_range() after it.
The code will do:
mm/kmsan/shadow.c +7 -0
@@ -258,6 +258,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_vmap (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; }
Thanks a lot for catching this, I learned quite a bit about the TLB along the way.
Thanks
Best Regards
Hao
Quoted 32 lines
Quoted 32 lines
Quoted 6 lines
Fixes 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")Suggested-by Suren Baghdasaryan <[email protected]>Signed-off-by Hao Ge <[email protected]>Reviewed-by Suren Baghdasaryan <[email protected]>Quoted 23 lines
--- mm/kmsan/shadow.c | 5 +++++ 1 file changed, 5 insertions(+)mm/kmsan/shadow.c +5
-1@@ -258,6 +258,11 @@ 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. */ +kmsan_enter_runtime (); +__vunmap_range_noflush (shadow_start, shadow_end, +gfpflags_allow_blocking (gfp_mask)); +kmsan_leave_runtime (); err = mapped; goto ret; } --2.25.1
Re: [PATCH v5 02/11] mm/kmsan: undo the shadow mapping when the origin mapping fails
Quoted 80 lines · Hao Ge
Hi Suren
Quoted 20 lines · Suren Baghdasaryan
Quoted 9 lines · Suren Baghdasaryan
Quoted 7 lines · Hao Ge
kmsan_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 aWARN_ON()and-EBUSYinvmap_pages_pte_range().Quoted 4 lines
Quoted 4 lines
Undo the shadow mapping on that error path, the same way
kmsan_ioremap_page_range()cleans up after a partial failure.__vunmap_range_noflush()only clears the PTEs; nothing has touched the shadow mapping, so no TLB flush is needed.After a second thought,
kmsan_ioremap_page_range()does not skip TLB flushing.__vunmap_range_noflush()comment at
https://elixir.bootlin.com/linux/v7.4-rc1/source/mm/vmalloc.c#L497
says:Quoted 5 lines
The caller is responsible for calling
flush_cache_vmap()before calling this function, andflush_tlb_kernel_rangeafter it has returned successfully ...". There are no mentions of guarantees that mapped PTEs require explicit access to be cached. Please double-check if this is safe.That's a subtle detail. I've looked up the relevant documentation. You are right, and my "no TLB flush is needed" was wrong.
The Arm "Learn the architecture" AArch64 memory management guide says it in "TLBs and Changing translations":
https://support.arm.com/documentation/101811/0106/TLBs-and-Changing-translations
If you change a translation table entry, or the controls that affect how entries are interpreted, then you need to invalidate the affected entries in the TLB. If you do not invalidate those entries, then the processor might continue to use the old translation.
The undo changes the shadow PTEs, so the TLB has to be invalidated.
I also checked the Arm Architecture Reference Manual:
https://support.arm.com/documentation/ddi0487/md/-Part-D-The-AArch64-System-Level-Architecture/-Chapter-D8-The-AArch64-Virtual-Memory-System-Architecture/-D8-16-Translation-Lookaside-Buffers
rule R SQBCS:If a stage of translation is enabled for a translation regime that is not out-of-context, any translation table entry for that stage that does not cause a Translation fault, an Address size fault, or an Access flag fault is permitted to be cached in a TLB or intermediate TLB caching structure as the result of an explicit or speculative access.
So it may already be cached by speculative access even if there was no explicit access.
For the kernel side,
Documentation/core-api/cachetlb.rst
https://elixir.bootlin.com/linux/v7.4-rc1/source/Documentation/core-api/cachetlb.rst
says that The "TLB" is abstracted under Linux as something the cpu uses to cache virtual-->physical address translations obtained from the software page tables. Meaning that if the software page tables change, it is possible for stale translations to exist in this "TLB" cache. Therefore when software page table changes occur, the kernel will invoke one of the following flush methods _after_ the page table changes occur:The condition there is the page table change, not whether the range was accessed.
Likewise, I believe
flush_cache_vmap()is also required.
The sequence incachetlb.rstisflush_cache_range()change_range_of_page_tables()flush_tlb_range()
so the cache flush has to happen before the unmap.
This is exactly the contract that the__vunmap_range_noflush()comment you quoted spells out, the caller is responsible for callingflush_cache_vmap()before the call andflush_tlb_kernel_range()after it.The code will do:
mm/kmsan/shadow.c int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, · line 262
Quoted 5 lines
mm/kmsan/shadow.c +2
-0@@ -258,6 +258,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_vmap (shadow_start, shadow_end);
Sorry, this should be flush_cache_vunmap.
Quoted 47 lines
mm/kmsan/shadow.c +5
-0+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; }Thanks a lot for catching this, I learned quite a bit about the TLB along the way.
Thanks
Best Regards
HaoQuoted 32 lines
Quoted 32 lines
Quoted 6 lines
Fixes 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")Suggested-by Suren Baghdasaryan <[email protected]>Signed-off-by Hao Ge <[email protected]>Reviewed-by Suren Baghdasaryan <[email protected]>Quoted 23 lines
--- mm/kmsan/shadow.c | 5 +++++ 1 file changed, 5 insertions(+)mm/kmsan/shadow.c +5
-1@@ -258,6 +258,11 @@ 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. */ +kmsan_enter_runtime (); +__vunmap_range_noflush (shadow_start, shadow_end, +gfpflags_allow_blocking (gfp_mask)); +kmsan_leave_runtime (); err = mapped; goto ret; } --2.25.1
Re: [PATCH v5 02/11] mm/kmsan: undo the shadow mapping when the origin mapping fails
Hi Suren
Quoted 80 lines · Hao Ge
Quoted 77 lines · Hao Ge
Hi Suren
Quoted 20 lines · Suren Baghdasaryan
Quoted 9 lines · Suren Baghdasaryan
Quoted 7 lines · Hao Ge
kmsan_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 aWARN_ON()and-EBUSYinvmap_pages_pte_range().Quoted 4 lines
Quoted 4 lines
Undo the shadow mapping on that error path, the same way
kmsan_ioremap_page_range()cleans up after a partial failure.__vunmap_range_noflush()only clears the PTEs; nothing has touched the shadow mapping, so no TLB flush is needed.After a second thought,
kmsan_ioremap_page_range()does not skip TLB flushing.__vunmap_range_noflush()comment at
https://elixir.bootlin.com/linux/v7.4-rc1/source/mm/vmalloc.c#L497
says:Quoted 5 lines
The caller is responsible for calling
flush_cache_vmap()before calling this function, andflush_tlb_kernel_rangeafter it has returned successfully ...". There are no mentions of guarantees that mapped PTEs require explicit access to be cached. Please double-check if this is safe.That's a subtle detail. I've looked up the relevant documentation. You are right, and my "no TLB flush is needed" was wrong.
The Arm "Learn the architecture" AArch64 memory management guide says it in "TLBs and Changing translations":
https://support.arm.com/documentation/101811/0106/TLBs-and-Changing-translations
If you change a translation table entry, or the controls that affect how entries are interpreted, then you need to invalidate the affected entries in the TLB. If you do not invalidate those entries, then the processor might continue to use the old translation.
The undo changes the shadow PTEs, so the TLB has to be invalidated.
I also checked the Arm Architecture Reference Manual:
https://support.arm.com/documentation/ddi0487/md/-Part-D-The-AArch64-System-Level-Architecture/-Chapter-D8-The-AArch64-Virtual-Memory-System-Architecture/-D8-16-Translation-Lookaside-Buffers
rule R SQBCS:If a stage of translation is enabled for a translation regime that is not out-of-context, any translation table entry for that stage that does not cause a Translation fault, an Address size fault, or an Access flag fault is permitted to be cached in a TLB or intermediate TLB caching structure as the result of an explicit or speculative access.
So it may already be cached by speculative access even if there was no explicit access.
For the kernel side,
Documentation/core-api/cachetlb.rst
https://elixir.bootlin.com/linux/v7.4-rc1/source/Documentation/core-api/cachetlb.rst
says that The "TLB" is abstracted under Linux as something the cpu uses to cache virtual-->physical address translations obtained from the software page tables. Meaning that if the software page tables change, it is possible for stale translations to exist in this "TLB" cache. Therefore when software page table changes occur, the kernel will invoke one of the following flush methods _after_ the page table changes occur:The condition there is the page table change, not whether the range was accessed.
Likewise, I believe
flush_cache_vmap()is also required.
The sequence incachetlb.rstisflush_cache_range()change_range_of_page_tables()flush_tlb_range()
so the cache flush has to happen before the unmap.
This is exactly the contract that the__vunmap_range_noflush()comment you quoted spells out, the caller is responsible for callingflush_cache_vmap()before the call andflush_tlb_kernel_range()after it.The code will do:
Quoted 23 lines
mm/kmsan/shadow.c int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, · line 262
Quoted 8 lines
mm/kmsan/shadow.c +2
-0@@ -258,6 +258,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_vmap (shadow_start, shadow_end);Sorry, this should be
flush_cache_vunmap.Quoted 12 lines
mm/kmsan/shadow.c +5
-0+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; }Thanks a lot for catching this, I learned quite a bit about the TLB along the way.
Let's revisit this. Unless I'm missing something, the unmap counterpart, kmsan_vunmap_range_noflush(), appears to be missing its TLB flush.
Taking vmalloc as the example:
vmalloc
__vmalloc_area_node()
vmap_pages_range()
vmap_pages_range_noflush()
kmsan_vmap_pages_range_noflush()
installs the shadow/origin and flushes their TLB at the tail /* flushed */
vfree
remove_vm_area
free_unmap_vmap_area
vunmap_range_noflush /*shadow/origin PTEs cleared,
no TLB flush */
free_vmap_area_noflush() /* area -> lazy list
... later, when the lazy list is settled:
__purge_vmap_area_lazy
flush_tlb_kernel_range(start, end);
/* no shadow/origin tlb flush */
So unlike the normal paths, KMSAN performs the TLB flush when the range is reused and re-mapped, rather than flushing it immediately after unmap.
I'm not sure this is safe. Reading the documentation, this should not be safe, perhaps I have misinterpreted the documentation.
If this was intentional, maybe a TLB flush is not required here, but if so I would expect it in the purge instead, together with the main range. After all, vmalloc is frequently called, and lazy TLB flushing is an existing optimization for vmalloc.
Would you mind taking a look at this concern, or letting me know who I could ask? This would be a good opportunity for me to brush up on my TLB knowledge, and I would greatly appreciate your input.
Thanks
Best Regards
Hao
Quoted 35 lines
Quoted 35 lines
Thanks
Best Regards
HaoQuoted 32 lines
Quoted 32 lines
Quoted 6 lines
Fixes 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")Suggested-by Suren Baghdasaryan <[email protected]>Signed-off-by Hao Ge <[email protected]>Reviewed-by Suren Baghdasaryan <[email protected]>Quoted 23 lines
--- mm/kmsan/shadow.c | 5 +++++ 1 file changed, 5 insertions(+)mm/kmsan/shadow.c +5
-1@@ -258,6 +258,11 @@ 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. */ +kmsan_enter_runtime (); +__vunmap_range_noflush (shadow_start, shadow_end, +gfpflags_allow_blocking (gfp_mask)); +kmsan_leave_runtime (); err = mapped; goto ret; } --2.25.1
PATCH v5 3/11 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 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/vmalloc.c +3 -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,9 @@ int vmap_page_range(unsigned long addr, unsigned long end, {int err; +if (WARN_ON_ONCE (!PAGE_ALIGNED (addr) || !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
Re: [PATCH v5 03/11] mm/vmalloc: reject an unaligned range in vmap_page_range()
Quoted 11 lines · Hao Ge
commit
a739e6b557af("mm: vmalloc:WARN_ONif mapping size is notPAGE_SIZEaligned") put the check invmap_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.
Quoted 8 lines
vmap_range_noflush()has a single caller,vmap_page_range(). Warn on addr and end there and bail out with-EINVALbefore any page table is installed, and drop the check fromvmap_pte_range().Suggested-by Suren Baghdasaryan <[email protected]>Suggested-by Dev Jain <[email protected]>Signed-off-by Hao Ge <[email protected]>---
Quoted 27 lines
mm/vmalloc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)mm/vmalloc.c +3
-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,9 @@ int vmap_page_range(unsigned long addr, unsigned long end, {int err; +if (WARN_ON_ONCE (!PAGE_ALIGNED (addr) || !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);
Re: [PATCH v5 03/11] mm/vmalloc: reject an unaligned range in vmap_page_range()
In reply to Hao GeQuoted 49 lines · Hao Ge
commit
a739e6b557af("mm: vmalloc:WARN_ONif mapping size is notPAGE_SIZEaligned") put the check invmap_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-EINVALbefore any page table is installed, and drop the check fromvmap_pte_range().Suggested-by Suren Baghdasaryan <[email protected]>Suggested-by Dev Jain <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)mm/vmalloc.c +3
-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,9 @@ int vmap_page_range(unsigned long addr, unsigned long end, {int err; +if (WARN_ON_ONCE (!PAGE_ALIGNED (addr) || !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
LGTM,
--
Uladzislau Rezki
Suren Baghdasaryan
Reviewed-byRe: [PATCH v5 03/11] mm/vmalloc: reject an unaligned range in vmap_page_range()
Quoted 53 lines · Uladzislau Rezki
Quoted 47 lines · Hao Ge
commit
a739e6b557af("mm: vmalloc:WARN_ONif mapping size is notPAGE_SIZEaligned") put the check invmap_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-EINVALbefore any page table is installed, and drop the check fromvmap_pte_range().Suggested-by Suren Baghdasaryan <[email protected]>Suggested-by Dev Jain <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)mm/vmalloc.c +3
-4@@ -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,9 @@ int vmap_page_range(unsigned long addr, unsigned long end, {int err; +if (WARN_ON_ONCE (!PAGE_ALIGNED (addr) || !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); --Quoted 2 lines
2.25.1
LGTM,
Quoted 1 line
Reviewed-by Uladzislau Rezki (Sony) <[email protected]>
Quoted 3 lines
--
Uladzislau Rezki
PATCH v5 4/11 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.
--- mm/vmalloc.c | 3 +++ 1 file changed, 3 insertions(+)
mm/vmalloc.c +3 -0
@@ -523,6 +523,9 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end,pgtbl_mod_mask mask =0 ;BUG_ON (addr >= end); +if (WARN_ON_ONCE (!PAGE_ALIGNED (addr) || !PAGE_ALIGNED (end))) +return ; + pgd =pgd_offset_k (addr); do { next = pgd_addr_end(addr, end);
Signature · 1 line
2.25.1
Re: [PATCH v5 04/11] mm/vmalloc: reject an unaligned range in __vunmap_range_noflush()
Quoted 21 lines · Hao Ge
The 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.Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 3 +++ 1 file changed, 3 insertions(+)mm/vmalloc.c +0
-0
mm/vmalloc.c void __vunmap_range_noflush(unsigned long start, unsigned long end, · line 525
Quoted 3 lines
mm/vmalloc.c +0
-0@@ -523,6 +523,9 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end,pgtbl_mod_mask mask =0 ;BUG_ON (addr >= end);
Nit: It is usually a practice to change the existing BUG_ON's to WARN_ON's too, if you are changing around that part.
mm/vmalloc.c void __vunmap_range_noflush(unsigned long start, unsigned long end, · line 527
Quoted 2 lines
mm/vmalloc.c +2
-0+if (WARN_ON_ONCE (!PAGE_ALIGNED (addr) || !PAGE_ALIGNED (end))) +return ;
Quoted 4 lines
mm/vmalloc.c +1
-0+ pgd =pgd_offset_k (addr); do { next = pgd_addr_end(addr, end);
Re: [PATCH v5 04/11] mm/vmalloc: reject an unaligned range in __vunmap_range_noflush()
Quoted 32 lines · Dev Jain
Quoted 21 lines · Hao Ge
The 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.Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 3 +++ 1 file changed, 3 insertions(+)mm/vmalloc.c +0
-0mm/vmalloc.c void __vunmap_range_noflush(unsigned long start, unsigned long end, · line 525
Quoted 3 lines
mm/vmalloc.c +0
-0@@ -523,6 +523,9 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end,pgtbl_mod_mask mask =0 ;BUG_ON (addr >= end);Nit: It is usually a practice to change the existing BUG_ON's to WARN_ON's too, if you are changing around that part.
Quoted 5 lines
mm/vmalloc.c void __vunmap_range_noflush(unsigned long start, unsigned long end, · line 527
Quoted 2 lines
mm/vmalloc.c +2
-0+if (WARN_ON_ONCE (!PAGE_ALIGNED (addr) || !PAGE_ALIGNED (end))) +return ;Reviewed-by Dev Jain <[email protected]>
Agree, please also replace BUG_ON() and feel free to add:
--
Uladzislau Rezki
Suren Baghdasaryan
Reviewed-byRe: [PATCH v5 04/11] mm/vmalloc: reject an unaligned range in __vunmap_range_noflush()
In reply to Dev JainQuoted 27 lines · Dev Jain
Quoted 23 lines · Hao Ge
The 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.Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 3 +++ 1 file changed, 3 insertions(+)mm/vmalloc.c +0
-0@@ -523,6 +523,9 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end,pgtbl_mod_mask mask =0 ;
Quoted 8 lines
mm/vmalloc.c void __vunmap_range_noflush(unsigned long start, unsigned long end, · line 525
Quoted 1 line
mm/vmalloc.c +0
-0BUG_ON (addr >= end);Nit: It is usually a practice to change the existing BUG_ON's to WARN_ON's too, if you are changing around that part.
Quoted 2 lines
mm/vmalloc.c +2
-0+if (WARN_ON_ONCE (!PAGE_ALIGNED (addr) || !PAGE_ALIGNED (end))) +return ;
nit: It's also better not to combine multiple conditions into one warning so that the generated report clearly shows which condition (code line) caused the warning. IOW:
if (WARN_ON_ONCE(!PAGE_ALIGNED(addr))
return;
if (WARN_ON_ONCE(!PAGE_ALIGNED(end))
return;
Quoted 2 lines
Reviewed-by Dev Jain <[email protected]>
Quoted 6 lines
Quoted 5 lines
mm/vmalloc.c +1
-0+ pgd =pgd_offset_k (addr); do { next = pgd_addr_end(addr, end);
Re: [PATCH v5 04/11] mm/vmalloc: reject an unaligned range in __vunmap_range_noflush()
Hi Suren and Dev
Quoted 28 lines · Suren Baghdasaryan
Quoted 27 lines · Dev Jain
Quoted 23 lines · Hao Ge
The 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.Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 3 +++ 1 file changed, 3 insertions(+)mm/vmalloc.c +0
-0@@ -523,6 +523,9 @@ void __vunmap_range_noflush(unsigned long start, unsigned long end,pgtbl_mod_mask mask =0 ;
Quoted 4 lines
Quoted 4 lines
mm/vmalloc.c void __vunmap_range_noflush(unsigned long start, unsigned long end, · line 525
Quoted 1 line
mm/vmalloc.c +0
-0BUG_ON (addr >= end);Nit: It is usually a practice to change the existing BUG_ON's to WARN_ON's too, if you are changing around that part.
Ack.
Quoted 8 lines
Quoted 4 lines
mm/vmalloc.c void __vunmap_range_noflush(unsigned long start, unsigned long end, · line 527
Quoted 4 lines
mm/vmalloc.c +2
-0+if (WARN_ON_ONCE (!PAGE_ALIGNED (addr) || !PAGE_ALIGNED (end))) +return ;nit: It's also better not to combine multiple conditions into one warning so that the generated report clearly shows which condition (code line) caused the warning. IOW:
Ack on applying the same to the previous patch in the series, "mm/vmalloc:
reject an unaligned range in vmap_page_range()", as well, it is done the same way there now.
Thanks
Best Regards
Hao
Quoted 19 lines
if (WARN_ON_ONCE(!PAGE_ALIGNED(addr))
return;if (WARN_ON_ONCE(!PAGE_ALIGNED(end))
return;Quoted 2 lines
Reviewed-by Dev Jain <[email protected]>Reviewed-by Suren Baghdasaryan <[email protected]>Quoted 6 lines
Quoted 5 lines
mm/vmalloc.c +1
-0+ pgd =pgd_offset_k (addr); do { next = pgd_addr_end(addr, end);
PATCH v5 5/11 mm/vmalloc: only flush the cache after the vmap succeeded
In reply to Hao Gevmap_page_range(), __vmap_pages_range() and vmap_pages_range_batched() call flush_cache_vmap() before they even look at the error code. A failed mapping is never used, so the flush is pointless at best.
According to the Sashiko review it is worse than pointless on UML:flush_cache_vmap() there is flush_tlb_kernel_range(), which syncs the PTEs into the host OS. Flushing a half-installed mapping therefore leaves host mappings behind for a range that was just freed again.
Only flush when the mapping, and for vmap_page_range() also the KMSAN metadata, is fully set up.
--- mm/vmalloc.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
mm/vmalloc.c +8 -3
@@ -371,10 +371,12 @@ int vmap_page_range(unsigned long addr, unsigned long end, err =vmap_range_noflush (addr, end, phys_addr,pgprot_nx (prot), ioremap_max_page_shift); -flush_cache_vmap (addr, end);if (!err) err =kmsan_ioremap_page_range (addr, end, phys_addr, prot, ioremap_max_page_shift); +if (!err) +flush_cache_vmap (addr, end); +return err; }
@@ -763,7 +765,9 @@ static int __vmap_pages_range(unsigned long addr, unsigned long end,int err; err =vmap_pages_range_noflush (addr, end, prot, pages, page_shift, gfp_mask); -flush_cache_vmap (addr, end); +if (!err) +flush_cache_vmap (addr, end); +return err; }
@@ -3729,7 +3733,8 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, pages + batch_idx, prev_shift); out: -flush_cache_vmap (addr, end); +if (!err) +flush_cache_vmap (addr, end);return err; }
Signature · 1 line
2.25.1
Uladzislau Rezki
maintainerRe: [PATCH v5 05/11] mm/vmalloc: only flush the cache after the vmap succeeded
Quoted 27 lines · Hao Ge
vmap_page_range(),__vmap_pages_range()andvmap_pages_range_batched()callflush_cache_vmap()before they even look at the error code. A failed mapping is never used, so the flush is pointless at best.According to the Sashiko review it is worse than pointless on UML:
flush_cache_vmap()there isflush_tlb_kernel_range(), which syncs the PTEs into the host OS. Flushing a half-installed mapping therefore leaves host mappings behind for a range that was just freed again.Only flush when the mapping, and for
vmap_page_range()also the KMSAN metadata, is fully set up.Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)mm/vmalloc.c +0
-0@@ -371,10 +371,12 @@ int vmap_page_range(unsigned long addr, unsigned long end, err =vmap_range_noflush (addr, end, phys_addr,pgprot_nx (prot), ioremap_max_page_shift);
mm/vmalloc.c int vmap_page_range(unsigned long addr, unsigned long end, · line 382
Quoted 10 lines
mm/vmalloc.c +3
-1-flush_cache_vmap (addr, end);if (!err) err =kmsan_ioremap_page_range (addr, end, phys_addr, prot, ioremap_max_page_shift); +if (!err) +flush_cache_vmap (addr, end); +return err; }
This is incomplete. Maybe merge with 6.
--
Uladzislau Rezki
Re: [PATCH v5 05/11] mm/vmalloc: only flush the cache after the vmap succeeded
Quoted 27 lines · Uladzislau Rezki
Quoted 25 lines · Hao Ge
vmap_page_range(),__vmap_pages_range()andvmap_pages_range_batched()callflush_cache_vmap()before they even look at the error code. A failed mapping is never used, so the flush is pointless at best.According to the Sashiko review it is worse than pointless on UML:
flush_cache_vmap()there isflush_tlb_kernel_range(), which syncs the PTEs into the host OS. Flushing a half-installed mapping therefore leaves host mappings behind for a range that was just freed again.Only flush when the mapping, and for
vmap_page_range()also the KMSAN metadata, is fully set up.Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)mm/vmalloc.c +0
-0@@ -371,10 +371,12 @@ int vmap_page_range(unsigned long addr, unsigned long end,
Quoted 13 lines
mm/vmalloc.c int vmap_page_range(unsigned long addr, unsigned long end, · line 382
Quoted 12 lines
mm/vmalloc.c +3
-1err =vmap_range_noflush (addr, end, phys_addr,pgprot_nx (prot), ioremap_max_page_shift); -flush_cache_vmap (addr, end);if (!err) err =kmsan_ioremap_page_range (addr, end, phys_addr, prot, ioremap_max_page_shift); +if (!err) +flush_cache_vmap (addr, end); +return err; }This is incomplete. Maybe merge with 6.
Yeah, by itself this would still keep failed partial mappings, so it doesn't really fix much, right? Merging with 6 makes more sense to me too.
Quoted 3 lines
--
Uladzislau Rezki
PATCH v5 6/11 mm/vmalloc: undo what vmap_page_range() mapped on failure
In reply to Hao Geioremap and arch setup code map their ranges with vmap_page_range(). When the walk fails half way, or the KMSAN metadata fails after the walk, the ptes installed so far stay behind. The caller frees the vm_area on the error path, so the next mapping that lands on the same range trips the BUG() in vmap_pte_range().
vmap_range_noflush() now takes a mapped_end argument that the walk levels advance as they install, and vmap_pte_range() BUG()s before it installs over a pte that is already set. The ptes below mapped_end are therefore all ours, while a pte above it can only have been put there by somebody else, for example when the caller's range overlaps an existing mapping. On a walk failure the undo is limited to [addr, mapped_end), going further would clear ptes that are not ours. If the walk completed and only the KMSAN metadata failed, the whole range was installed by this call and all of it is undone.
All callers are sleepable, the teardown passes can_resched = true.
--- mm/vmalloc.c | 52 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-)
mm/vmalloc.c +39 -13
@@ -117,7 +117,8 @@ static __always_inline unsigned long vmap_set_ptes(pte_t *ptep,static int vmap_pte_range (pmd_t *pmd,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) {pte_t *pte;u64 pfn;
@@ -143,6 +144,7 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, size =vmap_set_ptes (pte, addr, end, pfn, prot, max_page_shift); nr_pages =PFN_DOWN (size); + *mapped_end = addr + size; }while (pte += nr_pages, pfn += nr_pages, addr += size, addr != end);lazy_mmu_mode_disable ();
@@ -188,7 +190,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,10 +206,12 @@ 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, mapped_end);if (err)break ; }while (pmd++, phys_addr += (next - addr), addr = next, addr != end);
@@ -245,7 +250,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 +266,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 +310,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 +326,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 +340,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 +357,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,18 +376,32 @@ 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) || !PAGE_ALIGNED (end)))return -EINVAL ; err =vmap_range_noflush (addr, end, phys_addr,pgprot_nx (prot), - ioremap_max_page_shift); -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_noflush (addr, mapped_end, true); + }else { err =kmsan_ioremap_page_range (addr, end, phys_addr, prot, ioremap_max_page_shift); -if (!err) -flush_cache_vmap (addr, end); +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_noflush (addr, end, true); + }else { +flush_cache_vmap (addr, end); + } + }return err; }
Signature · 1 line
2.25.1
Uladzislau Rezki
maintainerRe: [PATCH v5 06/11] mm/vmalloc: undo what vmap_page_range() mapped on failure
Quoted 38 lines · Hao Ge
ioremap and arch setup code map their ranges with
vmap_page_range(). When the walk fails half way, or the KMSAN metadata fails after the walk, the ptes installed so far stay behind. The caller frees thevm_areaon the error path, so the next mapping that lands on the same range trips theBUG()invmap_pte_range().
vmap_range_noflush()now takes amapped_endargument that the walk levels advance as they install, andvmap_pte_range()BUG()s before it installs over a pte that is already set. The ptes belowmapped_endare therefore all ours, while a pte above it can only have been put there by somebody else, for example when the caller's range overlaps an existing mapping. On a walk failure the undo is limited to [addr,mapped_end), going further would clear ptes that are not ours. If the walk completed and only the KMSAN metadata failed, the whole range was installed by this call and all of it is undone.All callers are sleepable, the teardown passes
can_resched= true.Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 52 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-)mm/vmalloc.c +2
-1@@ -117,7 +117,8 @@ static __always_inline unsigned long vmap_set_ptes(pte_t *ptep,static int vmap_pte_range (pmd_t *pmd,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) {pte_t *pte;u64 pfn;
mm/vmalloc.c static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · line 149
Quoted 6 lines
mm/vmalloc.c +1
-0@@ -143,6 +144,7 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, size =vmap_set_ptes (pte, addr, end, pfn, prot, max_page_shift); nr_pages =PFN_DOWN (size); + *mapped_end = addr + size; }while (pte += nr_pages, pfn += nr_pages, addr += size, addr != end);
vmap_pte_range() either s=install everything or nothing. At least if i do not miss anything.
--
Uladzislau Rezki
--
Uladzislau Rezki
Re: [PATCH v5 06/11] mm/vmalloc: undo what vmap_page_range() mapped on failure
Hi Ulad
Quoted 40 lines · Uladzislau Rezki
Quoted 39 lines · Hao Ge
ioremap and arch setup code map their ranges with
vmap_page_range(). When the walk fails half way, or the KMSAN metadata fails after the walk, the ptes installed so far stay behind. The caller frees thevm_areaon the error path, so the next mapping that lands on the same range trips theBUG()invmap_pte_range().
vmap_range_noflush()now takes amapped_endargument that the walk levels advance as they install, andvmap_pte_range()BUG()s before it installs over a pte that is already set. The ptes belowmapped_endare therefore all ours, while a pte above it can only have been put there by somebody else, for example when the caller's range overlaps an existing mapping. On a walk failure the undo is limited to [addr,mapped_end), going further would clear ptes that are not ours. If the walk completed and only the KMSAN metadata failed, the whole range was installed by this call and all of it is undone.All callers are sleepable, the teardown passes
can_resched= true.Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 52 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-)mm/vmalloc.c +2
-1@@ -117,7 +117,8 @@ static __always_inline unsigned long vmap_set_ptes(pte_t *ptep,static int vmap_pte_range (pmd_t *pmd,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) {pte_t *pte;u64 pfn;@@ -143,6 +144,7 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
Quoted 7 lines
mm/vmalloc.c static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · line 149
Quoted 5 lines
mm/vmalloc.c +1
-0size =vmap_set_ptes (pte, addr, end, pfn, prot, max_page_shift); nr_pages =PFN_DOWN (size); + *mapped_end = addr + size; }while (pte += nr_pages, pfn += nr_pages, addr += size, addr != end);
vmap_pte_range()either s=install everything or nothing. At least if i do not miss anything.
Right, vmap_pte_range() either installs the whole range or nothing.
I will drop the mapped_end argument from it, and vmap_pmd_range() will advance *mapped_end = next after the call succeeds.
Thanks
Best Regards
Hao
Quoted 6 lines
--
Uladzislau Rezki--
Uladzislau Rezki
Re: [PATCH v5 06/11] mm/vmalloc: undo what vmap_page_range() mapped on failure
In reply to Hao GeQuoted 163 lines · Hao Ge
ioremap and arch setup code map their ranges with
vmap_page_range(). When the walk fails half way, or the KMSAN metadata fails after the walk, the ptes installed so far stay behind. The caller frees thevm_areaon the error path, so the next mapping that lands on the same range trips theBUG()invmap_pte_range().
vmap_range_noflush()now takes amapped_endargument that the walk levels advance as they install, andvmap_pte_range()BUG()s before it installs over a pte that is already set. The ptes belowmapped_endare therefore all ours, while a pte above it can only have been put there by somebody else, for example when the caller's range overlaps an existing mapping. On a walk failure the undo is limited to [addr,mapped_end), going further would clear ptes that are not ours. If the walk completed and only the KMSAN metadata failed, the whole range was installed by this call and all of it is undone.All callers are sleepable, the teardown passes
can_resched= true.Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 52 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-)mm/vmalloc.c +28
-12@@ -117,7 +117,8 @@ static __always_inline unsigned long vmap_set_ptes(pte_t *ptep,static int vmap_pte_range (pmd_t *pmd,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) {pte_t *pte;u64 pfn;@@ -143,6 +144,7 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, size =vmap_set_ptes (pte, addr, end, pfn, prot, max_page_shift); nr_pages =PFN_DOWN (size); + *mapped_end = addr + size; }while (pte += nr_pages, pfn += nr_pages, addr += size, addr != end);lazy_mmu_mode_disable ();@@ -188,7 +190,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,10 +206,12 @@ 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, mapped_end);if (err)break ; }while (pmd++, phys_addr += (next - addr), addr = next, addr != end);@@ -245,7 +250,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 +266,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 +310,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 +326,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 +340,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 +357,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,18 +376,32 @@ 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) || !PAGE_ALIGNED (end)))return -EINVAL ; err =vmap_range_noflush (addr, end, phys_addr,pgprot_nx (prot), - ioremap_max_page_shift); -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_noflush (addr, mapped_end, true); + }else { err =kmsan_ioremap_page_range (addr, end, phys_addr, prot, ioremap_max_page_shift); -if (!err)
mm/vmalloc.c static int vmap_range_noflush(unsigned long addr, unsigned long end, · line 402
Quoted 10 lines
mm/vmalloc.c +9
-1-flush_cache_vmap (addr, end); +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_noflush (addr, end, true); + }else { +flush_cache_vmap (addr, end);
This again skips TLB flushing after __vunmap_range_noflush(). I'm not sure that's ok even if there was no explicit access.
Quoted 8 lines
mm/vmalloc.c +2
-1+ } + }return err; } --2.25.1
PATCH v5 7/11 mm/vmalloc: undo what __vmap_pages_range_noflush() mapped on failure
In reply to Hao Ge__vmap_pages_range_noflush() maps a struct page array and may fail half way. The ptes installed so far stay behind, so a retry of the same range, the __GFP_NOFAIL loop in __vmalloc_area_node() or the module tags populate, trips the WARN_ON(!pte_none()) in vmap_pages_pte_range() and fails with -EBUSY.
vmap_pages_range_noflush_walk() now reports how many pages it installed through its running index into the pages array, and __vmap_pages_range_noflush() undoes [addr, addr + mapped_pages * PAGE_SIZE) on failure. vmap_pages_pte_range() fails with -EBUSY instead of installing over a pte that is already set, so the ptes below that point are all ours, while a pte above it can only have been put there by somebody else, for example when the caller's range overlaps an existing mapping. Going further would clear ptes that are not ours.
This path can run with a non-blocking gfp mask, the undo passes gfpflags_allow_blocking() as can_resched.
--- mm/kmsan/hooks.c | 4 ++-- mm/kmsan/shadow.c | 4 ++-- mm/vmalloc.c | 36 +++++++++++++++++++++++++++++------- mm/vmalloc.h | 3 ++- 4 files changed, 35 insertions(+), 12 deletions(-)
mm/kmsan/hooks.c +2 -2
@@ -208,7 +208,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;
@@ -218,7 +218,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
@@ -247,7 +247,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;
@@ -255,7 +255,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 +29 -7
@@ -725,7 +725,8 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,* may have contiguous chunks, as hinted by @shift. */ static int vmap_pages_range_noflush_walk (unsigned long addr,unsigned long end, -pgprot_t prot,struct page **pages,unsigned int shift) +pgprot_t prot,struct page **pages,unsigned int shift, +unsigned int *mapped_pages) {unsigned long start = addr;pgd_t *pgd;
@@ -748,6 +749,7 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,if (mask &ARCH_PAGE_TABLE_SYNC_MASK )arch_sync_kernel_mappings (start, end); + *mapped_pages = nr;return err; }
@@ -758,18 +760,36 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,* The caller is responsible for calling flush_cache_vmap() after this * function returns successfully and before the addresses are accessed. * +* On failure the PTEs installed by this call are torn down before +* returning, so a retry sees a clean range; PTEs that were already +* there when the walk failed are left alone. When @gfp_mask cannot +* block, the teardown skips cond_resched(), it may run in atomic +* context. +* * This is an internal function only. Do not use outside mm/. */ int __vmap_pages_range_noflush (unsigned long addr,unsigned long end, -pgprot_t prot,struct page **pages,unsigned int page_shift) +pgprot_t prot,struct page **pages,unsigned int page_shift, +gfp_t gfp_mask) { +int err; +unsigned int mapped_pages =0 ; +if (WARN_ON_ONCE (page_shift <PAGE_SHIFT ))return -EINVAL ;if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMALLOC )) page_shift =PAGE_SHIFT ; -return vmap_pages_range_noflush_walk (addr, end, prot, pages, page_shift); + err =vmap_pages_range_noflush_walk (addr, end, prot, pages, page_shift, + &mapped_pages); + +/* Undo the PTEs installed before the failure. */ +if (err && mapped_pages) +__vunmap_range_noflush (addr, addr + (mapped_pages *PAGE_SIZE ), +gfpflags_allow_blocking (gfp_mask)); + +return err; } int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
@@ -781,7 +801,8 @@ int vmap_pages_range_noflush(unsigned long addr, unsigned long end,if (ret)return ret; -return __vmap_pages_range_noflush (addr, end, prot, pages, page_shift); +return __vmap_pages_range_noflush (addr, end, prot, pages, page_shift, + gfp_mask); } static int __vmap_pages_range(unsigned long addr, unsigned long end,
@@ -3715,7 +3736,7 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages) {const unsigned int nr_pages = (end - addr) >>PAGE_SHIFT ; -unsigned int prev_shift =0 , batch_idx =0 ; +unsigned int prev_shift =0 , batch_idx =0 , nr_mapped;unsigned long batch_start = addr, batch_end = addr;int err;
@@ -3734,7 +3755,8 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,if (shift != prev_shift) { err =vmap_pages_range_noflush_walk (batch_start, batch_end, - prot, pages + batch_idx, prev_shift); + prot, pages + batch_idx, prev_shift, + &nr_mapped);if (err) goto out; prev_shift = shift;
@@ -3756,7 +3778,7 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,/* Remaining */ if (batch_start < end) err =vmap_pages_range_noflush_walk (batch_start, end, prot, - pages + batch_idx, prev_shift); + pages + batch_idx, prev_shift, &nr_mapped); out:if (!err)
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
[email protected]
AI reviewRe: [PATCH v5 07/11] mm/vmalloc: undo what __vmap_pages_range_noflush() mapped on failure
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] mm/vmalloc: vmap_pages_range_noflush_walk() truncates nr into unsigned int --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mm/vmalloc: vmap_pages_range_noflush_walk() truncates nr into unsigned int
--
commit f06c1baff5ffda8b5f9a1dce600b9d716bd0c648
Author: Hao Ge <[email protected]>
mm/vmalloc: undo what __vmap_pages_range_noflush() mapped on failure
This commit tracks the number of pages installed by vmap_pages_range_noflush_walk() and tears down those PTEs in __vmap_pages_range_noflush() when a mapping fails midway.
Quoted 49 lines
mm/vmalloc.c +14
-3@@ -725,7 +725,8 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,* may have contiguous chunks, as hinted by @shift. */ static int vmap_pages_range_noflush_walk (unsigned long addr,unsigned long end, -pgprot_t prot,struct page **pages,unsigned int shift) +pgprot_t prot,struct page **pages,unsigned int shift, +unsigned int *mapped_pages) {unsigned long start = addr;pgd_t *pgd;@@ -748,6 +749,7 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,if (mask &ARCH_PAGE_TABLE_SYNC_MASK )arch_sync_kernel_mappings (start, end); + *mapped_pages = nr;return err; }@@ -758,18 +760,36 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,* The caller is responsible for calling flush_cache_vmap() after this * function returns successfully and before the addresses are accessed. * +* On failure the PTEs installed by this call are torn down before +* returning, so a retry sees a clean range; PTEs that were already +* there when the walk failed are left alone. When @gfp_mask cannot +* block, the teardown skips cond_resched(), it may run in atomic +* context. +* * This is an internal function only. Do not use outside mm/. */ int __vmap_pages_range_noflush (unsigned long addr,unsigned long end, -pgprot_t prot,struct page **pages,unsigned int page_shift) +pgprot_t prot,struct page **pages,unsigned int page_shift, +gfp_t gfp_mask) { +int err; +unsigned int mapped_pages =0 ; +if (WARN_ON_ONCE (page_shift <PAGE_SHIFT ))return -EINVAL ;if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMALLOC )) page_shift =PAGE_SHIFT ; -return vmap_pages_range_noflush_walk (addr, end, prot, pages, page_shift);
mm/vmalloc.c static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end, · line 793
Quoted 10 lines
mm/vmalloc.c +9
-0+ err =vmap_pages_range_noflush_walk (addr, end, prot, pages, page_shift, + &mapped_pages); + +/* Undo the PTEs installed before the failure. */ +if (err && mapped_pages) +__vunmap_range_noflush (addr, addr + (mapped_pages *PAGE_SIZE ), +gfpflags_allow_blocking (gfp_mask)); + +return err; }
[Severity: Low]
Should mapped_pages (and nr_mapped in vmap_pages_range_batched()) be unsigned long instead of unsigned int?
vmap_pages_range_noflush_walk() tracks nr as an unsigned long across the page-table walk, so storing it into an unsigned int truncates the count to 32 bits and would leave the unmap range incomplete if a walk ever failed past UINT_MAX pages.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH v5 07/11] mm/vmalloc: undo what __vmap_pages_range_noflush() mapped on failure
Quoted 79 lines · On 2026/11/17 15:15, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mm/vmalloc:vmap_pages_range_noflush_walk()truncates nr into unsigned int
--commit
f06c1baff5ffda8b5f9a1dce600b9d716bd0c648
Author: Hao Ge <[email protected]>mm/vmalloc: undo what
__vmap_pages_range_noflush()mapped on failureThis commit tracks the number of pages installed by
vmap_pages_range_noflush_walk()and tears down those PTEs in__vmap_pages_range_noflush()when a mapping fails midway.Quoted 49 lines
mm/vmalloc.c +14
-3@@ -725,7 +725,8 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,* may have contiguous chunks, as hinted by @shift. */ static int vmap_pages_range_noflush_walk (unsigned long addr,unsigned long end, -pgprot_t prot,struct page **pages,unsigned int shift) +pgprot_t prot,struct page **pages,unsigned int shift, +unsigned int *mapped_pages) {unsigned long start = addr;pgd_t *pgd;@@ -748,6 +749,7 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,if (mask &ARCH_PAGE_TABLE_SYNC_MASK )arch_sync_kernel_mappings (start, end); + *mapped_pages = nr;return err; }@@ -758,18 +760,36 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,* The caller is responsible for calling flush_cache_vmap() after this * function returns successfully and before the addresses are accessed. * +* On failure the PTEs installed by this call are torn down before +* returning, so a retry sees a clean range; PTEs that were already +* there when the walk failed are left alone. When @gfp_mask cannot +* block, the teardown skips cond_resched(), it may run in atomic +* context. +* * This is an internal function only. Do not use outside mm/. */ int __vmap_pages_range_noflush (unsigned long addr,unsigned long end, -pgprot_t prot,struct page **pages,unsigned int page_shift) +pgprot_t prot,struct page **pages,unsigned int page_shift, +gfp_t gfp_mask) { +int err; +unsigned int mapped_pages =0 ; +if (WARN_ON_ONCE (page_shift <PAGE_SHIFT ))return -EINVAL ;if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMALLOC )) page_shift =PAGE_SHIFT ; -return vmap_pages_range_noflush_walk (addr, end, prot, pages, page_shift);mm/vmalloc.c static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end, · line 793
Quoted 10 lines
mm/vmalloc.c +9
-0+ err =vmap_pages_range_noflush_walk (addr, end, prot, pages, page_shift, + &mapped_pages); + +/* Undo the PTEs installed before the failure. */ +if (err && mapped_pages) +__vunmap_range_noflush (addr, addr + (mapped_pages *PAGE_SIZE ), +gfpflags_allow_blocking (gfp_mask)); + +return err; }[Severity: Low]
Shouldmapped_pages(andnr_mappedinvmap_pages_range_batched()) be unsigned long instead of unsigned int?
Quoted 5 lines
vmap_pages_range_noflush_walk()tracks nr as an unsigned long across the page-table walk, so storing it into an unsigned int truncates the count to 32 bits and would leave the unmap range incomplete if a walk ever failed pastUINT_MAXpages.
Sure, keeping them consistent does make sense.
I will hold this change and wait for more review comments on the rest of the patches.
Re: [PATCH v5 07/11] mm/vmalloc: undo what __vmap_pages_range_noflush() mapped on failure
In reply to Hao GeQuoted 165 lines · Hao Ge
__vmap_pages_range_noflush()maps a struct page array and may fail half way. The ptes installed so far stay behind, so a retry of the same range, the__GFP_NOFAILloop in__vmalloc_area_node()or the module tags populate, trips the WARN_ON(!pte_none()) invmap_pages_pte_range()and fails with-EBUSY.
vmap_pages_range_noflush_walk()now reports how many pages it installed through its running index into the pages array, and__vmap_pages_range_noflush()undoes [addr, addr +mapped_pages*PAGE_SIZE) on failure.vmap_pages_pte_range()fails with-EBUSYinstead of installing over a pte that is already set, so the ptes below that point are all ours, while a pte above it can only have been put there by somebody else, for example when the caller's range overlaps an existing mapping. Going further would clear ptes that are not ours.This path can run with a non-blocking gfp mask, the undo passes
gfpflags_allow_blocking()ascan_resched.Suggested-by Suren Baghdasaryan <[email protected]>Suggested-by Uladzislau Rezki <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/kmsan/hooks.c | 4 ++-- mm/kmsan/shadow.c | 4 ++-- mm/vmalloc.c | 36 +++++++++++++++++++++++++++++------- mm/vmalloc.h | 3 ++- 4 files changed, 35 insertions(+), 12 deletions(-)mm/kmsan/hooks.c +2
-2@@ -208,7 +208,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;@@ -218,7 +218,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@@ -247,7 +247,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;@@ -255,7 +255,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 +28
-6@@ -725,7 +725,8 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,* may have contiguous chunks, as hinted by @shift. */ static int vmap_pages_range_noflush_walk (unsigned long addr,unsigned long end, -pgprot_t prot,struct page **pages,unsigned int shift) +pgprot_t prot,struct page **pages,unsigned int shift, +unsigned int *mapped_pages) {unsigned long start = addr;pgd_t *pgd;@@ -748,6 +749,7 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,if (mask &ARCH_PAGE_TABLE_SYNC_MASK )arch_sync_kernel_mappings (start, end); + *mapped_pages = nr;return err; }@@ -758,18 +760,36 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,* The caller is responsible for calling flush_cache_vmap() after this * function returns successfully and before the addresses are accessed. * +* On failure the PTEs installed by this call are torn down before +* returning, so a retry sees a clean range; PTEs that were already +* there when the walk failed are left alone. When @gfp_mask cannot +* block, the teardown skips cond_resched(), it may run in atomic +* context. +* * This is an internal function only. Do not use outside mm/. */ int __vmap_pages_range_noflush (unsigned long addr,unsigned long end, -pgprot_t prot,struct page **pages,unsigned int page_shift) +pgprot_t prot,struct page **pages,unsigned int page_shift, +gfp_t gfp_mask) { +int err; +unsigned int mapped_pages =0 ; +if (WARN_ON_ONCE (page_shift <PAGE_SHIFT ))return -EINVAL ;if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMALLOC )) page_shift =PAGE_SHIFT ; -return vmap_pages_range_noflush_walk (addr, end, prot, pages, page_shift); + err =vmap_pages_range_noflush_walk (addr, end, prot, pages, page_shift, + &mapped_pages); + +/* Undo the PTEs installed before the failure. */ +if (err && mapped_pages) +__vunmap_range_noflush (addr, addr + (mapped_pages *PAGE_SIZE ), +gfpflags_allow_blocking (gfp_mask)); + +return err; } int vmap_pages_range_noflush(unsigned long addr, unsigned long end,@@ -781,7 +801,8 @@ int vmap_pages_range_noflush(unsigned long addr, unsigned long end,if (ret)return ret; -return __vmap_pages_range_noflush (addr, end, prot, pages, page_shift); +return __vmap_pages_range_noflush (addr, end, prot, pages, page_shift, + gfp_mask); } static int __vmap_pages_range(unsigned long addr, unsigned long end,@@ -3715,7 +3736,7 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages) {const unsigned int nr_pages = (end - addr) >>PAGE_SHIFT ; -unsigned int prev_shift =0 , batch_idx =0 ; +unsigned int prev_shift =0 , batch_idx =0 , nr_mapped;unsigned long batch_start = addr, batch_end = addr;int err;@@ -3734,7 +3755,8 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,if (shift != prev_shift) { err =vmap_pages_range_noflush_walk (batch_start, batch_end, - prot, pages + batch_idx, prev_shift); + prot, pages + batch_idx, prev_shift, + &nr_mapped);if (err) goto out; prev_shift = shift;
mm/vmalloc.c static int vmap_pages_range_batched(unsigned long addr, unsigned long end, · line 3784
Quoted 8 lines
mm/vmalloc.c +1
-1@@ -3756,7 +3778,7 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,/* Remaining */ if (batch_start < end) err =vmap_pages_range_noflush_walk (batch_start, end, prot, - pages + batch_idx, prev_shift); + pages + batch_idx, prev_shift, &nr_mapped); out:if (!err)
So, you are recording nr_mapped but not undoing vmap_pages_range_batched() partial mappings yet. You do that in patch #9. It's weird to fix one place but leave the other one until later. I would suggest merging patches 7 and 9 to have a complete fix.
Quoted 17 lines
mm/vmalloc.h +2
-2@@ -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); --2.25.1
Re: [PATCH v5 07/11] mm/vmalloc: undo what __vmap_pages_range_noflush() mapped on failure
Hi Suren
Quoted 172 lines · Suren Baghdasaryan
Quoted 171 lines · Hao Ge
__vmap_pages_range_noflush()maps a struct page array and may fail half way. The ptes installed so far stay behind, so a retry of the same range, the__GFP_NOFAILloop in__vmalloc_area_node()or the module tags populate, trips the WARN_ON(!pte_none()) invmap_pages_pte_range()and fails with-EBUSY.
vmap_pages_range_noflush_walk()now reports how many pages it installed through its running index into the pages array, and__vmap_pages_range_noflush()undoes [addr, addr +mapped_pages*PAGE_SIZE) on failure.vmap_pages_pte_range()fails with-EBUSYinstead of installing over a pte that is already set, so the ptes below that point are all ours, while a pte above it can only have been put there by somebody else, for example when the caller's range overlaps an existing mapping. Going further would clear ptes that are not ours.This path can run with a non-blocking gfp mask, the undo passes
gfpflags_allow_blocking()ascan_resched.Suggested-by Suren Baghdasaryan <[email protected]>Suggested-by Uladzislau Rezki <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/kmsan/hooks.c | 4 ++-- mm/kmsan/shadow.c | 4 ++-- mm/vmalloc.c | 36 +++++++++++++++++++++++++++++------- mm/vmalloc.h | 3 ++- 4 files changed, 35 insertions(+), 12 deletions(-)mm/kmsan/hooks.c +2
-2@@ -208,7 +208,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;@@ -218,7 +218,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@@ -247,7 +247,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;@@ -255,7 +255,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 +29
-7@@ -725,7 +725,8 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,* may have contiguous chunks, as hinted by @shift. */ static int vmap_pages_range_noflush_walk (unsigned long addr,unsigned long end, -pgprot_t prot,struct page **pages,unsigned int shift) +pgprot_t prot,struct page **pages,unsigned int shift, +unsigned int *mapped_pages) {unsigned long start = addr;pgd_t *pgd;@@ -748,6 +749,7 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,if (mask &ARCH_PAGE_TABLE_SYNC_MASK )arch_sync_kernel_mappings (start, end); + *mapped_pages = nr;return err; }@@ -758,18 +760,36 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,* The caller is responsible for calling flush_cache_vmap() after this * function returns successfully and before the addresses are accessed. * +* On failure the PTEs installed by this call are torn down before +* returning, so a retry sees a clean range; PTEs that were already +* there when the walk failed are left alone. When @gfp_mask cannot +* block, the teardown skips cond_resched(), it may run in atomic +* context. +* * This is an internal function only. Do not use outside mm/. */ int __vmap_pages_range_noflush (unsigned long addr,unsigned long end, -pgprot_t prot,struct page **pages,unsigned int page_shift) +pgprot_t prot,struct page **pages,unsigned int page_shift, +gfp_t gfp_mask) { +int err; +unsigned int mapped_pages =0 ; +if (WARN_ON_ONCE (page_shift <PAGE_SHIFT ))return -EINVAL ;if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMALLOC )) page_shift =PAGE_SHIFT ; -return vmap_pages_range_noflush_walk (addr, end, prot, pages, page_shift); + err =vmap_pages_range_noflush_walk (addr, end, prot, pages, page_shift, + &mapped_pages); + +/* Undo the PTEs installed before the failure. */ +if (err && mapped_pages) +__vunmap_range_noflush (addr, addr + (mapped_pages *PAGE_SIZE ), +gfpflags_allow_blocking (gfp_mask)); + +return err; } int vmap_pages_range_noflush(unsigned long addr, unsigned long end,@@ -781,7 +801,8 @@ int vmap_pages_range_noflush(unsigned long addr, unsigned long end,if (ret)return ret; -return __vmap_pages_range_noflush (addr, end, prot, pages, page_shift); +return __vmap_pages_range_noflush (addr, end, prot, pages, page_shift, + gfp_mask); } static int __vmap_pages_range(unsigned long addr, unsigned long end,@@ -3715,7 +3736,7 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages) {const unsigned int nr_pages = (end - addr) >>PAGE_SHIFT ; -unsigned int prev_shift =0 , batch_idx =0 ; +unsigned int prev_shift =0 , batch_idx =0 , nr_mapped;unsigned long batch_start = addr, batch_end = addr;int err;@@ -3734,7 +3755,8 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,if (shift != prev_shift) { err =vmap_pages_range_noflush_walk (batch_start, batch_end, - prot, pages + batch_idx, prev_shift); + prot, pages + batch_idx, prev_shift, + &nr_mapped);if (err) goto out; prev_shift = shift;@@ -3756,7 +3778,7 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,/* Remaining */ if (batch_start < end) err =vmap_pages_range_noflush_walk (batch_start, end, prot, - pages + batch_idx, prev_shift); + pages + batch_idx, prev_shift, &nr_mapped);
Quoted 7 lines
mm/vmalloc.c static int vmap_pages_range_batched(unsigned long addr, unsigned long end, · line 3784
Quoted 2 lines
mm/vmalloc.c +0
-0out:if (!err)So, you are recording
nr_mappedbut not undoingvmap_pages_range_batched()partial mappings yet. You do that in patch #9. It's weird to fix one place but leave the other one until later. I would suggest merging patches 7 and 9 to have a complete fix.
Yes, that looks correct when revisiting these patches.
Thanks
Best Regards
Hao
Quoted 18 lines
Quoted 18 lines
mm/vmalloc.h +2
-2@@ -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); --2.25.1
PATCH v5 8/11 mm/vmalloc: drop the KMSAN metadata in vmap_pages_range_noflush() on failure
In reply to Hao Gevmap_pages_range_noflush() sets up the KMSAN metadata for the whole range before it starts mapping. If the mapping fails, the page tables undo themselves now, but the metadata stays behind, and the next attempt to map the same range hits the leftover shadow PTEs.
Tear the metadata down on the error path, it was fully set up before the mapping failed. The teardown follows the page tables and skips cond_resched() when the caller's gfp mask cannot block.
--- mm/vmalloc.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
mm/vmalloc.c +12 -2
@@ -801,8 +801,18 @@ int vmap_pages_range_noflush(unsigned long addr, unsigned long end,if (ret)return ret; -return __vmap_pages_range_noflush (addr, end, prot, pages, page_shift, - gfp_mask); + + ret =__vmap_pages_range_noflush (addr, end, prot, pages, page_shift, + gfp_mask); +/* +* The page tables undo themselves on failure. Tear down the +* metadata that was fully set up before the mapping failed. +*/ +if (ret) +kmsan_vunmap_range_noflush (addr, end, +gfpflags_allow_blocking (gfp_mask)); + +return ret; } static int __vmap_pages_range(unsigned long addr, unsigned long end,
Signature · 1 line
2.25.1
Re: [PATCH v5 08/11] mm/vmalloc: drop the KMSAN metadata in vmap_pages_range_noflush() on failure
Quoted 9 lines · Hao Ge
vmap_pages_range_noflush()sets up the KMSAN metadata for the whole range before it starts mapping. If the mapping fails, the page tables undo themselves now, but the metadata stays behind, and the next attempt to map the same range hits the leftover shadow PTEs.Tear the metadata down on the error path, it was fully set up before the mapping failed. The teardown follows the page tables and skips
cond_resched()when the caller's gfp mask cannot block.
I think this patch should also be folded into patch 7. Leaving the metadata after unmapping a failed partial mapping introduces an inconsistency, which this patch fixes. So, in a sense you are fixing an inconsistency that you created in the patch 7. Instead of introducing an issue and then fixing it let's avoid introducing it in the first place. That's what merging those patches will achieve.
Quoted 34 lines
Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)mm/vmalloc.c +12
-3@@ -801,8 +801,18 @@ int vmap_pages_range_noflush(unsigned long addr, unsigned long end,if (ret)return ret; -return __vmap_pages_range_noflush (addr, end, prot, pages, page_shift, - gfp_mask); + + ret =__vmap_pages_range_noflush (addr, end, prot, pages, page_shift, + gfp_mask); +/* +* The page tables undo themselves on failure. Tear down the +* metadata that was fully set up before the mapping failed. +*/ +if (ret) +kmsan_vunmap_range_noflush (addr, end, +gfpflags_allow_blocking (gfp_mask)); + +return ret; } static int __vmap_pages_range(unsigned long addr, unsigned long end, --2.25.1
PATCH v5 9/11 mm/vmalloc: undo what vmap_pages_range_batched() mapped on failure
In reply to Hao Gevmap_pages_range_batched() maps the range batch by batch. When a batch fails, the batches before it are installed, and the failing one may have installed some of its pages. mapped_end follows the end of the last installed page, and the error path undoes [addr, mapped_end), never past the point where the walk stopped.
The KMSAN metadata is set up for the whole range up front, so it is torn down as a whole when the mapping fails, like in vmap_pages_range_noflush(). The only caller, vmap(), also calls vunmap() on its error path; the double teardown only walks over PTEs that are already cleared.
--- mm/vmalloc.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
mm/vmalloc.c +16 -6
@@ -3748,13 +3748,14 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,const unsigned int nr_pages = (end - addr) >>PAGE_SHIFT ;unsigned int prev_shift =0 , batch_idx =0 , nr_mapped;unsigned long batch_start = addr, batch_end = addr; +unsigned long mapped_end = addr;int err; err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages,PAGE_SHIFT ,GFP_KERNEL );if (err) - goto out; +return err;for (unsigned int i =0 ; i < nr_pages; ) { unsigned int shift = PAGE_SHIFT +
@@ -3764,9 +3765,10 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, prev_shift = shift;if (shift != prev_shift) { - err =vmap_pages_range_noflush_walk (batch_start, batch_end, - prot, pages + batch_idx, prev_shift, - &nr_mapped); + err =vmap_pages_range_noflush_walk (batch_start, + batch_end, prot, pages + batch_idx, + prev_shift, &nr_mapped); + mapped_end = batch_start + (nr_mapped *PAGE_SIZE );if (err) goto out; prev_shift = shift;
@@ -3786,13 +3788,21 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, }/* Remaining */ -if (batch_start < end) +if (batch_start < end) { err =vmap_pages_range_noflush_walk (batch_start, end, prot, pages + batch_idx, prev_shift, &nr_mapped); + mapped_end = batch_start + (nr_mapped *PAGE_SIZE ); + } out: -if (!err) +if (err) { +/* Undo the part the batches installed before they failed. */ +if (mapped_end > addr) +__vunmap_range_noflush (addr, mapped_end, true); +kmsan_vunmap_range_noflush (addr, end, true); + }else {flush_cache_vmap (addr, end); + }return err; }
Signature · 1 line
2.25.1
Re: [PATCH v5 09/11] mm/vmalloc: undo what vmap_pages_range_batched() mapped on failure
Quoted 7 lines · Hao Ge
vmap_pages_range_batched()maps the range batch by batch. When a batch fails, the batches before it are installed, and the failing one may have installed some of its pages.mapped_endfollows the end of the last installed page, and the error path undoes [addr,mapped_end), never past the point where the walk stopped.
Quoted 5 lines
The KMSAN metadata is set up for the whole range up front, so it is torn down as a whole when the mapping fails, like in
vmap_pages_range_noflush(). The only caller,vmap(), also callsvunmap()on its error path; the double teardown only walks over PTEs that are already cleared.
As I mentioned before I think this patch should be merged with patch 7.
Quoted 48 lines
Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)mm/vmalloc.c +7
-5@@ -3748,13 +3748,14 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,const unsigned int nr_pages = (end - addr) >>PAGE_SHIFT ;unsigned int prev_shift =0 , batch_idx =0 , nr_mapped;unsigned long batch_start = addr, batch_end = addr; +unsigned long mapped_end = addr;int err; err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages,PAGE_SHIFT ,GFP_KERNEL );if (err) - goto out; +return err;for (unsigned int i =0 ; i < nr_pages; ) { unsigned int shift = PAGE_SHIFT +@@ -3764,9 +3765,10 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, prev_shift = shift;if (shift != prev_shift) { - err =vmap_pages_range_noflush_walk (batch_start, batch_end, - prot, pages + batch_idx, prev_shift, - &nr_mapped); + err =vmap_pages_range_noflush_walk (batch_start, + batch_end, prot, pages + batch_idx, + prev_shift, &nr_mapped); + mapped_end = batch_start + (nr_mapped *PAGE_SIZE );if (err) goto out; prev_shift = shift;@@ -3786,13 +3788,21 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, }/* Remaining */ -if (batch_start < end) +if (batch_start < end) { err =vmap_pages_range_noflush_walk (batch_start, end, prot, pages + batch_idx, prev_shift, &nr_mapped);
mm/vmalloc.c static int vmap_pages_range_batched(unsigned long addr, unsigned long end, · line 3802
Quoted 10 lines
mm/vmalloc.c +7
-1+ mapped_end = batch_start + (nr_mapped *PAGE_SIZE ); + } out: -if (!err) +if (err) { +/* Undo the part the batches installed before they failed. */ +if (mapped_end > addr) +__vunmap_range_noflush (addr, mapped_end, true); +kmsan_vunmap_range_noflush (addr, end, true);
So, you are unmapping (addr, mapped_end) range but removing KMSAN metadata for the entire (addr, end) range. Is that intended?
Quoted 9 lines
mm/vmalloc.c +2
-1+ }else {flush_cache_vmap (addr, end); + }return err; } --2.25.1
Re: [PATCH v5 09/11] mm/vmalloc: undo what vmap_pages_range_batched() mapped on failure
Hi Suren
Quoted 8 lines · Suren Baghdasaryan
Quoted 7 lines · Hao Ge
vmap_pages_range_batched()maps the range batch by batch. When a batch fails, the batches before it are installed, and the failing one may have installed some of its pages.mapped_endfollows the end of the last installed page, and the error path undoes [addr,mapped_end), never past the point where the walk stopped.
Quoted 7 lines
Quoted 5 lines
The KMSAN metadata is set up for the whole range up front, so it is torn down as a whole when the mapping fails, like in
vmap_pages_range_noflush(). The only caller,vmap(), also callsvunmap()on its error path; the double teardown only walks over PTEs that are already cleared.As I mentioned before I think this patch should be merged with patch 7.
Ack.
Quoted 52 lines
Quoted 52 lines
Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)mm/vmalloc.c +9
-5@@ -3748,13 +3748,14 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,const unsigned int nr_pages = (end - addr) >>PAGE_SHIFT ;unsigned int prev_shift =0 , batch_idx =0 , nr_mapped;unsigned long batch_start = addr, batch_end = addr; +unsigned long mapped_end = addr;int err; err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages,PAGE_SHIFT ,GFP_KERNEL );if (err) - goto out; +return err;for (unsigned int i =0 ; i < nr_pages; ) { unsigned int shift = PAGE_SHIFT +@@ -3764,9 +3765,10 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, prev_shift = shift;if (shift != prev_shift) { - err =vmap_pages_range_noflush_walk (batch_start, batch_end, - prot, pages + batch_idx, prev_shift, - &nr_mapped); + err =vmap_pages_range_noflush_walk (batch_start, + batch_end, prot, pages + batch_idx, + prev_shift, &nr_mapped); + mapped_end = batch_start + (nr_mapped *PAGE_SIZE );if (err) goto out; prev_shift = shift;@@ -3786,13 +3788,21 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, }/* Remaining */ -if (batch_start < end) +if (batch_start < end) { err =vmap_pages_range_noflush_walk (batch_start, end, prot, pages + batch_idx, prev_shift, &nr_mapped); + mapped_end = batch_start + (nr_mapped *PAGE_SIZE ); + }
Quoted 10 lines
mm/vmalloc.c static int vmap_pages_range_batched(unsigned long addr, unsigned long end, · line 3802
Quoted 7 lines
mm/vmalloc.c +5
-1out: -if (!err) +if (err) { +/* Undo the part the batches installed before they failed. */ +if (mapped_end > addr) +__vunmap_range_noflush (addr, mapped_end, true); +kmsan_vunmap_range_noflush (addr, end, true);So, you are unmapping (addr,
mapped_end) range but removing KMSAN metadata for the entire (addr, end) range. Is that intended?
Yes, that is intended. The metadata for the whole range is set up before the mapping starts, kmsan_vmap_pages_range_noflush() at the top of vmap_pages_range_batched() installs the shadow and origin pages for the entire [addr, end) in one go. So on failure the metadata is fully installed and has to go as a whole, while the page tables only reached mapped_end and are undone up to there.
I know this looks somewhat awkward. The asymmetry comes from the metadata having to be set up before the mapping starts, I don't see a way to avoid it. Maybe add a comment explaining this?
WDYT?
Thanks
Best Regards
Hao
Quoted 10 lines
Quoted 10 lines
mm/vmalloc.c +2
-1+ }else {flush_cache_vmap (addr, end); + }return err; } --2.25.1
Re: [PATCH v5 09/11] mm/vmalloc: undo what vmap_pages_range_batched() mapped on failure
Quoted 93 lines · Hao Ge
Hi Suren
Quoted 8 lines · Suren Baghdasaryan
Quoted 7 lines · Hao Ge
vmap_pages_range_batched()maps the range batch by batch. When a batch fails, the batches before it are installed, and the failing one may have installed some of its pages.mapped_endfollows the end of the last installed page, and the error path undoes [addr,mapped_end), never past the point where the walk stopped.Quoted 7 lines
Quoted 5 lines
The KMSAN metadata is set up for the whole range up front, so it is torn down as a whole when the mapping fails, like in
vmap_pages_range_noflush(). The only caller,vmap(), also callsvunmap()on its error path; the double teardown only walks over PTEs that are already cleared.As I mentioned before I think this patch should be merged with patch 7.
Ack.
Quoted 52 lines
Quoted 52 lines
Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)mm/vmalloc.c +9
-5@@ -3748,13 +3748,14 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,const unsigned int nr_pages = (end - addr) >>PAGE_SHIFT ;unsigned int prev_shift =0 , batch_idx =0 , nr_mapped;unsigned long batch_start = addr, batch_end = addr; +unsigned long mapped_end = addr;int err; err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages,PAGE_SHIFT ,GFP_KERNEL );if (err) - goto out; +return err;for (unsigned int i =0 ; i < nr_pages; ) { unsigned int shift = PAGE_SHIFT +@@ -3764,9 +3765,10 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, prev_shift = shift;if (shift != prev_shift) { - err =vmap_pages_range_noflush_walk (batch_start, batch_end, - prot, pages + batch_idx, prev_shift, - &nr_mapped); + err =vmap_pages_range_noflush_walk (batch_start, + batch_end, prot, pages + batch_idx, + prev_shift, &nr_mapped); + mapped_end = batch_start + (nr_mapped *PAGE_SIZE );if (err) goto out; prev_shift = shift;@@ -3786,13 +3788,21 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, }/* Remaining */ -if (batch_start < end) +if (batch_start < end) { err =vmap_pages_range_noflush_walk (batch_start, end, prot, pages + batch_idx, prev_shift, &nr_mapped); + mapped_end = batch_start + (nr_mapped *PAGE_SIZE ); + }Quoted 10 lines
mm/vmalloc.c static int vmap_pages_range_batched(unsigned long addr, unsigned long end, · line 3802
Quoted 7 lines
mm/vmalloc.c +5
-1out: -if (!err) +if (err) { +/* Undo the part the batches installed before they failed. */ +if (mapped_end > addr) +__vunmap_range_noflush (addr, mapped_end, true); +kmsan_vunmap_range_noflush (addr, end, true);So, you are unmapping (addr,
mapped_end) range but removing KMSAN metadata for the entire (addr, end) range. Is that intended?Yes, that is intended. The metadata for the whole range is set up before the mapping starts,
kmsan_vmap_pages_range_noflush()at the top ofvmap_pages_range_batched()installs the shadow and origin pages for the entire [addr, end) in one go. So on failure the metadata is fully installed and has to go as a whole, while the page tables only reachedmapped_endand are undone up to there.
Quoted 3 lines
I know this looks somewhat awkward. The asymmetry comes from the metadata having to be set up before the mapping starts, I don't see a way to avoid it. Maybe add a comment explaining this?
Are there any side-effects like the one we found earlier with unmapping PTEs which we never installed?
I don't think a comment is needed as it's quite obvious.
Quoted 17 lines
WDYT?
Thanks
Best Regards
HaoQuoted 10 lines
Quoted 10 lines
mm/vmalloc.c +2
-1+ }else {flush_cache_vmap (addr, end); + }return err; } --2.25.1
Re: [PATCH v5 09/11] mm/vmalloc: undo what vmap_pages_range_batched() mapped on failure
Hi Suren
Quoted 94 lines · Suren Baghdasaryan
Quoted 93 lines · Hao Ge
Hi Suren
Quoted 8 lines · Suren Baghdasaryan
Quoted 7 lines · Hao Ge
vmap_pages_range_batched()maps the range batch by batch. When a batch fails, the batches before it are installed, and the failing one may have installed some of its pages.mapped_endfollows the end of the last installed page, and the error path undoes [addr,mapped_end), never past the point where the walk stopped.Quoted 7 lines
Quoted 5 lines
The KMSAN metadata is set up for the whole range up front, so it is torn down as a whole when the mapping fails, like in
vmap_pages_range_noflush(). The only caller,vmap(), also callsvunmap()on its error path; the double teardown only walks over PTEs that are already cleared.As I mentioned before I think this patch should be merged with patch 7.
Ack.
Quoted 52 lines
Quoted 52 lines
Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)mm/vmalloc.c +9
-5@@ -3748,13 +3748,14 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,const unsigned int nr_pages = (end - addr) >>PAGE_SHIFT ;unsigned int prev_shift =0 , batch_idx =0 , nr_mapped;unsigned long batch_start = addr, batch_end = addr; +unsigned long mapped_end = addr;int err; err =kmsan_vmap_pages_range_noflush (addr, end, prot, pages,PAGE_SHIFT ,GFP_KERNEL );if (err) - goto out; +return err;for (unsigned int i =0 ; i < nr_pages; ) { unsigned int shift = PAGE_SHIFT +@@ -3764,9 +3765,10 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, prev_shift = shift;if (shift != prev_shift) { - err =vmap_pages_range_noflush_walk (batch_start, batch_end, - prot, pages + batch_idx, prev_shift, - &nr_mapped); + err =vmap_pages_range_noflush_walk (batch_start, + batch_end, prot, pages + batch_idx, + prev_shift, &nr_mapped); + mapped_end = batch_start + (nr_mapped *PAGE_SIZE );if (err) goto out; prev_shift = shift;@@ -3786,13 +3788,21 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end, }/* Remaining */ -if (batch_start < end) +if (batch_start < end) { err =vmap_pages_range_noflush_walk (batch_start, end, prot, pages + batch_idx, prev_shift, &nr_mapped); + mapped_end = batch_start + (nr_mapped *PAGE_SIZE ); + }Quoted 10 lines
mm/vmalloc.c static int vmap_pages_range_batched(unsigned long addr, unsigned long end, · line 3802
Quoted 7 lines
mm/vmalloc.c +5
-1out: -if (!err) +if (err) { +/* Undo the part the batches installed before they failed. */ +if (mapped_end > addr) +__vunmap_range_noflush (addr, mapped_end, true); +kmsan_vunmap_range_noflush (addr, end, true);So, you are unmapping (addr,
mapped_end) range but removing KMSAN metadata for the entire (addr, end) range. Is that intended?Yes, that is intended. The metadata for the whole range is set up before the mapping starts,
kmsan_vmap_pages_range_noflush()at the top ofvmap_pages_range_batched()installs the shadow and origin pages for the entire [addr, end) in one go. So on failure the metadata is fully installed and has to go as a whole, while the page tables only reachedmapped_endand are undone up to there.
Quoted 6 lines
Quoted 3 lines
I know this looks somewhat awkward. The asymmetry comes from the metadata having to be set up before the mapping starts, I don't see a way to avoid it. Maybe add a comment explaining this?
Are there any side-effects like the one we found earlier with unmapping PTEs which we never installed?
I haven't found any so far. If I've missed a scenario, please do let me know.
I remain unsure whether kmsan_vunmap_range_noflush() needs a TLB flush - I already brought this up in the thread for patch 2.
Quoted 2 lines
I don't think a comment is needed as it's quite obvious.
OK.
Thanks again for your and Ulad'careful and patient review of this series. I recognise this version is a bit messy - I had a lot going on at the time, and I apologise for that.
I'll take that into account and keep improving.
Thanks
Best Regards
Hao
Quoted 17 lines
Quoted 17 lines
WDYT?
Thanks
Best Regards
HaoQuoted 10 lines
Quoted 10 lines
mm/vmalloc.c +2
-1+ }else {flush_cache_vmap (addr, end); + }return err; } --2.25.1
PATCH v5 10/11 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 v5 11/11 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