Skip to contentSkip to conversation

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

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

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Yadong Qi

author
#0 lore raw

PATCH v2 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().

---
v1 -> v2:
* Use WARN_ON instead of BUG_ON

---
 mm/vmalloc.c | 3 +++
 1 file changed, 3 insertions(+)
mm/vmalloc.c +3 -0
index 5edd536ba9d2..2cad593e4677 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 -ENOMEM;
+
 	pfn = phys_addr >> PAGE_SHIFT;
 	pte = pte_alloc_kernel_track(pmd, addr, mask);
 	if (!pte)
Signature · 1 line
2.43.5
#1 lore raw
Quoted 27 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().

---
v1 -> v2:
* Use WARN_ON instead of BUG_ON

---
 mm/vmalloc.c | 3 +++
 1 file changed, 3 insertions(+)
mm/vmalloc.c +0 -0
index 5edd536ba9d2..2cad593e4677 100644

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

Quoted 6 lines
mm/vmalloc.c +3 -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)))
+		return -ENOMEM;
+

EINVAL?

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

Quoted 3 lines
mm/vmalloc.c +0 -0
 	pfn = phys_addr >> PAGE_SHIFT;
 	pte = pte_alloc_kernel_track(pmd, addr, mask);
 	if (!pte)

---
Best Regards,
Huang, Ying

maintainer
#2 lore raw
Quoted 32 lines · Ying
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().

---
v1 -> v2:
* Use WARN_ON instead of BUG_ON

---
 mm/vmalloc.c | 3 +++
 1 file changed, 3 insertions(+)
mm/vmalloc.c +0 -0
index 5edd536ba9d2..2cad593e4677 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;
 
Quoted 6 lines

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

Quoted 3 lines
mm/vmalloc.c +3 -0
+	if (WARN_ON(!PAGE_ALIGNED(end - addr)))
+		return -ENOMEM;
+

EINVAL?

If this errno gets returned to userspace somehow, programmer is going to wonder what was invalid about the arguments which the program passed to the kernel.

But either way, the callers of vmap_pte_range() should be audited, to verify that they take appropriate action when this happens.

#3 lore raw
Quoted 52 lines

-----Original Message-----
From: Andrew Morton <[email protected]>
Sent: 2025年10月9日 15:04
To: Huang, Ying <[email protected]>
Cc: Yadong Qi <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v2] mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned

Quoted 24 lines · Ying
Quoted 22 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().

---
v1 -> v2:
* Use WARN_ON instead of BUG_ON

---
 mm/vmalloc.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 5 lines

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

Quoted 5 lines
mm/vmalloc.c +0 -0
index 5edd536ba9d2..2cad593e4677 100644
@@ -100,6 +100,9 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long

addr, unsigned long end,

Quoted 9 lines

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

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

EINVAL?

Quoted 6 lines

If this errno gets returned to userspace somehow, programmer is going to wonder what was invalid about the arguments which the program passed to the kernel.

But either way, the callers of vmap_pte_range() should be audited, to verify that they take appropriate action when this happens.

I believe we should modify vmap_pmd_range(), vmap_pud_range(), and vmap_p4d_range()
to accept an error code from the callee instead of simply returning -ENOMEM. If there are no other suggestions, I will create a new patch.