can: sja1000: plx_pci: fix error handling path in plx_pci_add_card()

Message ID 1669029031-7743-1-git-send-email-zhangchangzhong@huawei.com
State New
Headers
Series can: sja1000: plx_pci: fix error handling path in plx_pci_add_card() |

Commit Message

Zhang Changzhong Nov. 21, 2022, 11:10 a.m. UTC
  If pci_iomap() or register_sja1000dev() fails, netdev will not be
registered, but plx_pci_del_card() still deregisters the netdev.

To avoid this, let's free the netdev and clear card->net_dev[i] before
calling plx_pci_del_card(). In addition, add the missing pci_iounmap()
when the channel does not exist.

Compile tested only.

Fixes: 951f2f960e5b ("drivers/net/can/sja1000/plx_pci.c: eliminate double free")
Fixes: 24c4a3b29255 ("can: add support for CAN interface cards based on the PLX90xx PCI bridge")
Signed-off-by: Zhang Changzhong <zhangchangzhong@huawei.com>
---
 drivers/net/can/sja1000/plx_pci.c | 6 ++++++
 1 file changed, 6 insertions(+)
  

Comments

Marc Kleine-Budde Nov. 24, 2022, 2:46 p.m. UTC | #1
On 21.11.2022 19:10:30, Zhang Changzhong wrote:
> If pci_iomap() or register_sja1000dev() fails, netdev will not be
> registered, but plx_pci_del_card() still deregisters the netdev.
> 
> To avoid this, let's free the netdev and clear card->net_dev[i] before
> calling plx_pci_del_card(). In addition, add the missing pci_iounmap()
> when the channel does not exist.
> 
> Compile tested only.
> 
> Fixes: 951f2f960e5b ("drivers/net/can/sja1000/plx_pci.c: eliminate double free")
> Fixes: 24c4a3b29255 ("can: add support for CAN interface cards based on the PLX90xx PCI bridge")
> Signed-off-by: Zhang Changzhong <zhangchangzhong@huawei.com>
> ---
>  drivers/net/can/sja1000/plx_pci.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/drivers/net/can/sja1000/plx_pci.c b/drivers/net/can/sja1000/plx_pci.c
> index 5de1ebb..1158f5a 100644
> --- a/drivers/net/can/sja1000/plx_pci.c
> +++ b/drivers/net/can/sja1000/plx_pci.c
> @@ -678,6 +678,8 @@ static int plx_pci_add_card(struct pci_dev *pdev,
>  		if (!addr) {
>  			err = -ENOMEM;
>  			dev_err(&pdev->dev, "Failed to remap BAR%d\n", cm->bar);
> +			free_sja1000dev(dev);
> +			card->net_dev[i] = NULL;
>  			goto failure_cleanup;
>  		}
>  
> @@ -699,6 +701,9 @@ static int plx_pci_add_card(struct pci_dev *pdev,
>  			if (err) {
>  				dev_err(&pdev->dev, "Registering device failed "
>  					"(err=%d)\n", err);
> +				pci_iounmap(pdev, priv->reg_base);
> +				free_sja1000dev(dev);
> +				card->net_dev[i] = NULL;
>  				goto failure_cleanup;
>  			}
>  
> @@ -710,6 +715,7 @@ static int plx_pci_add_card(struct pci_dev *pdev,
>  		} else {
>  			dev_err(&pdev->dev, "Channel #%d not detected\n",
>  				i + 1);
> +			pci_iounmap(pdev, priv->reg_base);
>  			free_sja1000dev(dev);
>  			card->net_dev[i] = NULL;

There's quite some cleanup code doubling. Can you introduce a goto style
cleanup?

Marc
  

Patch

diff --git a/drivers/net/can/sja1000/plx_pci.c b/drivers/net/can/sja1000/plx_pci.c
index 5de1ebb..1158f5a 100644
--- a/drivers/net/can/sja1000/plx_pci.c
+++ b/drivers/net/can/sja1000/plx_pci.c
@@ -678,6 +678,8 @@  static int plx_pci_add_card(struct pci_dev *pdev,
 		if (!addr) {
 			err = -ENOMEM;
 			dev_err(&pdev->dev, "Failed to remap BAR%d\n", cm->bar);
+			free_sja1000dev(dev);
+			card->net_dev[i] = NULL;
 			goto failure_cleanup;
 		}
 
@@ -699,6 +701,9 @@  static int plx_pci_add_card(struct pci_dev *pdev,
 			if (err) {
 				dev_err(&pdev->dev, "Registering device failed "
 					"(err=%d)\n", err);
+				pci_iounmap(pdev, priv->reg_base);
+				free_sja1000dev(dev);
+				card->net_dev[i] = NULL;
 				goto failure_cleanup;
 			}
 
@@ -710,6 +715,7 @@  static int plx_pci_add_card(struct pci_dev *pdev,
 		} else {
 			dev_err(&pdev->dev, "Channel #%d not detected\n",
 				i + 1);
+			pci_iounmap(pdev, priv->reg_base);
 			free_sja1000dev(dev);
 			card->net_dev[i] = NULL;
 		}