Message ID | 20220520075737.758761-3-u.kleine-koenig@pengutronix.de (mailing list archive) |
---|---|
State | Accepted, archived |
Headers | show |
Series | clk: provide new devm helpers for prepared and enabled clocks | expand |
Quoting Uwe Kleine-König (2022-05-20 00:57:35) > Allow to add an exit hook to devm managed clocks. Also use > clk_get_optional() in devm_clk_get_optional instead of open coding it. > The generalisation will be used in the next commit to add some more > devm_clk helpers. > > Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> > Reviewed-by: Alexandru Ardelean <aardelean@deviqon.com> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> > --- Applied to clk-next
Hi Uwe, On 20.05.2022 09:57, Uwe Kleine-König wrote: > Allow to add an exit hook to devm managed clocks. Also use > clk_get_optional() in devm_clk_get_optional instead of open coding it. > The generalisation will be used in the next commit to add some more > devm_clk helpers. > > Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> > Reviewed-by: Alexandru Ardelean <aardelean@deviqon.com> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> This patch landed in linux next-20220620 as commit abae8e57e49a ("clk: generalize devm_clk_get() a bit"). Unfortunately it has ugly issue, see my comment in the code below. > --- > drivers/clk/clk-devres.c | 66 +++++++++++++++++++++++++++++----------- > 1 file changed, 49 insertions(+), 17 deletions(-) > > diff --git a/drivers/clk/clk-devres.c b/drivers/clk/clk-devres.c > index f9d5b7334341..c822f4ef1584 100644 > --- a/drivers/clk/clk-devres.c > +++ b/drivers/clk/clk-devres.c > @@ -4,39 +4,71 @@ > #include <linux/export.h> > #include <linux/gfp.h> > > +struct devm_clk_state { > + struct clk *clk; > + void (*exit)(struct clk *clk); > +}; > + > static void devm_clk_release(struct device *dev, void *res) > { > - clk_put(*(struct clk **)res); > + struct devm_clk_state *state = *(struct devm_clk_state **)res; This should be: struct devm_clk_state *state = res; otherwise it nukes badly during cleanup: 8<--- cut here --- Unable to handle kernel paging request at virtual address c254f810 [c254f810] *pgd=4241141e(bad) Internal error: Oops: 8000000d [#1] PREEMPT SMP ARM Modules linked in: CPU: 1 PID: 1 Comm: swapper/0 Not tainted 5.19.0-rc1-00002-gabae8e57e49a #5239 Hardware name: Samsung Exynos (Flattened Device Tree) PC is at 0xc254f810 LR is at devm_clk_release+0x1c/0x28 pc : [<c254f810>] lr : [<c05c93f4>] psr: a0000053 ... Flags: NzCv IRQs on FIQs off Mode SVC_32 ISA ARM Segment none Control: 10c5387d Table: 4000404a DAC: 00000051 ... Process swapper/0 (pid: 1, stack limit = 0x(ptrval)) Stack: (0xf0845dd0 to 0xf0846000) ... devm_clk_release from release_nodes+0x5c/0x80 release_nodes from devres_release_all+0x7c/0xc0 devres_release_all from device_unbind_cleanup+0xc/0x60 device_unbind_cleanup from really_probe+0x150/0x404 really_probe from __driver_probe_device+0xa0/0x208 __driver_probe_device from driver_probe_device+0x34/0xc4 driver_probe_device from __driver_attach+0xf0/0x1e4 __driver_attach from bus_for_each_dev+0x70/0xb0 bus_for_each_dev from bus_add_driver+0x174/0x218 bus_add_driver from driver_register+0x88/0x11c driver_register from do_one_initcall+0x64/0x380 do_one_initcall from kernel_init_freeable+0x1c0/0x224 kernel_init_freeable from kernel_init+0x18/0x12c kernel_init from ret_from_fork+0x14/0x2c Exception stack(0xf0845fb0 to 0xf0845ff8) ... ---[ end trace 0000000000000000 ]--- > + > + if (state->exit) > + state->exit(state->clk); > + > + clk_put(state->clk); > } > > -struct clk *devm_clk_get(struct device *dev, const char *id) > +static struct clk *__devm_clk_get(struct device *dev, const char *id, > + struct clk *(*get)(struct device *dev, const char *id), > + int (*init)(struct clk *clk), > + void (*exit)(struct clk *clk)) > { > - struct clk **ptr, *clk; > + struct devm_clk_state *state; > + struct clk *clk; > + int ret; > > - ptr = devres_alloc(devm_clk_release, sizeof(*ptr), GFP_KERNEL); > - if (!ptr) > + state = devres_alloc(devm_clk_release, sizeof(*state), GFP_KERNEL); > + if (!state) > return ERR_PTR(-ENOMEM); > > - clk = clk_get(dev, id); > - if (!IS_ERR(clk)) { > - *ptr = clk; > - devres_add(dev, ptr); > - } else { > - devres_free(ptr); > + clk = get(dev, id); > + if (IS_ERR(clk)) { > + ret = PTR_ERR(clk); > + goto err_clk_get; > } > > + if (init) { > + ret = init(clk); > + if (ret) > + goto err_clk_init; > + } > + > + state->clk = clk; > + state->exit = exit; > + > + devres_add(dev, state); > + > return clk; > + > +err_clk_init: > + > + clk_put(clk); > +err_clk_get: > + > + devres_free(state); > + return ERR_PTR(ret); > +} > + > +struct clk *devm_clk_get(struct device *dev, const char *id) > +{ > + return __devm_clk_get(dev, id, clk_get, NULL, NULL); > } > EXPORT_SYMBOL(devm_clk_get); > > struct clk *devm_clk_get_optional(struct device *dev, const char *id) > { > - struct clk *clk = devm_clk_get(dev, id); > - > - if (clk == ERR_PTR(-ENOENT)) > - return NULL; > - > - return clk; > + return __devm_clk_get(dev, id, clk_get_optional, NULL, NULL); > } > EXPORT_SYMBOL(devm_clk_get_optional); > Best regards
diff --git a/drivers/clk/clk-devres.c b/drivers/clk/clk-devres.c index f9d5b7334341..c822f4ef1584 100644 --- a/drivers/clk/clk-devres.c +++ b/drivers/clk/clk-devres.c @@ -4,39 +4,71 @@ #include <linux/export.h> #include <linux/gfp.h> +struct devm_clk_state { + struct clk *clk; + void (*exit)(struct clk *clk); +}; + static void devm_clk_release(struct device *dev, void *res) { - clk_put(*(struct clk **)res); + struct devm_clk_state *state = *(struct devm_clk_state **)res; + + if (state->exit) + state->exit(state->clk); + + clk_put(state->clk); } -struct clk *devm_clk_get(struct device *dev, const char *id) +static struct clk *__devm_clk_get(struct device *dev, const char *id, + struct clk *(*get)(struct device *dev, const char *id), + int (*init)(struct clk *clk), + void (*exit)(struct clk *clk)) { - struct clk **ptr, *clk; + struct devm_clk_state *state; + struct clk *clk; + int ret; - ptr = devres_alloc(devm_clk_release, sizeof(*ptr), GFP_KERNEL); - if (!ptr) + state = devres_alloc(devm_clk_release, sizeof(*state), GFP_KERNEL); + if (!state) return ERR_PTR(-ENOMEM); - clk = clk_get(dev, id); - if (!IS_ERR(clk)) { - *ptr = clk; - devres_add(dev, ptr); - } else { - devres_free(ptr); + clk = get(dev, id); + if (IS_ERR(clk)) { + ret = PTR_ERR(clk); + goto err_clk_get; } + if (init) { + ret = init(clk); + if (ret) + goto err_clk_init; + } + + state->clk = clk; + state->exit = exit; + + devres_add(dev, state); + return clk; + +err_clk_init: + + clk_put(clk); +err_clk_get: + + devres_free(state); + return ERR_PTR(ret); +} + +struct clk *devm_clk_get(struct device *dev, const char *id) +{ + return __devm_clk_get(dev, id, clk_get, NULL, NULL); } EXPORT_SYMBOL(devm_clk_get); struct clk *devm_clk_get_optional(struct device *dev, const char *id) { - struct clk *clk = devm_clk_get(dev, id); - - if (clk == ERR_PTR(-ENOENT)) - return NULL; - - return clk; + return __devm_clk_get(dev, id, clk_get_optional, NULL, NULL); } EXPORT_SYMBOL(devm_clk_get_optional);