Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6230984ybi; Wed, 31 Jul 2019 10:31:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqxJi5hMyrJ4RA5zF2TyX7jqPmg17Pmmqba8HBM/HRA9FxurRACor4lmMRJKiz2vjbe0uKZg X-Received: by 2002:a63:904:: with SMTP id 4mr41856030pgj.19.1564594316794; Wed, 31 Jul 2019 10:31:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564594316; cv=none; d=google.com; s=arc-20160816; b=tgTwH22S7XF7wEGyxGBDQIgVp02it5Uxh+UjSYacsBDuKJR0o66nJJgyv1OcGEFIe9 gFLXedq+isyXL8SuLQwG2Xf/54aI12amcE9JLBhnYZ3inGU9ktd6aCr3emyWkrfIWIx9 XQMDsaN0sOWKw2LZ9bfI1HFPgkQafZifwKC1wjhN9cdVJBQlbe7ypwqrbPY4jRhVs4YZ mXLX1Ze/mpG2izb67YtPrnnwkzovieIPLehY/PCNl43ZjSzhfPBZDvoa0An5iKLKdNb7 W9YDmmI7C/eV8u9nIc456wCKSQA/oSMMdmI6Rl/D5+VSpjV8EUvIMetj+ttArWzRd8Bi 482Q== 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=bKIWoBO1Ox9foRE7zQy0IoCXQXSDJQsZ/wpbYRrt7O8=; b=b410r4rT4xCLQCB58tdFEWVJxQj/ywg5+NMSZeYyq7lul8V4h6QYJK9SiamN4YmLbG BaiGGeicdHNTZVKWAezx4lEarXHr2uW1ZuHQMzwtUU3U/eU9LTjAf1In2AJXkIREyQb4 QCk0zRiQKGQd4wKx4i9E1+n3XVS1FoYKU4W9ff969VK12u2o0lqZlb+C3TT6JN9FhXTJ 8PewdYBPBfPV34YpAru06n+iBWBeL144G6rpxhLnPj+iXgTA/uqpfs68hknwUbzuCA6O nQjkuvssDbYF33+zUEo9K2fLg7wvLs+FPLrXdn3dY0R2vfCvDTl90fFBY/+HIO6PFB2g 4rTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b="PbSjR/3Y"; 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 y16si35360967pgj.169.2019.07.31.10.31.41; Wed, 31 Jul 2019 10:31:56 -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=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b="PbSjR/3Y"; 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 S2388700AbfGaPIr (ORCPT + 99 others); Wed, 31 Jul 2019 11:08:47 -0400 Received: from mail-ed1-f65.google.com ([209.85.208.65]:44816 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388648AbfGaPIe (ORCPT ); Wed, 31 Jul 2019 11:08:34 -0400 Received: by mail-ed1-f65.google.com with SMTP id k8so65978519edr.11 for ; Wed, 31 Jul 2019 08:08:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bKIWoBO1Ox9foRE7zQy0IoCXQXSDJQsZ/wpbYRrt7O8=; b=PbSjR/3YSpT3z0VTol0zIBTaptMjGhUYM3HUndjM3neCQkCTkww5qEep158FrhMPDB lOja6cV3oSHoyK5HuysL26jeiqkfqfvW5YCcm+LVastLopDh0evznn9Jq/ft27Nk0yls rnC1AKEeGTuwcFgRdFljMrIygrLWBDFyhvLXjbwshxlOfCLSeM8F5NTRRHEv0Ie5JSY0 b1aiesbbVNHw4aazLYgwIMzT8ab5pWkVI/0jSeHw4sNpg9XZaJV+IqQWo3AWpb2CYE/1 0iRZiF3T3Y4zZUpFAx0MzBe/Qihdixe290PHhrcx6T5Kzu6+jBCiV+w7prbZ/t0zig7E KLeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bKIWoBO1Ox9foRE7zQy0IoCXQXSDJQsZ/wpbYRrt7O8=; b=pyVvfQQLtzXkq8nZx+l8S0VOTj9pW8pIrhPbQyrHNl4Wl1h02xCa1Cn8/lINW3NUaq qOixcK8CMVy6Ehmm7pFhPDR1jrJngqv9NB3O8rknYvHxmDgOhCYnP4GiYfyTSdoLk6sf nSax15fwVB/8NDKVRhxDkSsRRFX74Pm3lVyoLf61PnJ5fj2Hmu9dpCNBhJf0TbnqnyLK PobnbbTuIgRrPvrk0vwRgEEG7k9ygGt1OQeqV5C01n5aRymPt9AmbGuDBRl2B2kK8c/M CGiwss089+XRp7vp2iK9AE7IipoCYtZohpKfv1BisglMGmREjTqj8iQL2Hw4GrJaI0vG EDow== X-Gm-Message-State: APjAAAVWU1nPHDLQSyTvywPi8jMcdjvFlq/UHrXnrsdAMqBiBxY7aoMn Q7ivR38qs9edNYKSAORbj6k= X-Received: by 2002:a05:6402:145a:: with SMTP id d26mr107237799edx.10.1564585712394; Wed, 31 Jul 2019 08:08:32 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id g7sm16942446eda.52.2019.07.31.08.08.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 08:08:30 -0700 (PDT) From: "Kirill A. Shutemov" X-Google-Original-From: "Kirill A. Shutemov" Received: by box.localdomain (Postfix, from userid 1000) id 57277104836; Wed, 31 Jul 2019 18:08:17 +0300 (+03) To: Andrew Morton , x86@kernel.org, Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Borislav Petkov , Peter Zijlstra , Andy Lutomirski , David Howells Cc: Kees Cook , Dave Hansen , Kai Huang , Jacob Pan , Alison Schofield , linux-mm@kvack.org, kvm@vger.kernel.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org, "Kirill A . Shutemov" Subject: [PATCHv2 50/59] x86/mm: Use common code for DMA memory encryption Date: Wed, 31 Jul 2019 18:08:04 +0300 Message-Id: <20190731150813.26289-51-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> References: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jacob Pan Replace sme_ code with x86 memory encryption common code such that Intel MKTME can be supported underneath generic DMA code. dma_to_phys() & phys_to_dma() results will be runtime modified by memory encryption code. Signed-off-by: Jacob Pan Signed-off-by: Kirill A. Shutemov --- arch/x86/include/asm/mem_encrypt.h | 29 +++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt_common.c | 2 +- include/linux/dma-direct.h | 4 ++-- include/linux/mem_encrypt.h | 23 ++++++++++------------- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h index 0c196c47d621..62a1493f389c 100644 --- a/arch/x86/include/asm/mem_encrypt.h +++ b/arch/x86/include/asm/mem_encrypt.h @@ -52,8 +52,19 @@ bool sev_active(void); #define __bss_decrypted __attribute__((__section__(".bss..decrypted"))) +/* + * The __sme_set() and __sme_clr() macros are useful for adding or removing + * the encryption mask from a value (e.g. when dealing with pagetable + * entries). + */ +#define __sme_set(x) ((x) | sme_me_mask) +#define __sme_clr(x) ((x) & ~sme_me_mask) + #else /* !CONFIG_AMD_MEM_ENCRYPT */ +#define __sme_set(x) (x) +#define __sme_clr(x) (x) + #define sme_me_mask 0ULL static inline void __init sme_early_encrypt(resource_size_t paddr, @@ -94,4 +105,22 @@ extern char __start_bss_decrypted[], __end_bss_decrypted[], __start_bss_decrypte #endif /* __ASSEMBLY__ */ +#ifdef CONFIG_X86_MEM_ENCRYPT_COMMON + +extern dma_addr_t __mem_encrypt_dma_set(dma_addr_t daddr, phys_addr_t paddr); +extern phys_addr_t __mem_encrypt_dma_clear(phys_addr_t paddr); + +#else +static inline dma_addr_t __mem_encrypt_dma_set(dma_addr_t daddr, phys_addr_t paddr) +{ + return daddr; +} + +static inline phys_addr_t __mem_encrypt_dma_clear(phys_addr_t paddr) +{ + return paddr; +} +#endif /* CONFIG_X86_MEM_ENCRYPT_COMMON */ + + #endif /* __X86_MEM_ENCRYPT_H__ */ diff --git a/arch/x86/mm/mem_encrypt_common.c b/arch/x86/mm/mem_encrypt_common.c index c11d70151735..588d6ea45624 100644 --- a/arch/x86/mm/mem_encrypt_common.c +++ b/arch/x86/mm/mem_encrypt_common.c @@ -1,6 +1,6 @@ #include -#include #include +#include #include /* diff --git a/include/linux/dma-direct.h b/include/linux/dma-direct.h index adf993a3bd58..6ce96b06c440 100644 --- a/include/linux/dma-direct.h +++ b/include/linux/dma-direct.h @@ -49,12 +49,12 @@ static inline bool force_dma_unencrypted(struct device *dev) */ static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) { - return __sme_set(__phys_to_dma(dev, paddr)); + return __mem_encrypt_dma_set(__phys_to_dma(dev, paddr), paddr); } static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) { - return __sme_clr(__dma_to_phys(dev, daddr)); + return __mem_encrypt_dma_clear(__dma_to_phys(dev, daddr)); } u64 dma_direct_get_required_mask(struct device *dev); diff --git a/include/linux/mem_encrypt.h b/include/linux/mem_encrypt.h index 470bd53a89df..88724aa7c065 100644 --- a/include/linux/mem_encrypt.h +++ b/include/linux/mem_encrypt.h @@ -23,6 +23,16 @@ static inline bool sme_active(void) { return false; } static inline bool sev_active(void) { return false; } +static inline dma_addr_t __mem_encrypt_dma_set(dma_addr_t daddr, phys_addr_t paddr) +{ + return daddr; +} + +static inline phys_addr_t __mem_encrypt_dma_clear(phys_addr_t paddr) +{ + return paddr; +} + #endif /* CONFIG_ARCH_HAS_MEM_ENCRYPT */ static inline bool mem_encrypt_active(void) @@ -35,19 +45,6 @@ static inline u64 sme_get_me_mask(void) return sme_me_mask; } -#ifdef CONFIG_AMD_MEM_ENCRYPT -/* - * The __sme_set() and __sme_clr() macros are useful for adding or removing - * the encryption mask from a value (e.g. when dealing with pagetable - * entries). - */ -#define __sme_set(x) ((x) | sme_me_mask) -#define __sme_clr(x) ((x) & ~sme_me_mask) -#else -#define __sme_set(x) (x) -#define __sme_clr(x) (x) -#endif - #endif /* __ASSEMBLY__ */ #endif /* __MEM_ENCRYPT_H__ */ -- 2.21.0