Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2066282rwb; Fri, 2 Dec 2022 05:10:19 -0800 (PST) X-Google-Smtp-Source: AA0mqf4/rKnd6A2rO5S7aJ5ElhiCQNOEfahnKw+xLFxv5KM5Qf+/ZqHmOOz9i+c9P/dnyRV3Q1XO X-Received: by 2002:a17:906:5a71:b0:7c0:b882:7ea with SMTP id my49-20020a1709065a7100b007c0b88207eamr4936989ejc.768.1669986619248; Fri, 02 Dec 2022 05:10:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669986619; cv=none; d=google.com; s=arc-20160816; b=UmgtQ3TI/scHVh644KScu8s95JEj8kisDjF2U5TTnr/rEEfXBo+ldmuPfboOU94+fA XpHswsui6H8egsfkUJvmzR5Ia/v5CLC38hZdJDiWkNs4SMfsTB1etk0ayjtkiQ4V5F+q VkuO8bCBNFRFKcBVUY2a6FFzwQwq7IIbPSVOt6OBs0t0p76xKJv7gh98JEcyIHDVdKLq KMUhDX7UqxZ+tkqmCV967n5mclHrtp8fZYkIgdxj0jTRMwItFJZv1yBwsP7HgwnVIJTc W5DdHxBSjibilneiwrd3XUFzeVxxZOaSoI+TU5EEDB0tyVkDyUfHlmt1PlSm3ScD6YIv ldwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=OVLSyu3sQu6IMcPmaA/lur44ppR1D0BP0Yizp5VSnqQ=; b=lxE1w/YnEC8F8ArXmBKhjgTz8keTG/EJI43RyTptuWnoHifc6TuY6P7J8nrmVx6ibf cln6FhmJfK6bwjo3wjSrjAI6ZpxYrpJ5CInbUPzIw53wvdWwY1xAbJfjpGsAfN0rpYwg ESekX7zG0IN99Q134CM9TpzPuD/psRHSy8PTiu2Q22wAb/ZCSZ5EKwaPYzBykkOsEyCA l7TinmWKKMwNFmrNajwbKUsaIlAq8EP8AwlQFYJhY5t/MvDoskIOmv/ixmK5l0l30FHA A1WJw7NWg8wgv981kilvA5WgJy3EXj+Em89YBgzO6JCGz4RSGSzfFtClvl4N0Abp0aRB p4Hw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-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 cx17-20020a05640222b100b00461ebc19dcesi2801390edb.56.2022.12.02.05.09.48; Fri, 02 Dec 2022 05:10:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-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-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-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 S233669AbiLBNFX (ORCPT + 99 others); Fri, 2 Dec 2022 08:05:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233659AbiLBNFP (ORCPT ); Fri, 2 Dec 2022 08:05:15 -0500 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3794ECF7B5 for ; Fri, 2 Dec 2022 05:05:14 -0800 (PST) Received: from dggpemm500002.china.huawei.com (unknown [172.30.72.57]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4NNtSW2VsKz15N2f; Fri, 2 Dec 2022 21:04:27 +0800 (CST) Received: from localhost.localdomain.localdomain (10.175.113.25) by dggpemm500002.china.huawei.com (7.185.36.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 2 Dec 2022 21:05:09 +0800 From: Xiongfeng Wang To: , , , , CC: , , Subject: [PATCH v2 2/2] hwrng: geode - Fix PCI device refcount leak Date: Fri, 2 Dec 2022 21:22:34 +0800 Message-ID: <20221202132234.60631-3-wangxiongfeng2@huawei.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20221202132234.60631-1-wangxiongfeng2@huawei.com> References: <20221202132234.60631-1-wangxiongfeng2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.113.25] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm500002.china.huawei.com (7.185.36.229) 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-crypto@vger.kernel.org for_each_pci_dev() is implemented by pci_get_device(). The comment of pci_get_device() says that it will increase the reference count for the returned pci_dev and also decrease the reference count for the input pci_dev @from if it is not NULL. If we break for_each_pci_dev() loop with pdev not NULL, we need to call pci_dev_put() to decrease the reference count. We add a new struct 'amd_geode_priv' to record pointer of the pci_dev and membase, and then add missing pci_dev_put() for the normal and error path. Fixes: ef5d862734b8 ("[PATCH] Add Geode HW RNG driver") Signed-off-by: Xiongfeng Wang --- drivers/char/hw_random/geode-rng.c | 36 +++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/drivers/char/hw_random/geode-rng.c b/drivers/char/hw_random/geode-rng.c index 138ce434f86b..12fbe8091831 100644 --- a/drivers/char/hw_random/geode-rng.c +++ b/drivers/char/hw_random/geode-rng.c @@ -51,6 +51,10 @@ static const struct pci_device_id pci_tbl[] = { }; MODULE_DEVICE_TABLE(pci, pci_tbl); +struct amd_geode_priv { + struct pci_dev *pcidev; + void __iomem *membase; +}; static int geode_rng_data_read(struct hwrng *rng, u32 *data) { @@ -90,6 +94,7 @@ static int __init geode_rng_init(void) const struct pci_device_id *ent; void __iomem *mem; unsigned long rng_base; + struct amd_geode_priv *priv; for_each_pci_dev(pdev) { ent = pci_match_id(pci_tbl, pdev); @@ -97,17 +102,26 @@ static int __init geode_rng_init(void) goto found; } /* Device not found. */ - goto out; + return err; found: + priv = kzalloc(sizeof(*priv), GFP_KERNEL); + if (!priv) { + err = -ENOMEM; + goto put_dev; + } + rng_base = pci_resource_start(pdev, 0); if (rng_base == 0) - goto out; + goto free_priv; err = -ENOMEM; mem = ioremap(rng_base, 0x58); if (!mem) - goto out; - geode_rng.priv = (unsigned long)mem; + goto free_priv; + + geode_rng.priv = (unsigned long)priv; + priv->membase = mem; + priv->pcidev = pdev; pr_info("AMD Geode RNG detected\n"); err = hwrng_register(&geode_rng); @@ -116,20 +130,26 @@ static int __init geode_rng_init(void) err); goto err_unmap; } -out: return err; err_unmap: iounmap(mem); - goto out; +free_priv: + kfree(priv); +put_dev: + pci_dev_put(pdev); + return err; } static void __exit geode_rng_exit(void) { - void __iomem *mem = (void __iomem *)geode_rng.priv; + struct amd_geode_priv *priv; + priv = (struct amd_geode_priv *)geode_rng.priv; hwrng_unregister(&geode_rng); - iounmap(mem); + iounmap(priv->membase); + pci_dev_put(priv->pcidev); + kfree(priv); } module_init(geode_rng_init); -- 2.20.1