Commit 3f4a782b authored by Mathieu Desnoyers's avatar Mathieu Desnoyers Committed by Dave Jones

[CPUFREQ] fix (utter) cpufreq_add_dev mess

OK, I've tried to clean it up the best I could, but please test this with
concurrent cpu hotplug and cpufreq add/remove in loops. I'm sure we will make
other interesting findings.

This is step one of fixing the overall locking dependency mess in cpufreq.
Signed-off-by: default avatarMathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
CC: Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
CC: rjw@sisk.pl
CC: mingo@elte.hu
CC: Shaohua Li <shaohua.li@intel.com>
CC: Pekka Enberg <penberg@cs.helsinki.fi>
CC: Dave Young <hidave.darkstar@gmail.com>
CC: "Rafael J. Wysocki" <rjw@sisk.pl>
CC: Rusty Russell <rusty@rustcorp.com.au>
CC: sven.wegener@stealer.net
CC: cpufreq@vger.kernel.org
CC: Thomas Renninger <trenn@suse.de>
Signed-off-by: default avatarDave Jones <davej@redhat.com>
parent ee88415c
...@@ -761,6 +761,10 @@ static struct kobj_type ktype_cpufreq = { ...@@ -761,6 +761,10 @@ static struct kobj_type ktype_cpufreq = {
* cpufreq_add_dev - add a CPU device * cpufreq_add_dev - add a CPU device
* *
* Adds the cpufreq interface for a CPU device. * Adds the cpufreq interface for a CPU device.
*
* The Oracle says: try running cpufreq registration/unregistration concurrently
* with with cpu hotplugging and all hell will break loose. Tried to clean this
* mess up, but more thorough testing is needed. - Mathieu
*/ */
static int cpufreq_add_dev(struct sys_device *sys_dev) static int cpufreq_add_dev(struct sys_device *sys_dev)
{ {
...@@ -804,15 +808,12 @@ static int cpufreq_add_dev(struct sys_device *sys_dev) ...@@ -804,15 +808,12 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
goto nomem_out; goto nomem_out;
} }
if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) { if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) {
kfree(policy);
ret = -ENOMEM; ret = -ENOMEM;
goto nomem_out; goto err_free_policy;
} }
if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) { if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) {
free_cpumask_var(policy->cpus);
kfree(policy);
ret = -ENOMEM; ret = -ENOMEM;
goto nomem_out; goto err_free_cpumask;
} }
policy->cpu = cpu; policy->cpu = cpu;
...@@ -820,7 +821,8 @@ static int cpufreq_add_dev(struct sys_device *sys_dev) ...@@ -820,7 +821,8 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
/* Initially set CPU itself as the policy_cpu */ /* Initially set CPU itself as the policy_cpu */
per_cpu(policy_cpu, cpu) = cpu; per_cpu(policy_cpu, cpu) = cpu;
lock_policy_rwsem_write(cpu); ret = (lock_policy_rwsem_write(cpu) < 0);
WARN_ON(ret);
init_completion(&policy->kobj_unregister); init_completion(&policy->kobj_unregister);
INIT_WORK(&policy->update, handle_update); INIT_WORK(&policy->update, handle_update);
...@@ -833,7 +835,7 @@ static int cpufreq_add_dev(struct sys_device *sys_dev) ...@@ -833,7 +835,7 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
ret = cpufreq_driver->init(policy); ret = cpufreq_driver->init(policy);
if (ret) { if (ret) {
dprintk("initialization failed\n"); dprintk("initialization failed\n");
goto err_out; goto err_unlock_policy;
} }
policy->user_policy.min = policy->min; policy->user_policy.min = policy->min;
policy->user_policy.max = policy->max; policy->user_policy.max = policy->max;
...@@ -858,15 +860,21 @@ static int cpufreq_add_dev(struct sys_device *sys_dev) ...@@ -858,15 +860,21 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
/* Check for existing affected CPUs. /* Check for existing affected CPUs.
* They may not be aware of it due to CPU Hotplug. * They may not be aware of it due to CPU Hotplug.
*/ */
managed_policy = cpufreq_cpu_get(j); /* FIXME: Where is this released? What about error paths? */ managed_policy = cpufreq_cpu_get(j);
if (unlikely(managed_policy)) { if (unlikely(managed_policy)) {
/* Set proper policy_cpu */ /* Set proper policy_cpu */
unlock_policy_rwsem_write(cpu); unlock_policy_rwsem_write(cpu);
per_cpu(policy_cpu, cpu) = managed_policy->cpu; per_cpu(policy_cpu, cpu) = managed_policy->cpu;
if (lock_policy_rwsem_write(cpu) < 0) if (lock_policy_rwsem_write(cpu) < 0) {
goto err_out_driver_exit; /* Should not go through policy unlock path */
if (cpufreq_driver->exit)
cpufreq_driver->exit(policy);
ret = -EBUSY;
cpufreq_cpu_put(managed_policy);
goto err_free_cpumask;
}
spin_lock_irqsave(&cpufreq_driver_lock, flags); spin_lock_irqsave(&cpufreq_driver_lock, flags);
cpumask_copy(managed_policy->cpus, policy->cpus); cpumask_copy(managed_policy->cpus, policy->cpus);
...@@ -877,12 +885,14 @@ static int cpufreq_add_dev(struct sys_device *sys_dev) ...@@ -877,12 +885,14 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
ret = sysfs_create_link(&sys_dev->kobj, ret = sysfs_create_link(&sys_dev->kobj,
&managed_policy->kobj, &managed_policy->kobj,
"cpufreq"); "cpufreq");
if (ret) if (!ret)
goto err_out_driver_exit; cpufreq_cpu_put(managed_policy);
/*
cpufreq_debug_enable_ratelimit(); * Success. We only needed to be added to the mask.
ret = 0; * Call driver->exit() because only the cpu parent of
goto err_out_driver_exit; /* call driver->exit() */ * the kobj needed to call init().
*/
goto out_driver_exit; /* call driver->exit() */
} }
} }
#endif #endif
...@@ -892,25 +902,25 @@ static int cpufreq_add_dev(struct sys_device *sys_dev) ...@@ -892,25 +902,25 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, &sys_dev->kobj, ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, &sys_dev->kobj,
"cpufreq"); "cpufreq");
if (ret) if (ret)
goto err_out_driver_exit; goto out_driver_exit;
/* set up files for this cpu device */ /* set up files for this cpu device */
drv_attr = cpufreq_driver->attr; drv_attr = cpufreq_driver->attr;
while ((drv_attr) && (*drv_attr)) { while ((drv_attr) && (*drv_attr)) {
ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
if (ret) if (ret)
goto err_out_driver_exit; goto err_out_kobj_put;
drv_attr++; drv_attr++;
} }
if (cpufreq_driver->get) { if (cpufreq_driver->get) {
ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
if (ret) if (ret)
goto err_out_driver_exit; goto err_out_kobj_put;
} }
if (cpufreq_driver->target) { if (cpufreq_driver->target) {
ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
if (ret) if (ret)
goto err_out_driver_exit; goto err_out_kobj_put;
} }
spin_lock_irqsave(&cpufreq_driver_lock, flags); spin_lock_irqsave(&cpufreq_driver_lock, flags);
...@@ -928,12 +938,14 @@ static int cpufreq_add_dev(struct sys_device *sys_dev) ...@@ -928,12 +938,14 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
continue; continue;
dprintk("CPU %u already managed, adding link\n", j); dprintk("CPU %u already managed, adding link\n", j);
cpufreq_cpu_get(cpu); managed_policy = cpufreq_cpu_get(cpu);
cpu_sys_dev = get_cpu_sysdev(j); cpu_sys_dev = get_cpu_sysdev(j);
ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj, ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
"cpufreq"); "cpufreq");
if (ret) if (ret) {
cpufreq_cpu_put(managed_policy);
goto err_out_unregister; goto err_out_unregister;
}
} }
policy->governor = NULL; /* to assure that the starting sequence is policy->governor = NULL; /* to assure that the starting sequence is
...@@ -965,17 +977,20 @@ static int cpufreq_add_dev(struct sys_device *sys_dev) ...@@ -965,17 +977,20 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
per_cpu(cpufreq_cpu_data, j) = NULL; per_cpu(cpufreq_cpu_data, j) = NULL;
spin_unlock_irqrestore(&cpufreq_driver_lock, flags); spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
err_out_kobj_put:
kobject_put(&policy->kobj); kobject_put(&policy->kobj);
wait_for_completion(&policy->kobj_unregister); wait_for_completion(&policy->kobj_unregister);
err_out_driver_exit: out_driver_exit:
if (cpufreq_driver->exit) if (cpufreq_driver->exit)
cpufreq_driver->exit(policy); cpufreq_driver->exit(policy);
err_out: err_unlock_policy:
unlock_policy_rwsem_write(cpu); unlock_policy_rwsem_write(cpu);
err_free_cpumask:
free_cpumask_var(policy->cpus);
err_free_policy:
kfree(policy); kfree(policy);
nomem_out: nomem_out:
module_put(cpufreq_driver->owner); module_put(cpufreq_driver->owner);
module_out: module_out:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment