Netdev List
 help / color / mirror / Atom feed
From: Mina Almasry <almasrymina@google.com>
To: netdev@vger.kernel.org
Cc: Mina Almasry <almasrymina@google.com>,
	"David S. Miller" <davem@davemloft.net>,
	 Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	 Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>, Shuah Khan <shuah@kernel.org>,
	 linux-kselftest@vger.kernel.org,
	Stanislav Fomichev <sdf.kernel@gmail.com>,
	 Bobby Eshleman <bobbyeshleman@meta.com>,
	Nikolay Aleksandrov <razor@blackwall.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	David Wei <dw@davidwei.uk>,
	 Pavel Begunkov <asml.silence@gmail.com>,
	Jens Axboe <axboe@kernel.dk>,  Weiming Shi <bestswngs@gmail.com>,
	io-uring@vger.kernel.org,
	 Jesper Dangaard Brouer <hawk@kernel.org>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>
Subject: [PATCH net-next v5 4/6] net: devmem: synchronize and unmap all device bindings on unregister
Date: Fri,  4 Dec 2026 23:10:28 +0000	[thread overview]
Message-ID: <20261204231030.3815575-5-almasrymina@google.com> (raw)
In-Reply-To: <20261204231030.3815575-1-almasrymina@google.com>

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 <almasrymina@google.com>
---
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


  parent reply	other threads:[~2026-12-04 23:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 23:10 [PATCH net-next v5 0/6] net: devmem: support leased RX and TX queues on netdev unregister Mina Almasry
2026-12-04 23:10 ` [PATCH net-next v5 1/6] net: guard leased queue ops against physical " Mina Almasry
2026-12-04 23:10 ` [PATCH net-next v5 2/6] net: devmem: fold net_devmem_lookup_dmabuf() into net_devmem_get_binding() Mina Almasry
2026-12-04 23:10 ` [PATCH net-next v5 3/6] net: devmem: resolve leased RX queues to physical device on bind Mina Almasry
2026-12-04 23:10 ` Mina Almasry [this message]
2026-12-04 23:10 ` [PATCH net-next v5 5/6] net: propagate busy status on memory provider uninstall Mina Almasry
2026-12-04 23:10 ` [PATCH net-next v5 6/6] selftests: net: add leased devmem RX and TX netdev unregister tests Mina Almasry

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204231030.3815575-5-almasrymina@google.com \
    --to=almasrymina@google.com \
    --cc=asml.silence@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=bestswngs@gmail.com \
    --cc=bobbyeshleman@meta.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dw@davidwei.uk \
    --cc=edumazet@kernel.org \
    --cc=hawk@kernel.org \
    --cc=horms@kernel.org \
    --cc=ilias.apalodimas@linaro.org \
    --cc=io-uring@vger.kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=razor@blackwall.org \
    --cc=sdf.kernel@gmail.com \
    --cc=shuah@kernel.org \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox