From: Tom Todd <thomas.m.a.todd@gmail.com>
To: gregkh@linuxfoundation.org
Cc: joe@perches.com, stern@rowland.harvard.edu,
dan.carpenter@oracle.com, arvind.yadav.cs@gmail.com,
thomas.m.a.todd@gmail.com, linux-usb@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [v2] USB: core: devio: Restructured proc_ioctl for readability
Date: Sun, 12 Aug 2018 23:26:54 +0100 [thread overview]
Message-ID: <20180812222653.3341-1-thomas.m.a.todd@gmail.com> (raw)
On, Sun, 12 Aug 2018 11:34:59 -0700 Joe Perches wrote:
>On Sun, 2018-08-12 at 19:06 +0100, Tom Todd wrote:
>> Fixed a code styling issue
>while it's OK to fix style only issues, it's much better
>to reorganize the code for reader clarity.
Ok, thank you, I've taken your suggestions and created the a new version
Restructured method proc_ioctl for readability and fixed code style
errors.
Signed-off-by: Tom Todd <thomas.m.a.todd@gmail.com>
---
drivers/usb/core/devio.c | 67 ++++++++++++++++++++++------------------
1 file changed, 37 insertions(+), 30 deletions(-)
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 476dcc5f2da3..740e60e086e2 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -2117,46 +2117,52 @@ static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
/* alloc buffer */
size = _IOC_SIZE(ctl->ioctl_code);
if (size > 0) {
- buf = kmalloc(size, GFP_KERNEL);
- if (buf == NULL)
- return -ENOMEM;
- if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
- if (copy_from_user(buf, ctl->data, size)) {
- kfree(buf);
+ if (_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE) {
+ buf = memdup_user(ctl->data, size);
+ if (!buf)
return -EFAULT;
- }
} else {
- memset(buf, 0, size);
+ buf = kmalloc(size, GFP_KERNEL);
+ if (!buf)
+ return -ENOMEM;
}
}
if (!connected(ps)) {
- kfree(buf);
- return -ENODEV;
+ retval = -ENODEV;
+ goto exit;
}
- if (ps->dev->state != USB_STATE_CONFIGURED)
+ if (ps->dev->state != USB_STATE_CONFIGURED) {
retval = -EHOSTUNREACH;
- else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
+ goto exit;
+ }
+
+ intf = usb_ifnum_to_if(ps->dev, ctl->ifno);
+ if (!intf) {
retval = -EINVAL;
- else switch (ctl->ioctl_code) {
+ goto exit;
+ }
+ switch (ctl->ioctl_code) {
/* disconnect kernel driver from interface */
case USBDEVFS_DISCONNECT:
- if (intf->dev.driver) {
- driver = to_usb_driver(intf->dev.driver);
- dev_dbg(&intf->dev, "disconnect by usbfs\n");
- usb_driver_release_interface(driver, intf);
- } else
+ if (!intf->dev.driver) {
retval = -ENODATA;
+ goto exit;
+ }
+ driver = to_usb_driver(intf->dev.driver);
+ dev_dbg(&intf->dev, "disconnect by usbfs\n");
+ usb_driver_release_interface(driver, intf);
break;
/* let kernel drivers try to (re)bind to the interface */
case USBDEVFS_CONNECT:
- if (!intf->dev.driver)
- retval = device_attach(&intf->dev);
- else
+ if (!intf->dev.driver) {
retval = -EBUSY;
+ goto exit;
+ }
+ retval = device_attach(&intf->dev);
break;
/* talk directly to the interface's driver */
@@ -2165,20 +2171,21 @@ static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
driver = to_usb_driver(intf->dev.driver);
if (driver == NULL || driver->unlocked_ioctl == NULL) {
retval = -ENOTTY;
- } else {
- retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
- if (retval == -ENOIOCTLCMD)
- retval = -ENOTTY;
+ goto exit;
}
+ retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
+ if (retval == -ENOIOCTLCMD) {
+ retval = -ENOTTY;
+ goto exit;
+ }
+ break;
}
/* cleanup and return */
- if (retval >= 0
- && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
- && size > 0
- && copy_to_user(ctl->data, buf, size) != 0)
+ if (retval >= 0 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0 &&
+ size > 0 && copy_to_user(ctl->data, buf, size) != 0)
retval = -EFAULT;
-
+exit:
kfree(buf);
return retval;
}
next reply other threads:[~2018-08-12 22:26 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-12 22:26 Tom Todd [this message]
-- strict thread matches above, loose matches on Subject: below --
2018-08-12 22:46 [v2] USB: core: devio: Restructured proc_ioctl for readability Alan Stern
2018-08-13 6:34 Greg Kroah-Hartman
2018-08-13 18:22 Tom Todd
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=20180812222653.3341-1-thomas.m.a.todd@gmail.com \
--to=thomas.m.a.todd@gmail.com \
--cc=arvind.yadav.cs@gmail.com \
--cc=dan.carpenter@oracle.com \
--cc=gregkh@linuxfoundation.org \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=stern@rowland.harvard.edu \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).