usb: avoid redundant allocation and free of memory
authorWeitao Hou <houweitaoo@gmail.com>
Thu, 23 May 2019 11:52:08 +0000 (19:52 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 24 May 2019 07:53:24 +0000 (09:53 +0200)
If usb is not attached, it's unnessary to allocate, copy
and free memory

Signed-off-by: Weitao Hou <houweitaoo@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/core/devio.c

index fa783531ee889140d580e19a489cede393f12a10..aa17dab6c4ea22aad6ae3ad85e1a2d42bd17b163 100644 (file)
@@ -2130,6 +2130,9 @@ static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
        if (ps->privileges_dropped)
                return -EACCES;
 
+       if (!connected(ps))
+               return -ENODEV;
+
        /* alloc buffer */
        size = _IOC_SIZE(ctl->ioctl_code);
        if (size > 0) {
@@ -2146,11 +2149,6 @@ static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
                }
        }
 
-       if (!connected(ps)) {
-               kfree(buf);
-               return -ENODEV;
-       }
-
        if (ps->dev->state != USB_STATE_CONFIGURED)
                retval = -EHOSTUNREACH;
        else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))