Skip to content

Commit a5a98a7

Browse files
committed
thermal: core: Add and use cooling device guard
Add and use a special guard for cooling devices. This allows quite a few error code paths to be simplified among other things and brings in 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 c597b4e commit a5a98a7

File tree

7 files changed

+60
-84
lines changed

7 files changed

+60
-84
lines changed

drivers/thermal/gov_power_allocator.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -549,18 +549,17 @@ static void allow_maximum_power(struct thermal_zone_device *tz)
549549
cdev = instance->cdev;
550550

551551
instance->target = 0;
552-
mutex_lock(&cdev->lock);
553-
/*
554-
* Call for updating the cooling devices local stats and avoid
555-
* periods of dozen of seconds when those have not been
556-
* maintained.
557-
*/
558-
cdev->ops->get_requested_power(cdev, &req_power);
559-
560-
if (params->update_cdevs)
561-
__thermal_cdev_update(cdev);
562-
563-
mutex_unlock(&cdev->lock);
552+
scoped_guard(cooling_dev, cdev) {
553+
/*
554+
* Call for updating the cooling devices local stats and
555+
* avoid periods of dozen of seconds when those have not
556+
* been maintained.
557+
*/
558+
cdev->ops->get_requested_power(cdev, &req_power);
559+
560+
if (params->update_cdevs)
561+
__thermal_cdev_update(cdev);
562+
}
564563
}
565564
}
566565

drivers/thermal/gov_step_wise.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ static void thermal_zone_trip_update(struct thermal_zone_device *tz,
9797

9898
instance->initialized = true;
9999

100-
mutex_lock(&instance->cdev->lock);
101-
instance->cdev->updated = false; /* cdev needs update */
102-
mutex_unlock(&instance->cdev->lock);
100+
scoped_guard(cooling_dev, instance->cdev) {
101+
instance->cdev->updated = false; /* cdev needs update */
102+
}
103103
}
104104
}
105105

drivers/thermal/thermal_core.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -758,12 +758,10 @@ static int thermal_instance_add(struct thermal_instance *new_instance,
758758

759759
list_add_tail(&new_instance->trip_node, &td->thermal_instances);
760760

761-
mutex_lock(&cdev->lock);
761+
guard(cooling_dev)(cdev);
762762

763763
list_add_tail(&new_instance->cdev_node, &cdev->thermal_instances);
764764

765-
mutex_unlock(&cdev->lock);
766-
767765
return 0;
768766
}
769767

@@ -872,11 +870,9 @@ static void thermal_instance_delete(struct thermal_instance *instance)
872870
{
873871
list_del(&instance->trip_node);
874872

875-
mutex_lock(&instance->cdev->lock);
873+
guard(cooling_dev)(instance->cdev);
876874

877875
list_del(&instance->cdev_node);
878-
879-
mutex_unlock(&instance->cdev->lock);
880876
}
881877

882878
/**
@@ -1239,10 +1235,10 @@ void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
12391235
* Update under the cdev lock to prevent the state from being set beyond
12401236
* the new limit concurrently.
12411237
*/
1242-
mutex_lock(&cdev->lock);
1238+
guard(cooling_dev)(cdev);
12431239

12441240
if (cdev->ops->get_max_state(cdev, &cdev->max_state))
1245-
goto unlock;
1241+
return;
12461242

12471243
thermal_cooling_device_stats_reinit(cdev);
12481244

@@ -1269,12 +1265,9 @@ void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
12691265
}
12701266

12711267
if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state)
1272-
goto unlock;
1268+
return;
12731269

12741270
thermal_cooling_device_stats_update(cdev, state);
1275-
1276-
unlock:
1277-
mutex_unlock(&cdev->lock);
12781271
}
12791272
EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
12801273

drivers/thermal/thermal_debugfs.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,19 @@ void thermal_debug_cdev_add(struct thermal_cooling_device *cdev, int state)
516516
cdev->debugfs = thermal_dbg;
517517
}
518518

519+
static struct thermal_debugfs *thermal_debug_cdev_clear(struct thermal_cooling_device *cdev)
520+
{
521+
struct thermal_debugfs *thermal_dbg;
522+
523+
guard(cooling_dev)(cdev);
524+
525+
thermal_dbg = cdev->debugfs;
526+
if (thermal_dbg)
527+
cdev->debugfs = NULL;
528+
529+
return thermal_dbg;
530+
}
531+
519532
/**
520533
* thermal_debug_cdev_remove - Remove a cooling device debugfs entry
521534
*
@@ -527,17 +540,9 @@ void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev)
527540
{
528541
struct thermal_debugfs *thermal_dbg;
529542

530-
mutex_lock(&cdev->lock);
531-
532-
thermal_dbg = cdev->debugfs;
533-
if (!thermal_dbg) {
534-
mutex_unlock(&cdev->lock);
543+
thermal_dbg = thermal_debug_cdev_clear(cdev);
544+
if (!thermal_dbg)
535545
return;
536-
}
537-
538-
cdev->debugfs = NULL;
539-
540-
mutex_unlock(&cdev->lock);
541546

542547
mutex_lock(&thermal_dbg->lock);
543548

drivers/thermal/thermal_helpers.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,10 @@ bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz,
5858
const struct thermal_trip *trip,
5959
struct thermal_cooling_device *cdev)
6060
{
61-
bool ret;
62-
6361
guard(thermal_zone)(tz);
62+
guard(cooling_dev)(cdev);
6463

65-
mutex_lock(&cdev->lock);
66-
67-
ret = thermal_instance_present(tz, cdev, trip);
68-
69-
mutex_unlock(&cdev->lock);
70-
71-
return ret;
64+
return thermal_instance_present(tz, cdev, trip);
7265
}
7366
EXPORT_SYMBOL_GPL(thermal_trip_is_bound_to_cdev);
7467

@@ -197,12 +190,12 @@ void __thermal_cdev_update(struct thermal_cooling_device *cdev)
197190
*/
198191
void thermal_cdev_update(struct thermal_cooling_device *cdev)
199192
{
200-
mutex_lock(&cdev->lock);
193+
guard(cooling_dev)(cdev);
194+
201195
if (!cdev->updated) {
202196
__thermal_cdev_update(cdev);
203197
cdev->updated = true;
204198
}
205-
mutex_unlock(&cdev->lock);
206199
}
207200

208201
/**
@@ -211,11 +204,9 @@ void thermal_cdev_update(struct thermal_cooling_device *cdev)
211204
*/
212205
void thermal_cdev_update_nocheck(struct thermal_cooling_device *cdev)
213206
{
214-
mutex_lock(&cdev->lock);
207+
guard(cooling_dev)(cdev);
215208

216209
__thermal_cdev_update(cdev);
217-
218-
mutex_unlock(&cdev->lock);
219210
}
220211

221212
/**

drivers/thermal/thermal_sysfs.c

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,15 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
544544
if (state > cdev->max_state)
545545
return -EINVAL;
546546

547-
mutex_lock(&cdev->lock);
547+
guard(cooling_dev)(cdev);
548548

549549
result = cdev->ops->set_cur_state(cdev, state);
550-
if (!result)
551-
thermal_cooling_device_stats_update(cdev, state);
550+
if (result)
551+
return result;
552+
553+
thermal_cooling_device_stats_update(cdev, state);
552554

553-
mutex_unlock(&cdev->lock);
554-
return result ? result : count;
555+
return count;
555556
}
556557

557558
static struct device_attribute
@@ -625,21 +626,18 @@ static ssize_t total_trans_show(struct device *dev,
625626
{
626627
struct thermal_cooling_device *cdev = to_cooling_device(dev);
627628
struct cooling_dev_stats *stats;
628-
int ret = 0;
629+
int ret;
629630

630-
mutex_lock(&cdev->lock);
631+
guard(cooling_dev)(cdev);
631632

632633
stats = cdev->stats;
633634
if (!stats)
634-
goto unlock;
635+
return 0;
635636

636637
spin_lock(&stats->lock);
637638
ret = sprintf(buf, "%u\n", stats->total_trans);
638639
spin_unlock(&stats->lock);
639640

640-
unlock:
641-
mutex_unlock(&cdev->lock);
642-
643641
return ret;
644642
}
645643

@@ -652,11 +650,11 @@ time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
652650
ssize_t len = 0;
653651
int i;
654652

655-
mutex_lock(&cdev->lock);
653+
guard(cooling_dev)(cdev);
656654

657655
stats = cdev->stats;
658656
if (!stats)
659-
goto unlock;
657+
return 0;
660658

661659
spin_lock(&stats->lock);
662660

@@ -668,9 +666,6 @@ time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
668666
}
669667
spin_unlock(&stats->lock);
670668

671-
unlock:
672-
mutex_unlock(&cdev->lock);
673-
674669
return len;
675670
}
676671

@@ -682,11 +677,11 @@ reset_store(struct device *dev, struct device_attribute *attr, const char *buf,
682677
struct cooling_dev_stats *stats;
683678
int i, states;
684679

685-
mutex_lock(&cdev->lock);
680+
guard(cooling_dev)(cdev);
686681

687682
stats = cdev->stats;
688683
if (!stats)
689-
goto unlock;
684+
return count;
690685

691686
states = cdev->max_state + 1;
692687

@@ -702,9 +697,6 @@ reset_store(struct device *dev, struct device_attribute *attr, const char *buf,
702697

703698
spin_unlock(&stats->lock);
704699

705-
unlock:
706-
mutex_unlock(&cdev->lock);
707-
708700
return count;
709701
}
710702

@@ -716,13 +708,11 @@ static ssize_t trans_table_show(struct device *dev,
716708
ssize_t len = 0;
717709
int i, j;
718710

719-
mutex_lock(&cdev->lock);
711+
guard(cooling_dev)(cdev);
720712

721713
stats = cdev->stats;
722-
if (!stats) {
723-
len = -ENODATA;
724-
goto unlock;
725-
}
714+
if (!stats)
715+
return -ENODATA;
726716

727717
len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
728718
len += snprintf(buf + len, PAGE_SIZE - len, " : ");
@@ -731,10 +721,8 @@ static ssize_t trans_table_show(struct device *dev,
731721
break;
732722
len += snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i);
733723
}
734-
if (len >= PAGE_SIZE) {
735-
len = PAGE_SIZE;
736-
goto unlock;
737-
}
724+
if (len >= PAGE_SIZE)
725+
return PAGE_SIZE;
738726

739727
len += snprintf(buf + len, PAGE_SIZE - len, "\n");
740728

@@ -760,9 +748,6 @@ static ssize_t trans_table_show(struct device *dev,
760748
len = -EFBIG;
761749
}
762750

763-
unlock:
764-
mutex_unlock(&cdev->lock);
765-
766751
return len;
767752
}
768753

include/linux/thermal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ struct thermal_cooling_device {
140140
#endif
141141
};
142142

143+
DEFINE_GUARD(cooling_dev, struct thermal_cooling_device *, mutex_lock(&_T->lock),
144+
mutex_unlock(&_T->lock))
145+
143146
/* Structure to define Thermal Zone parameters */
144147
struct thermal_zone_params {
145148
const char *governor_name;

0 commit comments

Comments
 (0)