Skip to content

Commit

Permalink
hwrng: amd - Fix PCI device refcount leak
Browse files Browse the repository at this point in the history
[ Upstream commit ecadb5b ]

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: 96d63c0 ("[PATCH] Add AMD HW RNG driver")
Signed-off-by: Xiongfeng Wang <wangxiongfeng2@huawei.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
fenghusthu authored and Sasha Levin committed Jan 15, 2023
1 parent 2dc1426 commit 80c5fef
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions drivers/char/hw_random/amd-rng.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,19 @@ static int __init 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",
Expand Down Expand Up @@ -185,6 +189,8 @@ static int __init mod_init(void)
release_region(pmbase + PMBASE_OFFSET, PMBASE_SIZE);
out:
kfree(priv);
put_dev:
pci_dev_put(pdev);
return err;
}

Expand All @@ -200,6 +206,8 @@ static void __exit mod_exit(void)

release_region(priv->pmbase + PMBASE_OFFSET, PMBASE_SIZE);

pci_dev_put(priv->pcidev);

kfree(priv);
}

Expand Down

0 comments on commit 80c5fef

Please sign in to comment.