From: Mina Almasry <almasrymina@google.com>
To: netdev@vger.kernel.org
Cc: Mina Almasry <almasrymina@google.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>, Shuah Khan <shuah@kernel.org>,
linux-kselftest@vger.kernel.org,
Weiming Shi <bestswngs@gmail.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Nikolay Aleksandrov <razor@blackwall.org>,
David Wei <dw@davidwei.uk>,
Stanislav Fomichev <sdf.kernel@gmail.com>,
Bobby Eshleman <bobbyeshleman@meta.com>,
Stanislav Fomichev <sdf@fomichev.me>
Subject: [PATCH net-next v4 6/7] net: devmem: resolve physical map_dev for leased RX bindings
Date: Fri, 20 Nov 2026 04:59:17 +0000 [thread overview]
Message-ID: <20261120045918.1239746-7-almasrymina@google.com> (raw)
In-Reply-To: <20261120045918.1239746-1-almasrymina@google.com>
Previously, 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.
Suggested-by: Stanislav Fomichev <sdf@fomichev.me>
Signed-off-by: Mina Almasry <almasrymina@google.com>
---
net/core/devmem.c | 11 +++++++++--
net/core/netdev-genl.c | 19 ++++++++++++++++---
2 files changed, 25 insertions(+), 5 deletions(-)
diff --git a/net/core/devmem.c b/net/core/devmem.c
index 80c3e149cd762..84bb7cebe9a39 100644
--- a/net/core/devmem.c
+++ b/net/core/devmem.c
@@ -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);
diff --git a/net/core/netdev-genl.c b/net/core/netdev-genl.c
index c2b8ec0058a34..74e3f3d89ab4b 100644
--- a/net/core/netdev-genl.c
+++ b/net/core/netdev-genl.c
@@ -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;
--
2.56.1.750.gf70dc682cf-goog
next prev parent reply other threads:[~2026-11-20 5:50 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 4:59 [PATCH net-next v4 0/7] net: devmem: support leased RX and TX queues on netdev unregister Mina Almasry
2026-11-20 4:59 ` [PATCH net-next v4 1/7] net: devmem: pass net_device pointer to net_devmem_lock_dev() Mina Almasry
2026-11-20 4:59 ` [PATCH net-next v4 2/7] net: devmem: rename binding dev and vdev to map_dev and bind_dev Mina Almasry
2026-11-20 4:59 ` [PATCH net-next v4 3/7] net: guard memory provider RX queue open and close on unregister Mina Almasry
2026-11-20 4:59 ` [PATCH net-next v4 4/7] net: devmem: fold net_devmem_lookup_dmabuf() into net_devmem_get_binding() Mina Almasry
2026-11-20 4:59 ` [PATCH net-next v4 5/7] net: devmem: always populate bind_dev and detach both devices on unregister Mina Almasry
2026-11-20 4:59 ` Mina Almasry [this message]
2026-11-20 4:59 ` [PATCH net-next v4 7/7] selftests: net: add leased devmem RX and TX netdev unregister tests Mina Almasry
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261120045918.1239746-7-almasrymina@google.com \
--to=almasrymina@google.com \
--cc=bestswngs@gmail.com \
--cc=bobbyeshleman@meta.com \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=dw@davidwei.uk \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=razor@blackwall.org \
--cc=sdf.kernel@gmail.com \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox