Skip to contentSkip to conversation

ata: ahci_tegra: remove devm_kcalloc()

PATCH In review Rosen Penev 3 participants 3 messages May 30 – Jun 1

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

PATCH v2 1/1 ata: ahci_tegra: remove devm_kcalloc()

Combine allocations into one by using a flexible array member.

---
v2: Rebase

 drivers/ata/ahci_tegra.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/ata/ahci_tegra.c +6 -9
index 554f05e09f98..142ba2ae1c83 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;
+
+	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(&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))
@@ -578,12 +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_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.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Unchecked dereference of soc when retrieving soc->num_supplies during memory allocation, leading to an immediate kernel panic if of_device_get_match_data() returns NULL. --

commit bf92e6df451c5a93edb05970118c13a24829ffcf
Author: Rosen Penev <[email protected]>

ata: ahci_tegra: remove devm_kcalloc()

This commit combines the allocation for the tegra_ahci_priv structure and its supplies flexible array into a single allocation to remove the use of devm_kcalloc().

Quoted 4 lines
drivers/ata/ahci_tegra.c +0 -0
index 554f05e09f983..142ba2ae1c831 100644

[ ... ]

Quoted 9 lines
drivers/ata/ahci_tegra.c +1 -0
@@ -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;

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

Quoted 6 lines
drivers/ata/ahci_tegra.c +2 -1
@@ -523,14 +525,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);

[Severity: High]
This isn't a bug introduced by this patch, but can of_device_get_match_data() return NULL if the driver is probed via sysfs bind with driver_override?

If it returns NULL, the immediate dereference of soc->num_supplies in the struct_size() macro will cause a NULL pointer dereference. While this vulnerability existed in the codebase before this patch, this patch shifts the dereference earlier in the probe sequence.

Could a NULL check be added for soc before it is dereferenced?

Quoted 3 lines
drivers/ata/ahci_tegra.c +0 -0
 	if (!tegra)
 		return -ENOMEM;
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Rosen Penev
Quoted 5 lines · Rosen Penev

Combine allocations into one by using a flexible array member.

---
v2: Rebase

V1: https://lore.kernel.org/linux-ide/[email protected]/

Jon Hunter said that he (in addition to myself) was not a fan of this:
https://lore.kernel.org/linux-ide/[email protected]/

Since there are no changes since V1 (except for a rebase), and no new motivation/reasoning in the commit log, I intend to ignore this patch, unless Jon feels otherwise.

Kind regards,
Niklas