Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp597877ybz; Wed, 15 Apr 2020 14:51:24 -0700 (PDT) X-Google-Smtp-Source: APiQypLAL+VYkFmW8KBx69mMKTdvbL7Bb8HVXGmKGJVjSM82ytBWib/IONuN316BpKbMB6Wtn7DN X-Received: by 2002:a50:9e29:: with SMTP id z38mr27604249ede.345.1586987484287; Wed, 15 Apr 2020 14:51:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586987484; cv=none; d=google.com; s=arc-20160816; b=yC6gw3NGsSUhTtADtZ9blXm3YqTxcAjsTMXnWCsqfWIrM5qhr+Iaa5BCJXaYZceo16 k2N3KRdA+ZVRo0/FZE8PRzAjpHMhDIIB1pfCIkAUFq9Hyc2e2sFuSAXiX65wiyk2bPXg oXs9HaJlWA82rPt+HMktWVDiJh0P4Oc+CbUaj6fQvIw8ZUeO9U9257YI6tG8DcMK9jHr 7lzlvv7QPaNBfX+UxfaaJPcLmsUP+QkoZiLPRWvOmSDMqG09tgnPAux2BCh3rIJ7ba+I dAxfm0TaUDPmECjVRIe5AeZqCSdQIAcJvuRJ/pFPw1Q8oFm6f7fpgViFgau7/j018uO+ WfXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date:dkim-signature; bh=IlUPdhKtIbUzJOWhzH8G7CEIltrnGC+nIP0WHGWZMgU=; b=P3jZJ0p0Zw0ld+H8nwenMrgw7EZr7NXL7pk14cfi0IjbZXaecv97HSoIdqFoxZuyTv Txzq1NCWkB/wPb90Lktu++MZ7y+i+kBCwCVpYBW5QM9P6sdmtbLBB9bSXj1iC2qqIG9W g6kBhmhh8pXb78YW3vd/uA5uZO7OgrvE5b6XnNkigihLR5ulE0l425xVr6I+0kiH2L2l LP75nci+IbUQple++ik/xYJbz/Pp8sdUtFOsTLlVA6hw/UKQ0IgbnyUIYyGaI60/T9ge HV30cBmyhtNitojm+jjM/AsYKFE4CgXVJiChFBJ8IQnooKtYrQwLIwaPWcdjtJVHt+bU cpxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=VdPvHOue; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o19si5292668ejm.258.2020.04.15.14.51.01; Wed, 15 Apr 2020 14:51:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=VdPvHOue; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2504818AbgDNTbE (ORCPT + 99 others); Tue, 14 Apr 2020 15:31:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S2504776AbgDNT3u (ORCPT ); Tue, 14 Apr 2020 15:29:50 -0400 Received: from mail-pl1-x641.google.com (mail-pl1-x641.google.com [IPv6:2607:f8b0:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40EF1C02C444 for ; Tue, 14 Apr 2020 12:20:43 -0700 (PDT) Received: by mail-pl1-x641.google.com with SMTP id z6so299831plk.10 for ; Tue, 14 Apr 2020 12:20:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=IlUPdhKtIbUzJOWhzH8G7CEIltrnGC+nIP0WHGWZMgU=; b=VdPvHOuedrUPrbOlD0+nUakV+M1CzUEwuJFqiS3Xx7oaGbOw2harQFQq1U59pVYsiG +QAD+SkkXgVARYN0MSOc0QwaB2Jgy8v3XTXlIsRTdRDSV8yk9zZHAQ2y4jvFiEyz3XHE 7YXQrKCLBOBDCC/2P8bENLJ4utU8LnGnCk131xMn2eazCAg5NFPkJcVTAkcrtjoNjt8o umW7WurDWUYEqIfZJ68QphhKbw9EaCZb9BBq6Je3u0xwLEcXVEn2jxMK+RQ5r4Q0twdT 2IuNMJY0g08zNEFIWsZWpLucJ4rmre2MUBIKozG0Ui0kmYPsm860qwPiHHd0LeKrhfpt CKQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=IlUPdhKtIbUzJOWhzH8G7CEIltrnGC+nIP0WHGWZMgU=; b=n0NRigkDbGTy8GBXo3xnTciGz4jIktHnUSL+bSs3ZbJM02TGH3K0AII6ypC3pJsJIB ZypRHOVIIkAx2Riyo/iBdysttx5SM2RTXMM0os3MghuiWhS2dDMgfUnzINSU7YZ8SnNm DmNGlVohsB8zYIRvT7jJZ92LubW4CEChCe4OEwvTKdNiLu+79inhQyu8F5F/FjV8ccHN orc/7qxuOi6dLp/B/K70hRpSjxhYq3J4O7ArZ5tP2RsRsb0EGzj3zMGJIgdCq9hi1+iU Wqamxl5IdOgQ2lbqwVq3OfEmpv85/BLlO0K1SX6hsVEcB6Fic/OPFwPoOnF+kBul2pTa xnAA== X-Gm-Message-State: AGi0Puaj7MzAI0Xf4xWBXVfunjZp+sXY7dfu5tvE7ygQ3cyF1946JJPK LdlQDjUjkvJbm6GWHWEwteI/686Lr1w= X-Received: by 2002:a17:90a:3086:: with SMTP id h6mr1912285pjb.49.1586892042521; Tue, 14 Apr 2020 12:20:42 -0700 (PDT) Received: from [2620:15c:17:3:3a5:23a7:5e32:4598] ([2620:15c:17:3:3a5:23a7:5e32:4598]) by smtp.gmail.com with ESMTPSA id a1sm11692054pfl.188.2020.04.14.12.20.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2020 12:20:41 -0700 (PDT) Date: Tue, 14 Apr 2020 12:20:41 -0700 (PDT) From: David Rientjes X-X-Sender: rientjes@chino.kir.corp.google.com To: Christoph Hellwig cc: Tom Lendacky , "Singh, Brijesh" , "Grimm, Jon" , Joerg Roedel , "linux-kernel@vger.kernel.org" , "iommu@lists.linux-foundation.org" Subject: Re: [rfc v2 4/6] dma-direct: atomic allocations must come from atomic coherent pools In-Reply-To: <20200414064332.GB23359@lst.de> Message-ID: References: <20200414064332.GB23359@lst.de> User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 14 Apr 2020, Christoph Hellwig wrote: > > + /* > > + * Unencrypted memory must come directly from DMA atomic pools if > > + * blocking is not allowed. > > + */ > > + if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) && > > + force_dma_unencrypted(dev) && !gfpflags_allow_blocking(gfp)) { > > + ret = dma_alloc_from_pool(dev, PAGE_ALIGN(size), &page, gfp); > > + if (!ret) > > + return NULL; > > + goto done; > > + } > > + > > if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && > > dma_alloc_need_uncached(dev, attrs) && > > !gfpflags_allow_blocking(gfp)) { > > Can we keep a single conditional for the pool allocations? Maybe > add a new dma_alloc_from_pool helper ala: > > static inline bool dma_alloc_from_pool(struct device *dev, gfp_t gfp) > { > if (!IS_ENABLED(CONFIG_DMA_COHERENT_POOL)) > return false; > if (gfpflags_allow_blocking(gfp)) > return false; > if (force_dma_unencrypted(dev)) > return true; > if (dma_alloc_need_uncached(dev)) > return true; > } Looks good, fixed. I renamed it to dma_should_alloc_from_pool() to avoid confusing it with the actual allocation function and added a dma_should_free_from_pool() as well. diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -75,6 +75,39 @@ static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size) min_not_zero(dev->coherent_dma_mask, dev->bus_dma_limit); } +/* + * Decrypting memory is allowed to block, so if this device requires + * unencrypted memory it must come from atomic pools. + */ +static inline bool dma_should_alloc_from_pool(struct device *dev, gfp_t gfp, + unsigned long attrs) +{ + if (!IS_ENABLED(CONFIG_DMA_COHERENTPOOL)) + return false; + if (gfpflags_allow_blocking(gfp)) + return false; + if (force_dma_unencrypted(dev)) + return true; + if (!IS_ENABLED(CONFIG_DMA_DIRECT_REMAP)) + return false; + if (dma_alloc_need_uncached(dev, attrs)) + return true; + return false; +} + +static inline bool dma_should_free_from_pool(struct device *dev, + unsigned long attrs) +{ + if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL)) + return true; + if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) && + !force_dma_unencrypted(dev)) + return false; + if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP)) + return true; + return false; +} + struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, gfp_t gfp, unsigned long attrs) { @@ -124,9 +157,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, struct page *page; void *ret; - if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && - dma_alloc_need_uncached(dev, attrs) && - !gfpflags_allow_blocking(gfp)) { + if (dma_should_alloc_from_pool(dev, gfp, attrs)) { ret = dma_alloc_from_pool(dev, PAGE_ALIGN(size), &page, gfp); if (!ret) return NULL; @@ -202,6 +233,11 @@ void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr, { unsigned int page_order = get_order(size); + /* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */ + if (dma_should_free_from_pool(dev, attrs) && + dma_free_from_pool(dev, cpu_addr, PAGE_ALIGN(size))) + return; + if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) && !force_dma_unencrypted(dev)) { /* cpu_addr is a struct page cookie, not a kernel address */ @@ -209,10 +245,6 @@ void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr, return; } - if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && - dma_free_from_pool(dev, cpu_addr, PAGE_ALIGN(size))) - return; - if (force_dma_unencrypted(dev)) set_memory_encrypted((unsigned long)cpu_addr, 1 << page_order);