Skip to contentSkip to conversation

vfio: simplify kmalloc+copy_from_user to memdup_user

PATCH In review Fengguang Wu 2 participants 2 messages Nov 2, 2012 – Nov 9, 2012 VFIO driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio: simplify kmalloc+copy_from_user to memdup_user

Generated by: coccinelle/api/memdup_user.cocci

---

 drivers/vfio/pci/vfio_pci.c |   13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/vfio/pci/vfio_pci.c +4 -9
@@ -327,15 +327,10 @@ static long vfio_pci_ioctl(void *device_
 			    hdr.count > vfio_pci_get_irq_count(vdev, hdr.index))
 				return -EINVAL;
 
-			data = kmalloc(hdr.count * size, GFP_KERNEL);
-			if (!data)
-				return -ENOMEM;
-
-			if (copy_from_user(data, (void __user *)(arg + minsz),
-					   hdr.count * size)) {
-				kfree(data);
-				return -EFAULT;
-			}
+			data = memdup_user((void __user *)(arg + minsz),
+					   hdr.count * size);
+			if (IS_ERR(data))
+				return PTR_ERR(data);
 		}
 
 		mutex_lock(&vdev->igate);
#1 lore raw
Quoted 21 lines · Fengguang Wu

Generated by: coccinelle/api/memdup_user.cocci

---

 drivers/vfio/pci/vfio_pci.c |   13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/vfio/pci/vfio_pci.c +0 -6
@@ -327,15 +327,10 @@ static long vfio_pci_ioctl(void *device_
 			    hdr.count > vfio_pci_get_irq_count(vdev, hdr.index))
 				return -EINVAL;
 
-			data = kmalloc(hdr.count * size, GFP_KERNEL);
-			if (!data)
-				return -ENOMEM;
-
-			if (copy_from_user(data, (void __user *)(arg + minsz),
-					   hdr.count * size)) {

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(void *device_ · line 336

Quoted 10 lines
drivers/vfio/pci/vfio_pci.c +4 -3
-				kfree(data);
-				return -EFAULT;
-			}
+			data = memdup_user((void __user *)(arg + minsz),
+					   hdr.count * size);
+			if (IS_ERR(data))
+				return PTR_ERR(data);
 		}
 
 		mutex_lock(&vdev->igate);

Thanks for the patch. Looks reasonable to me. I'll add it to my next tree for 3.8. Thanks,

Alex