Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1539623

Re: [PATCH] md: Combine two kmalloc() calls into one in sb_equal()

From Joe Perches <joe@perches.com>
Newsgroups linux.kernel
Subject Re: [PATCH] md: Combine two kmalloc() calls into one in sb_equal()
Date 2016-12-09 20:10 +0100
Message-ID <sMyIp-77f-13@gated-at.bofh.it> (permalink)
References <sMyfo-6Hl-13@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Fri, 2016-12-09 at 19:30 +0100, SF Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Fri, 9 Dec 2016 19:09:13 +0100
> 
> The function "kmalloc" was called in one case by the function "sb_equal"
> without checking immediately if it failed.
> This issue was detected by using the Coccinelle software.
> 
> Perform the desired memory allocation (and release at the end)
> by a single function call instead.
> 
> Fixes: 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 ("Linux-2.6.12-rc2")

Making a change does not mean fixes.

There's nothing particularly _wrong_ with the code as-is.

2 kmemdup calls might make the code more obvious.

There's a small optimization possible in that only the
first MB_SB_GENERIC_CONSTANT_WORDS of the struct are
actually compared.  Alloc and copy of both entire structs
is inefficient and unnecessary.

Perhaps something like the below would be marginally
better/faster, but the whole thing is dubious.

static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
{
	int ret;
	void *tmp1, *tmp2;

	tmp1 = kmemdup(sb1, MD_SB_GENERIC_CONSTANT_WORDS * sizeof(__u32), GFP_KERNEL);
	tmp2 = kmemdup(sb2, MD_SB_GENERIC_CONSTANT_WORDS * sizeof(__u32), GFP_KERNEL);

	if (!tmp1 || !tmp2) {
		ret = 0;
		goto out;
	}

	/*
	 * nr_disks is not constant
	 */
	((mdp_super_t *)tmp1)->nr_disks = 0;
	((mdp_super_t *)tmp2)->nr_disks = 0;

	ret = memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * sizeof(__u32)) == 0;

out:
	kfree(tmp1);
	kfree(tmp2);
	return ret;
}

> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  drivers/md/md.c | 13 ++++---------
>  1 file changed, 4 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index b088668269b0..86caf2536255 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -843,15 +843,12 @@ static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
>  	int ret;
>  	mdp_super_t *tmp1, *tmp2;
>  
> -	tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
> -	tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
> -
> -	if (!tmp1 || !tmp2) {
> -		ret = 0;
> -		goto abort;
> -	}
> +	tmp1 = kmalloc(2 * sizeof(*tmp1), GFP_KERNEL);
> +	if (!tmp1)
> +		return 0;
>  
>  	*tmp1 = *sb1;
> +	tmp2 = tmp1 + 1;
>  	*tmp2 = *sb2;
>  
>  	/*
> @@ -861,9 +858,7 @@ static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
>  	tmp2->nr_disks = 0;
>  
>  	ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
> -abort:
>  	kfree(tmp1);
> -	kfree(tmp2);
>  	return ret;
>  }

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH] md: Combine two kmalloc() calls into one in sb_equal() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-12-09 19:40 +0100
  Re: [PATCH] md: Combine two kmalloc() calls into one in sb_equal() Joe Perches <joe@perches.com> - 2016-12-09 20:10 +0100
    Re: md: Combine two kmalloc() calls into one in sb_equal() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-12-09 21:10 +0100
      Re: md: Combine two kmalloc() calls into one in sb_equal() Joe Perches <joe@perches.com> - 2016-12-09 22:00 +0100
    Re: [PATCH] md: Combine two kmalloc() calls into one in sb_equal() Al Viro <viro@ZenIV.linux.org.uk> - 2016-12-09 22:40 +0100
      Re: [PATCH] md: Combine two kmalloc() calls into one in sb_equal() Joe Perches <joe@perches.com> - 2016-12-09 23:00 +0100
  Re: [PATCH] md: Combine two kmalloc() calls into one in sb_equal() Bernd Schubert <bernd.schubert@fastmail.fm> - 2016-12-09 20:20 +0100
    Re: md: Combine two kmalloc() calls into one in sb_equal() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-12-09 21:00 +0100
      Re: md: Combine two kmalloc() calls into one in sb_equal() Bernd Schubert <bernd.schubert@fastmail.fm> - 2016-12-09 22:20 +0100
        Re: md: Combine two kmalloc() calls into one in sb_equal() SF Markus Elfring <elfring@users.sourceforge.net> - 2016-12-09 23:00 +0100
          Re: md: Combine two kmalloc() calls into one in sb_equal() Bernd Schubert <bernd.schubert@fastmail.fm> - 2016-12-09 23:10 +0100

csiph-web