Skip to contentSkip to conversation

x86/therm_throt.c: Fix error handling in thermal_throttle_add_dev

PATCH In review Fenghua Yu 4 participants 6 messages Sep 8, 2010 – Oct 8, 2010 x86

lore
Share & tools
lore

Fenghua Yu

author
#0 lore raw

PATCH v2 1/1 x86/therm_throt.c: Fix error handling in thermal_throttle_add_dev

From: Fenghua Yu <[email protected]>

When sysfs_add_file_to_group fails, thermal_throttle_add_dev removes the created group and returns with the error code and the driver cleans up and returns with the error code. Thus the driver either installs all devices successfully or doesn't install any device at all.

---
 arch/x86/kernel/cpu/mcheck/therm_throt.c |   36 ++++++++++++++++++++++++++---
 1 files changed, 32 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/mcheck/therm_throt.c +32 -4
index c2a8b26..5099e90 100644
@@ -211,19 +211,33 @@ static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev)
 	if (err)
 		return err;
 
-	if (cpu_has(c, X86_FEATURE_PLN))
+	if (cpu_has(c, X86_FEATURE_PLN)) {
 		err = sysfs_add_file_to_group(&sys_dev->kobj,
 					      &attr_core_power_limit_count.attr,
 					      thermal_attr_group.name);
-	if (cpu_has(c, X86_FEATURE_PTS))
+		if (err)
+			goto error;
+	}
+
+	if (cpu_has(c, X86_FEATURE_PTS)) {
 		err = sysfs_add_file_to_group(&sys_dev->kobj,
 					      &attr_package_throttle_count.attr,
 					      thermal_attr_group.name);
-		if (cpu_has(c, X86_FEATURE_PLN))
+		if (err)
+			goto error;
+
+		if (cpu_has(c, X86_FEATURE_PLN)) {
 			err = sysfs_add_file_to_group(&sys_dev->kobj,
 					&attr_package_power_limit_count.attr,
 					thermal_attr_group.name);
+			if (err)
+				goto error;
+		}
+	}
 
+	return 0;
+error:
+	sysfs_remove_group(&sys_dev->kobj, &thermal_attr_group);
 	return err;
 }
 
@@ -275,6 +289,7 @@ static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata  static __init int thermal_throttle_init_device(void)
 {
 	unsigned int cpu = 0;
+	int i;
 	int err;
 
 	if (!atomic_read(&therm_throt_en))
@@ -288,13 +303,26 @@ static __init int thermal_throttle_init_device(void)
 	/* connect live CPUs to sysfs */
 	for_each_online_cpu(cpu) {
 		err = thermal_throttle_add_dev(get_cpu_sysdev(cpu));
-		WARN_ON(err);
+		if (err)
+			goto error;
 	}
 #ifdef CONFIG_HOTPLUG_CPU
 	mutex_unlock(&therm_cpu_lock);
 #endif
 
 	return 0;
+error:
+	WARN_ON(err);
+
+	/* cleanup. */
+	for (i = 0; i < cpu; i++)
+		thermal_throttle_remove_dev(get_cpu_sysdev(i));
+#ifdef CONFIG_HOTPLUG_CPU
+	mutex_unlock(&therm_cpu_lock);
+#endif
+	unregister_hotcpu_notifier(&thermal_throttle_cpu_notifier);
+
+	return err;
 }
 device_initcall(thermal_throttle_init_device);
 
Signature · 7 lines
1.6.0.3


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#1 lore raw
Quoted 6 lines · Fenghua Yu

From: Fenghua Yu <[email protected]>

When sysfs_add_file_to_group fails, thermal_throttle_add_dev removes the created group and returns with the error code and the driver cleans up and returns with the error code. Thus the driver either installs all devices successfully or doesn't install any device at all.

I don't think this makes any sense. While I generally agree with the idea that a given device (actually, CPU feature) should either be fully available or not available at all, I don't get the point of preventing the driver from loading because one device couldn't be initialized for whatever reason. I don't know of any other driver behaving this way.

What's the rationale? I think Ingo's wording was inaccurate and when he wrote "we should either initialize a driver fully - or not intialize it at all" he really meant "device" not "driver. Ingo?

Quoted 37 lines
---
 arch/x86/kernel/cpu/mcheck/therm_throt.c |   36 ++++++++++++++++++++++++++---
 1 files changed, 32 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/mcheck/therm_throt.c +11 -3
index c2a8b26..5099e90 100644
@@ -211,19 +211,33 @@ static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev)
 	if (err)
 		return err;
 
