linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Suren Baghdasaryan <surenb@google.com>
To: Hao Ge <hao.ge@linux.dev>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Kent Overstreet <kent.overstreet@linux.dev>,
	 linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	Hao Ge <gehao@kylinos.cn>
Subject: Re: [PATCH] mm/alloc_tag: add the ARCH_NEEDS_WEAK_PER_CPU macro when statically defining the percpu variable alloc_tag_counters.
Date: Wed, 11 Jun 2025 08:24:33 -0700	[thread overview]
Message-ID: <CAJuCfpGueFFKwyhG6Lz44dtJOZbicFoB5S=44GV_oyLUn8oQtA@mail.gmail.com> (raw)
In-Reply-To: <b132dd1f-984b-452e-b19b-18cdecb2842a@linux.dev>

On Tue, Jun 10, 2025 at 10:27 PM Hao Ge <hao.ge@linux.dev> wrote:
>
>
> On 2025/6/10 00:39, Suren Baghdasaryan wrote:
> > On Sun, Jun 8, 2025 at 11:08 PM Hao Ge <hao.ge@linux.dev> wrote:
> >>
> >> On 2025/5/29 15:35, Hao Ge wrote:
> >>> From: Hao Ge <gehao@kylinos.cn>
> >>>
> >>> Recently discovered this entry while checking kallsyms on ARM64:
> >>> ffff800083e509c0 D _shared_alloc_tag
> >>>
> >>> If ARCH_NEEDS_WEAK_PER_CPU is not defined,there's no need to statically
> >>> define the percpu variable alloc_tag_counters.
> >>>
> >>> Therefore,add therelevant macro guards at the appropriate location.
> >>>
> >>> Fixes: 22d407b164ff ("lib: add allocation tagging support for memory allocation profiling")
> >>> 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..d1dab80b70ad 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 ARCH_NEEDS_WEAK_PER_CPU
> >>>    DEFINE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);
> >>>    EXPORT_SYMBOL(_shared_alloc_tag);
> >>> +#endif /* ARCH_NEEDS_WEAK_PER_CPU */
> >>>
> >>>    DEFINE_STATIC_KEY_MAYBE(CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT,
> >>>                        mem_alloc_profiling_key);
> >> Hi Suren
> >>
> >>
> >> I'm sorry to bother you. As mentioned in my commit message,
> >>
> >> in fact, on the ARM64 architecture, the _shared_alloc_tag percpu
> >> variable is not needed.
> >>
> >> In my understanding, it will create a copy for each CPU.
> >>
> >>    The alloc_tag_counters variable will occupy 16 bytes,
> >>
> >> and as the number of CPUs increases, more and more memory will be wasted
> >> in this segment.
> >>
> >> I realized that this modification was a mistake. It resulted in a build
> >> error, and the link is as follows:
> >>
> >> https://lore.kernel.org/all/202506080448.KWN8arrX-lkp@intel.com/
> >>
> >> After I studied the comments of DECLARE_PER_CPU_SECTION, I roughly
> >> understood why this is the case.
> >>
> >> But so far, I haven't come up with a good way to solve this problem. Do
> >> you have any suggestions?
> > Hi Hao,
> > The problem here is that ARCH_NEEDS_WEAK_PER_CPU is not a Kconfig
> > option, it gets defined only on 2 architectures and only when building
> > modules here https://elixir.bootlin.com/linux/v6.15.1/source/arch/alpha/include/asm/percpu.h#L14
> > and here https://elixir.bootlin.com/linux/v6.15.1/source/arch/s390/include/asm/percpu.h#L21.
> > A nicer way to deal with that is to make if a Kconfig option which is
> > enabled only for alpha and s390 and then do something like this:
> >
> >   #if defined(MODULE) && defined(ARCH_NEEDS_WEAK_PER_CPU)
> > #define MODULE_NEEDS_WEAK_PER_CPU
> > #endif
> >
> > and change all the usages of ARCH_NEEDS_WEAK_PER_CPU with
> > MODULE_NEEDS_WEAK_PER_CPU.
> > Did I explain the idea clearly?
> > Thanks,
> > Suren.
> >
> Hi Suren

Hi Hao,

>
> Thanks for your guidance.
> I understand this train of thought.
>
> I've been thinking about a problem: I only added the
> ARCH_NEEDS_WEAK_PER_CPU
>
> macro to isolate the definition of _shared_alloc_tag.
>
> Since s390 defines this macro, why did this build error occur?

The problem is that ARCH_NEEDS_WEAK_PER_CPU is not a Kconfig option,
it's just a definition, for s390 it's here:
https://elixir.bootlin.com/linux/v6.15.1/source/arch/s390/include/asm/percpu.h#L21
So, even for s390 if you are building core kernel code (not a module),
ARCH_NEEDS_WEAK_PER_CPU will be undefined, however if you are building
a module on s390 then it is defined. So, your change effectively
results in _shared_alloc_tag being compiled out in the core kernel
while it's used when you build a module. Therefore during linking
modules can't link to that symbol in the core kernel. Hope this
explains the issue.

The way I would fix this is by making ARCH_NEEDS_WEAK_PER_CPU a
Kconfig option and enable it for s390 and alpha, would replace old
definitions from
https://elixir.bootlin.com/linux/v6.15.1/source/arch/s390/include/asm/percpu.h#L21
and https://elixir.bootlin.com/linux/v6.15.1/source/arch/alpha/include/asm/percpu.h#L14
with:

#if defined(MODULE) && defined(ARCH_NEEDS_WEAK_PER_CPU)
#define MODULE_NEEDS_WEAK_PER_CPU
#endif

Then use MODULE_NEEDS_WEAK_PER_CPU instead of ARCH_NEEDS_WEAK_PER_CPU
in all the current places in the kernel code. Lastly, to compile out
_shared_alloc_tag your current patch should work fine because on s390
and alpha ARCH_NEEDS_WEAK_PER_CPU will be defined after all these
changes.
Does that make sense?

>
> Could you please help explain it again?
>
> Thanks
> Best Regards
> Hao
>
> >>
> >> Thanks
> >>
> >> Best Regards
> >>
> >> Hao
> >>
> >>
> >>
> >>


  reply	other threads:[~2025-06-11 15:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-29  7:35 Hao Ge
2025-06-09  6:08 ` Hao Ge
2025-06-09 16:39   ` Suren Baghdasaryan
2025-06-11  5:26     ` Hao Ge
2025-06-11 15:24       ` Suren Baghdasaryan [this message]
2025-06-12  1:38         ` Hao Ge

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAJuCfpGueFFKwyhG6Lz44dtJOZbicFoB5S=44GV_oyLUn8oQtA@mail.gmail.com' \
    --to=surenb@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=gehao@kylinos.cn \
    --cc=hao.ge@linux.dev \
    --cc=kent.overstreet@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox