mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net] net: macb: initialize PTP lock before registering clock
@ 2026-08-30 15:35 Runyu Xiao
  2026-08-31 15:24 ` Vadim Fedorenko
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Runyu Xiao @ 2026-08-30 15:35 UTC (permalink / raw)
  To: nicolas.ferre
  Cc: claudiu.beznea, richardcochran, netdev, linux-kernel, runyu.xiao,
	jianhao.xu, stable

gem_ptp_init() registers the PTP clock before initializing
bp->tsu_clk_lock. ptp_clock_register() publishes the PTP character
device, so a userspace operation can enter a PTP callback as soon as
registration completes. Those callbacks use tsu_clk_lock and can therefore
observe the lock before it has been initialized.

Initialize the lock before registering the PTP clock so all published
callbacks see valid synchronization state.

Fixes: ab91f0a9b5f4 ("net: macb: Add hardware PTP support")
Cc: stable@vger.kernel.org
Assisted-by: Codex:GPT-5
Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>
---
 drivers/net/ethernet/cadence/macb_ptp.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/cadence/macb_ptp.c b/drivers/net/ethernet/cadence/macb_ptp.c
index e5195d7da..055160c42 100644
--- a/drivers/net/ethernet/cadence/macb_ptp.c
+++ b/drivers/net/ethernet/cadence/macb_ptp.c
@@ -330,6 +330,8 @@ void gem_ptp_init(struct net_device *netdev)
 
 	bp->ptp_clock_info = gem_ptp_caps_template;
 
+	spin_lock_init(&bp->tsu_clk_lock);
+
 	/* nominal frequency and maximum adjustment in ppb */
 	bp->tsu_rate = bp->ptp_info->get_tsu_rate(bp);
 	bp->ptp_clock_info.max_adj = bp->ptp_info->get_ptp_max_adj();
@@ -345,8 +347,6 @@ void gem_ptp_init(struct net_device *netdev)
 		return;
 	}
 
-	spin_lock_init(&bp->tsu_clk_lock);
-
 	gem_ptp_init_tsu(bp);
 
 	dev_info(&bp->pdev->dev, "%s ptp clock registered.\n",
@@ -467,4 +467,3 @@ int gem_set_hwtst(struct net_device *netdev,
 
 	return 0;
 }
-
-- 
2.34.1

^ permalink raw reply	[flat|nested] 7+ messages in thread
* [PATCH net v2] net: macb: initialize PTP state before registering clock
@ 2026-09-21 11:11 Runyu Xiao
  2026-09-21 11:22 ` Runyu Xiao
  0 siblings, 1 reply; 7+ messages in thread
From: Runyu Xiao @ 2026-09-21 11:11 UTC (permalink / raw)
  To: Théo Lebrun, Conor Dooley, Andrew Lunn, David S . Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Richard Cochran
  Cc: netdev, linux-kernel, stable, Runyu Xiao, Jianhao Xu

gem_ptp_init() registers the PTP clock before initializing
bp->tsu_clk_lock and the TSU hardware. Since ptp_clock_register()
publishes the PTP character device, userspace may invoke PTP callbacks
before the lock and hardware are ready.

In addition, gem_ptp_init() is called from both the interface open and
resume paths. Reinitializing tsu_clk_lock there can reset the lock while
timestamp processing is using it.

This race is theoretical and has not been observed in practice.

Initialize tsu_clk_lock once during probe and initialize the TSU before
registering the PTP clock.

Fixes: ab91f0a9b5f4 ("net: macb: Add hardware PTP support")
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/netdev/20260830153520.2710520-1-runyu.xiao@seu.edu.cn/
Assisted-by: LLM Codex
Reviewed-by: Théo Lebrun <theo.lebrun@bootlin.com>
Reviewed-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
Signed-off-by: Runyu Xiao <runyu.xiao@seu.edu.cn>

---
v2:
- Initialize tsu_clk_lock once during probe and initialize the TSU before
  registering the PTP clock.
- Clarify that the race is theoretical and carry the Reviewed-by trailers.
---
 drivers/net/ethernet/cadence/macb_main.c | 1 +
 drivers/net/ethernet/cadence/macb_ptp.c  | 6 +-----
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
index 76ee4f506..01b42b21a 100644
--- a/drivers/net/ethernet/cadence/macb_main.c
+++ b/drivers/net/ethernet/cadence/macb_main.c
@@ -5876,6 +5876,7 @@ static int macb_probe(struct platform_device *pdev)
 	}
 	spin_lock_init(&bp->lock);
 	spin_lock_init(&bp->stats_lock);
+	spin_lock_init(&bp->tsu_clk_lock);
 
 	/* setup capabilities */
 	macb_configure_caps(bp, macb_config);
diff --git a/drivers/net/ethernet/cadence/macb_ptp.c b/drivers/net/ethernet/cadence/macb_ptp.c
index e5195d7da..e9c9c2692 100644
--- a/drivers/net/ethernet/cadence/macb_ptp.c
+++ b/drivers/net/ethernet/cadence/macb_ptp.c
@@ -334,6 +334,7 @@ void gem_ptp_init(struct net_device *netdev)
 	bp->tsu_rate = bp->ptp_info->get_tsu_rate(bp);
 	bp->ptp_clock_info.max_adj = bp->ptp_info->get_ptp_max_adj();
 	gem_ptp_init_timer(bp);
+	gem_ptp_init_tsu(bp);
 	bp->ptp_clock = ptp_clock_register(&bp->ptp_clock_info, &netdev->dev);
 	if (IS_ERR(bp->ptp_clock)) {
 		pr_err("ptp clock register failed: %ld\n",
@@ -345,10 +346,6 @@ void gem_ptp_init(struct net_device *netdev)
 		return;
 	}
 
-	spin_lock_init(&bp->tsu_clk_lock);
-
-	gem_ptp_init_tsu(bp);
-
 	dev_info(&bp->pdev->dev, "%s ptp clock registered.\n",
 		 GEM_PTP_TIMER_NAME);
 }
@@ -467,4 +464,3 @@ int gem_set_hwtst(struct net_device *netdev,
 
 	return 0;
 }
-
-- 
2.34.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-09-21 11:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-30 15:35 [PATCH net] net: macb: initialize PTP lock before registering clock Runyu Xiao
2026-08-31 15:24 ` Vadim Fedorenko
2026-09-02 15:35 ` [net] " netdev-bot+sashiko
2026-09-04  3:04 ` [PATCH net v2] net: macb: initialize PTP state " Runyu Xiao
2026-09-04 16:29   ` Théo Lebrun
2026-09-21 11:11 Runyu Xiao
2026-09-21 11:22 ` Runyu Xiao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®