From patchwork Wed Apr 5 16:02:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shuah Khan X-Patchwork-Id: 9664585 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 84136602B5 for ; Wed, 5 Apr 2017 16:03:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 75AB428159 for ; Wed, 5 Apr 2017 16:03:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6A1EE285A0; Wed, 5 Apr 2017 16:03:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C383A28159 for ; Wed, 5 Apr 2017 16:03:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=YlYPO6jsKPal6SkNcyxq7PrFcBjaDkkp14sR3homKtA=; b=SKT YjxQld/Oxcq3/I1PBXV1LJR7qbFlM5oI9W/BamWneWvKpYE2ZmGX3QAeP/bNPb9bCUTVVzQmN78vj MijYzTLB8UcV3HqsZ2HGPhcC8IVh8IbyGeaZAYvqu8FiAEtAl/Aty2SQTSP6YhVFNF/iR8cqGPSCT /R7MkF7D0BWyYLnXv9x6gEec5f3sK2f5DCd+yYtsWKyp4+dy5nzAQO4pa+Vs4DhEFFGKw1+bcgTHr NKYRboygzAJKXn8PFGt70cruhY+jG6cr+2oXKfg9khpVR7u6DIBKAGFU6vKFTPOYWKQXIxLrSMPwn RIDF85p4E/NxveBXRc3YIMc9Ph/WMew==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cvnOz-0001I2-DX; Wed, 05 Apr 2017 16:03:21 +0000 Received: from merlin.infradead.org ([2001:4978:20e::2]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cvnOw-0001Hm-QP for linux-arm-kernel@bombadil.infradead.org; Wed, 05 Apr 2017 16:03:18 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=0DZTo+qZ3Bwtgp1epmPhzpozNCClzcy6vlT//VZj29w=; b=b0AaOrFAHoTLl5ADbgyCLE8d4 YIEABp9I1t3Kfuqz6MIOipePrkGza39fdxwOwv4LnaNdTDcaIlhE0ph43xejIIKBCc4I1PMidzh7j SxrTWfIVXyRvXFRosP/WRKlGV5CVuDJgLfy8weMG8bKL14ClUVLVyqbunxuc/9yxJELmnvG8yXp1P IszBE8+mC1YPxudzVzSsX6eYdKd1kCPEAJkwpUA9bzwY75m0q1ki3Rcy5ElAABgXfJUtG7amVJoqs fSxU0juOJjZLHdCvTI6UFeSxrch/pjFFm1fUWgsa4xLXJL5TMkYsgfMGXJk3d0bug2YT7Tbjz3sab j+R1qcxYQ==; Received: from ec2-52-27-115-49.us-west-2.compute.amazonaws.com ([52.27.115.49] helo=osg.samsung.com) by merlin.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cvnOu-00041R-C3 for linux-arm-kernel@lists.infradead.org; Wed, 05 Apr 2017 16:03:17 +0000 Received: from localhost (localhost [127.0.0.1]) by osg.samsung.com (Postfix) with ESMTP id 4E4D8A05EE; Wed, 5 Apr 2017 16:03:14 +0000 (UTC) X-Virus-Scanned: amavisd-new at osg.samsung.com Received: from osg.samsung.com ([127.0.0.1]) by localhost (s-opensource.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ksn8ttmGocNL; Wed, 5 Apr 2017 16:03:13 +0000 (UTC) Received: from localhost.localdomain (c-24-9-64-241.hsd1.co.comcast.net [24.9.64.241]) by osg.samsung.com (Postfix) with ESMTPSA id EB30FA05F0; Wed, 5 Apr 2017 16:03:11 +0000 (UTC) From: Shuah Khan To: linux@armlinux.org.uk, gregkh@linuxfoundation.org, pawel@osciak.com, m.szyprowski@samsung.com, kyungmin.park@samsung.com, mchehab@kernel.org Subject: [PATCH] arm: dma: fix sharing of coherent DMA memory without struct page Date: Wed, 5 Apr 2017 10:02:42 -0600 Message-Id: <20170405160242.14195-1-shuahkh@osg.samsung.com> X-Mailer: git-send-email 2.9.3 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170405_120316_551061_E0E0AC40 X-CRM114-Status: GOOD ( 28.32 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: will.deacon@arm.com, ming.l@ssi.samsung.com, festevam@gmail.com, sagi@grimberg.me, vinod.koul@intel.com, Shuah Khan , krzk@kernel.org, javier@osg.samsung.com, dledford@redhat.com, javier@dowhile0.org, linux-media@vger.kernel.org, alexander.h.duyck@intel.com, jroedel@suse.de, acourbot@nvidia.com, gregory.clement@free-electrons.com, linux-arm-kernel@lists.infradead.org, niklas.soderlund+renesas@ragnatech.se, martin.petersen@oracle.com, Robin.Murphy@arm.com, linux-kernel@vger.kernel.org, andrew.smirnov@gmail.com, mauricfo@linux.vnet.ibm.com, bart.vanassche@sandisk.com, sricharan@codeaurora.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP When coherent DMA memory without struct page is shared, importer fails to find the page and runs into kernel page fault when it tries to dmabuf_ops_attach/map_sg/map_page the invalid page found in the sg_table. Please see www.spinics.net/lists/stable/msg164204.html for more information on this problem. This solution allows coherent DMA memory without struct page to be shared by providing a way for the exporter to tag the DMA buffer as a special buffer without struct page association and passing the information in sg_table to the importer. This information is used in attach/map_sg to avoid cleaning D-cache and mapping. The details of the change are: Framework: - Add a new dma_attrs field to struct scatterlist. - Add a new DMA_ATTR_DEV_COHERENT_NOPAGE attribute to clearly identify Coherent memory without struct page. - Add a new dma_check_dev_coherent() interface to check if memory is the device coherent area. There is no way to tell where the memory returned by dma_alloc_attrs() came from. Exporter logic: - Add logic to vb2_dc_alloc() to call dma_check_dev_coherent() and set DMA_ATTR_DEV_COHERENT_NOPAGE based the results of the check. This is done in the exporter context. - Add logic to arm_dma_get_sgtable() to identify memory without struct page using DMA_ATTR_DEV_COHERENT_NOPAGE attribute. If this attr is set, arm_dma_get_sgtable() will set page as the cpu_addr and update dma_address and dma_attrs fields in struct scatterlist for this sgl. This is done in exporter context when buffer is exported. With this Note: This change is made on top of Russell King's patch that added !pfn_valid(pfn) check to arm_dma_get_sgtable() to error out on invalid pages. Coherent memory without struct page will trigger this error. Importer logic: - Add logic to vb2_dc_dmabuf_ops_attach() to identify memory without struct page using DMA_ATTR_DEV_COHERENT_NOPAGE attribute when it copies the sg_table from the exporter. It will copy dma_attrs and dma_address fields. With this logic, dmabuf_ops_attach will no longer trip on an invalid page. - Add logic to arm_dma_map_sg() to avoid mapping the page when sg_table has DMA_ATTR_DEV_COHERENT_NOPAGE buffer. - Add logic to arm_dma_unmap_sg() to do nothing for sg entries with DMA_ATTR_DEV_COHERENT_NOPAGE attribute. Without this change the following use-case that runs into kernel pagefault when importer tries to attach the exported buffer. With this change it works: (what a relief after watching pagefaults for weeks!!) gst-launch-1.0 filesrc location=~/GH3_MOV_HD.mp4 ! qtdemux ! h264parse ! v4l2video4dec capture-io-mode=dmabuf ! v4l2video7convert output-io-mode=dmabuf-import ! kmssink force-modesetting=true I am sending RFC patch to get feedback on the approach and see if I missed anything. Signed-off-by: Shuah Khan --- arch/arm/mm/dma-mapping.c | 34 ++++++++++++++++++++++---- drivers/base/dma-coherent.c | 25 +++++++++++++++++++ drivers/media/v4l2-core/videobuf2-dma-contig.c | 6 +++++ include/linux/dma-mapping.h | 8 ++++++ include/linux/scatterlist.h | 1 + 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c index 63eabb0..75c6692 100644 --- a/arch/arm/mm/dma-mapping.c +++ b/arch/arm/mm/dma-mapping.c @@ -939,13 +939,28 @@ int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt, void *cpu_addr, dma_addr_t handle, size_t size, unsigned long attrs) { - struct page *page = pfn_to_page(dma_to_pfn(dev, handle)); + unsigned long pfn = dma_to_pfn(dev, handle); + struct page *page; int ret; + /* If the PFN is not valid, we do not have a struct page. */ + if (!pfn_valid(pfn)) { + /* If memory is from per-device coherent area, use cpu_addr */ + if (attrs & DMA_ATTR_DEV_COHERENT_NOPAGE) + page = cpu_addr; + else + return -ENXIO; + } else + page = pfn_to_page(pfn); + ret = sg_alloc_table(sgt, 1, GFP_KERNEL); if (unlikely(ret)) return ret; + if (attrs & DMA_ATTR_DEV_COHERENT_NOPAGE) + sgt->sgl->dma_address = handle; + + sgt->sgl->dma_attrs = attrs; sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); return 0; } @@ -1080,10 +1095,17 @@ int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, #ifdef CONFIG_NEED_SG_DMA_LENGTH s->dma_length = s->length; #endif - s->dma_address = ops->map_page(dev, sg_page(s), s->offset, + /* + * there is no struct page for this DMA buffer. + * s->dma_address is the handle + */ + if (!(s->dma_attrs & DMA_ATTR_DEV_COHERENT_NOPAGE)) { + s->dma_address = ops->map_page(dev, sg_page(s), + s->offset, s->length, dir, attrs); - if (dma_mapping_error(dev, s->dma_address)) - goto bad_mapping; + if (dma_mapping_error(dev, s->dma_address)) + goto bad_mapping; + } } return nents; @@ -1112,7 +1134,9 @@ void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, int i; for_each_sg(sg, s, nents, i) - ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs); + if (!(s->dma_attrs & DMA_ATTR_DEV_COHERENT_NOPAGE)) + ops->unmap_page(dev, sg_dma_address(s), + sg_dma_len(s), dir, attrs); } /** diff --git a/drivers/base/dma-coherent.c b/drivers/base/dma-coherent.c index 640a7e6..d08cf44 100644 --- a/drivers/base/dma-coherent.c +++ b/drivers/base/dma-coherent.c @@ -209,6 +209,31 @@ int dma_alloc_from_coherent(struct device *dev, ssize_t size, EXPORT_SYMBOL(dma_alloc_from_coherent); /** + * dma_check_dev_coherent() - checks if memory is from the device coherent area + * + * @dev: device whose coherent area is checked to validate memory + * @dma_handle: dma handle associated with the allocated memory + * @vaddr: the virtual address to the allocated area. + * + * Returns true if memory does belong to the per-device cohrent area. + * false otherwise. + */ +bool dma_check_dev_coherent(struct device *dev, dma_addr_t dma_handle, + void *vaddr) +{ + struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL; + + if (mem && vaddr >= mem->virt_base && + vaddr < (mem->virt_base + (mem->size << PAGE_SHIFT)) && + dma_handle >= mem->device_base && + dma_handle < (mem->device_base + (mem->size << PAGE_SHIFT))) + return true; + + return false; +} +EXPORT_SYMBOL(dma_check_dev_coherent); + +/** * dma_release_from_coherent() - try to free the memory allocated from per-device coherent memory pool * @dev: device from which the memory was allocated * @order: the order of pages allocated diff --git a/drivers/media/v4l2-core/videobuf2-dma-contig.c b/drivers/media/v4l2-core/videobuf2-dma-contig.c index fb6a177..f7caf2b 100644 --- a/drivers/media/v4l2-core/videobuf2-dma-contig.c +++ b/drivers/media/v4l2-core/videobuf2-dma-contig.c @@ -161,6 +161,9 @@ static void *vb2_dc_alloc(struct device *dev, unsigned long attrs, if ((buf->attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) buf->vaddr = buf->cookie; + if (dma_check_dev_coherent(dev, buf->dma_addr, buf->cookie)) + buf->attrs |= DMA_ATTR_DEV_COHERENT_NOPAGE; + /* Prevent the device from being released while the buffer is used */ buf->dev = get_device(dev); buf->size = size; @@ -248,6 +251,9 @@ static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev, rd = buf->sgt_base->sgl; wr = sgt->sgl; for (i = 0; i < sgt->orig_nents; ++i) { + if (rd->dma_attrs & DMA_ATTR_DEV_COHERENT_NOPAGE) + wr->dma_address = rd->dma_address; + wr->dma_attrs = rd->dma_attrs; sg_set_page(wr, sg_page(rd), rd->length, rd->offset); rd = sg_next(rd); wr = sg_next(wr); diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 0977317..9f3ec53 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -70,6 +70,12 @@ #define DMA_ATTR_PRIVILEGED (1UL << 9) /* + * DMA_ATTR_DEV_COHERENT_NOPAGE: This is a hint to the DMA-mapping sub-system + * that this memory isn't backed by struct page. + */ +#define DMA_ATTR_DEV_COHERENT_NOPAGE (1UL << 10) + +/* * A dma_addr_t can hold any valid DMA or bus address for the platform. * It can be given to a device to use as a DMA source or target. A CPU cannot * reference a dma_addr_t directly because there may be translation between @@ -160,6 +166,8 @@ static inline int is_device_dma_capable(struct device *dev) */ int dma_alloc_from_coherent(struct device *dev, ssize_t size, dma_addr_t *dma_handle, void **ret); +bool dma_check_dev_coherent(struct device *dev, dma_addr_t dma_handle, + void *vaddr); int dma_release_from_coherent(struct device *dev, int order, void *vaddr); int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma, diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index cb3c8fe..7da610b 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -18,6 +18,7 @@ struct scatterlist { #ifdef CONFIG_NEED_SG_DMA_LENGTH unsigned int dma_length; #endif + unsigned long dma_attrs; }; /*