From patchwork Thu Apr 13 21:47:56 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shuah Khan X-Patchwork-Id: 9680045 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 0D222601C3 for ; Thu, 13 Apr 2017 21:48:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F11D528471 for ; Thu, 13 Apr 2017 21:48:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E55EC27C05; Thu, 13 Apr 2017 21:48:54 +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 6B7B127C05 for ; Thu, 13 Apr 2017 21:48:54 +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=C2zEhm7qpUNoyOy9yyV3rDGawlABViKfQXtI+us6xfY=; b=oJ7 oX5rrxAdQZh62VXBSQgx4/5gAyU8S1RZs8l3nhI6aghI/ZSPVFNwkLE6dRpqKhYi3s/fZhjlvjgTx 8VRKR0jDu1QKKkKy6WkGJMwuNzAP+H1eovdmMyVdIKZVLhxVYwhC2DuMPncfiA8FihTXZ7mX7C6Hp Sx3ohxla7Nad6PdugosybSmJfL2TUA5O+lbXY4qu4a3WVqzKm3jMYsmMv4HBNT+vlUxQoBkYn3XkK Y9H54s6l3SB1dINXtH04/7hsewzi3NgVv4yFs1/BHN550T9wfgJ+feS8X/FFVNYYUqFqeNStTxvAB 0vBOJiQ70XuUhYm4fVQ0MgWiQcpzEdA==; 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 1cymbj-0005Wl-Ng; Thu, 13 Apr 2017 21:48:51 +0000 Received: from ec2-52-27-115-49.us-west-2.compute.amazonaws.com ([52.27.115.49] helo=osg.samsung.com) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cymbf-0005VL-VT for linux-arm-kernel@lists.infradead.org; Thu, 13 Apr 2017 21:48:50 +0000 Received: from localhost (localhost [127.0.0.1]) by osg.samsung.com (Postfix) with ESMTP id 1F0ECA05F1; Thu, 13 Apr 2017 21:48:50 +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 ZMznU1jLN0u2; Thu, 13 Apr 2017 21:48:49 +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 22C46A05EC; Thu, 13 Apr 2017 21:48:48 +0000 (UTC) From: Shuah Khan To: linux@armlinux.org.uk, gregkh@linuxfoundation.org Subject: [PATCH] ARM: dma-mapping: add check for coherent DMA memory without struct page Date: Thu, 13 Apr 2017 15:47:56 -0600 Message-Id: <20170413214756.21265-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-20170413_144848_063336_79C02E69 X-CRM114-Status: GOOD ( 16.66 ) 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: alexander.h.duyck@intel.com, niklas.soderlund+renesas@ragnatech.se, jroedel@suse.de, vinod.koul@intel.com, vgupta@synopsys.com, will.deacon@arm.com, Shuah Khan , krzk@kernel.org, linux-kernel@vger.kernel.org, dledford@redhat.com, mauricfo@linux.vnet.ibm.com, benjamin.gaignard@linaro.org, sricharan@codeaurora.org, bart.vanassche@sandisk.com, akpm@linux-foundation.org, Robin.Murphy@arm.com, linux-arm-kernel@lists.infradead.org, m.szyprowski@samsung.com 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. Add a new dma_check_dev_coherent() interface to check if memory is from the device coherent area. There is no way to tell where the memory returned by dma_alloc_attrs() came from. arm_dma_get_sgtable() checks for invalid pages, however this check could pass even for memory obtained the coherent allocator. Add an additional check to call dma_check_dev_coherent() to confirm that it is indeed the coherent DMA memory and fail the sgtable creation with -EINVAL. Signed-off-by: Shuah Khan --- arch/arm/mm/dma-mapping.c | 11 ++++++++--- drivers/base/dma-coherent.c | 25 +++++++++++++++++++++++++ include/linux/dma-mapping.h | 2 ++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c index 475811f..27c7d9a 100644 --- a/arch/arm/mm/dma-mapping.c +++ b/arch/arm/mm/dma-mapping.c @@ -954,9 +954,14 @@ int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt, struct page *page; int ret; - /* If the PFN is not valid, we do not have a struct page */ - if (!pfn_valid(pfn)) - return -ENXIO; + /* + * If the PFN is not valid, we do not have a struct page + * As this check can pass even for memory obtained through + * the coherent allocator, do an additional check to determine + * if this is coherent DMA memory. + */ + if (!pfn_valid(pfn) && dma_check_dev_coherent(dev, handle, cpu_addr)) + return -EINVAL; page = pfn_to_page(pfn); 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/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 0977317..b10e70d 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -160,6 +160,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,