From patchwork Thu Sep 14 01:55:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384028 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2EF41EE0212 for ; Thu, 14 Sep 2023 01:56:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 751808D0003; Wed, 13 Sep 2023 21:56:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 666D48D0001; Wed, 13 Sep 2023 21:56:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B8918D0003; Wed, 13 Sep 2023 21:56:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 32D4E8D0001 for ; Wed, 13 Sep 2023 21:56:20 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 14EAC1CAC01 for ; Thu, 14 Sep 2023 01:56:20 +0000 (UTC) X-FDA: 81233538120.06.DAEC3A2 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf02.hostedemail.com (Postfix) with ESMTP id 4E3CB80007 for ; Thu, 14 Sep 2023 01:56:18 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=fNl3rtW3; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3QWgCZQYKCEo4qmzvos00sxq.o0yxuz69-yyw7mow.03s@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3QWgCZQYKCEo4qmzvos00sxq.o0yxuz69-yyw7mow.03s@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656578; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Jl+0PkL6fF6lt3zrM7PMQo1yyUvP9Wp8yN6Ux9vmUNY=; b=6hcFM34JzURnsNJOipQf+dGsXK8tOhFKWQ4AThib1DDI1h5N70yOJph/iFXQs4BKTxugt6 OtpX+nI+dlg7FUeH1uglbElYPCYPLSd8RzZISZYS8I4ESE1Gq3O2DLfZMfuK3QqNy4/hlI +zILttvPWeBczm50nCp3nKEGNP8lQyo= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=fNl3rtW3; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3QWgCZQYKCEo4qmzvos00sxq.o0yxuz69-yyw7mow.03s@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3QWgCZQYKCEo4qmzvos00sxq.o0yxuz69-yyw7mow.03s@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656578; a=rsa-sha256; cv=none; b=3cOdMK46tWOmHb44cHlNiDaVv1BgTajQkfLu2am4NZNOm7Exzppk4Za4PLR2uUwB7Ji2ic ie0WOnzQ0omr+Eje5dbUeq0tN9mAjMn5z6QrGI6U/5MILJE9UnvxUi5v0Zd5vnJetGgFtU lj1PTf6X399L7oIhtxkyQCkJGpiDzPU= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-59bcabc69easo6890057b3.1 for ; Wed, 13 Sep 2023 18:56:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656577; x=1695261377; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Jl+0PkL6fF6lt3zrM7PMQo1yyUvP9Wp8yN6Ux9vmUNY=; b=fNl3rtW3D62J/GfyGnG2BGJvlDueFNptaOI19rUF+70S/0Rt05tsHppFm2ko2L/mEW Vtox+Ppxj3aYbFheOpNBP1i9i9sy18HyizA4zrbsmDOPfke+BVMZiLxW6KPPRfqsOrYm /byLYicmXjC6ozk1S7qQB8gpvutgBPsLtF2pjOUd889u17rXUlQePSdkG6EVbplDHoxB JkpuNN4uiDdd7Nfi8J7m3P1NwzWsd7pQqslRfafLx3stFZydluedrIcqL6bcLaLQXXyQ 9YUWRJLU8l+xWA5eP/eS6MHvA6XYIGu11uORTz/RtlSPseWvIcjkJm9GqTNxgc4/Br2k p12A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656577; x=1695261377; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Jl+0PkL6fF6lt3zrM7PMQo1yyUvP9Wp8yN6Ux9vmUNY=; b=t5X4IdGnNJPpLIxCbYbh1XtT/Hr+vpGa+1kLQzLD6vnTBwacOJfGiqoxzF2/+uFaRH uYs3/X3/aArE8g8N9eQsFmwxSBi4EZm5M0kFNcR7wLMJiscMg/4eBQpJRuAFCCBsnhlc Gv8+loM69+jQFuHI+/3DCq8uprt6Ze1+a5nVI/m+60eVW7vkq7nsZiMxRZRPmbYA+3xd ABjBdNlGwp5gGs6/Mt+ja3lz3AlQOtAldMRDRR4nWHQbgjlxUF6SPO8zGmWW3MH4Fyyz gq3DQDQotSG5iAx7qpYiH0HAoFl1MfnHyeu8ccI1fFZgcXoXux9Wz/mkdjuOH9Zqs3FX Sk3w== X-Gm-Message-State: AOJu0YwGDoQMYqR2l9x94iLkl0lxtGtvGcpnOdSpv5tYit2On9PYai9K x5Yw3AofHS2U+ZJXpL7f2k3RrajrndA= X-Google-Smtp-Source: AGHT+IGMW5Fpgp6gfJWk0eHuGZ3TAZOaiXnaa6CntAf7ojvb+izdwYFJ9WwJ3V4CWPMkeT0TBFGRR2456Wo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:ed5:b0:59b:eace:d46f with SMTP id cs21-20020a05690c0ed500b0059beaced46fmr23980ywb.8.1694656577511; Wed, 13 Sep 2023 18:56:17 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:19 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-22-seanjc@google.com> Subject: [RFC PATCH v12 21/33] KVM: x86: Add support for "protected VMs" that can utilize private memory From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: 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-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov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XkxxcZyixRhM6DkLxk1BdUB O3FThnjc 0Kg5lCOCdMrZoSI1fIO2c9tQiX+X+1LfqbxqAtsMyqSRLw4U5KPRPU4pJAz7+MKgwIYlLVVfK4dQAoOpQnH3EzLaUERRAcVcyBWALjkHUAZDhUB02L+OyNgLhkVbcvqNzBKUjJZpZmgXPjTD6UrEdyuP/oL2nhMtVXqZkNZ0aw8cvTCH0wV9NoGqdwly6U0wrNatHSbz/R+U1mgqvSy1ExQ8O0UQZ2vAVuPgBDtBwIsuwRdrZ4mPkt1OM7oluLsrerxvMXojbaiZNFzozy4HWStnYAhpgii7hnu7jm8dvYaxl0ieTrq/a36wzhgGHcGevvysZ+kS+4xbkhTQRKPdF2oAOBDRPcR6GNRwnlv5+2c+qOWN1KSKt/ILd45AWF9Lz7pKyXHFD6iT8S6bC4U0byrkAC6L12iagX5oD5Uj8gAjza9UgIxnHrlRDvapypPih6y/kCNG1F72PbqDaisrDdR5EXPWuH77NGLvSoLaocgUlD0j7jZH2cjYb2KyxYPJHj+2jpMYAZmpp3KCsNaZpEuPe5UNhEXYJ+brJm4182OlpaewaSe9rOhgyBpQ1sO+B3bh8v7O2GGeh1OZMk1fYP2pPg/6AusP2lKbCc6ZejVwl/nsG/S0VCLG1v2YwMi4SVw8q/NFV1fYIlVeXYfJDmAqrr16/apEFoUshE4etcF9bPBFXeQR4YKYmSLST3EcgZt+CZMmb1J9ZAXqsxwCPVrSswO+pYhbwSx24KbFlnL/mEDyHAAVOHquOdoSZiR4qPv37 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Signed-off-by: Sean Christopherson --- 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 c44ef5295a12..5e08f2a157ef 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 the extension KVM_CAP_ARM_VM_IPA_SIZE. When supported, use @@ -8558,6 +8577,19 @@ block sizes is exposed in KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES as a This capability indicates KVM supports per-page memory attributes and ioctls KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES/KVM_SET_MEMORY_ATTRIBUTES are available. +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 ========================= diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 44d67a97304e..95018cc653f5 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1245,6 +1245,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; @@ -2079,6 +2080,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 != KVM_X86_DEFAULT_VM) +#else +#define kvm_arch_has_private_mem(kvm) false +#endif + static inline u16 kvm_read_ldt(void) { u16 ldt; @@ -2127,14 +2134,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_MASK ? 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 091b74599c22..8452ed0228cb 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 "protected" + 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_internal.h index 4efbf43b4b18..71ba4f833dc1 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -298,6 +298,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, + .is_private = 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 f1da61236670..767236b4d771 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4441,6 +4441,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 == KVM_X86_DEFAULT_VM || + (type == 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 = 0; @@ -4631,6 +4638,11 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_X86_NOTIFY_VMEXIT: r = kvm_caps.has_notify_vmexit; break; + case KVM_CAP_VM_TYPES: + r = BIT(KVM_X86_DEFAULT_VM); + if (kvm_is_vm_type_supported(KVM_X86_SW_PROTECTED_VM)) + r |= BIT(KVM_X86_SW_PROTECTED_VM); + break; default: break; } @@ -12302,9 +12314,11 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) int ret; unsigned long flags; - if (type) + if (!kvm_is_vm_type_supported(type)) return -EINVAL; + kvm->arch.vm_type = type; + ret = kvm_page_track_init(kvm); if (ret) goto out; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 2df18796fd8e..65fc983af840 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1233,6 +1233,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_USER_MEMORY2 230 #define KVM_CAP_MEMORY_ATTRIBUTES 231 #define KVM_CAP_GUEST_MEMFD 232 +#define KVM_CAP_VM_TYPES 233 #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