dma_shared_buf_map() returns the DMA addresses a dma_shared_buf was
mapped to as a struct dma_addr_list. However existing DMA-buf importers
expect an sg_table from dma_buf_map_sgt(), so the core has to be able to
present the addresses as one.

Add dma_addr_list_to_sgt() to build an sg_table over a struct
dma_addr_list and dma_addr_list_free_sgt() to free it. The table carries
DMA addresses only. It has no CPU side, so nothing handed the table can
reach a struct page, and orig_nents is zero so that this can be
detected, the same as the tables dma_buf_phys_vec_to_sgt() builds today.

The table is only a view of the mapping. The addresses stay mapped until
dma_shared_buf_unmap() is called, which must not happen while the
table is in use.

Signed-off-by: Alistair Popple <[email protected]>
---
 drivers/dma-buf/dma-shared-buf.c | 86 ++++++++++++++++++++++++++++++++++++++++
 include/linux/dma-shared-buf.h   |  4 ++
 2 files changed, 90 insertions(+)

diff --git a/drivers/dma-buf/dma-shared-buf.c b/drivers/dma-buf/dma-shared-buf.c
index d5d179b771f2..c346d4c05de8 100644
--- a/drivers/dma-buf/dma-shared-buf.c
+++ b/drivers/dma-buf/dma-shared-buf.c
@@ -4,9 +4,11 @@
  * be mapped for whichever device needs to access it.
  */
 #include <linux/dma-shared-buf.h>
+#include <linux/align.h>
 #include <linux/iommu.h>
 #include <linux/overflow.h>
 #include <linux/pci-p2pdma.h>
+#include <linux/scatterlist.h>
 #include <linux/slab.h>
 
 /**
@@ -207,3 +209,87 @@ int dma_shared_buf_iommu_map(const struct dma_shared_buf 
*shared_buf,
                iommu_unmap(domain, start_iova, iova - start_iova);
        return ret;
 }
+
+#define MAX_SG_ENT_SZ ALIGN_DOWN(UINT_MAX, PAGE_SIZE)
+
+static struct scatterlist *fill_sg_entry(struct scatterlist *sgl, size_t 
length,
+                                        dma_addr_t addr)
+{
+       size_t len;
+
+       while (length) {
+               len = min(length, MAX_SG_ENT_SZ);
+               length -= len;
+               /*
+                * The scatterlist has no CPU list, only the DMA list. Always
+                * set the page related values to NULL to ensure nothing
+                * handed the table can use them. The phys_addr based DMA API
+                * does not require the CPU list for mapping or unmapping.
+                */
+               sg_set_page(sgl, NULL, 0, 0);
+               sg_dma_address(sgl) = addr;
+               sg_dma_len(sgl) = len;
+               addr += len;
+               /* Unconditionally advance. On last segment, this becomes NULL 
*/
+               sgl = sg_next(sgl);
+       }
+
+       return sgl;
+}
+
+/**
+ * dma_addr_list_to_sgt - Build an sg_table over the DMA addresses of a mapped
+ *                        dma_shared_buf
+ * @sgt: The table to build
+ * @addrs: The DMA addresses dma_shared_buf_map() returned
+ *
+ * For a caller which has to present the mapping as an sg_table. The table
+ * carries the DMA side only. The CPU side is left empty so that nothing handed
+ * the table can reach a struct page. The table does not own the mapping:
+ * @addrs must stay mapped while the table is in use and is still released with
+ * dma_shared_buf_unmap().
+ *
+ * Returns: 0 on success, in which case the table is freed with
+ * dma_addr_list_free_sgt(), or a negative error value.
+ */
+int dma_addr_list_to_sgt(struct sg_table *sgt,
+                        const struct dma_addr_list *addrs)
+{
+       struct scatterlist *sgl;
+       unsigned int nents = 0;
+       unsigned int i;
+       int ret;
+
+       for (i = 0; i < addrs->nr_ranges; i++) {
+               unsigned int added = DIV_ROUND_UP(addrs->ranges[i].len,
+                                                 MAX_SG_ENT_SZ);
+
+               if (check_add_overflow(nents, added, &nents))
+                       return -E2BIG;
+       }
+
+       ret = sg_alloc_table(sgt, nents, GFP_KERNEL | __GFP_ZERO);
+       if (ret)
+               return ret;
+
+       sgl = sgt->sgl;
+       for (i = 0; i < addrs->nr_ranges; i++)
+               sgl = fill_sg_entry(sgl, addrs->ranges[i].len,
+                                   addrs->ranges[i].addr);
+       /* fill_sg_entry() hands back NULL once the last entry is filled */
+       WARN_ON_ONCE(sgl);
+
+       sgt->orig_nents = 0;
+       return 0;
+}
+
+/**
+ * dma_addr_list_free_sgt - Free a table built by dma_addr_list_to_sgt()
+ * @sgt: The table
+ */
+void dma_addr_list_free_sgt(struct sg_table *sgt)
+{
+       /* sg_free_table() frees nothing for orig_nents == 0, so restore it */
+       sgt->orig_nents = sgt->nents;
+       sg_free_table(sgt);
+}
diff --git a/include/linux/dma-shared-buf.h b/include/linux/dma-shared-buf.h
index 2a3f6297d23f..62e614b50771 100644
--- a/include/linux/dma-shared-buf.h
+++ b/include/linux/dma-shared-buf.h
@@ -11,6 +11,7 @@
 
 struct iommu_domain;
 struct p2pdma_provider;
+struct sg_table;
 
 /**
  * struct dma_shared_buf - Memory to be DMA mapped, as described by its owner
@@ -77,5 +78,8 @@ void dma_shared_buf_unmap(struct device *dev, struct 
dma_addr_list *addrs,
 int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
                             struct iommu_domain *domain, unsigned long iova,
                             size_t offset, size_t size, int prot, gfp_t gfp);
+int dma_addr_list_to_sgt(struct sg_table *sgt,
+                        const struct dma_addr_list *addrs);
+void dma_addr_list_free_sgt(struct sg_table *sgt);
 
 #endif /* _LINUX_DMA_SHARED_BUF_H */

-- 
2.54.0

Reply via email to