-	if (cpu_has(c, X86_FEATURE_PLN))
+	if (cpu_has(c, X86_FEATURE_PLN)) {
 		err = sysfs_add_file_to_group(&sys_dev->kobj,
 					      &attr_core_power_limit_count.attr,
 					      thermal_attr_group.name);
-	if (cpu_has(c, X86_FEATURE_PTS))
+		if (err)
+			goto error;
+	}
+
+	if (cpu_has(c, X86_FEATURE_PTS)) {
 		err = sysfs_add_file_to_group(&sys_dev->kobj,
 					      &attr_package_throttle_count.attr,
 					      thermal_attr_group.name);
-		if (cpu_has(c, X86_FEATURE_PLN))
+		if (err)
+			goto error;
+
+		if (cpu_has(c, X86_FEATURE_PLN)) {
 			err = sysfs_add_file_to_group(&sys_dev->kobj,
 					&attr_package_power_limit_count.attr,
 					thermal_attr_group.name);
+			if (err)

arch/x86/kernel/cpu/mcheck/therm_throt.c static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev) · line 243

Quoted 10 lines
arch/x86/kernel/cpu/mcheck/therm_throt.c +6 -0
+				goto error;
+		}
+	}
 
+	return 0;
+error:
+	sysfs_remove_group(&sys_dev->kobj, &thermal_attr_group);
 	return err;
 }
 

I'm fine with the above...

Quoted 25 lines
arch/x86/kernel/cpu/mcheck/therm_throt.c +8 -1
@@ -275,6 +289,7 @@ static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata >  static __init int thermal_throttle_init_device(void)
 {
 	unsigned int cpu = 0;
+	int i;
 	int err;
 
 	if (!atomic_read(&therm_throt_en))
@@ -288,13 +303,26 @@ static __init int thermal_throttle_init_device(void)
 	/* connect live CPUs to sysfs */
 	for_each_online_cpu(cpu) {
 		err = thermal_throttle_add_dev(get_cpu_sysdev(cpu));
-		WARN_ON(err);
+		if (err)
+			goto error;
 	}
 #ifdef CONFIG_HOTPLUG_CPU
 	mutex_unlock(&therm_cpu_lock);
 #endif
 
 	return 0;
+error:
+	WARN_ON(err);
+
+	/* cleanup. */
+	for (i = 0; i < cpu; i++)

arch/x86/kernel/cpu/mcheck/therm_throt.c static __init int thermal_throttle_init_device(void) · line 328

Quoted 10 lines
arch/x86/kernel/cpu/mcheck/therm_throt.c +7 -0
+		thermal_throttle_remove_dev(get_cpu_sysdev(i));
+#ifdef CONFIG_HOTPLUG_CPU
+	mutex_unlock(&therm_cpu_lock);
+#endif
+	unregister_hotcpu_notifier(&thermal_throttle_cpu_notifier);
+
+	return err;
 }
 device_initcall(thermal_throttle_init_device);
 

... but not with this!

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#2 lore raw
Quoted 14 lines · Jean Delvare
Quoted 6 lines · Fenghua Yu

From: Fenghua Yu <[email protected]>

When sysfs_add_file_to_group fails, thermal_throttle_add_dev removes the created group and returns with the error code and the driver cleans up and returns with the error code. Thus the driver either installs all devices successfully or doesn't install any device at all.

I don't think this makes any sense. While I generally agree with the idea that a given device (actually, CPU feature) should either be fully available or not available at all, I don't get the point of preventing the driver from loading because one device couldn't be initialized for whatever reason. I don't know of any other driver behaving this way.

Quoted 4 lines

What's the rationale? I think Ingo's wording was inaccurate and when he wrote "we should either initialize a driver fully - or not intialize it at all" he really meant "device" not "driver. Ingo?

Question is what happens if an error is returned from device initialization. If it causes the driver not to be loaded, it will have to clean up first. To avoid that, it would have to drop the error from individual device initializations, like it did before.

As such, it really comes down to philosophy and personal preference. Mine would be to return the error and fail driver installation (after all, something must really be wrong for that to happen), but then philosopy isn't really my field ... so I'll yield to others.

Guenter

Quoted 79 lines
Quoted 37 lines
---
 arch/x86/kernel/cpu/mcheck/therm_throt.c |   36 ++++++++++++++++++++++++++---
 1 files changed, 32 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/mcheck/therm_throt.c +11 -3
index c2a8b26..5099e90 100644
@@ -211,19 +211,33 @@ static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev)
 	if (err)
 		return err;
 
-	if (cpu_has(c, X86_FEATURE_PLN))
+	if (cpu_has(c, X86_FEATURE_PLN)) {
 		err = sysfs_add_file_to_group(&sys_dev->kobj,
 					      &attr_core_power_limit_count.attr,
 					      thermal_attr_group.name);
-	if (cpu_has(c, X86_FEATURE_PTS))
+		if (err)
+			goto error;
+	}
+
+	if (cpu_has(c, X86_FEATURE_PTS)) {
 		err = sysfs_add_file_to_group(&sys_dev->kobj,
 					      &attr_package_throttle_count.attr,
 					      thermal_attr_group.name);
-		if (cpu_has(c, X86_FEATURE_PLN))
+		if (err)
+			goto error;
+
+		if (cpu_has(c, X86_FEATURE_PLN)) {
 			err = sysfs_add_file_to_group(&sys_dev->kobj,
 					&attr_package_power_limit_count.attr,
 					thermal_attr_group.name);
+			if (err)

arch/x86/kernel/cpu/mcheck/therm_throt.c static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev) · line 243

Quoted 10 lines
arch/x86/kernel/cpu/mcheck/therm_throt.c +6 -0
+				goto error;
+		}
+	}
 
+	return 0;
+error:
+	sysfs_remove_group(&sys_dev->kobj, &thermal_attr_group);
 	return err;
 }
 

I'm fine with the above...

Quoted 29 lines
arch/x86/kernel/cpu/mcheck/therm_throt.c +12 -1
@@ -275,6 +289,7 @@ static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata > >  static __init int thermal_throttle_init_device(void)
 {
 	unsigned int cpu = 0;
+	int i;
 	int err;
 
 	if (!atomic_read(&therm_throt_en))
@@ -288,13 +303,26 @@ static __init int thermal_throttle_init_device(void)
 	/* connect live CPUs to sysfs */
 	for_each_online_cpu(cpu) {
 		err = thermal_throttle_add_dev(get_cpu_sysdev(cpu));
-		WARN_ON(err);
+		if (err)
+			goto error;
 	}
 #ifdef CONFIG_HOTPLUG_CPU
 	mutex_unlock(&therm_cpu_lock);
 #endif
 
 	return 0;
+error:
+	WARN_ON(err);
+
+	/* cleanup. */
+	for (i = 0; i < cpu; i++)
+		thermal_throttle_remove_dev(get_cpu_sysdev(i));
+#ifdef CONFIG_HOTPLUG_CPU
+	mutex_unlock(&therm_cpu_lock);
+#endif
Quoted 11 lines

arch/x86/kernel/cpu/mcheck/therm_throt.c static __init int thermal_throttle_init_device(void) · line 328

Quoted 6 lines
arch/x86/kernel/cpu/mcheck/therm_throt.c +3 -0
+	unregister_hotcpu_notifier(&thermal_throttle_cpu_notifier);
+
+	return err;
 }
 device_initcall(thermal_throttle_init_device);
 

... but not with this!

Signature · 1 line
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#3 lore raw

Hi Guenter,

Quoted 24 lines · Guenter Roeck
Quoted 14 lines · Jean Delvare
Quoted 6 lines · Fenghua Yu

From: Fenghua Yu <[email protected]>

When sysfs_add_file_to_group fails, thermal_throttle_add_dev removes the created group and returns with the error code and the driver cleans up and returns with the error code. Thus the driver either installs all devices successfully or doesn't install any device at all.

I don't think this makes any sense. While I generally agree with the idea that a given device (actually, CPU feature) should either be fully available or not available at all, I don't get the point of preventing the driver from loading because one device couldn't be initialized for whatever reason. I don't know of any other driver behaving this way.

Quoted 3 lines

What's the rationale? I think Ingo's wording was inaccurate and when he wrote "we should either initialize a driver fully - or not intialize it at all" he really meant "device" not "driver. Ingo?

Question is what happens if an error is returned from device initialization. If it causes the driver not to be loaded, it will have to clean up first. To avoid that, it would have to drop the error from individual device initializations, like it did before.

Quoted 4 lines

As such, it really comes down to philosophy and personal preference. Mine would be to return the error and fail driver installation (after all, something must really be wrong for that to happen), but then philosopy isn't really my field ... so I'll yield to others.

I beg to disagree. The Linux 2.6 device driver model focuses on clear separation between device instantiation and driver initialization. When you properly stick to the model, both steps are totally separate, and even happen in different modules, so device instantiation and initialization can have no effect on the driver, which can even pre-exist.

