Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp7064227rwb; Wed, 23 Nov 2022 01:23:59 -0800 (PST) X-Google-Smtp-Source: AA0mqf7a+kztN1IL48Ae9BjSu5q5mmYUwAFNalUnCN6CZoSdbF+SxfYFWbKBxC3xqnPdHosE3rWw X-Received: by 2002:a17:90b:2688:b0:218:b9e1:ebef with SMTP id pl8-20020a17090b268800b00218b9e1ebefmr12991645pjb.65.1669195438838; Wed, 23 Nov 2022 01:23:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669195438; cv=none; d=google.com; s=arc-20160816; b=fCd7MENMv2Un7TpFvc11oPJeDoZEAbdlaWiwmYATqEBv6K+h9z+CiSwR+sYHLZ2Ef6 JxkNmyO2JXobGITQfWpYcSfDLKrDAsw9U96pC0vtiXeV2CXJc0xV/ZEdlt8PShax1hfS EwXk/PNjRHDsHVYrzzV1V1FrCGBCIvk5PcIyBnbsVXZDXx1cZvQVtlNV39Cs75FQtVyd hdE4gE05ctmy0FdLXGCdo/rQEPjLC8dWPOW/AVrDv/HMNF3DdYM3fCXTuyV8YFnIIHjR 6/ow8fP5z8GxYEK/wOQ7NtWVHHQm+l48wrHZxoDFQLvKv9M3EFpsZWd38tgyfeyokc59 qB6Q== 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 :message-id:date:subject:cc:to:from; bh=MYLc1nWNcgWtNiKjUs7FBwPx/N0RI8hNesocq/65GYA=; b=VzpbQCR8kiTEBcbbWFp3NsIrv3i/KiZqVPvHu1DoNIfWmYo9Lvi+cYdLy6ON54gUQy zREzwjBFM9r0168NUAcisjPhKq+o9uOKHraVjxwc6HVlMWZXigPDYIk+KU3P71FUrnSP QQqilD/wNzK+KI6squKyc8gcM70K4TDTsMeuAC/kKkke0nejj1BUX+cDH988qiH+4bZR kVf+Bx2z4j10WnW1yqUI0fHApznh8vs8bo92tUwRKrtCv/fZwt43E77hlZgqR7mdQzeY 9ybirhzoH5jCliy05M65WMcPpL/rbV/WGtpOFH86Ebp972DJG0mqknQ+sMSoDYp/8+vE biuw== 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 cl6-20020a17090af68600b00202b78e2adasi1167930pjb.102.2022.11.23.01.23.39; Wed, 23 Nov 2022 01:23:58 -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 S237524AbiKWJXN (ORCPT + 99 others); Wed, 23 Nov 2022 04:23:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237533AbiKWJWZ (ORCPT ); Wed, 23 Nov 2022 04:22:25 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04AF910B40D for ; Wed, 23 Nov 2022 01:22:01 -0800 (PST) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NHFxN5wXjzmW6g; Wed, 23 Nov 2022 17:21:28 +0800 (CST) Received: from dggpemm500002.china.huawei.com (7.185.36.229) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 23 Nov 2022 17:22:00 +0800 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; Wed, 23 Nov 2022 17:21:59 +0800 From: Xiongfeng Wang To: , , , CC: , , Subject: [PATCH] hwrng: amd - Fix PCI device refcount leak Date: Wed, 23 Nov 2022 17:39:49 +0800 Message-ID: <20221123093949.115579-1-wangxiongfeng2@huawei.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.113.25] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) 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..46e997366b15 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; } @@ -201,6 +207,8 @@ static void __exit amd_rng_mod_exit(void) release_region(priv->pmbase + PMBASE_OFFSET, PMBASE_SIZE); kfree(priv); + + pci_dev_put(priv->pcidev); } module_init(amd_rng_mod_init); -- 2.20.1