Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

clk: Annotate struct clk_hw_onecell_data with __counted_by

Prepare for the coming implementation by GCC and Clang of the __counted_by
attribute. Flexible array members annotated with __counted_by can have
their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS
(for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family
functions).

As found with Coccinelle[1], add __counted_by for struct clk_hw_onecell_data.
Additionally, since the element count member must be set before accessing
the annotated flexible array member, move its initialization earlier.

[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci

Cc: Michael Turquette <mturquette@baylibre.com>
Cc: Stephen Boyd <sboyd@kernel.org>
Cc: Joel Stanley <joel@jms.id.au>
Cc: Andrew Jeffery <andrew@aj.id.au>
Cc: Taichi Sugaya <sugaya.taichi@socionext.com>
Cc: Takao Orito <orito.takao@socionext.com>
Cc: Qin Jian <qinjian@cqplus1.com>
Cc: Andrew Lunn <andrew@lunn.ch>
Cc: Gregory Clement <gregory.clement@bootlin.com>
Cc: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
Cc: Andy Gross <agross@kernel.org>
Cc: Bjorn Andersson <andersson@kernel.org>
Cc: Konrad Dybcio <konrad.dybcio@linaro.org>
Cc: Sergio Paracuellos <sergio.paracuellos@gmail.com>
Cc: Matthias Brugger <matthias.bgg@gmail.com>
Cc: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Chen-Yu Tsai <wens@csie.org>
Cc: Jernej Skrabec <jernej.skrabec@gmail.com>
Cc: David Airlie <airlied@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Samuel Holland <samuel@sholland.org>
Cc: Vinod Koul <vkoul@kernel.org>
Cc: Kishon Vijay Abraham I <kishon@kernel.org>
Cc: linux-clk@vger.kernel.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-aspeed@lists.ozlabs.org
Cc: linux-arm-msm@vger.kernel.org
Cc: linux-mediatek@lists.infradead.org
Cc: dri-devel@lists.freedesktop.org
Cc: linux-sunxi@lists.linux.dev
Cc: linux-phy@lists.infradead.org
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20230817203019.never.795-kees@kernel.org
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Stephen Boyd <sboyd@kernel.org>

authored by

Kees Cook and committed by
Stephen Boyd
f316cdff 06c2afb8

+11 -16
+1 -2
drivers/clk/clk-aspeed.c
··· 701 701 GFP_KERNEL); 702 702 if (!aspeed_clk_data) 703 703 return; 704 + aspeed_clk_data->num = ASPEED_NUM_CLKS; 704 705 705 706 /* 706 707 * This way all clocks fetched before the platform device probes, ··· 733 732 aspeed_ast2500_cc(map); 734 733 else 735 734 pr_err("unknown platform, failed to add clocks\n"); 736 - 737 - aspeed_clk_data->num = ASPEED_NUM_CLKS; 738 735 ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, aspeed_clk_data); 739 736 if (ret) 740 737 pr_err("failed to add DT provider: %d\n", ret);
+1 -1
drivers/clk/clk-ast2600.c
··· 839 839 ASPEED_G6_NUM_CLKS), GFP_KERNEL); 840 840 if (!aspeed_g6_clk_data) 841 841 return; 842 + aspeed_g6_clk_data->num = ASPEED_G6_NUM_CLKS; 842 843 843 844 /* 844 845 * This way all clocks fetched before the platform device probes, ··· 861 860 } 862 861 863 862 aspeed_g6_cc(map); 864 - aspeed_g6_clk_data->num = ASPEED_G6_NUM_CLKS; 865 863 ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, aspeed_g6_clk_data); 866 864 if (ret) 867 865 pr_err("failed to add DT provider: %d\n", ret);
+1 -1
drivers/clk/clk-gemini.c
··· 404 404 GFP_KERNEL); 405 405 if (!gemini_clk_data) 406 406 return; 407 + gemini_clk_data->num = GEMINI_NUM_CLKS; 407 408 408 409 /* 409 410 * This way all clock fetched before the platform device probes, ··· 458 457 gemini_clk_data->hws[GEMINI_CLK_APB] = hw; 459 458 460 459 /* Register the clocks to be accessed by the device tree */ 461 - gemini_clk_data->num = GEMINI_NUM_CLKS; 462 460 of_clk_add_hw_provider(np, of_clk_hw_onecell_get, gemini_clk_data); 463 461 } 464 462 CLK_OF_DECLARE_DRIVER(gemini_cc, "cortina,gemini-syscon", gemini_cc_init);
+1 -2
drivers/clk/clk-milbeaut.c
··· 618 618 619 619 if (!m10v_clk_data) 620 620 return; 621 + m10v_clk_data->num = M10V_NUM_CLKS; 621 622 622 623 base = of_iomap(np, 0); 623 624 if (!base) { ··· 655 654 base + CLKSEL(1), 0, 3, 0, rclk_table, 656 655 &m10v_crglock, NULL); 657 656 m10v_clk_data->hws[M10V_RCLK_ID] = hw; 658 - 659 - m10v_clk_data->num = M10V_NUM_CLKS; 660 657 of_clk_add_hw_provider(np, of_clk_hw_onecell_get, m10v_clk_data); 661 658 } 662 659 CLK_OF_DECLARE_DRIVER(m10v_cc, "socionext,milbeaut-m10v-ccu", m10v_cc_init);
+1 -2
drivers/clk/clk-sp7021.c
··· 621 621 GFP_KERNEL); 622 622 if (!clk_data) 623 623 return -ENOMEM; 624 + clk_data->num = CLK_MAX; 624 625 625 626 hws = clk_data->hws; 626 627 pd_ext.index = 0; ··· 688 687 if (IS_ERR(hws[i])) 689 688 return PTR_ERR(hws[i]); 690 689 } 691 - 692 - clk_data->num = CLK_MAX; 693 690 694 691 return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data); 695 692 }
+1 -1
drivers/clk/mvebu/cp110-system-controller.c
··· 240 240 GFP_KERNEL); 241 241 if (!cp110_clk_data) 242 242 return -ENOMEM; 243 + cp110_clk_data->num = CP110_CLK_NUM; 243 244 244 245 cp110_clks = cp110_clk_data->hws; 245 - cp110_clk_data->num = CP110_CLK_NUM; 246 246 247 247 /* Register the PLL0 which is the root of the hw tree */ 248 248 pll0_name = ap_cp_unique_name(dev, syscon_node, "pll0");
+1 -1
drivers/clk/qcom/clk-cpu-8996.c
··· 590 590 data = devm_kzalloc(dev, struct_size(data, hws, 2), GFP_KERNEL); 591 591 if (!data) 592 592 return -ENOMEM; 593 + data->num = 2; 593 594 594 595 base = devm_platform_ioremap_resource(pdev, 0); 595 596 if (IS_ERR(base)) ··· 606 605 607 606 data->hws[0] = &pwrcl_pmux.clkr.hw; 608 607 data->hws[1] = &perfcl_pmux.clkr.hw; 609 - data->num = 2; 610 608 611 609 return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, data); 612 610 }
+1 -2
drivers/clk/ralink/clk-mt7621.c
··· 521 521 GFP_KERNEL); 522 522 if (!clk_data) 523 523 return -ENOMEM; 524 + clk_data->num = count; 524 525 525 526 for (i = 0; i < ARRAY_SIZE(mt7621_clks_base); i++) 526 527 clk_data->hws[i] = mt7621_clk_early[i]; ··· 537 536 dev_err(dev, "Couldn't register fixed clock gates\n"); 538 537 goto unreg_clk_fixed; 539 538 } 540 - 541 - clk_data->num = count; 542 539 543 540 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data); 544 541 if (ret) {
+1 -2
drivers/gpu/drm/sun4i/sun8i_tcon_top.c
··· 141 141 GFP_KERNEL); 142 142 if (!clk_data) 143 143 return -ENOMEM; 144 + clk_data->num = CLK_NUM; 144 145 tcon_top->clk_data = clk_data; 145 146 146 147 spin_lock_init(&tcon_top->reg_lock); ··· 213 212 ret = PTR_ERR(clk_data->hws[i]); 214 213 goto err_unregister_gates; 215 214 } 216 - 217 - clk_data->num = CLK_NUM; 218 215 219 216 ret = of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get, 220 217 clk_data);
+1 -1
drivers/phy/qualcomm/phy-qcom-edp.c
··· 746 746 data = devm_kzalloc(edp->dev, struct_size(data, hws, 2), GFP_KERNEL); 747 747 if (!data) 748 748 return -ENOMEM; 749 + data->num = 2; 749 750 750 751 snprintf(name, sizeof(name), "%s::link_clk", dev_name(edp->dev)); 751 752 init.ops = &qcom_edp_dp_link_clk_ops; ··· 766 765 767 766 data->hws[0] = &edp->dp_link_hw; 768 767 data->hws[1] = &edp->dp_pixel_hw; 769 - data->num = 2; 770 768 771 769 return devm_of_clk_add_hw_provider(edp->dev, of_clk_hw_onecell_get, data); 772 770 }
+1 -1
include/linux/clk-provider.h
··· 1379 1379 1380 1380 struct clk_hw_onecell_data { 1381 1381 unsigned int num; 1382 - struct clk_hw *hws[]; 1382 + struct clk_hw *hws[] __counted_by(num); 1383 1383 }; 1384 1384 1385 1385 #define CLK_OF_DECLARE(name, compat, fn) \