Linux IOMMU Development
 help / color / mirror / Atom feed
From: Marek Szyprowski <m.szyprowski@samsung.com>
To: dri-devel@lists.freedesktop.org,
	iommu@lists.linux-foundation.org, linaro-mm-sig@lists.linaro.org,
	linux-kernel@vger.kernel.org
Cc: Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>,
	David Airlie <airlied@linux.ie>, Daniel Vetter <daniel@ffwll.ch>,
	Robin Murphy <robin.murphy@arm.com>,
	Christoph Hellwig <hch@lst.de>,
	linux-arm-kernel@lists.infradead.org
Subject: [PATCH v4 02/38] scatterlist: add generic wrappers for iterating over sgtable objects
Date: Tue, 12 May 2020 11:00:22 +0200	[thread overview]
Message-ID: <20200512090058.14910-2-m.szyprowski@samsung.com> (raw)
In-Reply-To: <20200512090058.14910-1-m.szyprowski@samsung.com>

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(-)

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
  *
-- 
1.9.1

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

  reply	other threads:[~2020-05-12  9:01 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20200512085722eucas1p2fbaab30e49c9ddadc64b27db856e5921@eucas1p2.samsung.com>
2020-05-12  8:57 ` [PATCH v4 00/38] DRM: fix struct sg_table nents vs. orig_nents misuse Marek Szyprowski
2020-05-12  9:00   ` [PATCH v4 01/38] dma-mapping: add generic helpers for mapping sgtable objects Marek Szyprowski
2020-05-12  9:00     ` Marek Szyprowski [this message]
2020-05-12 12:18       ` [PATCH v4 02/38] scatterlist: add generic wrappers for iterating over " Christoph Hellwig
2020-05-13 13:24       ` Robin Murphy
2020-05-12  9:00     ` [PATCH v4 03/38] iommu: add generic helper for mapping " Marek Szyprowski
2020-05-12 12:18       ` Christoph Hellwig
2020-05-13  9:03       ` Joerg Roedel
2020-05-13 13:27       ` Robin Murphy
2020-05-12  9:00     ` [PATCH v4 04/38] drm: prime: add common helper to check scatterlist contiguity Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 05/38] drm: prime: use sgtable iterators in drm_prime_sg_to_page_addr_arrays() Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 06/38] drm: core: fix common struct sg_table related issues Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 07/38] drm: amdgpu: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 08/38] drm: armada: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 09/38] drm: etnaviv: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 10/38] drm: exynos: use common helper for a scatterlist contiguity check Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 11/38] drm: exynos: fix common struct sg_table related issues Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 12/38] drm: i915: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 13/38] drm: lima: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 14/38] drm: mediatek: use common helper for a scatterlist contiguity check Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 15/38] drm: mediatek: use common helper for extracting pages array Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 16/38] drm: msm: fix common struct sg_table related issues Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 17/38] drm: omapdrm: use common helper for extracting pages array Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 18/38] drm: omapdrm: fix common struct sg_table related issues Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 19/38] drm: panfrost: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 20/38] drm: radeon: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 21/38] drm: rockchip: use common helper for a scatterlist contiguity check Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 22/38] drm: rockchip: fix common struct sg_table related issues Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 23/38] drm: tegra: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 24/38] drm: v3d: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 25/38] drm: virtio: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 26/38] drm: vmwgfx: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 27/38] xen: gntdev: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 28/38] drm: host1x: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 29/38] drm: rcar-du: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 30/38] dmabuf: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 31/38] staging: ion: remove dead code Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 32/38] staging: ion: fix common struct sg_table related issues Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 33/38] staging: tegra-vde: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 34/38] misc: fastrpc: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 35/38] rapidio: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 36/38] samples: vfio-mdev/mbochs: " Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 37/38] media: pci: fix common ALSA DMA-mapping related codes Marek Szyprowski
2020-05-12  9:00     ` [PATCH v4 38/38] videobuf2: use sgtable-based scatterlist wrappers Marek Szyprowski
2020-05-12 17:52       ` Ruhl, Michael J
2020-05-12 20:33         ` Marek Szyprowski
2020-05-13 12:01           ` Ruhl, Michael J
2020-05-12 12:18     ` [PATCH v4 01/38] dma-mapping: add generic helpers for mapping sgtable objects Christoph Hellwig
2020-05-12 13:00       ` Marek Szyprowski
2020-05-12 13:09         ` Christoph Hellwig
2020-05-12 13:19           ` Marek Szyprowski
2020-05-13 13:23     ` Robin Murphy
2020-05-12 12:19   ` [PATCH v4 00/38] DRM: fix struct sg_table nents vs. orig_nents misuse Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200512090058.14910-2-m.szyprowski@samsung.com \
    --to=m.szyprowski@samsung.com \
    --cc=airlied@linux.ie \
    --cc=b.zolnierkie@samsung.com \
    --cc=daniel@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hch@lst.de \
    --cc=iommu@lists.linux-foundation.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robin.murphy@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox