Skip to contentSkip to conversation

phy: renesas: rcar-gen2: three cleanups

PATCH In review Felix Gu 4 participants 9 messages Aug 4 – Oct 3

Subsystems (2)
lore
Share & tools
lore

Felix Gu

author
#0 lore raw

v2 cover phy: renesas: rcar-gen2: three cleanups

---
Changes in v2:
- Merge patch 1 and patch 2 into one series.
- Collect Krzysztof's reviewed-by for patch 1.
- Add patch 3 to use dev_err_probe() in probe function.
- Link to v1: https://patch.msgid.link/[email protected]

To: Vinod Koul <[email protected]>
To: Neil Armstrong <[email protected]>
To: Geert Uytterhoeven <[email protected]>
To: Magnus Damm <[email protected]>
To: Krzysztof Kozlowski <[email protected]>
To: Kishon Vijay Abraham I <[email protected]>
To: Sergei Shtylyov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
Felix Gu (3):
phy: renesas: rcar-gen2: Fix double of_node_put on phy creation failure
phy: renesas: rcar-gen2: Return -EINVAL for out-of-range channel reg
phy: renesas: rcar-gen2: Use dev_err_probe() in probe

 drivers/phy/renesas/phy-rcar-gen2.c | 36 ++++++++++++++----------------------
 1 file changed, 14 insertions(+), 22 deletions(-)

---
base-commit: 415606a7be939835db9b0d6b711887586646346d
change-id: 20260803-rcar-gen2-140f014cd384

Best regards,

Signature · 1 line
Felix Gu <[email protected]>
author
#1 lore raw

PATCH v2 1/3 phy: renesas: rcar-gen2: Fix double of_node_put on phy creation failure

for_each_child_of_node_scoped() releases the node reference on scope exit, so the explicit of_node_put(np) in the devm_phy_create() error path drops it twice.

Drop the redundant of_node_put() and let the scoped cleanup handle it.

Fixes b64b32791fb5 ("phy: renesas: rcar-gen2: Simplify with scoped for each OF child loop")
---
 drivers/phy/renesas/phy-rcar-gen2.c | 1 -
 1 file changed, 1 deletion(-)
drivers/phy/renesas/phy-rcar-gen2.c +0 -1
index 6c671254c625..5a272e25e051 100644
@@ -405,7 +405,6 @@ static int rcar_gen2_phy_probe(struct platform_device *pdev)
 						   data->gen2_phy_ops);
 			if (IS_ERR(phy->phy)) {
 				dev_err(dev, "Failed to create PHY\n");
-				of_node_put(np);
 				return PTR_ERR(phy->phy);
 			}
 			phy_set_drvdata(phy->phy, phy);
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/3] phy: renesas: rcar-gen2: Fix double of_node_put on phy creation failure

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

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

Pre-existing issues:
- [High] Missing reference count increment for device node leads to a dangling pointer.
- [High] TOCTOU race condition between child count and iteration leads to out-of-bounds array write. --

--- Patch [1]: [PATCH v2 1/3] phy: renesas: rcar-gen2: Fix double of_node_put on phy creation failure --- commit 047745265c7b61da44949752ecc277b2156237a3
Author: Felix Gu <[email protected]>

phy: renesas: rcar-gen2: Fix double of_node_put on phy creation failure

This commit fixes a double of_node_put() issue in the error path of devm_phy_create(). The scoped iterator for_each_child_of_node_scoped() already handles dropping the node reference, so the explicit of_node_put() was redundant.

drivers/phy/renesas/phy-rcar-gen2.c static int rcar_gen2_phy_probe(struct platform_device *pdev) · line 407

