From: Bobby Eshleman <[email protected]>

When a netkit virtual device leases queues from a physical NIC, devmem
TX bindings created on the netkit device must still result in the dmabuf
being mapped for dma by the physical device. This patch accomplishes
this by teaching the bind handler to search for the underlying
DMA-capable device by looking it up via leased rx queues. The function
netdev_find_netmem_tx_dev(), used for finding the underlying DMA-capable
device, can be extended to support other non-netkit NETMEM_TX_NO_DMA
devices in the future if needed.

Additionally, this patch extends validate_xmit_unreadable_skb() to
support the netkit case, where the skb is validated twice: once on the
netkit guest device and again on the physical NIC after BPF redirect or
ip forwarding.

Signed-off-by: Bobby Eshleman <[email protected]>
---
Changes in v2:
- In validate_xmit_unreadable_skb() to check netmem_tx mode before
  inspecting frags (Jakub)
- Lock bind_dev around netdev_queue_get_dma_dev() when bind_dev !=
  netdev to fix lockdep (Sashiko)
---
 net/core/dev.c         | 21 ++++++++++++-------
 net/core/devmem.c      |  6 ++++--
 net/core/devmem.h      |  9 ++++++--
 net/core/netdev-genl.c | 57 +++++++++++++++++++++++++++++++++++++++++++++-----
 4 files changed, 77 insertions(+), 16 deletions(-)

