Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2681844yba; Mon, 22 Apr 2019 11:03:23 -0700 (PDT) X-Google-Smtp-Source: APXvYqxhFEjYOKy99qKApzrkMbSZIQ1MHWg1xknn5SY6Out1jRnyLSdFxxlT59s+DXdYjStuAKkz X-Received: by 2002:a17:902:2a6a:: with SMTP id i97mr21619608plb.273.1555956203609; Mon, 22 Apr 2019 11:03:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555956203; cv=none; d=google.com; s=arc-20160816; b=f/dVHYDj76VycX8XaQm+iCqaKE4quqYI42p2p9vVaHkM7zm0+I9hwSZr+hDQ+1A77v ZBKVtL6LeEknOyaTHOMjjoR3yShbNZLLbpiv9kvIGfGOTyutCmdJ6LCGQlsWq1cqnaVT pEcOSTijzCr1fVr7fV4rYr9ioVeYe9NaJI8WmxTsOFCsaY0PZhadh8vKalf5nBBuLCos f8z0gC8RlwiCbkiMvxgnQDd7f+577WqdHF/JGu6CaZ3uu4twSpARKdaMaMrew1si8+e5 VFgJOrPuUwvRuMtBzl4SnnOf0tuyAGXMDMisDwnRxMX11F6f7SoflY8Tpun+UHLtg0Ys +i+A== 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=XW/EX+1cKYLb8HO9zkOoHyXehbEvcLd17yPK0ZQQDNc=; b=AOZJNLaHgakkckN9qPdVujcKi+AzyXluH3y0muNKuosryArmNKHqdrVq6nSY2knSzN GbUQwi/tNs2oVkVIXBzq9G4jh5QSUiVBeqfD78GXCD5ZwuhG3oPfdpi0uPf3GUypWPzn UYwlabEny7gZveCNQ7FFkFa2qm26DvABjB5kGCkmCDTN3YXuxutkopzlQsUJpg0W2W7u wxuN/ucRpPQ8tVQ+Chuma1SgW1QV5SUoBSexovvGMhhu5rlFIReW91YT3F4ergOPsYtI fysnIq6SB8WEPGv0R2xALTa/V+ZbM7vWFc9tO17ysf5g5YKrsOPP2i76uoU3e01bY8qu 6x5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=OIktzgtG; 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 x3si13457301plo.274.2019.04.22.11.03.08; Mon, 22 Apr 2019 11:03:23 -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=OIktzgtG; 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 S1728579AbfDVSA4 (ORCPT + 99 others); Mon, 22 Apr 2019 14:00:56 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:38844 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728557AbfDVSAy (ORCPT ); Mon, 22 Apr 2019 14:00:54 -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=XW/EX+1cKYLb8HO9zkOoHyXehbEvcLd17yPK0ZQQDNc=; b=OIktzgtG/bUGU3SKFm4lWk5eVm Al3DAaIEe7jrrnbn/y4/3r1Jd3ivzEuESH6ebUBJ08B8t1Fefg7Nvn2zC8OMgS7G/7vDU/M0dF/Xf 84u4xVnP1s4aJfcrhClnB0ygni2k9vDenIL7pe4KPKMqHtqVu+Q68MbhJ8riK9UY9Q1Fo1A/EtSHp uzu4BldfRKUHM26TpK3wWvykRvyas9xIysnZuQMKkP0anbGWzdcO8HV3RsGWuAoRJ0Gr9UC5aUzH3 rsRZ9XWyWOMG6PgEWcyan7/0+Ng0n3PVu5KZTSU7116bmtc7Nab9xoAU9rxA/gupgWKevDi7p5XDv XUoKOPsw==; 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 1hIdFH-0002Ay-LA; Mon, 22 Apr 2019 18:00:48 +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 15/26] iommu/dma: Refactor iommu_dma_alloc Date: Mon, 22 Apr 2019 19:59:31 +0200 Message-Id: <20190422175942.18788-16-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 From: Robin Murphy Shuffle around the self-contained atomic and non-contiguous cases to return early and get out of the way of the CMA case that we're about to work on next. Signed-off-by: Robin Murphy [hch: slight changes to the code flow] Signed-off-by: Christoph Hellwig --- drivers/iommu/dma-iommu.c | 60 +++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 9658c4cc3cfe..504dd27312bb 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -956,14 +956,19 @@ static void *iommu_dma_alloc(struct device *dev, size_t size, { 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); size_t iosize = size; + struct page *page; void *addr; size = PAGE_ALIGN(size); gfp |= __GFP_ZERO; + if (gfpflags_allow_blocking(gfp) && + !(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) + return iommu_dma_alloc_remap(dev, iosize, handle, gfp, attrs); + if (!gfpflags_allow_blocking(gfp)) { - struct page *page; /* * In atomic context we can't remap anything, so we'll only * get the virtually contiguous buffer we need by way of a @@ -985,39 +990,34 @@ static void *iommu_dma_alloc(struct device *dev, size_t size, __free_pages(page, get_order(size)); else dma_free_from_pool(addr, size); - addr = NULL; - } - } else if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) { - pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs); - struct page *page; - - page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT, - get_order(size), gfp & __GFP_NOWARN); - if (!page) return NULL; - - *handle = __iommu_dma_map(dev, page_to_phys(page), iosize, ioprot); - if (*handle == DMA_MAPPING_ERROR) { - dma_release_from_contiguous(dev, page, - size >> PAGE_SHIFT); - return NULL; - } - addr = dma_common_contiguous_remap(page, size, VM_USERMAP, - prot, - __builtin_return_address(0)); - if (addr) { - if (!coherent) - arch_dma_prep_coherent(page, iosize); - memset(addr, 0, size); - } else { - __iommu_dma_unmap(dev, *handle, iosize); - dma_release_from_contiguous(dev, page, - size >> PAGE_SHIFT); } - } else { - addr = iommu_dma_alloc_remap(dev, iosize, handle, gfp, attrs); + return addr; } + + page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT, + get_order(size), gfp & __GFP_NOWARN); + if (!page) + return NULL; + + *handle = __iommu_dma_map(dev, page_to_phys(page), iosize, ioprot); + if (*handle == DMA_MAPPING_ERROR) + goto out_free_pages; + + addr = dma_common_contiguous_remap(page, size, VM_USERMAP, prot, + __builtin_return_address(0)); + if (!addr) + goto out_unmap; + + if (!coherent) + arch_dma_prep_coherent(page, iosize); + memset(addr, 0, size); return addr; +out_unmap: + __iommu_dma_unmap(dev, *handle, iosize); +out_free_pages: + dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT); + return NULL; } static int __iommu_dma_mmap_pfn(struct vm_area_struct *vma, -- 2.20.1