igb_xdp() can run while the device is runtime suspended or resuming, and __igb_resume() rebuilds the queue counts and the rings without RTNL. igb_xdp_setup() and igb_xsk_pool_setup() can then see a queue count whose rings don't exist yet (NULL dereference), or a NAPI that isn't enabled yet (napi_disable() never returns). Changing the XDP program of a runtime suspended device also opened it twice. Resume the device for the duration of igb_xdp(), as ethtool does. If resuming fails, the rings don't exist, or have no resources and their NAPIs were never enabled: don't touch them when disabling an XSK pool, refuse to enable one or to add an XDP program, and only update the software state when removing or replacing one, as failing that would leak the program. Tested with the 82576 emulated by QEMU by closing a zero-copy AF_XDP socket and attaching an XDP program while the device is runtime suspended; the latter no longer warns in __netif_napi_del_locked() because of the double open. The races themselves weren't reproduced. Fixes: 9cbc948b5a20 ("igb: add XDP support") Fixes: 80f6ccf9f116 ("igb: Introduce XSK data structures and helpers") Cc: stab…@vger.kernel.org Signed-off-by: Antonio Quartulli <anto…@mandelbit.com> --- v6: handle a failed resume: skip rings without resources, refuse to enable a pool or add an XDP program, and only update the software state when removing or replacing one (Sashiko); dropped Janis's Reviewed-by v5: new patch (Sashiko) drivers/net/ethernet/intel/igb/igb_main.c | 31 ++++++++++++++++++++--- drivers/net/ethernet/intel/igb/igb_xsk.c | 8 ++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/drivers/net/ethernet/intel/igb/igb_main.c b/drivers/net/ethernet/intel/igb/igb_main.c index 55352df..e5a2870 100644 --- a/drivers/net/ethernet/intel/igb/igb_main.c +++ b/drivers/net/ethernet/intel/igb/igb_main.c @@ -2909,6 +2909,16 @@ static int igb_xdp_setup(struct net_device *dev, struct netdev_bpf *bpf) } } + /* After a failed resume the device is detached and its rings have no + * resources: only update the software state, and don't add a program + */ + if (running && !netif_device_present(dev) && + (!adapter->num_rx_queues || !adapter->rx_ring[0]->desc)) { + if (prog && !adapter->xdp_prog) + return -ENODEV; + running = false; + } + old_prog = xchg(&adapter->xdp_prog, prog); need_reset = (!!prog != !!old_prog); @@ -2943,16 +2953,29 @@ static int igb_xdp_setup(struct net_device *dev, struct netdev_bpf *bpf) static int igb_xdp(struct net_device *dev, struct netdev_bpf *xdp) { struct igb_adapter *adapter = netdev_priv(dev); + int err; + + /* The rings only exist while the device is resumed, and resuming + * rebuilds them without RTNL. If resuming fails, igb_xdp_setup() and + * igb_xsk_pool_setup() find the device down. + */ + pm_runtime_get_sync(&adapter->pdev->dev); switch (xdp->command) { case XDP_SETUP_PROG: - return igb_xdp_setup(dev, xdp); + err = igb_xdp_setup(dev, xdp); + break; case XDP_SETUP_XSK_POOL: - return igb_xsk_pool_setup(adapter, xdp->xsk.pool, - xdp->xsk.queue_id); + err = igb_xsk_pool_setup(adapter, xdp->xsk.pool, + xdp->xsk.queue_id); + break; default: - return -EINVAL; + err = -EINVAL; } + + pm_runtime_put(&adapter->pdev->dev); + + return err; } int igb_xdp_xmit_back(struct igb_adapter *adapter, struct xdp_buff *xdp) diff --git a/drivers/net/ethernet/intel/igb/igb_xsk.c b/drivers/net/ethernet/intel/igb/igb_xsk.c index b6234e3..e70f818 100644 --- a/drivers/net/ethernet/intel/igb/igb_xsk.c +++ b/drivers/net/ethernet/intel/igb/igb_xsk.c @@ -120,6 +120,10 @@ static int igb_xsk_pool_enable(struct igb_adapter *adapter, qid >= netdev->real_num_tx_queues) return -EINVAL; + /* After a failed resume the rings have no resources */ + if (netif_running(netdev) && !adapter->rx_ring[qid]->desc) + return -ENODEV; + err = xsk_pool_dma_map(pool, &adapter->pdev->dev, IGB_RX_DMA_ATTR); if (err) return err; @@ -160,11 +164,11 @@ static int igb_xsk_pool_disable(struct igb_adapter *adapter, u16 qid) rx_ring = adapter->rx_ring[qid]; /* The stack can know more queues than the device has, e.g. while * runtime suspended, which frees the rings: skip the rings it - * doesn't have. + * doesn't have, or that have no resources after a failed resume. */ if_running = netif_running(adapter->netdev) && igb_xdp_is_enabled(adapter) && - qid < adapter->num_rx_queues; + qid < adapter->num_rx_queues && rx_ring->desc; if (if_running) igb_txrx_ring_disable(adapter, qid);
-- 2.56.1