Skip to contentSkip to conversation

hwrng: revert managed API changes for amd and geode

PATCH In review Prarit Bhargava 2 participants 4 messages Mar 14, 2017 – Mar 16, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover hwrng: revert managed API changes for amd and geode

When booting top-of-tree the following WARN_ON triggers in the kernel on a 15h AMD system.

WARNING: CPU: 2 PID: 621 at drivers/base/dd.c:349 driver_probe_device+0x38c
Modules linked in: i2c_amd756(+) amd_rng sg pcspkr parport_pc(+) parport k8
CPU: 2 PID: 621 Comm: systemd-udevd Not tainted 4.11.0-0.rc1.git0.1.el7_UNS
Hardware name: To Be Filled By O.E.M. To Be Filled By O.E.M./TYAN High-End 
Call Trace:
dump_stack+0x63/0x8e
__warn+0xd1/0xf0
warn_slowpath_null+0x1d/0x20
driver_probe_device+0x38c/0x470
__driver_attach+0xc9/0xf0
? driver_probe_device+0x470/0x470
bus_for_each_dev+0x5d/0x90
driver_attach+0x1e/0x20
bus_add_driver+0x1d0/0x290
driver_register+0x60/0xe0
? 0xffffffffa0037000
__pci_register_driver+0x4c/0x50
amd756_driver_init+0x1e/0x1000 [i2c_amd756]
do_one_initcall+0x51/0x1b0
? __vunmap+0x85/0xd0
? do_init_module+0x27/0x1fa
do_init_module+0x60/0x1fa
load_module+0x15d1/0x1ad0
? m_show+0x1c0/0x1c0
SYSC_finit_module+0xa9/0xd0

There are PCI devices that contain both a RNG and SMBUS device. The RNG device is initialized by the amd-rng driver but the driver does not register against the device. The SMBUS device is initialized by the i2c-amd756 driver and registers against the device and hits the WARN_ON() because the amd-rng driver has already allocated resources against the device.

The amd-rng driver was incorrectly migrated to the device resource model (devres), and after code inspection I found that the geode-rng driver was also incorrectly migrated. These drivers are using devres but do not register a driver against the device, and both drivers are expecting a memory cleanup on a driver detach that will never happen. This results in a memory leak when the driver is unloaded and the inability to reload the driver.

Revert 31b2a73c9c5f ("hwrng: amd - Migrate to managed API"), and 6e9b5e76882c ("hwrng: geode - Migrate to managed API").

Fixes 31b2a73c9c5f ("hwrng: amd - Migrate to managed API").
Fixes 6e9b5e76882c ("hwrng: geode - Migrate to managed API")
Cc Matt Mackall <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Corentin LABBE <[email protected]>
Cc PrasannaKumar Muralidharan <[email protected]>
Cc Wei Yongjun <[email protected]>

Prarit Bhargava (2):
hwrng: amd - Revert managed API changes
hwrng: geode - Revert managed API changes

 drivers/char/hw_random/amd-rng.c   |   42 ++++++++++++++++++++++++------
 drivers/char/hw_random/geode-rng.c |   50 +++++++++++++++++++++++++-----------
 2 files changed, 69 insertions(+), 23 deletions(-)
Signature · 1 line
1.7.9.3
#1 lore raw

PATCH 1/2 hwrng: amd - Revert managed API changes

After commit 31b2a73c9c5f ("hwrng: amd - Migrate to managed API"), the amd-rng driver uses devres with pci_dev->dev to keep track of resources, but does not actually register a PCI driver. This results in the following issues:

1. The message

WARNING: CPU: 2 PID: 621 at drivers/base/dd.c:349 driver_probe_device+0x38c

is output when the i2c_amd756 driver loads and attempts to register a PCI driver. The PCI & device subsystems assume that no resources have been registered for the device, and the WARN_ON() triggers since amd-rng has already do so.

2. The driver leaks memory because the driver does not attach to a
device. The driver only uses the PCI device as a reference. devm_*()
functions will release resources on driver detach, which the amd-rng driver will never do. As a result,

3. The driver cannot be reloaded because there is always a use of the ioport and region after the first load of the driver.

Revert the changes made by 31b2a73c9c5f ("hwrng: amd - Migrate to managed API").

Fixes 31b2a73c9c5f ("hwrng: amd - Migrate to managed API").
Cc Matt Mackall <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Corentin LABBE <[email protected]>
Cc PrasannaKumar Muralidharan <[email protected]>
Cc Wei Yongjun <[email protected]>
---
 drivers/char/hw_random/amd-rng.c |   42 ++++++++++++++++++++++++++++++--------
 1 file changed, 34 insertions(+), 8 deletions(-)
