Skip to content

Commit ed97d10

Browse files
groeckrafaeljw
authored andcommitted
thermal/core: Move parameter validation from __thermal_zone_get_temp to thermal_zone_get_temp
All callers of __thermal_zone_get_temp() already validated the thermal zone parameters. Move validation to thermal_zone_get_temp() where it is actually needed. Also add kernel documentation for __thermal_zone_get_temp(), listing the requirement that the function must be called with validated parameters and with thermal device mutex held. Signed-off-by: Guenter Roeck <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 1c6b300 commit ed97d10

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

drivers/thermal/thermal_helpers.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ get_thermal_instance(struct thermal_zone_device *tz,
6464
}
6565
EXPORT_SYMBOL(get_thermal_instance);
6666

67+
/**
68+
* __thermal_zone_get_temp() - returns the temperature of a thermal zone
69+
* @tz: a valid pointer to a struct thermal_zone_device
70+
* @temp: a valid pointer to where to store the resulting temperature.
71+
*
72+
* When a valid thermal zone reference is passed, it will fetch its
73+
* temperature and fill @temp.
74+
*
75+
* Both tz and tz->ops must be valid pointers when calling this function,
76+
* and the tz->ops->get_temp callback must be provided.
77+
* The function must be called under tz->lock.
78+
*
79+
* Return: On success returns 0, an error code otherwise
80+
*/
6781
int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
6882
{
6983
int ret = -EINVAL;
@@ -73,9 +87,6 @@ int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
7387

7488
lockdep_assert_held(&tz->lock);
7589

76-
if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
77-
return -EINVAL;
78-
7990
ret = tz->ops->get_temp(tz, temp);
8091

8192
if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
@@ -114,13 +125,22 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
114125
{
115126
int ret;
116127

128+
if (IS_ERR_OR_NULL(tz))
129+
return -EINVAL;
130+
117131
mutex_lock(&tz->lock);
118132

133+
if (!tz->ops->get_temp) {
134+
ret = -EINVAL;
135+
goto unlock;
136+
}
137+
119138
if (device_is_registered(&tz->device))
120139
ret = __thermal_zone_get_temp(tz, temp);
121140
else
122141
ret = -ENODEV;
123142

143+
unlock:
124144
mutex_unlock(&tz->lock);
125145

126146
return ret;

0 commit comments

Comments
 (0)