Skip to content

Commit 3a3d2b0

Browse files
author
Sylwester Nawrocki
committed
clkdev: Fix race condition in clock lookup from device tree
There is currently a race condition in the device tree part of clk_get() function, since the pointer returned from of_clk_get_by_name() may become invalid before __clk_get() call. E.g. due to the clock provider driver remove() callback being called in between of_clk_get_by_name() and __clk_get(). Fix this by doing both the look up and __clk_get() operations with the clock providers list mutex held. This ensures that the clock pointer returned from __of_clk_get_from_provider() call and passed to __clk_get() is valid, as long as the clock supplier module first removes its clock provider instance and then does clk_unregister() on the corresponding clocks. Signed-off-by: Sylwester Nawrocki <s.nawrocki@samsung.com> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com> Reviewed-by: Mike Turquette <mturquette@linaro.org> Acked-by: Russell King <rmk+kernel@arm.linux.org.uk>
1 parent d6782c2 commit 3a3d2b0

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

drivers/clk/clkdev.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <linux/clkdev.h>
2222
#include <linux/of.h>
2323

24+
#include "clk.h"
25+
2426
static LIST_HEAD(clocks);
2527
static DEFINE_MUTEX(clocks_mutex);
2628

@@ -39,7 +41,13 @@ struct clk *of_clk_get(struct device_node *np, int index)
3941
if (rc)
4042
return ERR_PTR(rc);
4143

42-
clk = of_clk_get_from_provider(&clkspec);
44+
of_clk_lock();
45+
clk = __of_clk_get_from_provider(&clkspec);
46+
47+
if (!IS_ERR(clk) && !__clk_get(clk))
48+
clk = ERR_PTR(-ENOENT);
49+
50+
of_clk_unlock();
4351
of_node_put(clkspec.np);
4452
return clk;
4553
}
@@ -157,7 +165,7 @@ struct clk *clk_get(struct device *dev, const char *con_id)
157165

158166
if (dev) {
159167
clk = of_clk_get_by_name(dev->of_node, con_id);
160-
if (!IS_ERR(clk) && __clk_get(clk))
168+
if (!IS_ERR(clk))
161169
return clk;
162170
}
163171

0 commit comments

Comments
 (0)