* [PATCH] mm/sparse: keep mem_section_usage_size() internal
@ 2026-08-04 10:01 Muchun Song
2026-08-04 10:09 ` David Hildenbrand (Arm)
0 siblings, 1 reply; 3+ messages in thread
From: Muchun Song @ 2026-08-04 10:01 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, linux-mm
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, linux-kernel, Muchun Song,
muchun.song
mem_section_usage_size() is only needed by sparsemem implementation code,
but declaring it in mmzone.h makes the helper visible to all mmzone.h
users.
Move the helper to sparse.h so sparse.c and sparse-vmemmap.c can share
it through the internal header. While doing so, calculate the allocation
size with struct_size(), which ties the expression to the pageblock_flags
trailing array instead of open-coding the struct header plus bitmap size.
Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
include/linux/mmzone.h | 1 -
mm/sparse.c | 10 ----------
mm/sparse.h | 6 ++++++
3 files changed, 6 insertions(+), 11 deletions(-)
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index a26c8b855222..86891163e0ef 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -2070,7 +2070,6 @@ static inline struct mem_section *__nr_to_section(unsigned long nr)
#endif
return &mem_section[root][nr & SECTION_ROOT_MASK];
}
-extern size_t mem_section_usage_size(void);
/*
* We use the lower bits of the mem_map pointer to store a little bit of
diff --git a/mm/sparse.c b/mm/sparse.c
index 704a9dec2b9a..46458e5300e2 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -213,16 +213,6 @@ static void __init memblocks_present(void)
memory_present(nid, start, end);
}
-static unsigned long usemap_size(void)
-{
- return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
-}
-
-size_t mem_section_usage_size(void)
-{
- return sizeof(struct mem_section_usage) + usemap_size();
-}
-
#ifdef CONFIG_SPARSEMEM_VMEMMAP
unsigned long __init section_map_size(void)
{
diff --git a/mm/sparse.h b/mm/sparse.h
index 95aa031213f2..4960a17b706f 100644
--- a/mm/sparse.h
+++ b/mm/sparse.h
@@ -47,6 +47,12 @@ static inline void __section_mark_present(struct mem_section *ms,
ms->section_mem_map |= SECTION_MARKED_PRESENT;
}
+
+static inline size_t mem_section_usage_size(void)
+{
+ return struct_size((struct mem_section_usage *)NULL, pageblock_flags,
+ BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS));
+}
#else
static inline void sparse_init(void) {}
#endif /* CONFIG_SPARSEMEM */
base-commit: 9a4cdc958dd79fc6c3b20b51a10debec6ca09fec
--
2.54.0
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] mm/sparse: keep mem_section_usage_size() internal
2026-08-04 10:01 [PATCH] mm/sparse: keep mem_section_usage_size() internal Muchun Song
@ 2026-08-04 10:09 ` David Hildenbrand (Arm)
2026-08-04 11:10 ` Muchun Song
0 siblings, 1 reply; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-08-04 10:09 UTC (permalink / raw)
To: Muchun Song, Andrew Morton, linux-mm
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, linux-kernel, muchun.song
On 8/4/26 12:01, Muchun Song wrote:
> mem_section_usage_size() is only needed by sparsemem implementation code,
> but declaring it in mmzone.h makes the helper visible to all mmzone.h
> users.
Worth mentioning that
commit ae751d567baa08342e5e34b378b72a6f9b2cfada
Author: David Hildenbrand (Arm) <david@kernel.org>
Date: Mon May 11 16:05:34 2026 +0200
mm/bootmem_info: stop marking mem_section_usage as MIX_SECTION_INFO
Remove the last external user.
>
> Move the helper to sparse.h so sparse.c and sparse-vmemmap.c can share
> it through the internal header. While doing so, calculate the allocation
> size with struct_size(), which ties the expression to the pageblock_flags
> trailing array instead of open-coding the struct header plus bitmap size.
>
> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> ---
> include/linux/mmzone.h | 1 -
> mm/sparse.c | 10 ----------
> mm/sparse.h | 6 ++++++
> 3 files changed, 6 insertions(+), 11 deletions(-)
>
> diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
> index a26c8b855222..86891163e0ef 100644
> --- a/include/linux/mmzone.h
> +++ b/include/linux/mmzone.h
> @@ -2070,7 +2070,6 @@ static inline struct mem_section *__nr_to_section(unsigned long nr)
> #endif
> return &mem_section[root][nr & SECTION_ROOT_MASK];
> }
> -extern size_t mem_section_usage_size(void);
>
> /*
> * We use the lower bits of the mem_map pointer to store a little bit of
> diff --git a/mm/sparse.c b/mm/sparse.c
> index 704a9dec2b9a..46458e5300e2 100644
> --- a/mm/sparse.c
> +++ b/mm/sparse.c
> @@ -213,16 +213,6 @@ static void __init memblocks_present(void)
> memory_present(nid, start, end);
> }
>
> -static unsigned long usemap_size(void)
> -{
> - return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
> -}
> -
> -size_t mem_section_usage_size(void)
> -{
> - return sizeof(struct mem_section_usage) + usemap_size();
> -}
> -
> #ifdef CONFIG_SPARSEMEM_VMEMMAP
> unsigned long __init section_map_size(void)
> {
> diff --git a/mm/sparse.h b/mm/sparse.h
> index 95aa031213f2..4960a17b706f 100644
> --- a/mm/sparse.h
> +++ b/mm/sparse.h
> @@ -47,6 +47,12 @@ static inline void __section_mark_present(struct mem_section *ms,
>
> ms->section_mem_map |= SECTION_MARKED_PRESENT;
> }
> +
> +static inline size_t mem_section_usage_size(void)
> +{
> + return struct_size((struct mem_section_usage *)NULL, pageblock_flags,
> + BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS));
> +}
There is struct_size_t() that avoids the manual "NULL" trick.
--
Cheers,
David
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] mm/sparse: keep mem_section_usage_size() internal
2026-08-04 10:09 ` David Hildenbrand (Arm)
@ 2026-08-04 11:10 ` Muchun Song
0 siblings, 0 replies; 3+ messages in thread
From: Muchun Song @ 2026-08-04 11:10 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: Muchun Song, Andrew Morton, linux-mm, Lorenzo Stoakes,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, linux-kernel
> On Aug 4, 2026, at 18:09, David Hildenbrand (Arm) <david@kernel.org> wrote:
>
> On 8/4/26 12:01, Muchun Song wrote:
>> mem_section_usage_size() is only needed by sparsemem implementation code,
>> but declaring it in mmzone.h makes the helper visible to all mmzone.h
>> users.
>
> Worth mentioning that
>
> commit ae751d567baa08342e5e34b378b72a6f9b2cfada
> Author: David Hildenbrand (Arm) <david@kernel.org>
> Date: Mon May 11 16:05:34 2026 +0200
>
> mm/bootmem_info: stop marking mem_section_usage as MIX_SECTION_INFO
>
> Remove the last external user.
Will do.
>
>>
>> Move the helper to sparse.h so sparse.c and sparse-vmemmap.c can share
>> it through the internal header. While doing so, calculate the allocation
>> size with struct_size(), which ties the expression to the pageblock_flags
>> trailing array instead of open-coding the struct header plus bitmap size.
>>
>> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
>> ---
>> include/linux/mmzone.h | 1 -
>> mm/sparse.c | 10 ----------
>> mm/sparse.h | 6 ++++++
>> 3 files changed, 6 insertions(+), 11 deletions(-)
>>
>> diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
>> index a26c8b855222..86891163e0ef 100644
>> --- a/include/linux/mmzone.h
>> +++ b/include/linux/mmzone.h
>> @@ -2070,7 +2070,6 @@ static inline struct mem_section *__nr_to_section(unsigned long nr)
>> #endif
>> return &mem_section[root][nr & SECTION_ROOT_MASK];
>> }
>> -extern size_t mem_section_usage_size(void);
>>
>> /*
>> * We use the lower bits of the mem_map pointer to store a little bit of
>> diff --git a/mm/sparse.c b/mm/sparse.c
>> index 704a9dec2b9a..46458e5300e2 100644
>> --- a/mm/sparse.c
>> +++ b/mm/sparse.c
>> @@ -213,16 +213,6 @@ static void __init memblocks_present(void)
>> memory_present(nid, start, end);
>> }
>>
>> -static unsigned long usemap_size(void)
>> -{
>> - return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
>> -}
>> -
>> -size_t mem_section_usage_size(void)
>> -{
>> - return sizeof(struct mem_section_usage) + usemap_size();
>> -}
>> -
>> #ifdef CONFIG_SPARSEMEM_VMEMMAP
>> unsigned long __init section_map_size(void)
>> {
>> diff --git a/mm/sparse.h b/mm/sparse.h
>> index 95aa031213f2..4960a17b706f 100644
>> --- a/mm/sparse.h
>> +++ b/mm/sparse.h
>> @@ -47,6 +47,12 @@ static inline void __section_mark_present(struct mem_section *ms,
>>
>> ms->section_mem_map |= SECTION_MARKED_PRESENT;
>> }
>> +
>> +static inline size_t mem_section_usage_size(void)
>> +{
>> + return struct_size((struct mem_section_usage *)NULL, pageblock_flags,
>> + BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS));
>> +}
>
> There is struct_size_t() that avoids the manual "NULL" trick.
I hadn't noticed that API before. Thanks for the heads-up.
I'll send a v2 to fix this.
Muchun,
Thanks.
>
> --
> Cheers,
>
> David
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-08-04 11:11 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-04 10:01 [PATCH] mm/sparse: keep mem_section_usage_size() internal Muchun Song
2026-08-04 10:09 ` David Hildenbrand (Arm)
2026-08-04 11:10 ` Muchun Song
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®