diff options
author | Gustavo A. R. Silva <gustavo@embeddedor.com> | 2019-06-10 11:04:12 -0400 |
---|---|---|
committer | Jens Axboe <axboe@kernel.dk> | 2019-06-15 03:47:58 -0400 |
commit | f1f8f292cd12292289cae87aac3a5c035186ec54 (patch) | |
tree | 849c847a9195ed97b0cb733da4e921aed6870ff5 /block | |
parent | 78b90a2ce8424eb4be4a6a1623dc7c07af8303aa (diff) |
block: bio: 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 bio_map_data {
...
struct iovec iov[];
};
instance = kmalloc(sizeof(sizeof(struct bio_map_data) + sizeof(struct iovec) *
count, GFP_KERNEL);
Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:
instance = kmalloc(struct_size(instance, iov, count), GFP_KERNEL);
This code was detected with the help of Coccinelle.
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'block')
-rw-r--r-- | block/bio.c | 3 |
1 files changed, 1 insertions, 2 deletions
diff --git a/block/bio.c b/block/bio.c index 683cbb40f051..4bcdcd3f63f4 100644 --- a/block/bio.c +++ b/block/bio.c | |||
@@ -1120,8 +1120,7 @@ static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data, | |||
1120 | if (data->nr_segs > UIO_MAXIOV) | 1120 | if (data->nr_segs > UIO_MAXIOV) |
1121 | return NULL; | 1121 | return NULL; |
1122 | 1122 | ||
1123 | bmd = kmalloc(sizeof(struct bio_map_data) + | 1123 | bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask); |
1124 | sizeof(struct iovec) * data->nr_segs, gfp_mask); | ||
1125 | if (!bmd) | 1124 | if (!bmd) |
1126 | return NULL; | 1125 | return NULL; |
1127 | memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs); | 1126 | memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs); |