diff mbox

IB/qib: Use memdup_user() rather than duplicating its implementation

Message ID 620b3961-6e05-6638-4ca8-18766acc91f4@users.sourceforge.net (mailing list archive)
State Accepted
Headers show

Commit Message

SF Markus Elfring Aug. 19, 2016, 7:06 a.m. UTC
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Fri, 19 Aug 2016 08:50:23 +0200

Reuse existing functionality from memdup_user() instead of keeping
duplicate source code.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/infiniband/hw/qib/qib_fs.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

Comments

Marciniszyn, Mike Aug. 19, 2016, 3:27 p.m. UTC | #1
> Subject: [PATCH] IB/qib: Use memdup_user() rather than duplicating its

> diff --git a/drivers/infiniband/hw/qib/qib_fs.c


I would be even more aggressive at reducing lines of code.

For example do direct returns when ok to do:
        if (pos != 0 || count != sizeof(struct qib_flash))
                return -EINVAL;

        tmp = memdup_user(buf, count);
        if (IS_ERR(tmp))
                return PTR_ERR(tmp);

The bail_tmp: label is then not needed.

Mike
Leon Romanovsky Aug. 19, 2016, 3:37 p.m. UTC | #2
On Fri, Aug 19, 2016 at 09:06:20AM +0200, SF Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Fri, 19 Aug 2016 08:50:23 +0200
> 
> Reuse existing functionality from memdup_user() instead of keeping
> duplicate source code.
> 
> This issue was detected by using the Coccinelle software.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>

Reviewed-by: Leon Romanovsky <leonro@mellanox.com>
Leon Romanovsky Aug. 19, 2016, 3:41 p.m. UTC | #3
On Fri, Aug 19, 2016 at 03:27:20PM +0000, Marciniszyn, Mike wrote:
> > Subject: [PATCH] IB/qib: Use memdup_user() rather than duplicating its
> > diff --git a/drivers/infiniband/hw/qib/qib_fs.c
> 
> I would be even more aggressive at reducing lines of code.
> 
> For example do direct returns when ok to do:
>         if (pos != 0 || count != sizeof(struct qib_flash))
>                 return -EINVAL;
> 
>         tmp = memdup_user(buf, count);
>         if (IS_ERR(tmp))
>                 return PTR_ERR(tmp);
> 
> The bail_tmp: label is then not needed.

You still need to free tmp allocation if qib_eeprom_write failed and
this is your bail_tmp.

341         tmp = kmalloc(count, GFP_KERNEL);
342         if (!tmp) {
343                 ret = -ENOMEM;
344                 goto bail;
345         }
346
347         if (copy_from_user(tmp, buf, count)) {
348                 ret = -EFAULT;
349                 goto bail_tmp;
350         }
351
352         dd = private2dd(file);
353         if (qib_eeprom_write(dd, pos, tmp, count)) {
354                 ret = -ENXIO;
355                 qib_dev_err(dd, "failed to write to flash\n");
356                 goto bail_tmp;
357         }

> 
> Mike
>
Marciniszyn, Mike Aug. 19, 2016, 3:42 p.m. UTC | #4
> >
> > The bail_tmp: label is then not needed.
> 
> You still need to free tmp allocation if qib_eeprom_write failed and this is
> your bail_tmp.
> 

Typo.   The bail: label is not needed.

Mike
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Leon Romanovsky Aug. 19, 2016, 3:45 p.m. UTC | #5
On Fri, Aug 19, 2016 at 03:42:29PM +0000, Marciniszyn, Mike wrote:
> > >
> > > The bail_tmp: label is then not needed.
> > 
> > You still need to free tmp allocation if qib_eeprom_write failed and this is
> > your bail_tmp.
> > 
> 
> Typo.   The bail: label is not needed.

Yeah, it makes sense.

Thanks

> 
> Mike
Doug Ledford Aug. 23, 2016, 4:43 p.m. UTC | #6
On 8/19/2016 11:27 AM, Marciniszyn, Mike wrote:
>> Subject: [PATCH] IB/qib: Use memdup_user() rather than duplicating its
>> diff --git a/drivers/infiniband/hw/qib/qib_fs.c
> 
> I would be even more aggressive at reducing lines of code.
> 
> For example do direct returns when ok to do:
>         if (pos != 0 || count != sizeof(struct qib_flash))
>                 return -EINVAL;
> 
>         tmp = memdup_user(buf, count);
>         if (IS_ERR(tmp))
>                 return PTR_ERR(tmp);
> 
> The bail_tmp: label is then not needed.
> 
> Mike
> 

With Mike's additional cleanups in place, patch applied.
diff mbox

Patch

diff --git a/drivers/infiniband/hw/qib/qib_fs.c b/drivers/infiniband/hw/qib/qib_fs.c
index fcdf3791..910f0d9 100644
--- a/drivers/infiniband/hw/qib/qib_fs.c
+++ b/drivers/infiniband/hw/qib/qib_fs.c
@@ -338,17 +338,12 @@  static ssize_t flash_write(struct file *file, const char __user *buf,
 		goto bail;
 	}
 
-	tmp = kmalloc(count, GFP_KERNEL);
-	if (!tmp) {
-		ret = -ENOMEM;
+	tmp = memdup_user(buf, count);
+	if (IS_ERR(tmp)) {
+		ret = PTR_ERR(tmp);
 		goto bail;
 	}
 
-	if (copy_from_user(tmp, buf, count)) {
-		ret = -EFAULT;
-		goto bail_tmp;
-	}
-
 	dd = private2dd(file);
 	if (qib_eeprom_write(dd, pos, tmp, count)) {
 		ret = -ENXIO;