Skip to contentSkip to conversation

mm/vmalloc: fix up vmap error handling

PATCH Superseded Hao Ge 4 participants 20 messages Oct 26 – Nov 14

Subsystems (6)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Hao Ge

author
#0 lore raw

v3 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 BUG() in vmap_pte_range() for huge mappings and a warning on the small-page path.

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 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 is its disabled-KMSAN counterpart. Patches 3 to 7 clean up the range checks and error reporting: unaligned ranges are clamped or rejected, the callers get the real error code instead of a flat -ENOMEM, and the cache is only flushed once the mapping succeeded. Patch 8 adds the rollback
to __vmap_pages_range_noflush() and vmap_page_range(). Patches 9 and 10 drop the powerpc and percpu cleanups that are not needed anymore.

Changes since v2:
- new patches 3 to 7 fix the range checks and the error reporting; the empty range check of v2's patch 3 moved into patch 6
- patch 8 (was patch 3) skips cond_resched() in the rollback when the caller's gfp mask cannot block
- commit messages reworded

[1] https://lore.kernel.org/all/[email protected]/

v2 link: https://lore.kernel.org/all/[email protected]/
v1 link: https://lore.kernel.org/all/[email protected]/

Hao Ge (10):
mm/kmsan: undo the shadow mapping when the origin mapping fails mm/kmsan: skip vunmap_range_noflush when KMSAN is disabled mm/vmalloc: clamp an unaligned range in vunmap_pte_range() mm/vmalloc: propagate errors in vmap_pages_p*d_range() mm/vmalloc: reject an unaligned range in vmap_pages_pte_range() mm/vmalloc: reject empty and unaligned ranges in
__vmap_pages_range_noflush()
mm/vmalloc: only flush the cache after the vmap succeeded 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 +-
 include/linux/kmsan.h            |   8 +-
 mm/kmsan/hooks.c                 |  21 ++++--
 mm/kmsan/shadow.c                |   9 ++-
 mm/percpu-vm.c                   |   5 +-
 mm/vmalloc.c                     | 125 ++++++++++++++++++++++---------
 mm/vmalloc.h                     |   6 +-
 8 files changed, 127 insertions(+), 56 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v3 1/10 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.

Fixes 47ebd0310e89 ("mm: kmsan: handle alloc failures in kmsan_vmap_pages_range_noflush()")
---
 mm/kmsan/shadow.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/kmsan/shadow.c +4 -0
index 0c88d89bf0d6..2166086d3dc3 100644
@@ -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
author
#2 lore raw

PATCH v3 2/10 mm/kmsan: skip vunmap_range_noflush when KMSAN is disabled

In reply to Hao Ge

kmsan_vmap_pages_range_noflush() returns 0 when kmsan_enabled is false. The unmap side does not, so it still walks the shadow and origin page tables even though KMSAN never installed any metadata there.

Add the matching early return.

---
 mm/kmsan/hooks.c | 3 +++
 1 file changed, 3 insertions(+)
mm/kmsan/hooks.c +3 -0
index a706666db097..107f66841274 100644
@@ -138,6 +138,9 @@ static unsigned long vmalloc_origin(unsigned long addr)
 
 void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end)
 {
+	if (!kmsan_enabled)
+		return;
+
 	__vunmap_range_noflush(vmalloc_shadow(start), vmalloc_shadow(end));
 	__vunmap_range_noflush(vmalloc_origin(start), vmalloc_origin(end));
 	flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end));
Signature · 1 line
2.25.1
author
#3 lore raw

PATCH v3 3/10 mm/vmalloc: clamp an unaligned range in vunmap_pte_range()

In reply to Hao Ge

commit a739e6b557af ("mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned") made vmap_pte_range() warn and bail out when the size is not a multiple of PAGE_SIZE. That loop steps in page multiples and stops only at addr == end; with an unaligned size addr never lands on end, so the loop cannot terminate and walks off the end of the page table.

vunmap_pte_range() has the same loop without any guard, so an unaligned range would clear PTEs past end without ever stopping, walking off the end of the page table.

Add the check there too. The range is clamped down to the last full page rather than dropped, and the loop is skipped when nothing is left, so a buggy caller still gets its PTEs cleared as far as they go.

---
 mm/vmalloc.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/vmalloc.c +5 -0
index 44e0eb32708f..7131f60d1a8e 100644
@@ -393,6 +393,11 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 	pte_t ptent;
 	unsigned long size = PAGE_SIZE;
 
+	if (WARN_ON_ONCE(!PAGE_ALIGNED(end - addr)))
+		end = addr + PAGE_ALIGN_DOWN(end - addr);
+	if (end == addr)
+		return;
+
 	pte = pte_offset_kernel(pmd, addr);
 	lazy_mmu_mode_enable();
 
Signature · 1 line
2.25.1
author
#9 lore raw

PATCH v3 4/10 mm/vmalloc: propagate errors in vmap_pages_p*d_range()

In reply to Hao Ge

vmap_pages_pmd_range(), vmap_pages_pud_range() and vmap_pages_p4d_range() return -ENOMEM whenever the level below them fails. But vmap_pages_pte_range() can also return -EINVAL for a page without a valid pfn and -EBUSY for a pte that is already mapped. Pass the error up as-is instead, like the vmap_p*d_range() helpers for vmap_page_range() do.

---
 mm/vmalloc.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
mm/vmalloc.c +15 -9
index 7131f60d1a8e..d60b99b81ec9 100644
@@ -600,16 +600,18 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,
 {
 	pmd_t *pmd;
 	unsigned long next;
+	int err = 0;
 
 	pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
 	if (!pmd)
 		return -ENOMEM;
 	do {
 		next = pmd_addr_end(addr, end);
-		if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask))
-			return -ENOMEM;
+		err = vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask);
+		if (err)
+			break;
 	} while (pmd++, addr = next, addr != end);
-	return 0;
+	return err;
 }
 
 static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
@@ -618,16 +620,18 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
 {
 	pud_t *pud;
 	unsigned long next;
+	int err = 0;
 
 	pud = pud_alloc_track(&init_mm, p4d, addr, mask);
 	if (!pud)
 		return -ENOMEM;
 	do {
 		next = pud_addr_end(addr, end);
-		if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask))
-			return -ENOMEM;
+		err = vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask);
+		if (err)
+			break;
 	} while (pud++, addr = next, addr != end);
-	return 0;
+	return err;
 }
 
 static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
@@ -636,16 +640,18 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
 {
 	p4d_t *p4d;
 	unsigned long next;
+	int err = 0;
 
 	p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
 	if (!p4d)
 		return -ENOMEM;
 	do {
 		next = p4d_addr_end(addr, end);
-		if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask))
-			return -ENOMEM;
+		err = vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask);
+		if (err)
+			break;
 	} while (p4d++, addr = next, addr != end);
-	return 0;
+	return err;
 }
 
 static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
Signature · 1 line
2.25.1
author
#14 lore raw

PATCH v3 5/10 mm/vmalloc: reject an unaligned range in vmap_pages_pte_range()

In reply to Hao Ge

commit a739e6b557af ("mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned") made vmap_pte_range() warn and bail out when the size is not a multiple of PAGE_SIZE. Its loop only stops at addr == end and otherwise steps over it and maps over whatever follows the page table. vmap_pages_pte_range() has the same kind of loop but was never given the check.

Add it there as a backstop.

---
 mm/vmalloc.c | 3 +++
 1 file changed, 3 insertions(+)
mm/vmalloc.c +3 -0
index d60b99b81ec9..c0f33392c6ad 100644
@@ -557,6 +557,9 @@ static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr,
 	int err = 0;
 	pte_t *pte;
 
+	if (WARN_ON_ONCE(!PAGE_ALIGNED(end - addr)))
+		return -EINVAL;
+
 	/*
 	 * nr is a running index into the array which helps higher level
 	 * callers keep track of where we're up to.
Signature · 1 line
2.25.1
author
#15 lore raw

PATCH v3 6/10 mm/vmalloc: reject empty and unaligned ranges in __vmap_pages_range_noflush()

In reply to Hao Ge

__vmap_pages_range_noflush() expects a non-empty, page-aligned [addr, end) range but does not check it.

An empty range dies in the BUG_ON(addr >= end) of vmap_small_pages_range_noflush() on the small-page path, while the huge-page path maps nothing and returns 0.

An unaligned range is caught by vmap_pages_pte_range() on the small-page path, but the huge-page path misses it, because its chunks are always page-aligned, so the check in vmap_pte_range() passes, and the mapping just starts at the page boundary below addr.

Check both at the entry of __vmap_pages_range_noflush() and return -EINVAL.

---
 mm/vmalloc.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/vmalloc.c +6 -0
index c0f33392c6ad..4d74144dfaff 100644
@@ -698,6 +698,12 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end,
 {
 	unsigned int i, nr = (end - addr) >> PAGE_SHIFT;
 
+	if (WARN_ON_ONCE(addr >= end))
+		return -EINVAL;
+
+	if (WARN_ON_ONCE(!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(end)))
+		return -EINVAL;
+
 	if (WARN_ON_ONCE(page_shift < PAGE_SHIFT))
 		return -EINVAL;
 
Signature · 1 line
2.25.1
author
#16 lore raw

PATCH v3 7/10 mm/vmalloc: only flush the cache after the vmap succeeded

In reply to Hao Ge

vmap_page_range() and __vmap_pages_range() 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 | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
mm/vmalloc.c +9 -3
index 4d74144dfaff..96e15d6cfc6d 100644
@@ -359,11 +359,14 @@ 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);
-	return err;
+	if (err)
+		return err;
+
+	flush_cache_vmap(addr, end);
+	return 0;
 }
 
 int ioremap_page_range(unsigned long addr, unsigned long end,
@@ -745,8 +748,11 @@ 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);
+	if (err)
+		return err;
+
 	flush_cache_vmap(addr, end);
-	return err;
+	return 0;
 }
 
 /**
Signature · 1 line
2.25.1
author
#17 lore raw

PATCH v3 8/10 mm/vmalloc: undo partial mappings inside the mapping functions

In reply to Hao Ge

The vmap entry points can install some PTEs, then fail and leave the partial mapping behind.

The callers do not agree on who cleans it up. pcpu_map_pages() and kmsan_ioremap_page_range() roll back what they mapped, while vm_module_tags_populate() and the __GFP_NOFAIL retry loop in __vmalloc_area_node() expect a clean range from the mapping function. For them the leftovers stay mapped, and the next attempt to map the same range 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(), which only clears the PTEs. No TLB flush is needed, nothing has touched the mapping yet. vmap_pages_range_noflush() also drops the KMSAN metadata of the failed mapping.

The undo walk skips its cond_resched() when the caller's gfp mask cannot block: __vmalloc() with GFP_ATOMIC or GFP_NOWAIT can run in atomic context, where cond_resched() cannot schedule and its __might_resched() annotation fires on CONFIG_DEBUG_ATOMIC_SLEEP builds. __vunmap_range_noflush() takes a can_resched argument for that, which the sleepable callers pass as true.

---
 include/linux/kmsan.h |  8 +++--
 mm/kmsan/hooks.c      | 18 ++++++----
 mm/kmsan/shadow.c     |  7 ++--
 mm/vmalloc.c          | 78 ++++++++++++++++++++++++++++++-------------
 mm/vmalloc.h          |  6 ++--
 5 files changed, 79 insertions(+), 38 deletions(-)
include/linux/kmsan.h +6 -2
index 7da9fd506b39..0016c032d1e7 100644
@@ -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 +11 -7
index 107f66841274..0cd1edaa1120 100644
@@ -136,13 +136,16 @@ 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)
 {
 	if (!kmsan_enabled)
 		return;
 
-	__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));
 }
@@ -171,8 +174,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);
 }
@@ -207,7 +211,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;
@@ -217,7 +221,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 +4 -3
index 2166086d3dc3..452760a333e4 100644
@@ -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,12 +255,13 @@ 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. */
 		kmsan_enter_runtime();
-		__vunmap_range_noflush(shadow_start, shadow_end);
+		__vunmap_range_noflush(shadow_start, shadow_end,
+				       gfpflags_allow_blocking(gfp_mask));
 		kmsan_leave_runtime();
 		err = mapped;
 		goto ret;
mm/vmalloc.c +54 -24

163 lines not on this page · raw message

mm/vmalloc.h +4 -2
index d8372a4f65c4..9ea28845f713 100644
@@ -40,10 +40,12 @@ 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);
 
-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
author
#18 lore raw

PATCH v3 9/10 powerpc: drop redundant unmaps of failed vmap mappings

In reply to Hao Ge

vmap_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
index 5c064485197a..93029d85e6ea 100644
@@ -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
index e27342ef128b..f74269636d5e 100644
@@ -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
author
#19 lore raw

PATCH v3 10/10 mm/percpu: stop unmapping the CPU that failed to map

In reply to Hao Ge

__pcpu_map_pages() clears the PTEs it installed when it fails, so pcpu_map_pages() only has to tear down the CPUs it mapped before the failing one.

---
 mm/percpu-vm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/percpu-vm.c +3 -2
index 509d8901835c..59d3a336a6bd 100644
@@ -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
#4 lore raw

Re: [PATCH v3 03/10] mm/vmalloc: clamp an unaligned range in vunmap_pte_range()

In reply to Hao Ge
Quoted 3 lines · Hao Ge

commit a739e6b557af ("mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned") made vmap_pte_range() warn and bail out when the size is not a multiple of PAGE_SIZE. That loop steps in page

Why are we checking the alignment so late into vmap_pte_range? Why not simply do

if (!IS_ALIGNED(addr, PAGE_SIZE) || !IS_ALIGNED(end, PAGE_SIZE))

	return; (or return -EINVAL);

at the start of vmap_range_noflush and vunmap_range_noflush?

Quoted 35 lines

multiples and stops only at addr == end; with an unaligned size addr never lands on end, so the loop cannot terminate and walks off the end of the page table.

vunmap_pte_range() has the same loop without any guard, so an unaligned range would clear PTEs past end without ever stopping, walking off the end of the page table.

Add the check there too. The range is clamped down to the last full page rather than dropped, and the loop is skipped when nothing is left, so a buggy caller still gets its PTEs cleared as far as they go.

---
 mm/vmalloc.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/vmalloc.c +5 -0
index 44e0eb32708f..7131f60d1a8e 100644
@@ -393,6 +393,11 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 	pte_t ptent;
 	unsigned long size = PAGE_SIZE;
 
+	if (WARN_ON_ONCE(!PAGE_ALIGNED(end - addr)))
+		end = addr + PAGE_ALIGN_DOWN(end - addr);
+	if (end == addr)
+		return;
+
 	pte = pte_offset_kernel(pmd, addr);
 	lazy_mmu_mode_enable();
 
Reviewed-by on v3 4/10
#10 lore raw

Re: [PATCH v3 04/10] mm/vmalloc: propagate errors in vmap_pages_p*d_range()

In reply to Hao Ge
Quoted 9 lines · Hao Ge

vmap_pages_pmd_range(), vmap_pages_pud_range() and vmap_pages_p4d_range() return -ENOMEM whenever the level below them fails. But vmap_pages_pte_range() can also return -EINVAL for a page without a valid pfn and -EBUSY for a pte that is already mapped. Pass the error up as-is instead, like the vmap_p*d_range() helpers for vmap_page_range() do.

---

Fixes?

Quoted 73 lines
 mm/vmalloc.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
mm/vmalloc.c +15 -9
index 7131f60d1a8e..d60b99b81ec9 100644
@@ -600,16 +600,18 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,
 {
 	pmd_t *pmd;
 	unsigned long next;
+	int err = 0;
 
 	pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
 	if (!pmd)
 		return -ENOMEM;
 	do {
 		next = pmd_addr_end(addr, end);
-		if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask))
-			return -ENOMEM;
+		err = vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask);
+		if (err)
+			break;
 	} while (pmd++, addr = next, addr != end);
-	return 0;
+	return err;
 }
 
 static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
@@ -618,16 +620,18 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
 {
 	pud_t *pud;
 	unsigned long next;
+	int err = 0;
 
 	pud = pud_alloc_track(&init_mm, p4d, addr, mask);
 	if (!pud)
 		return -ENOMEM;
 	do {
 		next = pud_addr_end(addr, end);
-		if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask))
-			return -ENOMEM;
+		err = vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask);
+		if (err)
+			break;
 	} while (pud++, addr = next, addr != end);
-	return 0;
+	return err;
 }
 
 static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
@@ -636,16 +640,18 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
 {
 	p4d_t *p4d;
 	unsigned long next;
+	int err = 0;
 
 	p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
 	if (!p4d)
 		return -ENOMEM;
 	do {
 		next = p4d_addr_end(addr, end);
-		if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask))
-			return -ENOMEM;
+		err = vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask);
+		if (err)
+			break;
 	} while (p4d++, addr = next, addr != end);
-	return 0;
+	return err;
 }
 
 static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
author
#5 lore raw

Re: [PATCH v3 03/10] mm/vmalloc: clamp an unaligned range in vunmap_pte_range()

In reply to Dev Jain

Hi Dev

Thanks for your review.

Quoted 7 lines · Dev Jain
Quoted 3 lines · Hao Ge

commit a739e6b557af ("mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned") made vmap_pte_range() warn and bail out when the size is not a multiple of PAGE_SIZE. That loop steps in page

Quoted 7 lines

Why are we checking the alignment so late into vmap_pte_range? Why not simply do

if (!IS_ALIGNED(addr, PAGE_SIZE) || !IS_ALIGNED(end, PAGE_SIZE))

	return; (or return -EINVAL);

at the start of vmap_range_noflush and vunmap_range_noflush?

I think adding that check at the entry of vunmap_range_noflush() goes against its design intent. Its job is to tear down the mappings of the given VA range, so even when the caller passes a size that is not page-aligned, we still have to unmap whatever is mapped within that range.

So the patch clamps end down to the last full page and keeps clearing. A buggy caller still gets its mapping torn down as far as it goes, and nothing is left behind. The check here is just for protection.

This is just my preliminary understanding; I may be mistaken.

Thanks
Best Regards
Hao

But that's just my understanding, happy to be corrected.

Quoted 38 lines
Quoted 37 lines

multiples and stops only at addr == end; with an unaligned size addr never lands on end, so the loop cannot terminate and walks off the end of the page table.

vunmap_pte_range() has the same loop without any guard, so an unaligned range would clear PTEs past end without ever stopping, walking off the end of the page table.

Add the check there too. The range is clamped down to the last full page rather than dropped, and the loop is skipped when nothing is left, so a buggy caller still gets its PTEs cleared as far as they go.

---
 mm/vmalloc.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/vmalloc.c +5 -0
