Skip to content

Commit d1c8aa2

Browse files
committed
thermal: core: Manage thermal_list_lock using a mutex guard
Switch over the thermal core to using a mutex guard for thermal_list_lock management. 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 6f60ae7 commit d1c8aa2

File tree

1 file changed

+68
-80
lines changed

1 file changed

+68
-80
lines changed

drivers/thermal/thermal_core.c

Lines changed: 68 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ int thermal_register_governor(struct thermal_governor *governor)
140140
def_governor = governor;
141141
}
142142

143-
mutex_lock(&thermal_list_lock);
143+
guard(mutex)(&thermal_list_lock);
144144

145145
list_for_each_entry(pos, &thermal_tz_list, node) {
146146
/*
@@ -163,7 +163,6 @@ int thermal_register_governor(struct thermal_governor *governor)
163163
}
164164
}
165165

166-
mutex_unlock(&thermal_list_lock);
167166
mutex_unlock(&thermal_governor_lock);
168167

169168
return err;
@@ -181,16 +180,16 @@ void thermal_unregister_governor(struct thermal_governor *governor)
181180
if (!__find_governor(governor->name))
182181
goto exit;
183182

184-
mutex_lock(&thermal_list_lock);
183+
list_del(&governor->governor_list);
184+
185+
guard(mutex)(&thermal_list_lock);
185186

186187
list_for_each_entry(pos, &thermal_tz_list, node) {
187188
if (!strncasecmp(pos->governor->name, governor->name,
188189
THERMAL_NAME_LENGTH))
189190
thermal_set_governor(pos, NULL);
190191
}
191192

192-
mutex_unlock(&thermal_list_lock);
193-
list_del(&governor->governor_list);
194193
exit:
195194
mutex_unlock(&thermal_governor_lock);
196195
}
@@ -688,51 +687,52 @@ int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
688687
void *), void *data)
689688
{
690689
struct thermal_cooling_device *cdev;
691-
int ret = 0;
692690

693-
mutex_lock(&thermal_list_lock);
691+
guard(mutex)(&thermal_list_lock);
692+
694693
list_for_each_entry(cdev, &thermal_cdev_list, node) {
694+
int ret;
695+
695696
ret = cb(cdev, data);
696697
if (ret)
697-
break;
698+
return ret;
698699
}
699-
mutex_unlock(&thermal_list_lock);
700700

701-
return ret;
701+
return 0;
702702
}
703703

704704
int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
705705
void *data)
706706
{
707707
struct thermal_zone_device *tz;
708-
int ret = 0;
709708

710-
mutex_lock(&thermal_list_lock);
709+
guard(mutex)(&thermal_list_lock);
710+
711711
list_for_each_entry(tz, &thermal_tz_list, node) {
712+
int ret;
713+
712714
ret = cb(tz, data);
713715
if (ret)
714-
break;
716+
return ret;
715717
}
716-
mutex_unlock(&thermal_list_lock);
717718

718-
return ret;
719+
return 0;
719720
}
720721

721722
struct thermal_zone_device *thermal_zone_get_by_id(int id)
722723
{
723-
struct thermal_zone_device *tz, *match = NULL;
724+
struct thermal_zone_device *tz;
725+
726+
guard(mutex)(&thermal_list_lock);
724727

725-
mutex_lock(&thermal_list_lock);
726728
list_for_each_entry(tz, &thermal_tz_list, node) {
727729
if (tz->id == id) {
728730
get_device(&tz->device);
729-
match = tz;
730-
break;
731+
return tz;
731732
}
732733
}
733-
mutex_unlock(&thermal_list_lock);
734734

735-
return match;
735+
return NULL;
736736
}
737737

738738
/*
@@ -971,14 +971,12 @@ static void thermal_cooling_device_init_complete(struct thermal_cooling_device *
971971
{
972972
struct thermal_zone_device *tz;
973973

974-
mutex_lock(&thermal_list_lock);
974+
guard(mutex)(&thermal_list_lock);
975975

976976
list_add(&cdev->node, &thermal_cdev_list);
977977

978978
list_for_each_entry(tz, &thermal_tz_list, node)
979979
thermal_zone_cdev_bind(tz, cdev);
980-
981-
mutex_unlock(&thermal_list_lock);
982980
}
983981

984982
/**
@@ -1212,10 +1210,10 @@ void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
12121210
* Hold thermal_list_lock throughout the update to prevent the device
12131211
* from going away while being updated.
12141212
*/
1215-
mutex_lock(&thermal_list_lock);
1213+
guard(mutex)(&thermal_list_lock);
12161214

12171215
if (!thermal_cooling_device_present(cdev))
1218-
goto unlock_list;
1216+
return;
12191217

12201218
/*
12211219
* Update under the cdev lock to prevent the state from being set beyond
@@ -1257,9 +1255,6 @@ void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
12571255

12581256
unlock:
12591257
mutex_unlock(&cdev->lock);
1260-
1261-
unlock_list:
1262-
mutex_unlock(&thermal_list_lock);
12631258
}
12641259
EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
12651260

@@ -1283,24 +1278,18 @@ static void thermal_zone_cdev_unbind(struct thermal_zone_device *tz,
12831278
static bool thermal_cooling_device_exit(struct thermal_cooling_device *cdev)
12841279
{
12851280
struct thermal_zone_device *tz;
1286-
bool ret = true;
12871281

1288-
mutex_lock(&thermal_list_lock);
1282+
guard(mutex)(&thermal_list_lock);
12891283

1290-
if (!thermal_cooling_device_present(cdev)) {
1291-
ret = false;
1292-
goto unlock;
1293-
}
1284+
if (!thermal_cooling_device_present(cdev))
1285+
return false;
12941286

12951287
list_del(&cdev->node);
12961288

12971289
list_for_each_entry(tz, &thermal_tz_list, node)
12981290
thermal_zone_cdev_unbind(tz, cdev);
12991291

1300-
unlock:
1301-
mutex_unlock(&thermal_list_lock);
1302-
1303-
return ret;
1292+
return true;
13041293
}
13051294

13061295
/**
@@ -1347,7 +1336,7 @@ static void thermal_zone_init_complete(struct thermal_zone_device *tz)
13471336
{
13481337
struct thermal_cooling_device *cdev;
13491338

1350-
mutex_lock(&thermal_list_lock);
1339+
guard(mutex)(&thermal_list_lock);
13511340

13521341
list_add_tail(&tz->node, &thermal_tz_list);
13531342

@@ -1367,8 +1356,6 @@ static void thermal_zone_init_complete(struct thermal_zone_device *tz)
13671356
tz->state |= TZ_STATE_FLAG_SUSPENDED;
13681357

13691358
__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1370-
1371-
mutex_unlock(&thermal_list_lock);
13721359
}
13731360

13741361
/**
@@ -1595,14 +1582,11 @@ EXPORT_SYMBOL_GPL(thermal_zone_device);
15951582
static bool thermal_zone_exit(struct thermal_zone_device *tz)
15961583
{
15971584
struct thermal_cooling_device *cdev;
1598-
bool ret = true;
15991585

1600-
mutex_lock(&thermal_list_lock);
1586+
guard(mutex)(&thermal_list_lock);
16011587

1602-
if (list_empty(&tz->node)) {
1603-
ret = false;
1604-
goto unlock;
1605-
}
1588+
if (list_empty(&tz->node))
1589+
return false;
16061590

16071591
guard(thermal_zone)(tz);
16081592

@@ -1613,10 +1597,7 @@ static bool thermal_zone_exit(struct thermal_zone_device *tz)
16131597
list_for_each_entry(cdev, &thermal_cdev_list, node)
16141598
__thermal_zone_cdev_unbind(tz, cdev);
16151599

1616-
unlock:
1617-
mutex_unlock(&thermal_list_lock);
1618-
1619-
return ret;
1600+
return true;
16201601
}
16211602

16221603
/**
@@ -1669,24 +1650,23 @@ struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
16691650
unsigned int found = 0;
16701651

16711652
if (!name)
1672-
goto exit;
1653+
return ERR_PTR(-EINVAL);
1654+
1655+
guard(mutex)(&thermal_list_lock);
16731656

1674-
mutex_lock(&thermal_list_lock);
16751657
list_for_each_entry(pos, &thermal_tz_list, node)
16761658
if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
16771659
found++;
16781660
ref = pos;
16791661
}
1680-
mutex_unlock(&thermal_list_lock);
16811662

1682-
/* nothing has been found, thus an error code for it */
1683-
if (found == 0)
1684-
ref = ERR_PTR(-ENODEV);
1685-
else if (found > 1)
1686-
/* Success only when an unique zone is found */
1687-
ref = ERR_PTR(-EEXIST);
1663+
if (!found)
1664+
return ERR_PTR(-ENODEV);
1665+
1666+
/* Success only when one zone is found. */
1667+
if (found > 1)
1668+
return ERR_PTR(-EEXIST);
16881669

1689-
exit:
16901670
return ref;
16911671
}
16921672
EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
@@ -1727,6 +1707,18 @@ static void thermal_zone_pm_prepare(struct thermal_zone_device *tz)
17271707
tz->state |= TZ_STATE_FLAG_SUSPENDED;
17281708
}
17291709

