Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3692759yba; Mon, 29 Apr 2019 07:02:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqy+Mb4KdNgXdzqmtgXDKY4uRL411syN4g76ZAhfETInk42FzCBt61u2qYqqDnvrYeIl5ldi X-Received: by 2002:a63:5466:: with SMTP id e38mr59957592pgm.340.1556546571922; Mon, 29 Apr 2019 07:02:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556546571; cv=none; d=google.com; s=arc-20160816; b=tLqNNVVRJZ964fDLVr9C9S0jNTR/v86jXiCr4L/sdTKEVNDq88Yb4VY71KwMpNrGOq //AMDS3jcsNqjZGHk3Gk+2Z6SJ9qpUfr9GSEy6F197jxdwYeLx4cJ3NKnnfvtrCc4Jh5 e5rP+zYrClhj7+L9eajXP1tpISqtSSyy5HU5+Bx/3OAaMTvjGMoYIOIDhI8aaI8TkND7 WOBoUGOL7uPD5/DBf2tqxXAYSzlObU9jMJVAttmeOvq554CrwlhTRlaO1bhBv7k/WQMK dGnIj+uTn8EuQ2wwCB+IneiA1snPS2ZPpSG7T+MWWNxmTlATLonIKsja5uq1eMJXR8mC kRMQ== 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=73g/bsXH6xstwbzXFBKiAib2ZlOJUvL35cLydF3OPRY=; b=A1sK5X0h1IPKFR1TJxQSgh2hsHmxqXVK2eQMHZ44siWOemxqkcki4eF/u1dN8BJUFd xgmaktXwC0OnNWEi7b8aJjUYfCOYu51i3pn5W8+b3xlXGvlrWUnJR4pQpCRrmI0+baSn oOuxf68NKvRfREuOhKLWVO7SAFwMzcsuunfj+t4x4JVIOn3jsLzg3ceTkRhn87UWO9WA LubAGbZyZEtQaBQXPPkV9s/EXYzPGPlZzZ9pIE3F083evBKZZSZOz1gK+Jcg3wCfaKKE PRlWy32CIZ9GRYXJLI27xBT1PXNHfiB0RWmW/HS4YCWdPXoADqmWrMBJ5yU0W3LqeWlJ mQig== 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 v7si26769019pgs.387.2019.04.29.07.02.32; Mon, 29 Apr 2019 07:02:51 -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 S1728267AbfD2N7r (ORCPT + 99 others); Mon, 29 Apr 2019 09:59:47 -0400 Received: from foss.arm.com ([217.140.101.70]:57654 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726321AbfD2N7q (ORCPT ); Mon, 29 Apr 2019 09:59:46 -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 47E23A78; Mon, 29 Apr 2019 06:59:46 -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 D67293F5C1; Mon, 29 Apr 2019 06:59:44 -0700 (PDT) Subject: Re: [PATCH 14/26] iommu/dma: Refactor iommu_dma_free 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-15-hch@lst.de> From: Robin Murphy Message-ID: <8321a363-f448-3e48-48f6-58d2b44a2900@arm.com> Date: Mon, 29 Apr 2019 14:59:43 +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-15-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: > From: Robin Murphy > > The freeing logic was made particularly horrible by part of it being > opaque to the arch wrapper, which led to a lot of convoluted repetition > to ensure each path did everything in the right order. Now that it's > all private, we can pick apart and consolidate the logically-distinct > steps of freeing the IOMMU mapping, the underlying pages, and the CPU > remap (if necessary) into something much more manageable. > > Signed-off-by: Robin Murphy > [various cosmetic changes to the code flow] Hmm, I do still prefer my original flow with the dma_common_free_remap() call right out of the way at the end rather than being a special case in the middle of all the page-freeing (which is the kind of existing complexity I was trying to eliminate). I guess you've done this to avoid having "if (!dma_release_from_contiguous(...))..." twice like I ended up with, which is fair enough I suppose - once we manage to solve the new dma_{alloc,free}_contiguous() interface that may tip the balance so I can always revisit this then. > Signed-off-by: Christoph Hellwig > --- > drivers/iommu/dma-iommu.c | 75 ++++++++++++++++++--------------------- > 1 file changed, 35 insertions(+), 40 deletions(-) > > diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c > index 4632b9d301a1..9658c4cc3cfe 100644 > --- a/drivers/iommu/dma-iommu.c > +++ b/drivers/iommu/dma-iommu.c > @@ -916,6 +916,41 @@ static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle, > __iommu_dma_unmap(dev, handle, size); > } > > +static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, > + dma_addr_t handle, unsigned long attrs) > +{ > + size_t alloc_size = PAGE_ALIGN(size); > + int count = alloc_size >> PAGE_SHIFT; > + struct page *page = NULL; > + > + __iommu_dma_unmap(dev, handle, size); > + > + /* Non-coherent atomic allocation? Easy */ > + if (dma_free_from_pool(cpu_addr, alloc_size)) > + return; > + > + if (is_vmalloc_addr(cpu_addr)) { > + /* > + * If it the address is remapped, then it's either non-coherent s/If it/If/ My "easy; more involved; most complex" narrative definitely doesn't translate very well to a different order :) Otherwise, Reluctantly-Acked-by: Robin Murphy > + * or highmem CMA, or an iommu_dma_alloc_remap() construction. > + */ > + struct page **pages = __iommu_dma_get_pages(cpu_addr); > + > + if (pages) > + __iommu_dma_free_pages(pages, count); > + else > + page = vmalloc_to_page(cpu_addr); > + > + dma_common_free_remap(cpu_addr, alloc_size, VM_USERMAP); > + } else { > + /* Lowmem means a coherent atomic or CMA allocation */ > + page = virt_to_page(cpu_addr); > + } > + > + if (page && !dma_release_from_contiguous(dev, page, count)) > + __free_pages(page, get_order(alloc_size)); > +} > + > static void *iommu_dma_alloc(struct device *dev, size_t size, > dma_addr_t *handle, gfp_t gfp, unsigned long attrs) > { > @@ -985,46 +1020,6 @@ static void *iommu_dma_alloc(struct device *dev, size_t size, > return addr; > } > > -static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, > - dma_addr_t handle, unsigned long attrs) > -{ > - size_t iosize = size; > - > - size = PAGE_ALIGN(size); > - /* > - * @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_remap(), for all > - * non-atomic allocations. > - * - A non-cacheable alias from the atomic pool, for atomic > - * allocations by non-coherent devices. > - * - A normal lowmem address, for atomic allocations by > - * coherent devices. > - * Hence how dodgy the below logic looks... > - */ > - if (dma_in_atomic_pool(cpu_addr, size)) { > - __iommu_dma_unmap(dev, handle, iosize); > - dma_free_from_pool(cpu_addr, size); > - } else if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) { > - struct page *page = vmalloc_to_page(cpu_addr); > - > - __iommu_dma_unmap(dev, handle, iosize); > - dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT); > - dma_common_free_remap(cpu_addr, size, VM_USERMAP); > - } else if (is_vmalloc_addr(cpu_addr)){ > - struct page **pages = __iommu_dma_get_pages(cpu_addr); > - > - if (WARN_ON(!pages)) > - return; > - __iommu_dma_unmap(dev, handle, iosize); > - __iommu_dma_free_pages(pages, size >> PAGE_SHIFT); > - dma_common_free_remap(cpu_addr, size, VM_USERMAP); > - } else { > - __iommu_dma_unmap(dev, handle, iosize); > - __free_pages(virt_to_page(cpu_addr), get_order(size)); > - } > -} > - > static int __iommu_dma_mmap_pfn(struct vm_area_struct *vma, > unsigned long pfn, size_t size) > { >