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, linux-kernel@vger.kernel.org,
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>,
Dragos Tatulea <dtatulea@nvidia.com>
Subject: [PATCH net-next v2 4/6] net: devmem: always populate bind_dev and detach both devices on unregister
Date: Sun, 8 Nov 2026 05:58:07 +0000 [thread overview]
Message-ID: <20261108055809.2122934-5-almasrymina@google.com> (raw)
In-Reply-To: <20261108055809.2122934-1-almasrymina@google.com>
Previously, bind_dev was only populated when bind-tx was called on a
virtual netdev (where map_dev != netdev), and socket teardown, queue
uninstall, and net_devmem_dev_unregister() did not uniformly track both
bind_dev and map_dev across RX and TX bindings.
Always populate bind_dev with the net_device the user called bind-rx or
bind-tx on (equal to map_dev for direct bindings, or the virtual device
for leased bindings), and handle teardown and unregistration of both
pointers in either order:
- Lock &binding->bind_dev during socket teardown in
netdev_nl_sock_priv_destroy() and pass binding->bind_dev to
netif_mp_close_rxq() in net_devmem_unbind_dmabuf(), because bound_rxqs
stores queues belonging to bind_dev and netif_mp_close_rxq() expects
the leasee device so it can redirect through the lease and lock
map_dev.
- Require binding->map_dev to remain non-NULL in
net_devmem_get_binding() so TX lookups fail once map_dev unregisters
even if bind_dev is still registered.
- Clear binding->bind_dev in mp_dmabuf_devmem_uninstall() once
bound_rxqs is empty, leaving leased bindings mapped until the binding
is freed or map_dev is unregistered, because
netif_rxq_cleanup_unlease() runs while map_dev may still be up and
ignores __netif_mp_close_rxq() failures.
- In net_devmem_dev_unregister(dev), if binding->bind_dev == dev, clear
NET_DEVMEM_BINDING_LIVE to stop new lookups, and clear
binding->bind_dev only when bound_rxqs is empty or dev is not a queue
leasee so leased RX bindings keep binding->bind_dev valid until
ndo_uninit's netif_rxq_cleanup_unlease() uninstalls the virtual
queues. If binding->map_dev == dev, clear NET_DEVMEM_BINDING_LIVE,
unmap the dmabuf while map_dev is still valid, and clear
binding->map_dev.
Signed-off-by: Mina Almasry <almasrymina@google.com>
---
net/core/devmem.c | 62 ++++++++++++++++++++----------------------
net/core/devmem.h | 8 ++++--
net/core/netdev-genl.c | 16 +++++------
3 files changed, 43 insertions(+), 43 deletions(-)
diff --git a/net/core/devmem.c b/net/core/devmem.c
index 6446355e787d4..bb785a871d1c4 100644
--- a/net/core/devmem.c
+++ b/net/core/devmem.c
@@ -86,7 +86,7 @@ net_devmem_dmabuf_binding_unmap(struct net_devmem_dmabuf_binding *binding)
/**
* net_devmem_lock_dev() - lock a binding's net_device pointer if still alive
- * @dev_ptr: pointer to the net_device field on a devmem binding
+ * @dev_ptr: pointer to &binding->map_dev or &binding->bind_dev
* @dev_tracker: reftracker for the held net_device reference
*
* Safely dereference @dev_ptr, acquire netdev_lock() on the device, and
@@ -95,17 +95,18 @@ net_devmem_dmabuf_binding_unmap(struct net_devmem_dmabuf_binding *binding)
* Invariant: *@dev_ptr is set once at bind time and is only ever cleared to
* NULL while holding netdev_lock() on that device (in
* mp_dmabuf_devmem_uninstall(), net_devmem_unbind_dmabuf(), or
- * net_devmem_dev_unregister() during NETDEV_UNREGISTER before
- * synchronize_net() and netdev_run_todo() free the net_device).
+ * net_devmem_dev_unregister(), all before synchronize_net() and
+ * netdev_run_todo() free the net_device).
*
* Taking rcu_read_lock() guarantees that a non-NULL device read from
* *@dev_ptr has not yet completed unregister_netdevice_many_notify()
* (which waits for an RCU grace period via synchronize_net() after
- * NETDEV_UNREGISTER before freeing the net_device), so netdev_hold() can
- * safely acquire a reference before sleeping on netdev_lock(). Once
- * netdev_lock() is held, re-checking that READ_ONCE(*dev_ptr) == dev
- * guarantees that the device has not yet been detached from the binding and
- * cannot finish unregistering until net_devmem_unlock_dev().
+ * NETDEV_UNREGISTER and ndo_uninit before freeing the net_device), so
+ * netdev_hold() can safely acquire a reference before sleeping on
+ * netdev_lock(). Once netdev_lock() is held, re-checking that
+ * READ_ONCE(*dev_ptr) == dev guarantees that the device has not yet been
+ * detached from the binding and cannot finish unregistering until
+ * net_devmem_unlock_dev().
*
* Return: The locked and held net_device if *@dev_ptr is still non-NULL,
* or NULL if already cleared.
@@ -227,14 +228,14 @@ void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding)
rxq_idx = get_netdev_rx_queue_index(rxq);
- if (netif_mp_close_rxq(binding->map_dev, rxq_idx, &mp_params))
+ if (netif_mp_close_rxq(binding->bind_dev, rxq_idx, &mp_params))
busy = true;
}
- if (binding->direction == DMA_FROM_DEVICE && binding->map_dev &&
- !busy) {
- net_devmem_dmabuf_binding_unmap(binding);
- WRITE_ONCE(binding->map_dev, NULL);
+ if (binding->bind_dev) {
+ if (binding->direction == DMA_FROM_DEVICE && !busy)
+ net_devmem_dmabuf_binding_unmap(binding);
+ WRITE_ONCE(binding->bind_dev, NULL);
}
percpu_ref_kill(&binding->ref);
@@ -252,24 +253,20 @@ 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->map_dev) != dev ||
- xa_get_mark(&net_devmem_dmabuf_bindings, xa_idx,
- NET_DEVMEM_BINDING_LIVE))
- continue;
- } else {
- if (READ_ONCE(binding->bind_dev) == dev)
- WRITE_ONCE(binding->bind_dev, NULL);
- if (READ_ONCE(binding->map_dev) != dev)
- continue;
- WRITE_ONCE(binding->bind_dev, NULL);
+ if (READ_ONCE(binding->bind_dev) == dev) {
__xa_clear_mark(&net_devmem_dmabuf_bindings, xa_idx,
NET_DEVMEM_BINDING_LIVE);
+ if (xa_empty(&binding->bound_rxqs) ||
+ !netif_is_queue_leasee(dev))
+ WRITE_ONCE(binding->bind_dev, NULL);
}
+ if (READ_ONCE(binding->map_dev) != dev)
+ continue;
+ __xa_clear_mark(&net_devmem_dmabuf_bindings, xa_idx,
+ NET_DEVMEM_BINDING_LIVE);
xa_unlock(&net_devmem_dmabuf_bindings);
- if (!netif_is_queue_leasee(dev))
- net_devmem_dmabuf_binding_unmap(binding);
+ net_devmem_dmabuf_binding_unmap(binding);
xa_lock(&net_devmem_dmabuf_bindings);
WRITE_ONCE(binding->map_dev, NULL);
}
@@ -529,7 +526,7 @@ struct net_devmem_dmabuf_binding *net_devmem_get_binding(struct sock *sk,
unsigned int dmabuf_id)
{
struct net_devmem_dmabuf_binding *binding;
- struct net_device *dst_dev;
+ struct net_device *dst_dev, *map_dev;
struct dst_entry *dst;
int err = 0;
@@ -560,9 +557,10 @@ struct net_devmem_dmabuf_binding *net_devmem_get_binding(struct sock *sk,
* net_device.
*/
dst_dev = dst_dev_rcu(dst);
- if (unlikely(!dst_dev) ||
- unlikely(dst_dev != READ_ONCE(binding->map_dev) &&
- dst_dev != READ_ONCE(binding->bind_dev))) {
+ map_dev = READ_ONCE(binding->map_dev);
+ if (unlikely(!dst_dev || !map_dev ||
+ (dst_dev != map_dev &&
+ dst_dev != READ_ONCE(binding->bind_dev)))) {
err = -ENODEV;
goto out_unlock;
}
@@ -686,8 +684,8 @@ static void mp_dmabuf_devmem_uninstall(void *mp_priv,
if (xa_empty(&binding->bound_rxqs)) {
if (!rxq->lease)
net_devmem_dmabuf_binding_unmap(binding);
- ASSERT_EXCLUSIVE_WRITER(binding->map_dev);
- WRITE_ONCE(binding->map_dev, NULL);
+ ASSERT_EXCLUSIVE_WRITER(binding->bind_dev);
+ WRITE_ONCE(binding->bind_dev, NULL);
}
break;
}
diff --git a/net/core/devmem.h b/net/core/devmem.h
index 9dcb629e10bfd..ff431e5e398e0 100644
--- a/net/core/devmem.h
+++ b/net/core/devmem.h
@@ -25,8 +25,10 @@ struct net_devmem_dmabuf_binding {
* time and cleared to NULL under netdev_lock(map_dev).
*/
struct net_device *map_dev;
- /* Device the user called bind-rx or bind-tx on. Set at bind time and
- * cleared to NULL on unregistration.
+ /* Device the user called bind-rx or bind-tx on. Equal to map_dev for
+ * direct bindings, or the virtual device (e.g. netkit) for leased
+ * bindings. Set at bind time and cleared to NULL under
+ * netdev_lock(bind_dev).
*/
struct net_device *bind_dev;
possible_net_t net;
@@ -44,7 +46,7 @@ struct net_devmem_dmabuf_binding {
* 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
+ * Both RX and TX bindings are also unmapped earlier if map_dev is
* unregistered.
*
* net_devmem_net_iov_inc_ref() on dmabuf net_iovs will increment this
diff --git a/net/core/netdev-genl.c b/net/core/netdev-genl.c
index 962d7a8c75e9a..9e56c26fc65f9 100644
--- a/net/core/netdev-genl.c
+++ b/net/core/netdev-genl.c
@@ -1110,9 +1110,10 @@ int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct genl_info *info)
goto err_rxq_bitmap;
}
- binding = net_devmem_bind_dmabuf(netdev, NULL, dma_dev, DMA_FROM_DEVICE,
- dmabuf_fd, niov_shift, priv,
- NETLINK_CB(skb).sk, info->extack);
+ binding = net_devmem_bind_dmabuf(netdev, netdev, dma_dev,
+ DMA_FROM_DEVICE, dmabuf_fd,
+ niov_shift, priv, NETLINK_CB(skb).sk,
+ info->extack);
if (IS_ERR(binding)) {
err = PTR_ERR(binding);
goto err_rxq_bitmap;
@@ -1257,10 +1258,9 @@ int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct genl_info *info)
dma_dev = netdev_queue_get_dma_dev(map_dev, 0, NETDEV_QUEUE_TYPE_TX);
- binding = net_devmem_bind_dmabuf(map_dev,
- map_dev != netdev ? netdev : NULL,
- dma_dev, DMA_TO_DEVICE, dmabuf_fd,
- PAGE_SHIFT, priv, NETLINK_CB(skb).sk,
+ binding = net_devmem_bind_dmabuf(map_dev, netdev, dma_dev,
+ DMA_TO_DEVICE, dmabuf_fd, PAGE_SHIFT,
+ priv, NETLINK_CB(skb).sk,
info->extack);
if (IS_ERR(binding)) {
err = PTR_ERR(binding);
@@ -1475,7 +1475,7 @@ void netdev_nl_sock_priv_destroy(struct netdev_nl_sock *priv)
mutex_lock(&priv->lock);
list_for_each_entry_safe(binding, temp, &priv->bindings, list) {
- dev = net_devmem_lock_dev(&binding->map_dev, &dev_tracker);
+ dev = net_devmem_lock_dev(&binding->bind_dev, &dev_tracker);
net_devmem_unbind_dmabuf(binding);
net_devmem_unlock_dev(dev, &dev_tracker);
}
--
2.56.0.455.g6c5a503b38-goog
next prev parent reply other threads:[~2026-11-08 5:58 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-08 5:58 [PATCH net-next v2 0/6] net: devmem: support leased RX and TX queues on netdev unregister Mina Almasry
2026-11-08 5:58 ` [PATCH net-next v2 1/6] net: devmem: pass net_device pointer to net_devmem_lock_dev() Mina Almasry
2026-11-08 5:58 ` [PATCH net-next v2 2/6] net: devmem: rename binding dev and vdev to map_dev and bind_dev Mina Almasry
2026-11-08 5:58 ` [PATCH net-next v2 3/6] net: guard memory provider RX queue open and close on unregister Mina Almasry
2026-11-08 5:58 ` Mina Almasry [this message]
2026-11-08 5:58 ` [PATCH net-next v2 5/6] net: devmem: resolve physical map_dev for leased RX bindings Mina Almasry
2026-11-08 5:58 ` [PATCH net-next v2 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=20261108055809.2122934-5-almasrymina@google.com \
--to=almasrymina@google.com \
--cc=bobbyeshleman@meta.com \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=dtatulea@nvidia.com \
--cc=dw@davidwei.uk \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.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
all inboxes | Powered by JetHome®