forked from mirrors/linux
		
	thermal: convert cpu_cooling to use an IDA
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. Signed-off-by: Matthew Wilcox <mawilcox@microsoft.com> Signed-off-by: Zhang Rui <rui.zhang@intel.com>
This commit is contained in:
		
							parent
							
								
									7a6639dca6
								
							
						
					
					
						commit
						ae60608962
					
				
					 1 changed files with 11 additions and 52 deletions
				
			
		| 
						 | 
					@ -26,6 +26,7 @@
 | 
				
			||||||
#include <linux/thermal.h>
 | 
					#include <linux/thermal.h>
 | 
				
			||||||
#include <linux/cpufreq.h>
 | 
					#include <linux/cpufreq.h>
 | 
				
			||||||
#include <linux/err.h>
 | 
					#include <linux/err.h>
 | 
				
			||||||
 | 
					#include <linux/idr.h>
 | 
				
			||||||
#include <linux/pm_opp.h>
 | 
					#include <linux/pm_opp.h>
 | 
				
			||||||
#include <linux/slab.h>
 | 
					#include <linux/slab.h>
 | 
				
			||||||
#include <linux/cpu.h>
 | 
					#include <linux/cpu.h>
 | 
				
			||||||
| 
						 | 
					@ -104,50 +105,13 @@ struct cpufreq_cooling_device {
 | 
				
			||||||
	struct device *cpu_dev;
 | 
						struct device *cpu_dev;
 | 
				
			||||||
	get_static_t plat_get_static_power;
 | 
						get_static_t plat_get_static_power;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
static DEFINE_IDR(cpufreq_idr);
 | 
					static DEFINE_IDA(cpufreq_ida);
 | 
				
			||||||
static DEFINE_MUTEX(cooling_cpufreq_lock);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
static unsigned int cpufreq_dev_count;
 | 
					static unsigned int cpufreq_dev_count;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static DEFINE_MUTEX(cooling_list_lock);
 | 
					static DEFINE_MUTEX(cooling_list_lock);
 | 
				
			||||||
static LIST_HEAD(cpufreq_dev_list);
 | 
					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 */
 | 
					/* 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;
 | 
							cooling_ops = &cpufreq_cooling_ops;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
 | 
						ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
 | 
				
			||||||
	if (ret) {
 | 
						if (ret < 0) {
 | 
				
			||||||
		cool_dev = ERR_PTR(ret);
 | 
							cool_dev = ERR_PTR(ret);
 | 
				
			||||||
		goto free_power_table;
 | 
							goto free_power_table;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						cpufreq_dev->id = ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Fill freq-table in descending order of frequencies */
 | 
						/* Fill freq-table in descending order of frequencies */
 | 
				
			||||||
	for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) {
 | 
						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,
 | 
						cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
 | 
				
			||||||
						      cooling_ops);
 | 
											      cooling_ops);
 | 
				
			||||||
	if (IS_ERR(cool_dev))
 | 
						if (IS_ERR(cool_dev))
 | 
				
			||||||
		goto remove_idr;
 | 
							goto remove_ida;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0];
 | 
						cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0];
 | 
				
			||||||
	cpufreq_dev->cool_dev = cool_dev;
 | 
						cpufreq_dev->cool_dev = cool_dev;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mutex_lock(&cooling_cpufreq_lock);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	mutex_lock(&cooling_list_lock);
 | 
						mutex_lock(&cooling_list_lock);
 | 
				
			||||||
	list_add(&cpufreq_dev->node, &cpufreq_dev_list);
 | 
						list_add(&cpufreq_dev->node, &cpufreq_dev_list);
 | 
				
			||||||
	mutex_unlock(&cooling_list_lock);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Register the notifier for first cpufreq cooling device */
 | 
						/* Register the notifier for first cpufreq cooling device */
 | 
				
			||||||
	if (!cpufreq_dev_count++)
 | 
						if (!cpufreq_dev_count++)
 | 
				
			||||||
		cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
 | 
							cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
 | 
				
			||||||
					  CPUFREQ_POLICY_NOTIFIER);
 | 
										  CPUFREQ_POLICY_NOTIFIER);
 | 
				
			||||||
	mutex_unlock(&cooling_cpufreq_lock);
 | 
						mutex_unlock(&cooling_list_lock);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	goto put_policy;
 | 
						goto put_policy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
remove_idr:
 | 
					remove_ida:
 | 
				
			||||||
	release_idr(&cpufreq_idr, cpufreq_dev->id);
 | 
						ida_simple_remove(&cpufreq_ida, cpufreq_dev->id);
 | 
				
			||||||
free_power_table:
 | 
					free_power_table:
 | 
				
			||||||
	kfree(cpufreq_dev->dyn_power_table);
 | 
						kfree(cpufreq_dev->dyn_power_table);
 | 
				
			||||||
free_table:
 | 
					free_table:
 | 
				
			||||||
| 
						 | 
					@ -1059,20 +1021,17 @@ void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cpufreq_dev = cdev->devdata;
 | 
						cpufreq_dev = cdev->devdata;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mutex_lock(&cooling_list_lock);
 | 
				
			||||||
	/* Unregister the notifier for the last cpufreq cooling device */
 | 
						/* Unregister the notifier for the last cpufreq cooling device */
 | 
				
			||||||
	mutex_lock(&cooling_cpufreq_lock);
 | 
					 | 
				
			||||||
	if (!--cpufreq_dev_count)
 | 
						if (!--cpufreq_dev_count)
 | 
				
			||||||
		cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
 | 
							cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
 | 
				
			||||||
					    CPUFREQ_POLICY_NOTIFIER);
 | 
										    CPUFREQ_POLICY_NOTIFIER);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mutex_lock(&cooling_list_lock);
 | 
					 | 
				
			||||||
	list_del(&cpufreq_dev->node);
 | 
						list_del(&cpufreq_dev->node);
 | 
				
			||||||
	mutex_unlock(&cooling_list_lock);
 | 
						mutex_unlock(&cooling_list_lock);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mutex_unlock(&cooling_cpufreq_lock);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
 | 
						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->dyn_power_table);
 | 
				
			||||||
	kfree(cpufreq_dev->time_in_idle_timestamp);
 | 
						kfree(cpufreq_dev->time_in_idle_timestamp);
 | 
				
			||||||
	kfree(cpufreq_dev->time_in_idle);
 | 
						kfree(cpufreq_dev->time_in_idle);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue