Skip to content

Commit bcd6765

Browse files
GustavoARSilvasnitm
authored andcommitted
dm raid1: use struct_size() with kzalloc()
One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example: struct mirror_set { ... struct mirror mirror[0]; }; size = sizeof(struct mirror_set) + count * sizeof(struct mirror); instance = kzalloc(size, GFP_KERNEL) Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper: instance = kzalloc(struct_size(instance, mirror, count), GFP_KERNEL) Notice that, in this case, variable len is not necessary, hence it is removed. This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com> Signed-off-by: Mike Snitzer <snitzer@redhat.com>
1 parent 5229b48 commit bcd6765

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

drivers/md/dm-raid1.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -878,12 +878,9 @@ static struct mirror_set *alloc_context(unsigned int nr_mirrors,
878878
struct dm_target *ti,
879879
struct dm_dirty_log *dl)
880880
{
881-
size_t len;
882-
struct mirror_set *ms = NULL;
883-
884-
len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
881+
struct mirror_set *ms =
882+
kzalloc(struct_size(ms, mirror, nr_mirrors), GFP_KERNEL);
885883

886-
ms = kzalloc(len, GFP_KERNEL);
887884
if (!ms) {
888885
ti->error = "Cannot allocate mirror context";
889886
return NULL;

0 commit comments

Comments
 (0)