public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] clk: visconti: pll: use kzalloc_flex
@ 2026-03-26  4:23 Rosen Penev
  2026-03-30 13:07 ` Brian Masney
  2026-03-31  1:05 ` nobuhiro.iwamatsu.x90
  0 siblings, 2 replies; 3+ messages in thread
From: Rosen Penev @ 2026-03-26  4:23 UTC (permalink / raw)
  To: linux-clk
  Cc: Michael Turquette, Stephen Boyd, Nobuhiro Iwamatsu, Kees Cook,
	Gustavo A. R. Silva,
	moderated list:ARM/TOSHIBA VISCONTI ARCHITECTURE, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

Simplify allocation by using a flexible array member and kzalloc_flex.

Add __counted_by for extra runtime analysis. Assign after allocation as
required by __counted_by.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/clk/visconti/pll.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/drivers/clk/visconti/pll.c b/drivers/clk/visconti/pll.c
index 6fd02c4b641e..ca29d12f9316 100644
--- a/drivers/clk/visconti/pll.c
+++ b/drivers/clk/visconti/pll.c
@@ -21,9 +21,9 @@ struct visconti_pll {
 	void __iomem	*pll_base;
 	spinlock_t	*lock;
 	unsigned long flags;
-	const struct visconti_pll_rate_table *rate_table;
 	size_t rate_count;
 	struct visconti_pll_provider *ctx;
+	struct visconti_pll_rate_table rate_table[] __counted_by(rate_count);
 };

 #define PLL_CONF_REG		0x0000
