mm: vmalloc: BUG_ON if mapping size is not PAGE_SIZE aligned
Subsystems (2)
PATCH 1/1 mm: vmalloc: BUG_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 BUG_ON().
--- mm/vmalloc.c | 2 ++ 1 file changed, 2 insertions(+)
mm/vmalloc.c +2 -0
@@ -100,6 +100,8 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,struct page *page;unsigned long size =PAGE_SIZE ; +BUG_ON (!PAGE_ALIGNED (end - addr)); + pfn = phys_addr >>PAGE_SHIFT ; pte =pte_alloc_kernel_track (pmd, addr, mask);if (!pte)
Signature · 1 line
2.43.5
Andrew Morton
maintainerQuoted 18 lines · On Thu, 9 Oct 2025 11:59:43 +0800 Yadong Qi <[email protected]>…
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 withBUG_ON()...
mm/vmalloc.c static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · line 107
Quoted 8 lines
mm/vmalloc.c +2
-0@@ -100,6 +100,8 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,struct page *page;unsigned long size =PAGE_SIZE ; +BUG_ON (!PAGE_ALIGNED (end - addr)); + pfn = phys_addr >>PAGE_SHIFT ; pte =pte_alloc_kernel_track (pmd, addr, mask);if (!pte)
We try to avoid adding BUG()s - deliberately crashing the kernel is pretty cruel to the user. It's far better to WARN and to continue in some fashion so the user can at least gather logs, etc.
How about
if (WARN_ON(!PAGE_ALIGNED(end - addr))) return -ENOMEM;
?
(Or VM_WARN_ON)
Quoted 31 lines
Quoted 18 lines
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 withBUG_ON()...
mm/vmalloc.c static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · line 107
Quoted 8 lines
mm/vmalloc.c +2
-0@@ -100,6 +100,8 @@ static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,struct page *page;unsigned long size =PAGE_SIZE ; +BUG_ON (!PAGE_ALIGNED (end - addr)); + pfn = phys_addr >>PAGE_SHIFT ; pte =pte_alloc_kernel_track (pmd, addr, mask);if (!pte)We try to avoid adding BUG()s - deliberately crashing the kernel is pretty cruel to the user. It's far better to WARN and to continue in some fashion so the user can at least gather logs, etc.
Quoted 8 lines
How about
if (WARN_ON(!PAGE_ALIGNED(end - addr))) return -ENOMEM;?
(Or
VM_WARN_ON)
I agree with Andrew. Using WARN_ON/VM_WARN_ON and returning an error is the way to go.
AFAIK, we are moving away from BUG_ON() in MM whenever an error can be handled gracefully.
Cheers,
Lance
Quoted 44 lines
-----Original Message-----
From: Andrew Morton <[email protected]>
Sent: 2025年10月9日 12:17
To: Yadong Qi <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] mm: vmalloc:BUG_ONif mapping size is notPAGE_SIZEalignedmm/vmalloc.c static int vmap_pte_range(pmd_t *pmd, unsigned long
Quoted 18 lines · On Thu, 9 Oct 2025 11:59:43 +0800 Yadong Qi <[email protected]>…
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 withBUG_ON()...
mm/vmalloc.c +0
-0@@ -100,6 +100,8 @@ static int vmap_pte_range(pmd_t *pmd, unsigned longaddr, unsigned long end,
mm/vmalloc.c static int vmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, · line 107
Quoted 8 lines
mm/vmalloc.c +2
-0struct page *page;unsigned long size =PAGE_SIZE ; +BUG_ON (!PAGE_ALIGNED (end - addr)); + pfn = phys_addr >>PAGE_SHIFT ; pte =pte_alloc_kernel_track (pmd, addr, mask);if (!pte)We try to avoid adding BUG()s - deliberately crashing the kernel is pretty cruel to the user. It's far better to WARN and to continue in some fashion so the user can at least gather logs, etc.
Quoted 8 lines
How about
if (WARN_ON(!PAGE_ALIGNED(end - addr))) return -ENOMEM;?
(Or
VM_WARN_ON)
Sure, I will send a new patch with WARN_ON.
Best Regard
Yadong