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>,
	 Stanislav Fomichev <sdf@fomichev.me>
Subject: [PATCH net-next v5 2/6] net: devmem: fold net_devmem_lookup_dmabuf() into net_devmem_get_binding()
Date: Fri,  4 Dec 2026 23:10:26 +0000	[thread overview]
Message-ID: <20261204231030.3815575-3-almasrymina@google.com> (raw)
In-Reply-To: <20261204231030.3815575-1-almasrymina@google.com>

net_devmem_lookup_dmabuf() has only one caller, net_devmem_get_binding(),
which currently enters two separate RCU read-side critical sections: one
inside net_devmem_lookup_dmabuf() to find the NET_DEVMEM_BINDING_LIVE
entry and acquire a reference, and a second in net_devmem_get_binding()
to verify dst_dev against binding->dev and binding->vdev.

Because the lookup and device checks are split across two RCU read-side
critical sections, waiting for synchronize_net() after clearing
NET_DEVMEM_BINDING_LIVE does not guarantee that in-flight
net_devmem_get_binding() callers have finished validating the binding
and returned.

Fold net_devmem_lookup_dmabuf() into net_devmem_get_binding() so the
entire lookup, validation, device check, and
net_devmem_dmabuf_binding_get() run inside a single rcu_read_lock()
section. The binding stays alive for the duration of the RCU read-side
critical section because net_devmem_unbind_dmabuf() calls
synchronize_net() after clearing NET_DEVMEM_BINDING_LIVE and before
percpu_ref_kill(). Deferring net_devmem_dmabuf_binding_get() until all
checks pass also eliminates the error-path reference put.

Acked-by: Stanislav Fomichev <sdf@fomichev.me>
Reviewed-by: Nikolay Aleksandrov <razor@blackwall.org>
Signed-off-by: Mina Almasry <almasrymina@google.com>
---
 net/core/devmem.c | 38 ++++++++++++--------------------------
 net/core/devmem.h |  6 ------
 2 files changed, 12 insertions(+), 32 deletions(-)

diff --git a/net/core/devmem.c b/net/core/devmem.c
index 3714364b5d9a5..f00c1d8fcc2d2 100644
--- a/net/core/devmem.c
+++ b/net/core/devmem.c
@@ -211,7 +211,7 @@ void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding)
 	xa_clear_mark(&net_devmem_dmabuf_bindings, binding->id,
 		      NET_DEVMEM_BINDING_LIVE);
 
-	/* Ensure no tx net_devmem_lookup_dmabuf() are in flight after the
+	/* Ensure no tx net_devmem_get_binding() are in flight after the
 	 * mark is cleared.
 	 */
 	synchronize_net();
@@ -519,23 +519,6 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
 	return ERR_PTR(err);
 }
 
-struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id)
-{
-	struct net_devmem_dmabuf_binding *binding;
-	unsigned long xa_idx = id;
-
-	rcu_read_lock();
-	binding = xa_find(&net_devmem_dmabuf_bindings, &xa_idx, id,
-			  NET_DEVMEM_BINDING_LIVE);
-	if (binding) {
-		if (!net_devmem_dmabuf_binding_get(binding))
-			binding = NULL;
-	}
-	rcu_read_unlock();
-
-	return binding;
-}
-
 void net_devmem_net_iov_inc_ref(struct net_iov *niov)
 {
 	net_devmem_dmabuf_binding_get(net_devmem_iov_binding(niov));
@@ -550,19 +533,21 @@ struct net_devmem_dmabuf_binding *net_devmem_get_binding(struct sock *sk,
 							 unsigned int dmabuf_id)
 {
 	struct net_devmem_dmabuf_binding *binding;
+	unsigned long xa_idx = dmabuf_id;
 	struct net_device *dst_dev;
 	struct dst_entry *dst;
-	int err = 0;
+	int err;
 
-	binding = net_devmem_lookup_dmabuf(dmabuf_id);
+	rcu_read_lock();
+	binding = xa_find(&net_devmem_dmabuf_bindings, &xa_idx, dmabuf_id,
+			  NET_DEVMEM_BINDING_LIVE);
 	if (!binding || !binding->tx_vec ||
 	    !net_eq(sock_net(sk), read_pnet(&binding->net)) ||
 	    !uid_eq(sk_uid(sk), binding->uid)) {
 		err = -EINVAL;
-		goto out_err;
+		goto out_unlock;
 	}
 
-	rcu_read_lock();
 	dst = __sk_dst_get(sk);
 	/* If dst is NULL (route expired), attempt to rebuild it. */
 	if (unlikely(!dst)) {
@@ -588,15 +573,16 @@ struct net_devmem_dmabuf_binding *net_devmem_get_binding(struct sock *sk,
 		goto out_unlock;
 	}
 
+	if (!net_devmem_dmabuf_binding_get(binding)) {
+		err = -EINVAL;
+		goto out_unlock;
+	}
+
 	rcu_read_unlock();
 	return binding;
 
 out_unlock:
 	rcu_read_unlock();
-out_err:
-	if (binding)
-		net_devmem_dmabuf_binding_put(binding);
-
 	return ERR_PTR(err);
 }
 
diff --git a/net/core/devmem.h b/net/core/devmem.h
index c4f476947dfaa..d3a5a482d99bf 100644
--- a/net/core/devmem.h
+++ b/net/core/devmem.h
@@ -98,7 +98,6 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
 		       struct netdev_nl_sock *priv,
 		       const struct sock *nl_sk,
 		       struct netlink_ext_ack *extack);
-struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id);
 struct net_device *
 net_devmem_lock_dev(struct net_devmem_dmabuf_binding *binding,
 		    netdevice_tracker *dev_tracker);
@@ -183,11 +182,6 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
 	return ERR_PTR(-EOPNOTSUPP);
 }
 
-static inline struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id)
-{
-	return NULL;
-}
-
 static inline struct net_device *
 net_devmem_lock_dev(struct net_devmem_dmabuf_binding *binding,
 		    netdevice_tracker *dev_tracker)
-- 
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 ` Mina Almasry [this message]
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 ` [PATCH net-next v5 4/6] net: devmem: synchronize and unmap all device bindings on unregister Mina Almasry
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-3-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=sdf@fomichev.me \
    --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