cpufreq: Move the IS_ENABLED(CPU_THERMAL) macro in a stub
Subsystems (5)
PATCH v2 2/2 thermal/drivers/cpu_cooling: Unregister with the policy
In reply to Daniel LezcanoCurrently the function cpufreq_cooling_register() returns a cooling device pointer which is used back as a pointer to call the function cpufreq_cooling_unregister(). Even if it is correct, it would make sense to not leak the structure inside a cpufreq driver and keep the code thermal code self-encapsulate. Moreover, that forces to add an extra variable in each driver using this function.
Instead of passing the cooling device to unregister, pass the policy.
Because the cpufreq_cooling_unregister() function uses the policy to unregister itself. The only purpose of the cooling device pointer is to unregister the cpu cooling device.
As there is no more need of this pointer, remove it.
--- drivers/cpufreq/arm_big_little.c | 9 +++------ drivers/cpufreq/cpufreq.c | 10 ++++------ drivers/thermal/cpu_cooling.c | 18 ++++++++++-------- drivers/thermal/imx_thermal.c | 12 ++++++------ .../thermal/ti-soc-thermal/ti-thermal-common.c | 10 +++++----- include/linux/cpu_cooling.h | 6 +++--- include/linux/cpufreq.h | 3 --- 7 files changed, 31 insertions(+), 37 deletions(-)
drivers/cpufreq/arm_big_little.c +3 -6
@@ -56,7 +56,6 @@ static bool bL_switching_enabled;#define ACTUAL_FREQ (cluster, freq) ((cluster == A7_CLUSTER) ? freq << 1 : freq)#define VIRT_FREQ (cluster, freq) ((cluster == A7_CLUSTER) ? freq >> 1 : freq) -static struct thermal_cooling_device *cdev[MAX_CLUSTERS ];static const struct cpufreq_arm_bL_ops *arm_bL_ops;static struct clk *clk[MAX_CLUSTERS ];static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS +1 ];
@@ -501,10 +500,8 @@ static int bL_cpufreq_exit(struct cpufreq_policy *policy)struct device *cpu_dev;int cur_cluster =cpu_to_cluster (policy->cpu); -if (cur_cluster <MAX_CLUSTERS ) { -cpufreq_cooling_unregister (cdev[cur_cluster]); - cdev[cur_cluster] =NULL ; - } +if (cur_cluster <MAX_CLUSTERS ) +cpufreq_cooling_unregister (policy); cpu_dev =get_cpu_device (policy->cpu);if (!cpu_dev) {
@@ -527,7 +524,7 @@ static void bL_cpufreq_ready(struct cpufreq_policy *policy)if (cur_cluster >=MAX_CLUSTERS )return ; - cdev[cur_cluster] =of_cpufreq_cooling_register (policy); +of_cpufreq_cooling_register (policy); }static struct cpufreq_driver bL_cpufreq_driver = {
drivers/cpufreq/cpufreq.c +4 -6
@@ -1379,8 +1379,8 @@ static int cpufreq_online(unsigned int cpu) cpufreq_driver->ready (policy);if (cpufreq_thermal_control_enabled (cpufreq_driver)) - policy->cdev =of_cpufreq_cooling_register (policy); - +of_cpufreq_cooling_register (policy); +pr_debug ("initialization complete\n" );return 0 ;
@@ -1468,10 +1468,8 @@ static int cpufreq_offline(unsigned int cpu) goto unlock; } -if (cpufreq_thermal_control_enabled (cpufreq_driver)) { -cpufreq_cooling_unregister (policy->cdev); - policy->cdev =NULL ; - } +if (cpufreq_thermal_control_enabled (cpufreq_driver)) +cpufreq_cooling_unregister (policy);if (cpufreq_driver->stop_cpu) cpufreq_driver->stop_cpu (policy);
drivers/thermal/cpu_cooling.c +10 -8
@@ -78,6 +78,7 @@ struct cpufreq_cooling_device {struct cpufreq_policy *policy;struct list_head node;struct time_in_idle *idle_time; +struct thermal_cooling_device *cdev; };static DEFINE_IDA (cpufreq_ida);
@@ -606,6 +607,7 @@ __cpufreq_cooling_register(struct device_node *np, goto remove_ida; cpufreq_cdev->clipped_freq =get_state_freq (cpufreq_cdev,0 ); + cpufreq_cdev->cdev = cdev;mutex_lock (&cooling_list_lock);/* Register the notifier for first cpufreq cooling device */
@@ -699,18 +701,18 @@ EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);* * This interface function unregisters the "thermal-cpufreq-%x" cooling device. */ -void cpufreq_cooling_unregister (struct thermal_cooling_device *cdev) +void cpufreq_cooling_unregister (struct cpufreq_policy *policy) {struct cpufreq_cooling_device *cpufreq_cdev;bool last; -if (!cdev) -return ; - - cpufreq_cdev = cdev->devdata; -mutex_lock (&cooling_list_lock); -list_del (&cpufreq_cdev->node); +list_for_each_entry (cpufreq_cdev, &cpufreq_cdev_list, node) { +if (cpufreq_cdev->policy == policy) { +list_del (&cpufreq_cdev->node); +break ; + } + }/* Unregister the notifier for the last cpufreq cooling device */ last =list_empty (&cpufreq_cdev_list);mutex_unlock (&cooling_list_lock);
@@ -719,7 +721,7 @@ void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)cpufreq_unregister_notifier (&thermal_cpufreq_notifier_block,CPUFREQ_POLICY_NOTIFIER ); -thermal_cooling_device_unregister (cdev); +thermal_cooling_device_unregister (cpufreq_cdev->cdev);ida_simple_remove (&cpufreq_ida, cpufreq_cdev->id);kfree (cpufreq_cdev->idle_time);kfree (cpufreq_cdev);
drivers/thermal/imx_thermal.c +6 -6
@@ -203,7 +203,6 @@ static struct thermal_soc_data thermal_imx7d_data = {struct imx_thermal_data {struct cpufreq_policy *policy;struct thermal_zone_device *tz; -struct thermal_cooling_device *cdev;enum thermal_device_mode mode;struct regmap *tempmon;u32 c1, c2;/* See formula in imx_init_calib() */
@@ -656,6 +655,7 @@ MODULE_DEVICE_TABLE(of, of_imx_thermal_match);static int imx_thermal_register_legacy_cooling (struct imx_thermal_data *data) {struct device_node *np; +struct thermal_cooling_device *cdev;int ret; data->policy =cpufreq_cpu_get (0 );
@@ -667,9 +667,9 @@ static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data) np =of_get_cpu_node (data->policy->cpu,NULL );if (!np || !of_find_property (np,"#cooling-cells" ,NULL )) { - data->cdev =cpufreq_cooling_register (data->policy); -if (IS_ERR (data->cdev)) { - ret =PTR_ERR (data->cdev); + cdev =cpufreq_cooling_register (data->policy); +if (IS_ERR (cdev)) { + ret =PTR_ERR (cdev);cpufreq_cpu_put (data->policy);return ret; }
@@ -680,7 +680,7 @@ static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)static void imx_thermal_unregister_legacy_cooling (struct imx_thermal_data *data) { -cpufreq_cooling_unregister (data->cdev); +cpufreq_cooling_unregister (data->policy);cpufreq_cpu_put (data->policy); }
@@ -872,7 +872,7 @@ static int imx_thermal_remove(struct platform_device *pdev)clk_disable_unprepare (data->thermal_clk);thermal_zone_device_unregister (data->tz); -cpufreq_cooling_unregister (data->cdev); +cpufreq_cooling_unregister (data->policy);cpufreq_cpu_put (data->policy);return 0 ;
drivers/thermal/ti-soc-thermal/ti-thermal-common.c +5 -5
@@ -41,7 +41,6 @@ struct ti_thermal_data {struct cpufreq_policy *policy;struct thermal_zone_device *ti_thermal;struct thermal_zone_device *pcb_tz; -struct thermal_cooling_device *cool_dev;struct ti_bandgap *bgp;enum thermal_device_mode mode;struct work_struct thermal_wq;
@@ -233,6 +232,7 @@ int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id) {struct ti_thermal_data *data;struct device_node *np = bgp->dev->of_node; +struct thermal_cooling_device *cdev;/* * We are assuming here that if one deploys the zone
@@ -256,9 +256,9 @@ int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id) }/* Register cooling device */ - data->cool_dev =cpufreq_cooling_register (data->policy); -if (IS_ERR (data->cool_dev)) { -int ret =PTR_ERR (data->cool_dev); + cdev =cpufreq_cooling_register (data->policy); +if (IS_ERR (cdev)) { +int ret =PTR_ERR (cdev);dev_err (bgp->dev,"Failed to register cpu cooling device %d\n" , ret);cpufreq_cpu_put (data->policy);
@@ -277,7 +277,7 @@ int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id) data =ti_bandgap_get_sensor_data (bgp, id);if (data) { -cpufreq_cooling_unregister (data->cool_dev); +cpufreq_cooling_unregister (data->policy);if (data->policy)cpufreq_cpu_put (data->policy); }
include/linux/cpu_cooling.h +3 -3
@@ -29,9 +29,9 @@ cpufreq_cooling_register(struct cpufreq_policy *policy);/** * cpufreq_cooling_unregister - function to remove cpufreq cooling device. -* @cdev: thermal cooling device pointer. +* @policy: cpufreq policy */ -void cpufreq_cooling_unregister (struct thermal_cooling_device *cdev); +void cpufreq_cooling_unregister (struct cpufreq_policy *policy);#else /* !CONFIG_CPU_THERMAL */ static inline struct thermal_cooling_device *
@@ -41,7 +41,7 @@ cpufreq_cooling_register(struct cpufreq_policy *policy) }static inline -void cpufreq_cooling_unregister (struct thermal_cooling_device *cdev) +void cpufreq_cooling_unregister (struct cpufreq_policy *policy) {return ; }
include/linux/cpufreq.h +0 -3
@@ -144,9 +144,6 @@ struct cpufreq_policy {/* For cpufreq driver's internal use */ void *driver_data; - -/* Pointer to the cooling device if used for thermal mitigation */ -struct thermal_cooling_device *cdev; };struct cpufreq_freqs {
Signature · 1 line
2.17.1
PATCH v2 1/2 cpufreq: Move the IS_ENABLED(CPU_THERMAL) macro in a stub
The cpufreq_online and the cpufreq_offline [un]register the driver as a cooling device. This is done if the driver is flagged as a cooling device in addition with a IS_ENABLED macro to compile out the branching code.
Group this test in a stub function added in the cpufreq header instead of having the IS_ENABLED in the code path.
--- drivers/cpufreq/cpufreq.c | 6 ++---- include/linux/cpufreq.h | 5 +++++ 2 files changed, 7 insertions(+), 4 deletions(-)
drivers/cpufreq/cpufreq.c +2 -4
@@ -1378,8 +1378,7 @@ static int cpufreq_online(unsigned int cpu)if (cpufreq_driver->ready) cpufreq_driver->ready (policy); -if (IS_ENABLED (CONFIG_CPU_THERMAL ) && - cpufreq_driver->flags &CPUFREQ_IS_COOLING_DEV ) +if (cpufreq_thermal_control_enabled (cpufreq_driver)) policy->cdev =of_cpufreq_cooling_register (policy);pr_debug ("initialization complete\n" );
@@ -1469,8 +1468,7 @@ static int cpufreq_offline(unsigned int cpu) goto unlock; } -if (IS_ENABLED (CONFIG_CPU_THERMAL ) && - cpufreq_driver->flags &CPUFREQ_IS_COOLING_DEV ) { +if (cpufreq_thermal_control_enabled (cpufreq_driver)) {cpufreq_cooling_unregister (policy->cdev); policy->cdev =NULL ; }
include/linux/cpufreq.h +5 -0
@@ -409,6 +409,11 @@ int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);const char *cpufreq_get_current_driver (void );void *cpufreq_get_driver_data (void ); +static inline int cpufreq_thermal_control_enabled (struct cpufreq_driver *drv) +{ +return IS_ENABLED (CPU_THERMAL ) && (drv->flags &CPUFREQ_IS_COOLING_DEV ); +} +static inline void cpufreq_verify_within_limits (struct cpufreq_policy *policy,unsigned int min,unsigned int max) {
Signature · 1 line
2.17.1
Quoted 5 lines · Daniel Lezcano
The
cpufreq_onlineand thecpufreq_offline[un]register the driver as a cooling device. This is done if the driver is flagged as a cooling device in addition with aIS_ENABLEDmacro to compile out the branching code.
Quoted 8 lines
Group this test in a stub function added in the cpufreq header instead of having the
IS_ENABLEDin the code path.Signed-off-by Daniel Lezcano <[email protected]>--- drivers/cpufreq/cpufreq.c | 6 ++---- include/linux/cpufreq.h | 5 +++++ 2 files changed, 7 insertions(+), 4 deletions(-)
Signature · 1 line
viresh
Rafael J. Wysocki
maintainerRe: [PATCH v2 2/2] thermal/drivers/cpu_cooling: Unregister with the policy
In reply to Daniel Lezcano) On Mon, Jun 24, 2019 at 3:17 PM Daniel Lezcano
Quoted 7 lines · <[email protected]> wrote:
Currently the function
cpufreq_cooling_register()returns a cooling device pointer which is used back as a pointer to call the functioncpufreq_cooling_unregister(). Even if it is correct, it would make sense to not leak the structure inside a cpufreq driver and keep the code thermal code self-encapsulate. Moreover, that forces to add an extra variable in each driver using this function.
[cut]
drivers/thermal/imx_thermal.c static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data) · line 670
Quoted 8 lines
drivers/thermal/imx_thermal.c +1
-3@@ -667,9 +667,9 @@ static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data) np =of_get_cpu_node (data->policy->cpu,NULL );if (!np || !of_find_property (np,"#cooling-cells" ,NULL )) { - data->cdev =cpufreq_cooling_register (data->policy); -if (IS_ERR (data->cdev)) { - ret =PTR_ERR (data->cdev); + cdev =cpufreq_cooling_register (data->policy);
It looks like after the changes in this patch the only reason for returning (struct thermal_cooling_device *) from cpufreq_cooling_register() is error checking, but it would be much more straightforward to return int for this purpose.
Moreover, that would prevent the callers of it from doing incorrect things with the returned pointers (like using it to unregister the cooling device).
Quoted 5 lines
drivers/thermal/imx_thermal.c +2
-0+if (IS_ERR (cdev)) { + ret =PTR_ERR (cdev);cpufreq_cpu_put (data->policy);return ret; }