Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider. Critical: 0 · High: 1 · Medium: 4 · 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). - [High] rtl8127a_ptp_remove() (lines 790-807) unpublishes the mii_timestamper without synchronization and leaves the core's hwprov… - [Medium] When the core is in its default configuration, the new mii_timestamper never receives any traffic or configuration requests. - [Medium] rtl8127a_ptp_rx_tstamp() (lines 490-511) latches the per-type RX timestamp (SEL_RX|MSGTYPE|EXEC), reads TRX_TS_TIME and writes the… - [Medium] In the TX path, rtl8127a_ptp_tx_tstamp() and rtl8127a_ptp_egress_time() take INSR.INT_TX_TS together with the per-… - [Medium] INSR has a single INT_TX_TS bit for all four TX event message types, while TRX_TS_STA has a separate TX_*_RDY bit per type (bits… - [Low] In rtl8127a_ptp_hwtstamp_set() (lines 697-708), when __rtl8127a_ptp_hwtstamp_enable() fails, the error path sets… - [Low] The single TX slot is handed from rtl8127a_ptp_txtstamp() to the aux worker without a lock, and only the first consumer read pairs with… - [Low] rtl8127a_ptp_trx_ts_cmd() (lines 237-240) does a read-modify- write of RTL8127A_PTP_TRX_TS_STA through rtl8127a_ptp_modify(). - [Low] rtl8127a_ptp_link_change_notify() (lines 548-567) treats a cleared CTL_EN on link-up, while hw_enabled is set, as meaning 'the… -- net: phy: realtek: add hardware timestamping for RTL8127a This patch registers a mii_timestamper for the RTL8127a internal PHY so that it can timestamp PTPv2 event messages over UDP. The timestamps are read over MDIO from the PTP aux worker. RX frames are queued until their timestamp has been read, and only one TX timestamp can be pending at a time.
> diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c > index 72d0663a90c20..eff057e14c101 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: Low] TRX_TS_STA mixes command bits (RD, SEL_RX, MSGTYPE, EXEC) with the TX_*_RDY status flags in bits 12-15. The mask passed to rtl8127a_ptp_modify() clears only SEL_RX and MSGTYPE. Won't any RDY bits that are set when the register is read be written back together with the latch command? If those flags are write-1-to-clear, consider an RX latch from rtl8127a_ptp_rx_tstamp() while a tracked TX timestamp is pending, for example a Sync received while a Delay_Req is outstanding. The RX latch could clear the TX RDY bit. The 1 ms RDY poll in rtl8127a_ptp_egress_time() would then fail and the TX timestamp would be dropped. If the flags are read-only, how do they ever get cleared? Should the status bits be masked out of the write-back, or should their clearing semantics be handled explicitly? [ ... ]
> @@ -323,6 +416,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) {
[Severity: Low] Only the first lockless check pairs with the smp_store_release() in rtl8127a_ptp_txtstamp(). This re-check under the mutex is a plain load. Can it see a different skb than the one acquired above, without ordering the later loads of tx_start and tx_msgtype? aux worker: smp_load_acquire() sees skb1, blocks on ptp->lock hwtstamp_set(): TX_OFF, completes skb1, clears TX_IN_PROGRESS hwtstamp_set(): TX_ON txtstamp(): on another CPU, arms skb2 via smp_store_release() aux worker: takes ptp->lock, plain load sees skb2 mutex_lock() orders the worker only against the previous lock holder, not against txtstamp(). On weakly ordered CPUs the reads of tx_start and tx_msgtype could still return skb1's values. A stale tx_msgtype would latch the wrong message type, and a stale tx_start could time skb2 out early. rtl8127a_ptp_do_aux_work() also reads ptp->tx_start without READ_ONCE() while txtstamp() may be writing it, which KCSAN would report as a data race. Would smp_load_acquire() for this re-check, plus READ_ONCE() and WRITE_ONCE() for tx_start, be enough?
> + mutex_unlock(&ptp->lock); > + return true; > + } > + > + /* 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] Could INT_TX_TS here, and the per-type TX_*_RDY bit polled in rtl8127a_ptp_egress_time(), belong to an earlier frame rather than the tracked one? Neither bit, nor the latch, is reset when rtl8127a_ptp_txtstamp() starts tracking a new skb. txtstamp() also schedules the worker with delay 0 from the xmit path, before the frame has reached the PHY. The hardware presumably stamps every PTPv2 UDP event frame it transmits, including frames the driver does not track: - frames txtstamp() dropped because TX_IN_PROGRESS was already set - frames sent without SKBTX_HW_TSTAMP - a tracked frame that timed out or was dropped on TX_OFF and went out later (the branch above only acks INT_TX_TS) - frames that went out after the worker's INSR ack Each of these could leave INT_TX_TS asserted and a RDY bit set over an old latch value. The next tracked frame of that type would pass this check at once and the RDY poll would succeed immediately. skb_complete_tx_timestamp() would then report the earlier frame's egress time. Any unprivileged local process can send a UDP datagram to port 319 with a PTPv2 event header. Could that inject wrong TX timestamps into a privileged PTP daemon? The Kconfig note that "a single PTP application on the link is assumed" doesn't seem to cover this case.
> + > + 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);
[Severity: Medium] INSR has a single INT_TX_TS bit for all four event message types, while TRX_TS_STA has one RDY bit per type. Suppose INT_TX_TS was raised by a different message type than the tracked one. This code acks it, and rtl8127a_ptp_egress_time() then polls the tracked type's RDY bit for only the 1 ms budget of rtl8127a_ptp_poll(). The tracked frame could still be queued in the MAC, for example behind a busy TX ring or because of pause frames. Won't the poll then return -ETIMEDOUT, log a "poll failed" error, and drop the skb, even though RTL8127A_PTP_TX_TIMEOUT has not expired? The timestamp the hardware produces later would then stay in the latch and RDY bit as stale state for the next request. Should "INT_TX_TS set but RDY clear" be treated as not ready yet (return false) instead of as a final failure? [ ... ]
> +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] Does anything here confirm that the hardware actually captured a new timestamp for this frame? The latched value is written into skb_hwtstamps() unconditionally. __rtl8127a_ptp_hwtstamp_enable() enables INT_RX_TS in INER, but nothing ever reads or acknowledges it in INSR. Once it fires, it stays set. The TX path at least checks INSR and a per-type RDY bit. There are a couple of ways a stale value could end up in the latch. rtl8127a_ptp_rxtstamp() rejects only PTP_CLASS_L2, so IPv6 and VLAN-tagged UDP PTPv2 frames are queued. It isn't clear that the CTL_TYPE_3 | BIT(12) configuration stamps those frames. rxtstamp() also checks rx_enabled without the lock before queueing the skb. rtl8127a_ptp_hwtstamp_set(FILTER_NONE) can then clear CTL_EN and pulse PHY_RST. This function latches from the disabled block without rechecking hw_enabled or rx_enabled under ptp->lock. In each case the frame would be delivered with an earlier frame's timestamp, or a value read after the reset, as a valid hardware timestamp. The commit message only says that a later message of the same type can overwrite the latch. Can freshness be checked here, or should the limitation be documented? [ ... ]
> @@ -331,8 +542,207 @@ static const struct ptp_clock_info rtl8127a_ptp_info = {
[ ... ]
> +void rtl8127a_ptp_link_change_notify(struct phy_device *phydev) > +{ > + struct rtl8127a_ptp *ptp = phydev->priv; > + int ret = 0; > + > + if (!ptp || !phydev->link) > + return; > + > + mutex_lock(&ptp->lock); > + /* 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);
[Severity: Low] If a cleared CTL_EN means the PTP block was reset, could the same reset also have cleared the clock state? Only the timestamp enable sequence is rerun here. The rate programmed by rtl8127a_ptp_adjfine() through RTL8127A_PTP_RATE_WRITE, and the time set by settime/adjtime, are not restored. The driver also keeps no copy of the last rate. A tx_skb pending across the reset is not invalidated either, so it only finishes at the 15 second timeout. Would the PHC then run at the nominal rate while the PTP stack assumes its last adjustment still applies? [ ... ]
> + /* 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. > + */ > + if (enable) > + __rtl8127a_ptp_hwtstamp_enable(ptp, false); > + ptp->hw_enabled = false;
[Severity: Low] Can hw_enabled be set to false here while the hardware is still on? - If enable is false and the disable sequence fails part-way (for example after the SYNCE_CTL or INER modify), CTL_EN is never cleared. - If enable is true and this rollback call fails, its return value is ignored. In both cases the INER RX/TX interrupt enables and CTL_EN could stay set. The registers are only reprogrammed when enable != ptp->hw_enabled, so a later TX_OFF/FILTER_NONE request makes no register access at all. The block would keep timestamping with its interrupts enabled while the driver reports timestamping as off. [ ... ]
> @@ -362,7 +775,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: Medium] phydev->default_timestamp is not set, so phy_is_default_hwtstamp() returns false. Is this timestamper used at all in the default configuration? net/core/dev_ioctl.c:dev_set_hwtstamp_phylib() { ... phy_ts = phy_is_default_hwtstamp(dev->phydev); ... if (!phy_ts || dev->see_all_hwtstamp_requests) { if (!ops->ndo_hwtstamp_set) return -EOPNOTSUPP; ... } r8169 has no ndo_hwtstamp_set/get. That leads to three problems: - SIOCSHWTSTAMP from ptp4l would return -EOPNOTSUPP. - __ethtool_get_ts_info() would report only software timestamping, with phc_index -1. - skb_clone_tx_timestamp() and skb_defer_rx_timestamp() return early, so rtl8127a_ptp_txtstamp() and rtl8127a_ptp_rxtstamp() are never called. Timestamping only works once user space selects the PHY as the hwtstamp provider with ETHTOOL_MSG_TSCONFIG_SET. bcm-phy-ptp, dp83640, micrel, microchip_rds_ptp, mscc_ptp and nxp-c45-tja11xx all set default_timestamp = true. The v5 changelog says default_timestamp was dropped on purpose. However, the new Kconfig help says the PHY timestamps "on behalf of the MAC, so no support from the MAC driver is needed", and the commit message makes a similar claim. Should default_timestamp be set, or should the need to select the provider explicitly be documented?
> @@ -374,7 +794,14 @@ void rtl8127a_ptp_remove(struct phy_device *phydev) > if (!ptp) > return; > > + phydev->mii_ts = NULL; > + > rtl8127a_ptp_hwtstamp_enable(ptp, false); > + /* 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: High] Does anything wait for rtl8127a_ptp_rxtstamp() or rtl8127a_ptp_txtstamp() calls that are already in flight before this teardown? skb_defer_rx_timestamp() and skb_clone_tx_timestamp() load phydev->mii_ts and call through it. A CPU that loaded the pointer before the plain store above can still be running the callbacks: - skb_queue_tail() after skb_queue_purge() leaks the skb. - smp_store_release(&ptp->tx_skb) after the tx_skb check here leaks the clone and its socket reference. - ptp_schedule_worker(ptp->clock, 0) after ptp_clock_unregister() has called kthread_destroy_worker() would use a freed kworker. - Once remove returns, devres frees struct rtl8127a_ptp, so later accesses to ptp->state, tx_type or rx_queue.lock would be use-after-free. v4 had synchronize_net() here, and v5 removed it. rtl8127a_ptp_tx_complete() is also called here without ptp->lock. Could a concurrent rtl8127a_ptp_hwtstamp_set() that turns TX off see the same tx_skb and free it a second time? Separately, phy_remove() calls drv->remove() without calling phy_detach(), and only phy_detach() clears dev->hwprov. After a sysfs unbind of the PHY driver while the netdev is up, hwprov would still select this phydev. The hwprov branch of skb_defer_rx_timestamp() does this: mii_ts = phydev->mii_ts; if (likely(mii_ts->rxtstamp)) return mii_ts->rxtstamp(mii_ts, skb, type); There is no NULL check. Wouldn't the next PTP packet received from any peer then dereference NULL in softirq context? The missing NULL check in the core predates this patch (dp83640 clears mii_ts the same way). However, explicit hwprov selection is the only way to use this driver's timestamping, so the crash is directly reachable here.
-- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/2026…%40realsil.com.cn