Convert thermal subsystem to the IDA allocator
cover Convert thermal subsystem to the IDA allocator
From: Matthew Wilcox <[email protected]>
None of the ID allocations are in a hot path, so I think it's better to use the ida_simple_get/ida_simple_remove API instead of having individual mutexes.
I've only compile-tested these changes ... even that is quite tricky given the maze of Kconfig options which make it hard to select those drivers. I eventually gave up and played games with the header files to get the compiler to compile them cleanly.
Matthew Wilcox (4):
thermal core: convert ID allocation to IDA
thermal: convert clock cooling to use an IDA
thermal: convert cpu_cooling to use an IDA
thermal: convert devfreq_cooling to use an IDA
drivers/thermal/clock_cooling.c | 50 +++++--------------------- drivers/thermal/cpu_cooling.c | 63 ++++++-------------------------- drivers/thermal/devfreq_cooling.c | 53 +++++---------------------- drivers/thermal/thermal_core.c | 75 ++++++++++++++------------------------- include/linux/thermal.h | 4 +-- 5 files changed, 56 insertions(+), 189 deletions(-)
Signature · 1 line
2.11.0
PATCH 1/4 thermal core: convert ID allocation to IDA
From: Matthew Wilcox <[email protected]>
The thermal core does not use the ability to look up pointers by ID, so convert it from using an IDR to the more space-efficient IDA.
--- drivers/thermal/thermal_core.c | 75 +++++++++++++++--------------------------- include/linux/thermal.h | 4 +-- 2 files changed, 28 insertions(+), 51 deletions(-)
drivers/thermal/thermal_core.c +26 -49
@@ -36,9 +36,8 @@ MODULE_AUTHOR("Zhang Rui");MODULE_DESCRIPTION ("Generic thermal management sysfs support" );MODULE_LICENSE ("GPL v2" ); -static DEFINE_IDR (thermal_tz_idr); -static DEFINE_IDR (thermal_cdev_idr); -static DEFINE_MUTEX (thermal_idr_lock); +static DEFINE_IDA (thermal_tz_ida); +static DEFINE_IDA (thermal_cdev_ida);static LIST_HEAD (thermal_tz_list);static LIST_HEAD (thermal_cdev_list);
@@ -589,29 +588,6 @@ void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,* - thermal zone devices lifecycle: registration, unregistration, * binding, and unbinding. */ -static int get_idr (struct idr *idr,struct mutex *lock,int *id) -{ -int ret; - -if (lock) -mutex_lock (lock); - ret =idr_alloc (idr,NULL ,0 ,0 ,GFP_KERNEL ); -if (lock) -mutex_unlock (lock); -if (unlikely (ret <0 )) -return ret; - *id = ret; -return 0 ; -} - -static void release_idr (struct idr *idr,struct mutex *lock,int id) -{ -if (lock) -mutex_lock (lock); -idr_remove (idr, id); -if (lock) -mutex_unlock (lock); -}/** * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
@@ -685,15 +661,16 @@ int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, dev->target =THERMAL_NO_TARGET ; dev->weight = weight; - result =get_idr (&tz->idr, &tz->lock, &dev->id); -if (result) + result =ida_simple_get (&tz->ida,0 ,0 ,GFP_KERNEL ); +if (result <0 ) goto free_mem; + dev->id = result;sprintf (dev->name,"cdev%d" , dev->id); result =sysfs_create_link (&tz->device.kobj, &cdev->device.kobj, dev->name);if (result) - goto release_idr; + goto release_ida;sprintf (dev->attr_name,"cdev%d_trip_point" , dev->id);sysfs_attr_init (&dev->attr.attr);
@@ -737,8 +714,8 @@ int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,device_remove_file (&tz->device, &dev->attr); remove_symbol_link:sysfs_remove_link (&tz->device.kobj, dev->name); -release_idr: -release_idr (&tz->idr, &tz->lock, dev->id); +release_ida: +ida_simple_remove (&tz->ida, dev->id); free_mem:kfree (dev);return result;
@@ -785,7 +762,7 @@ int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,device_remove_file (&tz->device, &pos->weight_attr);device_remove_file (&tz->device, &pos->attr);sysfs_remove_link (&tz->device.kobj, pos->name); -release_idr (&tz->idr, &tz->lock, pos->id); +ida_simple_remove (&tz->ida, pos->id);kfree (pos);return 0 ; }
@@ -920,12 +897,13 @@ __thermal_cooling_device_register(struct device_node *np,if (!cdev)return ERR_PTR (-ENOMEM ); - result =get_idr (&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); -if (result) { + result =ida_simple_get (&thermal_cdev_ida,0 ,0 ,GFP_KERNEL ); +if (result <0 ) {kfree (cdev);return ERR_PTR (result); } + cdev->id = result;strlcpy (cdev->type, type ? :"" ,sizeof (cdev->type));mutex_init (&cdev->lock);INIT_LIST_HEAD (&cdev->thermal_instances);
@@ -938,7 +916,7 @@ __thermal_cooling_device_register(struct device_node *np,dev_set_name (&cdev->device,"cooling_device%d" , cdev->id); result =device_register (&cdev->device);if (result) { -release_idr (&thermal_cdev_idr, &thermal_idr_lock, cdev->id); +ida_simple_remove (&thermal_cdev_ida, cdev->id);kfree (cdev);return ERR_PTR (result); }
@@ -1065,7 +1043,7 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)mutex_unlock (&thermal_list_lock); -release_idr (&thermal_cdev_idr, &thermal_idr_lock, cdev->id); +ida_simple_remove (&thermal_cdev_ida, cdev->id);device_unregister (&cdev->device); }EXPORT_SYMBOL_GPL (thermal_cooling_device_unregister);
@@ -1167,14 +1145,15 @@ thermal_zone_device_register(const char *type, int trips, int mask,return ERR_PTR (-ENOMEM );INIT_LIST_HEAD (&tz->thermal_instances); -idr_init (&tz->idr); +ida_init (&tz->ida);mutex_init (&tz->lock); - result =get_idr (&thermal_tz_idr, &thermal_idr_lock, &tz->id); -if (result) { + result =ida_simple_get (&thermal_tz_ida,0 ,0 ,GFP_KERNEL ); +if (result <0 ) {kfree (tz);return ERR_PTR (result); } + tz->id = result;strlcpy (tz->type, type,sizeof (tz->type)); tz->ops = ops; tz->tzp = tzp;
@@ -1196,7 +1175,7 @@ thermal_zone_device_register(const char *type, int trips, int mask,dev_set_name (&tz->device,"thermal_zone%d" , tz->id); result =device_register (&tz->device);if (result) { -release_idr (&thermal_tz_idr, &thermal_idr_lock, tz->id); +ida_simple_remove (&thermal_tz_ida, tz->id);kfree (tz);return ERR_PTR (result); }
@@ -1250,7 +1229,7 @@ thermal_zone_device_register(const char *type, int trips, int mask,return tz; unregister: -release_idr (&thermal_tz_idr, &thermal_idr_lock, tz->id); +ida_simple_remove (&thermal_tz_ida, tz->id);device_unregister (&tz->device);return ERR_PTR (result); }
@@ -1312,8 +1291,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)thermal_set_governor (tz,NULL );thermal_remove_hwmon_sysfs (tz); -release_idr (&thermal_tz_idr, &thermal_idr_lock, tz->id); -idr_destroy (&tz->idr); +ida_simple_remove (&thermal_tz_ida, tz->id); +ida_destroy (&tz->ida);mutex_destroy (&tz->lock);device_unregister (&tz->device);kfree (tz->device.groups);
@@ -1514,9 +1493,8 @@ static int __init thermal_init(void) unregister_governors:thermal_unregister_governors (); error: -idr_destroy (&thermal_tz_idr); -idr_destroy (&thermal_cdev_idr); -mutex_destroy (&thermal_idr_lock); +ida_destroy (&thermal_tz_ida); +ida_destroy (&thermal_cdev_ida);mutex_destroy (&thermal_list_lock);mutex_destroy (&thermal_governor_lock);return result;
@@ -1529,9 +1507,8 @@ static void __exit thermal_exit(void)genetlink_exit ();class_unregister (&thermal_class);thermal_unregister_governors (); -idr_destroy (&thermal_tz_idr); -idr_destroy (&thermal_cdev_idr); -mutex_destroy (&thermal_idr_lock); +ida_destroy (&thermal_tz_ida); +ida_destroy (&thermal_cdev_ida);mutex_destroy (&thermal_list_lock);mutex_destroy (&thermal_governor_lock); }
include/linux/thermal.h +2 -2
@@ -194,7 +194,7 @@ struct thermal_attr {* @governor: pointer to the governor for this thermal zone * @governor_data: private pointer for governor data * @thermal_instances: list of &struct thermal_instance of this thermal zone -* @idr: &struct idr to generate unique id for this zone's cooling +* @ida: &struct ida to generate unique id for this zone's cooling * devices * @lock: lock to protect thermal_instances list * @node: node in thermal_tz_list (in thermal_core.c)
@@ -227,7 +227,7 @@ struct thermal_zone_device {struct thermal_governor *governor;void *governor_data;struct list_head thermal_instances; -struct idr idr; +struct ida ida;struct mutex lock;struct list_head node;struct delayed_work poll_queue;
Signature · 1 line
2.11.0
PATCH 2/4 thermal: convert clock cooling to use an IDA
In reply to Matthew WilcoxFrom: Matthew Wilcox <[email protected]>
thermal clock cooling does not use the ability to look up pointers by ID, so convert it from using an IDR to the more space-efficient IDA.
--- drivers/thermal/clock_cooling.c | 50 +++++++---------------------------------- 1 file changed, 8 insertions(+), 42 deletions(-)
drivers/thermal/clock_cooling.c +8 -42
@@ -65,42 +65,7 @@ struct clock_cooling_device { };#define to_clock_cooling_device (x) \ container_of(x, struct clock_cooling_device, clk_rate_change_nb) -static DEFINE_IDR (clock_idr); -static DEFINE_MUTEX (cooling_clock_lock); - -/** -* clock_cooling_get_idr - function to get an unique id. -* @id: int * value generated by this function. -* -* This function will populate @id with an unique -* id, using the idr API. -* -* Return: 0 on success, an error code on failure. -*/ -static int clock_cooling_get_idr (int *id) -{ -int ret; - -mutex_lock (&cooling_clock_lock); - ret =idr_alloc (&clock_idr,NULL ,0 ,0 ,GFP_KERNEL ); -mutex_unlock (&cooling_clock_lock); -if (unlikely (ret <0 )) -return ret; - *id = ret; - -return 0 ; -} - -/** -* release_idr - function to free the unique id. -* @id: int value representing the unique id. -*/ -static void release_idr (int id) -{ -mutex_lock (&cooling_clock_lock); -idr_remove (&clock_idr, id); -mutex_unlock (&cooling_clock_lock); -} +static DEFINE_IDA (clock_ida);/* Below code defines functions to be used for clock as cooling device */
@@ -432,16 +397,17 @@ clock_cooling_register(struct device *dev, const char *clock_name)if (IS_ERR (ccdev->clk))return ERR_CAST (ccdev->clk); - ret =clock_cooling_get_idr (&ccdev->id); -if (ret) -return ERR_PTR (-EINVAL ); + ret =ida_simple_get (&clock_ida,0 ,0 ,GFP_KERNEL ); +if (ret <0 ) +return ERR_PTR (ret); + ccdev->id = ret;snprintf (dev_name,sizeof (dev_name),"thermal-clock-%d" , ccdev->id); cdev =thermal_cooling_device_register (dev_name, ccdev, &clock_cooling_ops);if (IS_ERR (cdev)) { -release_idr (ccdev->id); +ida_simple_remove (&clock_ida, ccdev->id);return ERR_PTR (-EINVAL ); } ccdev->cdev = cdev;
@@ -450,7 +416,7 @@ clock_cooling_register(struct device *dev, const char *clock_name)/* Assuming someone has already filled the opp table for this device */ ret =dev_pm_opp_init_cpufreq_table (dev, &ccdev->freq_table);if (ret) { -release_idr (ccdev->id); +ida_simple_remove (&clock_ida, ccdev->id);return ERR_PTR (ret); } ccdev->clock_state =0 ;
@@ -481,6 +447,6 @@ void clock_cooling_unregister(struct thermal_cooling_device *cdev)dev_pm_opp_free_cpufreq_table (ccdev->dev, &ccdev->freq_table);thermal_cooling_device_unregister (ccdev->cdev); -release_idr (ccdev->id); +ida_simple_remove (&clock_ida, ccdev->id); }EXPORT_SYMBOL_GPL (clock_cooling_unregister);
Signature · 1 line
2.11.0
PATCH 3/4 thermal: convert cpu_cooling to use an IDA
In reply to Matthew WilcoxFrom: Matthew Wilcox <[email protected]>
thermal cpu cooling does not use the ability to look up pointers by ID, so convert it from using an IDR to the more space-efficient IDA.
The cooling_cpufreq_lock was being used to protect cpufreq_dev_count as well as the IDR. Rather than keep the mutex to protect a single integer, I expanded the scope of cooling_list_lock to also cover cpufreq_dev_count. We could also convert cpufreq_dev_count into an atomic.
--- drivers/thermal/cpu_cooling.c | 63 ++++++++----------------------------------- 1 file changed, 11 insertions(+), 52 deletions(-)
drivers/thermal/cpu_cooling.c +11 -52
@@ -26,6 +26,7 @@#include <linux/thermal.h> #include <linux/cpufreq.h> #include <linux/err.h> +#include <linux/idr.h> #include <linux/pm_opp.h> #include <linux/slab.h> #include <linux/cpu.h>
@@ -104,50 +105,13 @@ struct cpufreq_cooling_device {struct device *cpu_dev;get_static_t plat_get_static_power; }; -static DEFINE_IDR (cpufreq_idr); -static DEFINE_MUTEX (cooling_cpufreq_lock); +static DEFINE_IDA (cpufreq_ida);static unsigned int cpufreq_dev_count;static DEFINE_MUTEX (cooling_list_lock);static LIST_HEAD (cpufreq_dev_list); -/** -* get_idr - function to get a unique id. -* @idr: struct idr * handle used to create a id. -* @id: int * value generated by this function. -* -* This function will populate @id with an unique -* id, using the idr API. -* -* Return: 0 on success, an error code on failure. -*/ -static int get_idr (struct idr *idr,int *id) -{ -int ret; - -mutex_lock (&cooling_cpufreq_lock); - ret =idr_alloc (idr,NULL ,0 ,0 ,GFP_KERNEL ); -mutex_unlock (&cooling_cpufreq_lock); -if (unlikely (ret <0 )) -return ret; - *id = ret; - -return 0 ; -} - -/** -* release_idr - function to free the unique id. -* @idr: struct idr * handle used for creating the id. -* @id: int value representing the unique id. -*/ -static void release_idr (struct idr *idr,int id) -{ -mutex_lock (&cooling_cpufreq_lock); -idr_remove (idr, id); -mutex_unlock (&cooling_cpufreq_lock); -} -/* Below code defines functions to be used for cpufreq as cooling device */ /**
@@ -874,11 +838,12 @@ __cpufreq_cooling_register(struct device_node *np, cooling_ops = &cpufreq_cooling_ops; } - ret =get_idr (&cpufreq_idr, &cpufreq_dev->id); -if (ret) { + ret =ida_simple_get (&cpufreq_ida,0 ,0 ,GFP_KERNEL ); +if (ret <0 ) { cool_dev =ERR_PTR (ret); goto free_power_table; } + cpufreq_dev->id = ret;/* Fill freq-table in descending order of frequencies */ for (i =0 , freq =-1 ; i <= cpufreq_dev->max_level; i++) {
@@ -898,27 +863,24 @@ __cpufreq_cooling_register(struct device_node *np, cool_dev =thermal_of_cooling_device_register (np, dev_name, cpufreq_dev, cooling_ops);if (IS_ERR (cool_dev)) - goto remove_idr; + goto remove_ida; cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0 ]; cpufreq_dev->cool_dev = cool_dev; -mutex_lock (&cooling_cpufreq_lock); -mutex_lock (&cooling_list_lock);list_add (&cpufreq_dev->node, &cpufreq_dev_list); -mutex_unlock (&cooling_list_lock);/* Register the notifier for first cpufreq cooling device */ if (!cpufreq_dev_count++)cpufreq_register_notifier (&thermal_cpufreq_notifier_block,CPUFREQ_POLICY_NOTIFIER ); -mutex_unlock (&cooling_cpufreq_lock); +mutex_unlock (&cooling_list_lock); goto put_policy; -remove_idr: -release_idr (&cpufreq_idr, cpufreq_dev->id); +remove_ida: +ida_simple_remove (&cpufreq_ida, cpufreq_dev->id); free_power_table:kfree (cpufreq_dev->dyn_power_table); free_table:
@@ -1059,20 +1021,17 @@ void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) cpufreq_dev = cdev->devdata; +mutex_lock (&cooling_list_lock);/* Unregister the notifier for the last cpufreq cooling device */ -mutex_lock (&cooling_cpufreq_lock);if (!--cpufreq_dev_count)cpufreq_unregister_notifier (&thermal_cpufreq_notifier_block,CPUFREQ_POLICY_NOTIFIER ); -mutex_lock (&cooling_list_lock);list_del (&cpufreq_dev->node);mutex_unlock (&cooling_list_lock); -mutex_unlock (&cooling_cpufreq_lock); -thermal_cooling_device_unregister (cpufreq_dev->cool_dev); -release_idr (&cpufreq_idr, cpufreq_dev->id); +ida_simple_remove (&cpufreq_ida, cpufreq_dev->id);kfree (cpufreq_dev->dyn_power_table);kfree (cpufreq_dev->time_in_idle_timestamp);kfree (cpufreq_dev->time_in_idle);
Signature · 1 line
2.11.0
PATCH 4/4 thermal: convert devfreq_cooling to use an IDA
In reply to Matthew WilcoxFrom: Matthew Wilcox <[email protected]>
thermal devfreq cooling does not use the ability to look up pointers by ID, so convert it from using an IDR to the more space-efficient IDA.
--- drivers/thermal/devfreq_cooling.c | 53 +++++++-------------------------------- 1 file changed, 9 insertions(+), 44 deletions(-)
drivers/thermal/devfreq_cooling.c +9 -44
@@ -21,14 +21,14 @@#include <linux/devfreq.h> #include <linux/devfreq_cooling.h> #include <linux/export.h> +#include <linux/idr.h> #include <linux/slab.h> #include <linux/pm_opp.h> #include <linux/thermal.h> #include <trace/events/thermal.h> -static DEFINE_MUTEX (devfreq_lock); -static DEFINE_IDR (devfreq_idr); +static DEFINE_IDA (devfreq_ida);/** * struct devfreq_cooling_device - Devfreq cooling device
@@ -58,42 +58,6 @@ struct devfreq_cooling_device { };/** -* get_idr - function to get a unique id. -* @idr: struct idr * handle used to create a id. -* @id: int * value generated by this function. -* -* This function will populate @id with an unique -* id, using the idr API. -* -* Return: 0 on success, an error code on failure. -*/ -static int get_idr (struct idr *idr,int *id) -{ -int ret; - -mutex_lock (&devfreq_lock); - ret =idr_alloc (idr,NULL ,0 ,0 ,GFP_KERNEL ); -mutex_unlock (&devfreq_lock); -if (unlikely (ret <0 )) -return ret; - *id = ret; - -return 0 ; -} - -/** -* release_idr - function to free the unique id. -* @idr: struct idr * handle used for creating the id. -* @id: int value representing the unique id. -*/ -static void release_idr (struct idr *idr,int id) -{ -mutex_lock (&devfreq_lock); -idr_remove (idr, id); -mutex_unlock (&devfreq_lock); -} - -/** * partition_enable_opps() - disable all opps above a given state * @dfc: Pointer to devfreq we are operating on * @cdev_state: cooling device state we're setting
@@ -496,9 +460,10 @@ of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,if (err) goto free_dfc; - err =get_idr (&devfreq_idr, &dfc->id); -if (err) + err =ida_simple_get (&devfreq_ida,0 ,0 ,GFP_KERNEL ); +if (err <0 ) goto free_tables; + dfc->id = err;snprintf (dev_name,sizeof (dev_name),"thermal-devfreq-%d" , dfc->id);
@@ -509,15 +474,15 @@ of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,dev_err (df->dev.parent,"Failed to register devfreq cooling device (%d)\n" , err); - goto release_idr; + goto release_ida; } dfc->cdev = cdev;return cdev; -release_idr: -release_idr (&devfreq_idr, dfc->id); +release_ida: +ida_simple_remove (&devfreq_ida, dfc->id); free_tables:kfree (dfc->power_table);kfree (dfc->freq_table);
@@ -565,7 +530,7 @@ void devfreq_cooling_unregister(struct thermal_cooling_device *cdev) dfc = cdev->devdata;thermal_cooling_device_unregister (dfc->cdev); -release_idr (&devfreq_idr, dfc->id); +ida_simple_remove (&devfreq_ida, dfc->id);kfree (dfc->power_table);kfree (dfc->freq_table);
Signature · 1 line
2.11.0
Zhang Rui
reviewerHi, Matthew,
Quoted 6 lines · Matthew Wilcox
From: Matthew Wilcox <[email protected]>
None of the ID allocations are in a hot path, so I think it's better to use the ida_simple_get/ida_simple_remove API instead of having individual mutexes.
I agree. Thanks for the patches.
Applied and queued for 4.11.
thanks,
rui
Quoted 23 lines
I've only compile-tested these changes ... even that is quite tricky given
the maze ofKconfigoptions which make it hard to select those drivers.
I eventually gave up and played games with the header files to get the
compiler to compile them cleanly.Matthew Wilcox (4):
thermal core: convert ID allocation to IDA
thermal: convert clock cooling to use an IDA
thermal: convertcpu_coolingto use an IDA
thermal: convertdevfreq_coolingto use an IDAdrivers/thermal/clock_cooling.c | 50 +++++--------------------- drivers/thermal/cpu_cooling.c | 63 ++++++-----------------------
---
drivers/thermal/devfreq_cooling.c | 53 +++++---------------------- drivers/thermal/thermal_core.c | 75 ++++++++++++++---------------
----------
include/linux/thermal.h | 4 +--5 files changed, 56 insertions(+), 189 deletions(-)