net: fec: Propagate PTP initialization errors
PATCH 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.
Also propagate failures from devm_request_irq() and ptp_clock_register().
Update the function declaration in fec.h accordingly.
Found by manual code inspection.
--- drivers/net/ethernet/freescale/fec.h | 2 +- drivers/net/ethernet/freescale/fec_ptp.c | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-)
drivers/net/ethernet/freescale/fec.h +1 -1
@@ -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_ptp.c +11 -8
@@ -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,29 @@ 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 =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)) { fep->ptp_clock =NULL ;dev_err (&pdev->dev,"ptp_clock_register failed\n" ); +return PTR_ERR (fep->ptp_clock); }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
PATCH 2/2 net: fec: Handle PTP initialization errors in probe
From: bui duc phuc <[email protected]>
Check the return value of fec_ptp_init() and abort the probe if PTP initialization fails.
Only call fec_ptp_stop() when PTP support was initialized to avoid stopping an uninitialized PTP instance during error handling.
Found by manual code inspection.
--- drivers/net/ethernet/freescale/fec_main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/fec_main.c +7 -3
@@ -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)
@@ -5457,7 +5460,8 @@ fec_probe(struct platform_device *pdev) failed_irq:fec_enet_deinit (ndev); failed_init: -fec_ptp_stop (pdev); +if (fep->bufdesc_ex) +fec_ptp_stop (pdev); failed_reset:pm_runtime_put_noidle (&pdev->dev);pm_runtime_disable (&pdev->dev);
Signature · 1 line
2.43.0
Paolo Abeni
maintainerQuoted 14 lines · On 8/20/26 1:14 PM, [email protected] wrote:
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.
Quoted 7 lines
Also propagate failures from
devm_request_irq()andptp_clock_register().Update the function declaration in
fec.haccordingly.Found by manual code inspection.
Signed-off-by bui duc phuc <[email protected]>
## Form letter - net-next-closed
We have already submitted our pull request with net-next material for v7.3, and therefore net-next is closed for new drivers, features, code refactoring and optimizations. We are currently accepting bug fixes only.
Please repost when net-next reopens after Aug 31st.
RFC patches sent for review only are obviously welcome at any time.
See: https://www.kernel.org/doc/html/next/process/maintainer-netdev.html#development-cycle
Wei Fang
maintainerQuoted 17 lines
drivers/net/ethernet/freescale/fec_ptp.c +2
-6-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,29 @@ 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 =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 ) {
drivers/net/ethernet/freescale/fec_ptp.c void fec_ptp_init(struct platform_device *pdev, int · line 793
Quoted 10 lines
drivers/net/ethernet/freescale/fec_ptp.c +5
-2+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);
Why remove the diagnostic log?
drivers/net/ethernet/freescale/fec_ptp.c void fec_ptp_init(struct platform_device *pdev, int irq_idx) · line 799
Quoted 8 lines
drivers/net/ethernet/freescale/fec_ptp.c +2
-0+return ret; } fep->ptp_clock =ptp_clock_register (&fep->ptp_caps, &pdev->dev);if (IS_ERR (fep->ptp_clock)) { fep->ptp_clock =NULL ;dev_err (&pdev->dev,"ptp_clock_register failed\n" ); +return PTR_ERR (fep->ptp_clock);
fep->ptp_clock is set to NULL on the line immediately before PTR_ERR(fep->ptp_clock) is evaluated, so PTR_ERR receives NULL rather than the original ERR_PTR value.
Quoted 10 lines
drivers/net/ethernet/freescale/fec_ptp.c +2
-0}schedule_delayed_work (&fep->time_keep,HZ ); + +return 0 ; }void
fec_ptp_save_state(struct fec_enet_private *fep)
--
2.43.0
Additionally, the second patch can be merged with this patch. There is no need to use two separate patches.
[email protected]
AI reviewRe: [PATCH 2/2] net: fec: Handle PTP initialization errors in probe
In reply to [email protected]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] The patch fixes an unconditional fec_ptp_stop() in the probe error path but misses the identical bug in fec_drv_remove(), leading to a NULL pointer dereference.
- [High] Use-after-free in devm-managed interrupt handlers during driver removal due to premature free_netdev().
- [High] PTP device is exposed to userspace before probe completes, leading to potential bus faults on probe error paths.
- [High] INIT_WORK(&fep->tx_timeout_work) is called after register_netdev(), creating a race window where an uninitialized work queue can be scheduled. --
--- Patch [2]: [PATCH 2/2] net: fec: Handle PTP initialization errors in probe --- commit 41e2a49ed28a7c17df87f2ff80ee17b2b80b3801
Author: bui duc phuc <[email protected]>
net: fec: Handle PTP initialization errors in probe
Check the return value of fec_ptp_init() and abort the probe if PTP initialization fails. Only call fec_ptp_stop() when PTP support was initialized to avoid stopping an uninitialized PTP instance during error handling.
Quoted 5 lines
drivers/net/ethernet/freescale/fec_main.c +0
-0
drivers/net/ethernet/freescale/fec_main.c fec_probe(struct platform_device *pdev) · line 5391
Quoted 10 lines
drivers/net/ethernet/freescale/fec_main.c +5
-2@@ -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; + }
[Severity: High]
This is a pre-existing issue, but does fec_ptp_init() expose the PTP device to userspace before the probe completes?
drivers/net/ethernet/freescale/fec_ptp.c:fec_ptp_init() {
...
fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev);
...
}This makes the character device accessible to userspace before network device registration completes. If register_netdev() fails later in fec_probe(), the error path shuts down hardware clocks.
If userspace accesses the PTP device concurrently during this window (e.g. by issuing an ioctl), could the PTP callbacks attempt to access disabled hardware registers?
Quoted 3 lines
drivers/net/ethernet/freescale/fec_main.c +0
-0ret =fec_enet_init (ndev);if (ret)
[ ... ]
drivers/net/ethernet/freescale/fec_main.c fec_probe(struct platform_device *pdev) · line 5464
Quoted 7 lines
drivers/net/ethernet/freescale/fec_main.c +2
-1@@ -5457,7 +5460,8 @@ fec_probe(struct platform_device *pdev) failed_irq:fec_enet_deinit (ndev); failed_init: -fec_ptp_stop (pdev); +if (fep->bufdesc_ex) +fec_ptp_stop (pdev);
[Severity: High]
This isn't a bug introduced by this patch, but since you are fixing the unconditional call to fec_ptp_stop() here in the probe error path, should we also fix the exact same issue in fec_drv_remove()?
drivers/net/ethernet/freescale/fec_main.c:fec_drv_remove() {
...
cancel_work_sync(&fep->tx_timeout_work);
fec_ptp_stop(pdev);
unregister_netdev(ndev);
...
}When the driver is unbound from a device without PTP support, fec_drv_remove() unconditionally calls fec_ptp_stop(). Since PTP was never initialized, hrtimer_cancel() inside fec_ptp_stop() will read from a zero-initialized timer, dereferencing a null base pointer.
[Severity: High]
This is a pre-existing issue, but does fec_drv_remove() have a use-after-free risk with devm-managed interrupts?
drivers/net/ethernet/freescale/fec_main.c:fec_drv_remove() {
...
fec_enet_deinit(ndev);
free_netdev(ndev);
}The interrupt is requested with devm_request_irq() in fec_probe(), meaning its lifetime extends beyond fec_drv_remove(). However, fec_drv_remove() explicitly calls free_netdev(), freeing the ndev and fep structures.
If a spurious or shared interrupt fires after free_netdev() returns but before devres executes free_irq(), could the interrupt handler dereference the freed memory?
[Severity: High]
This isn't a bug introduced by this patch, but does the initialization order in fec_probe() create a race window for tx_timeout_work?
drivers/net/ethernet/freescale/fec_main.c:fec_probe() {
...
ret = register_netdev(ndev);
if (ret)
goto failed_register;
...
INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
...
}Calling register_netdev() makes the network device visible to the system, so userspace can configure the interface and start transmitting traffic.
If a hardware stall causes the ndo_tx_timeout callback to be invoked before INIT_WORK() executes, could schedule_work(&fep->tx_timeout_work) operate on an uninitialized work_struct?
Quoted 3 lines
drivers/net/ethernet/freescale/fec_main.c +0
-0failed_reset:pm_runtime_put_noidle (&pdev->dev);pm_runtime_disable (&pdev->dev);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewThank 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:
New issues:
- [Medium] fec_ptp_init() incorrectly returns 0 (success) instead of propagating the actual error code when ptp_clock_register() fails.
Pre-existing issues:
- [High] Concurrent execution of the PPS interrupt handler and device teardown causes a use-after-free on fep->ptp_clock. --
--- Patch [1]: [PATCH 1/2] net: fec: Propagate PTP initialization errors --- commit 72db297960b04e49a3fe9e579f0ca646d2fb313b
Author: bui duc phuc <[email protected]>
net: fec: Propagate PTP initialization errors
Change fec_ptp_init() to return an error code instead of silently ignoring failures during PTP initialization.
Quoted 4 lines
drivers/net/ethernet/freescale/fec_ptp.c +0
-0
[ ... ]
Quoted 1 line
drivers/net/ethernet/freescale/fec_ptp.c +0
-0@@ -779,26 +779,29 @@ void fec_ptp_init(struct platform_device *pdev, int irq_idx)
[ ... ]
drivers/net/ethernet/freescale/fec_ptp.c void fec_ptp_init(struct platform_device *pdev, int irq_idx) · line 793
Quoted 8 lines
drivers/net/ethernet/freescale/fec_ptp.c +1
-2if (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; }
[Severity: High]
This is a pre-existing issue, but is it safe to use devm_request_irq() here without synchronizing during teardown?
During module removal, fec_drv_remove() calls fec_ptp_stop(), which sets fep->pps_enable = 0 and unregisters fep->ptp_clock. However, since the interrupt is managed by devres, it isn't freed or synchronized until after the remove function completes.
If the interrupt fires concurrently on another CPU:
CPU 1 (fec_ptp_stop)
ptp_clock_unregister(fep->ptp_clock);
CPU 2 (fec_pps_interrupt)
if (fep->pps_enable) {
event.type = PTP_CLOCK_PPS;
ptp_clock_event(fep->ptp_clock, &event);
}If CPU 2 reads fep->pps_enable as true just before CPU 1 clears it, could it then dereference the freed fep->ptp_clock?
drivers/net/ethernet/freescale/fec_ptp.c void fec_ptp_init(struct platform_device *pdev, int irq_idx) · line 799
Quoted 6 lines
drivers/net/ethernet/freescale/fec_ptp.c +1
-0fep->ptp_clock =ptp_clock_register (&fep->ptp_caps, &pdev->dev);if (IS_ERR (fep->ptp_clock)) { fep->ptp_clock =NULL ;dev_err (&pdev->dev,"ptp_clock_register failed\n" ); +return PTR_ERR (fep->ptp_clock);
[Severity: Medium]
Since fep->ptp_clock is set to NULL on the line directly above this one, doesn't PTR_ERR(fep->ptp_clock) evaluate to 0 here?
This would cause the function to return success instead of propagating the negative error code from ptp_clock_register().
Quoted 3 lines
drivers/net/ethernet/freescale/fec_ptp.c +0
-0}schedule_delayed_work (&fep->time_keep,HZ );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi Wei Fang,
Thank you for your review.
Quoted 9 lines
drivers/net/ethernet/freescale/fec_ptp.c void fec_ptp_init(struct platform_device *pdev, int irq_idx) · line 792
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);Why remove the diagnostic log?
The reason is that the error is already logged deeper in the call chain, after devm_request_irq() goes through several layers, with sufficient details:
-----------------------------------------------------------------------------------------
return dev_err_probe(dev, rc, "request_irq(%u) %ps %ps %s\n",
irq, handler, thread_fn, devname ? : "");
-----------------------------------------------------------------------------------------
Therefore, I removed the diagnostic in this function to avoid duplicate error messages.
If you prefer, I can keep it.
Quoted 9 lines
Quoted 4 lines
if (IS_ERR(fep->ptp_clock)) { fep->ptp_clock = NULL; dev_err(&pdev->dev, "ptp_clock_register failed\n");+ return
PTR_ERR(fep->ptp_clock);
fep->ptp_clockis set to NULL on the line immediately beforePTR_ERR(fep->ptp_clock)is evaluated, soPTR_ERRreceives NULL rather than the originalERR_PTRvalue.
You're right. This was an oversight on my part.
The error value should be saved before clearing fep->ptp_clock:
---------------------------------------------------------------------------
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;
}--------------------------------------------------------------------------
Quoted 4 lines
Additionally, the second patch can be merged with this patch. There is no need to use two separate patches.
The error propagation part in the second patch is directly related to the first patch :
------------------------------------------------------------
- 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;
+ }
------------------------------------------------------------
So I agree that this part can be merged into the first patch. However, the change in failed_init is an independent pre-existing bug:
-------------------------------------------------
failed_init:
- fec_ptp_stop(pdev);
+ if (fep->bufdesc_ex)
+ fec_ptp_stop(pdev);
-------------------------------------------------
Therefore, I think it would be better to keep this part as a separate patch so that the Fixes: tag can correctly identify the commit that introduced the bug.
What do you think?
Best regards,
Phuc
Wei Fang
maintainerQuoted 21 lines
Quoted 2 lines
Quoted 2 lines
+ if (irq > 0) { ret = devm_request_irq(&pdev->dev, irq,
fec_pps_interrupt,Quoted 3 lines
Quoted 3 lines
0, pdev->name, ndev); if (ret < 0)- dev_warn(&pdev->dev, "request for pps irq
failed(%d)\n",
Quoted 4 lines
Quoted 1 line
- ret);
Why remove the diagnostic log?
The reason is that the error is already logged deeper in the call chain, after
devm_request_irq()goes through several layers, with sufficient details:-----------------------------------------------------------------------------------------
return dev_err_probe(dev, rc, "request_irq(%u)%ps %ps %s\n",
irq, handler,thread_fn, devname ? :
"");
-----------------------------------------------------------------------------------------
Quoted 3 lines
Therefore, I removed the diagnostic in this function to avoid duplicate error messages.
If you prefer, I can keep it.
That makes sense, you'd better add a description for removing the log in the commit message. :)
Quoted 18 lines
Quoted 4 lines
Additionally, the second patch can be merged with this patch. There is no need to use two separate patches.
The error propagation part in the second patch is directly related to the first patch :
------------------------------------------------------------
(quoted diff) +5
-3- 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; + } ------------------------------------------------------------
Quoted 1 line
So I agree that this part can be merged into the first patch.
Yes, this patch and the above part should be in a patch. And it should target to net-next tree.
Quoted 1 line
However, the change in
failed_initis an independent pre-existing bug:
You should add a separate patch to fix the pre-existing issue, and the patch targets to net tree. Patches to net tree are welcome at any time. And you need to add the target tree in the subject so that netdev maintainers know which git tree the patch should be applied to. For example:
[PATCH net] -> net tree
[PATCH net-next] -> net-next treeSee: https://elixir.bootlin.com/linux/v7.2/source/Documentation/process/maintainer-netdev.rst#L61
Quoted 8 lines
-------------------------------------------------
failed_init:(quoted diff) +2
-2- fec_ptp_stop(pdev); + if (fep->bufdesc_ex) + fec_ptp_stop(pdev); -------------------------------------------------
Quoted 4 lines
Therefore, I think it would be better to keep this part as a separate patch so that the Fixes: tag can correctly identify the commit that introduced the bug.
What do you think?
I do not see the Fixes tag in the second patch, please add the Fixes tag and send a separate patch to the net tree.
Hi Wei Fang,
Thank you for your feedback.
Quoted 9 lines
You should add a separate patch to fix the pre-existing issue, and the patch targets to net tree. Patches to net tree are welcome at any time. And you need to add the target tree in the subject so that netdev maintainers know which git tree the patch should be applied to. For example:
[PATCH net] -> net tree [PATCH net-next] -> net-next tree
Thanks for the clarification.
I will split this into two separate patches: one for the main issue addressed by
the current series, which I will send to net-next after August 31, and another patch to fix the pre-existing issue, with a Fixes tag, which I will send to net soon.
Best regards,
Phuc
Quoted 4 lines
New issues:
- [Medium]fec_ptp_init()incorrectly returns 0 (success) instead of propagating the actual error code whenptp_clock_register()fails.
The ptp_clock_register() error handling issue will be addressed when I resend the patch to net-next after August 31.
Quoted 4 lines
Pre-existing issues:
- [High] Concurrent execution of the PPS interrupt handler and device teardown causes a use-after-free onfep->ptp_clock. --
This series fixes the error path when devm_request_irq() fails by propagating the error to the probe function.
The reported race between the PPS interrupt handler and device teardown is a pre-existing issue and is unrelated to the error propagation changes in this series.
I would like to leave this issue out of the current series.