From: netdev-bot+sashiko@kernel.org
To: javen_xu@realsil.com.cn
Cc: andrew@lunn.ch, hkallweit1@gmail.com, linux@armlinux.org.uk,
davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
pabeni@redhat.com, nb@tipi-net.de, maxime.chevallier@bootlin.com,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
daniel@makrotopia.org, vladimir.oltean@nxp.com
Subject: Re: [PATCH net-next v4 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
Date: Sun, 29 Nov 2026 09:50:14 +0000 [thread overview]
Message-ID: <179594581417.3146.17307818043948869054@kernel.org> (raw)
In-Reply-To: <20261125090217.1198-4-javen_xu@realsil.com.cn>
Thank you for your contribution! Sashiko AI review found 10 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 6 · Low: 3
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] rtl8127a_ptp_remove() relies on clearing
phydev->default_timestamp and then calling synchronize_net() to keep…
- [Medium] rtl8127a_ptp_hwtstamp_set(), rtl8127a_ptp_hwtstamp_get() and
rtl8127a_ptp_ts_info() are reached through…
- [Medium] rtl8127a_ptp_tx_tstamp() takes INSR.INT_TX_TS as proof that
the tracked tx_skb has been timestamped.
- [Medium] rtl8127a_ptp_do_aux_work() drains rx_queue with `while ((skb =
skb_dequeue(&ptp->rx_queue)))` before it looks at the pending TX…
- [Medium] rtl8127a_ptp_trx_ts_cmd() does a read-modify-write of
RTL8127A_PTP_TRX_TS_STA through rtl8127a_ptp_modify().
- [Medium] rtl8127a_ptp_rx_tstamp() issues the RX latch for the skb's
message type and attaches whatever TRX_TS_TIME holds as…
- [Medium] net: phy: realtek: pending tx_skb kept on TX timestamp toggle
in rtl8127a_ptp_hwtstamp_set()
- [Low] When __rtl8127a_ptp_hwtstamp_enable() fails partway,
rtl8127a_ptp_hwtstamp_set() records hw_enabled=false, rx_filter=NONE…
- [Low] The v3 changelog for this patch says "Restore timestamping in
config_init()", but rtl8127a_ptp_config_init() only clears time_set.
- [Low] The enable sequence in __rtl8127a_ptp_hwtstamp_enable() ends by
setting RTL8127A_PHY_RST (0xa640 bit 15) through rtl8127a_ptp_modify().
--
net: phy: realtek: add hardware timestamping for RTL8127a
This patch registers a mii_timestamper for the RTL8127a internal PHY.
With it, the PHY timestamps PTPv2 event messages over UDP without any
support from the MAC. Timestamps are read over MDIO from the PTP aux
worker: RX skbs are queued until their timestamp is latched, and only
one TX skb is tracked at a time.
> diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
> index 83ee125517b00..18a2d7800f116 100644
> --- a/drivers/net/phy/realtek/realtek_ptp.c
> +++ b/drivers/net/phy/realtek/realtek_ptp.c
[ ... ]
> @@ -189,32 +227,80 @@ static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp,
> return 0;
> }
>
> -static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
> +/* Latch the RX or TX timestamp of the given message type into the
> + * RTL8127A_PTP_TRX_TS_TIME registers.
> + */
> +static int rtl8127a_ptp_trx_ts_cmd(struct rtl8127a_ptp *ptp, u16 cmd)
> {
> int ret;
>
> - mutex_lock(&ptp->lock);
> + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_TRX_TS_STA,
> + RTL8127A_PTP_TRX_TS_SEL_RX |
> + RTL8127A_PTP_TRX_TS_MSGTYPE,
> + RTL8127A_PTP_TRX_TS_RD | cmd);
[Severity: Medium]
This read-modify-write masks only SEL_RX and MSGTYPE. Every other bit
read from TRX_TS_STA is written back as it was. That includes EXEC and
the TX ready bits RTL8127A_PTP_TX_PDLYRSP_RDY through
RTL8127A_PTP_TX_SYNC_RDY (bits 12-15).
If those RDY bits are write-1-to-clear, could an RX latch from the drain
loop in rtl8127a_ptp_do_aux_work() clear the RDY bit the pending tx_skb
is waiting for?
rtl8127a_ptp_do_aux_work()
rtl8127a_ptp_rx_tstamp()
rtl8127a_ptp_trx_ts_cmd() <- writes TX_*_RDY back
rtl8127a_ptp_tx_tstamp()
rtl8127a_ptp_egress_time()
rtl8127a_ptp_poll(..., rtl8127a_ptp_tx_rdy[msgtype], true)
In that case the RDY poll would time out after 1ms and the TX timestamp
would be dropped. One example: an E2E slave receives other slaves'
multicast Delay_Req while its own Delay_Req timestamp is still pending.
[ ... ]
> @@ -223,17 +309,24 @@ static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
[ ... ]
> if (ret)
> - goto out;
> + return ret;
>
> - ret = rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
> - RTL8127A_PHY_RST);
> -out:
> + return rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
> + RTL8127A_PHY_RST);
> +}
[Severity: Low]
This sets RTL8127A_PHY_RST but never deasserts it or polls for it to
self-clear. CLK_CFG_EXEC and TRX_TS_RD, by contrast, are polled.
Before this patch, the sequence ran only for disable, at probe and
remove. Now the enable variant also runs at runtime:
- from rtl8127a_ptp_hwtstamp_set() on every off to on change
- from rtl8127a_ptp_link_change_notify() right after link-up
What does this bit do? If it resets the PHY or the PTP datapath, can
the link that just came up bounce? If it is level-triggered, does the
block stay in reset?
The aux worker may also issue TRX_TS_STA commands right after this,
with no settle delay.
[ ... ]
> @@ -323,6 +416,111 @@ static int rtl8127a_ptp_settime64(struct ptp_clock_info *info,
[ ... ]
> +/* Returns true if no TX timestamp is pending anymore */
> +static bool rtl8127a_ptp_tx_tstamp(struct rtl8127a_ptp *ptp)
> +{
[ ... ]
> + ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_INSR);
> + if (ret < 0 || !(ret & RTL8127A_PTP_INT_TX_TS)) {
> + mutex_unlock(&ptp->lock);
> + return false;
> + }
> +
> + ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR,
> + RTL8127A_PTP_INT_TX_TS);
> + if (!ret)
> + ret = rtl8127a_ptp_egress_time(ptp, ptp->tx_msgtype, &ts);
[Severity: Medium]
Can INSR.INT_TX_TS be stale at this point?
All TX event messages share this bit, and it is acknowledged only while
a tx_skb is tracked. rtl8127a_ptp_txtstamp() does not clear INSR or the
TX_*_RDY bits when it starts tracking a new skb. So the bit can still be
set from any event frame the PHY timestamped while nothing was tracked,
for example:
- frames sent while the hardware is on only because an RX filter was
set (rtl8127a_ptp_hwtstamp_set() sets enable = true for any RX
filter, even with HWTSTAMP_TX_OFF)
- frames without SKBTX_HW_TSTAMP
- frames dropped in rtl8127a_ptp_txtstamp() because
RTL8127A_PTP_TX_IN_PROGRESS was already held
If TX is switched on later while RX is already on, enable equals
hw_enabled. The INSR write of 0xffff in __rtl8127a_ptp_hwtstamp_enable()
is then skipped.
r8169 calls skb_tx_timestamp() before ringing the doorbell, and
rtl8127a_ptp_txtstamp() schedules the worker with zero delay. The worker
can therefore see the stale INT_TX_TS before the tracked frame is on the
wire, and go straight into the 1ms RDY poll in
rtl8127a_ptp_egress_time().
If the RDY bit for this message type is still set from an earlier frame,
does this latch the earlier frame's timestamp and report it for this
skb? Without a sequenceId, nothing would catch the mismatch.
If the RDY bit is not set, the skb is freed without a timestamp whenever
the frame takes more than 1ms to go out.
[ ... ]
> +static void rtl8127a_ptp_rx_tstamp(struct rtl8127a_ptp *ptp,
> + struct sk_buff *skb)
> +{
> + u8 msgtype = RTL8127A_PTP_SKB_CB(skb)->msgtype;
> + struct timespec64 ts;
> + int ret;
> +
> + mutex_lock(&ptp->lock);
> + ret = rtl8127a_ptp_trx_ts_cmd(ptp, RTL8127A_PTP_TRX_TS_SEL_RX |
> + FIELD_PREP(RTL8127A_PTP_TRX_TS_MSGTYPE,
> + msgtype) |
> + RTL8127A_PTP_TRX_TS_EXEC);
> + if (!ret)
> + ret = rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_TRX_TS_TIME,
> + &ts);
> + mutex_unlock(&ptp->lock);
> +
> + if (!ret)
> + skb_hwtstamps(skb)->hwtstamp = timespec64_to_ktime(ts);
[Severity: Medium]
Is there a check anywhere that the hardware captured a timestamp for
this frame?
The TX path checks INSR.INT_TX_TS and the per-type RDY bit. Here,
whatever TRX_TS_TIME holds is attached as a valid hardware timestamp.
__rtl8127a_ptp_hwtstamp_enable() unmasks INT_RX_TS in INER with
INT_CLR_MODE set, but nothing reads or acknowledges INT_RX_TS.
rtl8127a_ptp_rxtstamp() accepts every non-L2 PTPv2 event frame. That
includes:
- any IPv4 or IPv6 variant covered by HWTSTAMP_FILTER_PTP_V2_L4_EVENT
- frames received right after enable
- frames received after a PTP block reset, before
rtl8127a_ptp_link_change_notify() restores it
If the hardware did not timestamp such a frame, could the frame be
delivered with an earlier frame's timestamp, or with zero?
The commit message documents only overwrites by a later message of the
same type.
> +
> + netif_rx(skb);
> +}
> +
> +static long rtl8127a_ptp_do_aux_work(struct ptp_clock_info *info)
> +{
> + struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
> + struct sk_buff *skb;
> +
> + while ((skb = skb_dequeue(&ptp->rx_queue)))
> + rtl8127a_ptp_rx_tstamp(ptp, skb);
> +
> + if (rtl8127a_ptp_tx_tstamp(ptp))
> + return -1;
[Severity: Medium]
Can this loop starve the pending TX timestamp?
Each rtl8127a_ptp_rx_tstamp() call does about nine MDIO accesses,
including a poll of up to 1ms. While rx_enabled is set,
rtl8127a_ptp_rxtstamp() queues any PTPv2 UDP event message from any
sender. RTL8127A_PTP_RX_QUEUE_MAX bounds memory, not how long this loop
runs.
A remote host can send event messages to UDP port 319 faster than the
worker services them, and then rx_queue never drains. In that case:
- rtl8127a_ptp_tx_tstamp() is never reached
- the pending TX skb is dropped after RTL8127A_PTP_TX_TIMEOUT
- the PTP kthread stays busy for as long as the traffic continues
Other drivers, such as mv88e6xxx, splice the queue and process only a
bounded batch per invocation. Would that approach work here?
[ ... ]
> @@ -374,6 +573,13 @@ void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
> goto out;
> ptp->time_set = true;
> }
> +
> + /* Restore timestamping in case the PTP block was reset */
> + if (ptp->hw_enabled) {
> + ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_CTL);
> + if (ret >= 0 && !(ret & RTL8127A_PTP_CTL_EN))
> + ret = __rtl8127a_ptp_hwtstamp_enable(ptp, true);
> + }
> out:
> mutex_unlock(&ptp->lock);
>
> @@ -381,6 +587,180 @@ void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
> phydev_warn(phydev, "failed to set PTP time: %d\n", ret);
> }
[Severity: Low]
The v3 changelog says:
- Restore timestamping in config_init()
However, rtl8127a_ptp_config_init() only clears time_set. The restore is
here in rtl8127a_ptp_link_change_notify(). It runs only on link-up, and
it is skipped when setting the clock time fails and the code jumps to
out.
If the CTL read or __rtl8127a_ptp_hwtstamp_enable() fails here, the
error is logged as "failed to set PTP time". Should a failure to restore
timestamping get its own message, separate from a clock-set failure?
[ ... ]
> + /* The hardware is either on or off, only reprogram it on a change */
> + mutex_lock(&ptp->lock);
> + if (enable != ptp->hw_enabled) {
> + /* Stop timestamping before turning the hardware off */
> + if (!enable) {
> + WRITE_ONCE(ptp->tx_type, HWTSTAMP_TX_OFF);
> + WRITE_ONCE(ptp->rx_enabled, false);
> + }
> +
> + ret = __rtl8127a_ptp_hwtstamp_enable(ptp, enable);
> + if (ret) {
> + /* The hardware state is unknown, turn timestamping
> + * off and have the next request reprogram it.
> + */
> + ptp->hw_enabled = false;
[Severity: Low]
Does the next request actually reprogram the hardware?
__rtl8127a_ptp_hwtstamp_enable() returns early on any error, with no
unwind. r8168_phy_ocp_read() can return -ETIMEDOUT, so for example the
PHY_RST_CTL modify can fail after CTL has been written with CTL_EN set.
A later HWTSTAMP_TX_OFF / HWTSTAMP_FILTER_NONE request then computes
enable == false == hw_enabled and never touches the hardware.
rtl8127a_ptp_link_change_notify() also skips the restore, because
hw_enabled is false.
Can the PTP block stay enabled and keep raising INSR status while
software reports timestamping as off?
> + ptp->rx_filter = HWTSTAMP_FILTER_NONE;
> + WRITE_ONCE(ptp->rx_enabled, false);
> + WRITE_ONCE(ptp->tx_type, HWTSTAMP_TX_OFF);
> + goto out;
> + }
> +
> + ptp->hw_enabled = enable;
> + }
> +
> + ptp->rx_filter = config->rx_filter;
> + WRITE_ONCE(ptp->rx_enabled, rx_enabled);
> + WRITE_ONCE(ptp->tx_type, config->tx_type);
> +out:
> + mutex_unlock(&ptp->lock);
> + /* Let the aux worker drop a TX timestamp that is no longer wanted */
> + ptp_schedule_worker(ptp->clock, 0);
[Severity: Medium]
Does a quick off/on toggle leave the old tx_skb pending?
When TX timestamping is turned off, rtl8127a_ptp_hwtstamp_set() only
writes HWTSTAMP_TX_OFF to ptp->tx_type and kicks the aux worker. The
pending ptp->tx_skb is dropped only if rtl8127a_ptp_tx_tstamp() still
sees tx_type != HWTSTAMP_TX_ON once it holds ptp->lock.
A second request can set HWTSTAMP_TX_ON again before that happens. The
worker may still be draining rx_queue over MDIO, or the kthread may not
have run yet. In that case the worker sees HWTSTAMP_TX_ON and keeps the
skb from the earlier session. A PTP daemon restart is one way to hit
this.
>From then on:
- RTL8127A_PTP_TX_IN_PROGRESS stays set, so rtl8127a_ptp_txtstamp()
drops every new TX timestamp request until RTL8127A_PTP_TX_TIMEOUT
expires up to 15 seconds later
- tx_msgtype and tx_start still belong to the old skb. If one of the
dropped frames has the same message type and raises
INSR.INT_TX_TS, rtl8127a_ptp_egress_time() latches that frame's
timestamp and rtl8127a_ptp_tx_complete() reports it on the stale
skb
If the off request also turned the hardware off, the following on
request goes through __rtl8127a_ptp_hwtstamp_enable() again, which
clears INSR. The old frame's timestamp is then gone for good, and only
the timeout or a wrong match can release the skb.
Would it be better to release the pending skb from
rtl8127a_ptp_hwtstamp_set() itself when TX is turned off? Another
option is to record a generation along with tx_skb and have the worker
compare it under ptp->lock. Note that the worker currently checks
tx_skb before it takes ptp->lock, so it would need to recheck tx_skb
once the lock is held.
[Severity: Medium]
What stops this callback from racing with rtl8127a_ptp_remove()?
Three callbacks are reached through RTNL or netdev ops lock callers:
- rtl8127a_ptp_hwtstamp_set() via __phy_hwtstamp_set()
- rtl8127a_ptp_hwtstamp_get() via __phy_hwtstamp_get()
- rtl8127a_ptp_ts_info() via the ethtool tsinfo path
Those callers check phydev->mii_ts only once. On a sysfs unbind,
phy_remove() calls rtl8127a_ptp_remove() without holding either lock.
synchronize_net() does not wait for these process-context callers.
For example:
CPU1 (unbind) CPU2 (SIOCSHWTSTAMP)
rtl8127a_ptp_remove()
rtl8127a_ptp_hwtstamp_enable()
mutex_lock(&ptp->lock)
rtl8127a_ptp_hwtstamp_set()
mutex_lock(&ptp->lock) waits
mutex_unlock(&ptp->lock)
ptp_clock_unregister()
sees hw_enabled == false
__rtl8127a_ptp_hwtstamp_enable(ptp, true)
ptp_schedule_worker(ptp->clock, 0)
remove() never updates hw_enabled, so CPU2 turns the hardware back on.
CPU2 then schedules work on the kthread worker that
ptp_clock_unregister() already destroyed.
rtl8127a_ptp_ts_info() can likewise call ptp_clock_index() on an
unregistered clock. Once remove() returns, all three callbacks would be
running on devm-freed ptp memory.
[ ... ]
> @@ -422,7 +813,17 @@ void rtl8127a_ptp_remove(struct phy_device *phydev)
> if (!ptp)
> return;
>
> + phydev->default_timestamp = false;
> + /* Wait for rxtstamp()/txtstamp() callers that still see mii_ts. */
> + synchronize_net();
> + phydev->mii_ts = NULL;
[Severity: High]
Does clearing default_timestamp also stop callers that come in through
dev->hwprov?
If ethtool tsconfig selected the PHY as the hwtstamp provider, then
skb_defer_rx_timestamp() and skb_clone_tx_timestamp() take the phydev
from hwprov. They never call phy_is_default_hwtstamp():
net/core/timestamping.c:skb_defer_rx_timestamp() {
...
hwprov = rcu_dereference(skb->dev->hwprov);
if (hwprov) {
...
phydev = hwprov->phydev;
} else {
phydev = skb->dev->phydev;
if (!phy_is_default_hwtstamp(phydev)) {
...
}
rcu_read_unlock();
...
mii_ts = phydev->mii_ts;
if (likely(mii_ts->rxtstamp))
return mii_ts->rxtstamp(mii_ts, skb, type);
...
}
dev->hwprov is cleared only in phy_detach(), and phy_remove() calls the
driver's remove() without detaching. Suppose the PHY driver is unbound
through sysfs while the netdev is still attached. Would the next PTP
event packet received then dereference a NULL mii_ts here?
A reader that loaded the old mii_ts before it was cleared can still
call rtl8127a_ptp_rxtstamp() or rtl8127a_ptp_txtstamp(). remove() never
clears rx_enabled or tx_type, so:
- the skb could be queued to rx_queue, or stored in tx_skb, after
skb_queue_purge()
- ptp_schedule_worker() would run on the kthread worker that
ptp_clock_unregister() destroyed
- the ptp struct itself is devm memory and is freed after remove()
returns
The v4 changelog says "Wait for in-flight rxtstamp()/txtstamp() in
remove()". However, skb_defer_rx_timestamp() and skb_clone_tx_timestamp()
dereference mii_ts outside the RCU section, and nothing waits after
mii_ts is cleared.
Can the hwprov path be covered as well?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125090217.1198-1-javen_xu%40realsil.com.cn
prev parent reply other threads:[~2026-11-29 9:50 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 9:02 [PATCH net-next v4 0/3] net: phy: realtek: add PTP support for RTL8127a javen
2026-11-25 9:02 ` [PATCH net-next v4 1/3] r8169: hold MDIO bus lock while loading firmware javen
2026-11-29 9:50 ` netdev-bot+sashiko
2026-11-25 9:02 ` [PATCH net-next v4 2/3] net: phy: realtek: add PTP clock support for RTL8127a javen
2026-11-29 9:50 ` netdev-bot+sashiko
2026-11-25 9:02 ` [PATCH net-next v4 3/3] net: phy: realtek: add hardware timestamping " javen
2026-11-25 9:43 ` Vladimir Oltean
2026-11-26 6:13 ` Javen
2026-11-26 14:08 ` Andrew Lunn
2026-11-29 9:50 ` netdev-bot+sashiko [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179594581417.3146.17307818043948869054@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=andrew@lunn.ch \
--cc=daniel@makrotopia.org \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=hkallweit1@gmail.com \
--cc=javen_xu@realsil.com.cn \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=maxime.chevallier@bootlin.com \
--cc=nb@tipi-net.de \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=vladimir.oltean@nxp.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox