Skip to contentSkip to conversation

clk: mvebu: use kzalloc_flex

PATCH Applied Rosen Penev 3 participants 3 messages Apr 3 – Apr 29

Subsystems (2)

v2 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 clk: mvebu: use kzalloc_flex

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.

---
v2: remove now unused goto label.

 drivers/clk/mvebu/common.c | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)
drivers/clk/mvebu/common.c +8 -13
index 28f2e1b2a932..0f9ae5f5cefd 100644
@@ -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;
@@ -289,8 +286,6 @@ void __init mvebu_clk_gating_setup(struct device_node *np,
 	register_syscore(&clk_gate_syscore);
 
 	return;
-gates_out:
-	kfree(ctrl);
 ctrl_out:
 	iounmap(base);
 }

--
2.53.0

Reviewed-by on v2 1/1
#1 lore raw

Hi Rosen,

Quoted 10 lines · Rosen Penev

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.

---
v2: remove now unused goto label.

This is the third time that I've asked you this [1]: For the future, if someone asks for changes in a previous version, then be sure to CC them on the next revision. I was the one that found the unused goto in v1.

[1] https://lore.kernel.org/linux-clk/[email protected]/
https://lore.kernel.org/linux-clk/[email protected]/

maintainer Applied
#2 lore raw
In reply to Rosen Penev

Quoting Rosen Penev (2026-04-03 12:47:01)

Quoted 9 lines

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.

---

Applied to clk-next