Re: [PATCH 12/14] mm/sparse: move sparse_init_one_section() to internal.h

From: Lorenzo Stoakes (Oracle)

Date: Tue Mar 17 2026 - 16:00:36 EST


On Tue, Mar 17, 2026 at 05:56:50PM +0100, David Hildenbrand (Arm) wrote:
> While at it, convert the BUG_ON to a WARN_ON, avoid long lines, and merge
> sparse_encode_mem_map() into sparse_init_one_section().
>
> Clarify the comment a bit, pointing at page_to_pfn().
>
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

Don't need a long line to merge my:

Reviewed-by: Lorenzo Stoakes (Oracle) <ljs@xxxxxxxxxx>

Here!

> ---
> include/linux/mmzone.h | 2 +-
> mm/internal.h | 22 ++++++++++++++++++++++
> mm/sparse.c | 24 ------------------------
> 3 files changed, 23 insertions(+), 25 deletions(-)
>
> diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
> index b694c69dee04..dcbbf36ed88c 100644
> --- a/include/linux/mmzone.h
> +++ b/include/linux/mmzone.h
> @@ -2008,7 +2008,7 @@ struct mem_section {
> /*
> * This is, logically, a pointer to an array of struct
> * pages. However, it is stored with some other magic.
> - * (see sparse.c::sparse_init_one_section())

What::the::hell::was::this::before, we're not C++ developers!!

> + * (see sparse_init_one_section())
> *
> * Additionally during early boot we encode node id of
> * the location of the section here to guide allocation.
> diff --git a/mm/internal.h b/mm/internal.h
> index 5f5c45d80aca..bcf4df97b185 100644
> --- a/mm/internal.h
> +++ b/mm/internal.h
> @@ -965,6 +965,28 @@ void memmap_init_range(unsigned long, int, unsigned long, unsigned long,
> */
> #ifdef CONFIG_SPARSEMEM
> void sparse_init(void);
> +
> +static inline void sparse_init_one_section(struct mem_section *ms,
> + unsigned long pnum, struct page *mem_map,
> + struct mem_section_usage *usage, unsigned long flags)
> +{
> + unsigned long coded_mem_map;
> +
> + BUILD_BUG_ON(SECTION_MAP_LAST_BIT > PFN_SECTION_SHIFT);
> +
> + /*
> + * We encode the start PFN of the section into the mem_map such that
> + * page_to_pfn() on !CONFIG_SPARSEMEM_VMEMMAP can simply subtract it
> + * from the page pointer to obtain the PFN.
> + */
> + coded_mem_map = (unsigned long)(mem_map - section_nr_to_pfn(pnum));
> + VM_WARN_ON(coded_mem_map & ~SECTION_MAP_MASK);

Maybe VM_WARN_ON_ONCE()?

> +
> + ms->section_mem_map &= ~SECTION_MAP_MASK;
> + ms->section_mem_map |= coded_mem_map;
> + ms->section_mem_map |= SECTION_HAS_MEM_MAP | flags;

I mean this is pretty nitty but prefer 'flags | SECTION_HAS_MEM_MAP' to
show you appended to the flags :P

> + ms->usage = usage;
> +}
> #else
> static inline void sparse_init(void) {}
> #endif /* CONFIG_SPARSEMEM */
> diff --git a/mm/sparse.c b/mm/sparse.c
> index b5a2de43ac40..6f5f340301a3 100644
> --- a/mm/sparse.c
> +++ b/mm/sparse.c
> @@ -256,30 +256,6 @@ static void __init memblocks_present(void)
> memory_present(nid, start, end);
> }
>
> -/*
> - * Subtle, we encode the real pfn into the mem_map such that
> - * the identity pfn - section_mem_map will return the actual
> - * physical page frame number.
> - */
> -static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
> -{
> - unsigned long coded_mem_map =
> - (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
> - BUILD_BUG_ON(SECTION_MAP_LAST_BIT > PFN_SECTION_SHIFT);
> - BUG_ON(coded_mem_map & ~SECTION_MAP_MASK);
> - return coded_mem_map;
> -}
> -
> -static void __meminit sparse_init_one_section(struct mem_section *ms,
> - unsigned long pnum, struct page *mem_map,
> - struct mem_section_usage *usage, unsigned long flags)
> -{
> - ms->section_mem_map &= ~SECTION_MAP_MASK;
> - ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum)
> - | SECTION_HAS_MEM_MAP | flags;
> - ms->usage = usage;
> -}
> -
> static unsigned long usemap_size(void)
> {
> return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
> --
> 2.43.0
>