Skip to content

Commit a42a583

Browse files
committed
thermal: core: Reference count the zone in thermal_zone_get_by_id()
There are places in the thermal netlink code where nothing prevents the thermal zone object from going away while being accessed after it has been returned by thermal_zone_get_by_id(). To address this, make thermal_zone_get_by_id() get a reference on the thermal zone device object to be returned with the help of get_device(), under thermal_list_lock, and adjust all of its callers to this change with the help of the cleanup.h infrastructure. Fixes: 1ce50e7 ("thermal: core: genetlink support for events/cmd/sampling") Cc: 6.8+ <[email protected]> # 6.8+ Signed-off-by: Rafael J. Wysocki <[email protected]> Reviewed-by: Lukasz Luba <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 9852d85 commit a42a583

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

drivers/thermal/thermal_core.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ struct thermal_zone_device *thermal_zone_get_by_id(int id)
728728
mutex_lock(&thermal_list_lock);
729729
list_for_each_entry(tz, &thermal_tz_list, node) {
730730
if (tz->id == id) {
731+
get_device(&tz->device);
731732
match = tz;
732733
break;
733734
}

drivers/thermal/thermal_core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
194194

195195
struct thermal_zone_device *thermal_zone_get_by_id(int id);
196196

197+
DEFINE_CLASS(thermal_zone_get_by_id, struct thermal_zone_device *,
198+
if (_T) put_device(&_T->device), thermal_zone_get_by_id(id), int id)
199+
197200
static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
198201
{
199202
return cdev->ops->get_requested_power && cdev->ops->state2power &&

drivers/thermal/thermal_netlink.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,6 @@ static int thermal_genl_cmd_tz_get_trip(struct param *p)
443443
{
444444
struct sk_buff *msg = p->msg;
445445
const struct thermal_trip_desc *td;
446-
struct thermal_zone_device *tz;
447446
struct nlattr *start_trip;
448447
int id;
449448

@@ -452,7 +451,7 @@ static int thermal_genl_cmd_tz_get_trip(struct param *p)
452451

453452
id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
454453

455-
tz = thermal_zone_get_by_id(id);
454+
CLASS(thermal_zone_get_by_id, tz)(id);
456455
if (!tz)
457456
return -EINVAL;
458457

@@ -488,15 +487,14 @@ static int thermal_genl_cmd_tz_get_trip(struct param *p)
488487
static int thermal_genl_cmd_tz_get_temp(struct param *p)
489488
{
490489
struct sk_buff *msg = p->msg;
491-
struct thermal_zone_device *tz;
492490
int temp, ret, id;
493491

494492
if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
495493
return -EINVAL;
496494

497495
id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
498496

499-
tz = thermal_zone_get_by_id(id);
497+
CLASS(thermal_zone_get_by_id, tz)(id);
500498
if (!tz)
501499
return -EINVAL;
502500

@@ -514,15 +512,14 @@ static int thermal_genl_cmd_tz_get_temp(struct param *p)
514512
static int thermal_genl_cmd_tz_get_gov(struct param *p)
515513
{
516514
struct sk_buff *msg = p->msg;
517-
struct thermal_zone_device *tz;
518515
int id, ret = 0;
519516

520517
if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
521518
return -EINVAL;
522519

523520
id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
524521

525-
tz = thermal_zone_get_by_id(id);
522+
CLASS(thermal_zone_get_by_id, tz)(id);
526523
if (!tz)
527524
return -EINVAL;
528525

0 commit comments

Comments
 (0)