From patchwork Fri Nov 25 13:46:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Changzhong X-Patchwork-Id: 26015 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp4021612wrr; Fri, 25 Nov 2022 05:34:57 -0800 (PST) X-Google-Smtp-Source: AA0mqf4V8FMDOdTzAPFP1QOh6np9pSgIaWhGrHKwCP4WgiUn7mpLFq6VMoerxNgU8k3qLvglybKY X-Received: by 2002:a17:907:104d:b0:79e:3312:9a0f with SMTP id oy13-20020a170907104d00b0079e33129a0fmr30140626ejb.576.1669383297729; Fri, 25 Nov 2022 05:34:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669383297; cv=none; d=google.com; s=arc-20160816; b=kP+CT+Dq/q82TbTV6cln9UdvRX6PnLC2+EiUzfxIGeMvFEw6v3ZA5ipNM2ugkTdCWy s2Yvk1uSTm8KQ+/3nybjBdNbtkkcVOl8QdmHGExKWrD2LbKDu6LoAZSpjzn/4CNdzn7K JX6zDtdbZ/bFryM87DpZseHnbLStOZ03xdcqUSSIzY3Xy5rgxYpuPqwkK1RaFHyQOVhl C9EcdZkqqhmjRzd7ckzVs3VhJ03B3ynQxqIwNe8a0LQY3EW4gG55PbzancNNyC94Xdzf tdlbtBkEfVk9C0gL4/O1fodepppZwwoeds9VK+XNkbPBSf+WztPthRiMc5xE9QUaFwnN YMDQ== 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 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.34.33; Fri, 25 Nov 2022 05:34:57 -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 + 99 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750475261072223094?= X-GMAIL-MSGID: =?utf-8?q?1750475261072223094?= 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);