mbox series

[v2,0/5] pinctrl: at91: Cleanups

Message ID 20230213154532.32992-1-andriy.shevchenko@linux.intel.com
Headers show
Series pinctrl: at91: Cleanups | expand

Message

Andy Shevchenko Feb. 13, 2023, 3:45 p.m. UTC
The kasprintf() patch makes me look into the driver code and besides
missed fix, there is a room to improve. Hence this series.

Compile tested only.

Changes in v2:
- fixed compilation errors (LKP)

Andy Shevchenko (5):
  pinctrl: at91: use devm_kasprintf() to avoid potential leaks (part 2)
  pinctrl: at91: Don't mix non-devm calls with devm ones
  pinctrl: at91: Use of_device_get_match_data()
  pinctrl: at91: Use dev_err_probe() instead of custom messaging
  pinctrl: at91: Utilise temporary variable for struct device

 drivers/pinctrl/pinctrl-at91.c | 164 ++++++++++++++-------------------
 1 file changed, 67 insertions(+), 97 deletions(-)

Comments

Claudiu Beznea Feb. 14, 2023, 11:05 a.m. UTC | #1
On 13.02.2023 17:45, Andy Shevchenko wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> 
> Use devm_kasprintf() instead of kasprintf() to avoid any potential
> leaks. At the moment drivers have no remove functionality hence
> there is no need for fixes tag.
> 
> While at it, switch to use devm_kasprintf_strarray().
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Reviewed-by: Claudiu Beznea <claudiu.beznea@microchip.com>
Tested-by: Claudiu Beznea <claudiu.beznea@microchip.com>


> ---
>  drivers/pinctrl/pinctrl-at91.c | 23 +++++++++++++++++------
>  1 file changed, 17 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/pinctrl/pinctrl-at91.c b/drivers/pinctrl/pinctrl-at91.c
> index 2e7debb905d5..5c01765c7a2a 100644
> --- a/drivers/pinctrl/pinctrl-at91.c
> +++ b/drivers/pinctrl/pinctrl-at91.c
> @@ -18,6 +18,7 @@
>  #include <linux/pm.h>
>  #include <linux/seq_file.h>
>  #include <linux/slab.h>
> +#include <linux/string_helpers.h>
> 
>  /* Since we request GPIOs from ourself */
>  #include <linux/pinctrl/consumer.h>
> @@ -1371,6 +1372,7 @@ static int at91_pinctrl_probe_dt(struct platform_device *pdev,
> 
>  static int at91_pinctrl_probe(struct platform_device *pdev)
>  {
> +       struct device *dev = &pdev->dev;
>         struct at91_pinctrl *info;
>         struct pinctrl_pin_desc *pdesc;
>         int ret, i, j, k;
> @@ -1394,9 +1396,19 @@ static int at91_pinctrl_probe(struct platform_device *pdev)
>                 return -ENOMEM;
> 
>         for (i = 0, k = 0; i < gpio_banks; i++) {
> +               char **names;
> +
> +               names = devm_kasprintf_strarray(dev, "pio", MAX_NB_GPIO_PER_BANK);
> +               if (!names)
> +                       return -ENOMEM;
> +
>                 for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
> +                       char *name = names[j];
> +
> +                       strreplace(name, '-', i + 'A');
> +
>                         pdesc->number = k;
> -                       pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
> +                       pdesc->name = name;
>                         pdesc++;
>                 }
>         }
> @@ -1797,7 +1809,8 @@ static const struct of_device_id at91_gpio_of_match[] = {
> 
>  static int at91_gpio_probe(struct platform_device *pdev)
>  {
> -       struct device_node *np = pdev->dev.of_node;
> +       struct device *dev = &pdev->dev;
> +       struct device_node *np = dev->of_node;
>         struct at91_gpio_chip *at91_chip = NULL;
>         struct gpio_chip *chip;
>         struct pinctrl_gpio_range *range;
> @@ -1866,16 +1879,14 @@ static int at91_gpio_probe(struct platform_device *pdev)
>                         chip->ngpio = ngpio;
>         }
> 
> -       names = devm_kcalloc(&pdev->dev, chip->ngpio, sizeof(char *),
> -                            GFP_KERNEL);
> -
> +       names = devm_kasprintf_strarray(dev, "pio", chip->ngpio);
>         if (!names) {
>                 ret = -ENOMEM;
>                 goto clk_enable_err;
>         }
> 
>         for (i = 0; i < chip->ngpio; i++)
> -               names[i] = devm_kasprintf(&pdev->dev, GFP_KERNEL, "pio%c%d", alias_idx + 'A', i);
> +               strreplace(names[i], '-', alias_idx + 'A');
> 
>         chip->names = (const char *const *)names;
> 
> --
> 2.39.1
>
Claudiu Beznea Feb. 14, 2023, 11:11 a.m. UTC | #2
On 13.02.2023 17:45, Andy Shevchenko wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> 
> We have a temporary variable to keep pointer to struct device.
> Utilise it inside the ->probe() implementation.

Apart from this the patch also removes some { } and does some alignments.
For clarity some of these might go better in a different patch.

> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/pinctrl/pinctrl-at91.c | 63 +++++++++++++++-------------------
>  1 file changed, 27 insertions(+), 36 deletions(-)
> 
> diff --git a/drivers/pinctrl/pinctrl-at91.c b/drivers/pinctrl/pinctrl-at91.c
> index 08f88403affb..7ada36ca64eb 100644
> --- a/drivers/pinctrl/pinctrl-at91.c
> +++ b/drivers/pinctrl/pinctrl-at91.c
> @@ -1304,7 +1304,7 @@ static int at91_pinctrl_probe_dt(struct platform_device *pdev,
>         if (!np)
>                 return -ENODEV;
> 
> -       info->dev = &pdev->dev;
> +       info->dev = dev;
>         info->ops = of_device_get_match_data(dev);
>         at91_pinctrl_child_count(info, np);
> 
> @@ -1324,35 +1324,30 @@ static int at91_pinctrl_probe_dt(struct platform_device *pdev,
>         if (ret)
>                 return ret;
> 
> -       dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
> +       dev_dbg(dev, "nmux = %d\n", info->nmux);
> 
> -       dev_dbg(&pdev->dev, "mux-mask\n");
> +       dev_dbg(dev, "mux-mask\n");
>         tmp = info->mux_mask;
>         for (i = 0; i < gpio_banks; i++) {
> -               for (j = 0; j < info->nmux; j++, tmp++) {

here

> -                       dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
> -               }
> +               for (j = 0; j < info->nmux; j++, tmp++)
> +                       dev_dbg(dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
>         }
> 
> -       dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
> -       dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
> -       info->functions = devm_kcalloc(&pdev->dev,
> -                                       info->nfunctions,
> -                                       sizeof(struct at91_pmx_func),
> -                                       GFP_KERNEL);
> +       dev_dbg(dev, "nfunctions = %d\n", info->nfunctions);
> +       dev_dbg(dev, "ngroups = %d\n", info->ngroups);
> +       info->functions = devm_kcalloc(dev, info->nfunctions, sizeof(*info->functions),
> +                                      GFP_KERNEL);
>         if (!info->functions)
>                 return -ENOMEM;
> 
> -       info->groups = devm_kcalloc(&pdev->dev,
> -                                       info->ngroups,
> -                                       sizeof(struct at91_pin_group),
> -                                       GFP_KERNEL);
> +       info->groups = devm_kcalloc(dev, info->ngroups, sizeof(*info->groups),
> +                                   GFP_KERNEL);
>         if (!info->groups)
>                 return -ENOMEM;
> 
> -       dev_dbg(&pdev->dev, "nbanks = %d\n", gpio_banks);
> -       dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
> -       dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
> +       dev_dbg(dev, "nbanks = %d\n", gpio_banks);
> +       dev_dbg(dev, "nfunctions = %d\n", info->nfunctions);
> +       dev_dbg(dev, "ngroups = %d\n", info->ngroups);
> 
>         i = 0;
> 
> @@ -1376,7 +1371,7 @@ static int at91_pinctrl_probe(struct platform_device *pdev)
>         struct pinctrl_pin_desc *pdesc;
>         int ret, i, j, k;
> 
> -       info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
> +       info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
>         if (!info)
>                 return -ENOMEM;
> 
> @@ -1384,13 +1379,10 @@ static int at91_pinctrl_probe(struct platform_device *pdev)
>         if (ret)
>                 return ret;
> 
> -       at91_pinctrl_desc.name = dev_name(&pdev->dev);
> +       at91_pinctrl_desc.name = dev_name(dev);
>         at91_pinctrl_desc.npins = gpio_banks * MAX_NB_GPIO_PER_BANK;
>         at91_pinctrl_desc.pins = pdesc =
> -               devm_kcalloc(&pdev->dev,
> -                            at91_pinctrl_desc.npins, sizeof(*pdesc),
> -                            GFP_KERNEL);
> -
> +               devm_kcalloc(dev, at91_pinctrl_desc.npins, sizeof(*pdesc), GFP_KERNEL);
>         if (!at91_pinctrl_desc.pins)
>                 return -ENOMEM;
> 
> @@ -1413,8 +1405,7 @@ static int at91_pinctrl_probe(struct platform_device *pdev)
>         }
> 
>         platform_set_drvdata(pdev, info);
> -       info->pctl = devm_pinctrl_register(&pdev->dev, &at91_pinctrl_desc,
> -                                          info);
> +       info->pctl = devm_pinctrl_register(dev, &at91_pinctrl_desc, info);
>         if (IS_ERR(info->pctl))
>                 return dev_err_probe(dev, PTR_ERR(info->pctl), "could not register AT91 pinctrl driver\n");
> 
> @@ -1423,7 +1414,7 @@ static int at91_pinctrl_probe(struct platform_device *pdev)
>                 if (gpio_chips[i])
>                         pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
> 
> -       dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
> +       dev_info(dev, "initialized AT91 pinctrl driver\n");
> 
>         return 0;
>  }
> @@ -1714,6 +1705,7 @@ static void gpio_irq_handler(struct irq_desc *desc)
>  static int at91_gpio_of_irq_setup(struct platform_device *pdev,
>                                   struct at91_gpio_chip *at91_gpio)
>  {
> +       struct device           *dev = &pdev->dev;
>         struct gpio_chip        *gpiochip_prev = NULL;
>         struct at91_gpio_chip   *prev = NULL;
>         struct irq_data         *d = irq_get_irq_data(at91_gpio->pioc_virq);
> @@ -1721,8 +1713,7 @@ static int at91_gpio_of_irq_setup(struct platform_device *pdev,
>         struct gpio_irq_chip    *girq;
>         int i;
> 
> -       gpio_irqchip = devm_kzalloc(&pdev->dev, sizeof(*gpio_irqchip),
> -                                   GFP_KERNEL);
> +       gpio_irqchip = devm_kzalloc(dev, sizeof(*gpio_irqchip), GFP_KERNEL);
>         if (!gpio_irqchip)
>                 return -ENOMEM;
> 
> @@ -1758,7 +1749,7 @@ static int at91_gpio_of_irq_setup(struct platform_device *pdev,
>         if (!gpiochip_prev) {
>                 girq->parent_handler = gpio_irq_handler;
>                 girq->num_parents = 1;
> -               girq->parents = devm_kcalloc(&pdev->dev, 1,
> +               girq->parents = devm_kcalloc(dev, girq->num_parents,
>                                              sizeof(*girq->parents),
>                                              GFP_KERNEL);
>                 if (!girq->parents)
> @@ -1824,7 +1815,7 @@ static int at91_gpio_probe(struct platform_device *pdev)
>         if (irq < 0)
>                 return irq;
> 
> -       at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
> +       at91_chip = devm_kzalloc(dev, sizeof(*at91_chip), GFP_KERNEL);
>         if (!at91_chip)
>                 return -ENOMEM;
> 
> @@ -1836,7 +1827,7 @@ static int at91_gpio_probe(struct platform_device *pdev)
>         at91_chip->pioc_virq = irq;
>         at91_chip->pioc_idx = alias_idx;
> 
> -       at91_chip->clock = devm_clk_get_enabled(&pdev->dev, NULL);
> +       at91_chip->clock = devm_clk_get_enabled(dev, NULL);
>         if (IS_ERR(at91_chip->clock))
>                 return dev_err_probe(dev, PTR_ERR(at91_chip->clock), "failed to get clock, ignoring.\n");
> 
> @@ -1844,8 +1835,8 @@ static int at91_gpio_probe(struct platform_device *pdev)
>         at91_chip->id = alias_idx;
> 
>         chip = &at91_chip->chip;
> -       chip->label = dev_name(&pdev->dev);
> -       chip->parent = &pdev->dev;
> +       chip->label = dev_name(dev);
> +       chip->parent = dev;
>         chip->owner = THIS_MODULE;
>         chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
> 
> @@ -1886,7 +1877,7 @@ static int at91_gpio_probe(struct platform_device *pdev)
>         platform_set_drvdata(pdev, at91_chip);
>         gpio_banks = max(gpio_banks, alias_idx + 1);
> 
> -       dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
> +       dev_info(dev, "at address %p\n", at91_chip->regbase);
> 
>         return 0;
>  }
> --
> 2.39.1
>
Andy Shevchenko Feb. 15, 2023, 1:38 p.m. UTC | #3
On Tue, Feb 14, 2023 at 11:11:58AM +0000, Claudiu.Beznea@microchip.com wrote:
> On 13.02.2023 17:45, Andy Shevchenko wrote:
> > EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> > 
> > We have a temporary variable to keep pointer to struct device.
> > Utilise it inside the ->probe() implementation.
> 
> Apart from this the patch also removes some { } and does some alignments.
> For clarity some of these might go better in a different patch.

I was under impression that checkpatch will complain about it, but it appears
that it does not. I will issue a new version with this updated.

Thanks for your reviews and testing!