Use memdup_user() helper instead of open-coding to simplify the code.

Signed-off-by: Geliang Tang <geliangt...@gmail.com>
---
 drivers/char/agp/compat_ioctl.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/drivers/char/agp/compat_ioctl.c b/drivers/char/agp/compat_ioctl.c
index 2053f70..a0a3cf0 100644
--- a/drivers/char/agp/compat_ioctl.c
+++ b/drivers/char/agp/compat_ioctl.c
@@ -98,9 +98,10 @@ static int compat_agpioc_reserve_wrap(struct 
agp_file_private *priv, void __user
                if (ureserve.seg_count >= 16384)
                        return -EINVAL;
 
-               usegment = kmalloc(sizeof(*usegment) * ureserve.seg_count, 
GFP_KERNEL);
-               if (!usegment)
-                       return -ENOMEM;
+               usegment = memdup_user((void __user *)ureserve.seg_list,
+                                      sizeof(*usegment) * ureserve.seg_count);
+               if (IS_ERR(usegment))
+                       return PTR_ERR(usegment);
 
                ksegment = kmalloc(sizeof(*ksegment) * kreserve.seg_count, 
GFP_KERNEL);
                if (!ksegment) {
@@ -108,13 +109,6 @@ static int compat_agpioc_reserve_wrap(struct 
agp_file_private *priv, void __user
                        return -ENOMEM;
                }
 
-               if (copy_from_user(usegment, (void __user *) ureserve.seg_list,
-                                  sizeof(*usegment) * ureserve.seg_count)) {
-                       kfree(usegment);
-                       kfree(ksegment);
-                       return -EFAULT;
-               }
-
                for (seg = 0; seg < ureserve.seg_count; seg++) {
                        ksegment[seg].pg_start = usegment[seg].pg_start;
                        ksegment[seg].pg_count = usegment[seg].pg_count;
-- 
2.9.3

Reply via email to