Re: [PATCH v2 13/15] mm/sparse: move sparse_init_one_section() to internal.h
From: David Hildenbrand (Arm)
Date: Mon Mar 23 2026 - 04:49:20 EST
On 3/20/26 23:13, David Hildenbrand (Arm) wrote:
> While at it, convert the BUG_ON to a VM_WARN_ON_ONCE, avoid long lines, and
> merge sparse_encode_mem_map() into its only caller
> sparse_init_one_section().
>
> Clarify the comment a bit, pointing at page_to_pfn().
>
> Reviewed-by: Lorenzo Stoakes (Oracle) <ljs@xxxxxxxxxx>
> Reviewed-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> ---
> 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())
> + * (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..2f188f7702f7 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);
> +
> + ms->section_mem_map &= ~SECTION_MAP_MASK;
> + ms->section_mem_map |= coded_mem_map;
> + ms->section_mem_map |= flags | SECTION_HAS_MEM_MAP;
> + ms->usage = usage;
The following fixup on top: