Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2682436yba; Mon, 22 Apr 2019 11:03:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqwZ+gRNztTrv1NYmmaWyy8lyLcJJEEKM0q3wclCXUVVsj2Aho2YC3AmGkRmNzG4kk+y56Wl X-Received: by 2002:a65:62c3:: with SMTP id m3mr20469171pgv.159.1555956235946; Mon, 22 Apr 2019 11:03:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555956235; cv=none; d=google.com; s=arc-20160816; b=p3wm9Gk0bauqWWDljNFcyuWgcXgChopxqabmzgMeOK9TfCiypGrjFzUE6QQuMZvwwG V1RS14wts1o8vtC9MRVuskNXa0bkp3ZcqFuYp0pheThUARQDZNnIBKdrufD8BIuS/vtf YMg0okXqOo/tauaJmFx/f3Zp0jGN54eRplQG989gU9V8lMYfwpqdx4rdaACRYOd9glix k67pFfOqrfAwFojRFm6JHqV0zyhejYdodHIaU3aWm6lO1YlKk3hlMJgsmlibD9rWM9Ic vdmznEE+hB1hsyTWTUfyzi3K9p0AT5WcJMo6NoV+k7almHSsnkeE7DtdXAZmQTqUjYhU VpRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=NOoBXWwkJOm8VP4xxXLmI1zFdDQEeSeAfGSIL5/M4YY=; b=IgS2XNum6eYjLcdJaFvbE76aHZOijtE31pBBGEUpSMwQr8anLvp7MhmVIABOQBWr8Z eEX02G90kshiFT7F9SvHEiLzWXezwEkFJX/g14qAtMb4yJDLud7ER+mjKf3Wd2yO+NBA XCyVw3ud/St59mmSceUZYgNN58pFmcaMMOwQ8YOiT5Z15JkmbQp68XSQL1bJc8Mk3RY+ 2SgQuD4FiQDDszGLWKXJmZQKOQi80DvNORHZ+8WTpzIlwrI1Cpa4Oye/7SqBndlTQA3v I6+w8mgxIzWojEJuaIHYysWyC5esSY17EYUTsnfqVi1acioFmYhIkHkW/UuEzBJiHowU Y8zw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=X6ATbSUQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h189si14125900pfc.283.2019.04.22.11.03.40; Mon, 22 Apr 2019 11:03:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=X6ATbSUQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728444AbfDVSCG (ORCPT + 99 others); Mon, 22 Apr 2019 14:02:06 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:38090 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728519AbfDVSAr (ORCPT ); Mon, 22 Apr 2019 14:00:47 -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=NOoBXWwkJOm8VP4xxXLmI1zFdDQEeSeAfGSIL5/M4YY=; b=X6ATbSUQbCHaG71O+XYtn9i/mV qTnzBIuTapEW2Gcou1ZQefLkhJDsfeRjOeP+7HTR8ruPZE187xHzS7ZaVVU5mvDSXYqKoU+kczfs6 3qvF/iGW2RF14KwhxWA2RDt5XeevbD0jivRbmMep0uxwGhisR6iCnTopwzYuEkZ/geTsW7m3qmfdI c3H/hU3wT25oLgu02ZGmtwkgPQpvQVSRQew+iXV3lCUYxxiS8RZ7iK6x+nGAE2k9D5LY+ywLmr0DT /MMib8/pVrridNUxnAgA9e+I43CBT5qdiN+wCoK/UGkNE2bkXa+Lzz85SaVzj6hoGQ3KBAzizXb2W mUj8Y+DA==; Received: from 213-225-37-80.nat.highway.a1.net ([213.225.37.80] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hIdFA-00020t-4f; Mon, 22 Apr 2019 18:00:40 +0000 From: Christoph Hellwig To: Robin Murphy Cc: Joerg Roedel , Catalin Marinas , Will Deacon , Tom Lendacky , iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 12/26] iommu/dma: Refactor the page array remapping allocator Date: Mon, 22 Apr 2019 19:59:28 +0200 Message-Id: <20190422175942.18788-13-hch@lst.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190422175942.18788-1-hch@lst.de> References: <20190422175942.18788-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 Move the call to dma_common_pages_remap into __iommu_dma_alloc and rename it to iommu_dma_alloc_remap. This creates a self-contained helper for remapped pages allocation and mapping. Signed-off-by: Christoph Hellwig --- drivers/iommu/dma-iommu.c | 54 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 8e2d9733113e..b8e46e89a60a 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -535,9 +535,9 @@ static struct page **__iommu_dma_get_pages(void *cpu_addr) } /** - * iommu_dma_free - Free a buffer allocated by __iommu_dma_alloc() + * iommu_dma_free - Free a buffer allocated by iommu_dma_alloc_remap() * @dev: Device which owns this buffer - * @pages: Array of buffer pages as returned by __iommu_dma_alloc() + * @pages: Array of buffer pages as returned by __iommu_dma_alloc_remap() * @size: Size of buffer in bytes * @handle: DMA address of buffer * @@ -553,33 +553,35 @@ static void __iommu_dma_free(struct device *dev, struct page **pages, } /** - * __iommu_dma_alloc - Allocate and map a buffer contiguous in IOVA space + * iommu_dma_alloc_remap - Allocate and map a buffer contiguous in IOVA space * @dev: Device to allocate memory for. Must be a real device * attached to an iommu_dma_domain * @size: Size of buffer in bytes + * @dma_handle: Out argument for allocated DMA handle * @gfp: Allocation flags * @attrs: DMA attributes for this allocation - * @prot: IOMMU mapping flags - * @handle: Out argument for allocated DMA handle * * If @size is less than PAGE_SIZE, then a full CPU page will be allocated, * but an IOMMU which supports smaller pages might not map the whole thing. * - * Return: Array of struct page pointers describing the buffer, - * or NULL on failure. + * Return: Mapped virtual address, or NULL on failure. */ -static struct page **__iommu_dma_alloc(struct device *dev, size_t size, - gfp_t gfp, unsigned long attrs, int prot, dma_addr_t *handle) +static void *iommu_dma_alloc_remap(struct device *dev, size_t size, + dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) { struct iommu_domain *domain = iommu_get_dma_domain(dev); struct iommu_dma_cookie *cookie = domain->iova_cookie; struct iova_domain *iovad = &cookie->iovad; + bool coherent = dev_is_dma_coherent(dev); + int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs); + pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs); + unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap; struct page **pages; struct sg_table sgt; dma_addr_t iova; - unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap; + void *vaddr; - *handle = DMA_MAPPING_ERROR; + *dma_handle = DMA_MAPPING_ERROR; min_size = alloc_sizes & -alloc_sizes; if (min_size < PAGE_SIZE) { @@ -605,7 +607,7 @@ static struct page **__iommu_dma_alloc(struct device *dev, size_t size, if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, GFP_KERNEL)) goto out_free_iova; - if (!(prot & IOMMU_CACHE)) { + if (!(ioprot & IOMMU_CACHE)) { struct scatterlist *sg; int i; @@ -613,14 +615,21 @@ static struct page **__iommu_dma_alloc(struct device *dev, size_t size, arch_dma_prep_coherent(sg_page(sg), sg->length); } - if (iommu_map_sg(domain, iova, sgt.sgl, sgt.orig_nents, prot) + if (iommu_map_sg(domain, iova, sgt.sgl, sgt.orig_nents, ioprot) < size) goto out_free_sg; - *handle = iova; + vaddr = dma_common_pages_remap(pages, size, VM_USERMAP, prot, + __builtin_return_address(0)); + if (!vaddr) + goto out_unmap; + + *dma_handle = iova; sg_free_table(&sgt); - return pages; + return vaddr; +out_unmap: + __iommu_dma_unmap(dev, iova, size); out_free_sg: sg_free_table(&sgt); out_free_iova: @@ -989,18 +998,7 @@ static void *iommu_dma_alloc(struct device *dev, size_t size, size >> PAGE_SHIFT); } } else { - pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs); - struct page **pages; - - pages = __iommu_dma_alloc(dev, iosize, gfp, attrs, ioprot, - handle); - if (!pages) - return NULL; - - addr = dma_common_pages_remap(pages, size, VM_USERMAP, prot, - __builtin_return_address(0)); - if (!addr) - __iommu_dma_free(dev, pages, iosize, handle); + addr = iommu_dma_alloc_remap(dev, iosize, handle, gfp, attrs); } return addr; } @@ -1014,7 +1012,7 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, /* * @cpu_addr will be one of 4 things depending on how it was allocated: * - A remapped array of pages for contiguous allocations. - * - A remapped array of pages from __iommu_dma_alloc(), for all + * - A remapped array of pages from iommu_dma_alloc_remap(), for all * non-atomic allocations. * - A non-cacheable alias from the atomic pool, for atomic * allocations by non-coherent devices. -- 2.20.1