Skip to content

Commit 798ad2a

Browse files
bebarinogregkh
authored andcommitted
clk: Fix double free due to devm_clk_register()
commit 293ba3b upstream. Now that clk_unregister() frees the struct clk we're unregistering we'll free memory twice: first we'll call kfree() in __clk_release() with an address kmalloc doesn't know about and second we'll call kfree() in the devres layer. Remove the allocation of struct clk in devm_clk_register() and let clk_release() handle it. This fixes slab errors like: ============================================================================= BUG kmalloc-128 (Not tainted): Invalid object pointer 0xed08e8d0 ----------------------------------------------------------------------------- Disabling lock debugging due to kernel taint INFO: Slab 0xeec503f8 objects=25 used=15 fp=0xed08ea00 flags=0x4081 CPU: 2 PID: 73 Comm: rmmod Tainted: G B 3.14.0-11032-g526e9c764381 #34 [<c0014be0>] (unwind_backtrace) from [<c0012240>] (show_stack+0x10/0x14) [<c0012240>] (show_stack) from [<c04b74dc>] (dump_stack+0x70/0xbc) [<c04b74dc>] (dump_stack) from [<c00f6778>] (slab_err+0x74/0x84) [<c00f6778>] (slab_err) from [<c04b6278>] (free_debug_processing+0x2cc/0x31c) [<c04b6278>] (free_debug_processing) from [<c04b6300>] (__slab_free+0x38/0x41c) [<c04b6300>] (__slab_free) from [<c03931bc>] (clk_unregister+0xd4/0x140) [<c03931bc>] (clk_unregister) from [<c02fb774>] (release_nodes+0x164/0x1d8) [<c02fb774>] (release_nodes) from [<c02f8698>] (__device_release_driver+0x60/0xb0) [<c02f8698>] (__device_release_driver) from [<c02f9080>] (driver_detach+0xb4/0xb8) [<c02f9080>] (driver_detach) from [<c02f8480>] (bus_remove_driver+0x5c/0xc4) [<c02f8480>] (bus_remove_driver) from [<c008c9b8>] (SyS_delete_module+0x148/0x1d8) [<c008c9b8>] (SyS_delete_module) from [<c000ef80>] (ret_fast_syscall+0x0/0x48) FIX kmalloc-128: Object at 0xed08e8d0 not freed Fixes: fcb0ee6 (clk: Implement clk_unregister) Cc: Jiada Wang <[email protected]> Cc: Sylwester Nawrocki <[email protected]> Cc: Kyungmin Park <[email protected]> Signed-off-by: Stephen Boyd <[email protected]> Signed-off-by: Mike Turquette <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 5549414 commit 798ad2a

File tree

1 file changed

+30
-41
lines changed

1 file changed

+30
-41
lines changed

drivers/clk/clk.c

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,9 +1977,28 @@ struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
19771977
}
19781978
EXPORT_SYMBOL_GPL(__clk_register);
19791979

1980-
static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
1980+
/**
1981+
* clk_register - allocate a new clock, register it and return an opaque cookie
1982+
* @dev: device that is registering this clock
1983+
* @hw: link to hardware-specific clock data
1984+
*
1985+
* clk_register is the primary interface for populating the clock tree with new
1986+
* clock nodes. It returns a pointer to the newly allocated struct clk which
1987+
* cannot be dereferenced by driver code but may be used in conjuction with the
1988+
* rest of the clock API. In the event of an error clk_register will return an
1989+
* error code; drivers must test for an error code after calling clk_register.
1990+
*/
1991+
struct clk *clk_register(struct device *dev, struct clk_hw *hw)
19811992
{
19821993
int i, ret;
1994+
struct clk *clk;
1995+
1996+
clk = kzalloc(sizeof(*clk), GFP_KERNEL);
1997+
if (!clk) {
1998+
pr_err("%s: could not allocate clk\n", __func__);
1999+
ret = -ENOMEM;
2000+
goto fail_out;
2001+
}
19832002

19842003
clk->name = kstrdup(hw->init->name, GFP_KERNEL);
19852004
if (!clk->name) {
@@ -2019,7 +2038,7 @@ static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
20192038

20202039
ret = __clk_init(dev, clk);
20212040
if (!ret)
2022-
return 0;
2041+
return clk;
20232042

20242043
fail_parent_names_copy:
20252044
while (--i >= 0)
@@ -2028,36 +2047,6 @@ static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
20282047
fail_parent_names:
20292048
kfree(clk->name);
20302049
fail_name:
2031-
return ret;
2032-
}
2033-
2034-
/**
2035-
* clk_register - allocate a new clock, register it and return an opaque cookie
2036-
* @dev: device that is registering this clock
2037-
* @hw: link to hardware-specific clock data
2038-
*
2039-
* clk_register is the primary interface for populating the clock tree with new
2040-
* clock nodes. It returns a pointer to the newly allocated struct clk which
2041-
* cannot be dereferenced by driver code but may be used in conjuction with the
2042-
* rest of the clock API. In the event of an error clk_register will return an
2043-
* error code; drivers must test for an error code after calling clk_register.
2044-
*/
2045-
struct clk *clk_register(struct device *dev, struct clk_hw *hw)
2046-
{
2047-
int ret;
2048-
struct clk *clk;
2049-
2050-
clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2051-
if (!clk) {
2052-
pr_err("%s: could not allocate clk\n", __func__);
2053-
ret = -ENOMEM;
2054-
goto fail_out;
2055-
}
2056-
2057-
ret = _clk_register(dev, hw, clk);
2058-
if (!ret)
2059-
return clk;
2060-
20612050
kfree(clk);
20622051
fail_out:
20632052
return ERR_PTR(ret);
@@ -2166,7 +2155,7 @@ EXPORT_SYMBOL_GPL(clk_unregister);
21662155

21672156
static void devm_clk_release(struct device *dev, void *res)
21682157
{
2169-
clk_unregister(res);
2158+
clk_unregister(*(struct clk **)res);
21702159
}
21712160

21722161
/**
@@ -2181,18 +2170,18 @@ static void devm_clk_release(struct device *dev, void *res)
21812170
struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
21822171
{
21832172
struct clk *clk;
2184-
int ret;
2173+
struct clk **clkp;
21852174

2186-
clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL);
2187-
if (!clk)
2175+
clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2176+
if (!clkp)
21882177
return ERR_PTR(-ENOMEM);
21892178

2190-
ret = _clk_register(dev, hw, clk);
2191-
if (!ret) {
2192-
devres_add(dev, clk);
2179+
clk = clk_register(dev, hw);
2180+
if (!IS_ERR(clk)) {
2181+
*clkp = clk;
2182+
devres_add(dev, clkp);
21932183
} else {
2194-
devres_free(clk);
2195-
clk = ERR_PTR(ret);
2184+
devres_free(clkp);
21962185
}
21972186

21982187
return clk;

0 commit comments

Comments
 (0)