[PATCH] clk: ti: Fix reference imbalance in ti_find_clock_provider

From: Jan Kiszka
Date: Mon Aug 08 2022 - 18:27:10 EST


From: Jan Kiszka <jan.kiszka@xxxxxxxxxxx>

When a clock is found via clock-output-names, we need to reference it
explicitly to match of_find_node_by_name behavior. Failing to do so
causes warnings like this:

------------[ cut here ]------------
WARNING: CPU: 0 PID: 0 at lib/refcount.c:25 kobject_get+0x9c/0xa0
refcount_t: addition on 0; use-after-free.
Modules linked in:
CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.19.0+ #37
Hardware name: Generic AM33XX (Flattened Device Tree)
unwind_backtrace from show_stack+0x10/0x14
show_stack from dump_stack_lvl+0x40/0x4c
dump_stack_lvl from __warn+0xc8/0x13c
__warn from warn_slowpath_fmt+0x78/0xa8
warn_slowpath_fmt from kobject_get+0x9c/0xa0
kobject_get from of_node_get+0x14/0x1c
of_node_get from of_fwnode_get+0x34/0x40
of_fwnode_get from fwnode_full_name_string+0x34/0xa0
fwnode_full_name_string from device_node_string+0x604/0x6d0
device_node_string from pointer+0x38c/0x5b0
pointer from vsnprintf+0x230/0x3cc
vsnprintf from vprintk_store+0x10c/0x3fc
vprintk_store from vprintk_emit+0x70/0x240
vprintk_emit from vprintk_default+0x20/0x28
vprintk_default from _printk+0x2c/0x5c
_printk from of_node_release+0x11c/0x124
of_node_release from kobject_put+0x98/0x104
kobject_put from ti_dt_clocks_register+0x2d8/0x3a8
ti_dt_clocks_register from am33xx_dt_clk_init+0x14/0xa4
am33xx_dt_clk_init from omap_init_time_of+0x8/0x10
omap_init_time_of from start_kernel+0x4f8/0x6b0
start_kernel from 0x0
---[ end trace 0000000000000000 ]---

Fixes: 51f661ef9a10 ("clk: ti: Add ti_find_clock_provider() to use clock-output-names")
Signed-off-by: Jan Kiszka <jan.kiszka@xxxxxxxxxxx>
---

Tested against 5.19 as at least current Linus master does not boot on
the BeagleBone Black.

drivers/clk/ti/clk.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/ti/clk.c b/drivers/clk/ti/clk.c
index ef2a445c63a3..5261642974df 100644
--- a/drivers/clk/ti/clk.c
+++ b/drivers/clk/ti/clk.c
@@ -142,8 +142,10 @@ static struct device_node *ti_find_clock_provider(struct device_node *from,
of_node_put(from);
kfree(tmp);

- if (found)
+ if (found) {
+ of_node_get(np);
return np;
+ }

/* Fall back to using old node name base provider name */
return of_find_node_by_name(from, name);
--
2.35.3