[PATCH 1/3] thermal: cpufreq_cooling: Use a copy of local ops for each cooling device

From: Lukasz Luba
Date: Fri Jun 10 2022 - 06:04:03 EST


It is very unlikely that one CPU cluster would have the EM and some other
won't have it (because EM registration failed or DT lacks needed entry).
Although, we should avoid modifying global variable with callbacks anyway.
Redesign this and add safety for such situation.

Signed-off-by: Lukasz Luba <lukasz.luba@xxxxxxx>
---
drivers/thermal/cpufreq_cooling.c | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/drivers/thermal/cpufreq_cooling.c b/drivers/thermal/cpufreq_cooling.c
index b8151d95a806..e33183785fac 100644
--- a/drivers/thermal/cpufreq_cooling.c
+++ b/drivers/thermal/cpufreq_cooling.c
@@ -554,7 +554,12 @@ __cpufreq_cooling_register(struct device_node *np,
/* max_level is an index, not a counter */
cpufreq_cdev->max_level = i - 1;

- cooling_ops = &cpufreq_cooling_ops;
+ cooling_ops = kmemdup(&cpufreq_cooling_ops, sizeof(*cooling_ops),
+ GFP_KERNEL);
+ if (!cooling_ops) {
+ cdev = ERR_PTR(-ENOMEM);
+ goto free_idle_time;
+ }

#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
if (em_is_sane(cpufreq_cdev, em)) {
@@ -568,7 +573,7 @@ __cpufreq_cooling_register(struct device_node *np,
pr_err("%s: unsorted frequency tables are not supported\n",
__func__);
cdev = ERR_PTR(-EINVAL);
- goto free_idle_time;
+ goto free_cooling_ops;
}

ret = freq_qos_add_request(&policy->constraints,
@@ -578,7 +583,7 @@ __cpufreq_cooling_register(struct device_node *np,
pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
ret);
cdev = ERR_PTR(ret);
- goto free_idle_time;
+ goto free_cooling_ops;
}

cdev = ERR_PTR(-ENOMEM);
@@ -597,6 +602,8 @@ __cpufreq_cooling_register(struct device_node *np,

remove_qos_req:
freq_qos_remove_request(&cpufreq_cdev->qos_req);
+free_cooling_ops:
+ kfree(cooling_ops);
free_idle_time:
free_idle_time(cpufreq_cdev);
free_cdev:
@@ -677,16 +684,19 @@ EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
*/
void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
{
+ const struct thermal_cooling_device_ops *ops;
struct cpufreq_cooling_device *cpufreq_cdev;

if (!cdev)
return;

cpufreq_cdev = cdev->devdata;
+ ops = cdev->ops;

thermal_cooling_device_unregister(cdev);
freq_qos_remove_request(&cpufreq_cdev->qos_req);
free_idle_time(cpufreq_cdev);
kfree(cpufreq_cdev);
+ kfree(ops);
}
EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);
--
2.17.1