drivers/char/hw_random/amd-rng.c +34 -8
index 4a99ac756f08..9959c762da2f 100644
@@ -55,6 +55,7 @@
 struct amd768_priv {
 	void __iomem *iobase;
 	struct pci_dev *pcidev;
+	u32 pmbase;
 };
 
 static int amd_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
@@ -148,33 +149,58 @@ static int __init mod_init(void)
 	if (pmbase == 0)
 		return -EIO;
 
-	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
 	if (!priv)
 		return -ENOMEM;
 
-	if (!devm_request_region(&pdev->dev, pmbase + PMBASE_OFFSET,
-				PMBASE_SIZE, DRV_NAME)) {
+	if (!request_region(pmbase + PMBASE_OFFSET, PMBASE_SIZE, DRV_NAME)) {
 		dev_err(&pdev->dev, DRV_NAME " region 0x%x already in use!\n",
 			pmbase + 0xF0);
-		return -EBUSY;
+		err = -EBUSY;
+		goto out;
 	}
 
-	priv->iobase = devm_ioport_map(&pdev->dev, pmbase + PMBASE_OFFSET,
-			PMBASE_SIZE);
+	priv->iobase = ioport_map(pmbase + PMBASE_OFFSET, PMBASE_SIZE);
 	if (!priv->iobase) {
 		pr_err(DRV_NAME "Cannot map ioport\n");
-		return -ENOMEM;
+		err = -EINVAL;
+		goto err_iomap;
 	}
 
 	amd_rng.priv = (unsigned long)priv;
+	priv->pmbase = pmbase;
 	priv->pcidev = pdev;
 
 	pr_info(DRV_NAME " detected\n");
-	return devm_hwrng_register(&pdev->dev, &amd_rng);
+	err = hwrng_register(&amd_rng);
+	if (err) {
+		pr_err(DRV_NAME " registering failed (%d)\n", err);
+		goto err_hwrng;
+	}
+	return 0;
+
+err_hwrng:
+	ioport_unmap(priv->iobase);
+err_iomap:
+	release_region(pmbase + PMBASE_OFFSET, PMBASE_SIZE);
+out:
+	kfree(priv);
+	return err;
 }
 
 static void __exit mod_exit(void)
 {
+	struct amd768_priv *priv;
+
+	priv = (struct amd768_priv *)amd_rng.priv;
+
+	hwrng_unregister(&amd_rng);
+
+	ioport_unmap(priv->iobase);
+
+	release_region(priv->pmbase + PMBASE_OFFSET, PMBASE_SIZE);
+
+	kfree(priv);
 }
 
 module_init(mod_init);
Signature · 1 line
1.7.9.3
#2 lore raw

PATCH 2/2 hwrng: geode - Revert managed API changes

In reply to Prarit Bhargava

After commit e9afc746299d ("hwrng: geode - Use linux/io.h instead of asm/io.h") the geode-rng driver uses devres with pci_dev->dev to keep track of resources, but does not actually register a PCI driver. This results in the following issues:

1. The driver leaks memory because the driver does not attach to a
device. The driver only uses the PCI device as a reference. devm_*()
functions will release resources on driver detach, which the geode-rng driver will never do. As a result,

2. The driver cannot be reloaded because there is always a use of the ioport and region after the first load of the driver.

Revert the changes made by e9afc746299d ("hwrng: geode - Use linux/io.h instead of asm/io.h").

Fixes 6e9b5e76882c ("hwrng: geode - Migrate to managed API")
Cc Matt Mackall <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Corentin LABBE <[email protected]>
Cc PrasannaKumar Muralidharan <[email protected]>
Cc Wei Yongjun <[email protected]>
---
 drivers/char/hw_random/geode-rng.c |   50 +++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 15 deletions(-)
drivers/char/hw_random/geode-rng.c +35 -15
index e7a245942029..e1d421a36a13 100644
@@ -31,6 +31,9 @@
 #include <linux/module.h>
 #include <linux/pci.h>
 
+
+#define PFX	KBUILD_MODNAME ": "
+
 #define GEODE_RNG_DATA_REG   0x50
 #define GEODE_RNG_STATUS_REG 0x54
 
@@ -82,6 +85,7 @@ static int geode_rng_data_present(struct hwrng *rng, int wait)
 
 static int __init mod_init(void)
 {
+	int err = -ENODEV;
 	struct pci_dev *pdev = NULL;
 	const struct pci_device_id *ent;
 	void __iomem *mem;
@@ -89,27 +93,43 @@ static int __init mod_init(void)
 
 	for_each_pci_dev(pdev) {
 		ent = pci_match_id(pci_tbl, pdev);
-		if (ent) {
-			rng_base = pci_resource_start(pdev, 0);
-			if (rng_base == 0)
-				return -ENODEV;
-
-			mem = devm_ioremap(&pdev->dev, rng_base, 0x58);
-			if (!mem)
-				return -ENOMEM;
-			geode_rng.priv = (unsigned long)mem;
-
-			pr_info("AMD Geode RNG detected\n");
-			return devm_hwrng_register(&pdev->dev, &geode_rng);
-		}
+		if (ent)
+			goto found;
 	}
-
 	/* Device not found. */
-	return -ENODEV;
+	goto out;
+
+found:
+	rng_base = pci_resource_start(pdev, 0);
+	if (rng_base == 0)
+		goto out;
+	err = -ENOMEM;
+	mem = ioremap(rng_base, 0x58);
+	if (!mem)
+		goto out;
+	geode_rng.priv = (unsigned long)mem;
+
+	pr_info("AMD Geode RNG detected\n");
+	err = hwrng_register(&geode_rng);
+	if (err) {
+		pr_err(PFX "RNG registering failed (%d)\n",
+		       err);
+		goto err_unmap;
+	}
+out:
+	return err;
+
+err_unmap:
+	iounmap(mem);
+	goto out;
 }
 
 static void __exit mod_exit(void)
 {
+	void __iomem *mem = (void __iomem *)geode_rng.priv;
+
+	hwrng_unregister(&geode_rng);
+	iounmap(mem);
 }
 
 module_init(mod_init);
Signature · 1 line
1.7.9.3
maintainer
#3 lore raw
In reply to Prarit Bhargava
Quoted 46 lines · Prarit Bhargava

When booting top-of-tree the following WARN_ON triggers in the kernel on a 15h AMD system.

WARNING: CPU: 2 PID: 621 at drivers/base/dd.c:349 driver_probe_device+0x38c
Modules linked in: i2c_amd756(+) amd_rng sg pcspkr parport_pc(+) parport k8
CPU: 2 PID: 621 Comm: systemd-udevd Not tainted 4.11.0-0.rc1.git0.1.el7_UNS
Hardware name: To Be Filled By O.E.M. To Be Filled By O.E.M./TYAN High-End 
Call Trace:
dump_stack+0x63/0x8e
__warn+0xd1/0xf0
warn_slowpath_null+0x1d/0x20
driver_probe_device+0x38c/0x470
__driver_attach+0xc9/0xf0
? driver_probe_device+0x470/0x470
bus_for_each_dev+0x5d/0x90
driver_attach+0x1e/0x20
bus_add_driver+0x1d0/0x290
driver_register+0x60/0xe0
? 0xffffffffa0037000
__pci_register_driver+0x4c/0x50
amd756_driver_init+0x1e/0x1000 [i2c_amd756]
do_one_initcall+0x51/0x1b0
? __vunmap+0x85/0xd0
? do_init_module+0x27/0x1fa
do_init_module+0x60/0x1fa
load_module+0x15d1/0x1ad0
? m_show+0x1c0/0x1c0
SYSC_finit_module+0xa9/0xd0

There are PCI devices that contain both a RNG and SMBUS device. The RNG device is initialized by the amd-rng driver but the driver does not register against the device. The SMBUS device is initialized by the i2c-amd756 driver and registers against the device and hits the WARN_ON() because the amd-rng driver has already allocated resources against the device.

The amd-rng driver was incorrectly migrated to the device resource model (devres), and after code inspection I found that the geode-rng driver was also incorrectly migrated. These drivers are using devres but do not register a driver against the device, and both drivers are expecting a memory cleanup on a driver detach that will never happen. This results in a memory leak when the driver is unloaded and the inability to reload the driver.

Revert 31b2a73c9c5f ("hwrng: amd - Migrate to managed API"), and 6e9b5e76882c ("hwrng: geode - Migrate to managed API").

Quoted 10 lines
Fixes 31b2a73c9c5f ("hwrng: amd - Migrate to managed API").
Fixes 6e9b5e76882c ("hwrng: geode - Migrate to managed API")
Cc Matt Mackall <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Corentin LABBE <[email protected]>
Cc PrasannaKumar Muralidharan <[email protected]>
Cc Wei Yongjun <[email protected]>

Both patches applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt