Skip to contentSkip to conversation

ata: ahci_tegra: remove kcalloc

PATCH Superseded Rosen Penev 4 participants 6 messages Mar 24 – Mar 25

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ata: ahci_tegra: remove kcalloc

Combine allocations into one by using a flexible array member.

---
 drivers/ata/ahci_tegra.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/ata/ahci_tegra.c +6 -9
index 44584eed6374..5972fe04ff3f 100644
@@ -175,8 +175,9 @@ struct tegra_ahci_priv {
 	struct reset_control	   *sata_cold_rst;
 	/* Needs special handling, cannot use ahci_platform */
 	struct clk		   *sata_clk;
-	struct regulator_bulk_data *supplies;
 	const struct tegra_ahci_soc *soc;
+
+	struct regulator_bulk_data supplies[];
 };
 
 static void tegra_ahci_handle_quirks(struct ahci_host_priv *hpriv)
@@ -512,6 +513,7 @@ static const struct scsi_host_template ahci_platform_sht = {
 
 static int tegra_ahci_probe(struct platform_device *pdev)
 {
+	const struct tegra_ahci_soc *soc;
 	struct ahci_host_priv *hpriv;
 	struct tegra_ahci_priv *tegra;
 	struct resource *res;
@@ -521,14 +523,15 @@ static int tegra_ahci_probe(struct platform_device *pdev)
 	if (IS_ERR(hpriv))
 		return PTR_ERR(hpriv);
 
-	tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
+	soc = of_device_get_match_data(&pdev->dev);
+	tegra = devm_kzalloc(&pdev->dev, struct_size(tegra, supplies, soc->num_supplies), GFP_KERNEL);
 	if (!tegra)
 		return -ENOMEM;
 
 	hpriv->plat_data = tegra;
 
 	tegra->pdev = pdev;
-	tegra->soc = of_device_get_match_data(&pdev->dev);
+	tegra->soc = soc;
 
 	tegra->sata_regs = devm_platform_ioremap_resource(pdev, 1);
 	if (IS_ERR(tegra->sata_regs))
@@ -571,12 +574,6 @@ static int tegra_ahci_probe(struct platform_device *pdev)
 		return PTR_ERR(tegra->sata_clk);
 	}
 
-	tegra->supplies = devm_kcalloc(&pdev->dev,
-				       tegra->soc->num_supplies,
-				       sizeof(*tegra->supplies), GFP_KERNEL);
-	if (!tegra->supplies)
-		return -ENOMEM;
-
 	regulator_bulk_set_supply_names(tegra->supplies,
 					tegra->soc->supply_names,
 					tegra->soc->num_supplies);
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 12 lines · Rosen Penev

Combine allocations into one by using a flexible array member.

---
 drivers/ata/ahci_tegra.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/ata/ahci_tegra.c +0 -0
index 44584eed6374..5972fe04ff3f 100644

drivers/ata/ahci_tegra.c struct tegra_ahci_priv { · line 180

Quoted 7 lines
drivers/ata/ahci_tegra.c +2 -1
@@ -175,8 +175,9 @@ struct tegra_ahci_priv {
 	struct reset_control	   *sata_cold_rst;
 	/* Needs special handling, cannot use ahci_platform */
 	struct clk		   *sata_clk;
-	struct regulator_bulk_data *supplies;
 	const struct tegra_ahci_soc *soc;
+
+	struct regulator_bulk_data supplies[];

I think this needs a __counted_by() annotation, but not sure if that is possible given that soc->num_supplies is not in this structure. Might need a copy of it.

Quoted 42 lines
drivers/ata/ahci_tegra.c +4 -8
 };
 
 static void tegra_ahci_handle_quirks(struct ahci_host_priv *hpriv)
@@ -512,6 +513,7 @@ static const struct scsi_host_template ahci_platform_sht = {
 
 static int tegra_ahci_probe(struct platform_device *pdev)
 {
+	const struct tegra_ahci_soc *soc;
 	struct ahci_host_priv *hpriv;
 	struct tegra_ahci_priv *tegra;
 	struct resource *res;
@@ -521,14 +523,15 @@ static int tegra_ahci_probe(struct platform_device *pdev)
 	if (IS_ERR(hpriv))
 		return PTR_ERR(hpriv);
 
-	tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
+	soc = of_device_get_match_data(&pdev->dev);
+	tegra = devm_kzalloc(&pdev->dev, struct_size(tegra, supplies, soc->num_supplies), GFP_KERNEL);
 	if (!tegra)
 		return -ENOMEM;
 
 	hpriv->plat_data = tegra;
 
 	tegra->pdev = pdev;
-	tegra->soc = of_device_get_match_data(&pdev->dev);
+	tegra->soc = soc;
 
 	tegra->sata_regs = devm_platform_ioremap_resource(pdev, 1);
 	if (IS_ERR(tegra->sata_regs))
@@ -571,12 +574,6 @@ static int tegra_ahci_probe(struct platform_device *pdev)
 		return PTR_ERR(tegra->sata_clk);
 	}
 
-	tegra->supplies = devm_kcalloc(&pdev->dev,
-				       tegra->soc->num_supplies,
-				       sizeof(*tegra->supplies), GFP_KERNEL);
-	if (!tegra->supplies)
-		return -ENOMEM;
-
 	regulator_bulk_set_supply_names(tegra->supplies,
 					tegra->soc->supply_names,
 					tegra->soc->num_supplies);
Signature · 2 lines
Damien Le Moal
Western Digital Research
#2 lore raw
Quoted 9 lines · Damien Le Moal
Quoted 7 lines · Rosen Penev

Combine allocations into one by using a flexible array member.

---
 drivers/ata/ahci_tegra.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
Quoted 15 lines

drivers/ata/ahci_tegra.c struct tegra_ahci_priv { · line 180

Quoted 12 lines
drivers/ata/ahci_tegra.c +2 -1
index 44584eed6374..5972fe04ff3f 100644
@@ -175,8 +175,9 @@ struct tegra_ahci_priv {
      struct reset_control       *sata_cold_rst;
      /* Needs special handling, cannot use ahci_platform */
      struct clk                 *sata_clk;
-     struct regulator_bulk_data *supplies;
      const struct tegra_ahci_soc *soc;
+
+     struct regulator_bulk_data supplies[];

I think this needs a __counted_by() annotation, but not sure if that is possible given that soc->num_supplies is not in this structure. Might need a copy of it.

Is it really worth it to make a copy?

Quoted 48 lines
Quoted 33 lines
drivers/ata/ahci_tegra.c +4 -2
 };
 
 static void tegra_ahci_handle_quirks(struct ahci_host_priv *hpriv)
@@ -512,6 +513,7 @@ static const struct scsi_host_template ahci_platform_sht = {
 
 static int tegra_ahci_probe(struct platform_device *pdev)
 {
+     const struct tegra_ahci_soc *soc;
      struct ahci_host_priv *hpriv;
      struct tegra_ahci_priv *tegra;
      struct resource *res;
@@ -521,14 +523,15 @@ static int tegra_ahci_probe(struct platform_device *pdev)
      if (IS_ERR(hpriv))
              return PTR_ERR(hpriv);
 
-     tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
+     soc = of_device_get_match_data(&pdev->dev);
+     tegra = devm_kzalloc(&pdev->dev, struct_size(tegra, supplies, soc->num_supplies), GFP_KERNEL);
      if (!tegra)
              return -ENOMEM;
 
      hpriv->plat_data = tegra;
 
      tegra->pdev = pdev;
-     tegra->soc = of_device_get_match_data(&pdev->dev);
+     tegra->soc = soc;
 
      tegra->sata_regs = devm_platform_ioremap_resource(pdev, 1);
      if (IS_ERR(tegra->sata_regs))
@@ -571,12 +574,6 @@ static int tegra_ahci_probe(struct platform_device *pdev)
              return PTR_ERR(tegra->sata_clk);
      }

drivers/ata/ahci_tegra.c static int tegra_ahci_probe(struct platform_device *pdev) · line 579

Quoted 10 lines
drivers/ata/ahci_tegra.c +0 -6
 
-     tegra->supplies = devm_kcalloc(&pdev->dev,
-                                    tegra->soc->num_supplies,
-                                    sizeof(*tegra->supplies), GFP_KERNEL);
-     if (!tegra->supplies)
-             return -ENOMEM;
-
      regulator_bulk_set_supply_names(tegra->supplies,
                                      tegra->soc->supply_names,
                                      tegra->soc->num_supplies);

--
Damien Le Moal
Western Digital Research

maintainer
#3 lore raw
In reply to Rosen Penev

Hello Rosen,

subject is a bit misleading:
"remove kcalloc"
you are removing devm_kcalloc(), so device managed.

Quoted 12 lines · Rosen Penev

Combine allocations into one by using a flexible array member.

---
 drivers/ata/ahci_tegra.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/ata/ahci_tegra.c +0 -0
index 44584eed6374..5972fe04ff3f 100644

drivers/ata/ahci_tegra.c struct tegra_ahci_priv { · line 180

Quoted 7 lines
drivers/ata/ahci_tegra.c +2 -1
@@ -175,8 +175,9 @@ struct tegra_ahci_priv {
 	struct reset_control	   *sata_cold_rst;
 	/* Needs special handling, cannot use ahci_platform */
 	struct clk		   *sata_clk;
-	struct regulator_bulk_data *supplies;
 	const struct tegra_ahci_soc *soc;
+
+	struct regulator_bulk_data supplies[];

Personally I'm not a big fan of flexible array members, as there can be only one. And if you use it you want to use counted_by().

Yes, there are two device managed allocations. But is that so bad?

Since it is device managed, it will get freed on device removal anyway.

Kind regards,
Niklas

maintainer
#4 lore raw
Quoted 31 lines · Niklas Cassel

Hello Rosen,

subject is a bit misleading:
"remove kcalloc"
you are removing devm_kcalloc(), so device managed.

Quoted 12 lines · Rosen Penev

Combine allocations into one by using a flexible array member.

---
  drivers/ata/ahci_tegra.c | 15 ++++++---------
  1 file changed, 6 insertions(+), 9 deletions(-)
drivers/ata/ahci_tegra.c +0 -0
index 44584eed6374..5972fe04ff3f 100644

drivers/ata/ahci_tegra.c struct tegra_ahci_priv { · line 180

Quoted 7 lines
drivers/ata/ahci_tegra.c +2 -1
@@ -175,8 +175,9 @@ struct tegra_ahci_priv {
  	struct reset_control	   *sata_cold_rst;
  	/* Needs special handling, cannot use ahci_platform */
  	struct clk		   *sata_clk;
-	struct regulator_bulk_data *supplies;
  	const struct tegra_ahci_soc *soc;
+
+	struct regulator_bulk_data supplies[];

Personally I'm not a big fan of flexible array members, as there can be only one. And if you use it you want to use counted_by().

Quoted 3 lines

Yes, there are two device managed allocations. But is that so bad?

Since it is device managed, it will get freed on device removal anyway.

FWIW I am not a big fan of this either. It is not an obvious bang for the buck for me. The one downside I see is that it does leave the door open for someone accidentally putting another variable after the flexible array member. Yes we should catch this in review, but there really should be at least a comment saying this must be the final member of the struct.

Jon

Signature · 1 line
nvpublic
#5 lore raw
Quoted 36 lines · Jon Hunter
Quoted 33 lines · Niklas Cassel

Hello Rosen,

subject is a bit misleading:
"remove kcalloc"
you are removing devm_kcalloc(), so device managed.

Quoted 12 lines · Rosen Penev

Combine allocations into one by using a flexible array member.

---
  drivers/ata/ahci_tegra.c | 15 ++++++---------
  1 file changed, 6 insertions(+), 9 deletions(-)
drivers/ata/ahci_tegra.c +0 -0
index 44584eed6374..5972fe04ff3f 100644

drivers/ata/ahci_tegra.c struct tegra_ahci_priv { · line 180

Quoted 7 lines
drivers/ata/ahci_tegra.c +2 -1
@@ -175,8 +175,9 @@ struct tegra_ahci_priv {
     struct reset_control       *sata_cold_rst;
     /* Needs special handling, cannot use ahci_platform */
     struct clk                 *sata_clk;
-    struct regulator_bulk_data *supplies;
     const struct tegra_ahci_soc *soc;
+
+    struct regulator_bulk_data supplies[];

Personally I'm not a big fan of flexible array members, as there can be only one. And if you use it you want to use counted_by().

Yes, there are two device managed allocations. But is that so bad?

Quoted 8 lines
Quoted 1 line

Since it is device managed, it will get freed on device removal anyway.

FWIW I am not a big fan of this either. It is not an obvious bang for the buck for me. The one downside I see is that it does leave the door open for someone accidentally putting another variable after the flexible array member. Yes we should catch this in review, but there really should be at least a comment saying this must be the final member of the struct.

That will eventually become a compile time error. Currently there are a bunch of those cases that need to get fixed before that happens. Hardening people are working on it.

Quoted 6 lines

Jon

--
nvpublic