Skip to contentSkip to conversation

ata: ahci_tegra: use devm_kzalloc_flex()

PATCH In review Rosen Penev 2 participants 2 messages Nov 11

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

PATCH v3 1/1 ata: ahci_tegra: use devm_kzalloc_flex()

Combine allocations into one by using a flexible array member.

---
v3: use devm_kzalloc_flex()
v2: Rebase

 drivers/ata/ahci_tegra.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/ata/ahci_tegra.c +6 -8
index 2b9b98643128..c6ec344c2e6c 100644
@@ -176,8 +176,9 @@ struct tegra_ahci_priv {
 	/* Needs special handling, cannot use ahci_platform */
 	struct clk		   *sata_clk;
 	struct tegra_pmc	   *pmc;
-	struct regulator_bulk_data *supplies;
 	const struct tegra_ahci_soc *soc;
+	/* this must be the last member */
+	struct regulator_bulk_data supplies[];
 };
 
 static void tegra_ahci_handle_quirks(struct ahci_host_priv *hpriv)
@@ -514,6 +515,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;
@@ -523,14 +525,15 @@ static int tegra_ahci_probe(struct platform_device *pdev)
 	if (IS_ERR(hpriv))
 		return PTR_ERR(hpriv);
 
-	tegra = devm_kzalloc_obj(&pdev->dev, *tegra);
+	soc = of_device_get_match_data(&pdev->dev);
+	tegra = devm_kzalloc_flex(&pdev->dev, *tegra, supplies, soc->num_supplies);
 	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))
@@ -578,11 +581,6 @@ static int tegra_ahci_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, PTR_ERR(tegra->pmc),
 				     "failed to get PMC\n");
 
-	tegra->supplies = devm_kzalloc_objs(&pdev->dev, *tegra->supplies,
-					    tegra->soc->num_supplies);
-	if (!tegra->supplies)
-		return -ENOMEM;
-
 	regulator_bulk_set_supply_names(tegra->supplies,
 					tegra->soc->supply_names,
 					tegra->soc->num_supplies);
Signature · 1 line
2.56.0
maintainer
#1 lore raw

Hello Rosen,

Quoted 1 line · Rosen Penev

Combine allocations into one by using a flexible array member.

Thanks for adding the comment and fixing the subject.

However, the commit log still does not explain the benefit, so my reply to V2 still applies:
https://lore.kernel.org/linux-ide/ah1HD1aOyTnMBEqD@ryzen/

A flexible array member also has disadvantages: nothing can be added after it, and there can be only one per struct.

If we use one anyway, it should be annotated with __counted_by(), as Damien and I asked for in V1. devm_kzalloc_flex() calls __set_flex_counter(), so that should mostly be a matter of adding a counter member.

Since neither Jon nor I am a fan of this change, I will only apply a V4 if Jon explicitly asks for it.

Kind regards,
Niklas