Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1945545pxk; Tue, 1 Sep 2020 11:30:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzI7pgFY9u0Gt2gSwPAbnE1XZMiiNV7eC8wFCGWTmCxmgL3Kw8WaUgFnQ1yuRhs4io2jfZL X-Received: by 2002:a17:906:b154:: with SMTP id bt20mr2977540ejb.272.1598985009795; Tue, 01 Sep 2020 11:30:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598985009; cv=none; d=google.com; s=arc-20160816; b=Qijv9u99FPuZixRVm/i/9Hqc0EvFf1AWUOHoEL1UeYDPDwnyT8/aElbnDMHojiToh1 zfWyhAni/8+1SsmmeE0gJOjJOPpu1SvK6hqg/eDhqJ6DegV8aDOgEUmyskz31LyHApHf yYwQtAOKhSZwbklUS4nquUdpnqQ+bvtGeGe1JxGa7g54f0tQzRoZnrECiNBYTxIQedQT B3iQi1/4z5+YbDzNrpF2Y++IIU+ZeDaeS9qbb5ycdY4y/+k+ayl9BKz9mjW6V3RXDO02 fuVA//wyls+huLmYwP7TjWawJAmLts5m9lA3wFPxnal3D/UPR3JAcHo9gOoNYKd279bc rDiQ== 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=t0pMSxzhG63440nY1faprnTvmr+Jem7r8z/gJlvKHpI=; b=d/2ue5/LRe2RHJCDrz9qM2JYpWh2Y8lukLX1Kxp1gBlMxotj20h42ajmFDTHVYPodx ptUoRpCua6FIrSj7kj03D9F8v2LGoamaN119EA6aLmJYzU3tzN9J24YQGg2qCa2CX2u2 CocoJlbxqpmWGR3M2sSVUEyJGU3/GePl+NcnHcPdlbBlHYN9XXMpH8oWwtNBzINaT0We biZ5v5ylZW1SSYGc6d1Qa25SDu4HLveLWzPtRwN+kdXP+f03inqAiL7cq2E4dhfpsh/I eMQ9jxNnqgFlhcD9pJaL8ypRcjSpM5T5RLwOLpP02+uTh8OLZNxwkPtuTXOcsaBqarvF 4MNQ== 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 y89si1078033ede.586.2020.09.01.11.29.45; Tue, 01 Sep 2020 11:30:09 -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 S1730240AbgIAS0g (ORCPT + 99 others); Tue, 1 Sep 2020 14:26:36 -0400 Received: from foss.arm.com ([217.140.110.172]:48016 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727991AbgIAS0g (ORCPT ); Tue, 1 Sep 2020 14:26:36 -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 4B5561FB; Tue, 1 Sep 2020 11:26:35 -0700 (PDT) Received: from [10.57.40.122] (unknown [10.57.40.122]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 63A0F3F71F; Tue, 1 Sep 2020 11:26:33 -0700 (PDT) Subject: Re: [PATCH v9 04/32] drm: armada: fix common struct sg_table related issues 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 , Bartlomiej Zolnierkiewicz , linux-arm-kernel@lists.infradead.org, David Airlie , Daniel Vetter , Russell King References: <20200826063316.23486-1-m.szyprowski@samsung.com> <20200826063316.23486-5-m.szyprowski@samsung.com> From: Robin Murphy Message-ID: Date: Tue, 1 Sep 2020 19:26:32 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <20200826063316.23486-5-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 2020-08-26 07:32, Marek Szyprowski wrote: > 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 > --- > 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 8005614d2e6b..bedd8937d8a1 100644 > --- a/drivers/gpu/drm/armada/armada_gem.c > +++ b/drivers/gpu/drm/armada/armada_gem.c > @@ -395,7 +395,7 @@ armada_gem_prime_map_dma_buf(struct dma_buf_attachment *attach, > > 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 @@ armada_gem_prime_map_dma_buf(struct dma_buf_attachment *attach, > 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; I think it might be even nicer to get rid of "num" entirely and convert the cleanup path to for_each_sgtable_sg() for completeness - AFAICS it should only need an extra "if (sg_page(sg))..." check in that loop. Then "count" could possibly be squashed into its one remaining use as well, but maybe it's worth keeping for readability. Robin. > goto release; > } > } else if (dobj->page) { > @@ -418,7 +418,7 @@ armada_gem_prime_map_dma_buf(struct dma_buf_attachment *attach, > > 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)); > } > >