mtd: concat: use flex array
v2 cover mtd: concat: use flex array
Simplifies allocation. There may or may not be missing frees here. One allocation is simpler to reason about.
v2: fix mistake in counted_by in first commit directly.
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
PATCH v2 1/2 mtd: virt_concat: use single allocation for node
Simpler to reason about and avoids having to free nodes separately.
Also add __counted_by attribute for extra runtime analysis.
--- drivers/mtd/mtd_virt_concat.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-)
drivers/mtd/mtd_virt_concat.c +2 -9
@@ -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(count); };/**
@@ -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); } }
Signature · 1 line
2.53.0
PATCH v2 2/2 mtd: concat: replace alloc + calloc with 1 alloc
In reply to Rosen PenevA 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.
--- drivers/mtd/mtd_virt_concat.c | 8 +------- drivers/mtd/mtdconcat.c | 5 +---- include/linux/mtd/concat.h | 2 +- 3 files changed, 3 insertions(+), 12 deletions(-)
drivers/mtd/mtd_virt_concat.c +1 -7
@@ -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);
drivers/mtd/mtdconcat.c +1 -4
@@ -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 cprintk (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
include/linux/mtd/concat.h +1 -1
@@ -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(
Signature · 1 line
2.53.0
Quoted 10 lines · Rosen Penev
Simplifies allocation. There may or may not be missing frees here. One allocation is simpler to reason about.
v2: fix mistake in
counted_byin first commit directly.Rosen Penev (2):
mtd:virt_concat: use single allocation for node
mtd: concat: replace alloc + calloc with 1 alloc[...]
Applied to mtd/next, thanks!
[1/2] mtd: virt_concat: use single allocation for node
commit: c685e6e8d88d544e8c4429b06c3e6795cbba32dd
[2/2] mtd: concat: replace alloc + calloc with 1 alloc
commit: e19eaffc5213fdd6179e849d3032929fae0d8c2cPatche(s) should be available on mtd/linux.git and will be part of the next PR (provided that no robot complains by then).
Kind regards,
Miquèl