lib/scatterlist: Provide a DMA page iterator
authorJason Gunthorpe <jgg@mellanox.com>
Fri, 4 Jan 2019 18:40:21 +0000 (11:40 -0700)
committerJason Gunthorpe <jgg@mellanox.com>
Mon, 11 Feb 2019 22:02:33 +0000 (15:02 -0700)
Commit 2db76d7c3c6d ("lib/scatterlist: sg_page_iter: support sg lists w/o
backing pages") introduced the sg_page_iter_dma_address() function without
providing a way to use it in the general case. If the sg_dma_len() is not
equal to the sg length callers cannot safely use the
for_each_sg_page/sg_page_iter_dma_address combination.

Resolve this API mistake by providing a DMA specific iterator,
for_each_sg_dma_page(), that uses the right length so
sg_page_iter_dma_address() works as expected with all sglists.

A new iterator type is introduced to provide compile-time safety against
wrongly mixing accessors and iterators.

Acked-by: Christoph Hellwig <hch@lst.de> (for scatterlist)
Acked-by: Thomas Hellstrom <thellstrom@vmware.com>
Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com> (ipu3-cio2)
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
.clang-format
drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
drivers/media/pci/intel/ipu3/ipu3-cio2.c
include/linux/scatterlist.h
lib/scatterlist.c

index bc2ffb2a0b5366b2d31bc533486ec30e33f3d057..335ce29ab8132c35ba6da2bbe8e1092760cc904a 100644 (file)
@@ -240,6 +240,7 @@ ForEachMacros:
   - 'for_each_set_bit'
   - 'for_each_set_bit_from'
   - 'for_each_sg'
+  - 'for_each_sg_dma_page'
   - 'for_each_sg_page'
   - 'for_each_sibling_event'
   - '__for_each_thread'
index 31786b200afc470d73d4f661a4e9358959d686f8..a3357ff7540d21a9e5ebb68def7027ffaf60866c 100644 (file)
@@ -311,7 +311,13 @@ static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
 
 static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
 {
-       return sg_page_iter_dma_address(&viter->iter);
+       /*
+        * FIXME: This driver wrongly mixes DMA and CPU SG list iteration and
+        * needs revision. See
+        * https://lore.kernel.org/lkml/20190104223531.GA1705@ziepe.ca/
+        */
+       return sg_page_iter_dma_address(
+               container_of(&viter->iter, struct sg_dma_page_iter, base));
 }
 
 
index cdb79ae2d8dc726f523f03f786783daec9bb1378..9fbfbda74171329d6851bf12b22493bc94b013bf 100644 (file)
@@ -846,7 +846,7 @@ static int cio2_vb2_buf_init(struct vb2_buffer *vb)
        unsigned int pages = DIV_ROUND_UP(vb->planes[0].length, CIO2_PAGE_SIZE);
        unsigned int lops = DIV_ROUND_UP(pages + 1, entries_per_page);
        struct sg_table *sg;
-       struct sg_page_iter sg_iter;
+       struct sg_dma_page_iter sg_iter;
        int i, j;
 
        if (lops <= 0 || lops > CIO2_MAX_LOPS) {
@@ -873,7 +873,7 @@ static int cio2_vb2_buf_init(struct vb2_buffer *vb)
                b->offset = sg->sgl->offset;
 
        i = j = 0;
-       for_each_sg_page(sg->sgl, &sg_iter, sg->nents, 0) {
+       for_each_sg_dma_page (sg->sgl, &sg_iter, sg->nents, 0) {
                if (!pages--)
                        break;
                b->lop[i][j] = sg_page_iter_dma_address(&sg_iter) >> PAGE_SHIFT;
index b96f0d0b5b8f304be188e5d8662674739a2df0a6..b4be960c7e5dbaec396180c8f870b588078d5e41 100644 (file)
@@ -339,12 +339,12 @@ int sg_alloc_table_chained(struct sg_table *table, int nents,
 /*
  * sg page iterator
  *
- * Iterates over sg entries page-by-page.  On each successful iteration,
- * you can call sg_page_iter_page(@piter) and sg_page_iter_dma_address(@piter)
- * to get the current page and its dma address. @piter->sg will point to the
- * sg holding this page and @piter->sg_pgoffset to the page's page offset
- * within the sg. The iteration will stop either when a maximum number of sg
- * entries was reached or a terminating sg (sg_last(sg) == true) was reached.
+ * Iterates over sg entries page-by-page.  On each successful iteration, you
+ * can call sg_page_iter_page(@piter) to get the current page and its dma
+ * address. @piter->sg will point to the sg holding this page and
+ * @piter->sg_pgoffset to the page's page offset within the sg. The iteration
+ * will stop either when a maximum number of sg entries was reached or a
+ * terminating sg (sg_last(sg) == true) was reached.
  */
 struct sg_page_iter {
        struct scatterlist      *sg;            /* sg holding the page */
@@ -356,7 +356,19 @@ struct sg_page_iter {
                                                 * next step */
 };
 
+/*
+ * sg page iterator for DMA addresses
+ *
+ * This is the same as sg_page_iter however you can call
+ * sg_page_iter_dma_address(@dma_iter) to get the page's DMA
+ * address. sg_page_iter_page() cannot be called on this iterator.
+ */
+struct sg_dma_page_iter {
+       struct sg_page_iter base;
+};
+
 bool __sg_page_iter_next(struct sg_page_iter *piter);
+bool __sg_page_iter_dma_next(struct sg_dma_page_iter *dma_iter);
 void __sg_page_iter_start(struct sg_page_iter *piter,
                          struct scatterlist *sglist, unsigned int nents,
                          unsigned long pgoffset);
@@ -372,11 +384,13 @@ static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
 /**
  * sg_page_iter_dma_address - get the dma address of the current page held by
  * the page iterator.
- * @piter:     page iterator holding the page
+ * @dma_iter:  page iterator holding the page
  */
-static inline dma_addr_t sg_page_iter_dma_address(struct sg_page_iter *piter)
+static inline dma_addr_t
+sg_page_iter_dma_address(struct sg_dma_page_iter *dma_iter)
 {
-       return sg_dma_address(piter->sg) + (piter->sg_pgoffset << PAGE_SHIFT);
+       return sg_dma_address(dma_iter->base.sg) +
+              (dma_iter->base.sg_pgoffset << PAGE_SHIFT);
 }
 
 /**
@@ -385,11 +399,28 @@ static inline dma_addr_t sg_page_iter_dma_address(struct sg_page_iter *piter)
  * @piter:     page iterator to hold current page, sg, sg_pgoffset
  * @nents:     maximum number of sg entries to iterate over
  * @pgoffset:  starting page offset
+ *
+ * Callers may use sg_page_iter_page() to get each page pointer.
  */
 #define for_each_sg_page(sglist, piter, nents, pgoffset)                  \
        for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
             __sg_page_iter_next(piter);)
 
+/**
+ * for_each_sg_dma_page - iterate over the pages of the given sg list
+ * @sglist:    sglist to iterate over
+ * @dma_iter:  page iterator to hold current page
+ * @dma_nents: maximum number of sg entries to iterate over, this is the value
+ *              returned from dma_map_sg
+ * @pgoffset:  starting page offset
+ *
+ * Callers may use sg_page_iter_dma_address() to get each page's DMA address.
+ */
+#define for_each_sg_dma_page(sglist, dma_iter, dma_nents, pgoffset)            \
+       for (__sg_page_iter_start(&(dma_iter)->base, sglist, dma_nents,        \
+                                 pgoffset);                                   \
+            __sg_page_iter_dma_next(dma_iter);)
+
 /*
  * Mapping sg iterator
  *
index 9ba349e775ef0839b11eb18d1bc671357ab68ae3..739dc9fe2c55ecce2777a498db03396673d5a9c3 100644 (file)
@@ -625,6 +625,32 @@ bool __sg_page_iter_next(struct sg_page_iter *piter)
 }
 EXPORT_SYMBOL(__sg_page_iter_next);
 
+static int sg_dma_page_count(struct scatterlist *sg)
+{
+       return PAGE_ALIGN(sg->offset + sg_dma_len(sg)) >> PAGE_SHIFT;
+}
+
+bool __sg_page_iter_dma_next(struct sg_dma_page_iter *dma_iter)
+{
+       struct sg_page_iter *piter = &dma_iter->base;
+
+       if (!piter->__nents || !piter->sg)
+               return false;
+
+       piter->sg_pgoffset += piter->__pg_advance;
+       piter->__pg_advance = 1;
+
+       while (piter->sg_pgoffset >= sg_dma_page_count(piter->sg)) {
+               piter->sg_pgoffset -= sg_dma_page_count(piter->sg);
+               piter->sg = sg_next(piter->sg);
+               if (!--piter->__nents || !piter->sg)
+                       return false;
+       }
+
+       return true;
+}
+EXPORT_SYMBOL(__sg_page_iter_dma_next);
+
 /**
  * sg_miter_start - start mapping iteration over a sg list
  * @miter: sg mapping iter to be started