[PATCH] soc/microchip: fix invalid free in mpfs_sys_controller_delete

Conor Dooley mail at conchuod.ie
Fri Mar 18 10:21:08 PDT 2022


From: Conor Dooley <conor.dooley at microchip.com>

Fix an invalid kfree in mpfs_sys_controller_delete, by replacing the
devm_kzalloc with a regular kzalloc. Change the error handling in the
probe function to free the sys_controller struct if the probe fails.

> cocci warnings: (new ones prefixed by >>)
> >> drivers/soc/microchip/mpfs-sys-controller.c:73:1-6: WARNING: invalid free of devm_ allocated data
Link: https://lore.kernel.org/linux-mm/202203180259.lgIylRZV-lkp@intel.com/

Fixes: d0054a470c33 ("soc: add microchip polarfire soc system controller")
Reported-by: kernel test robot <lkp at intel.com>
Signed-off-by: Conor Dooley <conor.dooley at microchip.com>
Signed-off-by: Conor Dooley <mail at conchuod.ie>
---
 drivers/soc/microchip/mpfs-sys-controller.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/drivers/soc/microchip/mpfs-sys-controller.c b/drivers/soc/microchip/mpfs-sys-controller.c
index 31f3f29fc1ae..6e20207b5756 100644
--- a/drivers/soc/microchip/mpfs-sys-controller.c
+++ b/drivers/soc/microchip/mpfs-sys-controller.c
@@ -95,9 +95,9 @@ static int mpfs_sys_controller_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct mpfs_sys_controller *sys_controller;
-	int i;
+	int i, ret;
 
-	sys_controller = devm_kzalloc(dev, sizeof(*sys_controller), GFP_KERNEL);
+	sys_controller = kzalloc(sizeof(*sys_controller), GFP_KERNEL);
 	if (!sys_controller)
 		return -ENOMEM;
 
@@ -106,9 +106,12 @@ static int mpfs_sys_controller_probe(struct platform_device *pdev)
 	sys_controller->client.tx_block = 1U;
 
 	sys_controller->chan = mbox_request_channel(&sys_controller->client, 0);
-	if (IS_ERR(sys_controller->chan))
-		return dev_err_probe(dev, PTR_ERR(sys_controller->chan),
-				     "Failed to get mbox channel\n");
+	if (IS_ERR(sys_controller->chan)) {
+		ret = dev_err_probe(dev, PTR_ERR(sys_controller->chan),
+				    "Failed to get mbox channel\n");
+		kfree(sys_controller);
+		return ret;
+	}
 
 	init_completion(&sys_controller->c);
 	kref_init(&sys_controller->consumers);
-- 
2.32.0




More information about the linux-riscv mailing list