Message ID | 20200512090058.14910-2-m.szyprowski@samsung.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | DRM: fix struct sg_table nents vs. orig_nents misuse | expand |
On 2020-05-12 10:00 am, Marek Szyprowski wrote: > struct sg_table is a common structure used for describing a memory > buffer. It consists of a scatterlist with memory pages and DMA addresses > (sgl entry), as well as the number of scatterlist entries: CPU pages > (orig_nents entry) and DMA mapped pages (nents entry). > > It turned out that it was a common mistake to misuse nents and orig_nents > entries, calling the scatterlist iterating functions with a wrong number > of the entries. > > To avoid such issues, lets introduce a common wrappers operating directly > on the struct sg_table objects, which take care of the proper use of > the nents and orig_nents entries. > > While touching this, lets clarify some ambiguities in the comments for > the existing for_each helpers. Reviewed-by: Robin Murphy <robin.murphy@arm.com> > Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> > --- > For more information, see '[PATCH v4 00/38] DRM: fix struct sg_table nents > vs. orig_nents misuse' thread: > https://lore.kernel.org/dri-devel/20200512085710.14688-1-m.szyprowski@samsung.com/T/ > --- > include/linux/scatterlist.h | 50 ++++++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 47 insertions(+), 3 deletions(-) > > diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h > index 6eec50f..4f922af 100644 > --- a/include/linux/scatterlist.h > +++ b/include/linux/scatterlist.h > @@ -151,6 +151,20 @@ static inline void sg_set_buf(struct scatterlist *sg, const void *buf, > #define for_each_sg(sglist, sg, nr, __i) \ > for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg)) > > +/* > + * Loop over each sg element in the given sg_table object. > + */ > +#define for_each_sgtable_sg(sgt, sg, i) \ > + for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) > + > +/* > + * Loop over each sg element in the given *DMA mapped* sg_table object. > + * Please use sg_dma_address(sg) and sg_dma_len(sg) to extract DMA addresses > + * of the each element. > + */ > +#define for_each_sgtable_dma_sg(sgt, sg, i) \ > + for_each_sg(sgt->sgl, sg, sgt->nents, i) > + > /** > * sg_chain - Chain two sglists together > * @prv: First scatterlist > @@ -401,9 +415,10 @@ static inline struct page *sg_page_iter_page(struct sg_page_iter *piter) > * @sglist: sglist to iterate over > * @piter: page iterator to hold current page, sg, sg_pgoffset > * @nents: maximum number of sg entries to iterate over > - * @pgoffset: starting page offset > + * @pgoffset: starting page offset (in pages) > * > * Callers may use sg_page_iter_page() to get each page pointer. > + * In each loop it operates on PAGE_SIZE unit. > */ > #define for_each_sg_page(sglist, piter, nents, pgoffset) \ > for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \ > @@ -412,18 +427,47 @@ static inline struct page *sg_page_iter_page(struct sg_page_iter *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_iter: DMA 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 > + * @pgoffset: starting page offset (in pages) > * > * Callers may use sg_page_iter_dma_address() to get each page's DMA address. > + * In each loop it operates on PAGE_SIZE unit. > */ > #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);) > > +/** > + * for_each_sgtable_page - iterate over all pages in the sg_table object > + * @sgt: sg_table object to iterate over > + * @piter: page iterator to hold current page > + * @pgoffset: starting page offset (in pages) > + * > + * Iterates over the all memory pages in the buffer described by > + * a scatterlist stored in the given sg_table object. > + * See also for_each_sg_page(). In each loop it operates on PAGE_SIZE unit. > + */ > +#define for_each_sgtable_page(sgt, piter, pgoffset) \ > + for_each_sg_page(sgt->sgl, piter, sgt->orig_nents, pgoffset) > + > +/** > + * for_each_sgtable_dma_page - iterate over the DMA mapped sg_table object > + * @sgt: sg_table object to iterate over > + * @dma_iter: DMA page iterator to hold current page > + * @pgoffset: starting page offset (in pages) > + * > + * Iterates over the all DMA mapped pages in the buffer described by > + * a scatterlist stored in the given sg_table object. > + * See also for_each_sg_dma_page(). In each loop it operates on PAGE_SIZE > + * unit. > + */ > +#define for_each_sgtable_dma_page(sgt, dma_iter, pgoffset) \ > + for_each_sg_dma_page(sgt->sgl, dma_iter, sgt->nents, pgoffset) > + > + > /* > * Mapping sg iterator > * >
diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index 6eec50f..4f922af 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -151,6 +151,20 @@ static inline void sg_set_buf(struct scatterlist *sg, const void *buf, #define for_each_sg(sglist, sg, nr, __i) \ for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg)) +/* + * Loop over each sg element in the given sg_table object. + */ +#define for_each_sgtable_sg(sgt, sg, i) \ + for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) + +/* + * Loop over each sg element in the given *DMA mapped* sg_table object. + * Please use sg_dma_address(sg) and sg_dma_len(sg) to extract DMA addresses + * of the each element. + */ +#define for_each_sgtable_dma_sg(sgt, sg, i) \ + for_each_sg(sgt->sgl, sg, sgt->nents, i) + /** * sg_chain - Chain two sglists together * @prv: First scatterlist @@ -401,9 +415,10 @@ static inline struct page *sg_page_iter_page(struct sg_page_iter *piter) * @sglist: sglist to iterate over * @piter: page iterator to hold current page, sg, sg_pgoffset * @nents: maximum number of sg entries to iterate over - * @pgoffset: starting page offset + * @pgoffset: starting page offset (in pages) * * Callers may use sg_page_iter_page() to get each page pointer. + * In each loop it operates on PAGE_SIZE unit. */ #define for_each_sg_page(sglist, piter, nents, pgoffset) \ for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \ @@ -412,18 +427,47 @@ static inline struct page *sg_page_iter_page(struct sg_page_iter *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_iter: DMA 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 + * @pgoffset: starting page offset (in pages) * * Callers may use sg_page_iter_dma_address() to get each page's DMA address. + * In each loop it operates on PAGE_SIZE unit. */ #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);) +/** + * for_each_sgtable_page - iterate over all pages in the sg_table object + * @sgt: sg_table object to iterate over + * @piter: page iterator to hold current page + * @pgoffset: starting page offset (in pages) + * + * Iterates over the all memory pages in the buffer described by + * a scatterlist stored in the given sg_table object. + * See also for_each_sg_page(). In each loop it operates on PAGE_SIZE unit. + */ +#define for_each_sgtable_page(sgt, piter, pgoffset) \ + for_each_sg_page(sgt->sgl, piter, sgt->orig_nents, pgoffset) + +/** + * for_each_sgtable_dma_page - iterate over the DMA mapped sg_table object + * @sgt: sg_table object to iterate over + * @dma_iter: DMA page iterator to hold current page + * @pgoffset: starting page offset (in pages) + * + * Iterates over the all DMA mapped pages in the buffer described by + * a scatterlist stored in the given sg_table object. + * See also for_each_sg_dma_page(). In each loop it operates on PAGE_SIZE + * unit. + */ +#define for_each_sgtable_dma_page(sgt, dma_iter, pgoffset) \ + for_each_sg_dma_page(sgt->sgl, dma_iter, sgt->nents, pgoffset) + + /* * Mapping sg iterator *
struct sg_table is a common structure used for describing a memory buffer. It consists of a scatterlist with memory pages and DMA addresses (sgl entry), as well as the number of scatterlist entries: CPU pages (orig_nents entry) and DMA mapped pages (nents entry). It turned out that it was a common mistake to misuse nents and orig_nents entries, calling the scatterlist iterating functions with a wrong number of the entries. To avoid such issues, lets introduce a common wrappers operating directly on the struct sg_table objects, which take care of the proper use of the nents and orig_nents entries. While touching this, lets clarify some ambiguities in the comments for the existing for_each helpers. Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> --- For more information, see '[PATCH v4 00/38] DRM: fix struct sg_table nents vs. orig_nents misuse' thread: https://lore.kernel.org/dri-devel/20200512085710.14688-1-m.szyprowski@samsung.com/T/ --- include/linux/scatterlist.h | 50 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-)