[PATCH] As the doc of of_parse_phandle() states: "The device_node pointer with refcount incremented. Use * of_node_put() on it when done."

Miaoqian Lin posted 1 patch 4 weeks, 1 day ago
drivers/gpu/drm/sysfb/simpledrm.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
[PATCH] As the doc of of_parse_phandle() states: "The device_node pointer with refcount incremented. Use * of_node_put() on it when done."
Posted by Miaoqian Lin 4 weeks, 1 day ago
The function doesn't calls
of_node_put() to release this reference, causing a reference leak.

Move the of_parse_phandle() call after devm_kzalloc() and add the missing
of_node_put() call immediately after of_address_to_resource() to properly
release the device node reference.

Found via static analysis.

Fixes: 9a10c7e6519b ("drm/simpledrm: Add support for system memory framebuffers")
Cc: stable@vger.kernel.org
Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
---
 drivers/gpu/drm/sysfb/simpledrm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/sysfb/simpledrm.c b/drivers/gpu/drm/sysfb/simpledrm.c
index 8530a3ef8a7a..f0bd7e958398 100644
--- a/drivers/gpu/drm/sysfb/simpledrm.c
+++ b/drivers/gpu/drm/sysfb/simpledrm.c
@@ -183,15 +183,16 @@ simplefb_get_memory_of(struct drm_device *dev, struct device_node *of_node)
 	struct resource *res;
 	int err;
 
-	np = of_parse_phandle(of_node, "memory-region", 0);
-	if (!np)
-		return NULL;
-
 	res = devm_kzalloc(dev->dev, sizeof(*res), GFP_KERNEL);
 	if (!res)
 		return ERR_PTR(-ENOMEM);
 
+	np = of_parse_phandle(of_node, "memory-region", 0);
+	if (!np)
+		return NULL;
+
 	err = of_address_to_resource(np, 0, res);
+	of_node_put(np);
 	if (err)
 		return ERR_PTR(err);
 
-- 
2.35.1
Re: [PATCH] As the doc of of_parse_phandle() states: …
Posted by Markus Elfring 4 weeks, 1 day ago
Please choose a more appropriate patch subject.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.17-rc4#n638


> Move the of_parse_phandle() call after devm_kzalloc() and add the missing
> of_node_put() call immediately after of_address_to_resource() to properly
> release the device node reference.

How do you think about to increase the application of scope-based resource management?
https://elixir.bootlin.com/linux/v6.17-rc4/source/include/linux/of.h#L138


> Found via static analysis.

Which concrete software tools would be involved for this purpose?

Regards,
Markus