Skip to contentSkip to conversation

alloc_tag: Use __ prefixed ELF section names

PATCH Superseded Ard Biesheuvel 4 participants 4 messages Nov 18 – Nov 22

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 alloc_tag: Use __ prefixed ELF section names

From: Ard Biesheuvel <[email protected]>

The compiler will emit static relocations related to a section .foo into a separate section called .rela.foo, i.e., it just appends the section name to the string ".rela"

When section names start with . or __, this results in relocation section names that are correctly matched by the various pattern rules in the various linker scripts across the tree.

Without any such leading delimiter, it may lead to spurious warnings such as

Quoted 1 line
Quoted 1 line

ld: warning: orphan section .relaalloc_tags' from init/main.o' being placed in section `.rela.dyn'

ld: warning: dot moved backwards before `.rela.dyn'
ld: .tmp_vmlinux1: section `.rela.dyn' can't be allocated in segment 1

Fix this by renaming the section to __alloc_tags. While at it, tweak the headers so that the definition of the name appears only a single time.

Cc Suren Baghdasaryan <[email protected]>
Cc Kent Overstreet <[email protected]>
Cc Hao Ge <[email protected]>
---
 include/asm-generic/codetag.lds.h | 14 +++++++++-----
 include/linux/alloc_tag.h         | 11 ++++++-----
 mm/alloc_tag.c                    |  6 +++---
 3 files changed, 18 insertions(+), 13 deletions(-)
include/asm-generic/codetag.lds.h +9 -5
index 4948e5d4e9d9..443e3a8fa60b 100644
@@ -2,6 +2,10 @@
 #ifndef __ASM_GENERIC_CODETAG_LDS_H
 #define __ASM_GENERIC_CODETAG_LDS_H
 
+#include <linux/compiler_types.h>
+
+#define ALLOC_TAG_SECTION_NAME	__alloc_tags
+
 #ifdef CONFIG_MEM_ALLOC_PROFILING
 #define IF_MEM_ALLOC_PROFILING(...) __VA_ARGS__
 #else
@@ -10,15 +14,15 @@
 
 #define SECTION_WITH_BOUNDARIES(_name)	\
 	. = ALIGN(8);			\
-	__start_##_name = .;		\
+	__PASTE(__start_, _name) = .;	\
 	KEEP(*(_name))			\
-	__stop_##_name = .;
+	__PASTE(__stop_, _name) = .;
 
 #define CODETAG_SECTIONS()		\
-	IF_MEM_ALLOC_PROFILING(SECTION_WITH_BOUNDARIES(alloc_tags))
+	IF_MEM_ALLOC_PROFILING(SECTION_WITH_BOUNDARIES(ALLOC_TAG_SECTION_NAME))
 
 #define MOD_SEPARATE_CODETAG_SECTION(_name)	\
-	.codetag.##_name 0 : {			\
+	.codetag._name 0 : {			\
 		SECTION_WITH_BOUNDARIES(_name)	\
 	}
 
@@ -28,6 +32,6 @@
  * unload them individually once unused.
  */
 #define MOD_SEPARATE_CODETAG_SECTIONS()		\
-	IF_MEM_ALLOC_PROFILING(MOD_SEPARATE_CODETAG_SECTION(alloc_tags))
+	IF_MEM_ALLOC_PROFILING(MOD_SEPARATE_CODETAG_SECTION(ALLOC_TAG_SECTION_NAME))
 
 #endif /* __ASM_GENERIC_CODETAG_LDS_H */
include/linux/alloc_tag.h +6 -5
index 7f2d80a59792..0703592a3302 100644
@@ -15,6 +15,9 @@
 #include <linux/static_key.h>
 #include <linux/irqflags.h>
 
+/* for ALLOC_TAG_SECTION_NAME */
+#include <asm-generic/codetag.lds.h>
+
 struct alloc_tag_counters {
 	u64 bytes;
 	u64 calls;
@@ -74,8 +77,6 @@ static inline void set_codetag_empty(union codetag_ref *ref)
 
 #ifdef CONFIG_MEM_ALLOC_PROFILING
 
-#define ALLOC_TAG_SECTION_NAME	"alloc_tags"
-
 struct codetag_bytes {
 	struct codetag *ct;
 	s64 bytes;
@@ -98,7 +99,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);
 
 #define DEFINE_ALLOC_TAG(_alloc_tag)						\
 	static struct alloc_tag _alloc_tag __used __aligned(8)			\
-	__section(ALLOC_TAG_SECTION_NAME) = {					\
+	__section(__stringify(ALLOC_TAG_SECTION_NAME)) = {			\
 		.ct = CODE_TAG_INIT,						\
 		.counters = &_shared_alloc_tag };
 
@@ -108,7 +109,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);
 
 #define DEFINE_ALLOC_TAG(_alloc_tag)						\
 	static struct alloc_tag _alloc_tag __used __aligned(8)			\
-	__section(ALLOC_TAG_SECTION_NAME) = {					\
+	__section(__stringify(ALLOC_TAG_SECTION_NAME)) = {			\
 		.ct = CODE_TAG_INIT,						\
 		.counters = NULL };
 
@@ -117,7 +118,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);
 #define DEFINE_ALLOC_TAG(_alloc_tag)						\
 	static DEFINE_PER_CPU(struct alloc_tag_counters, _alloc_tag_cntr);	\
 	static struct alloc_tag _alloc_tag __used __aligned(8)			\
-	__section(ALLOC_TAG_SECTION_NAME) = {					\
+	__section(__stringify(ALLOC_TAG_SECTION_NAME)) = {			\
 		.ct = CODE_TAG_INIT,						\
 		.counters = &_alloc_tag_cntr };
 
mm/alloc_tag.c +3 -3
index 82e2c3448dcf..4c13483e68b6 100644
@@ -23,8 +23,8 @@
 
 #define ALLOCINFO_FILE_NAME		"allocinfo"
 #define MODULE_ALLOC_TAG_VMAP_SIZE	(100000UL * sizeof(struct alloc_tag))
-#define SECTION_START(NAME)		(CODETAG_SECTION_START_PREFIX NAME)
-#define SECTION_STOP(NAME)		(CODETAG_SECTION_STOP_PREFIX NAME)
+#define SECTION_START(NAME)		(CODETAG_SECTION_START_PREFIX #NAME)
+#define SECTION_STOP(NAME)		(CODETAG_SECTION_STOP_PREFIX #NAME)
 
 #ifdef CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT
 static bool mem_profiling_support = true;
@@ -1322,7 +1322,7 @@ static inline void sysctl_init(void) {}
 static int __init alloc_tag_init(void)
 {
 	const struct codetag_type_desc desc = {
-		.section		= ALLOC_TAG_SECTION_NAME,
+		.section		= __stringify(ALLOC_TAG_SECTION_NAME),
 		.tag_size		= sizeof(struct alloc_tag),
 #ifdef CONFIG_MODULES
 		.needs_section_mem	= needs_section_mem,
Signature · 1 line
2.56.1.703.gf848ed807c-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] alloc_tag: SECTION_START() and SECTION_STOP() fail to expand macro argument - [Low] alloc_tag…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] alloc_tag: SECTION_START() and SECTION_STOP() fail to expand macro argument
- [Low] alloc_tag: missing Fixes: trailer for unprefixed alloc_tags section name
--

commit 933587991e6a16ab600445b22347397f80335de9
Author: Ard Biesheuvel <[email protected]>

Quoted 17 lines

alloc_tag: Use __ prefixed ELF section names

The compiler will emit static relocations related to a section .foo into a separate section called .rela.foo, i.e., it just appends the section name to the string ".rela"

When section names start with . or __, this results in relocation section names that are correctly matched by the various pattern rules in the various linker scripts across the tree.

Without any such leading delimiter, it may lead to spurious warnings such as

Quoted 1 line
Quoted 1 line

ld: warning: orphan section .relaalloc_tags' from init/main.o' being placed in section `.rela.dyn'

