mm/vmalloc: make the mapping functions undo their partial mappings
Subsystems (6)
cover mm/vmalloc: make the mapping functions undo their partial mappings
__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 BUG() in vmap_pte_range() for huge mappings and a warning on the small-page path.
This moves the rollback into the mapping functions. A failed vmap leaves nothing mapped, and the callers no longer clean up after it.
Patch 1 is the KMSAN part. The shadow and the origin of a range are mapped by two separate calls, so the rollback there has to cover both. Patch 2 adds it to __vmap_pages_range_noflush() and vmap_page_range(). Patches 3 and 4 drop the powerpc and percpu cleanups that are not needed anymore.
Hao Ge (4):
mm/kmsan: undo the shadow mapping when the origin mapping fails mm/vmalloc: undo partial mappings inside the mapping functions
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 +-- mm/kmsan/shadow.c | 4 +++ mm/percpu-vm.c | 5 ++-- mm/vmalloc.c | 44 +++++++++++++++++++++++--------- 5 files changed, 42 insertions(+), 20 deletions(-)
Signature · 1 line
2.25.1
RFC PATCH 1/4 mm/kmsan: undo the shadow mapping when the origin mapping fails
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 BUG()s on the huge mapping path, or gets -EBUSY with a WARN_ON() on the small page one.
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 | 4 ++++ 1 file changed, 4 insertions(+)
mm/kmsan/shadow.c +4 -0
@@ -258,6 +258,10 @@ 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); +kmsan_leave_runtime (); err = mapped; goto ret; }
Signature · 1 line
2.25.1
RFC PATCH 2/4 mm/vmalloc: undo partial mappings inside the mapping functions
In reply to Hao Ge__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 BUG() in vmap_pte_range() for huge mappings and a warning on the small-page path.
After discussing with Suren and Ulad, we decided to put the rollback into the entry points of the vmap API rather than into every low-level helper. __vmap_pages_range_noflush() undoes the whole range it was asked to map when it fails, and vmap_page_range() undoes its range for the ioremap-style mappings.
The rollback is __vunmap_range_noflush(), it only clears the PTEs, no TLB flush, nothing has touched these mappings. vmap_pages_range_noflush() drops the KMSAN metadata when the data mapping fails, and an empty range bails out with -EINVAL now, which used to BUG_ON() on the small-page path and map nothing at all on the huge-page one.
--- mm/vmalloc.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-)
mm/vmalloc.c +32 -12
@@ -363,6 +363,9 @@ int vmap_page_range(unsigned long addr, unsigned long end,if (!err) err =kmsan_ioremap_page_range (addr, end, phys_addr, prot, ioremap_max_page_shift); +if (err) +__vunmap_range_noflush (addr, end); +return err; }
@@ -683,27 +686,35 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages, unsigned int page_shift) { unsigned int i, nr = (end - addr) >> PAGE_SHIFT; +unsigned long start = addr; +int err =0 ; + +if (WARN_ON_ONCE (addr >= end)) +return -EINVAL ;if (WARN_ON_ONCE (page_shift <PAGE_SHIFT ))return -EINVAL ;if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMALLOC ) || - page_shift ==PAGE_SHIFT ) -return vmap_small_pages_range_noflush (addr, end, prot, pages); - -for (i =0 ; i < nr; i +=1U << (page_shift -PAGE_SHIFT )) { -int err; - - err =vmap_range_noflush (addr, addr + (1UL << page_shift), + page_shift ==PAGE_SHIFT ) { + err =vmap_small_pages_range_noflush (addr, end, prot, pages); + }else { +for (i =0 ; i < nr; i +=1U << (page_shift -PAGE_SHIFT )) { + err =vmap_range_noflush (addr, addr + (1UL << page_shift),page_to_phys (pages[i]), prot, page_shift); -if (err) -return err; +if (err) +break ; - addr +=1UL << page_shift; + addr +=1UL << page_shift; + } } -return 0 ; +/* Undo the PTEs installed before the failure. */ +if (err) +__vunmap_range_noflush (start, end); + +return err; } int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
@@ -715,7 +726,16 @@ 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); + + ret =__vmap_pages_range_noflush (addr, end, prot, pages, page_shift); +/* +* 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); + +return ret; } static int __vmap_pages_range(unsigned long addr, unsigned long end,
Signature · 1 line
2.25.1
RFC PATCH 3/4 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
RFC PATCH 4/4 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
Re: [RFC PATCH 1/4] mm/kmsan: undo the shadow mapping when the origin mapping fails
In reply to Hao GeQuoted 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 BUG()s on the huge mapping path, or gets-EBUSYwith aWARN_ON()on the small page one.
Quoted 8 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.Fixes 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")Signed-off-by Hao Ge <[email protected]>
Quoted 22 lines
--- mm/kmsan/shadow.c | 4 ++++ 1 file changed, 4 insertions(+)mm/kmsan/shadow.c +4
-1@@ -258,6 +258,10 @@ 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); +kmsan_leave_runtime (); err = mapped; goto ret; } --2.25.1
Re: [RFC PATCH 2/4] mm/vmalloc: undo partial mappings inside the mapping functions
In reply to Hao GeQuoted 78 lines · Hao Ge
__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()andkmsan_ioremap_page_range()roll back what they mapped before the failure, whilevm_module_tags_populate()and the__GFP_NOFAILretry loop in__vmalloc_area_node()rely on the mapping functions cleaning up and do not call anything likevunmap_range()themselves. When the same range is mapped again, the attempt hits the leftovers and fails, with aBUG()invmap_pte_range()for huge mappings and a warning on the small-page path.After discussing with Suren and Ulad, we decided to put the rollback into the entry points of the vmap API rather than into every low-level helper.
__vmap_pages_range_noflush()undoes the whole range it was asked to map when it fails, andvmap_page_range()undoes its range for the ioremap-style mappings.The rollback is
__vunmap_range_noflush(), it only clears the PTEs, no TLB flush, nothing has touched these mappings.vmap_pages_range_noflush()drops the KMSAN metadata when the data mapping fails, and an empty range bails out with-EINVALnow, which used toBUG_ON()on the small-page path and map nothing at all on the huge-page one.Reported-by Sashiko <[email protected]>Suggested-by Uladyslau Rezki <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-)mm/vmalloc.c +15
-9@@ -363,6 +363,9 @@ int vmap_page_range(unsigned long addr, unsigned long end,if (!err) err =kmsan_ioremap_page_range (addr, end, phys_addr, prot, ioremap_max_page_shift); +if (err) +__vunmap_range_noflush (addr, end); +return err; }@@ -683,27 +686,35 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages, unsigned int page_shift) { unsigned int i, nr = (end - addr) >> PAGE_SHIFT; +unsigned long start = addr; +int err =0 ; + +if (WARN_ON_ONCE (addr >= end)) +return -EINVAL ;if (WARN_ON_ONCE (page_shift <PAGE_SHIFT ))return -EINVAL ;if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMALLOC ) || - page_shift ==PAGE_SHIFT ) -return vmap_small_pages_range_noflush (addr, end, prot, pages); - -for (i =0 ; i < nr; i +=1U << (page_shift -PAGE_SHIFT )) { -int err; - - err =vmap_range_noflush (addr, addr + (1UL << page_shift), + page_shift ==PAGE_SHIFT ) { + err =vmap_small_pages_range_noflush (addr, end, prot, pages); + }else { +for (i =0 ; i < nr; i +=1U << (page_shift -PAGE_SHIFT )) { + err =vmap_range_noflush (addr, addr + (1UL << page_shift),page_to_phys (pages[i]), prot, page_shift); -if (err) -return err; +if (err) +break ;
mm/vmalloc.c int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, · line 715
Quoted 10 lines
mm/vmalloc.c +5
-2- addr +=1UL << page_shift; + addr +=1UL << page_shift; + } } -return 0 ; +/* Undo the PTEs installed before the failure. */ +if (err) +__vunmap_range_noflush (start, end);
Would it be too complicated to track how much was actually mapped and unmap only that portion?
Quoted 26 lines
mm/vmalloc.c +12
-2+ +return err; } int vmap_pages_range_noflush(unsigned long addr, unsigned long end,@@ -715,7 +726,16 @@ 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); + + ret =__vmap_pages_range_noflush (addr, end, prot, pages, page_shift); +/* +* 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); + +return ret; } static int __vmap_pages_range(unsigned long addr, unsigned long end, --2.25.1
Re: [RFC PATCH 3/4] powerpc: drop redundant unmaps of failed vmap mappings
In reply to Hao GeQuoted 6 lines · Hao Ge
vmap_page_range()andioremap_page_range()clear the PTEs they installed when they fail, so thevunmap_range()inremap_isa_base()andioremap_phb()has nothing left to remove.Signed-off-by Hao Ge <[email protected]>
Quoted 40 lines
--- 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
-4@@ -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; } --2.25.1
Re: [RFC PATCH 4/4] mm/percpu: stop unmapping the CPU that failed to map
In reply to Hao GeQuoted 6 lines · Hao Ge
__pcpu_map_pages()clears the PTEs it installed when it fails, sopcpu_map_pages()only has to tear down the CPUs it mapped before the failing one.Signed-off-by Hao Ge <[email protected]>
Quoted 25 lines
--- mm/percpu-vm.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)mm/percpu-vm.c +3
-3@@ -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; --2.25.1
Uladzislau Rezki
maintainerRe: [RFC PATCH 2/4] mm/vmalloc: undo partial mappings inside the mapping functions
In reply to Suren BaghdasaryanQuoted 85 lines · Suren Baghdasaryan
Quoted 84 lines · Hao Ge
__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()andkmsan_ioremap_page_range()roll back what they mapped before the failure, whilevm_module_tags_populate()and the__GFP_NOFAILretry loop in__vmalloc_area_node()rely on the mapping functions cleaning up and do not call anything likevunmap_range()themselves. When the same range is mapped again, the attempt hits the leftovers and fails, with aBUG()invmap_pte_range()for huge mappings and a warning on the small-page path.After discussing with Suren and Ulad, we decided to put the rollback into the entry points of the vmap API rather than into every low-level helper.
__vmap_pages_range_noflush()undoes the whole range it was asked to map when it fails, andvmap_page_range()undoes its range for the ioremap-style mappings.The rollback is
__vunmap_range_noflush(), it only clears the PTEs, no TLB flush, nothing has touched these mappings.vmap_pages_range_noflush()drops the KMSAN metadata when the data mapping fails, and an empty range bails out with-EINVALnow, which used toBUG_ON()on the small-page path and map nothing at all on the huge-page one.Reported-by Sashiko <[email protected]>Suggested-by Uladyslau Rezki <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-)mm/vmalloc.c +17
-10@@ -363,6 +363,9 @@ int vmap_page_range(unsigned long addr, unsigned long end,if (!err) err =kmsan_ioremap_page_range (addr, end, phys_addr, prot, ioremap_max_page_shift); +if (err) +__vunmap_range_noflush (addr, end); +return err; }@@ -683,27 +686,35 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages, unsigned int page_shift) { unsigned int i, nr = (end - addr) >> PAGE_SHIFT; +unsigned long start = addr; +int err =0 ; + +if (WARN_ON_ONCE (addr >= end)) +return -EINVAL ;if (WARN_ON_ONCE (page_shift <PAGE_SHIFT ))return -EINVAL ;if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMALLOC ) || - page_shift ==PAGE_SHIFT ) -return vmap_small_pages_range_noflush (addr, end, prot, pages); - -for (i =0 ; i < nr; i +=1U << (page_shift -PAGE_SHIFT )) { -int err; - - err =vmap_range_noflush (addr, addr + (1UL << page_shift), + page_shift ==PAGE_SHIFT ) { + err =vmap_small_pages_range_noflush (addr, end, prot, pages); + }else { +for (i =0 ; i < nr; i +=1U << (page_shift -PAGE_SHIFT )) { + err =vmap_range_noflush (addr, addr + (1UL << page_shift),page_to_phys (pages[i]), prot, page_shift); -if (err) -return err; +if (err) +break ; - addr +=1UL << page_shift; + addr +=1UL << page_shift; + } }
Quoted 8 lines
mm/vmalloc.c int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, · line 715
Quoted 4 lines
mm/vmalloc.c +3
-1-return 0 ; +/* Undo the PTEs installed before the failure. */ +if (err) +__vunmap_range_noflush (start, end);Would it be too complicated to track how much was actually mapped and unmap only that portion?
I think it is possible to vunmap the range which is already unmapped. i.e. in this case it is better to no to track anything as soon as a mapping failure is a corner case. Also it is more reliable without extra track logic.
--
Uladzislau Rezki
Re: [RFC PATCH 3/4] powerpc: drop redundant unmaps of failed vmap mappings
In reply to Suren BaghdasaryanQuoted 46 lines · Suren Baghdasaryan
Quoted 6 lines · Hao Ge
vmap_page_range()andioremap_page_range()clear the PTEs they installed when they fail, so thevunmap_range()inremap_isa_base()andioremap_phb()has nothing left to remove.Signed-off-by Hao Ge <[email protected]>Reviewed-by Suren Baghdasaryan <[email protected]>Quoted 35 lines
--- 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 ; - }
Quoted 5 lines
Quoted 5 lines
arch/powerpc/kernel/pci_64.c +0
-1return (void __iomem *)addr; } --2.25.1
--
Uladzislau Rezki
Re: [RFC PATCH 4/4] mm/percpu: stop unmapping the CPU that failed to map
In reply to Suren BaghdasaryanQuoted 28 lines · Suren Baghdasaryan
Quoted 6 lines · Hao Ge
__pcpu_map_pages()clears the PTEs it installed when it fails, sopcpu_map_pages()only has to tear down the CPUs it mapped before the failing one.Signed-off-by Hao Ge <[email protected]>Reviewed-by Suren Baghdasaryan <[email protected]>Quoted 17 lines
--- mm/percpu-vm.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)mm/percpu-vm.c +1
-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 ;
Quoted 8 lines
Quoted 8 lines
mm/percpu-vm.c +2
-1+__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; --2.25.1
--
Uladzislau Rezki
Re: [RFC PATCH 2/4] mm/vmalloc: undo partial mappings inside the mapping functions
In reply to Uladzislau RezkiHi Uladzislau and Suren
Quoted 94 lines · Uladzislau Rezki
Quoted 85 lines · Suren Baghdasaryan
Quoted 84 lines · Hao Ge
__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()andkmsan_ioremap_page_range()roll back what they mapped before the failure, whilevm_module_tags_populate()and the__GFP_NOFAILretry loop in__vmalloc_area_node()rely on the mapping functions cleaning up and do not call anything likevunmap_range()themselves. When the same range is mapped again, the attempt hits the leftovers and fails, with aBUG()invmap_pte_range()for huge mappings and a warning on the small-page path.After discussing with Suren and Ulad, we decided to put the rollback into the entry points of the vmap API rather than into every low-level helper.
__vmap_pages_range_noflush()undoes the whole range it was asked to map when it fails, andvmap_page_range()undoes its range for the ioremap-style mappings.The rollback is
__vunmap_range_noflush(), it only clears the PTEs, no TLB flush, nothing has touched these mappings.vmap_pages_range_noflush()drops the KMSAN metadata when the data mapping fails, and an empty range bails out with-EINVALnow, which used toBUG_ON()on the small-page path and map nothing at all on the huge-page one.Reported-by Sashiko <[email protected]>Suggested-by Uladyslau Rezki <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-)mm/vmalloc.c +17
-10@@ -363,6 +363,9 @@ int vmap_page_range(unsigned long addr, unsigned long end,if (!err) err =kmsan_ioremap_page_range (addr, end, phys_addr, prot, ioremap_max_page_shift); +if (err) +__vunmap_range_noflush (addr, end); +return err; }@@ -683,27 +686,35 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages, unsigned int page_shift) { unsigned int i, nr = (end - addr) >> PAGE_SHIFT; +unsigned long start = addr; +int err =0 ; + +if (WARN_ON_ONCE (addr >= end)) +return -EINVAL ;if (WARN_ON_ONCE (page_shift <PAGE_SHIFT ))return -EINVAL ;if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMALLOC ) || - page_shift ==PAGE_SHIFT ) -return vmap_small_pages_range_noflush (addr, end, prot, pages); - -for (i =0 ; i < nr; i +=1U << (page_shift -PAGE_SHIFT )) { -int err; - - err =vmap_range_noflush (addr, addr + (1UL << page_shift), + page_shift ==PAGE_SHIFT ) { + err =vmap_small_pages_range_noflush (addr, end, prot, pages); + }else { +for (i =0 ; i < nr; i +=1U << (page_shift -PAGE_SHIFT )) { + err =vmap_range_noflush (addr, addr + (1UL << page_shift),page_to_phys (pages[i]), prot, page_shift); -if (err) -return err; +if (err) +break ; - addr +=1UL << page_shift; + addr +=1UL << page_shift; + } }Quoted 8 lines
mm/vmalloc.c int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, · line 715
Quoted 4 lines
mm/vmalloc.c +3
-1-return 0 ; +/* Undo the PTEs installed before the failure. */ +if (err) +__vunmap_range_noflush (start, end);Would it be too complicated to track how much was actually mapped and unmap only that portion?
Quoted 4 lines
I think it is possible to vunmap the range which is already unmapped. i.e. in this case it is better to no to track anything as soon as a mapping failure is a corner case. Also it is more reliable without extra track logic.
Thank you both.
I take note of Suren's concerns. The worst case is a single PMD where only the first page was installed, __vunmap_range_noflush() then loops 512 times to undo one PTE, plus 511 empty ones.
And tracking the exact progress would require vmap_pages_p4d/pud/pmd_range() to report how far they got. That also puts counting instructions on the successful mapping path, while the saving only pays off on the failure one.
So I agree with Ulad.
Thanks
Best Regards
Hao
Quoted 3 lines
--
Uladzislau Rezki
Re: [RFC PATCH 2/4] mm/vmalloc: undo partial mappings inside the mapping functions
Quoted 108 lines · Hao Ge
Hi Uladzislau and Suren
Quoted 94 lines · Uladzislau Rezki
Quoted 85 lines · Suren Baghdasaryan
Quoted 84 lines · Hao Ge
__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()andkmsan_ioremap_page_range()roll back what they mapped before the failure, whilevm_module_tags_populate()and the__GFP_NOFAILretry loop in__vmalloc_area_node()rely on the mapping functions cleaning up and do not call anything likevunmap_range()themselves. When the same range is mapped again, the attempt hits the leftovers and fails, with aBUG()invmap_pte_range()for huge mappings and a warning on the small-page path.After discussing with Suren and Ulad, we decided to put the rollback into the entry points of the vmap API rather than into every low-level helper.
__vmap_pages_range_noflush()undoes the whole range it was asked to map when it fails, andvmap_page_range()undoes its range for the ioremap-style mappings.The rollback is
__vunmap_range_noflush(), it only clears the PTEs, no TLB flush, nothing has touched these mappings.vmap_pages_range_noflush()drops the KMSAN metadata when the data mapping fails, and an empty range bails out with-EINVALnow, which used toBUG_ON()on the small-page path and map nothing at all on the huge-page one.Reported-by Sashiko <[email protected]>Suggested-by Uladyslau Rezki <[email protected]>Signed-off-by Hao Ge <[email protected]>--- mm/vmalloc.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-)mm/vmalloc.c +17
-10@@ -363,6 +363,9 @@ int vmap_page_range(unsigned long addr, unsigned long end,if (!err) err =kmsan_ioremap_page_range (addr, end, phys_addr, prot, ioremap_max_page_shift); +if (err) +__vunmap_range_noflush (addr, end); +return err; }@@ -683,27 +686,35 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages, unsigned int page_shift) { unsigned int i, nr = (end - addr) >> PAGE_SHIFT; +unsigned long start = addr; +int err =0 ; + +if (WARN_ON_ONCE (addr >= end)) +return -EINVAL ;if (WARN_ON_ONCE (page_shift <PAGE_SHIFT ))return -EINVAL ;if (!IS_ENABLED (CONFIG_HAVE_ARCH_HUGE_VMALLOC ) || - page_shift ==PAGE_SHIFT ) -return vmap_small_pages_range_noflush (addr, end, prot, pages); - -for (i =0 ; i < nr; i +=1U << (page_shift -PAGE_SHIFT )) { -int err; - - err =vmap_range_noflush (addr, addr + (1UL << page_shift), + page_shift ==PAGE_SHIFT ) { + err =vmap_small_pages_range_noflush (addr, end, prot, pages); + }else { +for (i =0 ; i < nr; i +=1U << (page_shift -PAGE_SHIFT )) { + err =vmap_range_noflush (addr, addr + (1UL << page_shift),page_to_phys (pages[i]), prot, page_shift); -if (err) -return err; +if (err) +break ; - addr +=1UL << page_shift; + addr +=1UL << page_shift; + } }Quoted 8 lines
mm/vmalloc.c int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, · line 715
Quoted 4 lines
mm/vmalloc.c +3
-1-return 0 ; +/* Undo the PTEs installed before the failure. */ +if (err) +__vunmap_range_noflush (start, end);Would it be too complicated to track how much was actually mapped and unmap only that portion?
Quoted 4 lines
I think it is possible to vunmap the range which is already unmapped. i.e. in this case it is better to no to track anything as soon as a mapping failure is a corner case. Also it is more reliable without extra track logic.
Thank you both.
I take note of Suren's concerns. The worst case is a single PMD where only the first page was installed,__vunmap_range_noflush()then loops 512 times to undo one PTE, plus 511 empty ones.
Quoted 5 lines
And tracking the exact progress would require vmap_pages_p4d/pud/pmd_range() to report how far they got. That also puts counting instructions on the successful mapping path, while the saving only pays off on the failure one.
So I agree with Ulad.
Got it. Makes sense.
Quoted 8 lines
Thanks
Best Regards
HaoQuoted 3 lines
--
Uladzislau Rezki