Skip to content

Commit e19eaff

Browse files
nehebmiquelraynal
authored andcommitted
mtd: concat: replace alloc + calloc with 1 alloc
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> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
1 parent c685e6e commit e19eaff

3 files changed

Lines changed: 3 additions & 12 deletions

File tree

drivers/mtd/mtd_virt_concat.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,12 @@ static int mtd_virt_concat_create_item(struct device_node *parts,
182182
for (i = 1; i < count; i++)
183183
item->nodes[i] = of_parse_phandle(parts, CONCAT_PROP, (i - 1));
184184

185-
concat = kzalloc(sizeof(*concat), GFP_KERNEL);
185+
concat = kzalloc_flex(*concat, subdev, count, GFP_KERNEL);
186186
if (!concat) {
187187
kfree(item);
188188
return -ENOMEM;
189189
}
190190

191-
concat->subdev = kcalloc(count, sizeof(*concat->subdev), GFP_KERNEL);
192-
if (!concat->subdev) {
193-
kfree(item);
194-
kfree(concat);
195-
return -ENOMEM;
196-
}
197191
item->concat = concat;
198192

199193
list_add_tail(&item->head, &concat_node_list);

drivers/mtd/mtdconcat.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,6 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to c
627627
const char *name)
628628
{ /* name for the new device */
629629
int i;
630-
size_t size;
631630
struct mtd_concat *concat;
632631
struct mtd_info *subdev_master = NULL;
633632
uint32_t max_erasesize, curr_erasesize;
@@ -640,15 +639,13 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to c
640639
printk(KERN_NOTICE "into device \"%s\"\n", name);
641640

642641
/* allocate the device structure */
643-
size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
644-
concat = kzalloc(size, GFP_KERNEL);
642+
concat = kzalloc_flex(*concat, subdev, num_devs, GFP_KERNEL);
645643
if (!concat) {
646644
printk
647645
("memory allocation error while creating concatenated device \"%s\"\n",
648646
name);
649647
return NULL;
650648
}
651-
concat->subdev = (struct mtd_info **) (concat + 1);
652649

653650
/*
654651
* Set up the new "super" device's MTD object structure, check for

include/linux/mtd/concat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
struct mtd_concat {
1919
struct mtd_info mtd;
2020
int num_subdev;
21-
struct mtd_info **subdev;
21+
struct mtd_info *subdev[];
2222
};
2323

2424
struct mtd_info *mtd_concat_create(

0 commit comments

Comments
 (0)