Re: [PATCH net-next v6 4/4] net: phy: realtek: add hardware timestamping for RTL8127a
From: netdev-bot+sashiko
Date: Fri Dec 04 2026 - 21:59:33 EST
Thank you for your contribution! Sashiko AI review found 12 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 8 · Low: 4
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).
- [Medium] The driver only notices that the PTP block was reset when user
space calls rtl8127a_ptp_hwtstamp_set() again with an enabling config.
- [Medium] rtl8127a_ptp_hwtstamp_set() starts with `if (config->flags)
return -EINVAL;`.
- [Medium] rtl8127a_ptp_tx_tstamp() treats the INSR TX_TS bit as proof
that the pending skb has been stamped.
- [Medium] The existing PHC ops wrap every MDIO access in
rtl8127a_ptp_pm_get().
- [Medium] rtl8127a_ptp_remove() sets phydev->mii_ts = NULL and
immediately tears the timestamper down:…
- [Medium] rtl8127a_ptp_rx_tstamp() always issues SEL_RX|MSGTYPE|EXEC and
reports the TRX_TS_TIME latch as the frame's hardware timestamp.
- [Medium] net: phy: realtek: aux worker accesses disabled PTP block on
RX off or remove
- [Medium] net: phy: realtek: rtl8127a_ptp_remove() disables hw before
clock unregister
- [Low] rtl8127a_ptp_probe() installs phydev->mii_ts but never sets
phydev->default_timestamp.
- [Low] rtl8127a_ptp_tx_tstamp() pairs its smp_load_acquire(&ptp->tx_skb)
with txtstamp's smp_store_release().
- [Low] The enable path overwrites the whole RTL8127A_PTP_CTL register
with RTL8127A_PTP_CTL_TYPE_3 | BIT(12).
- [Low] net: phy: realtek: rtl8127a_ptp_hwtstamp_disable() duplicates
enable helper
--
net: phy: realtek: add hardware timestamping for RTL8127a
This patch registers a mii_timestamper for the RTL8127A internal PHY, so
PTPv2 event messages over UDP get hardware timestamps in both directions.
Timestamps are read over MDIO from the PTP aux worker. RX skbs are queued
until their timestamp is read, and a single pending TX skb is completed
once the hardware reports its timestamp.
> diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
> index 16ba3fe9697a0..9f82466c2b23c 100644
> --- a/drivers/net/phy/realtek/realtek_ptp.c
> +++ b/drivers/net/phy/realtek/realtek_ptp.c
[ ... ]
> @@ -172,6 +214,98 @@ static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp,
[ ... ]
> +static int __rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp,
> + bool enable)
> +{
[Severity: Low]
This isn't a bug, but the enable == false branch of
__rtl8127a_ptp_hwtstamp_enable() runs the same register sequence as
rtl8127a_ptp_hwtstamp_disable():
- clear SYNCE_EN in SYNCE_CTL
- mask RX_TS and TX_TS in INER
- clear CTL_EN in CTL
- set PHY_RST in PHY_RST_CTL
Could rtl8127a_ptp_hwtstamp_disable() take ptp->lock and call
__rtl8127a_ptp_hwtstamp_enable(ptp, false) instead? Probe, remove and
rtl8127a_ptp_hwtstamp_set() would then share one disable sequence, so
a later fix to it can't end up in only one of the two copies.
[ ... ]
> + ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR, 0xffff);
> + if (ret)
> + return ret;
> +
> + ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CTL,
> + RTL8127A_PTP_CTL_TYPE_3 | BIT(12));
> + } else {
> + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_INER,
> + RTL8127A_PTP_INT_RX_TS |
> + RTL8127A_PTP_INT_TX_TS, 0);
> + if (ret)
> + return ret;
> +
> + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_CTL,
> + RTL8127A_PTP_CTL_EN, 0);
> + }
[Severity: Low]
This isn't a bug, but the enable path overwrites the whole
RTL8127A_PTP_CTL register with RTL8127A_PTP_CTL_TYPE_3 | BIT(12).
RTL8127A_PTP_CTL_TYPE_3 is 0x003f. It quietly includes RTL8127A_PTP_CTL_EN
plus bits 1-5, and BIT(12) has no name or comment.
The disable path only clears RTL8127A_PTP_CTL_EN. Bits 1-5 and 12 stay
set, so an off/on toggle never returns the register to a known state.
Could these bits get named defines, with a note on which encapsulations
they select? Without that, the software filters in rtl8127a_ptp_rxtstamp()
and rtl8127a_ptp_txtstamp() can't be checked against what the hardware
actually stamps.
[ ... ]
> @@ -325,6 +459,124 @@ 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)
> +{
> + struct skb_shared_hwtstamps hwts = {};
> + struct timespec64 ts;
> + int ret;
> +
> + /* Pairs with smp_store_release() in rtl8127a_ptp_txtstamp() */
> + if (!smp_load_acquire(&ptp->tx_skb))
> + return true;
> +
> + mutex_lock(&ptp->lock);
> +
> + /* rtl8127a_ptp_hwtstamp_set() may have dropped it meanwhile */
> + if (!ptp->tx_skb) {
> + mutex_unlock(&ptp->lock);
> + return true;
> + }
[Severity: Low]
The smp_load_acquire() above pairs with the smp_store_release() in
rtl8127a_ptp_txtstamp(). Does that ordering still hold for this second,
plain read of ptp->tx_skb under the mutex?
Take this sequence while the worker is blocked on ptp->lock:
worker: smp_load_acquire() sees skb A
hwtstamp_set(TX OFF) drops A
hwtstamp_set(TX ON)
rtl8127a_ptp_txtstamp() writes tx_msgtype and tx_start,
then smp_store_release(&ptp->tx_skb, B)
worker: mutex_lock() pairs with hwtstamp_set()'s unlock
The worker's mutex acquire then orders against hwtstamp_set(), not
against rtl8127a_ptp_txtstamp(). On weakly ordered CPUs, could the plain
reads return B for tx_skb but A's tx_msgtype and tx_start? That would
latch the wrong message type, or time B out immediately.
Would using smp_load_acquire() for this re-check close the window?
rtl8127a_ptp_do_aux_work() has a similar unlocked plain read of tx_start
after this function returns. It races with the plain write in
rtl8127a_ptp_txtstamp():
return time_before(jiffies, ptp->tx_start + RTL8127A_PTP_TX_POLL_FAST) ?
0 : 1;
KCSAN would likely flag both.
> +
> + /* Give up if TX timestamping was turned off or nothing arrived */
> + if (READ_ONCE(ptp->tx_type) != HWTSTAMP_TX_ON ||
> + time_is_before_jiffies(ptp->tx_start + RTL8127A_PTP_TX_TIMEOUT)) {
> + rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR,
> + RTL8127A_PTP_INT_TX_TS);
> +
> + rtl8127a_ptp_tx_complete(ptp, NULL);
> + mutex_unlock(&ptp->lock);
> + return true;
> + }
> +
> + ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_INSR);
> + if (ret < 0 || !(ret & RTL8127A_PTP_INT_TX_TS)) {
> + mutex_unlock(&ptp->lock);
> + return false;
> + }
[Severity: Medium]
The existing PHC ops wrap their MDIO accesses in rtl8127a_ptp_pm_get(),
whose comment says:
/* The PHY is accessed through the MAC, which may be runtime suspended
* while there is no link. Don't resume it for the PHC, fail instead.
Does the aux worker need the same guard, both here and in
rtl8127a_ptp_rx_tstamp()?
This function polls INSR for up to RTL8127A_PTP_TX_TIMEOUT (15 s). r8169
schedules a runtime suspend 10 s after carrier loss:
rtl8169_runtime_idle()
if (!netif_running(tp->dev) || !netif_carrier_ok(tp->dev))
pm_schedule_suspend(device, 10000);
If the cable is pulled while a TX skb is pending, the worker keeps doing
MDIO accesses after the MAC has entered D3hot. In that state
r8168_phy_ocp_read() can return 0xffff as valid data, because the
all-ones GPHY_OCP read satisfies the flag wait:
return rtl_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
(RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT;
An INSR value of 0xffff looks like RTL8127A_PTP_INT_TX_TS is set, and the
RDY poll in rtl8127a_ptp_egress_time() passes at once. The following
writes are lost silently because r8168_phy_ocp_write() returns void. The
RD-clear poll then times out, and every one of these MMIO accesses goes
to a device in D3hot.
> +
> + 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);
> +
> + if (ret) {
> + rtl8127a_ptp_tx_complete(ptp, NULL);
> + } else {
> + hwts.hwtstamp = timespec64_to_ktime(ts);
> + rtl8127a_ptp_tx_complete(ptp, &hwts);
> + }
[Severity: Medium]
Can the RTL8127A_PTP_INT_TX_TS bit checked above belong to a different
frame than ptp->tx_skb?
That INSR bit is shared by all four event message types. Event frames the
driver did not track also set it, for example:
- frames whose clone rtl8127a_ptp_txtstamp() dropped because
RTL8127A_PTP_TX_IN_PROGRESS was already set, e.g. after ptp4l's own
tx_timestamp_timeout fired
- frames sent without SKBTX_HW_TSTAMP
- frames sent while tx_type was OFF but the block stayed on for RX.
Switching TX from OFF to ON while hw_enabled is already true does not
clear INSR.
INSR is only cleared at enable time and here, and the worker returns
early while tx_skb is NULL. So a stale TX_TS bit can be waiting when the
next tracked skb arrives.
rtl8127a_ptp_egress_time() then polls the per-type RDY bit through
rtl8127a_ptp_poll(), which waits only 1 ms:
return phy_read_mmd_poll_timeout(ptp->phydev, MDIO_MMD_VEND2, reg, val,
!!(val & mask) == set, 1, 1000, false);
If the tracked frame has not left within that 1 ms, the -ETIMEDOUT drops
the skb here. The function returns true, rtl8127a_ptp_do_aux_work() stops
polling, and the 15 s timeout is never reached.
If the RDY bit for that type is instead still set from an earlier
untracked frame, would the older frame's timestamp be latched and passed
to skb_complete_tx_timestamp() as a valid timestamp?
[ ... ]
> +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);
> +
> + netif_rx(skb);
> +}
[Severity: Medium]
Can rtl8127a_ptp_rx_tstamp() latch a timestamp from a PTP block that
has already been turned off?
rtl8127a_ptp_tx_tstamp() re-checks ptp->tx_type under ptp->lock before
it touches any register. This path has no such check. Once it holds
ptp->lock, it always issues the SEL_RX latch through
rtl8127a_ptp_trx_ts_cmd() and reads RTL8127A_PTP_TRX_TS_TIME.
Take hwtstamp_set() turning everything off while event frames are
queued:
CPU1 (aux worker) CPU2 (hwtstamp_set, all off)
rtl8127a_ptp_do_aux_work()
splice rx_queue
mutex_lock(&ptp->lock)
rx_enabled = false
__rtl8127a_ptp_hwtstamp_enable(false)
mutex_unlock(&ptp->lock)
rtl8127a_ptp_rx_tstamp()
rtl8127a_ptp_trx_ts_cmd()
rtl8127a_ptp_read_time()
At that point the block has CTL_EN cleared and PHY_RST set. Two things
can happen:
- The RD-clear poll in rtl8127a_ptp_trx_ts_cmd() times out, and
phy_read_mmd_poll_timeout() logs it through phydev_err().
- The poll completes, and whatever the reset block holds in the latch
is stored in skb_hwtstamps() and passed to netif_rx(). That happens
right after user space asked for RX timestamping to stop.
hwtstamp_set() cannot reach the skbs that are already spliced into the
local received list. So I think the check has to happen per skb, under
ptp->lock. Would it be enough to test ptp->hw_enabled (or rx_enabled)
there, and deliver the skb without a timestamp when it is off?
rtl8127a_ptp_remove() has the same window. It disables the block with
rtl8127a_ptp_hwtstamp_disable() before ptp_clock_unregister() stops
the worker, and it leaves hw_enabled, tx_type and rx_enabled set. In
that window both rtl8127a_ptp_rx_tstamp() and rtl8127a_ptp_tx_tstamp()
run against the disabled block, and the tx_type re-check does not help.
[Severity: Medium]
Does anything here confirm that the hardware captured a new RX timestamp
for this frame?
The TX path at least checks INSR and a per-type RDY bit. This path always
issues SEL_RX | MSGTYPE | EXEC and reports whatever is in
RTL8127A_PTP_TRX_TS_TIME.
__rtl8127a_ptp_hwtstamp_enable() enables RTL8127A_PTP_INT_RX_TS in INER,
but the RX side never reads or clears it. Only the 0xffff write at enable
time clears it, so it stays set after the first RX event.
rtl8127a_ptp_rxtstamp() accepts any non-L2 PTPv2 class, including IPv4,
IPv6 and VLAN variants. The hardware is programmed with the undocumented
RTL8127A_PTP_CTL_TYPE_3 | BIT(12), so it is not clear that the PHY stamps
all of them.
For a frame the PHY did not stamp, or one received after the PTP block
was reset, would this pass the previous latch contents (or zero) to
netif_rx() as a valid hardware timestamp?
The commit message describes a timestamp being overwritten by a later
message of the same type, but not this case.
[ ... ]
> @@ -333,8 +585,193 @@ static const struct ptp_clock_info rtl8127a_ptp_info = {
[ ... ]
> +static int rtl8127a_ptp_hwtstamp_set(struct mii_timestamper *mii_ts,
> + struct kernel_hwtstamp_config *config,
> + struct netlink_ext_ack *extack)
> +{
> + struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
> + mii_ts);
> + bool enable = false;
> + bool rx_enabled;
> + int ret = 0;
> +
> + if (config->flags)
> + return -EINVAL;
[Severity: Medium]
Does rejecting every flag here break timestamping through a bond?
net_hwtstamp_validate() only rejects flags outside HWTSTAMP_FLAG_MASK, so
HWTSTAMP_FLAG_BONDED_PHC_INDEX is a valid flag. bond_hwtstamp_set()
requires it and passes the same cfg down to the active slave:
bond_hwtstamp_set()
if (!(cfg->flags & HWTSTAMP_FLAG_BONDED_PHC_INDEX))
return -EOPNOTSUPP;
...
generic_hwtstamp_set_lower(real_dev, cfg, extack)
-> dev_set_hwtstamp_phylib()
-> phy_hwtstamp_set()
-> mii_ts->hwtstamp_set()
That call then fails with -EINVAL here, so hardware timestamping on an
RTL8127A PHY can never be enabled through a bond.
Could this check either be dropped, since the core already validates the
mask, or reject only flags & ~HWTSTAMP_FLAG_MASK?
[ ... ]
> + /* The hardware is either on or off, only reprogram it on a change */
> + mutex_lock(&ptp->lock);
> + /* Reprogram the PTP clock in case it was reset meanwhile */
> + if (enable && ptp->hw_enabled) {
> + ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_CTL);
> + if (ret < 0 || !(ret & RTL8127A_PTP_CTL_EN))
> + ptp->hw_enabled = false;
> + ret = 0;
> + }
[Severity: Medium]
Is a new hwtstamp_set() call from user space the only point where a reset
PTP block gets noticed?
v6 dropped the config_init() and link_change_notify() restore paths.
r8169 re-initializes the internal PHY on every open and on every runtime
or system resume:
rtl8169_up()
rtl8169_init_phy()
phy_init_hw()
phy_resume()
r8169_hw_phy_config()
...
genphy_soft_reset()
After such a reset, ptp->hw_enabled, tx_type and rx_enabled still say
timestamping is on, and rtl8127a_ptp_hwtstamp_get() reports that cached
state.
rtl8127a_ptp_txtstamp() keeps taking TX clones. INSR never reports TX_TS,
so each clone is held for 15 s and then dropped.
rtl8127a_ptp_rxtstamp() keeps diverting RX event frames and stamping
them with stale latch contents.
ptp4l recovers, because it re-issues SIOCSHWTSTAMP after a link fault.
Consumers that configure timestamping only once, such as chrony, would
not. The commit message doesn't mention this.
Also, the comment says "Reprogram the PTP clock", but the recovery path
through __rtl8127a_ptp_hwtstamp_enable() only rewrites SYNCE_CTL, INER,
GEN_CFG, INSR, CTL and PHY_RST_CTL. It does not restore the clock rate or
time. Is the comment accurate?
[ ... ]
> @@ -364,7 +804,14 @@ int rtl8127a_ptp_probe(struct phy_device *phydev)
> if (IS_ERR(ptp->clock))
> return PTR_ERR(ptp->clock);
>
> + ptp->mii_ts.rxtstamp = rtl8127a_ptp_rxtstamp;
> + ptp->mii_ts.txtstamp = rtl8127a_ptp_txtstamp;
> + ptp->mii_ts.hwtstamp_set = rtl8127a_ptp_hwtstamp_set;
> + ptp->mii_ts.hwtstamp_get = rtl8127a_ptp_hwtstamp_get;
> + ptp->mii_ts.ts_info = rtl8127a_ptp_ts_info;
> +
> phydev->priv = ptp;
> + phydev->mii_ts = &ptp->mii_ts;
[Severity: Low]
This isn't a bug, but phydev->default_timestamp is never set here. The
other PHY timestamping drivers (bcm-phy-ptp, dp83640, micrel,
microchip_rds_ptp, mscc_ptp and nxp-c45-tja11xx) all set it.
With dev->hwprov unset, phy_is_default_hwtstamp() returns false.
dev_set_hwtstamp_phylib() then falls through to r8169, which has no
ndo_hwtstamp_set, and returns -EOPNOTSUPP. skb_clone_tx_timestamp() and
skb_defer_rx_timestamp() also skip the PHY:
} else {
phydev = skb->dev->phydev;
if (!phy_is_default_hwtstamp(phydev)) {
rcu_read_unlock();
return false;
}
}
The earlier series commit "net: ethtool: tsconfig: allow selecting a
provider" says new PHY drivers are expected to rely on dev->hwprov rather
than default_timestamp, so this looks intentional.
Could the commit message or the Kconfig help say that the PHY first has
to be selected as the hwtstamp provider through ethtool tsconfig? The new
help text only says:
and timestamps PTPv2 event messages over UDP on behalf of the
MAC, so no support from the MAC driver is needed.
>
> return 0;
> }
> @@ -376,7 +823,14 @@ void rtl8127a_ptp_remove(struct phy_device *phydev)
> if (!ptp)
> return;
>
> + phydev->mii_ts = NULL;
> +
> rtl8127a_ptp_hwtstamp_disable(ptp);
> + /* Also stops the aux worker */
> ptp_clock_unregister(ptp->clock);
> +
> + if (ptp->tx_skb)
> + rtl8127a_ptp_tx_complete(ptp, NULL);
> + skb_queue_purge(&ptp->rx_queue);
> phydev->priv = NULL;
> }
[Severity: Medium]
Should ptp_clock_unregister() be called before
rtl8127a_ptp_hwtstamp_disable() here?
Probe uses the opposite order: it disables the block first and only
then registers the clock. Here the block is disabled and PHY_RST is
set while /dev/ptpN is still registered and the aux worker may still
be queued or running.
Until ptp_clock_unregister() cancels ptp->aux_work,
rtl8127a_ptp_do_aux_work() can still process queued RX skbs and a
pending TX skb. It issues latch commands and MDIO polls against a
block that has just been reset. That shows up as poll timeouts logged
through phydev_err(), or as timestamps read from the reset block and
handed to netif_rx() during teardown. The posix clock ops can also
still be called from user space in that window.
Unregistering the clock first would stop the worker and the clock ops
before the hardware is touched. The block could then be disabled, and
tx_skb and rx_queue cleaned up as before. Is there a reason the
disable has to come first?
[Severity: Medium]
Can rtl8127a_ptp_txtstamp() or rtl8127a_ptp_rxtstamp() still be running
when this teardown happens?
skb_clone_tx_timestamp() and skb_defer_rx_timestamp() load
phydev->mii_ts and call into the driver with no synchronization against
remove. The synchronize_net() that covered this window was dropped in v5.
ptp is allocated with devm_kzalloc() and is freed right after remove()
returns.
For example, if root unbinds the PHY driver through sysfs while the
netdev is up with the PHY selected as provider:
CPU1 (xmit) CPU2 (remove)
rtl8127a_ptp_txtstamp()
phydev->mii_ts = NULL
ptp_clock_unregister()
if (ptp->tx_skb) /* NULL */
test_and_set_bit_lock()
smp_store_release(&ptp->tx_skb, skb)
ptp_schedule_worker(ptp->clock, 0)
The clone holds a socket reference from skb_clone_sk(), so it leaks.
ptp_schedule_worker() then runs against a destroyed kworker and a
released ptp_clock.
rtl8127a_ptp_rxtstamp(), called from NAPI, has the same problem. Its
skb_queue_tail() can run after the skb_queue_purge() here, and it also
schedules the destroyed worker.
rtl8127a_ptp_hwtstamp_set() runs under RTNL, which remove() does not
take. Could it re-enable the block after rtl8127a_ptp_hwtstamp_disable(),
or take ptp->lock after devm has freed it?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204021116.66-1-javen_xu%40realsil.com.cn