Skip to contentSkip to conversation

mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned

PATCH Superseded Yadong Qi 2 participants 3 messages Oct 9, 2025

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Yadong Qi

author
#0 lore raw

PATCH v3 1/1 mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned

In mm/vmalloc.c, the function vmap_pte_range() assumes that the mapping size is aligned to PAGE_SIZE. If this assumption is violated, the loop will become infinite because the termination condition (addr != end) will never be met. This can lead to overwriting other VA ranges and/or random pages physically follow the page table.

It's the caller's responsibility to ensure that the mapping size is aligned to PAGE_SIZE. However, the memory corruption is hard to root cause. To identify the programming error in the caller easier, check whether the mapping size is PAGE_SIZE aligned with WARN_ON().

---
v2 -> v3:
* change error code from ENOMEM to EINVAL
* modify callers of vmap_pte_range to handle return code
v1 -> v2:
* Use WARN_ON instead of BUG_ON

---
 mm/vmalloc.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
mm/vmalloc.c +18 -11
index 5edd536ba9d2..1fa52f203795 100644
@@ -100,6 +100,9 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 	struct page *page;
 	unsigned long size = PAGE_SIZE;
 
+	if (WARN_ON(!PAGE_ALIGNED(end - addr)))
+		return -EINVAL;
+
 	pfn = phys_addr >> PAGE_SHIFT;
 	pte = pte_alloc_kernel_track(pmd, addr, mask);
 	if (!pte)
@@ -167,6 +170,7 @@ static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
 {
 	pmd_t *pmd;
 	unsigned long next;
+	int err;
 
 	pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
 	if (!pmd)
@@ -180,10 +184,11 @@ static int vmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
 			continue;
 		}
 
-		if (vmap_pte_range(pmd, addr, next, phys_addr, prot, max_page_shift, mask))
-			return -ENOMEM;
+		err = vmap_pte_range(pmd, addr, next, phys_addr, prot, max_page_shift, mask);
+		if (err)
+			break;
 	} while (pmd++, phys_addr += (next - addr), addr = next, addr != end);
-	return 0;
+	return err;
 }
 
 static int vmap_try_huge_pud(pud_t *pud, unsigned long addr, unsigned long end,
@@ -217,6 +222,7 @@ static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
 {
 	pud_t *pud;
 	unsigned long next;
+	int err;
 
 	pud = pud_alloc_track(&init_mm, p4d, addr, mask);
 	if (!pud)
@@ -230,11 +236,11 @@ static int vmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
 			continue;
 		}
 
-		if (vmap_pmd_range(pud, addr, next, phys_addr, prot,
-					max_page_shift, mask))
-			return -ENOMEM;
+		err = vmap_pmd_range(pud, addr, next, phys_addr, prot, max_page_shift, mask);
+		if (err)
+			break;
 	} while (pud++, phys_addr += (next - addr), addr = next, addr != end);
-	return 0;
+	return err;
 }
 
 static int vmap_try_huge_p4d(p4d_t *p4d, unsigned long addr, unsigned long end,
@@ -268,6 +274,7 @@ static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
 {
 	p4d_t *p4d;
 	unsigned long next;
+	int err;
 
 	p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
 	if (!p4d)
@@ -281,11 +288,11 @@ static int vmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
 			continue;
 		}
 
-		if (vmap_pud_range(p4d, addr, next, phys_addr, prot,
-					max_page_shift, mask))
-			return -ENOMEM;
+		err = vmap_pud_range(p4d, addr, next, phys_addr, prot, max_page_shift, mask);
+		if (err)
+			break;
 	} while (p4d++, phys_addr += (next - addr), addr = next, addr != end);
-	return 0;
+	return err;
 }
 
 static int vmap_range_noflush(unsigned long addr, unsigned long end,
Signature · 1 line
2.43.5
maintainer
#1 lore raw
Quoted 30 lines · Yadong Qi

In mm/vmalloc.c, the function vmap_pte_range() assumes that the mapping size is aligned to PAGE_SIZE. If this assumption is violated, the loop will become infinite because the termination condition (addr != end) will never be met. This can lead to overwriting other VA ranges and/or random pages physically follow the page table.

It's the caller's responsibility to ensure that the mapping size is aligned to PAGE_SIZE. However, the memory corruption is hard to root cause. To identify the programming error in the caller easier, check whether the mapping size is PAGE_SIZE aligned with WARN_ON().

---
v2 -> v3:
* change error code from ENOMEM to EINVAL
* modify callers of vmap_pte_range to handle return code
v1 -> v2:
* Use WARN_ON instead of BUG_ON

---
 mm/vmalloc.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
mm/vmalloc.c +0 -0
index 5edd536ba9d2..1fa52f203795 100644

mm/vmalloc.c static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · line 104

Quoted 5 lines
mm/vmalloc.c +1 -0
@@ -100,6 +100,9 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
 	struct page *page;
 	unsigned long size = PAGE_SIZE;
 
+	if (WARN_ON(!PAGE_ALIGNED(end - addr)))
 

And it might be worth to use WARN_ON_ONCE() otherwise there is a risk that a kernel buffer would contain only such warnings.

--
Uladzislau Rezki

#2 lore raw
Quoted 41 lines

-----Original Message-----
From: Uladzislau Rezki <[email protected]>
Sent: 2025年10月9日 17:46
To: Yadong Qi <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v3] mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned

mm/vmalloc.c static int vmap_pte_range(pmd_t *pmd, unsigned long

Quoted 30 lines · Yadong Qi

In mm/vmalloc.c, the function vmap_pte_range() assumes that the mapping size is aligned to PAGE_SIZE. If this assumption is violated, the loop will become infinite because the termination condition (addr != end) will never be met. This can lead to overwriting other VA ranges and/or random pages physically follow the page table.

It's the caller's responsibility to ensure that the mapping size is aligned to PAGE_SIZE. However, the memory corruption is hard to root cause. To identify the programming error in the caller easier, check whether the mapping size is PAGE_SIZE aligned with WARN_ON().

---
v2 -> v3:
* change error code from ENOMEM to EINVAL
* modify callers of vmap_pte_range to handle return code
v1 -> v2:
* Use WARN_ON instead of BUG_ON

---
 mm/vmalloc.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
mm/vmalloc.c +0 -0
index 5edd536ba9d2..1fa52f203795 100644
@@ -100,6 +100,9 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long

addr, unsigned long end,

Quoted 8 lines

mm/vmalloc.c static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,

Quoted 5 lines
mm/vmalloc.c +1 -0
 	struct page *page;
 	unsigned long size = PAGE_SIZE;
 
+	if (WARN_ON(!PAGE_ALIGNED(end - addr)))
 

And it might be worth to use WARN_ON_ONCE() otherwise there is a risk that a kernel buffer would contain only such warnings.

Sure, will change in next version.

Best Regard
Yadong