Skip to contentSkip to conversation

phy: miphy28lp: kzalloc + kcalloc to single kzalloc

PATCH In review Rosen Penev 2 participants 3 messages Mar 4 – Mar 6

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

PATCH 1/1 phy: miphy28lp: kzalloc + kcalloc to single kzalloc

Use flex array to simplify allocation.

Allows using __counted_by for extra runtime analysis.

---
 drivers/phy/st/phy-miphy28lp.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/phy/st/phy-miphy28lp.c +5 -7
index 43cef89af55e..fd931ede7162 100644
@@ -224,8 +224,8 @@ struct miphy28lp_dev {
 	struct device *dev;
 	struct regmap *regmap;
 	struct mutex miphy_mutex;
-	struct miphy28lp_phy **phys;
 	int nphys;
+	struct miphy28lp_phy *phys[] __counted_by(nphys);
 };
 
 enum miphy_sata_gen { SATA_GEN1, SATA_GEN2, SATA_GEN3 };
@@ -1168,16 +1168,14 @@ static int miphy28lp_probe(struct platform_device *pdev)
 	struct phy_provider *provider;
 	struct phy *phy;
 	int ret, port = 0;
+	size_t nphys;
 
-	miphy_dev = devm_kzalloc(&pdev->dev, sizeof(*miphy_dev), GFP_KERNEL);
+	nphys = of_get_child_count(np);
+	miphy_dev = devm_kzalloc(&pdev->dev, struct_size(wiphy_dev, phys, nphys) ,GFP_KERNEL);
 	if (!miphy_dev)
 		return -ENOMEM;
 
-	miphy_dev->nphys = of_get_child_count(np);
-	miphy_dev->phys = devm_kcalloc(&pdev->dev, miphy_dev->nphys,
-				       sizeof(*miphy_dev->phys), GFP_KERNEL);
-	if (!miphy_dev->phys)
-		return -ENOMEM;
+	miphy_dev->nphys = nphys;
 
 	miphy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
 	if (IS_ERR(miphy_dev->regmap)) {
Signature · 1 line
2.53.0
#1 lore raw
Quoted 24 lines · Rosen Penev

Use flex array to simplify allocation.

Allows using __counted_by for extra runtime analysis.

---
  drivers/phy/st/phy-miphy28lp.c | 12 +++++-------
  1 file changed, 5 insertions(+), 7 deletions(-)
drivers/phy/st/phy-miphy28lp.c +1 -1
index 43cef89af55e..fd931ede7162 100644
@@ -224,8 +224,8 @@ struct miphy28lp_dev {
  	struct device *dev;
  	struct regmap *regmap;
  	struct mutex miphy_mutex;
-	struct miphy28lp_phy **phys;
  	int nphys;
+	struct miphy28lp_phy *phys[] __counted_by(nphys);
  };
 
  enum miphy_sata_gen { SATA_GEN1, SATA_GEN2, SATA_GEN3 };

drivers/phy/st/phy-miphy28lp.c static int miphy28lp_probe(struct platform_device *pdev) · line 1174

Quoted 8 lines
drivers/phy/st/phy-miphy28lp.c +3 -1
@@ -1168,16 +1168,14 @@ static int miphy28lp_probe(struct platform_device *pdev)
  	struct phy_provider *provider;
  	struct phy *phy;
  	int ret, port = 0;
+	size_t nphys;
 
-	miphy_dev = devm_kzalloc(&pdev->dev, sizeof(*miphy_dev), GFP_KERNEL);
+	nphys = of_get_child_count(np);
+	miphy_dev = devm_kzalloc(&pdev->dev, struct_size(wiphy_dev, phys, nphys) ,TGFP_KERNEL);

This is bad... it looks like you didn't build any of these patches.

-Gustavo

Quoted 12 lines
drivers/phy/st/phy-miphy28lp.c +1 -5
  	if (!miphy_dev)
  		return -ENOMEM;
 
-	miphy_dev->nphys = of_get_child_count(np);
-	miphy_dev->phys = devm_kcalloc(&pdev->dev, miphy_dev->nphys,
-				       sizeof(*miphy_dev->phys), GFP_KERNEL);
-	if (!miphy_dev->phys)
-		return -ENOMEM;
+	miphy_dev->nphys = nphys;
 
  	miphy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  	if (IS_ERR(miphy_dev->regmap)) {
#2 lore raw
Quoted 33 lines · Gustavo A. R. Silva
Quoted 29 lines · Rosen Penev

Use flex array to simplify allocation.

Allows using __counted_by for extra runtime analysis.

---
  drivers/phy/st/phy-miphy28lp.c | 12 +++++-------
  1 file changed, 5 insertions(+), 7 deletions(-)
drivers/phy/st/phy-miphy28lp.c +2 -1
index 43cef89af55e..fd931ede7162 100644
@@ -224,8 +224,8 @@ struct miphy28lp_dev {
      struct device *dev;
      struct regmap *regmap;
      struct mutex miphy_mutex;
-     struct miphy28lp_phy **phys;
      int nphys;
+     struct miphy28lp_phy *phys[] __counted_by(nphys);
  };
 
  enum miphy_sata_gen { SATA_GEN1, SATA_GEN2, SATA_GEN3 };
@@ -1168,16 +1168,14 @@ static int miphy28lp_probe(struct platform_device *pdev)
      struct phy_provider *provider;
      struct phy *phy;
      int ret, port = 0;
+     size_t nphys;
 
Quoted 5 lines

drivers/phy/st/phy-miphy28lp.c static int miphy28lp_probe(struct platform_device *pdev)

Quoted 3 lines
drivers/phy/st/phy-miphy28lp.c +2 -1
-     miphy_dev = devm_kzalloc(&pdev->dev, sizeof(*miphy_dev), GFP_KERNEL);
+     nphys = of_get_child_count(np);
+     miphy_dev = devm_kzalloc(&pdev->dev, struct_size(wiphy_dev, phys, nphys) ,TGFP_KERNEL);

This is bad... it looks like you didn't build any of these patches.

Missing COMPILE_TEST on this driver. Will fix.

Quoted 16 lines

-Gustavo

Quoted 12 lines
drivers/phy/st/phy-miphy28lp.c +1 -5
      if (!miphy_dev)
              return -ENOMEM;
 
-     miphy_dev->nphys = of_get_child_count(np);
-     miphy_dev->phys = devm_kcalloc(&pdev->dev, miphy_dev->nphys,
-                                    sizeof(*miphy_dev->phys), GFP_KERNEL);
-     if (!miphy_dev->phys)
-             return -ENOMEM;
+     miphy_dev->nphys = nphys;
 
      miphy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
      if (IS_ERR(miphy_dev->regmap)) {