* [PATCH net-next v5 1/6] net: guard leased queue ops against physical netdev unregister
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 ` 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
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Mina Almasry @ 2026-12-04 23:10 UTC (permalink / raw)
To: netdev
Cc: Mina Almasry, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Willem de Bruijn, Simon Horman, Shuah Khan,
linux-kselftest, Stanislav Fomichev, Bobby Eshleman,
Nikolay Aleksandrov, Daniel Borkmann, David Wei, Pavel Begunkov,
Jens Axboe, Weiming Shi, io-uring, Jesper Dangaard Brouer,
Ilias Apalodimas
When a virtual device (e.g. netkit) leases an RX queue from a physical
device, netdev_rx_queue_lease() holds a reference on the physical device
until netkit's ndo_uninit() runs. During unregister_netdevice_many_notify()
on the physical device, operations traversing the lease from the virtual
device can race with teardown after Loop 1 marks the physical device
NETREG_UNREGISTERING:
CPU0 (unregister phys_dev) CPU1 (leasee path)
-------------------------- ------------------
dev_close(phys_dev)
reg_state = NETREG_UNREGISTERING
netdev_lock(virt_dev)
netdev_lock(phys_dev)
-> binds/opens/queries dying dev
-> or reconfigs closed phys_dev
dev_memory_provider_uninstall(phys)
NETDEV_UNREGISTER(phys)
cascaded netkit_uninit() -> unlease
In addition, in a same-batch unregister_netdevice_many([netkit, phys_dev]),
Loop 1 sets NETREG_UNREGISTERING on both devices and netkit's ndo_uninit()
-> netif_rxq_cleanup_unlease() can call __netif_mp_close_rxq(phys_dev)
before phys_dev's dev_memory_provider_uninstall() runs, attempting queue
reconfiguration on an unregistering device.
Add netdev_lock_if_alive() to atomically lock a net_device only if it has
not entered unregistration, and use it in netif_mp_open_rxq(),
netdev_queue_get_dma_dev(), and netdev_nl_bind_tx_doit() (also re-checking
netif_device_present() under the physical device's lock).
In __netif_mp_close_rxq(), when !netif_is_alive(dev) and mp_params is
still installed, validate and clear rxq->mp_params directly without
invoking netdev_rx_queue_reconfig(), since the device has already been
closed.
These races were identified by code inspection while adding leased-queue
unregister support for devmem; 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:
- Replace netif_lease_valid() with netdev_lock_if_alive() and guard
netdev_queue_get_dma_dev(), netif_mp_open_rxq(), and
netdev_nl_bind_tx_doit() (Paolo).
---
include/net/netdev_lock.h | 9 +++++++++
net/core/netdev-genl.c | 14 ++++++++++++--
net/core/netdev_queues.c | 3 ++-
net/core/netdev_rx_queue.c | 19 ++++++++++++++-----
4 files changed, 37 insertions(+), 8 deletions(-)
diff --git a/include/net/netdev_lock.h b/include/net/netdev_lock.h
index edd8cc2e2b36e..71e6f26b0d971 100644
--- a/include/net/netdev_lock.h
+++ b/include/net/netdev_lock.h
@@ -12,6 +12,15 @@ static inline bool netdev_trylock(struct net_device *dev)
return mutex_trylock(&dev->lock);
}
+static inline bool __must_check netdev_lock_if_alive(struct net_device *dev)
+{
+ netdev_lock(dev);
+ if (netif_is_alive(dev))
+ return true;
+ netdev_unlock(dev);
+ return false;
+}
+
static inline void netdev_assert_locked(const struct net_device *dev)
{
lockdep_assert_held(&dev->lock);
diff --git a/net/core/netdev-genl.c b/net/core/netdev-genl.c
index d2ccb1090496d..87dac9c5e84c3 100644
--- a/net/core/netdev-genl.c
+++ b/net/core/netdev-genl.c
@@ -1253,8 +1253,18 @@ int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct genl_info *info)
goto err_unlock_netdev;
}
- if (bind_dev != netdev)
- netdev_lock(bind_dev);
+ if (bind_dev != netdev) {
+ if (!netdev_lock_if_alive(bind_dev)) {
+ err = -ENODEV;
+ NL_SET_ERR_MSG(info->extack,
+ "Device is being unregistered");
+ goto err_unlock_netdev;
+ }
+ if (!netif_device_present(bind_dev)) {
+ err = -ENODEV;
+ goto err_unlock_bind_dev;
+ }
+ }
dma_dev = netdev_queue_get_dma_dev(bind_dev, 0, NETDEV_QUEUE_TYPE_TX);
diff --git a/net/core/netdev_queues.c b/net/core/netdev_queues.c
index 27cb6397bdd4e..c796957c07f4b 100644
--- a/net/core/netdev_queues.c
+++ b/net/core/netdev_queues.c
@@ -50,7 +50,8 @@ struct device *netdev_queue_get_dma_dev(struct net_device *dev,
hw_rxq = __netif_get_rx_queue(dev, idx)->lease;
- netdev_lock(hw_rxq->dev);
+ if (!netdev_lock_if_alive(hw_rxq->dev))
+ return NULL;
idx = get_netdev_rx_queue_index(hw_rxq);
dma_dev = __netdev_queue_get_dma_dev(hw_rxq->dev, idx);
netdev_unlock(hw_rxq->dev);
diff --git a/net/core/netdev_rx_queue.c b/net/core/netdev_rx_queue.c
index a7261fedd5e76..e3ae99210caeb 100644
--- a/net/core/netdev_rx_queue.c
+++ b/net/core/netdev_rx_queue.c
@@ -269,7 +269,10 @@ int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
return -EOPNOTSUPP;
}
- netdev_lock(dev);
+ if (!netdev_lock_if_alive(dev)) {
+ NL_SET_ERR_MSG(extack, "Device is being unregistered");
+ return -ENODEV;
+ }
ret = __netif_mp_open_rxq(dev, rxq_idx, p, extack);
netdev_unlock(dev);
return ret;
@@ -284,17 +287,23 @@ static int __netif_mp_close_rxq(struct net_device *dev, unsigned int rxq_idx,
rxq = __netif_get_rx_queue(dev, rxq_idx);
- /* Callers holding a netdev ref may get here after we already
- * went thru shutdown via dev_memory_provider_uninstall().
+ /* Callers holding a netdev ref may get here after the device entered
+ * unregistration, either before or after
+ * dev_memory_provider_uninstall() ran. The device is already closed,
+ * so clear mp_params without reconfiguring the queue.
*/
- if (dev->reg_state > NETREG_REGISTERED &&
- !rxq->mp_params.mp_ops)
+ if (!netif_is_alive(dev) && !rxq->mp_params.mp_ops)
return 0;
if (WARN_ON_ONCE(rxq->mp_params.mp_ops != old_p->mp_ops ||
rxq->mp_params.mp_priv != old_p->mp_priv))
return -EINVAL;
+ if (!netif_is_alive(dev)) {
+ memset(&rxq->mp_params, 0, sizeof(rxq->mp_params));
+ return 0;
+ }
+
netdev_queue_config(dev, rxq_idx, &qcfg[0]);
memset(&rxq->mp_params, 0, sizeof(rxq->mp_params));
netdev_queue_config(dev, rxq_idx, &qcfg[1]);
--
2.56.1.915.g69d5ec7298-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net-next v5 2/6] net: devmem: fold net_devmem_lookup_dmabuf() into net_devmem_get_binding()
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
2026-12-04 23:10 ` [PATCH net-next v5 3/6] net: devmem: resolve leased RX queues to physical device on bind Mina Almasry
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Mina Almasry @ 2026-12-04 23:10 UTC (permalink / raw)
To: netdev
Cc: Mina Almasry, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Willem de Bruijn, Simon Horman, Shuah Khan,
linux-kselftest, Stanislav Fomichev, Bobby Eshleman,
Nikolay Aleksandrov, Daniel Borkmann, David Wei, Pavel Begunkov,
Jens Axboe, Weiming Shi, io-uring, Jesper Dangaard Brouer,
Ilias Apalodimas, Stanislav Fomichev
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
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net-next v5 3/6] net: devmem: resolve leased RX queues to physical device on bind
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 ` 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
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Mina Almasry @ 2026-12-04 23:10 UTC (permalink / raw)
To: netdev
Cc: Mina Almasry, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Willem de Bruijn, Simon Horman, Shuah Khan,
linux-kselftest, Stanislav Fomichev, Bobby Eshleman,
Nikolay Aleksandrov, Daniel Borkmann, David Wei, Pavel Begunkov,
Jens Axboe, Weiming Shi, io-uring, Jesper Dangaard Brouer,
Ilias Apalodimas
Currently, netdev_nl_bind_rx_doit() stores the virtual leasee (e.g.
netkit) in binding->dev and its virtual RX queues in binding->bound_rxqs,
relying on netif_mp_{open,close}_rxq() to redirect each queue operation to
the underlying physical device. This breaks netdev unregistration for
leased RX bindings in several ways:
- When the physical device unregisters first,
dev_memory_provider_uninstall(phys_dev) passes phys_rxq to
mp_dmabuf_devmem_uninstall(), which searches binding->bound_rxqs for
virt_rxq, fails to match, and leaves the dmabuf mapped.
- When the virtual device unregisters first, mp_dmabuf_devmem_uninstall()
skips unmapping whenever rxq->lease is set, leaking the DMA mapping on
the physical device until the netlink socket closes.
- Leaving rxq->mp_params clearing to each memory provider (done by
io_pp_uninstall() but not mp_dmabuf_devmem_uninstall()) causes the
cascaded netif_rxq_cleanup_unlease() on physical device unregister to
invoke ->uninstall() a second time on an already-uninstalled queue.
- netdev_nl_sock_priv_destroy() locks binding->dev (virt_dev) while
physical device teardown locks phys_dev, leaving unmap/unbind
unsynchronized across the two lock domains.
- Calling netdev_queue_get_dma_dev() and netif_mp_open_rxq() without
holding netdev_lock(phys_dev) continuously leaves a TOCTOU window where
phys_dev can begin unregistering mid-bind.
Resolve leased RX queues to their physical net_device and physical
rx_queue indices once at bind-rx time, holding netdev_lock(virt_dev) ->
netdev_lock(phys_dev) continuously across DMA device lookup, dmabuf
mapping, and queue opens:
- Store phys_dev in binding->dev and phys_rxq in binding->bound_rxqs so
RX teardown paths (dev_memory_provider_uninstall(),
netif_rxq_cleanup_unlease(), netdev_nl_sock_priv_destroy(), and
net_devmem_dev_unregister()) serialize on netdev_lock(phys_dev).
- Reject direct bind-rx on leased-out physical queues, unleased or mixed
virtual queues, and queues spanning multiple physical devices up front
in netdev_nl_resolve_rx_phys(), and reject any queue returning a NULL
DMA device in netdev_nl_get_dma_dev().
- Expose __netdev_queue_get_dma_dev() and __netif_mp_{open,close}_rxq()
internally so devmem operates directly on the already-validated and
locked phys_dev, while netif_mp_{open,close}_rxq() continues to proxy
for io_uring/zcrx.
- Clear rxq->mp_params centrally in __netif_mp_uninstall_rxq() (dropping
the now-redundant clearing from io_pp_uninstall()), pass phys_rxq to
__netif_mp_uninstall_rxq() in netif_rxq_cleanup_unlease(), and drop
the !rxq->lease guard in mp_dmabuf_devmem_uninstall() so virtual-first,
physical-first, and same-batch teardown all unmap once bound_rxqs
empties.
These issues were identified by code inspection and selftest development
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:
- Drop v4's dual-device bind_dev/map_dev split in favour of resolving
leased RX queues to phys_dev and phys_rxq at bind-rx time while
holding netdev_lock(virt_dev) -> netdev_lock(phys_dev) continuously
(Willem).
- Clear rxq->mp_params centrally in __netif_mp_uninstall_rxq().
---
io_uring/zcrx.c | 4 --
net/core/dev.h | 10 ++-
net/core/devmem.c | 36 +++++-----
net/core/netdev-genl.c | 130 +++++++++++++++++++++++++++++++++----
net/core/netdev_queues.c | 4 +-
net/core/netdev_rx_queue.c | 35 ++++++----
6 files changed, 166 insertions(+), 53 deletions(-)
diff --git a/io_uring/zcrx.c b/io_uring/zcrx.c
index 21e042b3ccb97..20597bfa146d6 100644
--- a/io_uring/zcrx.c
+++ b/io_uring/zcrx.c
@@ -1459,15 +1459,11 @@ static int io_pp_nl_fill(void *mp_priv, struct sk_buff *rsp,
static void io_pp_uninstall(void *mp_priv, struct netdev_rx_queue *rxq)
{
- struct pp_memory_provider_params *p = &rxq->mp_params;
struct io_zcrx_ifq *ifq = mp_priv;
guard(mutex)(&ifq->pp_lock);
io_zcrx_unmap_areas(ifq);
io_zcrx_drop_netdev(ifq);
-
- p->mp_ops = NULL;
- p->mp_priv = NULL;
}
static const struct memory_provider_ops io_uring_pp_zc_ops = {
diff --git a/net/core/dev.h b/net/core/dev.h
index 4e9e8cd156977..183c580006b54 100644
--- a/net/core/dev.h
+++ b/net/core/dev.h
@@ -119,12 +119,16 @@ bool netif_rxq_has_mp(struct net_device *dev, unsigned int rxq_idx);
bool netif_rxq_is_leased(struct net_device *dev, unsigned int rxq_idx);
bool netif_is_queue_leasee(const struct net_device *dev);
+struct device *__netdev_queue_get_dma_dev(struct net_device *dev,
+ unsigned int idx);
+int __netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
+ const struct pp_memory_provider_params *p,
+ struct netlink_ext_ack *extack);
+int __netif_mp_close_rxq(struct net_device *dev, unsigned int rxq_idx,
+ const struct pp_memory_provider_params *old_p);
void __netif_mp_uninstall_rxq(struct netdev_rx_queue *rxq,
const struct pp_memory_provider_params *p);
-void netif_rxq_cleanup_unlease(struct netdev_rx_queue *phys_rxq,
- struct netdev_rx_queue *virt_rxq);
-
/* netdev management, shared between various uAPI entry points */
struct netdev_name_node {
struct hlist_node hlist;
diff --git a/net/core/devmem.c b/net/core/devmem.c
index f00c1d8fcc2d2..3a894ff06b32b 100644
--- a/net/core/devmem.c
+++ b/net/core/devmem.c
@@ -219,21 +219,24 @@ void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding)
if (binding->list.next)
list_del(&binding->list);
- xa_for_each(&binding->bound_rxqs, xa_idx, rxq) {
- const struct pp_memory_provider_params mp_params = {
- .mp_priv = binding,
- .mp_ops = &dmabuf_devmem_ops,
- };
-
- rxq_idx = get_netdev_rx_queue_index(rxq);
-
- if (netif_mp_close_rxq(binding->dev, rxq_idx, &mp_params))
- busy = true;
- }
+ if (READ_ONCE(binding->dev)) {
+ xa_for_each(&binding->bound_rxqs, xa_idx, rxq) {
+ const struct pp_memory_provider_params mp_params = {
+ .mp_priv = binding,
+ .mp_ops = &dmabuf_devmem_ops,
+ };
+
+ rxq_idx = get_netdev_rx_queue_index(rxq);
+
+ if (__netif_mp_close_rxq(binding->dev, rxq_idx,
+ &mp_params))
+ busy = true;
+ }
- if (binding->direction == DMA_FROM_DEVICE && binding->dev && !busy) {
- net_devmem_dmabuf_binding_unmap(binding);
- WRITE_ONCE(binding->dev, NULL);
+ if (binding->direction == DMA_FROM_DEVICE && !busy) {
+ net_devmem_dmabuf_binding_unmap(binding);
+ WRITE_ONCE(binding->dev, NULL);
+ }
}
percpu_ref_kill(&binding->ref);
@@ -296,7 +299,7 @@ int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx,
if (err)
return err;
- err = netif_mp_open_rxq(dev, rxq_idx, &mp_params, extack);
+ err = __netif_mp_open_rxq(dev, rxq_idx, &mp_params, extack);
if (err)
xa_erase(&binding->bound_rxqs, xa_idx);
@@ -693,8 +696,7 @@ static void mp_dmabuf_devmem_uninstall(void *mp_priv,
xa_erase(&binding->bound_rxqs, xa_idx);
if (xa_empty(&binding->bound_rxqs)) {
- if (!rxq->lease)
- net_devmem_dmabuf_binding_unmap(binding);
+ net_devmem_dmabuf_binding_unmap(binding);
ASSERT_EXCLUSIVE_WRITER(binding->dev);
WRITE_ONCE(binding->dev, NULL);
}
diff --git a/net/core/netdev-genl.c b/net/core/netdev-genl.c
index 87dac9c5e84c3..5150e8b0a0c57 100644
--- a/net/core/netdev-genl.c
+++ b/net/core/netdev-genl.c
@@ -1003,6 +1003,91 @@ static int netdev_nl_read_rxq_bitmap(struct genl_info *info,
return 0;
}
+static int netdev_nl_resolve_rx_phys(struct net_device *netdev,
+ unsigned long *rxq_bitmap,
+ struct net_device **phys_dev_out,
+ unsigned long **phys_bitmap_out,
+ struct netlink_ext_ack *extack)
+{
+ struct net_device *phys_dev = NULL;
+ unsigned long *phys_bitmap;
+ u32 rxq_idx;
+ int err;
+
+ if (!netif_is_queue_leasee(netdev)) {
+ for_each_set_bit(rxq_idx, rxq_bitmap,
+ netdev->real_num_rx_queues) {
+ if (netif_rxq_is_leased(netdev, rxq_idx)) {
+ NL_SET_ERR_MSG(extack,
+ "rx queue leased to a virtual netdev");
+ return -EOPNOTSUPP;
+ }
+ }
+ *phys_dev_out = netdev;
+ *phys_bitmap_out = rxq_bitmap;
+ return 0;
+ }
+
+ for_each_set_bit(rxq_idx, rxq_bitmap, netdev->real_num_rx_queues) {
+ struct netdev_rx_queue *rxq = __netif_get_rx_queue(netdev,
+ rxq_idx);
+
+ if (!rxq->lease || !rxq->lease->dev->dev.parent) {
+ NL_SET_ERR_MSG(extack,
+ "rx queue belongs to a virtual netdev");
+ return -EOPNOTSUPP;
+ }
+ if (phys_dev && rxq->lease->dev != phys_dev) {
+ NL_SET_ERR_MSG(extack,
+ "Cannot bind queues from different physical devices");
+ return -EOPNOTSUPP;
+ }
+ phys_dev = rxq->lease->dev;
+ }
+
+ if (!phys_dev)
+ return -EINVAL;
+
+ if (!netdev_lock_if_alive(phys_dev)) {
+ NL_SET_ERR_MSG(extack, "Device is being unregistered");
+ return -ENODEV;
+ }
+
+ if (!netif_device_present(phys_dev)) {
+ err = -ENODEV;
+ goto err_unlock_phys;
+ }
+
+ phys_bitmap = bitmap_zalloc(phys_dev->real_num_rx_queues, GFP_KERNEL);
+ if (!phys_bitmap) {
+ err = -ENOMEM;
+ goto err_unlock_phys;
+ }
+
+ for_each_set_bit(rxq_idx, rxq_bitmap, netdev->real_num_rx_queues) {
+ struct netdev_rx_queue *phys_rxq =
+ __netif_get_rx_queue(netdev, rxq_idx)->lease;
+ unsigned int phys_rxq_idx = get_netdev_rx_queue_index(phys_rxq);
+
+ if (WARN_ON_ONCE(phys_rxq_idx >=
+ phys_dev->real_num_rx_queues)) {
+ err = -ERANGE;
+ goto err_free_bitmap;
+ }
+ bitmap_set(phys_bitmap, phys_rxq_idx, 1);
+ }
+
+ *phys_dev_out = phys_dev;
+ *phys_bitmap_out = phys_bitmap;
+ return 0;
+
+err_free_bitmap:
+ bitmap_free(phys_bitmap);
+err_unlock_phys:
+ netdev_unlock(phys_dev);
+ return err;
+}
+
static struct device *
netdev_nl_get_dma_dev(struct net_device *netdev, unsigned long *rxq_bitmap,
struct netlink_ext_ack *extack)
@@ -1013,8 +1098,11 @@ netdev_nl_get_dma_dev(struct net_device *netdev, unsigned long *rxq_bitmap,
for_each_set_bit(rxq_idx, rxq_bitmap, netdev->real_num_rx_queues) {
struct device *rxq_dma_dev;
- rxq_dma_dev = netdev_queue_get_dma_dev(netdev, rxq_idx,
- NETDEV_QUEUE_TYPE_RX);
+ rxq_dma_dev = __netdev_queue_get_dma_dev(netdev, rxq_idx);
+ if (!rxq_dma_dev) {
+ NL_SET_ERR_MSG(extack, "Device doesn't support DMA");
+ return ERR_PTR(-EOPNOTSUPP);
+ }
if (dma_dev && rxq_dma_dev != dma_dev) {
NL_SET_ERR_MSG_FMT(extack, "DMA device mismatch between queue %u and %u (multi-PF device?)",
rxq_idx, prev_rxq_idx);
@@ -1031,11 +1119,11 @@ netdev_nl_get_dma_dev(struct net_device *netdev, unsigned long *rxq_bitmap,
int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct genl_info *info)
{
struct net_devmem_dmabuf_binding *binding;
+ unsigned long *rxq_bitmap, *phys_bitmap;
unsigned int niov_shift = PAGE_SHIFT;
+ struct net_device *netdev, *phys_dev;
u32 ifindex, dmabuf_fd, rxq_idx;
struct netdev_nl_sock *priv;
- struct net_device *netdev;
- unsigned long *rxq_bitmap;
struct device *dma_dev;
struct sk_buff *rsp;
int err = 0;
@@ -1104,23 +1192,29 @@ int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct genl_info *info)
if (err)
goto err_rxq_bitmap;
- dma_dev = netdev_nl_get_dma_dev(netdev, rxq_bitmap, info->extack);
+ err = netdev_nl_resolve_rx_phys(netdev, rxq_bitmap, &phys_dev,
+ &phys_bitmap, info->extack);
+ if (err)
+ goto err_rxq_bitmap;
+
+ dma_dev = netdev_nl_get_dma_dev(phys_dev, phys_bitmap, info->extack);
if (IS_ERR(dma_dev)) {
err = PTR_ERR(dma_dev);
- goto err_rxq_bitmap;
+ goto err_unlock_phys;
}
- 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(phys_dev, NULL, 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;
+ goto err_unlock_phys;
}
- for_each_set_bit(rxq_idx, rxq_bitmap, netdev->real_num_rx_queues) {
- err = net_devmem_bind_dmabuf_to_queue(netdev, rxq_idx, binding,
- info->extack);
+ for_each_set_bit(rxq_idx, phys_bitmap, phys_dev->real_num_rx_queues) {
+ err = net_devmem_bind_dmabuf_to_queue(phys_dev, rxq_idx,
+ binding, info->extack);
if (err)
goto err_unbind;
}
@@ -1132,6 +1226,11 @@ int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct genl_info *info)
err = genlmsg_reply(rsp, info);
+ if (phys_dev != netdev) {
+ bitmap_free(phys_bitmap);
+ netdev_unlock(phys_dev);
+ }
+
bitmap_free(rxq_bitmap);
netdev_unlock(netdev);
@@ -1142,6 +1241,11 @@ int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct genl_info *info)
err_unbind:
net_devmem_unbind_dmabuf(binding);
+err_unlock_phys:
+ if (phys_dev != netdev) {
+ bitmap_free(phys_bitmap);
+ netdev_unlock(phys_dev);
+ }
err_rxq_bitmap:
bitmap_free(rxq_bitmap);
err_unlock:
diff --git a/net/core/netdev_queues.c b/net/core/netdev_queues.c
index c796957c07f4b..19ef14d58e045 100644
--- a/net/core/netdev_queues.c
+++ b/net/core/netdev_queues.c
@@ -6,8 +6,8 @@
#include "dev.h"
-static struct device *
-__netdev_queue_get_dma_dev(struct net_device *dev, unsigned int idx)
+struct device *__netdev_queue_get_dma_dev(struct net_device *dev,
+ unsigned int idx)
{
const struct netdev_queue_mgmt_ops *queue_ops = dev->queue_mgmt_ops;
struct device *dma_dev;
diff --git a/net/core/netdev_rx_queue.c b/net/core/netdev_rx_queue.c
index e3ae99210caeb..dc918f6cd4eeb 100644
--- a/net/core/netdev_rx_queue.c
+++ b/net/core/netdev_rx_queue.c
@@ -10,6 +10,8 @@
#include "dev.h"
#include "page_pool_priv.h"
+static void netif_rxq_cleanup_unlease(struct netdev_rx_queue *phys_rxq);
+
void netdev_rx_queue_lease(struct netdev_rx_queue *rxq_dst,
struct netdev_rx_queue *rxq_src)
{
@@ -28,7 +30,7 @@ void netdev_rx_queue_unlease(struct netdev_rx_queue *rxq_dst,
netdev_assert_locked(rxq_dst->dev);
netdev_assert_locked(rxq_src->dev);
- netif_rxq_cleanup_unlease(rxq_src, rxq_dst);
+ netif_rxq_cleanup_unlease(rxq_src);
WRITE_ONCE(rxq_src->lease, NULL);
WRITE_ONCE(rxq_dst->lease, NULL);
@@ -184,9 +186,9 @@ int netdev_rx_queue_restart(struct net_device *dev, unsigned int rxq_idx)
}
EXPORT_SYMBOL_NS_GPL(netdev_rx_queue_restart, "NETDEV_INTERNAL");
-static int __netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
- const struct pp_memory_provider_params *p,
- struct netlink_ext_ack *extack)
+int __netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
+ const struct pp_memory_provider_params *p,
+ struct netlink_ext_ack *extack)
{
const struct netdev_queue_mgmt_ops *qops = dev->queue_mgmt_ops;
struct netdev_queue_config qcfg[2];
@@ -278,8 +280,8 @@ int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
return ret;
}
-static int __netif_mp_close_rxq(struct net_device *dev, unsigned int rxq_idx,
- const struct pp_memory_provider_params *old_p)
+int __netif_mp_close_rxq(struct net_device *dev, unsigned int rxq_idx,
+ const struct pp_memory_provider_params *old_p)
{
struct netdev_queue_config qcfg[2];
struct netdev_rx_queue *rxq;
@@ -338,20 +340,25 @@ int netif_mp_close_rxq(struct net_device *dev, unsigned int rxq_idx,
void __netif_mp_uninstall_rxq(struct netdev_rx_queue *rxq,
const struct pp_memory_provider_params *p)
{
- if (p->mp_ops && p->mp_ops->uninstall)
- p->mp_ops->uninstall(p->mp_priv, rxq);
+ struct pp_memory_provider_params tmp = *p;
+
+ if (!tmp.mp_ops)
+ return;
+
+ memset(&rxq->mp_params, 0, sizeof(rxq->mp_params));
+ if (tmp.mp_ops->uninstall)
+ tmp.mp_ops->uninstall(tmp.mp_priv, rxq);
}
/* Clean up memory provider state when a queue lease is torn down. If
- * a memory provider was installed on the physical queue via the lease,
+ * a memory provider was installed on the physical queue while leased,
* close it now. The memory provider is a property of the queue itself,
- * and it was _guaranteed_ to be installed on the physical queue via
- * the lease redirection. The extra __netif_mp_close_rxq is needed
+ * and it was _guaranteed_ to be installed on the physical queue while
+ * the lease was active. The extra __netif_mp_close_rxq is needed
* since the physical queue can outlive the virtual queue in the lease
* case, so it needs to be reconfigured to clear the memory provider.
*/
-void netif_rxq_cleanup_unlease(struct netdev_rx_queue *phys_rxq,
- struct netdev_rx_queue *virt_rxq)
+static void netif_rxq_cleanup_unlease(struct netdev_rx_queue *phys_rxq)
{
unsigned int rxq_idx = get_netdev_rx_queue_index(phys_rxq);
struct pp_memory_provider_params p = phys_rxq->mp_params;
@@ -360,5 +367,5 @@ void netif_rxq_cleanup_unlease(struct netdev_rx_queue *phys_rxq,
return;
__netif_mp_close_rxq(phys_rxq->dev, rxq_idx, &p);
- __netif_mp_uninstall_rxq(virt_rxq, &p);
+ __netif_mp_uninstall_rxq(phys_rxq, &p);
}
--
2.56.1.915.g69d5ec7298-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net-next v5 4/6] net: devmem: synchronize and unmap all device bindings on unregister
2026-12-04 23:10 [PATCH net-next v5 0/6] net: devmem: support leased RX and TX queues on netdev unregister Mina Almasry
` (2 preceding siblings ...)
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
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
5 siblings, 0 replies; 7+ messages in thread
From: Mina Almasry @ 2026-12-04 23:10 UTC (permalink / raw)
To: netdev
Cc: Mina Almasry, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Willem de Bruijn, Simon Horman, Shuah Khan,
linux-kselftest, Stanislav Fomichev, Bobby Eshleman,
Nikolay Aleksandrov, Daniel Borkmann, David Wei, Pavel Begunkov,
Jens Axboe, Weiming Shi, io-uring, Jesper Dangaard Brouer,
Ilias Apalodimas
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
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net-next v5 5/6] net: propagate busy status on memory provider uninstall
2026-12-04 23:10 [PATCH net-next v5 0/6] net: devmem: support leased RX and TX queues on netdev unregister Mina Almasry
` (3 preceding siblings ...)
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 ` 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
5 siblings, 0 replies; 7+ messages in thread
From: Mina Almasry @ 2026-12-04 23:10 UTC (permalink / raw)
To: netdev
Cc: Mina Almasry, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Willem de Bruijn, Simon Horman, Shuah Khan,
linux-kselftest, Stanislav Fomichev, Bobby Eshleman,
Nikolay Aleksandrov, Daniel Borkmann, David Wei, Pavel Begunkov,
Jens Axboe, Weiming Shi, io-uring, Jesper Dangaard Brouer,
Ilias Apalodimas
netif_rxq_cleanup_unlease() currently discards the return value of
__netif_mp_close_rxq(), so memory providers cannot tell whether closing
a leased queue failed and hardware may still be using the page pool.
Tracking failed closes in a local variable in net_devmem_unbind_dmabuf()
is also insufficient once leased-queue uninstalls span multiple queues
or precede userspace socket close.
Pass a bool busy flag (!!err from __netif_mp_close_rxq()) through
memory_provider_ops->uninstall() and record failed closes in a new
binding->rxq_busy flag on struct net_devmem_dmabuf_binding. Both
mp_dmabuf_devmem_uninstall() and net_devmem_unbind_dmabuf() then defer
unmapping whenever any queue of the binding failed to close, leaving
cleanup to net_devmem_dev_unregister() or
__net_devmem_dmabuf_binding_free(). Existing io_uring/zcrx uninstall
behavior is left unchanged.
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 busy queue-close propagation into its own patch, adding
bool busy to memory_provider_ops->uninstall() and binding->rxq_busy.
---
include/net/page_pool/memory_provider.h | 7 ++++++-
io_uring/zcrx.c | 3 ++-
net/core/dev.c | 2 +-
net/core/dev.h | 3 ++-
net/core/devmem.c | 12 +++++++-----
net/core/devmem.h | 1 +
net/core/netdev_rx_queue.c | 10 ++++++----
7 files changed, 25 insertions(+), 13 deletions(-)
diff --git a/include/net/page_pool/memory_provider.h b/include/net/page_pool/memory_provider.h
index bceb5f7740e2f..82eac3feefc94 100644
--- a/include/net/page_pool/memory_provider.h
+++ b/include/net/page_pool/memory_provider.h
@@ -26,7 +26,12 @@ struct memory_provider_ops {
void (*destroy)(struct page_pool *pool);
int (*nl_fill)(void *mp_priv, struct sk_buff *rsp,
struct netdev_rx_queue *rxq);
- void (*uninstall)(void *mp_priv, struct netdev_rx_queue *rxq);
+ /* Called under netdev_lock(rxq->dev) on device unregister or queue
+ * unlease. If @busy is true, closing @rxq failed and hardware may
+ * still be using the page pool.
+ */
+ void (*uninstall)(void *mp_priv, struct netdev_rx_queue *rxq,
+ bool busy);
};
bool net_mp_niov_set_dma_addr(struct net_iov *niov, dma_addr_t addr);
diff --git a/io_uring/zcrx.c b/io_uring/zcrx.c
index 20597bfa146d6..ed44835a783b5 100644
--- a/io_uring/zcrx.c
+++ b/io_uring/zcrx.c
@@ -1457,7 +1457,8 @@ static int io_pp_nl_fill(void *mp_priv, struct sk_buff *rsp,
return 0;
}
-static void io_pp_uninstall(void *mp_priv, struct netdev_rx_queue *rxq)
+static void io_pp_uninstall(void *mp_priv, struct netdev_rx_queue *rxq,
+ bool busy)
{
struct io_zcrx_ifq *ifq = mp_priv;
diff --git a/net/core/dev.c b/net/core/dev.c
index b7757f91fdf92..55af817b61e5a 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -12774,7 +12774,7 @@ static void dev_memory_provider_uninstall(struct net_device *dev)
for (i = 0; i < dev->real_num_rx_queues; i++) {
struct netdev_rx_queue *rxq = &dev->_rx[i];
- __netif_mp_uninstall_rxq(rxq, &rxq->mp_params);
+ __netif_mp_uninstall_rxq(rxq, &rxq->mp_params, false);
}
}
diff --git a/net/core/dev.h b/net/core/dev.h
index 183c580006b54..2132cbde9d232 100644
--- a/net/core/dev.h
+++ b/net/core/dev.h
@@ -127,7 +127,8 @@ int __netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
int __netif_mp_close_rxq(struct net_device *dev, unsigned int rxq_idx,
const struct pp_memory_provider_params *old_p);
void __netif_mp_uninstall_rxq(struct netdev_rx_queue *rxq,
- const struct pp_memory_provider_params *p);
+ const struct pp_memory_provider_params *p,
+ bool busy);
/* netdev management, shared between various uAPI entry points */
struct netdev_name_node {
diff --git a/net/core/devmem.c b/net/core/devmem.c
index a395f100d2f33..01621c0f8b485 100644
--- a/net/core/devmem.c
+++ b/net/core/devmem.c
@@ -206,7 +206,6 @@ void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding)
struct netdev_rx_queue *rxq;
unsigned long xa_idx;
unsigned int rxq_idx;
- bool busy = false;
xa_clear_mark(&net_devmem_dmabuf_bindings, binding->id,
NET_DEVMEM_BINDING_LIVE);
@@ -230,10 +229,11 @@ void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding)
if (__netif_mp_close_rxq(binding->dev, rxq_idx,
&mp_params))
- busy = true;
+ binding->rxq_busy = true;
}
- if (binding->direction == DMA_FROM_DEVICE && !busy) {
+ if (binding->direction == DMA_FROM_DEVICE &&
+ !binding->rxq_busy) {
net_devmem_dmabuf_binding_unmap(binding);
WRITE_ONCE(binding->dev, NULL);
}
@@ -692,7 +692,7 @@ static int mp_dmabuf_devmem_nl_fill(void *mp_priv, struct sk_buff *rsp,
}
static void mp_dmabuf_devmem_uninstall(void *mp_priv,
- struct netdev_rx_queue *rxq)
+ struct netdev_rx_queue *rxq, bool busy)
{
struct net_devmem_dmabuf_binding *binding = mp_priv;
struct netdev_rx_queue *bound_rxq;
@@ -702,8 +702,10 @@ static void mp_dmabuf_devmem_uninstall(void *mp_priv,
if (bound_rxq != rxq)
continue;
+ if (busy)
+ binding->rxq_busy = true;
xa_erase(&binding->bound_rxqs, xa_idx);
- if (xa_empty(&binding->bound_rxqs)) {
+ if (!binding->rxq_busy && xa_empty(&binding->bound_rxqs)) {
net_devmem_dmabuf_binding_unmap(binding);
ASSERT_EXCLUSIVE_WRITER(binding->dev);
WRITE_ONCE(binding->dev, NULL);
diff --git a/net/core/devmem.h b/net/core/devmem.h
index 4036aab286c4d..cd65c16c4337d 100644
--- a/net/core/devmem.h
+++ b/net/core/devmem.h
@@ -83,6 +83,7 @@ struct net_devmem_dmabuf_binding {
struct net_iov **tx_vec;
unsigned int niov_shift;
+ bool rxq_busy;
struct work_struct unbind_w;
};
diff --git a/net/core/netdev_rx_queue.c b/net/core/netdev_rx_queue.c
index dc918f6cd4eeb..78eee3d243f75 100644
--- a/net/core/netdev_rx_queue.c
+++ b/net/core/netdev_rx_queue.c
@@ -338,7 +338,8 @@ int netif_mp_close_rxq(struct net_device *dev, unsigned int rxq_idx,
}
void __netif_mp_uninstall_rxq(struct netdev_rx_queue *rxq,
- const struct pp_memory_provider_params *p)
+ const struct pp_memory_provider_params *p,
+ bool busy)
{
struct pp_memory_provider_params tmp = *p;
@@ -347,7 +348,7 @@ void __netif_mp_uninstall_rxq(struct netdev_rx_queue *rxq,
memset(&rxq->mp_params, 0, sizeof(rxq->mp_params));
if (tmp.mp_ops->uninstall)
- tmp.mp_ops->uninstall(tmp.mp_priv, rxq);
+ tmp.mp_ops->uninstall(tmp.mp_priv, rxq, busy);
}
/* Clean up memory provider state when a queue lease is torn down. If
@@ -362,10 +363,11 @@ static void netif_rxq_cleanup_unlease(struct netdev_rx_queue *phys_rxq)
{
unsigned int rxq_idx = get_netdev_rx_queue_index(phys_rxq);
struct pp_memory_provider_params p = phys_rxq->mp_params;
+ int err;
if (!p.mp_ops)
return;
- __netif_mp_close_rxq(phys_rxq->dev, rxq_idx, &p);
- __netif_mp_uninstall_rxq(phys_rxq, &p);
+ err = __netif_mp_close_rxq(phys_rxq->dev, rxq_idx, &p);
+ __netif_mp_uninstall_rxq(phys_rxq, &p, !!err);
}
--
2.56.1.915.g69d5ec7298-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net-next v5 6/6] selftests: net: add leased devmem RX and TX netdev unregister tests
2026-12-04 23:10 [PATCH net-next v5 0/6] net: devmem: support leased RX and TX queues on netdev unregister Mina Almasry
` (4 preceding siblings ...)
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 ` Mina Almasry
5 siblings, 0 replies; 7+ messages in thread
From: Mina Almasry @ 2026-12-04 23:10 UTC (permalink / raw)
To: netdev
Cc: Mina Almasry, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Willem de Bruijn, Simon Horman, Shuah Khan,
linux-kselftest, Stanislav Fomichev, Bobby Eshleman,
Nikolay Aleksandrov, Daniel Borkmann, David Wei, Pavel Begunkov,
Jens Axboe, Weiming Shi, io-uring, Jesper Dangaard Brouer,
Ilias Apalodimas
Extend nl_netdev.py to cover netdev unregistration and validation for
devmem RX and TX bindings on a virtual netkit device leasing an RX queue
from netdevsim:
1. devmem_lease_bind_rx_unregister_check: verifies direct bind-rx on a
leased-out physical queue and mixed unleased/leased bitmaps on netkit
are rejected with EOPNOTSUPP, and verifies clean immediate dmabuf
detach both when netkit is deleted first (while netdevsim stays up)
and when netdevsim is unregistered directly (cascading unregistration
to netkit).
2. devmem_lease_bind_rx_netns_check: binds a leased RX dmabuf, moves
netkit into a child netns, and verifies clean dmabuf detach and
physical queue cleanup when the netns exits while the netlink socket
remains open.
3. devmem_lease_bind_tx_unregister_check: binds a TX dmabuf to a netkit
device backed by a leased netdevsim queue and verifies the physical
TX mapping remains attached when netkit is deleted first and detaches
immediately when netdevsim is unregistered.
Signed-off-by: Mina Almasry <almasrymina@google.com>
---
v4 -> v5:
- Add negative checks for direct bind-rx on leased-out physical queues
and mixed unleased/leased bitmaps on netkit.
- Add devmem_lease_bind_rx_netns_check for netkit netns exit teardown.
- Update devmem_lease_bind_tx_unregister_check to verify the physical TX
mapping remains attached when netkit is deleted first.
---
tools/testing/selftests/net/nl_netdev.py | 223 ++++++++++++++++++++++-
1 file changed, 219 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/net/nl_netdev.py b/tools/testing/selftests/net/nl_netdev.py
index dd0321c0b463a..bfc1e165df633 100755
--- a/tools/testing/selftests/net/nl_netdev.py
+++ b/tools/testing/selftests/net/nl_netdev.py
@@ -12,8 +12,10 @@ import os
import struct
from os import system
from lib.py import ksft_run, ksft_exit, KsftFailEx, KsftSkipEx
-from lib.py import ksft_eq, ksft_ge, ksft_ne, ksft_raises, ksft_busy_wait
-from lib.py import EthtoolFamily, NetdevFamily, NetdevSimDev, NlError, defer, ip
+from lib.py import ksft_eq, ksft_ge, ksft_in, ksft_ne, ksft_not_in
+from lib.py import ksft_raises, ksft_busy_wait
+from lib.py import EthtoolFamily, NetdevFamily, NetdevSimDev, Netlink
+from lib.py import NetNS, NlError, RtnlFamily, cmd, defer, ip
def empty_check(nf) -> None:
@@ -423,12 +425,22 @@ def _dmabuf_attach_count(dmabuf_fd) -> int:
raise KsftFailEx(f"dma-buf {ino} not found in {path}")
-def _devmem_bind(bind, attrs) -> None:
+def _devmem_bind(bind, attrs, phys_attrs=None) -> None:
"""Run a devmem bind op, skip the test if the device can't do devmem."""
try:
bind(attrs)
except NlError as e:
if e.error == errno.EOPNOTSUPP:
+ if phys_attrs is not None:
+ try:
+ bind(phys_attrs)
+ except NlError as probe_e:
+ if probe_e.error == errno.EOPNOTSUPP:
+ raise KsftSkipEx(
+ f"devmem binding not supported: {probe_e}"
+ ) from probe_e
+ raise e from probe_e
+ raise
raise KsftSkipEx(f"devmem binding not supported: {e}") from e
raise
@@ -483,6 +495,206 @@ def devmem_bind_tx_unregister_check(_nf) -> None:
nf_priv.close()
+def _create_netkit_single(rxqueues=2):
+ """Create a single-device netkit interface for queue lease tests."""
+ ifname = f"nk{os.getpid()}"
+ try:
+ with RtnlFamily() as rtnl:
+ rtnl.newlink(
+ {
+ "ifname": ifname,
+ "linkinfo": {
+ "kind": "netkit",
+ "data": {
+ "mode": "l2",
+ "pairing": "single",
+ },
+ },
+ "num-rx-queues": rxqueues,
+ },
+ flags=[Netlink.NLM_F_CREATE, Netlink.NLM_F_EXCL],
+ )
+ except NlError as e:
+ if e.error in (errno.EOPNOTSUPP, errno.ENOENT):
+ raise KsftSkipEx(f"netkit single mode not supported: {e}") from e
+ raise
+ defer(cmd, f"ip link del dev {ifname}", fail=False)
+
+ link = ip(f"-d link show dev {ifname}", json=True)[0]
+ return ifname, link["ifindex"]
+
+
+def _queue_lease(nf, nk_ifindex, phys_ifindex, phys_qid=1):
+ try:
+ return nf.queue_create({
+ "ifindex": nk_ifindex,
+ "type": "rx",
+ "lease": {
+ "ifindex": phys_ifindex,
+ "queue": {"id": phys_qid, "type": "rx"},
+ },
+ })
+ except NlError as e:
+ if e.error == errno.EOPNOTSUPP:
+ raise KsftSkipEx(f"queue lease not supported: {e}") from e
+ raise
+
+
+def devmem_lease_bind_rx_unregister_check(nf) -> None:
+ """Verify leased RX devmem bindings detach cleanly on virt/phys unregister."""
+ for remove_virt_first in (False, True):
+ dmabuf_fd = _create_udmabuf()
+ defer(os.close, dmabuf_fd)
+ nf_priv = NetdevFamily()
+ defer(nf_priv.close)
+ nsimdev = NetdevSimDev(queue_count=2)
+ defer(nsimdev.remove)
+ nsim = nsimdev.nsims[0]
+
+ ip(f"link set dev {nsim.ifname} up")
+ with EthtoolFamily() as ef:
+ ef.rings_set({"header": {"dev-index": nsim.ifindex},
+ "tcp-data-split": "enabled"})
+
+ nk_ifname, nk_ifindex = _create_netkit_single(rxqueues=2)
+ ip(f"link set dev {nk_ifname} up")
+
+ res = _queue_lease(nf, nk_ifindex, nsim.ifindex)
+
+ _devmem_bind(nf_priv.bind_rx, {
+ "ifindex": nk_ifindex,
+ "fd": dmabuf_fd,
+ "queues": [{"id": res["id"], "type": "rx"}],
+ }, phys_attrs={
+ "ifindex": nsim.ifindex,
+ "fd": dmabuf_fd,
+ "queues": [{"id": 0, "type": "rx"}],
+ })
+ ksft_ge(_dmabuf_attach_count(dmabuf_fd), 1)
+ phys_q = nf.queue_get({"ifindex": nsim.ifindex, "id": 1, "type": "rx"})
+ ksft_in("dmabuf", phys_q)
+ ksft_in("lease", phys_q)
+
+ if not remove_virt_first:
+ # Reject direct bind-rx on a leased-out physical queue.
+ with ksft_raises(NlError) as e:
+ nf_priv.bind_rx({
+ "ifindex": nsim.ifindex,
+ "fd": dmabuf_fd,
+ "queues": [{"id": 1, "type": "rx"}],
+ })
+ ksft_eq(e.exception.error, errno.EOPNOTSUPP)
+
+ # Reject mixed unleased + leased queue bitmaps on the virtual device.
+ with ksft_raises(NlError) as e:
+ nf_priv.bind_rx({
+ "ifindex": nk_ifindex,
+ "fd": dmabuf_fd,
+ "queues": [
+ {"id": 0, "type": "rx"},
+ {"id": res["id"], "type": "rx"},
+ ],
+ })
+ ksft_eq(e.exception.error, errno.EOPNOTSUPP)
+
+ if remove_virt_first:
+ ip(f"link del dev {nk_ifname}")
+ phys_q = nf.queue_get({"ifindex": nsim.ifindex, "id": 1,
+ "type": "rx"})
+ ksft_not_in("dmabuf", phys_q)
+ ksft_not_in("lease", phys_q)
+ ksft_eq(_dmabuf_attach_count(dmabuf_fd), 0)
+ nsimdev.remove()
+ else:
+ nsimdev.remove()
+ ksft_ne(cmd(f"ip link show dev {nk_ifname}", fail=False).ret, 0)
+ cmd(f"ip link del dev {nk_ifname}", fail=False)
+ ksft_eq(_dmabuf_attach_count(dmabuf_fd), 0)
+
+ nf_priv.close()
+
+
+def devmem_lease_bind_rx_netns_check(nf) -> None:
+ """Verify leased RX devmem bindings detach cleanly on netns teardown."""
+ dmabuf_fd = _create_udmabuf()
+ defer(os.close, dmabuf_fd)
+ nf_priv = NetdevFamily()
+ defer(nf_priv.close)
+ nsimdev = NetdevSimDev(queue_count=2)
+ defer(nsimdev.remove)
+ nsim = nsimdev.nsims[0]
+
+ ip(f"link set dev {nsim.ifname} up")
+ with EthtoolFamily() as ef:
+ ef.rings_set({"header": {"dev-index": nsim.ifindex},
+ "tcp-data-split": "enabled"})
+
+ nk_ifname, nk_ifindex = _create_netkit_single(rxqueues=2)
+ ip(f"link set dev {nk_ifname} up")
+
+ res = _queue_lease(nf, nk_ifindex, nsim.ifindex)
+ _devmem_bind(nf_priv.bind_rx, {
+ "ifindex": nk_ifindex,
+ "fd": dmabuf_fd,
+ "queues": [{"id": res["id"], "type": "rx"}],
+ }, phys_attrs={
+ "ifindex": nsim.ifindex,
+ "fd": dmabuf_fd,
+ "queues": [{"id": 0, "type": "rx"}],
+ })
+ ksft_ge(_dmabuf_attach_count(dmabuf_fd), 1)
+
+ with NetNS() as netns:
+ ip(f"link set dev {nk_ifname} netns {netns.name}")
+ ip(f"link set dev {nk_ifname} up", ns=netns)
+
+ ksft_busy_wait(lambda: _dmabuf_attach_count(dmabuf_fd) == 0, deadline=10)
+ phys_q = nf.queue_get({"ifindex": nsim.ifindex, "id": 1, "type": "rx"})
+ ksft_not_in("dmabuf", phys_q)
+ ksft_not_in("lease", phys_q)
+ nf_priv.close()
+
+
+def devmem_lease_bind_tx_unregister_check(nf) -> None:
+ """Verify leased TX devmem bindings detach cleanly on virt/phys unregister."""
+ for remove_virt_first in (False, True):
+ dmabuf_fd = _create_udmabuf()
+ defer(os.close, dmabuf_fd)
+ nf_priv = NetdevFamily()
+ defer(nf_priv.close)
+ nsimdev = NetdevSimDev(queue_count=2)
+ defer(nsimdev.remove)
+ nsim = nsimdev.nsims[0]
+
+ ip(f"link set dev {nsim.ifname} up")
+
+ nk_ifname, nk_ifindex = _create_netkit_single(rxqueues=2)
+ ip(f"link set dev {nk_ifname} up")
+
+ _queue_lease(nf, nk_ifindex, nsim.ifindex)
+
+ _devmem_bind(nf_priv.bind_tx, {
+ "ifindex": nk_ifindex,
+ "fd": dmabuf_fd,
+ }, phys_attrs={
+ "ifindex": nsim.ifindex,
+ "fd": dmabuf_fd,
+ })
+ ksft_ge(_dmabuf_attach_count(dmabuf_fd), 1)
+
+ if remove_virt_first:
+ ip(f"link del dev {nk_ifname}")
+ ksft_ge(_dmabuf_attach_count(dmabuf_fd), 1)
+ nsimdev.remove()
+ else:
+ nsimdev.remove()
+ ksft_ne(cmd(f"ip link show dev {nk_ifname}", fail=False).ret, 0)
+ cmd(f"ip link del dev {nk_ifname}", fail=False)
+ ksft_eq(_dmabuf_attach_count(dmabuf_fd), 0)
+
+ nf_priv.close()
+
+
def main() -> None:
""" Ksft boiler plate main """
nf = NetdevFamily()
@@ -498,7 +710,10 @@ def main() -> None:
page_pool_ifindex_leak_check,
page_pool_stats_ifindex_check,
devmem_bind_rx_unregister_check,
- devmem_bind_tx_unregister_check
+ devmem_bind_tx_unregister_check,
+ devmem_lease_bind_rx_unregister_check,
+ devmem_lease_bind_rx_netns_check,
+ devmem_lease_bind_tx_unregister_check
],
args=(nf, ))
ksft_exit()
--
2.56.1.915.g69d5ec7298-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread