From da9f1312b06e4c03d43d21494fb5beea7f9e096d Mon Sep 17 00:00:00 2001 From: Aamir Ahmed Date: Sat, 5 Sep 2026 21:48:35 +0100 Subject: [PATCH] clk: tenstorrent: Assign .num before accessing .hws Commit f316cdff8d67 ("clk: Annotate struct clk_hw_onecell_data with __counted_by") annotated the hws member of 'struct clk_hw_onecell_data' with __counted_by, which informs the bounds sanitizer (UBSAN_BOUNDS) about the number of elements in .hws[], so that it can warn when .hws[] is accessed out of bounds. As noted in that change, the __counted_by member must be initialized with the number of elements before the first array access happens, otherwise there will be a warning from each access prior to the initialization because the number of elements is zero. This occurs in atlantis_prcm_clocks_register() due to .num being assigned only after every clock has been stored in .hws[]. With CONFIG_UBSAN_BOUNDS and a compiler that implements __counted_by (GCC 15.1+ or Clang 20.1+), this triggers an array-index-out-of-bounds report during probe, and with CONFIG_UBSAN_TRAP the first store traps so the clock provider never registers. Move the .num initialization to right after the allocation. Fixes: 23c8ebc95284 ("clk: tenstorrent: Add Atlantis clock controller driver") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Aamir Ahmed Reviewed-by: Gustavo A. R. Silva Reviewed-by: Anirudh Srinivasan Reviewed-by: Drew Fustini (Tenstorrent) Link: https://patch.msgid.link/AS8P251MB00013BD9A093C06D2D60E10EC8B42@AS8P251MB0001.EURP251.PROD.OUTLOOK.COM [fustini: drop num_clks, use data->num per Gustavo] Signed-off-by: Drew Fustini (Tenstorrent) --- drivers/clk/tenstorrent/atlantis-prcm.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/clk/tenstorrent/atlantis-prcm.c b/drivers/clk/tenstorrent/atlantis-prcm.c index 6d4386eeb7da..6772035ec872 100644 --- a/drivers/clk/tenstorrent/atlantis-prcm.c +++ b/drivers/clk/tenstorrent/atlantis-prcm.c @@ -789,13 +789,14 @@ static int atlantis_prcm_clocks_register(struct device *dev, { struct clk_hw_onecell_data *clk_data; int i, ret; - size_t num_clks = data->num; clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, data->num), GFP_KERNEL); if (!clk_data) return -ENOMEM; + clk_data->num = data->num; + for (i = 0; i < data->num; i++) { struct clk_hw *hw = data->hws[i]; struct atlantis_clk_common *common = @@ -809,8 +810,6 @@ static int atlantis_prcm_clocks_register(struct device *dev, clk_data->hws[common->clkid] = hw; } - clk_data->num = num_clks; - return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data); }