Skip to contentSkip to conversation

thermal/drivers/core: Use put_device() if device_register() fails

PATCH RESEND Superseded Yue Hu 2 participants 3 messages Aug 6, 2019 – Aug 7, 2019 Thermal

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yue Hu

author
#0 lore raw

PATCH RESEND 1/1 thermal/drivers/core: Use put_device() if device_register() fails

From: Yue Hu <[email protected]>

Never directly free @dev after calling device_register(), even if it returned an error! Always use put_device() to give up the reference initialized.

---
 drivers/thermal/thermal_core.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/thermal/thermal_core.c +7 -4
index 6bab66e..ffe5d8e 100644
@@ -985,7 +985,7 @@ static void bind_cdev(struct thermal_cooling_device *cdev)
 	result = device_register(&cdev->device);
 	if (result) {
 		ida_simple_remove(&thermal_cdev_ida, cdev->id);
-		kfree(cdev);
+		put_device(&cdev->device);
 		return ERR_PTR(result);
 	}
 
@@ -1292,7 +1292,7 @@ struct thermal_zone_device *
 	dev_set_name(&tz->device, "thermal_zone%d", tz->id);
 	result = device_register(&tz->device);
 	if (result)
-		goto remove_device_groups;
+		goto release_device;
 
 	for (count = 0; count < trips; count++) {
 		if (tz->ops->get_trip_type(tz, count, &trip_type))
@@ -1347,8 +1347,11 @@ struct thermal_zone_device *
 	device_unregister(&tz->device);
 	return ERR_PTR(result);
 
-remove_device_groups:
-	thermal_zone_destroy_device_groups(tz);
+release_device:
+	ida_simple_remove(&thermal_tz_ida, tz->id);
+	put_device(&tz->device);
+	return ERR_PTR(result);
+
 remove_id:
 	ida_simple_remove(&thermal_tz_ida, tz->id);
 free_tz:
Signature · 1 line
1.9.1
#1 lore raw
Quoted 8 lines · Yue Hu

From: Yue Hu <[email protected]>

Never directly free @dev after calling device_register(), even if it returned an error! Always use put_device() to give up the reference initialized.

---

Thanks for catching this. Instead of adding a new rollback block, can you do a cleanup in the rollback routine and then add this patch with the proper label at the end of the function?

eg.

unregister:

	device_unregister(&tz->device);
release_device:
	put_device(&tz->device);

...

remove_id:

	ida_simple_remove(&thermal_tz_ida, tz->id);
free_tz:
	kfree(tz);
	return ERR_PTR(result);
Quoted 40 lines
 drivers/thermal/thermal_core.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/thermal/thermal_core.c +7 -4
index 6bab66e..ffe5d8e 100644
@@ -985,7 +985,7 @@ static void bind_cdev(struct thermal_cooling_device *cdev)
 	result = device_register(&cdev->device);
 	if (result) {
 		ida_simple_remove(&thermal_cdev_ida, cdev->id);
-		kfree(cdev);
+		put_device(&cdev->device);
 		return ERR_PTR(result);
 	}
 
@@ -1292,7 +1292,7 @@ struct thermal_zone_device *
 	dev_set_name(&tz->device, "thermal_zone%d", tz->id);
 	result = device_register(&tz->device);
 	if (result)
-		goto remove_device_groups;
+		goto release_device;
 
 	for (count = 0; count < trips; count++) {
 		if (tz->ops->get_trip_type(tz, count, &trip_type))
@@ -1347,8 +1347,11 @@ struct thermal_zone_device *
 	device_unregister(&tz->device);
 	return ERR_PTR(result);
 
-remove_device_groups:
-	thermal_zone_destroy_device_groups(tz);
+release_device:
+	ida_simple_remove(&thermal_tz_ida, tz->id);
+	put_device(&tz->device);
+	return ERR_PTR(result);
+
 remove_id:
 	ida_simple_remove(&thermal_tz_ida, tz->id);
 free_tz:
Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
author
#2 lore raw
Quoted 7 lines · On Tue, 6 Aug 2019 12:26:18 +0200 Daniel Lezcano <[email protected]>…
Quoted 6 lines · Yue Hu

From: Yue Hu <[email protected]>

Never directly free @dev after calling device_register(), even if it returned an error! Always use put_device() to give up the reference initialized.

Quoted 6 lines
Quoted 2 lines

---

Thanks for catching this. Instead of adding a new rollback block, can you do a cleanup in the rollback routine and then add this patch with the proper label at the end of the function?

Ok, i will try to clean up the rollback code in the v2.

Thank you.

Quoted 58 lines

eg.

unregister:

	device_unregister(&tz->device);
release_device:
	put_device(&tz->device);

...

remove_id:

	ida_simple_remove(&thermal_tz_ida, tz->id);
free_tz:
	kfree(tz);
	return ERR_PTR(result);
Quoted 40 lines
 drivers/thermal/thermal_core.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/thermal/thermal_core.c +7 -4
index 6bab66e..ffe5d8e 100644
@@ -985,7 +985,7 @@ static void bind_cdev(struct thermal_cooling_device *cdev)
 	result = device_register(&cdev->device);
 	if (result) {
 		ida_simple_remove(&thermal_cdev_ida, cdev->id);
-		kfree(cdev);
+		put_device(&cdev->device);
 		return ERR_PTR(result);
 	}
 
@@ -1292,7 +1292,7 @@ struct thermal_zone_device *
 	dev_set_name(&tz->device, "thermal_zone%d", tz->id);
 	result = device_register(&tz->device);
 	if (result)
-		goto remove_device_groups;
+		goto release_device;
 
 	for (count = 0; count < trips; count++) {
 		if (tz->ops->get_trip_type(tz, count, &trip_type))
@@ -1347,8 +1347,11 @@ struct thermal_zone_device *
 	device_unregister(&tz->device);
 	return ERR_PTR(result);
 
-remove_device_groups:
-	thermal_zone_destroy_device_groups(tz);
+release_device:
+	ida_simple_remove(&thermal_tz_ida, tz->id);
+	put_device(&tz->device);
+	return ERR_PTR(result);
+
 remove_id:
 	ida_simple_remove(&thermal_tz_ida, tz->id);
 free_tz: