mm: vmalloc: WARN_ON if mapping size is not PAGE_SIZE aligned
Subsystems (2)
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
@@ -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
Quoted 27 lines · Yadong Qi
In
mm/vmalloc.c, the functionvmap_pte_range()assumes that the mapping size is aligned toPAGE_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 isPAGE_SIZEaligned withWARN_ON().Signed-off-by Yadong Qi <[email protected]>Reviewed-by Huang Ying <[email protected]>---
v1 -> v2:
* UseWARN_ONinstead ofBUG_ON--- mm/vmalloc.c | 3 +++ 1 file changed, 3 insertions(+)mm/vmalloc.c +0
-0
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
-0pfn = phys_addr >>PAGE_SHIFT ; pte =pte_alloc_kernel_track (pmd, addr, mask);if (!pte)
---
Best Regards,
Huang, Ying
Andrew Morton
maintainerQuoted 32 lines · Ying
Quoted 30 lines · Yadong Qi
In
mm/vmalloc.c, the functionvmap_pte_range()assumes that the mapping size is aligned toPAGE_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 isPAGE_SIZEaligned withWARN_ON().Signed-off-by Yadong Qi <[email protected]>Reviewed-by Huang Ying <[email protected]>---
v1 -> v2:
* UseWARN_ONinstead ofBUG_ON--- mm/vmalloc.c | 3 +++ 1 file changed, 3 insertions(+)mm/vmalloc.c +0
-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 ;
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.
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_ONif mapping size is notPAGE_SIZEalignedQuoted 24 lines · Ying
Quoted 22 lines · Yadong Qi
In
mm/vmalloc.c, the functionvmap_pte_range()assumes that the mapping size is aligned toPAGE_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 isPAGE_SIZEaligned withWARN_ON().Signed-off-by Yadong Qi <[email protected]>Reviewed-by Huang Ying <[email protected]>---
v1 -> v2:
* UseWARN_ONinstead ofBUG_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@@ -100,6 +100,9 @@ static int vmap_pte_range(pmd_t *pmd, unsigned longaddr, 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
-0struct 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.