From patchwork Tue Apr 25 17:57:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Marinas X-Patchwork-Id: 9698877 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 5601C60245 for ; Tue, 25 Apr 2017 17:58:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 451F0284DA for ; Tue, 25 Apr 2017 17:58:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 39520284EE; Tue, 25 Apr 2017 17:58:18 +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 C3A91284DA for ; Tue, 25 Apr 2017 17:58:17 +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=smlTHjfAWCWh8CXnNyLtx0XsALV8R+qZf9Bdw5icsgk=; b=Iza 4PrFxMxqKx7hECpPfgcD/8FE3qZHOsUXV5tyvbi+b4T0kyCRk49hl/CkyY2juuSxW88bHsC76WDrZ Rw9SPsRUlWs58thLezmSysEJckslCd5Kwa4Aub3kWz55dmRlIUwkGAev4DZMEV6wDX6zOIJGqbcRz MgVyFLVsmwLXqxCUq2hwttjK1trjOVIIHhFt5UV4/JpXTLjiWfKma67tZ/YFZpOWbuF4CBMohPj5C DseThLPPyCT+zoVQ8371zbvIWcSD9t46Sl/9rV+K2lKkpkhtNDZZZCUew/2+aaInNJbBwmUU/w0o6 8vtUSt8AC3Sp92V813yJMUNF7fHiXQA==; 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 1d34jB-0001Kh-Ap; Tue, 25 Apr 2017 17:58:17 +0000 Received: from foss.arm.com ([217.140.101.70]) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1d34j7-00016L-KL for linux-arm-kernel@lists.infradead.org; Tue, 25 Apr 2017 17:58:15 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 872741682; Tue, 25 Apr 2017 10:57:50 -0700 (PDT) Received: from e104818-lin.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 1C3783F4FF; Tue, 25 Apr 2017 10:57:48 -0700 (PDT) From: Catalin Marinas To: linux-kernel@vger.kernel.org Subject: [RFC PATCH] drivers: dma-mapping: Do not attempt to create a scatterlist for from_coherent buffers Date: Tue, 25 Apr 2017 18:57:39 +0100 Message-Id: <1493143059-2113-1-git-send-email-catalin.marinas@arm.com> X-Mailer: git-send-email 2.1.4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170425_105813_672071_E270675B X-CRM114-Status: GOOD ( 11.88 ) 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: Russell King - ARM Linux , Greg Kroah-Hartman , a.hajda@samsung.com, geert@linux-m68k.org, robin.murphy@arm.com, linux-arm-kernel@lists.infradead.org, Marek Szyprowski 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 Memory returned by dma_alloc_from_coherent() is not backed by struct page and creating a scatterlist would use invalid page pointers. The patch introduces the dma_vaddr_from_coherent() function and the corresponding check in dma_get_sgtable_attrs(). Fixes: d2b7428eb0ca ("common: dma-mapping: introduce dma_get_sgtable() function") Cc: Marek Szyprowski Cc: Greg Kroah-Hartman Cc: Russell King - ARM Linux Signed-off-by: Catalin Marinas --- In a recent discussion around the iommu DMA ops on arm64, Russell pointed out that dma_get_sgtable is not safe since the coherent DMA memory is not always backed by struct page. Russell has queued an arm-specific patch checking for pfn_valid() but I thought I'd make a more generic fix. This patch aims to bring the dma_get_sgtable() API in line with the dma_alloc/mmap/free with respect to the from_coherent memory. drivers/base/dma-coherent.c | 9 +++++++++ include/linux/dma-mapping.h | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/drivers/base/dma-coherent.c b/drivers/base/dma-coherent.c index 640a7e63c453..1d2cdbefb850 100644 --- a/drivers/base/dma-coherent.c +++ b/drivers/base/dma-coherent.c @@ -279,6 +279,15 @@ int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma, } EXPORT_SYMBOL(dma_mmap_from_coherent); +int dma_vaddr_from_coherent(struct device *dev, void *vaddr, size_t size) +{ + struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL; + + return mem && vaddr >= mem->virt_base && + vaddr + size <= (mem->virt_base + (mem->size << PAGE_SHIFT)); +} +EXPORT_SYMBOL(dma_vaddr_from_coherent); + /* * Support for reserved memory regions defined in device tree */ diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 0977317c6835..4dc99c6db184 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -164,10 +164,12 @@ 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, void *cpu_addr, size_t size, int *ret); +int dma_vaddr_from_coherent(struct device *dev, void *vaddr, size_t size); #else #define dma_alloc_from_coherent(dev, size, handle, ret) (0) #define dma_release_from_coherent(dev, order, vaddr) (0) #define dma_mmap_from_coherent(dev, vma, vaddr, order, ret) (0) +#define dma_vaddr_from_coherent(dev, vaddr, size) (0) #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */ #ifdef CONFIG_HAS_DMA @@ -461,6 +463,9 @@ dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr, { const struct dma_map_ops *ops = get_dma_ops(dev); BUG_ON(!ops); + /* dma_alloc_from_coherent() memory is not backed by struct page */ + if (dma_vaddr_from_coherent(dev, cpu_addr, size)) + return -ENXIO; if (ops->get_sgtable) return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size, attrs);