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>,
	Russell King <linux@armlinux.org.uk>,
	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 08/38] drm: armada: fix common struct sg_table related issues
Date: Tue, 12 May 2020 11:00:28 +0200	[thread overview]
Message-ID: <20200512090058.14910-8-m.szyprowski@samsung.com> (raw)
In-Reply-To: <20200512090058.14910-1-m.szyprowski@samsung.com>

The Documentation/DMA-API-HOWTO.txt states that the dma_map_sg() function
returns the number of the created entries in the DMA address space.
However the subsequent calls to the dma_sync_sg_for_{device,cpu}() and
dma_unmap_sg must be called with the original number of the entries
passed to the dma_map_sg().

struct sg_table is a common structure used for describing a non-contiguous
memory buffer, used commonly in the DRM and graphics subsystems. 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 DMA-mapping functions with a wrong number of entries or
ignoring the number of mapped entries returned by the dma_map_sg()
function.

To avoid such issues, lets use a common dma-mapping wrappers operating
directly on the struct sg_table objects and use scatterlist page
iterators where possible. This, almost always, hides references to the
nents and orig_nents entries, making the code robust, easier to follow
and copy/paste safe.

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/
---
 drivers/gpu/drm/armada/armada_gem.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/gpu/drm/armada/armada_gem.c b/drivers/gpu/drm/armada/armada_gem.c
index 976685f..0d61da5 100644
--- a/drivers/gpu/drm/armada/armada_gem.c
+++ b/drivers/gpu/drm/armada/armada_gem.c
@@ -395,7 +395,7 @@ int armada_gem_pwrite_ioctl(struct drm_device *dev, void *data,
 
 		mapping = dobj->obj.filp->f_mapping;
 
-		for_each_sg(sgt->sgl, sg, count, i) {
+		for_each_sgtable_sg(sgt, sg, i) {
 			struct page *page;
 
 			page = shmem_read_mapping_page(mapping, i);
@@ -407,8 +407,8 @@ int armada_gem_pwrite_ioctl(struct drm_device *dev, void *data,
 			sg_set_page(sg, page, PAGE_SIZE, 0);
 		}
 
-		if (dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir) == 0) {
-			num = sgt->nents;
+		if (dma_map_sgtable(attach->dev, sgt, dir, 0)) {
+			num = count;
 			goto release;
 		}
 	} else if (dobj->page) {
@@ -418,7 +418,7 @@ int armada_gem_pwrite_ioctl(struct drm_device *dev, void *data,
 
 		sg_set_page(sgt->sgl, dobj->page, dobj->obj.size, 0);
 
-		if (dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir) == 0)
+		if (dma_map_sgtable(attach->dev, sgt, dir, 0))
 			goto free_table;
 	} else if (dobj->linear) {
 		/* Single contiguous physical region - no struct page */
@@ -449,11 +449,11 @@ static void armada_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach,
 	int i;
 
 	if (!dobj->linear)
-		dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
+		dma_unmap_sgtable(attach->dev, sgt, dir, 0);
 
 	if (dobj->obj.filp) {
 		struct scatterlist *sg;
-		for_each_sg(sgt->sgl, sg, sgt->nents, i)
+		for_each_sgtable_sg(sgt, sg, i)
 			put_page(sg_page(sg));
 	}
 
-- 
1.9.1

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

  parent 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     ` [PATCH v4 02/38] scatterlist: add generic wrappers for iterating over " Marek Szyprowski
2020-05-12 12:18       ` 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     ` Marek Szyprowski [this message]
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-8-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=linux@armlinux.org.uk \
    --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