Quoted 8 lines
drivers/phy/renesas/phy-rcar-gen2.c +0 -0
index 6c671254c6258..5a272e25e051c 100644
@@ -405,7 +405,6 @@ static int rcar_gen2_phy_probe(struct platform_device *pdev)
 						   data->gen2_phy_ops);
 			if (IS_ERR(phy->phy)) {
 				dev_err(dev, "Failed to create PHY\n");

[Severity: High]
This is a pre-existing issue, but does this code leave a dangling pointer when saving the node?

Looking at rcar_gen2_phy_probe(), we save np to channel->of_node:

	for_each_child_of_node_scoped(dev->of_node, np) {
		struct rcar_gen2_channel *channel = drv->channels + i;
		...
		channel->of_node = np;

Because for_each_child_of_node_scoped() automatically drops the reference on the node when advancing or exiting, channel->of_node becomes a dangling pointer.

Later in rcar_gen2_phy_xlate(), this potentially dangling pointer is used for comparison:

	if (np == drv->channels[i].of_node)

Could this falsely match a newly allocated node due to pointer aliasing if a DT overlay is applied and removed? Should we call of_node_get() before saving it to channel->of_node?

[Severity: High]
This is also a pre-existing issue, but is there a potential out-of-bounds write if the number of child nodes changes?

Earlier in rcar_gen2_phy_probe(), the driver allocates the channels array based on the child count:

	drv->num_channels = of_get_child_count(dev->of_node);
	drv->channels = devm_kcalloc(dev, drv->num_channels, ...);

Then it iterates over the children and increments i:

	for_each_child_of_node_scoped(dev->of_node, np) {
		struct rcar_gen2_channel *channel = drv->channels + i;
		...
		i++;
	}

If child nodes are added dynamically (e.g., via a DT overlay) between the call to of_get_child_count() and the for_each_child_of_node_scoped() loop, could i exceed drv->num_channels and cause an out-of-bounds write when accessing drv->channels + i?

Quoted 4 lines
drivers/phy/renesas/phy-rcar-gen2.c +0 -1
-				of_node_put(np);
 				return PTR_ERR(phy->phy);
 			}
 			phy_set_drvdata(phy->phy, phy);
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
author
#3 lore raw

PATCH v2 2/3 phy: renesas: rcar-gen2: Return -EINVAL for out-of-range channel reg

In reply to Felix Gu

When of_property_read_u32() succeeds but channel_num exceeds data->num_channels, rcar_gen2_phy_probe() returns error which is 0, so probe reports success even though no PHY provider is registered.

Return -EINVAL in that case.

Fixes 1233f59f745b ("phy: Renesas R-Car Gen2 PHY driver")
---
 drivers/phy/renesas/phy-rcar-gen2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/phy/renesas/phy-rcar-gen2.c +1 -1
index 5a272e25e051..b18727ed41a1 100644
@@ -390,7 +390,7 @@ static int rcar_gen2_phy_probe(struct platform_device *pdev)
 		error = of_property_read_u32(np, "reg", &channel_num);
 		if (error || channel_num >= data->num_channels) {
 			dev_err(dev, "Invalid \"reg\" property\n");
-			return error;
+			return error ?: -EINVAL;
 		}
 		channel->select_mask = select_mask[channel_num];
 
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v2 2/3] phy: renesas: rcar-gen2: Return -EINVAL for out-of-range channel reg

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

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

Pre-existing issues:
- [High] Pointer to device node 'np' is stored in 'channel->of_node' without holding a reference, leading to a dangling pointer.
- [High] Race condition in probe: PHY provider is registered before driver data is set.
- [High] The return value of clk_prepare_enable() is ignored, leading to a potential system lockup if the clock fails to enable.
- [Medium] Holding a spinlock with interrupts disabled for 340 microseconds violates kernel latency constraints. --

--- Patch [2]: [PATCH v2 2/3] phy: renesas: rcar-gen2: Return -EINVAL for out-of-range channel reg --- commit b403c6500188baed7349ae963beab9f0eda61eb2
Author: Felix Gu <[email protected]>

phy: renesas: rcar-gen2: Return -EINVAL for out-of-range channel reg

When of_property_read_u32() succeeds but channel_num exceeds data->num_channels, rcar_gen2_phy_probe() returns error which is 0, so probe reports success even though no PHY provider is registered.

Return -EINVAL in that case.

Fixes 1233f59f745b ("phy: Renesas R-Car Gen2 PHY driver")

drivers/phy/renesas/phy-rcar-gen2.c static int rcar_gen2_phy_probe(struct platform_device *pdev) · line 396

Quoted 13 lines
drivers/phy/renesas/phy-rcar-gen2.c +1 -1
index 5a272e25e051c..b18727ed41a10 100644
@@ -390,7 +390,7 @@ static int rcar_gen2_phy_probe(struct platform_device *pdev)
 		error = of_property_read_u32(np, "reg", &channel_num);
 		if (error || channel_num >= data->num_channels) {
 			dev_err(dev, "Invalid \"reg\" property\n");
-			return error;
+			return error ?: -EINVAL;
 		}
 		channel->select_mask = select_mask[channel_num];
 

While reviewing this patch, I noticed a few pre-existing issues in this file that might be worth addressing:

In rcar_gen2_phy_probe():

Quoted 6 lines
	for_each_child_of_node_scoped(dev->of_node, np) {
		struct rcar_gen2_channel *channel = drv->channels + i;
		u32 channel_num;
		int error, n;

		channel->of_node = np;

[Severity: High]
This isn't a bug introduced by this patch, but the device node pointer 'np' is assigned to the persistent channel structure without taking a reference. Since 'for_each_child_of_node_scoped' automatically drops the reference on loop exit, does this leave a dangling pointer in 'channel->of_node'? Later, rcar_gen2_phy_xlate() compares 'np == drv->channels[i].of_node', which might lead to an ABA problem if the node is freed and reallocated. Should we call of_node_get() here?

Later in rcar_gen2_phy_probe():

Quoted 7 lines
	provider = devm_of_phy_provider_register(dev, rcar_gen2_phy_xlate);
	if (IS_ERR(provider)) {
		dev_err(dev, "Failed to register PHY provider\n");
		return PTR_ERR(provider);
	}

	dev_set_drvdata(dev, drv);

[Severity: High]
This is also a pre-existing issue, but is there a race condition here during probe? We register the PHY provider and expose it to consumers before we initialize the driver data with dev_set_drvdata(). If a consumer driver tries to acquire the PHY right after devm_of_phy_provider_register(), rcar_gen2_phy_xlate() could execute, call dev_get_drvdata(), and receive NULL.

In rcar_gen2_phy_init():

Quoted 4 lines
	clk_prepare_enable(drv->clk);

	spin_lock_irqsave(&drv->lock, flags);
	ugctrl2 = readl(drv->base + USBHS_UGCTRL2);

[Severity: High]
This is a pre-existing issue, but does this code need to check the return value of clk_prepare_enable()? If the clock fails to enable, we proceed to immediately access the hardware registers which could lock up the system or cause a synchronous external abort.

In rz_g1c_phy_power_on():

Quoted 9 lines
	spin_lock_irqsave(&drv->lock, flags);

	/* Power on USBHS PHY */
	value = readl(base + USBHS_UGCTRL);
	value &= ~USBHS_UGCTRL_PLLRESET;
	writel(value, base + USBHS_UGCTRL);

	/* As per the data sheet wait 340 micro sec for power stable */
	udelay(340);

[Severity: Medium]
This is a pre-existing issue, but is it safe to hold a spinlock with interrupts disabled for 340 microseconds here? This seems like it would severely impact system latency, potentially causing missed interrupts.

Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2

-- 
linux-phy mailing list
[email protected]
https://lists.infradead.org/mailman/listinfo/linux-phy
author
#5 lore raw

PATCH v2 3/3 phy: renesas: rcar-gen2: Use dev_err_probe() in probe

In reply to Felix Gu

Convert the error paths in rcar_gen2_phy_probe() to dev_err_probe().

---
 drivers/phy/renesas/phy-rcar-gen2.c | 35 ++++++++++++++---------------------
 1 file changed, 14 insertions(+), 21 deletions(-)
drivers/phy/renesas/phy-rcar-gen2.c +14 -21
index b18727ed41a1..581f6768e2bb 100644
@@ -342,17 +342,13 @@ static int rcar_gen2_phy_probe(struct platform_device *pdev)
 	const struct rcar_gen2_phy_data *data;
 	int i = 0;
 
-	if (!dev->of_node) {
-		dev_err(dev,
-			"This driver is required to be instantiated from device tree\n");
-		return -EINVAL;
-	}
+	if (!dev->of_node)
+		return dev_err_probe(dev, -EINVAL,
+				     "This driver is required to be instantiated from device tree\n");
 
 	clk = devm_clk_get(dev, "usbhs");
-	if (IS_ERR(clk)) {
-		dev_err(dev, "Can't get USBHS clock\n");
-		return PTR_ERR(clk);
-	}
+	if (IS_ERR(clk))
+		return dev_err_probe(dev, PTR_ERR(clk), "Can't get USBHS clock\n");
 
 	base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(base))
@@ -388,10 +384,9 @@ static int rcar_gen2_phy_probe(struct platform_device *pdev)
 		channel->selected_phy = -1;
 
 		error = of_property_read_u32(np, "reg", &channel_num);
-		if (error || channel_num >= data->num_channels) {
-			dev_err(dev, "Invalid \"reg\" property\n");
-			return error ?: -EINVAL;
-		}
+		if (error || channel_num >= data->num_channels)
+			return dev_err_probe(dev, error ?: -EINVAL,
+					     "Invalid \"reg\" property\n");
 		channel->select_mask = select_mask[channel_num];
 
 		for (n = 0; n < PHYS_PER_CHANNEL; n++) {
@@ -403,10 +398,9 @@ static int rcar_gen2_phy_probe(struct platform_device *pdev)
 
 			phy->phy = devm_phy_create(dev, NULL,
 						   data->gen2_phy_ops);
-			if (IS_ERR(phy->phy)) {
-				dev_err(dev, "Failed to create PHY\n");
-				return PTR_ERR(phy->phy);
-			}
+			if (IS_ERR(phy->phy))
+				return dev_err_probe(dev, PTR_ERR(phy->phy),
+						     "Failed to create PHY\n");
 			phy_set_drvdata(phy->phy, phy);
 		}
 
@@ -414,10 +408,9 @@ static int rcar_gen2_phy_probe(struct platform_device *pdev)
 	}
 
 	provider = devm_of_phy_provider_register(dev, rcar_gen2_phy_xlate);
-	if (IS_ERR(provider)) {
-		dev_err(dev, "Failed to register PHY provider\n");
-		return PTR_ERR(provider);
-	}
+	if (IS_ERR(provider))
+		return dev_err_probe(dev, PTR_ERR(provider),
+				     "Failed to register PHY provider\n");
 
 	dev_set_drvdata(dev, drv);
 
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v2 3/3] phy: renesas: rcar-gen2: Use dev_err_probe() in probe

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

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

Pre-existing issues:
- [High] The PHY provider is registered before the driver data is set, creating a race window where PHY lookup can fail.
- [High] The driver saves a scoped device node pointer into a persistent structure without incrementing its reference count, leading to a dangling pointer.
- [High] A Time-Of-Check to Time-Of-Use (TOCTOU) race condition with device tree modifications can lead to an array out-of-bounds access. --

--- Patch [3]: [PATCH v2 3/3] phy: renesas: rcar-gen2: Use dev_err_probe() in probe --- commit 709d9b8218087abc6526b29b382856eb53ccb3ba
Author: Felix Gu <[email protected]>

