net: devmem: support leased RX and TX queues on netdev unregister
Subsystems (3)
v4 cover net: devmem: support leased RX and TX queues on netdev unregister
Support leased RX and TX devmem bindings when either the virtual device (e.g. netkit) or the physical backing net_device is unregistered:
1. Pass struct net_device **dev_ptr to net_devmem_lock_dev() so callers can lock either binding->map_dev or binding->bind_dev.
2. Rename binding->dev and binding->vdev to binding->map_dev (physical DMA device) and binding->bind_dev (device passed to bind-rx/bind-tx).
3. Introduce netif_lease_valid() and guard netif_mp_open_rxq(), netif_mp_close_rxq(), and netif_rxq_cleanup_unlease() against an unregistering physical device reached through a queue lease.
4. Fold net_devmem_lookup_dmabuf() into net_devmem_get_binding() so the xarray lookup, validation, device check, and reference acquisition run under a single rcu_read_lock() section.
5. Always populate bind_dev on RX and TX bindings and handle teardown and netdev unregistration of both bind_dev and map_dev, waiting for synchronize_net() after clearing NET_DEVMEM_BINDING_LIVE before unmapping bindings whose map_dev is unregistering.
6. Resolve the physical backing map_dev in netdev_nl_get_dma_dev() for leased RX bindings, lock map_dev around net_devmem_bind_dmabuf() on bind-rx when map_dev differs from netdev, assert both devices are locked, and check netif_lease_valid(map_dev).
7. Add kselftests in nl_netdev.py covering leased RX and TX devmem bindings across both virtual-first and physical-first netdev unregistration orders.
These issues were discovered during AI code review (Sashiko) of the earlier non-leased devmem unregister series (and subsequent mailing list review) and confirmed by code inspection (not observed in production or reproduced on real hardware). Tested with the nl_netdev.py selftest on netdevsim added in this series.
v4:
- Rebase onto latest net-next/main.
- Fold net_devmem_lookup_dmabuf() into net_devmem_get_binding() under a single rcu_read_lock() (deferring net_devmem_dmabuf_binding_get() until after all checks pass) and update net_devmem_dev_unregister() to use a two-pass loop with a single synchronize_net() after clearing NET_DEVMEM_BINDING_LIVE and before unmapping bindings with
map_dev == dev (Paolo).
- Document issue discovery, validation, and testing in commit messages and the cover letter (netdev-bot+sinfo).
- v3: https://lore.kernel.org/netdev/[email protected]/
v3:
- Rebase onto latest net-next/main.
- Add netif_lease_valid() helper and keep lease validity checks in the lease-aware callers (netif_mp_open_rxq(), netif_mp_close_rxq(), netif_rxq_cleanup_unlease(), and net_devmem_bind_dmabuf()) rather than inside __netif_mp_open_rxq() / __netif_mp_close_rxq(), and document why mp_params is cleared directly when !netif_lease_valid(dev) (Stanislav, Sashiko).
- Guard the bound_rxqs loop in net_devmem_unbind_dmabuf() with
if (binding->bind_dev), keep the !netif_is_queue_leasee(dev) guard in net_devmem_dev_unregister() in patch 4/6 until patch 5/6 resolves binding->map_dev to the physical device so the series is bisectable at every commit, and document the use-after-free in dma_unmap_sg_attrs() on physical netdev teardown in patch 5/6 (Sashiko).
- In nl_netdev.py, verify dmabuf and lease on the physical queue after binding, skip when queue_create returns EOPNOTSUPP, probe the physical device on EOPNOTSUPP so a broken lease lookup is not masked as SKIP, and clean up nk_ifname between loop iterations (Sashiko).
- v2: https://lore.kernel.org/netdev/[email protected]/
v2:
- Split patch 3/4 from v1 into 3 smaller commits (patches 3/6..5/6).
- Clear rxq->mp_params and return 0 in __netif_mp_close_rxq() whenever dev is already unregistering, avoiding queue reconfiguration on an already-shut-down map_dev during cascaded lease teardown (Sashiko).
- Drop unnecessary xa_erase() in net_devmem_unbind_dmabuf() and use a local map_dev variable in net_devmem_get_binding() (Stanislav).
- Restore ASSERT_EXCLUSIVE_WRITER(binding->bind_dev) in mp_dmabuf_devmem_uninstall(), drop redundant bind_dev->reg_state check in net_devmem_bind_dmabuf(), and expand commit message rationale (Dragos, Stanislav).
- v1: https://lore.kernel.org/netdev/[email protected]/
Mina Almasry (7):
net: devmem: pass net_device pointer to net_devmem_lock_dev()
net: devmem: rename binding dev and vdev to map_dev and bind_dev
net: guard memory provider RX queue open and close on unregister
net: devmem: fold net_devmem_lookup_dmabuf() into
net_devmem_get_binding()
net: devmem: always populate bind_dev and detach both devices on
unregister
net: devmem: resolve physical map_dev for leased RX bindings
selftests: net: add leased devmem RX and TX netdev unregister tests
net/core/dev.c | 2 +- net/core/dev.h | 7 + net/core/devmem.c | 178 ++++++++++++----------- net/core/devmem.h | 31 ++-- net/core/netdev-genl.c | 59 +++++--- net/core/netdev_rx_queue.c | 31 +++- tools/testing/selftests/net/nl_netdev.py | 159 +++++++++++++++++++- 7 files changed, 333 insertions(+), 134 deletions(-)
base-commit: 2208ab00f4f213ade91ab775e7f8c4dacdcc856e
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v4 1/7 net: devmem: pass net_device pointer to net_devmem_lock_dev()
Upcoming changes for leased queue support will track the physical DMA device and the bound device in separate net_device pointers on struct net_devmem_dmabuf_binding, with different teardown paths locking different pointers.
Generalize net_devmem_lock_dev() to take a struct net_device **dev_ptr instead of struct net_devmem_dmabuf_binding *. No functional change.
--- net/core/devmem.c | 36 ++++++++++++++++++------------------ net/core/devmem.h | 4 ++-- net/core/netdev-genl.c | 2 +- 3 files changed, 21 insertions(+), 21 deletions(-)
net/core/devmem.c +18 -18
@@ -85,39 +85,39 @@ net_devmem_dmabuf_binding_unmap(struct net_devmem_dmabuf_binding *binding) }/** -* net_devmem_lock_dev() - lock a binding's net_device if still alive -* @binding: devmem binding whose @dev pointer to lock +* 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_tracker: reftracker for the held net_device reference * -* Safely dereference @binding->dev, acquire netdev_lock() on the device, and -* verify that @binding->dev has not been cleared while waiting for the lock. +* Safely dereference @dev_ptr, acquire netdev_lock() on the device, and +* verify that *@dev_ptr has not been cleared while waiting for the lock. * -* Invariant: @binding->dev is set once at bind time and is only ever cleared to -* NULL while holding netdev_lock(dev) (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). +* 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). * -* Taking rcu_read_lock() guarantees that a non-NULL @dev read from -* @binding->dev has not yet completed unregister_netdevice_many_notify() +* 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(dev) is held, re-checking that READ_ONCE(binding->dev) == dev -* guarantees that the device has not yet been detached from @binding and +* 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 @binding->dev is still non-NULL, +* Return: The locked and held net_device if *@dev_ptr is still non-NULL, * or NULL if already cleared. */ struct net_device * -net_devmem_lock_dev (struct net_devmem_dmabuf_binding *binding, +net_devmem_lock_dev (struct net_device **dev_ptr,netdevice_tracker *dev_tracker) {struct net_device *dev;rcu_read_lock (); - dev =READ_ONCE (binding->dev); + dev =READ_ONCE (*dev_ptr);if (!dev) {rcu_read_unlock ();return NULL ;
@@ -126,7 +126,7 @@ net_devmem_lock_dev(struct net_devmem_dmabuf_binding *binding,rcu_read_unlock ();netdev_lock (dev); -if (READ_ONCE (binding->dev) != dev) { +if (READ_ONCE (*dev_ptr) != dev) {netdev_unlock (dev);netdev_put (dev, dev_tracker);return NULL ;
@@ -157,7 +157,7 @@ void __net_devmem_dmabuf_binding_free(struct work_struct *wq) binding->area.num_niovs, binding->free_count);kvfree (binding->area.niovs); - dev =net_devmem_lock_dev (binding, &dev_tracker); + dev =net_devmem_lock_dev (&binding->dev, &dev_tracker);net_devmem_dmabuf_binding_unmap (binding);xa_erase (&net_devmem_dmabuf_bindings, binding->id);net_devmem_unlock_dev (dev, &dev_tracker);
net/core/devmem.h +2 -2
@@ -100,7 +100,7 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev, 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, +net_devmem_lock_dev (struct net_device **dev_ptr,netdevice_tracker *dev_tracker);void net_devmem_unlock_dev (struct net_device *dev,netdevice_tracker *dev_tracker);
@@ -189,7 +189,7 @@ static inline struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id) }static inline struct net_device * -net_devmem_lock_dev (struct net_devmem_dmabuf_binding *binding, +net_devmem_lock_dev (struct net_device **dev_ptr,netdevice_tracker *dev_tracker) {return NULL ;
net/core/netdev-genl.c +1 -1
@@ -1476,7 +1476,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, &dev_tracker); + dev =net_devmem_lock_dev (&binding->dev, &dev_tracker);net_devmem_unbind_dmabuf (binding);net_devmem_unlock_dev (dev, &dev_tracker); }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v4 2/7 net: devmem: rename binding dev and vdev to map_dev and bind_dev
In reply to Mina Almasrystruct net_devmem_dmabuf_binding currently tracks two device pointers under the names 'dev' and 'vdev':
- 'dev' is the physical net_device whose dma_dev holds the DMA mapping.
- 'vdev' is the virtual net_device the user bound to when using leased queues.
Rename 'dev' to 'map_dev' and 'vdev' to 'bind_dev' (typed as struct net_device *), and rename the matching local variable in netdev_nl_bind_tx_doit() from 'bind_dev' to 'map_dev' to match. No functional change.
--- net/core/dev.c | 2 +- net/core/devmem.c | 38 ++++++++++++++++++++------------------ net/core/devmem.h | 17 +++++++++-------- net/core/netdev-genl.c | 31 +++++++++++++++---------------- 4 files changed, 45 insertions(+), 43 deletions(-)
net/core/dev.c +1 -1
@@ -4192,7 +4192,7 @@ static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb, niov =netmem_to_net_iov (skb_frag_netmem (&shinfo->frags[0 ]));if (net_is_devmem_iov (niov) && (net_devmem_iov_binding (niov)->direction !=DMA_TO_DEVICE || -READ_ONCE (net_devmem_iov_binding (niov)->dev) != dev)) +READ_ONCE (net_devmem_iov_binding (niov)->map_dev) != dev)) goto out_free; }
net/core/devmem.c +20 -18
@@ -157,7 +157,7 @@ void __net_devmem_dmabuf_binding_free(struct work_struct *wq) binding->area.num_niovs, binding->free_count);kvfree (binding->area.niovs); - dev =net_devmem_lock_dev (&binding->dev, &dev_tracker); + dev =net_devmem_lock_dev (&binding->map_dev, &dev_tracker);net_devmem_dmabuf_binding_unmap (binding);xa_erase (&net_devmem_dmabuf_bindings, binding->id);net_devmem_unlock_dev (dev, &dev_tracker);
@@ -227,13 +227,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->dev, rxq_idx, &mp_params)) +if (netif_mp_close_rxq (binding->map_dev, rxq_idx, &mp_params)) busy = true; } -if (binding->direction ==DMA_FROM_DEVICE && binding->dev && !busy) { +if (binding->direction ==DMA_FROM_DEVICE && binding->map_dev && + !busy) {net_devmem_dmabuf_binding_unmap (binding); -WRITE_ONCE (binding->dev,NULL ); +WRITE_ONCE (binding->map_dev,NULL ); }percpu_ref_kill (&binding->ref);
@@ -252,16 +253,16 @@ 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 || +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->vdev) == dev) -WRITE_ONCE (binding->vdev,NULL ); -if (READ_ONCE (binding->dev) != dev) +if (READ_ONCE (binding->bind_dev) == dev) +WRITE_ONCE (binding->bind_dev,NULL ); +if (READ_ONCE (binding->map_dev) != dev)continue ; -WRITE_ONCE (binding->vdev,NULL ); +WRITE_ONCE (binding->bind_dev,NULL );__xa_clear_mark (&net_devmem_dmabuf_bindings, xa_idx,NET_DEVMEM_BINDING_LIVE ); }
@@ -270,7 +271,7 @@ void net_devmem_dev_unregister(struct net_device *dev)if (!netif_is_queue_leasee (dev))net_devmem_dmabuf_binding_unmap (binding);xa_lock (&net_devmem_dmabuf_bindings); -WRITE_ONCE (binding->dev,NULL ); +WRITE_ONCE (binding->map_dev,NULL ); }xa_unlock (&net_devmem_dmabuf_bindings); }
@@ -318,7 +319,8 @@ static bool net_devmem_sgt_aligned(struct sg_table *sgt, size_t size)/* niov_shift == 0 picks the net_iov size from the dmabuf, TX only */ struct net_devmem_dmabuf_binding * -net_devmem_bind_dmabuf(struct net_device *dev, void *vdev, +net_devmem_bind_dmabuf(struct net_device *map_dev, + struct net_device *bind_dev, struct device *dma_dev, enum dma_data_direction direction, unsigned int dmabuf_fd, unsigned int niov_shift,
@@ -346,14 +348,14 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,return ERR_CAST (dmabuf); binding =kzalloc_node (sizeof (*binding),GFP_KERNEL , -dev_to_node (&dev->dev)); +dev_to_node (&map_dev->dev));if (!binding) { err = -ENOMEM ; goto err_put_dmabuf; } - binding->dev = dev; - binding->vdev = vdev; + binding->map_dev = map_dev; + binding->bind_dev = bind_dev;write_pnet (&binding->net,sock_net (nl_sk)); binding->uid =sk_uid (nl_sk);xa_init_flags (&binding->bound_rxqs,XA_FLAGS_ALLOC );
@@ -582,8 +584,8 @@ struct net_devmem_dmabuf_binding *net_devmem_get_binding(struct sock *sk,*/ dst_dev =dst_dev_rcu (dst);if (unlikely (!dst_dev) || -unlikely (dst_dev !=READ_ONCE (binding->dev) && - dst_dev !=READ_ONCE (binding->vdev))) { +unlikely (dst_dev !=READ_ONCE (binding->map_dev) && + dst_dev !=READ_ONCE (binding->bind_dev))) { err = -ENODEV ; goto out_unlock; }
@@ -709,8 +711,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->dev); -WRITE_ONCE (binding->dev,NULL ); +ASSERT_EXCLUSIVE_WRITER (binding->map_dev); +WRITE_ONCE (binding->map_dev,NULL ); }break ; }
net/core/devmem.h +9 -8
@@ -22,14 +22,13 @@ struct net_devmem_dmabuf_binding {struct dma_buf_attachment *attachment;struct sg_table *sgt;/* Physical NIC that does the actual DMA for this binding. Set at bind -* time and cleared to NULL under netdev_lock(dev). +* time and cleared to NULL under netdev_lock(map_dev). */ -struct net_device *dev; -/* Opaque cookie identifying the virtual device (e.g. netkit) the user -* called bind-tx on. Used only for pointer comparison via READ_ONCE() -* and cleared with WRITE_ONCE() on unregistration. Never dereferenced. +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. */ -void *vdev; +struct net_device *bind_dev;possible_net_t net;kuid_t uid;/* Serialize net_devmem_dmabuf_binding_unmap() on attachment and sgt */
@@ -91,7 +90,8 @@ struct net_devmem_dmabuf_binding {#if defined(CONFIG_NET_DEVMEM )void __net_devmem_dmabuf_binding_free (struct work_struct *wq);struct net_devmem_dmabuf_binding * -net_devmem_bind_dmabuf(struct net_device *dev, void *vdev, +net_devmem_bind_dmabuf(struct net_device *map_dev, + struct net_device *bind_dev, struct device *dma_dev, enum dma_data_direction direction, unsigned int dmabuf_fd, unsigned int niov_shift,
@@ -171,7 +171,8 @@ static inline void net_devmem_net_iov_dec_ref(struct net_iov *niov) }static inline struct net_devmem_dmabuf_binding * -net_devmem_bind_dmabuf(struct net_device *dev, void *vdev, +net_devmem_bind_dmabuf(struct net_device *map_dev, + struct net_device *bind_dev, struct device *dma_dev, enum dma_data_direction direction, unsigned int dmabuf_fd,
net/core/netdev-genl.c +15 -16
@@ -1195,9 +1195,8 @@ netdev_find_netmem_tx_dev(struct net_device *dev)int netdev_nl_bind_tx_doit (struct sk_buff *skb,struct genl_info *info) {struct net_devmem_dmabuf_binding *binding; -struct net_device *bind_dev; +struct net_device *netdev, *map_dev;struct netdev_nl_sock *priv; -struct net_device *netdev;struct device *dma_dev;u32 ifindex, dmabuf_fd;struct sk_buff *rsp;
@@ -1245,27 +1244,27 @@ int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct genl_info *info) goto err_unlock_netdev; } - bind_dev =netdev_find_netmem_tx_dev (netdev); -if (!bind_dev) { + map_dev =netdev_find_netmem_tx_dev (netdev); +if (!map_dev) { err = -EOPNOTSUPP ;NL_SET_ERR_MSG (info->extack,"No DMA-capable device found for netmem TX" ); goto err_unlock_netdev; } -if (bind_dev != netdev) -netdev_lock (bind_dev); +if (map_dev != netdev) +netdev_lock (map_dev); - dma_dev =netdev_queue_get_dma_dev (bind_dev,0 ,NETDEV_QUEUE_TYPE_TX ); + dma_dev =netdev_queue_get_dma_dev (map_dev,0 ,NETDEV_QUEUE_TYPE_TX ); - binding =net_devmem_bind_dmabuf (bind_dev, - bind_dev != netdev ? netdev :NULL , + binding =net_devmem_bind_dmabuf (map_dev, + map_dev != netdev ? netdev :NULL , dma_dev,DMA_TO_DEVICE , dmabuf_fd,0 , priv,NETLINK_CB (skb).sk, info->extack);if (IS_ERR (binding)) { err =PTR_ERR (binding); - goto err_unlock_bind_dev; + goto err_unlock_map_dev; }/* rsp was allocated large enough */
@@ -1273,16 +1272,16 @@ int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct genl_info *info)genlmsg_end (rsp, hdr); -if (bind_dev != netdev) -netdev_unlock (bind_dev); +if (map_dev != netdev) +netdev_unlock (map_dev);netdev_unlock (netdev);mutex_unlock (&priv->lock);return genlmsg_reply (rsp, info); -err_unlock_bind_dev: -if (bind_dev != netdev) -netdev_unlock (bind_dev); +err_unlock_map_dev: +if (map_dev != netdev) +netdev_unlock (map_dev); err_unlock_netdev:netdev_unlock (netdev); err_unlock_sock:
@@ -1476,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->dev, &dev_tracker); + dev =net_devmem_lock_dev (&binding->map_dev, &dev_tracker);net_devmem_unbind_dmabuf (binding);net_devmem_unlock_dev (dev, &dev_tracker); }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v4 3/7 net: guard memory provider RX queue open and close on unregister
In reply to Mina AlmasryWhen an RX queue is leased from a physical backing net_device to a virtual net_device (e.g., netkit), memory provider operations reach the physical net_device through rxq->lease and lock it directly via netdev_lock(dev) rather than looking it up via netdev_get_by_index_lock().
Introduce a netif_lease_valid() helper checking netif_is_alive(dev) under netdev_lock(dev) and use it in the lease-aware callers while keeping __netif_mp_open_rxq() and __netif_mp_close_rxq() unaware of queue leasing:
1. In netif_mp_open_rxq(), if the leased physical device is already unregistering (!netif_lease_valid(dev)), netdev_get_by_index_lock() on the virtual netdev succeeds before cascaded unregistration runs, allowing a new memory provider to be installed on the physical device while it is unregistering. Reject the open with -ENODEV.
2. In netif_mp_close_rxq() and netif_rxq_cleanup_unlease(), __netif_mp_close_rxq() reconfigures the physical RX queue so that it can continue operating with a regular page_pool when the physical netdev outlives the virtual queue or memory provider. When the physical backing netdev is itself unregistering (!netif_lease_valid(dev)), its queue will not outlive the lease and, matching the non-leased unregistration path (where dev_memory_provider_uninstall() detaches the provider without calling netdev_rx_queue_reconfig()), the physical queue should not be reconfigured. However, unlike non-leased queues, mp_dmabuf_devmem_uninstall() on the physical netdev does not match bound_rxqs (which stores the virtual rxq), leaving phys_rxq->mp_params populated until socket teardown or the virtual leasee's netif_rxq_cleanup_unlease() runs. When !netif_lease_valid(dev), skip __netif_mp_close_rxq() and clear mp_params on the physical queue directly so that a subsequent netif_rxq_cleanup_unlease() or dev_memory_provider_uninstall() sees !mp_ops and does not uninstall a second time.
These issues were discovered during AI code review (Sashiko) of the earlier non-leased devmem unregister series and confirmed by code inspection (not observed in production or reproduced on real hardware). Tested with the nl_netdev.py selftest on netdevsim added in this series.
--- net/core/dev.h | 7 +++++++ net/core/netdev_rx_queue.c | 31 ++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-)
net/core/dev.h +7 -0
@@ -119,6 +119,13 @@ 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); +static inline bool netif_lease_valid (const struct net_device *dev) +{ +netdev_assert_locked (dev); + +return netif_is_alive (dev); +} +void __netif_mp_uninstall_rxq (struct netdev_rx_queue *rxq,const struct pp_memory_provider_params *p);
net/core/netdev_rx_queue.c +26 -5
@@ -270,7 +270,12 @@ int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx, }netdev_lock (dev); - ret =__netif_mp_open_rxq (dev, rxq_idx, p, extack); +if (netif_lease_valid (dev)) { + ret =__netif_mp_open_rxq (dev, rxq_idx, p, extack); + }else { +NL_SET_ERR_MSG (extack,"Device is being unregistered" ); + ret = -ENODEV ; + }netdev_unlock (dev);return ret; }
@@ -309,19 +314,29 @@ static int __netif_mp_close_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) { -int ret; +struct netdev_rx_queue *rxq; +int ret =0 ;if (WARN_ON_ONCE (rxq_idx >= dev->real_num_rx_queues))return -EINVAL ;if (!netif_rxq_is_leased (dev, rxq_idx))return __netif_mp_close_rxq (dev, rxq_idx, old_p); -if (!__netif_get_rx_queue_lease (&dev, &rxq_idx,NETIF_VIRT_TO_PHYS )) { + rxq =__netif_get_rx_queue_lease (&dev, &rxq_idx,NETIF_VIRT_TO_PHYS ); +if (!rxq) {WARN_ON_ONCE (1 );return -EINVAL ; }netdev_lock (dev); - ret =__netif_mp_close_rxq (dev, rxq_idx, old_p); +/* If the physical netdev is already unregistering, the queue will not +* outlive the lease; only clear mp_params so subsequent +* netif_rxq_cleanup_unlease() or dev_memory_provider_uninstall() is a +* no-op. +*/ +if (netif_lease_valid (dev)) + ret =__netif_mp_close_rxq (dev, rxq_idx, old_p); +else +memset (&rxq->mp_params,0 ,sizeof (rxq->mp_params));netdev_unlock (dev);return ret; }
@@ -340,6 +355,9 @@ void __netif_mp_uninstall_rxq(struct netdev_rx_queue *rxq,* the lease redirection. 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. +* If the physical netdev is also unregistering, its queue will not +* outlive the lease, so only clear mp_params without reconfiguring the +* queue. */ void netif_rxq_cleanup_unlease (struct netdev_rx_queue *phys_rxq,struct netdev_rx_queue *virt_rxq)
@@ -350,6 +368,9 @@ void netif_rxq_cleanup_unlease(struct netdev_rx_queue *phys_rxq,if (!p.mp_ops)return ; -__netif_mp_close_rxq (phys_rxq->dev, rxq_idx, &p); +if (netif_lease_valid (phys_rxq->dev)) +__netif_mp_close_rxq (phys_rxq->dev, rxq_idx, &p); +else +memset (&phys_rxq->mp_params,0 ,sizeof (phys_rxq->mp_params));__netif_mp_uninstall_rxq (virt_rxq, &p); }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
[email protected]
AI reviewRe: [PATCH net-next v4 3/7] net: guard memory provider RX queue open and close on unregister
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message has no Fixes: tag, even though the patch changes behaviour in existing lease-redirect code.
--
net: guard memory provider RX queue open and close on unregister
This patch adds a netif_lease_valid() helper that checks netif_is_alive() under netdev_lock(). netif_mp_open_rxq() now uses it to reject opening a memory provider on a leased physical device that is unregistering. netif_mp_close_rxq() and netif_rxq_cleanup_unlease() now use it to clear mp_params instead of reconfiguring a dying physical queue.
Quoted 7 lines
These issues were discovered during AI code review (Sashiko) of the earlier non-leased devmem unregister series and confirmed by code inspection (not observed in production or reproduced on real hardware). Tested with the
nl_netdev.pyselftest on netdevsim added in this series.Suggested-by Stanislav Fomichev <[email protected]>Signed-off-by Mina Almasry <[email protected]>
[Severity: Low]
Should this commit carry a Fixes: tag?
The patch changes how existing lease redirection code behaves in net/core/netdev_rx_queue.c. Both states it closes can be reached on current kernels.
In netif_mp_open_rxq(), a memory provider can be installed on a physical device that is already NETREG_UNREGISTERING, when the call comes through a leasee that is still registered.
In netif_rxq_cleanup_unlease() and netif_mp_close_rxq(), netdev_rx_queue_reconfig() can be called on a dying physical device. For example:
unregister_netdevice_many_notify()
dev_memory_provider_uninstall()
call_netdevice_notifiers(NETDEV_UNREGISTER)
netkit_notifier()
netkit_check_lease_unregister()
unregister_netdevice_many() /* on the leasee */
...
netif_rxq_cleanup_unlease()
__netif_mp_close_rxq() /* reconfigures phys queue */
Here is where this code came from:
5602ad61ebee ("net: Proxy netif_mp_{open,close}_rxq for leased queues")
added the lease-redirected open/close paths and netif_rxq_cleanup_unlease()
581d28606cdd ("net: remove the netif_get_rx_queue_lease_locked() helpers")
added the direct netdev_lock(dev) on the physical device
37314c9dbe95 ("net: Stop leased rxq before uninstalling its memory provider")
gave netif_rxq_cleanup_unlease() its current form, with the mp_params snapshot
Would something like this be appropriate?
Without a Fixes: tag, stable and backport tooling may skip this for kernels that already ship queue leasing.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120045918.1239746-1-almasrymina%40google.com
PATCH v4 4/7 net: devmem: fold net_devmem_lookup_dmabuf() into net_devmem_get_binding()
In reply to Mina Almasrynet_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.
--- net/core/devmem.c | 38 ++++++++++++-------------------------- net/core/devmem.h | 6 ------ 2 files changed, 12 insertions(+), 32 deletions(-)
net/core/devmem.c +12 -26
@@ -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); }
net/core/devmem.h +0 -6
@@ -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)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
[email protected]
AI reviewRe: [PATCH net-next v4 4/7] net: devmem: fold net_devmem_lookup_dmabuf() into net_devmem_get_binding()
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message presents the fold as closing a gap: with two RCU sections, synchronize_net() after clearing NET_DEVMEM_BINDING_LIVE…
--
net: devmem: fold net_devmem_lookup_dmabuf() into net_devmem_get_binding()
This patch folds net_devmem_lookup_dmabuf() into its only caller, net_devmem_get_binding(). The xarray lookup, binding validation, dst_dev check and net_devmem_dmabuf_binding_get() now all run inside one rcu_read_lock() section. Because the reference is taken only after all checks pass, the error path no longer needs a reference put.
Quoted 5 lines
Because the lookup and device checks are split across two RCU read-side critical sections, waiting for
synchronize_net()after clearingNET_DEVMEM_BINDING_LIVEdoes not guarantee that in-flightnet_devmem_get_binding()callers have finished validating the binding and returned.
[Severity: Low]
Does anything in the tree at this commit depend on closing that gap yet?
For a TX binding, net_devmem_unbind_dmabuf() only calls list_del() and percpu_ref_kill() after synchronize_net(). bound_rxqs is empty, and the map_dev clear only runs for DMA_FROM_DEVICE:
net/core/devmem.c:net_devmem_unbind_dmabuf() {
...
synchronize_net();
if (binding->list.next)
list_del(&binding->list);
...
if (binding->direction == DMA_FROM_DEVICE && binding->map_dev &&
!busy) {
net_devmem_dmabuf_binding_unmap(binding);
WRITE_ONCE(binding->map_dev, NULL);
}
percpu_ref_kill(&binding->ref);
}So this path writes none of the fields net_devmem_get_binding() checks (tx_vec, net, uid, map_dev, bind_dev).
net_devmem_dev_unregister() does change TX-checked state. It clears NET_DEVMEM_BINDING_LIVE and bind_dev, unmaps, and then clears map_dev. It never calls synchronize_net(), though, so it does not use the single-section guarantee either:
net/core/devmem.c:net_devmem_dev_unregister() {
...
WRITE_ONCE(binding->bind_dev, NULL);
__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);
xa_lock(&net_devmem_dmabuf_bindings);
WRITE_ONCE(binding->map_dev, NULL);
...
}Before this patch, net_devmem_lookup_dmabuf() already held a percpu_ref through the second RCU section. The late checks therefore could not race with teardown in a way that mattered.
The new guarantee seems to be used first by the later commit "net:
devmem: always populate bind_dev and detach both devices on unregister". That commit adds synchronize_net() to net_devmem_dev_unregister() before it clears map_dev.
Could the commit message say this is preparation for that change and has no functional change by itself? As written, it describes a gap but no failure, and it has no Fixes: tag. That makes it hard for maintainers and stable backporters to know whether it is meant as a fix.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120045918.1239746-1-almasrymina%40google.com
PATCH v4 5/7 net: devmem: always populate bind_dev and detach both devices on unregister
In reply to Mina AlmasryPreviously, 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), first walk the bindings to 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 any binding has binding->map_dev == dev, wait for an RCU grace period via synchronize_net() so no in-flight net_devmem_get_binding() lookups remain, then re-walk the bindings to unmap the dmabuf while map_dev is still valid and clear binding->map_dev.
These issues were discovered during AI code review (Sashiko) of the earlier non-leased devmem unregister series (and subsequent mailing list review) and confirmed by code inspection (not observed in production or reproduced on real hardware). Tested with the nl_netdev.py selftest on netdevsim added in this series.
--- net/core/devmem.c | 91 +++++++++++++++++++++++++----------------- net/core/devmem.h | 8 ++-- net/core/netdev-genl.c | 17 ++++---- 3 files changed, 67 insertions(+), 49 deletions(-)
net/core/devmem.c +54 -37
@@ -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.
@@ -219,22 +220,23 @@ 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, - }; +if (binding->bind_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); + rxq_idx =get_netdev_rx_queue_index (rxq); -if (netif_mp_close_rxq (binding->map_dev, rxq_idx, &mp_params)) - busy = true; - } +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->direction ==DMA_FROM_DEVICE && !busy) +net_devmem_dmabuf_binding_unmap (binding); +WRITE_ONCE (binding->bind_dev,NULL ); }percpu_ref_kill (&binding->ref);
@@ -244,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);
@@ -252,20 +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->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) +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 ; -WRITE_ONCE (binding->bind_dev,NULL ); + } +if (READ_ONCE (binding->map_dev) == dev) {__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 +* NET_DEVMEM_BINDING_LIVE is cleared. +*/ +synchronize_net (); + +xa_lock (&net_devmem_dmabuf_bindings); +xa_for_each (&net_devmem_dmabuf_bindings, xa_idx, binding) { +if (READ_ONCE (binding->map_dev) != dev) +continue ;xa_unlock (&net_devmem_dmabuf_bindings);if (!netif_is_queue_leasee (dev))
@@ -535,8 +551,8 @@ 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, *map_dev;unsigned long xa_idx = dmabuf_id; -struct net_device *dst_dev;struct dst_entry *dst;int err;
@@ -568,9 +584,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; }
@@ -697,8 +714,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 ; }
net/core/devmem.h +5 -3
@@ -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
net/core/netdev-genl.c +8 -9
@@ -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,11 +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, -0 , priv,NETLINK_CB (skb).sk, - info->extack); + binding =net_devmem_bind_dmabuf (map_dev, netdev, dma_dev, +DMA_TO_DEVICE , dmabuf_fd,0 , priv, +NETLINK_CB (skb).sk, info->extack);if (IS_ERR (binding)) { err =PTR_ERR (binding); goto err_unlock_map_dev;
@@ -1475,7 +1474,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); }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
[email protected]
AI reviewRe: [PATCH net-next v4 5/7] net: devmem: always populate bind_dev and detach both devices on unregister
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] Undisclosed user-visible behaviour change for leased TX bindings.
- [Low] The commit message presents this patch as fixing 'issues' found by AI review of the earlier devmem unregister series, 'confirmed by…
--
net: devmem: always populate bind_dev and detach both devices on unregister
This patch always sets binding->bind_dev to the device that bind-rx or bind-tx was called on. Socket teardown, queue close and queue uninstall now use bind_dev, and net_devmem_dev_unregister() handles bind_dev and map_dev separately, calling synchronize_net() before unmapping when map_dev unregisters. net_devmem_get_binding() also now requires map_dev to be non-NULL.
Quoted 4 lines
net/core/devmem.c +0
-0
[ ... ]
Quoted 11 lines
net/core/devmem.c +0
-7@@ -252,20 +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->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)
net/core/devmem.c void net_devmem_dev_unregister(struct net_device *dev) · line 264
Quoted 10 lines
net/core/devmem.c +6
-3+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 ; -WRITE_ONCE (binding->bind_dev,NULL ); + }
[Severity: Medium]
Does this change what happens to leased TX bindings when only the virtual device goes away?
If bind-tx is called on a netkit device, netdev_find_netmem_tx_dev() picks the physical NIC as map_dev, and bind_dev is now the netkit device. When the netkit device is deleted and the physical NIC stays registered, this first walk matches bind_dev == dev and clears NET_DEVMEM_BINDING_LIVE.
After that, net_devmem_get_binding() fails here:
binding = xa_find(&net_devmem_dmabuf_bindings, &xa_idx, dmabuf_id, NET_DEVMEM_BINDING_LIVE);
So every later devmem sendmsg with that dmabuf id returns -EINVAL, even when dst_dev == map_dev.
Before this patch, the code from ede1618064a9 ("net: devmem: detach TX bindings on NETDEV_UNREGISTER") only cleared the virtual device pointer in this case and left the binding live. That commit gave its reason as "only clear binding->vdev so no new TX sends match the virtual device while allowing in-flight TX packets on binding->dev to finish DMA safely".
TX bindings have an empty bound_rxqs, so bind_dev is cleared here anyway. net_devmem_get_binding() would then only match map_dev. Does correctness need the LIVE mark to be cleared in this case?
If the change is intended, could the commit message say that it reverses the earlier design, and why? Right now it only says "first walk the bindings to clear NET_DEVMEM_BINDING_LIVE to stop new lookups".
The same goes for the synchronize_net() added further down for the map_dev case. ede1618064a9 said a grace period was not needed there.
This behaviour is still the same at the end of the series. The selftest added later in the series (devmem_lease_bind_tx_unregister_check with remove_virt_first) only checks the dmabuf attach count. It does not check whether sends through the physical device still work.
[ ... ]
Quoted 4 lines
net/core/devmem.c +0
-0@@ -568,9 +584,10 @@ struct net_devmem_dmabuf_binding *net_devmem_get_binding(struct sock *sk,* net_device. */ dst_dev =dst_dev_rcu (dst);
net/core/devmem.c struct net_devmem_dmabuf_binding *net_devmem_get_binding(struct sock *sk, · line 593
Quoted 10 lines
net/core/devmem.c +4
-3-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; }
[Severity: Low]
The commit message describes this check as:
- 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.
Is that different from the baseline behaviour? Before this patch, net_devmem_dev_unregister() already cleared NET_DEVMEM_BINDING_LIVE for TX bindings when the physical device unregistered. Lookups already failed at xa_find().
With this patch, map_dev is only cleared in net_devmem_dev_unregister(), and only after LIVE is cleared and synchronize_net() returns. So the !map_dev test looks like defence in depth rather than a fix.
The message also says "These issues were discovered during AI code review (Sashiko) ... and confirmed by code inspection". It does not name a concrete failure at baseline.
At this commit, netdev_nl_bind_rx_doit() still passes netdev as both map_dev and bind_dev. The rework seems to mostly prepare for the next patch, "net: devmem: resolve physical map_dev for leased RX bindings", which fixes the actual use-after-free.
This patch rewrites logic from c4466d2c3e77 ("net: devmem: unmap dma_buf synchronously on queue uninstall") and ede1618064a9 ("net: devmem: detach TX bindings on NETDEV_UNREGISTER"). Both of those carry Fixes: tags. Should this patch carry Fixes: tags too, so stable backporters know whether it has to go with them?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120045918.1239746-1-almasrymina%40google.com
Re: [PATCH net-next v4 5/7] net: devmem: always populate bind_dev and detach both devices on unregister
Quoted 89 lines · On Sun, Nov 22, 2026 at 8:59 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] Undisclosed user-visible behaviour change for leased TX bindings.
- [Low] The commit message presents this patch as fixing 'issues' found by AI review of the earlier devmem unregister series, 'confirmed by…--
net: devmem: always populate
bind_devand detach both devices on unregisterThis patch always sets
binding->bind_devto the device that bind-rx or bind-tx was called on. Socket teardown, queue close and queue uninstall now usebind_dev, andnet_devmem_dev_unregister()handlesbind_devandmap_devseparately, callingsynchronize_net()before unmapping whenmap_devunregisters.net_devmem_get_binding()also now requiresmap_devto be non-NULL.Quoted 4 lines
net/core/devmem.c +0
-0[ ... ]
Quoted 11 lines
net/core/devmem.c +0
-7@@ -252,20 +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->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)net/core/devmem.c void net_devmem_dev_unregister(struct net_device *dev) · line 264
Quoted 10 lines
net/core/devmem.c +6
-3+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 ; -WRITE_ONCE (binding->bind_dev,NULL ); + }[Severity: Medium]
Does this change what happens to leased TX bindings when only the virtual device goes away?If bind-tx is called on a netkit device,
netdev_find_netmem_tx_dev()picks the physical NIC asmap_dev, andbind_devis now the netkit device. When the netkit device is deleted and the physical NIC stays registered, this first walk matchesbind_dev== dev and clearsNET_DEVMEM_BINDING_LIVE.After that,
net_devmem_get_binding()fails here:binding = xa_find(&net_devmem_dmabuf_bindings,
&xa_idx,dmabuf_id,
NET_DEVMEM_BINDING_LIVE);So every later devmem sendmsg with that dmabuf id returns
-EINVAL, even whendst_dev==map_dev.Before this patch, the code from
ede1618064a9("net: devmem: detach TX bindings onNETDEV_UNREGISTER") only cleared the virtual device pointer in this case and left the binding live. That commit gave its reason as "only clearbinding->vdevso no new TX sends match the virtual device while allowing in-flight TX packets onbinding->devto finish DMA safely".TX bindings have an empty
bound_rxqs, sobind_devis cleared here anyway.net_devmem_get_binding()would then only matchmap_dev. Does correctness need the LIVE mark to be cleared in this case?If the change is intended, could the commit message say that it reverses the earlier design, and why? Right now it only says "first walk the bindings to clear
NET_DEVMEM_BINDING_LIVEto stop new lookups".
Quoted 8 lines
The same goes for the
synchronize_net()added further down for themap_devcase.ede1618064a9said a grace period was not needed there.This behaviour is still the same at the end of the series. The selftest added later in the series (
devmem_lease_bind_tx_unregister_checkwithremove_virt_first) only checks the dmabuf attach count. It does not check whether sends through the physical device still work.
This seems like some kind of hallucination? AFAICT after this patch we're making the binding not live anymore, so we don't match it against new TX, but in validate_xmit we're checking against map_dev, not bind_dev, so in-flight packets should be fine as long as the map_dev is in tact.
Also, I'm not sure we care so much about what happens exactly to the in-flight TX packets when the binding is being unregistered. The application is probably ending tx anyway.
Also with so many life cycle bugs in the lease case, I'm worried someone can reproduce a consistent way to crash some setups with these issues.
Signature · 2 lines
Thanks, Mina
PATCH v4 6/7 net: devmem: resolve physical map_dev for leased RX bindings
In reply to Mina AlmasryPreviously, netdev_nl_bind_rx_doit() passed netdev as both map_dev and bind_dev to net_devmem_bind_dmabuf(), so leased RX bindings stored the virtual netdev in binding->map_dev without tracking the physical backing netdev at all. When the physical backing netdev unregistered while a leased RX binding was active, net_devmem_dev_unregister() did not match the physical netdev and mp_dmabuf_devmem_uninstall() on the virtual queue skipped unmapping because rxq->lease was set, leaving the dmabuf attachment mapped against the freed physical dma_dev until __net_devmem_dmabuf_binding_free() triggered a use-after-free in dma_unmap_sg_attrs().
Return the physical backing net_device from netdev_nl_get_dma_dev() when the RX queue is leased from another netdev, lock map_dev around net_devmem_bind_dmabuf() when map_dev != netdev (matching netdev_nl_bind_tx_doit()), assert in net_devmem_bind_dmabuf() that both bind_dev and map_dev are locked, and reject binding when !netif_lease_valid(map_dev).
Now that binding->map_dev always points to the physical DMA device for both RX and TX bindings (never a queue leasee), drop the !netif_is_queue_leasee(dev) guard in net_devmem_dev_unregister().
This issue was discovered during AI code review (Sashiko) of the earlier non-leased devmem unregister series and confirmed by code inspection (not observed in production or reproduced on real hardware). Tested with the nl_netdev.py selftest on netdevsim added in this series.
--- net/core/devmem.c | 11 +++++++++-- net/core/netdev-genl.c | 19 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-)
net/core/devmem.c +9 -2
@@ -284,8 +284,7 @@ void net_devmem_dev_unregister(struct net_device *dev)continue ;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 ); }
@@ -354,6 +353,14 @@ net_devmem_bind_dmabuf(struct net_device *map_dev,size_t i;long err; +netdev_assert_locked (bind_dev); +netdev_assert_locked (map_dev); + +if (!netif_lease_valid (map_dev)) { +NL_SET_ERR_MSG (extack,"Device is being unregistered" ); +return ERR_PTR (-ENODEV ); + } +if (!dma_dev) {NL_SET_ERR_MSG (extack,"Device doesn't support DMA" );return ERR_PTR (-EOPNOTSUPP );
net/core/netdev-genl.c +16 -3
@@ -1005,12 +1005,15 @@ static int netdev_nl_read_rxq_bitmap(struct genl_info *info,static struct device *netdev_nl_get_dma_dev (struct net_device *netdev,unsigned long *rxq_bitmap, +struct net_device **map_dev,struct netlink_ext_ack *extack) {struct device *dma_dev =NULL ;u32 rxq_idx, prev_rxq_idx; + *map_dev = netdev;for_each_set_bit (rxq_idx, rxq_bitmap, netdev->real_num_rx_queues) { +struct netdev_rx_queue *rxq;struct device *rxq_dma_dev; rxq_dma_dev = netdev_queue_get_dma_dev(netdev, rxq_idx,
@@ -1023,6 +1026,11 @@ netdev_nl_get_dma_dev(struct net_device *netdev, unsigned long *rxq_bitmap, dma_dev = rxq_dma_dev; prev_rxq_idx = rxq_idx; +if (netif_rxq_is_leased (netdev, rxq_idx) && +netif_is_queue_leasee (netdev)) { + rxq =__netif_get_rx_queue (netdev, rxq_idx); + *map_dev = rxq->lease->dev; + } }return dma_dev;
@@ -1032,9 +1040,9 @@ int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct genl_info *info) {struct net_devmem_dmabuf_binding *binding;unsigned int niov_shift =PAGE_SHIFT ; +struct net_device *netdev, *map_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;
@@ -1104,16 +1112,21 @@ 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); + dma_dev =netdev_nl_get_dma_dev (netdev, rxq_bitmap, &map_dev, + info->extack);if (IS_ERR (dma_dev)) { err =PTR_ERR (dma_dev); goto err_rxq_bitmap; } - binding =net_devmem_bind_dmabuf (netdev, netdev, dma_dev, +if (map_dev != netdev) +netdev_lock (map_dev); + binding =net_devmem_bind_dmabuf (map_dev, netdev, dma_dev,DMA_FROM_DEVICE , dmabuf_fd, niov_shift, priv,NETLINK_CB (skb).sk, info->extack); +if (map_dev != netdev) +netdev_unlock (map_dev);if (IS_ERR (binding)) { err =PTR_ERR (binding); goto err_rxq_bitmap;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
[email protected]
AI reviewRe: [PATCH net-next v4 6/7] net: devmem: resolve physical map_dev for leased RX bindings
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message for "net: devmem: resolve physical map_dev for leased RX bindings" describes a use-after-free and has no Fixes: tag.
--
net: devmem: resolve physical map_dev for leased RX bindings
This commit makes netdev_nl_get_dma_dev() return the physical backing netdev when an RX queue is leased. That netdev is used as map_dev for net_devmem_bind_dmabuf() and is locked around the call. With map_dev now always the physical DMA device, the commit drops the !netif_is_queue_leasee(dev) guard in net_devmem_dev_unregister(), so leased RX bindings get unmapped when the physical netdev unregisters.
Quoted 10 lines
Previously,
netdev_nl_bind_rx_doit()passed netdev as bothmap_devandbind_devtonet_devmem_bind_dmabuf(), so leased RX bindings stored the virtual netdev inbinding->map_devwithout tracking the physical backing netdev at all. When the physical backing netdev unregistered while a leased RX binding was active,net_devmem_dev_unregister()did not match the physical netdev andmp_dmabuf_devmem_uninstall()on the virtual queue skipped unmapping becauserxq->leasewas set, leaving the dmabuf attachment mapped against the freed physicaldma_devuntil__net_devmem_dmabuf_binding_free()triggered a use-after-free indma_unmap_sg_attrs().
[ ... ]
Quoted 2 lines
Suggested-by Stanislav Fomichev <[email protected]>Signed-off-by Mina Almasry <[email protected]>
[Severity: Low]
Should this commit carry Fixes: tags? The use-after-free described above seems to come from two earlier commits together.
The first is 222b5566a02d ("net: Proxy netdev_queue_get_dma_dev for leased queues"). It made devmem bind-rx possible on a virtual leasee such as netkit, but netdev_nl_bind_rx_doit() still passed the virtual netdev as both map_dev and bind_dev.
The second is c4466d2c3e77 ("net: devmem: unmap dma_buf synchronously on queue uninstall"). It added the guard in net_devmem_dev_unregister() that this patch removes:
if (!netif_is_queue_leasee(dev))
net_devmem_dmabuf_binding_unmap(binding);c4466d2c3e77 fixed the non-leased form of the same use-after-free and has two Fixes: tags. Without a tag here, could stable trees that contain both commits keep the leased form unfixed? For example:
The fix also depends on earlier patches in this series that the commit message doesn't mention. netif_lease_valid() comes from "net: guard memory provider RX queue open and close on unregister". bind_dev is only always populated after "net: devmem: always populate bind_dev and detach both devices on unregister".
If this commit were backported alone, wouldn't it fail to build or behave differently? Could the commit message list these dependencies?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120045918.1239746-1-almasrymina%40google.com
PATCH v4 7/7 selftests: net: add leased devmem RX and TX netdev unregister tests
In reply to Mina AlmasryExtend nl_netdev.py to cover netdev unregistration while devmem RX and TX bindings are active on a virtual netkit device leasing an RX queue from netdevsim:
1. devmem_lease_bind_rx_unregister_check: binds an RX dmabuf to a netkit device backed by a leased netdevsim queue and verifies clean teardown both when the virtual netkit device is deleted first (unleasing the queue while netdevsim remains up) and when the underlying netdevsim device is unregistered directly (which uninstalls the physical queue memory provider and cascades unregistration to the leasing netkit).
2. devmem_lease_bind_tx_unregister_check: binds a TX dmabuf to a netkit device backed by a leased netdevsim queue and verifies clean teardown both when the virtual netkit device is deleted first and when the underlying netdevsim device is unregistered directly.
--- tools/testing/selftests/net/nl_netdev.py | 159 ++++++++++++++++++++++- 1 file changed, 155 insertions(+), 4 deletions(-)
tools/testing/selftests/net/nl_netdev.py +155 -4
@@ -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 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,143 @@ 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 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_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 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 +647,9 @@ 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_tx_unregister_check ], args=(nf, )) ksft_exit()
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Quoted 76 lines · Mina Almasry
Support leased RX and TX devmem bindings when either the virtual device (e.g. netkit) or the physical backing
net_deviceis unregistered:1. Pass struct
net_device**dev_ptr tonet_devmem_lock_dev()so callers can lock eitherbinding->map_devorbinding->bind_dev.
2. Renamebinding->devandbinding->vdevtobinding->map_dev(physical DMA device) andbinding->bind_dev(device passed to bind-rx/bind-tx).
3. Introducenetif_lease_valid()and guardnetif_mp_open_rxq(),netif_mp_close_rxq(), andnetif_rxq_cleanup_unlease()against an unregistering physical device reached through a queue lease.
4. Foldnet_devmem_lookup_dmabuf()intonet_devmem_get_binding()so the xarray lookup, validation, device check, and reference acquisition run under a singlercu_read_lock()section.
5. Always populatebind_devon RX and TX bindings and handle teardown and netdev unregistration of bothbind_devandmap_dev, waiting forsynchronize_net()after clearingNET_DEVMEM_BINDING_LIVEbefore unmapping bindings whosemap_devis unregistering.
6. Resolve the physical backingmap_devinnetdev_nl_get_dma_dev()for leased RX bindings, lockmap_devaroundnet_devmem_bind_dmabuf()on bind-rx whenmap_devdiffers from netdev, assert both devices are locked, and checknetif_lease_valid(map_dev).
7. Add kselftests innl_netdev.pycovering leased RX and TX devmem bindings across both virtual-first and physical-first netdev unregistration orders.These issues were discovered during AI code review (Sashiko) of the earlier non-leased devmem unregister series (and subsequent mailing list review) and confirmed by code inspection (not observed in production or reproduced on real hardware). Tested with the
nl_netdev.pyselftest on netdevsim added in this series.v4:
- Rebase onto latest net-next/main.
- Foldnet_devmem_lookup_dmabuf()intonet_devmem_get_binding()under a singlercu_read_lock()(deferringnet_devmem_dmabuf_binding_get()until after all checks pass) and updatenet_devmem_dev_unregister()to use a two-pass loop with a singlesynchronize_net()after clearingNET_DEVMEM_BINDING_LIVEand before unmapping bindings with
map_dev== dev (Paolo).
- Document issue discovery, validation, and testing in commit messages and the cover letter (netdev-bot+sinfo).
- v3: https://lore.kernel.org/netdev/[email protected]/v3:
- Rebase onto latest net-next/main.
- Addnetif_lease_valid()helper and keep lease validity checks in the lease-aware callers (netif_mp_open_rxq(),netif_mp_close_rxq(),netif_rxq_cleanup_unlease(), andnet_devmem_bind_dmabuf()) rather than inside__netif_mp_open_rxq()/__netif_mp_close_rxq(), and document whymp_paramsis cleared directly when !netif_lease_valid(dev) (Stanislav, Sashiko).
- Guard thebound_rxqsloop innet_devmem_unbind_dmabuf()with
if (binding->bind_dev), keep the !netif_is_queue_leasee(dev) guard innet_devmem_dev_unregister()in patch 4/6 until patch 5/6 resolvesbinding->map_devto the physical device so the series is bisectable at every commit, and document the use-after-free indma_unmap_sg_attrs()on physical netdev teardown in patch 5/6 (Sashiko).
- Innl_netdev.py, verify dmabuf and lease on the physical queue after binding, skip whenqueue_createreturns EOPNOTSUPP, probe the physical device on EOPNOTSUPP so a broken lease lookup is not masked as SKIP, and clean upnk_ifnamebetween loop iterations (Sashiko).
- v2: https://lore.kernel.org/netdev/[email protected]/v2:
- Split patch 3/4 from v1 into 3 smaller commits (patches 3/6..5/6).
- Clearrxq->mp_paramsand return 0 in__netif_mp_close_rxq()whenever dev is already unregistering, avoiding queue reconfiguration on an already-shut-downmap_devduring cascaded lease teardown (Sashiko).
- Drop unnecessaryxa_erase()innet_devmem_unbind_dmabuf()and use a localmap_devvariable innet_devmem_get_binding()(Stanislav).
- RestoreASSERT_EXCLUSIVE_WRITER(binding->bind_dev)inmp_dmabuf_devmem_uninstall(), drop redundantbind_dev->reg_statecheck innet_devmem_bind_dmabuf(), and expand commit message rationale (Dragos, Stanislav).
- v1: https://lore.kernel.org/netdev/[email protected]/
Quoted 10 lines
Mina Almasry (7):
net: devmem: passnet_devicepointer tonet_devmem_lock_dev()
net: devmem: rename binding dev and vdev tomap_devandbind_dev
net: guard memory provider RX queue open and close on unregister
net: devmem: foldnet_devmem_lookup_dmabuf()into
net_devmem_get_binding()
net: devmem: always populatebind_devand detach both devices on
unregister
net: devmem: resolve physicalmap_devfor leased RX bindings
selftests: net: add leased devmem RX and TX netdev unregister tests
Quoted 97 lines · Mina Almasry
Support leased RX and TX devmem bindings when either the virtual device (e.g. netkit) or the physical backing
net_deviceis unregistered:1. Pass struct
net_device**dev_ptr tonet_devmem_lock_dev()so callerscan lock either binding->map_dev or binding->bind_dev. 2. Rename binding->dev and binding->vdev to binding->map_dev (physical DMA device) and binding->bind_dev (device passed to bind-rx/bind-tx).3. Introduce
netif_lease_valid()and guardnetif_mp_open_rxq(),netif_mp_close_rxq(), andnetif_rxq_cleanup_unlease()against an unregistering physical device reached through a queue lease.
4. Foldnet_devmem_lookup_dmabuf()intonet_devmem_get_binding()so the xarray lookup, validation, device check, and reference acquisition run under a singlercu_read_lock()section.
5. Always populatebind_devon RX and TX bindings and handle teardown and netdev unregistration of bothbind_devandmap_dev, waiting forsynchronize_net()after clearingNET_DEVMEM_BINDING_LIVEbefore unmapping bindings whosemap_devis unregistering.
6. Resolve the physical backingmap_devinnetdev_nl_get_dma_dev()for leased RX bindings, lockmap_devaroundnet_devmem_bind_dmabuf()on bind-rx whenmap_devdiffers from netdev, assert both devices are locked, and checknetif_lease_valid(map_dev).
7. Add kselftests innl_netdev.pycovering leased RX and TX devmem bindings across both virtual-first and physical-first netdev unregistration orders.These issues were discovered during AI code review (Sashiko) of the earlier non-leased devmem unregister series (and subsequent mailing list review) and confirmed by code inspection (not observed in production or reproduced on real hardware). Tested with the
nl_netdev.pyselftest on netdevsim added in this series.v4:
- Rebase onto latest net-next/main.
- Foldnet_devmem_lookup_dmabuf()intonet_devmem_get_binding()under a singlercu_read_lock()(deferringnet_devmem_dmabuf_binding_get()until after all checks pass) and updatenet_devmem_dev_unregister()to use a two-pass loop with a singlesynchronize_net()after clearingNET_DEVMEM_BINDING_LIVEand before unmapping bindings with
map_dev== dev (Paolo).
- Document issue discovery, validation, and testing in commit messages and the cover letter (netdev-bot+sinfo).
- v3: https://lore.kernel.org/netdev/[email protected]/v3:
- Rebase onto latest net-next/main.
- Addnetif_lease_valid()helper and keep lease validity checks in the lease-aware callers (netif_mp_open_rxq(),netif_mp_close_rxq(),netif_rxq_cleanup_unlease(), andnet_devmem_bind_dmabuf()) rather than inside__netif_mp_open_rxq()/__netif_mp_close_rxq(), and document whymp_paramsis cleared directly when !netif_lease_valid(dev) (Stanislav, Sashiko).
- Guard thebound_rxqsloop innet_devmem_unbind_dmabuf()with
if (binding->bind_dev), keep the !netif_is_queue_leasee(dev) guard innet_devmem_dev_unregister()in patch 4/6 until patch 5/6 resolvesbinding->map_devto the physical device so the series is bisectable at every commit, and document the use-after-free indma_unmap_sg_attrs()on physical netdev teardown in patch 5/6 (Sashiko).
- Innl_netdev.py, verify dmabuf and lease on the physical queue after binding, skip whenqueue_createreturns EOPNOTSUPP, probe the physical device on EOPNOTSUPP so a broken lease lookup is not masked as SKIP, and clean upnk_ifnamebetween loop iterations (Sashiko).
- v2: https://lore.kernel.org/netdev/[email protected]/v2:
- Split patch 3/4 from v1 into 3 smaller commits (patches 3/6..5/6).
- Clearrxq->mp_paramsand return 0 in__netif_mp_close_rxq()whenever dev is already unregistering, avoiding queue reconfiguration on an already-shut-downmap_devduring cascaded lease teardown (Sashiko).
- Drop unnecessaryxa_erase()innet_devmem_unbind_dmabuf()and use a localmap_devvariable innet_devmem_get_binding()(Stanislav).
- RestoreASSERT_EXCLUSIVE_WRITER(binding->bind_dev)inmp_dmabuf_devmem_uninstall(), drop redundantbind_dev->reg_statecheck innet_devmem_bind_dmabuf(), and expand commit message rationale (Dragos, Stanislav).
- v1: https://lore.kernel.org/netdev/[email protected]/Mina Almasry (7):
net: devmem: passnet_devicepointer tonet_devmem_lock_dev()
net: devmem: rename binding dev and vdev tomap_devandbind_dev
net: guard memory provider RX queue open and close on unregister
net: devmem: foldnet_devmem_lookup_dmabuf()into
net_devmem_get_binding()
net: devmem: always populatebind_devand detach both devices on
unregister
net: devmem: resolve physicalmap_devfor leased RX bindings
selftests: net: add leased devmem RX and TX netdev unregister testsnet/core/dev.c | 2 +- net/core/dev.h | 7 + net/core/devmem.c | 178 ++++++++++++----------- net/core/devmem.h | 31 ++-- net/core/netdev-genl.c | 59 +++++--- net/core/netdev_rx_queue.c | 31 +++- tools/testing/selftests/net/nl_netdev.py | 159 +++++++++++++++++++- 7 files changed, 333 insertions(+), 134 deletions(-)
Quoted 1 line
base-commit:
2208ab00f4f213ade91ab775e7f8c4dacdcc856e
The set looks good to me, I also ran a few basic lease queue tests w/ netkit. For the set: