mm: Restrict _shared_alloc_tag static definition to CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU
Subsystems (7)
Hao Ge
reviewerv5 cover mm: Restrict _shared_alloc_tag static definition to CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU
From: Hao Ge <[email protected]>
Recently discovered this entry while checking kallsyms on ARM64:ffff800083e509c0 D _shared_alloc_tag
If 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.
Therefore, we need to implement isolation for this purpose.
However, currently ARCH_NEEDS_WEAK_PER_CPU is a #define and is enclosed within the #if defined(MODULE) conditional block.
When building the core kernel code for s390 or alpha architectures, ARCH_NEEDS_WEAK_PER_CPU remains undefined (as it is gated by #if defined(MODULE)). However, when building modules for these architectures, the macro is explicitly defined.
Therefore, we remove all instances of ARCH_NEEDS_WEAK_PER_CPU from the code and introduced CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU to replace the relevant logic. We can now conditionally define the perpcu variable _shared_alloc_tag based on CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU. This allows architectures (such as s390/alpha) that require weak definitions for percpu variables in modules to include the definition, while others can omit it via compile-time exclusion.
The following version can be regarded as the most original version:
https://lore.kernel.org/all/[email protected]/
But unfortunately, it caused build errors on s390.
Based on Suren's guidance and suggestions,
I've refined it into this patch series.
Many thanks to Suren for his patient instruction.
Verify:
1. On Arm64:
nm vmlinux | grep "_shared_alloc_tag",no output is returned.
2. On S390:
Compile tested.
nm vmlinux | grep "_shared_alloc_tag"
00000000015605b4 r __crc__shared_alloc_tag
0000000001585fef r __kstrtab__shared_alloc_tag
0000000001586897 r __kstrtabns__shared_alloc_tag
00000000014f6548 r __ksymtab__shared_alloc_tag
0000000001a8fa28 D _shared_alloc_tag
nm net/ceph/libceph.ko | grep "_shared"
U _shared_alloc_tag
3. On alpha
Compile tested.
nm vmlinux | grep "_shared_alloc_tag"
fffffc0000b080fa r __kstrtab__shared_alloc_tag
fffffc0000b07ee7 r __kstrtabns__shared_alloc_tag
fffffc0000adee98 r __ksymtab__shared_alloc_tag
fffffc0000b83d38 D _shared_alloc_tag
nm crypto/cryptomgr.ko | grep "_share"
U _shared_alloc_tagv5: Regarding the omission of defined(MODULE) in alloc_tag.h where
only #ifdef CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU was used, I apologize for this error.
Please find version 5 attached to address this issue.
v4:
Merge previous patches into a single patch.
Remove all instances of ARCH_MODULE_NEEDS_WEAK_PER_CPU from v3 and use CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU to
replace the relevant logic.
Replace CONFIG_ARCH_NEEDS_WEAK_PER_CPU with CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU in v3, as weak percpu support is only required for modules ,making the name more semantically accurate.
David, Mike, Matthew, Kent, Heiko and Suren have all provided valuable input. Thanks for this.
v3:
Suren pointed out that patches 1-2 can be merged into a single patch in version 2. And the commit message for patch 3 can be made more concise.Make corresponding modifications based on the pointed-out issues and update the corresponding commit message.
v2:
Heiko pointed out that when defining MODULE_NEEDS_WEAK_PER_CPU, the CONFIG_ARCH_NEEDS_WEAK_PER_CPU condition in the v1 version should be removed,as it is always true for s390 and alpha architectures.And He also pointed out that patches 2-4 need to be merged into one patch. Modify the code according to the suggestions and update the corresponding commit message
Hao Ge (1):
mm/percpu: Conditionally define _shared_alloc_tag via
CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU
arch/alpha/Kconfig | 1 + arch/alpha/include/asm/percpu.h | 5 ++--- arch/s390/Kconfig | 1 + arch/s390/include/asm/percpu.h | 5 ++--- include/linux/alloc_tag.h | 6 +++--- include/linux/percpu-defs.h | 7 ++++--- lib/alloc_tag.c | 2 ++ mm/Kconfig | 7 +++++++ 8 files changed, 22 insertions(+), 12 deletions(-)
Signature · 1 line
2.25.1
Hao Ge
reviewerPATCH v5 1/1 mm/percpu: Conditionally define _shared_alloc_tag via CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU
From: Hao Ge <[email protected]>
Recently discovered this entry while checking kallsyms on ARM64:ffff800083e509c0 D _shared_alloc_tag
If 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.
Therefore, we need to implement isolation for this purpose.
When building the core kernel code for s390 or alpha architectures, ARCH_NEEDS_WEAK_PER_CPU remains undefined (as it is gated by #if defined(MODULE)). However, when building modules for these architectures, the macro is explicitly defined.
Therefore, we remove all instances of ARCH_NEEDS_WEAK_PER_CPU from the code and introduced CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU to replace the relevant logic. We can now conditionally define the perpcu variable _shared_alloc_tag based on CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU. This allows architectures (such as s390/alpha) that require weak definitions for percpu variables in modules to include the definition, while others can omit it via compile-time exclusion.
--- arch/alpha/Kconfig | 1 + arch/alpha/include/asm/percpu.h | 5 ++--- arch/s390/Kconfig | 1 + arch/s390/include/asm/percpu.h | 5 ++--- include/linux/alloc_tag.h | 6 +++--- include/linux/percpu-defs.h | 7 ++++--- lib/alloc_tag.c | 2 ++ mm/Kconfig | 7 +++++++ 8 files changed, 22 insertions(+), 12 deletions(-)
arch/alpha/Kconfig +1 -0
@@ -7,6 +7,7 @@ config ALPHA select ARCH_HAS_DMA_OPS if PCI select ARCH_MIGHT_HAVE_PC_PARPORT select ARCH_MIGHT_HAVE_PC_SERIO + select ARCH_MODULE_NEEDS_WEAK_PER_CPU if SMP select ARCH_NO_PREEMPT select ARCH_NO_SG_CHAIN select ARCH_USE_CMPXCHG_LOCKREF
arch/alpha/include/asm/percpu.h +2 -3
@@ -9,10 +9,9 @@* way above 4G. * * Always use weak definitions for percpu variables in modules. +* Therefore, we have enabled CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU +* in the Kconfig. */ -#if defined(MODULE ) && defined(CONFIG_SMP ) -#define ARCH_NEEDS_WEAK_PER_CPU -#endif #include <asm-generic/percpu.h>
arch/s390/Kconfig +1 -0
@@ -132,6 +132,7 @@ config S390 select ARCH_INLINE_WRITE_UNLOCK_IRQ select ARCH_INLINE_WRITE_UNLOCK_IRQRESTORE select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE + select ARCH_MODULE_NEEDS_WEAK_PER_CPU select ARCH_STACKWALK select ARCH_SUPPORTS_ATOMIC_RMW select ARCH_SUPPORTS_DEBUG_PAGEALLOC
arch/s390/include/asm/percpu.h +2 -3
@@ -16,10 +16,9 @@* For 64 bit module code, the module may be more than 4G above the * per cpu area, use weak definitions to force the compiler to * generate external references. +* Therefore, we have enabled CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU +* in the Kconfig. */ -#if defined(MODULE ) -#define ARCH_NEEDS_WEAK_PER_CPU -#endif /* * We use a compare-and-swap loop since that uses less cpu cycles than
include/linux/alloc_tag.h +3 -3
@@ -88,7 +88,7 @@ static inline struct alloc_tag *ct_to_alloc_tag(struct codetag *ct) return container_of(ct, struct alloc_tag, ct); } -#ifdef ARCH_NEEDS_WEAK_PER_CPU +#if defined(CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU ) && defined(MODULE )/* * When percpu variables are required to be defined as weak, static percpu * variables can't be used inside a function (see comments for DECLARE_PER_CPU_SECTION).
@@ -102,7 +102,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag); .ct = CODE_TAG_INIT, \ .counters = &_shared_alloc_tag }; -#else /* ARCH_NEEDS_WEAK_PER_CPU */ +#else /* CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU && MODULE */ #ifdef MODULE
@@ -123,7 +123,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);#endif /* MODULE */ -#endif /* ARCH_NEEDS_WEAK_PER_CPU */ +#endif /* CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU && MODULE */ DECLARE_STATIC_KEY_MAYBE (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , mem_alloc_profiling_key);
include/linux/percpu-defs.h +4 -3
@@ -63,14 +63,15 @@* 1. The symbol must be globally unique, even the static ones. * 2. Static percpu variables cannot be defined inside a function. * -* Archs which need weak percpu definitions should define -* ARCH_NEEDS_WEAK_PER_CPU in asm/percpu.h when necessary. +* Archs which need weak percpu definitions should set +* CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU when necessary. * * To ensure that the generic code observes the above two * restrictions, if CONFIG_DEBUG_FORCE_WEAK_PER_CPU is set weak * definition is used for all cases. */ -#if defined(ARCH_NEEDS_WEAK_PER_CPU ) || defined(CONFIG_DEBUG_FORCE_WEAK_PER_CPU ) +#if defined(CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU ) && defined(MODULE ) || \ + defined(CONFIG_DEBUG_FORCE_WEAK_PER_CPU )/* * __pcpu_scope_* dummy variable is used to enforce scope. It * receives the static modifier when it's used in front of
lib/alloc_tag.c +2 -0
@@ -24,8 +24,10 @@ static bool mem_profiling_support;static struct codetag_type *alloc_tag_cttype; +#ifdef CONFIG_ARCH_MODULE_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);
mm/Kconfig +7 -0
@@ -929,6 +929,13 @@ config ARCH_SUPPORTS_PUD_PFNMAP def_bool y depends on ARCH_SUPPORTS_HUGE_PFNMAP && HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +# +# Architectures that always use weak definitions for percpu +# variables in modules should set this. +# +config ARCH_MODULE_NEEDS_WEAK_PER_CPU + bool + # # UP and nommu archs use km based percpu allocator #
Signature · 1 line
2.25.1
Re: [PATCH v5 1/1] mm/percpu: Conditionally define _shared_alloc_tag via CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU
On Tue, Jun 17, 2025 at 03:10:52PM +0800, Hao Ge wrote:
Hi Hao,
Quoted 36 lines
From: Hao Ge <[email protected]>
Recently discovered this entry while checking kallsyms on ARM64:
ffff800083e509c0D_shared_alloc_tagIf
ARCH_NEEDS_WEAK_PER_CPUis not defined(it is only defined for s390 and alpha architectures), there's no need to statically define the percpu variable_shared_alloc_tag.Therefore, we need to implement isolation for this purpose.
When building the core kernel code for s390 or alpha architectures,
ARCH_NEEDS_WEAK_PER_CPUremains undefined (as it is gated by #if defined(MODULE)). However, when building modules for these architectures, the macro is explicitly defined.Therefore, we remove all instances of
ARCH_NEEDS_WEAK_PER_CPUfrom the code and introducedCONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPUto replace the relevant logic. We can now conditionally define the perpcu variable_shared_alloc_tagbased onCONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU. This allows architectures (such as s390/alpha) that require weak definitions for percpu variables in modules to include the definition, while others can omit it via compile-time exclusion.Suggested-by Suren Baghdasaryan <[email protected]>Signed-off-by Hao Ge <[email protected]>--- arch/alpha/Kconfig | 1 + arch/alpha/include/asm/percpu.h | 5 ++--- arch/s390/Kconfig | 1 + arch/s390/include/asm/percpu.h | 5 ++--- include/linux/alloc_tag.h | 6 +++--- include/linux/percpu-defs.h | 7 ++++--- lib/alloc_tag.c | 2 ++ mm/Kconfig | 7 +++++++ 8 files changed, 22 insertions(+), 12 deletions(-)
...
Thanks!
Suren Baghdasaryan
maintainerRe: [PATCH v5 1/1] mm/percpu: Conditionally define _shared_alloc_tag via CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU
In reply to Hao GeQuoted 137 lines · Hao Ge
From: Hao Ge <[email protected]>
Recently discovered this entry while checking kallsyms on ARM64:
ffff800083e509c0D_shared_alloc_tagIf
ARCH_NEEDS_WEAK_PER_CPUis not defined(it is only defined for s390 and alpha architectures), there's no need to statically define the percpu variable_shared_alloc_tag.Therefore, we need to implement isolation for this purpose.
When building the core kernel code for s390 or alpha architectures,
ARCH_NEEDS_WEAK_PER_CPUremains undefined (as it is gated by #if defined(MODULE)). However, when building modules for these architectures, the macro is explicitly defined.Therefore, we remove all instances of
ARCH_NEEDS_WEAK_PER_CPUfrom the code and introducedCONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPUto replace the relevant logic. We can now conditionally define the perpcu variable_shared_alloc_tagbased onCONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU. This allows architectures (such as s390/alpha) that require weak definitions for percpu variables in modules to include the definition, while others can omit it via compile-time exclusion.Suggested-by Suren Baghdasaryan <[email protected]>Signed-off-by Hao Ge <[email protected]>--- arch/alpha/Kconfig | 1 + arch/alpha/include/asm/percpu.h | 5 ++--- arch/s390/Kconfig | 1 + arch/s390/include/asm/percpu.h | 5 ++--- include/linux/alloc_tag.h | 6 +++--- include/linux/percpu-defs.h | 7 ++++--- lib/alloc_tag.c | 2 ++ mm/Kconfig | 7 +++++++ 8 files changed, 22 insertions(+), 12 deletions(-)arch/alpha/Kconfig +1
-0@@ -7,6 +7,7 @@ config ALPHA select ARCH_HAS_DMA_OPS if PCI select ARCH_MIGHT_HAVE_PC_PARPORT select ARCH_MIGHT_HAVE_PC_SERIO + select ARCH_MODULE_NEEDS_WEAK_PER_CPU if SMP select ARCH_NO_PREEMPT select ARCH_NO_SG_CHAIN select ARCH_USE_CMPXCHG_LOCKREFarch/alpha/include/asm/percpu.h +2
-3@@ -9,10 +9,9 @@* way above 4G. * * Always use weak definitions for percpu variables in modules. +* Therefore, we have enabled CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU +* in the Kconfig. */ -#if defined(MODULE ) && defined(CONFIG_SMP ) -#define ARCH_NEEDS_WEAK_PER_CPU -#endif #include <asm-generic/percpu.h> arch/s390/Kconfig +1
-0@@ -132,6 +132,7 @@ config S390 select ARCH_INLINE_WRITE_UNLOCK_IRQ select ARCH_INLINE_WRITE_UNLOCK_IRQRESTORE select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE + select ARCH_MODULE_NEEDS_WEAK_PER_CPU select ARCH_STACKWALK select ARCH_SUPPORTS_ATOMIC_RMW select ARCH_SUPPORTS_DEBUG_PAGEALLOCarch/s390/include/asm/percpu.h +2
-3@@ -16,10 +16,9 @@* For 64 bit module code, the module may be more than 4G above the * per cpu area, use weak definitions to force the compiler to * generate external references. +* Therefore, we have enabled CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU +* in the Kconfig. */ -#if defined(MODULE ) -#define ARCH_NEEDS_WEAK_PER_CPU -#endif /* * We use a compare-and-swap loop since that uses less cpu cycles than include/linux/alloc_tag.h +3
-3@@ -88,7 +88,7 @@ static inline struct alloc_tag *ct_to_alloc_tag(struct codetag *ct) return container_of(ct, struct alloc_tag, ct); } -#ifdef ARCH_NEEDS_WEAK_PER_CPU +#if defined(CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU ) && defined(MODULE )/* * When percpu variables are required to be defined as weak, static percpu * variables can't be used inside a function (see comments for DECLARE_PER_CPU_SECTION). @@ -102,7 +102,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag); .ct = CODE_TAG_INIT, \ .counters = &_shared_alloc_tag }; -#else /* ARCH_NEEDS_WEAK_PER_CPU */ +#else /* CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU && MODULE */ #ifdef MODULE @@ -123,7 +123,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);#endif /* MODULE */ -#endif /* ARCH_NEEDS_WEAK_PER_CPU */ +#endif /* CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU && MODULE */ DECLARE_STATIC_KEY_MAYBE (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , mem_alloc_profiling_key);include/linux/percpu-defs.h +0
-2@@ -63,14 +63,15 @@* 1. The symbol must be globally unique, even the static ones. * 2. Static percpu variables cannot be defined inside a function. * -* Archs which need weak percpu definitions should define -* ARCH_NEEDS_WEAK_PER_CPU in asm/percpu.h when necessary.
include/linux/percpu-defs.h line 74
Quoted 10 lines
include/linux/percpu-defs.h +4
-1+* Archs which need weak percpu definitions should set +* CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU when necessary. * * To ensure that the generic code observes the above two * restrictions, if CONFIG_DEBUG_FORCE_WEAK_PER_CPU is set weak * definition is used for all cases. */ -#if defined(ARCH_NEEDS_WEAK_PER_CPU ) || defined(CONFIG_DEBUG_FORCE_WEAK_PER_CPU ) +#if defined(CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU ) && defined(MODULE ) || \ + defined(CONFIG_DEBUG_FORCE_WEAK_PER_CPU )
Please enclose defined(CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU) && defined(MODULE) part of the condition in parentheses.
Quoted 39 lines
include/linux/percpu-defs.h +0
-0/* * __pcpu_scope_* dummy variable is used to enforce scope. It * receives the static modifier when it's used in front of lib/alloc_tag.c +2
-0@@ -24,8 +24,10 @@ static bool mem_profiling_support;static struct codetag_type *alloc_tag_cttype; +#ifdef CONFIG_ARCH_MODULE_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);mm/Kconfig +7
-1@@ -929,6 +929,13 @@ config ARCH_SUPPORTS_PUD_PFNMAP def_bool y depends on ARCH_SUPPORTS_HUGE_PFNMAP && HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +# +# Architectures that always use weak definitions for percpu +# variables in modules should set this. +# +config ARCH_MODULE_NEEDS_WEAK_PER_CPU + bool + # # UP and nommu archs use km based percpu allocator # --2.25.1