Skip to contentSkip to conversation

clk: Assorted bug fixes across clock core and drivers

PATCH In review Lee Jones 5 participants 15 messages Nov 23

Subsystems (10)
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

cover clk: Assorted bug fixes across clock core and drivers

These issues were reported by Sashiko.

None of these changes have been tested on physical hardware.

Cc "Uwe Kleine-König" <[email protected]>
Cc "Zhao, Linaro" <[email protected]>
Cc Abel Vesa <[email protected]>
Cc Alim Akhtar <[email protected]>
Cc Amit Daniel Kachhap <[email protected]>
Cc Anirudh Srinivasan <[email protected]>
Cc Brian Masney <[email protected]>
Cc Chander Kashyap <[email protected]>
Cc Chanwoo Choi <[email protected]>
Cc Dong Aisheng <[email protected]>
Cc Drew Fustini <[email protected]>
Cc Fabio Estevam <[email protected]>
Cc Francesco Dolcini <[email protected]>
Cc Frank Li <[email protected]>
Cc Heiko Stuebner <[email protected]>
Cc Jerome Brunet <[email protected]>
Cc Joel Stanley <[email protected]>
Cc Jonathan Hunter <[email protected]>
Cc Jyri Sarha <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Lad Prabhakar <[email protected]>
Cc Liam Beguin <[email protected]>
Cc Linaro Ltd <[email protected]>
Cc Lubomir Rintel <[email protected]>
Cc Lukasz Luba <[email protected]>
Cc Marian Mihailescu <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Mike Looijmans <[email protected]>
Cc Peng Fan <[email protected]>
Cc Pengpeng Hou <[email protected]>
Cc Pengutronix Kernel Team <[email protected]>
Cc Peter Griffin <[email protected]>
Cc Prashant Gaikwad <[email protected]>
Cc Rosen Penev <[email protected]>
Cc Sascha Hauer <[email protected]>
Cc Sergej Sawazki <[email protected]>
Cc Shawn Guo <[email protected]>
Cc Stephen Boyd <[email protected]>
Cc Sylwester Nawrocki <[email protected]>
Cc Thierry Reding <[email protected]>
Cc Thomas Abraham <[email protected]>
Cc Tomasz Figa <[email protected]>

Lee Jones (10):
clk: imx: imx8qxp-lpcg: Prevent stack overflow when parsing clocks
clk: lmk04832: Validate child reg index in lmk04832_probe()
clk: tegra: bpmp: Bound num_parents by MRQ_CLK_MAX_PARENTS
clk: composite: Initialise sub-hw pointers in determine_rate()
clk: divider: Avoid division by zero in _div_round_closest()
clk: gpio: Reject gate clocks with multiple parents
clk: samsung: exynos5420: Terminate PLL rate tables with sentinels
clk: tenstorrent: Poll atomically in atlantis_clk_pll_enable()
clk: cdce925: Use devm_of_clk_add_hw_provider() to avoid UAF on unbind
clk: mmp: audio: Use devm_of_clk_add_hw_provider() to prevent UAF

 drivers/clk/clk-cdce925.c               |  4 ++--
 drivers/clk/clk-composite.c             |  2 ++
 drivers/clk/clk-divider.c               |  3 +++
 drivers/clk/clk-gpio.c                  |  3 +++
 drivers/clk/clk-lmk04832.c              | 10 ++++++++--
 drivers/clk/imx/clk-imx8qxp-lpcg.c      |  4 ++++
 drivers/clk/mmp/clk-audio.c             |  4 ++--
 drivers/clk/samsung/clk-exynos5420.c    |  4 ++++
 drivers/clk/tegra/clk-bpmp.c            | 14 +++++++-------
 drivers/clk/tenstorrent/atlantis-prcm.c |  9 +++++----
 10 files changed, 40 insertions(+), 17 deletions(-)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#1 lore raw

PATCH 1/10 clk: imx: imx8qxp-lpcg: Prevent stack overflow when parsing clocks

