Skip to contentSkip to conversation

gpio: mockup: updates for 4.13

PATCH In review Bartosz Golaszewski 3 participants 18 messages Jun 9, 2017 – Jun 20, 2017

Subsystems (2)
lore
Share & tools
lore
#1 lore raw

PATCH v3 1/8 gpio: mockup: improve the debugfs input sanitization

In reply to 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_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
index ba8d62a..da76267 100644
@@ -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
#3 lore raw

PATCH v3 2/8 gpio: mockup: tweak gpio_mockup_event_write()

In reply to Bartosz Golaszewski

Invert 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
index da76267..d78e8e0 100644
@@ -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
#5 lore raw

PATCH v3 3/8 gpio: mockup: refuse to accept an odd number of GPIO ranges

In reply to Bartosz Golaszewski

Currently 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
index d78e8e0..d95d37a 100644
@@ -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
#7 lore raw

PATCH v3 4/8 gpio: mockup: improve readability

In reply to 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 | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
drivers/gpio/gpio-mockup.c +13 -7
index d95d37a..0cb6cba 100644
@@ -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
#9 lore raw

PATCH v3 5/8 gpio: mockup: don't return magic numbers from probe()

In reply to Bartosz Golaszewski

When 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
index 0cb6cba..ab2d38e 100644
@@ -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
#13 lore raw

PATCH v3 7/8 gpio: mockup: add myself as author

In reply to Bartosz Golaszewski

Just 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
index 2f4fe41..536a229 100644
@@ -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
#15 lore raw

PATCH v3 8/8 gpio: mockup: use devm_kcalloc() where applicable

In reply to Bartosz Golaszewski

When allocating a zeroed array of objects use devm_kcalloc() instead of manually calculating the required size and using devm_kzalloc().

---
 drivers/gpio/gpio-mockup.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpio/gpio-mockup.c +4 -4
index 536a229..a6565e1 100644
@@ -128,7 +128,7 @@ static int gpio_mockup_name_lines(struct device *dev,
 	char **names;
 	int i;
 
-	names = devm_kzalloc(dev, sizeof(char *) * gc->ngpio, GFP_KERNEL);
+	names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
 	if (!names)
 		return -ENOMEM;
 
@@ -308,8 +308,8 @@ static int gpio_mockup_add(struct device *dev,
 	gc->get_direction = gpio_mockup_get_direction;
 	gc->to_irq = gpio_mockup_to_irq;
 
-	chip->lines = devm_kzalloc(dev, sizeof(*chip->lines) * gc->ngpio,
-				   GFP_KERNEL);
+	chip->lines = devm_kcalloc(dev, gc->ngpio,
+				   sizeof(*chip->lines), GFP_KERNEL);
 	if (!chip->lines)
 		return -ENOMEM;
 
@@ -346,7 +346,7 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 	/* Each chip is described by two values. */
 	num_chips = gpio_mockup_params_nr / 2;
 
-	chips = devm_kzalloc(dev, sizeof(*chips) * num_chips, GFP_KERNEL);
+	chips = devm_kcalloc(dev, num_chips, sizeof(*chips), GFP_KERNEL);
 	if (!chips)
 		return -ENOMEM;
 
Signature · 1 line
2.9.3
#0 lore raw

v3 cover gpio: mockup: updates for 4.13

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

v2 -> v3:
- added the patch replacing devm_kzalloc() with devm_kcalloc() where applicable

Bartosz Golaszewski (8):
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
gpio: mockup: use devm_kcalloc() where applicable

 drivers/gpio/gpio-mockup.c | 67 ++++++++++++++++++++++++----------------------
 1 file changed, 35 insertions(+), 32 deletions(-)
Signature · 1 line
2.9.3
#11 lore raw

PATCH v3 6/8 gpio: mockup: improve the error message

Indicate 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
index ab2d38e..2f4fe41 100644
@@ -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
Reviewed-by on the v3 series
#17 lore raw
In reply to Bartosz Golaszewski
Quoted 11 lines · Bartosz Golaszewski

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

v2 -> v3:
- added the patch replacing devm_kzalloc() with devm_kcalloc() where applicable

FWIW,

Quoted 16 lines

Bartosz Golaszewski (8):
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
gpio: mockup: use devm_kcalloc() where applicable

 drivers/gpio/gpio-mockup.c | 67 ++++++++++++++++++++++++----------------------
 1 file changed, 35 insertions(+), 32 deletions(-)

--
2.9.3

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw

Re: [PATCH v3 1/8] gpio: mockup: improve the debugfs input sanitization

In reply to Bartosz Golaszewski
Quoted 11 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_enabled check so that we indicate an error on invalid input even if nobody is waiting for events.

Patch applied.

Yours,
Linus Walleij