diff mbox series

net: thunderbolt: Fix error handling in tbnet_init()

Message ID 20221114081936.35804-1-yuancan@huawei.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: thunderbolt: Fix error handling in tbnet_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: 18 this patch: 18
netdev/cc_maintainers success CCed 10 of 10 maintainers
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: 18 this patch: 18
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 15 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. 14, 2022, 8:19 a.m. UTC
A problem about insmod thunderbolt-net failed is triggered with following
log given while lsmod does not show thunderbolt_net:

 insmod: ERROR: could not insert module thunderbolt-net.ko: File exists

The reason is that tbnet_init() returns tb_register_service_driver()
directly without checking its return value, if tb_register_service_driver()
failed, it returns without removing property directory, resulting the
property directory can never be created later.

 tbnet_init()
   tb_register_property_dir() # register property directory
   tb_register_service_driver()
     driver_register()
       bus_add_driver()
         priv = kzalloc(...) # OOM happened
   # return without remove property directory

Fix by remove property directory when tb_register_service_driver() returns
error.

Fixes: e69b6c02b4c3 ("net: Add support for networking over Thunderbolt cable")
Signed-off-by: Yuan Can <yuancan@huawei.com>
---
 drivers/net/thunderbolt.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

Comments

Mika Westerberg Nov. 14, 2022, 10:09 a.m. UTC | #1
Hi,

On Mon, Nov 14, 2022 at 08:19:36AM +0000, Yuan Can wrote:
> A problem about insmod thunderbolt-net failed is triggered with following
> log given while lsmod does not show thunderbolt_net:
> 
>  insmod: ERROR: could not insert module thunderbolt-net.ko: File exists
> 
> The reason is that tbnet_init() returns tb_register_service_driver()
> directly without checking its return value, if tb_register_service_driver()
> failed, it returns without removing property directory, resulting the
> property directory can never be created later.
> 
>  tbnet_init()
>    tb_register_property_dir() # register property directory
>    tb_register_service_driver()
>      driver_register()
>        bus_add_driver()
>          priv = kzalloc(...) # OOM happened
>    # return without remove property directory
> 
> Fix by remove property directory when tb_register_service_driver() returns
> error.
> 
> Fixes: e69b6c02b4c3 ("net: Add support for networking over Thunderbolt cable")
> Signed-off-by: Yuan Can <yuancan@huawei.com>
> ---
>  drivers/net/thunderbolt.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/thunderbolt.c b/drivers/net/thunderbolt.c
> index 83fcaeb2ac5e..fe6a9881cc75 100644
> --- a/drivers/net/thunderbolt.c
> +++ b/drivers/net/thunderbolt.c
> @@ -1396,7 +1396,14 @@ static int __init tbnet_init(void)
>  		return ret;
>  	}
>  
> -	return tb_register_service_driver(&tbnet_driver);
> +	ret = tb_register_service_driver(&tbnet_driver);
> +	if (ret) {
> +		tb_unregister_property_dir("network", tbnet_dir);
> +		tb_property_free_dir(tbnet_dir);
> +		return ret;
> +	}
> +
> +	return 0;

Okay but I suggest that you make it like:

	ret = tb_register_property_dir("network", tbnet_dir);
	if (ret)
		goto err_free_dir;

	ret = tb_register_service_driver(&tbnet_driver);
	if (ret)
		goto err_unregister;

	return 0;

err_unregister:
	tb_unregister_property_dir("network", tbnet_dir);
err_free_dir:
	tb_property_free_dir(tbnet_dir);

	return ret;

}
Yuan Can Nov. 14, 2022, 12:06 p.m. UTC | #2
在 2022/11/14 18:09, Mika Westerberg 写道:
> Hi,
>
> On Mon, Nov 14, 2022 at 08:19:36AM +0000, Yuan Can wrote:
>> A problem about insmod thunderbolt-net failed is triggered with following
>> log given while lsmod does not show thunderbolt_net:
>>
>>   insmod: ERROR: could not insert module thunderbolt-net.ko: File exists
>>
>> The reason is that tbnet_init() returns tb_register_service_driver()
>> directly without checking its return value, if tb_register_service_driver()
>> failed, it returns without removing property directory, resulting the
>> property directory can never be created later.
>>
>>   tbnet_init()
>>     tb_register_property_dir() # register property directory
>>     tb_register_service_driver()
>>       driver_register()
>>         bus_add_driver()
>>           priv = kzalloc(...) # OOM happened
>>     # return without remove property directory
>>
>> Fix by remove property directory when tb_register_service_driver() returns
>> error.
>>
>> Fixes: e69b6c02b4c3 ("net: Add support for networking over Thunderbolt cable")
>> Signed-off-by: Yuan Can <yuancan@huawei.com>
>> ---
>>   drivers/net/thunderbolt.c | 9 ++++++++-
>>   1 file changed, 8 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/net/thunderbolt.c b/drivers/net/thunderbolt.c
>> index 83fcaeb2ac5e..fe6a9881cc75 100644
>> --- a/drivers/net/thunderbolt.c
>> +++ b/drivers/net/thunderbolt.c
>> @@ -1396,7 +1396,14 @@ static int __init tbnet_init(void)
>>   		return ret;
>>   	}
>>   
>> -	return tb_register_service_driver(&tbnet_driver);
>> +	ret = tb_register_service_driver(&tbnet_driver);
>> +	if (ret) {
>> +		tb_unregister_property_dir("network", tbnet_dir);
>> +		tb_property_free_dir(tbnet_dir);
>> +		return ret;
>> +	}
>> +
>> +	return 0;
> Okay but I suggest that you make it like:
>
> 	ret = tb_register_property_dir("network", tbnet_dir);
> 	if (ret)
> 		goto err_free_dir;
>
> 	ret = tb_register_service_driver(&tbnet_driver);
> 	if (ret)
> 		goto err_unregister;
>
> 	return 0;
>
> err_unregister:
> 	tb_unregister_property_dir("network", tbnet_dir);
> err_free_dir:
> 	tb_property_free_dir(tbnet_dir);
>
> 	return ret;
>
> }
Ok, thanks for the suggestion! I will switch to this style in the v2 patch.
diff mbox series

Patch

diff --git a/drivers/net/thunderbolt.c b/drivers/net/thunderbolt.c
index 83fcaeb2ac5e..fe6a9881cc75 100644
--- a/drivers/net/thunderbolt.c
+++ b/drivers/net/thunderbolt.c
@@ -1396,7 +1396,14 @@  static int __init tbnet_init(void)
 		return ret;
 	}
 
-	return tb_register_service_driver(&tbnet_driver);
+	ret = tb_register_service_driver(&tbnet_driver);
+	if (ret) {
+		tb_unregister_property_dir("network", tbnet_dir);
+		tb_property_free_dir(tbnet_dir);
+		return ret;
+	}
+
+	return 0;
 }
 module_init(tbnet_init);