index 44e0eb32708f..7131f60d1a8e 100644
@@ -393,6 +393,11 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 	pte_t ptent;
 	unsigned long size = PAGE_SIZE;
 
+	if (WARN_ON_ONCE(!PAGE_ALIGNED(end - addr)))
+		end = addr + PAGE_ALIGN_DOWN(end - addr);
+	if (end == addr)
+		return;
+
 	pte = pte_offset_kernel(pmd, addr);
 	lazy_mmu_mode_enable();
 
author
#11 lore raw

Re: [PATCH v3 04/10] mm/vmalloc: propagate errors in vmap_pages_p*d_range()

In reply to Dev Jain

Hi Dev

Thanks for your review.

Quoted 10 lines · Dev Jain
Quoted 7 lines · Hao Ge

vmap_pages_pmd_range(), vmap_pages_pud_range() and vmap_pages_p4d_range() return -ENOMEM whenever the level below them fails. But vmap_pages_pte_range() can also return -EINVAL for a page without a valid pfn and -EBUSY for a pte that is already mapped. Pass the error up as-is instead, like the vmap_p*d_range() helpers for vmap_page_range() do.

Quoted 5 lines
Quoted 2 lines

---

Fixes?

Sorry, I missed that.

Quoted 1 line

Thanks! If you have some time, I'd also appreciate a review of the remaining patches in the series -- or Suren and Uladzislau, if they get a chance. No rush, I know everyone is busy.

Thanks
Best Regards
Hao

Quoted 76 lines
Quoted 75 lines

 mm/vmalloc.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
mm/vmalloc.c +15 -9
index 7131f60d1a8e..d60b99b81ec9 100644
@@ -600,16 +600,18 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,
 {
 	pmd_t *pmd;
 	unsigned long next;
+	int err = 0;
 
 	pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
 	if (!pmd)
 		return -ENOMEM;
 	do {
 		next = pmd_addr_end(addr, end);
-		if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask))
-			return -ENOMEM;
+		err = vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask);
+		if (err)
+			break;
 	} while (pmd++, addr = next, addr != end);
-	return 0;
+	return err;
 }
 
 static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
@@ -618,16 +620,18 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
 {
 	pud_t *pud;
 	unsigned long next;
+	int err = 0;
 
 	pud = pud_alloc_track(&init_mm, p4d, addr, mask);
 	if (!pud)
 		return -ENOMEM;
 	do {
 		next = pud_addr_end(addr, end);
-		if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask))
-			return -ENOMEM;
+		err = vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask);
+		if (err)
+			break;
 	} while (pud++, addr = next, addr != end);
-	return 0;
+	return err;
 }
 
 static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
@@ -636,16 +640,18 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
 {
 	p4d_t *p4d;
 	unsigned long next;
+	int err = 0;
 
 	p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
 	if (!p4d)
 		return -ENOMEM;
 	do {
 		next = p4d_addr_end(addr, end);
-		if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask))
-			return -ENOMEM;
+		err = vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask);
+		if (err)
+			break;
 	} while (p4d++, addr = next, addr != end);
-	return 0;
+	return err;
 }
 
 static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
#6 lore raw

Re: [PATCH v3 03/10] mm/vmalloc: clamp an unaligned range in vunmap_pte_range()

In reply to Hao Ge
Quoted 26 lines · Hao Ge

Hi Dev

Thanks for your review.

Quoted 7 lines · Dev Jain
Quoted 3 lines · Hao Ge

commit a739e6b557af ("mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned") made vmap_pte_range() warn and bail out when the size is not a multiple of PAGE_SIZE. That loop steps in page

Quoted 7 lines

Why are we checking the alignment so late into vmap_pte_range? Why not simply do

if (!IS_ALIGNED(addr, PAGE_SIZE) || !IS_ALIGNED(end, PAGE_SIZE))

	return; (or return -EINVAL);

at the start of vmap_range_noflush and vunmap_range_noflush?

I think adding that check at the entry of vunmap_range_noflush() goes against its design intent. Its job is to tear down the mappings of the given VA range, so even when the caller passes a size that is not page-aligned, we still have to unmap whatever is mapped within that range.

Quoted 6 lines

So the patch clamps end down to the last full page and keeps clearing. A buggy caller still gets its mapping torn down as far as it goes, and nothing is left behind. The check here is just for protection.

This is just my preliminary understanding; I may be mistaken.

Hmm I get you, but this is very ugly. We are trying to tolerate a blatant bug in the caller with not-so-nice code. But see below.

Quoted 38 lines

Thanks
Best Regards
Hao

But that's just my understanding, happy to be corrected.

Quoted 29 lines
Quoted 29 lines

multiples and stops only at addr == end; with an unaligned size addr never lands on end, so the loop cannot terminate and walks off the end of the page table.

vunmap_pte_range() has the same loop without any guard, so an unaligned range would clear PTEs past end without ever stopping, walking off the end of the page table.

Add the check there too. The range is clamped down to the last full page rather than dropped, and the loop is skipped when nothing is left, so a buggy caller still gets its PTEs cleared as far as they go.

---
 mm/vmalloc.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/vmalloc.c +0 -0
index 44e0eb32708f..7131f60d1a8e 100644
@@ -393,6 +393,11 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 	pte_t ptent;
 	unsigned long size = PAGE_SIZE;
 
Quoted 2 lines
Quoted 2 lines

mm/vmalloc.c static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · line 397

Quoted 2 lines
mm/vmalloc.c +2 -0
+	if (WARN_ON_ONCE(!PAGE_ALIGNED(end - addr)))
+		end = addr + PAGE_ALIGN_DOWN(end - addr);

For simplicity assume PAGE_SIZE is 4, start is 3, end is 10. With your code we will get

addr = 3, end = 3 + ALIGN_DOWN(10 - 3) = 3 + 4 = 7.
So the range becomes [3, 7) clearing only two ptes, the one covering [0, 3], the second covering [4, 7]. I feel we should rather make the range from [3, 10] to [0, 11], clearing three ptes.

So, at the start of vunmap_range_noflush we could basically WARN_ON on non-alignment, and then clamp the start down, and clamp the end up to PAGE_SIZE.

And with this also throw in a patch to bring the -EINVAL bailout in vmap_pte_range to the top, in vmap_range_noflush and check both start and end alignment.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
mm/vmalloc.c +3 -0
+	if (end == addr)
+		return;
+
 	pte = pte_offset_kernel(pmd, addr);
 	lazy_mmu_mode_enable();
 
#12 lore raw

Re: [PATCH v3 04/10] mm/vmalloc: propagate errors in vmap_pages_p*d_range()

In reply to Hao Ge
Quoted 22 lines · Hao Ge

Hi Dev

Thanks for your review.

Quoted 10 lines · Dev Jain
Quoted 7 lines · Hao Ge

vmap_pages_pmd_range(), vmap_pages_pud_range() and vmap_pages_p4d_range() return -ENOMEM whenever the level below them fails. But vmap_pages_pte_range() can also return -EINVAL for a page without a valid pfn and -EBUSY for a pte that is already mapped. Pass the error up as-is instead, like the vmap_p*d_range() helpers for vmap_page_range() do.

Quoted 5 lines
Quoted 2 lines

---

Fixes?

Quoted 7 lines

Sorry, I missed that.

Quoted 1 line

Thanks! If you have some time, I'd also appreciate a review of the remaining patches in the series -- or Suren and Uladzislau, if they get a chance. No rush, I know everyone is busy.

Yep, it's in my review queue.

Quoted 81 lines

Thanks
Best Regards
Hao

Quoted 76 lines
Quoted 75 lines

 mm/vmalloc.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
mm/vmalloc.c +15 -9
index 7131f60d1a8e..d60b99b81ec9 100644
@@ -600,16 +600,18 @@ static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,
 {
     pmd_t *pmd;
     unsigned long next;
+    int err = 0;
 
     pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
     if (!pmd)
             return -ENOMEM;
     do {
             next = pmd_addr_end(addr, end);
-            if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask))
-                    return -ENOMEM;
+            err = vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask);
+            if (err)
+                    break;
     } while (pmd++, addr = next, addr != end);
-    return 0;
+    return err;
 }
 
 static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
@@ -618,16 +620,18 @@ static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
 {
     pud_t *pud;
     unsigned long next;
+    int err = 0;
 
     pud = pud_alloc_track(&init_mm, p4d, addr, mask);
     if (!pud)
             return -ENOMEM;
     do {
             next = pud_addr_end(addr, end);
-            if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask))
-                    return -ENOMEM;
+            err = vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask);
+            if (err)
+                    break;
     } while (pud++, addr = next, addr != end);
-    return 0;
+    return err;
 }
 
 static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
@@ -636,16 +640,18 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
 {
     p4d_t *p4d;
     unsigned long next;
+    int err = 0;
 
     p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
     if (!p4d)
             return -ENOMEM;
     do {
             next = p4d_addr_end(addr, end);
-            if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask))
-                    return -ENOMEM;
+            err = vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask);
+            if (err)
+                    break;
     } while (p4d++, addr = next, addr != end);
-    return 0;
+    return err;
 }
 
 static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end,
author
#7 lore raw

Re: [PATCH v3 03/10] mm/vmalloc: clamp an unaligned range in vunmap_pte_range()

In reply to Dev Jain
Quoted 82 lines · Dev Jain
Quoted 26 lines · Hao Ge

Hi Dev

Thanks for your review.

