public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
From: Ido Schimmel <idosch@idosch.org>
To: Zhang Qilong <zhangqilong3@huawei.com>
Cc: rafael@kernel.org, daniel.lezcano@linaro.org, amitk@kernel.org,
	rui.zhang@intel.com, linux-pm@vger.kernel.org
Subject: Re: [PATCH] thermal/of: Fix possible memleak in thermal_of_zone_register()
Date: Thu, 20 Oct 2022 13:45:43 +0300	[thread overview]
Message-ID: <Y1Em16K5jxk3BQ0h@shredder> (raw)
In-Reply-To: <20221020080048.56377-1-zhangqilong3@huawei.com>

On Thu, Oct 20, 2022 at 04:00:48PM +0800, Zhang Qilong wrote:
> diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
> index d4b6335ace15..fc8fa27480a1 100644
> --- a/drivers/thermal/thermal_of.c
> +++ b/drivers/thermal/thermal_of.c
> @@ -596,10 +596,6 @@ struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor,
>  	int ntrips, mask;
>  	int ret;
>  
> -	of_ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
> -	if (!of_ops)
> -		return ERR_PTR(-ENOMEM);
> -
>  	np = of_thermal_zone_find(sensor, id);
>  	if (IS_ERR(np)) {
>  		if (PTR_ERR(np) != -ENODEV)
> @@ -626,6 +622,12 @@ struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor,
>  		goto out_kfree_trips;
>  	}
>  
> +	of_ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
> +	if (!of_ops) {
> +		ret = -ENOMEM;
> +		goto out_kfree_tzp;
> +	}
> +
>  	of_ops->get_trip_type = of_ops->get_trip_type ? : of_thermal_get_trip_type;
>  	of_ops->get_trip_temp = of_ops->get_trip_temp ? : of_thermal_get_trip_temp;
>  	of_ops->get_trip_hyst = of_ops->get_trip_hyst ? : of_thermal_get_trip_hyst;
> @@ -656,6 +658,7 @@ struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor,
>  	return tz;
>  
>  out_kfree_tzp:
> +	kfree(of_ops);
>  	kfree(tzp);
>  out_kfree_trips:
>  	kfree(trips);

The patch looks correct, but it can be cleaner. Ideally, you would have
a separate label to free 'of_ops' like we have for other variables.
Also, the error path is not symmetric with thermal_of_zone_unregister()
where this variable is the last to be freed, not the first.

I also encountered this issue and posted a patch:
https://lore.kernel.org/linux-pm/20221020103658.802457-1-idosch@nvidia.com/

Unless you see something wrong with it, can you please test and see if
it fixes your issue?

Thanks

  reply	other threads:[~2022-10-20 10:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-20  8:00 [PATCH] thermal/of: Fix possible memleak in thermal_of_zone_register() Zhang Qilong
2022-10-20 10:45 ` Ido Schimmel [this message]
2022-10-21  2:06   ` 答复: " zhangqilong

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=Y1Em16K5jxk3BQ0h@shredder \
    --to=idosch@idosch.org \
    --cc=amitk@kernel.org \
    --cc=daniel.lezcano@linaro.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=rui.zhang@intel.com \
    --cc=zhangqilong3@huawei.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