Switch iommufd to using dma-shared-buf to program the iommu domains.
iommufd is the only importer permitted to use dma_buf_iommu_map(), which
the module restriction on the exports enforces.

Only an exporter handing out a dma_shared_buf can be mapped this way so
IOMMU_IOAS_MAP_FILE refuses any other buffer up front with
dma_buf_iommu_mappable(), the same way the private vfio interface failed
for anything but a vfio DMA-buf. Filling a domain no longer reads
addresses through the pfn reader. Instead the area hands
dma_buf_iommu_map() its domain, IOVA and window into the buffer and the
core has the dma_shared_buf programmed, which also makes exporters with
more than one range work.

Unfilling and revoke go through dma_buf_iommu_unmap(). iommufd
keeps no address at all, only a revoked flag that the exporter's
invalidate_mappings() sets.

Signed-off-by: Alistair Popple <[email protected]>
---
 drivers/iommu/iommufd/io_pagetable.c    |   1 +
 drivers/iommu/iommufd/io_pagetable.h    |  27 +++-
 drivers/iommu/iommufd/iommufd_private.h |   8 --
 drivers/iommu/iommufd/pages.c           | 219 ++++++++++++++------------------
 drivers/iommu/iommufd/selftest.c        |  66 +++++-----
 5 files changed, 158 insertions(+), 163 deletions(-)

