Skip to contentSkip to conversation

pinctrl: starfive: jh7110: use struct_size

PATCH In review Rosen Penev 4 participants 4 messages Apr 25 – May 12

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pinctrl: starfive: jh7110: use struct_size

Instead of an extra kcalloc, Use a flexible array member to combine allocations. Saves a pointer in the struct.

---
 drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c | 12 +++++-------
 drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h |  2 +-
 2 files changed, 6 insertions(+), 8 deletions(-)
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c +5 -7
index e44480e71ea8..3572e8edd9f3 100644
@@ -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);
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h +1 -1
index 2da2d6858008..188fc9d96269 100644
@@ -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 {
Signature · 1 line
2.54.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Rosen Penev

Instead of an extra kcalloc, Use a flexible array member to combine allocations. Saves a pointer in the struct.

Looks good.

Best regards,
Hal

Quoted 50 lines
---
 drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c | 12 +++++-------
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h |  2 +-
 2 files changed, 6 insertions(+), 8 deletions(-)
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c +5 -7
index e44480e71ea8..3572e8edd9f3 100644
@@ -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))
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c +0 -0
 		return PTR_ERR(sfp->base);
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h +1 -2
index 2da2d6858008..188fc9d96269 100644
@@ -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

#3 lore raw
In reply to Rosen Penev

Hi Rosen,

Quoted 4 lines · Rosen Penev

Instead of an extra kcalloc, Use a flexible array member to combine allocations. Saves a pointer in the struct.

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

drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c int jh7110_pinctrl_probe(struct platform_device *pdev) · line 865

Quoted 12 lines
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c +5 -0
@@ -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);

drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c int jh7110_pinctrl_probe(struct platform_device *pdev) · line 871

Quoted 13 lines
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c +0 -7
        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

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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