Skip to contentSkip to conversation

thermal: sysfs: Simplifications of trip point attribute callbacks

PATCH Superseded Rafael J. Wysocki 3 participants 9 messages Nov 30, 2023 – Dec 1, 2023 Thermal

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover thermal: sysfs: Simplifications of trip point attribute callbacks

Hi,

These patches simplify trip point attribute callbacks in two ways.

The first patch, which is a direct replacement for

https://patchwork.kernel.org/project/linux-pm/patch/4869676.GXAFRqVoOG@kreacher/

gets rid of redundant steps from the _store() callbacks for the trip point temperature and hysteresis and makes them use zone locking only as necessary.

The second patch eliminates zone locking from all of the _show() callbacks for trip point temperature, hysteresis and type, because it is not needed there.

Please refer to the individual patch changelogs for details.

Thanks!

#1 lore raw

PATCH 1/2 thermal: sysfs: Rework the handling of trip point updates

From: Rafael J. Wysocki <[email protected]>

Both trip_point_temp_store() and trip_point_hyst_store() use thermal_zone_set_trip() to update a given trip point, but none of them actually needs to change more than one field in struct thermal_trip representing it. However, each of them effectively calls __thermal_zone_get_trip() twice in a row for the same trip index value, once directly and once via thermal_zone_set_trip(), which is not particularly efficient, and the way in which thermal_zone_set_trip() carries out the update is not particularly straightforward.

Moreover, some checks done by them both need not go under the thermal zone lock.

Rework trip_point_temp_store() and trip_point_hyst_store() to address the above, move the part of thermal_zone_set_trip() that is still useful to a new function called thermal_zone_trip_updated() and drop the rest of it.

While at it, make trip_point_hyst_store() reject negative hysteresis values.

---
 drivers/thermal/thermal_core.h  |    2 +
 drivers/thermal/thermal_sysfs.c |   70 ++++++++++++++++++++++++++--------------
 drivers/thermal/thermal_trip.c  |   45 +++++--------------------
 include/linux/thermal.h         |    3 -
 4 files changed, 57 insertions(+), 63 deletions(-)
drivers/thermal/thermal_core.h +2 -0
Index: linux-pm/drivers/thermal/thermal_core.h
===================================================================
@@ -124,6 +124,8 @@ int __thermal_zone_get_trip(struct therm
 			    struct thermal_trip *trip);
 int thermal_zone_trip_id(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);
 
 /* sysfs I/F */
drivers/thermal/thermal_sysfs.c +46 -24

102 lines not on this page · raw message

drivers/thermal/thermal_trip.c +9 -36

54 lines not on this page · raw message

include/linux/thermal.h +0 -3
Index: linux-pm/include/linux/thermal.h
===================================================================
@@ -283,9 +283,6 @@ int __thermal_zone_get_trip(struct therm
 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
 			  struct thermal_trip *trip);
 
-int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id,
-			  const struct thermal_trip *trip);
-
 int for_each_thermal_trip(struct thermal_zone_device *tz,
 			  int (*cb)(struct thermal_trip *, void *),
 			  void *data);
#2 lore raw

PATCH 2/2 thermal: sysfs: Eliminate unnecessary zone locking

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The _show() callback functions of the trip point sysfs attributes, temperature, hysteresis and type, need not use thermal zone locking, because the layout of the data structures they access does not change after the thermal zone registration.

Namely, they all need to access a specific entry in the thermal zone's trips[] table that is always present for non-tripless thermal zones and its size cannot change after the thermal zone has been registered. Thus it is always safe to access the trips[] table of a registered thermal zone from each of the sysfs attributes in question.

Moreover, the type of a trip point does not change after registering its thermal zone, and while its temperature and hysteresis can change, for example due to a firmware-induced thermal zone update, holding the zone lock around reading them is pointless, because it does not prevent stale values from being returned to user space. For example, a trip point temperature can always change ater trip_point_temp_show() has read it and before the function's return statement is executed, regardless of whether or not zone locking is used.

For this reason, drop the zone locking from trip_point_type_show(), trip_point_temp_show(), and trip_point_hyst_show().

---
 drivers/thermal/thermal_sysfs.c |   60 ++++++++++++++--------------------------
 1 file changed, 21 insertions(+), 39 deletions(-)
drivers/thermal/thermal_sysfs.c +21 -39
Index: linux-pm/drivers/thermal/thermal_sysfs.c
===================================================================
@@ -83,25 +83,18 @@ trip_point_type_show(struct device *dev,
 		     char *buf)
 {
 	struct thermal_zone_device *tz = to_thermal_zone(dev);
-	struct thermal_trip trip;
-	int trip_id, result;
+	int trip_id;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
 
 	if (sscanf(attr->attr.name, "trip_point_%d_type", &trip_id) != 1)
 		return -EINVAL;
 
-	mutex_lock(&tz->lock);
-
-	if (device_is_registered(dev))
-		result = __thermal_zone_get_trip(tz, trip_id, &trip);
-	else
-		result = -ENODEV;
-
-	mutex_unlock(&tz->lock);
-
-	if (result)
-		return result;
+	if (trip_id < 0 || trip_id > tz->num_trips)
+		return -EINVAL;
 
-	switch (trip.type) {
+	switch (tz->trips[trip_id].type) {
 	case THERMAL_TRIP_CRITICAL:
 		return sprintf(buf, "critical\n");
 	case THERMAL_TRIP_HOT:
@@ -164,25 +157,18 @@ trip_point_temp_show(struct device *dev,
 		     char *buf)
 {
 	struct thermal_zone_device *tz = to_thermal_zone(dev);
-	struct thermal_trip trip;
-	int trip_id, ret;
+	int trip_id;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
 
 	if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
 		return -EINVAL;
 
-	mutex_lock(&tz->lock);
-
-	if (device_is_registered(dev))
-		ret = __thermal_zone_get_trip(tz, trip_id, &trip);
-	else
-		ret = -ENODEV;
-
-	mutex_unlock(&tz->lock);
-
-	if (ret)
-		return ret;
+	if (trip_id < 0 || trip_id > tz->num_trips)
+		return -EINVAL;
 
-	return sprintf(buf, "%d\n", trip.temperature);
+	return sprintf(buf, "%d\n", tz->trips[trip_id].temperature);
 }
 
 static ssize_t
@@ -234,22 +220,18 @@ trip_point_hyst_show(struct device *dev,
 		     char *buf)
 {
 	struct thermal_zone_device *tz = to_thermal_zone(dev);
-	struct thermal_trip trip;
-	int trip_id, ret;
+	int trip_id;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
 
 	if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
 		return -EINVAL;
 
-	mutex_lock(&tz->lock);
-
-	if (device_is_registered(dev))
-		ret = __thermal_zone_get_trip(tz, trip_id, &trip);
-	else
-		ret = -ENODEV;
-
-	mutex_unlock(&tz->lock);
+	if (trip_id < 0 || trip_id > tz->num_trips)
+		return -EINVAL;
 
-	return ret ? ret : sprintf(buf, "%d\n", trip.hysteresis);
+	return sprintf(buf, "%d\n", tz->trips[trip_id].hysteresis);
 }
 
 static ssize_t
#3 lore raw

Re: [PATCH v1 2/2] thermal: sysfs: Eliminate unnecessary zone locking

Quoted 51 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The _show() callback functions of the trip point sysfs attributes, temperature, hysteresis and type, need not use thermal zone locking, because the layout of the data structures they access does not change after the thermal zone registration.

Namely, they all need to access a specific entry in the thermal zone's trips[] table that is always present for non-tripless thermal zones and its size cannot change after the thermal zone has been registered. Thus it is always safe to access the trips[] table of a registered thermal zone from each of the sysfs attributes in question.

Moreover, the type of a trip point does not change after registering its thermal zone, and while its temperature and hysteresis can change, for example due to a firmware-induced thermal zone update, holding the zone lock around reading them is pointless, because it does not prevent stale values from being returned to user space. For example, a trip point temperature can always change ater trip_point_temp_show() has read it and before the function's return statement is executed, regardless of whether or not zone locking is used.

For this reason, drop the zone locking from trip_point_type_show(), trip_point_temp_show(), and trip_point_hyst_show().

---
 drivers/thermal/thermal_sysfs.c |   60 ++++++++++++++--------------------------
 1 file changed, 21 insertions(+), 39 deletions(-)

Index: linux-pm/drivers/thermal/thermal_sysfs.c
===================================================================

drivers/thermal/thermal_sysfs.c +4 -4
@@ -83,25 +83,18 @@ trip_point_type_show(struct device *dev,
                     char *buf)
 {
        struct thermal_zone_device *tz = to_thermal_zone(dev);
-       struct thermal_trip trip;
-       int trip_id, result;
+       int trip_id;
+
+       if (!device_is_registered(dev))
+               return -ENODEV;
 
        if (sscanf(attr->attr.name, "trip_point_%d_type", &trip_id) != 1)
                return -EINVAL;
 
-       mutex_lock(&tz->lock);
-

drivers/thermal/thermal_sysfs.c trip_point_type_show(struct device *dev, · line 94

Quoted 10 lines
drivers/thermal/thermal_sysfs.c +1 -9
-       if (device_is_registered(dev))
-               result = __thermal_zone_get_trip(tz, trip_id, &trip);
-       else
-               result = -ENODEV;
-
-       mutex_unlock(&tz->lock);
-
-       if (result)
-               return result;
+       if (trip_id < 0 || trip_id > tz->num_trips)

An off-by-one here, it should be trip_id >= tz->num_trips (and analogously below).

I'll send an update of this shortly.

Quoted 74 lines
drivers/thermal/thermal_sysfs.c +16 -26
+               return -EINVAL;
 
-       switch (trip.type) {
+       switch (tz->trips[trip_id].type) {
        case THERMAL_TRIP_CRITICAL:
                return sprintf(buf, "critical\n");
        case THERMAL_TRIP_HOT:
@@ -164,25 +157,18 @@ trip_point_temp_show(struct device *dev,
                     char *buf)
 {
        struct thermal_zone_device *tz = to_thermal_zone(dev);
-       struct thermal_trip trip;
-       int trip_id, ret;
+       int trip_id;
+
+       if (!device_is_registered(dev))
+               return -ENODEV;
 
        if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
                return -EINVAL;
 
-       mutex_lock(&tz->lock);
-
-       if (device_is_registered(dev))
-               ret = __thermal_zone_get_trip(tz, trip_id, &trip);
-       else
-               ret = -ENODEV;
-
-       mutex_unlock(&tz->lock);
-
-       if (ret)
-               return ret;
+       if (trip_id < 0 || trip_id > tz->num_trips)
+               return -EINVAL;
 
-       return sprintf(buf, "%d\n", trip.temperature);
+       return sprintf(buf, "%d\n", tz->trips[trip_id].temperature);
 }
 
 static ssize_t
@@ -234,22 +220,18 @@ trip_point_hyst_show(struct device *dev,
                     char *buf)
 {
        struct thermal_zone_device *tz = to_thermal_zone(dev);
-       struct thermal_trip trip;
-       int trip_id, ret;
+       int trip_id;
+
+       if (!device_is_registered(dev))
+               return -ENODEV;
 
        if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
                return -EINVAL;
 
-       mutex_lock(&tz->lock);
-
-       if (device_is_registered(dev))
-               ret = __thermal_zone_get_trip(tz, trip_id, &trip);
-       else
-               ret = -ENODEV;
-
-       mutex_unlock(&tz->lock);
+       if (trip_id < 0 || trip_id > tz->num_trips)
+               return -EINVAL;
 
-       return ret ? ret : sprintf(buf, "%d\n", trip.hysteresis);
+       return sprintf(buf, "%d\n", tz->trips[trip_id].hysteresis);
 }
 
 static ssize_t
#4 lore raw

Re: [PATCH v1 2/2] thermal: sysfs: Eliminate unnecessary zone locking

In reply to Rafael J. Wysocki
Quoted 22 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The _show() callback functions of the trip point sysfs attributes, temperature, hysteresis and type, need not use thermal zone locking, because the layout of the data structures they access does not change after the thermal zone registration.

Namely, they all need to access a specific entry in the thermal zone's trips[] table that is always present for non-tripless thermal zones and its size cannot change after the thermal zone has been registered. Thus it is always safe to access the trips[] table of a registered thermal zone from each of the sysfs attributes in question.

Moreover, the type of a trip point does not change after registering its thermal zone, and while its temperature and hysteresis can change, for example due to a firmware-induced thermal zone update, holding the zone lock around reading them is pointless, because it does not prevent stale values from being returned to user space. For example, a trip point temperature can always change ater trip_point_temp_show() has read it and before the function's return statement is executed, regardless of whether or not zone locking is used.

Quoted 2 lines

For this reason, drop the zone locking from trip_point_type_show(), trip_point_temp_show(), and trip_point_hyst_show().

Isn't the lock used to protect against device_del() from thermal_zone_device_unregister() ?

Quoted 109 lines
---
  drivers/thermal/thermal_sysfs.c |   60 ++++++++++++++--------------------------
  1 file changed, 21 insertions(+), 39 deletions(-)

Index: linux-pm/drivers/thermal/thermal_sysfs.c
===================================================================

drivers/thermal/thermal_sysfs.c +21 -39
@@ -83,25 +83,18 @@ trip_point_type_show(struct device *dev,
  		     char *buf)
  {
  	struct thermal_zone_device *tz = to_thermal_zone(dev);
-	struct thermal_trip trip;
-	int trip_id, result;
+	int trip_id;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
 
  	if (sscanf(attr->attr.name, "trip_point_%d_type", &trip_id) != 1)
  		return -EINVAL;
 
-	mutex_lock(&tz->lock);
-
-	if (device_is_registered(dev))
-		result = __thermal_zone_get_trip(tz, trip_id, &trip);
-	else
-		result = -ENODEV;
-
-	mutex_unlock(&tz->lock);
-
-	if (result)
-		return result;
+	if (trip_id < 0 || trip_id > tz->num_trips)
+		return -EINVAL;
 
-	switch (trip.type) {
+	switch (tz->trips[trip_id].type) {
  	case THERMAL_TRIP_CRITICAL:
  		return sprintf(buf, "critical\n");
  	case THERMAL_TRIP_HOT:
@@ -164,25 +157,18 @@ trip_point_temp_show(struct device *dev,
  		     char *buf)
  {
  	struct thermal_zone_device *tz = to_thermal_zone(dev);
-	struct thermal_trip trip;
-	int trip_id, ret;
+	int trip_id;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
 
  	if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
  		return -EINVAL;
 
-	mutex_lock(&tz->lock);
-
-	if (device_is_registered(dev))
-		ret = __thermal_zone_get_trip(tz, trip_id, &trip);
-	else
-		ret = -ENODEV;
-
-	mutex_unlock(&tz->lock);
-
-	if (ret)
-		return ret;
+	if (trip_id < 0 || trip_id > tz->num_trips)
+		return -EINVAL;
 
-	return sprintf(buf, "%d\n", trip.temperature);
+	return sprintf(buf, "%d\n", tz->trips[trip_id].temperature);
  }
 
  static ssize_t
@@ -234,22 +220,18 @@ trip_point_hyst_show(struct device *dev,
  		     char *buf)
  {
  	struct thermal_zone_device *tz = to_thermal_zone(dev);
-	struct thermal_trip trip;
-	int trip_id, ret;
+	int trip_id;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
 
  	if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
  		return -EINVAL;
 
-	mutex_lock(&tz->lock);
-
-	if (device_is_registered(dev))
-		ret = __thermal_zone_get_trip(tz, trip_id, &trip);
-	else
-		ret = -ENODEV;
-
-	mutex_unlock(&tz->lock);
+	if (trip_id < 0 || trip_id > tz->num_trips)
+		return -EINVAL;
 
-	return ret ? ret : sprintf(buf, "%d\n", trip.hysteresis);
+	return sprintf(buf, "%d\n", tz->trips[trip_id].hysteresis);
  }
 
  static ssize_t
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#5 lore raw

Re: [PATCH v1 2/2] thermal: sysfs: Eliminate unnecessary zone locking

Quoted 24 lines · Daniel Lezcano
Quoted 22 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The _show() callback functions of the trip point sysfs attributes, temperature, hysteresis and type, need not use thermal zone locking, because the layout of the data structures they access does not change after the thermal zone registration.

Namely, they all need to access a specific entry in the thermal zone's trips[] table that is always present for non-tripless thermal zones and its size cannot change after the thermal zone has been registered. Thus it is always safe to access the trips[] table of a registered thermal zone from each of the sysfs attributes in question.

Moreover, the type of a trip point does not change after registering its thermal zone, and while its temperature and hysteresis can change, for example due to a firmware-induced thermal zone update, holding the zone lock around reading them is pointless, because it does not prevent stale values from being returned to user space. For example, a trip point temperature can always change ater trip_point_temp_show() has read it and before the function's return statement is executed, regardless of whether or not zone locking is used.

Quoted 5 lines
Quoted 2 lines

For this reason, drop the zone locking from trip_point_type_show(), trip_point_temp_show(), and trip_point_hyst_show().

Isn't the lock used to protect against device_del() from thermal_zone_device_unregister() ?

Ah, I missed the zone locking around device_del() in there.

OK, please scratch this series for now.

#6 lore raw

PATCH 2/2 thermal: sysfs: Simplifications of trip point attribute callbacks

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The _show() callback functions of the trip point sysfs attributes, temperature, hysteresis and type, need not use thermal zone locking, because the layout of the data structures they access does not change after the thermal zone registration.

Namely, they all need to access a specific entry in the thermal zone's trips[] table that is always present for non-tripless thermal zones and its size cannot change after the thermal zone has been registered. Thus it is always safe to access the trips[] table of a registered thermal zone from each of the sysfs attributes in question.

Moreover, the type of a trip point does not change after registering its thermal zone, and while its temperature and hysteresis can change, for example due to a firmware-induced thermal zone update, holding the zone lock around reading them is pointless, because it does not prevent stale values from being returned to user space. For example, a trip point temperature can always change ater trip_point_temp_show() has read it and before the function's return statement is executed, regardless of whether or not zone locking is used.

For this reason, drop the zone locking from trip_point_type_show(), trip_point_temp_show(), and trip_point_hyst_show().

---

v1 -> v1.1: Use >= instead of > (which was incorrect) in 3 places.

---
 drivers/thermal/thermal_sysfs.c |   60 ++++++++++++++--------------------------
 1 file changed, 21 insertions(+), 39 deletions(-)
drivers/thermal/thermal_sysfs.c +21 -39
Index: linux-pm/drivers/thermal/thermal_sysfs.c
===================================================================
@@ -83,25 +83,18 @@ trip_point_type_show(struct device *dev,
 		     char *buf)
 {
 	struct thermal_zone_device *tz = to_thermal_zone(dev);
-	struct thermal_trip trip;
-	int trip_id, result;
+	int trip_id;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
 
 	if (sscanf(attr->attr.name, "trip_point_%d_type", &trip_id) != 1)
 		return -EINVAL;
 
-	mutex_lock(&tz->lock);
-
-	if (device_is_registered(dev))
-		result = __thermal_zone_get_trip(tz, trip_id, &trip);
-	else
-		result = -ENODEV;
-
-	mutex_unlock(&tz->lock);
-
-	if (result)
-		return result;
+	if (trip_id < 0 || trip_id >= tz->num_trips)
+		return -EINVAL;
 
-	switch (trip.type) {
+	switch (tz->trips[trip_id].type) {
 	case THERMAL_TRIP_CRITICAL:
 		return sprintf(buf, "critical\n");
 	case THERMAL_TRIP_HOT:
@@ -164,25 +157,18 @@ trip_point_temp_show(struct device *dev,
 		     char *buf)
 {
 	struct thermal_zone_device *tz = to_thermal_zone(dev);
-	struct thermal_trip trip;
-	int trip_id, ret;
+	int trip_id;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
 
 	if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
 		return -EINVAL;
 
-	mutex_lock(&tz->lock);
-
-	if (device_is_registered(dev))
-		ret = __thermal_zone_get_trip(tz, trip_id, &trip);
-	else
-		ret = -ENODEV;
-
-	mutex_unlock(&tz->lock);
-
-	if (ret)
-		return ret;
+	if (trip_id < 0 || trip_id >= tz->num_trips)
+		return -EINVAL;
 
-	return sprintf(buf, "%d\n", trip.temperature);
+	return sprintf(buf, "%d\n", tz->trips[trip_id].temperature);
 }
 
 static ssize_t
@@ -234,22 +220,18 @@ trip_point_hyst_show(struct device *dev,
 		     char *buf)
 {
 	struct thermal_zone_device *tz = to_thermal_zone(dev);
-	struct thermal_trip trip;
-	int trip_id, ret;
+	int trip_id;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
 
 	if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
 		return -EINVAL;
 
-	mutex_lock(&tz->lock);
-
-	if (device_is_registered(dev))
-		ret = __thermal_zone_get_trip(tz, trip_id, &trip);
-	else
-		ret = -ENODEV;
-
-	mutex_unlock(&tz->lock);
+	if (trip_id < 0 || trip_id >= tz->num_trips)
+		return -EINVAL;
 
-	return ret ? ret : sprintf(buf, "%d\n", trip.hysteresis);
+	return sprintf(buf, "%d\n", tz->trips[trip_id].hysteresis);
 }
 
 static ssize_t
#7 lore raw

Wrong subject, sorry for the noise. Will resend.

Quoted 25 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The _show() callback functions of the trip point sysfs attributes, temperature, hysteresis and type, need not use thermal zone locking, because the layout of the data structures they access does not change after the thermal zone registration.

Namely, they all need to access a specific entry in the thermal zone's trips[] table that is always present for non-tripless thermal zones and its size cannot change after the thermal zone has been registered. Thus it is always safe to access the trips[] table of a registered thermal zone from each of the sysfs attributes in question.

Moreover, the type of a trip point does not change after registering its thermal zone, and while its temperature and hysteresis can change, for example due to a firmware-induced thermal zone update, holding the zone lock around reading them is pointless, because it does not prevent stale values from being returned to user space. For example, a trip point temperature can always change ater trip_point_temp_show() has read it and before the function's return statement is executed, regardless of whether or not zone locking is used.

For this reason, drop the zone locking from trip_point_type_show(), trip_point_temp_show(), and trip_point_hyst_show().

#8 lore raw

PATCH 2/2 thermal: sysfs: Eliminate unnecessary zone locking

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The _show() callback functions of the trip point sysfs attributes, temperature, hysteresis and type, need not use thermal zone locking, because the layout of the data structures they access does not change after the thermal zone registration.

Namely, they all need to access a specific entry in the thermal zone's trips[] table that is always present for non-tripless thermal zones and its size cannot change after the thermal zone has been registered. Thus it is always safe to access the trips[] table of a registered thermal zone from each of the sysfs attributes in question.

Moreover, the type of a trip point does not change after registering its thermal zone, and while its temperature and hysteresis can change, for example due to a firmware-induced thermal zone update, holding the zone lock around reading them is pointless, because it does not prevent stale values from being returned to user space. For example, a trip point temperature can always change ater trip_point_temp_show() has read it and before the function's return statement is executed, regardless of whether or not zone locking is used.

For this reason, drop the zone locking from trip_point_type_show(), trip_point_temp_show(), and trip_point_hyst_show().

---

v1 -> v1.1: Use >= instead of > (which was incorrect) in 3 places.

---
 drivers/thermal/thermal_sysfs.c |   60 ++++++++++++++--------------------------
 1 file changed, 21 insertions(+), 39 deletions(-)
drivers/thermal/thermal_sysfs.c +21 -39
Index: linux-pm/drivers/thermal/thermal_sysfs.c
===================================================================
@@ -83,25 +83,18 @@ trip_point_type_show(struct device *dev,
 		     char *buf)
 {
 	struct thermal_zone_device *tz = to_thermal_zone(dev);
-	struct thermal_trip trip;
-	int trip_id, result;
+	int trip_id;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
 
 	if (sscanf(attr->attr.name, "trip_point_%d_type", &trip_id) != 1)
 		return -EINVAL;
 
-	mutex_lock(&tz->lock);
-
-	if (device_is_registered(dev))
-		result = __thermal_zone_get_trip(tz, trip_id, &trip);
-	else
-		result = -ENODEV;
-
-	mutex_unlock(&tz->lock);
-
-	if (result)
-		return result;
+	if (trip_id < 0 || trip_id >= tz->num_trips)
+		return -EINVAL;
 
-	switch (trip.type) {
+	switch (tz->trips[trip_id].type) {
 	case THERMAL_TRIP_CRITICAL:
 		return sprintf(buf, "critical\n");
 	case THERMAL_TRIP_HOT:
@@ -164,25 +157,18 @@ trip_point_temp_show(struct device *dev,
 		     char *buf)
 {
 	struct thermal_zone_device *tz = to_thermal_zone(dev);
-	struct thermal_trip trip;
-	int trip_id, ret;
+	int trip_id;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
 
 	if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
 		return -EINVAL;
 
-	mutex_lock(&tz->lock);
-
-	if (device_is_registered(dev))
-		ret = __thermal_zone_get_trip(tz, trip_id, &trip);
-	else
-		ret = -ENODEV;
-
-	mutex_unlock(&tz->lock);
-
-	if (ret)
-		return ret;
+	if (trip_id < 0 || trip_id >= tz->num_trips)
+		return -EINVAL;
 
-	return sprintf(buf, "%d\n", trip.temperature);
+	return sprintf(buf, "%d\n", tz->trips[trip_id].temperature);
 }
 
 static ssize_t
@@ -234,22 +220,18 @@ trip_point_hyst_show(struct device *dev,
 		     char *buf)
 {
 	struct thermal_zone_device *tz = to_thermal_zone(dev);
-	struct thermal_trip trip;
-	int trip_id, ret;
+	int trip_id;
+
+	if (!device_is_registered(dev))
+		return -ENODEV;
 
 	if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
 		return -EINVAL;
 
-	mutex_lock(&tz->lock);
-
-	if (device_is_registered(dev))
-		ret = __thermal_zone_get_trip(tz, trip_id, &trip);
-	else
-		ret = -ENODEV;
-
-	mutex_unlock(&tz->lock);
+	if (trip_id < 0 || trip_id >= tz->num_trips)
+		return -EINVAL;
 
-	return ret ? ret : sprintf(buf, "%d\n", trip.hysteresis);
+	return sprintf(buf, "%d\n", tz->trips[trip_id].hysteresis);
 }
 
 static ssize_t