Re: [RFC PATCH net-next v6 05/15] netdev: support binding dma-buf to netdevice

From
Mina Almasry <hidden>
Date
2024-03-05 20:00:22
Also in
bpf, dri-devel, linux-alpha, linux-arch, linux-doc, linux-kselftest, linux-media, linux-mips, lkml, netdev, sparclinux
Reply to
Arnd Bergmann, 2024-03-05
On Tue, Mar 5, 2024 at 1:05 AM Arnd Bergmann [off-list ref] wrote:
On Tue, Mar 5, 2024, at 03:01, Mina Almasry wrote:
quoted
+int netdev_bind_dmabuf(struct net_device *dev, unsigned int dmabuf_fd,
+                    struct netdev_dmabuf_binding **out)
+{
+     struct netdev_dmabuf_binding *binding;
+     static u32 id_alloc_next;
+     struct scatterlist *sg;
+     struct dma_buf *dmabuf;
+     unsigned int sg_idx, i;
+     unsigned long virtual;
+     int err;
+
+     if (!capable(CAP_NET_ADMIN))
+             return -EPERM;
+
+     dmabuf = dma_buf_get(dmabuf_fd);
+     if (IS_ERR_OR_NULL(dmabuf))
+             return -EBADFD;
You should never need to use IS_ERR_OR_NULL() for a properly
defined kernel interface. This one should always return an
error or a valid pointer, so don't check for NULL.
Thanks for clarifying. I will convert to IS_ERR().
quoted
+     binding->attachment = dma_buf_attach(binding->dmabuf, dev->dev.parent);
+     if (IS_ERR(binding->attachment)) {
+             err = PTR_ERR(binding->attachment);
+             goto err_free_id;
+     }
+
+     binding->sgt =
+             dma_buf_map_attachment(binding->attachment, DMA_BIDIRECTIONAL);
+     if (IS_ERR(binding->sgt)) {
+             err = PTR_ERR(binding->sgt);
+             goto err_detach;
+     }
Should there be a check to verify that this buffer
is suitable for network data?

In general, dmabuf allows buffers that are uncached or reside
in MMIO space of another device, but I think this would break
when you get an skb with those buffers and try to parse the
data inside of the kernel on architectures where MMIO space
is not a normal pointer or unaligned access is disallowed on
uncached data.

        Arnd
A key goal of this patch series is that the kernel does not try to
parse the skb frags that reside in the dma-buf for that precise
reason. This is achieved using patch "net: add support for skbs with
unreadable frags" which disables the kernel touching the payload in
these skbs, and "tcp: RX path for devmem TCP" which implements a uapi
where the kernel hands the data in the dmabuf to the userspace via a
cmsg that gives the user a pointer to the data in the dmabuf (offset +
size).

So really AFACT the only restriction here is that the NIC should be
able to DMA into the dmabuf that we're attaching, and dma_buf_attach()
fails in this scenario so we're covered there.

-- 
Thanks,
Mina
Thread (73 messages) 73 messages, 10 authors, last reply 2024-04-08
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help