Skip to contentSkip to conversation

vmxnet3: fix native XDP crash on oversized frames and XDP loss on reset

PATCH New Philipp Sudakas 3 participants 9 messages Nov 25 – Nov 29

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v3 cover vmxnet3: fix native XDP crash on oversized frames and XDP loss on reset

Fixes for native XDP on vmxnet3. A single frame from any host on the same L2 segment can crash a guest that has an XDP program attached; any device reset silently stops XDP; and a failure while switching XDP on or off leaves a stale program pointer and a half torn-down device.

Patch 1: the rx data ring XDP path leaves rx_ctx.skb pointing at the ring head page after a non-PASS verdict. Patch 4 relies on rx_ctx.skb being NULL between frames.

Patch 2: when vmxnet3_xdp_set() fails to recreate the rx queues or to reactivate the device, it neither rolls back the program exchange nor restarts or closes the device. A failed attach now brings the device back up without XDP and returns the error; a failed detach is completed, so the driver and the core agree; if the device cannot be restarted, the interface is closed. Patch 5 removes the side effect that used to hide the stale pointer.

Patch 3: with an MTU between 3051 and VMXNET3_XDP_MAX_MTU, full-sized frames do not fit the 3072-byte rx head buffer. Limit the XDP MTU so that they do.

Patch 4: a frame that does not fit in one XDP rx buffer (sop without eop) reaches the skb path, which takes the page_pool page for an skb:
kernel BUG in skb_put(). With MTU 1500, a single untagged frame of 1523..1526 bytes is enough. Drop such frames. This also fixes an older cross-poll BUG_ON on the non-XDP path; see the commit message.

Patch 5: vmxnet3_rq_cleanup_all() clears xdp_bpf_prog on every quiesce (MTU change, down/up, ring resize, reset). XDP stops being called while the program is still reported as attached, and its reference leaks.

Reproduced with stock modules on Debian kernels 6.12.113, 6.12.117 and 7.2.12 (7.2.12 untainted), VMware virtual hardware version 20. A small reproducer (raw frame sender and an XDP_PASS counter program) is available on request.

Testing (v3):
- Run-tested: the patched vmxnet3 files of net as of today, with the driver's current vmxnet3_ethtool.c and headers, built as a module for a Debian 7.2.12 kernel; VMware virtual hardware version 20, MTU 1500, native XDP:
- untagged frames of 1514..1522 bytes reach XDP; 1523, 1524 and 1526 bytes are dropped and counted in drop_total; no crash (v2 also: 1530 bytes with a VLAN 0 tag are dropped);
- 5000 frames of 1500 bytes mixed with 5000 of 1524 bytes: exactly

    5000 XDP passes and 5000 drops;
  - with XDP_DROP attached, small frames over the rx data ring
    (64 and 100 bytes) are dropped by XDP, followed by normal and
    oversized frames without issue;
  - after an MTU change, down/up and an rx ring resize the same
    program keeps running; after detach no program is left loaded;
  - MTU 3051 is refused with XDP attached and attaching at MTU 3051
    is refused; at MTU 3050 frames of 3064 bytes reach XDP and 3073
    bytes are dropped;
  - replacing a running program: the new one runs, the old one is
    freed;

- failures were forced with a test-only module parameter that makes the next N vmxnet3_rq_create_all() calls in vmxnet3_xdp_set() fail with -ENOMEM:
- attach, 1 failure: the error is returned, no XDP program is attached or left loaded, the interface stays up and receives, and a new attach works;
- attach, 2 failures (recovery fails too): the error is returned, no program, the interface is down; after "ip link set up" it receives;
- detach, 1 failure: 0 is returned, no program is attached or left loaded, the interface is down; after "ip link set up" it receives;
- attach while the interface is down does not reconfigure the device and is not affected.
No warnings in dmesg. (An intermediate version that used vmxnet3_force_close() on failure triggered "Trying to free already-free IRQ", hence dev_close() in patch 2.)
- Not covered: the rx_error paths, failures inside vmxnet3_activate_dev() itself (only the queue creation failure was forced), and VLAN tags other than 0, which the test switch does not pass.

Changes in v3:
- Patch 2 reworked after automated review of v2 (Sashiko): on a failed attach restart the device without XDP, complete a failed detach instead of restoring the old program (dev_xdp_detach_link() ignores the error), close the interface if the device cannot be restarted, return the real error, change the redirect target feature only once the change is done (also when the interface is down), and use rtnl_dereference(). It does not wait for VMXNET3_STATE_BIT_RESETTING under RTNL, which could deadlock against vmxnet3_reset_work(). New subject.
- Patch 4: count the two body buffer allocation failures in drop_total, so that skipping rx_error for a dropped frame never loses a count; the commit message now says that the non-XDP path changes too, and that the drop is not counted against the NAPI budget, like other non-PASS XDP verdicts.
- Patch 3: fixed a line over 80 columns.
- v2: https://lore.kernel.org/netdev/[email protected]/

Changes in v2:
- New patches 1, 2 and 3, the rx_error double count fix in patch 4, and no NAPI budget accounting for the dropped frame in patch 4.
- v1: https://lore.kernel.org/netdev/[email protected]/

The problems were found and analysed, and the patches drafted, with the help of an AI coding assistant; reproduction and testing were run on the reporter's lab systems.

