public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
From: "Rafael J. Wysocki" <rafael@kernel.org>
To: Linux PM <linux-pm@vger.kernel.org>
Cc: Daniel Lezcano <daniel.lezcano@linaro.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Lukasz Luba <lukasz.luba@arm.com>, Armin Wolf <w_armin@gmx.de>
Subject: [PATCH v3 1/6] thermal: core: Fix thermal zone governor cleanup issues
Date: Mon, 06 Apr 2026 18:07:39 +0200	[thread overview]
Message-ID: <2288346.irdbgypaU6@rafael.j.wysocki> (raw)
In-Reply-To: <5119690.31r3eYUQgx@rafael.j.wysocki>

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

If thermal_zone_device_register_with_trips() fails after adding
a thermal governor to the thermal zone being registered, the
governor is not removed from it as appropriate which may lead to
a memory leak, so address this by adding the governor cleanup to
the rollback path.

In turn, thermal_zone_device_unregister() calls thermal_set_governor()
without acquiring the thermal zone lock beforehand which may race with
a governor update via sysfs and may lead to a use-after-free in that
case, so address it by placing the cleanup thermal_set_governor()
call after the wait_for_completion() one, which reflects the
registration error path ordering.

Fixes: e33df1d2f3a0 ("thermal: let governors have private data for each thermal zone")
Cc: All applicable <stable@vger.kernel.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---

v2 -> v3: New patch

---
 drivers/thermal/thermal_core.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -1618,7 +1618,7 @@ thermal_zone_device_register_with_trips(
 	/* Add nodes that are always present via .groups */
 	result = thermal_zone_create_device_groups(tz);
 	if (result)
-		goto remove_id;
+		goto remove_governor;
 
 	result = device_register(&tz->device);
 	if (result)
@@ -1649,6 +1649,8 @@ unregister:
 release_device:
 	put_device(&tz->device);
 	wait_for_completion(&tz->removal);
+remove_governor:
+	thermal_set_governor(tz, NULL);
 remove_id:
 	ida_free(&thermal_tz_ida, id);
 free_tzp:
@@ -1731,8 +1733,6 @@ void thermal_zone_device_unregister(stru
 
 	cancel_delayed_work_sync(&tz->poll_queue);
 
-	thermal_set_governor(tz, NULL);
-
 	thermal_thresholds_exit(tz);
 	thermal_remove_hwmon_sysfs(tz);
 	ida_free(&thermal_tz_ida, tz->id);
@@ -1744,6 +1744,9 @@ void thermal_zone_device_unregister(stru
 	thermal_notify_tz_delete(tz);
 
 	wait_for_completion(&tz->removal);
+
+	thermal_set_governor(tz, NULL);
+
 	kfree(tz->tzp);
 	kfree(tz);
 }




  reply	other threads:[~2026-04-06 16:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-06 16:04 [PATCH v3 0/6] thermal: core: Fixes, simplifications and suspend/resume relocation Rafael J. Wysocki
2026-04-06 16:07 ` Rafael J. Wysocki [this message]
2026-04-06 16:09 ` [PATCH v3 2/6] thermal: core: Free thermal zone ID later during removal Rafael J. Wysocki
2026-04-06 16:10 ` [PATCH v3 3/6] thermal: core: Drop redundant check from thermal_zone_device_update() Rafael J. Wysocki
2026-04-06 16:11 ` [PATCH v3 4/6] thermal: core: Change thermal_wq to be unbound and not freezable Rafael J. Wysocki
2026-04-06 16:14 ` [PATCH v3 5/6] thermal: core: Allocate thermal_class statically Rafael J. Wysocki
2026-04-06 16:16 ` [PATCH v3 6/6] thermal: core: Suspend thermal zones later and resume them earlier Rafael J. Wysocki
2026-04-06 21:40   ` Armin Wolf

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=2288346.irdbgypaU6@rafael.j.wysocki \
    --to=rafael@kernel.org \
    --cc=daniel.lezcano@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=lukasz.luba@arm.com \
    --cc=w_armin@gmx.de \
    /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