Skip to contentSkip to conversation

clk: Set __counted_by counts before filling the arrays

PATCH In review Ben Cressey 3 participants 24 messages Dec 4

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

cover clk: Set __counted_by counts before filling the arrays

Ten clk drivers access an array annotated with __counted_by while its count is still zero. CONFIG_UBSAN_BOUNDS reports those accesses, and CONFIG_UBSAN_TRAP can stop the boot or the probe on them. In v7.4-rc5, this affects Clang 20 and 21, where CONFIG_CC_HAS_BROKEN_COUNTED_BY_REF keeps the flex allocators from setting the count. Nine of the drivers are also affected in older releases.

The bm1880 driver needs two patches, because its array is also one element short. Patch 9 fixes the size, and patch 10 sets the count earlier.

The Cadence Sierra PHY driver has the same late count, and its fix goes to linux-phy. On next-20261202, patch 3 needs a three-way merge.

Functional tests ran under QEMU TCG on arm64, with RAM-backed registers and with stubs for the lmk04832 SPI bus and the zynqmp firmware. Mainline with Clang 20 and 21 drew reports before the series and none after it. So did backports to v7.3 (GCC 15, Clang 22) and 6.18.y (GCC 15). The clps711x, mmp, sifive and jh7100 drivers ran outside their platforms, and were also built for their own architectures. The sifive driver also ran on QEMU's sifive_u. Not tested on hardware.

Code and text developed with LLM assistance.

---
Ben Cressey (11):
clk: anlogic: Assign .num before accessing .hws
clk: lmk04832: Assign .num before accessing .hws
clk: mmp: audio: Assign .num before accessing .hws
clk: sifive: prci: Assign .num before accessing .hws
clk: visconti: Assign .num before accessing .hws
clk: clocking-wizard: Assign .num before accessing .hws
clk: clps711x: Assign .num before accessing .hws
clk: starfive: jh7100: Assign .num_reg before accessing .reg
clk: bm1880: Size .hws by the highest clock id
clk: bm1880: Assign .num before accessing .hws
clk: zynqmp: Assign .num before accessing .hws

 drivers/clk/anlogic/cru_dr1.c              |  4 ++--
 drivers/clk/clk-bm1880.c                   | 11 ++++-------
 drivers/clk/clk-clps711x.c                 |  2 +-
 drivers/clk/clk-lmk04832.c                 |  3 ++-
 drivers/clk/mmp/clk-audio.c                |  2 +-
 drivers/clk/sifive/sifive-prci.c           |  4 ++--
 drivers/clk/starfive/clk-starfive-jh7100.c |  1 +
 drivers/clk/visconti/clkc.c                |  2 +-
 drivers/clk/xilinx/clk-xlnx-clock-wizard.c |  2 +-
 drivers/clk/zynqmp/clkc.c                  |  2 +-
 10 files changed, 16 insertions(+), 17 deletions(-)

---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261203-clk-onecell-count-order-90a91cb82afb

#1 lore raw

PATCH 1/11 clk: anlogic: Assign .num before accessing .hws

hws[] is annotated with __counted_by(num), but dr1_cru_clk_register() fills it before it sets num. With Clang 20 and 21, devm_kzalloc_flex() leaves num at zero. CONFIG_UBSAN_BOUNDS then reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes c4e2f5e69ba4 ("clk: anlogic: add cru support for Anlogic DR1V90 SoC")
---
 drivers/clk/anlogic/cru_dr1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/anlogic/cru_dr1.c +2 -2
