From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1EA22C04AAF for ; Mon, 20 May 2019 07:31:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E7F69206B6 for ; Mon, 20 May 2019 07:31:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="RtxKlzUi" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730714AbfETHbf (ORCPT ); Mon, 20 May 2019 03:31:35 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:47224 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730458AbfETHbd (ORCPT ); Mon, 20 May 2019 03:31:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From :Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=Xz7XOfQtMgoZk4uVprpgAOsWsuEpNmNUA5R9KSuMpZY=; b=RtxKlzUiUnmwhsm9Rjf7Spv5eh 2LNf1LcNfr1615C1KopG9WPL+FAWYShEVYkXnDn58hB6Fqlm4KdVFbYMWsL5ol5y9LLyE+3Z6uNcm i2sojhxrjIeEEfXiNHXN/LRmwOSpFmccPtnHb1heLwErLDc1iYM/f6wfATita0IPydj2Dwyiodxbd exoLQ5/NX3dqhoMwKWVKHFx6BTxG5UjvsC3hR7pzGXFti2K6EsdE7A9TrqXeftMrWaaswTGw3HyBj SyN4lDrYnOztjYzxRtFvCDxVQcOClZ5jEABzewj1y9EptqSAdyn7BsOjZ5j41vl7ZZRdZwrwmK4wa 66KBjT6g==; Received: from 089144206147.atnat0015.highway.bob.at ([89.144.206.147] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hSclb-0004hd-AR; Mon, 20 May 2019 07:31:27 +0000 From: Christoph Hellwig To: Robin Murphy Cc: Joerg Roedel , Catalin Marinas , Will Deacon , Tom Murphy , iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 18/24] iommu/dma: Refactor iommu_dma_alloc, part 2 Date: Mon, 20 May 2019 09:29:42 +0200 Message-Id: <20190520072948.11412-19-hch@lst.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190520072948.11412-1-hch@lst.de> References: <20190520072948.11412-1-hch@lst.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org All the logic in iommu_dma_alloc that deals with page allocation from the CMA or page allocators can be split into a self-contained helper, and we can than map the result of that or the atomic pool allocation with the iommu later. This also allows reusing __iommu_dma_free to tear down the allocations and MMU mappings when the IOMMU mapping fails. Based on a patch from Robin Murphy. Signed-off-by: Christoph Hellwig --- drivers/iommu/dma-iommu.c | 65 +++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 4e27a29f4458..84761adbb1d4 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -972,35 +972,14 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, __iommu_dma_free(dev, size, cpu_addr); } -static void *iommu_dma_alloc(struct device *dev, size_t size, - dma_addr_t *handle, gfp_t gfp, unsigned long attrs) +static void *iommu_dma_alloc_pages(struct device *dev, size_t size, + struct page **pagep, gfp_t gfp, unsigned long attrs) { bool coherent = dev_is_dma_coherent(dev); - int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs); size_t alloc_size = PAGE_ALIGN(size); struct page *page = NULL; void *cpu_addr; - gfp |= __GFP_ZERO; - - if (gfpflags_allow_blocking(gfp) && - !(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) - return iommu_dma_alloc_remap(dev, size, handle, gfp, attrs); - - if (!gfpflags_allow_blocking(gfp) && !coherent) { - cpu_addr = dma_alloc_from_pool(alloc_size, &page, gfp); - if (!cpu_addr) - return NULL; - - *handle = __iommu_dma_map(dev, page_to_phys(page), size, - ioprot); - if (*handle == DMA_MAPPING_ERROR) { - dma_free_from_pool(cpu_addr, alloc_size); - return NULL; - } - return cpu_addr; - } - if (gfpflags_allow_blocking(gfp)) page = dma_alloc_from_contiguous(dev, alloc_size >> PAGE_SHIFT, get_order(alloc_size), @@ -1010,33 +989,59 @@ static void *iommu_dma_alloc(struct device *dev, size_t size, if (!page) return NULL; - *handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot); - if (*handle == DMA_MAPPING_ERROR) - goto out_free_pages; - if (!coherent || PageHighMem(page)) { pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs); cpu_addr = dma_common_contiguous_remap(page, alloc_size, VM_USERMAP, prot, __builtin_return_address(0)); if (!cpu_addr) - goto out_unmap; + goto out_free_pages; if (!coherent) arch_dma_prep_coherent(page, size); } else { cpu_addr = page_address(page); } + + *pagep = page; memset(cpu_addr, 0, alloc_size); return cpu_addr; -out_unmap: - __iommu_dma_unmap(dev, *handle, size); out_free_pages: if (!dma_release_from_contiguous(dev, page, alloc_size >> PAGE_SHIFT)) __free_pages(page, get_order(alloc_size)); return NULL; } +static void *iommu_dma_alloc(struct device *dev, size_t size, + dma_addr_t *handle, gfp_t gfp, unsigned long attrs) +{ + bool coherent = dev_is_dma_coherent(dev); + int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs); + struct page *page = NULL; + void *cpu_addr; + + gfp |= __GFP_ZERO; + + if (gfpflags_allow_blocking(gfp) && + !(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) + return iommu_dma_alloc_remap(dev, size, handle, gfp, attrs); + + if (!gfpflags_allow_blocking(gfp) && !coherent) + cpu_addr = dma_alloc_from_pool(PAGE_ALIGN(size), &page, gfp); + else + cpu_addr = iommu_dma_alloc_pages(dev, size, &page, gfp, attrs); + if (!cpu_addr) + return NULL; + + *handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot); + if (*handle == DMA_MAPPING_ERROR) { + __iommu_dma_free(dev, size, cpu_addr); + return NULL; + } + + return cpu_addr; +} + static int __iommu_dma_mmap_pfn(struct vm_area_struct *vma, unsigned long pfn, size_t size) { -- 2.20.1 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31659C04AAF for ; Mon, 20 May 2019 07:32:46 +0000 (UTC) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 09FFB20656 for ; Mon, 20 May 2019 07:32:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="RtxKlzUi" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 09FFB20656 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=iommu-bounces@lists.linux-foundation.org Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 99719BA9; Mon, 20 May 2019 07:31:36 +0000 (UTC) Received: from smtp2.linuxfoundation.org (smtp2.linux-foundation.org [172.17.192.36]) by mail.linuxfoundation.org (Postfix) with ESMTPS id A1DD5A95 for ; Mon, 20 May 2019 07:31:34 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by smtp2.linuxfoundation.org (Postfix) with ESMTPS id 843B11DD62 for ; Mon, 20 May 2019 07:31:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From :Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=Xz7XOfQtMgoZk4uVprpgAOsWsuEpNmNUA5R9KSuMpZY=; b=RtxKlzUiUnmwhsm9Rjf7Spv5eh 2LNf1LcNfr1615C1KopG9WPL+FAWYShEVYkXnDn58hB6Fqlm4KdVFbYMWsL5ol5y9LLyE+3Z6uNcm i2sojhxrjIeEEfXiNHXN/LRmwOSpFmccPtnHb1heLwErLDc1iYM/f6wfATita0IPydj2Dwyiodxbd exoLQ5/NX3dqhoMwKWVKHFx6BTxG5UjvsC3hR7pzGXFti2K6EsdE7A9TrqXeftMrWaaswTGw3HyBj SyN4lDrYnOztjYzxRtFvCDxVQcOClZ5jEABzewj1y9EptqSAdyn7BsOjZ5j41vl7ZZRdZwrwmK4wa 66KBjT6g==; Received: from 089144206147.atnat0015.highway.bob.at ([89.144.206.147] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hSclb-0004hd-AR; Mon, 20 May 2019 07:31:27 +0000 From: Christoph Hellwig To: Robin Murphy Subject: [PATCH 18/24] iommu/dma: Refactor iommu_dma_alloc, part 2 Date: Mon, 20 May 2019 09:29:42 +0200 Message-Id: <20190520072948.11412-19-hch@lst.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190520072948.11412-1-hch@lst.de> References: <20190520072948.11412-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Cc: Tom Murphy , Catalin Marinas , Will Deacon , linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org X-BeenThere: iommu@lists.linux-foundation.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: Development issues for Linux IOMMU support List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: iommu-bounces@lists.linux-foundation.org Errors-To: iommu-bounces@lists.linux-foundation.org All the logic in iommu_dma_alloc that deals with page allocation from the CMA or page allocators can be split into a self-contained helper, and we can than map the result of that or the atomic pool allocation with the iommu later. This also allows reusing __iommu_dma_free to tear down the allocations and MMU mappings when the IOMMU mapping fails. Based on a patch from Robin Murphy. Signed-off-by: Christoph Hellwig --- drivers/iommu/dma-iommu.c | 65 +++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 4e27a29f4458..84761adbb1d4 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -972,35 +972,14 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, __iommu_dma_free(dev, size, cpu_addr); } -static void *iommu_dma_alloc(struct device *dev, size_t size, - dma_addr_t *handle, gfp_t gfp, unsigned long attrs) +static void *iommu_dma_alloc_pages(struct device *dev, size_t size, + struct page **pagep, gfp_t gfp, unsigned long attrs) { bool coherent = dev_is_dma_coherent(dev); - int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs); size_t alloc_size = PAGE_ALIGN(size); struct page *page = NULL; void *cpu_addr; - gfp |= __GFP_ZERO; - - if (gfpflags_allow_blocking(gfp) && - !(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) - return iommu_dma_alloc_remap(dev, size, handle, gfp, attrs); - - if (!gfpflags_allow_blocking(gfp) && !coherent) { - cpu_addr = dma_alloc_from_pool(alloc_size, &page, gfp); - if (!cpu_addr) - return NULL; - - *handle = __iommu_dma_map(dev, page_to_phys(page), size, - ioprot); - if (*handle == DMA_MAPPING_ERROR) { - dma_free_from_pool(cpu_addr, alloc_size); - return NULL; - } - return cpu_addr; - } - if (gfpflags_allow_blocking(gfp)) page = dma_alloc_from_contiguous(dev, alloc_size >> PAGE_SHIFT, get_order(alloc_size), @@ -1010,33 +989,59 @@ static void *iommu_dma_alloc(struct device *dev, size_t size, if (!page) return NULL; - *handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot); - if (*handle == DMA_MAPPING_ERROR) - goto out_free_pages; - if (!coherent || PageHighMem(page)) { pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs); cpu_addr = dma_common_contiguous_remap(page, alloc_size, VM_USERMAP, prot, __builtin_return_address(0)); if (!cpu_addr) - goto out_unmap; + goto out_free_pages; if (!coherent) arch_dma_prep_coherent(page, size); } else { cpu_addr = page_address(page); } + + *pagep = page; memset(cpu_addr, 0, alloc_size); return cpu_addr; -out_unmap: - __iommu_dma_unmap(dev, *handle, size); out_free_pages: if (!dma_release_from_contiguous(dev, page, alloc_size >> PAGE_SHIFT)) __free_pages(page, get_order(alloc_size)); return NULL; } +static void *iommu_dma_alloc(struct device *dev, size_t size, + dma_addr_t *handle, gfp_t gfp, unsigned long attrs) +{ + bool coherent = dev_is_dma_coherent(dev); + int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs); + struct page *page = NULL; + void *cpu_addr; + + gfp |= __GFP_ZERO; + + if (gfpflags_allow_blocking(gfp) && + !(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) + return iommu_dma_alloc_remap(dev, size, handle, gfp, attrs); + + if (!gfpflags_allow_blocking(gfp) && !coherent) + cpu_addr = dma_alloc_from_pool(PAGE_ALIGN(size), &page, gfp); + else + cpu_addr = iommu_dma_alloc_pages(dev, size, &page, gfp, attrs); + if (!cpu_addr) + return NULL; + + *handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot); + if (*handle == DMA_MAPPING_ERROR) { + __iommu_dma_free(dev, size, cpu_addr); + return NULL; + } + + return cpu_addr; +} + static int __iommu_dma_mmap_pfn(struct vm_area_struct *vma, unsigned long pfn, size_t size) { -- 2.20.1 _______________________________________________ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.0 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 677F5C04AAF for ; Mon, 20 May 2019 07:33:29 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 37C1C20656 for ; Mon, 20 May 2019 07:33:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="kSkY8gFB" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 37C1C20656 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: 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: List-Owner; bh=rIzQKe+WBy5xYz3d3Zjx17EZ53AIAyoOLS6JtOnhGoE=; b=kSkY8gFB7xidPi yLFZyjriXmJpS1ZoLnlwZ+vOBto4wQJVzbIkdt9nJz1s/+R7PwunQno/AkEGWX2FJyybvvDhN+wCj fxyr6wfTCAwpONjI5xfxCIZKOTleM5r0FAVBhW0aStXFiIoEYuxTIDE8ppffRAXAZf6L9tPJCnr2o +d9LajVhcS2jwXd0zOiTQbsyJ+dQ+bTjej2zBaFJJSQhwd7kGYaYnhBE+gW7st6iVDPnJcFNRb7Z4 PrJ1FMgOFHljHn4dw+ZQNvP0tGgLDJxYjqE6Vzb8mbDtZSQZeQj1PplFHdb++1dowzS3c6CLKGF60 T1ovrXaEXkfib1Xq6Yyw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hScnR-0006ns-Ma; Mon, 20 May 2019 07:33:21 +0000 Received: from 089144206147.atnat0015.highway.bob.at ([89.144.206.147] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hSclb-0004hd-AR; Mon, 20 May 2019 07:31:27 +0000 From: Christoph Hellwig To: Robin Murphy Subject: [PATCH 18/24] iommu/dma: Refactor iommu_dma_alloc, part 2 Date: Mon, 20 May 2019 09:29:42 +0200 Message-Id: <20190520072948.11412-19-hch@lst.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190520072948.11412-1-hch@lst.de> References: <20190520072948.11412-1-hch@lst.de> MIME-Version: 1.0 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: Tom Murphy , Catalin Marinas , Joerg Roedel , Will Deacon , linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org All the logic in iommu_dma_alloc that deals with page allocation from the CMA or page allocators can be split into a self-contained helper, and we can than map the result of that or the atomic pool allocation with the iommu later. This also allows reusing __iommu_dma_free to tear down the allocations and MMU mappings when the IOMMU mapping fails. Based on a patch from Robin Murphy. Signed-off-by: Christoph Hellwig --- drivers/iommu/dma-iommu.c | 65 +++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 4e27a29f4458..84761adbb1d4 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -972,35 +972,14 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, __iommu_dma_free(dev, size, cpu_addr); } -static void *iommu_dma_alloc(struct device *dev, size_t size, - dma_addr_t *handle, gfp_t gfp, unsigned long attrs) +static void *iommu_dma_alloc_pages(struct device *dev, size_t size, + struct page **pagep, gfp_t gfp, unsigned long attrs) { bool coherent = dev_is_dma_coherent(dev); - int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs); size_t alloc_size = PAGE_ALIGN(size); struct page *page = NULL; void *cpu_addr; - gfp |= __GFP_ZERO; - - if (gfpflags_allow_blocking(gfp) && - !(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) - return iommu_dma_alloc_remap(dev, size, handle, gfp, attrs); - - if (!gfpflags_allow_blocking(gfp) && !coherent) { - cpu_addr = dma_alloc_from_pool(alloc_size, &page, gfp); - if (!cpu_addr) - return NULL; - - *handle = __iommu_dma_map(dev, page_to_phys(page), size, - ioprot); - if (*handle == DMA_MAPPING_ERROR) { - dma_free_from_pool(cpu_addr, alloc_size); - return NULL; - } - return cpu_addr; - } - if (gfpflags_allow_blocking(gfp)) page = dma_alloc_from_contiguous(dev, alloc_size >> PAGE_SHIFT, get_order(alloc_size), @@ -1010,33 +989,59 @@ static void *iommu_dma_alloc(struct device *dev, size_t size, if (!page) return NULL; - *handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot); - if (*handle == DMA_MAPPING_ERROR) - goto out_free_pages; - if (!coherent || PageHighMem(page)) { pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs); cpu_addr = dma_common_contiguous_remap(page, alloc_size, VM_USERMAP, prot, __builtin_return_address(0)); if (!cpu_addr) - goto out_unmap; + goto out_free_pages; if (!coherent) arch_dma_prep_coherent(page, size); } else { cpu_addr = page_address(page); } + + *pagep = page; memset(cpu_addr, 0, alloc_size); return cpu_addr; -out_unmap: - __iommu_dma_unmap(dev, *handle, size); out_free_pages: if (!dma_release_from_contiguous(dev, page, alloc_size >> PAGE_SHIFT)) __free_pages(page, get_order(alloc_size)); return NULL; } +static void *iommu_dma_alloc(struct device *dev, size_t size, + dma_addr_t *handle, gfp_t gfp, unsigned long attrs) +{ + bool coherent = dev_is_dma_coherent(dev); + int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs); + struct page *page = NULL; + void *cpu_addr; + + gfp |= __GFP_ZERO; + + if (gfpflags_allow_blocking(gfp) && + !(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) + return iommu_dma_alloc_remap(dev, size, handle, gfp, attrs); + + if (!gfpflags_allow_blocking(gfp) && !coherent) + cpu_addr = dma_alloc_from_pool(PAGE_ALIGN(size), &page, gfp); + else + cpu_addr = iommu_dma_alloc_pages(dev, size, &page, gfp, attrs); + if (!cpu_addr) + return NULL; + + *handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot); + if (*handle == DMA_MAPPING_ERROR) { + __iommu_dma_free(dev, size, cpu_addr); + return NULL; + } + + return cpu_addr; +} + static int __iommu_dma_mmap_pfn(struct vm_area_struct *vma, unsigned long pfn, size_t size) { -- 2.20.1 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel