diff mbox series

net: hinic: Fix error handling in hinic_module_init()

Message ID 20221109112315.125135-1-yuancan@huawei.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: hinic: Fix error handling in hinic_module_init() | expand

Checks

Context Check Description
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1 this patch: 1
netdev/cc_maintainers warning 1 maintainers not CCed: cai.huoqing@linux.dev
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 1 this patch: 1
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 18 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Yuan Can Nov. 9, 2022, 11:23 a.m. UTC
A problem about hinic create debugfs failed is triggered with the
following log given:

 [  931.419023] debugfs: Directory 'hinic' with parent '/' already present!

The reason is that hinic_module_init() returns pci_register_driver()
directly without checking its return value, if pci_register_driver()
failed, it returns without destroy the newly created debugfs, resulting
the debugfs of hinic can never be created later.

 hinic_module_init()
   hinic_dbg_register_debugfs() # create debugfs directory
   pci_register_driver()
     driver_register()
       bus_add_driver()
         priv = kzalloc(...) # OOM happened
   # return without destroy debugfs directory

Fix by removing debugfs when pci_register_driver() returns error.

Fixes: 253ac3a97921 ("hinic: add support to query sq info")
Signed-off-by: Yuan Can <yuancan@huawei.com>
---
 drivers/net/ethernet/huawei/hinic/hinic_main.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

Comments

Francois Romieu Nov. 9, 2022, 5:56 p.m. UTC | #1
Yuan Can <yuancan@huawei.com> :
[...]
> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_main.c b/drivers/net/ethernet/huawei/hinic/hinic_main.c
> index e1f54a2f28b2..b2fcd83d58fa 100644
> --- a/drivers/net/ethernet/huawei/hinic/hinic_main.c
> +++ b/drivers/net/ethernet/huawei/hinic/hinic_main.c
> @@ -1474,8 +1474,17 @@ static struct pci_driver hinic_driver = {
>  
>  static int __init hinic_module_init(void)
>  {
> +	int ret;
> +
>  	hinic_dbg_register_debugfs(HINIC_DRV_NAME);
> -	return pci_register_driver(&hinic_driver);
> +
> +	ret = pci_register_driver(&hinic_driver);
> +	if (ret) {
> +		hinic_dbg_unregister_debugfs();
> +		return ret;
> +	}
> +
> +	return 0;
>  }

You can remove some fat:

	ret = pci_register_driver(&hinic_driver);
	if (ret)
		hinic_dbg_unregister_debugfs();

	return ret;
Yuan Can Nov. 10, 2022, 2 a.m. UTC | #2
在 2022/11/10 1:56, Francois Romieu 写道:
> Yuan Can <yuancan@huawei.com> :
> [...]
>> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_main.c b/drivers/net/ethernet/huawei/hinic/hinic_main.c
>> index e1f54a2f28b2..b2fcd83d58fa 100644
>> --- a/drivers/net/ethernet/huawei/hinic/hinic_main.c
>> +++ b/drivers/net/ethernet/huawei/hinic/hinic_main.c
>> @@ -1474,8 +1474,17 @@ static struct pci_driver hinic_driver = {
>>   
>>   static int __init hinic_module_init(void)
>>   {
>> +	int ret;
>> +
>>   	hinic_dbg_register_debugfs(HINIC_DRV_NAME);
>> -	return pci_register_driver(&hinic_driver);
>> +
>> +	ret = pci_register_driver(&hinic_driver);
>> +	if (ret) {
>> +		hinic_dbg_unregister_debugfs();
>> +		return ret;
>> +	}
>> +
>> +	return 0;
>>   }
> You can remove some fat:
>
> 	ret = pci_register_driver(&hinic_driver);
> 	if (ret)
> 		hinic_dbg_unregister_debugfs();
>
> 	return ret;
Thanks for the suggestion! I will change to this style.
diff mbox series

Patch

diff --git a/drivers/net/ethernet/huawei/hinic/hinic_main.c b/drivers/net/ethernet/huawei/hinic/hinic_main.c
index e1f54a2f28b2..b2fcd83d58fa 100644
--- a/drivers/net/ethernet/huawei/hinic/hinic_main.c
+++ b/drivers/net/ethernet/huawei/hinic/hinic_main.c
@@ -1474,8 +1474,17 @@  static struct pci_driver hinic_driver = {
 
 static int __init hinic_module_init(void)
 {
+	int ret;
+
 	hinic_dbg_register_debugfs(HINIC_DRV_NAME);
-	return pci_register_driver(&hinic_driver);
+
+	ret = pci_register_driver(&hinic_driver);
+	if (ret) {
+		hinic_dbg_unregister_debugfs();
+		return ret;
+	}
+
+	return 0;
 }
 
 static void __exit hinic_module_exit(void)