index 478a52d5fa36c..b4ab385455e8b 100644
@@ -187,6 +187,8 @@ int dr1_cru_clk_register(struct device *dev, void __iomem *base,
 	if (!priv)
 		return -ENOMEM;
 
+	priv->num = nr_clks;
+
 	for (i = 0; i < nr_clks; i++) {
 		const struct cru_clk *clk = &clks[i];
 
@@ -200,8 +202,6 @@ int dr1_cru_clk_register(struct device *dev, void __iomem *base,
 		priv->hws[i] = clk->hw;
 	}
 
-	priv->num = nr_clks;
-
 	ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, priv);
 	if (ret)
 		dev_err(dev, "failed to add clock hardware provider\n");
Signature · 1 line
2.56.0
Reviewed-by on v1 1/11
#2 lore raw

Re: [PATCH 01/11] clk: anlogic: Assign .num before accessing .hws

Hi,

Quoted 41 lines · Ben Cressey

hws[] is annotated with __counted_by(num), but dr1_cru_clk_register() fills it before it sets num. With Clang 20 and 21, devm_kzalloc_flex() leaves num at zero. CONFIG_UBSAN_BOUNDS then reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes c4e2f5e69ba4 ("clk: anlogic: add cru support for Anlogic DR1V90 SoC")
---
 drivers/clk/anlogic/cru_dr1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/anlogic/cru_dr1.c +2 -2
index 478a52d5fa36c..b4ab385455e8b 100644
@@ -187,6 +187,8 @@ int dr1_cru_clk_register(struct device *dev, void __iomem *base,
 	if (!priv)
 		return -ENOMEM;
 
+	priv->num = nr_clks;
+
 	for (i = 0; i < nr_clks; i++) {
 		const struct cru_clk *clk = &clks[i];
 
@@ -200,8 +202,6 @@ int dr1_cru_clk_register(struct device *dev, void __iomem *base,
 		priv->hws[i] = clk->hw;
 	}
 
-	priv->num = nr_clks;
-
 	ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, priv);
 	if (ret)
 		dev_err(dev, "failed to add clock hardware provider\n");
Signature · 2 lines
-- 
2.56.0

Justin

#3 lore raw

PATCH 2/11 clk: lmk04832: Assign .num before accessing .hws

In reply to Ben Cressey

hws[] is annotated with __counted_by(num), but lmk04832_register_clkout() fills it before lmk04832_probe() sets num. The count is still zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.6 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/clk-lmk04832.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/clk/clk-lmk04832.c +2 -1
index e6e771e8eb145..ad66cf097b9ba 100644
@@ -1435,6 +1435,8 @@ static int lmk04832_probe(struct spi_device *spi)
 		return ret;
 	}
 
+	lmk->clk_data->num = info->num_channels;
+
 	device_property_read_u32(lmk->dev, "ti,vco-hz", &lmk->vco_rate);
 
 	lmk->sysref_ddly = 8;
@@ -1530,7 +1532,6 @@ static int lmk04832_probe(struct spi_device *spi)
 		}
 	}
 
-	lmk->clk_data->num = info->num_channels;
 	ret = devm_of_clk_add_hw_provider(lmk->dev, of_clk_hw_onecell_get,
 					  lmk->clk_data);
 	if (ret) {
Signature · 1 line
2.56.0
Reviewed-by on v1 2/11
#4 lore raw

Re: [PATCH 02/11] clk: lmk04832: Assign .num before accessing .hws

Hi,

Quoted 43 lines · Ben Cressey

hws[] is annotated with __counted_by(num), but lmk04832_register_clkout() fills it before lmk04832_probe() sets num. The count is still zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.6 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/clk-lmk04832.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/clk/clk-lmk04832.c +2 -1
index e6e771e8eb145..ad66cf097b9ba 100644
@@ -1435,6 +1435,8 @@ static int lmk04832_probe(struct spi_device *spi)
 		return ret;
 	}
 
+	lmk->clk_data->num = info->num_channels;
+
 	device_property_read_u32(lmk->dev, "ti,vco-hz", &lmk->vco_rate);
 
 	lmk->sysref_ddly = 8;
@@ -1530,7 +1532,6 @@ static int lmk04832_probe(struct spi_device *spi)
 		}
 	}
 
-	lmk->clk_data->num = info->num_channels;
 	ret = devm_of_clk_add_hw_provider(lmk->dev, of_clk_hw_onecell_get,
 					  lmk->clk_data);
 	if (ret) {
Signature · 2 lines
-- 
2.56.0

Justin

#5 lore raw

PATCH 3/11 clk: mmp: audio: Assign .num before accessing .hws

In reply to Ben Cressey

hws[] is annotated with __counted_by(num), but register_clocks() fills it before it sets num. The count is still zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.6 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/mmp/clk-audio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/mmp/clk-audio.c +1 -1
index 19233684fbf9b..6b74aaed64b41 100644
@@ -340,7 +340,6 @@ static int register_clocks(struct mmp2_audio_clk *priv, struct device *dev)
 	priv->clk_data.hws[MMP2_CLK_AUDIO_SYSCLK] = &priv->sysclk_gate.hw;
 	priv->clk_data.hws[MMP2_CLK_AUDIO_SSPA0] = &priv->sspa0_gate.hw;
 	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);
@@ -356,6 +355,7 @@ static int mmp2_audio_clk_probe(struct platform_device *pdev)
 	if (!priv)
 		return -ENOMEM;
 
+	priv->clk_data.num = CLK_AUDIO_NR_CLKS;
 	spin_lock_init(&priv->lock);
 	platform_set_drvdata(pdev, priv);
 
Signature · 1 line
2.56.0
Reviewed-by on v1 3/11
#6 lore raw

Re: [PATCH 03/11] clk: mmp: audio: Assign .num before accessing .hws

Hi,

Quoted 42 lines · Ben Cressey

hws[] is annotated with __counted_by(num), but register_clocks() fills it before it sets num. The count is still zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.6 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/mmp/clk-audio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/mmp/clk-audio.c +1 -1
index 19233684fbf9b..6b74aaed64b41 100644
@@ -340,7 +340,6 @@ static int register_clocks(struct mmp2_audio_clk *priv, struct device *dev)
 	priv->clk_data.hws[MMP2_CLK_AUDIO_SYSCLK] = &priv->sysclk_gate.hw;
 	priv->clk_data.hws[MMP2_CLK_AUDIO_SSPA0] = &priv->sspa0_gate.hw;
 	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);
@@ -356,6 +355,7 @@ static int mmp2_audio_clk_probe(struct platform_device *pdev)
 	if (!priv)
 		return -ENOMEM;
 
+	priv->clk_data.num = CLK_AUDIO_NR_CLKS;
 	spin_lock_init(&priv->lock);
 	platform_set_drvdata(pdev, priv);
 
Signature · 2 lines
-- 
2.56.0

Justin

#7 lore raw

PATCH 4/11 clk: sifive: prci: Assign .num before accessing .hws

In reply to Ben Cressey

hws[] is annotated with __counted_by(num), but __prci_register_clocks() fills it before it sets num. The count is still zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.6 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/sifive/sifive-prci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/sifive/sifive-prci.c +2 -2
index 402b905d010ed..b4544d024dd88 100644
@@ -540,8 +540,6 @@ static int __prci_register_clocks(struct device *dev, struct __prci_data *pd,
 		pd->hw_clks.hws[i] = &pic->hw;
 	}
 
-	pd->hw_clks.num = i;
-
 	r = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
 					&pd->hw_clks);
 	if (r) {
@@ -571,6 +569,8 @@ static int sifive_prci_probe(struct platform_device *pdev)
 	if (!pd)
 		return -ENOMEM;
 
+	pd->hw_clks.num = desc->num_clks;
+
 	pd->va = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(pd->va))
 		return PTR_ERR(pd->va);
Signature · 1 line
2.56.0
Reviewed-by on v1 4/11
#8 lore raw

Re: [PATCH 04/11] clk: sifive: prci: Assign .num before accessing .hws

Hi,

Quoted 44 lines · Ben Cressey

hws[] is annotated with __counted_by(num), but __prci_register_clocks() fills it before it sets num. The count is still zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.6 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/sifive/sifive-prci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/sifive/sifive-prci.c +2 -2
index 402b905d010ed..b4544d024dd88 100644
@@ -540,8 +540,6 @@ static int __prci_register_clocks(struct device *dev, struct __prci_data *pd,
 		pd->hw_clks.hws[i] = &pic->hw;
 	}
 
-	pd->hw_clks.num = i;
-
 	r = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
 					&pd->hw_clks);
 	if (r) {
@@ -571,6 +569,8 @@ static int sifive_prci_probe(struct platform_device *pdev)
 	if (!pd)
 		return -ENOMEM;
 
+	pd->hw_clks.num = desc->num_clks;
+
 	pd->va = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(pd->va))
 		return PTR_ERR(pd->va);
