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>,
Simon Horman <horms@kernel.org>, Shuah Khan <shuah@kernel.org>,
linux-kselftest@vger.kernel.org,
Weiming Shi <bestswngs@gmail.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Nikolay Aleksandrov <razor@blackwall.org>,
David Wei <dw@davidwei.uk>,
Stanislav Fomichev <sdf.kernel@gmail.com>,
Bobby Eshleman <bobbyeshleman@meta.com>
Subject: [PATCH net-next v4 4/7] net: devmem: fold net_devmem_lookup_dmabuf() into net_devmem_get_binding()
Date: Fri, 20 Nov 2026 04:59:15 +0000 [thread overview]
Message-ID: <20261120045918.1239746-5-almasrymina@google.com> (raw)
In-Reply-To: <20261120045918.1239746-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->map_dev and binding->bind_dev.
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.
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 604718bed3cb2..6f85b90950f8b 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();
@@ -521,23 +521,6 @@ net_devmem_bind_dmabuf(struct net_device *map_dev,
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));
@@ -552,19 +535,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)) {
@@ -590,15 +575,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 9dcb629e10bfd..5bfb1b21d19f5 100644
--- a/net/core/devmem.h
+++ b/net/core/devmem.h
@@ -98,7 +98,6 @@ net_devmem_bind_dmabuf(struct net_device *map_dev,
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_device **dev_ptr,
netdevice_tracker *dev_tracker);
@@ -184,11 +183,6 @@ net_devmem_bind_dmabuf(struct net_device *map_dev,
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_device **dev_ptr,
netdevice_tracker *dev_tracker)
--
2.56.1.750.gf70dc682cf-goog
next prev parent reply other threads:[~2026-11-20 5:50 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 4:59 [PATCH net-next v4 0/7] net: devmem: support leased RX and TX queues on netdev unregister Mina Almasry
2026-11-20 4:59 ` [PATCH net-next v4 1/7] net: devmem: pass net_device pointer to net_devmem_lock_dev() Mina Almasry
2026-11-20 4:59 ` [PATCH net-next v4 2/7] net: devmem: rename binding dev and vdev to map_dev and bind_dev Mina Almasry
2026-11-20 4:59 ` [PATCH net-next v4 3/7] net: guard memory provider RX queue open and close on unregister Mina Almasry
2026-11-20 4:59 ` Mina Almasry [this message]
2026-11-20 4:59 ` [PATCH net-next v4 5/7] net: devmem: always populate bind_dev and detach both devices " Mina Almasry
2026-11-20 4:59 ` [PATCH net-next v4 6/7] net: devmem: resolve physical map_dev for leased RX bindings Mina Almasry
2026-11-20 4:59 ` [PATCH net-next v4 7/7] 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=20261120045918.1239746-5-almasrymina@google.com \
--to=almasrymina@google.com \
--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=horms@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 \
/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