Philipp Sudakas (5):
vmxnet3: clear rx_ctx.skb after a data ring XDP verdict
vmxnet3: fix the error path of XDP program setup
vmxnet3: limit the XDP MTU to the rx head buffer size
vmxnet3: drop XDP frames that do not fit in one rx buffer
vmxnet3: keep the XDP program across device resets

 drivers/net/vmxnet3/vmxnet3_drv.c | 31 +++++++++++-
 drivers/net/vmxnet3/vmxnet3_xdp.c | 84 +++++++++++++++++++++----------
 drivers/net/vmxnet3/vmxnet3_xdp.h | 10 +++-
 3 files changed, 95 insertions(+), 30 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v3 1/5 vmxnet3: clear rx_ctx.skb after a data ring XDP verdict

For a completion from the rx data ring, vmxnet3_rq_rx_complete() sets ctx->skb = rbi->skb in the sop block before running XDP on the data ring copy. With XDP enabled, the ring 0 head buffer is VMXNET3_RX_BUF_XDP and that union member holds a page_pool page, not an skb. On XDP_PASS ctx->skb is replaced with the new skb, but on any other verdict, XDP_DROP included, it keeps the page pointer.

The stale value then belongs to no frame. If the next completion is a single descriptor with rcd->err set, vmxnet3_rx_error() passes it to dev_kfree_skb_irq(). The ring buffer XDP path already clears ctx->skb after a non-PASS verdict; do the same here.

The problem was found and analysed, and this patch drafted, with the help of an AI coding assistant. It was reported by an automated review of an earlier version of this series.

Fixes 54f00cce1178 ("vmxnet3: Add XDP support.")
---
 drivers/net/vmxnet3/vmxnet3_drv.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/vmxnet3/vmxnet3_drv.c +1 -0
index f8df83f..e8be95c 100644
@@ -1742,6 +1742,7 @@ skip_xdp:
 					ctx->skb = skb_xdp_pass;
 					goto sop_done;
 				}
+				ctx->skb = NULL;
 				need_flush |= act == XDP_REDIRECT;
 
 				goto rcd_done;
Signature · 1 line
2.47.3
#2 lore raw

PATCH v3 2/5 vmxnet3: fix the error path of XDP program setup

In reply to Philipp Sudakas

vmxnet3_xdp_set() publishes the new program and drops its reference to the old one before it recreates the rx queues and reactivates the device. If vmxnet3_rq_create_all() or vmxnet3_activate_dev() then fails, it returns -EOPNOTSUPP without rolling anything back:

- On a failed attach, dev_xdp_install() drops the reference it took for the new program, but the driver still points to it.
- On a failed detach, the driver has already dropped the old program. The core keeps it recorded as attached, except on XDP link release, where dev_xdp_detach_link() ignores the error.
- The interface stays up with NAPI disabled, IRQs freed and, if queue creation failed, no rx rings. vmxnet3_suspend(), vmxnet3_resume() or a pending vmxnet3_reset_work() can then hang or crash.

So far the stale program pointer was mostly hidden because every error path of vmxnet3_activate_dev() goes through vmxnet3_rq_cleanup_all(), which also clears xdp_bpf_prog.

On a failed attach (the old program is always NULL there, as replacing a program does not reconfigure the device), drop the new program, bring the device back up without XDP and return the error. On a failed detach, complete the detach and return 0, so that the driver and the core agree. In both cases, if the device cannot be restarted, close the interface with dev_close(): the device is already quiesced, with NAPI disabled and IRQs freed, and vmxnet3_force_close() would quiesce it again and free the IRQs a second time.

Release the old program, and update the redirect target feature, only once the change is done. The feature is now also updated when the program is set while the interface is down, as the program now stays in effect when the interface comes up. Use rtnl_dereference() to read the current program: ndo_bpf runs under RTNL, not in an RCU read-side section.

The problem was found and analysed, and this patch drafted, with the help of an AI coding assistant. It was also reported by an automated review of an earlier version of this series.

Fixes 54f00cce1178 ("vmxnet3: Add XDP support.")
---
 drivers/net/vmxnet3/vmxnet3_xdp.c | 84 +++++++++++++++++++++----------
 1 file changed, 58 insertions(+), 26 deletions(-)
drivers/net/vmxnet3/vmxnet3_xdp.c +58 -26
index 5f47049..a578667 100644
@@ -33,6 +33,23 @@ vmxnet3_xdp_get_tq(struct vmxnet3_adapter *adapter)
 	return tq;
 }
 
+/* Recreate the rx queues in the mode given by adapter->xdp_bpf_prog and
+ * reactivate the quiesced device.
+ */
+static int
+vmxnet3_xdp_restart_dev(struct vmxnet3_adapter *adapter)
+{
+	int err;
+
+	vmxnet3_reset_dev(adapter);
+	vmxnet3_rq_destroy_all(adapter);
+	vmxnet3_adjust_rx_ring_size(adapter);
+	err = vmxnet3_rq_create_all(adapter);
+	if (err)
+		return err;
+	return vmxnet3_activate_dev(adapter);
+}
+
 static int
 vmxnet3_xdp_set(struct net_device *netdev, struct netdev_bpf *bpf,
 		struct netlink_ext_ack *extack)
@@ -55,46 +72,61 @@ vmxnet3_xdp_set(struct net_device *netdev, struct netdev_bpf *bpf,
 		adapter->netdev->features &= ~NETIF_F_LRO;
 	}
 
-	old_bpf_prog = rcu_dereference(adapter->xdp_bpf_prog);
+	old_bpf_prog = rtnl_dereference(adapter->xdp_bpf_prog);
 	if (!new_bpf_prog && !old_bpf_prog)
 		return 0;
 
 	running = netif_running(netdev);
 	need_update = !!old_bpf_prog != !!new_bpf_prog;
 
-	if (running && need_update)
-		vmxnet3_quiesce_dev(adapter);
+	if (!running || !need_update) {
+		vmxnet3_xdp_exchange_program(adapter, new_bpf_prog);
+		goto done;
+	}
 
+	vmxnet3_quiesce_dev(adapter);
 	vmxnet3_xdp_exchange_program(adapter, new_bpf_prog);
-	if (old_bpf_prog)
-		bpf_prog_put(old_bpf_prog);
-
-	if (!running || !need_update)
-		return 0;
-
-	if (new_bpf_prog)
-		xdp_features_set_redirect_target(netdev, false);
-	else
-		xdp_features_clear_redirect_target(netdev);
-
-	vmxnet3_reset_dev(adapter);
-	vmxnet3_rq_destroy_all(adapter);
-	vmxnet3_adjust_rx_ring_size(adapter);
-	err = vmxnet3_rq_create_all(adapter);
-	if (err) {
-		NL_SET_ERR_MSG_MOD(extack,
-				   "failed to re-create rx queues for XDP.");
-		return -EOPNOTSUPP;
+	err = vmxnet3_xdp_restart_dev(adapter);
+	if (err && new_bpf_prog) {
+		/* Attach failed (old_bpf_prog is NULL here): the core drops
+		 * its reference to the new program and keeps nothing
+		 * attached. Bring the device back up without XDP.
+		 */
+		NL_SET_ERR_MSG_MOD(extack, "failed to restart device for XDP");
+		vmxnet3_xdp_exchange_program(adapter, NULL);
+		if (vmxnet3_xdp_restart_dev(adapter))
+			goto err_close;
+		return err;
 	}
-	err = vmxnet3_activate_dev(adapter);
 	if (err) {
-		NL_SET_ERR_MSG_MOD(extack,
-				   "failed to activate device for XDP.");
-		return -EOPNOTSUPP;
+		/* Detach failed. The core may ignore an error here (XDP link
+		 * release), so complete the detach on a closed device.
+		 */
+		netdev_err(netdev, "XDP removal failed to restart, closing\n");
+		goto err_close;
 	}
 	clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
 
+done:
+	if (need_update) {
+		if (new_bpf_prog)
+			xdp_features_set_redirect_target(netdev, false);
+		else
+			xdp_features_clear_redirect_target(netdev);
+	}
+	if (old_bpf_prog)
+		bpf_prog_put(old_bpf_prog);
 	return 0;
+
+err_close:
+	/* The device is left quiesced, with IRQs freed and NAPI disabled,
+	 * as vmxnet3_close() expects after a quiesce: close the interface.
+	 * vmxnet3_force_close() would quiesce it and free the IRQs again.
+	 */
+	dev_close(netdev);
+	if (new_bpf_prog)
+		return err;
+	goto done;
 }
 
 /* This is the main xdp call used by kernel to set/unset eBPF program. */
Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH net v3 2/5] vmxnet3: fix the error path of XDP program setup

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] vmxnet3: RTNL deadlock and missing RESETTING bit in vmxnet3_xdp_set() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] vmxnet3: RTNL deadlock and missing RESETTING bit in vmxnet3_xdp_set()
--

commit 584df80e321cf69400a7b01cc8f4da9b076182a5
Author: Philipp Sudakas <[email protected]>

vmxnet3: fix the error path of XDP program setup

This commit reworks vmxnet3_xdp_set() to recover or close the interface when recreating the RX queues or reactivating the device fails during XDP attach or detach.

Quoted 4 lines
drivers/net/vmxnet3/vmxnet3_xdp.c +0 -0
index 5f470499e6002..a57866753d0fb 100644

[ ... ]

Quoted 76 lines
drivers/net/vmxnet3/vmxnet3_xdp.c +32 -26
@@ -55,46 +72,61 @@ vmxnet3_xdp_set(struct net_device *netdev, struct netdev_bpf *bpf,
 		adapter->netdev->features &= ~NETIF_F_LRO;
 	}
 
-	old_bpf_prog = rcu_dereference(adapter->xdp_bpf_prog);
+	old_bpf_prog = rtnl_dereference(adapter->xdp_bpf_prog);
 	if (!new_bpf_prog && !old_bpf_prog)
 		return 0;
 
 	running = netif_running(netdev);
 	need_update = !!old_bpf_prog != !!new_bpf_prog;
 
-	if (running && need_update)
-		vmxnet3_quiesce_dev(adapter);
+	if (!running || !need_update) {
+		vmxnet3_xdp_exchange_program(adapter, new_bpf_prog);
+		goto done;
+	}
 
+	vmxnet3_quiesce_dev(adapter);
 	vmxnet3_xdp_exchange_program(adapter, new_bpf_prog);
