Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3613702ybz; Mon, 4 May 2020 06:29:56 -0700 (PDT) X-Google-Smtp-Source: APiQypIND59cem2Zi8pK/3gEGSEJYgSLG0k13lgDCITbqpGSme6Ycmekk3lJPYc01krZ+c5Rmbio X-Received: by 2002:a17:906:1c94:: with SMTP id g20mr14315910ejh.319.1588598995847; Mon, 04 May 2020 06:29:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588598995; cv=none; d=google.com; s=arc-20160816; b=SNEvQ1YUkuaPMsn0Omor9doQGfjjJ5V5SMEv+jWpxxSOROWYlcW8DTStGYX8hoyepQ TGM6p3y2lWmp03tR2zi+Lw/F3gVNA32OigjNl82PeKs8Mx7kKc1Ders8kIqpCXx6N4H3 8Klfh6G6Yq7KMFXTDi2RymEkNy94x7jc5GtbgGvOLN9H9cLDv9DL4tfAcgmEZzpbcbJW 18GBbp/jxD1vIYKl4QrXRcriMOkfu4wH8BnUuIY+Nf7Kwn49I4Ew/pZy3BMAgZXnuoXB vkjM2pyikSmLIG3BPRScUU7A8qISgkKFxzt3q5B5v8Jc/QaP0CjAPR7OJjyNhsCkP3bB 2ACQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=V609F3jVPNfCWoXDXNBGmzCzgiVOXW1VXjkzIVpYwDE=; b=IDdNwDPfezcLqvr5dy43olDE8HZYQPYmUqBSnBhkGazV0Ffr6rsYTooHjRZCzbQojF 7Vlt8b0gV83F9uDBmoTTwhpRGiwDT4N71jeegYCcne2duf+Up6OdRi3Z02aERZD6sNua 9KGZv7fZCqt3XHagh8m6jMTUnuyMWuaicTl0FUNEMEzZikq6U6RNdUPGHPmeCnfk+Me6 eKnmHe1S2cTyOmsiM/kX/UveMyHO3Wbgcc19rqmPJjhoQT2VwDt9G5FA6jqopsOhV0/t tv86G6xIvMIT3MvQAxZmUyHjNm9G693Lo6YHD+asFXeq0q8G/BbfdXUKxpVDzw9YuFP7 T8+g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s7si6158197edy.460.2020.05.04.06.29.30; Mon, 04 May 2020 06:29:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728068AbgEDN2C (ORCPT + 99 others); Mon, 4 May 2020 09:28:02 -0400 Received: from foss.arm.com ([217.140.110.172]:44816 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727104AbgEDN2B (ORCPT ); Mon, 4 May 2020 09:28:01 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C61241FB; Mon, 4 May 2020 06:28:00 -0700 (PDT) Received: from [192.168.1.84] (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D88F43F71F; Mon, 4 May 2020 06:27:57 -0700 (PDT) Subject: Re: [PATCH v2 09/21] drm: panfrost: fix sg_table nents vs. orig_nents misuse To: Marek Szyprowski , "dri-devel@lists.freedesktop.org" , "iommu@lists.linux-foundation.org" , "linaro-mm-sig@lists.linaro.org" , "linux-kernel@vger.kernel.org" Cc: Christoph Hellwig , Robin Murphy , Bartlomiej Zolnierkiewicz , "linux-arm-kernel@lists.infradead.org" , David Airlie , Daniel Vetter , Rob Herring , Tomeu Vizoso , Alyssa Rosenzweig References: <20200504125017.5494-1-m.szyprowski@samsung.com> <20200504125359.5678-1-m.szyprowski@samsung.com> <20200504125359.5678-9-m.szyprowski@samsung.com> From: Steven Price Message-ID: <607fc87f-eb1c-6362-d8ff-3ac6ccf31bdf@arm.com> Date: Mon, 4 May 2020 14:27:56 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <20200504125359.5678-9-m.szyprowski@samsung.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > > Signed-off-by: Marek Szyprowski > --- > 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; > } >