[PATCH] pinctrl: starfive: jh7110: use struct_size

Rosen Penev posted 1 patch 1 month, 3 weeks ago
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c | 12 +++++-------
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h |  2 +-
2 files changed, 6 insertions(+), 8 deletions(-)
[PATCH] pinctrl: starfive: jh7110: use struct_size
Posted by Rosen Penev 1 month, 3 weeks ago
Instead of an extra kcalloc, Use a flexible array member to combine
allocations. Saves a pointer in the struct.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c | 12 +++++-------
 drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h |  2 +-
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c b/drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c
index e44480e71ea8..3572e8edd9f3 100644
--- a/drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c
+++ b/drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c
@@ -857,17 +857,15 @@ int jh7110_pinctrl_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
+#if IS_ENABLED(CONFIG_PM_SLEEP)
+	sfp = devm_kzalloc(dev, struct_size(sfp, saved_regs, info->nsaved_regs),
+			GFP_KERNEL);
+#else
 	sfp = devm_kzalloc(dev, sizeof(*sfp), GFP_KERNEL);
+#endif
 	if (!sfp)
 		return -ENOMEM;
 
-#if IS_ENABLED(CONFIG_PM_SLEEP)
-	sfp->saved_regs = devm_kcalloc(dev, info->nsaved_regs,
-				       sizeof(*sfp->saved_regs), GFP_KERNEL);
-	if (!sfp->saved_regs)
-		return -ENOMEM;
-#endif
-
 	sfp->base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(sfp->base))
 		return PTR_ERR(sfp->base);
diff --git a/drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h b/drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h
index 2da2d6858008..188fc9d96269 100644
--- a/drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h
+++ b/drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h
@@ -21,7 +21,7 @@ struct jh7110_pinctrl {
 	/* register read/write mutex */
 	struct mutex mutex;
 	const struct jh7110_pinctrl_soc_info *info;
-	u32 *saved_regs;
+	u32 saved_regs[];
 };
 
 struct jh7110_gpio_irq_reg {
-- 
2.54.0
Re: [PATCH] pinctrl: starfive: jh7110: use struct_size
Posted by Geert Uytterhoeven 1 month, 1 week ago
Hi Rosen,

On Sat, 25 Apr 2026 at 03:40, Rosen Penev <rosenp@gmail.com> wrote:
> Instead of an extra kcalloc, Use a flexible array member to combine
> allocations. Saves a pointer in the struct.
>
> Signed-off-by: Rosen Penev <rosenp@gmail.com>

Thanks for your patch, which is now commit 87182ef0bf93c283 ("pinctrl:
starfive: jh7110: use struct_size") in pinctrl/for-next.

> --- a/drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c
> +++ b/drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c
> @@ -857,17 +857,15 @@ int jh7110_pinctrl_probe(struct platform_device *pdev)
>                 return -EINVAL;
>         }
>
> +#if IS_ENABLED(CONFIG_PM_SLEEP)
> +       sfp = devm_kzalloc(dev, struct_size(sfp, saved_regs, info->nsaved_regs),
> +                       GFP_KERNEL);
> +#else
>         sfp = devm_kzalloc(dev, sizeof(*sfp), GFP_KERNEL);
> +#endif

You can avoid the #ifdef and increase compile-coverage using

    unsigned int n = IS_ENABLED(CONFIG_PM_SLEEP) ? info->nsaved_regs : 0;
    ...
    sfp = devm_kzalloc(dev, struct_size(sfp, saved_regs, n, GFP_KERNEL);


>         if (!sfp)
>                 return -ENOMEM;
>
> -#if IS_ENABLED(CONFIG_PM_SLEEP)
> -       sfp->saved_regs = devm_kcalloc(dev, info->nsaved_regs,
> -                                      sizeof(*sfp->saved_regs), GFP_KERNEL);
> -       if (!sfp->saved_regs)
> -               return -ENOMEM;
> -#endif
> -
>         sfp->base = devm_platform_ioremap_resource(pdev, 0);
>         if (IS_ERR(sfp->base))
>                 return PTR_ERR(sfp->base);

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Re: [PATCH] pinctrl: starfive: jh7110: use struct_size
Posted by Linus Walleij 1 month, 3 weeks ago
On Sat, Apr 25, 2026 at 3:40 AM Rosen Penev <rosenp@gmail.com> wrote:

> Instead of an extra kcalloc, Use a flexible array member to combine
> allocations. Saves a pointer in the struct.
>
> Signed-off-by: Rosen Penev <rosenp@gmail.com>

Patch applied!

Yours,
Linus Walleij