diff --git a/drivers/iommu/iommufd/io_pagetable.c 
b/drivers/iommu/iommufd/io_pagetable.c
index 5ac736cf5bb5..fc0c49a9ed76 100644
--- a/drivers/iommu/iommufd/io_pagetable.c
+++ b/drivers/iommu/iommufd/io_pagetable.c
@@ -1145,6 +1145,7 @@ static int iopt_fill_domain(struct io_pagetable *iopt,
                if (!pages)
                        continue;
 
+               guard(iopt_dmabuf_resv)(pages);
                guard(mutex)(&pages->mutex);
                if (iopt_is_dmabuf(pages)) {
                        rc = iopt_dmabuf_track_domain(pages, area, domain);
diff --git a/drivers/iommu/iommufd/io_pagetable.h 
b/drivers/iommu/iommufd/io_pagetable.h
index 27e3e311d395..f8d9523e896d 100644
--- a/drivers/iommu/iommufd/io_pagetable.h
+++ b/drivers/iommu/iommufd/io_pagetable.h
@@ -6,6 +6,7 @@
 #define __IO_PAGETABLE_H
 
 #include <linux/dma-buf.h>
+#include <linux/dma-resv.h>
 #include <linux/interval_tree.h>
 #include <linux/kref.h>
 #include <linux/mutex.h>
@@ -202,7 +203,8 @@ struct iopt_pages_dmabuf_track {
 
 struct iopt_pages_dmabuf {
        struct dma_buf_attachment *attach;
-       struct phys_vec phys;
+       /* Set by the exporter's invalidate_mappings(), never cleared */
+       bool revoked;
        /* Always PAGE_SIZE aligned */
        unsigned long start;
        struct list_head tracker;
@@ -259,10 +261,31 @@ static inline bool iopt_dmabuf_revoked(struct iopt_pages 
*pages)
 {
        lockdep_assert_held(&pages->mutex);
        if (iopt_is_dmabuf(pages))
-               return pages->dmabuf.phys.len == 0;
+               return pages->dmabuf.revoked;
        return false;
 }
 
+/*
+ * Filling a domain from a dmabuf calls into the exporter, which needs the
+ * dma_resv lock. It nests outside pages->mutex, the order iopt_map_dmabuf()
+ * establishes, and the exporter's revoke takes the same two locks in the same
+ * order, so a fill never races a revoke.
+ */
+static inline void iopt_dmabuf_resv_lock(struct iopt_pages *pages)
+{
+       if (iopt_is_dmabuf(pages))
+               dma_resv_lock(pages->dmabuf.attach->dmabuf->resv, NULL);
+}
+
+static inline void iopt_dmabuf_resv_unlock(struct iopt_pages *pages)
+{
+       if (iopt_is_dmabuf(pages))
+               dma_resv_unlock(pages->dmabuf.attach->dmabuf->resv);
+}
+
+DEFINE_GUARD(iopt_dmabuf_resv, struct iopt_pages *, iopt_dmabuf_resv_lock(_T),
+            iopt_dmabuf_resv_unlock(_T))
+
 struct iopt_pages *iopt_alloc_user_pages(void __user *uptr,
                                         unsigned long length, bool writable);
 struct iopt_pages *iopt_alloc_file_pages(struct file *file,
diff --git a/drivers/iommu/iommufd/iommufd_private.h 
b/drivers/iommu/iommufd/iommufd_private.h
index eb2e85b27e42..6ffc0c22ccfc 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -739,8 +739,6 @@ bool iommufd_should_fail(void);
 int __init iommufd_test_init(void);
 void iommufd_test_exit(void);
 bool iommufd_selftest_is_mock_dev(struct device *dev);
-int iommufd_test_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
-                                    struct phys_vec *phys);
 #else
 static inline void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
                                                 unsigned int ioas_id,
@@ -762,11 +760,5 @@ static inline bool iommufd_selftest_is_mock_dev(struct 
device *dev)
 {
        return false;
 }
-static inline int
-iommufd_test_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
-                                struct phys_vec *phys)
-{
-       return -EOPNOTSUPP;
-}
 #endif
 #endif
diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c
index fcdbde007a9a..aeac509e574f 100644
--- a/drivers/iommu/iommufd/pages.c
+++ b/drivers/iommu/iommufd/pages.c
@@ -46,6 +46,7 @@
  * ULONG_MAX so last_index + 1 cannot overflow.
  */
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-resv.h>
 #include <linux/file.h>
 #include <linux/highmem.h>
@@ -55,7 +56,6 @@
 #include <linux/overflow.h>
 #include <linux/slab.h>
 #include <linux/sched/mm.h>
-#include <linux/vfio_pci_core.h>
 
 #include "double_span.h"
 #include "io_pagetable.h"
@@ -250,6 +250,70 @@ static void iopt_area_unmap_domain_range(struct iopt_area 
*area,
                                   start_iova + 1);
 }
 
+/* The dmabuf version of iopt_area_unmap_domain_range() */
+static void iopt_dmabuf_unmap_domain_range(struct iopt_area *area,
+                                          struct iopt_pages *pages,
+                                          struct iommu_domain *domain,
+                                          unsigned long start_index,
+                                          unsigned long last_index)
+{
+       unsigned long start_iova = iopt_area_index_to_iova(area, start_index);
+
+       dma_buf_iommu_unmap(pages->dmabuf.attach, domain, start_iova,
+                           iopt_area_index_to_iova_last(area, last_index) -
+                                   start_iova + 1);
+}
+
+/*
+ * Map the whole area into @domain from the dmabuf. The core does the walk
+ * over the exporter's dma_shared_buf; this only hands it the window of the
+ * buffer the area covers. Requires the dma_resv lock, see
+ * iopt_dmabuf_resv_lock().
+ */
+static int iopt_area_fill_domain_dmabuf(struct iopt_area *area,
+                                       struct iopt_pages *pages,
+                                       struct iommu_domain *domain)
+{
+       unsigned long iova = iopt_area_iova(area);
+
+       dma_resv_assert_held(pages->dmabuf.attach->dmabuf->resv);
+       return dma_buf_iommu_map(pages->dmabuf.attach, domain, iova,
+                                pages->dmabuf.start +
+                                        iopt_area_start_byte(area, iova),
+                                iopt_area_length(area), area->iommu_prot,
+                                GFP_KERNEL_ACCOUNT);
+}
+
+/* Map the whole area into every domain of its iopt, or into none of them */
+static int iopt_area_fill_domains_dmabuf(struct iopt_area *area,
+                                        struct iopt_pages *pages)
+{
+       struct iommu_domain *domain;
+       unsigned long unmap_index;
+       unsigned long index;
+       int rc;
+
+       if (iopt_dmabuf_revoked(pages))
+               return 0;
+
+       xa_for_each(&area->iopt->domains, index, domain) {
+               rc = iopt_area_fill_domain_dmabuf(area, pages, domain);
+               if (rc)
+                       goto err_unmap;
+       }
+       return 0;
+
+err_unmap:
+       xa_for_each(&area->iopt->domains, unmap_index, domain) {
+               if (unmap_index >= index)
+                       break;
+               iopt_dmabuf_unmap_domain_range(area, pages, domain,
+                                              iopt_area_index(area),
+                                              iopt_area_last_index(area));
+       }
+       return rc;
+}
+
 static struct iopt_area *iopt_pages_find_domain_area(struct iopt_pages *pages,
                                                     unsigned long index)
 {
@@ -261,11 +325,6 @@ static struct iopt_area 
*iopt_pages_find_domain_area(struct iopt_pages *pages,
        return container_of(node, struct iopt_area, pages_node);
 }
 
-enum batch_kind {
-       BATCH_CPU_MEMORY = 0,
-       BATCH_MMIO,
-};
-
 /*
  * A simple datastructure to hold a vector of PFNs, optimized for contiguous
  * PFNs. This is used as a temporary holding memory for shuttling pfns from one
@@ -279,7 +338,6 @@ struct pfn_batch {
        unsigned int array_size;
        unsigned int end;
        unsigned int total_pfns;
-       enum batch_kind kind;
 };
 enum { MAX_NPFNS = type_max(typeof(((struct pfn_batch *)0)->npfns[0])) };
 
@@ -289,7 +347,6 @@ static void batch_clear(struct pfn_batch *batch)
        batch->end = 0;
        batch->pfns[0] = 0;
        batch->npfns[0] = 0;
-       batch->kind = 0;
 }
 
 /*
@@ -359,17 +416,10 @@ static void batch_destroy(struct pfn_batch *batch, void 
*backup)
 }
 
 static bool batch_add_pfn_num(struct pfn_batch *batch, unsigned long pfn,
-                             u32 nr, enum batch_kind kind)
+                             u32 nr)
 {
        unsigned int end = batch->end;
 
-       if (batch->kind != kind) {
-               /* One kind per batch */
-               if (batch->end != 0)
-                       return false;
-               batch->kind = kind;
-       }
-
        if (end && pfn == batch->pfns[end - 1] + batch->npfns[end - 1] &&
            nr <= MAX_NPFNS - batch->npfns[end - 1]) {
                batch->npfns[end - 1] += nr;
@@ -396,7 +446,7 @@ static void batch_remove_pfn_num(struct pfn_batch *batch, 
unsigned long nr)
 /* true if the pfn was added, false otherwise */
 static bool batch_add_pfn(struct pfn_batch *batch, unsigned long pfn)
 {
-       return batch_add_pfn_num(batch, pfn, 1, BATCH_CPU_MEMORY);
+       return batch_add_pfn_num(batch, pfn, 1);
 }
 
 /*
@@ -517,11 +567,6 @@ static int batch_to_domain(struct pfn_batch *batch, struct 
iommu_domain *domain,
        unsigned long iova;
        int rc;
 
-       if (batch->kind == BATCH_MMIO) {
-               iommu_prot &= ~IOMMU_CACHE;
-               iommu_prot |= IOMMU_MMIO;
-       }
-
        /* The first index might be a partial page */
        if (start_index == iopt_area_index(area))
                page_offset = area->page_offset;
@@ -675,7 +720,7 @@ static int batch_from_folios(struct pfn_batch *batch, 
struct folio ***folios_p,
                nr = min(nr, npages);
                npages -= nr;
 
-               if (!batch_add_pfn_num(batch, pfn, nr, BATCH_CPU_MEMORY))
+               if (!batch_add_pfn_num(batch, pfn, nr))
                        break;
                if (nr > 1) {
                        rc = folio_add_pins(folio, nr - 1);
@@ -1084,41 +1129,6 @@ static int pfn_reader_user_update_pinned(struct 
pfn_reader_user *user,
        return iopt_pages_update_pinned(pages, npages, inc, user);
 }
 
-struct pfn_reader_dmabuf {
-       struct phys_vec phys;
-       unsigned long start_offset;
-};
-
-static int pfn_reader_dmabuf_init(struct pfn_reader_dmabuf *dmabuf,
-                                 struct iopt_pages *pages)
-{
-       /* Callers must not get here if the dmabuf was already revoked */
-       if (WARN_ON(iopt_dmabuf_revoked(pages)))
-               return -EINVAL;
-
-       dmabuf->phys = pages->dmabuf.phys;
-       dmabuf->start_offset = pages->dmabuf.start;
-       return 0;
-}
-
-static int pfn_reader_fill_dmabuf(struct pfn_reader_dmabuf *dmabuf,
-                                 struct pfn_batch *batch,
-                                 unsigned long start_index,
-                                 unsigned long last_index)
-{
-       unsigned long start = dmabuf->start_offset + start_index * PAGE_SIZE;
-
-       /*
-        * start/last_index and start are all PAGE_SIZE aligned, the batch is
-        * always filled using page size aligned PFNs just like the other types.
-        * If the dmabuf has been sliced on a sub page offset then the common
-        * batch to domain code will adjust it before mapping to the domain.
-        */
-       batch_add_pfn_num(batch, PHYS_PFN(dmabuf->phys.paddr + start),
-                         last_index - start_index + 1, BATCH_MMIO);
-       return 0;
-}
-
 /*
  * PFNs are stored in three places, in order of preference:
  * - The iopt_pages xarray. This is only populated if there is a
@@ -1137,10 +1147,7 @@ struct pfn_reader {
        unsigned long batch_end_index;
        unsigned long last_index;
 
-       union {
-               struct pfn_reader_user user;
-               struct pfn_reader_dmabuf dmabuf;
-       };
+       struct pfn_reader_user user;
 };
 
 static int pfn_reader_update_pinned(struct pfn_reader *pfns)
@@ -1185,15 +1192,6 @@ static int pfn_reader_fill_span(struct pfn_reader *pfns)
            WARN_ON(span->last_used < start_index))
                return -EINVAL;
 
-       /*
-        * Always read a dmabuf from its phys, even where a domain already
-        * maps it: a domain can only report CPU memory PFNs, losing
-        * BATCH_MMIO. last_hole aliases last_used, so this covers any span.
-        */
-       if (iopt_is_dmabuf(pfns->pages))
-               return pfn_reader_fill_dmabuf(&pfns->dmabuf, &pfns->batch,
-                                             start_index, span->last_hole);
-
        if (span->is_used == 1) {
                batch_from_xarray(&pfns->batch, &pfns->pages->pinned_pfns,
                                  start_index, span->last_used);
@@ -1283,14 +1281,15 @@ static int pfn_reader_init(struct pfn_reader *pfns, 
struct iopt_pages *pages,
 
        lockdep_assert_held(&pages->mutex);
 
+       /* dmabufs are mapped by the dma-buf core, not read as PFNs */
+       if (WARN_ON(iopt_is_dmabuf(pages)))
+               return -EINVAL;
+
        pfns->pages = pages;
        pfns->batch_start_index = start_index;
        pfns->batch_end_index = start_index;
        pfns->last_index = last_index;
-       if (iopt_is_dmabuf(pages))
-               pfn_reader_dmabuf_init(&pfns->dmabuf, pages);
-       else
-               pfn_reader_user_init(&pfns->user, pages);
+       pfn_reader_user_init(&pfns->user, pages);
        rc = batch_init(&pfns->batch, last_index - start_index + 1);
        if (rc)
                return rc;
@@ -1311,12 +1310,8 @@ static int pfn_reader_init(struct pfn_reader *pfns, 
struct iopt_pages *pages,
 static void pfn_reader_release_pins(struct pfn_reader *pfns)
 {
        struct iopt_pages *pages = pfns->pages;
-       struct pfn_reader_user *user;
-
-       if (iopt_is_dmabuf(pages))
-               return;
+       struct pfn_reader_user *user = &pfns->user;
 
-       user = &pfns->user;
        if (user->upages_end > pfns->batch_end_index) {
                /* Any pages not transferred to the batch are just unpinned */
 
@@ -1346,8 +1341,7 @@ static void pfn_reader_destroy(struct pfn_reader *pfns)
        struct iopt_pages *pages = pfns->pages;
 
        pfn_reader_release_pins(pfns);
-       if (!iopt_is_dmabuf(pfns->pages))
-               pfn_reader_user_destroy(&pfns->user, pfns->pages);
+       pfn_reader_user_destroy(&pfns->user, pfns->pages);
        batch_destroy(&pfns->batch, NULL);
        WARN_ON(pages->last_npinned != pages->npinned);
 }
@@ -1455,11 +1449,11 @@ static void iopt_revoke_notify(struct 
dma_buf_attachment *attach)
        list_for_each_entry(track, &pages->dmabuf.tracker, elm) {
                struct iopt_area *area = track->area;
 
-               iopt_area_unmap_domain_range(area, track->domain,
-                                            iopt_area_index(area),
-                                            iopt_area_last_index(area));
+               iopt_dmabuf_unmap_domain_range(area, pages, track->domain,
+                                              iopt_area_index(area),
+                                              iopt_area_last_index(area));
        }
-       pages->dmabuf.phys.len = 0;
+       pages->dmabuf.revoked = true;
 }
 
 static const struct dma_buf_attach_ops iopt_dmabuf_attach_revoke_ops = {
@@ -1467,38 +1461,16 @@ static const struct dma_buf_attach_ops 
iopt_dmabuf_attach_revoke_ops = {
        .invalidate_mappings = iopt_revoke_notify,
 };
 
-/*
- * iommufd and vfio have a circular dependency. Future work for a phys
- * based private interconnect will remove this.
- */
-static int
-sym_vfio_pci_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
-                                struct phys_vec *phys)
-{
-       typeof(&vfio_pci_dma_buf_iommufd_map) fn;
-       int rc;
-
-       rc = iommufd_test_dma_buf_iommufd_map(attachment, phys);
-       if (rc != -EOPNOTSUPP)
-               return rc;
-
-       if (!IS_ENABLED(CONFIG_VFIO_PCI_DMABUF))
-               return -EOPNOTSUPP;
-
-       fn = symbol_get(vfio_pci_dma_buf_iommufd_map);
-       if (!fn)
-               return -EOPNOTSUPP;
-       rc = fn(attachment, phys);
-       symbol_put(vfio_pci_dma_buf_iommufd_map);
-       return rc;
-}
-
 static int iopt_map_dmabuf(struct iommufd_ctx *ictx, struct iopt_pages *pages,
                           struct dma_buf *dmabuf)
 {
        struct dma_buf_attachment *attach;
        int rc;
 
+       /* Fail at import rather than when a domain is first filled */
+       if (!dma_buf_iommu_mappable(dmabuf))
+               return -EOPNOTSUPP;
+
        attach = dma_buf_dynamic_attach(dmabuf, iommufd_global_device(),
                                        &iopt_dmabuf_attach_revoke_ops, pages);
        if (IS_ERR(attach))
@@ -1518,18 +1490,12 @@ static int iopt_map_dmabuf(struct iommufd_ctx *ictx, 
struct iopt_pages *pages,
        if (rc)
                goto err_detach;
 
-       rc = sym_vfio_pci_dma_buf_iommufd_map(attach, &pages->dmabuf.phys);
-       if (rc)
-               goto err_unpin;
-
        dma_resv_unlock(dmabuf->resv);
 
        /* On success iopt_release_pages() will detach and put the dmabuf. */
        pages->dmabuf.attach = attach;
        return 0;
 
-err_unpin:
-       dma_buf_unpin(attach);
 err_detach:
        dma_resv_unlock(dmabuf->resv);
        dma_buf_detach(dmabuf, attach);
@@ -1764,8 +1730,8 @@ static void __iopt_area_unfill_domain(struct iopt_area 
*area,
        if (iopt_is_dmabuf(pages)) {
                if (WARN_ON(iopt_dmabuf_revoked(pages)))
                        return;
-               iopt_area_unmap_domain_range(area, domain, start_index,
-                                            last_index);
+               iopt_dmabuf_unmap_domain_range(area, pages, domain, start_index,
+                                              last_index);
                return;
        }
 
@@ -1870,6 +1836,9 @@ int iopt_area_fill_domain(struct iopt_area *area, struct 
iommu_domain *domain)
        if (iopt_dmabuf_revoked(area->pages))
                return 0;
 
+       if (iopt_is_dmabuf(area->pages))
+               return iopt_area_fill_domain_dmabuf(area, area->pages, domain);
+
        rc = pfn_reader_first(&pfns, area->pages, iopt_area_index(area),
                              iopt_area_last_index(area));
        if (rc)
@@ -1928,14 +1897,16 @@ int iopt_area_fill_domains(struct iopt_area *area, 
struct iopt_pages *pages)
        if (xa_empty(&area->iopt->domains))
                return 0;
 
+       iopt_dmabuf_resv_lock(pages);
        mutex_lock(&pages->mutex);
        if (iopt_is_dmabuf(pages)) {
                rc = iopt_dmabuf_track_all_domains(area, pages);
                if (rc)
                        goto out_unlock;
-       }
-
-       if (!iopt_dmabuf_revoked(pages)) {
+               rc = iopt_area_fill_domains_dmabuf(area, pages);
+               if (rc)
+                       goto out_untrack;
+       } else {
                rc = pfn_reader_first(&pfns, pages, iopt_area_index(area),
                                      iopt_area_last_index(area));
                if (rc)
@@ -1966,6 +1937,7 @@ int iopt_area_fill_domains(struct iopt_area *area, struct 
iopt_pages *pages)
        area->storage_domain = xa_load(&area->iopt->domains, 0);
        interval_tree_insert(&area->pages_node, &pages->domains_itree);
        mutex_unlock(&pages->mutex);
+       iopt_dmabuf_resv_unlock(pages);
        return 0;
 
 out_unmap:
@@ -1999,6 +1971,7 @@ int iopt_area_fill_domains(struct iopt_area *area, struct 
iopt_pages *pages)
                iopt_dmabuf_untrack_all_domains(area, pages);
 out_unlock:
        mutex_unlock(&pages->mutex);
+       iopt_dmabuf_resv_unlock(pages);
        return rc;
 }
 
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index 25387969ab75..f0ba2624f7b3 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -1959,9 +1959,14 @@ struct iommufd_test_dma_buf {
        bool revoked;
 };
 
+/* Like vfio, a revoked buffer refuses new importers at attach */
 static int iommufd_test_dma_buf_attach(struct dma_buf *dmabuf,
                                       struct dma_buf_attachment *attachment)
 {
+       struct iommufd_test_dma_buf *priv = dmabuf->priv;
+
+       if (priv->revoked)
+               return -ENODEV;
        return 0;
 }
 
@@ -1970,19 +1975,6 @@ static void iommufd_test_dma_buf_detach(struct dma_buf 
*dmabuf,
 {
 }
 
-static struct sg_table *
-iommufd_test_dma_buf_map(struct dma_buf_attachment *attachment,
-                        enum dma_data_direction dir)
-{
-       return ERR_PTR(-EOPNOTSUPP);
-}
-
-static void iommufd_test_dma_buf_unmap(struct dma_buf_attachment *attachment,
-                                      struct sg_table *sgt,
-                                      enum dma_data_direction dir)
-{
-}
-
 static void iommufd_test_dma_buf_release(struct dma_buf *dmabuf)
 {
        struct iommufd_test_dma_buf *priv = dmabuf->priv;
@@ -1991,32 +1983,46 @@ static void iommufd_test_dma_buf_release(struct dma_buf 
*dmabuf)
        kfree(priv);
 }
 
-static const struct dma_buf_ops iommufd_test_dmabuf_ops = {
-       .attach = iommufd_test_dma_buf_attach,
-       .detach = iommufd_test_dma_buf_detach,
-       .map_dma_buf = iommufd_test_dma_buf_map,
-       .release = iommufd_test_dma_buf_release,
-       .unmap_dma_buf = iommufd_test_dma_buf_unmap,
-};
-
-int iommufd_test_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
-                                    struct phys_vec *phys)
+static struct dma_shared_buf *
+iommufd_test_dma_buf_map_dma_shared_buf(struct dma_buf_attachment *attachment)
 {
        struct iommufd_test_dma_buf *priv = attachment->dmabuf->priv;
+       struct dma_shared_buf *shared_buf;
 
        dma_resv_assert_held(attachment->dmabuf->resv);
 
-       if (attachment->dmabuf->ops != &iommufd_test_dmabuf_ops)
-               return -EOPNOTSUPP;
-
        if (priv->revoked)
-               return -ENODEV;
+               return ERR_PTR(-ENODEV);
 
-       phys->paddr = virt_to_phys(priv->memory);
-       phys->len = priv->length;
-       return 0;
+       shared_buf = kvmalloc(struct_size(shared_buf, ranges, 1), GFP_KERNEL);
+       if (!shared_buf)
+               return ERR_PTR(-ENOMEM);
+
+       /* System memory: no provider */
+       shared_buf->provider = NULL;
+       shared_buf->attrs = 0;
+       shared_buf->size = priv->length;
+       shared_buf->nr_ranges = 1;
+       shared_buf->ranges[0].paddr = virt_to_phys(priv->memory);
+       shared_buf->ranges[0].len = priv->length;
+       return shared_buf;
+}
+
+static void
+iommufd_test_dma_buf_unmap_dma_shared_buf(struct dma_buf_attachment *attach,
+                                         struct dma_shared_buf *shared_buf)
+{
+       kvfree(shared_buf);
 }
 
+static const struct dma_buf_ops iommufd_test_dmabuf_ops = {
+       .attach = iommufd_test_dma_buf_attach,
+       .detach = iommufd_test_dma_buf_detach,
+       .release = iommufd_test_dma_buf_release,
+       .map_dma_shared_buf = iommufd_test_dma_buf_map_dma_shared_buf,
+       .unmap_dma_shared_buf = iommufd_test_dma_buf_unmap_dma_shared_buf,
+};
+
 static int iommufd_test_dmabuf_get(struct iommufd_ucmd *ucmd,
                                   unsigned int open_flags,
                                   size_t len)

-- 
2.54.0

Reply via email to