Skip to content

Commit 9d05ae5

Browse files
committed
clk: Initialize struct clk_core kref earlier
Initialize this kref once we allocate memory for the struct clk_core so that we can reuse the release function to free any memory associated with the structure. This mostly consolidates code, but also clarifies that the kref lifetime exists once the container structure (struct clk_core) is allocated instead of leaving it in a half-baked state for most of __clk_core_init(). Reviewed-by: Douglas Anderson <[email protected]> Signed-off-by: Stephen Boyd <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 6f63af7 commit 9d05ae5

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

drivers/clk/clk.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3981,8 +3981,6 @@ static int __clk_core_init(struct clk_core *core)
39813981
}
39823982

39833983
clk_core_reparent_orphans_nolock();
3984-
3985-
kref_init(&core->ref);
39863984
out:
39873985
clk_pm_runtime_put(core);
39883986
unlock:
@@ -4211,6 +4209,16 @@ static void clk_core_free_parent_map(struct clk_core *core)
42114209
kfree(core->parents);
42124210
}
42134211

4212+
/* Free memory allocated for a struct clk_core */
4213+
static void __clk_release(struct kref *ref)
4214+
{
4215+
struct clk_core *core = container_of(ref, struct clk_core, ref);
4216+
4217+
clk_core_free_parent_map(core);
4218+
kfree_const(core->name);
4219+
kfree(core);
4220+
}
4221+
42144222
static struct clk *
42154223
__clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
42164224
{
@@ -4231,6 +4239,8 @@ __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
42314239
goto fail_out;
42324240
}
42334241

4242+
kref_init(&core->ref);
4243+
42344244
core->name = kstrdup_const(init->name, GFP_KERNEL);
42354245
if (!core->name) {
42364246
ret = -ENOMEM;
@@ -4285,12 +4295,10 @@ __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
42854295
hw->clk = NULL;
42864296

42874297
fail_create_clk:
4288-
clk_core_free_parent_map(core);
42894298
fail_parents:
42904299
fail_ops:
4291-
kfree_const(core->name);
42924300
fail_name:
4293-
kfree(core);
4301+
kref_put(&core->ref, __clk_release);
42944302
fail_out:
42954303
return ERR_PTR(ret);
42964304
}
@@ -4370,16 +4378,6 @@ int of_clk_hw_register(struct device_node *node, struct clk_hw *hw)
43704378
}
43714379
EXPORT_SYMBOL_GPL(of_clk_hw_register);
43724380

4373-
/* Free memory allocated for a clock. */
4374-
static void __clk_release(struct kref *ref)
4375-
{
4376-
struct clk_core *core = container_of(ref, struct clk_core, ref);
4377-
4378-
clk_core_free_parent_map(core);
4379-
kfree_const(core->name);
4380-
kfree(core);
4381-
}
4382-
43834381
/*
43844382
* Empty clk_ops for unregistered clocks. These are used temporarily
43854383
* after clk_unregister() was called on a clock and until last clock

0 commit comments

Comments
 (0)