Re: [PATCH v2 22/28] arch, mm: consolidate initialization of nodes, zones and memory map

Klara Modin
2026-01-03T20:32:30Z

Hi,

On 2026-01-03 20:54:23 +0200, Mike Rapoport wrote:
> Hi,
>
> On Sat, Jan 03, 2026 at 12:33:29AM +0100, Klara Modin wrote:
> > On 2026-01-02 08:59:58 +0200, Mike Rapoport wrote:
> > > From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
> > >
> > > To initialize node, zone and memory map data structures every architecture
> > > calls free_area_init() during setup_arch() and passes it an array of zone
> > > limits.
> > >
> > > Beside code duplication it creates "interesting" ordering cases between
> > > allocation and initialization of hugetlb and the memory map. Some
> > > architectures allocate hugetlb pages very early in setup_arch() in certain
> > > cases, some only create hugetlb CMA areas in setup_arch() and sometimes
> > > hugetlb allocations happen mm_core_init().
> > >
> > > With arch_zone_limits_init() helper available now on all architectures it
> > > is no longer necessary to call free_area_init() from architecture setup
> > > code. Rather core MM initialization can call arch_zone_limits_init() in a
> > > single place.
> > >
> > > This allows to unify ordering of hugetlb vs memory map allocation and
> > > initialization.
> > >
> > > Remove the call to free_area_init() from architecture specific code and
> > > place it in a new mm_core_init_early() function that is called immediately
> > > after setup_arch().
> > >
> > > After this refactoring it is possible to consolidate hugetlb allocations
> > > and eliminate differences in ordering of hugetlb and memory map
> > > initialization among different architectures.
> > >
> > > As the first step of this consolidation move hugetlb_bootmem_alloc() to
> > > mm_core_early_init().
> > >
> > > Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> >
> > This breaks boot on my Raspberry Pi 1. The reason seems to be the use of
> > page_folio() when initializing the dynamically allocated zero page in
> > arm, which doesn't work when free_area_init() hasn't been called yet.
>
> I believe the reason is rather the use of virt_to_phys() that now happens
> before the memory map is ready.
>

Right, that makes sense, the fault just becomes apparent when
page_folio() is called on some bogus address then?