-	if (old_bpf_prog)
-		bpf_prog_put(old_bpf_prog);
-
-	if (!running || !need_update)
-		return 0;
-
-	if (new_bpf_prog)
-		xdp_features_set_redirect_target(netdev, false);
-	else
-		xdp_features_clear_redirect_target(netdev);
-
-	vmxnet3_reset_dev(adapter);
-	vmxnet3_rq_destroy_all(adapter);
-	vmxnet3_adjust_rx_ring_size(adapter);
-	err = vmxnet3_rq_create_all(adapter);
-	if (err) {
-		NL_SET_ERR_MSG_MOD(extack,
-				   "failed to re-create rx queues for XDP.");
-		return -EOPNOTSUPP;
+	err = vmxnet3_xdp_restart_dev(adapter);
+	if (err && new_bpf_prog) {
+		/* Attach failed (old_bpf_prog is NULL here): the core drops
+		 * its reference to the new program and keeps nothing
+		 * attached. Bring the device back up without XDP.
+		 */
+		NL_SET_ERR_MSG_MOD(extack, "failed to restart device for XDP");
+		vmxnet3_xdp_exchange_program(adapter, NULL);
+		if (vmxnet3_xdp_restart_dev(adapter))
+			goto err_close;
+		return err;
 	}
-	err = vmxnet3_activate_dev(adapter);
 	if (err) {
-		NL_SET_ERR_MSG_MOD(extack,
-				   "failed to activate device for XDP.");
-		return -EOPNOTSUPP;
+		/* Detach failed. The core may ignore an error here (XDP link
+		 * release), so complete the detach on a closed device.
+		 */
+		netdev_err(netdev, "XDP removal failed to restart, closing\n");
+		goto err_close;
 	}
 	clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
 
+done:
+	if (need_update) {
+		if (new_bpf_prog)
+			xdp_features_set_redirect_target(netdev, false);
+		else
+			xdp_features_clear_redirect_target(netdev);
+	}
+	if (old_bpf_prog)
+		bpf_prog_put(old_bpf_prog);
 	return 0;
+

drivers/net/vmxnet3/vmxnet3_xdp.c vmxnet3_xdp_set(struct net_device *netdev, struct netdev_bpf *bpf, · line 130

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_xdp.c +9 -0
+err_close:
+	/* The device is left quiesced, with IRQs freed and NAPI disabled,
+	 * as vmxnet3_close() expects after a quiesce: close the interface.
+	 * vmxnet3_force_close() would quiesce it and free the IRQs again.
+	 */
+	dev_close(netdev);
+	if (new_bpf_prog)
+		return err;
+	goto done;
 }

[Severity: Medium]
Can jumping to err_close deadlock on VMXNET3_STATE_BIT_RESETTING and rtnl_lock?

vmxnet3_xdp_set() runs under rtnl_lock and clears VMXNET3_STATE_BIT_RESETTING on the success path, but unlike vmxnet3_change_mtu() and vmxnet3_set_ringparam() it never waits to acquire VMXNET3_STATE_BIT_RESETTING before quiescing the device, nor clears it before calling dev_close():

CPU0 CPU1 (kworker)
vmxnet3_xdp_set() [rtnl_lock held]
vmxnet3_quiesce_dev()

                                      vmxnet3_reset_work()
                                        test_and_set_bit(..._RESETTING)
                                        rtnl_lock()  <-- blocks
  vmxnet3_xdp_restart_dev() fails
  goto err_close
  dev_close()
    ...
      vmxnet3_close()
        while (test_and_set_bit(..._RESETTING))
          usleep_range(1000, 2000);  <-- spins forever

*** DEADLOCK ***

Because vmxnet3_xdp_set() does not set VMXNET3_STATE_BIT_RESETTING on entry, it also lacks serialization against paths such as vmxnet3_shutdown_device() that rely on that bit without holding rtnl_lock, and can clear the bit on the success path while another thread owns it.

Should vmxnet3_xdp_set() acquire VMXNET3_STATE_BIT_RESETTING with a test_and_set_bit() loop before vmxnet3_quiesce_dev() and clear it on the rollback and err_close paths before dev_close(), matching vmxnet3_change_mtu()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

Re: [PATCH net v3 2/5] vmxnet3: fix the error path of XDP program setup

In reply to Philipp Sudakas
Thank 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 · High: 1 · Medium: 0 · Low: 0

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] The new err_close path in vmxnet3_xdp_set() (drivers/net/vmxnet3/vmxnet3_xdp.c:121-129) can deadlock RTNL against…

--

vmxnet3: fix the error path of XDP program setup

This commit reworks the error handling in vmxnet3_xdp_set(). A failed attach now drops the new program and restarts the device without XDP, and a failed detach completes the detach. If the device cannot be restarted, the interface is closed with dev_close().

Quoted 4 lines
drivers/net/vmxnet3/vmxnet3_xdp.c +0 -0
index 5f470499e6002..a57866753d0fb 100644

[ ... ]

Quoted 1 line
drivers/net/vmxnet3/vmxnet3_xdp.c +0 -0
@@ -55,46 +72,61 @@ vmxnet3_xdp_set(struct net_device *netdev, struct netdev_bpf *bpf,

[ ... ]

Quoted 2 lines
drivers/net/vmxnet3/vmxnet3_xdp.c +1 -0
+	vmxnet3_quiesce_dev(adapter);
 	vmxnet3_xdp_exchange_program(adapter, new_bpf_prog);

[ ... ]

Quoted 24 lines
drivers/net/vmxnet3/vmxnet3_xdp.c +16 -4
+	err = vmxnet3_xdp_restart_dev(adapter);
+	if (err && new_bpf_prog) {
+		/* Attach failed (old_bpf_prog is NULL here): the core drops
+		 * its reference to the new program and keeps nothing
+		 * attached. Bring the device back up without XDP.
+		 */
+		NL_SET_ERR_MSG_MOD(extack, "failed to restart device for XDP");
+		vmxnet3_xdp_exchange_program(adapter, NULL);
+		if (vmxnet3_xdp_restart_dev(adapter))
+			goto err_close;
+		return err;
 	}
-	err = vmxnet3_activate_dev(adapter);
 	if (err) {
-		NL_SET_ERR_MSG_MOD(extack,
-				   "failed to activate device for XDP.");
-		return -EOPNOTSUPP;
+		/* Detach failed. The core may ignore an error here (XDP link
+		 * release), so complete the detach on a closed device.
+		 */
+		netdev_err(netdev, "XDP removal failed to restart, closing\n");
+		goto err_close;
 	}
 	clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);

[ ... ]

drivers/net/vmxnet3/vmxnet3_xdp.c vmxnet3_xdp_set(struct net_device *netdev, struct netdev_bpf *bpf, · line 126

Quoted 6 lines
drivers/net/vmxnet3/vmxnet3_xdp.c +6 -0
+err_close:
+	/* The device is left quiesced, with IRQs freed and NAPI disabled,
+	 * as vmxnet3_close() expects after a quiesce: close the interface.
+	 * vmxnet3_force_close() would quiesce it and free the IRQs again.
+	 */
+	dev_close(netdev);

[Severity: High]
Can this dev_close() call deadlock RTNL against vmxnet3_reset_work()?

vmxnet3_xdp_set() runs as ndo_bpf with RTNL held and never takes VMXNET3_STATE_BIT_RESETTING. dev_close() ends up in vmxnet3_close(), which waits for that bit with no timeout:

drivers/net/vmxnet3/vmxnet3_drv.c:vmxnet3_close() {
    ...
	while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
		usleep_range(1000, 2000);
    ...
}

vmxnet3_reset_work() takes the bit first and RTNL second:

drivers/net/vmxnet3/vmxnet3_drv.c:vmxnet3_reset_work() {
    ...
	if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
		return;

	/* if the device is closed, we must leave it alone */
	rtnl_lock();
    ...
}

reset_work can be queued by a tx timeout (vmxnet3_tx_timeout()) or a device error event (vmxnet3_process_events()) while an XDP attach or detach holds RTNL. If that happens, the sequence could be:

reset_work vmxnet3_xdp_set() (RTNL held)
test_and_set_bit(RESETTING)
rtnl_lock() <- blocks

                                  vmxnet3_quiesce_dev()
                                  vmxnet3_xdp_restart_dev() fails
                                  dev_close()
                                    vmxnet3_close()
                                      spins on RESETTING

The RTNL holder then waits for a bit that only the RTNL waiter can clear. Both the failed detach path and the failed second restart on the attach path reach err_close.

Before this patch these failure paths returned -EOPNOTSUPP without calling dev_close(), so reset_work would eventually get RTNL and run. The commit message lists "a pending vmxnet3_reset_work() can then hang or crash" as one of the hazards being fixed. A hang still looks reachable through the new dev_close() call.

vmxnet3_force_close() guards against this case:

	/*
	 * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
	 * vmxnet3_close() will deadlock.
	 */
	BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));

The err_close comment explains why dev_close() is used instead of vmxnet3_force_close() in terms of IRQ and NAPI state. It does not cover who owns RESETTING.

The RESETTING-before-RTNL ordering in vmxnet3_reset_work() is older than this patch and also affects a plain ifdown, but this patch adds a new path into it. Would taking rtnl_lock() before test_and_set_bit() in vmxnet3_reset_work() be a reasonable complementary fix?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125075255.1517-1-filzador%40gmail.com
#5 lore raw

PATCH v3 3/5 vmxnet3: limit the XDP MTU to the rx head buffer size

In reply to Philipp Sudakas

With XDP, rx ring 0 head buffers are page_pool pages, but the length given to the device is skb_buf_size, which vmxnet3_adjust_rx_ring_size() caps at VMXNET3_MAX_SKB_BUF_SIZE (3072). VMXNET3_XDP_MAX_MTU only accounts for the page size, about 3494 on x86_64 with 4K pages.

With an MTU between 3051 and VMXNET3_XDP_MAX_MTU, a full-sized frame does not fit the head buffer and the device splits it over a head and a body descriptor. Such completions take the skb path with a page in rbi->skb, which crashes the guest; see the next patch for the details.

Cap VMXNET3_XDP_MAX_MTU at VMXNET3_MAX_SKB_BUF_SIZE - VMXNET3_MAX_ETH_HDR_SIZE, so that up to that MTU skb_buf_size is MTU + VMXNET3_MAX_ETH_HDR_SIZE and a frame of MTU plus Ethernet and VLAN headers fits the head buffer.

