Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3642219yba; Mon, 29 Apr 2019 06:12:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqxOVZXwHirEpGHE/BkDP/TQEGZIRfNXHvwDaqUU1kocRk4mhcxd49msbXrr7sFBgk5SXo2I X-Received: by 2002:a17:902:1d4a:: with SMTP id u10mr35539023plu.272.1556543538639; Mon, 29 Apr 2019 06:12:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556543538; cv=none; d=google.com; s=arc-20160816; b=j7ZXkpVZQL9b/R06YciF1WLa9Ek0KiuuOYJ/mzb1LuGepFB/IAcGyESWFmi4n/WdgK 7Hryh4YBB3wRJpQcRHnsR8ebDeBnsHi9GNBNabJf5k5nNj8feTaEffNjBGnGbBj2CMq8 /VQVoeeRIESKozChuLkYcuqSXeewDPvHtjUnwDlWfHpuKmmfjn48R70FcVVm/H4z/7Wi CUNb4948somXq/B44ofzyWqQCQ/fo3Z8QxNjMsioWDiSa4qwwtGgH3LzzdZrFN0usCZG pfhlJsbXnlUoDoM68KGnJZAmSAo9UM0modoAZ+PtZPMk+Ok5ul2iJyfvqwJaz3YmgZVc dnYA== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=l7MfJe6RR03C5PQt+q19O8XXRTdpC5+c4h8Hvrhs5ek=; b=z42/4OL8oZHIaA6MVSYUwYfPkN6W/8b1RyDqkgs20uMlQ6onziyor0qct0lIxK0aZZ y1qChcDTckgPZI2OLycz1Ox17Y2qWMkUULQfWOxi7yghTX9VtqvR6WvQPXR5XnKGPWJY B+X65pC72DbrLaB6+BtLhAD4zgw8IO5uj9uGpWav9i03oU6yYv4FV2iZAy7nQ7uiQuoe EE1s0VebXxi7wtDs8whBdnCEAulP2pdLkDGx7keUUpjjauau4NVNjCOd+Jh3c6mHqMUr cEDthgQzzH6+t3L4zfI36x0f55pFgGrEn9iBS7+pq3UYbu97fTvAlC0yNTScIWi3pgI8 mGAA== ARC-Authentication-Results: i=1; mx.google.com; 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 ci9si13579092plb.408.2019.04.29.06.12.03; Mon, 29 Apr 2019 06:12:18 -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; 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 S1728155AbfD2NKp (ORCPT + 99 others); Mon, 29 Apr 2019 09:10:45 -0400 Received: from foss.arm.com ([217.140.101.70]:56428 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725838AbfD2NKp (ORCPT ); Mon, 29 Apr 2019 09:10:45 -0400 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 8C948A78; Mon, 29 Apr 2019 06:10:44 -0700 (PDT) Received: from [10.1.196.75] (e110467-lin.cambridge.arm.com [10.1.196.75]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2716E3F71A; Mon, 29 Apr 2019 06:10:43 -0700 (PDT) Subject: Re: [PATCH 12/26] iommu/dma: Refactor the page array remapping allocator To: Christoph Hellwig 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 References: <20190422175942.18788-1-hch@lst.de> <20190422175942.18788-13-hch@lst.de> From: Robin Murphy Message-ID: <847e0d85-36c6-01d1-6547-5ca9d3f0931a@arm.com> Date: Mon, 29 Apr 2019 14:10:41 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20190422175942.18788-13-hch@lst.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 22/04/2019 18:59, Christoph Hellwig wrote: > 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. Reviewed-by: Robin Murphy > 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. >