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

Ben Cressey ben at cressey.dev
Thu Dec 3 16:15:42 PST 2026


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")
Cc: stable at vger.kernel.org
Signed-off-by: Ben Cressey <ben at cressey.dev>
---
 drivers/clk/sifive/sifive-prci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/clk/sifive/sifive-prci.c b/drivers/clk/sifive/sifive-prci.c
index 402b905d010ed..b4544d024dd88 100644
--- a/drivers/clk/sifive/sifive-prci.c
+++ b/drivers/clk/sifive/sifive-prci.c
@@ -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);

-- 
2.56.0




More information about the linux-arm-kernel mailing list