1710+
static void thermal_pm_notify_prepare(void)
1711+
{
1712+
struct thermal_zone_device *tz;
1713+
1714+
guard(mutex)(&thermal_list_lock);
1715+
1716+
thermal_pm_suspended = true;
1717+
1718+
list_for_each_entry(tz, &thermal_tz_list, node)
1719+
thermal_zone_pm_prepare(tz);
1720+
}
1721+
17301722
static void thermal_zone_pm_complete(struct thermal_zone_device *tz)
17311723
{
17321724
guard(thermal_zone)(tz);
@@ -1745,35 +1737,31 @@ static void thermal_zone_pm_complete(struct thermal_zone_device *tz)
17451737
mod_delayed_work(system_freezable_power_efficient_wq, &tz->poll_queue, 0);
17461738
}
17471739

1748-
static int thermal_pm_notify(struct notifier_block *nb,
1749-
unsigned long mode, void *_unused)
1740+
static void thermal_pm_notify_complete(void)
17501741
{
17511742
struct thermal_zone_device *tz;
17521743

1744+
guard(mutex)(&thermal_list_lock);
1745+
1746+
thermal_pm_suspended = false;
1747+
1748+
list_for_each_entry(tz, &thermal_tz_list, node)
1749+
thermal_zone_pm_complete(tz);
1750+
}
1751+
1752+
static int thermal_pm_notify(struct notifier_block *nb,
1753+
unsigned long mode, void *_unused)
1754+
{
17531755
switch (mode) {
17541756
case PM_HIBERNATION_PREPARE:
17551757
case PM_RESTORE_PREPARE:
17561758
case PM_SUSPEND_PREPARE:
1757-
mutex_lock(&thermal_list_lock);
1758-
1759-
thermal_pm_suspended = true;
1760-
1761-
list_for_each_entry(tz, &thermal_tz_list, node)
1762-
thermal_zone_pm_prepare(tz);
1763-
1764-
mutex_unlock(&thermal_list_lock);
1759+
thermal_pm_notify_prepare();
17651760
break;
17661761
case PM_POST_HIBERNATION:
17671762
case PM_POST_RESTORE:
17681763
case PM_POST_SUSPEND:
1769-
mutex_lock(&thermal_list_lock);
1770-
1771-
thermal_pm_suspended = false;
1772-
1773-
list_for_each_entry(tz, &thermal_tz_list, node)
1774-
thermal_zone_pm_complete(tz);
1775-
1776-
mutex_unlock(&thermal_list_lock);
1764+
thermal_pm_notify_complete();
17771765
break;
17781766
default:
17791767
break;

0 commit comments

Comments
 (0)