Signature · 2 lines
-- 
2.56.0

Justin

#9 lore raw

PATCH 5/11 clk: visconti: Assign .num before accessing .hws

In reply to Ben Cressey

hws[] is annotated with __counted_by(num), but visconti_init_clk() fills it before it sets num. The count is still zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.6 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/visconti/clkc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/visconti/clkc.c +1 -1
index a75163b69751e..f8bb93d7027bc 100644
@@ -195,9 +195,9 @@ struct visconti_clk_provider *visconti_init_clk(struct device *dev,
 	if (!ctx)
 		return ERR_PTR(-ENOMEM);
 
+	ctx->clk_data.num = nr_clks;
 	for (i = 0; i < nr_clks; ++i)
 		ctx->clk_data.hws[i] = ERR_PTR(-ENOENT);
-	ctx->clk_data.num = nr_clks;
 
 	ctx->dev = dev;
 	ctx->regmap = regmap;
Signature · 1 line
2.56.0
Reviewed-by on v1 5/11
#10 lore raw

Re: [PATCH 05/11] clk: visconti: Assign .num before accessing .hws

Hi,

Quoted 37 lines · Ben Cressey

hws[] is annotated with __counted_by(num), but visconti_init_clk() fills it before it sets num. The count is still zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.6 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/visconti/clkc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/visconti/clkc.c +1 -1
index a75163b69751e..f8bb93d7027bc 100644
@@ -195,9 +195,9 @@ struct visconti_clk_provider *visconti_init_clk(struct device *dev,
 	if (!ctx)
 		return ERR_PTR(-ENOMEM);
 
+	ctx->clk_data.num = nr_clks;
 	for (i = 0; i < nr_clks; ++i)
 		ctx->clk_data.hws[i] = ERR_PTR(-ENOENT);
-	ctx->clk_data.num = nr_clks;
 
 	ctx->dev = dev;
 	ctx->regmap = regmap;
Signature · 2 lines
-- 
2.56.0

Justin

#11 lore raw

PATCH 6/11 clk: clocking-wizard: Assign .num before accessing .hws

In reply to Ben Cressey

hws[] is annotated with __counted_by(num), but clk_wzrd_register_output_clocks() fills it before clk_wzrd_probe() sets num. The count is still zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.13 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe. Nodes with xlnx,static-config do not reach this code.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes bb487924b161 ("clk: clocking-wizard: use newer clk_hw API")
---
 drivers/clk/xilinx/clk-xlnx-clock-wizard.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/xilinx/clk-xlnx-clock-wizard.c +1 -1
index 9f8830b40122a..d1e9b11da2011 100644
@@ -1302,6 +1302,7 @@ static int clk_wzrd_probe(struct platform_device *pdev)
 				     nr_outputs);
 	if (!clk_wzrd)
 		return -ENOMEM;
+	clk_wzrd->clk_data.num = nr_outputs;
 	platform_set_drvdata(pdev, clk_wzrd);
 
 	clk_wzrd->base = devm_platform_ioremap_resource(pdev, 0);
@@ -1337,7 +1338,6 @@ static int clk_wzrd_probe(struct platform_device *pdev)
 		if (ret)
 			return ret;
 
-		clk_wzrd->clk_data.num = nr_outputs;
 		ret = devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_onecell_get,
 						  &clk_wzrd->clk_data);
 		if (ret) {
Signature · 1 line
2.56.0
Reviewed-by on v1 6/11
#12 lore raw

Re: [PATCH 06/11] clk: clocking-wizard: Assign .num before accessing .hws

Hi,

Quoted 43 lines · Ben Cressey

hws[] is annotated with __counted_by(num), but clk_wzrd_register_output_clocks() fills it before clk_wzrd_probe() sets num. The count is still zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.13 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe. Nodes with xlnx,static-config do not reach this code.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes bb487924b161 ("clk: clocking-wizard: use newer clk_hw API")
---
 drivers/clk/xilinx/clk-xlnx-clock-wizard.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/xilinx/clk-xlnx-clock-wizard.c +1 -1
index 9f8830b40122a..d1e9b11da2011 100644
@@ -1302,6 +1302,7 @@ static int clk_wzrd_probe(struct platform_device *pdev)
 				     nr_outputs);
 	if (!clk_wzrd)
 		return -ENOMEM;
+	clk_wzrd->clk_data.num = nr_outputs;
 	platform_set_drvdata(pdev, clk_wzrd);
 
 	clk_wzrd->base = devm_platform_ioremap_resource(pdev, 0);
@@ -1337,7 +1338,6 @@ static int clk_wzrd_probe(struct platform_device *pdev)
 		if (ret)
 			return ret;
 
-		clk_wzrd->clk_data.num = nr_outputs;
 		ret = devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_onecell_get,
 						  &clk_wzrd->clk_data);
 		if (ret) {
Signature · 2 lines
-- 
2.56.0

Justin

#13 lore raw

PATCH 7/11 clk: clps711x: Assign .num before accessing .hws

In reply to Ben Cressey

hws[] is annotated with __counted_by(num), but clps711x_clk_init_dt() fills it before it sets num. The count is still zero with Clang 20 and 21, where kzalloc_flex() does not set it, and on v6.6 to v6.19, which use kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during boot.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/clk-clps711x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/clk-clps711x.c +1 -1
index 27a57a9b3cb34..e2185b3daac99 100644
@@ -57,6 +57,7 @@ static void __init clps711x_clk_init_dt(struct device_node *np)
 				    CLPS711X_CLK_MAX);
 	BUG_ON(!clps711x_clk);
 
+	clps711x_clk->clk_data.num = CLPS711X_CLK_MAX;
 	spin_lock_init(&clps711x_clk->lock);
 
 	/* Read PLL multiplier value and sanity check */
@@ -139,7 +140,6 @@ static void __init clps711x_clk_init_dt(struct device_node *np)
 			pr_err("clk %i: register failed with %ld\n",
 			       tmp, PTR_ERR(clps711x_clk->clk_data.hws[tmp]));
 
-	clps711x_clk->clk_data.num = CLPS711X_CLK_MAX;
 	of_clk_add_hw_provider(np, of_clk_hw_onecell_get,
 			       &clps711x_clk->clk_data);
 }
Signature · 1 line
2.56.0
Reviewed-by on v1 7/11
#14 lore raw

Re: [PATCH 07/11] clk: clps711x: Assign .num before accessing .hws

Hi,

Quoted 41 lines · Ben Cressey

hws[] is annotated with __counted_by(num), but clps711x_clk_init_dt() fills it before it sets num. The count is still zero with Clang 20 and 21, where kzalloc_flex() does not set it, and on v6.6 to v6.19, which use kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during boot.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/clk-clps711x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/clk-clps711x.c +1 -1
index 27a57a9b3cb34..e2185b3daac99 100644
@@ -57,6 +57,7 @@ static void __init clps711x_clk_init_dt(struct device_node *np)
 				    CLPS711X_CLK_MAX);
 	BUG_ON(!clps711x_clk);
 
+	clps711x_clk->clk_data.num = CLPS711X_CLK_MAX;
 	spin_lock_init(&clps711x_clk->lock);
 
 	/* Read PLL multiplier value and sanity check */
@@ -139,7 +140,6 @@ static void __init clps711x_clk_init_dt(struct device_node *np)
 			pr_err("clk %i: register failed with %ld\n",
 			       tmp, PTR_ERR(clps711x_clk->clk_data.hws[tmp]));
 
-	clps711x_clk->clk_data.num = CLPS711X_CLK_MAX;
 	of_clk_add_hw_provider(np, of_clk_hw_onecell_get,
 			       &clps711x_clk->clk_data);
 }
Signature · 2 lines
-- 
2.56.0

Justin

#15 lore raw

PATCH 8/11 clk: starfive: jh7100: Assign .num_reg before accessing .reg

In reply to Ben Cressey

reg[] is annotated with __counted_by(num_reg), but clk_starfive_jh7100_probe() does not set num_reg. The count stays zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.14 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the probe trap.

Set num_reg as the other jh71x0 drivers do.

Found by an LLM-assisted check of __counted_by users.

Fixes 838c0a41d7ed ("clk: starfive: Make _clk_get become a common helper function")
Cc <[email protected]> # 6.14.x
---
 drivers/clk/starfive/clk-starfive-jh7100.c | 1 +
 1 file changed, 1 insertion(+)