> > The following oopses are generated:
> >
> > 8<--- cut here ---
> > Unable to handle kernel paging request at virtual address 003dfb44 when read
> > [003dfb44] *pgd=00000000
> > Internal error: Oops: 5 [#1] ARM
> > CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 6.19.0-rc3-03898-g7975b0084358 #451 NONE
> > Hardware name: BCM2835
> > PC is at paging_init (include/linux/page-flags.h:284 (discriminator 2) arch/arm/mm/mmu.c:1790 (discriminator 2))
> > LR is at paging_init (arch/arm/mm/mmu.c:1789 (discriminator 1))
>
> ...
>
> > 8<--- cut here ---
> >
> > and the second one repeats for some time afterwards.
> >
> > I experimented a little by allocating the zero page statically as many
> > other arches do which fixes the issue as it does not need to be
> > initialized at this point anymore, though I have no idea if that's
> > appropriate.
>
> Do you mean putting the zero in the BSS like, e.g. arm64? I don't see a
> reason why this shouldn't work.
>

Yes, exactly that. The diff I had was:

diff --git a/arch/arm/include/asm/pgtable.h b/arch/arm/include/asm/pgtable.h
index 86378eec7757..6fa9acd6a7f5 100644
--- a/arch/arm/include/asm/pgtable.h
+++ b/arch/arm/include/asm/pgtable.h
@@ -15,8 +15,8 @@
* ZERO_PAGE is a global shared page that is always zero: used
* for zero-mapped memory areas etc..
*/
-extern struct page *empty_zero_page;
-#define ZERO_PAGE(vaddr) (empty_zero_page)
+extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
+#define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
#endif

#include <asm-generic/pgtable-nopud.h>
diff --git a/arch/arm/mm/mmu.c b/arch/arm/mm/mmu.c
index 8bac96e205ac..518def8314e7 100644
--- a/arch/arm/mm/mmu.c
+++ b/arch/arm/mm/mmu.c
@@ -45,7 +45,7 @@ extern unsigned long __atags_pointer;
* empty_zero_page is a special page that is used for
* zero-initialized data and COW.
*/
-struct page *empty_zero_page;
+unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
EXPORT_SYMBOL(empty_zero_page);

/*
@@ -1754,8 +1754,6 @@ static void __init early_fixmap_shutdown(void)
*/
void __init paging_init(const struct machine_desc *mdesc)
{
- void *zero_page;
-
#ifdef CONFIG_XIP_KERNEL
/* Store the kernel RW RAM region start/end in these variables */
kernel_sec_start = CONFIG_PHYS_OFFSET & SECTION_MASK;
@@ -1781,13 +1779,7 @@ void __init paging_init(const struct machine_desc *mdesc)

top_pmd = pmd_off_k(0xffff0000);

- /* allocate the zero page. */
- zero_page = early_alloc(PAGE_SIZE);
-
bootmem_init();
-
- empty_zero_page = virt_to_page(zero_page);
- __flush_dcache_folio(NULL, page_folio(empty_zero_page));
}

void __init early_mm_init(const struct machine_desc *mdesc)
diff --git a/arch/arm/mm/nommu.c b/arch/arm/mm/nommu.c
index d638cc87807e..7e42d8accec6 100644
--- a/arch/arm/mm/nommu.c
+++ b/arch/arm/mm/nommu.c
@@ -31,7 +31,7 @@ unsigned long vectors_base;
* empty_zero_page is a special page that is used for
* zero-initialized data and COW.
*/
-struct page *empty_zero_page;
+unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
EXPORT_SYMBOL(empty_zero_page);

#ifdef CONFIG_ARM_MPU
@@ -156,18 +156,10 @@ void __init adjust_lowmem_bounds(void)
*/
void __init paging_init(const struct machine_desc *mdesc)
{
- void *zero_page;
-
early_trap_init((void *)vectors_base);
mpu_setup();

- /* allocate the zero page. */
- zero_page = (void *)memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
-
bootmem_init();
-
- empty_zero_page = virt_to_page(zero_page);
- flush_dcache_page(empty_zero_page);
}

/*

> I also have a patch with some minor changes that still keeps
> empty_zero_page allocated, but avoids virt_to_page() and folio_page()
> dance. Can you please test it in your setup?
>
> From 8a213c13211106d592fbe96b68ee29879ed739f8 Mon Sep 17 00:00:00 2001
> From: "Mike Rapoport (Microsoft)" <rppt@kernel.org>
> Date: Sat, 3 Jan 2026 20:40:09 +0200
> Subject: [PATCH] arm: make initialization of zero page independent of the
> memory map
>
> Unlike most architectures, arm keeps a struct page pointer to the
> empty_zero_page and to initialize it requires conversion of a virtual
> address to page which makes it necessary to have memory map initialized
> before creating the empty_zero_page.
>
> Make empty_zero_page a void * to decouple it's initialization from the
> initialization of the memory map.
>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> ---
> arch/arm/include/asm/pgtable.h | 4 ++--
> arch/arm/mm/mmu.c | 10 +++-------
> arch/arm/mm/nommu.c | 10 +++-------
> 3 files changed, 8 insertions(+), 16 deletions(-)
>
> diff --git a/arch/arm/include/asm/pgtable.h b/arch/arm/include/asm/pgtable.h
> index 86378eec7757..08bbd2aed6c9 100644
> --- a/arch/arm/include/asm/pgtable.h
> +++ b/arch/arm/include/asm/pgtable.h
> @@ -15,8 +15,8 @@
> * ZERO_PAGE is a global shared page that is always zero: used
> * for zero-mapped memory areas etc..
> */
> -extern struct page *empty_zero_page;
> -#define ZERO_PAGE(vaddr) (empty_zero_page)
> +extern void *empty_zero_page;
> +#define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
> #endif
>
> #include <asm-generic/pgtable-nopud.h>
> diff --git a/arch/arm/mm/mmu.c b/arch/arm/mm/mmu.c
> index 8bac96e205ac..867258f1ae09 100644
> --- a/arch/arm/mm/mmu.c
> +++ b/arch/arm/mm/mmu.c
> @@ -45,7 +45,7 @@ extern unsigned long __atags_pointer;
> * empty_zero_page is a special page that is used for
> * zero-initialized data and COW.
> */
> -struct page *empty_zero_page;
> +void *empty_zero_page;
> EXPORT_SYMBOL(empty_zero_page);
>
> /*
> @@ -1754,8 +1754,6 @@ static void __init early_fixmap_shutdown(void)
> */
> void __init paging_init(const struct machine_desc *mdesc)
> {
> - void *zero_page;
> -
> #ifdef CONFIG_XIP_KERNEL
> /* Store the kernel RW RAM region start/end in these variables */
> kernel_sec_start = CONFIG_PHYS_OFFSET & SECTION_MASK;
> @@ -1782,12 +1780,10 @@ void __init paging_init(const struct machine_desc *mdesc)
> top_pmd = pmd_off_k(0xffff0000);
>
> /* allocate the zero page. */
> - zero_page = early_alloc(PAGE_SIZE);
> + empty_zero_page = early_alloc(PAGE_SIZE);
> + __cpuc_flush_dcache_area(empty_zero_page, PAGE_SIZE);
>
> bootmem_init();
> -
> - empty_zero_page = virt_to_page(zero_page);
> - __flush_dcache_folio(NULL, page_folio(empty_zero_page));
> }
>
> void __init early_mm_init(const struct machine_desc *mdesc)
> diff --git a/arch/arm/mm/nommu.c b/arch/arm/mm/nommu.c
> index d638cc87807e..f80ff5a69fbb 100644
> --- a/arch/arm/mm/nommu.c
> +++ b/arch/arm/mm/nommu.c
> @@ -31,7 +31,7 @@ unsigned long vectors_base;
> * empty_zero_page is a special page that is used for
> * zero-initialized data and COW.
> */
> -struct page *empty_zero_page;
> +void *empty_zero_page;
> EXPORT_SYMBOL(empty_zero_page);
>
> #ifdef CONFIG_ARM_MPU
> @@ -156,18 +156,14 @@ void __init adjust_lowmem_bounds(void)
> */
> void __init paging_init(const struct machine_desc *mdesc)
> {
> - void *zero_page;
> -
> early_trap_init((void *)vectors_base);
> mpu_setup();
>
> /* allocate the zero page. */
> - zero_page = (void *)memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
> + empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
> + __cpuc_flush_dcache_area(empty_zero_page, PAGE_SIZE);
>
> bootmem_init();
> -
> - empty_zero_page = virt_to_page(zero_page);
> - flush_dcache_page(empty_zero_page);
> }
>
> /*
> --
> 2.51.0
>

This also works for me.

Thanks,
Tested-by: Klara Modin <klarasmodin@gmail.com>

>
> > Regards,
> > Klara Modin
> >
>
> --
> Sincerely yours,
> Mike.