Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp490520pxj; Thu, 3 Jun 2021 11:35:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzSpS3h1OAXKTDbL7HdUD/YWDE1qdPVHhA7o3RE2G/wUWggTUegIg+2wKjGrSEe9eDjst3A X-Received: by 2002:aa7:cad6:: with SMTP id l22mr788983edt.303.1622745325219; Thu, 03 Jun 2021 11:35:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622745325; cv=none; d=google.com; s=arc-20160816; b=g8NDeyc6YjPP6HfJhoQHiVlHjV1TJfDMh2W0+sxLzT6hEV772goyyRQFJtrmcU8Vew 4ltjRC/vldEw6Gkv29+ZEzI4APCXs5j8Lz2+HPXneYQGgl/OdTK7JONsxy313TOZSmgL VNvk2CVGwHWLNS8gdEYBzsZaUd+lY354ya79o9My6cenAfcbnqoQO29gFF0xpkQu/A5t v8ZaT4DcFjxFulm7nnQ/rsUJf0S65QRWmG+f+acsetap+0z+hn/zcxtaD+dfBYYiyid/ tL5anI9FNGFg9cnE3M+8ccrjQmo2Gpl/iBLuafaD+HLq+vOOXrvhT/Dhy9VniwOOqIEw cWsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:ironport-sdr:ironport-sdr; bh=YGZMlSjujVbgAen4/DvlWaxdcrFTQRObyUz828A8tQg=; b=T482f5e980wPMDk0Yhy7RzSrJ3hgFexBF86JF4y3k8yM+WsVjWf1J5KepwP7OXqxwi 9O0ieGJRLoUHlkTvM6oH2BtBt8RJXu1CuQ+2RZgl3R9+38mKXkMZEv/5EXXGjeyScZWt MryDt9b8TFkVCzYLmUx0RO4AIQOEsq4/flBD78mMidmYNpdpcLITFKRqTlCHusC2EbQi EHCnTM+FGFJZW9AnFBhhf1C6GaaR3Aa+No0UhRyno5mukcT2yeEt1Ug0CQVkEjw3fwkz fY+BgVQJzfDpC8JbDAGlZRiQHL2qINfldtDTG0CTA+8G/C0rqCCI63fVp5rqcfT187Mo C6Bw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dc11si2774092ejb.584.2021.06.03.11.35.01; Thu, 03 Jun 2021 11:35:25 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229888AbhFCSfq (ORCPT + 99 others); Thu, 3 Jun 2021 14:35:46 -0400 Received: from mga09.intel.com ([134.134.136.24]:51916 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229881AbhFCSfp (ORCPT ); Thu, 3 Jun 2021 14:35:45 -0400 IronPort-SDR: nTWbcctoTbJQyziP4SOl4Aw4YamsZXANl7YmbQXQmyxP4VBcJT/qtWd2C8sLCgPuNg9zkgFrVS x0F0NzH3wkqA== X-IronPort-AV: E=McAfee;i="6200,9189,10004"; a="204101412" X-IronPort-AV: E=Sophos;i="5.83,246,1616482800"; d="scan'208";a="204101412" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jun 2021 11:33:56 -0700 IronPort-SDR: S2Mvcw5l149MaBFfOIgSvmArchHKNEO57xPABoVdEmi5dPSPT35e1bFw+5CXUsHZCuGFa77oJw 0nuntYJEijDQ== X-IronPort-AV: E=Sophos;i="5.83,246,1616482800"; d="scan'208";a="480333355" Received: from jacobmon-mobl.amr.corp.intel.com (HELO skuppusw-mobl5.amr.corp.intel.com) ([10.212.36.217]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jun 2021 11:33:55 -0700 Subject: Re: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction To: Borislav Petkov Cc: Peter Zijlstra , Andy Lutomirski , Dave Hansen , Tony Luck , Andi Kleen , Kirill Shutemov , Kuppuswamy Sathyanarayanan , Dan Williams , Raj Ashok , Sean Christopherson , linux-kernel@vger.kernel.org, Tom Lendacky References: <20210527042356.3983284-2-sathyanarayanan.kuppuswamy@linux.intel.com> <20210601211417.2177598-1-sathyanarayanan.kuppuswamy@linux.intel.com> From: "Kuppuswamy, Sathyanarayanan" Message-ID: Date: Thu, 3 Jun 2021 11:33:53 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 6/3/21 11:14 AM, Borislav Petkov wrote: > On Tue, Jun 01, 2021 at 02:14:17PM -0700, Kuppuswamy Sathyanarayanan wrote: >> diff --git a/include/linux/protected_guest.h b/include/linux/protected_guest.h >> new file mode 100644 >> index 000000000000..303dfba81d52 >> --- /dev/null >> +++ b/include/linux/protected_guest.h >> @@ -0,0 +1,40 @@ >> +/* SPDX-License-Identifier: GPL-2.0-only */ >> +#ifndef _LINUX_PROTECTED_GUEST_H >> +#define _LINUX_PROTECTED_GUEST_H 1 >> + >> +#include >> + >> +/* Protected Guest Feature Flags (leave 0-0xff for arch specific flags) */ >> + >> +/* Support for guest encryption */ >> +#define VM_MEM_ENCRYPT 0x100 >> +/* Encryption support is active */ >> +#define VM_MEM_ENCRYPT_ACTIVE 0x101 >> +/* Support for unrolled string IO */ >> +#define VM_UNROLL_STRING_IO 0x102 >> +/* Support for host memory encryption */ >> +#define VM_HOST_MEM_ENCRYPT 0x103 >> +/* Support for shared mapping initialization (after early init) */ >> +#define VM_SHARED_MAPPING_INIT 0x104 > > Ok, a couple of things: > > first of all, those flags with that VM_ prefix make me think of > "virtual memory" instead of "virtual machine". So they should be > something else, like, say > > PR_G_... for Protected Guest or so. Or PR_GUEST or ... I would prefer PR_GUEST over PR_G_ > > (yeah, good namespaces are all taken. ) > > Then, about the function name length, I'm fine if we did: > > prot_guest_has() > > or something even shorter, if you folks have a good suggestion. > > Anyway, below is a diff ontop of your tree with what I think the > barebones of this should be. > > As a reply to this message I went and converted sme_active() to use > protected_guest_has() too. > > Comments, complaints? > > Thx. > > --- > diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h > index 1492b0eb29d0..9c80c68d75b5 100644 > --- a/arch/x86/include/asm/mem_encrypt.h > +++ b/arch/x86/include/asm/mem_encrypt.h > @@ -56,8 +56,6 @@ bool sev_es_active(void); > > #define __bss_decrypted __section(".bss..decrypted") > > -bool amd_protected_guest_has(unsigned long flag); > - > #else /* !CONFIG_AMD_MEM_ENCRYPT */ > > #define sme_me_mask 0ULL > @@ -88,8 +86,6 @@ early_set_memory_encrypted(unsigned long vaddr, unsigned long size) { return 0; > > static inline void mem_encrypt_free_decrypted_mem(void) { } > > -static inline bool amd_protected_guest_has(unsigned long flag) { return false; } > - > #define __bss_decrypted > > #endif /* CONFIG_AMD_MEM_ENCRYPT */ > diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h > index fa5cd05d3b5b..f09996c6a272 100644 > --- a/arch/x86/include/asm/sev.h > +++ b/arch/x86/include/asm/sev.h > @@ -11,6 +11,7 @@ > #include > #include > #include > +#include > > #define GHCB_PROTO_OUR 0x0001UL > #define GHCB_PROTOCOL_MAX 1ULL > @@ -81,12 +82,15 @@ static __always_inline void sev_es_nmi_complete(void) > __sev_es_nmi_complete(); > } > extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd); > +bool sev_protected_guest_has(unsigned long flag); > + > #else > static inline void sev_es_ist_enter(struct pt_regs *regs) { } > static inline void sev_es_ist_exit(void) { } > static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; } > static inline void sev_es_nmi_complete(void) { } > static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; } > +static inline bool sev_protected_guest_has(unsigned long flag) { return false; } > #endif > > -#endif > +#endif /* __ASM_ENCRYPTED_STATE_H */ > diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c > index f7a743d122eb..01a224fdb897 100644 > --- a/arch/x86/kernel/sev.c > +++ b/arch/x86/kernel/sev.c > @@ -1402,3 +1402,14 @@ bool __init handle_vc_boot_ghcb(struct pt_regs *regs) > while (true) > halt(); > } > + > +bool sev_protected_guest_has(unsigned long flag) > +{ > + switch (flag) { > + case VM_MEM_ENCRYPT: > + case VM_MEM_ENCRYPT_ACTIVE: > + return true; > + } > + > + return false; > +} I assume this file will get compiled for both SEV and SME cases. > diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c > index ced658e79753..49d11bb6e02a 100644 > --- a/arch/x86/mm/mem_encrypt.c > +++ b/arch/x86/mm/mem_encrypt.c > @@ -391,18 +391,6 @@ bool noinstr sev_es_active(void) > return sev_status & MSR_AMD64_SEV_ES_ENABLED; > } > > -bool amd_protected_guest_has(unsigned long flag) > -{ > - switch (flag) { > - case VM_MEM_ENCRYPT: > - case VM_MEM_ENCRYPT_ACTIVE: > - return true; > - } > - > - return false; > -} > -EXPORT_SYMBOL_GPL(amd_protected_guest_has); > - > /* Override for DMA direct allocation check - AMD specific initialization */ > bool amd_force_dma_unencrypted(struct device *dev) > { > diff --git a/include/linux/protected_guest.h b/include/linux/protected_guest.h > index 6855d5b3e244..bb4b1a06b21f 100644 > --- a/include/linux/protected_guest.h > +++ b/include/linux/protected_guest.h > @@ -2,7 +2,9 @@ > #ifndef _LINUX_PROTECTED_GUEST_H > #define _LINUX_PROTECTED_GUEST_H 1 > > -#include > +#include > +#include > +#include > > /* Protected Guest Feature Flags (leave 0-0xff for arch specific flags) */ > > @@ -20,23 +22,18 @@ > #define VM_DISABLE_UNCORE_SUPPORT 0x105 > > #if defined(CONFIG_INTEL_TDX_GUEST) || defined(CONFIG_AMD_MEM_ENCRYPT) > - > -#include > - > static inline bool protected_guest_has(unsigned long flag) > { > if (is_tdx_guest()) > return tdx_protected_guest_has(flag); > - else if (mem_encrypt_active()) > - return amd_protected_guest_has(flag); > + else if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) > + return sev_protected_guest_has(flag); Since you are checking for AMD vendor ID, why not use amd_protected_guest_has()? > > return false; > } > > #else > - > static inline bool protected_guest_has(unsigned long flag) { return false; } > - > #endif > > -#endif > +#endif /* _LINUX_PROTECTED_GUEST_H */ > > -- Sathyanarayanan Kuppuswamy Linux Kernel Developer