Skip to contentSkip to conversation

net: fec: Propagate PTP initialization errors

PATCH Superseded [email protected] 4 participants 7 messages Sep 9 – Sep 11

Subsystems (4)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/2 net: fec: Propagate PTP initialization errors

From: bui duc phuc <[email protected]>

Change fec_ptp_init() to return an error code instead of silently ignoring failures during PTP initialization.

The PPS IRQ is not required for the FEC/PTP functionality, so its absence should not make the probe fail. However, an unavailable optional IRQ should be distinguished from an actual error returned during the IRQ lookup.

If a platform does not support the PPS IRQ, it can omit the IRQ from its device tree and the optional lookup will return -ENXIO. Propagate other errors from the IRQ lookup instead of silently ignoring them. In particular, silently ignoring -EPROBE_DEFER can cause the driver to continue probing instead of deferring as required.

Also propagate failures from devm_request_irq() and ptp_clock_register().

Update the function declaration in fec.h accordingly.

Found by manual code inspection.

Fixes b86bcb299092 ("net: fec_ptp: Use platform_get_irq_xxx_optional() to avoid error message")

---

Link v1:
https://lore.kernel.org/all/[email protected]/
Link v2:
https://lore.kernel.org/all/[email protected]/
Link v3:
https://lore.kernel.org/all/[email protected]/
Link v4:
https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Squash the two patches into one.
- Add error handling for platform_get_irq_byname_optional().
- Fix error handling for ptp_clock_register().
Changes in v3:
- Retarget the patch to the net tree instead of net-next.
- Add error handling for platform_get_irq_byname_optional() in fec_probe() for the fec_enet_interrupt IRQ.
Changes in v4:
- Use a goto label for cleanup instead of returning directly on errors from platform_get_irq_byname_optional() in probe().
- Update the commit message.
Changes in v5:
- Split the optional IRQ handling in fec_main.c into a separate patch.

 drivers/net/ethernet/freescale/fec.h      |  2 +-
 drivers/net/ethernet/freescale/fec_main.c |  7 +++++--
 drivers/net/ethernet/freescale/fec_ptp.c  | 22 ++++++++++++++--------
 3 files changed, 20 insertions(+), 11 deletions(-)
drivers/net/ethernet/freescale/fec.h +1 -1
index 7176803146f3..8831da37326b 100644
@@ -692,7 +692,7 @@ struct fec_enet_private {
 	u64 ethtool_stats[];
 };
 
-void fec_ptp_init(struct platform_device *pdev, int irq_idx);
+int fec_ptp_init(struct platform_device *pdev, int irq_idx);
 void fec_ptp_restore_state(struct fec_enet_private *fep);
 void fec_ptp_save_state(struct fec_enet_private *fep);
 void fec_ptp_stop(struct platform_device *pdev);
drivers/net/ethernet/freescale/fec_main.c +5 -2
index 794ec427b0ee..c6e29b5c2abb 100644
@@ -5384,8 +5384,11 @@ fec_probe(struct platform_device *pdev)
 		goto failed_reset;
 
 	irq_cnt = fec_enet_get_irq_cnt(pdev);
-	if (fep->bufdesc_ex)
-		fec_ptp_init(pdev, irq_cnt);
+	if (fep->bufdesc_ex) {
+		ret = fec_ptp_init(pdev, irq_cnt);
+		if (ret)
+			goto failed_reset;
+	}
 
 	ret = fec_enet_init(ndev);
 	if (ret)
drivers/net/ethernet/freescale/fec_ptp.c +14 -8
index 56801c2009d5..557a76797eba 100644
@@ -736,7 +736,7 @@ static irqreturn_t fec_pps_interrupt(int irq, void *dev_id)
  * cyclecounter init routine and exits.
  */
 
-void fec_ptp_init(struct platform_device *pdev, int irq_idx)
+int fec_ptp_init(struct platform_device *pdev, int irq_idx)
 {
 	struct net_device *ndev = platform_get_drvdata(pdev);
 	struct fec_enet_private *fep = netdev_priv(ndev);
@@ -779,26 +779,32 @@ void fec_ptp_init(struct platform_device *pdev, int irq_idx)
 		      HRTIMER_MODE_REL);
 
 	irq = platform_get_irq_byname_optional(pdev, "pps");
-	if (irq < 0)
+	if (irq < 0 && irq != -ENXIO)
+		return irq;
+	if (irq == -ENXIO) {
 		irq = platform_get_irq_optional(pdev, irq_idx);
-	/* Failure to get an irq is not fatal,
-	 * only the PTP_CLOCK_PPS clock events should stop
-	 */
-	if (irq >= 0) {
+		if (irq < 0 && irq != -ENXIO)
+			return irq;
+	}
+
+	if (irq > 0) {
 		ret = devm_request_irq(&pdev->dev, irq, fec_pps_interrupt,
 				       0, pdev->name, ndev);
 		if (ret < 0)
-			dev_warn(&pdev->dev, "request for pps irq failed(%d)\n",
-				 ret);
+			return ret;
 	}
 
 	fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev);
 	if (IS_ERR(fep->ptp_clock)) {
+		ret = PTR_ERR(fep->ptp_clock);
 		fep->ptp_clock = NULL;
 		dev_err(&pdev->dev, "ptp_clock_register failed\n");
+		return ret;
 	}
 
 	schedule_delayed_work(&fep->time_keep, HZ);
+
+	return 0;
 }
 
 void fec_ptp_save_state(struct fec_enet_private *fep)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 2/2 net: fec: Handle optional IRQ lookup errors correctly

From: bui duc phuc <[email protected]>

Handle errors from platform_get_irq_byname_optional() explicitly while preserving the existing fallback to platform_get_irq() when the named IRQ is not available.

Propagate errors other than -ENXIO from the optional IRQ lookup instead of silently falling back to the indexed IRQ lookup.

In particular, silently ignoring -EPROBE_DEFER can cause the driver to continue probing instead of deferring as required.

Fixes 3b56be218f65 ("net: fec_main: Use platform_get_irq_byname_optional() to avoid error message")
---
 drivers/net/ethernet/freescale/fec_main.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/fec_main.c +8 -3
index c6e29b5c2abb..f7636f23d1eb 100644
@@ -5397,12 +5397,17 @@ fec_probe(struct platform_device *pdev)
 	for (i = 0; i < irq_cnt; i++) {
 		snprintf(irq_name, sizeof(irq_name), "int%d", i);
 		irq = platform_get_irq_byname_optional(pdev, irq_name);
-		if (irq < 0)
-			irq = platform_get_irq(pdev, i);
-		if (irq < 0) {
+		if (irq < 0 && irq != -ENXIO) {
 			ret = irq;
 			goto failed_irq;
 		}
+		if (irq == -ENXIO) {
+			irq = platform_get_irq(pdev, i);
+			if (irq < 0) {
+				ret = irq;
+				goto failed_irq;
+			}
+		}
 		ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
 				       0, pdev->name, ndev);
 		if (ret)
Signature · 1 line
2.43.0
maintainer Reviewed-by on v5 1/2
#3 lore raw
In reply to [email protected]
Quoted 16 lines

Change fec_ptp_init() to return an error code instead of silently ignoring failures during PTP initialization.

The PPS IRQ is not required for the FEC/PTP functionality, so its absence should not make the probe fail. However, an unavailable optional IRQ should be distinguished from an actual error returned during the IRQ lookup.

If a platform does not support the PPS IRQ, it can omit the IRQ from its device tree and the optional lookup will return -ENXIO. Propagate other errors from the IRQ lookup instead of silently ignoring them. In particular, silently ignoring -EPROBE_DEFER can cause the driver to continue probing instead of deferring as required.

Also propagate failures from devm_request_irq() and ptp_clock_register().

Quoted 7 lines

Update the function declaration in fec.h accordingly.

Found by manual code inspection.

Fixes b86bcb299092 ("net: fec_ptp: Use platform_get_irq_xxx_optional() to

avoid error message")

maintainer Reviewed-by on v5 2/2
#2 lore raw

RE: [PATCH net v5 2/2] net: fec: Handle optional IRQ lookup errors correctly

In reply to [email protected]
Quoted 7 lines

Handle errors from platform_get_irq_byname_optional() explicitly while preserving the existing fallback to platform_get_irq() when the named IRQ is not available.

Propagate errors other than -ENXIO from the optional IRQ lookup instead of silently falling back to the indexed IRQ lookup.

Quoted 6 lines

In particular, silently ignoring -EPROBE_DEFER can cause the driver to continue probing instead of deferring as required.

Fixes 3b56be218f65 ("net: fec_main: Use platform_get_irq_byname_optional()

to avoid error message")

#4 lore raw
In reply to [email protected]
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] Use-after-free and crash during probe error paths due to devm_request_irq() lifecycle mismatch with manual free_netdev() and clk_disable_unprepare().
- [High] Missing synchronization in fec_ptp_stop causes a race condition leading to a use-after-free of the ptp_clock structure in the PPS interrupt handler. --

commit 4b37ab3f35f3d2df1a812ad2d83b4453fa8b03f2
Author: bui duc phuc <[email protected]>

net: fec: Propagate PTP initialization errors

This patch changes fec_ptp_init() to return an error code instead of silently ignoring failures during PTP initialization. It ensures that unavailable optional IRQs are handled correctly, and propagates errors from devm_request_irq() and ptp_clock_register().

Link v1: https://lore.kernel.org/all/[email protected]/
Link v2: https://lore.kernel.org/all/[email protected]/
Link v3: https://lore.kernel.org/all/[email protected]/
Link v4: https://lore.kernel.org/all/[email protected]/

Quoted 17 lines
drivers/net/ethernet/freescale/fec_main.c +5 -2
index 794ec427b0ee6..c6e29b5c2abb4 100644
@@ -5384,8 +5384,11 @@ fec_probe(struct platform_device *pdev)
 		goto failed_reset;
 
 	irq_cnt = fec_enet_get_irq_cnt(pdev);
-	if (fep->bufdesc_ex)
-		fec_ptp_init(pdev, irq_cnt);
+	if (fep->bufdesc_ex) {
+		ret = fec_ptp_init(pdev, irq_cnt);
+		if (ret)
+			goto failed_reset;
+	}
 
 	ret = fec_enet_init(ndev);

[ ... ]

Quoted 4 lines
drivers/net/ethernet/freescale/fec_ptp.c +0 -0
index 56801c2009d59..557a76797ebaa 100644

[ ... ]

Quoted 1 line
drivers/net/ethernet/freescale/fec_ptp.c +0 -0
@@ -779,26 +779,32 @@ void fec_ptp_init(struct platform_device *pdev, int irq_idx)

[ ... ]

Quoted 6 lines
drivers/net/ethernet/freescale/fec_ptp.c +1 -2
+	if (irq > 0) {
 		ret = devm_request_irq(&pdev->dev, irq, fec_pps_interrupt,
 				       0, pdev->name, ndev);
 		if (ret < 0)
-			dev_warn(&pdev->dev, "request for pps irq failed(%d)\n",
-				 ret);

drivers/net/ethernet/freescale/fec_ptp.c void fec_ptp_init(struct platform_device *pdev, int irq_idx) · line 803

Quoted 10 lines
drivers/net/ethernet/freescale/fec_ptp.c +3 -0
+			return ret;
 	}
 
 	fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev);
 	if (IS_ERR(fep->ptp_clock)) {
+		ret = PTR_ERR(fep->ptp_clock);
 		fep->ptp_clock = NULL;
 		dev_err(&pdev->dev, "ptp_clock_register failed\n");
+		return ret;
 	}

[Severity: High]
This is a pre-existing issue, but could this new error path trigger a use-after-free or a crash if an interrupt fires during probe teardown?

If ptp_clock_register() fails, we now return an error after the PPS interrupt has already been requested via devm_request_irq().

Back in fec_probe(), this error causes a jump to the teardown path:

    ret = fec_ptp_init(pdev, irq_cnt);
    if (ret)
        goto failed_reset;

The teardown logic manually disables clocks and eventually frees the netdev:

failed_clk_ahb:

    clk_disable_unprepare(fep->clk_ipg);
...
    free_netdev(ndev);
    return ret;

Since the interrupt is managed by devm, it remains active until the probe function fully returns. If the interrupt fires during this window, the fec_pps_interrupt() handler can dereference the freed ndev structure and read unclocked hardware registers.

Is it possible to explicitly release or disable the devm-managed interrupt before returning the error here?

[Severity: High]
This is also a pre-existing issue, but does the teardown logic in fec_ptp_stop() contain a race condition with the PPS interrupt?

When cleaning up, fec_ptp_stop() unregisters the clock without synchronizing against the active PPS interrupt:

    if (fep->pps_enable)
        fec_ptp_enable_pps(fep, 0);
    ...
    if (fep->ptp_clock)
        ptp_clock_unregister(fep->ptp_clock);

Because the IRQ is devm-managed and not explicitly disabled, fec_pps_interrupt() could execute concurrently. It reads fep->pps_enable without a lock or memory barrier, meaning it might see a stale value and dereference fep->ptp_clock after it has been freed.

Would it be safer to disable the interrupt or call synchronize_irq() before unregistering the clock?

Quoted 5 lines
drivers/net/ethernet/freescale/fec_ptp.c +2 -0
 	schedule_delayed_work(&fep->time_keep, HZ);
+
+	return 0;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#5 lore raw
In reply to [email protected]

drivers/net/ethernet/freescale/fec_ptp.c static irqreturn_t fec_pps_interrupt(int irq, void *dev_id) · line 739

Quoted 5 lines · On Wed, 9 Sep 2026 13:47:11 +0700 [email protected] wrote:

* cyclecounter init routine and exits.
*/

drivers/net/ethernet/freescale/fec_ptp.c +1 -1
-void fec_ptp_init(struct platform_device *pdev, int irq_idx)
+int fec_ptp_init(struct platform_device *pdev, int irq_idx)

This function has a kdoc, you need to document the return value now.

Please repost this for net-next without the Fixes tags.

#6 lore raw

Hi Jakub,

Thank you for your review.

Quoted 6 lines

drivers/net/ethernet/freescale/fec_ptp.c static irqreturn_t fec_pps_interrupt(int irq, void *dev_id) · line 739

Quoted 2 lines
drivers/net/ethernet/freescale/fec_ptp.c +1 -1
-void fec_ptp_init(struct platform_device *pdev, int irq_idx)
+int fec_ptp_init(struct platform_device *pdev, int irq_idx)

This function has a kdoc, you need to document the return value now.

Please repost this for net-next without the Fixes tags.

I will update the KDoc and repost this for net-next without the Fixes:
tags soon.

Best regards,
Phuc