imx_lpcg_parse_clks_from_dt() allocates output_names, parent_names, and bit_offset on the stack with IMX_LPCG_MAX_CLKS (8) entries each to match the hardware limit of eight clock outputs per LPCG block. However, it only checks whether of_property_count_u32_elems() returns a negative error code and enforces no upper bound on count.

If a Device Tree node specifies more than IMX_LPCG_MAX_CLKS entries in clock-indices, subsequent calls to of_property_read_u32_array(), of_clk_parent_fill(), and of_property_read_string_array() write past the end of these stack buffers. Reject counts exceeding IMX_LPCG_MAX_CLKS.

Fixes d5f1e6a2bb61 ("clk: imx: imx8qxp-lpcg: add parsing clocks from device tree")
---
 drivers/clk/imx/clk-imx8qxp-lpcg.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/clk/imx/clk-imx8qxp-lpcg.c +4 -0
index 9ad016106b1e..5eb4bdbaf7d8 100644
@@ -199,6 +199,10 @@ static int imx_lpcg_parse_clks_from_dt(struct platform_device *pdev,
 		dev_err(&pdev->dev, "failed to count clocks\n");
 		return -EINVAL;
 	}
+	if (count > IMX_LPCG_MAX_CLKS) {
+		dev_err(&pdev->dev, "too many clocks: %d\n", count);
+		return -EINVAL;
+	}
 
 	/*
 	 * A trick here is that we set the num of clks to the MAX instead
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Reviewed-by on v1 1/10
#2 lore raw

Re: [PATCH 01/10] clk: imx: imx8qxp-lpcg: Prevent stack overflow when parsing clocks

Quoted 11 lines · Lee Jones

imx_lpcg_parse_clks_from_dt() allocates output_names, parent_names, and bit_offset on the stack with IMX_LPCG_MAX_CLKS (8) entries each to match the hardware limit of eight clock outputs per LPCG block. However, it only checks whether of_property_count_u32_elems() returns a negative error code and enforces no upper bound on count.

If a Device Tree node specifies more than IMX_LPCG_MAX_CLKS entries in clock-indices, subsequent calls to of_property_read_u32_array(), of_clk_parent_fill(), and of_property_read_string_array() write past the end of these stack buffers. Reject counts exceeding IMX_LPCG_MAX_CLKS.

Quoted 5 lines
Fixes d5f1e6a2bb61 ("clk: imx: imx8qxp-lpcg: add parsing clocks from device tree")

---

Quoted 22 lines
 drivers/clk/imx/clk-imx8qxp-lpcg.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/clk/imx/clk-imx8qxp-lpcg.c +4 -1
index 9ad016106b1e..5eb4bdbaf7d8 100644
@@ -199,6 +199,10 @@ static int imx_lpcg_parse_clks_from_dt(struct platform_device *pdev,
 		dev_err(&pdev->dev, "failed to count clocks\n");
 		return -EINVAL;
 	}
+	if (count > IMX_LPCG_MAX_CLKS) {
+		dev_err(&pdev->dev, "too many clocks: %d\n", count);
+		return -EINVAL;
+	}
 
 	/*
 	 * A trick here is that we set the num of clks to the MAX instead
--

2.56.1.750.gf70dc682cf-goog

author
#3 lore raw

PATCH 2/10 clk: lmk04832: Validate child reg index in lmk04832_probe()

In reply to Lee Jones

lmk04832_probe() allocates lmk->clkout with info->num_channels (14) elements, then iterates over child Device Tree nodes and reads each child's "reg" property into a signed integer without bounds validation. It immediately indexes lmk->clkout[reg] to populate .format and .sysref, causing a heap out-of-bounds write if "reg" is greater than or equal to info->num_channels (or negative when interpreted as a signed int).

Read "reg" as u32 and reject values greater than or equal to info->num_channels before indexing lmk->clkout.

Fixes 3bc61cfd6f4a ("clk: add support for the lmk04832")
---
 drivers/clk/clk-lmk04832.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/clk/clk-lmk04832.c +8 -2
index e6e771e8eb14..757b0eba739f 100644
@@ -1453,7 +1453,7 @@ static int lmk04832_probe(struct spi_device *spi)
 				 &lmk->sysref_pulse_cnt);
 
 	for_each_child_of_node_scoped(lmk->dev->of_node, child) {
-		int reg;
+		u32 reg;
 
 		ret = of_property_read_u32(child, "reg", &reg);
 		if (ret) {
@@ -1462,12 +1462,18 @@ static int lmk04832_probe(struct spi_device *spi)
 			return ret;
 		}
 
+		if (reg >= info->num_channels) {
+			dev_err(lmk->dev, "invalid reg %u in child: %s\n",
+				reg, child->full_name);
+			return -EINVAL;
+		}
+
 		of_property_read_u32(child, "ti,clkout-fmt",
 				     &lmk->clkout[reg].format);
 
 		if (lmk->clkout[reg].format >= 0x0a && reg % 2 == 0
 		    && reg != 8 && reg != 10)
-			dev_err(lmk->dev, "invalid format for clkout%02d\n",
+			dev_err(lmk->dev, "invalid format for clkout%02u\n",
 				reg);
 
 		lmk->clkout[reg].sysref =
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#4 lore raw

PATCH 3/10 clk: tegra: bpmp: Bound num_parents by MRQ_CLK_MAX_PARENTS

In reply to Lee Jones

Both cmd_clk_get_all_info_response and tegra_bpmp_clk_info size their parents arrays to MRQ_CLK_MAX_PARENTS (16), whereas response.num_parents is an 8-bit unsigned integer received from BPMP firmware.

tegra_bpmp_clk_get_info() copies response.num_parents entries from the stack response into the heap info structure without checking against MRQ_CLK_MAX_PARENTS, and the caller only checks against U8_MAX after the copy has already overflowed both arrays.

Validate that response.num_parents does not exceed MRQ_CLK_MAX_PARENTS before copying parent IDs, and drop the redundant U8_MAX check.

Fixes ca6f2796eef7 ("clk: tegra: Add BPMP clock driver")
---
 drivers/clk/tegra/clk-bpmp.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/clk/tegra/clk-bpmp.c +7 -7
index 0bb28f96274f..3649324e2e1f 100644
@@ -376,6 +376,13 @@ static int tegra_bpmp_clk_get_info(struct tegra_bpmp *bpmp, unsigned int id,
 			return -E2BIG;
 	}
 
+	if (response.num_parents > MRQ_CLK_MAX_PARENTS) {
+		dev_err(bpmp->dev,
+			"clock %u has too many parents (%u, max: %u)\n",
+			id, response.num_parents, MRQ_CLK_MAX_PARENTS);
+		return -EINVAL;
+	}
+
 	info->num_parents = response.num_parents;
 
 	for (i = 0; i < info->num_parents; i++)
@@ -453,13 +460,6 @@ static int tegra_bpmp_probe_clocks(struct tegra_bpmp *bpmp,
 		if (err < 0)
 			continue;
 
-		if (info->num_parents >= U8_MAX) {
-			dev_err(bpmp->dev,
-				"clock %u has too many parents (%u, max: %u)\n",
-				id, info->num_parents, U8_MAX);
-			continue;
-		}
-
 		/* clock not exposed by BPMP */
 		if (info->name[0] == '\0') {
 			holes++;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#5 lore raw

PATCH 4/10 clk: composite: Initialise sub-hw pointers in determine_rate()

In reply to Lee Jones

__clk_hw_register_composite() leaves mux_hw->core and rate_hw->core unset at registration time, relying on the composite callbacks to bind them via __clk_hw_set_clk(). While the fallback branches of clk_composite_determine_rate() and clk_composite_set_rate_and_parent() call __clk_hw_set_clk(), the primary rate + mux branch omits it.

When a composite clock with both rate and mux operations has only a single parent, __clk_init_parent() skips .get_parent(), leaving mux_hw->core NULL. Subsequent calls to clk_hw_get_parent(mux_hw) or clk_hw_get_num_parents(mux_hw) in clk_composite_determine_rate() then dereference NULL.

Call __clk_hw_set_clk() for both rate_hw and mux_hw in the primary branch of clk_composite_determine_rate().

Fixes 2f508a955a67 ("clk: Convert basic types to clk_hw based provider APIs")
---
 drivers/clk/clk-composite.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/clk/clk-composite.c +2 -0
index 11842bce5918..c7c1ff590e14 100644
@@ -69,6 +69,8 @@ static int clk_composite_determine_rate(struct clk_hw *hw,
 
 	if (rate_hw && rate_ops && rate_ops->determine_rate &&
 	    mux_hw && mux_ops && mux_ops->set_parent) {
+		__clk_hw_set_clk(rate_hw, hw);
+		__clk_hw_set_clk(mux_hw, hw);
 		req->best_parent_hw = NULL;
 
 		if (clk_hw_get_flags(hw) & CLK_SET_RATE_NO_REPARENT) {
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#6 lore raw

PATCH 5/10 clk: divider: Avoid division by zero in _div_round_closest()

In reply to Lee Jones

When CLK_SET_RATE_PARENT is not set on a CLK_DIVIDER_ROUND_CLOSEST divider, clk_divider_bestdiv() invokes _div_round_closest() before clamping the resulting divisor to at least 1. If the requested rate exceeds parent_rate (for instance via clk_round_rate(clk, ULONG_MAX)) or parent_rate is zero, parent_rate / rate evaluates to 0.

Passing down == 0 into __rounddown_pow_of_two() triggers an undefined shift and truncates back to 0, after which DIV_ROUND_UP_ULL() divides by zero.

Since a divider cannot increase its parent rate, return the minimum valid divisor directly whenever rate is greater than or equal to parent_rate.

Fixes 9315514252a9 ("clk: divider: fix calculation of initial best divider when rounding to closest")
---
 drivers/clk/clk-divider.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/clk/clk-divider.c +3 -0
index f68216c65d3d..d05dc595ec4c 100644
@@ -244,6 +244,9 @@ static int _div_round_closest(const struct clk_div_table *table,
 	int up, down;
 	unsigned long up_rate, down_rate;
 
+	if (rate >= parent_rate)
+		return table ? _round_up_table(table, 1) : 1;
+
 	up = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
 	down = parent_rate / rate;
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#7 lore raw

Re: [PATCH 05/10] clk: divider: Avoid division by zero in _div_round_closest()

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] clk: divider: 64-bit addition overflow…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] clk: divider: 64-bit addition overflow in _div_round() and divider_get_val() [1]
--

commit 794cea87b2763d8416c120885c5c264136bf9dfd
Author: Lee Jones <[email protected]>

clk: divider: Avoid division by zero in _div_round_closest()

This commit returns the minimum valid divisor from _div_round_closest() when the requested rate is greater than or equal to parent_rate, avoiding a zero divisor and subsequent division by zero.

drivers/clk/clk-divider.c static int _div_round_closest(const struct clk_div_table *table, · line 251

Quoted 13 lines
drivers/clk/clk-divider.c +3 -0
index f68216c65d3df..d05dc595ec4c7 100644
@@ -244,6 +244,9 @@ static int _div_round_closest(const struct clk_div_table *table,
 	int up, down;
 	unsigned long up_rate, down_rate;
 
+	if (rate >= parent_rate)
+		return table ? _round_up_table(table, 1) : 1;
+
 	up = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
 	down = parent_rate / rate;

[Severity: High]
This problem wasn't introduced by this patch, but can DIV_ROUND_UP_ULL() still overflow 64-bit addition in _div_round_up() and divider_get_val() [1]?

drivers/clk/clk-divider.c:_div_round_up() {
	int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);

	if (flags & CLK_DIVIDER_POWER_OF_TWO)
		div = __roundup_pow_of_two(div);
	...
}

On 64-bit platforms, when rate > U64_MAX - parent_rate + 1 (for example, via clk_round_rate(clk, ULONG_MAX) or when clk_divider_bestdiv() clamps an overflowing target_parent_rate to ULONG_MAX), (u64)parent_rate + rate - 1 wraps around and DIV_ROUND_UP_ULL() evaluates to 0 instead of 1. That passes 0 to __roundup_pow_of_two() when CLK_DIVIDER_POWER_OF_TWO is set (triggering an undefined shift) or underflows div - 1 in _get_val() when called from divider_get_val().

[1] https://sashiko.dev/#/bug/linux-4e20dfb7-a4d5-4b44-bb0d-e7b8ced3408c

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author
#8 lore raw

PATCH 6/10 clk: gpio: Reject gate clocks with multiple parents

In reply to Lee Jones

gpio_clk_driver_probe() verifies that a gpio-mux-clock has exactly two parents, but passes an unvalidated parent count for a gpio-gate-clock into clk_register_gpio(). clk_register_gpio() points init.parent_data at a two-element stack array, so a gate clock node specifying more than two parents causes clk_core_populate_parent_map() to read past the end of the stack buffer during registration.

A GPIO gate clock has no parent selection ops and its binding permits at most one parent clock. Reject gate clocks with more than one parent in gpio_clk_driver_probe().

Fixes 9a9b5a4af066 ("clk: gpio: Use DT way of specifying parents")
---
 drivers/clk/clk-gpio.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/clk/clk-gpio.c +3 -0
index df0856a39c8d..2337e8a093f0 100644
@@ -208,6 +208,9 @@ static int gpio_clk_driver_probe(struct platform_device *pdev)
 	if (is_mux && num_parents != 2) {
 		dev_err(dev, "mux-clock must have 2 parents\n");
 		return -EINVAL;
+	} else if (!is_mux && num_parents > 1) {
+		return dev_err_probe(dev, -EINVAL,
+				     "gate-clock must have at most 1 parent\n");
 	}
 
 	gpio_name = is_mux ? "select" : "enable";
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#9 lore raw

PATCH 7/10 clk: samsung: exynos5420: Terminate PLL rate tables with sentinels

In reply to Lee Jones

_samsung_clk_register_pll() sizes each PLL rate table by scanning until it encounters an entry with .rate == 0, then copies the counted entries into pll->rate_table. However, exynos5420_pll2550x_24mhz_tbl, exynos5422_bpll_rate_table, exynos5420_epll_24mhz_tbl, and exynos5420_vpll_24mhz_tbl all omit a terminating zero sentinel, causing out-of-bounds reads into adjacent read-only data and populating pll->rate_table with bogus entries.

Append a sentinel entry to each of the four PLL rate tables.

Fixes ca5b40293822 ("clk: samsung: register exynos5420 apll/kpll configuration data")
Fixes 9842452acd3d ("clk: samsung: exynos542x: Add EPLL rate table")
Fixes 8b4a7acf7b30 ("clk: samsung: add BPLL rate table for Exynos 5422 SoC")
Fixes b92981de92cb ("clk: samsung: exynos5420: Add VPLL rate table")
---
 drivers/clk/samsung/clk-exynos5420.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/clk/samsung/clk-exynos5420.c +4 -0
index 400a5c59815c..35baeabf3ccb 100644
@@ -1419,6 +1419,7 @@ static const struct samsung_pll_rate_table exynos5420_pll2550x_24mhz_tbl[] __ini
 	PLL_35XX_RATE(24 * MHZ, 400000000,  200, 3, 2),
 	PLL_35XX_RATE(24 * MHZ, 300000000,  200, 2, 3),
 	PLL_35XX_RATE(24 * MHZ, 200000000,  200, 3, 3),
+	{ /* sentinel */ }
 };
 
 static const struct samsung_pll_rate_table exynos5422_bpll_rate_table[] = {
@@ -1430,6 +1431,7 @@ static const struct samsung_pll_rate_table exynos5422_bpll_rate_table[] = {
 	PLL_35XX_RATE(24 * MHZ, 275000000, 275, 3, 3),
 	PLL_35XX_RATE(24 * MHZ, 206000000, 206, 3, 3),
 	PLL_35XX_RATE(24 * MHZ, 165000000, 110, 2, 3),
+	{ /* sentinel */ }
 };
 
 static const struct samsung_pll_rate_table exynos5420_epll_24mhz_tbl[] = {
@@ -1448,6 +1450,7 @@ static const struct samsung_pll_rate_table exynos5420_epll_24mhz_tbl[] = {
 	PLL_36XX_RATE(24 * MHZ,  49152000U, 197, 3, 5, -25690),
 	PLL_36XX_RATE(24 * MHZ,  45158401U, 90, 3, 4, 20762),
 	PLL_36XX_RATE(24 * MHZ,  32768001U, 131, 3, 5, 4719),
+	{ /* sentinel */ }
 };
 
 static const struct samsung_pll_rate_table exynos5420_vpll_24mhz_tbl[] = {
@@ -1459,6 +1462,7 @@ static const struct samsung_pll_rate_table exynos5420_vpll_24mhz_tbl[] = {
 	PLL_35XX_RATE(24 * MHZ, 266000000U,  266, 3, 3),
 	PLL_35XX_RATE(24 * MHZ, 177000000U,  118, 2, 3),
 	PLL_35XX_RATE(24 * MHZ, 100000000U,  200, 3, 4),
+	{ /* sentinel */ }
 };
 
 static struct samsung_pll_clock exynos5x_plls[nr_plls] __initdata = {
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#10 lore raw

Re: [PATCH 07/10] clk: samsung: exynos5420: Terminate PLL rate tables with sentinels

Quoted 8 lines · Lee Jones

_samsung_clk_register_pll() sizes each PLL rate table by scanning until it encounters an entry with .rate == 0, then copies the counted entries into pll->rate_table. However, exynos5420_pll2550x_24mhz_tbl, exynos5422_bpll_rate_table, exynos5420_epll_24mhz_tbl, and exynos5420_vpll_24mhz_tbl all omit a terminating zero sentinel, causing out-of-bounds reads into adjacent read-only data and populating pll->rate_table with bogus entries.

Quoted 7 lines

Append a sentinel entry to each of the four PLL rate tables.

Fixes ca5b40293822 ("clk: samsung: register exynos5420 apll/kpll configuration data")
Fixes 9842452acd3d ("clk: samsung: exynos542x: Add EPLL rate table")
Fixes 8b4a7acf7b30 ("clk: samsung: add BPLL rate table for Exynos 5422 SoC")
Fixes b92981de92cb ("clk: samsung: exynos5420: Add VPLL rate table")

Also drivers/clk/samsung/clk-artpec8.c, artpec9, exynos5410, clk-exynos7885.c and probably others?

Best regards,
Krzysztof

author
#11 lore raw

PATCH 8/10 clk: tenstorrent: Poll atomically in atlantis_clk_pll_enable()

In reply to Lee Jones

The Common Clock Framework invokes .enable callbacks in atomic context with interrupts disabled and enable_lock held. In atlantis_clk_pll_ops, atlantis_clk_pll_enable() is registered as the .enable callback yet waits for PLL lock using regmap_read_poll_timeout() with a 500 us sleep interval, triggering a sleep-in-atomic bug via usleep_range().

Since the PRCM regmap is backed by MMIO, use
regmap_read_poll_timeout_atomic() instead.

Fixes 23c8ebc95284 ("clk: tenstorrent: Add Atlantis clock controller driver")
---
 drivers/clk/tenstorrent/atlantis-prcm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/clk/tenstorrent/atlantis-prcm.c +5 -4
index eccc8015da23..d9155f133713 100644
@@ -313,10 +313,11 @@ static int atlantis_clk_pll_enable(struct clk_hw *hw)
 			   PLL_CFG_EN_BIT, PLL_CFG_EN_BIT);
 
 	/* Step 3: Wait for PLL lock */
-	ret = regmap_read_poll_timeout(pll->common.regmap,
-				       pll->config.reg_offset, val,
-				       val & PLL_CFG_LOCK_BIT,
-				       PLL_BYPASS_WAIT_US, PLL_LOCK_TIMEOUT_US);
+	ret = regmap_read_poll_timeout_atomic(pll->common.regmap,
+					      pll->config.reg_offset, val,
+					      val & PLL_CFG_LOCK_BIT,
+					      PLL_BYPASS_WAIT_US,
+					      PLL_LOCK_TIMEOUT_US);
 	if (ret) {
 		pr_err("PLL failed to lock within timeout\n");
 		return ret;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Reviewed-by on v1 8/10
#12 lore raw

Re: [PATCH 08/10] clk: tenstorrent: Poll atomically in atlantis_clk_pll_enable()

Hi Lee,

Quoted 10 lines · Lee Jones

The Common Clock Framework invokes .enable callbacks in atomic context with interrupts disabled and enable_lock held. In atlantis_clk_pll_ops, atlantis_clk_pll_enable() is registered as the .enable callback yet waits for PLL lock using regmap_read_poll_timeout() with a 500 us sleep interval, triggering a sleep-in-atomic bug via usleep_range().

Since the PRCM regmap is backed by MMIO, use
regmap_read_poll_timeout_atomic() instead.

Quoted 7 lines
Fixes 23c8ebc95284 ("clk: tenstorrent: Add Atlantis clock controller driver")
---
 drivers/clk/tenstorrent/atlantis-prcm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Thanks, I was able to reproduce the bug on the tt-atlantis qemu machine by using a kernel built with CONFIG_DEBUG_ATOMIC_SLEEP=y, CONFIG_DEBUG_PREEMPT=y, CONFIG_PROVE_LOCKING=y.

[    1.895417] BUG: sleeping function called from invalid context at
drivers/clk/tenstorrent/atlantis-prcm.c:329
[    1.896093] in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid:
1, name: swapper/0
[    1.896616] preempt_count: 1, expected: 0
[    1.896990] 3 locks held by swapper/0/1:
[    1.897251]  #0: ff600001808a7938 (&dev->mutex){....}-{4:4}, at:
__driver_attach+0x68/0x11e
[    1.898561]  #1: ffffffff81d70bb8 (prepare_lock){+.+.}-{4:4}, at:
clk_prepare_lock+0x1c/0x80
[    1.899134]  #2: ffffffff81d70b30 (enable_lock){....}-{3:3}, at:
clk_enable_lock+0xb0/0xd8
[    1.899750] irq event stamp: 25978
[    1.899968] hardirqs last  enabled at (25977): [<ffffffff80d12254>]
_raw_spin_unlock_irqrestore+0x4e/0x70
[    1.900552] hardirqs last disabled at (25978): [<ffffffff80767d00>]
clk_enable_lock+0xa0/0xd8
[    1.901195] softirqs last  enabled at (25724): [<ffffffff80053e72>]
handle_softirqs+0x54e/0x590
[    1.901798] softirqs last disabled at (25717): [<ffffffff80054132>]
__irq_exit_rcu+0x1f0/0x25c
[    1.902422] Preemption disabled at:
[    1.902447] [<ffffffff80767d10>] clk_enable_lock+0xb0/0xd8
[    1.903375] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted
7.2.0-00071-g9a0518d4d70d #3 PREEMPTLAZY
[    1.903558] Hardware name: Tenstorrent Atlantis RISC-V Machine (DT)
[    1.903643] Call Trace:
[    1.903687] [<ffffffff8001927a>] dump_backtrace+0x1c/0x24
[    1.903718] [<ffffffff800024ba>] show_stack+0x28/0x34
[    1.903734] [<ffffffff80011262>] dump_stack_lvl+0x60/0x88
[    1.903750] [<ffffffff8001129e>] dump_stack+0x14/0x1c
[    1.903766] [<ffffffff80084b76>] __might_resched+0x23a/0x26c
[    1.903786] [<ffffffff80084be6>] __might_sleep+0x3e/0x62
[    1.903801] [<ffffffff8077fb02>] atlantis_clk_pll_enable+0xd0/0x1aa
[    1.903818] [<ffffffff8076d10a>] clk_core_enable+0x92/0x2fe
[    1.903835] [<ffffffff8076e130>] __clk_register+0x5c4/0x874
[    1.903851] [<ffffffff8076e558>] devm_clk_hw_register+0x44/0x96
[    1.903867] [<ffffffff8077f812>] atlantis_prcm_probe+0xbe/0x18c
[    1.903883] [<ffffffff80806eda>] platform_probe+0x46/0x78
[    1.903899] [<ffffffff80804946>] really_probe+0x86/0x234
[    1.903914] [<ffffffff80804b58>] __driver_probe_device+0x64/0x104
[    1.903930] [<ffffffff80804cba>] driver_probe_device+0x2c/0x98
[    1.903946] [<ffffffff80804e22>] __driver_attach+0x70/0x11e
[    1.903961] [<ffffffff80802908>] bus_for_each_dev+0x60/0xae
[    1.903976] [<ffffffff80804488>] driver_attach+0x1a/0x22
[    1.903991] [<ffffffff80803c36>] bus_add_driver+0xce/0x1d6
[    1.904006] [<ffffffff80805a5a>] driver_register+0x3e/0xd8
[    1.904022] [<ffffffff80806c0c>] __platform_driver_register+0x1e/0x26
[    1.904038] [<ffffffff80e31bc8>] atlantis_prcm_driver_init+0x22/0x2a
[    1.904057] [<ffffffff8001296e>] do_one_initcall+0x76/0x39a
[    1.904073] [<ffffffff80e01416>] kernel_init_freeable+0x2b4/0x33e
[    1.904088] [<ffffffff80d065d4>] kernel_init+0x1e/0x13a
[    1.904103] [<ffffffff8001440e>] ret_from_fork_kernel+0x20/0x3b0
[    1.904119] [<ffffffff80d13c22>] ret_from_fork_kernel_asm+0x16/0x18

This patch fixes the issue.

Regards
Anirudh Srinivasan

author
#13 lore raw

PATCH 9/10 clk: cdce925: Use devm_of_clk_add_hw_provider() to avoid UAF on unbind

In reply to Lee Jones

cdce925_probe() allocates its clk_cdce925_chip state and registers its clocks using devres, but registers its OF clock provider with the unmanaged of_clk_add_hw_provider() and provides no remove callback. When the I2C device is unbound or the module is unloaded, data is freed while the provider remains on the global OF clock provider list, causing of_clk_cdce925_get() to dereference freed memory on subsequent lookups.

Register the provider with devm_of_clk_add_hw_provider() so it is automatically removed before the clocks and driver state are released.

Fixes 19fbbbbcd3a3 ("Add TI CDCE925 I2C controlled clock synthesizer driver")
---
 drivers/clk/clk-cdce925.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/clk-cdce925.c +2 -2
index 52d350f91496..6dfba3158d8e 100644
@@ -795,8 +795,8 @@ static int cdce925_probe(struct i2c_client *client)
 	}
 
 	/* Register the output clocks */
-	err = of_clk_add_hw_provider(client->dev.of_node, of_clk_cdce925_get,
-				  data);
+	err = devm_of_clk_add_hw_provider(&client->dev, of_clk_cdce925_get,
+					  data);
 	if (err)
 		dev_err(&client->dev, "unable to add OF clock provider\n");
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#14 lore raw

PATCH 10/10 clk: mmp: audio: Use devm_of_clk_add_hw_provider() to prevent UAF

In reply to Lee Jones

mmp2_audio_clk_probe() allocates priv via devres and registers all clocks with devm_clk_hw_register(), but register_clocks() registers the OF clock provider using the unmanaged of_clk_add_hw_provider() while mmp2_audio_clk_remove() never deletes the provider. Unbinding the driver frees priv while leaving a dangling clk_data pointer on the global OF clock provider list.

Use devm_of_clk_add_hw_provider() so the provider is automatically removed on teardown before the clocks and private data are freed.

Fixes 725262d29139 ("clk: mmp2: Add audio clock controller driver")
---
 drivers/clk/mmp/clk-audio.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/mmp/clk-audio.c +2 -2
index 19233684fbf9..9ef5cca1e1f3 100644
@@ -342,8 +342,8 @@ static int register_clocks(struct mmp2_audio_clk *priv, struct device *dev)
 	priv->clk_data.hws[MMP2_CLK_AUDIO_SSPA1] = &priv->sspa1_gate.hw;
 	priv->clk_data.num = CLK_AUDIO_NR_CLKS;
 
-	return of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
-				      &priv->clk_data);
+	return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
+					   &priv->clk_data);
 }
 
 static int mmp2_audio_clk_probe(struct platform_device *pdev)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog