Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

thermal/core: Create a helper __thermal_cdev_update() without a lock

There is a need to have a helper function which updates cooling device
state from the governors code. With this change governor can use
lock and unlock while calling helper function. This avoid unnecessary
second time lock/unlock which was in previous solution present in
governor implementation. This new helper function must be called
with mutex 'cdev->lock' hold.

The changed been discussed and part of code presented in thread:
https://lore.kernel.org/linux-pm/20210419084536.25000-1-lukasz.luba@arm.com/

Co-developed-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Signed-off-by: Lukasz Luba <lukasz.luba@arm.com>
Link: https://lore.kernel.org/r/20210422114308.29684-2-lukasz.luba@arm.com

authored by

Lukasz Luba and committed by
Daniel Lezcano
b70dbf40 26b2f03d

+18 -10
+1
drivers/thermal/thermal_core.h
··· 66 66 } 67 67 68 68 void thermal_cdev_update(struct thermal_cooling_device *); 69 + void __thermal_cdev_update(struct thermal_cooling_device *cdev); 69 70 70 71 /** 71 72 * struct thermal_trip - representation of a point in temperature domain
+17 -10
drivers/thermal/thermal_helpers.c
··· 192 192 thermal_cooling_device_stats_update(cdev, target); 193 193 } 194 194 195 - void thermal_cdev_update(struct thermal_cooling_device *cdev) 195 + void __thermal_cdev_update(struct thermal_cooling_device *cdev) 196 196 { 197 197 struct thermal_instance *instance; 198 198 unsigned long target = 0; 199 - 200 - mutex_lock(&cdev->lock); 201 - /* cooling device is updated*/ 202 - if (cdev->updated) { 203 - mutex_unlock(&cdev->lock); 204 - return; 205 - } 206 199 207 200 /* Make sure cdev enters the deepest cooling state */ 208 201 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) { ··· 209 216 210 217 thermal_cdev_set_cur_state(cdev, target); 211 218 212 - cdev->updated = true; 213 - mutex_unlock(&cdev->lock); 214 219 trace_cdev_update(cdev, target); 215 220 dev_dbg(&cdev->device, "set to state %lu\n", target); 221 + } 222 + 223 + /** 224 + * thermal_cdev_update - update cooling device state if needed 225 + * @cdev: pointer to struct thermal_cooling_device 226 + * 227 + * Update the cooling device state if there is a need. 228 + */ 229 + void thermal_cdev_update(struct thermal_cooling_device *cdev) 230 + { 231 + mutex_lock(&cdev->lock); 232 + if (!cdev->updated) { 233 + __thermal_cdev_update(cdev); 234 + cdev->updated = true; 235 + } 236 + mutex_unlock(&cdev->lock); 216 237 } 217 238 EXPORT_SYMBOL(thermal_cdev_update); 218 239