Skip to contentSkip to conversation

pinctrl: sophgo: allocate power_cfg with priv

PATCH In review Rosen Penev 2 participants 2 messages Apr 30 – May 5

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

PATCH 1/1 pinctrl: sophgo: allocate power_cfg with priv

Use a flexible array member to combine allocations and simplify code slightly.

---
 drivers/pinctrl/sophgo/pinctrl-cv18xx.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/pinctrl/sophgo/pinctrl-cv18xx.c +3 -7
index c3a2dcf71f2a..154ba9a4f08c 100644
@@ -27,8 +27,8 @@
 #include "pinctrl-cv18xx.h"
 
 struct cv1800_priv {
-	u32					*power_cfg;
 	void __iomem				*regs[2];
+	u32					power_cfg[];
 };
 
 static unsigned int cv1800_dt_get_pin_mux(u32 value)
@@ -417,15 +417,11 @@ static int cv1800_pinctrl_init(struct platform_device *pdev,
 	const struct sophgo_pinctrl_data *pctrl_data = pctrl->data;
 	struct cv1800_priv *priv;
 
-	priv = devm_kzalloc(&pdev->dev, sizeof(struct cv1800_priv), GFP_KERNEL);
+	priv = devm_kzalloc(&pdev->dev, struct_size(priv, power_cfg, pctrl_data->npds),
+			GFP_KERNEL);
 	if (!priv)
 		return -ENOMEM;
 
-	priv->power_cfg = devm_kcalloc(&pdev->dev, pctrl_data->npds,
-				       sizeof(u32), GFP_KERNEL);
-	if (!priv->power_cfg)
-		return -ENOMEM;
-
 	priv->regs[0] = devm_platform_ioremap_resource_byname(pdev, "sys");
 	if (IS_ERR(priv->regs[0]))
 		return PTR_ERR(priv->regs[0]);
Signature · 1 line
2.54.0
maintainer
#1 lore raw

Hi Rosen,

thanks for your patch!

Quoted 4 lines · Rosen Penev

Use a flexible array member to combine allocations and simplify code slightly.

I have applied the patch with the following augmentation:

iff --git a/drivers/pinctrl/sophgo/pinctrl-cv18xx.c b/drivers/pinctrl/sophgo/pinctrl-cv18xx.c index 154ba9a4f08c..59318a42690f 100644

drivers/pinctrl/sophgo/pinctrl-cv18xx.c +4 -2
@@ -27,8 +27,9 @@
 #include "pinctrl-cv18xx.h"
 
 struct cv1800_priv {
-       void __iomem                            *regs[2];
-       u32                                     power_cfg[];
+       void __iomem    *regs[2];
+       unsigned int    num_power_cfg;
+       u32             power_cfg[] __counted_by(num_power_cfg);
 };
 
 static unsigned int cv1800_dt_get_pin_mux(u32 value)
@@ -421,6 +422,7 @@ static int cv1800_pinctrl_init(struct platform_device *pdev,
                        GFP_KERNEL);
        if (!priv)
                return -ENOMEM;
+       priv->num_power_cfg = pctrl_data->npds;
 
        priv->regs[0] = devm_platform_ioremap_resource_byname(pdev, "sys");
        if (IS_ERR(priv->regs[0]))

I think this has been mentioned before?

Including a counter for dynamic arrays makes it possible for the compiler to insert runtime checks akin to those that are used for Pascal or Rust arrays making it harder for the arrays to be indexed out of range.

Yours,
Linus Walleij