Skip to contentSkip to conversation

clk: mvebu: use kzalloc_flex

PATCH In review Rosen Penev 3 participants 4 messages Apr 2 – Apr 7

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

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

---
 drivers/clk/mvebu/common.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
drivers/clk/mvebu/common.c +8 -11
index 28f2e1b2a932..4129690fcae0 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;
Signature · 1 line
2.53.0
#1 lore raw

Hi Rosen,

Quoted 46 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.

---
 drivers/clk/mvebu/common.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
drivers/clk/mvebu/common.c +8 -2
index 28f2e1b2a932..4129690fcae0 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;
 

drivers/clk/mvebu/common.c void __init mvebu_clk_gating_setup(struct device_node *np, · line 276

Quoted 10 lines
drivers/clk/mvebu/common.c +0 -8
 	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

Quoted 7 lines
drivers/clk/mvebu/common.c +0 -1
-
 	for (n = 0; n < ctrl->num_gates; n++) {
 		const char *parent =
 			(desc[n].parent) ? desc[n].parent : default_parent;
Signature · 1 line
2.53.0
#2 lore raw
In reply to Rosen Penev
kernel test robot noticed the following build warnings:

Hi Rosen,

kernel test robot noticed the following build warnings:

[auto build test WARNING on clk/clk-next]
[also build test WARNING on linus/master v7.0-rc6 next-20260403] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rosen-Penev/clk-mvebu-use-kzalloc_flex/20260405-093941
base:   https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git clk-next
patch link:    https://lore.kernel.org/r/20260402002011.89926-1-rosenp%40gmail.com
patch subject: [PATCH] clk: mvebu: use kzalloc_flex
config: arm-defconfig (https://download.01.org/0day-ci/archive/20260405/[email protected]/config)
compiler: clang version 23.0.0git (https://github.com/llvm/llvm-project c80443cd37b2e2788cba67ffa180a6331e5f0791)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260405/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/clk/mvebu/common.c:289:1: warning: unused label 'gates_out' [-Wunused-label]
     289 | gates_out:
         | ^~~~~~~~~~

1 warning generated.

vim +/gates_out +289 drivers/clk/mvebu/common.c

a97fbc3ee3e2a53 Thierry Reding        2025-10-29  236  
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  237  void __init mvebu_clk_gating_setup(struct device_node *np,
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  238  				   const struct clk_gating_soc_desc *desc)
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  239  {
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  240  	struct clk *clk;
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  241  	void __iomem *base;
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  242  	const char *default_parent = NULL;
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  243  	int n;
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  244  
f571053152f6607 Thomas Petazzoni      2014-11-21  245  	if (ctrl) {
7fbb639aea35383 Colin Ian King        2019-04-16  246  		pr_err("mvebu-clk-gating: cannot instantiate more than one gateable clock device\n");
f571053152f6607 Thomas Petazzoni      2014-11-21  247  		return;
f571053152f6607 Thomas Petazzoni      2014-11-21  248  	}
f571053152f6607 Thomas Petazzoni      2014-11-21  249  
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  250  	base = of_iomap(np, 0);
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  251  	if (WARN_ON(!base))
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  252  		return;
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  253  
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  254  	clk = of_clk_get(np, 0);
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  255  	if (!IS_ERR(clk)) {
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  256  		default_parent = __clk_get_name(clk);
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  257  		clk_put(clk);
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  258  	}
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  259  
c3d0c641ee00fc0 Rosen Penev           2026-04-01  260  	/* Count, allocate, and register clock gates */
c3d0c641ee00fc0 Rosen Penev           2026-04-01  261  	for (n = 0; desc[n].name;)
c3d0c641ee00fc0 Rosen Penev           2026-04-01  262  		n++;
c3d0c641ee00fc0 Rosen Penev           2026-04-01  263  
c3d0c641ee00fc0 Rosen Penev           2026-04-01  264  	ctrl = kzalloc_flex(*ctrl, gates, n);
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  265  	if (WARN_ON(!ctrl))
f98d007d332b819 Jisheng Zhang         2013-08-23  266  		goto ctrl_out;
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  267  
c3d0c641ee00fc0 Rosen Penev           2026-04-01  268  	ctrl->num_gates = n;
c3d0c641ee00fc0 Rosen Penev           2026-04-01  269  
87e392164ab6c0f Mike Turquette        2014-08-27  270  	/* lock must already be initialized */
87e392164ab6c0f Mike Turquette        2014-08-27  271  	ctrl->lock = &ctrl_gating_lock;
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  272  
f571053152f6607 Thomas Petazzoni      2014-11-21  273  	ctrl->base = base;
f571053152f6607 Thomas Petazzoni      2014-11-21  274  
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  275  	for (n = 0; n < ctrl->num_gates; n++) {
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  276  		const char *parent =
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  277  			(desc[n].parent) ? desc[n].parent : default_parent;
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  278  		ctrl->gates[n] = clk_register_gate(NULL, desc[n].name, parent,
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  279  					desc[n].flags, base, desc[n].bit_idx,
87e392164ab6c0f Mike Turquette        2014-08-27  280  					0, ctrl->lock);
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  281  		WARN_ON(IS_ERR(ctrl->gates[n]));
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  282  	}
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  283  
a45184099affdd5 Sebastian Hesselbarth 2013-05-11  284  	of_clk_add_provider(np, clk_gating_get_src, ctrl);
f98d007d332b819 Jisheng Zhang         2013-08-23  285  
a97fbc3ee3e2a53 Thierry Reding        2025-10-29  286  	register_syscore(&clk_gate_syscore);
f571053152f6607 Thomas Petazzoni      2014-11-21  287  
f98d007d332b819 Jisheng Zhang         2013-08-23  288  	return;

f98d007d332b819 Jisheng Zhang 2013-08-23 @289 gates_out:

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw
In reply to Rosen Penev
kernel test robot noticed the following build warnings:

Hi Rosen,

kernel test robot noticed the following build warnings:

[auto build test WARNING on clk/clk-next]
[also build test WARNING on linus/master v7.0-rc6 next-20260403] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rosen-Penev/clk-mvebu-use-kzalloc_flex/20260405-093941
base:   https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git clk-next
patch link:    https://lore.kernel.org/r/20260402002011.89926-1-rosenp%40gmail.com
patch subject: [PATCH] clk: mvebu: use kzalloc_flex
config: arm-randconfig-001-20260405 (https://download.01.org/0day-ci/archive/20260405/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 11.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260405/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

drivers/clk/mvebu/common.c: In function 'mvebu_clk_gating_setup':

>> drivers/clk/mvebu/common.c:289:1: warning: label 'gates_out' defined but not used [-Wunused-label]
     289 | gates_out:
         | ^~~~~~~~~

vim +/gates_out +289 drivers/clk/mvebu/common.c

a97fbc3ee3e2a5 Thierry Reding        2025-10-29  236  
a45184099affdd Sebastian Hesselbarth 2013-05-11  237  void __init mvebu_clk_gating_setup(struct device_node *np,
a45184099affdd Sebastian Hesselbarth 2013-05-11  238  				   const struct clk_gating_soc_desc *desc)
a45184099affdd Sebastian Hesselbarth 2013-05-11  239  {
a45184099affdd Sebastian Hesselbarth 2013-05-11  240  	struct clk *clk;
a45184099affdd Sebastian Hesselbarth 2013-05-11  241  	void __iomem *base;
a45184099affdd Sebastian Hesselbarth 2013-05-11  242  	const char *default_parent = NULL;
a45184099affdd Sebastian Hesselbarth 2013-05-11  243  	int n;
a45184099affdd Sebastian Hesselbarth 2013-05-11  244  
f571053152f660 Thomas Petazzoni      2014-11-21  245  	if (ctrl) {
7fbb639aea3538 Colin Ian King        2019-04-16  246  		pr_err("mvebu-clk-gating: cannot instantiate more than one gateable clock device\n");
f571053152f660 Thomas Petazzoni      2014-11-21  247  		return;
f571053152f660 Thomas Petazzoni      2014-11-21  248  	}
f571053152f660 Thomas Petazzoni      2014-11-21  249  
a45184099affdd Sebastian Hesselbarth 2013-05-11  250  	base = of_iomap(np, 0);
a45184099affdd Sebastian Hesselbarth 2013-05-11  251  	if (WARN_ON(!base))
a45184099affdd Sebastian Hesselbarth 2013-05-11  252  		return;
a45184099affdd Sebastian Hesselbarth 2013-05-11  253  
a45184099affdd Sebastian Hesselbarth 2013-05-11  254  	clk = of_clk_get(np, 0);
a45184099affdd Sebastian Hesselbarth 2013-05-11  255  	if (!IS_ERR(clk)) {
a45184099affdd Sebastian Hesselbarth 2013-05-11  256  		default_parent = __clk_get_name(clk);
a45184099affdd Sebastian Hesselbarth 2013-05-11  257  		clk_put(clk);
a45184099affdd Sebastian Hesselbarth 2013-05-11  258  	}
a45184099affdd Sebastian Hesselbarth 2013-05-11  259  
c3d0c641ee00fc Rosen Penev           2026-04-01  260  	/* Count, allocate, and register clock gates */
c3d0c641ee00fc Rosen Penev           2026-04-01  261  	for (n = 0; desc[n].name;)
c3d0c641ee00fc Rosen Penev           2026-04-01  262  		n++;
c3d0c641ee00fc Rosen Penev           2026-04-01  263  
c3d0c641ee00fc Rosen Penev           2026-04-01  264  	ctrl = kzalloc_flex(*ctrl, gates, n);
a45184099affdd Sebastian Hesselbarth 2013-05-11  265  	if (WARN_ON(!ctrl))
f98d007d332b81 Jisheng Zhang         2013-08-23  266  		goto ctrl_out;
a45184099affdd Sebastian Hesselbarth 2013-05-11  267  
c3d0c641ee00fc Rosen Penev           2026-04-01  268  	ctrl->num_gates = n;
c3d0c641ee00fc Rosen Penev           2026-04-01  269  
87e392164ab6c0 Mike Turquette        2014-08-27  270  	/* lock must already be initialized */
87e392164ab6c0 Mike Turquette        2014-08-27  271  	ctrl->lock = &ctrl_gating_lock;
a45184099affdd Sebastian Hesselbarth 2013-05-11  272  
f571053152f660 Thomas Petazzoni      2014-11-21  273  	ctrl->base = base;
f571053152f660 Thomas Petazzoni      2014-11-21  274  
a45184099affdd Sebastian Hesselbarth 2013-05-11  275  	for (n = 0; n < ctrl->num_gates; n++) {
a45184099affdd Sebastian Hesselbarth 2013-05-11  276  		const char *parent =
a45184099affdd Sebastian Hesselbarth 2013-05-11  277  			(desc[n].parent) ? desc[n].parent : default_parent;
a45184099affdd Sebastian Hesselbarth 2013-05-11  278  		ctrl->gates[n] = clk_register_gate(NULL, desc[n].name, parent,
a45184099affdd Sebastian Hesselbarth 2013-05-11  279  					desc[n].flags, base, desc[n].bit_idx,
87e392164ab6c0 Mike Turquette        2014-08-27  280  					0, ctrl->lock);
a45184099affdd Sebastian Hesselbarth 2013-05-11  281  		WARN_ON(IS_ERR(ctrl->gates[n]));
a45184099affdd Sebastian Hesselbarth 2013-05-11  282  	}
a45184099affdd Sebastian Hesselbarth 2013-05-11  283  
a45184099affdd Sebastian Hesselbarth 2013-05-11  284  	of_clk_add_provider(np, clk_gating_get_src, ctrl);
f98d007d332b81 Jisheng Zhang         2013-08-23  285  
a97fbc3ee3e2a5 Thierry Reding        2025-10-29  286  	register_syscore(&clk_gate_syscore);
f571053152f660 Thomas Petazzoni      2014-11-21  287  
f98d007d332b81 Jisheng Zhang         2013-08-23  288  	return;

f98d007d332b81 Jisheng Zhang 2013-08-23 @289 gates_out:

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki