Skip to contentSkip to conversation

alloc_tag and module codetag section fixes

PATCH Superseded Hao Ge 4 participants 18 messages Sep 2 – Sep 8

Subsystems (4)

v7 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Hao Ge

author reviewer
#0 lore raw

v7 cover alloc_tag and module codetag section fixes

I ran into an overflow problem in the module tag area. With profiling toggled off, the overflow check in reserve_module_tags() did not run, a module could load with more tags than the page flags can address, and re-enabling profiling then silently corrupted /proc/allocinfo. On overflow the fix shuts profiling down, releases the reservation and returns -EAGAIN, and the codetag section lands as regular module data in the same load, so the module loads without profiling.

Review of the earlier series by Sashiko turned up two more problems.

One is a race. layout_sections() and move_module() both asked codetag_needs_module_section() where a codetag section goes, and mem_profiling_support can change between the two calls, for instance when another module load overflows the tag index and shuts profiling down. move_module() then copied the codetag section to offset 0 of its regular destination and clobbered the first section placed in that region.

v7 reworks where codetag sections are allocated, on a prototype by Petr Pavlu [1]. The allocation runs before layout_sections() and the placement is decided in one step, so nothing re-asks the question and the race is gone. The retry is gone too, on -EAGAIN the section is laid out as regular module data right in the same load.

Following review feedback from Petr and Suren the series is now split into four patches.

Patch 1 moves release_module_tags() above reserve_module_tags(), since the failure paths now have to call it.

Patch 2 introduces SH_ENTSIZE_STANDALONE to mark sections with a separate allocation, per the ELF spec SHF_ALLOC means a section occupies memory during execution, and the percpu section does, only outside the regular module layout. It was previously excluded from the layout by clearing its SHF_ALLOC, overloading the flag with a loader-internal meaning. The mark lives in sh_entsize now, find_sec(".data..percpu") gives stable results again and apply_relocations() goes back to testing only SHF_ALLOC.

Patch 3 moves the codetag allocation out of move_module() in front of layout_sections(). On overflow reserve_module_tags() shuts profiling down, releases the reservation and returns -EAGAIN, and the section is laid out as regular module data, so the module loads without profiling instead of failing. Any other error fails the load. The release and the fallback belong together, without the release rmmod hits the stale entry and panics.

Patch 4, split out per Suren's suggestion, releases the reservation when vm_module_tags_populate() fails, so that path stops leaking on its own.

Tested on an x86_64 virtual machine:

Booted without sysctl.vm.mem_profiling=1,compressed:

# cat /proc/allocinfo is fine

Booted with sysctl.vm.mem_profiling=1,compressed:

# cat /proc/allocinfo is fine
# insmod overflow_tag.ko
# dmesg
  With module overflow_tag there are too many tags to fit in 13 page
  flag bits. Memory allocation profiling is disabled!
# rmmod overflow_tag
The module loads without profiling and unloads cleanly.

Changes in v7:
- split the rework following review feedback (Petr Pavlu, Suren Baghdasaryan)
- new patch 2 marks separately allocated sections with SH_ENTSIZE_STANDALONE instead of clearing SHF_ALLOC (suggested by Petr Pavlu)
- split the populate failure release into its own patch (suggested by Suren Baghdasaryan)

Changes in v6:
- rework on Petr's prototype and allocate codetag sections before layout_sections(), the retry and its state resets are gone
- fix the layout_sections()/move_module() race (Found by Sashiko)
- release the reservation on populate failure as well (Found by Sashiko)
- only -EAGAIN keeps the fallback, other errors fail the load

Changes in v5:
- add Fixes: and Cc: stable to patch 1/2 as well, since 2/2 does not compile without it (Andrew Morton)
- restore frob-adjusted mem[type].size on retry instead of zeroing, as s390 and parisc add GOT/PLT space there in module_frob_arch_sections() (Reported by Sashiko)
- drop the load_module() mem_profiling_support check; the percpu counter leak is pre-existing and orthogonal to this fix

Changes in v4:
- add a new patch (1/2) to move release_module_tags() above reserve_module_tags(); the overflow fix is 2/2
- release the reservation on the -EAGAIN path
- return -EAGAIN instead of -ENOMEM so the module can still load without profiling (Suren)
- reset sh_addr, mem[type].size and sym/str SHF_ALLOC before retry
- skip percpu counters in load_module() when profiling is off

Changes in v3:
- use pr_warn_once() instead of pr_warn()
- return -ENOMEM instead of -ENOSPC (Suren)
- expand the commit message to describe the /proc/allocinfo impact (Andrew)

Changes in v2:
- return an error after shutdown_mem_profiling() to skip vm_module_tags_populate()

v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v5: https://lore.kernel.org/all/[email protected]/
v6: https://lore.kernel.org/all/[email protected]/

Hao Ge (4):
alloc_tag: move release_module_tags() above reserve_module_tags()
module: introduce SH_ENTSIZE_STANDALONE for separately allocated
sections
module: allocate codetag sections before the regular module layout
alloc_tag: release the reservation when populate fails

 include/linux/module.h   |   2 +
 kernel/module/internal.h |   8 +++
 kernel/module/main.c     | 130 +++++++++++++++++++--------------------
 mm/alloc_tag.c           | 101 +++++++++++++++---------------
 4 files changed, 127 insertions(+), 114 deletions(-)
Signature · 1 line
2.25.1
author reviewer
#1 lore raw

PATCH v7 1/4 alloc_tag: move release_module_tags() above reserve_module_tags()

release_module_tags() is a cleanup helper. reserve_module_tags() can also fail after storing the reservation in the maple tree, in which case it should call release_module_tags() to undo it. Move the helper above reserve_module_tags() so no forward declaration is needed.

No functional change.

Fixes 4835f747d3ed ("alloc_tag: support for page allocation tag compression")
Fixes 0f9b685626da ("alloc_tag: populate memory for module tags as needed")
---
 mm/alloc_tag.c | 92 +++++++++++++++++++++++++-------------------------
 1 file changed, 46 insertions(+), 46 deletions(-)
mm/alloc_tag.c +46 -46
index b1d48532a25a..e7a79116ad81 100644
@@ -843,6 +843,52 @@ static int vm_module_tags_populate(void)
 	return 0;
 }
 
+static void release_module_tags(struct module *mod, bool used)
+{
+	MA_STATE(mas, &mod_area_mt, module_tags.size, module_tags.size);
+	struct alloc_tag *start_tag;
+	struct alloc_tag *end_tag;
+	struct module *val;
+
+	mas_lock(&mas);
+	mas_for_each_rev(&mas, val, 0)
+		if (val == mod)
+			break;
+
+	if (!val) /* module not found */
+		goto out;
+
+	if (!used)
+		goto release_area;
+
+	start_tag = (struct alloc_tag *)(module_tags.start_addr + mas.index);
+	end_tag = (struct alloc_tag *)(module_tags.start_addr + mas.last);
+	if (!clean_unused_counters(start_tag, end_tag)) {
+		struct alloc_tag *tag;
+
+		for (tag = start_tag; tag <= end_tag; tag++) {
+			struct alloc_tag_counters counter;
+
+			if (!tag->counters)
+				continue;
+
+			counter = alloc_tag_read(tag);
+			pr_info("%s:%u module %s func:%s has %llu allocated at module unload\n",
+				tag->ct.filename, tag->ct.lineno, tag->ct.modname,
+				tag->ct.function, counter.bytes);
+		}
+	} else {
+		used = false;
+	}
+release_area:
+	mas_store(&mas, used ? &unloaded_mod : NULL);
+	val = mas_prev_range(&mas, 0);
+	if (val == &prepend_mod)
+		mas_store(&mas, NULL);
+out:
+	mas_unlock(&mas);
+}
+
 static void *reserve_module_tags(struct module *mod, unsigned long size,
 				 unsigned int prepend, unsigned long align)
 {
@@ -930,52 +976,6 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
 	return (struct alloc_tag *)(module_tags.start_addr + offset);
 }
 
-static void release_module_tags(struct module *mod, bool used)
-{
-	MA_STATE(mas, &mod_area_mt, module_tags.size, module_tags.size);
-	struct alloc_tag *start_tag;
-	struct alloc_tag *end_tag;
-	struct module *val;
-
-	mas_lock(&mas);
-	mas_for_each_rev(&mas, val, 0)
-		if (val == mod)
-			break;
-
-	if (!val) /* module not found */
-		goto out;
-
-	if (!used)
-		goto release_area;
-
-	start_tag = (struct alloc_tag *)(module_tags.start_addr + mas.index);
-	end_tag = (struct alloc_tag *)(module_tags.start_addr + mas.last);
-	if (!clean_unused_counters(start_tag, end_tag)) {
-		struct alloc_tag *tag;
-
-		for (tag = start_tag; tag <= end_tag; tag++) {
-			struct alloc_tag_counters counter;
-
-			if (!tag->counters)
-				continue;
-
-			counter = alloc_tag_read(tag);
-			pr_info("%s:%u module %s func:%s has %llu allocated at module unload\n",
-				tag->ct.filename, tag->ct.lineno, tag->ct.modname,
-				tag->ct.function, counter.bytes);
-		}
-	} else {
-		used = false;
-	}
-release_area:
-	mas_store(&mas, used ? &unloaded_mod : NULL);
-	val = mas_prev_range(&mas, 0);
-	if (val == &prepend_mod)
-		mas_store(&mas, NULL);
-out:
-	mas_unlock(&mas);
-}
-
 static int load_module(struct module *mod, struct codetag *start, struct codetag *stop)
 {
 	/* Allocate module alloc_tag percpu counters */
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v7 1/4] alloc_tag: move release_module_tags() above reserve_module_tags()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The release_module_tags() function modifies the mod_area_mt maple tree while holding mas_lock (a spinlock), which forces mas_store() to use GFP_NOWAIT for internal node allocations. If the tree needs to allocate a node (e.g. to merge adjacent NULL slots via mas_wr_extend_null()) and memory pressure causes the allocation to fail, mas_store() silently aborts and leaves the maple state in an error condition. Because release_module_tags() does not check mas_is_err(&mas) or preallocate nodes, the mod pointer remains in the tree after the module is freed. If a subsequently loaded module reuses the exact same struct module * address and then is unloaded, release_module_tags() will match the stale pointer. This causes it to process the stale tag area, leading to a Use-After-Free when pr_info dereferences the old module's freed .rodata via tag->ct.modname. --

commit 85214c8b1888956f945e1850b83b26c40f80620f
Author: Hao Ge <[email protected]>

alloc_tag: move release_module_tags() above reserve_module_tags()

This patch moves the release_module_tags() helper function above reserve_module_tags() in the file. This allows reserve_module_tags() to call it for cleanup on failure paths without needing a forward declaration.

Quoted 4 lines
mm/alloc_tag.c +0 -0
index b334103104779..2070e682fe106 100644

[ ... ]

Quoted 7 lines
mm/alloc_tag.c +3 -0
@@ -835,6 +835,52 @@ static int vm_module_tags_populate(void)
 	return 0;
 }
 
+static void release_module_tags(struct module *mod, bool used)
+{
+	MA_STATE(mas, &mod_area_mt, module_tags.size, module_tags.size);

[ ... ]

mm/alloc_tag.c static int vm_module_tags_populate(void) · line 887

Quoted 5 lines
mm/alloc_tag.c +5 -0
+release_area:
+	mas_store(&mas, used ? &unloaded_mod : NULL);
+	val = mas_prev_range(&mas, 0);
+	if (val == &prepend_mod)
+		mas_store(&mas, NULL);

[Severity: High]
This isn't a bug introduced by this patch, but since the code is being moved here, could these mas_store() calls fail silently under memory pressure?

Since release_module_tags() modifies the maple tree while holding mas_lock() (a spinlock), mas_store() uses GFP_NOWAIT for internal node allocations, such as when it needs to merge adjacent NULL slots.

If a GFP_NOWAIT allocation fails, mas_store() aborts and leaves the maple state in an error condition. Because this function doesn't preallocate nodes before acquiring the lock or check mas_is_err(&mas) after the store, the stale module pointer remains in the tree.

If a subsequently loaded module reuses the exact same struct module address and is later unloaded, could this cause release_module_tags() to process the stale entry instead? This appears it would trigger a Use-After-Free when pr_info() dereferences the old module's freed .rodata section via tag->ct.modname.

Quoted 3 lines
mm/alloc_tag.c +3 -0
+out:
+	mas_unlock(&mas);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#3 lore raw

PATCH v7 2/4 module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

In reply to Hao Ge

SHF_ALLOC means, per the ELF spec, that a section occupies memory during process execution. Some module sections occupy memory outside the regular module layout, for example the percpu section with its per-CPU allocations. The loader currently excludes such a section from the layout by clearing its SHF_ALLOC, which overloads the flag with a loader-internal meaning. apply_relocations() needs a special case for the section, and find_sec(".data..percpu") returns different results before and after layout_and_allocate().

Introduce SH_ENTSIZE_STANDALONE to mark sections with a separate allocation. The percpu section is its first user. layout_sections() and move_module() skip marked sections, and apply_relocations() goes back to testing only SHF_ALLOC.

No functional change intended.

Fixes 4835f747d3ed ("alloc_tag: support for page allocation tag compression")
---
 include/linux/module.h   |  2 ++
 kernel/module/internal.h |  8 ++++++++
 kernel/module/main.c     | 31 +++++++++++++------------------
 3 files changed, 23 insertions(+), 18 deletions(-)
include/linux/module.h +2 -0
index 7566815fabbe..33548daa31a3 100644
@@ -325,6 +325,8 @@ enum mod_mem_type {
 	MOD_INIT_RODATA,
 
 	MOD_MEM_NUM_TYPES,
+
+	MOD_STANDALONE = -2,
 	MOD_INVALID = -1,
 };
 
kernel/module/internal.h +8 -0
index 061161cc79d9..4c738074a27b 100644
@@ -29,6 +29,14 @@
 #define SH_ENTSIZE_TYPE_MASK	((1UL << SH_ENTSIZE_TYPE_BITS) - 1)
 #define SH_ENTSIZE_OFFSET_MASK	((1UL << (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS)) - 1)
 
+/*
+ * Marker for sections with a separate allocation, which are not placed
+ * into mod->mem[].
+ */
+#define SH_ENTSIZE_STANDALONE					\
+	(((unsigned long)MOD_STANDALONE & SH_ENTSIZE_TYPE_MASK)	\
+	 << SH_ENTSIZE_TYPE_SHIFT)
+
 /* Maximum number of characters written by module_flags() */
 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
 
kernel/module/main.c +13 -18
index c32f1d370b73..078dae188aba 100644
@@ -1618,14 +1618,8 @@ static int apply_relocations(struct module *mod, const struct load_info *info)
 		if (infosec >= info->hdr->e_shnum)
 			continue;
 
-		/*
-		 * Don't bother with non-allocated sections.
-		 * An exception is the percpu section, which has separate allocations
-		 * for individual CPUs. We relocate the percpu section in the initial
-		 * ELF template and subsequently copy it to the per-CPU destinations.
-		 */
-		if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC) &&
-		    (!infosec || infosec != info->index.pcpu))
+		/* Don't bother with non-allocated sections. */
+		if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
 			continue;
 
 		if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
