* [PATCH 0/2] mtd: concat: use flex array
@ 2026-03-04 22:15 Rosen Penev
2026-03-04 22:15 ` [PATCH 1/2] mtd: virt_concat: use single allocation for node Rosen Penev
2026-03-04 22:15 ` [PATCH 2/2] mtd: concat: replace alloc + calloc with 1 alloc Rosen Penev
0 siblings, 2 replies; 5+ messages in thread
From: Rosen Penev @ 2026-03-04 22:15 UTC (permalink / raw)
To: linux-mtd
Cc: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, Kees Cook,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
Simplifies allocation. There may or may not be missing frees here. One
allocation is simpler to reason about.
Rosen Penev (2):
mtd: virt_concat: use single allocation for node
mtd: concat: replace alloc + calloc with 1 alloc
drivers/mtd/mtd_virt_concat.c | 19 +++----------------
drivers/mtd/mtdconcat.c | 5 +----
include/linux/mtd/concat.h | 2 +-
3 files changed, 5 insertions(+), 21 deletions(-)
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] mtd: virt_concat: use single allocation for node
2026-03-04 22:15 [PATCH 0/2] mtd: concat: use flex array Rosen Penev
@ 2026-03-04 22:15 ` Rosen Penev
2026-03-05 7:08 ` Philipp Matthias Hahn
2026-03-04 22:15 ` [PATCH 2/2] mtd: concat: replace alloc + calloc with 1 alloc Rosen Penev
1 sibling, 1 reply; 5+ messages in thread
From: Rosen Penev @ 2026-03-04 22:15 UTC (permalink / raw)
To: linux-mtd
Cc: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, Kees Cook,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
Simpler to reason about and avoids having to free nodes separately.
Also add __counted_by attribute for extra runtime analysis.
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/mtd/mtd_virt_concat.c | 11 ++---------
1 file changed, 2 insertions(+), 9 deletions(-)
diff --git a/drivers/mtd/mtd_virt_concat.c b/drivers/mtd/mtd_virt_concat.c
index aea88d1c9bc5..89e223edac6a 100644
--- a/drivers/mtd/mtd_virt_concat.c
+++ b/drivers/mtd/mtd_virt_concat.c
@@ -31,8 +31,8 @@ static LIST_HEAD(concat_node_list);
struct mtd_virt_concat_node {
struct list_head head;
unsigned int count;
- struct device_node **nodes;
struct mtd_concat *concat;
+ struct device_node *nodes[] __counted_by(nodes);
};
/**
@@ -133,7 +133,6 @@ int mtd_virt_concat_destroy(struct mtd_info *mtd)
for (idx = 0; idx < item->count; idx++)
of_node_put(item->nodes[idx]);
- kfree(item->nodes);
kfree(item);
}
return 0;
@@ -167,16 +166,11 @@ static int mtd_virt_concat_create_item(struct device_node *parts,
return 0;
}
- item = kzalloc(sizeof(*item), GFP_KERNEL);
+ item = kzalloc_flex(*item, nodes, count, GFP_KERNEL);
if (!item)
return -ENOMEM;
item->count = count;
- item->nodes = kcalloc(count, sizeof(*item->nodes), GFP_KERNEL);
- if (!item->nodes) {
- kfree(item);
- return -ENOMEM;
- }
/*
* The partition in which "part-concat-next" property
@@ -216,7 +210,6 @@ void mtd_virt_concat_destroy_items(void)
for (i = 0; i < item->count; i++)
of_node_put(item->nodes[i]);
- kfree(item->nodes);
kfree(item);
}
}
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/2] mtd: concat: replace alloc + calloc with 1 alloc
2026-03-04 22:15 [PATCH 0/2] mtd: concat: use flex array Rosen Penev
2026-03-04 22:15 ` [PATCH 1/2] mtd: virt_concat: use single allocation for node Rosen Penev
@ 2026-03-04 22:15 ` Rosen Penev
1 sibling, 0 replies; 5+ messages in thread
From: Rosen Penev @ 2026-03-04 22:15 UTC (permalink / raw)
To: linux-mtd
Cc: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, Kees Cook,
Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
A flex array can be used to reduce the allocation to 1.
And actually mtdconcat was using the pointer + 1 trick to point to the
overallocated area. Better alternatives exist.
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/mtd/mtd_virt_concat.c | 10 ++--------
drivers/mtd/mtdconcat.c | 5 +----
include/linux/mtd/concat.h | 2 +-
3 files changed, 4 insertions(+), 13 deletions(-)
diff --git a/drivers/mtd/mtd_virt_concat.c b/drivers/mtd/mtd_virt_concat.c
index 89e223edac6a..b56f2431974b 100644
--- a/drivers/mtd/mtd_virt_concat.c
+++ b/drivers/mtd/mtd_virt_concat.c
@@ -32,7 +32,7 @@ struct mtd_virt_concat_node {
struct list_head head;
unsigned int count;
struct mtd_concat *concat;
- struct device_node *nodes[] __counted_by(nodes);
+ struct device_node *nodes[] __counted_by(count);
};
/**
@@ -182,18 +182,12 @@ static int mtd_virt_concat_create_item(struct device_node *parts,
for (i = 1; i < count; i++)
item->nodes[i] = of_parse_phandle(parts, CONCAT_PROP, (i - 1));
- concat = kzalloc(sizeof(*concat), GFP_KERNEL);
+ concat = kzalloc_flex(*concat, subdev, count, GFP_KERNEL);
if (!concat) {
kfree(item);
return -ENOMEM;
}
- concat->subdev = kcalloc(count, sizeof(*concat->subdev), GFP_KERNEL);
- if (!concat->subdev) {
- kfree(item);
- kfree(concat);
- return -ENOMEM;
- }
item->concat = concat;
list_add_tail(&item->head, &concat_node_list);
diff --git a/drivers/mtd/mtdconcat.c b/drivers/mtd/mtdconcat.c
index 241d15235d01..c97167d51fe2 100644
--- a/drivers/mtd/mtdconcat.c
+++ b/drivers/mtd/mtdconcat.c
@@ -627,7 +627,6 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to c
const char *name)
{ /* name for the new device */
int i;
- size_t size;
struct mtd_concat *concat;
struct mtd_info *subdev_master = NULL;
uint32_t max_erasesize, curr_erasesize;
@@ -640,15 +639,13 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to c
printk(KERN_NOTICE "into device \"%s\"\n", name);
/* allocate the device structure */
- size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
- concat = kzalloc(size, GFP_KERNEL);
+ concat = kzalloc_flex(*concat, subdev, num_devs, GFP_KERNEL);
if (!concat) {
printk
("memory allocation error while creating concatenated device \"%s\"\n",
name);
return NULL;
}
- concat->subdev = (struct mtd_info **) (concat + 1);
/*
* Set up the new "super" device's MTD object structure, check for
diff --git a/include/linux/mtd/concat.h b/include/linux/mtd/concat.h
index 2cd9d48958a8..f8d4d6ac1fc1 100644
--- a/include/linux/mtd/concat.h
+++ b/include/linux/mtd/concat.h
@@ -18,7 +18,7 @@
struct mtd_concat {
struct mtd_info mtd;
int num_subdev;
- struct mtd_info **subdev;
+ struct mtd_info *subdev[];
};
struct mtd_info *mtd_concat_create(
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] mtd: virt_concat: use single allocation for node
2026-03-04 22:15 ` [PATCH 1/2] mtd: virt_concat: use single allocation for node Rosen Penev
@ 2026-03-05 7:08 ` Philipp Matthias Hahn
2026-03-05 8:52 ` Rosen Penev
0 siblings, 1 reply; 5+ messages in thread
From: Philipp Matthias Hahn @ 2026-03-05 7:08 UTC (permalink / raw)
To: Rosen Penev
Cc: linux-mtd, Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Kees Cook, Gustavo A. R. Silva, open list,
open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b
Hello,
Am Wed, Mar 04, 2026 at 02:15:29PM -0800 schrieb Rosen Penev:
> Also add __counted_by attribute for extra runtime analysis.
…
> diff --git a/drivers/mtd/mtd_virt_concat.c b/drivers/mtd/mtd_virt_concat.c
> index aea88d1c9bc5..89e223edac6a 100644
> --- a/drivers/mtd/mtd_virt_concat.c
> +++ b/drivers/mtd/mtd_virt_concat.c
> @@ -31,8 +31,8 @@ static LIST_HEAD(concat_node_list);
> struct mtd_virt_concat_node {
> struct list_head head;
> unsigned int count;
> - struct device_node **nodes;
> struct mtd_concat *concat;
> + struct device_node *nodes[] __counted_by(nodes);
Looks wrong. Your follow-up patch does that:
- struct device_node *nodes[] __counted_by(nodes);
+ struct device_node *nodes[] __counted_by(count);
Philipp
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] mtd: virt_concat: use single allocation for node
2026-03-05 7:08 ` Philipp Matthias Hahn
@ 2026-03-05 8:52 ` Rosen Penev
0 siblings, 0 replies; 5+ messages in thread
From: Rosen Penev @ 2026-03-05 8:52 UTC (permalink / raw)
To: Rosen Penev, linux-mtd, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, 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 4, 2026 at 11:08 PM Philipp Matthias Hahn <pmhahn@pmhahn.de> wrote:
>
> Hello,
>
> Am Wed, Mar 04, 2026 at 02:15:29PM -0800 schrieb Rosen Penev:
> > Also add __counted_by attribute for extra runtime analysis.
> …
> > diff --git a/drivers/mtd/mtd_virt_concat.c b/drivers/mtd/mtd_virt_concat.c
> > index aea88d1c9bc5..89e223edac6a 100644
> > --- a/drivers/mtd/mtd_virt_concat.c
> > +++ b/drivers/mtd/mtd_virt_concat.c
> > @@ -31,8 +31,8 @@ static LIST_HEAD(concat_node_list);
> > struct mtd_virt_concat_node {
> > struct list_head head;
> > unsigned int count;
> > - struct device_node **nodes;
> > struct mtd_concat *concat;
> > + struct device_node *nodes[] __counted_by(nodes);
>
> Looks wrong. Your follow-up patch does that:
> - struct device_node *nodes[] __counted_by(nodes);
> + struct device_node *nodes[] __counted_by(count);
D'oh. I'll resend in a day.
>
> Philipp
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-03-05 8:52 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-04 22:15 [PATCH 0/2] mtd: concat: use flex array Rosen Penev
2026-03-04 22:15 ` [PATCH 1/2] mtd: virt_concat: use single allocation for node Rosen Penev
2026-03-05 7:08 ` Philipp Matthias Hahn
2026-03-05 8:52 ` Rosen Penev
2026-03-04 22:15 ` [PATCH 2/2] mtd: concat: replace alloc + calloc with 1 alloc Rosen Penev
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox