Skip to content

Commit cba00d1

Browse files
committed
thermal: core: Add and use thermal zone guard
Add and use a guard for thermal zone locking. This allows quite a few error code paths to be simplified among other things and brings in a noticeable code size reduction for a good measure. No intentional functional impact. Signed-off-by: Rafael J. Wysocki <[email protected]> Link: https://patch.msgid.link/[email protected] Reviewed-by: Lukasz Luba <[email protected]>
1 parent 17f76be commit cba00d1

File tree

8 files changed

+86
-136
lines changed

8 files changed

+86
-136
lines changed

drivers/thermal/thermal_core.c

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,13 @@ int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
202202
int ret = -EINVAL;
203203

204204
mutex_lock(&thermal_governor_lock);
205-
mutex_lock(&tz->lock);
206205

207-
gov = __find_governor(strim(policy));
208-
if (!gov)
209-
goto exit;
206+
guard(thermal_zone)(tz);
210207

211-
ret = thermal_set_governor(tz, gov);
208+
gov = __find_governor(strim(policy));
209+
if (gov)
210+
ret = thermal_set_governor(tz, gov);
212211

213-
exit:
214-
mutex_unlock(&tz->lock);
215212
mutex_unlock(&thermal_governor_lock);
216213

217214
thermal_notify_tz_gov_change(tz, policy);
@@ -617,26 +614,18 @@ static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
617614
{
618615
int ret;
619616

620-
mutex_lock(&tz->lock);
617+
guard(thermal_zone)(tz);
621618

622619
/* do nothing if mode isn't changing */
623-
if (mode == tz->mode) {
624-
mutex_unlock(&tz->lock);
625-
620+
if (mode == tz->mode)
626621
return 0;
627-
}
628622

629623
ret = __thermal_zone_device_set_mode(tz, mode);
630-
if (ret) {
631-
mutex_unlock(&tz->lock);
632-
624+
if (ret)
633625
return ret;
634-
}
635626

636627
__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
637628

638-
mutex_unlock(&tz->lock);
639-
640629
if (mode == THERMAL_DEVICE_ENABLED)
641630
thermal_notify_tz_enable(tz);
642631
else
@@ -665,10 +654,10 @@ static bool thermal_zone_is_present(struct thermal_zone_device *tz)
665654
void thermal_zone_device_update(struct thermal_zone_device *tz,
666655
enum thermal_notify_event event)
667656
{
668-
mutex_lock(&tz->lock);
657+
guard(thermal_zone)(tz);
658+
669659
if (thermal_zone_is_present(tz))
670660
__thermal_zone_device_update(tz, event);
671-
mutex_unlock(&tz->lock);
672661
}
673662
EXPORT_SYMBOL_GPL(thermal_zone_device_update);
674663

@@ -972,12 +961,10 @@ static bool __thermal_zone_cdev_bind(struct thermal_zone_device *tz,
972961
static void thermal_zone_cdev_bind(struct thermal_zone_device *tz,
973962
struct thermal_cooling_device *cdev)
974963
{
975-
mutex_lock(&tz->lock);
964+
guard(thermal_zone)(tz);
976965

977966
if (__thermal_zone_cdev_bind(tz, cdev))
978967
__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
979-
980-
mutex_unlock(&tz->lock);
981968
}
982969

983970
/**
@@ -1284,11 +1271,9 @@ static void __thermal_zone_cdev_unbind(struct thermal_zone_device *tz,
12841271
static void thermal_zone_cdev_unbind(struct thermal_zone_device *tz,
12851272
struct thermal_cooling_device *cdev)
12861273
{
1287-
mutex_lock(&tz->lock);
1274+
guard(thermal_zone)(tz);
12881275

12891276
__thermal_zone_cdev_unbind(tz, cdev);
1290-
1291-
mutex_unlock(&tz->lock);
12921277
}
12931278

12941279
/**
@@ -1334,7 +1319,7 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
13341319
if (tz->ops.get_crit_temp)
13351320
return tz->ops.get_crit_temp(tz, temp);
13361321

1337-
mutex_lock(&tz->lock);
1322+
guard(thermal_zone)(tz);
13381323

13391324
for_each_trip_desc(tz, td) {
13401325
const struct thermal_trip *trip = &td->trip;
@@ -1346,8 +1331,6 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
13461331
}
13471332
}
13481333

1349-
mutex_unlock(&tz->lock);
1350-
13511334
return ret;
13521335
}
13531336
EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
@@ -1360,7 +1343,7 @@ static void thermal_zone_init_complete(struct thermal_zone_device *tz)
13601343

13611344
list_add_tail(&tz->node, &thermal_tz_list);
13621345

1363-
mutex_lock(&tz->lock);
1346+
guard(thermal_zone)(tz);
13641347

13651348
/* Bind cooling devices for this zone. */
13661349
list_for_each_entry(cdev, &thermal_cdev_list, node)
@@ -1377,8 +1360,6 @@ static void thermal_zone_init_complete(struct thermal_zone_device *tz)
13771360

13781361
__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
13791362

1380-
mutex_unlock(&tz->lock);
1381-
13821363
mutex_unlock(&thermal_list_lock);
13831364
}
13841365

@@ -1615,7 +1596,7 @@ static bool thermal_zone_exit(struct thermal_zone_device *tz)
16151596
goto unlock;
16161597
}
16171598

1618-
mutex_lock(&tz->lock);
1599+
guard(thermal_zone)(tz);
16191600

16201601
tz->state |= TZ_STATE_FLAG_EXIT;
16211602
list_del_init(&tz->node);
@@ -1624,8 +1605,6 @@ static bool thermal_zone_exit(struct thermal_zone_device *tz)
16241605
list_for_each_entry(cdev, &thermal_cdev_list, node)
16251606
__thermal_zone_cdev_unbind(tz, cdev);
16261607

1627-
mutex_unlock(&tz->lock);
1628-
16291608
unlock:
16301609
mutex_unlock(&thermal_list_lock);
16311610

@@ -1710,7 +1689,7 @@ static void thermal_zone_device_resume(struct work_struct *work)
17101689

17111690
tz = container_of(work, struct thermal_zone_device, poll_queue.work);
17121691

1713-
mutex_lock(&tz->lock);
1692+
guard(thermal_zone)(tz);
17141693

17151694
tz->state &= ~(TZ_STATE_FLAG_SUSPENDED | TZ_STATE_FLAG_RESUMING);
17161695

@@ -1720,13 +1699,11 @@ static void thermal_zone_device_resume(struct work_struct *work)
17201699
__thermal_zone_device_update(tz, THERMAL_TZ_RESUME);
17211700

17221701
complete(&tz->resume);
1723-
1724-
mutex_unlock(&tz->lock);
17251702
}
17261703

17271704
static void thermal_zone_pm_prepare(struct thermal_zone_device *tz)
17281705
{
1729-
mutex_lock(&tz->lock);
1706+
guard(thermal_zone)(tz);
17301707

17311708
if (tz->state & TZ_STATE_FLAG_RESUMING) {
17321709
/*
@@ -1742,13 +1719,11 @@ static void thermal_zone_pm_prepare(struct thermal_zone_device *tz)
17421719
}
17431720

17441721
tz->state |= TZ_STATE_FLAG_SUSPENDED;
1745-
1746-
mutex_unlock(&tz->lock);
17471722
}
17481723

17491724
static void thermal_zone_pm_complete(struct thermal_zone_device *tz)
17501725
{
1751-
mutex_lock(&tz->lock);
1726+
guard(thermal_zone)(tz);
17521727

17531728
cancel_delayed_work(&tz->poll_queue);
17541729

@@ -1762,8 +1737,6 @@ static void thermal_zone_pm_complete(struct thermal_zone_device *tz)
17621737
INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_resume);
17631738
/* Queue up the work without a delay. */
17641739
mod_delayed_work(system_freezable_power_efficient_wq, &tz->poll_queue, 0);
1765-
1766-
mutex_unlock(&tz->lock);
17671740
}
17681741

17691742
static int thermal_pm_notify(struct notifier_block *nb,

drivers/thermal/thermal_core.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef __THERMAL_CORE_H__
1010
#define __THERMAL_CORE_H__
1111

12+
#include <linux/cleanup.h>
1213
#include <linux/device.h>
1314
#include <linux/thermal.h>
1415

@@ -146,6 +147,9 @@ struct thermal_zone_device {
146147
struct thermal_trip_desc trips[] __counted_by(num_trips);
147148
};
148149

150+
DEFINE_GUARD(thermal_zone, struct thermal_zone_device *, mutex_lock(&_T->lock),
151+
mutex_unlock(&_T->lock))
152+
149153
/* Initial thermal zone temperature. */
150154
#define THERMAL_TEMP_INIT INT_MIN
151155

drivers/thermal/thermal_debugfs.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -885,24 +885,29 @@ void thermal_debug_tz_add(struct thermal_zone_device *tz)
885885
tz->debugfs = thermal_dbg;
886886
}
887887

888+
static struct thermal_debugfs *thermal_debug_tz_clear(struct thermal_zone_device *tz)
889+
{
890+
struct thermal_debugfs *thermal_dbg;
891+
892+
guard(thermal_zone)(tz);
893+
894+
thermal_dbg = tz->debugfs;
895+
if (thermal_dbg)
896+
tz->debugfs = NULL;
897+
898+
return thermal_dbg;
899+
}
900+
888901
void thermal_debug_tz_remove(struct thermal_zone_device *tz)
889902
{
890903
struct thermal_debugfs *thermal_dbg;
891904
struct tz_episode *tze, *tmp;
892905
struct tz_debugfs *tz_dbg;
893906
int *trips_crossed;
894907

895-
mutex_lock(&tz->lock);
896-
897-
thermal_dbg = tz->debugfs;
898-
if (!thermal_dbg) {
899-
mutex_unlock(&tz->lock);
908+
thermal_dbg = thermal_debug_tz_clear(tz);
909+
if (!thermal_dbg)
900910
return;
901-
}
902-
903-
tz->debugfs = NULL;
904-
905-
mutex_unlock(&tz->lock);
906911

907912
tz_dbg = &thermal_dbg->tz_dbg;
908913

drivers/thermal/thermal_helpers.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz,
6060
{
6161
bool ret;
6262

63-
mutex_lock(&tz->lock);
63+
guard(thermal_zone)(tz);
64+
6465
mutex_lock(&cdev->lock);
6566

6667
ret = thermal_instance_present(tz, cdev, trip);
6768

6869
mutex_unlock(&cdev->lock);
69-
mutex_unlock(&tz->lock);
7070

7171
return ret;
7272
}
@@ -138,19 +138,14 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
138138
if (IS_ERR_OR_NULL(tz))
139139
return -EINVAL;
140140

141-
mutex_lock(&tz->lock);
141+
guard(thermal_zone)(tz);
142142

143-
if (!tz->ops.get_temp) {
144-
ret = -EINVAL;
145-
goto unlock;
146-
}
143+
if (!tz->ops.get_temp)
144+
return -EINVAL;
147145

148146
ret = __thermal_zone_get_temp(tz, temp);
149147
if (!ret && *temp <= THERMAL_TEMP_INVALID)
150-
ret = -ENODATA;
151-
152-
unlock:
153-
mutex_unlock(&tz->lock);
148+
return -ENODATA;
154149

155150
return ret;
156151
}

drivers/thermal/thermal_hwmon.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,9 @@ temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
7878
int temperature;
7979
int ret;
8080

81-
mutex_lock(&tz->lock);
81+
guard(thermal_zone)(tz);
8282

8383
ret = tz->ops.get_crit_temp(tz, &temperature);
84-
85-
mutex_unlock(&tz->lock);
86-
8784
if (ret)
8885
return ret;
8986

drivers/thermal/thermal_netlink.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ static int thermal_genl_cmd_tz_get_trip(struct param *p)
459459
if (!start_trip)
460460
return -EMSGSIZE;
461461

462-
mutex_lock(&tz->lock);
462+
guard(thermal_zone)(tz);
463463

464464
for_each_trip_desc(tz, td) {
465465
const struct thermal_trip *trip = &td->trip;
@@ -469,19 +469,12 @@ static int thermal_genl_cmd_tz_get_trip(struct param *p)
469469
nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, trip->type) ||
470470
nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, trip->temperature) ||
471471
nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, trip->hysteresis))
472-
goto out_cancel_nest;
472+
return -EMSGSIZE;
473473
}
474474

475-
mutex_unlock(&tz->lock);
476-
477475
nla_nest_end(msg, start_trip);
478476

479477
return 0;
480-
481-
out_cancel_nest:
482-
mutex_unlock(&tz->lock);
483-
484-
return -EMSGSIZE;
485478
}
486479

487480
static int thermal_genl_cmd_tz_get_temp(struct param *p)
@@ -512,7 +505,7 @@ static int thermal_genl_cmd_tz_get_temp(struct param *p)
512505
static int thermal_genl_cmd_tz_get_gov(struct param *p)
513506
{
514507
struct sk_buff *msg = p->msg;
515-
int id, ret = 0;
508+
int id;
516509

517510
if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
518511
return -EINVAL;
@@ -523,16 +516,14 @@ static int thermal_genl_cmd_tz_get_gov(struct param *p)
523516
if (!tz)
524517
return -EINVAL;
525518

526-
mutex_lock(&tz->lock);
519+
guard(thermal_zone)(tz);
527520

528521
if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
529522
nla_put_string(msg, THERMAL_GENL_ATTR_TZ_GOV_NAME,
530523
tz->governor->name))
531-
ret = -EMSGSIZE;
532-
533-
mutex_unlock(&tz->lock);
524+
return -EMSGSIZE;
534525

535-
return ret;
526+
return 0;
536527
}
537528

538529
static int __thermal_genl_cmd_cdev_get(struct thermal_cooling_device *cdev,

0 commit comments

Comments
 (0)