* [RESEND PATCH v2] sparc: Fix leaked platform_device references from of_find_device_by_node()
@ 2026-12-02 8:45 Ma Ke
2026-12-02 8:53 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Ma Ke @ 2026-12-02 8:45 UTC (permalink / raw)
To: davem, andreas, o-takashi, make_ruc2021, u.kleine-koenig,
bhelgaas, dakr
Cc: sparclinux, akpm, stable
of_find_device_by_node() is documented in drivers/of/platform.c:
* Takes a reference to the embedded struct device which needs to be
* dropped after use.
Several functions in arch/sparc/kernel/of_device_common.c look a
device up that way and never drop the reference:
irq_of_parse_and_map(), of_address_to_resource(), of_iomap() and
of_propagate_archdata(). On sparc these are the arch overrides of the
generic OF helpers, so a reference leaks on every interrupt mapping,
address translation and ioremap, and of_propagate_archdata()
additionally leaks one per child node because it recurses.
Release the reference once the value has been read out of the device.
Found by code review.
Fixes: 44266215e3c8 ("sparc: Implement irq_of_parse_and_map() and irq_dispose_mapping().")
Fixes: 930952af15f8 ("sparc: Implement of_address_to_resource().")
Fixes: 9889376ab91f ("sparc: Implement of_iomap().")
Fixes: 5059625ed886 ("sparc: Add OF archdata propagation helper.")
Cc: stable@vger.kernel.org
Signed-off-by: Ma Ke <make_ruc2021@163.com>
---
Changes in v2:
- Cover irq_of_parse_and_map(), of_address_to_resource() and
of_iomap() as well. They are in the same file and leaked the
reference in the same way, so the subject was widened from "in
archdata propagation" to match, and the Fixes tags are now one per
function.
- of_create_pci_dev() in arch/sparc/kernel/pci.c looks similar but is
not. It hands the device to pci_init_dev_archdata(), which stores it
in sd->op, and keeps using it afterwards, so the reference is
deliberately held there.
---
arch/sparc/kernel/of_device_common.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/arch/sparc/kernel/of_device_common.c b/arch/sparc/kernel/of_device_common.c
index 7f2f9972b59b..e2e1c1e97f90 100644
--- a/arch/sparc/kernel/of_device_common.c
+++ b/arch/sparc/kernel/of_device_common.c
@@ -15,11 +15,15 @@
unsigned int irq_of_parse_and_map(struct device_node *node, int index)
{
struct platform_device *op = of_find_device_by_node(node);
+ unsigned int irq;
if (!op || index >= op->archdata.num_irqs)
return 0;
- return op->archdata.irqs[index];
+ irq = op->archdata.irqs[index];
+ platform_device_put(op);
+
+ return irq;
}
EXPORT_SYMBOL(irq_of_parse_and_map);
@@ -32,6 +36,8 @@ int of_address_to_resource(struct device_node *node, int index,
return -EINVAL;
memcpy(r, &op->archdata.resource[index], sizeof(*r));
+ platform_device_put(op);
+
return 0;
}
EXPORT_SYMBOL_GPL(of_address_to_resource);
@@ -40,13 +46,16 @@ void __iomem *of_iomap(struct device_node *node, int index)
{
struct platform_device *op = of_find_device_by_node(node);
struct resource *r;
+ void __iomem *ret;
if (!op || index >= op->num_resources)
return NULL;
r = &op->archdata.resource[index];
+ ret = of_ioremap(r, 0, resource_size(r), (char *) r->name);
+ platform_device_put(op);
- return of_ioremap(r, 0, resource_size(r), (char *) r->name);
+ return ret;
}
EXPORT_SYMBOL(of_iomap);
@@ -70,6 +79,8 @@ void of_propagate_archdata(struct platform_device *bus)
if (dp->child)
of_propagate_archdata(op);
+
+ platform_device_put(op);
}
}
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [RESEND PATCH v2] sparc: Fix leaked platform_device references from of_find_device_by_node()
2026-12-02 8:45 [RESEND PATCH v2] sparc: Fix leaked platform_device references from of_find_device_by_node() Ma Ke
@ 2026-12-02 8:53 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 8:53 UTC (permalink / raw)
To: Ma Ke; +Cc: sparclinux
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] sparc: platform_device ref leak on out-of-bounds index in OF
helpers
--
commit 0bdd0e6adc60c51f4dd86a3e1f30c5aaeddff7ad
Author: Ma Ke <make_ruc2021@163.com>
sparc: Fix leaked platform_device references from of_find_device_by_node()
This commit adds platform_device_put() calls to irq_of_parse_and_map(),
of_address_to_resource(), of_iomap(), and of_propagate_archdata() to drop
the reference acquired by of_find_device_by_node().
> diff --git a/arch/sparc/kernel/of_device_common.c b/arch/sparc/kernel/of_device_common.c
> index e717a56efc5d3..818bff54c2ecc 100644
> --- a/arch/sparc/kernel/of_device_common.c
> +++ b/arch/sparc/kernel/of_device_common.c
> @@ -16,11 +16,15 @@
> unsigned int irq_of_parse_and_map(struct device_node *node, int index)
> {
> struct platform_device *op = of_find_device_by_node(node);
> + unsigned int irq;
>
> if (!op || index >= op->archdata.num_irqs)
> return 0;
>
> - return op->archdata.irqs[index];
> + irq = op->archdata.irqs[index];
> + platform_device_put(op);
> +
> + return irq;
> }
> EXPORT_SYMBOL(irq_of_parse_and_map);
>
> @@ -33,6 +37,8 @@ int of_address_to_resource(struct device_node *node, int index,
> return -EINVAL;
>
> memcpy(r, &op->archdata.resource[index], sizeof(*r));
> + platform_device_put(op);
> +
> return 0;
> }
> EXPORT_SYMBOL_GPL(of_address_to_resource);
> @@ -41,13 +47,16 @@ void __iomem *of_iomap(struct device_node *node, int index)
> {
> struct platform_device *op = of_find_device_by_node(node);
> struct resource *r;
> + void __iomem *ret;
>
> if (!op || index >= op->num_resources)
> return NULL;
>
> r = &op->archdata.resource[index];
> + ret = of_ioremap(r, 0, resource_size(r), (char *) r->name);
> + platform_device_put(op);
>
> - return of_ioremap(r, 0, resource_size(r), (char *) r->name);
> + return ret;
> }
[Severity: Medium]
Do irq_of_parse_and_map(), of_address_to_resource(), and of_iomap() still
leak op when op is non-NULL and index is out of bounds?
Callers such as of_device_alloc() probe resources by incrementing index
until of_address_to_resource() returns -EINVAL, hitting the early return
with a valid op on every call.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202084556.1829535-1-make_ruc2021@163.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 8:53 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 8:45 [RESEND PATCH v2] sparc: Fix leaked platform_device references from of_find_device_by_node() Ma Ke
2026-12-02 8:53 ` sashiko-bot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.