Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp4687313yba; Mon, 20 May 2019 01:50:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqx8iyi8ZDdRcu7+nMDBGiLpgJ4zrn7slMTgFqmhHo9E0nu13zbRlK76iznE3IzLIdd9RxQ3 X-Received: by 2002:a17:902:6683:: with SMTP id e3mr67383671plk.291.1558342246439; Mon, 20 May 2019 01:50:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558342246; cv=none; d=google.com; s=arc-20160816; b=bTDdoyd0qxbdW1jfELPN1Mam6dI7b6HlH4U7hfCIqvvTenscaJvQuNSTKCQOhoCY4N Q26Rt7TUjmaCeI2N2yqhlwpeajOxR97rFQ4RvmLCzdkrLsNnKfL3B8IE9IWBX/DUEpSQ nXu2TezYnpU/AJwV82abV2CA7skLVak8fI+dgcSwxed4XgstpWtmfm9jrqhucIEZxZJr 90esiUzuKr+2MDggBL6TPu/YhUxjwbv5IvIxM5acepADN65jaMsiJrysh2ZRIpWROCfP uPcSkfwnWvBbRUuqJdrWTHlnE2N0UdSTOw6s7bv5ES/9G1rxeYozK/3lbt8VRS/oIFby CxxA== 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=Xz7XOfQtMgoZk4uVprpgAOsWsuEpNmNUA5R9KSuMpZY=; b=JhPof1ZSKMGCnUlglAEXHCg6O9K7J4EvDahiMCX8u5I19rR9wrHyr6FYkH9v+k2zSi v/bezZdXSJr9PdW8AU/VewFH2G8qU0TIiqoahJtvVSM4C4NlK6yxOcmYV29qnAKlR1YD JxvZaWf5s+TcFI7I1ATSTjs+hysOmzRuluYKxRHsVeiUg5YiEYdQkgSbDWG1GZvvjDCJ UWaljCGqyw3eo8jhm8Xa73wWBJc7J5xihz+tBfQnXVLXfEiYE/nsHJ/DqSxFW9V7VbEu WOctFd/3sKnzQUuMFvcpY+2hPMMc8CILRUmCMBe3lymwy902Y7dopoCd23aqEdH0Z0Fl E1vQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=RtxKlzUi; 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 y8si17227384pgo.168.2019.05.20.01.50.31; Mon, 20 May 2019 01:50:46 -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=RtxKlzUi; 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 S1731035AbfETHbe (ORCPT + 99 others); Mon, 20 May 2019 03:31:34 -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