[PATCH v2 5/6] staging: pi433: Remove duplicated code using the "goto" error recovery scheme.

Shahar Avidar posted 6 patches 1 year, 10 months ago
[PATCH v2 5/6] staging: pi433: Remove duplicated code using the "goto" error recovery scheme.
Posted by Shahar Avidar 1 year, 10 months ago
pi433_init had "unregister_chrdev" called twice.
Remove it using goto statements.

Signed-off-by: Shahar Avidar <ikobh7@gmail.com>
---
v2->v1:
Followed by Dan Carpenter's <dan.carpenter@linaro.org> comments:
        - Remove empty "fail" goto tag.
        - Reorder pi433 init & exit calls so they have reverse order.
        - Add "unreg_spi_drv" goto tag.
        - Check "debugfs_create_dir" return value.
        - Update "if" statments for consistency.

 drivers/staging/pi433/pi433_if.c | 33 +++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)

diff --git a/drivers/staging/pi433/pi433_if.c b/drivers/staging/pi433/pi433_if.c
index 62ce75b07bf0..47e0c51ffe2e 100644
--- a/drivers/staging/pi433/pi433_if.c
+++ b/drivers/staging/pi433/pi433_if.c
@@ -1399,25 +1399,32 @@ static int __init pi433_init(void)
 	 * Last, register the driver which manages those device numbers.
 	 */
 	status = alloc_chrdev_region(&pi433_devt, 0, N_PI433_MINORS, "pi433");
-	if (status < 0)
+	if (status)
 		return status;
 
 	status = class_register(&pi433_class);
-	if (status) {
-		unregister_chrdev(MAJOR(pi433_devt),
-				  pi433_spi_driver.driver.name);
-		return status;
-	}
-
-	root_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
+	if (status)
+		goto unreg_chrdev;
 
 	status = spi_register_driver(&pi433_spi_driver);
-	if (status < 0) {
-		class_unregister(&pi433_class);
-		unregister_chrdev(MAJOR(pi433_devt),
-				  pi433_spi_driver.driver.name);
+	if (status)
+		goto unreg_class;
+
+	root_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
+	if (IS_ERR(root_dir)) {
+		status = PTR_ERR(root_dir);
+		goto unreg_spi_drv;
 	}
 
+	return 0;
+
+unreg_spi_drv:
+	spi_unregister_driver(&pi433_spi_driver);
+unreg_class:
+	class_unregister(&pi433_class);
+unreg_chrdev:
+	unregister_chrdev(MAJOR(pi433_devt), pi433_spi_driver.driver.name);
+
 	return status;
 }
 
@@ -1425,10 +1432,10 @@ module_init(pi433_init);
 
 static void __exit pi433_exit(void)
 {
+	debugfs_remove(root_dir);
 	spi_unregister_driver(&pi433_spi_driver);
 	class_unregister(&pi433_class);
 	unregister_chrdev(MAJOR(pi433_devt), pi433_spi_driver.driver.name);
-	debugfs_remove(root_dir);
 }
 module_exit(pi433_exit);
 
-- 
2.34.1
Re: [PATCH v2 5/6] staging: pi433: Remove duplicated code using the "goto" error recovery scheme.
Posted by Dan Carpenter 1 year, 10 months ago
On Tue, Apr 02, 2024 at 02:14:13PM +0300, Shahar Avidar wrote:
> pi433_init had "unregister_chrdev" called twice.
> Remove it using goto statements.
> 
> Signed-off-by: Shahar Avidar <ikobh7@gmail.com>

Your commit message needs to mention all the other changes you made,
especially the bug fix.  I feel like you're doing too many changes
really.  Just use the gotos to clean up but don't re-order the calls.

You need to add a Fixes tag.

> ---
> v2->v1:
> Followed by Dan Carpenter's <dan.carpenter@linaro.org> comments:
>         - Remove empty "fail" goto tag.
>         - Reorder pi433 init & exit calls so they have reverse order.

Keep the init calls the same.  Re-order the exit calls.  It's a bit of
a gray area if you can re-order the exit calls in this same patch.  Do
it in a separate patch just to be safe.

>         - Add "unreg_spi_drv" goto tag.
>         - Check "debugfs_create_dir" return value.

No.  This is wrong.  Debugfs function are not supposed to be checked.
https://staticthinking.wordpress.com/2023/07/24/debugfs-functions-are-not-supposed-to-be-checked/

>         - Update "if" statments for consistency.

I like this cleanup, but it's not worth the effort.  Just leave it
as-is unless you really really need to change it.  Do it in a separate
patch.

What qualifies as One Thing Per Patch is a bit of gray area.  Greg tends
to want things split up into tiny patches.  You have to know your
maintainer.

regards,
dan carpenter
Re: [PATCH v2 5/6] staging: pi433: Remove duplicated code using the "goto" error recovery scheme.
Posted by Shahar Avidar 1 year, 10 months ago
On 02/04/2024 19:46, Dan Carpenter wrote:
> On Tue, Apr 02, 2024 at 02:14:13PM +0300, Shahar Avidar wrote:
>> pi433_init had "unregister_chrdev" called twice.
>> Remove it using goto statements.
>>
>> Signed-off-by: Shahar Avidar <ikobh7@gmail.com>
> 
> Your commit message needs to mention all the other changes you made,
> especially the bug fix.  I feel like you're doing too many changes
> really.  Just use the gotos to clean up but don't re-order the calls.
Thanks again for your inputs.
I've reverted some of the changes & split the 5th patch.

> 
> You need to add a Fixes tag.
> I've added the "Fixes" tag, so as the "Reported-by" tag, naming you.

>> ---
>> v2->v1:
>> Followed by Dan Carpenter's <dan.carpenter@linaro.org> comments:
>>          - Remove empty "fail" goto tag.
>>          - Reorder pi433 init & exit calls so they have reverse order.
> 
> Keep the init calls the same.  Re-order the exit calls.  It's a bit of
> a gray area if you can re-order the exit calls in this same patch.  Do
> it in a separate patch just to be safe.
> 
>>          - Add "unreg_spi_drv" goto tag.
>>          - Check "debugfs_create_dir" return value.
> 
> No.  This is wrong.  Debugfs function are not supposed to be checked.
> https://staticthinking.wordpress.com/2023/07/24/debugfs-functions-are-not-supposed-to-be-checked >
Great blog post again. I'm learning.

>>          - Update "if" statments for consistency.
> 
> I like this cleanup, but it's not worth the effort.  Just leave it
> as-is unless you really really need to change it.  Do it in a separate
> patch.
Reverted.

> 
> What qualifies as One Thing Per Patch is a bit of gray area.  Greg tends
> to want things split up into tiny patches.  You have to know your
> maintainer.
> 
> regards,
> dan carpenter
> 
-- 
Regards,

Shahar