diff mbox series

[v3] can: etas_es58x: fix error handling

Message ID 20211115083756.25971-1-paskripkin@gmail.com (mailing list archive)
State Awaiting Upstream
Delegated to: Netdev Maintainers
Headers show
Series [v3] can: etas_es58x: fix error handling | expand

Checks

Context Check Description
netdev/tree_selection success Series ignored based on subject

Commit Message

Pavel Skripkin Nov. 15, 2021, 8:37 a.m. UTC
When register_candev() fails there are 2 possible device states:
NETREG_UNINITIALIZED and NETREG_UNREGISTERED. None of them are suitable
for calling unregister_candev(), because of following checks in
unregister_netdevice_many():

	if (dev->reg_state == NETREG_UNINITIALIZED)
		WARN_ON(1);
...
	BUG_ON(dev->reg_state != NETREG_REGISTERED);

To avoid possible BUG_ON or WARN_ON let's free current netdev before
returning from es58x_init_netdev() and leave others (registered)
net devices for es58x_free_netdevs().

Fixes: 8537257874e9 ("can: etas_es58x: add core support for ETAS ES58X CAN USB interfaces")
Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
---

Changes in v3:
	- Moved back es58x_dev->netdev[channel_idx] initialization,
	  since it's unsafe to intialize it _after_ register_candev()
	  call. Thanks to Johan Hovold <johan@kernel.org> for spotting
	  it

Changes in v2:
	- Fixed Fixes: tag
	- Moved es58x_dev->netdev[channel_idx] initialization at the end
	  of the function

---
 drivers/net/can/usb/etas_es58x/es58x_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

Vincent Mailhol Nov. 15, 2021, 9:15 a.m. UTC | #1
On Mon. 15 Nov 2021 at 17:37, Pavel Skripkin <paskripkin@gmail.com> wrote:
> When register_candev() fails there are 2 possible device states:
> NETREG_UNINITIALIZED and NETREG_UNREGISTERED. None of them are suitable
> for calling unregister_candev(), because of following checks in
> unregister_netdevice_many():
>
>         if (dev->reg_state == NETREG_UNINITIALIZED)
>                 WARN_ON(1);
> ...
>         BUG_ON(dev->reg_state != NETREG_REGISTERED);
>
> To avoid possible BUG_ON or WARN_ON let's free current netdev before
> returning from es58x_init_netdev() and leave others (registered)
> net devices for es58x_free_netdevs().
>
> Fixes: 8537257874e9 ("can: etas_es58x: add core support for ETAS ES58X CAN USB interfaces")
> Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>

Acked-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>

> ---
>
> Changes in v3:
>         - Moved back es58x_dev->netdev[channel_idx] initialization,
>           since it's unsafe to intialize it _after_ register_candev()
>           call. Thanks to Johan Hovold <johan@kernel.org> for spotting
>           it

My bad on that. I missed the fact that the netdev_ops becomes
active once register_candev() returns.

> Changes in v2:
>         - Fixed Fixes: tag
>         - Moved es58x_dev->netdev[channel_idx] initialization at the end
>           of the function
>
> ---
>  drivers/net/can/usb/etas_es58x/es58x_core.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/net/can/usb/etas_es58x/es58x_core.c b/drivers/net/can/usb/etas_es58x/es58x_core.c
> index 96a13c770e4a..41c721f2fbbe 100644
> --- a/drivers/net/can/usb/etas_es58x/es58x_core.c
> +++ b/drivers/net/can/usb/etas_es58x/es58x_core.c
> @@ -2098,8 +2098,11 @@ static int es58x_init_netdev(struct es58x_device *es58x_dev, int channel_idx)
>         netdev->flags |= IFF_ECHO;      /* We support local echo */
>
>         ret = register_candev(netdev);
> -       if (ret)
> +       if (ret) {
> +               free_candev(netdev);
> +               es58x_dev->netdev[channel_idx] = NULL;
>                 return ret;
> +       }
>
>         netdev_queue_set_dql_min_limit(netdev_get_tx_queue(netdev, 0),
>                                        es58x_dev->param->dql_min_limit);
> --
> 2.33.1
>
diff mbox series

Patch

diff --git a/drivers/net/can/usb/etas_es58x/es58x_core.c b/drivers/net/can/usb/etas_es58x/es58x_core.c
index 96a13c770e4a..41c721f2fbbe 100644
--- a/drivers/net/can/usb/etas_es58x/es58x_core.c
+++ b/drivers/net/can/usb/etas_es58x/es58x_core.c
@@ -2098,8 +2098,11 @@  static int es58x_init_netdev(struct es58x_device *es58x_dev, int channel_idx)
 	netdev->flags |= IFF_ECHO;	/* We support local echo */
 
 	ret = register_candev(netdev);
-	if (ret)
+	if (ret) {
+		free_candev(netdev);
+		es58x_dev->netdev[channel_idx] = NULL;
 		return ret;
+	}
 
 	netdev_queue_set_dql_min_limit(netdev_get_tx_queue(netdev, 0),
 				       es58x_dev->param->dql_min_limit);