Re: [PATCH 01/10] dm snapshot: Use kmalloc_array() in init_origin_hash()

From: Paul Bolle
Date: Thu Sep 29 2016 - 05:54:42 EST


Andy, Joe,

On Thu, 2016-09-29 at 11:07 +0200, SF Markus Elfring wrote:
> * Multiplications for the size determination of memory allocations
> Â indicated that array data structures should be processed.
> Â Thus use the corresponding function "kmalloc_array".
>
> Â This issue was detected by using the Coccinelle software.

We have no hope of fixing Markus' homegrown coccinelle script. But we
could try to fix the checkpatch false positive here. Something like:

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 206a6b3..b47201d 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -5693,7 +5693,7 @@ sub process {
$r2 = $a1;
}
if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
- !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
+ !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*\b/)) {
if (WARN("ALLOC_WITH_MULTIPLY",
"Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
$fix) {

Does that work for you too?

> --- a/drivers/md/dm-snap.c
> +++ b/drivers/md/dm-snap.c
> @@ -326,8 +326,9 @@ static int init_origin_hash(void)
> Â{
> Â int i;
> Â
> - _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
> - ÂÂÂGFP_KERNEL);
> + _origins = kmalloc_array(ORIGIN_HASH_SIZE,
> + Âsizeof(*_origins),
> + ÂGFP_KERNEL);
> Â if (!_origins) {
> Â DMERR("unable to allocate memory for _origins");
> Â return -ENOMEM;
> @@ -335,8 +336,9 @@ static int init_origin_hash(void)
> Â for (i = 0; i < ORIGIN_HASH_SIZE; i++)
> Â INIT_LIST_HEAD(_origins + i);
> Â
> - _dm_origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
> - ÂÂÂÂÂÂGFP_KERNEL);
> + _dm_origins = kmalloc_array(ORIGIN_HASH_SIZE,
> + ÂÂÂÂsizeof(*_dm_origins),
> + ÂÂÂÂGFP_KERNEL);
> Â if (!_dm_origins) {
> Â DMERR("unable to allocate memory for _dm_origins");
> Â kfree(_origins);

Thanks,


Paul Bolle