[1/2] thermal/core: fix potential memory leak

Message ID 20230719011636.2893238-1-peng.fan@oss.nxp.com
State New
Headers
Series [1/2] thermal/core: fix potential memory leak |

Commit Message

Peng Fan (OSS) July 19, 2023, 1:16 a.m. UTC
  From: Peng Fan <peng.fan@nxp.com>

thermal_set_governor may allocate memory for tz->governor_data, so
need free it in failure handling path.

Addresses-Coverity: 25777220 ("Memory leak")
Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
 drivers/thermal/thermal_core.c | 3 +++
 1 file changed, 3 insertions(+)
  

Comments

Rafael J. Wysocki July 20, 2023, 7:02 p.m. UTC | #1
On Wed, Jul 19, 2023 at 3:12 AM Peng Fan (OSS) <peng.fan@oss.nxp.com> wrote:
>
> From: Peng Fan <peng.fan@nxp.com>
>
> thermal_set_governor may allocate memory for tz->governor_data, so
> need free it in failure handling path.
>
> Addresses-Coverity: 25777220 ("Memory leak")
> Signed-off-by: Peng Fan <peng.fan@nxp.com>
> ---
>  drivers/thermal/thermal_core.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index 842f678c1c3e..f633924406ad 100644
> --- a/drivers/thermal/thermal_core.c
> +++ b/drivers/thermal/thermal_core.c
> @@ -1355,6 +1355,9 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
>         return tz;
>
>  unregister:
> +       if (tz->governor && tz->governor->unbind_from_tz)
> +               tz->governor->unbind_from_tz(tz);
> +
>         device_del(&tz->device);
>  release_device:
>         put_device(&tz->device);
> --

Daniel, can you take a look at this series please and let me know what
you think?
  
Rafael J. Wysocki Aug. 22, 2023, 7:10 p.m. UTC | #2
On Wed, Jul 19, 2023 at 3:12 AM Peng Fan (OSS) <peng.fan@oss.nxp.com> wrote:
>
> From: Peng Fan <peng.fan@nxp.com>
>
> thermal_set_governor may allocate memory for tz->governor_data, so
> need free it in failure handling path.
>
> Addresses-Coverity: 25777220 ("Memory leak")
> Signed-off-by: Peng Fan <peng.fan@nxp.com>
> ---
>  drivers/thermal/thermal_core.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index 842f678c1c3e..f633924406ad 100644
> --- a/drivers/thermal/thermal_core.c
> +++ b/drivers/thermal/thermal_core.c
> @@ -1355,6 +1355,9 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
>         return tz;
>
>  unregister:
> +       if (tz->governor && tz->governor->unbind_from_tz)
> +               tz->governor->unbind_from_tz(tz);

It looks like thermal_governor_lock should be held around this, shouldn't it?

> +
>         device_del(&tz->device);
>  release_device:
>         put_device(&tz->device);
> --
> 2.37.1
>
  

Patch

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 842f678c1c3e..f633924406ad 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -1355,6 +1355,9 @@  thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
 	return tz;
 
 unregister:
+	if (tz->governor && tz->governor->unbind_from_tz)
+		tz->governor->unbind_from_tz(tz);
+
 	device_del(&tz->device);
 release_device:
 	put_device(&tz->device);