Skip to contentSkip to conversation

pinctrl: starfive: Fix some jh7110 issues found in late review

PATCH In review Linus Walleij 2 participants 5 messages May 17 – May 25

Subsystems (3)
lore
Share & tools
lore

Linus Walleij

author maintainer
#0 lore raw

cover pinctrl: starfive: Fix some jh7110 issues found in late review

Found a counted_by() issue and implemented a suggestion from Geert.

---
Linus Walleij (2):
pinctrl: starfive: jh7110: Use __counted_by() flexarray
pinctrl: starfive: jh7110: Avoid ifdeffery

 drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c | 11 +++++------
 drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h |  3 ++-
 2 files changed, 7 insertions(+), 7 deletions(-)

---
base-commit: 42ec31ec8df4675af621ec686db7410153beebfd
change-id: 20260518-jh7110-66ec4fa3b3db

Best regards,

Signature · 1 line
Linus Walleij <[email protected]>
author maintainer
#1 lore raw

PATCH 1/2 pinctrl: starfive: jh7110: Use __counted_by() flexarray

Flexible arrays should use __counted_by() to be able to do runtime checks that the array does not go out of range.

Cc Rosen Penev <[email protected]>
Fixes 87182ef0bf93 ("pinctrl: starfive: jh7110: use struct_size")
---
 drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c | 1 +
 drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c +1 -0
index 3572e8edd9f3..3fb9aa8ddf07 100644
@@ -865,6 +865,7 @@ int jh7110_pinctrl_probe(struct platform_device *pdev)
 #endif
 	if (!sfp)
 		return -ENOMEM;
+	sfp->num_saved_regs = info->nsaved_regs;
 
 	sfp->base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(sfp->base))
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.h +2 -1
index 188fc9d96269..12568be28527 100644
@@ -21,7 +21,8 @@ struct jh7110_pinctrl {
 	/* register read/write mutex */
 	struct mutex mutex;
 	const struct jh7110_pinctrl_soc_info *info;
-	u32 saved_regs[];
+	unsigned int num_saved_regs;
+	u32 saved_regs[] __counted_by(num_saved_regs);
 };
 
 struct jh7110_gpio_irq_reg {
Signature · 1 line
2.54.0
author maintainer
#2 lore raw

PATCH 2/2 pinctrl: starfive: jh7110: Avoid ifdeffery

In reply to Linus Walleij

Use IS_ENABLED() inline assigning a variable instead of ifdeffery.

---
 drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c +5 -7
index 3fb9aa8ddf07..ec359cb873c4 100644
@@ -845,6 +845,7 @@ int jh7110_pinctrl_probe(struct platform_device *pdev)
 	struct jh7110_pinctrl *sfp;
 	struct pinctrl_desc *jh7110_pinctrl_desc;
 	struct reset_control *rst;
+	unsigned int num_saved_regs;
 	struct clk *clk;
 	int ret;
 
@@ -857,15 +858,12 @@ 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
+	num_saved_regs = IS_ENABLED(CONFIG_PM_SLEEP) ? info->nsaved_regs : 0;
+	sfp = devm_kzalloc(dev, struct_size(sfp, saved_regs, num_saved_regs),
+			   GFP_KERNEL);
 	if (!sfp)
 		return -ENOMEM;
-	sfp->num_saved_regs = info->nsaved_regs;
+	sfp->num_saved_regs = num_saved_regs;
 
 	sfp->base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(sfp->base))
Signature · 1 line
2.54.0
#3 lore raw

Re: [PATCH 2/2] pinctrl: starfive: jh7110: Avoid ifdeffery

Quoted 30 lines · Linus Walleij

Use IS_ENABLED() inline assigning a variable instead of ifdeffery.

---
 drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c +1 -3
index 3fb9aa8ddf07..ec359cb873c4 100644
@@ -845,6 +845,7 @@ int jh7110_pinctrl_probe(struct platform_device *pdev)
        struct jh7110_pinctrl *sfp;
        struct pinctrl_desc *jh7110_pinctrl_desc;
        struct reset_control *rst;
+       unsigned int num_saved_regs;
        struct clk *clk;
        int ret;
 
@@ -857,15 +858,12 @@ 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);

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

Quoted 10 lines
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c +4 -4
-#else
-       sfp = devm_kzalloc(dev, sizeof(*sfp), GFP_KERNEL);
-#endif
+       num_saved_regs = IS_ENABLED(CONFIG_PM_SLEEP) ? info->nsaved_regs : 0;
+       sfp = devm_kzalloc(dev, struct_size(sfp, saved_regs, num_saved_regs),
+                          GFP_KERNEL);
        if (!sfp)
                return -ENOMEM;
-       sfp->num_saved_regs = info->nsaved_regs;
+       sfp->num_saved_regs = num_saved_regs;

yeah and this is used absolutely nowhere. Both for loops are of the form

for (i = 0 ; i < sfp->info->nsaved_regs ; i++)

I really don't think __counted_by is worth it here.

Quoted 7 lines
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c +0 -1
        sfp->base = devm_platform_ioremap_resource(pdev, 0);
        if (IS_ERR(sfp->base))
 
--

2.54.0

author maintainer
#4 lore raw

Re: [PATCH 2/2] pinctrl: starfive: jh7110: Avoid ifdeffery

Quoted 8 lines · Rosen Penev

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

Quoted 2 lines
drivers/pinctrl/starfive/pinctrl-starfive-jh7110.c +1 -1
-       sfp->num_saved_regs = info->nsaved_regs;
+       sfp->num_saved_regs = num_saved_regs;

yeah and this is used absolutely nowhere. Both for loops are of the form

for (i = 0 ; i < sfp->info->nsaved_regs ; i++)

I really don't think __counted_by is worth it here.

__counted_by() is used by compiler instrumentation, i.e. by hardening code that is not visible directly in the driver.

During compilation, code will be inserted that checks boundaries of the array at runtime.

Yours,
Linus Walleij