thermal: sysfs: Get to trip attributes via the attribute argument of show/store
cover thermal: sysfs: Get to trip attributes via the attribute argument of show/store
Hi Everyone,
This series reworks the trip point sysfs interface to get to trips via the attribute argument of show/store instead of using the attribute name to get a trip ID and look up a trip using it.
It also adds a small cleanup (the last patch) for a good measure.
Thanks!
PATCH 1/3 thermal: core: Store trip sysfs attributes in thermal_trip_desc
From: Rafael J. Wysocki <[email protected]>
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.
--- drivers/thermal/thermal_core.h | 23 ++++---- drivers/thermal/thermal_sysfs.c | 103 ++++++++++++---------------------------- 2 files changed, 45 insertions(+), 81 deletions(-)
drivers/thermal/thermal_core.h +12 -11
@@ -15,8 +15,20 @@#include "thermal_netlink.h" #include "thermal_debugfs.h" +struct thermal_attr { +struct device_attribute attr; +char name[THERMAL_NAME_LENGTH ]; +}; + +struct thermal_trip_attrs { +struct thermal_attr type; +struct thermal_attr temp; +struct thermal_attr hyst; +}; +struct thermal_trip_desc {struct thermal_trip trip; +struct thermal_trip_attrs trip_attrs;struct list_head notify_list_node;int notify_temp;int threshold;
@@ -56,9 +68,6 @@ struct thermal_governor {* @device: &struct device for this thermal zone * @removal: removal completion * @resume: resume completion -* @trip_temp_attrs: attributes for trip points for sysfs: trip temperature -* @trip_type_attrs: attributes for trip points for sysfs: trip type -* @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis * @mode: current mode of this thermal zone * @devdata: private pointer for device private data * @num_trips: number of trip points the thermal zone supports
@@ -102,9 +111,6 @@ struct thermal_zone_device {struct completion removal;struct completion resume;struct attribute_group trips_attribute_group; -struct thermal_attr *trip_temp_attrs; -struct thermal_attr *trip_type_attrs; -struct thermal_attr *trip_hyst_attrs;enum thermal_device_mode mode;void *devdata;int num_trips;
@@ -188,11 +194,6 @@ int for_each_thermal_governor(int (*cb)(struct thermal_zone_device *thermal_zone_get_by_id (int id); -struct thermal_attr { -struct device_attribute attr; -char name[THERMAL_NAME_LENGTH ]; -}; -static inline bool cdev_is_power_actor (struct thermal_cooling_device *cdev) { return cdev->ops->get_requested_power && cdev->ops->state2power &&
drivers/thermal/thermal_sysfs.c +33 -70
133 lines not on this page · raw message
PATCH 2/3 thermal: sysfs: Get to trips via attribute pointers
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
The _store() and _show() functions for sysfs attributes corresponding to trip point parameters (type, temperature and hysteresis) read the trip ID from the attribute name and then use the trip ID as the index in the given thermal zone's trips table to get to the trip object they want.
Instead of doing this, make them use the attribute pointer they get as the second argument to get to the trip object embedded in the same struct thermal_trip_desc as the struct device_attribute pointed to by it, which is much more straightforward and less overhead.
--- drivers/thermal/thermal_sysfs.c | 54 ++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 34 deletions(-)
drivers/thermal/thermal_sysfs.c +20 -34
@@ -12,6 +12,7 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/container_of.h> #include <linux/sysfs.h> #include <linux/device.h> #include <linux/err.h>
@@ -78,39 +79,38 @@ mode_store(struct device *dev, struct dereturn count; } +#define thermal_trip_of_attr (_ptr_, _attr_) \ + ({ \ + struct thermal_trip_desc *td; \ + \ + td = container_of(_ptr_, struct thermal_trip_desc, \ + trip_attrs._attr_.attr); \ + &td->trip; \ + }) +static ssize_t trip_point_type_show (struct device *dev,struct device_attribute *attr,char *buf) { -struct thermal_zone_device *tz =to_thermal_zone (dev); -int trip_id; - -if (sscanf (attr->attr.name,"trip_point_%d_type" , &trip_id) !=1 ) -return -EINVAL ; +struct thermal_trip *trip =thermal_trip_of_attr (attr, type); -return sprintf (buf,"%s\n" ,thermal_trip_type_name (tz->trips[trip_id].trip.type)); +return sprintf (buf,"%s\n" ,thermal_trip_type_name (trip->type)); }static ssize_t trip_point_temp_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t count) { +struct thermal_trip *trip =thermal_trip_of_attr (attr, temp);struct thermal_zone_device *tz =to_thermal_zone (dev); -struct thermal_trip *trip; -int trip_id, ret; -int temp; +int ret, temp; ret =kstrtoint (buf,10 , &temp);if (ret)return -EINVAL ; -if (sscanf (attr->attr.name,"trip_point_%d_temp" , &trip_id) !=1 ) -return -EINVAL ; -mutex_lock (&tz->lock); - trip = &tz->trips[trip_id].trip; -if (temp != trip->temperature) {if (tz->ops.set_trip_temp) { ret = tz->ops.set_trip_temp (tz, trip, temp);
@@ -133,35 +133,25 @@ static ssize_t trip_point_temp_show(struct device *dev, struct device_attribute *attr, char *buf) { - struct thermal_zone_device *tz = to_thermal_zone(dev); -int trip_id; + struct thermal_trip *trip = thermal_trip_of_attr(attr, temp); -if (sscanf (attr->attr.name,"trip_point_%d_temp" , &trip_id) !=1 ) -return -EINVAL ; - -return sprintf (buf,"%d\n" ,READ_ONCE (tz->trips[trip_id].trip.temperature)); +return sprintf (buf,"%d\n" ,READ_ONCE (trip->temperature)); }static ssize_t trip_point_hyst_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t count) { +struct thermal_trip *trip =thermal_trip_of_attr (attr, hyst);struct thermal_zone_device *tz =to_thermal_zone (dev); -struct thermal_trip *trip; -int trip_id, ret; -int hyst; +int ret, hyst; ret =kstrtoint (buf,10 , &hyst);if (ret || hyst <0 )return -EINVAL ; -if (sscanf (attr->attr.name,"trip_point_%d_hyst" , &trip_id) !=1 ) -return -EINVAL ; -mutex_lock (&tz->lock); - trip = &tz->trips[trip_id].trip; -if (hyst != trip->hysteresis) {WRITE_ONCE (trip->hysteresis, hyst);
@@ -177,13 +167,9 @@ static ssize_t trip_point_hyst_show(struct device *dev, struct device_attribute *attr, char *buf) { - struct thermal_zone_device *tz = to_thermal_zone(dev); -int trip_id; - -if (sscanf (attr->attr.name,"trip_point_%d_hyst" , &trip_id) !=1 ) -return -EINVAL ; + struct thermal_trip *trip = thermal_trip_of_attr(attr, hyst); -return sprintf (buf,"%d\n" ,READ_ONCE (tz->trips[trip_id].trip.hysteresis)); +return sprintf (buf,"%d\n" ,READ_ONCE (trip->hysteresis)); } static ssize_t
PATCH 3/3 thermal: sysfs: Refine the handling of trip hysteresis changes
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Change trip_point_hyst_store() and replace thermal_zone_trip_updated() with thermal_zone_set_trip_hyst() to follow the trip_point_temp_store() code pattern for more consistency.
No intentional functional impact.
--- drivers/thermal/thermal_core.h | 4 ++-- drivers/thermal/thermal_sysfs.c | 4 ++-- drivers/thermal/thermal_trip.c | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-)
drivers/thermal/thermal_core.h +2 -2
@@ -258,11 +258,11 @@ const char *thermal_trip_type_name(enumvoid thermal_zone_set_trips (struct thermal_zone_device *tz);int thermal_zone_trip_id (const struct thermal_zone_device *tz,const struct thermal_trip *trip); -void thermal_zone_trip_updated (struct thermal_zone_device *tz, -const struct thermal_trip *trip);int __thermal_zone_get_temp (struct thermal_zone_device *tz,int *temp);void thermal_zone_trip_down (struct thermal_zone_device *tz,const struct thermal_trip *trip); +void thermal_zone_set_trip_hyst (struct thermal_zone_device *tz, +struct thermal_trip *trip,int hyst);/* sysfs I/F */ int thermal_zone_create_device_groups (struct thermal_zone_device *tz);
drivers/thermal/thermal_sysfs.c +2 -2
@@ -153,9 +153,9 @@ trip_point_hyst_store(struct device *devmutex_lock (&tz->lock);if (hyst != trip->hysteresis) { -WRITE_ONCE (trip->hysteresis, hyst); +thermal_zone_set_trip_hyst (tz, trip, hyst); -thermal_zone_trip_updated (tz, trip); +__thermal_zone_device_update (tz,THERMAL_TRIP_CHANGED ); }mutex_unlock (&tz->lock);
drivers/thermal/thermal_trip.c +3 -3
@@ -118,11 +118,11 @@ int thermal_zone_trip_id(const struct threturn trip_to_trip_desc (trip) - tz->trips; } -void thermal_zone_trip_updated (struct thermal_zone_device *tz, -const struct thermal_trip *trip) +void thermal_zone_set_trip_hyst (struct thermal_zone_device *tz, +struct thermal_trip *trip,int hyst) { +WRITE_ONCE (trip->hysteresis, hyst);thermal_notify_tz_trip_change (tz, trip); -__thermal_zone_device_update (tz,THERMAL_TRIP_CHANGED ); } void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
Rafael J. Wysocki
maintainerHi Everyone,
Quoted 8 lines · Rafael J. Wysocki
Hi Everyone,
This series reworks the trip point sysfs interface to get to trips via the attribute argument of show/store instead of using the attribute name to get a trip ID and look up a trip using it.
It also adds a small cleanup (the last patch) for a good measure.
This material should not be controversial (which I also gather from the lack of responses) and it is a clear improvement IMV in terms of code flow simplifications and the reduction of its size, so I'm going to move it to my linux-next branch as 6.12-candidate.
Thanks!