Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp3438993pxa; Tue, 25 Aug 2020 23:38:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwk/WM5Xf5ZeCQ5re+KuFRlqKTZpOeEu6a3xWMLvdjCRih+D2aXY11DZi6sMhpzZVH0v7wq X-Received: by 2002:aa7:d70a:: with SMTP id t10mr10518920edq.161.1598423917067; Tue, 25 Aug 2020 23:38:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598423917; cv=none; d=google.com; s=arc-20160816; b=Ay3CUw54VwDm6Nvk4tZW1NePSFi0w4DB60WbgId7hv0X/ca+BlODiBsw/y0IqcClTi L3Etm89B7guqV8w7evxqfMXUR4umv8KsLbcBHCxG2Czay1qETQTCQ6Ud3x2wDQvgcfAe MtJcTFyt0tm1jN6k/5gAF7nUcc/2veMU/3ew69TOKWk5/Fz2NMaALpzbd3CS7IXivsK7 ETaSVrstxGqpig2+RAL6oSwC3uBY6MsgawUlc7JPcUFf8JhzLk0Y+Snn1lLqqs3MoVoU ggo1JiwT0Xjr3HgeIKDpXqEfg5rdHm1AE+2akccxfGtRGKPhuwoILLqYJpuB7omWGNs7 2Vhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:cms-type:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dkim-filter; bh=uHaHrVj9uEjHIe4StK4tJYVPskphxegmYKiq5klM8TA=; b=DGnrPw40/mNSwqVRX8qa6A44MPbDleOf5UbaIAqjFeJdxjt3XQy4vHVq2MMeQqinBJ tO5SXDjZ8HMo9TU9eDUaUrz14siJ2/aHF8MvB9kxpUAEBPvZaftHd6Sql2ERNyfCF3Gu ExgFqJER3Cahq9iJpJKeK6iYKPdC1zTyFhGGHDRHmbfP2CPy59tQ+Rklkf4I1C+CW7Eq 0Lw8X5FjwXSqTaon5BkYpor0LVaNF8HhKn/cycUbsd0ogwQpHZoL3ltEmk+69/YgpTcb o+LAwPqi5czm79ZPZG1X4jvvtN75sIxciDG105hRJ9PDJFWT+AQu0V2ft2SWG3FlG1B7 fthQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=GF0XVZPp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r1si992732ejc.332.2020.08.25.23.38.14; Tue, 25 Aug 2020 23:38:37 -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; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=GF0XVZPp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726972AbgHZGg3 (ORCPT + 99 others); Wed, 26 Aug 2020 02:36:29 -0400 Received: from mailout2.w1.samsung.com ([210.118.77.12]:39705 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726887AbgHZGfu (ORCPT ); Wed, 26 Aug 2020 02:35:50 -0400 Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20200826063545euoutp0215fe2cc9eb109682124a8e916e75416c~uve2G3ZhV1443214432euoutp02N for ; Wed, 26 Aug 2020 06:35:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20200826063545euoutp0215fe2cc9eb109682124a8e916e75416c~uve2G3ZhV1443214432euoutp02N DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1598423745; bh=uHaHrVj9uEjHIe4StK4tJYVPskphxegmYKiq5klM8TA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GF0XVZPpiUXrTF0UXfKg4eTpjVkqSO8GPhtlWoFBBpF+opblWYWTfDtYCs4h2H+M9 YSadWlLKgEfxmOGP6JfKtX2bBScvchyWz0IwnaCa5LP8Hul9GnsQjmLrv6zQH8AUrx PR9wEfkCrCeTAne5MdSYGH4gvYA+hYF9N+E43mNY= Received: from eusmges2new.samsung.com (unknown [203.254.199.244]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20200826063545eucas1p23e973a046c1118d2848630fbbf80ac8d~uve1w86Aw0398703987eucas1p2p; Wed, 26 Aug 2020 06:35:45 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges2new.samsung.com (EUCPMTA) with SMTP id 58.BD.05997.1C2064F5; Wed, 26 Aug 2020 07:35:45 +0100 (BST) Received: from eusmtrp2.samsung.com (unknown [182.198.249.139]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20200826063544eucas1p15980885bbdbc43e35f350f228994bee7~uve1Yp4IX1480314803eucas1p1t; Wed, 26 Aug 2020 06:35:44 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp2.samsung.com (KnoxPortal) with ESMTP id 20200826063544eusmtrp2fb3d8e5660340ba67abfec026186393b~uve1YAfVx0466204662eusmtrp2g; Wed, 26 Aug 2020 06:35:44 +0000 (GMT) X-AuditID: cbfec7f4-65dff7000000176d-eb-5f4602c1993e Received: from eusmtip2.samsung.com ( [203.254.199.222]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id AC.53.06017.0C2064F5; Wed, 26 Aug 2020 07:35:44 +0100 (BST) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20200826063544eusmtip20c68027087fa433706e7ee60ee2698a4~uve0ycNPa0092300923eusmtip2T; Wed, 26 Aug 2020 06:35:44 +0000 (GMT) From: Marek Szyprowski To: dri-devel@lists.freedesktop.org, iommu@lists.linux-foundation.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Cc: Marek Szyprowski , Christoph Hellwig , Robin Murphy , Bartlomiej Zolnierkiewicz , linux-arm-kernel@lists.infradead.org, David Airlie , Daniel Vetter , Arnd Bergmann , Greg Kroah-Hartman Subject: [PATCH v9 28/32] misc: fastrpc: fix common struct sg_table related issues Date: Wed, 26 Aug 2020 08:33:12 +0200 Message-Id: <20200826063316.23486-29-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200826063316.23486-1-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrLKsWRmVeSWpSXmKPExsWy7djP87oHmdziDQ61cVr0njvJZPF30jF2 i40z1rNa/N82kdniytf3bBbNi9ezWaxcfZTJYsF+a4svVx4yWWx6fI3V4vKuOWwWa4/cZbc4 +OEJqwOvx5p5axg9fv+axOix99sCFo/t3x6weuyfu4bd4373cSaPzUvqPW7/e8zsMfnGckaP 3Tcb2Dz6tqxi9Pi8SS6AJ4rLJiU1J7MstUjfLoEro+3sfdaCKwIV17/fZWlg/MDbxcjJISFg IrF4xx3mLkYuDiGBFYwSaz/dYoJwvjBKrFz8kA3C+cwoMf9uIwtMy57nU6CqljNKnP1xgx2u 5e+KS8wgVWwChhJdb7vYQGwRgVZGiRO9PCBFzAKvmCTmzelkBUkIC4RInDx8DayBRUBVomUi RJxXwE7i5MQJ7BDr5CVWbzgAVsMJFD/e3Q92k4TAKXaJ6XtXM0MUuUgcevGKDcIWlnh1fAtU s4zE/53zmSAamhklHp5byw7h9DBKXG6awQhRZS1x59wvoG4OoPs0Jdbv0ocIO0pcmPSfBSQs IcAnceOtIEiYGcictG06M0SYV6KjTQiiWk1i1vF1cG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c2MxCSZy3Q+BgjJBAemJJanZqakFqEUwfEwenVANjf2aO2lyrHUKqn2Y1 XT4jKsxWLPiRY+4S9pd8i1+WxsfKsYVHOq+1PR8cfIfvr9qMyEnxuh+09N5vD1nfov55i+l8 Ba8TbYU6gb2TGsx/XBXR/npO7tLXsDdm5YU/Z//RDQjcEN1yaGnTF68VP033HXucW3bx7+e9 6pc8lzpZ/poVKZY3u/KREktxRqKhFnNRcSIAobSgz7ACAAA= X-CMS-MailID: 20200826063544eucas1p15980885bbdbc43e35f350f228994bee7 X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20200826063544eucas1p15980885bbdbc43e35f350f228994bee7 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20200826063544eucas1p15980885bbdbc43e35f350f228994bee7 References: <20200826063316.23486-1-m.szyprowski@samsung.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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/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) -- 2.17.1