diff --git a/net/core/dev.c b/net/core/dev.c
index 06c195906231..74eb4eb170cd 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -3990,23 +3990,30 @@ static struct sk_buff *sk_validate_xmit_skb(struct 
sk_buff *skb,
 static struct sk_buff *validate_xmit_unreadable_skb(struct sk_buff *skb,
                                                    struct net_device *dev)
 {
+       struct net_devmem_dmabuf_binding *binding;
        struct skb_shared_info *shinfo;
        struct net_iov *niov;
 
        if (likely(skb_frags_readable(skb)))
                goto out;
 
-       if (!dev->netmem_tx)
+       if (dev->netmem_tx == NETMEM_TX_NONE)
                goto out_free;
 
+       if (dev->netmem_tx == NETMEM_TX_NO_DMA)
+               goto out;
+
        shinfo = skb_shinfo(skb);
+       if (shinfo->nr_frags == 0)
+               goto out;
 
-       if (shinfo->nr_frags > 0) {
-               niov = netmem_to_net_iov(skb_frag_netmem(&shinfo->frags[0]));
-               if (net_is_devmem_iov(niov) &&
-                   READ_ONCE(net_devmem_iov_binding(niov)->dev) != dev)
-                       goto out_free;
-       }
+       niov = netmem_to_net_iov(skb_frag_netmem(&shinfo->frags[0]));
+       if (!net_is_devmem_iov(niov))
+               goto out_free;
+
+       binding = net_devmem_iov_binding(niov);
+       if (READ_ONCE(binding->dev) != dev)
+               goto out_free;
 
 out:
        return skb;
diff --git a/net/core/devmem.c b/net/core/devmem.c
index cde4c89bc146..644c286b778f 100644
--- a/net/core/devmem.c
+++ b/net/core/devmem.c
@@ -181,7 +181,7 @@ int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, 
u32 rxq_idx,
 }
 
 struct net_devmem_dmabuf_binding *
-net_devmem_bind_dmabuf(struct net_device *dev,
+net_devmem_bind_dmabuf(struct net_device *dev, struct net_device *vdev,
                       struct device *dma_dev,
                       enum dma_data_direction direction,
                       unsigned int dmabuf_fd, struct netdev_nl_sock *priv,
@@ -212,6 +212,7 @@ net_devmem_bind_dmabuf(struct net_device *dev,
        }
 
        binding->dev = dev;
+       binding->vdev = vdev;
        xa_init_flags(&binding->bound_rxqs, XA_FLAGS_ALLOC);
 
        err = percpu_ref_init(&binding->ref,
@@ -397,7 +398,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))) {
+           unlikely(dst_dev != READ_ONCE(binding->dev) &&
+                    dst_dev != READ_ONCE(binding->vdev))) {
                err = -ENODEV;
                goto out_unlock;
        }
diff --git a/net/core/devmem.h b/net/core/devmem.h
index 1c5c18581fcb..f399632b3c4b 100644
--- a/net/core/devmem.h
+++ b/net/core/devmem.h
@@ -19,7 +19,12 @@ struct net_devmem_dmabuf_binding {
        struct dma_buf *dmabuf;
        struct dma_buf_attachment *attachment;
        struct sg_table *sgt;
+       /* Physical NIC that does the actual DMA for this binding. */
        struct net_device *dev;
+       /* Virtual device (e.g. netkit) the user called bind-tx on. Must be
+        * NETMEM_TX_NO_DMA.
+        */
+       struct net_device *vdev;
        struct gen_pool *chunk_pool;
        /* Protect dev */
        struct mutex lock;
@@ -84,7 +89,7 @@ struct dmabuf_genpool_chunk_owner {
 
 void __net_devmem_dmabuf_binding_free(struct work_struct *wq);
 struct net_devmem_dmabuf_binding *
-net_devmem_bind_dmabuf(struct net_device *dev,
+net_devmem_bind_dmabuf(struct net_device *dev, struct net_device *vdev,
                       struct device *dma_dev,
                       enum dma_data_direction direction,
                       unsigned int dmabuf_fd, struct netdev_nl_sock *priv,
@@ -165,7 +170,7 @@ static inline void net_devmem_put_net_iov(struct net_iov 
*niov)
 }
 
 static inline struct net_devmem_dmabuf_binding *
-net_devmem_bind_dmabuf(struct net_device *dev,
+net_devmem_bind_dmabuf(struct net_device *dev, struct net_device *vdev,
                       struct device *dma_dev,
                       enum dma_data_direction direction,
                       unsigned int dmabuf_fd,
diff --git a/net/core/netdev-genl.c b/net/core/netdev-genl.c
index b8f6076d8007..0e296c3bb677 100644
--- a/net/core/netdev-genl.c
+++ b/net/core/netdev-genl.c
@@ -1077,7 +1077,7 @@ int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct 
genl_info *info)
                goto err_rxq_bitmap;
        }
 
-       binding = net_devmem_bind_dmabuf(netdev, dma_dev, DMA_FROM_DEVICE,
+       binding = net_devmem_bind_dmabuf(netdev, NULL, dma_dev, DMA_FROM_DEVICE,
                                         dmabuf_fd, priv, info->extack);
        if (IS_ERR(binding)) {
                err = PTR_ERR(binding);
@@ -1119,9 +1119,42 @@ int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct 
genl_info *info)
        return err;
 }
 
+/* Find the DMA-capable device for netmem TX binding.
+ * For NETMEM_TX_DMA devices, returns the device itself.
+ * For NETMEM_TX_NO_DMA devices (e.g. netkit), walks leased queues
+ * to find the underlying physical device.
+ * Returns NULL if no suitable device is found.
+ */
+static struct net_device *netdev_find_netmem_tx_dev(struct net_device *dev)
+{
+       struct netdev_rx_queue *lease_rxq;
+       struct net_device *phys_dev;
+       int i;
+
+       if (dev->netmem_tx == NETMEM_TX_DMA)
+               return dev;
+
+       if (dev->netmem_tx != NETMEM_TX_NO_DMA)
+               return NULL;
+
+       for (i = 0; i < dev->real_num_rx_queues; i++) {
+               lease_rxq = READ_ONCE(__netif_get_rx_queue(dev, i)->lease);
+               if (!lease_rxq)
+                       continue;
+
+               phys_dev = lease_rxq->dev;
+               if (netif_device_present(phys_dev) &&
+                   phys_dev->netmem_tx == NETMEM_TX_DMA)
+                       return phys_dev;
+       }
+
+       return NULL;
+}
+
 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 netdev_nl_sock *priv;
        struct net_device *netdev;
        struct device *dma_dev;
@@ -1164,16 +1197,30 @@ int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct 
genl_info *info)
                goto err_unlock_netdev;
        }
 
-       if (!netdev->netmem_tx) {
+       if (netdev->netmem_tx == NETMEM_TX_NONE) {
                err = -EOPNOTSUPP;
                NL_SET_ERR_MSG(info->extack,
                               "Driver does not support netmem TX");
                goto err_unlock_netdev;
        }
 
-       dma_dev = netdev_queue_get_dma_dev(netdev, 0, NETDEV_QUEUE_TYPE_TX);
-       binding = net_devmem_bind_dmabuf(netdev, dma_dev, DMA_TO_DEVICE,
-                                        dmabuf_fd, priv, info->extack);
+       bind_dev = netdev_find_netmem_tx_dev(netdev);
+       if (!bind_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);
+       dma_dev = netdev_queue_get_dma_dev(bind_dev, 0, NETDEV_QUEUE_TYPE_TX);
+       if (bind_dev != netdev)
+               netdev_unlock(bind_dev);
+       binding = net_devmem_bind_dmabuf(bind_dev,
+                                        bind_dev != netdev ? netdev : NULL,
+                                        dma_dev, DMA_TO_DEVICE, dmabuf_fd,
+                                        priv, info->extack);
        if (IS_ERR(binding)) {
                err = PTR_ERR(binding);
                goto err_unlock_netdev;

-- 
2.52.0


Reply via email to