Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2040028rdf; Mon, 6 Nov 2023 03:03:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IFOPBDfHc/l5AEacIYmfgnHR9sOZwhgUQwXYGm84PyqWOw8oCDb3SgIWmsDoAvLW0W3P5JI X-Received: by 2002:a17:903:22cc:b0:1cc:6acc:8fa0 with SMTP id y12-20020a17090322cc00b001cc6acc8fa0mr18490013plg.34.1699268584707; Mon, 06 Nov 2023 03:03:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699268584; cv=none; d=google.com; s=arc-20160816; b=XnBz2Tg64NA02BWPJKMdplJptZnNm0mJK7ZKP3pHC61ikd/bnSEUo/hMmcZHm6B/Gb wfAviNMbndp4EP7LOOoN+Qz4IpW3xixAA207ikPxsfyNsPKmU+tOCDxDvu0zsi+2djOx dzpNFPsrK3vx2KsizBCado6Xa/uvc+Nyhm7I+bEGRUzQIK3lnm8sx7zSm7g5dx+zeKby G29s97V3XV8E2DjnIjtOlOpCRbUhVFRKlkq76Pll40E+kGiIdTp+KWgL9Qntls4PEEvz MmYF5D30lp/hzEmlf6fs3YpwhJn+QtBk0yI23NUkF1uu7E7I06Cc5jKMNap0zI6TE46V eEQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=V2GJ8/Y90IpmxpdY5rYjfIxqDFVHPJDdAKZ/UZv6WBc=; fh=6NDLbPegWN39ihzRI+XD38qv7OUBTPlYZUUvtWMZU2Q=; b=PdV/nxdtn7jPbTS8tgX9Q2FF0htuH1fByZX1dear15ZKEitP82dz4g/5cTcibZyf5X NyHxyno0t+8pKgS2m9Z87QFr2OXlFeKe4ouWyAjpJPqR8W7fvtEgXBGhPZ/S9S987Etl hg0Kq0qo98qcwYdpmwKoV+5VO2fm9oD2r4trz/iA7c5giCO12bT4/EowqsJzyg4lZPxf 74wpTdujnKRjzc+fT4JGgPSljB0fEEpNpgfYSIjJoDXI7z+RJ5ZSCRh2CSp4cBIa0Do2 nJko9w6HFly8nyH0V3QnoiVuv+6mSB4Y2mNVBYJf4xrDuYjiA8Vqmzy0S1X14Q/wZTdm JBog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vYNcYifD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id i9-20020a17090332c900b001c6183af4d3si8664030plr.332.2023.11.06.03.03.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:03:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vYNcYifD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 13E2380A3662; Mon, 6 Nov 2023 03:03:01 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229478AbjKFLCi (ORCPT + 99 others); Mon, 6 Nov 2023 06:02:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231454AbjKFLCg (ORCPT ); Mon, 6 Nov 2023 06:02:36 -0500 Received: from mail-yw1-x112b.google.com (mail-yw1-x112b.google.com [IPv6:2607:f8b0:4864:20::112b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BABC3FA for ; Mon, 6 Nov 2023 03:02:32 -0800 (PST) Received: by mail-yw1-x112b.google.com with SMTP id 00721157ae682-5b35579f475so49837507b3.3 for ; Mon, 06 Nov 2023 03:02:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699268552; x=1699873352; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=V2GJ8/Y90IpmxpdY5rYjfIxqDFVHPJDdAKZ/UZv6WBc=; b=vYNcYifDPP598PI8ch9gsg0/D6zQlCvtxo4xnaAmv4fBAEd2qbBuirTXWg5IE8hpfH LtHI/BJZARdS5opWd+N0g0u+i2LlMo5/aBnmCjyP2IV/WxnBkXmr0PcFeIEMPxbu5YbF WhzEaCkLFxtZmqTJmDxipCXdb4338WocA2daOHgVW2lSr6IC/8HDdprHZgT6OAhepVxi jcUco6+YnH8jYxVP6QO7nFuTB7NpiAD/t60K7W81hKm0Aq2zViYGuefiHn/aEYXXGZWk DJ+gsmhJHo1Fz6VjGvxRJ1HHcIFRrdotH5oSuvwI2ONM9vf3TJXxVzul3L0SPLqVjJ85 Msiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699268552; x=1699873352; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=V2GJ8/Y90IpmxpdY5rYjfIxqDFVHPJDdAKZ/UZv6WBc=; b=r7nS+C7r0TNXJXuzYzWGmRkekHjQ2O3OZkVl6Rzqym7Ez55xbIRD8u2+Bs1FHsXdnH ivCQkDpSgLBTamTNA4mI8HgsOuc7RlJU8ZJCON0cqnRBiS6GQWCRzd7NNHTVEB1ee31T cGDMRyBS6DUEoQPLt3hitVgPmvDRktlHkVOhuuAuUFuTRzzUCFNLNPvK96TgN5Kf/Qpd QM81OURTgx0k6CvkmEMgKHAwMrip4dYk7kysC7EpQEX/2sPMqY5RyXeik4XSwvxUEEO0 l9IEUkP9cqVwXK08kpREt3iBU6mCYTAnkwKyz9YveBSYaC4TXqEa8Njn0IPJMU0RC34c KtyQ== X-Gm-Message-State: AOJu0YwN1Q5cHbYCZXu3cYZLheRRJKACcHnSs55feAdO8vovn0Xu/qKo 1VHhDVQO3u9D6IQ7PvjpeAK4pGZzcGf7lE3QIw60WQ== X-Received: by 2002:a0d:ead2:0:b0:5a2:20ec:40be with SMTP id t201-20020a0dead2000000b005a220ec40bemr12462308ywe.29.1699268551723; Mon, 06 Nov 2023 03:02:31 -0800 (PST) MIME-Version: 1.0 References: <20231105163040.14904-1-pbonzini@redhat.com> <20231105163040.14904-22-pbonzini@redhat.com> In-Reply-To: <20231105163040.14904-22-pbonzini@redhat.com> From: Fuad Tabba Date: Mon, 6 Nov 2023 11:01:56 +0000 Message-ID: Subject: Re: [PATCH 21/34] KVM: x86: Add support for "protected VMs" that can utilize private memory To: Paolo Bonzini Cc: Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson , Alexander Viro , Christian Brauner , "Matthew Wilcox (Oracle)" , Andrew Morton , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Xu Yilun , Chao Peng , Jarkko Sakkinen , Anish Moorthy , David Matlack , Yu Zhang , Isaku Yamahata , =?UTF-8?B?TWlja2HDq2wgU2FsYcO8bg==?= , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A. Shutemov" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 06 Nov 2023 03:03:01 -0800 (PST) Hi, On Sun, Nov 5, 2023 at 4:33=E2=80=AFPM Paolo Bonzini = wrote: > > From: Sean Christopherson > > Add a new x86 VM type, KVM_X86_SW_PROTECTED_VM, to serve as a development > and testing vehicle for Confidential (CoCo) VMs, and potentially to even > become a "real" product in the distant future, e.g. a la pKVM. > > The private memory support in KVM x86 is aimed at AMD's SEV-SNP and > Intel's TDX, but those technologies are extremely complex (understatement= ), > difficult to debug, don't support running as nested guests, and require > hardware that's isn't universally accessible. I.e. relying SEV-SNP or TD= X (replied to v13 earlier, sorry) nit: "that isn't" Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba Cheers, /fuad > for maintaining guest private memory isn't a realistic option. > > At the very least, KVM_X86_SW_PROTECTED_VM will enable a variety of > selftests for guest_memfd and private memory support without requiring > unique hardware. > > Signed-off-by: Sean Christopherson > Reviewed-by: Paolo Bonzini > Message-Id: <20231027182217.3615211-24-seanjc@google.com> > Signed-off-by: Paolo Bonzini > --- > Documentation/virt/kvm/api.rst | 32 ++++++++++++++++++++++++++++++++ > arch/x86/include/asm/kvm_host.h | 15 +++++++++------ > arch/x86/include/uapi/asm/kvm.h | 3 +++ > arch/x86/kvm/Kconfig | 12 ++++++++++++ > arch/x86/kvm/mmu/mmu_internal.h | 1 + > arch/x86/kvm/x86.c | 16 +++++++++++++++- > include/uapi/linux/kvm.h | 1 + > virt/kvm/Kconfig | 5 +++++ > 8 files changed, 78 insertions(+), 7 deletions(-) > > diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.= rst > index 4a9a291380ad..38882263278d 100644 > --- a/Documentation/virt/kvm/api.rst > +++ b/Documentation/virt/kvm/api.rst > @@ -147,10 +147,29 @@ described as 'basic' will be available. > The new VM has no virtual cpus and no memory. > You probably want to use 0 as machine type. > > +X86: > +^^^^ > + > +Supported X86 VM types can be queried via KVM_CAP_VM_TYPES. > + > +S390: > +^^^^^ > + > In order to create user controlled virtual machines on S390, check > KVM_CAP_S390_UCONTROL and use the flag KVM_VM_S390_UCONTROL as > privileged user (CAP_SYS_ADMIN). > > +MIPS: > +^^^^^ > + > +To use hardware assisted virtualization on MIPS (VZ ASE) rather than > +the default trap & emulate implementation (which changes the virtual > +memory layout to fit in user mode), check KVM_CAP_MIPS_VZ and use the > +flag KVM_VM_MIPS_VZ. > + > +ARM64: > +^^^^^^ > + > On arm64, the physical address size for a VM (IPA Size limit) is limited > to 40bits by default. The limit can be configured if the host supports t= he > extension KVM_CAP_ARM_VM_IPA_SIZE. When supported, use > @@ -8766,6 +8785,19 @@ block sizes is exposed in KVM_CAP_ARM_SUPPORTED_BL= OCK_SIZES as a > 64-bit bitmap (each bit describing a block size). The default value is > 0, to disable the eager page splitting. > > +8.41 KVM_CAP_VM_TYPES > +--------------------- > + > +:Capability: KVM_CAP_MEMORY_ATTRIBUTES > +:Architectures: x86 > +:Type: system ioctl > + > +This capability returns a bitmap of support VM types. The 1-setting of = bit @n > +means the VM type with value @n is supported. Possible values of @n are= :: > + > + #define KVM_X86_DEFAULT_VM 0 > + #define KVM_X86_SW_PROTECTED_VM 1 > + > 9. Known KVM API problems > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_h= ost.h > index 75ab0da06e64..a565a2e70f30 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -1255,6 +1255,7 @@ enum kvm_apicv_inhibit { > }; > > struct kvm_arch { > + unsigned long vm_type; > unsigned long n_used_mmu_pages; > unsigned long n_requested_mmu_pages; > unsigned long n_max_mmu_pages; > @@ -2089,6 +2090,12 @@ void kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t = new_pgd); > void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level, > int tdp_max_root_level, int tdp_huge_page_level); > > +#ifdef CONFIG_KVM_PRIVATE_MEM > +#define kvm_arch_has_private_mem(kvm) ((kvm)->arch.vm_type !=3D KVM_X86_= DEFAULT_VM) > +#else > +#define kvm_arch_has_private_mem(kvm) false > +#endif > + > static inline u16 kvm_read_ldt(void) > { > u16 ldt; > @@ -2137,14 +2144,10 @@ enum { > #define HF_SMM_INSIDE_NMI_MASK (1 << 2) > > # define KVM_MAX_NR_ADDRESS_SPACES 2 > +/* SMM is currently unsupported for guests with private memory. */ > +# define kvm_arch_nr_memslot_as_ids(kvm) (kvm_arch_has_private_mem(kvm) = ? 1 : 2) > # define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_M= ASK ? 1 : 0) > # define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, (role= ).smm) > - > -static inline int kvm_arch_nr_memslot_as_ids(struct kvm *kvm) > -{ > - return KVM_MAX_NR_ADDRESS_SPACES; > -} > - > #else > # define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, 0) > #endif > diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/= kvm.h > index 1a6a1f987949..a448d0964fc0 100644 > --- a/arch/x86/include/uapi/asm/kvm.h > +++ b/arch/x86/include/uapi/asm/kvm.h > @@ -562,4 +562,7 @@ struct kvm_pmu_event_filter { > /* x86-specific KVM_EXIT_HYPERCALL flags. */ > #define KVM_EXIT_HYPERCALL_LONG_MODE BIT(0) > > +#define KVM_X86_DEFAULT_VM 0 > +#define KVM_X86_SW_PROTECTED_VM 1 > + > #endif /* _ASM_X86_KVM_H */ > diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig > index e61383674c75..c1716e83d176 100644 > --- a/arch/x86/kvm/Kconfig > +++ b/arch/x86/kvm/Kconfig > @@ -77,6 +77,18 @@ config KVM_WERROR > > If in doubt, say "N". > > +config KVM_SW_PROTECTED_VM > + bool "Enable support for KVM software-protected VMs" > + depends on EXPERT > + depends on X86_64 > + select KVM_GENERIC_PRIVATE_MEM > + help > + Enable support for KVM software-protected VMs. Currently "prot= ected" > + means the VM can be backed with memory provided by > + KVM_CREATE_GUEST_MEMFD. > + > + If unsure, say "N". > + > config KVM_INTEL > tristate "KVM for Intel (and compatible) processors support" > depends on KVM && IA32_FEAT_CTL > diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_inter= nal.h > index 86c7cb692786..b66a7d47e0e4 100644 > --- a/arch/x86/kvm/mmu/mmu_internal.h > +++ b/arch/x86/kvm/mmu/mmu_internal.h > @@ -297,6 +297,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vc= pu *vcpu, gpa_t cr2_or_gpa, > .max_level =3D KVM_MAX_HUGEPAGE_LEVEL, > .req_level =3D PG_LEVEL_4K, > .goal_level =3D PG_LEVEL_4K, > + .is_private =3D kvm_mem_is_private(vcpu->kvm, cr2_or_gpa = >> PAGE_SHIFT), > }; > int r; > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index f521c97f5c64..6d0772b47041 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -4548,6 +4548,13 @@ static int kvm_ioctl_get_supported_hv_cpuid(struct= kvm_vcpu *vcpu, > return 0; > } > > +static bool kvm_is_vm_type_supported(unsigned long type) > +{ > + return type =3D=3D KVM_X86_DEFAULT_VM || > + (type =3D=3D KVM_X86_SW_PROTECTED_VM && > + IS_ENABLED(CONFIG_KVM_SW_PROTECTED_VM) && tdp_enabled); > +} > + > int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) > { > int r =3D 0; > @@ -4739,6 +4746,11 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, = long ext) > case KVM_CAP_X86_NOTIFY_VMEXIT: > r =3D kvm_caps.has_notify_vmexit; > break; > + case KVM_CAP_VM_TYPES: > + r =3D BIT(KVM_X86_DEFAULT_VM); > + if (kvm_is_vm_type_supported(KVM_X86_SW_PROTECTED_VM)) > + r |=3D BIT(KVM_X86_SW_PROTECTED_VM); > + break; > default: > break; > } > @@ -12436,9 +12448,11 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned l= ong type) > int ret; > unsigned long flags; > > - if (type) > + if (!kvm_is_vm_type_supported(type)) > return -EINVAL; > > + kvm->arch.vm_type =3D type; > + > ret =3D kvm_page_track_init(kvm); > if (ret) > goto out; > diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h > index 8eb10f560c69..e9cb2df67a1d 100644 > --- a/include/uapi/linux/kvm.h > +++ b/include/uapi/linux/kvm.h > @@ -1227,6 +1227,7 @@ struct kvm_ppc_resize_hpt { > #define KVM_CAP_MEMORY_FAULT_INFO 232 > #define KVM_CAP_MEMORY_ATTRIBUTES 233 > #define KVM_CAP_GUEST_MEMFD 234 > +#define KVM_CAP_VM_TYPES 235 > > #ifdef KVM_CAP_IRQ_ROUTING > > diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig > index 08afef022db9..2c964586aa14 100644 > --- a/virt/kvm/Kconfig > +++ b/virt/kvm/Kconfig > @@ -104,3 +104,8 @@ config KVM_GENERIC_MEMORY_ATTRIBUTES > config KVM_PRIVATE_MEM > select XARRAY_MULTI > bool > + > +config KVM_GENERIC_PRIVATE_MEM > + select KVM_GENERIC_MEMORY_ATTRIBUTES > + select KVM_PRIVATE_MEM > + bool > -- > 2.39.1 > >