ibmveth: two TX and reset fixes
v2 cover ibmveth: two TX and reset fixes
Two independent bugs in ibmveth that exist on today's kernels.
1. After ethtool -L shrinks the TX queues, a sender that already picked a removed queue (AF_PACKET PACKET_QDISC_BYPASS, pktgen) can memcpy into a NULL long-term buffer. Drop that packet, and stop TX with netif_tx_disable() so a sender already in xmit is done before ethtool -L allocates or frees a buffer.
2. ibmveth_reset() closes and reopens the device without checking whether the admin already set it down, and can bring it back up. Skip the reopen if the device is not running.
Both came up during review of the v8 multi-queue series [1]: patch 1 on v8 06/15, patch 2 while splitting that series. Neither was reproduced; the fixes are based on analysis of the code paths, and each commit message says so. They carry Fixes: tags, but the triggers are narrow and the second multi-queue series builds on them, so please take them through net-next. The first multi-queue series, posted separately, does not depend on them.
Tested: on a PowerVM LPAR: ethtool -L TX shrink under traffic, ifdown/up under iperf3, and a queued reset while the admin has set the device down. v2 on QEMU pseries only: TX shrink and restore under UDP traffic, MTU change, open/close churn.
Changes in v2:
- 1/2: wake the TX queues under netif_tx_lock_bh(), and map a new long-term buffer before publishing its pointer (Sashiko)
- 2/2: say the bug was found during review and was not reproduced
PATCH v2 1/2 ibmveth: drop TX on a queue whose long term buffer was freed
ibmveth_set_channels() frees the TX long term buffer (LTB) of every queue it removes and then wakes all TX queues. A sender that picked its queue before the shrink, such as AF_PACKET with PACKET_QDISC_BYPASS or pktgen, can call ibmveth_start_xmit() on a removed queue after that, and memcpy() into a NULL LTB.
Read the LTB pointer once and drop the packet if it is NULL.
Stop TX with netif_tx_disable() instead of netif_tx_stop_all_queues(), so a sender already in ibmveth_start_xmit() has left before set_channels() allocates, maps or frees an LTB. On a grow, that sender could otherwise see a new LTB before it is DMA mapped, or one that the failure path frees again.
Map a new LTB before publishing its pointer with WRITE_ONCE(), and leave the pointer NULL if the mapping fails, so a sender can only see an LTB that is fully set up.
Wake the queues under netif_tx_lock_bh(). The LTB updates run with no txq lock held, and a sender that sees DRV_XOFF clear under its txq lock is not otherwise ordered against them. Taking every txq lock before the wake makes it see the cleared pointer on a shrink and the new pointer and DMA address on a grow.
This came up during review of the ibmveth multi-queue series. It was not reproduced; the fix is based on analysis of the code paths.
---
Changes in v2:
- wake the TX queues under netif_tx_lock_bh() (Sashiko)
- publish the LTB pointer only after it is mapped (Sashiko)
drivers/net/ethernet/ibm/ibmveth.c | 37 +++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 11 deletions(-)
drivers/net/ethernet/ibm/ibmveth.c +26 -11
@@ -599,7 +599,7 @@ static void ibmveth_free_tx_ltb(struct ibmveth_adapter *adapter, int idx)if (!ptr)return ; - adapter->tx_ltb_ptr[idx] =NULL ; +WRITE_ONCE (adapter->tx_ltb_ptr[idx],NULL );dma_unmap_single (&adapter->vdev->dev, adapter->tx_ltb_dma[idx], adapter->tx_ltb_size,DMA_TO_DEVICE );kfree (ptr);
@@ -607,25 +607,25 @@ static void ibmveth_free_tx_ltb(struct ibmveth_adapter *adapter, int idx)static int ibmveth_allocate_tx_ltb (struct ibmveth_adapter *adapter,int idx) { - adapter->tx_ltb_ptr[idx] =kzalloc (adapter->tx_ltb_size, -GFP_KERNEL ); -if (!adapter->tx_ltb_ptr[idx]) { +void *ptr; + + ptr =kzalloc (adapter->tx_ltb_size,GFP_KERNEL ); +if (!ptr) {netdev_err (adapter->netdev,"unable to allocate tx long term buffer\n" );return -ENOMEM ; } - adapter->tx_ltb_dma[idx] =dma_map_single (&adapter->vdev->dev, - adapter->tx_ltb_ptr[idx], + adapter->tx_ltb_dma[idx] =dma_map_single (&adapter->vdev->dev, ptr, adapter->tx_ltb_size,DMA_TO_DEVICE );if (dma_mapping_error (&adapter->vdev->dev, adapter->tx_ltb_dma[idx])) {netdev_err (adapter->netdev,"unable to DMA map tx long term buffer\n" ); -kfree (adapter->tx_ltb_ptr[idx]); - adapter->tx_ltb_ptr[idx] =NULL ; +kfree (ptr);return -ENOMEM ; } +WRITE_ONCE (adapter->tx_ltb_ptr[idx], ptr);return 0 ; }
@@ -1195,7 +1195,7 @@ static int ibmveth_set_channels(struct net_device *netdev,/* We have IBMVETH_MAX_QUEUES netdev_queue's allocated * but we may need to alloc/free the ltb's. */ -netif_tx_stop_all_queues (netdev); +netif_tx_disable (netdev);/* Allocate any queue that we need */ for (i = old; i < goal; i++) {
@@ -1229,7 +1229,12 @@ static int ibmveth_set_channels(struct net_device *netdev,ibmveth_free_tx_ltb (adapter, i -1 ); } +/* netif_tx_lock() takes each txq lock, so a sender that then sees +* DRV_XOFF clear under its txq lock also sees the LTB updates above. +*/ +netif_tx_lock_bh (netdev);netif_tx_wake_all_queues (netdev); +netif_tx_unlock_bh (netdev);return rc; }
@@ -1304,6 +1309,16 @@ static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,union ibmveth_buf_desc desc;int i, queue_num =skb_get_queue_mapping (skb);unsigned long mss =0 ; +void *ltb; + +/* A sender that picked its queue before ethtool -L shrank the TX +* queues can get here after that queue's LTB was freed. +*/ + ltb =READ_ONCE (adapter->tx_ltb_ptr[queue_num]); +if (unlikely (!ltb)) { + netdev->stats.tx_dropped++; + goto out; + }if (ibmveth_is_packet_unsupported (skb, netdev)) goto out;
@@ -1359,13 +1374,13 @@ static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb, netdev->stats.tx_dropped++; goto out; } -memcpy (adapter->tx_ltb_ptr[queue_num], skb->data,skb_headlen (skb)); +memcpy (ltb, skb->data,skb_headlen (skb)); total_bytes =skb_headlen (skb);/* Copy frags into mapped buffers */ for (i =0 ; i <skb_shinfo (skb)->nr_frags; i++) {const skb_frag_t *frag = &skb_shinfo (skb)->frags[i]; -memcpy (adapter->tx_ltb_ptr[queue_num] + total_bytes, +memcpy (ltb + total_bytes,skb_frag_address_safe (frag),skb_frag_size (frag)); total_bytes +=skb_frag_size (frag); }
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH net-next v2 1/2] ibmveth: drop TX on a queue whose long term buffer was freed
Quoted 106 lines
ibmveth_set_channels()frees the TX long term buffer (LTB) of every queue it removes and then wakes all TX queues. A sender that picked its queue before the shrink, such asAF_PACKETwithPACKET_QDISC_BYPASSor pktgen, can callibmveth_start_xmit()on a removed queue after that, andmemcpy()into a NULL LTB.Read the LTB pointer once and drop the packet if it is NULL.
Stop TX with
netif_tx_disable()instead ofnetif_tx_stop_all_queues(), so a sender already inibmveth_start_xmit()has left beforeset_channels()allocates, maps or frees an LTB. On a grow, that sender could otherwise see a new LTB before it is DMA mapped, or one that the failure path frees again.Map a new LTB before publishing its pointer with
WRITE_ONCE(), and leave the pointer NULL if the mapping fails, so a sender can only see an LTB that is fully set up.Wake the queues under
netif_tx_lock_bh(). The LTB updates run with no txq lock held, and a sender that seesDRV_XOFFclear under its txq lock is not otherwise ordered against them. Taking every txq lock before the wake makes it see the cleared pointer on a shrink and the new pointer and DMA address on a grow.This came up during review of the ibmveth multi-queue series. It was not reproduced; the fix is based on analysis of the code paths.
Fixes 10c2aba89cc0 ("ibmveth: Ethtool set queue support")Signed-off-by Mingming Cao <[email protected]>---
Changes in v2:
- wake the TX queues undernetif_tx_lock_bh()(Sashiko)
- publish the LTB pointer only after it is mapped (Sashiko)drivers/net/ethernet/ibm/ibmveth.c | 37 +++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 11 deletions(-)drivers/net/ethernet/ibm/ibmveth.c +14
-9@@ -599,7 +599,7 @@ static void ibmveth_free_tx_ltb(struct ibmveth_adapter *adapter, int idx)if (!ptr)return ; - adapter->tx_ltb_ptr[idx] =NULL ; +WRITE_ONCE (adapter->tx_ltb_ptr[idx],NULL );dma_unmap_single (&adapter->vdev->dev, adapter->tx_ltb_dma[idx], adapter->tx_ltb_size,DMA_TO_DEVICE );kfree (ptr);@@ -607,25 +607,25 @@ static void ibmveth_free_tx_ltb(struct ibmveth_adapter *adapter, int idx)static int ibmveth_allocate_tx_ltb (struct ibmveth_adapter *adapter,int idx) { - adapter->tx_ltb_ptr[idx] =kzalloc (adapter->tx_ltb_size, -GFP_KERNEL ); -if (!adapter->tx_ltb_ptr[idx]) { +void *ptr; + + ptr =kzalloc (adapter->tx_ltb_size,GFP_KERNEL ); +if (!ptr) {netdev_err (adapter->netdev,"unable to allocate tx long term buffer\n" );return -ENOMEM ; } - adapter->tx_ltb_dma[idx] =dma_map_single (&adapter->vdev->dev, - adapter->tx_ltb_ptr[idx], + adapter->tx_ltb_dma[idx] =dma_map_single (&adapter->vdev->dev, ptr, adapter->tx_ltb_size,DMA_TO_DEVICE );if (dma_mapping_error (&adapter->vdev->dev, adapter->tx_ltb_dma[idx])) {netdev_err (adapter->netdev,"unable to DMA map tx long term buffer\n" ); -kfree (adapter->tx_ltb_ptr[idx]); - adapter->tx_ltb_ptr[idx] =NULL ; +kfree (ptr);return -ENOMEM ; } +WRITE_ONCE (adapter->tx_ltb_ptr[idx], ptr);return 0 ; }@@ -1195,7 +1195,7 @@ static int ibmveth_set_channels(struct net_device *netdev,/* We have IBMVETH_MAX_QUEUES netdev_queue's allocated * but we may need to alloc/free the ltb's. */ -netif_tx_stop_all_queues (netdev); +netif_tx_disable (netdev);/* Allocate any queue that we need */ for (i = old; i < goal; i++) {@@ -1229,7 +1229,12 @@ static int ibmveth_set_channels(struct net_device *netdev,ibmveth_free_tx_ltb (adapter, i -1 ); } +/* netif_tx_lock() takes each txq lock, so a sender that then sees +* DRV_XOFF clear under its txq lock also sees the LTB updates above. +*/ +netif_tx_lock_bh (netdev);netif_tx_wake_all_queues (netdev); +netif_tx_unlock_bh (netdev);return rc; }
drivers/net/ethernet/ibm/ibmveth.c static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb, · line 1312
Quoted 4 lines
drivers/net/ethernet/ibm/ibmveth.c +1
-0@@ -1304,6 +1309,16 @@ static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,union ibmveth_buf_desc desc;int i, queue_num =skb_get_queue_mapping (skb);unsigned long mss =0 ; +void *ltb;
nit: this same pointer is called 'ptr' in ibmveth_allocate_tx_ltb, and here it is 'ltb'. I think the latter fits better.
Quoted 31 lines
drivers/net/ethernet/ibm/ibmveth.c +11
-2+ +/* A sender that picked its queue before ethtool -L shrank the TX +* queues can get here after that queue's LTB was freed. +*/ + ltb =READ_ONCE (adapter->tx_ltb_ptr[queue_num]); +if (unlikely (!ltb)) { + netdev->stats.tx_dropped++; + goto out; + }if (ibmveth_is_packet_unsupported (skb, netdev)) goto out;@@ -1359,13 +1374,13 @@ static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb, netdev->stats.tx_dropped++; goto out; } -memcpy (adapter->tx_ltb_ptr[queue_num], skb->data,skb_headlen (skb)); +memcpy (ltb, skb->data,skb_headlen (skb)); total_bytes =skb_headlen (skb);/* Copy frags into mapped buffers */ for (i =0 ; i <skb_shinfo (skb)->nr_frags; i++) {const skb_frag_t *frag = &skb_shinfo (skb)->frags[i]; -memcpy (adapter->tx_ltb_ptr[queue_num] + total_bytes, +memcpy (ltb + total_bytes,skb_frag_address_safe (frag),skb_frag_size (frag)); total_bytes +=skb_frag_size (frag); }Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 2/2 ibmveth: do not reopen a device that was set down before reset ran
In reply to Mingming Caoibmveth_reset() runs from a work item queued by the RX, poll and replenish paths. It calls dev_close() and dev_open() without checking whether the device is still up. If the admin sets the device down between queueing the work and the work taking rtnl_lock(), dev_close() does nothing and dev_open() brings the device back up.
Only close and reopen the device if it is running.
This came up during review of the ibmveth multi-queue series. It was not reproduced; the fix is based on analysis of the code paths.
---
Changes in v2:
- say the bug was found in review and not reproduced
drivers/net/ethernet/ibm/ibmveth.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ethernet/ibm/ibmveth.c +4 -2
@@ -904,8 +904,10 @@ static void ibmveth_reset(struct work_struct *w)rtnl_lock (); -dev_close (adapter->netdev); -dev_open (adapter->netdev,NULL ); +if (netif_running (netdev)) { +dev_close (netdev); +dev_open (netdev,NULL ); + }rtnl_unlock ();
Signature · 1 line
2.50.1 (Apple Git-155)
Hi Mingming
Quoted 20 lines
Two independent bugs in ibmveth that exist on today's kernels.
1. After ethtool -L shrinks the TX queues, a sender that already picked a removed queue (
AF_PACKETPACKET_QDISC_BYPASS, pktgen) can memcpy into a NULL long-term buffer. Drop that packet, and stop TX withnetif_tx_disable()so a sender already in xmit is done before ethtool -L allocates or frees a buffer.2.
ibmveth_reset()closes and reopens the device without checking whether the admin already set it down, and can bring it back up. Skip the reopen if the device is not running.Both came up during review of the v8 multi-queue series [1]: patch 1 on v8 06/15, patch 2 while splitting that series. Neither was reproduced; the fixes are based on analysis of the code paths, and each commit message says so. They carry Fixes: tags, but the triggers are narrow and the second multi-queue series builds on them, so please take them through net-next. The first multi-queue series, posted separately, does not depend on them.
Quoted 4 lines
Tested: on a PowerVM LPAR: ethtool -L TX shrink under traffic, ifdown/up under iperf3, and a queued reset while the admin has set the device down. v2 on QEMU pseries only: TX shrink and restore under UDP traffic, MTU change, open/close churn.
1/2 was pretty easy to reproduce in QEMU:
BUG: Kernel NULL pointer dereference on write at 0x00000000 NIP [c00000000009dac0] memcpy_power7+0x40/0x7d0 LR [c00800000079104c] ibmveth_start_xmit+0xd4/0x550 [ibmveth] Call Trace: pktgen_xmit+0x144/0x1490 [pktgen] pktgen_thread_worker+0x12c/0xaa8 [pktgen] kthread+0x124/0x130
With the series applied, the splat is gone.
Nit: in 1/2, the same buffer is called "ptr" in ibmveth_allocate_tx_ltb() and "ltb" in ibmveth_start_xmit().
Quoted 9 lines
Changes in v2:
- 1/2: wake the TX queues undernetif_tx_lock_bh(), and map a new long-term buffer before publishing its pointer (Sashiko)
- 2/2: say the bug was found during review and was not reproduced
Quoted 36 lines · Daniel Machon
Hi Mingming
Quoted 20 lines
Two independent bugs in ibmveth that exist on today's kernels.
1. After ethtool -L shrinks the TX queues, a sender that already picked a removed queue (
AF_PACKETPACKET_QDISC_BYPASS, pktgen) can memcpy into a NULL long-term buffer. Drop that packet, and stop TX withnetif_tx_disable()so a sender already in xmit is done before ethtool -L allocates or frees a buffer.2.
ibmveth_reset()closes and reopens the device without checking whether the admin already set it down, and can bring it back up. Skip the reopen if the device is not running.Both came up during review of the v8 multi-queue series [1]: patch 1 on v8 06/15, patch 2 while splitting that series. Neither was reproduced; the fixes are based on analysis of the code paths, and each commit message says so. They carry Fixes: tags, but the triggers are narrow and the second multi-queue series builds on them, so please take them through net-next. The first multi-queue series, posted separately, does not depend on them.
Quoted 4 lines
Tested: on a PowerVM LPAR: ethtool -L TX shrink under traffic, ifdown/up under iperf3, and a queued reset while the admin has set the device down. v2 on QEMU pseries only: TX shrink and restore under UDP traffic, MTU change, open/close churn.
1/2 was pretty easy to reproduce in QEMU:
BUG: Kernel NULL pointer dereference on write at 0x00000000 NIP [c00000000009dac0] memcpy_power7+0x40/0x7d0 LR [c00800000079104c] ibmveth_start_xmit+0xd4/0x550 [ibmveth] Call Trace: pktgen_xmit+0x144/0x1490 [pktgen] pktgen_thread_worker+0x12c/0xaa8 [pktgen] kthread+0x124/0x130
Quoted 4 lines
With the series applied, the splat is gone.
Nit: in 1/2, the same buffer is called "ptr" in
ibmveth_allocate_tx_ltb()and "ltb" inibmveth_start_xmit().
Hi Daniel,
Thanks for the review, and for reproducing 1/2 on QEMU. We only had the code analysis for it, so the pktgen splat in |ibmveth_start_xmit()| confirms the bug. It is also good to know the series fixes it.
You're right about the nit: |ibmveth_allocate_tx_ltb()| calls the buffer |ptr| and |ibmveth_start_xmit()| calls it |ltb|. Part 1 is already merged into net-next, so I won't send a separate patch for it. Series 2 only calls that function and doesn't change it. I'll add the rename to the series 2 patch that sits next to it if that looks fine to the reviewers, and otherwise leave it.
I'll also add your pktgen reproducer to my QEMU testing.
Quoted 9 lines
Quoted 8 lines
Changes in v2:
- 1/2: wake the TX queues undernetif_tx_lock_bh(), and map a new long-term buffer before publishing its pointer (Sashiko)
- 2/2: say the bug was found during review and was not reproducedReviewed-by Daniel Machon <[email protected]>
Thanks again,
Mingming
This series was applied to netdev/net-next.git (main) by Willem de Bruijn <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Willem de Bruijn <[email protected]>:
Quoted 9 lines · On Sun, 22 Nov 2026 23:51:00 -0800 you wrote:
Two independent bugs in ibmveth that exist on today's kernels.
1. After ethtool -L shrinks the TX queues, a sender that already picked a removed queue (
AF_PACKETPACKET_QDISC_BYPASS, pktgen) can memcpy into a NULL long-term buffer. Drop that packet, and stop TX withnetif_tx_disable()so a sender already in xmit is done before ethtool -L allocates or frees a buffer.[...]
Here is the summary with links:
- [net-next,v2,1/2] ibmveth: drop TX on a queue whose long term buffer was freed
https://git.kernel.org/netdev/net-next/c/eeb2b4377350 - [net-next,v2,2/2] ibmveth: do not reopen a device that was set down before reset ran https://git.kernel.org/netdev/net-next/c/60838e936e1c
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html