public inbox for linux-clk@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] clk: mvebu: use kzalloc_flex
@ 2026-04-02  0:20 Rosen Penev
  2026-04-03 13:29 ` Brian Masney
  0 siblings, 1 reply; 2+ messages in thread
From: Rosen Penev @ 2026-04-02  0:20 UTC (permalink / raw)
  To: linux-clk
  Cc: Andrew Lunn, Gregory Clement, Sebastian Hesselbarth,
	Michael Turquette, Stephen Boyd, Kees Cook, Gustavo A. R. Silva,
	moderated list:ARM/Marvell Kirkwood and Armada 370, 375, 38x,...,
	open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

Use a flexible array member to combine kzalloc and kcalloc in one
allocation so they can be freed together.

Add __counted_by for extra runtime analysis. Move counting variable
assignment right after allocation as done by kzalloc_flex with GCC >=
15.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/clk/mvebu/common.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/drivers/clk/mvebu/common.c b/drivers/clk/mvebu/common.c
index 28f2e1b2a932..4129690fcae0 100644
--- a/drivers/clk/mvebu/common.c
+++ b/drivers/clk/mvebu/common.c
@@ -189,10 +189,10 @@ DEFINE_SPINLOCK(ctrl_gating_lock);
 
 struct clk_gating_ctrl {
 	spinlock_t *lock;
-	struct clk **gates;
 	int num_gates;
 	void __iomem *base;
 	u32 saved_reg;
+	struct clk *gates[] __counted_by(num_gates);
 };
 
 static struct clk_gating_ctrl *ctrl;
@@ -257,24 +257,21 @@ void __init mvebu_clk_gating_setup(struct device_node *np,
 		clk_put(clk);
 	}
 
-	ctrl = kzalloc_obj(*ctrl);
+	/* Count, allocate, and register clock gates */
+	for (n = 0; desc[n].name;)
+		n++;
+
+	ctrl = kzalloc_flex(*ctrl, gates, n);
 	if (WARN_ON(!ctrl))
 		goto ctrl_out;
 
+	ctrl->num_gates = n;
+
 	/* lock must already be initialized */
 	ctrl->lock = &ctrl_gating_lock;
 
 	ctrl->base = base;
 
-	/* Count, allocate, and register clock gates */
-	for (n = 0; desc[n].name;)
-		n++;
-
-	ctrl->num_gates = n;
-	ctrl->gates = kzalloc_objs(*ctrl->gates, ctrl->num_gates);
-	if (WARN_ON(!ctrl->gates))
-		goto gates_out;
-
 	for (n = 0; n < ctrl->num_gates; n++) {
 		const char *parent =
 			(desc[n].parent) ? desc[n].parent : default_parent;
-- 
2.53.0


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

* Re: [PATCH] clk: mvebu: use kzalloc_flex
  2026-04-02  0:20 [PATCH] clk: mvebu: use kzalloc_flex Rosen Penev
@ 2026-04-03 13:29 ` Brian Masney
  0 siblings, 0 replies; 2+ messages in thread
From: Brian Masney @ 2026-04-03 13:29 UTC (permalink / raw)
  To: Rosen Penev
  Cc: linux-clk, Andrew Lunn, Gregory Clement, Sebastian Hesselbarth,
	Michael Turquette, Stephen Boyd, Kees Cook, Gustavo A. R. Silva,
	moderated list:ARM/Marvell Kirkwood and Armada 370, 375, 38x,...,
	open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

Hi Rosen,

On Wed, Apr 01, 2026 at 05:20:11PM -0700, Rosen Penev wrote:
> Use a flexible array member to combine kzalloc and kcalloc in one
> allocation so they can be freed together.
> 
> Add __counted_by for extra runtime analysis. Move counting variable
> assignment right after allocation as done by kzalloc_flex with GCC >=
> 15.
> 
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
>  drivers/clk/mvebu/common.c | 19 ++++++++-----------
>  1 file changed, 8 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/clk/mvebu/common.c b/drivers/clk/mvebu/common.c
> index 28f2e1b2a932..4129690fcae0 100644
> --- a/drivers/clk/mvebu/common.c
> +++ b/drivers/clk/mvebu/common.c
> @@ -189,10 +189,10 @@ DEFINE_SPINLOCK(ctrl_gating_lock);
>  
>  struct clk_gating_ctrl {
>  	spinlock_t *lock;
> -	struct clk **gates;
>  	int num_gates;
>  	void __iomem *base;
>  	u32 saved_reg;
> +	struct clk *gates[] __counted_by(num_gates);
>  };
>  
>  static struct clk_gating_ctrl *ctrl;
> @@ -257,24 +257,21 @@ void __init mvebu_clk_gating_setup(struct device_node *np,
>  		clk_put(clk);
>  	}
>  
> -	ctrl = kzalloc_obj(*ctrl);
> +	/* Count, allocate, and register clock gates */
> +	for (n = 0; desc[n].name;)
> +		n++;
> +
> +	ctrl = kzalloc_flex(*ctrl, gates, n);
>  	if (WARN_ON(!ctrl))
>  		goto ctrl_out;
>  
> +	ctrl->num_gates = n;
> +
>  	/* lock must already be initialized */
>  	ctrl->lock = &ctrl_gating_lock;
>  
>  	ctrl->base = base;
>  
> -	/* Count, allocate, and register clock gates */
> -	for (n = 0; desc[n].name;)
> -		n++;
> -
> -	ctrl->num_gates = n;
> -	ctrl->gates = kzalloc_objs(*ctrl->gates, ctrl->num_gates);
> -	if (WARN_ON(!ctrl->gates))
> -		goto gates_out;

The gates_out label and kfree() needs to be removed.

Brian

> -
>  	for (n = 0; n < ctrl->num_gates; n++) {
>  		const char *parent =
>  			(desc[n].parent) ? desc[n].parent : default_parent;
> -- 
> 2.53.0
> 


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

end of thread, other threads:[~2026-04-03 13:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-02  0:20 [PATCH] clk: mvebu: use kzalloc_flex Rosen Penev
2026-04-03 13:29 ` Brian Masney

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