clk: tenstorrent: Assign .num before accessing .hws

Commit f316cdff8d ("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: 23c8ebc952 ("clk: tenstorrent: Add Atlantis clock controller driver")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Aamir Ahmed <elb12345@hotmail.co.uk>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Anirudh Srinivasan <asrinivasan@oss.tenstorrent.com>
Reviewed-by: Drew Fustini (Tenstorrent) <fustini@kernel.org>
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) <fustini@kernel.org>
This commit is contained in:
Aamir Ahmed 2026-09-05 21:48:35 +01:00 committed by Drew Fustini (Tenstorrent)
parent cee9395acd
commit da9f1312b0

View File

@ -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);
}