Skip to content

Commit 8adeac3

Browse files
GustavoARSilvasnitm
authored andcommitted
dm stripe: use struct_size() in kmalloc()
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 stripe_c { ... struct stripe stripe[0]; }; In this case alloc_context() and dm_array_too_big() are removed and replaced by the direct use of the struct_size() helper in kmalloc(). Notice that open-coded form is prone to type mistakes. 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 53be73a commit 8adeac3

2 files changed

Lines changed: 1 addition & 17 deletions

File tree

drivers/md/dm-stripe.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,6 @@ static void trigger_event(struct work_struct *work)
5555
dm_table_event(sc->ti->table);
5656
}
5757

58-
static inline struct stripe_c *alloc_context(unsigned int stripes)
59-
{
60-
size_t len;
61-
62-
if (dm_array_too_big(sizeof(struct stripe_c), sizeof(struct stripe),
63-
stripes))
64-
return NULL;
65-
66-
len = sizeof(struct stripe_c) + (sizeof(struct stripe) * stripes);
67-
68-
return kmalloc(len, GFP_KERNEL);
69-
}
70-
7158
/*
7259
* Parse a single <dev> <sector> pair
7360
*/
@@ -142,7 +129,7 @@ static int stripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
142129
return -EINVAL;
143130
}
144131

145-
sc = alloc_context(stripes);
132+
sc = kmalloc(struct_size(sc, stripe, stripes), GFP_KERNEL);
146133
if (!sc) {
147134
ti->error = "Memory allocation for striped context "
148135
"failed";

include/linux/device-mapper.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,6 @@ void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
594594
*/
595595
#define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
596596

597-
#define dm_array_too_big(fixed, obj, num) \
598-
((num) > (UINT_MAX - (fixed)) / (obj))
599-
600597
/*
601598
* Sector offset taken relative to the start of the target instead of
602599
* relative to the start of the device.

0 commit comments

Comments
 (0)