The problem was found and analysed, and this patch drafted, with the help of an AI coding assistant. It was reported by an automated review of an earlier version of this series.

Fixes 54f00cce1178 ("vmxnet3: Add XDP support.")
---
 drivers/net/vmxnet3/vmxnet3_xdp.h | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/net/vmxnet3/vmxnet3_xdp.h +8 -2
index f9d843e..8b69af3 100644
@@ -20,8 +20,14 @@
 #define VMXNET3_XDP_RX_OFFSET	VMXNET3_XDP_HEADROOM
 #define VMXNET3_XDP_MAX_FRSIZE	(PAGE_SIZE - VMXNET3_XDP_HEADROOM - \
 				 VMXNET3_XDP_RX_TAILROOM)
-#define VMXNET3_XDP_MAX_MTU	(VMXNET3_XDP_MAX_FRSIZE - ETH_HLEN - \
-				 2 * VLAN_HLEN - ETH_FCS_LEN)
+/* A frame must also fit the rx head buffer, at most
+ * VMXNET3_MAX_SKB_BUF_SIZE bytes (see vmxnet3_adjust_rx_ring_size()).
+ */
+#define VMXNET3_XDP_MAX_MTU	min_t(unsigned int, \
+				      VMXNET3_XDP_MAX_FRSIZE - ETH_HLEN - \
+				      2 * VLAN_HLEN - ETH_FCS_LEN, \
+				      VMXNET3_MAX_SKB_BUF_SIZE - \
+				      VMXNET3_MAX_ETH_HDR_SIZE)
 
 int vmxnet3_xdp(struct net_device *netdev, struct netdev_bpf *bpf);
 int vmxnet3_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
Signature · 1 line
2.47.3
#6 lore raw

PATCH v3 4/5 vmxnet3: drop XDP frames that do not fit in one rx buffer

In reply to Philipp Sudakas

With XDP enabled, rx ring 0 head buffers are page_pool pages and the device is given a buffer length of MTU + 22 (skb_buf_size; the previous patch keeps the XDP MTU low enough for that). The device still accepts somewhat longer frames (observed: up to MTU + 26 untagged, MTU + 30 with a VLAN tag) and splits them over a head and a body descriptor.

vmxnet3_rq_rx_complete() runs XDP only for completions with both sop and eop set. A frame with sop but without eop falls through to the skb path, which uses rbi->skb although, for a VMXNET3_RX_BUF_XDP buffer, that union member holds the page. The result is a kernel BUG in skb_put(), or at BUG_ON(ctx->skb != NULL || rbi->skb == NULL):

kernel BUG at net/core/skbuff.c:2637!

  RIP: 0010:skb_put+0x3c/0x40
  Call Trace:
   <IRQ>
   vmxnet3_rq_rx_complete+0x893/0x12b0 [vmxnet3]
   vmxnet3_poll_rx_only+0x38/0xd0 [vmxnet3]
   __napi_poll+0x2b/0x160
   net_rx_action+0x336/0x420

One frame of MTU + 23 to MTU + 26 bytes from any host on the same L2 segment is enough to crash a guest that has a native XDP program attached to a vmxnet3 interface.

XDP on vmxnet3 is single-buffer, so drop such frames: count them in drop_total, reuse the head buffer and skip the body buffers, as the existing allocation failure path does. The completions of one frame may span two polls, so start each poll with skip_page_frags set when no frame is in progress (rx_ctx.skb == NULL; the first patch of this series makes that hold for the data ring path too): a body buffer without a head can only belong to a dropped frame.

This also changes the non-XDP rx path, whose head buffer drops (skb allocation or DMA mapping failure) had the same problem before XDP support was added: if the poll ended before the body completion, the next poll started with skip_page_frags false and ctx->skb NULL, and the orphan body hit BUG_ON(ctx->skb == NULL && !skip_page_frags). Such bodies are now skipped.

Every path that sets skip_page_frags now counts the frame in drop_total; the two body buffer allocation failures did not. If the last body buffer of a dropped frame has rcd->err set, do not count the frame a second time in vmxnet3_rx_error() (nor in drop_err/drop_fcs).

Like the other non-PASS XDP verdicts and the existing drop paths, the dropped frame is not counted against the NAPI budget; that is a separate, pre-existing matter.

The problem was found and analysed, and this patch drafted, with the help of an AI coding assistant. It was reproduced and tested on the reporter's lab systems as described in the cover letter.

Fixes 54f00cce1178 ("vmxnet3: Add XDP support.")
---
 drivers/net/vmxnet3/vmxnet3_drv.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
drivers/net/vmxnet3/vmxnet3_drv.c +23 -1
index e8be95c..234897a 100644
@@ -1614,7 +1614,8 @@ vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
 		adapter->rx_prod_offset, adapter->rx_prod2_offset
 	};
 	u32 num_pkts = 0;
-	bool skip_page_frags = false;
+	/* A frame dropped at its head may continue in the next poll. */
+	bool skip_page_frags = !rq->rx_ctx.skb;
 	bool encap_lro = false;
 	struct Vmxnet3_RxCompDesc *rcd;
 	struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
