[PATCH v2] bus: hisi_lpc: fix missing platform_device_put() in hisi_lpc_acpi_probe()

Yang Yingliang posted 1 patch 3 years, 9 months ago
drivers/bus/hisi_lpc.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
[PATCH v2] bus: hisi_lpc: fix missing platform_device_put() in hisi_lpc_acpi_probe()
Posted by Yang Yingliang 3 years, 9 months ago
In error case in hisi_lpc_acpi_probe() after calling platform_device_alloc(),
if platform_device_add() is not called successfully, hisi_lpc_acpi_remove()
can't release the failed 'pdev' including its resource and data, so they will
be leaked, call platform_device_put() to put the refcount, then they will be
freed in platform_device_release().
I'v constructed this error case and tested this patch on D05 board.

Fixes: 99c0228d6ff1 ("HISI LPC: Re-Add ACPI child enumeration support")
Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
v2:
  - keep declartion of pdev reverse-firtree ordering
  - update commit message
---
 drivers/bus/hisi_lpc.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/bus/hisi_lpc.c b/drivers/bus/hisi_lpc.c
index 378f5d62a991..05e9357bf006 100644
--- a/drivers/bus/hisi_lpc.c
+++ b/drivers/bus/hisi_lpc.c
@@ -502,6 +502,7 @@ static void hisi_lpc_acpi_remove(struct device *hostdev)
 static int hisi_lpc_acpi_probe(struct device *hostdev)
 {
 	struct acpi_device *adev = ACPI_COMPANION(hostdev);
+	struct platform_device *pdev;
 	struct acpi_device *child;
 	int ret;
 
@@ -509,7 +510,6 @@ static int hisi_lpc_acpi_probe(struct device *hostdev)
 	list_for_each_entry(child, &adev->children, node) {
 		const char *hid = acpi_device_hid(child);
 		const struct hisi_lpc_acpi_cell *cell;
-		struct platform_device *pdev;
 		const struct resource *res;
 		bool found = false;
 		int num_res;
@@ -571,22 +571,24 @@ static int hisi_lpc_acpi_probe(struct device *hostdev)
 
 		ret = platform_device_add_resources(pdev, res, num_res);
 		if (ret)
-			goto fail;
+			goto fail_put_device;
 
 		ret = platform_device_add_data(pdev, cell->pdata,
 					       cell->pdata_size);
 		if (ret)
-			goto fail;
+			goto fail_put_device;
 
 		ret = platform_device_add(pdev);
 		if (ret)
-			goto fail;
+			goto fail_put_device;
 
 		acpi_device_set_enumerated(child);
 	}
 
 	return 0;
 
+fail_put_device:
+	platform_device_put(pdev);
 fail:
 	hisi_lpc_acpi_remove(hostdev);
 	return ret;
-- 
2.25.1
Re: [PATCH v2] bus: hisi_lpc: fix missing platform_device_put() in hisi_lpc_acpi_probe()
Posted by John Garry 3 years, 9 months ago
On 04/07/2022 10:34, Yang Yingliang wrote:
> In error case in hisi_lpc_acpi_probe() after calling platform_device_alloc(),
> if platform_device_add() is not called successfully, hisi_lpc_acpi_remove()
> can't release the failed 'pdev' including its resource and data, so they will
> be leaked, call platform_device_put() to put the refcount, then they will be
> freed in platform_device_release().
> I'v constructed this error case and tested this patch on D05 board.

/s/I'v/I've/

> 
> Fixes: 99c0228d6ff1 ("HISI LPC: Re-Add ACPI child enumeration support")
> Reported-by: Hulk Robot<hulkci@huawei.com>
> Signed-off-by: Yang Yingliang<yangyingliang@huawei.com>

Reviewed-by: John Garry <john.garry@huawei.com>