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.") Signed-off-by: Philipp Sudakas --- drivers/net/vmxnet3/vmxnet3_xdp.c | 84 +++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/drivers/net/vmxnet3/vmxnet3_xdp.c b/drivers/net/vmxnet3/vmxnet3_xdp.c index 5f47049..a578667 100644 --- a/drivers/net/vmxnet3/vmxnet3_xdp.c +++ b/drivers/net/vmxnet3/vmxnet3_xdp.c @@ -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. */ -- 2.47.3