x86/mm: Improve alloc handling of phys_*_init()
Subsystems (3)
v4 cover x86/mm: Improve alloc handling of phys_*_init()
Hi folks,
See changelog + more context below.
tl;dr:
* Currently alloc_low_page() uses GFP_ATOMIC after boot, which may fail
* Those failures aren't currently handled by phys_pud_init() and similar functions.
* Those failures can happen during memory hotplug
So:
1. Add handling for those allocation failures (patches 1-3)
2. Use GFP_KERNEL instead of GFP_ATOMIC (patch 4)
Previous version here:
https://lore.kernel.org/all/[email protected]/
=== Changelog ===
v2:
- Switch from special-casing zero values to ERR_PTR()
- Add patch to move from GFP_ATOMIC -> GFP_KERNEL
- Move commentary out of the patch message and into this cover letter v3:
- Fix -Wint-conversion issues
v4:
- new patch: move 'paddr_last' usage into phys_{pud,pmd}_init() so the return from those functions is no longer needed.
- new patch: make phys_*_init() and their callers return int
I'm not sure if patch 2/4 ("Allow error returns ...") should be separate from patch 3/4 ("Handle alloc failure ..."), but it's easy enough to combine them if need be.
=== Background ===
We recently started observing these null pointer dereferences happening in practice (albeit quite rarely), triggered by allocation failures during virtio-mem hotplug.
We use virtio-mem quite heavily - adding/removing memory based on resource usage of customer workloads across a fleet of VMs - so it's somewhat expected that we have occasional allocation failures here, if we run out of memory before hotplug takes place.
We started seeing this bug after upgrading from 6.6.64 to 6.12.26, but there didn't appear to be relevant changes in the codepaths involved, so we figured the upgrade was triggering a latent issue.
The possibility for this issue was also pointed out a while back:
Quoted 5 lines
For
alloc_low_pages(), I noticed the callers don’t check for allocation failure. I'm a little surprised that there haven't been reports of the allocation failing, because these operations could result in a lot more pages getting allocated way past boot, and failure causes a NULL pointer dereference.
https://lore.kernel.org/all/[email protected]/
For completeness, here's an example stack trace we saw (on 6.12.26):
BUG: kernel NULL pointer dereference, address: 0000000000000000 .... Call Trace: <TASK> phys_pud_init+0xa0/0x390 phys_p4d_init+0x93/0x330 __kernel_physical_mapping_init+0xa1/0x370 kernel_physical_mapping_init+0xf/0x20 init_memory_mapping+0x1fa/0x430 arch_add_memory+0x2b/0x50 add_memory_resource+0xe6/0x260 add_memory_driver_managed+0x78/0xc0 virtio_mem_add_memory+0x46/0xc0 virtio_mem_sbm_plug_and_add_mb+0xa3/0x160 virtio_mem_run_wq+0x1035/0x16c0 process_one_work+0x17a/0x3c0 worker_thread+0x2c5/0x3f0 ? _raw_spin_unlock_irqrestore+0x9/0x30 ? __pfx_worker_thread+0x10/0x10 kthread+0xdc/0x110 ? __pfx_kthread+0x10/0x10 ret_from_fork+0x35/0x60 ? __pfx_kthread+0x10/0x10 ret_from_fork_asm+0x1a/0x30 </TASK>
and the allocation failure preceding it:
kworker/0:2: page allocation failure: order:0, mode:0x920(GFP_ATOMIC|__GFP_ZERO), nodemask=(null),cpuset=/,mems_allowed=0
...
Call Trace: <TASK> dump_stack_lvl+0x5b/0x70 dump_stack+0x10/0x20 warn_alloc+0x103/0x180 __alloc_pages_slowpath.constprop.0+0x738/0xf30 __alloc_pages_noprof+0x1e9/0x340 alloc_pages_mpol_noprof+0x47/0x100 alloc_pages_noprof+0x4b/0x80 get_free_pages_noprof+0xc/0x40 alloc_low_pages+0xc2/0x150 phys_pud_init+0x82/0x390 ...
(everything from phys_pud_init and below was the same)
There's some additional context in a github issue we opened on our side:
https://github.com/neondatabase/autoscaling/issues/1391
=== Reproducing / Testing ===
I was able to partially reproduce the original issue we saw by modifying phys_pud_init() to simulate alloc_low_page() returning null after boot, and then doing memory hotplug to trigger the "failure". Something roughly like:
- pmd = alloc_low_page();
+ if (!after_bootmem)
+ pmd = alloc_low_page();
+ else
+ pmd = 0;
To test recovery, I also tried simulating just one alloc_low_page() failure after boot. This change seemed to handle it at a basic level (virito-mem hotplug succeeded with the right amount, after retrying), but I didn't dig further.
We also plan to test this in our production environment (where we should see the difference after a few days); as of 2025-06-13, we haven't yet rolled that out.
Em Sharnoff (4):
x86/mm: Update mapped addresses in phys_{pmd,pud}_init() x86/mm: Allow error returns from phys_*_init() x86/mm: Handle alloc failure in phys_*_init() x86/mm: Use GFP_KERNEL for alloc_low_pages() after boot
arch/x86/include/asm/pgtable.h | 3 +- arch/x86/mm/init.c | 29 ++++++--- arch/x86/mm/init_32.c | 6 +- arch/x86/mm/init_64.c | 116 ++++++++++++++++++++++----------- arch/x86/mm/mem_encrypt_amd.c | 8 ++- arch/x86/mm/mm_internal.h | 13 ++-- 6 files changed, 113 insertions(+), 62 deletions(-)
base-commit: 82f2b0b97b36ee3fcddf0f0780a9a0825d52fec3
Signature · 1 line
2.39.5
PATCH v4 1/4 x86/mm: Update mapped addresses in phys_{pmd,pud}_init()
Currently kernel_physical_mapping_init() and its dependents return the last physical address mapped ('paddr_last'). This makes it harder to cleanly handle allocation errors in those functions.
'paddr_last' is used to update 'pfn_mapped'/'max_pfn_mapped', so:
1. Introduce add_paddr_range_mapped() to do the update, translating from physical addresses to pfns
2. Call add_paddr_range_mapped() in phys_pud_init() where 'paddr_last' would otherwise be updated due to 1Gi pages.
- Note: this includes places where we set 'paddr_last = paddr_next',
as was added in 20167d3421a0 ("x86-64: Fix accounting in
kernel_physical_mapping_init()")add_paddr_range_mapped() is probably too expensive to be called every time a page is updated, so instead, phys_pte_init() continues to return 'paddr_last', and phys_pmd_init() calls add_paddr_range_mapped() only at the end of the loop (should mean it's called every 1Gi).
---
Changelog:
- v4: Add this patch
--- arch/x86/include/asm/pgtable.h | 3 +- arch/x86/mm/init.c | 23 +++++---- arch/x86/mm/init_32.c | 6 ++- arch/x86/mm/init_64.c | 88 +++++++++++++++++----------------- arch/x86/mm/mm_internal.h | 13 +++-- 5 files changed, 69 insertions(+), 64 deletions(-)
arch/x86/include/asm/pgtable.h +1 -2
@@ -1244,8 +1244,7 @@ extern int direct_gbpages;void init_mem_mapping (void );void early_alloc_pgt_buf (void );void __init poking_init (void ); -unsigned long init_memory_mapping (unsigned long start, -unsigned long end,pgprot_t prot); +void init_memory_mapping (unsigned long start,unsigned long end,pgprot_t prot);#ifdef CONFIG_X86_64 extern pgd_t trampoline_pgd_entry;
arch/x86/mm/init.c +14 -9
@@ -529,16 +529,24 @@ bool pfn_range_is_mapped(unsigned long start_pfn, unsigned long end_pfn)return false; } +/* +* Update max_pfn_mapped and range_pfn_mapped with the range of physical +* addresses mapped. The range may overlap with previous calls to this function. +*/ +void add_paddr_range_mapped (unsigned long start_paddr,unsigned long end_paddr) +{ +add_pfn_range_mapped (start_paddr >>PAGE_SHIFT , end_paddr >>PAGE_SHIFT ); +} +/* * Setup the direct mapping of the physical memory at PAGE_OFFSET. * This runs before bootmem is initialized and gets pages directly from * the physical memory. To access them they are temporarily mapped. */ -unsigned long __ref init_memory_mapping (unsigned long start, -unsigned long end,pgprot_t prot) +void __ref init_memory_mapping (unsigned long start, +unsigned long end,pgprot_t prot) {struct map_range mr[NR_RANGE_MR ]; -unsigned long ret =0 ;int nr_range, i; pr_debug("init_memory_mapping: [mem %#010lx-%#010lx]\n",
@@ -548,13 +556,10 @@ unsigned long __ref init_memory_mapping(unsigned long start, nr_range =split_mem_range (mr,0 , start, end);for (i =0 ; i < nr_range; i++) - ret =kernel_physical_mapping_init (mr[i].start, mr[i].end, - mr[i].page_size_mask, - prot); +kernel_physical_mapping_init (mr[i].start, mr[i].end, + mr[i].page_size_mask, prot); -add_pfn_range_mapped (start >>PAGE_SHIFT , ret >>PAGE_SHIFT ); - -return ret >>PAGE_SHIFT ; +return ; }/*
arch/x86/mm/init_32.c +4 -2
@@ -245,7 +245,7 @@ static inline int is_x86_32_kernel_text(unsigned long addr)* of max_low_pfn pages, by creating page tables starting from address * PAGE_OFFSET: */ -unsigned long __init +void __init kernel_physical_mapping_init(unsigned long start, unsigned long end, unsigned long page_size_mask,
@@ -382,7 +382,9 @@ kernel_physical_mapping_init(unsigned long start, mapping_iter =2 ; goto repeat; } -return last_map_addr; + +add_paddr_range_mapped (start, last_map_addr); +return ; }#ifdef CONFIG_HIGHMEM
arch/x86/mm/init_64.c +44 -44
215 lines not on this page · raw message
arch/x86/mm/mm_internal.h +6 -7
@@ -10,13 +10,12 @@ static inline void *alloc_low_page(void)void early_ioremap_page_table_range_init (void ); -unsigned long kernel_physical_mapping_init (unsigned long start, -unsigned long end, -unsigned long page_size_mask, -pgprot_t prot); -unsigned long kernel_physical_mapping_change (unsigned long start, -unsigned long end, -unsigned long page_size_mask); +void add_paddr_range_mapped (unsigned long start_paddr,unsigned long end_paddr); + +void kernel_physical_mapping_init (unsigned long start,unsigned long end, +unsigned long page_size_mask,pgprot_t prot); +void kernel_physical_mapping_change (unsigned long start,unsigned long end, +unsigned long page_size_mask);void zone_sizes_init (void );extern int after_bootmem;
Signature · 1 line
2.39.5
PATCH v4 2/4 x86/mm: Allow error returns from phys_*_init()
In reply to Em SharnoffPreparation for returning errors when alloc_low_page() fails.
phys_pte_init() is excluded because it can't fail, and it's useful for it to return 'paddr_last' instead.
This patch depends on the previous patch ("x86/mm: Update mapped addresses in phys_{pmd,pud}_init()").
---
Changleog:
- v2: Switch from special-casing zero value to using ERR_PTR()
- v3: Fix -Wint-conversion errors
- v4: Switch return type to int, split alloc handling into separate patch.
--- arch/x86/include/asm/pgtable.h | 2 +- arch/x86/mm/init.c | 14 +++-- arch/x86/mm/init_32.c | 4 +- arch/x86/mm/init_64.c | 100 ++++++++++++++++++++++----------- arch/x86/mm/mem_encrypt_amd.c | 8 ++- arch/x86/mm/mm_internal.h | 8 +-- 6 files changed, 87 insertions(+), 49 deletions(-)
arch/x86/include/asm/pgtable.h +1 -1
@@ -1244,7 +1244,7 @@ extern int direct_gbpages;void init_mem_mapping (void );void early_alloc_pgt_buf (void );void __init poking_init (void ); -void init_memory_mapping (unsigned long start,unsigned long end,pgprot_t prot); +int init_memory_mapping (unsigned long start,unsigned long end,pgprot_t prot);#ifdef CONFIG_X86_64 extern pgd_t trampoline_pgd_entry;
arch/x86/mm/init.c +9 -5
@@ -543,11 +543,12 @@ void add_paddr_range_mapped(unsigned long start_paddr, unsigned long end_paddr)* This runs before bootmem is initialized and gets pages directly from * the physical memory. To access them they are temporarily mapped. */ -void __ref init_memory_mapping (unsigned long start, +int __ref init_memory_mapping (unsigned long start,unsigned long end,pgprot_t prot) {struct map_range mr[NR_RANGE_MR ];int nr_range, i; +int ret;pr_debug ("init_memory_mapping: [mem %#010lx-%#010lx]\n" , start, end -1 );
@@ -555,11 +556,14 @@ void __ref init_memory_mapping(unsigned long start,memset (mr,0 ,sizeof (mr)); nr_range =split_mem_range (mr,0 , start, end); -for (i =0 ; i < nr_range; i++) -kernel_physical_mapping_init (mr[i].start, mr[i].end, - mr[i].page_size_mask, prot); +for (i =0 ; i < nr_range; i++) { + ret =kernel_physical_mapping_init (mr[i].start, mr[i].end, + mr[i].page_size_mask, prot); +if (ret) +return ret; + } -return ; +return 0 ; }/*
arch/x86/mm/init_32.c +2 -2
@@ -245,7 +245,7 @@ static inline int is_x86_32_kernel_text(unsigned long addr)* of max_low_pfn pages, by creating page tables starting from address * PAGE_OFFSET: */ -void __init +int __init kernel_physical_mapping_init(unsigned long start, unsigned long end, unsigned long page_size_mask,
@@ -384,7 +384,7 @@ kernel_physical_mapping_init(unsigned long start, }add_paddr_range_mapped (start, last_map_addr); -return ; +return 0 ; }#ifdef CONFIG_HIGHMEM
arch/x86/mm/init_64.c +66 -34
227 lines not on this page · raw message
arch/x86/mm/mem_encrypt_amd.c +5 -3
@@ -444,9 +444,11 @@ static int __init early_set_memory_enc_dec(unsigned long vaddr,* kernel_physical_mapping_change() does not flush the TLBs, so * a TLB flush is required after we exit from the for loop. */ -kernel_physical_mapping_change (__pa (vaddr & pmask), -__pa ((vaddr_end & pmask) + psize), - split_page_size_mask); + ret =kernel_physical_mapping_change (__pa (vaddr & pmask), +__pa ((vaddr_end & pmask) + psize), + split_page_size_mask); +if (ret) +return ret; } ret =0 ;
arch/x86/mm/mm_internal.h +4 -4
@@ -12,10 +12,10 @@ void early_ioremap_page_table_range_init(void);void add_paddr_range_mapped (unsigned long start_paddr,unsigned long end_paddr); -void kernel_physical_mapping_init (unsigned long start,unsigned long end, -unsigned long page_size_mask,pgprot_t prot); -void kernel_physical_mapping_change (unsigned long start,unsigned long end, -unsigned long page_size_mask); +int kernel_physical_mapping_init (unsigned long start,unsigned long end, +unsigned long page_size_mask,pgprot_t prot); +int kernel_physical_mapping_change (unsigned long start,unsigned long end, +unsigned long page_size_mask);void zone_sizes_init (void );extern int after_bootmem;
Signature · 1 line
2.39.5
PATCH v4 3/4 x86/mm: Handle alloc failure in phys_*_init()
In reply to Em SharnoffDuring memory hotplug, allocation failures in phys_*_init() aren't handled, which results in a null pointer dereference if they occur.
This patch depends on the previous patch ("x86/mm: Allow error returns from phys_*_init()").
---
Changelog:
- v4: Split this patch out from the error handling changes
--- arch/x86/mm/init_64.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/mm/init_64.c +8 -0
@@ -572,6 +572,8 @@ phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end, } pte =alloc_low_page (); +if (!pte) +return -ENOMEM ; paddr_last =phys_pte_init (pte, paddr, paddr_end, new_prot, init);spin_lock (&init_mm.page_table_lock);
@@ -664,6 +666,8 @@ phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end, } pmd =alloc_low_page (); +if (!pmd) +return -ENOMEM ; ret =phys_pmd_init (pmd, paddr, paddr_end, page_size_mask, prot, init);
@@ -726,6 +730,8 @@ phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end, } pud =alloc_low_page (); +if (!pud) +return -ENOMEM ; ret =phys_pud_init (pud, paddr,__pa (vaddr_end), page_size_mask, prot, init);
@@ -774,6 +780,8 @@ __kernel_physical_mapping_init(unsigned long paddr_start, } p4d =alloc_low_page (); +if (!p4d) +return -ENOMEM ; ret =phys_p4d_init (p4d,__pa (vaddr),__pa (vaddr_end), page_size_mask, prot, init);
Signature · 1 line
2.39.5
PATCH v4 4/4 x86/mm: Use GFP_KERNEL for alloc_low_pages() after boot
In reply to Em SharnoffCurrently it's GFP_ATOMIC. GFP_KERNEL seems more correct.
From Ingo M. [1]
Quoted 2 lines
There's no real reason why it should be
GFP_ATOMICAFAICS, other than some historic inertia that nobody bothered to fix.
and previously Mike R. [2]
Quoted 7 lines
The few callers that effectively use page allocator for the direct map updates are
gart_iommu_init()and memory hotplug. Neither of them happen in an atomic context so there is no reason to useGFP_ATOMICfor these allocations.Replace
GFP_ATOMICwithGFP_KERNELto avoid using atomic reserves for allocations that do not require that.
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
---
Changelog:
- v2: Add this patch
- v3: No changes
- v4: No changes
--- arch/x86/mm/init.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/mm/init.c +1 -1
@@ -131,7 +131,7 @@ __ref void *alloc_low_pages(unsigned int num)unsigned int order; order =get_order ((unsigned long )num <<PAGE_SHIFT ); -return (void *)__get_free_pages (GFP_ATOMIC | __GFP_ZERO, order); +return (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO, order); }if ((pgt_buf_end + num) > pgt_buf_top || !can_use_brk_pgt) {
Signature · 1 line
2.39.5