net_devmem_dev_unregister() currently has three issues when tearing down devmem bindings: 1. For TX bindings, it clears NET_DEVMEM_BINDING_LIVE and immediately calls net_devmem_dmabuf_binding_unmap() without waiting for an RCU grace period, racing with in-flight net_devmem_get_binding() lookups (both direct on dev and leased via binding->vdev). 2. For RX bindings, it skips bindings that still have NET_DEVMEM_BINDING_LIVE set. If an RX queue fails to close while userspace still holds the netlink socket open and defers unmapping, net_devmem_dev_unregister() would skip it too, leaving the dmabuf mapped past physical device unregistration. 3. It checks !netif_is_queue_leasee(dev), which is now obsolete since binding->dev always points to the physical device for both RX and TX bindings. Split net_devmem_dev_unregister() into two passes separated by a single synchronize_net(): - First pass clears binding->vdev for any binding referencing dev (both when unregistering a virtual device and when unregistering the physical device) and clears NET_DEVMEM_BINDING_LIVE for all bindings with binding->dev == dev. - If any bindings match binding->dev == dev, call synchronize_net() once to wait for in-flight net_devmem_get_binding() RCU readers to finish. - Second pass unmaps every binding with binding->dev == dev (both TX and any deferred-unmap RX bindings, regardless of whether LIVE was set) and clears binding->dev to NULL. These issues were identified by code inspection while extending devmem unregister teardown to leased queues; 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: - Split out net_devmem_dev_unregister() cleanup into its own patch. - Add single synchronize_net() between clearing NET_DEVMEM_BINDING_LIVE and unmapping to close the TX net_devmem_get_binding() race (Paolo). - Unmap all remaining bindings with binding->dev == dev (including live deferred-unmap RX bindings) and drop the now-obsolete !netif_is_queue_leasee(dev) check. --- net/core/devmem.c | 36 ++++++++++++++++++++++-------------- net/core/devmem.h | 5 ++--- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/net/core/devmem.c b/net/core/devmem.c index 3a894ff06b32b..a395f100d2f33 100644 --- a/net/core/devmem.c +++ b/net/core/devmem.c @@ -246,6 +246,7 @@ void net_devmem_dev_unregister(struct net_device *dev) { struct net_devmem_dmabuf_binding *binding; unsigned long xa_idx; + bool unmap = false; netdev_assert_locked(dev); @@ -254,26 +255,33 @@ void net_devmem_dev_unregister(struct net_device *dev) xa_lock(&net_devmem_dmabuf_bindings); xa_for_each(&net_devmem_dmabuf_bindings, xa_idx, binding) { - if (binding->direction == DMA_FROM_DEVICE) { - if (READ_ONCE(binding->dev) != dev || - xa_get_mark(&net_devmem_dmabuf_bindings, xa_idx, - NET_DEVMEM_BINDING_LIVE)) - continue; - } else { - if (READ_ONCE(binding->vdev) == dev) - WRITE_ONCE(binding->vdev, NULL); - if (READ_ONCE(binding->dev) != dev) - continue; + if (READ_ONCE(binding->vdev) == dev) + WRITE_ONCE(binding->vdev, NULL); + if (READ_ONCE(binding->dev) == dev) { WRITE_ONCE(binding->vdev, NULL); __xa_clear_mark(&net_devmem_dmabuf_bindings, xa_idx, NET_DEVMEM_BINDING_LIVE); + unmap = true; } + } + xa_unlock(&net_devmem_dmabuf_bindings); + + if (!unmap) + return; + + /* Ensure no tx net_devmem_get_binding() are in flight after the + * mark is cleared. + */ + synchronize_net(); - xa_unlock(&net_devmem_dmabuf_bindings); - if (!netif_is_queue_leasee(dev)) + xa_lock(&net_devmem_dmabuf_bindings); + xa_for_each(&net_devmem_dmabuf_bindings, xa_idx, binding) { + if (READ_ONCE(binding->dev) == dev) { + xa_unlock(&net_devmem_dmabuf_bindings); net_devmem_dmabuf_binding_unmap(binding); - xa_lock(&net_devmem_dmabuf_bindings); - WRITE_ONCE(binding->dev, NULL); + xa_lock(&net_devmem_dmabuf_bindings); + WRITE_ONCE(binding->dev, NULL); + } } xa_unlock(&net_devmem_dmabuf_bindings); } diff --git a/net/core/devmem.h b/net/core/devmem.h index d3a5a482d99bf..4036aab286c4d 100644 --- a/net/core/devmem.h +++ b/net/core/devmem.h @@ -44,9 +44,8 @@ struct net_devmem_dmabuf_binding { * The binding undos itself and unmaps the underlying dmabuf once all * those refs are dropped and the binding is no longer desired or in * use. RX bindings are unmapped earlier when their queues are closed or - * uninstalled, unless a leased or busy queue may still use the binding. - * TX bindings are also unmapped earlier if their device is - * unregistered. + * uninstalled, unless a busy queue may still use the binding. Bindings + * are also unmapped earlier if their device is unregistered. * * net_devmem_net_iov_inc_ref() on dmabuf net_iovs will increment this * reference, making sure that the binding remains alive until all the -- 2.56.1.915.g69d5ec7298-goog