Skip to contentSkip to conversation

char: xilinx_hwicap: unregister class on init errors

PATCH In review Myeonghun Pak 2 participants 2 messages Jun 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 char: xilinx_hwicap: unregister class on init errors

hwicap_module_init() registers icap_class before reserving the character-device region and registering the platform driver. If either of those later steps fails, the init path must undo the successful class registration before returning an error.

Route the chrdev registration failure through a class unwind label, and let the platform-driver registration failure fall through the existing chrdev unwind before unregistering the class. The normal module exit path is unchanged.

This issue was identified during our ongoing static-analysis research while reviewing kernel code.

Fixes ef141a0bb0dc ("[POWERPC] Xilinx: hwicap driver")
---
 drivers/char/xilinx_hwicap/xilinx_hwicap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/char/xilinx_hwicap/xilinx_hwicap.c +4 -1
index 34a345dc5e..9bb5fa642f 100644
@@ -760,7 +760,7 @@ static int __init hwicap_module_init(void)
 					HWICAP_DEVICES,
 					DRIVER_NAME);
 	if (retval < 0)
-		return retval;
+		goto failed_class;
 
 	retval = platform_driver_register(&hwicap_platform_driver);
 	if (retval)
@@ -771,6 +771,9 @@ static int __init hwicap_module_init(void)
  failed:
 	unregister_chrdev_region(devt, HWICAP_DEVICES);
 
+ failed_class:
+	class_unregister(&icap_class);
+
 	return retval;
 }
 
Signature · 1 line
2.47.1
Reviewed-by Radhey Shyam Pandey on v1 1/1
#1 lore raw
Quoted 13 lines

hwicap_module_init() registers icap_class before reserving the character-device region and registering the platform driver. If either of those later steps fails, the init path must undo the successful class registration before returning an error.

Route the chrdev registration failure through a class unwind label, and let the platform-driver registration failure fall through the existing chrdev unwind before unregistering the class. The normal module exit path is unchanged.

This issue was identified during our ongoing static-analysis research while reviewing kernel code.

Quoted 6 lines
Fixes ef141a0bb0dc ("[POWERPC] Xilinx: hwicap driver")

---

Thanks!

Quoted 26 lines
  drivers/char/xilinx_hwicap/xilinx_hwicap.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/char/xilinx_hwicap/xilinx_hwicap.c +4 -1
index 34a345dc5e..9bb5fa642f 100644
@@ -760,7 +760,7 @@ static int __init hwicap_module_init(void)
  					HWICAP_DEVICES,
  					DRIVER_NAME);
  	if (retval < 0)
-		return retval;
+		goto failed_class;
 
  	retval = platform_driver_register(&hwicap_platform_driver);
  	if (retval)
@@ -771,6 +771,9 @@ static int __init hwicap_module_init(void)
   failed:
  	unregister_chrdev_region(devt, HWICAP_DEVICES);
 
+ failed_class:
+	class_unregister(&icap_class);
+
  	return retval;
  }