* [PATCH] clk: versatile-icst: release regmap on registration failure
@ 2026-11-19 8:14 Wang Haoran
2026-11-19 14:52 ` Brian Masney
2026-11-24 16:32 ` Brian Masney
0 siblings, 2 replies; 5+ messages in thread
From: Wang Haoran @ 2026-11-19 8:14 UTC (permalink / raw)
To: linusw
Cc: sboyd, bmasney+clk, jbrunet+clk, linux-arm-kernel, linux-clk,
linux-kernel, wanghr2026
icst_clk_register() creates a non-devm regmap with regmap_init_mmio() and
hands it to icst_clk_setup(), which on clk_register() failure only frees
its own structs and drops the regmap pointer, so the regmap and its
debugfs entry leak permanently. A name clash is a natural trigger: a DT
with N same-named auxosc children registers "osc" once and then fails
-EEXIST on every subsequent registration, leaking N-1 regmaps per boot.
Release the regmap with regmap_exit() when icst_clk_setup() fails.
Signed-off-by: Wang Haoran <wanghr2026@mail.zgclab.edu.cn>
Fixes: 179c8fb3c2a6 ("clk: versatile-icst: convert to use regmap")
---
diff --git a/drivers/clk/versatile/clk-icst.c b/drivers/clk/versatile/clk-icst.c
index 37334afee040..e9c7353aa5d5 100644
--- a/drivers/clk/versatile/clk-icst.c
+++ b/drivers/clk/versatile/clk-icst.c
@@ -407,14 +407,18 @@ struct clk *icst_clk_register(struct device *dev,
.reg_stride = 4,
};
struct regmap *map;
+ struct clk *clk;
map = regmap_init_mmio(dev, base, &icst_regmap_conf);
if (IS_ERR(map)) {
pr_err("could not initialize ICST regmap\n");
return ERR_CAST(map);
}
- return icst_clk_setup(dev, desc, name, parent_name, map,
- ICST_VERSATILE);
+ clk = icst_clk_setup(dev, desc, name, parent_name, map, ICST_VERSATILE);
+ if (IS_ERR(clk))
+ regmap_exit(map);
+
+ return clk;
}
EXPORT_SYMBOL_GPL(icst_clk_register);
---
PoC reproduction (7.4.0-rc2, arm64 QEMU virt, unmodified kernel):
Device tree injection, spliced into the QEMU virt machine DT:
reserved-memory { #address-cells = <2>; #size-cells = <2>; ranges;
icst-regs@7f200000 { no-map; reg = <0x0 0x7f200000 0x0 0x1000>; }; };
syscon@7f200000 { compatible = "syscon"; reg = <0x0 0x7f200000 0x0 0x1000>;
osc@0 { compatible = "arm,versatile-cm-auxosc"; #clock-cells = <0>; };
osc@1 { compatible = "arm,versatile-cm-auxosc"; #clock-cells = <0>; }; };
Two same-compatible auxosc children make cm_osc_setup() run twice at boot
via CLK_OF_DECLARE: the first clk_register("osc") succeeds, the second hits
-EEXIST and its regmap is leaked. No trigger action is needed.
Result:
(null): error -EEXIST: failed to register clk 'osc' (0xffff000003407600)
===== LIVE REGMAP INSTANCES =====
drwxr-xr-x dummy0
drwxr-xr-x dummy1
dummy1 is the regmap leaked on the failed registration (dummy0 belongs to
the successfully registered clock). N duplicate nodes leak N-1 regmaps.
Note: the PoC was generated with AI assistance and the vulnerability was
found by static analysis. The device tree is trusted input (a bad DT
requires root or physical access), so this is not a security fix, but it
prevents a potential resource leak or system failure on a failed probe or
an old or malformed DT.
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] clk: versatile-icst: release regmap on registration failure 2026-11-19 8:14 [PATCH] clk: versatile-icst: release regmap on registration failure Wang Haoran @ 2026-11-19 14:52 ` Brian Masney 2026-11-22 9:29 ` 王浩然 2026-11-24 16:32 ` Brian Masney 1 sibling, 1 reply; 5+ messages in thread From: Brian Masney @ 2026-11-19 14:52 UTC (permalink / raw) To: Wang Haoran Cc: linusw, sboyd, bmasney+clk, jbrunet+clk, linux-arm-kernel, linux-clk, linux-kernel, Brian Masney Hi Wang, > icst_clk_register() creates a non-devm regmap with regmap_init_mmio() and > hands it to icst_clk_setup(), which on clk_register() failure only frees > its own structs and drops the regmap pointer, so the regmap and its > debugfs entry leak permanently. A name clash is a natural trigger: a DT > with N same-named auxosc children registers "osc" once and then fails > -EEXIST on every subsequent registration, leaking N-1 regmaps per boot. > > Release the regmap with regmap_exit() when icst_clk_setup() fails. > > Signed-off-by: Wang Haoran <wanghr2026@mail.zgclab.edu.cn> > Fixes: 179c8fb3c2a6 ("clk: versatile-icst: convert to use regmap") > > diff --git a/drivers/clk/versatile/clk-icst.c b/drivers/clk/versatile/clk-icst.c > index 37334afee040..e9c7353aa5d5 100644 > --- a/drivers/clk/versatile/clk-icst.c > +++ b/drivers/clk/versatile/clk-icst.c > @@ -407,14 +407,18 @@ struct clk *icst_clk_register(struct device *dev, > .reg_stride = 4, > }; > struct regmap *map; > + struct clk *clk; > > map = regmap_init_mmio(dev, base, &icst_regmap_conf); > if (IS_ERR(map)) { > pr_err("could not initialize ICST regmap\n"); > return ERR_CAST(map); > } > - return icst_clk_setup(dev, desc, name, parent_name, map, > - ICST_VERSATILE); > + clk = icst_clk_setup(dev, desc, name, parent_name, map, ICST_VERSATILE); > + if (IS_ERR(clk)) > + regmap_exit(map); There is a struct dev here, so devm_regmap_init_mmio() should fix this in a cleaner way. Brian -- Brian Masney <masneyb@kernel.org> ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Re: [PATCH] clk: versatile-icst: release regmap on registration failure 2026-11-19 14:52 ` Brian Masney @ 2026-11-22 9:29 ` 王浩然 2026-11-24 16:35 ` Brian Masney 0 siblings, 1 reply; 5+ messages in thread From: 王浩然 @ 2026-11-22 9:29 UTC (permalink / raw) To: Brian Masney Cc: linusw, sboyd, bmasney+clk, jbrunet+clk, linux-arm-kernel, linux-clk, linux-kernel Thanks for the review. devm_regmap_init_mmio() cannot be used here: the only in-tree caller passes dev == NULL. icst_clk_register() is reached from versatile_auxosc_clk_setup(), registered via CLK_OF_DECLARE: clk = icst_clk_register(NULL, desc, clk_name, parent_name, cm_base); (drivers/clk/versatile/clk-versatile.c:81) That runs at early boot, before any struct device exists, which is why the code uses the non-devm regmap_init_mmio() -- that function has an explicit "dev == NULL" path in __regmap_init(). I verified the proposed change directly: a byte-for-byte copy of icst_clk_register() with regmap_init_mmio() switched to devm_regmap_init_mmio(), called exactly like the real caller (dev == NULL, a real ioremap()'d base). It oopses in devres: Unable to handle kernel NULL pointer dereference at virtual address 00000000000002cc (offsetof(struct device, devres_lock)) ... Call trace: _raw_spin_lock_irqsave+0x5c/0x8c (P) devres_node_add+0x28/0x70 __devm_add_action+0x70/0x90 __devm_regmap_init+0x40/0x68 __devm_regmap_init_mmio_clk+0x84/0x80 icst_clk_register_devm.constprop.0+0x68/0xf8 [test_icst] devres_add()/devres_node_add() dereference dev->devres_lock, so any devm_* call here is a NULL dereference, whereas regmap_init_mmio() handles dev == NULL explicitly. Keeping it non-devm and releasing the regmap with regmap_exit() on the icst_clk_setup() failure path is therefore the correct fix. Regards, Wang Haoran > -----原始邮件----- > 发件人: "Brian Masney" <masneyb@kernel.org> > 发送时间:2026-11-19 22:52:51 (星期四) > 收件人: "Wang Haoran" <wanghr2026@mail.zgclab.edu.cn> > 抄送: linusw@kernel.org, sboyd@kernel.org, bmasney+clk@redhat.com, jbrunet+clk@baylibre.com, linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org, "Brian Masney" <masneyb@kernel.org> > 主题: Re: [PATCH] clk: versatile-icst: release regmap on registration failure > > Hi Wang, > > > icst_clk_register() creates a non-devm regmap with regmap_init_mmio() and > > hands it to icst_clk_setup(), which on clk_register() failure only frees > > its own structs and drops the regmap pointer, so the regmap and its > > debugfs entry leak permanently. A name clash is a natural trigger: a DT > > with N same-named auxosc children registers "osc" once and then fails > > -EEXIST on every subsequent registration, leaking N-1 regmaps per boot. > > > > Release the regmap with regmap_exit() when icst_clk_setup() fails. > > > > Signed-off-by: Wang Haoran <wanghr2026@mail.zgclab.edu.cn> > > Fixes: 179c8fb3c2a6 ("clk: versatile-icst: convert to use regmap") > > > > diff --git a/drivers/clk/versatile/clk-icst.c b/drivers/clk/versatile/clk-icst.c > > index 37334afee040..e9c7353aa5d5 100644 > > --- a/drivers/clk/versatile/clk-icst.c > > +++ b/drivers/clk/versatile/clk-icst.c > > @@ -407,14 +407,18 @@ struct clk *icst_clk_register(struct device *dev, > > .reg_stride = 4, > > }; > > struct regmap *map; > > + struct clk *clk; > > > > map = regmap_init_mmio(dev, base, &icst_regmap_conf); > > if (IS_ERR(map)) { > > pr_err("could not initialize ICST regmap\n"); > > return ERR_CAST(map); > > } > > - return icst_clk_setup(dev, desc, name, parent_name, map, > > - ICST_VERSATILE); > > + clk = icst_clk_setup(dev, desc, name, parent_name, map, ICST_VERSATILE); > > + if (IS_ERR(clk)) > > + regmap_exit(map); > > There is a struct dev here, so devm_regmap_init_mmio() should fix this > in a cleaner way. > > Brian > > -- > Brian Masney <masneyb@kernel.org> ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Re: [PATCH] clk: versatile-icst: release regmap on registration failure 2026-11-22 9:29 ` 王浩然 @ 2026-11-24 16:35 ` Brian Masney 0 siblings, 0 replies; 5+ messages in thread From: Brian Masney @ 2026-11-24 16:35 UTC (permalink / raw) To: 王浩然 Cc: linusw, sboyd, bmasney+clk, jbrunet+clk, linux-arm-kernel, linux-clk, linux-kernel Hi Wang, On Sun, Nov 22, 2026 at 05:29:13PM +0800, 王浩然 wrote: > Thanks for the review. > > > devm_regmap_init_mmio() cannot be used here: the only in-tree caller > passes dev == NULL. icst_clk_register() is reached from > versatile_auxosc_clk_setup(), registered via CLK_OF_DECLARE: > > > clk = icst_clk_register(NULL, desc, clk_name, parent_name, cm_base); > (drivers/clk/versatile/clk-versatile.c:81) I merged your change. I welcome another patch to remove the unused struct dev here. Brian ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] clk: versatile-icst: release regmap on registration failure 2026-11-19 8:14 [PATCH] clk: versatile-icst: release regmap on registration failure Wang Haoran 2026-11-19 14:52 ` Brian Masney @ 2026-11-24 16:32 ` Brian Masney 1 sibling, 0 replies; 5+ messages in thread From: Brian Masney @ 2026-11-24 16:32 UTC (permalink / raw) To: linusw, Wang Haoran Cc: sboyd, bmasney+clk, jbrunet+clk, linux-arm-kernel, linux-clk, linux-kernel, Brian Masney On Thu, 19 Nov 2026 16:14:53 +0800, Wang Haoran wrote: > clk: versatile-icst: release regmap on registration failure Applied, thanks! [1/1] clk: versatile-icst: release regmap on registration failure https://git.kernel.org/clk/linux/c/f0e15ebf8965 Best regards, -- Brian Masney <masneyb@kernel.org> ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-24 16:58 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-19 8:14 [PATCH] clk: versatile-icst: release regmap on registration failure Wang Haoran 2026-11-19 14:52 ` Brian Masney 2026-11-22 9:29 ` 王浩然 2026-11-24 16:35 ` Brian Masney 2026-11-24 16:32 ` Brian Masney
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox