clk: clk-loongson2: Fix the number count of clk provider
authorBinbin Zhou <zhoubinbin@loongson.cn>
Tue, 14 Jan 2025 13:00:29 +0000 (21:00 +0800)
committerStephen Boyd <sboyd@kernel.org>
Tue, 14 Jan 2025 19:14:33 +0000 (11:14 -0800)
Since commit 02fb4f008433 ("clk: clk-loongson2: Fix potential buffer
overflow in flexible-array member access"), the clk provider register is
failed.

The count of `clks_num` is shown below:

for (p = data; p->name; p++)
clks_num++;

In fact, `clks_num` represents the number of SoC clocks and should be
expressed as the maximum value of the clock binding id in use (p->id + 1).

Now we fix it to avoid the following error when trying to register a clk
provider:

[ 13.409595] of_clk_hw_onecell_get: invalid index 17

Cc: stable@vger.kernel.org
Cc: Gustavo A. R. Silva <gustavoars@kernel.org>
Fixes: 02fb4f008433 ("clk: clk-loongson2: Fix potential buffer overflow in flexible-array member access")
Signed-off-by: Binbin Zhou <zhoubinbin@loongson.cn>
Link: https://lore.kernel.org/r/82e43d89a9a6791129cf8ea14f4eeb666cd87be4.1736856470.git.zhoubinbin@loongson.cn
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
drivers/clk/clk-loongson2.c

index 7082b4309c6f1577530b597fbd1190c7dce51a4f..0d9485e83938a168d00ef78445f07655ec51b65a 100644 (file)
@@ -294,7 +294,7 @@ static int loongson2_clk_probe(struct platform_device *pdev)
                return -EINVAL;
 
        for (p = data; p->name; p++)
-               clks_num++;
+               clks_num = max(clks_num, p->id + 1);
 
        clp = devm_kzalloc(dev, struct_size(clp, clk_data.hws, clks_num),
                           GFP_KERNEL);
@@ -309,6 +309,9 @@ static int loongson2_clk_probe(struct platform_device *pdev)
        clp->clk_data.num = clks_num;
        clp->dev = dev;
 
+       /* Avoid returning NULL for unused id */
+       memset_p((void **)clp->clk_data.hws, ERR_PTR(-ENOENT), clks_num);
+
        for (i = 0; i < clks_num; i++) {
                p = &data[i];
                switch (p->type) {