Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2070981rwb; Fri, 2 Dec 2022 05:13:40 -0800 (PST) X-Google-Smtp-Source: AA0mqf7AnEJpcaw/gu99wtno5cnfx0935lyfSxXQY1RMSyaubp+xMinaL0i4FVSqQ9JaRvoFpdVX X-Received: by 2002:a05:6402:f1c:b0:46a:b1a9:c34e with SMTP id i28-20020a0564020f1c00b0046ab1a9c34emr6735756eda.212.1669986820163; Fri, 02 Dec 2022 05:13:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669986820; cv=none; d=google.com; s=arc-20160816; b=eWsHx4qQwLpdxAtDyKmXUE/qm+F9mlCSK1hxEnkaY4pL5Um4xZVSlmLa+1RwuZSTrE 324bdhTiY2T4bMFuLLYO7CKTYyC2mzO0GmrwNGLm7+B1Ke9Pq5AH5Lr8yjdolGYG8LET tFLu1aRgBMdQNRR/luQ6azueIuSGqkYXKtFK4uNZSJXGXguBHz1lJbiQ23ujC6kzRHBi 6qDfkgxeIuYfsLFkU8fpYbxVlLqVteOJgVfvlL/xHS12lOfzKdbo/xi/DIeWRppA204z vjz+mEE0WRFx7ClxRnXfeFoNMhGBlVRrt8rLhDBoQDvZwk2Y2UdHVzjvOpSeIqgBJJpV locA== 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=hMZY4S7gXBhu2bbxhID627+ResriHhOfeQJUipwSZJ0=; b=yIZWz+e/IvUYqv/8kvijN+l1r4mpoNjfpUCIJbGlPwviGew7uja+baWCS1+cUMga4M hLC/0lvjiAD0jQkP9D1ad0ofWx3IVnlQiHHqpagkk7b478PTnjyZJElH5wL5KMmFPqdj iZ8EwYmOK+ZmkbALrE5itVlDQ6KKAQA1i9gGhCDFfXH4T/wahYAQVuzoyx+VQNtWicCY d6iGu460Me5pMmQpdrw5mr+BqODdS0VnXgtdj2PvEuCVMWJqPvbg1b9jTcm8KpF7v/TN hGUd73qLfBturHnNHJ0Y9a5E+Sk0tbmhkQ+96dQxvWgkFu1NxtzMlPK+Ni18xyh45uIw FcVA== 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 y68-20020a50bb4a000000b00461a32e0e38si6085322ede.306.2022.12.02.05.13.11; Fri, 02 Dec 2022 05:13:40 -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 S233664AbiLBNFM (ORCPT + 99 others); Fri, 2 Dec 2022 08:05:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233651AbiLBNFM (ORCPT ); Fri, 2 Dec 2022 08:05:12 -0500 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49C04CF7B5 for ; Fri, 2 Dec 2022 05:05:11 -0800 (PST) Received: from dggpemm500002.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4NNtPM648szpW4X; Fri, 2 Dec 2022 21:01:43 +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 1/2] hwrng: amd - Fix PCI device refcount leak Date: Fri, 2 Dec 2022 21:22:33 +0800 Message-ID: <20221202132234.60631-2-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. Add the missing pci_dev_put() for the normal and error path. Fixes: 96d63c0297cc ("[PATCH] Add AMD HW RNG driver") Signed-off-by: Xiongfeng Wang --- drivers/char/hw_random/amd-rng.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/char/hw_random/amd-rng.c b/drivers/char/hw_random/amd-rng.c index c22d4184bb61..0555e3838bce 100644 --- a/drivers/char/hw_random/amd-rng.c +++ b/drivers/char/hw_random/amd-rng.c @@ -143,15 +143,19 @@ static int __init amd_rng_mod_init(void) found: err = pci_read_config_dword(pdev, 0x58, &pmbase); if (err) - return err; + goto put_dev; pmbase &= 0x0000FF00; - if (pmbase == 0) - return -EIO; + if (pmbase == 0) { + err = -EIO; + goto put_dev; + } priv = kzalloc(sizeof(*priv), GFP_KERNEL); - if (!priv) - return -ENOMEM; + if (!priv) { + err = -ENOMEM; + goto put_dev; + } if (!request_region(pmbase + PMBASE_OFFSET, PMBASE_SIZE, DRV_NAME)) { dev_err(&pdev->dev, DRV_NAME " region 0x%x already in use!\n", @@ -185,6 +189,8 @@ static int __init amd_rng_mod_init(void) release_region(pmbase + PMBASE_OFFSET, PMBASE_SIZE); out: kfree(priv); +put_dev: + pci_dev_put(pdev); return err; } @@ -200,6 +206,8 @@ static void __exit amd_rng_mod_exit(void) release_region(priv->pmbase + PMBASE_OFFSET, PMBASE_SIZE); + pci_dev_put(priv->pcidev); + kfree(priv); } -- 2.20.1