@@ -1662,10 +1663,29 @@ vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
 		       rxd->len != rbi->len);
 
 		if (unlikely(rcd->eop && rcd->err)) {
+			/* The last body buffer of a frame already dropped
+			 * and counted
+			 */
+			if (!rcd->sop && skip_page_frags)
+				goto rcd_done;
 			vmxnet3_rx_error(rq, rcd, ctx, adapter);
 			goto rcd_done;
 		}
 
+		if (rcd->sop && !rcd->eop &&
+		    rbi->buf_type == VMXNET3_RX_BUF_XDP &&
+		    !VMXNET3_RX_DATA_RING(adapter, rcd->rqID)) {
+			/* The frame did not fit one XDP buffer. XDP here is
+			 * single-buffer and the head holds a page, not an
+			 * skb: drop the frame, reuse the head buffer and
+			 * skip the body buffers.
+			 */
+			rq->stats.drop_total++;
+			ctx->skb = NULL;
+			skip_page_frags = true;
+			goto rcd_done;
+		}
+
 		if (rcd->sop && rcd->eop && vmxnet3_xdp_enabled(adapter)) {
 			struct sk_buff *skb_xdp_pass;
 			int act;
@@ -1845,6 +1865,7 @@ skip_xdp:
 				 */
 				if (unlikely(!new_page)) {
 					rq->stats.rx_buf_alloc_failure++;
+					rq->stats.drop_total++;
 					dev_kfree_skb(ctx->skb);
 					ctx->skb = NULL;
 					skip_page_frags = true;
@@ -1858,6 +1879,7 @@ skip_xdp:
 						      new_dma_addr)) {
 					put_page(new_page);
 					rq->stats.rx_buf_alloc_failure++;
+					rq->stats.drop_total++;
 					dev_kfree_skb(ctx->skb);
 					ctx->skb = NULL;
 					skip_page_frags = true;
Signature · 1 line
2.47.3
#7 lore raw

Re: [PATCH net v3 4/5] vmxnet3: drop XDP frames that do not fit in one rx buffer

Thank 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 · High: 0 (1) · Medium: 1 · Low: 0

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] Besides the XDP crash, this patch fixes a second, older crash in the non-XDP rx path, but its only Fixes: tag points at the XDP…

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it is a serious one in the same XDP rx path.

--

vmxnet3: drop XDP frames that do not fit in one rx buffer

With XDP enabled, a frame that spans a head and a body descriptor used to fall through to the skb path in vmxnet3_rq_rx_complete(). That path treated the page_pool page in rbi->skb as an skb and crashed. This patch drops such frames, starts each poll with skip_page_frags set when no frame is in progress, and adds drop_total accounting to the body buffer allocation failure paths.

drivers/net/vmxnet3/vmxnet3_drv.c vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq, · line 1618

Quoted 11 lines
drivers/net/vmxnet3/vmxnet3_drv.c +2 -1
index e8be95c1d9f8e..234897abfe0ad 100644
@@ -1614,7 +1614,8 @@ vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
 		adapter->rx_prod_offset, adapter->rx_prod2_offset
 	};
 	u32 num_pkts = 0;
-	bool skip_page_frags = false;
+	/* A frame dropped at its head may continue in the next poll. */
+	bool skip_page_frags = !rq->rx_ctx.skb;

[Severity: Medium]
This initialization also fixes an older crash in the non-XDP rx path. The commit message says so:

This also changes the non-XDP rx path, whose head buffer drops (skb allocation or DMA mapping failure) had the same problem before XDP support was added

Here is the sequence in vmxnet3_rq_rx_complete(). If netdev_alloc_skb_ip_align() or dma_map_single() fails on a head buffer, ctx->skb is set to NULL and skip_page_frags to true. The loop then exits because rcd->gen does not yet match rq->comp_ring.gen for the body completion. The next poll used to start with skip_page_frags == false, so the orphan body hit:

    BUG_ON(ctx->skb == NULL && !skip_page_frags);

That BUG_ON and the skip_page_frags handling appear to come from commit 5318d809d7b4 ("vmxnet3: fix starving rx ring whenoc_skb kb fails") from 2011. The only tag here is:

    Fixes: 54f00cce1178 ("vmxnet3: Add XDP support.")

which landed in v6.6.

Will stable trees that have the non-XDP code but not vmxnet3 XDP miss this crash fix? The fix is mixed in with XDP-only code (VMXNET3_RX_BUF_XDP, vmxnet3_xdp_enabled()), so it also looks hard to cherry-pick into those trees.

Could the non-XDP part go into a separate patch with its own

Fixes 5318d809d7b4 tag? That part is the poll-start skip_page_frags

initialization, the eop/err skip for an already dropped frame, and the extra drop_total accounting. Failing that, could this patch at least carry the additional tag?

Quoted 3 lines
drivers/net/vmxnet3/vmxnet3_drv.c +0 -0
 	bool encap_lro = false;
 	struct Vmxnet3_RxCompDesc *rcd;
 	struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;

[ ... ]

