Commit 418ee8e9 authored by Bartosz Golaszewski's avatar Bartosz Golaszewski Committed by Linus Walleij

gpiolib: only check line handle flags once

There's no need to check the validity of handle request flags more
than once, right after copying the data from user. Move the check
out of the for loop and simplify the error path by bailing out before
allocating any resources.
Signed-off-by: default avatarBartosz Golaszewski <brgl@bgdev.pl>
Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
parent eec1d566
...@@ -444,12 +444,19 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) ...@@ -444,12 +444,19 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
struct linehandle_state *lh; struct linehandle_state *lh;
struct file *file; struct file *file;
int fd, i, ret; int fd, i, ret;
u32 lflags;
if (copy_from_user(&handlereq, ip, sizeof(handlereq))) if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
return -EFAULT; return -EFAULT;
if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
return -EINVAL; return -EINVAL;
lflags = handlereq.flags;
/* Return an error if an unknown flag is set */
if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
return -EINVAL;
lh = kzalloc(sizeof(*lh), GFP_KERNEL); lh = kzalloc(sizeof(*lh), GFP_KERNEL);
if (!lh) if (!lh)
return -ENOMEM; return -ENOMEM;
...@@ -470,7 +477,6 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) ...@@ -470,7 +477,6 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
/* Request each GPIO */ /* Request each GPIO */
for (i = 0; i < handlereq.lines; i++) { for (i = 0; i < handlereq.lines; i++) {
u32 offset = handlereq.lineoffsets[i]; u32 offset = handlereq.lineoffsets[i];
u32 lflags = handlereq.flags;
struct gpio_desc *desc; struct gpio_desc *desc;
if (offset >= gdev->ngpio) { if (offset >= gdev->ngpio) {
...@@ -478,12 +484,6 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) ...@@ -478,12 +484,6 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
goto out_free_descs; goto out_free_descs;
} }
/* Return an error if a unknown flag is set */
if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) {
ret = -EINVAL;
goto out_free_descs;
}
desc = &gdev->descs[offset]; desc = &gdev->descs[offset];
ret = gpiod_request(desc, lh->label); ret = gpiod_request(desc, lh->label);
if (ret) if (ret)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment