diff mbox series

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

Message ID 20171213112520.7479-2-brgl@bgdev.pl
State New
Headers show
Series [1/2] gpiolib: constify label in gpio_device | expand

Commit Message

Bartosz Golaszewski Dec. 13, 2017, 11:25 a.m. UTC
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.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
---
 drivers/gpio/gpiolib.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Joe Perches Dec. 13, 2017, 7:44 p.m. UTC | #1
On Wed, 2017-12-13 at 12:25 +0100, Bartosz Golaszewski wrote:
> 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.
[]
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
[]
> @@ -1171,9 +1171,9 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
>  	}
>  
>  	if (chip->label)
> -		gdev->label = kstrdup(chip->label, GFP_KERNEL);
> +		gdev->label = kstrdup_const(chip->label, GFP_KERNEL);
>  	else
> -		gdev->label = kstrdup("unknown", GFP_KERNEL);
> +		gdev->label = kstrdup_const("unknown", GFP_KERNEL);

A plain ?: would be more intelligible
and a bit smaller object code too.

ie:
	gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);

>  	if (!gdev->label) {
>  		status = -ENOMEM;
>  		goto err_free_descs;

--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Bartosz Golaszewski Dec. 14, 2017, 2:30 p.m. UTC | #2
2017-12-13 20:44 GMT+01:00 Joe Perches <joe@perches.com>:
> On Wed, 2017-12-13 at 12:25 +0100, Bartosz Golaszewski wrote:
>> 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.
> []
>> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> []
>> @@ -1171,9 +1171,9 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
>>       }
>>
>>       if (chip->label)
>> -             gdev->label = kstrdup(chip->label, GFP_KERNEL);
>> +             gdev->label = kstrdup_const(chip->label, GFP_KERNEL);
>>       else
>> -             gdev->label = kstrdup("unknown", GFP_KERNEL);
>> +             gdev->label = kstrdup_const("unknown", GFP_KERNEL);
>
> A plain ?: would be more intelligible
> and a bit smaller object code too.
>
> ie:
>         gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
>
>>       if (!gdev->label) {
>>               status = -ENOMEM;
>>               goto err_free_descs;
>

Good point. Sent v2.

Thanks,
Bartosz
--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox series

Patch

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 56eec094184c..c078f7f35100 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -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);
 }
@@ -1171,9 +1171,9 @@  int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
 	}
 
 	if (chip->label)
-		gdev->label = kstrdup(chip->label, GFP_KERNEL);
+		gdev->label = kstrdup_const(chip->label, GFP_KERNEL);
 	else
-		gdev->label = kstrdup("unknown", GFP_KERNEL);
+		gdev->label = kstrdup_const("unknown", GFP_KERNEL);
 	if (!gdev->label) {
 		status = -ENOMEM;
 		goto err_free_descs;
@@ -1294,7 +1294,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: