Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp1699601ybf; Sun, 1 Mar 2020 16:06:28 -0800 (PST) X-Google-Smtp-Source: APXvYqwECjXS+dikrjUojx4oUndC+GTxm+tjqmWwCLbU9N2HkPkNFoJY/m+1M5PbsYPOh9XAm8Nq X-Received: by 2002:a05:6830:160c:: with SMTP id g12mr11035631otr.82.1583107587924; Sun, 01 Mar 2020 16:06:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583107587; cv=none; d=google.com; s=arc-20160816; b=LjTpH7N+Ejc7tNcGMR5iesDgbd+8kAO05UtcrOLLM594mRkrCbtsfCD9RzbAgEr/PD LpG2EjoWcW76uemODiWEouCUe8wZKUlRaFTq9Mv1hbOe+DfIHdn9WiksC5RKAoAVIQ6f khfHp+XtYAbYAwP2LU41BkXTfHEXfVSBtMCZ68Qb4GFVA9vAklU6mCTi/NRvBI/O6CrE tsrsIgq4ZTUY3HK5grYC9A/EJd7lUoxBNom+s1TtvTW6bjCMedoTuCMJZ6t08ASGHxT4 phWCpzCham9qpNy1f8sSnSLk6Z3Y/TbZYKrdNDqPsJWNoJ0Zg6tmsL8S8AvAD6AW5tf+ s7Xg== 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=n2z26x3A40N8extJFoFR/1kjizwIJ2psa4vNOP6D4IU=; b=YySNnGRaxOcJRTUyrHL1h/UMROaCrzGXHmAj5EWUMT59BTSgjUWMoVzjzyFCZgIgC+ e3GUHgdMA1ucuUzN0oEZX/gbt62ECXovDhT/0Hcs9VfRhGyLQDphf6/6cecErlHEZF2D V5iQ5nW8BMCHquOIiGuMM99a4lNXuOtKqaKXrxNHQEERKh0iCLO6WBBkqhisGchGoI+Z McqwyhXvG4Ejw/2JKDe18O87toqettABcKin2XEnMpZ0HBM+i8Veck0bhXmsno/ChNrU 11OFerk9ut3Sd65vVFq2hVHRZPlsFh4nlbhQMC/lv8S0I8wcIrh868ozkBJb3CPgFbBt QMMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=RA+paHxh; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z24si2526765otq.201.2020.03.01.16.06.16; Sun, 01 Mar 2020 16:06:27 -0800 (PST) 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=pass header.i=@google.com header.s=20161025 header.b=RA+paHxh; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726942AbgCBAF1 (ORCPT + 99 others); Sun, 1 Mar 2020 19:05:27 -0500 Received: from mail-pg1-f195.google.com ([209.85.215.195]:40688 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726621AbgCBAF0 (ORCPT ); Sun, 1 Mar 2020 19:05:26 -0500 Received: by mail-pg1-f195.google.com with SMTP id t24so4496334pgj.7 for ; Sun, 01 Mar 2020 16:05:25 -0800 (PST) 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=n2z26x3A40N8extJFoFR/1kjizwIJ2psa4vNOP6D4IU=; b=RA+paHxh3lsDtcCFaWrHYBqPiCWIW0WA5M85Temkt10PhSH7s8uSgbzK2jSCkY0/Ey 65+HTUWwpzLIbNV5d8f9mANOlLt8GBZHN4f0PK1lB57I7rrlZB4osmoQVzS/RAWolHcF aoPWKzGp/2tJPRbhNf5i1nIcaWo8w184BOcH1BFWBwxmp4Ngf9yLyVWc7eNcBhRmFsx8 fbpx4uab0LQ8K2X6DRTpHF8YQyKCB77cKqAfc7yyzMArYe8twJNOFXTKJTunU8Uhrmot Dk0zitSoME6hyWL4qH+fI3kexaY2qur98n6s5vS80dmxOPpwnF005AQyRlzI/R5hRS1w eEiQ== 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=n2z26x3A40N8extJFoFR/1kjizwIJ2psa4vNOP6D4IU=; b=f8YRHQll4ZaaJBQegi71K/jhpvjicZGvgbxPYMm2QeectKuDy+CfkdCXkl9kOTGLrD yyoLUevDJcB+FUnvY4G5Bso3/M8nGQmNr1B/bsMNax2464EZFCSWBjBxCR56RaLoXfhU 3OMuhAgrh2LDoshwLD/4lQEmBIJr5GCCwuqEhLbPN1nhBKp9BV0KqjeaTn/fjXrjEYdF Wne3UJW0wY/lXeT9KyjV8/qAcSIjD5jh2oKnbSv/0JJaqhcFCfp78y1XW4EMzLDgaA1z ZzPAUcdouwscOEpJ2Wy/b16YQZNqCn7OnD80BJViBnL4CIuNNTxk1KAsLaT9rUKlJ7li 0q1g== X-Gm-Message-State: APjAAAUX+heRl+0FgvQJp2aoSXvSQwqZoKjaz10RSyelA4493vDGOocH XXmsaCpECzoOyFGKDx7PzbhfEw== X-Received: by 2002:a63:fc56:: with SMTP id r22mr16298681pgk.147.1583107524304; Sun, 01 Mar 2020 16:05:24 -0800 (PST) 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 q13sm18195897pgh.30.2020.03.01.16.05.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Mar 2020 16:05:23 -0800 (PST) Date: Sun, 1 Mar 2020 16:05:23 -0800 (PST) From: David Rientjes X-X-Sender: rientjes@chino.kir.corp.google.com To: Christoph Hellwig , Tom Lendacky cc: "Singh, Brijesh" , "Grimm, Jon" , Joerg Roedel , baekhw@google.com, "linux-kernel@vger.kernel.org" , "iommu@lists.linux-foundation.org" Subject: [rfc 5/6] dma-direct: atomic allocations must come from unencrypted pools In-Reply-To: Message-ID: References: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) 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 When AMD memory encryption is enabled, all non-blocking DMA allocations must originate from the atomic pools depending on the device and the gfp mask of the allocation. Keep all memory in these pools unencrypted. Signed-off-by: David Rientjes --- arch/x86/Kconfig | 1 + kernel/dma/direct.c | 9 ++++----- kernel/dma/remap.c | 2 ++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1523,6 +1523,7 @@ config X86_CPA_STATISTICS config AMD_MEM_ENCRYPT bool "AMD Secure Memory Encryption (SME) support" depends on X86_64 && CPU_SUP_AMD + select DMA_DIRECT_REMAP select DYNAMIC_PHYSICAL_MASK select ARCH_USE_MEMREMAP_PROT select ARCH_HAS_FORCE_DMA_UNENCRYPTED diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -125,7 +125,6 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, void *ret; if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && - dma_alloc_need_uncached(dev, attrs) && !gfpflags_allow_blocking(gfp)) { ret = dma_alloc_from_pool(dev, PAGE_ALIGN(size), &page, gfp); if (!ret) @@ -202,6 +201,10 @@ void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr, { unsigned int page_order = get_order(size); + if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && + 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 +212,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); diff --git a/kernel/dma/remap.c b/kernel/dma/remap.c --- a/kernel/dma/remap.c +++ b/kernel/dma/remap.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -141,6 +142,7 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, if (!addr) goto free_page; + set_memory_decrypted((unsigned long)page_to_virt(page), nr_pages); ret = gen_pool_add_virt(pool, (unsigned long)addr, page_to_phys(page), pool_size, NUMA_NO_NODE); if (ret)