@@ -1716,7 +1710,7 @@ static void __layout_sections(struct module *mod, struct load_info *info, bool i
 
 			if ((s->sh_flags & masks[m][0]) != masks[m][0]
 			    || (s->sh_flags & masks[m][1])
-			    || s->sh_entsize != ~0UL
+			    || s->sh_entsize != ~0UL /* offset or standalone */
 			    || is_init != module_init_layout_section(sname))
 				continue;
 
@@ -1746,16 +1740,10 @@ static void __layout_sections(struct module *mod, struct load_info *info, bool i
 /*
  * Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  * might -- code, read-only data, read-write data, small data.  Tally
- * sizes, and place the offsets into sh_entsize fields: high bit means it
- * belongs in init.
+ * sizes, and place the offsets into sh_entsize fields.
  */
 static void layout_sections(struct module *mod, struct load_info *info)
 {
-	unsigned int i;
-
-	for (i = 0; i < info->hdr->e_shnum; i++)
-		info->sechdrs[i].sh_entsize = ~0UL;
-
 	pr_debug("Core section allocation order for %s:\n", mod->name);
 	__layout_sections(mod, info, false);
 
@@ -2811,7 +2799,8 @@ static int move_module(struct module *mod, struct load_info *info)
 		Elf_Shdr *shdr = &info->sechdrs[i];
 		const char *sname;
 
-		if (!(shdr->sh_flags & SHF_ALLOC))
+		if (!(shdr->sh_flags & SHF_ALLOC)
+		    || shdr->sh_entsize == SH_ENTSIZE_STANDALONE)
 			continue;
 
 		sname = info->secstrings + shdr->sh_name;
@@ -2943,6 +2932,7 @@ core_param(module_blacklist, module_blacklist, charp, 0400);
 static struct module *layout_and_allocate(struct load_info *info, int flags)
 {
 	struct module *mod;
+	unsigned int i;
 	int err;
 
 	/* Allow arches to frob section contents and sizes.  */
@@ -2956,8 +2946,13 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
 	if (err < 0)
 		return ERR_PTR(err);
 
+	/* Repurpose sh_entsize to track where each section is allocated. */
+	for (i = 0; i < info->hdr->e_shnum; i++)
+		info->sechdrs[i].sh_entsize = ~0UL;
+
 	/* We will do a special allocation for per-cpu sections later. */
-	info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
+	if (info->index.pcpu)
+		info->sechdrs[info->index.pcpu].sh_entsize = SH_ENTSIZE_STANDALONE;
 
 	/*
 	 * Mark relevant sections as SHF_RO_AFTER_INIT so layout_sections() can
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH v7 2/4] module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The `.data..percpu` section is incorrectly exported in sysfs with a dangling pointer to a freed…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The .data..percpu section is incorrectly exported in sysfs with a dangling pointer to a freed temporary buffer. --

commit 5082dc654c1b8850af405eef473b249c0d936de9
Author: Hao Ge <[email protected]>

module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

This commit introduces a new marker, SH_ENTSIZE_STANDALONE, to indicate sections with a separate allocation. Instead of clearing the SHF_ALLOC flag, it utilizes sh_entsize so that sections with separate allocations like the percpu section can be correctly skipped during layout.

Quoted 4 lines
kernel/module/main.c +0 -0
index d0e1e0bd2ad06..b114b815187a9 100644

[ ... ]

Quoted 10 lines
kernel/module/main.c +2 -1
@@ -2822,7 +2810,8 @@ static int move_module(struct module *mod, struct load_info *info)
 		Elf_Shdr *shdr = &info->sechdrs[i];
 		const char *sname;
 
-		if (!(shdr->sh_flags & SHF_ALLOC))
+		if (!(shdr->sh_flags & SHF_ALLOC)
+		    || shdr->sh_entsize == SH_ENTSIZE_STANDALONE)
 			continue;
 
 		sname = info->secstrings + shdr->sh_name;

[ ... ]

Quoted 5 lines
kernel/module/main.c +1 -0
@@ -2967,8 +2957,13 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
 	if (err < 0)
 		return ERR_PTR(err);
 
+	/* Repurpose sh_entsize to track where each section is allocated. */

kernel/module/main.c static struct module *layout_and_allocate(struct load_info *info, int flags) · line 2969

Quoted 10 lines
kernel/module/main.c +5 -1
+	for (i = 0; i < info->hdr->e_shnum; i++)
+		info->sechdrs[i].sh_entsize = ~0UL;
+
 	/* We will do a special allocation for per-cpu sections later. */
-	info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
+	if (info->index.pcpu)
+		info->sechdrs[info->index.pcpu].sh_entsize = SH_ENTSIZE_STANDALONE;
 
 	/*
 	 * Mark relevant sections as SHF_RO_AFTER_INIT so layout_sections() can

[Severity: Medium]
Because SHF_ALLOC is no longer cleared here, does this cause the .data..percpu section to be incorrectly exported via sysfs?

With SHF_ALLOC remaining intact, sect_empty() in kernel/module/sysfs.c now returns false for the percpu section, causing it to be processed instead of skipped.

Additionally, because move_module() explicitly skips sections marked with SH_ENTSIZE_STANDALONE, the sh_addr is never updated to the final allocated memory.

This means when add_sect_attrs() exports the section:

kernel/module/sysfs.c:add_sect_attrs() {
    ...
	if (sect_empty(sec))
		continue;
    ...
	sattr->private = (void *)sec->sh_addr;
    ...
}

Will sattr->private end up storing an unmodified sh_addr pointing to the freed temporary load_info buffer (info->hdr)?

This appears to leak a stale vmalloc pointer address to userspace when reading the sysfs file for the section.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author reviewer
#5 lore raw

Re: [PATCH v7 2/4] module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

Hi Petr and Suren

Quoted 71 lines · On 2026/9/2 16:33, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The .data..percpu section is incorrectly exported in sysfs with a dangling pointer to a freed temporary buffer. --

commit 5082dc654c1b8850af405eef473b249c0d936de9
Author: Hao Ge <[email protected]>

module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

This commit introduces a new marker, SH_ENTSIZE_STANDALONE, to indicate sections with a separate allocation. Instead of clearing the SHF_ALLOC flag, it utilizes sh_entsize so that sections with separate allocations like the percpu section can be correctly skipped during layout.

Quoted 4 lines
kernel/module/main.c +0 -0
index d0e1e0bd2ad06..b114b815187a9 100644

[ ... ]

Quoted 10 lines
kernel/module/main.c +2 -1
@@ -2822,7 +2810,8 @@ static int move_module(struct module *mod, struct load_info *info)
 		Elf_Shdr *shdr = &info->sechdrs[i];
 		const char *sname;
 
-		if (!(shdr->sh_flags & SHF_ALLOC))
+		if (!(shdr->sh_flags & SHF_ALLOC)
+		    || shdr->sh_entsize == SH_ENTSIZE_STANDALONE)
 			continue;
 
 		sname = info->secstrings + shdr->sh_name;

[ ... ]

Quoted 5 lines
kernel/module/main.c +1 -0
@@ -2967,8 +2957,13 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
 	if (err < 0)
 		return ERR_PTR(err);
 
+	/* Repurpose sh_entsize to track where each section is allocated. */

kernel/module/main.c static struct module *layout_and_allocate(struct load_info *info, int flags) · line 2969

Quoted 10 lines
kernel/module/main.c +5 -1
+	for (i = 0; i < info->hdr->e_shnum; i++)
+		info->sechdrs[i].sh_entsize = ~0UL;
+
 	/* We will do a special allocation for per-cpu sections later. */
-	info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
+	if (info->index.pcpu)
+		info->sechdrs[info->index.pcpu].sh_entsize = SH_ENTSIZE_STANDALONE;
 
 	/*
 	 * Mark relevant sections as SHF_RO_AFTER_INIT so layout_sections() can

[Severity: Medium]
Because SHF_ALLOC is no longer cleared here, does this cause the .data..percpu section to be incorrectly exported via sysfs?

With SHF_ALLOC remaining intact, sect_empty() in kernel/module/sysfs.c now returns false for the percpu section, causing it to be processed instead of skipped.

Additionally, because move_module() explicitly skips sections marked with SH_ENTSIZE_STANDALONE, the sh_addr is never updated to the final allocated memory.

This means when add_sect_attrs() exports the section:

kernel/module/sysfs.c:add_sect_attrs() {
    ...
	if (sect_empty(sec))
		continue;
    ...
	sattr->private = (void *)sec->sh_addr;
    ...
}
Quoted 6 lines

Will sattr->private end up storing an unmodified sh_addr pointing to the freed temporary load_info buffer (info->hdr)?

This appears to leak a stale vmalloc pointer address to userspace when reading the sysfs file for the section.

Yes, Sashiko is correct. The fix is just one line after percpu_modcopy():

info->sechdrs[info->index.pcpu].sh_addr = (unsigned long)mod_percpu(mod);

But .data..percpu will show up in /sys/module/*/sections/, it was never exported before. Stable will get this too via backport, since later fixes depend on it.

Could you please advise on the preferred way forward?

Thanks
Best Regards
Hao

maintainer
#6 lore raw

Re: [PATCH v7 2/4] module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

Quoted 77 lines · Hao Ge

Hi Petr and Suren

Quoted 74 lines · On 2026/9/2 16:33, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The .data..percpu section is incorrectly exported in sysfs with a dangling pointer to a freed temporary buffer. --

commit 5082dc654c1b8850af405eef473b249c0d936de9
Author: Hao Ge <[email protected]>

module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

This commit introduces a new marker, SH_ENTSIZE_STANDALONE, to indicate sections with a separate allocation. Instead of clearing the SHF_ALLOC flag, it utilizes sh_entsize so that sections with separate allocations like the percpu section can be correctly skipped during layout.

Quoted 4 lines
kernel/module/main.c +0 -0
index d0e1e0bd2ad06..b114b815187a9 100644

[ ... ]

Quoted 10 lines
kernel/module/main.c +2 -1
@@ -2822,7 +2810,8 @@ static int move_module(struct module *mod, struct load_info *info)
 		Elf_Shdr *shdr = &info->sechdrs[i];
 		const char *sname;
 
-		if (!(shdr->sh_flags & SHF_ALLOC))
+		if (!(shdr->sh_flags & SHF_ALLOC)
+		    || shdr->sh_entsize == SH_ENTSIZE_STANDALONE)
 			continue;
 
 		sname = info->secstrings + shdr->sh_name;

[ ... ]

Quoted 5 lines
kernel/module/main.c +1 -0
@@ -2967,8 +2957,13 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
 	if (err < 0)
 		return ERR_PTR(err);
 
+	/* Repurpose sh_entsize to track where each section is allocated. */

kernel/module/main.c static struct module *layout_and_allocate(struct load_info *info, int flags) · line 2969

Quoted 10 lines
kernel/module/main.c +5 -1
+	for (i = 0; i < info->hdr->e_shnum; i++)
+		info->sechdrs[i].sh_entsize = ~0UL;
+
 	/* We will do a special allocation for per-cpu sections later. */
-	info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
+	if (info->index.pcpu)
+		info->sechdrs[info->index.pcpu].sh_entsize = SH_ENTSIZE_STANDALONE;
 
 	/*
 	 * Mark relevant sections as SHF_RO_AFTER_INIT so layout_sections() can

[Severity: Medium]
Because SHF_ALLOC is no longer cleared here, does this cause the .data..percpu section to be incorrectly exported via sysfs?

With SHF_ALLOC remaining intact, sect_empty() in kernel/module/sysfs.c now returns false for the percpu section, causing it to be processed instead of skipped.

Additionally, because move_module() explicitly skips sections marked with SH_ENTSIZE_STANDALONE, the sh_addr is never updated to the final allocated memory.

This means when add_sect_attrs() exports the section:

kernel/module/sysfs.c:add_sect_attrs() {
    ...
	if (sect_empty(sec))
		continue;
    ...
	sattr->private = (void *)sec->sh_addr;
    ...
}

Will sattr->private end up storing an unmodified sh_addr pointing to the freed temporary load_info buffer (info->hdr)?

Quoted 6 lines
Quoted 3 lines

This appears to leak a stale vmalloc pointer address to userspace when reading the sysfs file for the section.

Yes, Sashiko is correct. The fix is just one line after percpu_modcopy():

info->sechdrs[info->index.pcpu].sh_addr = (unsigned long)mod_percpu(mod);

mod_percpu() (mod->percpu) isn't really the execution address of the .data..percpu section. That section in the ELF file is only a template to initialize the individual per-CPU variable areas.

I think the two options are:

1) Do not export the section under /sys/module/*/sections/.
2) Treat the boot CPU area as the canonical location of
.data..percpu. In other words, do:
info->sechdrs[info->index.pcpu].sh_addr = (unsigned long)per_cpu_ptr(mod->percpu, get_boot_cpu_id());

The /sys/module/*/sections/ interface is intended for debugging. From that perspective, exposing the .data..percpu address for the boot CPU may be useful and seems fairly reasonable to me. On the other hand, one could argue that this can cause confusion, since a loaded module has multiple .data..percpu instances.

I don't see a strong argument for either option, but I would lean toward the second.

Quoted 3 lines

But .data..percpu will show up in /sys/module/*/sections/, it was never exported before. Stable will get this too via backport, since later fixes depend on it.

I think exposing .data..percpu under /sys/module/*/sections/ is a minor change and is fine for stable. As mentioned, /sys/module/*/sections/ is a debugging interface. Additionally, most uses of per-CPU variables are in the core kernel, with only a handful modules defining per-CPU data. On my system, 30 modules out of 5383 contain a .data..percpu section.

Signature · 2 lines
Thanks,
Petr
author reviewer
#7 lore raw

Re: [PATCH v7 2/4] module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

Hi Petr

Quoted 81 lines · Petr Pavlu
Quoted 80 lines · Hao Ge

Hi Petr and Suren

Quoted 77 lines · On 2026/9/2 16:33, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The .data..percpu section is incorrectly exported in sysfs with a dangling pointer to a freed temporary buffer. --

commit 5082dc654c1b8850af405eef473b249c0d936de9
Author: Hao Ge <[email protected]>

module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

This commit introduces a new marker, SH_ENTSIZE_STANDALONE, to indicate sections with a separate allocation. Instead of clearing the SHF_ALLOC flag, it utilizes sh_entsize so that sections with separate allocations like the percpu section can be correctly skipped during layout.

Quoted 4 lines
kernel/module/main.c +0 -0
index d0e1e0bd2ad06..b114b815187a9 100644

[ ... ]

Quoted 10 lines
kernel/module/main.c +2 -1
@@ -2822,7 +2810,8 @@ static int move_module(struct module *mod, struct load_info *info)
 		Elf_Shdr *shdr = &info->sechdrs[i];
 		const char *sname;
 
-		if (!(shdr->sh_flags & SHF_ALLOC))
+		if (!(shdr->sh_flags & SHF_ALLOC)
+		    || shdr->sh_entsize == SH_ENTSIZE_STANDALONE)
 			continue;
 
 		sname = info->secstrings + shdr->sh_name;

[ ... ]

Quoted 5 lines
kernel/module/main.c +1 -0
@@ -2967,8 +2957,13 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
 	if (err < 0)
 		return ERR_PTR(err);
 
+	/* Repurpose sh_entsize to track where each section is allocated. */

kernel/module/main.c static struct module *layout_and_allocate(struct load_info *info, int flags) · line 2969

Quoted 10 lines
kernel/module/main.c +5 -1
+	for (i = 0; i < info->hdr->e_shnum; i++)
+		info->sechdrs[i].sh_entsize = ~0UL;
+
 	/* We will do a special allocation for per-cpu sections later. */
-	info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
+	if (info->index.pcpu)
+		info->sechdrs[info->index.pcpu].sh_entsize = SH_ENTSIZE_STANDALONE;
 
 	/*
 	 * Mark relevant sections as SHF_RO_AFTER_INIT so layout_sections() can

[Severity: Medium]
Because SHF_ALLOC is no longer cleared here, does this cause the .data..percpu section to be incorrectly exported via sysfs?

With SHF_ALLOC remaining intact, sect_empty() in kernel/module/sysfs.c now returns false for the percpu section, causing it to be processed instead of skipped.

Additionally, because move_module() explicitly skips sections marked with SH_ENTSIZE_STANDALONE, the sh_addr is never updated to the final allocated memory.

This means when add_sect_attrs() exports the section:

kernel/module/sysfs.c:add_sect_attrs() {
    ...
	if (sect_empty(sec))
		continue;
    ...
	sattr->private = (void *)sec->sh_addr;
    ...
}

Will sattr->private end up storing an unmodified sh_addr pointing to the freed temporary load_info buffer (info->hdr)?

This appears to leak a stale vmalloc pointer address to userspace when reading the sysfs file for the section.

Quoted 8 lines
Quoted 3 lines

Yes, Sashiko is correct. The fix is just one line after percpu_modcopy():

info->sechdrs[info->index.pcpu].sh_addr = (unsigned long)mod_percpu(mod);

mod_percpu() (mod->percpu) isn't really the execution address of the .data..percpu section. That section in the ELF file is only a template to initialize the individual per-CPU variable areas.

Yes, I used mod_percpu() to stay consistent with /proc/kallsyms.

Quoted 7 lines

I think the two options are:

1) Do not export the section under /sys/module/*/sections/.
2) Treat the boot CPU area as the canonical location of .data..percpu. In other words, do:

   info->sechdrs[info->index.pcpu].sh_addr = (unsigned long)per_cpu_ptr(mod->percpu, get_boot_cpu_id());
Quoted 9 lines

The /sys/module/*/sections/ interface is intended for debugging. From that perspective, exposing the .data..percpu address for the boot CPU may be useful and seems fairly reasonable to me. On the other hand, one could argue that this can cause confusion, since a loaded module has multiple .data..percpu instances.

I don't see a strong argument for either option, but I would lean toward the second.

OK. I spent the morning looking for relevant specs, but found none. Nor is this documented in Documentation/ABI/testing/sysfs-module.

https://elixir.bootlin.com/linux/v7.2.2/source/Documentation/ABI/testing/sysfs-module
https://elixir.bootlin.com/linux/v7.2.2/source/Documentation/ABI/stable/sysfs-module

But I've noticed similar usage in __is_module_percpu_address,
https://elixir.bootlin.com/linux/v7.2.2/source/kernel/module/main.c#L516

So I will first add documentation for this in Documentation/ABI/testing/sysfs-module.

Quoted 10 lines
Quoted 3 lines

But .data..percpu will show up in /sys/module/*/sections/, it was never exported before. Stable will get this too via backport, since later fixes depend on it.

I think exposing .data..percpu under /sys/module/*/sections/ is a minor change and is fine for stable. As mentioned, /sys/module/*/sections/ is a debugging interface. Additionally, most uses of per-CPU variables are in the core kernel, with only a handful modules defining per-CPU data. On my system, 30 modules out of 5383 contain a .data..percpu section.

Agree.

Thanks for your help.

Best Regards
Hao

maintainer
#8 lore raw

Re: [PATCH v7 2/4] module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

Quoted 86 lines · Hao Ge

Hi Petr

Quoted 83 lines · Petr Pavlu
Quoted 82 lines · Hao Ge

Hi Petr and Suren

Quoted 71 lines · On 2026/9/2 16:33, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The .data..percpu section is incorrectly exported in sysfs with a dangling pointer to a freed temporary buffer. --

commit 5082dc654c1b8850af405eef473b249c0d936de9
Author: Hao Ge <[email protected]>

module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

This commit introduces a new marker, SH_ENTSIZE_STANDALONE, to indicate sections with a separate allocation. Instead of clearing the SHF_ALLOC flag, it utilizes sh_entsize so that sections with separate allocations like the percpu section can be correctly skipped during layout.

Quoted 4 lines
kernel/module/main.c +0 -0
index d0e1e0bd2ad06..b114b815187a9 100644

[ ... ]

Quoted 10 lines
kernel/module/main.c +2 -1
@@ -2822,7 +2810,8 @@ static int move_module(struct module *mod, struct load_info *info)
 		Elf_Shdr *shdr = &info->sechdrs[i];
 		const char *sname;
 
-		if (!(shdr->sh_flags & SHF_ALLOC))
+		if (!(shdr->sh_flags & SHF_ALLOC)
+		    || shdr->sh_entsize == SH_ENTSIZE_STANDALONE)
 			continue;
 
 		sname = info->secstrings + shdr->sh_name;

[ ... ]

Quoted 5 lines
kernel/module/main.c +1 -0
@@ -2967,8 +2957,13 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
 	if (err < 0)
 		return ERR_PTR(err);
 
+	/* Repurpose sh_entsize to track where each section is allocated. */

kernel/module/main.c static struct module *layout_and_allocate(struct load_info *info, int flags) · line 2969

Quoted 10 lines
kernel/module/main.c +5 -1
+	for (i = 0; i < info->hdr->e_shnum; i++)
+		info->sechdrs[i].sh_entsize = ~0UL;
+
 	/* We will do a special allocation for per-cpu sections later. */
-	info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
+	if (info->index.pcpu)
+		info->sechdrs[info->index.pcpu].sh_entsize = SH_ENTSIZE_STANDALONE;
 
 	/*
 	 * Mark relevant sections as SHF_RO_AFTER_INIT so layout_sections() can

[Severity: Medium]
Because SHF_ALLOC is no longer cleared here, does this cause the .data..percpu section to be incorrectly exported via sysfs?

With SHF_ALLOC remaining intact, sect_empty() in kernel/module/sysfs.c now returns false for the percpu section, causing it to be processed instead of skipped.

Additionally, because move_module() explicitly skips sections marked with SH_ENTSIZE_STANDALONE, the sh_addr is never updated to the final allocated memory.

This means when add_sect_attrs() exports the section:

kernel/module/sysfs.c:add_sect_attrs() {
    ...
	if (sect_empty(sec))
		continue;
    ...
	sattr->private = (void *)sec->sh_addr;
    ...
}
Quoted 6 lines

Will sattr->private end up storing an unmodified sh_addr pointing to the freed temporary load_info buffer (info->hdr)?

This appears to leak a stale vmalloc pointer address to userspace when reading the sysfs file for the section.

Yes, Sashiko is correct. The fix is just one line after percpu_modcopy():

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

info->sechdrs[info->index.pcpu].sh_addr = (unsigned long)mod_percpu(mod);

mod_percpu() (mod->percpu) isn't really the execution address of the .data..percpu section. That section in the ELF file is only a template to initialize the individual per-CPU variable areas.

Yes, I used mod_percpu() to stay consistent with /proc/kallsyms.

That's a good point. /proc/kallsyms directly shows the st_value of each symbol. For per-CPU symbols in modules, that is mod->percpu+offset. I wonder whether it possible to make any use of such an address unless one also knows the per_cpu_offset() mapping.

However, maybe it is really simpler not to export .data..percpu under /sys/module/*/sections/, rather than trying to determine what address it should provide.

Signature · 2 lines
Thanks,
Petr
author reviewer
#9 lore raw

Re: [PATCH v7 2/4] module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

Hi Petr

Quoted 94 lines · Petr Pavlu
Quoted 93 lines · Hao Ge

Hi Petr

Quoted 89 lines · Petr Pavlu
Quoted 77 lines · Hao Ge

Hi Petr and Suren

Quoted 74 lines · On 2026/9/2 16:33, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The .data..percpu section is incorrectly exported in sysfs with a dangling pointer to a freed temporary buffer. --

commit 5082dc654c1b8850af405eef473b249c0d936de9
Author: Hao Ge <[email protected]>

module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

This commit introduces a new marker, SH_ENTSIZE_STANDALONE, to indicate sections with a separate allocation. Instead of clearing the SHF_ALLOC flag, it utilizes sh_entsize so that sections with separate allocations like the percpu section can be correctly skipped during layout.

Quoted 4 lines
kernel/module/main.c +0 -0
index d0e1e0bd2ad06..b114b815187a9 100644

[ ... ]

Quoted 10 lines
kernel/module/main.c +2 -1
@@ -2822,7 +2810,8 @@ static int move_module(struct module *mod, struct load_info *info)
 		Elf_Shdr *shdr = &info->sechdrs[i];
 		const char *sname;
 
-		if (!(shdr->sh_flags & SHF_ALLOC))
+		if (!(shdr->sh_flags & SHF_ALLOC)
+		    || shdr->sh_entsize == SH_ENTSIZE_STANDALONE)
 			continue;
 
 		sname = info->secstrings + shdr->sh_name;

[ ... ]

Quoted 5 lines
kernel/module/main.c +1 -0
@@ -2967,8 +2957,13 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
 	if (err < 0)
 		return ERR_PTR(err);
 
+	/* Repurpose sh_entsize to track where each section is allocated. */

kernel/module/main.c static struct module *layout_and_allocate(struct load_info *info, int flags) · line 2969

Quoted 10 lines
kernel/module/main.c +5 -1
+	for (i = 0; i < info->hdr->e_shnum; i++)
+		info->sechdrs[i].sh_entsize = ~0UL;
+
 	/* We will do a special allocation for per-cpu sections later. */
-	info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
+	if (info->index.pcpu)
+		info->sechdrs[info->index.pcpu].sh_entsize = SH_ENTSIZE_STANDALONE;
 
 	/*
 	 * Mark relevant sections as SHF_RO_AFTER_INIT so layout_sections() can

[Severity: Medium]
Because SHF_ALLOC is no longer cleared here, does this cause the .data..percpu section to be incorrectly exported via sysfs?

With SHF_ALLOC remaining intact, sect_empty() in kernel/module/sysfs.c now returns false for the percpu section, causing it to be processed instead of skipped.

Additionally, because move_module() explicitly skips sections marked with SH_ENTSIZE_STANDALONE, the sh_addr is never updated to the final allocated memory.

This means when add_sect_attrs() exports the section:

kernel/module/sysfs.c:add_sect_attrs() {
    ...
	if (sect_empty(sec))
		continue;
    ...
	sattr->private = (void *)sec->sh_addr;
    ...
}

Will sattr->private end up storing an unmodified sh_addr pointing to the freed temporary load_info buffer (info->hdr)?

Quoted 6 lines
Quoted 3 lines

This appears to leak a stale vmalloc pointer address to userspace when reading the sysfs file for the section.

Yes, Sashiko is correct. The fix is just one line after percpu_modcopy():

info->sechdrs[info->index.pcpu].sh_addr = (unsigned long)mod_percpu(mod);

mod_percpu() (mod->percpu) isn't really the execution address of the .data..percpu section. That section in the ELF file is only a template to initialize the individual per-CPU variable areas.

Quoted 7 lines
Quoted 1 line

Yes, I used mod_percpu() to stay consistent with /proc/kallsyms.

That's a good point. /proc/kallsyms directly shows the st_value of each symbol. For per-CPU symbols in modules, that is mod->percpu+offset. I wonder whether it possible to make any use of such an address unless one also knows the per_cpu_offset() mapping.

Right. Without the mapping it is not dereferenceable. bpftrace passes the kallsyms value to bpf_per_cpu_ptr(), which does the addition in the kernel. I would expect most tools do the same (crash, for example).

Quoted 4 lines

However, maybe it is really simpler not to export .data..percpu under /sys/module/*/sections/, rather than trying to determine what address it should provide.

OK, I will keep it hidden for now. We can revisit it some day if a real need for the entry ever comes up.

Thanks
Best Regards
Hao

maintainer
#10 lore raw

Re: [PATCH v7 2/4] module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections

In reply to Hao Ge
Quoted 36 lines · Hao Ge

SHF_ALLOC means, per the ELF spec, that a section occupies memory during process execution. Some module sections occupy memory outside the regular module layout, for example the percpu section with its per-CPU allocations. The loader currently excludes such a section from the layout by clearing its SHF_ALLOC, which overloads the flag with a loader-internal meaning. apply_relocations() needs a special case for the section, and find_sec(".data..percpu") returns different results before and after layout_and_allocate().

Introduce SH_ENTSIZE_STANDALONE to mark sections with a separate allocation. The percpu section is its first user. layout_sections() and move_module() skip marked sections, and apply_relocations() goes back to testing only SHF_ALLOC.

No functional change intended.

Fixes 4835f747d3ed ("alloc_tag: support for page allocation tag compression")

---
[...]

kernel/module/main.c +0 -3
index c32f1d370b73..078dae188aba 100644
@@ -1618,14 +1618,8 @@ static int apply_relocations(struct module *mod, const struct load_info *info)
 		if (infosec >= info->hdr->e_shnum)
 			continue;
 
-		/*
-		 * Don't bother with non-allocated sections.
-		 * An exception is the percpu section, which has separate allocations

kernel/module/main.c static int apply_relocations(struct module *mod, const struct load_info *info) · line 1625

Quoted 10 lines
kernel/module/main.c +2 -5
-		 * for individual CPUs. We relocate the percpu section in the initial
-		 * ELF template and subsequently copy it to the per-CPU destinations.
-		 */
-		if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC) &&
-		    (!infosec || infosec != info->index.pcpu))
+		/* Don't bother with non-allocated sections. */
+		if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
 			continue;
 
 		if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)

Please keep a comment about how .data..percpu is handled. This is not obvious and should be described explicitly.

Something like the following:

/*
* Don't bother with non-allocated sections. *
* Note that .data..percpu has separate allocations for
* individual CPUs. We relocate the section in the
* initial ELF template and subsequently copy it to the
* per-CPU destinations.
*/

Signature · 2 lines
Thanks,
Petr
author reviewer
#11 lore raw

PATCH v7 3/4 module: allocate codetag sections before the regular module layout

In reply to Hao Ge

Whether a codetag section goes to the codetag region is decided by layout_sections() and asked again in move_module(). A concurrent load can shut profiling down in between, and move_module() then copies the section to offset 0 of its regular destination, overwriting whatever is there.

Decide and allocate in one pass, before the layout. Allocation errors fail the load. On a tag area overflow profiling is already disabled, so -EAGAIN makes the section fall back to regular module data and the module still loads.

When profiling was toggled off the overflow check did not run, a module could load with more tags than the page flags can address, and re-enabling profiling then silently corrupted /proc/allocinfo. The check no longer depends on mem_alloc_profiling_enabled().

Fixes 4835f747d3ed ("alloc_tag: support for page allocation tag compression")
---
 kernel/module/main.c | 99 +++++++++++++++++++++++---------------------
 mm/alloc_tag.c       |  8 ++--
 2 files changed, 57 insertions(+), 50 deletions(-)
kernel/module/main.c +52 -47
index 078dae188aba..d0a8ac9b3ee5 100644
@@ -1717,20 +1717,6 @@ static void __layout_sections(struct module *mod, struct load_info *info, bool i
 			if (WARN_ON_ONCE(type == MOD_INVALID))
 				continue;
 
-			/*
-			 * Do not allocate codetag memory as we load it into
-			 * preallocated contiguous memory.
-			 */
-			if (codetag_needs_module_section(mod, sname, s->sh_size)) {
-				/*
-				 * s->sh_entsize won't be used but populate the
-				 * type field to avoid confusion.
-				 */
-				s->sh_entsize = ((unsigned long)(type) & SH_ENTSIZE_TYPE_MASK)
-						<< SH_ENTSIZE_TYPE_SHIFT;
-				continue;
-			}
-
 			s->sh_entsize = module_get_offset_and_type(mod, type, s, i);
 			pr_debug("\t%s\n", sname);
 		}
@@ -2777,7 +2763,6 @@ static int move_module(struct module *mod, struct load_info *info)
 {
 	int i, ret;
 	enum mod_mem_type t = MOD_MEM_NUM_TYPES;
-	bool codetag_section_found = false;
 
 	for_each_mod_mem_type(type) {
 		if (!mod->mem[type].size) {
@@ -2797,35 +2782,13 @@ static int move_module(struct module *mod, struct load_info *info)
 	for (i = 0; i < info->hdr->e_shnum; i++) {
 		void *dest;
 		Elf_Shdr *shdr = &info->sechdrs[i];
-		const char *sname;
 
 		if (!(shdr->sh_flags & SHF_ALLOC)
 		    || shdr->sh_entsize == SH_ENTSIZE_STANDALONE)
 			continue;
 
-		sname = info->secstrings + shdr->sh_name;
-		/*
-		 * Load codetag sections separately as they might still be used
-		 * after module unload.
-		 */
-		if (codetag_needs_module_section(mod, sname, shdr->sh_size)) {
-			dest = codetag_alloc_module_section(mod, sname, shdr->sh_size,
-					arch_mod_section_prepend(mod, i), shdr->sh_addralign);
-			if (WARN_ON(!dest)) {
-				ret = -EINVAL;
-				goto out_err;
-			}
-			if (IS_ERR(dest)) {
-				ret = PTR_ERR(dest);
-				goto out_err;
-			}
-			codetag_section_found = true;
-		} else {
-			enum mod_mem_type type = shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT;
-			unsigned long offset = shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK;
-
-			dest = mod->mem[type].base + offset;
-		}
+		dest = mod->mem[shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT].base +
+		       (shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK);
 
 		if (shdr->sh_type != SHT_NOBITS) {
 			/*
@@ -2857,8 +2820,6 @@ static int move_module(struct module *mod, struct load_info *info)
 	module_memory_restore_rox(mod);
 	while (t--)
 		module_memory_free(mod, t);
-	if (codetag_section_found)
-		codetag_free_module_sections(mod);
 
 	return ret;
 }
@@ -2929,6 +2890,47 @@ static bool blacklisted(const char *module_name)
 }
 core_param(module_blacklist, module_blacklist, charp, 0400);
 
+/*
+ * Allocate codetag sections separately. They are loaded into preallocated
+ * contiguous memory because they may still be used after the module is
+ * unloaded.
+ *
+ * If the separate allocation overflows, allocate the section normally
+ * so that the module can still be loaded.
+ */
+static int allocate_codetag_sections(struct load_info *info)
+{
+	for (unsigned int i = 1; i < info->hdr->e_shnum; i++) {
+		Elf_Shdr *shdr = &info->sechdrs[i];
+		const char *sname = info->secstrings + shdr->sh_name;
+		void *dest;
+
+		if (!codetag_needs_module_section(info->mod, sname, shdr->sh_size))
+			continue;
+
+		dest = codetag_alloc_module_section(info->mod, sname, shdr->sh_size,
+				arch_mod_section_prepend(info->mod, i), shdr->sh_addralign);
+		if (WARN_ON(!dest)) {
+			codetag_free_module_sections(info->mod);
+			return -EINVAL;
+		}
+		if (dest == ERR_PTR(-EAGAIN))
+			/* Allocate the section as a regular section. */
+			continue;
+		if (IS_ERR(dest)) {
+			codetag_free_module_sections(info->mod);
+			return PTR_ERR(dest);
+		}
+
+		if (shdr->sh_type != SHT_NOBITS)
+			memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
+		shdr->sh_addr = (unsigned long)dest;
+		shdr->sh_entsize = SH_ENTSIZE_STANDALONE;
+	}
+
+	return 0;
+}
+
 static struct module *layout_and_allocate(struct load_info *info, int flags)
 {
 	struct module *mod;
@@ -2961,18 +2963,21 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
 	 */
 	module_mark_ro_after_init(info->hdr, info->sechdrs, info->secstrings);
 
-	/*
-	 * Determine total sizes, and put offsets in sh_entsize.  For now
-	 * this is done generically; there doesn't appear to be any
-	 * special cases for the architectures.
-	 */
+	/* Allow codetag sections to be allocated separately first. */
+	err = allocate_codetag_sections(info);
+	if (err)
+		return ERR_PTR(err);
+
+	/* Determine total sizes and put offsets in sh_entsize. */
 	layout_sections(info->mod, info);
 	layout_symtab(info->mod, info);
 
 	/* Allocate and move to the final place */
 	err = move_module(info->mod, info);
-	if (err)
+	if (err) {
+		codetag_free_module_sections(info->mod);
 		return ERR_PTR(err);
+	}
 
 	/* Module has been copied to its final place now: return it. */
 	mod = (void *)info->sechdrs[info->index.mod].sh_addr;
mm/alloc_tag.c +5 -3
index e7a79116ad81..248496470904 100644
@@ -958,10 +958,12 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
 		int grow_res;
 
 		module_tags.size = offset + size;
-		if (mem_alloc_profiling_enabled() && !tags_addressable()) {
+		if (!tags_addressable()) {
 			shutdown_mem_profiling(true);
-			pr_warn("With module %s there are too many tags to fit in %d page flag bits. Memory allocation profiling is disabled!\n",
-				mod->name, NR_UNUSED_PAGEFLAG_BITS);
+			pr_warn_once("With module %s there are too many tags to fit in %d page flag bits. Memory allocation profiling is disabled!\n",
+				     mod->name, NR_UNUSED_PAGEFLAG_BITS);
+			release_module_tags(mod, false);
+			return ERR_PTR(-EAGAIN);
 		}
 
 		grow_res = vm_module_tags_populate();
Signature · 1 line
2.25.1
#12 lore raw

Re: [PATCH v7 3/4] module: allocate codetag sections before the regular module layout

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Per-CPU memory leak of module allocation tag counters on module unload when falling back to regular section allocation.
- [Medium] Skipping memcpy for SHT_NOBITS codetag sections leaves them filled with uninitialized kernel memory.

Pre-existing issues:
- [High] AB-BA deadlock between /proc/allocinfo file readers (pde->in_use) and module loading (cttype->mod_lock). --

commit 073ca6b0680c884f9d863cdcbd7d5db665a17c3b
Author: Hao Ge <[email protected]>

module: allocate codetag sections before the regular module layout

Whether a codetag section goes to the codetag region is decided by layout_sections() and asked again in move_module(). A concurrent load can shut profiling down in between, and move_module() then copies the section to offset 0 of its regular destination, overwriting whatever is there.

Quoted 4 lines
kernel/module/main.c +0 -0
index b114b815187a9..b94830c5b9a10 100644

[ ... ]

Quoted 21 lines
kernel/module/main.c +17 -0
@@ -2940,6 +2901,47 @@ static bool blacklisted(const char *module_name)
 }
 core_param(module_blacklist, module_blacklist, charp, 0400);
 
+/*
+ * Allocate codetag sections separately. They are loaded into preallocated
+ * contiguous memory because they may still be used after the module is
+ * unloaded.
+ *
+ * If the separate allocation overflows, allocate the section normally
+ * so that the module can still be loaded.
+ */
+static int allocate_codetag_sections(struct load_info *info)
+{
+	for (unsigned int i = 1; i < info->hdr->e_shnum; i++) {
+		Elf_Shdr *shdr = &info->sechdrs[i];
+		const char *sname = info->secstrings + shdr->sh_name;
+		void *dest;
+
+		if (!codetag_needs_module_section(info->mod, sname, shdr->sh_size))
+			continue;

kernel/module/main.c static bool blacklisted(const char *module_name) · line 2930

Quoted 10 lines
kernel/module/main.c +10 -0
+
+		dest = codetag_alloc_module_section(info->mod, sname, shdr->sh_size,
+				arch_mod_section_prepend(info->mod, i), shdr->sh_addralign);
+		if (WARN_ON(!dest)) {
+			codetag_free_module_sections(info->mod);
+			return -EINVAL;
+		}
+		if (dest == ERR_PTR(-EAGAIN))
+			/* Allocate the section as a regular section. */
+			continue;

[Severity: High]
Does this code leak the per-cpu memory on module unload when falling back to regular section allocation?

If tags_addressable() returns false, reserve_module_tags() returns -EAGAIN and removes the module from mod_area_mt. The fallback mechanism here allocates the tags in regular module memory, but the module loading process still unconditionally allocates per-cpu counters for each tag, oblivious to the fallback or the disabled profiling state:

    for (tag = start_tag; tag < stop_tag; tag++) {
        WARN_ON(tag->counters);
        tag->counters = alloc_percpu(struct alloc_tag_counters);

On unload, release_module_tags() fails to find the module in mod_area_mt because it was removed, skipping clean_unused_counters() and leaking the previously allocated per-cpu memory.

kernel/module/main.c static bool blacklisted(const char *module_name) · line 2926

Quoted 7 lines
kernel/module/main.c +7 -0
+		if (IS_ERR(dest)) {
+			codetag_free_module_sections(info->mod);
+			return PTR_ERR(dest);
+		}
+
+		if (shdr->sh_type != SHT_NOBITS)
+			memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);

[Severity: Medium]
Could this expose uninitialized kernel memory if the codetag section is parsed from an ELF module with the SHT_NOBITS type?

The code skips memcpy() but fails to use memset() to zero-initialize the destination buffer. Regular module sections are safe because module_memory_alloc() explicitly zeroes them, but reserve_module_tags() backs its memory with alloc_pages_bulk_node() without the __GFP_ZERO flag:

mm/alloc_tag.c:vm_module_tags_populate() {
...
allocated = alloc_pages_bulk_node(GFP_KERNEL | __GFP_NOWARN,
NUMA_NO_NODE, more_pages - nr, next_page + nr);
...
}

Quoted 7 lines
kernel/module/main.c +7 -0
+		shdr->sh_addr = (unsigned long)dest;
+		shdr->sh_entsize = SH_ENTSIZE_STANDALONE;
+	}
+
+	return 0;
+}
+

[ ... ]

mm/alloc_tag.c static void *reserve_module_tags(struct module *mod, unsigned long size, · line 954

Quoted 11 lines
mm/alloc_tag.c +1 -1
index 2070e682fe106..112a014d4b890 100644
@@ -950,10 +950,12 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
 		int grow_res;
 
 		module_tags.size = offset + size;
-		if (mem_alloc_profiling_enabled() && !tags_addressable()) {
+		if (!tags_addressable()) {
 			shutdown_mem_profiling(true);

[Severity: High]
This is a pre-existing issue, but does this shutdown_mem_profiling(true) call introduce an AB-BA deadlock between /proc/allocinfo file readers (pde->in_use) and module loading (cttype->mod_lock)?

A user reading /proc/allocinfo acquires the procfs in_use reference in use_pde(), then blocks on down_read(&alloc_tag_cttype->mod_lock) in allocinfo_start().

Concurrently, if a module loads and tags_addressable() fails, codetag_alloc_module_section() holds down_write(&cttype->mod_lock) and calls shutdown_mem_profiling(true), which removes the proc entry. This removal waits indefinitely for the in_use reference to drop to zero, hanging both procfs reads and module loading.

Quoted 9 lines
mm/alloc_tag.c +4 -2
-			pr_warn("With module %s there are too many tags to fit in %d page flag bits. Memory allocation profiling is disabled!\n",
-				mod->name, NR_UNUSED_PAGEFLAG_BITS);
+			pr_warn_once("With module %s there are too many tags to fit in %d page flag bits. Memory allocation profiling is disabled!\n",
+				     mod->name, NR_UNUSED_PAGEFLAG_BITS);
+			release_module_tags(mod, false);
+			return ERR_PTR(-EAGAIN);
 		}
 
 		grow_res = vm_module_tags_populate();
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author reviewer
#13 lore raw

Re: [PATCH v7 3/4] module: allocate codetag sections before the regular module layout

Quoted 72 lines · On 2026/9/2 16:36, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Per-CPU memory leak of module allocation tag counters on module unload when falling back to regular section allocation.
- [Medium] Skipping memcpy for SHT_NOBITS codetag sections leaves them filled with uninitialized kernel memory.

Pre-existing issues:
- [High] AB-BA deadlock between /proc/allocinfo file readers (pde->in_use) and module loading (cttype->mod_lock). --

commit 073ca6b0680c884f9d863cdcbd7d5db665a17c3b
Author: Hao Ge <[email protected]>

module: allocate codetag sections before the regular module layout

Whether a codetag section goes to the codetag region is decided by layout_sections() and asked again in move_module(). A concurrent load can shut profiling down in between, and move_module() then copies the section to offset 0 of its regular destination, overwriting whatever is there.

Quoted 4 lines
kernel/module/main.c +0 -0
index b114b815187a9..b94830c5b9a10 100644

[ ... ]

Quoted 21 lines
kernel/module/main.c +17 -0
@@ -2940,6 +2901,47 @@ static bool blacklisted(const char *module_name)
 }
 core_param(module_blacklist, module_blacklist, charp, 0400);
 
+/*
+ * Allocate codetag sections separately. They are loaded into preallocated
+ * contiguous memory because they may still be used after the module is
+ * unloaded.
+ *
+ * If the separate allocation overflows, allocate the section normally
+ * so that the module can still be loaded.
+ */
+static int allocate_codetag_sections(struct load_info *info)
+{
+	for (unsigned int i = 1; i < info->hdr->e_shnum; i++) {
+		Elf_Shdr *shdr = &info->sechdrs[i];
+		const char *sname = info->secstrings + shdr->sh_name;
+		void *dest;
+
+		if (!codetag_needs_module_section(info->mod, sname, shdr->sh_size))
+			continue;

kernel/module/main.c static bool blacklisted(const char *module_name) · line 2930

Quoted 10 lines
kernel/module/main.c +10 -0
+
+		dest = codetag_alloc_module_section(info->mod, sname, shdr->sh_size,
+				arch_mod_section_prepend(info->mod, i), shdr->sh_addralign);
+		if (WARN_ON(!dest)) {
+			codetag_free_module_sections(info->mod);
+			return -EINVAL;
+		}
+		if (dest == ERR_PTR(-EAGAIN))
+			/* Allocate the section as a regular section. */
+			continue;

[Severity: High]
Does this code leak the per-cpu memory on module unload when falling back to regular section allocation?

If tags_addressable() returns false, reserve_module_tags() returns -EAGAIN and removes the module from mod_area_mt. The fallback mechanism here allocates the tags in regular module memory, but the module loading process still unconditionally allocates per-cpu counters for each tag, oblivious to the fallback or the disabled profiling state:

Quoted 7 lines
    for (tag = start_tag; tag < stop_tag; tag++) {
        WARN_ON(tag->counters);
        tag->counters = alloc_percpu(struct alloc_tag_counters);

On unload, release_module_tags() fails to find the module in mod_area_mt because it was removed, skipping clean_unused_counters() and leaking the previously allocated per-cpu memory.

This has already been fixed by the commit below:
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git/commit/?id=394aff7091a0278b0495549b5d2ff3b318e639d2

Quoted 13 lines

kernel/module/main.c static bool blacklisted(const char *module_name) · line 2926

Quoted 8 lines
kernel/module/main.c +7 -0
+		if (IS_ERR(dest)) {
+			codetag_free_module_sections(info->mod);
+			return PTR_ERR(dest);
+		}
+
+		if (shdr->sh_type != SHT_NOBITS)
+			memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);

[Severity: Medium]
Could this expose uninitialized kernel memory if the codetag section is parsed from an ELF module with the SHT_NOBITS type?

Quoted 5 lines

The code skips memcpy() but fails to use memset() to zero-initialize the destination buffer. Regular module sections are safe because module_memory_alloc() explicitly zeroes them, but reserve_module_tags() backs its memory with alloc_pages_bulk_node() without the __GFP_ZERO flag:

OK. Considering that codetag is a general‑purpose framework, it shouldn't assume anything about which codetag types exist today or later. Will fix for the next version.

Quoted 52 lines

mm/alloc_tag.c:vm_module_tags_populate() {
...
allocated = alloc_pages_bulk_node(GFP_KERNEL | __GFP_NOWARN,
NUMA_NO_NODE, more_pages - nr, next_page + nr);
...
}

Quoted 7 lines
kernel/module/main.c +7 -0
+		shdr->sh_addr = (unsigned long)dest;
+		shdr->sh_entsize = SH_ENTSIZE_STANDALONE;
+	}
+
+	return 0;
+}
+

[ ... ]

mm/alloc_tag.c static void *reserve_module_tags(struct module *mod, unsigned long size, · line 954

Quoted 11 lines
mm/alloc_tag.c +1 -1
index 2070e682fe106..112a014d4b890 100644
@@ -950,10 +950,12 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
 		int grow_res;
 
 		module_tags.size = offset + size;
-		if (mem_alloc_profiling_enabled() && !tags_addressable()) {
+		if (!tags_addressable()) {
 			shutdown_mem_profiling(true);

[Severity: High]
This is a pre-existing issue, but does this shutdown_mem_profiling(true) call introduce an AB-BA deadlock between /proc/allocinfo file readers (pde->in_use) and module loading (cttype->mod_lock)?

A user reading /proc/allocinfo acquires the procfs in_use reference in use_pde(), then blocks on down_read(&alloc_tag_cttype->mod_lock) in allocinfo_start().

Concurrently, if a module loads and tags_addressable() fails, codetag_alloc_module_section() holds down_write(&cttype->mod_lock) and calls shutdown_mem_profiling(true), which removes the proc entry. This removal waits indefinitely for the in_use reference to drop to zero, hanging both procfs reads and module loading.

Quoted 8 lines
mm/alloc_tag.c +4 -2
-			pr_warn("With module %s there are too many tags to fit in %d page flag bits. Memory allocation profiling is disabled!\n",
-				mod->name, NR_UNUSED_PAGEFLAG_BITS);
+			pr_warn_once("With module %s there are too many tags to fit in %d page flag bits. Memory allocation profiling is disabled!\n",
+				     mod->name, NR_UNUSED_PAGEFLAG_BITS);
+			release_module_tags(mod, false);
+			return ERR_PTR(-EAGAIN);
 		}
 
Quoted 2 lines

mm/alloc_tag.c static void *reserve_module_tags(struct module *mod, unsigned long size, · line 969

Quoted 1 line
mm/alloc_tag.c +0 -0
 		grow_res = vm_module_tags_populate();

This has already been fixed by the commit below:
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git/commit/?id=4cd27d57fd9468491f2ea48cee93e233c55edda8

Thanks
Best Regards
Hao

author reviewer
#14 lore raw

PATCH v7 4/4 alloc_tag: release the reservation when populate fails

In reply to Hao Ge

vm_module_tags_populate() can fail after the reservation was stored in the maple tree, and the error return leaks the entry, since a failed load never unloads the module. Release it.

Fixes 0f9b685626da ("alloc_tag: populate memory for module tags as needed")
---
 mm/alloc_tag.c | 1 +
 1 file changed, 1 insertion(+)
mm/alloc_tag.c +1 -0
index 248496470904..3c25475becbc 100644
@@ -971,6 +971,7 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
 			shutdown_mem_profiling(true);
 			pr_err("Failed to allocate memory for allocation tags in the module %s. Memory allocation profiling is disabled!\n",
 			       mod->name);
+			release_module_tags(mod, false);
 			return ERR_PTR(grow_res);
 		}
 	}
Signature · 1 line
2.25.1
#15 lore raw

Re: [PATCH v7 4/4] alloc_tag: release the reservation when populate fails

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Missing page table teardown on vmap_pages_range() failure in vm_module_tags_populate() creates dangling PTEs pointing to freed physical memory.
- [High] Failure to revert module_tags.size after vm_module_tags_populate() fails creates a race condition that can return unmapped memory to the module loader, leading to a kernel crash.
- [High] Lockless race between alloc_tag_sub() and clean_unused_counters() causes use-after-free on per-CPU counters.
--

commit d4d7fb3ee0094651ba53e0f489d27eaa1b6cc2af
Author: Hao Ge <[email protected]>
Subject: alloc_tag: release the reservation when populate fails

This patch updates reserve_module_tags() to call release_module_tags() when vm_module_tags_populate() fails. This ensures the module tags reservation is correctly released on error paths.

mm/alloc_tag.c static void *reserve_module_tags(struct module *mod, unsigned long size, · line 969

Quoted 12 lines
mm/alloc_tag.c +1 -0
index 112a014d4b890..c11813cd9e153 100644
@@ -963,6 +963,7 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
 			shutdown_mem_profiling(true);
 			pr_err("Failed to allocate memory for allocation tags in the module %s. Memory allocation profiling is disabled!\n",
 			       mod->name);
+			release_module_tags(mod, false);
 			return ERR_PTR(grow_res);
 		}
 	}

[Severity: High]
This isn't a bug introduced by this patch, but is it possible for module_tags.size to remain inflated here on error?

Earlier in reserve_module_tags(), module_tags.size is optimistically updated:

	if (module_tags.size < offset + size) {
		int grow_res;

		module_tags.size = offset + size;
		[...]
		grow_res = vm_module_tags_populate();

If vm_module_tags_populate() fails, the error path calls shutdown_mem_profiling() and releases the module tags, but it does not revert module_tags.size.

Can a second thread loading a different module concurrently evaluate codetag_needs_module_section() as true just before shutdown_mem_profiling() disables profiling?

If this second thread enters reserve_module_tags(), it might find the newly freed space at the same offset in the maple tree. Because module_tags.size was left inflated, would the check "if (module_tags.size < offset + size)" evaluate to false, causing the second thread to skip vm_module_tags_populate()?

This could result in returning a pointer to unmapped virtual memory to the module loader, leading to a kernel crash when the loader subsequently calls memcpy() on this pointer.

[Severity: Critical]
This is a pre-existing issue, but does vm_module_tags_populate() clean up page tables correctly when it fails?

Looking at vm_module_tags_populate(), physical pages are mapped using vmap_pages_range():

		if (nr < more_pages ||
		    vmap_pages_range(phys_end, phys_end + (nr << PAGE_SHIFT),
				     PAGE_KERNEL, next_page, PAGE_SHIFT) < 0) {
			release_pages_arg arg = { .pages = next_page };

			/* Clean up and error out */
			release_pages(arg, nr);
			return -ENOMEM;
		}

If vmap_pages_range() fails midway through, it returns a negative error code but does not unmap the pages it already successfully mapped.

The error handler unconditionally calls release_pages(arg, nr) to return the physical pages to the allocator, but it fails to call vunmap_range() to clear the kernel page table entries.

Does this leave valid, writable kernel PTEs pointing to freed physical memory? If these physical pages are reallocated to other subsystems, could a subsequent write to the module_tags virtual memory region silently corrupt unrelated kernel memory?

[Severity: High]
This is also a pre-existing issue, but is there a lockless race between alloc_tag_sub() and clean_unused_counters() that could cause a use-after-free?

When an object allocated by a module is freed, alloc_tag_sub() locklessly decrements the profiling counters:

	this_cpu_sub(tag->counters->bytes, bytes);
	this_cpu_dec(tag->counters->calls);

Concurrently, another module load or unload may trigger clean_unused_counters(), which checks if the counters are completely unused based solely on the bytes counter:

		counter = alloc_tag_read(tag);
		if (!counter.bytes) {
			free_percpu(tag->counters);
			tag->counters = NULL;

If a thread executing alloc_tag_sub() is preempted or interrupted after this_cpu_sub() but before this_cpu_dec(), the global sum of bytes could reach 0 while the calls counter decrement is still pending.

Seeing bytes == 0, clean_unused_counters() could immediately call free_percpu(tag->counters).

When the preempted thread resumes, would it attempt to execute this_cpu_dec(tag->counters->calls) on the freed memory, causing a use-after-free that corrupts the per-CPU allocator state?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
maintainer
#16 lore raw
In reply to Hao Ge
Quoted 10 lines · On Wed, 2 Sep 2026 16:17:58 +0800 Hao Ge <[email protected]> wrote:

I ran into an overflow problem in the module tag area. With profiling toggled off, the overflow check in reserve_module_tags() did not run, a module could load with more tags than the page flags can address, and re-enabling profiling then silently corrupted /proc/allocinfo. On overflow the fix shuts profiling down, releases the reservation and returns -EAGAIN, and the codetag section lands as regular module data in the same load, so the module loads without profiling.

Review of the earlier series by Sashiko turned up two more problems.

Thanks. Sashiko had a lot more to say, as I assume you've seen:

	https://sashiko.dev/#/patchset/[email protected]

Also, "Based-on-a-patch-by:" isn't a recognized thing, although it's been used ~30 times in the past.

I'm ambivalent about ad-hoc invention of new tags, although it happens a lot. Perhaps it would be better to do

	Based on a patch by Petr Pavlu [1].
	...
	Link: ... [1]

?

author reviewer
#17 lore raw

Hi Andrew

Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Wed, 2 Sep 2026 16:17:58 +0800 Hao Ge <[email protected]> wrote:

I ran into an overflow problem in the module tag area. With profiling toggled off, the overflow check in reserve_module_tags() did not run, a module could load with more tags than the page flags can address, and re-enabling profiling then silently corrupted /proc/allocinfo. On overflow the fix shuts profiling down, releases the reservation and returns -EAGAIN, and the codetag section lands as regular module data in the same load, so the module loads without profiling.

Quoted 5 lines
Quoted 1 line

Review of the earlier series by Sashiko turned up two more problems.

Thanks. Sashiko had a lot more to say, as I assume you've seen:

	https://sashiko.dev/#/patchset/[email protected]

Thanks.

Initially I was confused why Sashiko didn't review this patch on top of mm‑new. Some of the issues Sashiko reported have already been fixed in mm‑new. I checked my old reviews and saw other mm‑module patches were using mm‑new. That makes me wonder if cross‑module changes follow a different baseline.

No big deal, Sashiko did find two valid problems.

Quoted 7 lines

Also, "Based-on-a-patch-by:" isn't a recognized thing, although it's been used ~30 times in the past.

I'm ambivalent about ad-hoc invention of new tags, although it happens a lot. Perhaps it would be better to do

Quoted 5 lines
	Based on a patch by Petr Pavlu [1].
	...
	Link: ... [1]

?

OK, will do for the next version.

Thanks
Best Regards
Hao