Skip to contentSkip to conversation

cpufreq: fix a deadlock during shutting down

PATCH NAK Amerigo Wang 3 participants 4 messages Feb 9, 2010 – Feb 10, 2010 CPU frequency scaling framework

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpufreq: fix a deadlock during shutting down

Benjamin reported that, the machine deadlocks right after printing the following when doing a shutdown:

halt/4071 is trying to acquire lock:
(s_active){++++.+}, at: [<c0000000001ef868>] .sysfs_addrm_finish+0x58/0xc0

but task is already holding lock:
(&per_cpu(cpu_policy_rwsem, cpu)){+.+.+.}, at: [<c0000000004cd6ac>] .lock_policy_rwsem_write+0x84/0xf4

which lock already depends on the new lock.

the existing dependency chain (in reverse order) is:

<nothing else ... machine deadlocked here>

This is because we are trying to kobject_put() a kobject while we are holding cpu policy rwsem. So just move kobject_put() down after releasing the rwsem.

Totally untested.

Cc Dave Jones <[email protected]>
Cc Thomas Renninger <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Venkatesh Pallipadi <[email protected]>

---

drivers/cpufreq/cpufreq.c +3 -1
index 67bc2ec..222b35f 100644
@@ -1113,6 +1113,7 @@ static int __cpufreq_remove_dev(struct sys_device *sys_dev)
 	unsigned int cpu = sys_dev->id;
 	unsigned long flags;
 	struct cpufreq_policy *data;
+	struct kobject *kobj;
 #ifdef CONFIG_SMP
 	struct sys_device *cpu_sys_dev;
 	unsigned int j;
@@ -1192,7 +1193,7 @@ static int __cpufreq_remove_dev(struct sys_device *sys_dev)
 	if (cpufreq_driver->target)
 		__cpufreq_governor(data, CPUFREQ_GOV_STOP);
 
-	kobject_put(&data->kobj);
+	kobj = &data->kobj;
 
 	/* we need to make sure that the underlying kobj is actually
 	 * not referenced anymore by anybody before we proceed with
@@ -1207,6 +1208,7 @@ static int __cpufreq_remove_dev(struct sys_device *sys_dev)
 
 	unlock_policy_rwsem_write(cpu);
 
+	kobject_put(kobj);
 	free_cpumask_var(data->related_cpus);
 	free_cpumask_var(data->cpus);
 	kfree(data);
#1 lore raw
Quoted 44 lines · Amerigo Wang

Benjamin reported that, the machine deadlocks right after printing the following when doing a shutdown:

halt/4071 is trying to acquire lock:
(s_active){++++.+}, at: [<c0000000001ef868>] .sysfs_addrm_finish+0x58/0xc0

but task is already holding lock:
(&per_cpu(cpu_policy_rwsem, cpu)){+.+.+.}, at: [<c0000000004cd6ac>] .lock_policy_rwsem_write+0x84/0xf4

which lock already depends on the new lock.

the existing dependency chain (in reverse order) is:

<nothing else ... machine deadlocked here>

This is because we are trying to kobject_put() a kobject while we are holding cpu policy rwsem. So just move kobject_put() down after releasing the rwsem.

Totally untested.

Cc Dave Jones <[email protected]>
Cc Thomas Renninger <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Venkatesh Pallipadi <[email protected]>

---

drivers/cpufreq/cpufreq.c +1 -0
index 67bc2ec..222b35f 100644
@@ -1113,6 +1113,7 @@ static int __cpufreq_remove_dev(struct sys_device *sys_dev)
 	unsigned int cpu = sys_dev->id;
 	unsigned long flags;
 	struct cpufreq_policy *data;
+	struct kobject *kobj;
 #ifdef CONFIG_SMP
 	struct sys_device *cpu_sys_dev;
 	unsigned int j;

drivers/cpufreq/cpufreq.c static int __cpufreq_remove_dev(struct sys_device *sys_dev) · line 1199

Quoted 8 lines
drivers/cpufreq/cpufreq.c +1 -1
@@ -1192,7 +1193,7 @@ static int __cpufreq_remove_dev(struct sys_device *sys_dev)
 	if (cpufreq_driver->target)
 		__cpufreq_governor(data, CPUFREQ_GOV_STOP);
 
-	kobject_put(&data->kobj);
+	kobj = &data->kobj;
 
 	/* we need to make sure that the underlying kobj is actually
 	 * not referenced anymore by anybody before we proceed with

NACK.

If you read this comment, it says:

	/* we need to make sure that the underlying kobj is actually
	 * not referenced anymore by anybody before we proceed with
	 * unloading.
	 */

That would suggest that the wait_for_completion(&data->kobj_unregister); would never actually return if you're holding a reference to it in your patch since it only completes when the last reference is dropped (the ->release function is cpufreq_sysfs_release()).

author
#2 lore raw
Quoted 59 lines · David Rientjes
Quoted 44 lines · Amerigo Wang

Benjamin reported that, the machine deadlocks right after printing the following when doing a shutdown:

halt/4071 is trying to acquire lock:
(s_active){++++.+}, at: [<c0000000001ef868>] .sysfs_addrm_finish+0x58/0xc0

but task is already holding lock:
(&per_cpu(cpu_policy_rwsem, cpu)){+.+.+.}, at: [<c0000000004cd6ac>] .lock_policy_rwsem_write+0x84/0xf4

which lock already depends on the new lock.

the existing dependency chain (in reverse order) is:

<nothing else ... machine deadlocked here>

This is because we are trying to kobject_put() a kobject while we are holding cpu policy rwsem. So just move kobject_put() down after releasing the rwsem.

Totally untested.

Cc Dave Jones <[email protected]>
Cc Thomas Renninger <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Venkatesh Pallipadi <[email protected]>

---

drivers/cpufreq/cpufreq.c +1 -0
index 67bc2ec..222b35f 100644
@@ -1113,6 +1113,7 @@ static int __cpufreq_remove_dev(struct sys_device *sys_dev)
 	unsigned int cpu = sys_dev->id;
 	unsigned long flags;
 	struct cpufreq_policy *data;
+	struct kobject *kobj;
 #ifdef CONFIG_SMP
 	struct sys_device *cpu_sys_dev;
 	unsigned int j;

drivers/cpufreq/cpufreq.c static int __cpufreq_remove_dev(struct sys_device *sys_dev) · line 1199

Quoted 8 lines
drivers/cpufreq/cpufreq.c +1 -1
@@ -1192,7 +1193,7 @@ static int __cpufreq_remove_dev(struct sys_device *sys_dev)
 	if (cpufreq_driver->target)
 		__cpufreq_governor(data, CPUFREQ_GOV_STOP);
 
-	kobject_put(&data->kobj);
+	kobj = &data->kobj;
 
 	/* we need to make sure that the underlying kobj is actually
 	 * not referenced anymore by anybody before we proceed with

NACK.

If you read this comment, it says:

Quoted 7 lines
	/* we need to make sure that the underlying kobj is actually
	 * not referenced anymore by anybody before we proceed with
	 * unloading.
	 */

That would suggest that the wait_for_completion(&data->kobj_unregister); would never actually return if you're holding a reference to it in your patch since it only completes when the last reference is dropped (the ->release function is cpufreq_sysfs_release()).

Oh, my bad.

Then this case seems to be more complex... But anyway, this is _not_ a bogus.

Thanks.

#3 lore raw
In reply to Amerigo Wang
Quoted 45 lines · Amerigo Wang

Benjamin reported that, the machine deadlocks right after printing the following when doing a shutdown:

halt/4071 is trying to acquire lock:
 (s_active){++++.+}, at: [<c0000000001ef868>] .sysfs_addrm_finish+0x58/0xc0

but task is already holding lock:
 (&per_cpu(cpu_policy_rwsem, cpu)){+.+.+.}, at: [<c0000000004cd6ac>] .lock_policy_rwsem_write+0x84/0xf4

which lock already depends on the new lock.

the existing dependency chain (in reverse order) is:

<nothing else ... machine deadlocked here>

This is because we are trying to kobject_put() a kobject while we are holding cpu policy rwsem. So just move kobject_put() down after releasing the rwsem.

Totally untested.

Cc Dave Jones <[email protected]>
Cc Thomas Renninger <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Venkatesh Pallipadi <[email protected]>

---

drivers/cpufreq/cpufreq.c +1 -0
index 67bc2ec..222b35f 100644
@@ -1113,6 +1113,7 @@ static int __cpufreq_remove_dev(struct sys_device *sys_dev)
        unsigned int cpu = sys_dev->id;
        unsigned long flags;
        struct cpufreq_policy *data;
+       struct kobject *kobj;
  #ifdef CONFIG_SMP
        struct sys_device *cpu_sys_dev;
        unsigned int j;

drivers/cpufreq/cpufreq.c static int __cpufreq_remove_dev(struct sys_device *sys_dev) · line 1199

Quoted 8 lines
drivers/cpufreq/cpufreq.c +1 -1
@@ -1192,7 +1193,7 @@ static int __cpufreq_remove_dev(struct sys_device *sys_dev)
        if (cpufreq_driver->target)
                __cpufreq_governor(data, CPUFREQ_GOV_STOP);
 
-       kobject_put(&data->kobj);
+       kobj = &data->kobj;
 
        /* we need to make sure that the underlying kobj is actually
         * not referenced anymore by anybody before we proceed with

Then kernel will wait_for_completion(&data->kobj_unregister); forever.....

Quoted 9 lines
drivers/cpufreq/cpufreq.c +1 -0
@@ -1207,6 +1208,7 @@ static int __cpufreq_remove_dev(struct sys_device *sys_dev)
 
        unlock_policy_rwsem_write(cpu);
 
+       kobject_put(kobj);
        free_cpumask_var(data->related_cpus);
        free_cpumask_var(data->cpus);
        kfree(data);