lib/scatterlist: Provide a DMA page iterator
Subsystems (8)
PATCH v2 1/1 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.
--- .clang-format | 1 + drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c | 8 +++- drivers/media/pci/intel/ipu3/ipu3-cio2.c | 4 +- include/linux/scatterlist.h | 49 ++++++++++++++++++---- lib/scatterlist.c | 26 ++++++++++++ 5 files changed, 76 insertions(+), 12 deletions(-)
v2:
- Drop the vmwgfx fix in favor of keeping it unchanged as there is no reviewer. Use an ugly case with a comment instead.
I'd like to apply this to the RDMA tree next week, a large series from Shiraz is waiting on it:
https://patchwork.kernel.org/project/linux-rdma/list/?series=71841&state=*
Regards,
Jason
.clang-format +1 -0
@@ -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'
drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c +7 -1
@@ -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/[email protected]/ +*/ +return sg_page_iter_dma_address ( + (struct sg_dma_page_iter *)&viter->iter); }
drivers/media/pci/intel/ipu3/ipu3-cio2.c +2 -2
@@ -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 ;
include/linux/scatterlist.h +40 -9
81 lines not on this page · raw message
lib/scatterlist.c +26 -0
@@ -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
Signature · 1 line
2.20.1
Quoted 11 lines · Jason Gunthorpe
Commit
2db76d7c3c6d("lib/scatterlist:sg_page_iter: support sg lists w/o backing pages") introduced thesg_page_iter_dma_address()function without providing a way to use it in the general case. If thesg_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 sosg_page_iter_dma_address()works as expected with all sglists.
Quoted 7 lines
A new iterator type is introduced to provide compile-time safety against wrongly mixing accessors and iterators.
Signed-off-by Jason Gunthorpe <[email protected]>--- .clang-format | 1 +
Thanks for updating the .clang-format, Jason! :-)
Cheers,
Miguel
Quoted 13 lines · Jason Gunthorpe
Commit
2db76d7c3c6d("lib/scatterlist:sg_page_iter: support sg lists w/o
backing pages") introduced thesg_page_iter_dma_address()function without
providing a way to use it in the general case. If thesg_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 sosg_page_iter_dma_address()works as expected with all sglists.
Quoted 7 lines
A new iterator type is introduced to provide compile-time safety against
wrongly mixing accessors and iterators.Signed-off-by Jason Gunthorpe <[email protected]>
For the vmwgfx part,
I'll take a deeper look to provide a vmwgfx fix as a follow up.
Thanks,
Thomas
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 5 lines · Jason Gunthorpe
drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c +1
-0+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c@@ -311,7 +311,13 @@ static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter) · line 320
Quoted 10 lines
drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c +7
-1static 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/[email protected]/ +*/ +return sg_page_iter_dma_address ( + (struct sg_dma_page_iter *)&viter->iter);
Occured to me this would be better written as:
return sg_page_iter_dma_address( container_of(&viter->iter, struct sg_dma_page_iter, base));
Since I think we are done with this now I'll fix it when I apply this patch
Thanks for all the acks everyone
Regards,
Jason
Jason Gunthorpe
AppliedQuoted 16 lines · Jason Gunthorpe
Commit
2db76d7c3c6d("lib/scatterlist:sg_page_iter: support sg lists w/o backing pages") introduced thesg_page_iter_dma_address()function without providing a way to use it in the general case. If thesg_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 sosg_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.
Signed-off-by Jason Gunthorpe <[email protected]>Acked-by Thomas Hellstrom <[email protected]>
Quoted 7 lines
--- .clang-format | 1 + drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c | 8 +++- drivers/media/pci/intel/ipu3/ipu3-cio2.c | 4 +- include/linux/scatterlist.h | 49 ++++++++++++++++++---- lib/scatterlist.c | 26 ++++++++++++ 5 files changed, 76 insertions(+), 12 deletions(-)
Applied to rdma.git's for-next, thanks everyone.
Jason