From patchwork Tue May 12 09:00:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 186518 Delivered-To: patch@linaro.org Received: by 2002:a92:8d81:0:0:0:0:0 with SMTP id w1csp2873974ill; Tue, 12 May 2020 02:01:56 -0700 (PDT) X-Google-Smtp-Source: APiQypLwRhKWSg4DX5+jlTpn6ISRJOwhIx3GsAubtp/dxrWsr28lU2nLmzSeIaEI8mB1seIkrRie X-Received: by 2002:a63:7c0a:: with SMTP id x10mr18005626pgc.78.1589274116638; Tue, 12 May 2020 02:01:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589274116; cv=none; d=google.com; s=arc-20160816; b=fBhjz6ACOzNEf8XHMp/fkVZ0Vy1wR8IF9WqNaIdp6X5fYvY+vdc+2iH/dp42ghSrkH A9kVP17dY75cUoOpyHiCbrDCSMrNJ4NmD0BYFridpaqcZj5zyZAxgMRXrvs3JtL9iIar wvc8PpNTXH0U5JlCnNt8kXyTHlPOKFKoU8FiUNC3z7ahrmhHX7A486vUoHzP8gzA3X4i 001WXI+v5sTSlablc6LSpjHdTK96w3MRGrU4pcz0XjXEgkubs48WCoMk5pQn8SnrL56M mBMVO5mjiEuNRn6gzYWpS7Jz6JoA9ojMeP4xY5Nye3PH1IZqlb8YhnGbK7V9vfS71p/+ i7xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:references:cms-type:in-reply-to:message-id:date :subject:to:from:dkim-signature:dkim-filter:delivered-to; bh=2vntjAodcK33WDtqrJVX/Kv9W6H9ydCDfDLexPrG8jA=; b=xq0J6fFAfEbmx46Hy6eRPVKlvpjd4TwTiE0rVxdLcJtudDoCS3cpQ9UHjVpOyZXfpl 1fqQt7I9iWWkO0+yZrRjtWT8f0RFHKxA7cRerbXADarOEJwNyKFC2+a/KK5YjnDq/4gd nG5/PslmYZGDQCyW5jTFrfnJnQVUlmR0gou2PzGwEUT68dYElMp13VNXxRLBnfBRX5fD eG95+LUT2EjjXVYAHhmBhVQ4GrAmaD9k4CeaziPK7cRdyf+jsBSoJR8KKlBzDDqqFS8U ytYc+ESLVH5lmc1OfbAXz0WKKhGpUsLBopEdPyFr+ldnAa7uAbhmeylHQ1EIWZ/ipyL7 KksQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@samsung.com header.s=mail20170921 header.b=lg820GM3; spf=pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 2610:10:20:722:a800:ff:fe36:1795 as permitted sender) smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from gabe.freedesktop.org (gabe.freedesktop.org. [2610:10:20:722:a800:ff:fe36:1795]) by mx.google.com with ESMTPS id n129si9613348pga.392.2020.05.12.02.01.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 May 2020 02:01:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 2610:10:20:722:a800:ff:fe36:1795 as permitted sender) client-ip=2610:10:20:722:a800:ff:fe36:1795; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@samsung.com header.s=mail20170921 header.b=lg820GM3; spf=pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 2610:10:20:722:a800:ff:fe36:1795 as permitted sender) smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id F0AAC6E8E8; Tue, 12 May 2020 09:01:35 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mailout1.w1.samsung.com (mailout1.w1.samsung.com [210.118.77.11]) by gabe.freedesktop.org (Postfix) with ESMTPS id A2AD86E8E3 for ; Tue, 12 May 2020 09:01:29 +0000 (UTC) Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout1.w1.samsung.com (KnoxPortal) with ESMTP id 20200512090127euoutp0130949b0d910c364fd4d8f6c8a232f98a~OPFy3TT4N2643526435euoutp01J for ; Tue, 12 May 2020 09:01:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20200512090127euoutp0130949b0d910c364fd4d8f6c8a232f98a~OPFy3TT4N2643526435euoutp01J DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1589274087; bh=xtDX8BGviEtRbw+qtq0o+AFcOoflB3PW51+MeG/P9Vc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lg820GM3RJ2Vk9G+aHjUNmqDKvzsnvaruNn4fDDm2Jp8pRTSuDuRJfh2xvKAVPHQ7 XlH3z1UVUuvUt+BdFDmmvsd96ESzEqJEsqDYy8GlVkVGNwpLI/wo1mlN2CGC+M9hCz DkEtaYPfl0orQVl6m5YCDLgB57lpm02/rSfSYvXI= Received: from eusmges3new.samsung.com (unknown [203.254.199.245]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20200512090126eucas1p101f5e02ec36131b74e821bce14c6f3d0~OPFyakiNr1621116211eucas1p1b; Tue, 12 May 2020 09:01:26 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges3new.samsung.com (EUCPMTA) with SMTP id 7A.7B.60698.6E56ABE5; Tue, 12 May 2020 10:01:26 +0100 (BST) Received: from eusmtrp2.samsung.com (unknown [182.198.249.139]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20200512090126eucas1p1ad8d5dfd09fce31d9a18691a76e9fa75~OPFyGFKV-3053030530eucas1p1y; Tue, 12 May 2020 09:01:26 +0000 (GMT) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eusmtrp2.samsung.com (KnoxPortal) with ESMTP id 20200512090126eusmtrp2164e8a1f47e23be294d333ff632cca58~OPFyFSfRJ0472704727eusmtrp2W; Tue, 12 May 2020 09:01:26 +0000 (GMT) X-AuditID: cbfec7f5-a0fff7000001ed1a-57-5eba65e6d790 Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id CE.45.08375.6E56ABE5; Tue, 12 May 2020 10:01:26 +0100 (BST) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20200512090125eusmtip1a261b1781c3fc6a5fbeaa821d4407673~OPFxT4e8k1257212572eusmtip1w; Tue, 12 May 2020 09:01:25 +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 Subject: [PATCH v4 32/38] staging: ion: fix common struct sg_table related issues Date: Tue, 12 May 2020 11:00:52 +0200 Message-Id: <20200512090058.14910-32-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200512090058.14910-1-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA0WSWUxTQRSGnbv1gtRcKpGJGpcaTdCIVohOhBBJTJxHDT4Y96JXINJCWkFR EguoURTiTsMugggUi2IuWipLRZFFsKgEFYqkiMGNpZYgAtpyXd6+85//n3NyMiwpc9Dz2Sj1 EV6jVkbLGU9KePqjffUAb9q71vRTgdLamgg0NNXLoLt6I42Svi1Dv4RLJDK3TkjQK+cQg1Ju GhlUUvaEQPm1QeiWbjkqMY8T6PurPgLds3fS6KUpm0HlDT0SJBh0NKof7qdRc49DgvI7JySb fLBQLdDYkGsAeCpnhMS2mscUfjSWT+Ebo04ad3eaGVw19p7GtTkGC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VcjILy6xVYo2tDDSM7S00DMysdQzNDaPtTIy VdK3s0lJzcksSy3St0vQy5hxbzlTwWy7itkXF7E2MK4x7mLk5JAQMJF4Nm01axcjF4eQwFJG iV0/7jNDJGQkTk5rYIWwhSX+XOtigyj6xCixYkMXWBGbgKFE11uIhIhAJ6PEtO6P7CAOs8AM Vol1B+6xg1QJCwRKtJ85wwhiswioSvROvQRm8wrYSex5eoYJYoW8xOoNB8CmcgLF78//wwJi CwkUSjy6+pZlAiPfAkaGVYwiqaXFuem5xYZ6xYm5xaV56XrJ+bmbGIExuu3Yz807GC9tDD7E KMDBqMTD22G0M06INbGsuDL3EKMEB7OSCG9LJlCINyWxsiq1KD++qDQntfgQoynQUROZpUST 84HpI68k3tDU0NzC0tDc2NzYzEJJnLdD4GCMkEB6YklqdmpqQWoRTB8TB6dUA6PlZ+EdAfb3 vtRutVxY7LAqIN3ZR7t9ed9DV5nHa14nXFm611aDZd2f97cm3M/ayf7UJ9un5t/JA3ERy699 WRnhNk8/e/WjubfsBOxnutoumJatvGbn9wMfNaSMJvXv+H+M44ShWqfdO0/Gtgtx81Zo5vT9 lvgs7PdCaMfBqf9trD1uOmf+8JBSYinOSDTUYi4qTgQAcfEsvOcCAAA= X-CMS-MailID: 20200512090126eucas1p1ad8d5dfd09fce31d9a18691a76e9fa75 X-Msg-Generator: CA X-RootMTR: 20200512090126eucas1p1ad8d5dfd09fce31d9a18691a76e9fa75 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20200512090126eucas1p1ad8d5dfd09fce31d9a18691a76e9fa75 References: <20200512085710.14688-1-m.szyprowski@samsung.com> <20200512090058.14910-1-m.szyprowski@samsung.com> X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: devel@driverdev.osuosl.org, Todd Kjos , Bartlomiej Zolnierkiewicz , David Airlie , Greg Kroah-Hartman , =?utf-8?q?Arve_Hj?= =?utf-8?b?w7hubmV2w6Vn?= , Martijn Coenen , Christian Brauner , Joel Fernandes , Laura Abbott , Robin Murphy , Christoph Hellwig , linux-arm-kernel@lists.infradead.org, Marek Szyprowski MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 --- For more information, see '[PATCH v4 00/38] DRM: fix struct sg_table nents vs. orig_nents misuse' thread: https://lore.kernel.org/dri-devel/20200512085710.14688-1-m.szyprowski@samsung.com/T/ --- drivers/staging/android/ion/ion.c | 25 +++++++-------- drivers/staging/android/ion/ion_heap.c | 44 ++++++++------------------- drivers/staging/android/ion/ion_system_heap.c | 2 +- 3 files changed, 25 insertions(+), 46 deletions(-) -- 1.9.1 _______________________________________________ dri-devel mailing list dri-devel@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/dri-devel diff --git a/drivers/staging/android/ion/ion.c b/drivers/staging/android/ion/ion.c index 38b51ea..2db041d 100644 --- a/drivers/staging/android/ion/ion.c +++ b/drivers/staging/android/ion/ion.c @@ -147,14 +147,14 @@ static struct sg_table *dup_sg_table(struct sg_table *table) if (!new_table) return ERR_PTR(-ENOMEM); - ret = sg_alloc_table(new_table, table->nents, GFP_KERNEL); + ret = sg_alloc_table(new_table, table->orig_nents, GFP_KERNEL); if (ret) { kfree(new_table); return ERR_PTR(-ENOMEM); } new_sg = new_table->sgl; - for_each_sg(table->sgl, sg, table->nents, i) { + for_each_sgtable_sg(table, sg, i) { memcpy(new_sg, sg, sizeof(*sg)); new_sg->dma_address = 0; new_sg = sg_next(new_sg); @@ -224,12 +224,13 @@ static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment, { struct ion_dma_buf_attachment *a = attachment->priv; struct sg_table *table; + int ret; table = a->table; - if (!dma_map_sg(attachment->dev, table->sgl, table->nents, - direction)) - return ERR_PTR(-ENOMEM); + ret = dma_map_sgtable(attachment->dev, table, direction); + if (ret) + return ERR_PTR(ret); return table; } @@ -238,7 +239,7 @@ static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment, struct sg_table *table, enum dma_data_direction direction) { - dma_unmap_sg(attachment->dev, table->sgl, table->nents, direction); + dma_unmap_sgtable(attachment->dev, table, direction); } static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) @@ -296,10 +297,8 @@ static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, } mutex_lock(&buffer->lock); - list_for_each_entry(a, &buffer->attachments, list) { - dma_sync_sg_for_cpu(a->dev, a->table->sgl, a->table->nents, - direction); - } + list_for_each_entry(a, &buffer->attachments, list) + dma_sync_sgtable_for_cpu(a->dev, a->table, direction); unlock: mutex_unlock(&buffer->lock); @@ -319,10 +318,8 @@ static int ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, } mutex_lock(&buffer->lock); - list_for_each_entry(a, &buffer->attachments, list) { - dma_sync_sg_for_device(a->dev, a->table->sgl, a->table->nents, - direction); - } + list_for_each_entry(a, &buffer->attachments, list) + dma_sync_sgtable_for_device(a->dev, a->table, direction); mutex_unlock(&buffer->lock); return 0; diff --git a/drivers/staging/android/ion/ion_heap.c b/drivers/staging/android/ion/ion_heap.c index 9c23b23..79f2794 100644 --- a/drivers/staging/android/ion/ion_heap.c +++ b/drivers/staging/android/ion/ion_heap.c @@ -20,8 +20,7 @@ void *ion_heap_map_kernel(struct ion_heap *heap, struct ion_buffer *buffer) { - struct scatterlist *sg; - int i, j; + struct sg_page_iter piter; void *vaddr; pgprot_t pgprot; struct sg_table *table = buffer->sg_table; @@ -38,14 +37,11 @@ void *ion_heap_map_kernel(struct ion_heap *heap, else pgprot = pgprot_writecombine(PAGE_KERNEL); - for_each_sg(table->sgl, sg, table->nents, i) { - int npages_this_entry = PAGE_ALIGN(sg->length) / PAGE_SIZE; - struct page *page = sg_page(sg); - - BUG_ON(i >= npages); - for (j = 0; j < npages_this_entry; j++) - *(tmp++) = page++; + for_each_sgtable_page(table, &piter, 0) { + BUG_ON(tmp - pages >= npages); + *tmp++ = sg_page_iter_page(&piter); } + vaddr = vmap(pages, npages, VM_MAP, pgprot); vfree(pages); @@ -64,32 +60,19 @@ void ion_heap_unmap_kernel(struct ion_heap *heap, int ion_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer, struct vm_area_struct *vma) { + struct sg_page_iter piter; struct sg_table *table = buffer->sg_table; unsigned long addr = vma->vm_start; - unsigned long offset = vma->vm_pgoff * PAGE_SIZE; - struct scatterlist *sg; - int i; int ret; - for_each_sg(table->sgl, sg, table->nents, i) { - struct page *page = sg_page(sg); - unsigned long remainder = vma->vm_end - addr; - unsigned long len = sg->length; + for_each_sgtable_page(table, &piter, vma->vm_pgoff) { + struct page *page = sg_page_iter_page(&piter); - if (offset >= sg->length) { - offset -= sg->length; - continue; - } else if (offset) { - page += offset / PAGE_SIZE; - len = sg->length - offset; - offset = 0; - } - len = min(len, remainder); - ret = remap_pfn_range(vma, addr, page_to_pfn(page), len, + ret = remap_pfn_range(vma, addr, page_to_pfn(page), PAGE_SIZE, vma->vm_page_prot); if (ret) return ret; - addr += len; + addr += PAGE_SIZE; if (addr >= vma->vm_end) return 0; } @@ -109,15 +92,14 @@ static int ion_heap_clear_pages(struct page **pages, int num, pgprot_t pgprot) return 0; } -static int ion_heap_sglist_zero(struct scatterlist *sgl, unsigned int nents, - pgprot_t pgprot) +static int ion_heap_sglist_zero(struct sg_table *sgt, pgprot_t pgprot) { int p = 0; int ret = 0; struct sg_page_iter piter; struct page *pages[32]; - for_each_sg_page(sgl, &piter, nents, 0) { + for_each_sgtable_page(sgt, &piter, 0) { pages[p++] = sg_page_iter_page(&piter); if (p == ARRAY_SIZE(pages)) { ret = ion_heap_clear_pages(pages, p, pgprot); @@ -142,7 +124,7 @@ int ion_heap_buffer_zero(struct ion_buffer *buffer) else pgprot = pgprot_writecombine(PAGE_KERNEL); - return ion_heap_sglist_zero(table->sgl, table->nents, pgprot); + return ion_heap_sglist_zero(table, pgprot); } void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer) diff --git a/drivers/staging/android/ion/ion_system_heap.c b/drivers/staging/android/ion/ion_system_heap.c index b83a1d1..eac0632 100644 --- a/drivers/staging/android/ion/ion_system_heap.c +++ b/drivers/staging/android/ion/ion_system_heap.c @@ -162,7 +162,7 @@ static void ion_system_heap_free(struct ion_buffer *buffer) if (!(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE)) ion_heap_buffer_zero(buffer); - for_each_sg(table->sgl, sg, table->nents, i) + for_each_sgtable_sg(table, sg, i) free_buffer_page(sys_heap, buffer, sg_page(sg)); sg_free_table(table); kfree(table);