Skip to contentSkip to conversation

gpiolib: gpio_device label improvements

PATCH In review Bartosz Golaszewski 2 participants 4 messages Dec 14, 2017 – Dec 20, 2017 GPIO

lore
Share & tools
lore
#0 lore raw

v2 cover gpiolib: gpio_device label improvements

Avoid unnecessary memory allocations with kstrdup_const().

v1 -> v2:
- use a ternary operator instead of an if/else in patch 2/2

Bartosz Golaszewski (2):
gpiolib: constify label in gpio_device
gpiolib: use kstrdup_const() for gpio_device label

 drivers/gpio/gpiolib.c | 9 +++------
 drivers/gpio/gpiolib.h | 2 +-
 2 files changed, 4 insertions(+), 7 deletions(-)
Signature · 1 line
2.15.1
#1 lore raw

PATCH v2 1/2 gpiolib: constify label in gpio_device

This string is never modified. Make it const.

---
 drivers/gpio/gpiolib.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpiolib.h +1 -1
index 5e1f7cc6eeb6..6e9228b94437 100644
@@ -58,7 +58,7 @@ struct gpio_device {
 	struct gpio_desc	*descs;
 	int			base;
 	u16			ngpio;
-	char			*label;
+	const char		*label;
 	void			*data;
 	struct list_head        list;
 
Signature · 1 line
2.15.1
#2 lore raw

PATCH v2 2/2 gpiolib: use kstrdup_const() for gpio_device label

In reply to Bartosz Golaszewski

Users often pass a pointer to a static string to gpiochip_add_data() family of functions. Avoid unnecessary memory allocations with the provided helper routine.

While at it: use a ternary operator instead of an if else for brevity.

---
 drivers/gpio/gpiolib.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/gpio/gpiolib.c +3 -6
index 56eec094184c..e9ec44ffaaaf 100644
@@ -1062,7 +1062,7 @@ static void gpiodevice_release(struct device *dev)
 
 	list_del(&gdev->list);
 	ida_simple_remove(&gpio_ida, gdev->id);
-	kfree(gdev->label);
+	kfree_const(gdev->label);
 	kfree(gdev->descs);
 	kfree(gdev);
 }
@@ -1170,10 +1170,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
 		goto err_free_descs;
 	}
 
-	if (chip->label)
-		gdev->label = kstrdup(chip->label, GFP_KERNEL);
-	else
-		gdev->label = kstrdup("unknown", GFP_KERNEL);
+	gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
 	if (!gdev->label) {
 		status = -ENOMEM;
 		goto err_free_descs;
@@ -1294,7 +1291,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
 	list_del(&gdev->list);
 	spin_unlock_irqrestore(&gpio_lock, flags);
 err_free_label:
-	kfree(gdev->label);
+	kfree_const(gdev->label);
 err_free_descs:
 	kfree(gdev->descs);
 err_free_gdev:
Signature · 1 line
2.15.1
#3 lore raw

Re: [PATCH v2 2/2] gpiolib: use kstrdup_const() for gpio_device label

Quoted 7 lines · Bartosz Golaszewski

Users often pass a pointer to a static string to gpiochip_add_data() family of functions. Avoid unnecessary memory allocations with the provided helper routine.

While at it: use a ternary operator instead of an if else for brevity.

Patch applied. (Patch 1/2 was already applied from an earlier version).

Yours,
Linus Walleij