netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] hsr: Handle failures in module init
@ 2024-03-14 10:10 Felix Maurer
  2024-03-14 10:50 ` Denis Kirjanov
  2024-03-14 12:59 ` Breno Leitao
  0 siblings, 2 replies; 5+ messages in thread
From: Felix Maurer @ 2024-03-14 10:10 UTC (permalink / raw)
  To: netdev; +Cc: davem, edumazet, kuba, pabeni, leitao

A failure during registration of the netdev notifier was not handled at
all. A failure during netlink initialization did not unregister the netdev
notifier.

Handle failures of netdev notifier registration and netlink initialization.
Both functions should only return negative values on failure and thereby
lead to the hsr module not being loaded.

Signed-off-by: Felix Maurer <fmaurer@redhat.com>
---
 net/hsr/hsr_main.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/net/hsr/hsr_main.c b/net/hsr/hsr_main.c
index cb83c8feb746..1c4a5b678688 100644
--- a/net/hsr/hsr_main.c
+++ b/net/hsr/hsr_main.c
@@ -148,14 +148,24 @@ static struct notifier_block hsr_nb = {
 
 static int __init hsr_init(void)
 {
-	int res;
+	int err;
 
 	BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
 
-	register_netdevice_notifier(&hsr_nb);
-	res = hsr_netlink_init();
+	err = register_netdevice_notifier(&hsr_nb);
+	if (err)
+		goto out;
+
+	err = hsr_netlink_init();
+	if (err)
+		goto cleanup;
 
-	return res;
+	return 0;
+
+cleanup:
+	unregister_netdevice_notifier(&hsr_nb);
+out:
+	return err;
 }
 
 static void __exit hsr_exit(void)
-- 
2.44.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH net] hsr: Handle failures in module init
  2024-03-14 10:10 [PATCH net] hsr: Handle failures in module init Felix Maurer
@ 2024-03-14 10:50 ` Denis Kirjanov
  2024-03-14 12:59 ` Breno Leitao
  1 sibling, 0 replies; 5+ messages in thread
From: Denis Kirjanov @ 2024-03-14 10:50 UTC (permalink / raw)
  To: Felix Maurer, netdev; +Cc: davem, edumazet, kuba, pabeni, leitao



On 3/14/24 13:10, Felix Maurer wrote:
> A failure during registration of the netdev notifier was not handled at
> all. A failure during netlink initialization did not unregister the netdev
> notifier.
> 
> Handle failures of netdev notifier registration and netlink initialization.
> Both functions should only return negative values on failure and thereby
> lead to the hsr module not being loaded.
> 
Fixes: f421436a591d3 ("net/hsr: Add support for the High-availability Seamless Redundancy protocol (HSRv0)")
> Signed-off-by: Felix Maurer <fmaurer@redhat.com>
> ---
>  net/hsr/hsr_main.c | 18 ++++++++++++++----
>  1 file changed, 14 insertions(+), 4 deletions(-)
> 
> diff --git a/net/hsr/hsr_main.c b/net/hsr/hsr_main.c
> index cb83c8feb746..1c4a5b678688 100644
> --- a/net/hsr/hsr_main.c
> +++ b/net/hsr/hsr_main.c
> @@ -148,14 +148,24 @@ static struct notifier_block hsr_nb = {
>  
>  static int __init hsr_init(void)
>  {
> -	int res;
> +	int err;
>  
>  	BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
>  
> -	register_netdevice_notifier(&hsr_nb);
> -	res = hsr_netlink_init();
> +	err = register_netdevice_notifier(&hsr_nb);
> +	if (err)
> +		goto out;
> +
> +	err = hsr_netlink_init();
> +	if (err)
> +		goto cleanup;
>  
> -	return res;
> +	return 0;
> +
> +cleanup:
> +	unregister_netdevice_notifier(&hsr_nb);
> +out:
> +	return err;
>  }
>  
>  static void __exit hsr_exit(void)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] hsr: Handle failures in module init
  2024-03-14 10:10 [PATCH net] hsr: Handle failures in module init Felix Maurer
  2024-03-14 10:50 ` Denis Kirjanov
@ 2024-03-14 12:59 ` Breno Leitao
  2024-03-14 15:56   ` Felix Maurer
  1 sibling, 1 reply; 5+ messages in thread
From: Breno Leitao @ 2024-03-14 12:59 UTC (permalink / raw)
  To: Felix Maurer; +Cc: netdev, davem, edumazet, kuba, pabeni

On Thu, Mar 14, 2024 at 11:10:52AM +0100, Felix Maurer wrote:
> A failure during registration of the netdev notifier was not handled at
> all. A failure during netlink initialization did not unregister the netdev
> notifier.
> 
> Handle failures of netdev notifier registration and netlink initialization.
> Both functions should only return negative values on failure and thereby
> lead to the hsr module not being loaded.
> 
> Signed-off-by: Felix Maurer <fmaurer@redhat.com>
> ---
>  net/hsr/hsr_main.c | 18 ++++++++++++++----
>  1 file changed, 14 insertions(+), 4 deletions(-)
> 
> diff --git a/net/hsr/hsr_main.c b/net/hsr/hsr_main.c
> index cb83c8feb746..1c4a5b678688 100644
> --- a/net/hsr/hsr_main.c
> +++ b/net/hsr/hsr_main.c
> @@ -148,14 +148,24 @@ static struct notifier_block hsr_nb = {
>  
>  static int __init hsr_init(void)
>  {
> -	int res;
> +	int err;
>  
>  	BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
>  
> -	register_netdevice_notifier(&hsr_nb);
> -	res = hsr_netlink_init();
> +	err = register_netdevice_notifier(&hsr_nb);
> +	if (err)
> +		goto out;

Can't you just 'return err' here? And avoid the `out` label below?

> +
> +	err = hsr_netlink_init();
> +	if (err)
> +		goto cleanup;

Same here, you can do something like the following and remove the
all the labels below, making the function a bit clearer.

	if (err) {
		unregister_netdevice_notifier(&hsr_nb);
		return err;
	}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] hsr: Handle failures in module init
  2024-03-14 12:59 ` Breno Leitao
@ 2024-03-14 15:56   ` Felix Maurer
  2024-03-18 13:01     ` Simon Horman
  0 siblings, 1 reply; 5+ messages in thread
From: Felix Maurer @ 2024-03-14 15:56 UTC (permalink / raw)
  To: Breno Leitao; +Cc: netdev, davem, edumazet, kuba, pabeni