Quoted 7 lines
drivers/net/vmxnet3/vmxnet3_drv.c +7 -0
+		if (rcd->sop && !rcd->eop &&
+		    rbi->buf_type == VMXNET3_RX_BUF_XDP &&
+		    !VMXNET3_RX_DATA_RING(adapter, rcd->rqID)) {
+			/* The frame did not fit one XDP buffer. XDP here is
+			 * single-buffer and the head holds a page, not an
+			 * skb: drop the frame, reuse the head buffer and
+			 * skip the body buffers.

drivers/net/vmxnet3/vmxnet3_drv.c vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq, · line 1691

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_drv.c +7 -0
+			 */
+			rq->stats.drop_total++;
+			ctx->skb = NULL;
+			skip_page_frags = true;
+			goto rcd_done;
+		}
+
 		if (rcd->sop && rcd->eop && vmxnet3_xdp_enabled(adapter)) {
 			struct sk_buff *skb_xdp_pass;
 			int act;

[Severity: High]
This is a pre-existing issue, not one introduced by this patch, but it sits in the same XDP rx path. Can vmxnet3_process_xdp() in vmxnet3_xdp.c leave the rx descriptor pointing at a page it has already given away?

On XDP_PASS the head page goes into an skb, and on a successful XDP_TX or XDP_REDIRECT it goes to the TX ring or the redirect target. Only then is a replacement allocated:

drivers/net/vmxnet3/vmxnet3_xdp.c:vmxnet3_process_xdp() {
    ...
	if (act == XDP_PASS) {
out_skb:
		*skb_xdp_pass = vmxnet3_build_skb(rq, page, &xdp);
		...
	}

	new_data = vmxnet3_pp_get_buff(rq->page_pool, &new_dma_addr,
				       GFP_ATOMIC);
	if (!new_data) {
		rq->stats.rx_buf_alloc_failure++;
		return XDP_DROP;
	}
	rbi->page = virt_to_page(new_data);
	rbi->dma_addr = new_dma_addr;
	rxd->addr = cpu_to_le64(rbi->dma_addr);
    ...
}

When that GFP_ATOMIC allocation fails, rbi->page, rbi->dma_addr and rxd->addr are left unchanged. The caller in vmxnet3_rq_rx_complete() then sees XDP_DROP. It sets ctx->skb = NULL, which leaks the skb just built in the PASS case. It does not set need_flush for a redirect that already succeeded. Then it jumps to rcd_done.

The refill at rcd_done sets rxd->gen = ring->gen and gives the descriptor back to the device with the old DMA address. The device can then DMA new rx data into a page that is being transmitted or is owned by a redirect target.

After TX completion that page can go back into the page pool and be handed to another rx descriptor. If the pool is full, it can be released and unmapped while this ring still points at it.

The XDP_DROP and XDP_ABORTED verdicts, and failed TX or redirect, look safe because page_pool_recycle_direct() refills the alloc cache just before the allocation. Should the replacement buffer be allocated before the page is handed off, or should a failed refill keep the descriptor out of the ring?

Quoted 3 lines
drivers/net/vmxnet3/vmxnet3_drv.c +0 -0
 			if (VMXNET3_RX_DATA_RING(adapter, rcd->rqID)) {
 				ctx->skb = NULL;

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125075255.1517-1-filzador%40gmail.com
#8 lore raw

PATCH v3 5/5 vmxnet3: keep the XDP program across device resets

In reply to Philipp Sudakas

vmxnet3_rq_cleanup_all() clears adapter->xdp_bpf_prog. It is called from vmxnet3_quiesce_dev(), so on every MTU change, down/up, ring resize, reset work and resume. After that the driver no longer runs XDP, while the core still reports the program as attached, and the driver's reference to the program is never dropped: a later detach finds xdp_bpf_prog already NULL and returns early.

Seen on 6.12.117 and 7.2.12: after "ip link set dev X mtu 1400", "ip link set dev X down; ip link set dev X up" or "ethtool -G X rx 512", the attached program is no longer called, and each detached program stays loaded.

Do not clear the pointer there. The program is exchanged and released by vmxnet3_xdp_set(), which also runs when the device is unregistered, and which since an earlier patch of this series leaves the driver and the core in agreement on failure. Rx rings are then recreated in XDP mode after a reset, as vmxnet3_xdp_enabled() stays true.

Since the program now survives an MTU change, reject an MTU above VMXNET3_XDP_MAX_MTU while XDP is attached, as vmxnet3_xdp_set() already refuses to attach a program at such an MTU.

The problem was found and analysed, and this patch drafted, with the help of an AI coding assistant. It was reproduced and tested on the reporter's lab systems as described in the cover letter.

Fixes 54f00cce1178 ("vmxnet3: Add XDP support.")
---
 drivers/net/vmxnet3/vmxnet3_drv.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/vmxnet3/vmxnet3_drv.c +5 -1
index 234897a..bc56b0f 100644
@@ -2111,7 +2111,6 @@ vmxnet3_rq_cleanup_all(struct vmxnet3_adapter *adapter)
 
 	for (i = 0; i < adapter->num_rx_queues; i++)
 		vmxnet3_rq_cleanup(&adapter->rx_queue[i], adapter);
-	rcu_assign_pointer(adapter->xdp_bpf_prog, NULL);
 }
 
 
@@ -3670,6 +3669,11 @@ vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
 	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
 	int err = 0;
 
+	if (vmxnet3_xdp_enabled(adapter) && new_mtu > VMXNET3_XDP_MAX_MTU) {
+		netdev_err(netdev, "MTU %d too large for XDP\n", new_mtu);
+		return -EINVAL;
+	}
+
 	/*
 	 * Reset_work may be in the middle of resetting the device, wait for its
 	 * completion.
Signature · 1 line
2.47.3