mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Huang, Ying" <ying.huang@linux.alibaba.com>
To: Luo Gengkun <luogengkun@huaweicloud.com>
Cc: mingo@redhat.com,  peterz@infradead.org,  juri.lelli@redhat.com,
	vincent.guittot@linaro.org,  dietmar.eggemann@arm.com,
	rostedt@goodmis.org,  bsegall@google.com,  mgorman@suse.de,
	vschneid@redhat.com,  huang.ying.caritas@gmail.com,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] sched/topology: Fix memory leak in the error path of sched_init_numa
Date: Mon, 13 Oct 2025 18:53:48 +0800	[thread overview]
Message-ID: <87ikgjt6mb.fsf@DESKTOP-5N7EMDA> (raw)
In-Reply-To: <20251013041348.350886-1-luogengkun@huaweicloud.com> (Luo Gengkun's message of "Mon, 13 Oct 2025 04:13:48 +0000")

Luo Gengkun <luogengkun@huaweicloud.com> writes:

> In sched_init_numa, masks are used to store memory, but the error path
> returns directly without freeing the allocated memory.
> To fix this, the freeing logic in sched_reset_numa can be extraced into a
> new function, free_masks, which can be called on the error path.

Good catch!  Thanks!

> Fixes: 0fb3978b0aac ("sched/numa: Fix NUMA topology for systems with CPU-less nodes")
> Signed-off-by: Luo Gengkun <luogengkun@huaweicloud.com>
> ---
>  kernel/sched/topology.c | 33 +++++++++++++++++++++------------
>  1 file changed, 21 insertions(+), 12 deletions(-)
>
> diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> index 444bdfdab731..fd03bb6669f5 100644
> --- a/kernel/sched/topology.c
> +++ b/kernel/sched/topology.c
> @@ -1924,6 +1924,20 @@ static void init_numa_topology_type(int offline_node)
>  
>  #define NR_DISTANCE_VALUES (1 << DISTANCE_BITS)
>  
> +static void free_masks(struct cpumask ***masks, int nr_levels)

The function name appears too general.  How about __sched_free_masks()?

> +{
> +	int i, j;
> +
> +	for (i = 0; i < nr_levels && masks; i++) {
> +		if (!masks[i])
> +			continue;
> +		for_each_node(j)
> +			kfree(masks[i][j]);
> +		kfree(masks[i]);
> +	}
> +	kfree(masks);
> +}
> +
>  void sched_init_numa(int offline_node)
>  {
>  	struct sched_domain_topology_level *tl;
> @@ -2003,15 +2017,19 @@ void sched_init_numa(int offline_node)
>  	 */
>  	for (i = 0; i < nr_levels; i++) {
>  		masks[i] = kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
> -		if (!masks[i])
> +		if (!masks[i]) {
> +			free_masks(masks, nr_levels);
>  			return;
> +		}
>  
>  		for_each_cpu_node_but(j, offline_node) {
>  			struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
>  			int k;
>  
> -			if (!mask)
> +			if (!mask) {
> +				free_masks(masks, nr_levels);
>  				return;
> +			}
>  
>  			masks[i][j] = mask;
>  
> @@ -2079,18 +2097,9 @@ static void sched_reset_numa(void)
>  	masks = sched_domains_numa_masks;
>  	rcu_assign_pointer(sched_domains_numa_masks, NULL);
>  	if (distances || masks) {
> -		int i, j;
> -
>  		synchronize_rcu();
>  		kfree(distances);
> -		for (i = 0; i < nr_levels && masks; i++) {
> -			if (!masks[i])
> -				continue;
> -			for_each_node(j)
> -				kfree(masks[i][j]);
> -			kfree(masks[i]);
> -		}
> -		kfree(masks);
> +		free_masks(masks, nr_levels);
>  	}
>  	if (sched_domain_topology_saved) {
>  		kfree(sched_domain_topology);

Otherwise, the patch LGTM.  Feel free to add my

Reviewed-by: Huang Ying <ying.huang@linux.alibaba.com>

in the future versions.

---
Best Regards,
Huang, Ying

      reply	other threads:[~2025-10-13 10:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-13  4:13 Luo Gengkun
2025-10-13 10:53 ` Huang, Ying [this message]

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=87ikgjt6mb.fsf@DESKTOP-5N7EMDA \
    --to=ying.huang@linux.alibaba.com \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=huang.ying.caritas@gmail.com \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luogengkun@huaweicloud.com \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    /path/to/YOUR_REPLY

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

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

all inboxes | Powered by JetHome®