OSDN Git Service

gpiolib: use kstrdup_const() for gpio_device label
authorBartosz Golaszewski <brgl@bgdev.pl>
Thu, 14 Dec 2017 14:29:20 +0000 (15:29 +0100)
committerLinus Walleij <linus.walleij@linaro.org>
Wed, 20 Dec 2017 08:16:39 +0000 (09:16 +0100)
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.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
drivers/gpio/gpiolib.c

index 56eec09..e9ec44f 100644 (file)
@@ -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 @@ err_remove_from_list:
        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: