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 2/2] EDAC/device: 3 to 1 allocations in edac_dev_feat_ctx
Date: Thu, 30 Apr 2026 15:00:46 -0700 [thread overview]
Message-ID: <20260430220046.72371-3-rosenp@gmail.com> (raw)
In-Reply-To: <20260430220046.72371-1-rosenp@gmail.com>
Simplifies memory handling slightly by using a flexible array member.
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/edac/edac_device.c | 40 +++++++++++++-------------------------
include/linux/edac.h | 2 +-
2 files changed, 15 insertions(+), 27 deletions(-)
diff --git a/drivers/edac/edac_device.c b/drivers/edac/edac_device.c
index 9c21997a50e0..a6c41ce68f13 100644
--- a/drivers/edac/edac_device.c
+++ b/drivers/edac/edac_device.c
@@ -569,8 +569,6 @@ static void edac_dev_release(struct device *dev)
{
struct edac_dev_feat_ctx *ctx = container_of(dev, struct edac_dev_feat_ctx, dev);
- kfree(ctx->mem_repair);
- kfree(ctx->scrub);
kfree(ctx->dev.groups);
kfree(ctx);
}
@@ -612,6 +610,7 @@ int edac_dev_register(struct device *parent, char *name,
int attr_gcnt = 0;
int ret = -ENOMEM;
int scrub_cnt = 0;
+ size_t alloc_size;
int feat;
if (!parent || !name || !num_features || !ras_features)
@@ -636,26 +635,18 @@ int edac_dev_register(struct device *parent, char *name,
}
}
- ctx = kzalloc_obj(*ctx);
+ alloc_size = struct_size(ctx, scrub, scrub_cnt);
+ alloc_size += sizeof(*ctx->mem_repair) * mem_repair_cnt;
+ ctx = kzalloc(alloc_size, GFP_KERNEL);
if (!ctx)
return -ENOMEM;
+ ctx->mem_repair = ctx->scrub + scrub_cnt;
+
ras_attr_groups = kzalloc_objs(*ras_attr_groups, attr_gcnt + 1);
if (!ras_attr_groups)
goto ctx_free;
- if (scrub_cnt) {
- ctx->scrub = kzalloc_objs(*ctx->scrub, scrub_cnt);
- if (!ctx->scrub)
- goto groups_free;
- }
-
- if (mem_repair_cnt) {
- ctx->mem_repair = kzalloc_objs(*ctx->mem_repair, mem_repair_cnt);
- if (!ctx->mem_repair)
- goto data_mem_free;
- }
-
attr_gcnt = 0;
scrub_cnt = 0;
mem_repair_cnt = 0;
@@ -664,7 +655,7 @@ int edac_dev_register(struct device *parent, char *name,
case RAS_FEAT_SCRUB:
if (!ras_features->scrub_ops || scrub_cnt != ras_features->instance) {
ret = -EINVAL;
- goto data_mem_free;
+ goto groups_free;
}
dev_data = &ctx->scrub[scrub_cnt];
@@ -674,7 +665,7 @@ int edac_dev_register(struct device *parent, char *name,
ret = edac_scrub_get_desc(parent, &ras_attr_groups[attr_gcnt],
ras_features->instance);
if (ret)
- goto data_mem_free;
+ goto groups_free;
scrub_cnt++;
attr_gcnt++;
@@ -682,7 +673,7 @@ int edac_dev_register(struct device *parent, char *name,
case RAS_FEAT_ECS:
if (!ras_features->ecs_ops) {
ret = -EINVAL;
- goto data_mem_free;
+ goto groups_free;
}
dev_data = &ctx->ecs;
@@ -691,7 +682,7 @@ int edac_dev_register(struct device *parent, char *name,
ret = edac_ecs_get_desc(parent, &ras_attr_groups[attr_gcnt],
ras_features->ecs_info.num_media_frus);
if (ret)
- goto data_mem_free;
+ goto groups_free;
attr_gcnt += ras_features->ecs_info.num_media_frus;
break;
@@ -699,7 +690,7 @@ int edac_dev_register(struct device *parent, char *name,
if (!ras_features->mem_repair_ops ||
mem_repair_cnt != ras_features->instance) {
ret = -EINVAL;
- goto data_mem_free;
+ goto groups_free;
}
dev_data = &ctx->mem_repair[mem_repair_cnt];
@@ -709,14 +700,14 @@ int edac_dev_register(struct device *parent, char *name,
ret = edac_mem_repair_get_desc(parent, &ras_attr_groups[attr_gcnt],
ras_features->instance);
if (ret)
- goto data_mem_free;
+ goto groups_free;
mem_repair_cnt++;
attr_gcnt++;
break;
default:
ret = -EINVAL;
- goto data_mem_free;
+ goto groups_free;
}
}
@@ -729,7 +720,7 @@ int edac_dev_register(struct device *parent, char *name,
ret = dev_set_name(&ctx->dev, "%s", name);
if (ret)
- goto data_mem_free;
+ goto groups_free;
ret = device_register(&ctx->dev);
if (ret) {
@@ -739,9 +730,6 @@ int edac_dev_register(struct device *parent, char *name,
return devm_add_action_or_reset(parent, edac_dev_unreg, &ctx->dev);
-data_mem_free:
- kfree(ctx->mem_repair);
- kfree(ctx->scrub);
groups_free:
kfree(ras_attr_groups);
ctx_free:
diff --git a/include/linux/edac.h b/include/linux/edac.h
index 7a3c1c60dea7..b3ca9e3d0521 100644
--- a/include/linux/edac.h
+++ b/include/linux/edac.h
@@ -864,9 +864,9 @@ struct edac_dev_data {
struct edac_dev_feat_ctx {
struct device dev;
void *private;
- struct edac_dev_data *scrub;
struct edac_dev_data ecs;
struct edac_dev_data *mem_repair;
+ struct edac_dev_data scrub[];
};
struct edac_dev_feature {
--
2.54.0
next prev parent 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 ` [PATCH 1/2] EDAC/device: simplify info allocation Rosen Penev
2026-05-01 14:47 ` Zhuo, Qiuxu
2026-04-30 22:00 ` Rosen Penev [this message]
2026-05-01 14:54 ` [PATCH 2/2] EDAC/device: 3 to 1 allocations in edac_dev_feat_ctx 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-3-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