Skip to contentSkip to conversation

pinctrl: rza2: embed pins in the priv struct

PATCH In review Rosen Penev 2 participants 2 messages May 27 – Jun 2

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

PATCH v2 1/1 pinctrl: rza2: embed pins in the priv struct

Turn the separately allocated pinctrl_pin_desc array into a flexible array member of struct rza2_pinctrl_priv, annotated with __counted_by(npins). The pin count is now computed before allocation so struct_size() can size the combined object, collapsing two allocations into one.

Change npins to unsigned int to avoid potential overflow/underflow errors.

Assisted-by Claude:Opus-4.7

---
v2: use unsigned inr

 drivers/pinctrl/renesas/pinctrl-rza2.c | 28 ++++++++++++--------------
 1 file changed, 13 insertions(+), 15 deletions(-)
drivers/pinctrl/renesas/pinctrl-rza2.c +13 -15
index 8618f32ed26a..15ae5a88d705 100644
@@ -44,12 +44,12 @@ struct rza2_pinctrl_priv {
 	struct device *dev;
 	void __iomem *base;
 
-	struct pinctrl_pin_desc *pins;
 	struct pinctrl_desc desc;
 	struct pinctrl_dev *pctl;
 	struct pinctrl_gpio_range gpio_range;
-	int npins;
+	unsigned int npins;
 	struct mutex mutex; /* serialize adding groups and functions */
+	struct pinctrl_pin_desc pins[] __counted_by(npins);
 };
 
 #define RZA2_PDR(port)		(0x0000 + (port) * 2)	/* Direction 16-bit */
@@ -289,21 +289,17 @@ static int rza2_gpio_register(struct rza2_pinctrl_priv *priv)
 
 static int rza2_pinctrl_register(struct rza2_pinctrl_priv *priv)
 {
-	struct pinctrl_pin_desc *pins;
+	struct pinctrl_pin_desc *pin;
 	unsigned int i;
 	int ret;
 
-	pins = devm_kcalloc(priv->dev, priv->npins, sizeof(*pins), GFP_KERNEL);
-	if (!pins)
-		return -ENOMEM;
-
-	priv->pins = pins;
-	priv->desc.pins = pins;
+	priv->desc.pins = priv->pins;
 	priv->desc.npins = priv->npins;
 
 	for (i = 0; i < priv->npins; i++) {
-		pins[i].number = i;
-		pins[i].name = rza2_gpio_names[i];
+		pin = &priv->pins[i];
+		pin->number = i;
+		pin->name = rza2_gpio_names[i];
 	}
 
 	ret = devm_pinctrl_register_and_init(priv->dev, &priv->desc, priv,
@@ -482,12 +478,17 @@ static const struct pinmux_ops rza2_pinmux_ops = {
 static int rza2_pinctrl_probe(struct platform_device *pdev)
 {
 	struct rza2_pinctrl_priv *priv;
+	unsigned int npins;
 	int ret;
 
-	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+	npins = (uintptr_t)of_device_get_match_data(&pdev->dev) *
+		      RZA2_PINS_PER_PORT;
+
+	priv = devm_kzalloc(&pdev->dev, struct_size(priv, pins, npins), GFP_KERNEL);
 	if (!priv)
 		return -ENOMEM;
 
+	priv->npins = npins;
 	priv->dev = &pdev->dev;
 
 	priv->base = devm_platform_ioremap_resource(pdev, 0);
@@ -498,9 +499,6 @@ static int rza2_pinctrl_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, priv);
 
-	priv->npins = (int)(uintptr_t)of_device_get_match_data(&pdev->dev) *
-		      RZA2_PINS_PER_PORT;
-
 	priv->desc.name		= DRIVER_NAME;
 	priv->desc.pctlops	= &rza2_pinctrl_ops;
 	priv->desc.pmxops	= &rza2_pinmux_ops;

--
2.54.0

Reviewed-by on v2 1/1
#1 lore raw

Hi Rosen,

Quoted 6 lines · Rosen Penev

Turn the separately allocated pinctrl_pin_desc array into a flexible array member of struct rza2_pinctrl_priv, annotated with __counted_by(npins). The pin count is now computed before allocation so struct_size() can size the combined object, collapsing two allocations into one.

Quoted 7 lines

Change npins to unsigned int to avoid potential overflow/underflow errors.

Assisted-by Claude:Opus-4.7

---
v2: use unsigned inr

Thanks for your patch!

drivers/pinctrl/renesas/pinctrl-rza2.c static int rza2_gpio_register(struct rza2_pinctrl_priv *priv) · line 292

Quoted 9 lines
drivers/pinctrl/renesas/pinctrl-rza2.c +1 -1
@@ -289,21 +289,17 @@ static int rza2_gpio_register(struct rza2_pinctrl_priv *priv)
 
 static int rza2_pinctrl_register(struct rza2_pinctrl_priv *priv)
 {
-       struct pinctrl_pin_desc *pins;
+       struct pinctrl_pin_desc *pin;

I will drop pin...

Quoted 8 lines
drivers/pinctrl/renesas/pinctrl-rza2.c +0 -5
        unsigned int i;
        int ret;
 
-       pins = devm_kcalloc(priv->dev, priv->npins, sizeof(*pins), GFP_KERNEL);
-       if (!pins)
-               return -ENOMEM;
-
-       priv->pins = pins;

drivers/pinctrl/renesas/pinctrl-rza2.c static int rza2_gpio_register(struct rza2_pinctrl_priv *priv) · line 302

Quoted 10 lines
drivers/pinctrl/renesas/pinctrl-rza2.c +4 -3
-       priv->desc.pins = pins;
+       priv->desc.pins = priv->pins;
        priv->desc.npins = priv->npins;
 
        for (i = 0; i < priv->npins; i++) {
-               pins[i].number = i;
-               pins[i].name = rza2_gpio_names[i];
+               pin = &priv->pins[i];
+               pin->number = i;
+               pin->name = rza2_gpio_names[i];

... and simplify this to

    priv->pins[i].number = i;
    priv->pins[i].name = rza2_gpio_names[i];

while applying.

drivers/pinctrl/renesas/pinctrl-rza2.c static int rza2_gpio_register(struct rza2_pinctrl_priv *priv) · line 305

Quoted 3 lines
drivers/pinctrl/renesas/pinctrl-rza2.c +0 -0
        }
 
        ret = devm_pinctrl_register_and_init(priv->dev, &priv->desc, priv,

i.e. will queue in renesas-pinctrl for v7.3, with the above fixed.

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