gpio: mockup: updates for 4.13
Subsystems (2)
v2 cover gpio: mockup: updates for 4.13
This series contains a couple bug fixes and other minor updates for the GPIO testing module.
v1 -> v2:
- omit applied patches
- use kstrtoint_from_user() for debugfs input sanitization
- add a patch improving the code in gpio_mockup_event_write()
- bail out from gpio_mockup_event_write() on invalid input even when nobody is listening for events
Bartosz Golaszewski (7):
gpio: mockup: improve the debugfs input sanitization
gpio: mockup: tweak gpio_mockup_event_write()
gpio: mockup: refuse to accept an odd number of GPIO ranges
gpio: mockup: improve readability
gpio: mockup: don't return magic numbers from probe()
gpio: mockup: improve the error message
gpio: mockup: add myself as author
drivers/gpio/gpio-mockup.c | 61 ++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 29 deletions(-)
Signature · 1 line
2.9.3
PATCH v2 1/7 gpio: mockup: improve the debugfs input sanitization
We're currently only checking the first character of the input to the debugfs event files, so a string like '0sdfdsf' is valid and indicates a falling edge event.
Be more strict and only allow '0', '1', '0\n' & '1\n'.
While we're at it: move the sanitization code before the irq_enabled check so that we indicate an error on invalid input even if nobody is waiting for events.
--- drivers/gpio/gpio-mockup.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-)
drivers/gpio/gpio-mockup.c +7 -12
@@ -208,8 +208,7 @@ static ssize_t gpio_mockup_event_write(struct file *file,struct seq_file *sfile;struct gpio_desc *desc;struct gpio_chip *gc; -int val; -char buf; +int rv, val; sfile = file->private_data; priv = sfile->private;
@@ -217,19 +216,15 @@ static ssize_t gpio_mockup_event_write(struct file *file, chip = priv->chip; gc = &chip->gc; + rv =kstrtoint_from_user (usr_buf, size,0 , &val); +if (rv) +return rv; +if (val !=0 && val !=1 ) +return -EINVAL ; +if (!chip->lines[priv->offset].irq_enabled)return size; -if (copy_from_user (&buf, usr_buf,1 )) -return -EFAULT ; - -if (buf =='0' ) - val =0 ; -else if (buf =='1' ) - val =1 ; -else -return -EINVAL ; -gpiod_set_value_cansleep (desc, val); priv->chip->irq_ctx.irq = gc->irq_base + priv->offset;irq_work_queue (&priv->chip->irq_ctx.work);
Signature · 1 line
2.9.3
Re: [PATCH v2 1/7] gpio: mockup: improve the debugfs input sanitization
Quoted 10 lines · Bartosz Golaszewski
We're currently only checking the first character of the input to the debugfs event files, so a string like '0sdfdsf' is valid and indicates a falling edge event.
Be more strict and only allow '0', '1', '0\n' & '1\n'.
While we're at it: move the sanitization code before the
irq_enabledcheck so that we indicate an error on invalid input even if nobody is waiting for events.
drivers/gpio/gpio-mockup.c static ssize_t gpio_mockup_event_write(struct file *file,
Quoted 9 lines
drivers/gpio/gpio-mockup.c +5
-2-int val; -char buf; +int rv, val; + rv =kstrtoint_from_user (usr_buf, size,0 , &val); +if (rv) +return rv; +if (val !=0 && val !=1 )
Wouldn't be easier to have
u8 rv;
ret = kstrtu8_from_user();
if (ret >= 2)
return ...;
?
Quoted 1 line
+ return
-EINVAL;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/7] gpio: mockup: improve the debugfs input sanitization
2017-05-30 20:52 GMT+02:00 Andy Shevchenko <[email protected]>:
Quoted 23 lines · Bartosz Golaszewski
Quoted 10 lines
We're currently only checking the first character of the input to the debugfs event files, so a string like '0sdfdsf' is valid and indicates a falling edge event.
Be more strict and only allow '0', '1', '0\n' & '1\n'.
While we're at it: move the sanitization code before the
irq_enabledcheck so that we indicate an error on invalid input even if nobody is waiting for events.drivers/gpio/gpio-mockup.c static ssize_t gpio_mockup_event_write(struct file *file,
Quoted 9 lines
drivers/gpio/gpio-mockup.c +5
-2-int val; -char buf; +int rv, val; + rv =kstrtoint_from_user (usr_buf, size,0 , &val); +if (rv) +return rv; +if (val !=0 && val !=1 )Wouldn't be easier to have
Quoted 7 lines
u8 rv;
ret =
kstrtu8_from_user();
if (ret >= 2)
return ...;?
kstrtu8_from_user() doesn't return the converted value, so you won't skip an if anyway and by using the int variant, we're avoiding a cast. I'd prefer it this way frankly.
Thanks,
Bartosz
Re: [PATCH v2 1/7] gpio: mockup: improve the debugfs input sanitization
Quoted 9 lines · Bartosz Golaszewski
2017-05-30 20:52 GMT+02:00 Andy Shevchenko <[email protected]>:
Quoted 8 lines · Bartosz Golaszewski
Quoted 2 lines
+ if (val != 0 && val != 1)
Wouldn't be easier to have
u8 rv;
Quoted 8 lines
Quoted 5 lines
ret =
kstrtu8_from_user();
if (ret >= 2)
return ...;?
kstrtu8_from_user()doesn't return the converted value, so you won't skip an if anyway
Oh, yes.
Quoted 2 lines
and by using the int variant, we're avoiding a cast. I'd prefer it this way frankly.
Fair enough. (Though I would go with (val < 0 && val > 1) condition, of course it's matter of taste)
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 2/7 gpio: mockup: tweak gpio_mockup_event_write()
In reply to Bartosz GolaszewskiInvert the logic of the irq_enabled check and only access the private data after the input is sanitized.
--- drivers/gpio/gpio-mockup.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
drivers/gpio/gpio-mockup.c +10 -11
@@ -210,24 +210,23 @@ static ssize_t gpio_mockup_event_write(struct file *file,struct gpio_chip *gc;int rv, val; - sfile = file->private_data; - priv = sfile->private; - desc = priv->desc; - chip = priv->chip; - gc = &chip->gc; - rv =kstrtoint_from_user (usr_buf, size,0 , &val);if (rv)return rv;if (val !=0 && val !=1 )return -EINVAL ; -if (!chip->lines[priv->offset].irq_enabled) -return size; + sfile = file->private_data; + priv = sfile->private; + desc = priv->desc; + chip = priv->chip; + gc = &chip->gc; -gpiod_set_value_cansleep (desc, val); - priv->chip->irq_ctx.irq = gc->irq_base + priv->offset; -irq_work_queue (&priv->chip->irq_ctx.work); +if (chip->lines[priv->offset].irq_enabled) { +gpiod_set_value_cansleep (desc, val); + priv->chip->irq_ctx.irq = gc->irq_base + priv->offset; +irq_work_queue (&priv->chip->irq_ctx.work); + }return size; }
Signature · 1 line
2.9.3
PATCH v2 3/7 gpio: mockup: refuse to accept an odd number of GPIO ranges
In reply to Bartosz GolaszewskiCurrently we ignore the last odd range value, since each chip is described by two values. Be more strict and require the user to pass an even number of ranges.
--- drivers/gpio/gpio-mockup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-mockup.c +1 -1
@@ -334,7 +334,7 @@ static int gpio_mockup_probe(struct platform_device *pdev)int ret, i, base, ngpio;char *chip_name; -if (gpio_mockup_params_nr <2 ) +if (gpio_mockup_params_nr <2 || (gpio_mockup_params_nr %2 ))return -EINVAL ; chips = devm_kzalloc(dev,
Signature · 1 line
2.9.3
PATCH v2 4/7 gpio: mockup: improve readability
In reply to Bartosz GolaszewskiWe currently shift bits here and there without actually explaining what we're doing. Add some helper variables with names indicating their purpose to improve the code readability.
--- drivers/gpio/gpio-mockup.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
drivers/gpio/gpio-mockup.c +13 -7
@@ -27,6 +27,11 @@#define GPIO_MOCKUP_NAME "gpio-mockup"#define GPIO_MOCKUP_MAX_GC 10 +/* +* We're storing two values per chip: the GPIO base and the number +* of GPIO lines. +*/ +#define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)enum {GPIO_MOCKUP_DIR_OUT =0 ,
@@ -62,7 +67,7 @@ struct gpio_mockup_dbgfs_private {int offset; }; -static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC <<1 ]; +static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES ];static int gpio_mockup_params_nr;module_param_array (gpio_mockup_ranges, int, &gpio_mockup_params_nr,0400 );
@@ -329,23 +334,24 @@ static int gpio_mockup_add(struct device *dev,static int gpio_mockup_probe (struct platform_device *pdev) { -struct gpio_mockup_chip *chips; +int ret, i, base, ngpio, num_chips;struct device *dev = &pdev->dev; -int ret, i, base, ngpio; +struct gpio_mockup_chip *chips;char *chip_name;if (gpio_mockup_params_nr <2 || (gpio_mockup_params_nr %2 ))return -EINVAL ; - chips =devm_kzalloc (dev, -sizeof (*chips) * (gpio_mockup_params_nr >>1 ), -GFP_KERNEL ); +/* Each chip is described by two values. */ + num_chips = gpio_mockup_params_nr /2 ; + + chips =devm_kzalloc (dev,sizeof (*chips) * num_chips,GFP_KERNEL );if (!chips)return -ENOMEM ;platform_set_drvdata (pdev, chips); -for (i =0 ; i < gpio_mockup_params_nr >>1 ; i++) { +for (i =0 ; i < num_chips; i++) { base = gpio_mockup_ranges[i *2 ];if (base ==-1 )
Signature · 1 line
2.9.3
Re: [PATCH v2 4/7] gpio: mockup: improve readability
drivers/gpio/gpio-mockup.c static int gpio_mockup_add(struct device *dev, · line 348
Quoted 8 lines · Bartosz Golaszewski
We currently shift bits here and there without actually explaining what we're doing. Add some helper variables with names indicating their purpose to improve the code readability.
drivers/gpio/gpio-mockup.c +4
-0+/* Each chip is described by two values. */ + num_chips = gpio_mockup_params_nr /2 ; + + chips =devm_kzalloc (dev,sizeof (*chips) * num_chips,GFP_KERNEL );
It's effectivelydevm_kcalloc()
ordevm_kmalloc_array()
depending on the requirement of zeroing a memory chunks.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 4/7] gpio: mockup: improve readability
2017-05-30 20:55 GMT+02:00 Andy Shevchenko <[email protected]>:
Quoted 10 lines · Bartosz Golaszewski
drivers/gpio/gpio-mockup.c static int gpio_mockup_add(struct device *dev, · line 348
Quoted 8 lines
We currently shift bits here and there without actually explaining what we're doing. Add some helper variables with names indicating their purpose to improve the code readability.
drivers/gpio/gpio-mockup.c +4
-0+/* Each chip is described by two values. */ + num_chips = gpio_mockup_params_nr /2 ; + + chips =devm_kzalloc (dev,sizeof (*chips) * num_chips,GFP_KERNEL );
Quoted 6 lines
It's effectively
devm_kcalloc()
ordevm_kmalloc_array()
depending on the requirement of zeroing a memory chunks.
Is there any advantage to using one of these here?
Thanks,
Bartosz
Re: [PATCH v2 4/7] gpio: mockup: improve readability
Quoted 11 lines · Bartosz Golaszewski
2017-05-30 20:55 GMT+02:00 Andy Shevchenko <[email protected]>:
Quoted 10 lines · Bartosz Golaszewski
Quoted 8 lines
We currently shift bits here and there without actually explaining what we're doing. Add some helper variables with names indicating their purpose to improve the code readability.
drivers/gpio/gpio-mockup.c +4
-0+/* Each chip is described by two values. */ + num_chips = gpio_mockup_params_nr /2 ; + + chips =devm_kzalloc (dev,sizeof (*chips) * num_chips,GFP_KERNEL );
Quoted 7 lines
Quoted 5 lines
It's effectively
devm_kcalloc()
ordevm_kmalloc_array()
depending on the requirement of zeroing a memory chunks.Is there any advantage to using one of these here?
Yes, though subtle one in this case. The caller will not care about (possible) overflow in multiplication.
Other (micro)optimizations might be in place in the future as well, but I dunno about this.
I would suggest to change.
P.S. And of course if you don't need zeroed area it would be (slight?) performance impact I suppose.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 4/7] gpio: mockup: improve readability
2017-05-31 16:57 GMT+02:00 Andy Shevchenko <[email protected]>:
Quoted 18 lines · Bartosz Golaszewski
Quoted 16 lines
2017-05-30 20:55 GMT+02:00 Andy Shevchenko <[email protected]>:
Quoted 15 lines · Bartosz Golaszewski
drivers/gpio/gpio-mockup.c static int gpio_mockup_add(struct device *dev, · line 348
Quoted 8 lines
We currently shift bits here and there without actually explaining what we're doing. Add some helper variables with names indicating their purpose to improve the code readability.
drivers/gpio/gpio-mockup.c +4
-0+/* Each chip is described by two values. */ + num_chips = gpio_mockup_params_nr /2 ; + + chips =devm_kzalloc (dev,sizeof (*chips) * num_chips,GFP_KERNEL );It's effectively
devm_kcalloc()
ordevm_kmalloc_array()
depending on the requirement of zeroing a memory chunks.
Quoted 9 lines
Quoted 1 line
Is there any advantage to using one of these here?
Yes, though subtle one in this case. The caller will not care about (possible) overflow in multiplication.
Other (micro)optimizations might be in place in the future as well, but I dunno about this.I would suggest to change.
Ok, I'll change it both for the lines and for the chips arrays and add a separate patch.
Thanks,
Bartosz
PATCH v2 5/7 gpio: mockup: don't return magic numbers from probe()
In reply to Bartosz GolaszewskiWhen the requested number of GPIO lines is 0, return -EINVAL, not -1 which is -EPERM.
--- drivers/gpio/gpio-mockup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-mockup.c +1 -1
@@ -369,7 +369,7 @@ static int gpio_mockup_probe(struct platform_device *pdev) ret =gpio_mockup_add (dev, &chips[i], chip_name, base, ngpio); }else { - ret =-1 ; + ret = -EINVAL ; }if (ret) {
Signature · 1 line
2.9.3
PATCH v2 6/7 gpio: mockup: improve the error message
In reply to Bartosz GolaszewskiIndicate the error number and make the message a bit more elaborate.
--- drivers/gpio/gpio-mockup.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpio/gpio-mockup.c +3 -2
@@ -373,8 +373,9 @@ static int gpio_mockup_probe(struct platform_device *pdev) }if (ret) { -dev_err (dev,"gpio<%d..%d> add failed\n" , - base, base <0 ? ngpio : base + ngpio); +dev_err (dev, +"adding gpiochip failed: %d (base: %d, ngpio: %d)\n" , + ret, base, base <0 ? ngpio : base + ngpio);return ret; }
Signature · 1 line
2.9.3
Re: [PATCH v2 6/7] gpio: mockup: improve the error message
drivers/gpio/gpio-mockup.c static int gpio_mockup_probe(struct platform_device *pdev) · line 378
Quoted 5 lines · Bartosz Golaszewski
Indicate the error number and make the message a bit more elaborate.
drivers/gpio/gpio-mockup.c +3
-0+dev_err (dev, +"adding gpiochip failed: %d (base: %d, ngpio: %d)\n" , + ret, base, base <0 ? ngpio : base + ngpio);
You may consider to use
'gpio_mockup_add' instead of 'adding gpiochip'. The latter points the reader first to gpiochip_add family of functions while you run a wrapper on top of it.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 6/7] gpio: mockup: improve the error message
2017-05-30 20:59 GMT+02:00 Andy Shevchenko <[email protected]>:
Quoted 12 lines · Bartosz Golaszewski
drivers/gpio/gpio-mockup.c static int gpio_mockup_probe(struct platform_device *pdev) · line 378
Quoted 5 lines
Indicate the error number and make the message a bit more elaborate.
drivers/gpio/gpio-mockup.c +3
-0+dev_err (dev, +"adding gpiochip failed: %d (base: %d, ngpio: %d)\n" , + ret, base, base <0 ? ngpio : base + ngpio);You may consider to use
'gpio_mockup_add' instead of 'adding gpiochip'. The latter points the reader first togpiochip_addfamily of functions while you run a wrapper on top of it.
But this message can also be emitted if the module params are invalid, in which case we don't even enter gpio_mockup_add().
Thanks,
Bartosz
Re: [PATCH v2 6/7] gpio: mockup: improve the error message
Quoted 8 lines · Bartosz Golaszewski
2017-05-30 20:59 GMT+02:00 Andy Shevchenko <[email protected]>:
Quoted 7 lines · Bartosz Golaszewski
Quoted 5 lines
Indicate the error number and make the message a bit more elaborate.
drivers/gpio/gpio-mockup.c +3
-0+dev_err (dev, +"adding gpiochip failed: %d (base: %d, ngpio: %d)\n" , + ret, base, base <0 ? ngpio : base + ngpio);
Quoted 8 lines
Quoted 5 lines
You may consider to use
'gpio_mockup_add' instead of 'adding gpiochip'. The latter points the reader first togpiochip_addfamily of functions while you run a wrapper on top of it.But this message can also be emitted if the module params are invalid, in which case we don't even enter
gpio_mockup_add().
...which unveils bad phrasing in the message. In that case "adding gpiochip" is also misleading.
I dunno if it requires separate patch to fix the phrasing, though it would be nice to make it more clear for both cases, or even split to two cases.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 6/7] gpio: mockup: improve the error message
2017-05-31 17:00 GMT+02:00 Andy Shevchenko <[email protected]>:
Quoted 15 lines · Bartosz Golaszewski
Quoted 14 lines
2017-05-30 20:59 GMT+02:00 Andy Shevchenko <[email protected]>:
Quoted 12 lines · Bartosz Golaszewski
drivers/gpio/gpio-mockup.c static int gpio_mockup_probe(struct platform_device *pdev) · line 378
Quoted 5 lines
Indicate the error number and make the message a bit more elaborate.
drivers/gpio/gpio-mockup.c +3
-0+dev_err (dev, +"adding gpiochip failed: %d (base: %d, ngpio: %d)\n" , + ret, base, base <0 ? ngpio : base + ngpio);You may consider to use
'gpio_mockup_add' instead of 'adding gpiochip'. The latter points the reader first togpiochip_addfamily of functions while you run a wrapper on top of it.
Quoted 6 lines
Quoted 2 lines
But this message can also be emitted if the module params are invalid, in which case we don't even enter
gpio_mockup_add()....which unveils bad phrasing in the message. In that case "adding gpiochip" is also misleading.
Not really. You can pass an invalid value later in the list which will only become apparent when it's reached. In that case previous gpiochips will be added correctly but probe will fail with -EINVAL after reaching the bad one in which case the message is right. I hope I'm being clear.
Thanks,
Bartosz
Re: [PATCH v2 6/7] gpio: mockup: improve the error message
2017-05-31 17:26 GMT+02:00 Bartosz Golaszewski <[email protected]>:
Quoted 23 lines
2017-05-31 17:00 GMT+02:00 Andy Shevchenko <[email protected]>:
Quoted 15 lines · Bartosz Golaszewski
Quoted 14 lines
2017-05-30 20:59 GMT+02:00 Andy Shevchenko <[email protected]>:
Quoted 12 lines · Bartosz Golaszewski
drivers/gpio/gpio-mockup.c static int gpio_mockup_probe(struct platform_device *pdev) · line 378
Quoted 5 lines
Indicate the error number and make the message a bit more elaborate.
drivers/gpio/gpio-mockup.c +3
-0+dev_err (dev, +"adding gpiochip failed: %d (base: %d, ngpio: %d)\n" , + ret, base, base <0 ? ngpio : base + ngpio);You may consider to use
'gpio_mockup_add' instead of 'adding gpiochip'. The latter points the reader first togpiochip_addfamily of functions while you run a wrapper on top of it.Quoted 6 lines
Quoted 2 lines
But this message can also be emitted if the module params are invalid, in which case we don't even enter
gpio_mockup_add()....which unveils bad phrasing in the message. In that case "adding gpiochip" is also misleading.
Quoted 6 lines
Not really. You can pass an invalid value later in the list which will only become apparent when it's reached. In that case previous gpiochips will be added correctly but probe will fail with
-EINVALafter reaching the bad one in which case the message is right. I hope I'm being clear.
Which made me think: maybe the next step would be to parse the arguments in the module init function and probe each dummy gpiochip separately...
Best regards,
Bartosz Golaszewski
PATCH v2 7/7 gpio: mockup: add myself as author
In reply to Bartosz GolaszewskiJust taking credit for the recent changes and new features. :)
--- drivers/gpio/gpio-mockup.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/gpio/gpio-mockup.c +2 -0
@@ -3,6 +3,7 @@* * Copyright (C) 2014 Kamlakant Patel <[email protected]> * Copyright (C) 2015-2016 Bamvor Jian Zhang <[email protected]> +* Copyright (C) 2017 Bartosz Golaszewski <[email protected]> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the
@@ -432,5 +433,6 @@ module_exit(mock_device_exit);MODULE_AUTHOR ("Kamlakant Patel <[email protected]>" );MODULE_AUTHOR ("Bamvor Jian Zhang <[email protected]>" ); +MODULE_AUTHOR ("Bartosz Golaszewski <[email protected]>" );MODULE_DESCRIPTION ("GPIO Testing driver" );MODULE_LICENSE ("GPL v2" );
Signature · 1 line
2.9.3