net: ravb: fix PTP clock lifetime
Subsystems (4)
v4 cover net: ravb: fix PTP clock lifetime
From: Xuanqiang Luo <[email protected]>
This series fixes RAVB PTP clock lifetime handling. It reports a cached PHC index without accessing the clock pointer and drains PTP interrupts before unregistering the clock.
Patch 1 caches the PHC index and handles registration failures.
Patch 2 detaches the clock with xchg() and drains the PTP IRQs before unregistering it.
---
Changes:
v4:
- Rebase onto Linux 7.2-rc7.
Patch 1:
- Cache the PHC index separately instead of locking clock access. (Vadim Fedorenko)
- Reword the subject and update the commit message for the cached PHC index approach.
- Add Vadim Fedorenko's Reviewed-by tag.
Patch 2:
- Replace priv->lock serialization with PTP interrupt disabling and IRQ draining before unregistering the clock. (Vadim Fedorenko)
- Use READ_ONCE() and WRITE_ONCE() for lockless clock pointer access and xchg() to detach the clock atomically.
- Track the error and management IRQs and synchronize all IRQs that can invoke the PTP handler.
- Update the commit message to describe the interrupt handler race and the new teardown sequence.
v3: https://lore.kernel.org/all/[email protected]/
Patch 1:
- Omit Niklas Söderlund's Reviewed-by tag because the implementation he reviewed has changed.
- Describe the NULL pointer dereference before the first open as the most likely failure mode in the commit message. (Jakub Kicinski, Sashiko)
- Normalize PTP clock registration failures to NULL.
(Jakub Kicinski, Sashiko)
- Keep hardware timestamping capabilities independent of PHC availability. (Jakub Kicinski, Sashiko)
Patch 2 (new):
- Serialize PTP clock publication and access with priv->lock, and detach the clock before unregistering it. (Jakub Kicinski, Sashiko)
v2: https://lore.kernel.org/all/[email protected]/
- Only advertise hardware timestamping support when a PHC is available (Niklas Söderlund).
v1: https://lore.kernel.org/all/[email protected]/
Xuanqiang Luo (2):
net: ravb: avoid dereferencing an invalid PTP clock
net: ravb: serialize PTP clock teardown
drivers/net/ethernet/renesas/ravb.h | 3 ++ drivers/net/ethernet/renesas/ravb_main.c | 9 +++-- drivers/net/ethernet/renesas/ravb_ptp.c | 44 ++++++++++++++++++++---- 3 files changed, 47 insertions(+), 9 deletions(-)
base-commit: db2ddb87143519e20a95aa36c60b36107b736a58
Signature · 1 line
2.43.0
PATCH v4 1/2 net: ravb: avoid dereferencing an invalid PTP clock
From: Xuanqiang Luo <[email protected]>
The PTP clock is unavailable before the first open, so querying its index can dereference a NULL pointer. Registration failures can also leave an error pointer in priv->ptp.clock.
Cache the PHC index separately and report -1 while no clock is registered. Normalize registration errors to NULL and preserve the static timestamping capabilities.
--- drivers/net/ethernet/renesas/ravb.h | 1 + drivers/net/ethernet/renesas/ravb_main.c | 3 ++- drivers/net/ethernet/renesas/ravb_ptp.c | 15 +++++++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-)
drivers/net/ethernet/renesas/ravb.h +1 -0
@@ -1028,6 +1028,7 @@ struct ravb_ptp_perout {struct ravb_ptp {struct ptp_clock *clock;struct ptp_clock_info info; +int phc_index;u32 default_addend;u32 current_addend;int extts[N_EXT_TS ];
drivers/net/ethernet/renesas/ravb_main.c +2 -1
@@ -1779,7 +1779,7 @@ static int ravb_get_ts_info(struct net_device *ndev, (1 <<HWTSTAMP_FILTER_NONE ) | (1 <<HWTSTAMP_FILTER_PTP_V2_L2_EVENT ) | (1 <<HWTSTAMP_FILTER_ALL ); - info->phc_index =ptp_clock_index (priv->ptp.clock); + info->phc_index =READ_ONCE (priv->ptp.phc_index); }return 0 ;
@@ -2953,6 +2953,7 @@ static int ravb_probe(struct platform_device *pdev) priv->rstc = rstc; priv->ndev = ndev; priv->pdev = pdev; + priv->ptp.phc_index =-1 ; priv->num_tx_ring[RAVB_BE ] =BE_TX_RING_SIZE ; priv->num_rx_ring[RAVB_BE ] =BE_RX_RING_SIZE ;if (info->nc_queues) {
drivers/net/ethernet/renesas/ravb_ptp.c +13 -2
@@ -315,6 +315,7 @@ void ravb_ptp_interrupt(struct net_device *ndev)void ravb_ptp_init (struct net_device *ndev,struct platform_device *pdev) {struct ravb_private *priv =netdev_priv (ndev); +struct ptp_clock *clock;unsigned long flags; priv->ptp.info = ravb_ptp_info;
@@ -327,7 +328,15 @@ void ravb_ptp_init(struct net_device *ndev, struct platform_device *pdev)ravb_modify (ndev,GCCR ,GCCR_TCSS ,GCCR_TCSS_ADJGPTP );spin_unlock_irqrestore (&priv->lock, flags); - priv->ptp.clock =ptp_clock_register (&priv->ptp.info, &pdev->dev); + clock =ptp_clock_register (&priv->ptp.info, &pdev->dev); +if (IS_ERR (clock)) { +netdev_err (ndev,"failed to register PTP clock: %pe\n" , clock); + clock =NULL ; + } + + priv->ptp.clock = clock; +if (clock) +WRITE_ONCE (priv->ptp.phc_index,ptp_clock_index (clock)); }void ravb_ptp_stop (struct net_device *ndev)
@@ -337,5 +346,7 @@ void ravb_ptp_stop(struct net_device *ndev)ravb_write (ndev,0 ,GIC );ravb_write (ndev,0 ,GIS ); -ptp_clock_unregister (priv->ptp.clock); +WRITE_ONCE (priv->ptp.phc_index,-1 ); +if (priv->ptp.clock) +ptp_clock_unregister (priv->ptp.clock); }
Signature · 1 line
2.43.0
PATCH v4 2/2 net: ravb: serialize PTP clock teardown
In reply to Xuanqiang LuoFrom: Xuanqiang Luo <[email protected]>
ravb_ptp_interrupt() can race with ravb_ptp_stop() and pass the clock to ptp_clock_event() while ptp_clock_unregister() is freeing it. This can lead to a use-after-free.
Use READ_ONCE() and WRITE_ONCE() for lockless access to the clock pointer. Atomically detach it with xchg() before disabling PTP interrupts, then synchronize all IRQs which can invoke ravb_ptp_interrupt() before unregistering the detached clock.
A handler which read the old pointer completes before the clock is unregistered, while later handlers read NULL and skip the event.
--- drivers/net/ethernet/renesas/ravb.h | 2 ++ drivers/net/ethernet/renesas/ravb_main.c | 6 ++-- drivers/net/ethernet/renesas/ravb_ptp.c | 37 +++++++++++++++++++----- 3 files changed, 35 insertions(+), 10 deletions(-)
drivers/net/ethernet/renesas/ravb.h +2 -0
@@ -1124,6 +1124,8 @@ struct ravb_private {int msg_enable;int speed;int emac_irq; +int err_irq; +int mgmt_irq; unsigned no_avb_link:1; unsigned avb_link_active_low:1;
drivers/net/ethernet/renesas/ravb_main.c +4 -2
@@ -2885,11 +2885,13 @@ static int ravb_setup_irqs(struct ravb_private *priv)return error;if (info->err_mgmt_irqs) { - error =ravb_setup_irq (priv,"err_a" ,"err_a" ,NULL , ravb_multi_interrupt); + error =ravb_setup_irq (priv,"err_a" ,"err_a" , &priv->err_irq, + ravb_multi_interrupt);if (error)return error; - error =ravb_setup_irq (priv,"mgmt_a" ,"mgmt_a" ,NULL , ravb_multi_interrupt); + error =ravb_setup_irq (priv,"mgmt_a" ,"mgmt_a" , &priv->mgmt_irq, + ravb_multi_interrupt);if (error)return error; }
drivers/net/ethernet/renesas/ravb_ptp.c +29 -8
@@ -289,16 +289,17 @@ static const struct ptp_clock_info ravb_ptp_info = {void ravb_ptp_interrupt (struct net_device *ndev) {struct ravb_private *priv =netdev_priv (ndev); +struct ptp_clock *clock =READ_ONCE (priv->ptp.clock);u32 gis =ravb_read (ndev,GIS ); gis &=ravb_read (ndev,GIC ); -if (gis &GIS_PTCF ) { +if ((gis &GIS_PTCF ) && clock) {struct ptp_clock_event event; event.type =PTP_CLOCK_EXTTS ; event.index =0 ; event.timestamp =ravb_read (ndev,GCPT ); -ptp_clock_event (priv->ptp.clock, &event); +ptp_clock_event (clock, &event); }if (gis &GIS_PTMF ) {struct ravb_ptp_perout *perout = priv->ptp.perout;
@@ -334,19 +335,39 @@ void ravb_ptp_init(struct net_device *ndev, struct platform_device *pdev) clock =NULL ; } - priv->ptp.clock = clock; +WRITE_ONCE (priv->ptp.clock, clock);if (clock)WRITE_ONCE (priv->ptp.phc_index,ptp_clock_index (clock)); } -void ravb_ptp_stop (struct net_device *ndev) +static void ravb_ptp_disable (struct net_device *ndev) { -struct ravb_private *priv =netdev_priv (ndev); -ravb_write (ndev,0 ,GIC );ravb_write (ndev,0 ,GIS ); +} + +static void ravb_ptp_sync_irqs (struct net_device *ndev) +{ +struct ravb_private *priv =netdev_priv (ndev); + +synchronize_irq (ndev->irq); +if (priv->info->err_mgmt_irqs) { +synchronize_irq (priv->err_irq); +synchronize_irq (priv->mgmt_irq); + } +} + +void ravb_ptp_stop (struct net_device *ndev) +{ +struct ravb_private *priv =netdev_priv (ndev); +struct ptp_clock *clock;WRITE_ONCE (priv->ptp.phc_index,-1 ); -if (priv->ptp.clock) -ptp_clock_unregister (priv->ptp.clock); + clock =xchg (&priv->ptp.clock,NULL ); + +ravb_ptp_disable (ndev); +ravb_ptp_sync_irqs (ndev); + +if (clock) +ptp_clock_unregister (clock); }
Signature · 1 line
2.43.0
Niklas Söderlund
maintainerHi Xuanqiang,
Thanks for your work.
Quoted 10 lines · Xuanqiang Luo
From: Xuanqiang Luo <[email protected]>
This series fixes RAVB PTP clock lifetime handling. It reports a cached PHC index without accessing the clock pointer and drains PTP interrupts before unregistering the clock.
Patch 1 caches the PHC index and handles registration failures.
Patch 2 detaches the clock with
xchg()and drains the PTP IRQs before unregistering it.
These patches are rather big change adding READ_ONCE() and WRITE_ONCE() to avoid a LLM warning? Or have you hit a real issue? How have you tested this work?
If you have a test-case could you share it? I have a pending series [1] that cleans up the whole RAVB driver ptp management which have grown rather organically. It have a small fix for the missing check of registering the clock. Would it be possible for you to test your work with that series too?
1. https://lore.kernel.org/all/20260811160200.2049987-1-niklas.soderlund%[email protected]/
Quoted 54 lines
---
Changes:
v4:
- Rebase onto Linux 7.2-rc7.
Patch 1:
- Cache the PHC index separately instead of locking clock access. (Vadim Fedorenko)
- Reword the subject and update the commit message for the cached PHC index approach.
- Add Vadim Fedorenko's Reviewed-by tag.
Patch 2:
- Replacepriv->lockserialization with PTP interrupt disabling and IRQ draining before unregistering the clock. (Vadim Fedorenko)
- UseREAD_ONCE()andWRITE_ONCE()for lockless clock pointer access andxchg()to detach the clock atomically.
- Track the error and management IRQs and synchronize all IRQs that can invoke the PTP handler.
- Update the commit message to describe the interrupt handler race and the new teardown sequence.v3: https://lore.kernel.org/all/[email protected]/
Patch 1:
- Omit Niklas Söderlund's Reviewed-by tag because the implementation he reviewed has changed.
- Describe the NULL pointer dereference before the first open as the most likely failure mode in the commit message. (Jakub Kicinski, Sashiko)
- Normalize PTP clock registration failures to NULL.
(Jakub Kicinski, Sashiko)
- Keep hardware timestamping capabilities independent of PHC availability. (Jakub Kicinski, Sashiko)
Patch 2 (new):
- Serialize PTP clock publication and access withpriv->lock, and detach the clock before unregistering it. (Jakub Kicinski, Sashiko)v2: https://lore.kernel.org/all/[email protected]/
- Only advertise hardware timestamping support when a PHC is available (Niklas Söderlund).v1: https://lore.kernel.org/all/[email protected]/
Xuanqiang Luo (2):
net: ravb: avoid dereferencing an invalid PTP clock
net: ravb: serialize PTP clock teardowndrivers/net/ethernet/renesas/ravb.h | 3 ++ drivers/net/ethernet/renesas/ravb_main.c | 9 +++-- drivers/net/ethernet/renesas/ravb_ptp.c | 44 ++++++++++++++++++++---- 3 files changed, 47 insertions(+), 9 deletions(-)base-commit:
db2ddb87143519e20a95aa36c60b36107b736a58Signature · 1 line
2.43.0
Signature · 2 lines
Kind Regards, Niklas Söderlund
Hi Niklas,
Quoted 11 lines · Niklas Söderlund
Hi Xuanqiang,
Thanks for your work.
Quoted 6 lines · Xuanqiang Luo
From: Xuanqiang Luo <[email protected]>
This series fixes RAVB PTP clock lifetime handling. It reports a cached PHC index without accessing the clock pointer and drains PTP interrupts before unregistering the clock.
Quoted 7 lines
Quoted 4 lines
Patch 1 caches the PHC index and handles registration failures.
Patch 2 detaches the clock with
xchg()and drains the PTP IRQs before unregistering it.These patches are rather big change adding
READ_ONCE()andWRITE_ONCE()to avoid a LLM warning? Or have you hit a real issue? How have you tested this work?
The READ_ONCE()/WRITE_ONCE() use is as follows.
PATCH 1 follows Vadim's suggestion to cache the PHC index. ravb_get_ts_info() can race with ravb_ptp_init(), so the accessors are there for that update.
PATCH 2 only addresses the race reported by Sashiko. The teardown follows Vadim's suggestion as well, and READ_ONCE()/WRITE_ONCE() looked appropriate there too.
I do not have RAVB hardware, so I have not reproduced the PATCH 2 issue.
Quoted 7 lines
If you have a test-case could you share it? I have a pending series [1] that cleans up the whole RAVB driver ptp management which have grown rather organically. It have a small fix for the missing check of registering the clock. Would it be possible for you to test your work with that series too?
1. https://lore.kernel.org/all/20260811160200.2049987-1-niklas.soderlund%[email protected]/
I looked at [1]. It is for net-next. But my goal is still a small fixfor net.I would prefer to keep this on net rather than rebase onto [1] for net-next. Does that work for you?Patch 8 of [1] moves the PHC index lookup into a callback, but that callback still does not check for a NULL clock. Userspace can run
"ethtool -T" before the interface is opened, so the original hole
is still there.
To keep the conflict small, may I drop PATCH 2 for now and send
only PATCH 1 to net? PATCH 2 is a larger change, and I have no
reproducer, even though it does address Sashiko's report. The race
can wait until it is actually hit. I am not sure this is theright approach; this is the first time I have been in thissituation.
Thanks,
Xuanqiang
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Tue, 11 Aug 2026 18:37:31 +0800 you wrote:
From: Xuanqiang Luo <[email protected]>
This series fixes RAVB PTP clock lifetime handling. It reports a cached PHC index without accessing the clock pointer and drains PTP interrupts before unregistering the clock.
Patch 1 caches the PHC index and handles registration failures.
[...]
Here is the summary with links:
- [net,v4,1/2] net: ravb: avoid dereferencing an invalid PTP clock
https://git.kernel.org/netdev/net/c/1f77af0aaf27 - [net,v4,2/2] net: ravb: serialize PTP clock teardown https://git.kernel.org/netdev/net/c/1cb9663789c5
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html