Himangi Saraogi
2013-Nov-06 20:55 UTC
[PATCH v2] btrfs:check-integrity.c: replace kmalloc with kmalloc_array and kzalloc with kzalloc_array.
This patch replaces kmalloc(size * nr, ) with kmalloc_array(nr, size)
as kmalloc_array() is preferred because it can check that the
calculation doesn''t wrap and won''t return a smaller
allocation.
Also kzalloc(size * nr) was replaced with kzalloc_array().
Signed-off-by: Himangi Saraogi <himangi774@gmail.com>
---
fs/btrfs/check-integrity.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/fs/btrfs/check-integrity.c b/fs/btrfs/check-integrity.c
index 1c47be1..925a52b 100644
--- a/fs/btrfs/check-integrity.c
+++ b/fs/btrfs/check-integrity.c
@@ -1660,9 +1660,10 @@ static int btrfsic_read_block(struct btrfsic_state
*state,
num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
PAGE_CACHE_SHIFT;
- block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) +
- sizeof(*block_ctx->pagev)) *
- num_pages, GFP_NOFS);
+ block_ctx->mem_to_free = kzalloc_array(num_pages,
+ (sizeof(*block_ctx->datav) +
+ sizeof(*block_ctx->pagev)),
+ GFP_NOFS);
if (!block_ctx->mem_to_free)
return -1;
block_ctx->datav = block_ctx->mem_to_free;
@@ -3031,8 +3032,9 @@ void btrfsic_submit_bio(int rw, struct bio *bio)
(unsigned long long)bio->bi_sector, dev_bytenr,
bio->bi_bdev);
- mapped_datav = kmalloc(sizeof(*mapped_datav) * bio->bi_vcnt,
- GFP_NOFS);
+ mapped_datav = kmalloc_array(bio->bi_vcnt,
+ sizeof(*mapped_datav),
+ GFP_NOFS);
if (!mapped_datav)
goto leave;
for (i = 0; i < bio->bi_vcnt; i++) {
--
1.7.9.5
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs"
in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Rusty Russell
2013-Nov-08 00:40 UTC
Re: [OPW kernel] [PATCH v2] btrfs:check-integrity.c: replace kmalloc with kmalloc_array and kzalloc with kzalloc_array.
Himangi Saraogi <himangi774@gmail.com> writes:> This patch replaces kmalloc(size * nr, ) with kmalloc_array(nr, size) > as kmalloc_array() is preferred because it can check that the > calculation doesn''t wrap and won''t return a smaller allocation. > Also kzalloc(size * nr) was replaced with kzalloc_array(). > > Signed-off-by: Himangi Saraogi <himangi774@gmail.com>Reviewed-by: Rusty Russell <rusty@rustcorp.com.au> Cheers, Rusty.> --- > fs/btrfs/check-integrity.c | 12 +++++++----- > 1 file changed, 7 insertions(+), 5 deletions(-) > > diff --git a/fs/btrfs/check-integrity.c b/fs/btrfs/check-integrity.c > index 1c47be1..925a52b 100644 > --- a/fs/btrfs/check-integrity.c > +++ b/fs/btrfs/check-integrity.c > @@ -1660,9 +1660,10 @@ static int btrfsic_read_block(struct btrfsic_state *state, > > num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >> > PAGE_CACHE_SHIFT; > - block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) + > - sizeof(*block_ctx->pagev)) * > - num_pages, GFP_NOFS); > + block_ctx->mem_to_free = kzalloc_array(num_pages, > + (sizeof(*block_ctx->datav) + > + sizeof(*block_ctx->pagev)), > + GFP_NOFS); > if (!block_ctx->mem_to_free) > return -1; > block_ctx->datav = block_ctx->mem_to_free; > @@ -3031,8 +3032,9 @@ void btrfsic_submit_bio(int rw, struct bio *bio) > (unsigned long long)bio->bi_sector, dev_bytenr, > bio->bi_bdev); > > - mapped_datav = kmalloc(sizeof(*mapped_datav) * bio->bi_vcnt, > - GFP_NOFS); > + mapped_datav = kmalloc_array(bio->bi_vcnt, > + sizeof(*mapped_datav), > + GFP_NOFS); > if (!mapped_datav) > goto leave; > for (i = 0; i < bio->bi_vcnt; i++) { > -- > 1.7.9.5 > > -- > You received this message because you are subscribed to the Google Groups "opw-kernel" group. > To unsubscribe from this group and stop receiving emails from it, send an email to opw-kernel+unsubscribe@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out.-- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Josef Bacik
2013-Nov-15 19:59 UTC
Re: [PATCH v2] btrfs:check-integrity.c: replace kmalloc with kmalloc_array and kzalloc with kzalloc_array.
On Thu, Nov 07, 2013 at 02:25:50AM +0530, Himangi Saraogi wrote:> This patch replaces kmalloc(size * nr, ) with kmalloc_array(nr, size) > as kmalloc_array() is preferred because it can check that the > calculation doesn''t wrap and won''t return a smaller allocation. > Also kzalloc(size * nr) was replaced with kzalloc_array(). > > Signed-off-by: Himangi Saraogi <himangi774@gmail.com>This fails to build. Thanks, Josef -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html