aboutsummaryrefslogtreecommitdiffstats
path: root/lib/scatterlist.c
diff options
context:
space:
mode:
authorJason Gunthorpe <jgg@mellanox.com>2019-01-04 13:40:21 -0500
committerJason Gunthorpe <jgg@mellanox.com>2019-02-11 17:02:33 -0500
commitd901b2760dc6cd5fbbf2eac31d71d94baa6c4aef (patch)
tree6598a5b011cea1bcdf160c2be9fd4e677d387e2b /lib/scatterlist.c
parentf368ff188ae4b3ef6f740a15999ea0373261b619 (diff)
lib/scatterlist: Provide a DMA page iterator
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>
Diffstat (limited to 'lib/scatterlist.c')
-rw-r--r--lib/scatterlist.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index 9ba349e775ef..739dc9fe2c55 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -625,6 +625,32 @@ bool __sg_page_iter_next(struct sg_page_iter *piter)
625} 625}
626EXPORT_SYMBOL(__sg_page_iter_next); 626EXPORT_SYMBOL(__sg_page_iter_next);
627 627
628static int sg_dma_page_count(struct scatterlist *sg)
629{
630 return PAGE_ALIGN(sg->offset + sg_dma_len(sg)) >> PAGE_SHIFT;
631}
632
633bool __sg_page_iter_dma_next(struct sg_dma_page_iter *dma_iter)
634{
635 struct sg_page_iter *piter = &dma_iter->base;
636
637 if (!piter->__nents || !piter->sg)
638 return false;
639
640 piter->sg_pgoffset += piter->__pg_advance;
641 piter->__pg_advance = 1;
642
643 while (piter->sg_pgoffset >= sg_dma_page_count(piter->sg)) {
644 piter->sg_pgoffset -= sg_dma_page_count(piter->sg);
645 piter->sg = sg_next(piter->sg);
646 if (!--piter->__nents || !piter->sg)
647 return false;
648 }
649
650 return true;
651}
652EXPORT_SYMBOL(__sg_page_iter_dma_next);
653
628/** 654/**
629 * sg_miter_start - start mapping iteration over a sg list 655 * sg_miter_start - start mapping iteration over a sg list
630 * @miter: sg mapping iter to be started 656 * @miter: sg mapping iter to be started