Skip to content

Commit 66b2633

Browse files
committed
thermal: core: Store trip sysfs attributes in thermal_trip_desc
Instead of allocating memory for trip point sysfs attributes separately, store them in struct thermal_trip_desc for each trip individually which allows a few extra memory allocations to be avoided. No intentional functional impact. Signed-off-by: Rafael J. Wysocki <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 8ecd953 commit 66b2633

File tree

2 files changed

+46
-82
lines changed

2 files changed

+46
-82
lines changed

drivers/thermal/thermal_core.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,20 @@
1515
#include "thermal_netlink.h"
1616
#include "thermal_debugfs.h"
1717

18+
struct thermal_attr {
19+
struct device_attribute attr;
20+
char name[THERMAL_NAME_LENGTH];
21+
};
22+
23+
struct thermal_trip_attrs {
24+
struct thermal_attr type;
25+
struct thermal_attr temp;
26+
struct thermal_attr hyst;
27+
};
28+
1829
struct thermal_trip_desc {
1930
struct thermal_trip trip;
31+
struct thermal_trip_attrs trip_attrs;
2032
struct list_head notify_list_node;
2133
int notify_temp;
2234
int threshold;
@@ -56,9 +68,6 @@ struct thermal_governor {
5668
* @device: &struct device for this thermal zone
5769
* @removal: removal completion
5870
* @resume: resume completion
59-
* @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
60-
* @trip_type_attrs: attributes for trip points for sysfs: trip type
61-
* @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
6271
* @mode: current mode of this thermal zone
6372
* @devdata: private pointer for device private data
6473
* @num_trips: number of trip points the thermal zone supports
@@ -102,9 +111,6 @@ struct thermal_zone_device {
102111
struct completion removal;
103112
struct completion resume;
104113
struct attribute_group trips_attribute_group;
105-
struct thermal_attr *trip_temp_attrs;
106-
struct thermal_attr *trip_type_attrs;
107-
struct thermal_attr *trip_hyst_attrs;
108114
enum thermal_device_mode mode;
109115
void *devdata;
110116
int num_trips;
@@ -188,11 +194,6 @@ int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
188194

189195
struct thermal_zone_device *thermal_zone_get_by_id(int id);
190196

191-
struct thermal_attr {
192-
struct device_attribute attr;
193-
char name[THERMAL_NAME_LENGTH];
194-
};
195-
196197
static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
197198
{
198199
return cdev->ops->get_requested_power && cdev->ops->state2power &&

drivers/thermal/thermal_sysfs.c

Lines changed: 34 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -382,87 +382,55 @@ static const struct attribute_group *thermal_zone_attribute_groups[] = {
382382
*/
383383
static int create_trip_attrs(struct thermal_zone_device *tz)
384384
{
385-
const struct thermal_trip_desc *td;
385+
struct thermal_trip_desc *td;
386386
struct attribute **attrs;
387-
388-
/* This function works only for zones with at least one trip */
389-
if (tz->num_trips <= 0)
390-
return -EINVAL;
391-
392-
tz->trip_type_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_type_attrs),
393-
GFP_KERNEL);
394-
if (!tz->trip_type_attrs)
395-
return -ENOMEM;
396-
397-
tz->trip_temp_attrs = kcalloc(tz->num_trips, sizeof(*tz->trip_temp_attrs),
398-
GFP_KERNEL);
399-
if (!tz->trip_temp_attrs) {
400-
kfree(tz->trip_type_attrs);
401-
return -ENOMEM;
402-
}
403-
404-
tz->trip_hyst_attrs = kcalloc(tz->num_trips,
405-
sizeof(*tz->trip_hyst_attrs),
406-
GFP_KERNEL);
407-
if (!tz->trip_hyst_attrs) {
408-
kfree(tz->trip_type_attrs);
409-
kfree(tz->trip_temp_attrs);
410-
return -ENOMEM;
411-
}
387+
int i;
412388

413389
attrs = kcalloc(tz->num_trips * 3 + 1, sizeof(*attrs), GFP_KERNEL);
414-
if (!attrs) {
415-
kfree(tz->trip_type_attrs);
416-
kfree(tz->trip_temp_attrs);
417-
kfree(tz->trip_hyst_attrs);
390+
if (!attrs)
418391
return -ENOMEM;
419-
}
420392

393+
i = 0;
421394
for_each_trip_desc(tz, td) {
422-
int indx = thermal_zone_trip_id(tz, &td->trip);
395+
struct thermal_trip_attrs *trip_attrs = &td->trip_attrs;
423396

424397
/* create trip type attribute */
425-
snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
426-
"trip_point_%d_type", indx);
398+
snprintf(trip_attrs->type.name, THERMAL_NAME_LENGTH,
399+
"trip_point_%d_type", i);
427400

428-
sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
429-
tz->trip_type_attrs[indx].attr.attr.name =
430-
tz->trip_type_attrs[indx].name;
431-
tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
432-
tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
433-
attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
401+
sysfs_attr_init(&trip_attrs->type.attr.attr);
402+
trip_attrs->type.attr.attr.name = trip_attrs->type.name;
403+
trip_attrs->type.attr.attr.mode = S_IRUGO;
404+
trip_attrs->type.attr.show = trip_point_type_show;
405+
attrs[i] = &trip_attrs->type.attr.attr;
434406

435407
/* create trip temp attribute */
436-
snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
437-
"trip_point_%d_temp", indx);
438-
439-
sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
440-
tz->trip_temp_attrs[indx].attr.attr.name =
441-
tz->trip_temp_attrs[indx].name;
442-
tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
443-
tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
408+
snprintf(trip_attrs->temp.name, THERMAL_NAME_LENGTH,
409+
"trip_point_%d_temp", i);
410+
411+
sysfs_attr_init(&trip_attrs->temp.attr.attr);
412+
trip_attrs->temp.attr.attr.name = trip_attrs->temp.name;
413+
trip_attrs->temp.attr.attr.mode = S_IRUGO;
414+
trip_attrs->temp.attr.show = trip_point_temp_show;
444415
if (td->trip.flags & THERMAL_TRIP_FLAG_RW_TEMP) {
445-
tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
446-
tz->trip_temp_attrs[indx].attr.store =
447-
trip_point_temp_store;
416+
trip_attrs->temp.attr.attr.mode |= S_IWUSR;
417+
trip_attrs->temp.attr.store = trip_point_temp_store;
448418
}
449-
attrs[indx + tz->num_trips] = &tz->trip_temp_attrs[indx].attr.attr;
419+
attrs[i + tz->num_trips] = &trip_attrs->temp.attr.attr;
450420

451-
snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
452-
"trip_point_%d_hyst", indx);
421+
snprintf(trip_attrs->hyst.name, THERMAL_NAME_LENGTH,
422+
"trip_point_%d_hyst", i);
453423

454-
sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
455-
tz->trip_hyst_attrs[indx].attr.attr.name =
456-
tz->trip_hyst_attrs[indx].name;
457-
tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
458-
tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
424+
sysfs_attr_init(&trip_attrs->hyst.attr.attr);
425+
trip_attrs->hyst.attr.attr.name = trip_attrs->hyst.name;
426+
trip_attrs->hyst.attr.attr.mode = S_IRUGO;
427+
trip_attrs->hyst.attr.show = trip_point_hyst_show;
459428
if (td->trip.flags & THERMAL_TRIP_FLAG_RW_HYST) {
460-
tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
461-
tz->trip_hyst_attrs[indx].attr.store =
462-
trip_point_hyst_store;
429+
trip_attrs->hyst.attr.attr.mode |= S_IWUSR;
430+
trip_attrs->hyst.attr.store = trip_point_hyst_store;
463431
}
464-
attrs[indx + tz->num_trips * 2] =
465-
&tz->trip_hyst_attrs[indx].attr.attr;
432+
attrs[i + 2 * tz->num_trips] = &trip_attrs->hyst.attr.attr;
433+
i++;
466434
}
467435
attrs[tz->num_trips * 3] = NULL;
468436

@@ -479,13 +447,8 @@ static int create_trip_attrs(struct thermal_zone_device *tz)
479447
*/
480448
static void destroy_trip_attrs(struct thermal_zone_device *tz)
481449
{
482-
if (!tz)
483-
return;
484-
485-
kfree(tz->trip_type_attrs);
486-
kfree(tz->trip_temp_attrs);
487-
kfree(tz->trip_hyst_attrs);
488-
kfree(tz->trips_attribute_group.attrs);
450+
if (tz)
451+
kfree(tz->trips_attribute_group.attrs);
489452
}
490453

491454
int thermal_zone_create_device_groups(struct thermal_zone_device *tz)

0 commit comments

Comments
 (0)