Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp3439915pxa; Tue, 25 Aug 2020 23:41:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyVqkHq/4Yzk9rPNu/JZ2y3hk0eOY92rqU2ppdaaQC5wfRfoRErw3DU9Y0OTKXEcW8BYjFm X-Received: by 2002:a17:906:7f99:: with SMTP id f25mr721936ejr.307.1598424067830; Tue, 25 Aug 2020 23:41:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598424067; cv=none; d=google.com; s=arc-20160816; b=SWb4pc+qjgDjKFaEzCSKst6gb2sYL7Z2BENAI776Ut8L/uNGW301o4ZoU95fO7SsEQ 8OkIWGyLiqMGA42ADAdg6b66GFT/M8p3DQ6g4dC82qSc+/LFvwPV0UZGQc9lvIUi2g0m l1ZPXTE4IQ1t+A1JSebOMHNsqMUWGeb0zJtE6ATHJmLe2SsORsnXMwVtP6oEluM0baAT eNXY1YJUpwsuaXNiQFMPQiN+xbIbPbl5EIToJGB/1z+V5NdLXUxhpSf+e1IVF0a9ROhQ sTPPSZsHtREPoULmV8NwUS6BT5Z5gFCHICac9ZmFFY6PdRerk4+pPAuLrNDARHjJltei vh+A== 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=8SvVp249TnvdrtsN3cK9UiYXaHvxLMtfidsvZb7ATx0=; b=h0Ov2LDXq+McwG5tTR8H157O5n/j8Vo+I5Zds7/YmulygEbhWCrY7BLNa9ewpNcyDv Bta9pmhRZILFagE2F8CFLgR5SF7XRv+LO/t3paCjsM46Shc+TvXvRo9qw4LuPvkyDbMi QtX+5HKxYAwJh/hMrhN0JmykL1iJ9b0vGnCsbRFNk2Jt29RfVhxIkw/2SaVJF/dOt6vK L65h2y0ZYvZMqqKZYAQm7dSI9xTHPdBf89DD/1oq8ysz3EmS1seH7MchYVkPtjp9jZss 438CAM73y4yxMuGSBex/9RGxZd/Fmn6jsrZ6l8hkk8EoL8mSaUAVp0PKt3LNwpyoZ1g5 mGAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=GMfzMNaD; 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 y8si784427edo.253.2020.08.25.23.40.45; Tue, 25 Aug 2020 23:41:07 -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=GMfzMNaD; 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 S1727792AbgHZGhO (ORCPT + 99 others); Wed, 26 Aug 2020 02:37:14 -0400 Received: from mailout1.w1.samsung.com ([210.118.77.11]:37771 "EHLO mailout1.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726867AbgHZGfp (ORCPT ); Wed, 26 Aug 2020 02:35:45 -0400 Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout1.w1.samsung.com (KnoxPortal) with ESMTP id 20200826063539euoutp01fbdc91f01a49f9105a08bd95830bff0d~uvewuxjLT2036820368euoutp01E for ; Wed, 26 Aug 2020 06:35:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20200826063539euoutp01fbdc91f01a49f9105a08bd95830bff0d~uvewuxjLT2036820368euoutp01E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1598423739; bh=8SvVp249TnvdrtsN3cK9UiYXaHvxLMtfidsvZb7ATx0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GMfzMNaD8tCyRezUqsl9ZZew+2XaHCp9rj1C3SO2NeQ4Kk+NMhVsrpxYXsV/xog/O BONpuFE9DkCIzkA0eJdlBblfzKsOgNcoJmmTtDbjFz4OPF1bu/uscmU0AUFwXJxQNB vxc5RNVvr57qOlFlHAvQN9yI0Hm1xAK6oiij6kCM= Received: from eusmges3new.samsung.com (unknown [203.254.199.245]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20200826063539eucas1p163681e3b94d83d94f1030ebe516e2260~uvewVtVP01942019420eucas1p1x; Wed, 26 Aug 2020 06:35:39 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges3new.samsung.com (EUCPMTA) with SMTP id 03.5D.06318.BB2064F5; Wed, 26 Aug 2020 07:35:39 +0100 (BST) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20200826063539eucas1p2ed946c6214cdb8574ab288350db22f1a~uvewBaXKt0401804018eucas1p2j; Wed, 26 Aug 2020 06:35:39 +0000 (GMT) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20200826063539eusmtrp1dc9bf141cf03c4673abfd423c5e6a255~uvewAxgWX1091110911eusmtrp1f; Wed, 26 Aug 2020 06:35:39 +0000 (GMT) X-AuditID: cbfec7f5-38bff700000018ae-ea-5f4602bb3e32 Received: from eusmtip2.samsung.com ( [203.254.199.222]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id 1A.D0.06314.AB2064F5; Wed, 26 Aug 2020 07:35:39 +0100 (BST) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20200826063538eusmtip25cde68f15a1e08c51df46c690f5e6122~uveveVu_j0474104741eusmtip2W; Wed, 26 Aug 2020 06:35:38 +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 , Eric Anholt Subject: [PATCH v9 19/32] drm: v3d: fix common struct sg_table related issues Date: Wed, 26 Aug 2020 08:33:03 +0200 Message-Id: <20200826063316.23486-20-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: H4sIAAAAAAAAA0WSe0hTURzHO7t3d9e1xW1TPGgoDCyy0syii5o9MLtFoERQCGYzLz5yKpta +keaWvisdEGiKVME31PnI1PzsWzLVuarMEzTrMBKTZ3Ld3m9Wv99ft/f93u+h8PBEVE11woP Douk5WHSUAnGRxt0i28PNHM8/Q7m1kAyo7uLQ9ZkV3HJPw2ZCDkwP42R7bf7AVla/oJDqtpc SePAGIfUjL/nkv1NjzGysnOYR3b8+sI9IaASpnUYVZFfAahnJhVKPTGNcqlPaXoOVVsURw2t jSOUcrAYUM0f4jHqXl0ZoOY0Nt7bffhuAXRocDQtd3S/yg+a6fjJjSg0v5n3TgXiwRSRCsxw SByGuVkpSCrg4yKiBMClhRLADkYA5xdmUHaYA3CmRs/bijR1McwsigE0qqc2FhuRWR3JMEY4 wdTJVIxhc+IOgC8zBEwAIZ5yYGFKy3oHjosJL2gc9WU8KGEHe/KWUUYWEu5wslbCdtnC8up2 hGGzdVmfdh9jjoGElge/qUtR1uQBn+t0m5cTw+/6uk3eBQ3KdJQNJAI41l3JY4d0APsTsgHr coUfu5cwphkh9sKqJkdWPgkfavp4jAyJHXBwcicjI+uY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EB6YklqdmpqQWoRTB8TB6dUA6PZTM0m9zAFQav59o82v7/GUlUz 0fbV/ktdO7zlo9oZ5rsWtAln/uT8MIHhaMoeryeS0za7N3nfvbd9XqBjnfd5ziBBdpupq+Lf ifvcjdr2NKfh3m07YZ7cNRLf15zZldP8w7y05RtrVuvO7A/WuiI6K7O3vtl2wPfk3rmRlcaa qjXCC7+H2yixFGckGmoxFxUnAgCW0JQipgIAAA== X-CMS-MailID: 20200826063539eucas1p2ed946c6214cdb8574ab288350db22f1a X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20200826063539eucas1p2ed946c6214cdb8574ab288350db22f1a X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20200826063539eucas1p2ed946c6214cdb8574ab288350db22f1a 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 Reviewed-by: Eric Anholt --- drivers/gpu/drm/v3d/v3d_mmu.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/v3d/v3d_mmu.c b/drivers/gpu/drm/v3d/v3d_mmu.c index 3b81ea28c0bb..5a453532901f 100644 --- a/drivers/gpu/drm/v3d/v3d_mmu.c +++ b/drivers/gpu/drm/v3d/v3d_mmu.c @@ -90,18 +90,17 @@ void v3d_mmu_insert_ptes(struct v3d_bo *bo) struct v3d_dev *v3d = to_v3d_dev(shmem_obj->base.dev); u32 page = bo->node.start; u32 page_prot = V3D_PTE_WRITEABLE | V3D_PTE_VALID; - unsigned int count; - struct scatterlist *sgl; + struct sg_dma_page_iter dma_iter; - for_each_sg(shmem_obj->sgt->sgl, sgl, shmem_obj->sgt->nents, count) { - u32 page_address = sg_dma_address(sgl) >> V3D_MMU_PAGE_SHIFT; + for_each_sgtable_dma_page(shmem_obj->sgt, &dma_iter, 0) { + dma_addr_t dma_addr = sg_page_iter_dma_address(&dma_iter); + u32 page_address = dma_addr >> V3D_MMU_PAGE_SHIFT; u32 pte = page_prot | page_address; u32 i; - BUG_ON(page_address + (sg_dma_len(sgl) >> V3D_MMU_PAGE_SHIFT) >= + BUG_ON(page_address + (PAGE_SIZE >> V3D_MMU_PAGE_SHIFT) >= BIT(24)); - - for (i = 0; i < sg_dma_len(sgl) >> V3D_MMU_PAGE_SHIFT; i++) + for (i = 0; i < PAGE_SIZE >> V3D_MMU_PAGE_SHIFT; i++) v3d->pt[page++] = pte + i; } -- 2.17.1