Skip to content

Commit ecadb5b

Browse files
fenghusthuherbertx
authored andcommitted
hwrng: amd - Fix PCI device refcount leak
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 <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 18daae5 commit ecadb5b

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

drivers/char/hw_random/amd-rng.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,19 @@ static int __init amd_rng_mod_init(void)
143143
found:
144144
err = pci_read_config_dword(pdev, 0x58, &pmbase);
145145
if (err)
146-
return err;
146+
goto put_dev;
147147

148148
pmbase &= 0x0000FF00;
149-
if (pmbase == 0)
150-
return -EIO;
149+
if (pmbase == 0) {
150+
err = -EIO;
151+
goto put_dev;
152+
}
151153

152154
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
153-
if (!priv)
154-
return -ENOMEM;
155+
if (!priv) {
156+
err = -ENOMEM;
157+
goto put_dev;
158+
}
155159

156160
if (!request_region(pmbase + PMBASE_OFFSET, PMBASE_SIZE, DRV_NAME)) {
157161
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)
185189
release_region(pmbase + PMBASE_OFFSET, PMBASE_SIZE);
186190
out:
187191
kfree(priv);
192+
put_dev:
193+
pci_dev_put(pdev);
188194
return err;
189195
}
190196

@@ -200,6 +206,8 @@ static void __exit amd_rng_mod_exit(void)
200206

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

209+
pci_dev_put(priv->pcidev);
210+
203211
kfree(priv);
204212
}
205213

0 commit comments

Comments
 (0)