public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Rosen Penev <rosenp@gmail.com>
To: linux-edac@vger.kernel.org
Cc: Borislav Petkov <bp@alien8.de>, Tony Luck <tony.luck@intel.com>,
	Kees Cook <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	linux-kernel@vger.kernel.org (open list),
	linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not
	covered by other areas):Keyword:\b__counted_by(_le|_be)?\b)
Subject: [PATCH 1/2] EDAC/device: simplify info allocation
Date: Thu, 30 Apr 2026 15:00:45 -0700	[thread overview]
Message-ID: <20260430220046.72371-2-rosenp@gmail.com> (raw)
In-Reply-To: <20260430220046.72371-1-rosenp@gmail.com>

Use a flexible array member to combine info and instance allocations.
Simplifies allocation slightly.

Add __counted_by for extra runtime analysis. Move counting variable
assignment to after allocation as kzalloc_flex already does with GCC >=
15.

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

diff --git a/drivers/edac/edac_device.c b/drivers/edac/edac_device.c
index cf0d3c2dfc04..9c21997a50e0 100644
--- a/drivers/edac/edac_device.c
+++ b/drivers/edac/edac_device.c
@@ -58,31 +58,25 @@ edac_device_alloc_ctl_info(unsigned pvt_sz, char *dev_name, unsigned nr_instance
 			   char *blk_name, unsigned nr_blocks, unsigned off_val,
 			   int device_index)
 {
-	struct edac_device_block *dev_blk, *blk_p, *blk;
+	struct edac_device_block *blk_p, *blk;
 	struct edac_device_instance *dev_inst, *inst;
 	struct edac_device_ctl_info *dev_ctl;
 	unsigned instance, block;
+	size_t alloc_size;
 	void *pvt;
 	int err;
 
 	edac_dbg(4, "instances=%d blocks=%d\n", nr_instances, nr_blocks);
 
-	dev_ctl = kzalloc_obj(struct edac_device_ctl_info);
+	alloc_size = struct_size(dev_ctl, instances, nr_instances);
+	alloc_size += sizeof(*dev_ctl->blocks) * nr_instances * nr_blocks;
+	dev_ctl = kzalloc(alloc_size, GFP_KERNEL);
 	if (!dev_ctl)
 		return NULL;
 
-	dev_inst = kzalloc_objs(struct edac_device_instance, nr_instances);
-	if (!dev_inst)
-		goto free;
-
-	dev_ctl->instances = dev_inst;
-
-	dev_blk = kzalloc_objs(struct edac_device_block,
-			       nr_instances * nr_blocks);
-	if (!dev_blk)
-		goto free;
+	dev_ctl->nr_instances = nr_instances;
 
-	dev_ctl->blocks = dev_blk;
+	dev_ctl->blocks = (struct edac_device_block *)(dev_ctl->instances + nr_instances);
 
 	if (pvt_sz) {
 		pvt = kzalloc(pvt_sz, GFP_KERNEL);
@@ -93,7 +87,6 @@ edac_device_alloc_ctl_info(unsigned pvt_sz, char *dev_name, unsigned nr_instance
 	}
 
 	dev_ctl->dev_idx	= device_index;
-	dev_ctl->nr_instances	= nr_instances;
 
 	/* Default logging of CEs and UEs */
 	dev_ctl->log_ce = 1;
@@ -107,7 +100,7 @@ edac_device_alloc_ctl_info(unsigned pvt_sz, char *dev_name, unsigned nr_instance
 		inst = &dev_inst[instance];
 		inst->ctl = dev_ctl;
 		inst->nr_blocks = nr_blocks;
-		blk_p = &dev_blk[instance * nr_blocks];
+		blk_p = &dev_ctl->blocks[instance * nr_blocks];
 		inst->blocks = blk_p;
 
 		/* name of this instance */
diff --git a/drivers/edac/edac_device.h b/drivers/edac/edac_device.h
index 24c1921aa490..4562461a3e8f 100644
--- a/drivers/edac/edac_device.h
+++ b/drivers/edac/edac_device.h
@@ -203,7 +203,6 @@ struct edac_device_ctl_info {
 	 * and the array of those instances
 	 */
 	u32 nr_instances;
-	struct edac_device_instance *instances;
 	struct edac_device_block *blocks;
 
 	/* Event counters for the this whole EDAC Device */
@@ -213,6 +212,7 @@ struct edac_device_ctl_info {
 	 * device this structure controls
 	 */
 	struct kobject kobj;
+	struct edac_device_instance instances[] __counted_by(nr_instances);
 };
 
 /* To get from the instance's wq to the beginning of the ctl structure */
@@ -341,8 +341,6 @@ static inline void __edac_device_free_ctl_info(struct edac_device_ctl_info *ci)
 {
 	if (ci) {
 		kfree(ci->pvt_info);
-		kfree(ci->blocks);
-		kfree(ci->instances);
 		kfree(ci);
 	}
 }
-- 
2.54.0


  reply	other threads:[~2026-04-30 22:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-30 22:00 [PATCH 0/2] EDAC/device: simplify allocation Rosen Penev
2026-04-30 22:00 ` Rosen Penev [this message]
2026-05-01 14:47   ` [PATCH 1/2] EDAC/device: simplify info allocation Zhuo, Qiuxu
2026-04-30 22:00 ` [PATCH 2/2] EDAC/device: 3 to 1 allocations in edac_dev_feat_ctx Rosen Penev
2026-05-01 14:54   ` Zhuo, Qiuxu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260430220046.72371-2-rosenp@gmail.com \
    --to=rosenp@gmail.com \
    --cc=bp@alien8.de \
    --cc=gustavoars@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-edac@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tony.luck@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox