From patchwork Fri Jan 21 22:29:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Dunn X-Patchwork-Id: 12720327 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5739C433EF for ; Fri, 21 Jan 2022 22:29:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230168AbiAUW3m (ORCPT ); Fri, 21 Jan 2022 17:29:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230118AbiAUW3l (ORCPT ); Fri, 21 Jan 2022 17:29:41 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9AB8AC06173B for ; Fri, 21 Jan 2022 14:29:41 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id p5-20020a170902bd0500b00148cb2d29ecso2205894pls.4 for ; Fri, 21 Jan 2022 14:29:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=aZXYB5NNtr+RbhBC4kyZ2vlroC6uq8YHN+NOx6a9vLM=; b=U2CorhnwekRPgqhGdhSv8a4HFBM3Oj183a61VKxsGXgdOEDbJoQ2UAfOjE08Q9nBFE sIljfqum22VNAtHFshKK12tr40hx56f1aezFx+SzOBZo02r3BkYrl/KFL2/xW5af9oHu 8GTUNgxs8XbNjVJ7bE0+SxaffIG3L28KymwmDHrs5cDLzG19w81uXmAoTwcP7XetVlI3 VUvfS6tj+qLt5XVFyOio4Ts+7SKfl5dB1pejL/WBY3Jz5J8GfuokobLUJjocdYrxf7JK 1OTDHmtfOlOvUc4VVixB+xzJimO+ox4wNGTFShx9FifiNjMsBxmOk5echmeI8ckYsGjc WVJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=aZXYB5NNtr+RbhBC4kyZ2vlroC6uq8YHN+NOx6a9vLM=; b=ovHjhIMZezt4Nf1ICZJNDjfuwEQuMaHQGOqQo7Bpuu4QwEwTiGErhgVLESFgEL7+8G q4CKg+cva62hUppHUSNcy8EO3OfI4wWM9Ypm6j4yKBueiMUgjUA0wxKUMYc8bmApyIpG ne8Bbb/8J/U8x4bhmyYRfFtmgE2fLt2fsbgGgkjD8URbClgAgq/5raUfiWIoZybYPPS8 fFCAfWz3itF99D6GMeAULSNtgVAPgBIjD6JaFylymilnjS7cIfJykmacBmsyoYWaON52 TDDUnF8hnga7PJoND6ed3tLPYEzI1WQ25UKJ1EhoNd5wsAbMUH/bqggNkRZa0K1+hH9q THYw== X-Gm-Message-State: AOAM530x0radhXcVBjMiKhUQ0nKM/zifkbDrw/1L/+SrEAYNoyedTLEL FhVdsrV00imdtzWucO9YRk3tGeYdNRL+2Z+nowSD+mTBXC1USYc6OBlQPpIx8eyRu/cfKVQntj4 KZN8CDsFybZlLdDFzGzPACNF+V3b6EmZ+/1DIPn8JZWLMqVG7mzvDkNozgpbDnx6Gaw== X-Google-Smtp-Source: ABdhPJzJmB9OJX1ii7VKmaukAPXesK904rsyUYeGXtTm/5ZmrL+vddoZalJuPVXfjeY9dS70+xFQti83zJgxcnM= X-Received: from daviddunn-glinux.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:782]) (user=daviddunn job=sendgmr) by 2002:a63:d417:: with SMTP id a23mr4270560pgh.297.1642804180673; Fri, 21 Jan 2022 14:29:40 -0800 (PST) Date: Fri, 21 Jan 2022 22:29:31 +0000 In-Reply-To: <20220121222933.696067-1-daviddunn@google.com> Message-Id: <20220121222933.696067-2-daviddunn@google.com> Mime-Version: 1.0 References: <20220121222933.696067-1-daviddunn@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v4 1/3] KVM: x86: Provide per VM capability for disabling PMU virtualization From: David Dunn To: kvm@vger.kernel.org, pbonzini@redhat.com, like.xu.linux@gmail.com, jmattson@google.com, cloudliang@tencent.com, seanjc@google.com Cc: daviddunn@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org KVM_CAP_PMU_DISABLE is used to disable PMU virtualization on individual x86 VMs. PMU configuration must be done prior to creating VCPUs. To enable future extension, KVM_CAP_PMU_CAPABILITY reports available settings via bitmask when queried via check_extension. For VMs that have PMU virtualization disabled, CPUID leaf 0xA will be cleared to notify guests. Signed-off-by: David Dunn --- Documentation/virt/kvm/api.rst | 21 +++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/cpuid.c | 8 ++++++++ arch/x86/kvm/svm/pmu.c | 2 +- arch/x86/kvm/vmx/pmu_intel.c | 2 +- arch/x86/kvm/x86.c | 12 ++++++++++++ include/uapi/linux/kvm.h | 4 ++++ tools/include/uapi/linux/kvm.h | 4 ++++ 8 files changed, 52 insertions(+), 2 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index bb8cfddbb22d..375d35e8ac47 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7559,3 +7559,24 @@ The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset of the result of KVM_CHECK_EXTENSION. KVM will forward to userspace the hypercalls whose corresponding bit is in the argument, and return ENOSYS for the others. + +8.35 KVM_CAP_PMU_CAPABILITY +--------------------------- + +:Capability KVM_CAP_PMU_CAPABILITY +:Architectures: x86 +:Type: vm + +This capability alters PMU virtualization in KVM. + +Calling KVM_CHECK_EXTENSION for this capability returns a bitmask of +PMU virtualization capabilities that can be adjusted on a VM. + +The argument to KVM_ENABLE_CAP is also a bitmask and selects specific +PMU virtualization capabilities to be applied to the VM. This can +only be invoked on a VM prior to the creation of VCPUs. + +At this time, KVM_CAP_PMU_DISABLE is the only capability. Setting +this capability will disable PMU virtualization for that VM. When +PMU virtualization is disabled, CPUID leaf 0xA will always be cleared +to properly inform Intel guests. diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 682ad02a4e58..5cdcd4a7671b 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1232,6 +1232,7 @@ struct kvm_arch { hpa_t hv_root_tdp; spinlock_t hv_root_tdp_lock; #endif + bool enable_pmu; }; struct kvm_vm_stat { diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 3902c28fb6cb..a91c4a00c913 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -217,6 +217,14 @@ static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_e cpuid_entry_change(best, X86_FEATURE_OSPKE, kvm_read_cr4_bits(vcpu, X86_CR4_PKE)); + best = cpuid_entry2_find(entries, nent, 0xA, 0); + if (best && !vcpu->kvm->arch.enable_pmu) { + best->eax = 0; + best->ebx = 0; + best->ecx = 0; + best->edx = 0; + } + best = cpuid_entry2_find(entries, nent, 0xD, 0); if (best) best->ebx = xstate_required_size(vcpu->arch.xcr0, false); diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c index 5aa45f13b16d..d4de52409335 100644 --- a/arch/x86/kvm/svm/pmu.c +++ b/arch/x86/kvm/svm/pmu.c @@ -101,7 +101,7 @@ static inline struct kvm_pmc *get_gp_pmc_amd(struct kvm_pmu *pmu, u32 msr, { struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu); - if (!enable_pmu) + if (!vcpu->kvm->arch.enable_pmu) return NULL; switch (msr) { diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 466d18fc0c5d..2c5868d77268 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -487,7 +487,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) pmu->reserved_bits = 0xffffffff00200000ull; entry = kvm_find_cpuid_entry(vcpu, 0xa, 0); - if (!entry || !enable_pmu) + if (!entry || !vcpu->kvm->arch.enable_pmu) return; eax.full = entry->eax; edx.full = entry->edx; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 55518b7d3b96..a033f019a3f0 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4326,6 +4326,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) if (r < sizeof(struct kvm_xsave)) r = sizeof(struct kvm_xsave); break; + case KVM_CAP_PMU_CAPABILITY: + r = enable_pmu ? KVM_CAP_PMU_MASK : 0; + break; } default: break; @@ -5937,6 +5940,14 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, kvm->arch.exit_on_emulation_error = cap->args[0]; r = 0; break; + case KVM_CAP_PMU_CAPABILITY: + r = -EINVAL; + if (!enable_pmu || kvm->created_vcpus > 0 || + cap->args[0] & ~KVM_CAP_PMU_MASK) + break; + kvm->arch.enable_pmu = !(cap->args[0] & KVM_CAP_PMU_DISABLE); + r = 0; + break; default: r = -EINVAL; break; @@ -11562,6 +11573,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); kvm->arch.guest_can_read_msr_platform_info = true; + kvm->arch.enable_pmu = enable_pmu; #if IS_ENABLED(CONFIG_HYPERV) spin_lock_init(&kvm->arch.hv_root_tdp_lock); diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 9563d294f181..1c5e6e172817 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1133,6 +1133,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM 206 #define KVM_CAP_VM_GPA_BITS 207 #define KVM_CAP_XSAVE2 208 +#define KVM_CAP_PMU_CAPABILITY 209 #ifdef KVM_CAP_IRQ_ROUTING @@ -1972,6 +1973,9 @@ struct kvm_dirty_gfn { #define KVM_BUS_LOCK_DETECTION_OFF (1 << 0) #define KVM_BUS_LOCK_DETECTION_EXIT (1 << 1) +#define KVM_CAP_PMU_DISABLE (1 << 0) +#define KVM_CAP_PMU_MASK (KVM_CAP_PMU_DISABLE) + /** * struct kvm_stats_header - Header of per vm/vcpu binary statistics data. * @flags: Some extra information for header, always 0 for now. diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h index 9563d294f181..a361cf6e8604 100644 --- a/tools/include/uapi/linux/kvm.h +++ b/tools/include/uapi/linux/kvm.h @@ -1133,6 +1133,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM 206 #define KVM_CAP_VM_GPA_BITS 207 #define KVM_CAP_XSAVE2 208 +#define KVM_CAP_PMU_CAPABILITY 209 #ifdef KVM_CAP_IRQ_ROUTING @@ -1972,6 +1973,9 @@ struct kvm_dirty_gfn { #define KVM_BUS_LOCK_DETECTION_OFF (1 << 0) #define KVM_BUS_LOCK_DETECTION_EXIT (1 << 1) +#define KVM_CAP_PMU_DISABLE (1 << 0) +#define KVM_CAP_PMU_MASK (KVM_PMU_CONFIG_DISABLE) + /** * struct kvm_stats_header - Header of per vm/vcpu binary statistics data. * @flags: Some extra information for header, always 0 for now. From patchwork Fri Jan 21 22:29:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Dunn X-Patchwork-Id: 12720328 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EBFC9C433EF for ; Fri, 21 Jan 2022 22:29:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230184AbiAUW3q (ORCPT ); Fri, 21 Jan 2022 17:29:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230186AbiAUW3p (ORCPT ); Fri, 21 Jan 2022 17:29:45 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88D2FC06173B for ; Fri, 21 Jan 2022 14:29:45 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id p17-20020a170903249100b0014af06caa65so2185152plw.6 for ; Fri, 21 Jan 2022 14:29:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=lHkAYzvU+gvACQS+B203C+Z1CPAITSVUhZfrMamGfEo=; b=Ma1llP5LV0ECB7GJH7JmX9CaH80a751vNfoPNRODB0yYw6K2ZPP9zwQjf6g5+TX6gI H8W21MDLPimBOAjcuC38OogSzIMQCB4UBBtNTiwR/JY3q7MAa/Zzq+tKqOuv05dcnmqn I/H2F4wZwl9HrFbBVMDZGga4o//DK0jTR0zkuZy6ytb2b2T4Db0yqc5o/+iO7/pOlg2m exAeD5Jm52oSzTEJE9Q+WEpTgj8rBCEEKY97jhVnpeOg8LLyGylRC11e+4IB3qZ5CFtt 191mkrhXxAN1lFtzAN4ZEKq1FqX3Nh/yRMcrN0HcAJ0iEYQjnfxyOZieP+6mBglo+VKX KaFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=lHkAYzvU+gvACQS+B203C+Z1CPAITSVUhZfrMamGfEo=; b=VZnCLv0zbwFpoO2p3aC6sTp/gzsleMlNSzwUbpcwcQSbZeGCamVsrEWEoay2jtfpps xX4HppEc+VT2JlcrL1/jqKogK8hsJAD1FofNfxmteCLDpIV97zFtTYXV3V65fgLObSjr Cp2TFy6E5Q1QyxfaFeuH7DBl5Y3Kpuqrbz600KNwj/VtU7HcVNewpsjK5z8RQEzTyArt vkw+QrFL0iTFqpZ1oXtfowF5Zn8txG6QrhBVGgGokr3jffUuku5WKB4ZwTJQysuebIBu qwl6zBDOdu8iZNwizzYATy8IFKtrP+ogq95jURHHJ5RU9YoljgAsD0U1cxMVOFNFWem8 eQSQ== X-Gm-Message-State: AOAM533GKg1D11joo36lmRR82JWFEY4Lti9W3uuaPJX6vr67TDSMPEha nU74V9NEso1giEjpXUD42VeyyRdueD8oeHSpGLwKgTJcNqa98YS2m8n+ZPKwG9E+PGwTOFl0if4 o3XrYjEjTFtJnKhWN4nEuNmwMxsByg35RB/5huvcymSVv+8d4iEZIXZDXXu+XUMqCKQ== X-Google-Smtp-Source: ABdhPJzu1Nq3fEP/asga4mSZXSdaqG5yvqtA3sv/LO75CSv8imkaoIjCDjfoXV7/0OfsSTSXTRBE/vs/txM+nGw= X-Received: from daviddunn-glinux.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:782]) (user=daviddunn job=sendgmr) by 2002:aa7:818b:0:b0:4c7:abe1:f94a with SMTP id g11-20020aa7818b000000b004c7abe1f94amr2457299pfi.66.1642804184708; Fri, 21 Jan 2022 14:29:44 -0800 (PST) Date: Fri, 21 Jan 2022 22:29:32 +0000 In-Reply-To: <20220121222933.696067-1-daviddunn@google.com> Message-Id: <20220121222933.696067-3-daviddunn@google.com> Mime-Version: 1.0 References: <20220121222933.696067-1-daviddunn@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v4 2/3] KVM: selftests: Allow creation of selftest VM without vcpus From: David Dunn To: kvm@vger.kernel.org, pbonzini@redhat.com, like.xu.linux@gmail.com, jmattson@google.com, cloudliang@tencent.com, seanjc@google.com Cc: daviddunn@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Break out portion of vm_create_with_vcpus so that selftests can modify the VM prior to creating vcpus. Signed-off-by: David Dunn --- .../selftests/kvm/include/kvm_util_base.h | 3 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 48 ++++++++++++++----- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 66775de26952..0454027d588d 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -336,6 +336,9 @@ struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, uint32_t num_percpu_pages, void *guest_code, uint32_t vcpuids[]); +/* First phase of vm_create_with_vcpus, allows customization before vcpu add */ +struct kvm_vm *vm_create_without_vcpus(enum vm_guest_mode mode, uint64_t pages); + /* * Adds a vCPU with reasonable defaults (e.g. a stack) * diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 8c53f96ab7fe..f44fd2210c66 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -362,6 +362,40 @@ struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm) return vm; } +/* + * VM Create without creating VCPUs + * + * Input Args: + * mode - VM Mode (e.g. VM_MODE_P52V48_4K) + * pages - pages of memory required for VM + * + * Output Args: None + * + * Return: + * Pointer to opaque structure that describes the created VM. + * + * Creates a VM with the mode specified by mode (e.g. VM_MODE_P52V48_4K). + */ +struct kvm_vm *vm_create_without_vcpus(enum vm_guest_mode mode, uint64_t pages) +{ + struct kvm_vm *vm; + +#ifdef __x86_64__ + /* + * Permission needs to be requested before KVM_SET_CPUID2. + */ + vm_xsave_req_perm(); +#endif + vm = vm_create(mode, pages, O_RDWR); + + kvm_vm_elf_load(vm, program_invocation_name); + +#ifdef __x86_64__ + vm_create_irqchip(vm); +#endif + return vm; +} + /* * VM Create with customized parameters * @@ -393,13 +427,6 @@ struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, struct kvm_vm *vm; int i; -#ifdef __x86_64__ - /* - * Permission needs to be requested before KVM_SET_CPUID2. - */ - vm_xsave_req_perm(); -#endif - /* Force slot0 memory size not small than DEFAULT_GUEST_PHY_PAGES */ if (slot0_mem_pages < DEFAULT_GUEST_PHY_PAGES) slot0_mem_pages = DEFAULT_GUEST_PHY_PAGES; @@ -419,13 +446,8 @@ struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, nr_vcpus, kvm_check_cap(KVM_CAP_MAX_VCPUS)); pages = vm_adjust_num_guest_pages(mode, pages); - vm = vm_create(mode, pages, O_RDWR); - kvm_vm_elf_load(vm, program_invocation_name); - -#ifdef __x86_64__ - vm_create_irqchip(vm); -#endif + vm = vm_create_without_vcpus(mode, pages); for (i = 0; i < nr_vcpus; ++i) { uint32_t vcpuid = vcpuids ? vcpuids[i] : i; From patchwork Fri Jan 21 22:29:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Dunn X-Patchwork-Id: 12720329 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A5267C433FE for ; Fri, 21 Jan 2022 22:29:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230362AbiAUW3t (ORCPT ); Fri, 21 Jan 2022 17:29:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229949AbiAUW3s (ORCPT ); Fri, 21 Jan 2022 17:29:48 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08E18C06173B for ; Fri, 21 Jan 2022 14:29:48 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id o9-20020a170902d4c900b0014ab4c82aacso2195018plg.16 for ; Fri, 21 Jan 2022 14:29:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=ccjI/IWR5Whh2qHqkqlzXUnNsqIJF67ztrFNI0VfcTU=; b=rQ+aq92nBfOUrsMfWPnWTUsKkN4XRhmHDAtMMVmKMPvxSh6uY5Ku7QDdXq75X/tlxe pOYL6nT3fTiwTwfVf7NQdu+k65FX8A7VWloE5UWTXcNqYghoIC2ToAWA+ilMVLB+27gF G4NEDZOq7zWipi4ZoKkD8dLK72Zn4m0Aq10jr24e64mYeKWw3U6j9UZw+6jV9ahreuKT vxHNl8hukSU1GQi68T3xxQ9Jo5uMDVFA5p50SF4eU8ZIQGtf3bvU8JxUp+6T+z/IX3ie p+Tcrnv1Ulw+nTojosgnD0u1b406P3VMz61ZvWZWCewFaJTowCRDJomfupq9KpKn7W3K 4UWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ccjI/IWR5Whh2qHqkqlzXUnNsqIJF67ztrFNI0VfcTU=; b=aHKBtjkuNSVeUZo7/YcZK1LJc1zST/bx46nGlbGeahLe/f5CPXiw+4JmWmOfCjUhvT ARUS4yS03rtJEb7t52qfK+ec1iAUxt/caImg9EMCx+PoUBMCUTZLdTQr7NPyNsccQ3zB Zz0gN410OXiX8tYn1JzVRTNUnnHdtUgtu4I7KywnuT8Io5Hr5BYQRFjNnqytkl1vymlw fA5g4C188NsGoJL7xNg47IbSEdDDyL2bnwohgYjqp2/zQU2LJ3lowCrAGSF16bxX9BMb KiNhf6mvW3+0YfqZ+wVUZWwZT3hb5jJkMcJZM8HUpSlwWJ8s8TZOof3Nr6Q5S1kg/de4 5hzA== X-Gm-Message-State: AOAM5325IWjBnNcm9iVS8sRm72YMVWq4VCNccJ1HLhPUdK3aBtIT/yZl wSVyG6kE3Bzm0Cdh/6uIAQH5xhZTG93p/nfCDYUux07hRBHLLzhbrmrTNLwohRlqFeURYk/4Ayv Y4PQKlzd7PzKtS3wU/mnbLocBftaet32x5wI3J1nQVC/jSAurFaapbGWL62CX84Dk1w== X-Google-Smtp-Source: ABdhPJwfWXndTh6X8eWgatCKmTxULPAEYfzSnr0flSVrN/nrg1dtkdRcAjZSlg3fTmXTAigL9r1S1xCKNal5IG0= X-Received: from daviddunn-glinux.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:782]) (user=daviddunn job=sendgmr) by 2002:a63:4c09:: with SMTP id z9mr2201896pga.471.1642804187375; Fri, 21 Jan 2022 14:29:47 -0800 (PST) Date: Fri, 21 Jan 2022 22:29:33 +0000 In-Reply-To: <20220121222933.696067-1-daviddunn@google.com> Message-Id: <20220121222933.696067-4-daviddunn@google.com> Mime-Version: 1.0 References: <20220121222933.696067-1-daviddunn@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v4 3/3] KVM: selftests: Verify disabling PMU virtualization via KVM_CAP_CONFIG_PMU From: David Dunn To: kvm@vger.kernel.org, pbonzini@redhat.com, like.xu.linux@gmail.com, jmattson@google.com, cloudliang@tencent.com, seanjc@google.com Cc: daviddunn@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org On a VM with PMU disabled via KVM_CAP_PMU_CONFIG, the PMU will not be usable by the guest. On Intel, this causes a #GP. And on AMD, the counters no longer increment. KVM_CAP_PMU_CONFIG must be invoked on a VM prior to creating VCPUs. Signed-off-by: David Dunn --- .../kvm/x86_64/pmu_event_filter_test.c | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c b/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c index c715adcbd487..cffa2e019eac 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c @@ -325,6 +325,49 @@ static void test_not_member_allow_list(struct kvm_vm *vm) TEST_ASSERT(!count, "Disallowed PMU Event is counting"); } +/* + * Verify KVM_CAP_PMU_DISABLE prevents the use of the PMU. + * + * Note that KVM_CAP_PMU_CAPABILITY must be invoked prior to creating VCPUs. + */ +static void test_pmu_config_disable(void (*guest_code)(void)) +{ + int r; + struct kvm_vm *vm; + struct kvm_cpuid2 *cpuid2; + struct kvm_cpuid_entry2 *entry; + struct kvm_enable_cap cap = { 0 }; + bool sane; + + r = kvm_check_cap(KVM_CAP_PMU_CAPABILITY); + if ((r & KVM_CAP_PMU_DISABLE) == 0) + return; + + vm = vm_create_without_vcpus(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES); + + cap.cap = KVM_CAP_PMU_CAPABILITY; + cap.args[0] = KVM_CAP_PMU_DISABLE; + r = vm_enable_cap(vm, &cap); + TEST_ASSERT(r == 0, "Failed KVM_CAP_PMU_DISABLE."); + + vm_vcpu_add_default(vm, VCPU_ID, guest_code); + vm_init_descriptor_tables(vm); + vcpu_init_descriptor_tables(vm, VCPU_ID); + + cpuid2 = vcpu_get_cpuid(vm, VCPU_ID); + entry = get_cpuid(cpuid2, 0xA, 0); + if (entry) { + TEST_ASSERT(entry->eax == 0 && entry->ebx == 0 && + entry->ecx == 0 && entry->edx == 0, + "CPUID should not advertise PMU when disabled."); + } + + sane = sanity_check_pmu(vm); + TEST_ASSERT(!sane, "Guest should not be able to use disabled PMU."); + + kvm_vm_free(vm); +} + /* * Check for a non-zero PMU version, at least one general-purpose * counter per logical processor, an EBX bit vector of length greater @@ -430,5 +473,7 @@ int main(int argc, char *argv[]) kvm_vm_free(vm); + test_pmu_config_disable(guest_code); + return 0; }