Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp10533595rwb; Fri, 25 Nov 2022 05:36:13 -0800 (PST) X-Google-Smtp-Source: AA0mqf6jTWnU8LIkzFWnf/y+4AcTYascm4+BMwCFi5r6IpN4iGzKigckXZRz3a5Gx3u6j0IF72pw X-Received: by 2002:ac2:46f8:0:b0:4af:5088:8fa3 with SMTP id q24-20020ac246f8000000b004af50888fa3mr13482691lfo.538.1669383372956; Fri, 25 Nov 2022 05:36:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669383372; cv=none; d=google.com; s=arc-20160816; b=KELUeqhcKGri0ieaJO678P+hhbCpZVlpwrJgk2udpVcthkliIM2nwTKFO4s7212U2I 7t68vuasHvdZGkwwEJBQKthNyEv+ZA6Ji/EEMdc+M/Oxm+n7hcdjkZhCarqCIz/AQwl5 FkCQ3CD1OUX/ix5vwJdcuwWULegIGpQqUwycPXFisJynq25Acrxe9yOUkwZd8fP4I6Cc 4/U8PtwgX47bWFxR6mZAdK5jHPrIJofKhEtBvuB9ttNC7xc8oxF786JjK2Nc5T7QHuex Jv8mkBHxw5dg+pJ92kpK/uNiPRt2r0Zj23/sWysPaec2osOYRxiT/PtnxYYZBVnrapoY hLag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=H2OuQA3UvuSHgYNCZtQ7KF7zQgw04EcHsexL9CLP/qA=; b=zvCgzqmBq6+BCFBk7btfRATWK+cCVIZgP9dhlcWWhxY/rTbjS8YKvU9EwCYsSTTEqu T8vV1V5tmCimSEpj7OGgfh0LkvX/X3eLovSnagYMhoq7ryY2B2BKKoPPiEntdNe+e0kH gwHq/ZX6SnE4W3S+71L8gdqh7LSiEzBV/PCjTp6jfOdLFhUjOWmHiJdBOpSOiTFzAbWt 5Rdc5/T6C3wj3zt/qxa618ix8yWu0fwxyRO3T6T04zIESX7WQ0M0j2RaJ0Mn49ZScJFt qtdC+Qy3d/kEW/6jjOEFZwog957VZD+wWE9aEBn26O6GE5BQ2Z32+FjBC0IjctXn9vEK DCTg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id vj23-20020a170907131700b0078db1258ae9si2331179ejb.555.2022.11.25.05.35.50; Fri, 25 Nov 2022 05:36:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229702AbiKYN2o (ORCPT + 86 others); Fri, 25 Nov 2022 08:28:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229526AbiKYN2n (ORCPT ); Fri, 25 Nov 2022 08:28:43 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5005222B3; Fri, 25 Nov 2022 05:28:40 -0800 (PST) Received: from dggpeml500021.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NJbK00KBfzmWCs; Fri, 25 Nov 2022 21:28:04 +0800 (CST) Received: from dggpeml500006.china.huawei.com (7.185.36.76) by dggpeml500021.china.huawei.com (7.185.36.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 25 Nov 2022 21:28:38 +0800 Received: from localhost.localdomain (10.175.112.70) by dggpeml500006.china.huawei.com (7.185.36.76) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 25 Nov 2022 21:28:37 +0800 From: Zhang Changzhong To: Wolfgang Grandegger , Marc Kleine-Budde , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Julia Lawall , Pavel Cheblakov CC: Zhang Changzhong , , , Subject: [PATCH v2] can: sja1000: plx_pci: fix error handling path in plx_pci_add_card() Date: Fri, 25 Nov 2022 21:46:14 +0800 Message-ID: <1669383975-17332-1-git-send-email-zhangchangzhong@huawei.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.112.70] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml500006.china.huawei.com (7.185.36.76) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- v1->v2: switch to goto style fix. drivers/net/can/sja1000/plx_pci.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/drivers/net/can/sja1000/plx_pci.c b/drivers/net/can/sja1000/plx_pci.c index 5de1ebb..134a8cb 100644 --- a/drivers/net/can/sja1000/plx_pci.c +++ b/drivers/net/can/sja1000/plx_pci.c @@ -678,7 +678,7 @@ 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); - goto failure_cleanup; + goto failure_free_dev; } priv->reg_base = addr + cm->offset; @@ -699,7 +699,7 @@ static int plx_pci_add_card(struct pci_dev *pdev, if (err) { dev_err(&pdev->dev, "Registering device failed " "(err=%d)\n", err); - goto failure_cleanup; + goto failure_iounmap; } card->channels++; @@ -710,6 +710,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; } @@ -738,6 +739,11 @@ static int plx_pci_add_card(struct pci_dev *pdev, } return 0; +failure_iounmap: + pci_iounmap(pdev, priv->reg_base); +failure_free_dev: + free_sja1000dev(dev); + card->net_dev[i] = NULL; failure_cleanup: dev_err(&pdev->dev, "Error: %d. Cleaning Up.\n", err); -- 2.9.5