drivers/clk/starfive/clk-starfive-jh7100.c +1 -0
index 079da88ec66cf..c16343a41b21c 100644
@@ -291,6 +291,7 @@ static int __init clk_starfive_jh7100_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	spin_lock_init(&priv->rmw_lock);
+	priv->num_reg = JH7100_CLK_PLL0_OUT;
 	priv->dev = &pdev->dev;
 	priv->base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(priv->base))
Signature · 1 line
2.56.0
Reviewed-by on v1 8/11
#16 lore raw

Re: [PATCH 08/11] clk: starfive: jh7100: Assign .num_reg before accessing .reg

Hi,

Quoted 34 lines · Ben Cressey

reg[] is annotated with __counted_by(num_reg), but clk_starfive_jh7100_probe() does not set num_reg. The count stays zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.14 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the probe trap.

Set num_reg as the other jh71x0 drivers do.

Found by an LLM-assisted check of __counted_by users.

Fixes 838c0a41d7ed ("clk: starfive: Make _clk_get become a common helper function")
Cc <[email protected]> # 6.14.x
---
 drivers/clk/starfive/clk-starfive-jh7100.c | 1 +
 1 file changed, 1 insertion(+)
drivers/clk/starfive/clk-starfive-jh7100.c +1 -0
index 079da88ec66cf..c16343a41b21c 100644
@@ -291,6 +291,7 @@ static int __init clk_starfive_jh7100_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	spin_lock_init(&priv->rmw_lock);
+	priv->num_reg = JH7100_CLK_PLL0_OUT;
 	priv->dev = &pdev->dev;
 	priv->base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(priv->base))
Signature · 2 lines
-- 
2.56.0

Justin

#17 lore raw

PATCH 9/11 clk: bm1880: Size .hws by the highest clock id

In reply to Ben Cressey

bm1880_clk_probe() sizes hws[] by the sum of the five clock tables, but the array is indexed by clock id. The sum is 68 while the ids in the tables run from 1 to 68. BM1880_CLK_AXI6 is stored one element past the end of hws[], which CONFIG_UBSAN_BOUNDS can report, and of_clk_hw_onecell_get() rejects its index.

Size hws[] by the highest clock id plus one. Keep that count in the driver rather than use BM1880_NR_CLKS from the binding header, as in commit 26ec588063d9 ("clk: nuvoton: ma35d1: Keep the clock count in the driver").

Found by an LLM-assisted review of __counted_by users.

Fixes 1ab4601da55b ("clk: Add common clock driver for BM1880 SoC")
---
 drivers/clk/clk-bm1880.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/clk/clk-bm1880.c +3 -5
index da8ff672ee0f9..dc13d90bdd919 100644
@@ -53,6 +53,8 @@
 #define BM1880_CLK_DIV27	0xac
 #define BM1880_CLK_DIV28	0xb0
 
+#define BM1880_CLK_COUNT	(BM1880_CLK_AXI6 + 1)
+
 #define to_bm1880_pll_clk(_hw) container_of(_hw, struct bm1880_pll_hw_clock, hw)
 #define to_bm1880_div_clk(_hw) container_of(_hw, struct bm1880_div_hw_clock, hw)
 
@@ -883,11 +885,7 @@ static int bm1880_clk_probe(struct platform_device *pdev)
 	if (IS_ERR(sys_base))
 		return PTR_ERR(sys_base);
 
-	num_clks = ARRAY_SIZE(bm1880_pll_clks) +
-		   ARRAY_SIZE(bm1880_div_clks) +
-		   ARRAY_SIZE(bm1880_mux_clks) +
-		   ARRAY_SIZE(bm1880_composite_clks) +
-		   ARRAY_SIZE(bm1880_gate_clks);
+	num_clks = BM1880_CLK_COUNT;
 
 	clk_data = devm_kzalloc_flex(dev, *clk_data, hw_data.hws, num_clks);
 	if (!clk_data)
Signature · 1 line
2.56.0
Reviewed-by on v1 9/11
#18 lore raw

Re: [PATCH 09/11] clk: bm1880: Size .hws by the highest clock id

Hi,

Quoted 50 lines · Ben Cressey

bm1880_clk_probe() sizes hws[] by the sum of the five clock tables, but the array is indexed by clock id. The sum is 68 while the ids in the tables run from 1 to 68. BM1880_CLK_AXI6 is stored one element past the end of hws[], which CONFIG_UBSAN_BOUNDS can report, and of_clk_hw_onecell_get() rejects its index.

Size hws[] by the highest clock id plus one. Keep that count in the driver rather than use BM1880_NR_CLKS from the binding header, as in commit 26ec588063d9 ("clk: nuvoton: ma35d1: Keep the clock count in the driver").

Found by an LLM-assisted review of __counted_by users.

Fixes 1ab4601da55b ("clk: Add common clock driver for BM1880 SoC")
---
 drivers/clk/clk-bm1880.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/clk/clk-bm1880.c +3 -5
index da8ff672ee0f9..dc13d90bdd919 100644
@@ -53,6 +53,8 @@
 #define BM1880_CLK_DIV27	0xac
 #define BM1880_CLK_DIV28	0xb0
 
+#define BM1880_CLK_COUNT	(BM1880_CLK_AXI6 + 1)
+
 #define to_bm1880_pll_clk(_hw) container_of(_hw, struct bm1880_pll_hw_clock, hw)
 #define to_bm1880_div_clk(_hw) container_of(_hw, struct bm1880_div_hw_clock, hw)
 
@@ -883,11 +885,7 @@ static int bm1880_clk_probe(struct platform_device *pdev)
 	if (IS_ERR(sys_base))
 		return PTR_ERR(sys_base);
 
-	num_clks = ARRAY_SIZE(bm1880_pll_clks) +
-		   ARRAY_SIZE(bm1880_div_clks) +
-		   ARRAY_SIZE(bm1880_mux_clks) +
-		   ARRAY_SIZE(bm1880_composite_clks) +
-		   ARRAY_SIZE(bm1880_gate_clks);
+	num_clks = BM1880_CLK_COUNT;
 
 	clk_data = devm_kzalloc_flex(dev, *clk_data, hw_data.hws, num_clks);
 	if (!clk_data)
Signature · 2 lines
-- 
2.56.0

Justin

#19 lore raw

PATCH 10/11 clk: bm1880: Assign .num before accessing .hws

In reply to Ben Cressey

hws[] is annotated with __counted_by(num), but bm1880_clk_probe() fills it before it sets num. The count is still zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.6 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num before the loop that fills hws[].

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/clk-bm1880.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/clk/clk-bm1880.c +1 -2
index dc13d90bdd919..e2511991eeea2 100644
@@ -893,12 +893,11 @@ static int bm1880_clk_probe(struct platform_device *pdev)
 
 	clk_data->pll_base = pll_base;
 	clk_data->sys_base = sys_base;
+	clk_data->hw_data.num = num_clks;
 
 	for (i = 0; i < num_clks; i++)
 		clk_data->hw_data.hws[i] = ERR_PTR(-ENOENT);
 
-	clk_data->hw_data.num = num_clks;
-
 	bm1880_clk_register_plls(bm1880_pll_clks,
 				 ARRAY_SIZE(bm1880_pll_clks),
 				 clk_data);
Signature · 1 line
2.56.0
Reviewed-by on v1 10/11
#20 lore raw

Re: [PATCH 10/11] clk: bm1880: Assign .num before accessing .hws

Hi,

Quoted 40 lines · Ben Cressey

hws[] is annotated with __counted_by(num), but bm1880_clk_probe() fills it before it sets num. The count is still zero with Clang 20 and 21, where devm_kzalloc_flex() does not set it, and on v6.6 to v7.3, which use devm_kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num before the loop that fills hws[].

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/clk-bm1880.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/clk/clk-bm1880.c +1 -2
index dc13d90bdd919..e2511991eeea2 100644
@@ -893,12 +893,11 @@ static int bm1880_clk_probe(struct platform_device *pdev)
 
 	clk_data->pll_base = pll_base;
 	clk_data->sys_base = sys_base;
+	clk_data->hw_data.num = num_clks;
 
 	for (i = 0; i < num_clks; i++)
 		clk_data->hw_data.hws[i] = ERR_PTR(-ENOENT);
 
-	clk_data->hw_data.num = num_clks;
-
 	bm1880_clk_register_plls(bm1880_pll_clks,
 				 ARRAY_SIZE(bm1880_pll_clks),
 				 clk_data);
Signature · 2 lines
-- 
2.56.0

Justin

#21 lore raw

PATCH 11/11 clk: zynqmp: Assign .num before accessing .hws

In reply to Ben Cressey

hws[] is annotated with __counted_by(num), but zynqmp_register_clocks() fills it before zynqmp_clk_setup() sets num. The count is still zero with Clang 20 and 21, where kzalloc_flex() does not set it, and on v6.6 to v6.19, which use kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/zynqmp/clkc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/zynqmp/clkc.c +1 -1
index d62f1901d0ab5..88d1281c284b8 100644
@@ -762,6 +762,7 @@ static int zynqmp_clk_setup(struct device_node *np)
 	zynqmp_data = kzalloc_flex(*zynqmp_data, hws, clock_max_idx);
 	if (!zynqmp_data)
 		return -ENOMEM;
+	zynqmp_data->num = clock_max_idx;
 
 	clock = kzalloc_objs(*clock, clock_max_idx);
 	if (!clock) {
@@ -772,7 +773,6 @@ static int zynqmp_clk_setup(struct device_node *np)
 	zynqmp_get_clock_info();
 	zynqmp_register_clocks(np);
 
-	zynqmp_data->num = clock_max_idx;
 	return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, zynqmp_data);
 }
 
Signature · 1 line
2.56.0
Reviewed-by on v1 11/11
#22 lore raw

Re: [PATCH 11/11] clk: zynqmp: Assign .num before accessing .hws

Hi,

Quoted 42 lines · Ben Cressey

hws[] is annotated with __counted_by(num), but zynqmp_register_clocks() fills it before zynqmp_clk_setup() sets num. The count is still zero with Clang 20 and 21, where kzalloc_flex() does not set it, and on v6.6 to v6.19, which use kzalloc(). With __counted_by enabled, CONFIG_UBSAN_BOUNDS reports the accesses and CONFIG_UBSAN_TRAP makes the first one trap during probe.

Set num right after the allocation.

Found by an LLM-assisted check of __counted_by users.

Fixes f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by")
---
 drivers/clk/zynqmp/clkc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/zynqmp/clkc.c +1 -1
index d62f1901d0ab5..88d1281c284b8 100644
@@ -762,6 +762,7 @@ static int zynqmp_clk_setup(struct device_node *np)
 	zynqmp_data = kzalloc_flex(*zynqmp_data, hws, clock_max_idx);
 	if (!zynqmp_data)
 		return -ENOMEM;
+	zynqmp_data->num = clock_max_idx;
 
 	clock = kzalloc_objs(*clock, clock_max_idx);
 	if (!clock) {
@@ -772,7 +773,6 @@ static int zynqmp_clk_setup(struct device_node *np)
 	zynqmp_get_clock_info();
 	zynqmp_register_clocks(np);
 
-	zynqmp_data->num = clock_max_idx;
 	return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, zynqmp_data);
 }
 
Signature · 2 lines
-- 
2.56.0

Justin

#23 lore raw
In reply to Ben Cressey
Quoted 6 lines · Ben Cressey

Ten clk drivers access an array annotated with __counted_by while its count is still zero. CONFIG_UBSAN_BOUNDS reports those accesses, and CONFIG_UBSAN_TRAP can stop the boot or the probe on them. In v7.4-rc5, this affects Clang 20 and 21, where CONFIG_CC_HAS_BROKEN_COUNTED_BY_REF keeps the flex allocators from setting the count. Nine of the drivers are also affected in older releases.

I was considering moving the Clang version support for counted_by up to where counted_by_ref isn't broken, as that would "fix" this too, and be "future proof". But it removes coverage for old Clang builds.

I'm fine either way: if the clk patches are tolerable, we can do that. I'm just worried there will be more instances of this problem sneaking in.

-Kees

Signature · 1 line
Kees Cook