Quoted 7 lines · Dev Jain
Quoted 3 lines · Hao Ge

commit a739e6b557af ("mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned") made vmap_pte_range() warn and bail out when the size is not a multiple of PAGE_SIZE. That loop steps in page

Quoted 7 lines

Why are we checking the alignment so late into vmap_pte_range? Why not simply do

if (!IS_ALIGNED(addr, PAGE_SIZE) || !IS_ALIGNED(end, PAGE_SIZE))

	return; (or return -EINVAL);

at the start of vmap_range_noflush and vunmap_range_noflush?

I think adding that check at the entry of vunmap_range_noflush() goes against its design intent. Its job is to tear down the mappings of the given VA range, so even when the caller passes a size that is not page-aligned, we still have to unmap whatever is mapped within that range.

Quoted 6 lines

So the patch clamps end down to the last full page and keeps clearing. A buggy caller still gets its mapping torn down as far as it goes, and nothing is left behind. The check here is just for protection.

This is just my preliminary understanding; I may be mistaken.

Hmm I get you, but this is very ugly. We are trying to tolerate a blatant bug in the caller with not-so-nice code. But see below.

Quoted 38 lines

Thanks
Best Regards
Hao

But that's just my understanding, happy to be corrected.

Quoted 29 lines
Quoted 29 lines

multiples and stops only at addr == end; with an unaligned size addr never lands on end, so the loop cannot terminate and walks off the end of the page table.

vunmap_pte_range() has the same loop without any guard, so an unaligned range would clear PTEs past end without ever stopping, walking off the end of the page table.

Add the check there too. The range is clamped down to the last full page rather than dropped, and the loop is skipped when nothing is left, so a buggy caller still gets its PTEs cleared as far as they go.

---
 mm/vmalloc.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/vmalloc.c +0 -0
index 44e0eb32708f..7131f60d1a8e 100644
@@ -393,6 +393,11 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 	pte_t ptent;
 	unsigned long size = PAGE_SIZE;
 
Quoted 2 lines
Quoted 2 lines

mm/vmalloc.c static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · line 397

Quoted 2 lines
mm/vmalloc.c +2 -0
+	if (WARN_ON_ONCE(!PAGE_ALIGNED(end - addr)))
+		end = addr + PAGE_ALIGN_DOWN(end - addr);

For simplicity assume PAGE_SIZE is 4, start is 3, end is 10. With your code we will get

Quoted 5 lines

addr = 3, end = 3 + ALIGN_DOWN(10 - 3) = 3 + 4 = 7.
So the range becomes [3, 7) clearing only two ptes, the one covering [0, 3], the second covering [4, 7]. I feel we should rather make the range from [3, 10] to [0, 11], clearing three ptes.

Thanks for the example. Looks like I nearly introduced a new bug while guarding against the old one, good thing you caught it.

Quoted 3 lines

So, at the start of vunmap_range_noflush we could basically WARN_ON on non-alignment, and then clamp the start down, and clamp the end up to PAGE_SIZE.

I'm wondering whether we should simply BUG_ON for non-aligned ranges here. Because the unmap could silently clear mappings at adjacent addresses, which may not even belong to the caller, and this kind of problem is really hard to track down afterwards. Say [0, 3) is in use by someone else, with PAGE_SIZE=4 an unmap starting at 3 still begins with the pte covering [0, 4), since the loop clears page by page, so that owner loses his mapping without doing anything wrong, and only sees the damage much later when he touches it again.

WDYT?

Quoted 4 lines

And with this also throw in a patch to bring the -EINVAL bailout in vmap_pte_range to the top, in vmap_range_noflush and check both start and end alignment.

For the vmap_range_noflush() check, fully agreed. The current check is indeed too late, the range is only found to be bad after part of it has already been mapped.

Looking forward to your valuable comments.

Thanks
Best Regards
Hao

Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
mm/vmalloc.c +3 -0
+	if (end == addr)
+		return;
+
 	pte = pte_offset_kernel(pmd, addr);
 	lazy_mmu_mode_enable();
 
maintainer
#13 lore raw

Re: [PATCH v3 04/10] mm/vmalloc: propagate errors in vmap_pages_p*d_range()

In reply to Suren Baghdasaryan
Quoted 26 lines · Suren Baghdasaryan
Quoted 24 lines · Hao Ge

Hi Dev

Thanks for your review.

Quoted 10 lines · Dev Jain
Quoted 7 lines · Hao Ge

vmap_pages_pmd_range(), vmap_pages_pud_range() and vmap_pages_p4d_range() return -ENOMEM whenever the level below them fails. But vmap_pages_pte_range() can also return -EINVAL for a page without a valid pfn and -EBUSY for a pte that is already mapped. Pass the error up as-is instead, like the vmap_p*d_range() helpers for vmap_page_range() do.

Quoted 5 lines
Quoted 2 lines

---

Fixes?

Sorry, I missed that.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Thanks! If you have some time, I'd also appreciate a review of the remaining patches in the series -- or Suren and Uladzislau, if they get a chance. No rush, I know everyone is busy.

Yep, it's in my review queue.

I see v4, so i will have a look!

Signature · 1 line
Uladzislau Rezki
#8 lore raw

Re: [PATCH v3 03/10] mm/vmalloc: clamp an unaligned range in vunmap_pte_range()

In reply to Hao Ge
Quoted 113 lines · Hao Ge
Quoted 82 lines · Dev Jain
Quoted 26 lines · Hao Ge

Hi Dev

Thanks for your review.

Quoted 7 lines · Dev Jain
Quoted 3 lines · Hao Ge

commit a739e6b557af ("mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned") made vmap_pte_range() warn and bail out when the size is not a multiple of PAGE_SIZE. That loop steps in page

Quoted 7 lines

Why are we checking the alignment so late into vmap_pte_range? Why not simply do

if (!IS_ALIGNED(addr, PAGE_SIZE) || !IS_ALIGNED(end, PAGE_SIZE))

	return; (or return -EINVAL);

at the start of vmap_range_noflush and vunmap_range_noflush?

I think adding that check at the entry of vunmap_range_noflush() goes against its design intent. Its job is to tear down the mappings of the given VA range, so even when the caller passes a size that is not page-aligned, we still have to unmap whatever is mapped within that range.

Quoted 6 lines

So the patch clamps end down to the last full page and keeps clearing. A buggy caller still gets its mapping torn down as far as it goes, and nothing is left behind. The check here is just for protection.

This is just my preliminary understanding; I may be mistaken.

Hmm I get you, but this is very ugly. We are trying to tolerate a blatant bug in the caller with not-so-nice code. But see below.

Quoted 38 lines

Thanks
Best Regards
Hao

But that's just my understanding, happy to be corrected.

Quoted 29 lines
Quoted 29 lines

multiples and stops only at addr == end; with an unaligned size addr never lands on end, so the loop cannot terminate and walks off the end of the page table.

vunmap_pte_range() has the same loop without any guard, so an unaligned range would clear PTEs past end without ever stopping, walking off the end of the page table.

Add the check there too. The range is clamped down to the last full page rather than dropped, and the loop is skipped when nothing is left, so a buggy caller still gets its PTEs cleared as far as they go.

---
 mm/vmalloc.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/vmalloc.c +0 -0
index 44e0eb32708f..7131f60d1a8e 100644
@@ -393,6 +393,11 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 	pte_t ptent;
 	unsigned long size = PAGE_SIZE;
 
Quoted 2 lines
Quoted 2 lines

mm/vmalloc.c static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · line 397

Quoted 2 lines
mm/vmalloc.c +2 -0
+	if (WARN_ON_ONCE(!PAGE_ALIGNED(end - addr)))
+		end = addr + PAGE_ALIGN_DOWN(end - addr);

For simplicity assume PAGE_SIZE is 4, start is 3, end is 10. With your code we will get

Quoted 5 lines

addr = 3, end = 3 + ALIGN_DOWN(10 - 3) = 3 + 4 = 7.
So the range becomes [3, 7) clearing only two ptes, the one covering [0, 3], the second covering [4, 7]. I feel we should rather make the range from [3, 10] to [0, 11], clearing three ptes.

Thanks for the example. Looks like I nearly introduced a new bug while guarding against the old one, good thing you caught it.

Quoted 3 lines

So, at the start of vunmap_range_noflush we could basically WARN_ON on non-alignment, and then clamp the start down, and clamp the end up to PAGE_SIZE.

I'm wondering whether we should simply BUG_ON for non-aligned ranges here. Because the unmap could silently clear mappings at adjacent addresses, which may not even belong to the caller, and this kind of problem is really hard to track down afterwards. Say [0, 3) is in use by someone else, with PAGE_SIZE=4 an unmap starting at 3 still begins with the pte covering [0, 4), since the loop clears page by page, so that owner loses his mapping without doing anything wrong, and only sees the damage much later when he touches it again.

WDYT?

Quoted 4 lines

And with this also throw in a patch to bring the -EINVAL bailout in vmap_pte_range to the top, in vmap_range_noflush and check both start and end alignment.

Quoted 5 lines

For the vmap_range_noflush() check, fully agreed. The current check is indeed too late, the range is only found to be bad after part of it has already been mapped.

Looking forward to your valuable comments.

I was on vacation, I'll review the next version soon.

Quoted 13 lines

Thanks
Best Regards
Hao

Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
mm/vmalloc.c +3 -0
+	if (end == addr)
+		return;
+
 	pte = pte_offset_kernel(pmd, addr);
 	lazy_mmu_mode_enable();