Skip to contentSkip to conversation

clk: visconti: pll: use kzalloc_flex

PATCH In review Rosen Penev 3 participants 3 messages Mar 26 – Mar 31

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 drivers/clk/visconti/pll.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/clk/visconti/pll.c +7 -10
index 6fd02c4b641e..ca29d12f9316 100644
@@ -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

Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Rosen Penev

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.

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

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Rosen Penev

Hi Rosen,

Thanks for your patch.

Quoted 11 lines

-----Original Message-----
From: Rosen Penev <[email protected]>
Sent: Thursday, March 26, 2026 1:23 PM
To: [email protected]
Cc: Michael Turquette <[email protected]>; Stephen Boyd <[email protected]>; iwamatsu nobuhiro(岩松 信洋 □DITC○CPT) <[email protected]>; Kees Cook <[email protected]>; Gustavo A. R. Silva <[email protected]>; moderated list:ARM/TOSHIBA VISCONTI ARCHITECTURE <[email protected]>; open list <[email protected]>; open list:KERNEL HARDENING (not covered by other areas):Keyword:\b__counted_by(_le|_be)?\b <[email protected]>
Subject: [PATCH] clk: visconti: pll: use kzalloc_flex

Quoted 5 lines

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.

Best regards,
Nobuhiro

Quoted 57 lines
---
 drivers/clk/visconti/pll.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/clk/visconti/pll.c +7 -11
@@ -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