* [PATCH] block: bio: use struct_size() in kmalloc()
@ 2019-05-17 9:12 xiaolinkui
2019-05-17 17:18 ` Chaitanya Kulkarni
2019-05-17 21:17 ` Jens Axboe
0 siblings, 2 replies; 7+ messages in thread
From: xiaolinkui @ 2019-05-17 9:12 UTC (permalink / raw)
To: axboe; +Cc: linux-block, linux-kernel, xiaolinkui
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 foo {
int stuff;
struct boo entry[];
};
instance = kmalloc(sizeof(struct foo) + count * sizeof(struct boo), 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, entry, count), GFP_KERNEL);
Signed-off-by: xiaolinkui <xiaolinkui@kylinos.cn>
---
block/bio.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/block/bio.c b/block/bio.c
index 683cbb4..847ac60 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -436,9 +436,7 @@ struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs,
if (nr_iovecs > UIO_MAXIOV)
return NULL;
- p = kmalloc(sizeof(struct bio) +
- nr_iovecs * sizeof(struct bio_vec),
- gfp_mask);
+ p = kmalloc(struct_size(bio, bi_io_vec, nr_iovecs), gfp_mask);
front_pad = 0;
inline_vecs = nr_iovecs;
} else {
@@ -1120,8 +1118,7 @@ static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data,
if (data->nr_segs > UIO_MAXIOV)
return NULL;
- bmd = kmalloc(sizeof(struct bio_map_data) +
- sizeof(struct iovec) * data->nr_segs, gfp_mask);
+ bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask);
if (!bmd)
return NULL;
memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs);
--
2.7.4
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] block: bio: use struct_size() in kmalloc()
2019-05-17 9:12 [PATCH] block: bio: use struct_size() in kmalloc() xiaolinkui
@ 2019-05-17 17:18 ` Chaitanya Kulkarni
2019-05-17 21:17 ` Jens Axboe
1 sibling, 0 replies; 7+ messages in thread
From: Chaitanya Kulkarni @ 2019-05-17 17:18 UTC (permalink / raw)
To: xiaolinkui, axboe; +Cc: linux-block, linux-kernel
Looks good.
Reviewed-by: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
On 5/17/19 2:17 AM, xiaolinkui wrote:
> 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 foo {
> int stuff;
> struct boo entry[];
> };
>
> instance = kmalloc(sizeof(struct foo) + count * sizeof(struct boo), 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, entry, count), GFP_KERNEL);
>
> Signed-off-by: xiaolinkui <xiaolinkui@kylinos.cn>
> ---
> block/bio.c | 7 ++-----
> 1 file changed, 2 insertions(+), 5 deletions(-)
>
> diff --git a/block/bio.c b/block/bio.c
> index 683cbb4..847ac60 100644
> --- a/block/bio.c
> +++ b/block/bio.c
> @@ -436,9 +436,7 @@ struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs,
> if (nr_iovecs > UIO_MAXIOV)
> return NULL;
>
> - p = kmalloc(sizeof(struct bio) +
> - nr_iovecs * sizeof(struct bio_vec),
> - gfp_mask);
> + p = kmalloc(struct_size(bio, bi_io_vec, nr_iovecs), gfp_mask);
> front_pad = 0;
> inline_vecs = nr_iovecs;
> } else {
> @@ -1120,8 +1118,7 @@ static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data,
> if (data->nr_segs > UIO_MAXIOV)
> return NULL;
>
> - bmd = kmalloc(sizeof(struct bio_map_data) +
> - sizeof(struct iovec) * data->nr_segs, gfp_mask);
> + bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask);
> if (!bmd)
> return NULL;
> memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs);
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] block: bio: use struct_size() in kmalloc()
2019-05-17 9:12 [PATCH] block: bio: use struct_size() in kmalloc() xiaolinkui
2019-05-17 17:18 ` Chaitanya Kulkarni
@ 2019-05-17 21:17 ` Jens Axboe
2019-05-17 22:59 ` Jens Axboe
1 sibling, 1 reply; 7+ messages in thread
From: Jens Axboe @ 2019-05-17 21:17 UTC (permalink / raw)
To: xiaolinkui; +Cc: linux-block, linux-kernel
On 5/17/19 3:12 AM, xiaolinkui wrote:
> 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 foo {
> int stuff;
> struct boo entry[];
> };
>
> instance = kmalloc(sizeof(struct foo) + count * sizeof(struct boo), 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, entry, count), GFP_KERNEL);
Applied, thanks.
--
Jens Axboe
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] block: bio: use struct_size() in kmalloc()
2019-05-17 21:17 ` Jens Axboe
@ 2019-05-17 22:59 ` Jens Axboe
0 siblings, 0 replies; 7+ messages in thread
From: Jens Axboe @ 2019-05-17 22:59 UTC (permalink / raw)
To: xiaolinkui; +Cc: linux-block, linux-kernel
On 5/17/19 3:17 PM, Jens Axboe wrote:
> On 5/17/19 3:12 AM, xiaolinkui wrote:
>> 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 foo {
>> int stuff;
>> struct boo entry[];
>> };
>>
>> instance = kmalloc(sizeof(struct foo) + count * sizeof(struct boo), 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, entry, count), GFP_KERNEL);
>
> Applied, thanks.
I take that back, you obviously didn't even compile this patch. Never
send untested crap, without explicitly saying so.
--
Jens Axboe
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] block: bio: Use struct_size() in kmalloc()
2019-06-10 15:04 [PATCH] block: bio: Use " Gustavo A. R. Silva
2019-06-10 17:53 ` Kees Cook
@ 2019-06-15 7:45 ` Jens Axboe
1 sibling, 0 replies; 7+ messages in thread
From: Jens Axboe @ 2019-06-15 7:45 UTC (permalink / raw)
To: Gustavo A. R. Silva; +Cc: linux-block, linux-kernel, Kees Cook
On 6/10/19 9:04 AM, Gustavo A. R. Silva wrote:
> 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.
Applied, thanks.
--
Jens Axboe
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] block: bio: Use struct_size() in kmalloc()
2019-06-10 15:04 [PATCH] block: bio: Use " Gustavo A. R. Silva
@ 2019-06-10 17:53 ` Kees Cook
2019-06-15 7:45 ` Jens Axboe
1 sibling, 0 replies; 7+ messages in thread
From: Kees Cook @ 2019-06-10 17:53 UTC (permalink / raw)
To: Gustavo A. R. Silva; +Cc: Jens Axboe, linux-block, linux-kernel
On Mon, Jun 10, 2019 at 10:04:12AM -0500, Gustavo A. R. Silva wrote:
> 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.
>
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
-Kees
> ---
> block/bio.c | 3 +--
> 1 file changed, 1 insertion(+), 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,
> if (data->nr_segs > UIO_MAXIOV)
> return NULL;
>
> - bmd = kmalloc(sizeof(struct bio_map_data) +
> - sizeof(struct iovec) * data->nr_segs, gfp_mask);
> + bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask);
> if (!bmd)
> return NULL;
> memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs);
> --
> 2.21.0
>
--
Kees Cook
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH] block: bio: Use struct_size() in kmalloc()
@ 2019-06-10 15:04 Gustavo A. R. Silva
2019-06-10 17:53 ` Kees Cook
2019-06-15 7:45 ` Jens Axboe
0 siblings, 2 replies; 7+ messages in thread
From: Gustavo A. R. Silva @ 2019-06-10 15:04 UTC (permalink / raw)
To: Jens Axboe; +Cc: linux-block, linux-kernel, Gustavo A. R. Silva, Kees Cook
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.
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
block/bio.c | 3 +--
1 file changed, 1 insertion(+), 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,
if (data->nr_segs > UIO_MAXIOV)
return NULL;
- bmd = kmalloc(sizeof(struct bio_map_data) +
- sizeof(struct iovec) * data->nr_segs, gfp_mask);
+ bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask);
if (!bmd)
return NULL;
memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs);
--
2.21.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
end of thread, other threads:[~2019-06-15 7:45 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-17 9:12 [PATCH] block: bio: use struct_size() in kmalloc() xiaolinkui
2019-05-17 17:18 ` Chaitanya Kulkarni
2019-05-17 21:17 ` Jens Axboe
2019-05-17 22:59 ` Jens Axboe
2019-06-10 15:04 [PATCH] block: bio: Use " Gustavo A. R. Silva
2019-06-10 17:53 ` Kees Cook
2019-06-15 7:45 ` Jens Axboe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).