[PATCH] drivers: Fix reference leak in altr_sysmgr_regmap_lookup_by_phandle()

Ma Ke posted 1 patch 3 months, 2 weeks ago
drivers/mfd/altera-sysmgr.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
[PATCH] drivers: Fix reference leak in altr_sysmgr_regmap_lookup_by_phandle()
Posted by Ma Ke 3 months, 2 weeks ago
altr_sysmgr_regmap_lookup_by_phandle() utilizes
driver_find_device_by_of_node() which internally calls
driver_find_device() to locate the matching device.
driver_find_device() increments the ref count of the found device by
calling get_device(), but altr_sysmgr_regmap_lookup_by_phandle() fails
to call put_device() to decrement the reference count before
returning. This results in a reference count leak of the device, which
may prevent the device from being properly released and cause a memory
leak.

Found by code review.

Cc: stable@vger.kernel.org
Fixes: cfba5de9b99f ("drivers: Introduce device lookup variants by of_node")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
 drivers/mfd/altera-sysmgr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/mfd/altera-sysmgr.c b/drivers/mfd/altera-sysmgr.c
index fb5f988e61f3..c6c763fb7bbe 100644
--- a/drivers/mfd/altera-sysmgr.c
+++ b/drivers/mfd/altera-sysmgr.c
@@ -98,6 +98,7 @@ struct regmap *altr_sysmgr_regmap_lookup_by_phandle(struct device_node *np,
 	struct device *dev;
 	struct altr_sysmgr *sysmgr;
 	struct device_node *sysmgr_np;
+	struct regmap *regmap;
 
 	if (property)
 		sysmgr_np = of_parse_phandle(np, property, 0);
@@ -116,8 +117,10 @@ struct regmap *altr_sysmgr_regmap_lookup_by_phandle(struct device_node *np,
 		return ERR_PTR(-EPROBE_DEFER);
 
 	sysmgr = dev_get_drvdata(dev);
+	regmap = sysmgr->regmap;
+	put_device(dev);
 
-	return sysmgr->regmap;
+	return regmap;
 }
 EXPORT_SYMBOL_GPL(altr_sysmgr_regmap_lookup_by_phandle);
 
-- 
2.17.1
Re: [PATCH] drivers: Fix reference leak in altr_sysmgr_regmap_lookup_by_phandle()
Posted by Markus Elfring 3 months, 2 weeks ago
…
> Found by code review.

See also the commit facd37d3882baad4a38afdd5f33908f6fc145d13
("mfd: altera-sysmgr: Fix device leak on sysmgr regmap lookup") from 2025-10-21.

Regards,
Markus
Re: [PATCH] drivers: Fix reference leak in altr_sysmgr_regmap_lookup_by_phandle()
Posted by Lee Jones 3 months ago
On Sat, 25 Oct 2025, Markus Elfring wrote:

> …
> > Found by code review.
> 
> See also the commit facd37d3882baad4a38afdd5f33908f6fc145d13
> ("mfd: altera-sysmgr: Fix device leak on sysmgr regmap lookup") from 2025-10-21.

This patch has the correct formatting and doesn't do extra, unnecessary
actions like store the regmap into a local variable for some unexplained
reason.  For those reasons, I'm planning on taking this one instead.

-- 
Lee Jones [李琼斯]