ld: warning: dot moved backwards before `.rela.dyn'
ld: .tmp_vmlinux1: section `.rela.dyn' can't be allocated in segment 1

Quoted 7 lines

Fix this by renaming the section to __alloc_tags. While at it, tweak the headers so that the definition of the name appears only a single time.

Cc Suren Baghdasaryan <[email protected]>
Cc Kent Overstreet <[email protected]>
Cc Hao Ge <[email protected]>

[Severity: Low]
This fixes the unprefixed alloc_tags section name. Should it include:

Fixes 22d407b164ff ("lib: add allocation tagging support for memory allocation profiling")

mm/alloc_tag.c line 30

Quoted 12 lines
mm/alloc_tag.c +2 -2
index e9b70268eacb0..00a68ab0c409a 100644
@@ -26,8 +26,8 @@
 
 #define ALLOCINFO_FILE_NAME		"allocinfo"
 #define MODULE_ALLOC_TAG_VMAP_SIZE	(100000UL * sizeof(struct alloc_tag))
-#define SECTION_START(NAME)		(CODETAG_SECTION_START_PREFIX NAME)
-#define SECTION_STOP(NAME)		(CODETAG_SECTION_STOP_PREFIX NAME)
+#define SECTION_START(NAME)		(CODETAG_SECTION_START_PREFIX #NAME)
+#define SECTION_STOP(NAME)		(CODETAG_SECTION_STOP_PREFIX #NAME)

[Severity: High]
Does #NAME in SECTION_START() and SECTION_STOP() stringify the macro name "ALLOC_TAG_SECTION_NAME" instead of expanding it to "__alloc_tags"?

In alloc_tag_sec_init(), kallsyms_lookup_name() will look up "__start_ALLOC_TAG_SECTION_NAME" and "__stop_ALLOC_TAG_SECTION_NAME" instead of "__start___alloc_tags" and "__stop___alloc_tags", returning 0 for both. When compressed page allocation profiling is enabled (sysctl.vm.mem_profiling=1,compressed), kernel_tags.count remains 0, causing idx_to_ref() and ref_to_idx() to misclassify built-in kernel tags as module tags and dereference an invalid module_tags.first_tag pointer on page allocation or free.

Using __stringify(NAME) instead of #NAME would expand the argument first.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 36 lines · On Thu, 19 Nov 2026, at 14:27, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] alloc_tag: SECTION_START() and SECTION_STOP() fail to expand macro argument
- [Low] alloc_tag: missing Fixes: trailer for unprefixed alloc_tags section name
--

commit 933587991e6a16ab600445b22347397f80335de9
Author: Ard Biesheuvel <[email protected]>

Quoted 24 lines

alloc_tag: Use __ prefixed ELF section names

The compiler will emit static relocations related to a section .foo into a separate section called .rela.foo, i.e., it just appends the section name to the string ".rela"

When section names start with . or __, this results in relocation section names that are correctly matched by the various pattern rules in the various linker scripts across the tree.

Without any such leading delimiter, it may lead to spurious warnings such as

Quoted 1 line
Quoted 1 line

ld: warning: orphan section .relaalloc_tags' from init/main.o' being placed in section `.rela.dyn'

ld: warning: dot moved backwards before `.rela.dyn'
ld: .tmp_vmlinux1: section `.rela.dyn' can't be allocated in segment 1

Fix this by renaming the section to __alloc_tags. While at it, tweak the headers so that the definition of the name appears only a single time.

Cc Suren Baghdasaryan <[email protected]>
Cc Kent Overstreet <[email protected]>
Cc Hao Ge <[email protected]>

...

Quoted 12 lines

Does #NAME in SECTION_START() and SECTION_STOP() stringify the macro name "ALLOC_TAG_SECTION_NAME" instead of expanding it to "__alloc_tags"?

In alloc_tag_sec_init(), kallsyms_lookup_name() will look up "__start_ALLOC_TAG_SECTION_NAME" and "__stop_ALLOC_TAG_SECTION_NAME" instead of "__start___alloc_tags" and "__stop___alloc_tags", returning 0 for both. When compressed page allocation profiling is enabled (sysctl.vm.mem_profiling=1,compressed), kernel_tags.count remains 0, causing idx_to_ref() and ref_to_idx() to misclassify built-in kernel tags as module tags and dereference an invalid module_tags.first_tag pointer on page allocation or free.

Quoted 2 lines

Using __stringify(NAME) instead of #NAME would expand the argument first.

Ugh the token pasting stuff always confuses me - I'll fold this in for v2.

maintainer Acked-by
#3 lore raw
Quoted 53 lines · Ard Biesheuvel
Quoted 36 lines · On Thu, 19 Nov 2026, at 14:27, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] alloc_tag: SECTION_START() and SECTION_STOP() fail to expand macro argument
- [Low] alloc_tag: missing Fixes: trailer for unprefixed alloc_tags section name
--

commit 933587991e6a16ab600445b22347397f80335de9
Author: Ard Biesheuvel <[email protected]>

Quoted 24 lines

alloc_tag: Use __ prefixed ELF section names

The compiler will emit static relocations related to a section .foo into a separate section called .rela.foo, i.e., it just appends the section name to the string ".rela"

When section names start with . or __, this results in relocation section names that are correctly matched by the various pattern rules in the various linker scripts across the tree.

Without any such leading delimiter, it may lead to spurious warnings such as

Quoted 1 line
Quoted 1 line

ld: warning: orphan section .relaalloc_tags' from init/main.o' being placed in section `.rela.dyn'

ld: warning: dot moved backwards before `.rela.dyn'
ld: .tmp_vmlinux1: section `.rela.dyn' can't be allocated in segment 1

Fix this by renaming the section to __alloc_tags. While at it, tweak the headers so that the definition of the name appears only a single time.

Cc Suren Baghdasaryan <[email protected]>
Cc Kent Overstreet <[email protected]>
Cc Hao Ge <[email protected]>

...

Quoted 12 lines

Does #NAME in SECTION_START() and SECTION_STOP() stringify the macro name "ALLOC_TAG_SECTION_NAME" instead of expanding it to "__alloc_tags"?

In alloc_tag_sec_init(), kallsyms_lookup_name() will look up "__start_ALLOC_TAG_SECTION_NAME" and "__stop_ALLOC_TAG_SECTION_NAME" instead of "__start___alloc_tags" and "__stop___alloc_tags", returning 0 for both. When compressed page allocation profiling is enabled (sysctl.vm.mem_profiling=1,compressed), kernel_tags.count remains 0, causing idx_to_ref() and ref_to_idx() to misclassify built-in kernel tags as module tags and dereference an invalid module_tags.first_tag pointer on page allocation or free.

Quoted 4 lines
Quoted 2 lines

Using __stringify(NAME) instead of #NAME would expand the argument first.

Ugh the token pasting stuff always confuses me - I'll fold this in for v2.

With that change, feel free to add:

Thanks!

Quoted 1 line