public inbox for linux-edac@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] EDAC/mc: Use kzalloc_flex()
@ 2026-03-12  5:08 Rosen Penev
  2026-03-19 17:15 ` Yazen Ghannam
  0 siblings, 1 reply; 2+ messages in thread
From: Rosen Penev @ 2026-03-12  5:08 UTC (permalink / raw)
  To: linux-edac
  Cc: Borislav Petkov, Tony Luck, Kees Cook, Gustavo A. R. Silva,
	open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

Change kzalloc + kcalloc to just kzalloc with a flexible array member.

Add __counted_by for extra runtime analysis when requested.

Move counting assignment immediately after allocation as required by
__counted_by.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/edac/edac_mc.c |  9 ++-------
 include/linux/edac.h   | 23 ++++++++++++-----------
 2 files changed, 14 insertions(+), 18 deletions(-)

diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c
index 29e9828422bb..1ffab0269aec 100644
--- a/drivers/edac/edac_mc.c
+++ b/drivers/edac/edac_mc.c
@@ -203,7 +203,6 @@ static void mci_release(struct device *dev)
 		kfree(mci->csrows);
 	}
 	kfree(mci->pvt_info);
-	kfree(mci->layers);
 	kfree(mci);
 }
 
@@ -361,25 +360,21 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num,
 			per_rank = true;
 	}
 
-	mci = kzalloc_obj(struct mem_ctl_info);
+	mci = kzalloc_flex(*mci, layers, n_layers);
 	if (!mci)
 		return NULL;
 
-	mci->layers = kzalloc_objs(struct edac_mc_layer, n_layers);
-	if (!mci->layers)
-		goto error;
-
 	mci->pvt_info = kzalloc(sz_pvt, GFP_KERNEL);
 	if (!mci->pvt_info)
 		goto error;
 
+	mci->n_layers = n_layers;
 	mci->dev.release = mci_release;
 	device_initialize(&mci->dev);
 
 	/* setup index and various internal pointers */
 	mci->mc_idx = mc_num;
 	mci->tot_dimms = tot_dimms;
-	mci->n_layers = n_layers;
 	memcpy(mci->layers, layers, sizeof(*layer) * n_layers);
 	mci->nr_csrows = tot_csrows;
 	mci->num_cschannel = tot_channels;
diff --git a/include/linux/edac.h b/include/linux/edac.h
index fa32f2aca22f..deba46b3ee25 100644
--- a/include/linux/edac.h
+++ b/include/linux/edac.h
@@ -541,17 +541,6 @@ struct mem_ctl_info {
 	struct csrow_info **csrows;
 	unsigned int nr_csrows, num_cschannel;
 
-	/*
-	 * Memory Controller hierarchy
-	 *
-	 * There are basically two types of memory controller: the ones that
-	 * sees memory sticks ("dimms"), and the ones that sees memory ranks.
-	 * All old memory controllers enumerate memories per rank, but most
-	 * of the recent drivers enumerate memories per DIMM, instead.
-	 * When the memory controller is per rank, csbased is true.
-	 */
-	unsigned int n_layers;
-	struct edac_mc_layer *layers;
 	bool csbased;
 
 	/*
@@ -609,6 +598,18 @@ struct mem_ctl_info {
 	u8 fake_inject_layer[EDAC_MAX_LAYERS];
 	bool fake_inject_ue;
 	u16 fake_inject_count;
+
+	/*
+	 * Memory Controller hierarchy
+	 *
+	 * There are basically two types of memory controller: the ones that
+	 * sees memory sticks ("dimms"), and the ones that sees memory ranks.
+	 * All old memory controllers enumerate memories per rank, but most
+	 * of the recent drivers enumerate memories per DIMM, instead.
+	 * When the memory controller is per rank, csbased is true.
+	 */
+	unsigned int n_layers;
+	struct edac_mc_layer layers[] __counted_by(n_layers);
 };
 
 #define mci_for_each_dimm(mci, dimm)				\
-- 
2.53.0


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

* Re: [PATCH] EDAC/mc: Use kzalloc_flex()
  2026-03-12  5:08 [PATCH] EDAC/mc: Use kzalloc_flex() Rosen Penev
@ 2026-03-19 17:15 ` Yazen Ghannam
  0 siblings, 0 replies; 2+ messages in thread
From: Yazen Ghannam @ 2026-03-19 17:15 UTC (permalink / raw)
  To: Rosen Penev
  Cc: linux-edac, Borislav Petkov, Tony Luck, Kees Cook,
	Gustavo A. R. Silva, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

On Wed, Mar 11, 2026 at 10:08:13PM -0700, Rosen Penev wrote:
> Change kzalloc + kcalloc to just kzalloc with a flexible array member.
> 
> Add __counted_by for extra runtime analysis when requested.
> 
> Move counting assignment immediately after allocation as required by
> __counted_by.
> 

[...]

> diff --git a/include/linux/edac.h b/include/linux/edac.h
[...]
> -	unsigned int n_layers;
> -	struct edac_mc_layer *layers;
>  	bool csbased;
>  
>  	/*
> @@ -609,6 +598,18 @@ struct mem_ctl_info {
>  	u8 fake_inject_layer[EDAC_MAX_LAYERS];
>  	bool fake_inject_ue;
>  	u16 fake_inject_count;
> +
[...]
> +	unsigned int n_layers;
> +	struct edac_mc_layer layers[] __counted_by(n_layers);
>  };
>  

So this is not a just an in-place macro conversion, correct?

You're redefining 'struct mem_ctl_info' to have a flexible array member.
I see this was done with the SB and i7 modules.

I'd prefer if this was explicitly stated in the commit message.

  "Convert struct mem_ctl_info to use flex array...and use the new flex
  array helpers to simplify initialization..."

Just an example of some highlights ^^^.

Otherwise, looks good to me.

Reviewed-by: Yazen Ghannam <yazen.ghannam@amd.com>

Thanks,
Yazen

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

end of thread, other threads:[~2026-03-19 17:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-12  5:08 [PATCH] EDAC/mc: Use kzalloc_flex() Rosen Penev
2026-03-19 17:15 ` Yazen Ghannam

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