Skip to contentSkip to conversation

phy: mediatek: tphy: simplify main allocation

PATCH Superseded Rosen Penev 3 participants 3 messages Aug 12 – Oct 12

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 phy: mediatek: tphy: simplify main allocation

Use a flexible array member to replace kzalloc + kcalloc with a single kzalloc.

Shuffled some code around as __counted_by requires the counting variable to be assigned first.

Removed NULL check from of_get_match_data as it's never NULL per the device table.

---
v2: add back NULL check

 drivers/phy/mediatek/phy-mtk-tphy.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/phy/mediatek/phy-mtk-tphy.c +7 -10
index acf506529507..e9b9e667b319 100644
@@ -340,10 +340,10 @@ struct mtk_tphy {
 	struct device *dev;
 	void __iomem *sif_base;	/* only shared sif */
 	const struct mtk_phy_pdata *pdata;
-	struct mtk_phy_instance **phys;
-	int nphys;
 	int src_ref_clk; /* MHZ, reference clock for slew rate calibrate */
 	int src_coef; /* coefficient for slew rate calibrate */
+	int nphys;
+	struct mtk_phy_instance *phys[] __counted_by(nphys);
 };
 
 #if IS_ENABLED(CONFIG_DEBUG_FS)
@@ -1572,22 +1572,19 @@ static int mtk_tphy_probe(struct platform_device *pdev)
 	struct mtk_tphy *tphy;
 	struct resource res;
 	int port, ret;
+	size_t nphys;
 
-	tphy = devm_kzalloc(dev, sizeof(*tphy), GFP_KERNEL);
+	nphys = of_get_child_count(np);
+	tphy = devm_kzalloc(dev, struct_size(tphy, phys, nphys), GFP_KERNEL);
 	if (!tphy)
 		return -ENOMEM;
 
+	tphy->nphys = nphys;
+	tphy->dev = dev;
 	tphy->pdata = of_device_get_match_data(dev);
 	if (!tphy->pdata)
 		return -EINVAL;
 
-	tphy->nphys = of_get_child_count(np);
-	tphy->phys = devm_kcalloc(dev, tphy->nphys,
-				       sizeof(*tphy->phys), GFP_KERNEL);
-	if (!tphy->phys)
-		return -ENOMEM;
-
-	tphy->dev = dev;
 	platform_set_drvdata(pdev, tphy);
 
 	sif_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Meaningful code and commit message mismatch regarding the removal of a NULL check. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Meaningful code and commit message mismatch regarding the removal of a NULL check. --

--- Patch [1]: [PATCHv2] phy: mediatek: tphy: simplify main allocation --- commit 293b5d4728ba8a57dc62ffcf5575f966eef04ba8
Author: Rosen Penev <[email protected]>

phy: mediatek: tphy: simplify main allocation

This commit simplifies the main allocation by replacing a separate kzalloc and kcalloc with a single kzalloc using a flexible array member. It also shuffles code to satisfy __counted_by requirements.

Quoted 7 lines

Removed NULL check from of_get_match_data as it's never NULL per the device table.

---
v2: add back NULL check

[Severity: Medium]
This isn't a bug, but the commit message body states the NULL check was removed, while the v2 notes and the patch itself show it was retained. Should the commit message be updated to remove this line so it accurately reflects the applied changes?

Quoted 4 lines
drivers/phy/mediatek/phy-mtk-tphy.c +0 -0
index acf5065295072..e9b9e667b3195 100644

[ ... ]

Quoted 1 line
drivers/phy/mediatek/phy-mtk-tphy.c +0 -0
@@ -1572,22 +1572,19 @@ static int mtk_tphy_probe(struct platform_device *pdev)

[ ... ]

Quoted 6 lines
drivers/phy/mediatek/phy-mtk-tphy.c +2 -0
+	tphy->nphys = nphys;
+	tphy->dev = dev;
 	tphy->pdata = of_device_get_match_data(dev);
 	if (!tphy->pdata)
 		return -EINVAL;
Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

-- 
linux-phy mailing list
[email protected]
https://lists.infradead.org/mailman/listinfo/linux-phy
#2 lore raw
In reply to Rosen Penev
Quoted 9 lines · Rosen Penev

Use a flexible array member to replace kzalloc + kcalloc with a single kzalloc.

Shuffled some code around as __counted_by requires the counting variable to be assigned first.

Removed NULL check from of_get_match_data as it's never NULL per the device table.

This statement is stale in v2 :(

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்