phy: renesas: rcar-gen2: Use dev_err_probe() in probe

Convert the error paths in rcar_gen2_phy_probe() to dev_err_probe().

Quoted 4 lines
drivers/phy/renesas/phy-rcar-gen2.c +0 -0
index b18727ed41a10..581f6768e2bb2 100644

[ ... ]

drivers/phy/renesas/phy-rcar-gen2.c static int rcar_gen2_phy_probe(struct platform_device *pdev) · line 384

Quoted 2 lines
drivers/phy/renesas/phy-rcar-gen2.c +0 -0
@@ -388,10 +384,9 @@ static int rcar_gen2_phy_probe(struct platform_device *pdev)
 		channel->selected_phy = -1;

[Severity: High]
This isn't a bug introduced by this patch, but could the assignment to channel->of_node just above this line result in a dangling pointer?

Looking at rcar_gen2_phy_probe(), the loop uses for_each_child_of_node_scoped(dev->of_node, np) which automatically decrements the reference count of np at the end of each iteration.

Since channel->of_node = np stores this pointer into the persistent channel structure without calling of_node_get(np), wouldn't this leave channel->of_node pointing to freed memory once the scope ends?

[Severity: High]
This isn't a bug introduced by this patch, but is there a risk of an out-of-bounds array access in this loop?

Earlier in rcar_gen2_phy_probe(), drv->channels is allocated based on of_get_child_count():

    drv->num_channels = of_get_child_count(dev->of_node);
    drv->channels = devm_kcalloc(dev, drv->num_channels, ...);

If a dynamic device tree overlay adds a child node concurrently before or during the for_each_child_of_node_scoped() loop, the loop will iterate more times than drv->num_channels. Would the assignment channel = drv->channels + i then write past the end of the allocation?

Quoted 9 lines
drivers/phy/renesas/phy-rcar-gen2.c +3 -4
 		error = of_property_read_u32(np, "reg", &channel_num);
-		if (error || channel_num >= data->num_channels) {
-			dev_err(dev, "Invalid \"reg\" property\n");
-			return error ?: -EINVAL;
-		}
+		if (error || channel_num >= data->num_channels)
+			return dev_err_probe(dev, error ?: -EINVAL,
+					     "Invalid \"reg\" property\n");
 		channel->select_mask = select_mask[channel_num];

[ ... ]

drivers/phy/renesas/phy-rcar-gen2.c static int rcar_gen2_phy_probe(struct platform_device *pdev) · line 415

Quoted 13 lines
drivers/phy/renesas/phy-rcar-gen2.c +3 -4
@@ -414,10 +408,9 @@ static int rcar_gen2_phy_probe(struct platform_device *pdev)
 	}
 
 	provider = devm_of_phy_provider_register(dev, rcar_gen2_phy_xlate);
-	if (IS_ERR(provider)) {
-		dev_err(dev, "Failed to register PHY provider\n");
-		return PTR_ERR(provider);
-	}
+	if (IS_ERR(provider))
+		return dev_err_probe(dev, PTR_ERR(provider),
+				     "Failed to register PHY provider\n");
 
 	dev_set_drvdata(dev, drv);

[Severity: High]
This isn't a bug introduced by this patch, but does the registration order create a race condition?

The PHY provider is registered via devm_of_phy_provider_register() before dev_set_drvdata(dev, drv) is called.

If a concurrent deferred probe or module load calls rcar_gen2_phy_xlate() during this window, it will try to access driver data that hasn't been set yet. Should dev_set_drvdata() be called before exposing the PHY provider to the system?

Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3

-- 
linux-phy mailing list
[email protected]
https://lists.infradead.org/mailman/listinfo/linux-phy
maintainer
#8 lore raw
In reply to Felix Gu

On 04-08-26, 23:24, Felix Gu wrote:

Please describe the changes and motivation in cover. Also this doesnt apply for me, pls rebase

Signature · 1 line
~Vinod