Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2009294pxk; Tue, 1 Sep 2020 13:16:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxgsF0gjLuKvkJYBNdTOUOSax3VAY2j/+o1otUw53GUCAx55gVYe0NNcWPBk0Vx+9ZQedtY X-Received: by 2002:a17:906:b317:: with SMTP id n23mr564386ejz.6.1598991381894; Tue, 01 Sep 2020 13:16:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598991381; cv=none; d=google.com; s=arc-20160816; b=M6TNWpA9qqybp41PX3wS5ihlrjgQyxuj8unug2vRIHR1CPiBcS1Wh5jexZOylC143E gjSb0IbtzVEQES1LlzyIBfEytMoFNXXDWEB4jdmQFPHHyLPi72BIO2NHyARd0xVVVR44 VsXXw1BFxRDkiN52Dsh736pWkkANRFhgfb7Co2+5Qmv4tQEHGZGNtX7hpqLe8doIr0k1 B8LGGKhGJQwfh997kNhS18pqhaqdYIq7pSeHiSYkD2yRLcyBPmYHYBun5khXBQUUS4rZ zx29gkDZGQEhc2Hjti6EejIlhhyRfbNzJFggFkm/FxCVsYsy1ThDKohtOpKINFrp0sWy PhTg== 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=0upRvZhl5FFDnZU/lHsCvkq0WnbWXnhzbxOmIIPFD2A=; b=dZmCCf/Bnu+6a3KJ1rGNYwhKZyDOndn6zt/tnhsUf+BEPtESC4a7fKfXas/yfcvHj2 0wmel2WTGSlHpMlUbWfJ6IrlRN5rr4ifV9QIYnUmq7zoYV7H7yUySg1hFvlYII9FbURC hV8K/PWtgTeIZBUhjVK1VqjV/Edw9z1JOX7zpXUxpCEy3N/O7VSstoLIUP7QoEIsouaw P2Zhbi9unhn49fw8XLFUaO23Sa0Pb7p9+7ByYzc+YQGzSqNeo/i4njq/UFbNGq0+ib3K aes3sw6+Ytmz3KTMvDWpfTerenSGYpQJhCkDlSVm5cYa118a4rDr4/O0J4b5YcPg67iR gFQQ== 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 a15si1469715ejc.25.2020.09.01.13.15.58; Tue, 01 Sep 2020 13:16:21 -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 S1729279AbgIAUOM (ORCPT + 99 others); Tue, 1 Sep 2020 16:14:12 -0400 Received: from foss.arm.com ([217.140.110.172]:49510 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726323AbgIAUOL (ORCPT ); Tue, 1 Sep 2020 16:14:11 -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 5BBC31FB; Tue, 1 Sep 2020 13:14:10 -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 211D43F66F; Tue, 1 Sep 2020 13:14:07 -0700 (PDT) Subject: Re: [PATCH v9 28/32] misc: fastrpc: 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 , Arnd Bergmann , Greg Kroah-Hartman References: <20200826063316.23486-1-m.szyprowski@samsung.com> <20200826063316.23486-29-m.szyprowski@samsung.com> From: Robin Murphy Message-ID: Date: Tue, 1 Sep 2020 21:14:06 +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-29-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:33, 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. Reviewed-by: Robin Murphy > Signed-off-by: Marek Szyprowski > --- > drivers/misc/fastrpc.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c > index 7939c55daceb..9d6867749316 100644 > --- a/drivers/misc/fastrpc.c > +++ b/drivers/misc/fastrpc.c > @@ -518,7 +518,7 @@ fastrpc_map_dma_buf(struct dma_buf_attachment *attachment, > > table = &a->sgt; > > - if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir)) > + if (!dma_map_sgtable(attachment->dev, table, dir, 0)) > return ERR_PTR(-ENOMEM); > > return table; > @@ -528,7 +528,7 @@ static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach, > struct sg_table *table, > enum dma_data_direction dir) > { > - dma_unmap_sg(attach->dev, table->sgl, table->nents, dir); > + dma_unmap_sgtable(attach->dev, table, dir, 0); > } > > static void fastrpc_release(struct dma_buf *dmabuf) >