net: sparx5/lan969x: release the in-flight slot of a stale TX skb
Subsystems (3)
Daniel Machon
maintainerPATCH 1/1 net: sparx5/lan969x: release the in-flight slot of a stale TX skb
sparx5_ptp_txtstamp_request() counts every two step TX skb waiting for its timestamp in sparx5->ptp_skbs, and refuses new ones once SPARX5_MAX_PTP_ID are in flight. sparx5_ptp_txtstamp_old_release() frees the skbs that have waited longer than SPARX5_PTP_TIMEOUT.
Before this patch, old_release() does not decrement ptp_skbs, so every skb whose timestamp never arrives, for example because the switch discarded the frame, keeps its slot forever. Once SPARX5_MAX_PTP_ID slots are lost, two step frames fail to transmit on every port of the switch.
Return the number of released skbs from old_release() and subtract it from ptp_skbs in txtstamp_request(), under the ptp_ts_id_lock it already takes.
This fixes the accounting only. A port's stale skbs are still aged out only when that port sends a two step frame, so a port that stops sending them keeps its stale skbs, and their slots, until it sends one again. That behaviour is unchanged by this patch.
Found by an AI code review agent. Reproduced on a lan9696 board with a modified kernel that drops the TX timestamps of two step frames, after which two step timestamping stopped working on the port. With the fix, timestamping keeps working on that board and on a sparx5 PCB135 board.
--- drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c +8 -2
@@ -204,9 +204,10 @@ static void sparx5_ptp_classify(struct sparx5_port *port, struct sk_buff *skb, *rew_op =IFH_REW_OP_TWO_STEP_PTP ; } -static void sparx5_ptp_txtstamp_old_release (struct sparx5_port *port) +static unsigned int sparx5_ptp_txtstamp_old_release (struct sparx5_port *port) {struct sk_buff *skb, *skb_tmp; +unsigned int purged =0 ;unsigned long flags;spin_lock_irqsave (&port->tx_skbs.lock, flags);
@@ -217,8 +218,11 @@ static void sparx5_ptp_txtstamp_old_release(struct sparx5_port *port)__skb_unlink (skb, &port->tx_skbs);dev_kfree_skb_any (skb); + purged++; }spin_unlock_irqrestore (&port->tx_skbs.lock, flags); + +return purged; } int sparx5_ptp_txtstamp_request(struct sparx5_port *port,
@@ -226,6 +230,7 @@ int sparx5_ptp_txtstamp_request(struct sparx5_port *port, {struct sparx5 *sparx5 = port->sparx5;u8 rew_op, pdu_type, pdu_w16_offset; +unsigned int purged;unsigned long flags;sparx5_ptp_classify (port, skb, &rew_op, &pdu_type, &pdu_w16_offset);
@@ -236,9 +241,10 @@ int sparx5_ptp_txtstamp_request(struct sparx5_port *port,if (rew_op !=IFH_REW_OP_TWO_STEP_PTP )return 0 ; -sparx5_ptp_txtstamp_old_release (port); + purged =sparx5_ptp_txtstamp_old_release (port);spin_lock_irqsave (&sparx5->ptp_ts_id_lock, flags); + sparx5->ptp_skbs -= purged;if (sparx5->ptp_skbs ==SPARX5_MAX_PTP_ID ) {spin_unlock_irqrestore (&sparx5->ptp_ts_id_lock, flags);return -EBUSY ;
---
base-commit: 829a09df899dcce860dcf5f70e09c1b2d7d425ab
change-id: 20261020-sparx5-ptp-stale-tx-slot-e52977bdddb2
Best regards,
Signature · 1 line
Daniel Machon <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 (1) · High: 0 (2) · Medium: 1 (1) · Low: 0 (1)
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] This patch adds a second ptp_skbs decrement path that is not coordinated with sparx5_ptp_txtstamp_release().
Pre-existing issues:
- [Critical] Double free and use-after-free of two-step PTP skbs that go through the drop path of sparx5_port_xmit_impl().
- [High] Use-after-free in lan969x_fdma_tx_clear_buf() (lan969x/lan969x_fdma.c:70-77) for two-step PTP skbs.
- [High] sparx5_ptp_txtstamp_request() adds the skb to port->tx_skbs with skb_queue_tail() (sparx5_ptp.c:255), which takes and releases…
- [Medium] Stale skbs, and their global ptp_skbs slots, are aged only when the same port issues another two-step request.
- [Low] The 10 ms SPARX5_PTP_TIMEOUT used by sparx5_ptp_txtstamp_old_release() treats a frame held in an egress…
--
net: sparx5/lan969x: release the in-flight slot of a stale TX skb
This patch makes sparx5_ptp_txtstamp_old_release() return how many stale two step TX skbs it freed. sparx5_ptp_txtstamp_request() then subtracts that number from sparx5->ptp_skbs under ptp_ts_id_lock. Without this, an skb whose timestamp never arrived kept one of the SPARX5_MAX_PTP_ID in-flight slots forever.
Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c +0
-0
[ ... ]
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c static void sparx5_ptp_txtstamp_old_release(struct sparx5_port *port) · line 222
Quoted 6 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c +1
-0@@ -217,8 +218,11 @@ static void sparx5_ptp_txtstamp_old_release(struct sparx5_port *port)__skb_unlink (skb, &port->tx_skbs);dev_kfree_skb_any (skb); + purged++; }
[Severity: Critical]
This isn't a bug introduced by this patch, but can this walk free a two step skb a second time?
In sparx5_port_xmit_impl(), sparx5_ptp_txtstamp_request() has already queued the skb on port->tx_skbs and incremented ptp_skbs. If the backend then fails with anything other than -EBUSY, the drop label only frees the skb:
drivers/net/ethernet/microchip/sparx5/sparx5_packet.c:sparx5_port_xmit_impl() {
...
if (ret == -EBUSY)
goto busy;
if (ret < 0)
goto drop;
...
drop:
stats->tx_dropped++;
sparx5->tx.dropped++;
dev_kfree_skb_any(skb);
return NETDEV_TX_OK;
busy:
if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
SPARX5_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP)
sparx5_ptp_txtstamp_release(port, skb);
return NETDEV_TX_BUSY;
}Only the busy path calls sparx5_ptp_txtstamp_release(). On the drop path the freed skb stays linked on port->tx_skbs.
On sparx5, sparx5_fdma_xmit() returns -EINVAL when the TX ring is full:
if (!fdma_db_is_done(fdma_db_get(fdma, fdma->dcb_index, 0))) return -EINVAL;
On lan969x, lan969x_fdma_xmit() can return -ENOMEM from pskb_expand_head(GFP_ATOMIC) or on dma_mapping_error().
The next two step xmit on that port, at least SPARX5_PTP_TIMEOUT later, would reach sparx5_ptp_txtstamp_old_release(). It would read SPARX5_SKB_CB(skb)->jiffies and skb->next from freed memory, call __skb_unlink() on it, and call dev_kfree_skb_any() on it again. The PTP IRQ handlers also walk this list and read cb->ts_id of the freed skb.
With this patch the double-freed entry is also counted in purged, so its slot gets reclaimed through the double free.
Should the drop path call sparx5_ptp_txtstamp_release() for two step skbs, as the busy path does?
[Severity: High]
This is a pre-existing issue, but on lan969x, can the FDMA TX ring still reference the skb freed here?
lan969x_fdma_xmit() stores the two step skb in the ring with db_buf->skb = skb and db_buf->ptp = true, and the PTP code is responsible for freeing it. This function and lan969x_ptp_irq_handler() both free the skb without clearing db->skb or checking whether the DCB has completed.
lan969x_fdma_tx_clear_buf() then dereferences db->skb before it checks db->ptp:
drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c:lan969x_fdma_tx_clear_buf() {
...
db->dev->stats.tx_bytes += db->skb->len;
db->dev->stats.tx_packets++;
sparx5->tx.packets++;
dma_unmap_single(sparx5->dev,
db->dma_addr,
db->skb->len,
DMA_TO_DEVICE);
if (!db->ptp)
napi_consume_skb(db->skb, weight);
...
}The PTP IRQ is a separate threaded IRQ with no ordering against NAPI. A delayed NAPI poll could therefore read skb->len from freed memory.
If old_release frees an skb whose DCB has not completed yet, for example because the FDMA stalled for more than 10 ms, wouldn't the device still DMA-read the freed buffer?
[Severity: Low]
This is a pre-existing issue, but is a fixed 10 ms SPARX5_PTP_TIMEOUT long enough here?
A frame held in an egress queue for more than 10 ms, for example behind a closed TAS gate or by pause frames, would be freed here as lost. When it later egresses, sparx5_ptp_irq_handler() and lan969x_ptp_irq_handler() find no matching skb and hit:
if (WARN_ON(!skb_match)) continue;
The continue skips the ptp_skbs decrement, so the counter itself stays consistent with port->tx_skbs.
[ ... ]
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c int sparx5_ptp_txtstamp_request(struct sparx5_port *port, · line 244
Quoted 6 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c +1
-1@@ -236,9 +241,10 @@ int sparx5_ptp_txtstamp_request(struct sparx5_port *port,if (rew_op !=IFH_REW_OP_TWO_STEP_PTP )return 0 ; -sparx5_ptp_txtstamp_old_release (port); + purged =sparx5_ptp_txtstamp_old_release (port);
[Severity: Medium]
This is a pre-existing issue, and the commit message already mentions it. Stale skbs, and their global ptp_skbs slots, are only aged when the same port sends another two step frame.
sparx5_ptp_hwtstamp_set() with HWTSTAMP_TX_OFF does not purge port->tx_skbs, and neither does port stop. Only sparx5_ptp_deinit() does. A port that stops sending two step frames, because the link went down or TX timestamping was turned off, keeps any lost skbs that had not expired at its last request.
If these leftovers add up to SPARX5_MAX_PTP_ID across ports, two step xmit on every other port gets -EBUSY. sparx5_port_xmit_impl() turns that into NETDEV_TX_BUSY.
Could port->tx_skbs also be purged from a path that does not depend on the port's own traffic, such as HWTSTAMP_TX_OFF or port stop?
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c int sparx5_ptp_txtstamp_request(struct sparx5_port *port, · line 247
Quoted 3 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c +1
-0spin_lock_irqsave (&sparx5->ptp_ts_id_lock, flags); + sparx5->ptp_skbs -= purged;
[Severity: Medium]
Can this decrement race with sparx5_ptp_txtstamp_release() and count the same skb down twice?
Each port netdev has SPX5_PRIOS TX queues and there is no ndo_select_queue, so two CPUs can run sparx5_port_xmit_impl() for the same port at the same time. sparx5_ptp_txtstamp_old_release() unlinks while holding only tx_skbs.lock. The busy path decrements without checking whether the skb is still on the list:
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c:sparx5_ptp_txtstamp_release() {
...
spin_lock_irqsave(&sparx5->ptp_ts_id_lock, flags);
port->ts_id--;
sparx5->ptp_skbs--;
skb_unlink(skb, &port->tx_skbs);
spin_unlock_irqrestore(&sparx5->ptp_ts_id_lock, flags);
}A possible sequence:
CPU0sparx5_ptp_txtstamp_request()
skb_queue_tail(&port->tx_skbs, X)
sparx5->ptp_skbs++spin_lock(&sparx5->tx_lock) /* waits */
CPU1sparx5_ptp_txtstamp_request()
sparx5_ptp_txtstamp_old_release()
/* X looks older than SPARX5_PTP_TIMEOUT */
__skb_unlink(X); dev_kfree_skb_any(X); purged++
sparx5->ptp_skbs -= purgedCPU0ops->fdma_xmit() or sparx5_inject() returns -EBUSY sparx5_ptp_txtstamp_release(port, X)
sparx5->ptp_skbs--
At HZ=100, SPARX5_PTP_TIMEOUT is one jiffy, so crossing a single tick is enough for X to look expired.
Before this patch the purge path did not decrement, so X was counted down only once. ptp_skbs is a u16 and the limit check only tests for equality.
If the counter wraps to 65535, does the SPARX5_MAX_PTP_ID limit stop working, so that ts_id values can be reused while they are still outstanding?
The same window already lets CPU0 keep using X after CPU1 has freed it. That part predates this patch; the double decrement is new.
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c int sparx5_ptp_txtstamp_request(struct sparx5_port *port, · line 250
Quoted 3 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c +0
-0if (sparx5->ptp_skbs ==SPARX5_MAX_PTP_ID ) {spin_unlock_irqrestore (&sparx5->ptp_ts_id_lock, flags);return -EBUSY ;
[Severity: High]
This isn't a bug introduced by this patch, but a little further down in sparx5_ptp_txtstamp_request() the skb is added to port->tx_skbs before its cb fields are written:
skb_queue_tail(&port->tx_skbs, skb); SPARX5_SKB_CB(skb)->ts_id = port->ts_id; SPARX5_SKB_CB(skb)->jiffies = jiffies;
skb_queue_tail() takes and releases tx_skbs.lock, and the cb writes happen under ptp_ts_id_lock only. sparx5_ptp_txtstamp_old_release() and both PTP IRQ handlers walk the list holding only tx_skbs.lock.
With several TX queues per port, another CPU could run old_release on the same port and read X's cb->jiffies before it is set. That field still holds leftover bytes from earlier layers such as the qdisc. If the value looks expired, would old_release free X while CPU0 is still filling in the cb and passing X to the injector or FDMA? The IRQ handlers could also match a stale cb->ts_id the same way.
Would writing ts_id and jiffies before skb_queue_tail() close this?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261029-sparx5-ptp-stale-tx-slot-v1-1-ec15f391bbb7%40microchip.com
Daniel Machon
maintainerQuoted 227 lines
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 (1) · High: 0 (2) · Medium: 1 (1) · Low: 0 (1)
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] This patch adds a second
ptp_skbsdecrement path that is not coordinated withsparx5_ptp_txtstamp_release().Pre-existing issues:
- [Critical] Double free and use-after-free of two-step PTP skbs that go through the drop path ofsparx5_port_xmit_impl().
- [High] Use-after-free inlan969x_fdma_tx_clear_buf()(lan969x/lan969x_fdma.c:70-77) for two-step PTP skbs.
- [High]sparx5_ptp_txtstamp_request()adds the skb toport->tx_skbswithskb_queue_tail()(sparx5_ptp.c:255), which takes and releases…
- [Medium] Stale skbs, and their globalptp_skbsslots, are aged only when the same port issues another two-step request.
- [Low] The 10 msSPARX5_PTP_TIMEOUTused bysparx5_ptp_txtstamp_old_release()treats a frame held in an egress…--
net: sparx5/lan969x: release the in-flight slot of a stale TX skb
This patch makes
sparx5_ptp_txtstamp_old_release()return how many stale two step TX skbs it freed.sparx5_ptp_txtstamp_request()then subtracts that number fromsparx5->ptp_skbsunderptp_ts_id_lock. Without this, an skb whose timestamp never arrived kept one of theSPARX5_MAX_PTP_IDin-flight slots forever.Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c +0
-0[ ... ]
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c static void sparx5_ptp_txtstamp_old_release(struct sparx5_port *port) · line 222
Quoted 6 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c +1
-0@@ -217,8 +218,11 @@ static void sparx5_ptp_txtstamp_old_release(struct sparx5_port *port)__skb_unlink (skb, &port->tx_skbs);dev_kfree_skb_any (skb); + purged++; }[Severity: Critical]
This isn't a bug introduced by this patch, but can this walk free a two step skb a second time?In
sparx5_port_xmit_impl(),sparx5_ptp_txtstamp_request()has already queued the skb onport->tx_skbsand incrementedptp_skbs. If the backend then fails with anything other than-EBUSY, the drop label only frees the skb:drivers/net/ethernet/microchip/sparx5/sparx5_packet.c:sparx5_port_xmit_impl() { ... if (ret == -EBUSY) goto busy; if (ret < 0) goto drop; ... drop: stats->tx_dropped++; sparx5->tx.dropped++; dev_kfree_skb_any(skb); return NETDEV_TX_OK; busy: if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && SPARX5_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP) sparx5_ptp_txtstamp_release(port, skb); return NETDEV_TX_BUSY; }Only the busy path calls
sparx5_ptp_txtstamp_release(). On the drop path the freed skb stays linked onport->tx_skbs.On sparx5,
sparx5_fdma_xmit()returns-EINVALwhen the TX ring is full:if (!fdma_db_is_done(fdma_db_get(fdma, fdma->dcb_index, 0))) return -EINVAL;On lan969x,
lan969x_fdma_xmit()can return-ENOMEMfrompskb_expand_head(GFP_ATOMIC)or ondma_mapping_error().The next two step xmit on that port, at least
SPARX5_PTP_TIMEOUTlater, would reachsparx5_ptp_txtstamp_old_release(). It would read SPARX5_SKB_CB(skb)->jiffies andskb->nextfrom freed memory, call__skb_unlink()on it, and calldev_kfree_skb_any()on it again. The PTP IRQ handlers also walk this list and readcb->ts_idof the freed skb.With this patch the double-freed entry is also counted in purged, so its slot gets reclaimed through the double free.
Should the drop path call
sparx5_ptp_txtstamp_release()for two step skbs, as the busy path does?[Severity: High]
This is a pre-existing issue, but on lan969x, can the FDMA TX ring still reference the skb freed here?
lan969x_fdma_xmit()stores the two step skb in the ring withdb_buf->skb= skb anddb_buf->ptp= true, and the PTP code is responsible for freeing it. This function andlan969x_ptp_irq_handler()both free the skb without clearingdb->skbor checking whether the DCB has completed.
lan969x_fdma_tx_clear_buf()then dereferencesdb->skbbefore it checksdb->ptp:drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c:lan969x_fdma_tx_clear_buf() { ... db->dev->stats.tx_bytes += db->skb->len; db->dev->stats.tx_packets++; sparx5->tx.packets++; dma_unmap_single(sparx5->dev, db->dma_addr, db->skb->len, DMA_TO_DEVICE); if (!db->ptp) napi_consume_skb(db->skb, weight); ... }The PTP IRQ is a separate threaded IRQ with no ordering against NAPI. A delayed NAPI poll could therefore read
skb->lenfrom freed memory.If
old_releasefrees an skb whose DCB has not completed yet, for example because the FDMA stalled for more than 10 ms, wouldn't the device still DMA-read the freed buffer?[Severity: Low]
This is a pre-existing issue, but is a fixed 10 msSPARX5_PTP_TIMEOUTlong enough here?A frame held in an egress queue for more than 10 ms, for example behind a closed TAS gate or by pause frames, would be freed here as lost. When it later egresses,
sparx5_ptp_irq_handler()andlan969x_ptp_irq_handler()find no matching skb and hit:if (WARN_ON(!skb_match)) continue;The continue skips the
ptp_skbsdecrement, so the counter itself stays consistent withport->tx_skbs.[ ... ]
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c int sparx5_ptp_txtstamp_request(struct sparx5_port *port, · line 244
Quoted 6 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c +1
-1@@ -236,9 +241,10 @@ int sparx5_ptp_txtstamp_request(struct sparx5_port *port,if (rew_op !=IFH_REW_OP_TWO_STEP_PTP )return 0 ; -sparx5_ptp_txtstamp_old_release (port); + purged =sparx5_ptp_txtstamp_old_release (port);[Severity: Medium]
This is a pre-existing issue, and the commit message already mentions it. Stale skbs, and their globalptp_skbsslots, are only aged when the same port sends another two step frame.
sparx5_ptp_hwtstamp_set()withHWTSTAMP_TX_OFFdoes not purgeport->tx_skbs, and neither does port stop. Onlysparx5_ptp_deinit()does. A port that stops sending two step frames, because the link went down or TX timestamping was turned off, keeps any lost skbs that had not expired at its last request.If these leftovers add up to
SPARX5_MAX_PTP_IDacross ports, two step xmit on every other port gets-EBUSY.sparx5_port_xmit_impl()turns that intoNETDEV_TX_BUSY.Could
port->tx_skbsalso be purged from a path that does not depend on the port's own traffic, such asHWTSTAMP_TX_OFFor port stop?drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c int sparx5_ptp_txtstamp_request(struct sparx5_port *port, · line 247
Quoted 3 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c +1
-0spin_lock_irqsave (&sparx5->ptp_ts_id_lock, flags); + sparx5->ptp_skbs -= purged;[Severity: Medium]
Can this decrement race withsparx5_ptp_txtstamp_release()and count the same skb down twice?Each port netdev has
SPX5_PRIOSTX queues and there is nondo_select_queue, so two CPUs can runsparx5_port_xmit_impl()for the same port at the same time.sparx5_ptp_txtstamp_old_release()unlinks while holding only tx_skbs.lock. The busy path decrements without checking whether the skb is still on the list:drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c:sparx5_ptp_txtstamp_release() { ... spin_lock_irqsave(&sparx5->ptp_ts_id_lock, flags); port->ts_id--; sparx5->ptp_skbs--; skb_unlink(skb, &port->tx_skbs); spin_unlock_irqrestore(&sparx5->ptp_ts_id_lock, flags); }A possible sequence:
CPU0
sparx5_ptp_txtstamp_request()skb_queue_tail(&port->tx_skbs, X) sparx5->ptp_skbs++
spin_lock(&sparx5->tx_lock)/* waits */CPU1
sparx5_ptp_txtstamp_request()sparx5_ptp_txtstamp_old_release() /* X looks older than SPARX5_PTP_TIMEOUT */ __skb_unlink(X); dev_kfree_skb_any(X); purged++ sparx5->ptp_skbs -= purgedCPU0
ops->fdma_xmit()orsparx5_inject()returns-EBUSYsparx5_ptp_txtstamp_release(port, X)sparx5->ptp_skbs--At HZ=100,
SPARX5_PTP_TIMEOUTis one jiffy, so crossing a single tick is enough for X to look expired.Before this patch the purge path did not decrement, so X was counted down only once.
ptp_skbsis a u16 and the limit check only tests for equality.
Quoted 6 lines
If the counter wraps to 65535, does the
SPARX5_MAX_PTP_IDlimit stop working, so thatts_idvalues can be reused while they are still outstanding?The same window already lets CPU0 keep using X after CPU1 has freed it. That part predates this patch; the double decrement is new.
This fixes a leak seen in normal operation. Each lost timestamp keeps an in-flight slot, and after SPARX5_MAX_PTP_ID losses, two step timestamping stops on all ports. The lan966x twin (074caa30a627) is already in net.
AFAICT, the wrong count can never be used. The second decrement is in the busy path, which on the very next line unlinks an skb that a purge on another TX queue has already freed. That use-after-free is pre-existing and crashes before the count matters.
I am working on a net-next series that reworks PTP TX timestamping and fixes this and several related issues.
Quoted 29 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c int sparx5_ptp_txtstamp_request(struct sparx5_port *port, · line 250
Quoted 4 lines
drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c +0
-0if (sparx5->ptp_skbs ==SPARX5_MAX_PTP_ID ) {spin_unlock_irqrestore (&sparx5->ptp_ts_id_lock, flags);return -EBUSY ;[Severity: High]
This isn't a bug introduced by this patch, but a little further down insparx5_ptp_txtstamp_request()the skb is added toport->tx_skbsbefore its cb fields are written:skb_queue_tail(&port->tx_skbs, skb); SPARX5_SKB_CB(skb)->ts_id = port->ts_id; SPARX5_SKB_CB(skb)->jiffies = jiffies;
skb_queue_tail()takes and releases tx_skbs.lock, and the cb writes happen underptp_ts_id_lockonly.sparx5_ptp_txtstamp_old_release()and both PTP IRQ handlers walk the list holding only tx_skbs.lock.With several TX queues per port, another CPU could run
old_releaseon the same port and read X'scb->jiffiesbefore it is set. That field still holds leftover bytes from earlier layers such as the qdisc. If the value looks expired, wouldold_releasefree X while CPU0 is still filling in the cb and passing X to the injector or FDMA? The IRQ handlers could also match a stalecb->ts_idthe same way.Would writing
ts_idand jiffies beforeskb_queue_tail()close this?--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261029-sparx5-ptp-stale-tx-slot-v1-1-ec15f391bbb7%40microchip.com
On Mon, Nov 02, 2026 at 10:14:49AM +0100, Daniel Machon wrote:
...
Quoted 57 lines
Quoted 45 lines
[Severity: Medium]
Can this decrement race withsparx5_ptp_txtstamp_release()and count the same skb down twice?Each port netdev has
SPX5_PRIOSTX queues and there is nondo_select_queue, so two CPUs can runsparx5_port_xmit_impl()for the same port at the same time.sparx5_ptp_txtstamp_old_release()unlinks while holding only tx_skbs.lock. The busy path decrements without checking whether the skb is still on the list:drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c:sparx5_ptp_txtstamp_release() { ... spin_lock_irqsave(&sparx5->ptp_ts_id_lock, flags); port->ts_id--; sparx5->ptp_skbs--; skb_unlink(skb, &port->tx_skbs); spin_unlock_irqrestore(&sparx5->ptp_ts_id_lock, flags); }A possible sequence:
CPU0
sparx5_ptp_txtstamp_request()skb_queue_tail(&port->tx_skbs, X) sparx5->ptp_skbs++
spin_lock(&sparx5->tx_lock)/* waits */CPU1
sparx5_ptp_txtstamp_request()sparx5_ptp_txtstamp_old_release() /* X looks older than SPARX5_PTP_TIMEOUT */ __skb_unlink(X); dev_kfree_skb_any(X); purged++ sparx5->ptp_skbs -= purgedCPU0
ops->fdma_xmit()orsparx5_inject()returns-EBUSYsparx5_ptp_txtstamp_release(port, X)sparx5->ptp_skbs--At HZ=100,
SPARX5_PTP_TIMEOUTis one jiffy, so crossing a single tick is enough for X to look expired.Before this patch the purge path did not decrement, so X was counted down only once.
ptp_skbsis a u16 and the limit check only tests for equality.Quoted 6 lines
If the counter wraps to 65535, does the
SPARX5_MAX_PTP_IDlimit stop working, so thatts_idvalues can be reused while they are still outstanding?The same window already lets CPU0 keep using X after CPU1 has freed it. That part predates this patch; the double decrement is new.
This fixes a leak seen in normal operation. Each lost timestamp keeps an in-flight slot, and after
SPARX5_MAX_PTP_IDlosses, two step timestamping stops on all ports. The lan966x twin (074caa30a627) is already in net.
Quoted 7 lines
AFAICT, the wrong count can never be used. The second decrement is in the busy path, which on the very next line unlinks an skb that a purge on another TX queue has already freed. That use-after-free is pre-existing and crashes before the count matters.
I am working on a net-next series that reworks PTP TX timestamping and fixes this and several related issues.
Thanks Daniel,
I agree that is a good approach.
...
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:
Quoted 5 lines · On Thu, 29 Oct 2026 12:48:10 +0100 you wrote:
sparx5_ptp_txtstamp_request()counts every two step TX skb waiting for its timestamp insparx5->ptp_skbs, and refuses new ones onceSPARX5_MAX_PTP_IDare in flight.sparx5_ptp_txtstamp_old_release()frees the skbs that have waited longer thanSPARX5_PTP_TIMEOUT.
Quoted 7 lines
Before this patch,
old_release()does not decrementptp_skbs, so every skb whose timestamp never arrives, for example because the switch discarded the frame, keeps its slot forever. OnceSPARX5_MAX_PTP_IDslots are lost, two step frames fail to transmit on every port of the switch.[...]
Here is the summary with links:
- [net] net: sparx5/lan969x: release the in-flight slot of a stale TX skb
https://git.kernel.org/netdev/net/c/873363ac591a
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html