When a virtual device (e.g. netkit) leases an RX queue from a physical device, netdev_rx_queue_lease() holds a reference on the physical device until netkit's ndo_uninit() runs. During unregister_netdevice_many_notify() on the physical device, operations traversing the lease from the virtual device can race with teardown after Loop 1 marks the physical device NETREG_UNREGISTERING: CPU0 (unregister phys_dev) CPU1 (leasee path) -------------------------- ------------------ dev_close(phys_dev) reg_state = NETREG_UNREGISTERING netdev_lock(virt_dev) netdev_lock(phys_dev) -> binds/opens/queries dying dev -> or reconfigs closed phys_dev dev_memory_provider_uninstall(phys) NETDEV_UNREGISTER(phys) cascaded netkit_uninit() -> unlease In addition, in a same-batch unregister_netdevice_many([netkit, phys_dev]), Loop 1 sets NETREG_UNREGISTERING on both devices and netkit's ndo_uninit() -> netif_rxq_cleanup_unlease() can call __netif_mp_close_rxq(phys_dev) before phys_dev's dev_memory_provider_uninstall() runs, attempting queue reconfiguration on an unregistering device. Add netdev_lock_if_alive() to atomically lock a net_device only if it has not entered unregistration, and use it in netif_mp_open_rxq(), netdev_queue_get_dma_dev(), and netdev_nl_bind_tx_doit() (also re-checking netif_device_present() under the physical device's lock). In __netif_mp_close_rxq(), when !netif_is_alive(dev) and mp_params is still installed, validate and clear rxq->mp_params directly without invoking netdev_rx_queue_reconfig(), since the device has already been closed. These races were identified by code inspection while adding leased-queue unregister support for devmem; they have not been observed in production or tested on real hardware. Verified with netdevsim and netkit selftests in nl_netdev.py and nk_qlease.py. Signed-off-by: Mina Almasry --- v4 -> v5: - Replace netif_lease_valid() with netdev_lock_if_alive() and guard netdev_queue_get_dma_dev(), netif_mp_open_rxq(), and netdev_nl_bind_tx_doit() (Paolo). --- include/net/netdev_lock.h | 9 +++++++++ net/core/netdev-genl.c | 14 ++++++++++++-- net/core/netdev_queues.c | 3 ++- net/core/netdev_rx_queue.c | 19 ++++++++++++++----- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/include/net/netdev_lock.h b/include/net/netdev_lock.h index edd8cc2e2b36e..71e6f26b0d971 100644 --- a/include/net/netdev_lock.h +++ b/include/net/netdev_lock.h @@ -12,6 +12,15 @@ static inline bool netdev_trylock(struct net_device *dev) return mutex_trylock(&dev->lock); } +static inline bool __must_check netdev_lock_if_alive(struct net_device *dev) +{ + netdev_lock(dev); + if (netif_is_alive(dev)) + return true; + netdev_unlock(dev); + return false; +} + static inline void netdev_assert_locked(const struct net_device *dev) { lockdep_assert_held(&dev->lock); diff --git a/net/core/netdev-genl.c b/net/core/netdev-genl.c index d2ccb1090496d..87dac9c5e84c3 100644 --- a/net/core/netdev-genl.c +++ b/net/core/netdev-genl.c @@ -1253,8 +1253,18 @@ int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct genl_info *info) goto err_unlock_netdev; } - if (bind_dev != netdev) - netdev_lock(bind_dev); + if (bind_dev != netdev) { + if (!netdev_lock_if_alive(bind_dev)) { + err = -ENODEV; + NL_SET_ERR_MSG(info->extack, + "Device is being unregistered"); + goto err_unlock_netdev; + } + if (!netif_device_present(bind_dev)) { + err = -ENODEV; + goto err_unlock_bind_dev; + } + } dma_dev = netdev_queue_get_dma_dev(bind_dev, 0, NETDEV_QUEUE_TYPE_TX); diff --git a/net/core/netdev_queues.c b/net/core/netdev_queues.c index 27cb6397bdd4e..c796957c07f4b 100644 --- a/net/core/netdev_queues.c +++ b/net/core/netdev_queues.c @@ -50,7 +50,8 @@ struct device *netdev_queue_get_dma_dev(struct net_device *dev, hw_rxq = __netif_get_rx_queue(dev, idx)->lease; - netdev_lock(hw_rxq->dev); + if (!netdev_lock_if_alive(hw_rxq->dev)) + return NULL; idx = get_netdev_rx_queue_index(hw_rxq); dma_dev = __netdev_queue_get_dma_dev(hw_rxq->dev, idx); netdev_unlock(hw_rxq->dev); diff --git a/net/core/netdev_rx_queue.c b/net/core/netdev_rx_queue.c index a7261fedd5e76..e3ae99210caeb 100644 --- a/net/core/netdev_rx_queue.c +++ b/net/core/netdev_rx_queue.c @@ -269,7 +269,10 @@ int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx, return -EOPNOTSUPP; } - netdev_lock(dev); + if (!netdev_lock_if_alive(dev)) { + NL_SET_ERR_MSG(extack, "Device is being unregistered"); + return -ENODEV; + } ret = __netif_mp_open_rxq(dev, rxq_idx, p, extack); netdev_unlock(dev); return ret; @@ -284,17 +287,23 @@ static int __netif_mp_close_rxq(struct net_device *dev, unsigned int rxq_idx, rxq = __netif_get_rx_queue(dev, rxq_idx); - /* Callers holding a netdev ref may get here after we already - * went thru shutdown via dev_memory_provider_uninstall(). + /* Callers holding a netdev ref may get here after the device entered + * unregistration, either before or after + * dev_memory_provider_uninstall() ran. The device is already closed, + * so clear mp_params without reconfiguring the queue. */ - if (dev->reg_state > NETREG_REGISTERED && - !rxq->mp_params.mp_ops) + if (!netif_is_alive(dev) && !rxq->mp_params.mp_ops) return 0; if (WARN_ON_ONCE(rxq->mp_params.mp_ops != old_p->mp_ops || rxq->mp_params.mp_priv != old_p->mp_priv)) return -EINVAL; + if (!netif_is_alive(dev)) { + memset(&rxq->mp_params, 0, sizeof(rxq->mp_params)); + return 0; + } + netdev_queue_config(dev, rxq_idx, &qcfg[0]); memset(&rxq->mp_params, 0, sizeof(rxq->mp_params)); netdev_queue_config(dev, rxq_idx, &qcfg[1]); -- 2.56.1.915.g69d5ec7298-goog