All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: "Markus Elfring" <Markus.Elfring@web.de>,
	linux-s390@vger.kernel.org,
	"Christian Bornträger" <borntraeger@de.ibm.com>,
	"Harald Freudenberger" <freude@linux.ibm.com>,
	"Heiko Carstens" <heiko.carstens@de.ibm.com>,
	"Ingo Franzki" <ifranzki@linux.ibm.com>,
	"Vasily Gorbik" <gor@linux.ibm.com>
Cc: LKML <linux-kernel@vger.kernel.org>, kernel-janitors@vger.kernel.org
Subject: Re: [PATCH] s390/pkey: Use memdup_user() rather than duplicating its implementation
Date: Wed, 06 Nov 2019 02:38:39 -0800	[thread overview]
Message-ID: <6137855bb4170c438c7436cbdb7dfd21639a8855.camel@perches.com> (raw)
In-Reply-To: <08422b7e-2071-ee52-049e-c3ac55bc67a9@web.de>

On Wed, 2019-11-06 at 11:22 +0100, Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Wed, 6 Nov 2019 11:11:42 +0100
> 
> Reuse existing functionality from memdup_user() instead of keeping
> duplicate source code.
> 
> Generated by: scripts/coccinelle/api/memdup_user.cocci
> 
> Delete local variables which became unnecessary with this refactoring
> in two function implementations.
> 
> Fixes: f2bbc96e7cfad3891b7bf9bd3e566b9b7ab4553d ("s390/pkey: add CCA AES cipher key support")

This doesn't fix anything and the Fixes: line is not appropriate.

> diff --git a/drivers/s390/crypto/pkey_api.c b/drivers/s390/crypto/pkey_api.c
[]
> @@ -715,36 +715,16 @@ static int pkey_apqns4keytype(enum pkey_key_type ktype,
> 
>  static void *_copy_key_from_user(void __user *ukey, size_t keylen)
>  {
> -	void *kkey;
> -
> -	if (!ukey || keylen < MINKEYBLOBSIZE || keylen > KEYBLOBBUFSIZE)
> -		return ERR_PTR(-EINVAL);
> -	kkey = kmalloc(keylen, GFP_KERNEL);
> -	if (!kkey)
> -		return ERR_PTR(-ENOMEM);
> -	if (copy_from_user(kkey, ukey, keylen)) {
> -		kfree(kkey);
> -		return ERR_PTR(-EFAULT);
> -	}
> -
> -	return kkey;
> +	return !ukey || keylen < MINKEYBLOBSIZE || keylen > KEYBLOBBUFSIZE
> +	       ? ERR_PTR(-EINVAL)
> +	       : memdup_user(ukey, keylen);

This is a very poor use of ternary ?: code.
This is much more readable for humans.

	if (!ukey || keylen < MINKEYBLOBSIZE || keylen > KBLOBBUFSIZE)
		return ERR_PTR(-EINVAL);

	return memdup_user(ukey, keylen);

The compiler will produce the same code.

>  static void *_copy_apqns_from_user(void __user *uapqns, size_t nr_apqns)
>  {
> -	void *kapqns = NULL;
> -	size_t nbytes;
> -
> -	if (uapqns && nr_apqns > 0) {
> -		nbytes = nr_apqns * sizeof(struct pkey_apqn);
> -		kapqns = kmalloc(nbytes, GFP_KERNEL);
> -		if (!kapqns)
> -			return ERR_PTR(-ENOMEM);
> -		if (copy_from_user(kapqns, uapqns, nbytes))
> -			return ERR_PTR(-EFAULT);
> -	}
> -
> -	return kapqns;
> +	return uapqns && nr_apqns > 0
> +	       ? memdup_user(uapqns, nr_apqns * sizeof(struct pkey_apqn))
> +	       : NULL;

And here you reverse the form of the earlier block.
Please be consistent and use this style:

	if (!uapqns || nr_apqns <= 0)
		return NULL;

	return memdup_user(uapqns, nr_apqns * sizeof(struct pkey_apqn));



WARNING: multiple messages have this Message-ID (diff)
From: Joe Perches <joe@perches.com>
To: "Markus Elfring" <Markus.Elfring@web.de>,
	linux-s390@vger.kernel.org,
	"Christian Bornträger" <borntraeger@de.ibm.com>,
	"Harald Freudenberger" <freude@linux.ibm.com>,
	"Heiko Carstens" <heiko.carstens@de.ibm.com>,
	"Ingo Franzki" <ifranzki@linux.ibm.com>,
	"Vasily Gorbik" <gor@linux.ibm.com>
Cc: LKML <linux-kernel@vger.kernel.org>, kernel-janitors@vger.kernel.org
Subject: Re: [PATCH] s390/pkey: Use memdup_user() rather than duplicating its implementation
Date: Wed, 06 Nov 2019 10:38:39 +0000	[thread overview]
Message-ID: <6137855bb4170c438c7436cbdb7dfd21639a8855.camel@perches.com> (raw)
In-Reply-To: <08422b7e-2071-ee52-049e-c3ac55bc67a9@web.de>

On Wed, 2019-11-06 at 11:22 +0100, Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Wed, 6 Nov 2019 11:11:42 +0100
> 
> Reuse existing functionality from memdup_user() instead of keeping
> duplicate source code.
> 
> Generated by: scripts/coccinelle/api/memdup_user.cocci
> 
> Delete local variables which became unnecessary with this refactoring
> in two function implementations.
> 
> Fixes: f2bbc96e7cfad3891b7bf9bd3e566b9b7ab4553d ("s390/pkey: add CCA AES cipher key support")

This doesn't fix anything and the Fixes: line is not appropriate.

> diff --git a/drivers/s390/crypto/pkey_api.c b/drivers/s390/crypto/pkey_api.c
[]
> @@ -715,36 +715,16 @@ static int pkey_apqns4keytype(enum pkey_key_type ktype,
> 
>  static void *_copy_key_from_user(void __user *ukey, size_t keylen)
>  {
> -	void *kkey;
> -
> -	if (!ukey || keylen < MINKEYBLOBSIZE || keylen > KEYBLOBBUFSIZE)
> -		return ERR_PTR(-EINVAL);
> -	kkey = kmalloc(keylen, GFP_KERNEL);
> -	if (!kkey)
> -		return ERR_PTR(-ENOMEM);
> -	if (copy_from_user(kkey, ukey, keylen)) {
> -		kfree(kkey);
> -		return ERR_PTR(-EFAULT);
> -	}
> -
> -	return kkey;
> +	return !ukey || keylen < MINKEYBLOBSIZE || keylen > KEYBLOBBUFSIZE
> +	       ? ERR_PTR(-EINVAL)
> +	       : memdup_user(ukey, keylen);

This is a very poor use of ternary ?: code.
This is much more readable for humans.

	if (!ukey || keylen < MINKEYBLOBSIZE || keylen > KBLOBBUFSIZE)
		return ERR_PTR(-EINVAL);

	return memdup_user(ukey, keylen);

The compiler will produce the same code.

>  static void *_copy_apqns_from_user(void __user *uapqns, size_t nr_apqns)
>  {
> -	void *kapqns = NULL;
> -	size_t nbytes;
> -
> -	if (uapqns && nr_apqns > 0) {
> -		nbytes = nr_apqns * sizeof(struct pkey_apqn);
> -		kapqns = kmalloc(nbytes, GFP_KERNEL);
> -		if (!kapqns)
> -			return ERR_PTR(-ENOMEM);
> -		if (copy_from_user(kapqns, uapqns, nbytes))
> -			return ERR_PTR(-EFAULT);
> -	}
> -
> -	return kapqns;
> +	return uapqns && nr_apqns > 0
> +	       ? memdup_user(uapqns, nr_apqns * sizeof(struct pkey_apqn))
> +	       : NULL;

And here you reverse the form of the earlier block.
Please be consistent and use this style:

	if (!uapqns || nr_apqns <= 0)
		return NULL;

	return memdup_user(uapqns, nr_apqns * sizeof(struct pkey_apqn));

  reply	other threads:[~2019-11-06 10:38 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-06 10:22 [PATCH] s390/pkey: Use memdup_user() rather than duplicating its implementation Markus Elfring
2019-11-06 10:22 ` Markus Elfring
2019-11-06 10:38 ` Joe Perches [this message]
2019-11-06 10:38   ` Joe Perches
2019-11-06 13:00   ` Markus Elfring
2019-11-06 13:00     ` Markus Elfring
2019-11-06 17:29     ` Joe Perches
2019-11-06 17:29       ` Joe Perches
2019-11-06 18:55       ` Markus Elfring
2019-11-06 18:55         ` Markus Elfring
2019-11-06 19:01         ` Joe Perches
2019-11-06 19:01           ` Joe Perches
2019-11-06 19:18           ` Markus Elfring
2019-11-06 19:18             ` Markus Elfring
2019-11-06 13:00   ` Markus Elfring
2019-11-06 13:00     ` Markus Elfring
2019-11-06 18:30     ` Christian Borntraeger
2019-11-06 18:30       ` Christian Borntraeger
2019-11-07  6:48       ` Dan Carpenter
2019-11-07  6:48         ` Dan Carpenter
2019-11-07  8:07         ` Christian Borntraeger
2019-11-07  8:07           ` Christian Borntraeger
2019-11-07 10:06   ` [PATCH v2] " Markus Elfring
2019-11-07 10:06     ` Markus Elfring
2019-11-07 12:44     ` Christian Borntraeger
2019-11-07 12:44       ` Christian Borntraeger
2019-11-07 13:45       ` Markus Elfring
2019-11-07 13:45         ` Markus Elfring
2019-11-07 13:54         ` Christian Borntraeger
2019-11-07 13:54           ` Christian Borntraeger
2019-11-07 14:27           ` Markus Elfring
2019-11-07 14:27             ` Markus Elfring
2019-11-08 11:32             ` Christian Borntraeger
2019-11-08 11:32               ` Christian Borntraeger
2019-11-08 17:14               ` [PATCH v3] " Markus Elfring
2019-11-08 17:14                 ` Markus Elfring
2019-11-11  7:54                 ` Christian Borntraeger
2019-11-11  7:54                   ` Christian Borntraeger
2019-11-11  8:11                   ` [v3] " Markus Elfring
2019-11-11  8:11                     ` Markus Elfring
2019-11-11  8:27                     ` Christian Borntraeger
2019-11-11  8:27                       ` Christian Borntraeger
2019-11-11  8:42                       ` Markus Elfring
2019-11-11  8:42                         ` Markus Elfring
2019-11-11  8:56                         ` Christian Borntraeger
2019-11-11  8:56                           ` Christian Borntraeger
2019-11-11  9:06                           ` Markus Elfring
2019-11-11  9:06                             ` Markus Elfring
2019-11-11  9:08                             ` Christian Borntraeger
2019-11-11  9:08                               ` Christian Borntraeger
2019-11-11  9:17                               ` Markus Elfring
2019-11-11  9:17                                 ` Markus Elfring
2019-11-11  9:18                                 ` Christian Borntraeger
2019-11-11  9:18                                   ` Christian Borntraeger
2019-11-11  9:26                                   ` Markus Elfring
2019-11-11  9:26                                     ` Markus Elfring
2019-11-11 14:45                   ` [PATCH v4] s390/pkey: Fix memory leak in error case by using memdup_user() rather than open coding Markus Elfring
2019-11-11 14:45                     ` Markus Elfring
2019-11-11 16:40                     ` Christian Borntraeger
2019-11-11 16:40                       ` [PATCH v4] s390/pkey: Fix memory leak in error case by using memdup_user() rather than open codi Christian Borntraeger
2019-11-13 17:09                       ` [v4] s390/pkey: Fix memory leak in error case by using memdup_user() rather than open coding Markus Elfring
2019-11-13 17:09                         ` Markus Elfring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6137855bb4170c438c7436cbdb7dfd21639a8855.camel@perches.com \
    --to=joe@perches.com \
    --cc=Markus.Elfring@web.de \
    --cc=borntraeger@de.ibm.com \
    --cc=freude@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=heiko.carstens@de.ibm.com \
    --cc=ifranzki@linux.ibm.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.