Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752514AbdGEHMe (ORCPT ); Wed, 5 Jul 2017 03:12:34 -0400 Received: from mail-pf0-f172.google.com ([209.85.192.172]:35606 "EHLO mail-pf0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752452AbdGEHM3 (ORCPT ); Wed, 5 Jul 2017 03:12:29 -0400 From: Tomasz Figa To: iommu@lists.linux-foundation.org Cc: linux-kernel@vger.kernel.org, Christoph Hellwig , Marek Szyprowski , Robin Murphy , Greg Kroah-Hartman , Joerg Roedel , Will Deacon , Vineet Gupta , Hans-Christian Noren Egtvedt , Mitchel Humpherys , Krzysztof Kozlowski , Arnd Bergmann , Tomasz Figa Subject: [RFC PATCH 2/5] base: dma-mapping: Provide a function to look up remapped pages Date: Wed, 5 Jul 2017 16:12:12 +0900 Message-Id: <20170705071215.17603-3-tfiga@chromium.org> X-Mailer: git-send-email 2.13.2.725.g09c95d1e9-goog In-Reply-To: <20170705071215.17603-1-tfiga@chromium.org> References: <20170705071215.17603-1-tfiga@chromium.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2262 Lines: 60 DMA API implementations, which use the dma_common_*() helpers, typically use them in pair with other helpers, such as iommu_dma_*(). For example, a typical .free() callback needs to retrieve the pages remapped earlier by dma_common_remap() and call iommu_dma_unmap() on them. Currently it is done by calling find_vm_area() manually, however it relies on implementation details of dma_common_remap() and is also difficult to expose to loadable modules, due to find_vm_area() being quite a low level function without its symbol exported. Improve this by providing a function to look-up the pages previously remapped. It hides implementation details, can do more sanity checks than find_vm_area() and can be exported for use in loadable modules. Signed-off-by: Tomasz Figa --- drivers/base/dma-mapping.c | 15 +++++++++++++++ include/linux/dma-mapping.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/drivers/base/dma-mapping.c b/drivers/base/dma-mapping.c index 1fda8df3d849..9add50dd7a08 100644 --- a/drivers/base/dma-mapping.c +++ b/drivers/base/dma-mapping.c @@ -335,6 +335,21 @@ void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags) vunmap(cpu_addr); } EXPORT_SYMBOL(dma_common_free_remap); + +struct page **dma_common_get_mapped_pages(void *cpu_addr, + unsigned long vm_flags) +{ + struct vm_struct *area = find_vm_area(cpu_addr); + + if (!area || (area->flags & vm_flags) != vm_flags) { + WARN(1, "trying to get pages for invalid coherent area: %p\n", + cpu_addr); + return NULL; + } + + return area->pages; +} +EXPORT_SYMBOL(dma_common_get_mapped_pages); #endif /* diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 843ab866e0f4..bd20435dac16 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -422,6 +422,8 @@ void *dma_common_pages_remap(struct page **pages, size_t size, unsigned long vm_flags, pgprot_t prot, const void *caller); void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags); +struct page **dma_common_get_mapped_pages(void *cpu_addr, + unsigned long vm_flags); /** * dma_mmap_attrs - map a coherent DMA allocation into user space -- 2.13.2.725.g09c95d1e9-goog