On 14.03.24 13:59, Breno Leitao wrote:
> On Thu, Mar 14, 2024 at 11:10:52AM +0100, Felix Maurer wrote:
>> A failure during registration of the netdev notifier was not handled at
>> all. A failure during netlink initialization did not unregister the netdev
>> notifier.
>>
>> Handle failures of netdev notifier registration and netlink initialization.
>> Both functions should only return negative values on failure and thereby
>> lead to the hsr module not being loaded.
>>
>> Signed-off-by: Felix Maurer <fmaurer@redhat.com>
>> ---
>>  net/hsr/hsr_main.c | 18 ++++++++++++++----
>>  1 file changed, 14 insertions(+), 4 deletions(-)
>>
>> diff --git a/net/hsr/hsr_main.c b/net/hsr/hsr_main.c
>> index cb83c8feb746..1c4a5b678688 100644
>> --- a/net/hsr/hsr_main.c
>> +++ b/net/hsr/hsr_main.c
>> @@ -148,14 +148,24 @@ static struct notifier_block hsr_nb = {
>>  
>>  static int __init hsr_init(void)
>>  {
>> -	int res;
>> +	int err;
>>  
>>  	BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
>>  
>> -	register_netdevice_notifier(&hsr_nb);
>> -	res = hsr_netlink_init();
>> +	err = register_netdevice_notifier(&hsr_nb);
>> +	if (err)
>> +		goto out;
> 
> Can't you just 'return err' here? And avoid the `out` label below?
> 
>> +
>> +	err = hsr_netlink_init();
>> +	if (err)
>> +		goto cleanup;
> 
> Same here, you can do something like the following and remove the
> all the labels below, making the function a bit clearer.
> 
> 	if (err) {
> 		unregister_netdevice_notifier(&hsr_nb);
> 		return err;
> 	}

I usually follow the pattern with labels to make sure the cleanup is not
forgotten later when extending the function. But there is likely not
much change in the module init, I'll remove the labels in the next
iteration.

Thanks,
   Felix


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] hsr: Handle failures in module init
  2024-03-14 15:56   ` Felix Maurer
@ 2024-03-18 13:01     ` Simon Horman
  0 siblings, 0 replies; 5+ messages in thread
From: Simon Horman @ 2024-03-18 13:01 UTC (permalink / raw)
  To: Felix Maurer; +Cc: Breno Leitao, netdev, davem, edumazet, kuba, pabeni

On Thu, Mar 14, 2024 at 04:56:35PM +0100, Felix Maurer wrote:
> On 14.03.24 13:59, Breno Leitao wrote:
> > On Thu, Mar 14, 2024 at 11:10:52AM +0100, Felix Maurer wrote:
> >> A failure during registration of the netdev notifier was not handled at
> >> all. A failure during netlink initialization did not unregister the netdev
> >> notifier.
> >>
> >> Handle failures of netdev notifier registration and netlink initialization.
> >> Both functions should only return negative values on failure and thereby
> >> lead to the hsr module not being loaded.
> >>
> >> Signed-off-by: Felix Maurer <fmaurer@redhat.com>
> >> ---
> >>  net/hsr/hsr_main.c | 18 ++++++++++++++----
> >>  1 file changed, 14 insertions(+), 4 deletions(-)
> >>
> >> diff --git a/net/hsr/hsr_main.c b/net/hsr/hsr_main.c
> >> index cb83c8feb746..1c4a5b678688 100644
> >> --- a/net/hsr/hsr_main.c
> >> +++ b/net/hsr/hsr_main.c
> >> @@ -148,14 +148,24 @@ static struct notifier_block hsr_nb = {
> >>  
> >>  static int __init hsr_init(void)
> >>  {
> >> -	int res;
> >> +	int err;
> >>  
> >>  	BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
> >>  
> >> -	register_netdevice_notifier(&hsr_nb);
> >> -	res = hsr_netlink_init();
> >> +	err = register_netdevice_notifier(&hsr_nb);
> >> +	if (err)
> >> +		goto out;
> > 
> > Can't you just 'return err' here? And avoid the `out` label below?
> > 
> >> +
> >> +	err = hsr_netlink_init();
> >> +	if (err)
> >> +		goto cleanup;
> > 
> > Same here, you can do something like the following and remove the
> > all the labels below, making the function a bit clearer.
> > 
> > 	if (err) {
> > 		unregister_netdevice_notifier(&hsr_nb);
> > 		return err;
> > 	}
> 
> I usually follow the pattern with labels to make sure the cleanup is not
> forgotten later when extending the function. But there is likely not
> much change in the module init, I'll remove the labels in the next
> iteration.

FWIIW, I think the use of labels is the right way to go: it is the
idomatic approach preferred in Networking code.

That said, dropping the out label would be fine by me,
as as simple return nice IMHO.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-03-18 13:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-14 10:10 [PATCH net] hsr: Handle failures in module init Felix Maurer
2024-03-14 10:50 ` Denis Kirjanov
2024-03-14 12:59 ` Breno Leitao
2024-03-14 15:56   ` Felix Maurer
2024-03-18 13:01     ` Simon Horman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).