public inbox for iommu@lists.linux-foundation.org
 help / color / mirror / Atom feed
From: Steven Price <steven.price@arm.com>
To: Marek Szyprowski <m.szyprowski@samsung.com>,
	"dri-devel@lists.freedesktop.org"
	<dri-devel@lists.freedesktop.org>,
	"iommu@lists.linux-foundation.org"
	<iommu@lists.linux-foundation.org>,
	"linaro-mm-sig@lists.linaro.org" <linaro-mm-sig@lists.linaro.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Cc: Rob Herring <robh@kernel.org>,
	Tomeu Vizoso <tomeu.vizoso@collabora.com>,
	Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>,
	David Airlie <airlied@linux.ie>,
	Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>,
	Daniel Vetter <daniel@ffwll.ch>,
	Robin Murphy <Robin.Murphy@arm.com>,
	Christoph Hellwig <hch@lst.de>,
	"linux-arm-kernel@lists.infradead.org"
	<linux-arm-kernel@lists.infradead.org>
Subject: Re: [PATCH v2 09/21] drm: panfrost: fix sg_table nents vs. orig_nents misuse
Date: Mon, 4 May 2020 14:27:56 +0100	[thread overview]
Message-ID: <607fc87f-eb1c-6362-d8ff-3ac6ccf31bdf@arm.com> (raw)
In-Reply-To: <20200504125359.5678-9-m.szyprowski@samsung.com>

On 04/05/2020 13:53, Marek Szyprowski wrote:
> The Documentation/DMA-API-HOWTO.txt states that dma_map_sg returns the
> numer of the created entries in the DMA address space. However the
> subsequent calls to dma_sync_sg_for_{device,cpu} and dma_unmap_sg must be
> called with the original number of entries passed to dma_map_sg. The
> sg_table->nents in turn holds the result of the dma_map_sg call as stated
> in include/linux/scatterlist.h. Adapt the code to obey those rules.

I find this commit message a bit confusing, but AFAICT the problem with 
the Panfrost code is really in mmu_map_sg() where we don't have the 
return value from dma_map_sg() and the for_each_sg() loop could (in 
theory) run off the end of the list.

The fix seems correct - store the return where it's meant to be (nents) 
and make sure when unmapping we use the original (orig_nents). So you 
might also consider adding:

Fixes: f3ba91228e8e ("drm/panfrost: Add initial panfrost driver")

Even better would be the wrappers you mention in the cover letter! ;)

Reviewed-by: Steven Price <steven.price@arm.com>

> 
> Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
> ---
> For more information, see '[PATCH v2 00/21] DRM: fix struct sg_table nents
> vs. orig_nents misuse' thread: https://lkml.org/lkml/2020/5/4/373
> ---
>   drivers/gpu/drm/panfrost/panfrost_gem.c | 3 ++-
>   drivers/gpu/drm/panfrost/panfrost_mmu.c | 4 +++-
>   2 files changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.c b/drivers/gpu/drm/panfrost/panfrost_gem.c
> index 17b654e..22fec7c 100644
> --- a/drivers/gpu/drm/panfrost/panfrost_gem.c
> +++ b/drivers/gpu/drm/panfrost/panfrost_gem.c
> @@ -42,7 +42,8 @@ static void panfrost_gem_free_object(struct drm_gem_object *obj)
>   		for (i = 0; i < n_sgt; i++) {
>   			if (bo->sgts[i].sgl) {
>   				dma_unmap_sg(pfdev->dev, bo->sgts[i].sgl,
> -					     bo->sgts[i].nents, DMA_BIDIRECTIONAL);
> +					     bo->sgts[i].orig_nents,
> +					     DMA_BIDIRECTIONAL);
>   				sg_free_table(&bo->sgts[i]);
>   			}
>   		}
> diff --git a/drivers/gpu/drm/panfrost/panfrost_mmu.c b/drivers/gpu/drm/panfrost/panfrost_mmu.c
> index ed28aeb..2d9b1f9 100644
> --- a/drivers/gpu/drm/panfrost/panfrost_mmu.c
> +++ b/drivers/gpu/drm/panfrost/panfrost_mmu.c
> @@ -517,7 +517,9 @@ static int panfrost_mmu_map_fault_addr(struct panfrost_device *pfdev, int as,
>   	if (ret)
>   		goto err_pages;
>   
> -	if (!dma_map_sg(pfdev->dev, sgt->sgl, sgt->nents, DMA_BIDIRECTIONAL)) {
> +	sgt->nents = dma_map_sg(pfdev->dev, sgt->sgl, sgt->orig_nents,
> +				DMA_BIDIRECTIONAL);
> +	if (!sgt->nents) {
>   		ret = -EINVAL;
>   		goto err_map;
>   	}
> 

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

  reply	other threads:[~2020-05-04 13:28 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20200504125032eucas1p2eeaf22690e6b557d69c834cc9dd75855@eucas1p2.samsung.com>
2020-05-04 12:50 ` [PATCH v2 00/21] DRM: fix struct sg_table nents vs. orig_nents misuse Marek Szyprowski
2020-05-04 12:52   ` Christoph Hellwig
2020-05-04 13:05     ` Marek Szyprowski
2020-05-04 13:11       ` Christoph Hellwig
2020-05-04 12:53   ` [PATCH v2 01/21] drm: core: fix " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 02/21] drm: amdgpu: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 03/21] drm: armada: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 04/21] drm: etnaviv: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 05/21] drm: exynos: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 06/21] drm: i915: fix sg_table nents vs. orig_nents misuse for dmabuf objects Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 07/21] drm: lima: fix sg_table nents vs. orig_nents misuse Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 08/21] drm: msm: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 09/21] drm: panfrost: " Marek Szyprowski
2020-05-04 13:27       ` Steven Price [this message]
2020-05-04 12:53     ` [PATCH v2 10/21] drm: radeon: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 11/21] drm: rockchip: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 12/21] drm: tegra: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 13/21] drm: virtio: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 14/21] drm: vmwgfx: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 15/21] drm: xen: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 16/21] drm: host1x: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 17/21] drm: rcar-du: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 18/21] xen: gntdev: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 19/21] dmabuf: " Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 20/21] media: pci: fix common ALSA DMA-mapping related code Marek Szyprowski
2020-05-04 12:53     ` [PATCH v2 21/21] staging: ion: fix sg_table nents vs. orig_nents misuse Marek Szyprowski

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=607fc87f-eb1c-6362-d8ff-3ac6ccf31bdf@arm.com \
    --to=steven.price@arm.com \
    --cc=Robin.Murphy@arm.com \
    --cc=airlied@linux.ie \
    --cc=alyssa.rosenzweig@collabora.com \
    --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=m.szyprowski@samsung.com \
    --cc=robh@kernel.org \
    --cc=tomeu.vizoso@collabora.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