All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hao Ge" <hao.ge@linux.dev>
Cc: "Petr Pavlu" <petr.pavlu@suse.com>, linux-modules@vger.kernel.org
Subject: Re: [PATCH v11 6/7] alloc_tag: skip percpu counter allocation when profiling is disabled
Date: Tue, 29 Sep 2026 08:40:45 +0000	[thread overview]
Message-ID: <20260929084045.9E4591F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260929082014.160587-7-hao.ge@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm: crash on freeing static percpu variable when module load fails or profiling disabled
--

commit 1ed77a1b45b5a7a434417c336e13dff08f4838d1
Author: Hao Ge <hao.ge@linux.dev>

alloc_tag: skip percpu counter allocation when profiling is disabled

After shutdown_mem_profiling() clears mem_profiling_support,
needs_section_mem() returns false, so later modules have their codetag
section placed as regular data and never enter the alloc_tag maple tree.
codetag_load_module() still called load_module(), which allocated a percpu
counter for every tag; release_module_tags() could not find these modules
on unload, so the counters leaked.

> diff --git a/lib/codetag.c b/lib/codetag.c
> index a9cda4c962a30..a0b600720afc1 100644
> --- a/lib/codetag.c
> +++ b/lib/codetag.c
[ ... ]
> @@ -388,7 +390,11 @@ void codetag_unload_module(struct module *mod)
>  			++cttype->content_id;
>  		}
>  		up_write(&cttype->mod_lock);
> -		if (found && cttype->desc.free_section_mem)
> +		/*
> +		 * A module whose module_load() returned -EOPNOTSUPP is not
> +		 * in the idr but may still hold reserved section memory.
> +		 */
> +		if (cttype->desc.free_section_mem)
>  			cttype->desc.free_section_mem(mod, true);
>  	}
>  	mutex_unlock(&codetag_lock);

[Severity: High]
Does this unconditional call to free_section_mem() lead to a crash when
freeing statically allocated percpu memory?

On architectures defining CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU,
DEFINE_ALLOC_TAG initializes tag->counters to the statically allocated
&_shared_alloc_tag.

If a module load skips dynamic counter allocation (either because
mem_profiling_support is false and load_module() returns -EOPNOTSUPP, or if
alloc_percpu() fails with -ENOMEM), tag->counters remains pointing to
&_shared_alloc_tag.

Because free_section_mem() is now called unconditionally here on module unload
or load failure, it triggers the cleanup path for these uninitialized tags:
release_module_tags() -> clean_unused_counters()

In clean_unused_counters(), since the static _shared_alloc_tag has 0 bytes,
it unconditionally calls free_percpu(tag->counters):

mm/alloc_tag.c:clean_unused_counters() {
	...
		if (!counter.bytes) {
			free_percpu(tag->counters);
			tag->counters = NULL;
	...
}

Calling free_percpu() on a statically allocated per-cpu variable that doesn't
belong to a managed chunk can corrupt memory and cause a crash.

> diff --git a/mm/alloc_tag.c b/mm/alloc_tag.c
> index 1a6108a596684..1106fc60dc89c 100644
> --- a/mm/alloc_tag.c
> +++ b/mm/alloc_tag.c
> @@ -981,6 +981,10 @@ static int load_module(struct module *mod, struct codetag *start, struct codetag
>  	struct alloc_tag *stop_tag;
>  	struct alloc_tag *tag;
>  
> +	/* Profiling disabled: load the module without its tags. */
> +	if (!mem_profiling_support)
> +		return -EOPNOTSUPP;
> +
>  	/* percpu counters for core allocations are already statically allocated */
>  	if (!mod)
>  		return 0;

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260929082014.160587-1-hao.ge@linux.dev?part=6

  reply	other threads:[~2026-09-29  8:40 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-29  8:20 [PATCH v11 0/7] alloc_tag and module codetag section fixes Hao Ge
2026-09-29  8:20 ` [PATCH v11 1/7] alloc_tag: move release_module_tags() above reserve_module_tags() Hao Ge
2026-09-29  8:20 ` [PATCH v11 2/7] mm/vmalloc: undo partial mappings inside the mapping functions Hao Ge
2026-09-29  8:33   ` sashiko-bot
2026-09-30 10:12   ` Uladzislau Rezki
2026-10-04 17:42     ` Suren Baghdasaryan
2026-10-09  8:23       ` Hao Ge
2026-10-08  6:53     ` Hao Ge
2026-10-08  8:09       ` Hao Ge
2026-10-08  8:13       ` Hao Ge
2026-09-29  8:20 ` [PATCH v11 3/7] alloc_tag: clean up the populate failure path Hao Ge
2026-10-04 17:55   ` Suren Baghdasaryan
2026-09-29  8:20 ` [PATCH v11 4/7] module: introduce SH_ENTSIZE_STANDALONE for separately allocated sections Hao Ge
2026-09-29  8:20 ` [PATCH v11 5/7] module: allocate codetag sections before the regular module layout Hao Ge
2026-09-29  8:20 ` [PATCH v11 6/7] alloc_tag: skip percpu counter allocation when profiling is disabled Hao Ge
2026-09-29  8:40   ` sashiko-bot [this message]
2026-09-29  8:20 ` [PATCH v11 7/7] alloc_tag: fix the /proc/allocinfo lifecycle Hao Ge
2026-10-04 18:23   ` Suren Baghdasaryan
2026-10-09 11:16     ` Hao Ge
2026-09-29 20:44 ` [PATCH v11 0/7] alloc_tag and module codetag section fixes Andrew Morton
2026-09-30  2:39   ` Suren Baghdasaryan
2026-09-30  3:26     ` Hao Ge
2026-09-30  8:34       ` Uladzislau Rezki
2026-10-04 17:42       ` Suren Baghdasaryan
2026-10-04 18:37       ` Suren Baghdasaryan
2026-10-09 11:30         ` Hao Ge

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20260929084045.9E4591F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=hao.ge@linux.dev \
    --cc=linux-modules@vger.kernel.org \
    --cc=petr.pavlu@suse.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

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

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