RDMA/umem: Allow pinned dmabuf umem usage
authorGal Pressman <galpress@amazon.com>
Tue, 12 Oct 2021 12:09:02 +0000 (15:09 +0300)
committerJason Gunthorpe <jgg@nvidia.com>
Thu, 28 Oct 2021 11:58:26 +0000 (08:58 -0300)
Introduce ib_umem_dmabuf_get_pinned() which allows the driver to get a
dmabuf umem which is pinned and does not require move_notify callback
implementation.

The returned umem is pinned and DMA mapped like standard cpu umems, and is
released through ib_umem_release() (incl. unpinning and unmapping).

Link: https://lore.kernel.org/r/20211012120903.96933-3-galpress@amazon.com
Signed-off-by: Gal Pressman <galpress@amazon.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
drivers/infiniband/core/umem_dmabuf.c
include/rdma/ib_umem.h

index e824baf4640d1b29c31fc04a420da8f77d10d9cb..372c21cd292729064271a63e19a76ddc67d828eb 100644 (file)
@@ -163,12 +163,63 @@ out_release_dmabuf:
 }
 EXPORT_SYMBOL(ib_umem_dmabuf_get);
 
+static void
+ib_umem_dmabuf_unsupported_move_notify(struct dma_buf_attachment *attach)
+{
+       struct ib_umem_dmabuf *umem_dmabuf = attach->importer_priv;
+
+       ibdev_warn_ratelimited(umem_dmabuf->umem.ibdev,
+                              "Invalidate callback should not be called when memory is pinned\n");
+}
+
+static struct dma_buf_attach_ops ib_umem_dmabuf_attach_pinned_ops = {
+       .allow_peer2peer = true,
+       .move_notify = ib_umem_dmabuf_unsupported_move_notify,
+};
+
+struct ib_umem_dmabuf *ib_umem_dmabuf_get_pinned(struct ib_device *device,
+                                                unsigned long offset,
+                                                size_t size, int fd,
+                                                int access)
+{
+       struct ib_umem_dmabuf *umem_dmabuf;
+       int err;
+
+       umem_dmabuf = ib_umem_dmabuf_get(device, offset, size, fd, access,
+                                        &ib_umem_dmabuf_attach_pinned_ops);
+       if (IS_ERR(umem_dmabuf))
+               return umem_dmabuf;
+
+       dma_resv_lock(umem_dmabuf->attach->dmabuf->resv, NULL);
+       err = dma_buf_pin(umem_dmabuf->attach);
+       if (err)
+               goto err_release;
+       umem_dmabuf->pinned = 1;
+
+       err = ib_umem_dmabuf_map_pages(umem_dmabuf);
+       if (err)
+               goto err_unpin;
+       dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
+
+       return umem_dmabuf;
+
+err_unpin:
+       dma_buf_unpin(umem_dmabuf->attach);
+err_release:
+       dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
+       ib_umem_release(&umem_dmabuf->umem);
+       return ERR_PTR(err);
+}
+EXPORT_SYMBOL(ib_umem_dmabuf_get_pinned);
+
 void ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf)
 {
        struct dma_buf *dmabuf = umem_dmabuf->attach->dmabuf;
 
        dma_resv_lock(dmabuf->resv, NULL);
        ib_umem_dmabuf_unmap_pages(umem_dmabuf);
+       if (umem_dmabuf->pinned)
+               dma_buf_unpin(umem_dmabuf->attach);
        dma_resv_unlock(dmabuf->resv);
 
        dma_buf_detach(dmabuf, umem_dmabuf->attach);
index 5ae9dff74dac8c8b500fb06b457335cc69cf52ab..92a673cd9b4fd7672c2cce88808c16f5b13ff496 100644 (file)
@@ -38,6 +38,7 @@ struct ib_umem_dmabuf {
        unsigned long first_sg_offset;
        unsigned long last_sg_trim;
        void *private;
+       u8 pinned : 1;
 };
 
 static inline struct ib_umem_dmabuf *to_ib_umem_dmabuf(struct ib_umem *umem)
@@ -139,6 +140,10 @@ struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
                                          unsigned long offset, size_t size,
                                          int fd, int access,
                                          const struct dma_buf_attach_ops *ops);
+struct ib_umem_dmabuf *ib_umem_dmabuf_get_pinned(struct ib_device *device,
+                                                unsigned long offset,
+                                                size_t size, int fd,
+                                                int access);
 int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf);
 void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf);
 void ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf);
@@ -179,6 +184,12 @@ struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
 {
        return ERR_PTR(-EOPNOTSUPP);
 }
+static inline struct ib_umem_dmabuf *
+ib_umem_dmabuf_get_pinned(struct ib_device *device, unsigned long offset,
+                         size_t size, int fd, int access)
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
 static inline int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf)
 {
        return -EOPNOTSUPP;