@@ -255,10 +255,6 @@ static struct clk_hw *visconti_register_pll(struct visconti_pll_provider *ctx,
 	size_t len;
 	int ret;

-	pll = kzalloc_obj(*pll);
-	if (!pll)
-		return ERR_PTR(-ENOMEM);
-
 	init.name = name;
 	init.flags = CLK_IGNORE_UNUSED;
 	init.parent_names = &parent_name;
@@ -266,11 +262,13 @@ static struct clk_hw *visconti_register_pll(struct visconti_pll_provider *ctx,

 	for (len = 0; rate_table[len].rate != 0; )
 		len++;
+
+	pll = kzalloc_flex(*pll, rate_table, len);
+	if (!pll)
+		return ERR_PTR(-ENOMEM);
+
 	pll->rate_count = len;
-	pll->rate_table = kmemdup_array(rate_table,
-					pll->rate_count, sizeof(*pll->rate_table),
-					GFP_KERNEL);
-	WARN(!pll->rate_table, "%s: could not allocate rate table for %s\n", __func__, name);
+	memcpy(pll->rate_table, rate_table, len * sizeof(*pll->rate_table));

 	init.ops = &visconti_pll_ops;
 	pll->hw.init = &init;
@@ -282,7 +280,6 @@ static struct clk_hw *visconti_register_pll(struct visconti_pll_provider *ctx,
 	ret = clk_hw_register(NULL, &pll->hw);
 	if (ret) {
 		pr_err("failed to register pll clock %s : %d\n", name, ret);
-		kfree(pll->rate_table);
 		kfree(pll);
 		pll_hw_clk = ERR_PTR(ret);
 	}
--
2.53.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] clk: visconti: pll: use kzalloc_flex
  2026-03-26  4:23 [PATCH] clk: visconti: pll: use kzalloc_flex Rosen Penev
@ 2026-03-30 13:07 ` Brian Masney
  2026-03-31  1:05 ` nobuhiro.iwamatsu.x90
  1 sibling, 0 replies; 3+ messages in thread
From: Brian Masney @ 2026-03-30 13:07 UTC (permalink / raw)
  To: Rosen Penev
  Cc: linux-clk, Michael Turquette, Stephen Boyd, Nobuhiro Iwamatsu,
	Kees Cook, Gustavo A. R. Silva,
	moderated list:ARM/TOSHIBA VISCONTI ARCHITECTURE, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

On Wed, Mar 25, 2026 at 09:23:17PM -0700, Rosen Penev wrote:
> Simplify allocation by using a flexible array member and kzalloc_flex.
> 
> Add __counted_by for extra runtime analysis. Assign after allocation as
> required by __counted_by.
> 
> Signed-off-by: Rosen Penev <rosenp@gmail.com>

Reviewed-by: Brian Masney <bmasney@redhat.com>

Note: Sashiko reported a separate issue related to the struct clk_init_data
not fully initialized to zero, and parent_data and parent_hws fields are
left containing stack garbage.

https://sashiko.dev/#/patchset/20260326042317.122536-1-rosenp%40gmail.com

I'll post a fix for this.

Brian


^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [PATCH] clk: visconti: pll: use kzalloc_flex
  2026-03-26  4:23 [PATCH] clk: visconti: pll: use kzalloc_flex Rosen Penev
  2026-03-30 13:07 ` Brian Masney
@ 2026-03-31  1:05 ` nobuhiro.iwamatsu.x90
  1 sibling, 0 replies; 3+ messages in thread
From: nobuhiro.iwamatsu.x90 @ 2026-03-31  1:05 UTC (permalink / raw)
  To: rosenp, linux-clk
  Cc: mturquette, sboyd, kees, gustavoars, linux-arm-kernel,
	linux-kernel, linux-hardening

Hi Rosen,

Thanks for your patch.

> -----Original Message-----
> From: Rosen Penev <rosenp@gmail.com>
> Sent: Thursday, March 26, 2026 1:23 PM
> To: linux-clk@vger.kernel.org
> Cc: Michael Turquette <mturquette@baylibre.com>; Stephen Boyd <sboyd@kernel.org>; iwamatsu nobuhiro(岩松 信洋
> □DITC○CPT) <nobuhiro.iwamatsu.x90@mail.toshiba>; Kees Cook <kees@kernel.org>; Gustavo A. R. Silva
> <gustavoars@kernel.org>; moderated list:ARM/TOSHIBA VISCONTI ARCHITECTURE
> <linux-arm-kernel@lists.infradead.org>; open list <linux-kernel@vger.kernel.org>; open list:KERNEL HARDENING (not
> covered by other areas):Keyword:\b__counted_by(_le|_be)?\b <linux-hardening@vger.kernel.org>
> Subject: [PATCH] clk: visconti: pll: use kzalloc_flex
> 
> Simplify allocation by using a flexible array member and kzalloc_flex.
> 
> Add __counted_by for extra runtime analysis. Assign after allocation as required by __counted_by.
> 
> Signed-off-by: Rosen Penev <rosenp@gmail.com>

Acked-by: Nobuhiro Iwamatsu <nobuhiro.iwamatsu.x90@mail.toshiba>

Best regards,
  Nobuhiro
> ---
>  drivers/clk/visconti/pll.c | 17 +++++++----------
>  1 file changed, 7 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/clk/visconti/pll.c b/drivers/clk/visconti/pll.c index 6fd02c4b641e..ca29d12f9316 100644
> --- a/drivers/clk/visconti/pll.c
> +++ b/drivers/clk/visconti/pll.c
> @@ -21,9 +21,9 @@ struct visconti_pll {
>  	void __iomem	*pll_base;
>  	spinlock_t	*lock;
>  	unsigned long flags;
> -	const struct visconti_pll_rate_table *rate_table;
>  	size_t rate_count;
>  	struct visconti_pll_provider *ctx;
> +	struct visconti_pll_rate_table rate_table[] __counted_by(rate_count);
>  };
> 
>  #define PLL_CONF_REG		0x0000
> @@ -255,10 +255,6 @@ static struct clk_hw *visconti_register_pll(struct visconti_pll_provider *ctx,
>  	size_t len;
>  	int ret;
> 
> -	pll = kzalloc_obj(*pll);
> -	if (!pll)
> -		return ERR_PTR(-ENOMEM);
> -
>  	init.name = name;
>  	init.flags = CLK_IGNORE_UNUSED;
>  	init.parent_names = &parent_name;
> @@ -266,11 +262,13 @@ static struct clk_hw *visconti_register_pll(struct visconti_pll_provider *ctx,
> 
>  	for (len = 0; rate_table[len].rate != 0; )
>  		len++;
> +
> +	pll = kzalloc_flex(*pll, rate_table, len);
> +	if (!pll)
> +		return ERR_PTR(-ENOMEM);
> +
>  	pll->rate_count = len;
> -	pll->rate_table = kmemdup_array(rate_table,
> -					pll->rate_count, sizeof(*pll->rate_table),
> -					GFP_KERNEL);
> -	WARN(!pll->rate_table, "%s: could not allocate rate table for %s\n", __func__, name);
> +	memcpy(pll->rate_table, rate_table, len * sizeof(*pll->rate_table));
> 
>  	init.ops = &visconti_pll_ops;
>  	pll->hw.init = &init;
> @@ -282,7 +280,6 @@ static struct clk_hw *visconti_register_pll(struct visconti_pll_provider *ctx,
>  	ret = clk_hw_register(NULL, &pll->hw);
>  	if (ret) {
>  		pr_err("failed to register pll clock %s : %d\n", name, ret);
> -		kfree(pll->rate_table);
>  		kfree(pll);
>  		pll_hw_clk = ERR_PTR(ret);
>  	}
> --
> 2.53.0



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-03-31  2:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-26  4:23 [PATCH] clk: visconti: pll: use kzalloc_flex Rosen Penev
2026-03-30 13:07 ` Brian Masney
2026-03-31  1:05 ` nobuhiro.iwamatsu.x90

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox