[PATCH] clk: versatile-icst: release regmap on registration failure

王浩然 wanghr2026 at mail.zgclab.edu.cn
Sun Nov 22 01:29:13 PST 2026


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 at kernel.org>
> 发送时间:2026-11-19 22:52:51 (星期四)
> 收件人: "Wang Haoran" <wanghr2026 at mail.zgclab.edu.cn>
> 抄送: linusw at kernel.org, sboyd at kernel.org, bmasney+clk at redhat.com, jbrunet+clk at baylibre.com, linux-arm-kernel at lists.infradead.org, linux-clk at vger.kernel.org, linux-kernel at vger.kernel.org, "Brian Masney" <masneyb at 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 at 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 at kernel.org>


More information about the linux-arm-kernel mailing list