[PATCH 5/5] mm/alloc_tag: add the CONFIG_ARCH_NEEDS_WEAK_PER_CPU macro when statically defining the percpu variable _shared_alloc_tag

Hao Ge posted 5 patches 4 months ago
There is a newer version of this series
[PATCH 5/5] mm/alloc_tag: add the CONFIG_ARCH_NEEDS_WEAK_PER_CPU macro when statically defining the percpu variable _shared_alloc_tag
Posted by Hao Ge 4 months ago
From: Hao Ge <gehao@kylinos.cn>

Recently discovered this entry while checking kallsyms on ARM64:
ffff800083e509c0 D _shared_alloc_tag

If CONFIG_ARCH_NEEDS_WEAK_PER_CPU is not defined(it is only defined for
s390 and alpha architectures),there's no need to statically define
the percpu variable _shared_alloc_tag. As the number of CPUs
increases,the wasted memory will grow correspondingly.

Enclose the definition of _shared_alloc_tag within the
CONFIG_ARCH_NEEDS_WEAK_PER_CPU condition.

Suggested-by: Suren Baghdasaryan <surenb@google.com>
Signed-off-by: Hao Ge <gehao@kylinos.cn>
---
 lib/alloc_tag.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c
index c7f602fa7b23..14fd66f26e42 100644
--- a/lib/alloc_tag.c
+++ b/lib/alloc_tag.c
@@ -24,8 +24,10 @@ static bool mem_profiling_support;
 
 static struct codetag_type *alloc_tag_cttype;
 
+#ifdef CONFIG_ARCH_NEEDS_WEAK_PER_CPU
 DEFINE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);
 EXPORT_SYMBOL(_shared_alloc_tag);
+#endif
 
 DEFINE_STATIC_KEY_MAYBE(CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT,
 			mem_alloc_profiling_key);
-- 
2.25.1
Re: [PATCH 5/5] mm/alloc_tag: add the CONFIG_ARCH_NEEDS_WEAK_PER_CPU macro when statically defining the percpu variable _shared_alloc_tag
Posted by Mike Rapoport 4 months ago
On Thu, Jun 12, 2025 at 04:27:30PM +0800, Hao Ge wrote:
> From: Hao Ge <gehao@kylinos.cn>
> 
> Recently discovered this entry while checking kallsyms on ARM64:
> ffff800083e509c0 D _shared_alloc_tag
> 
> If CONFIG_ARCH_NEEDS_WEAK_PER_CPU is not defined(it is only defined for
> s390 and alpha architectures),there's no need to statically define
> the percpu variable _shared_alloc_tag. As the number of CPUs
> increases,the wasted memory will grow correspondingly.
> 
> Enclose the definition of _shared_alloc_tag within the
> CONFIG_ARCH_NEEDS_WEAK_PER_CPU condition.
> 
> Suggested-by: Suren Baghdasaryan <surenb@google.com>
> Signed-off-by: Hao Ge <gehao@kylinos.cn>
> ---
>  lib/alloc_tag.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c
> index c7f602fa7b23..14fd66f26e42 100644
> --- a/lib/alloc_tag.c
> +++ b/lib/alloc_tag.c
> @@ -24,8 +24,10 @@ static bool mem_profiling_support;
>  
>  static struct codetag_type *alloc_tag_cttype;
>  
> +#ifdef CONFIG_ARCH_NEEDS_WEAK_PER_CPU

It should be enough to add #ifdef ARCH_NEEDS_WEAK_PER_CPU here instead of
all the churn.

>  DEFINE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);
>  EXPORT_SYMBOL(_shared_alloc_tag);
> +#endif
>  
>  DEFINE_STATIC_KEY_MAYBE(CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT,
>  			mem_alloc_profiling_key);
> -- 
> 2.25.1
> 

-- 
Sincerely yours,
Mike.
Re: [PATCH 5/5] mm/alloc_tag: add the CONFIG_ARCH_NEEDS_WEAK_PER_CPU macro when statically defining the percpu variable _shared_alloc_tag
Posted by Heiko Carstens 4 months ago
On Thu, Jun 12, 2025 at 11:57:49AM +0300, Mike Rapoport wrote:
> On Thu, Jun 12, 2025 at 04:27:30PM +0800, Hao Ge wrote:
> > From: Hao Ge <gehao@kylinos.cn>
> > 
> > Recently discovered this entry while checking kallsyms on ARM64:
> > ffff800083e509c0 D _shared_alloc_tag
> > 
> > If CONFIG_ARCH_NEEDS_WEAK_PER_CPU is not defined(it is only defined for
> > s390 and alpha architectures),there's no need to statically define
> > the percpu variable _shared_alloc_tag. As the number of CPUs
> > increases,the wasted memory will grow correspondingly.
> > 
> > Enclose the definition of _shared_alloc_tag within the
> > CONFIG_ARCH_NEEDS_WEAK_PER_CPU condition.
> > 
> > Suggested-by: Suren Baghdasaryan <surenb@google.com>
> > Signed-off-by: Hao Ge <gehao@kylinos.cn>
> > ---
> >  lib/alloc_tag.c | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c
> > index c7f602fa7b23..14fd66f26e42 100644
> > --- a/lib/alloc_tag.c
> > +++ b/lib/alloc_tag.c
> > @@ -24,8 +24,10 @@ static bool mem_profiling_support;
> >  
> >  static struct codetag_type *alloc_tag_cttype;
> >  
> > +#ifdef CONFIG_ARCH_NEEDS_WEAK_PER_CPU
> 
> It should be enough to add #ifdef ARCH_NEEDS_WEAK_PER_CPU here instead of
> all the churn.

That won't work since ARCH_NEEDS_WEAK_PER_CPU is only defined if MODULE is
also defined, which is not the case for core kernel code like lib/alloc_tag.c.

As a side note: I'm wondering if s390 still needs ARCH_NEEDS_WEAK_PER_CPU due
to all the compiler option changes we had recently. But that's a different
story and independent of this series.