The code we have here is different because there's no device instances being created, and no device driver binding taking place. But following the spirit of the model, device initialization should still not affect the success of driver loading.

Note BTW: Fenghua's patch will have to be rebased, as the following fix went in the way meanwhile:
http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commitdiff;hQe3c1b558b31b11bf5fc66d3c6f5adacf3573f7

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#4 lore raw
In reply to Fenghua Yu
Quoted 7 lines · Fenghua Yu

From: Fenghua Yu <[email protected]>

When sysfs_add_file_to_group fails, thermal_throttle_add_dev removes the created group and returns with the error code and the driver cleans up and returns with the error code. Thus the driver either installs all devices successfully or doesn't install any device at all.

Quoted 5 lines
---
 arch/x86/kernel/cpu/mcheck/therm_throt.c |   36 ++++++++++++++++++++++++++---
 1 files changed, 32 insertions(+), 4 deletions(-)

Ingo, Fenghua, what happened to this patch? There was so much discussion about it about the details that in the end nothing was done and the actual bug in the code (missing curly braces) is still not fixed while kernel 2.6.36 is about to be released. This is no good.

The original fix for the main bug was sent by Jin Dongming on August 26th, 2010:
http://lkml.org/lkml/2010/8/26/67

If we can't agree on how to handle errors, at least this easy patch (which fixes the _success_ path) should be applied immediately.

Quoted 76 lines
arch/x86/kernel/cpu/mcheck/therm_throt.c +32 -4
index c2a8b26..5099e90 100644
@@ -211,19 +211,33 @@ static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev)
 	if (err)
 		return err;
 
-	if (cpu_has(c, X86_FEATURE_PLN))
+	if (cpu_has(c, X86_FEATURE_PLN)) {
 		err = sysfs_add_file_to_group(&sys_dev->kobj,
 					      &attr_core_power_limit_count.attr,
 					      thermal_attr_group.name);
-	if (cpu_has(c, X86_FEATURE_PTS))
+		if (err)
+			goto error;
+	}
+
+	if (cpu_has(c, X86_FEATURE_PTS)) {
 		err = sysfs_add_file_to_group(&sys_dev->kobj,
 					      &attr_package_throttle_count.attr,
 					      thermal_attr_group.name);
-		if (cpu_has(c, X86_FEATURE_PLN))
+		if (err)
+			goto error;
+
+		if (cpu_has(c, X86_FEATURE_PLN)) {
 			err = sysfs_add_file_to_group(&sys_dev->kobj,
 					&attr_package_power_limit_count.attr,
 					thermal_attr_group.name);
+			if (err)
+				goto error;
+		}
+	}
 
+	return 0;
+error:
+	sysfs_remove_group(&sys_dev->kobj, &thermal_attr_group);
 	return err;
 }
 
@@ -275,6 +289,7 @@ static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata >  static __init int thermal_throttle_init_device(void)
 {
 	unsigned int cpu = 0;
+	int i;
 	int err;
 
 	if (!atomic_read(&therm_throt_en))
@@ -288,13 +303,26 @@ static __init int thermal_throttle_init_device(void)
 	/* connect live CPUs to sysfs */
 	for_each_online_cpu(cpu) {
 		err = thermal_throttle_add_dev(get_cpu_sysdev(cpu));
-		WARN_ON(err);
+		if (err)
+			goto error;
 	}
 #ifdef CONFIG_HOTPLUG_CPU
 	mutex_unlock(&therm_cpu_lock);
 #endif
 
 	return 0;
+error:
+	WARN_ON(err);
+
+	/* cleanup. */
+	for (i = 0; i < cpu; i++)
+		thermal_throttle_remove_dev(get_cpu_sysdev(i));
+#ifdef CONFIG_HOTPLUG_CPU
+	mutex_unlock(&therm_cpu_lock);
+#endif
+	unregister_hotcpu_notifier(&thermal_throttle_cpu_notifier);
+
+	return err;
 }
 device_initcall(thermal_throttle_init_device);
 
Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#5 lore raw
In reply to Fenghua Yu

arch/x86/kernel/cpu/mcheck/therm_throt.c static __init int thermal_throttle_init_device(void) · line 322

Quoted 7 lines · Fenghua Yu

#ifdef CONFIG_HOTPLUG_CPU

arch/x86/kernel/cpu/mcheck/therm_throt.c +3 -0
 	mutex_unlock(&therm_cpu_lock);
 #endif
 
+#ifdef CONFIG_HOTPLUG_CPU
+	mutex_unlock(&therm_cpu_lock);
+#endif

That's all very ugly.

Thanks,

Ingo

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors