From patchwork Wed Feb 9 17:29:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Dunn X-Patchwork-Id: 12740604 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 D4E56C433F5 for ; Wed, 9 Feb 2022 17:29:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238303AbiBIR3w (ORCPT ); Wed, 9 Feb 2022 12:29:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232618AbiBIR3u (ORCPT ); Wed, 9 Feb 2022 12:29:50 -0500 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4936DC0613C9 for ; Wed, 9 Feb 2022 09:29:53 -0800 (PST) Received: by mail-pj1-x104a.google.com with SMTP id z12-20020a17090a1fcc00b001b63e477f9fso2114470pjz.3 for ; Wed, 09 Feb 2022 09:29:53 -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=Mb03SItjo55ZPsSHbw8i8qIOyjBuu5+GBiaBDmefomY=; b=TgyvBw4T57OXvTyqbg9wUHHZ6CUISNxEPJPSEim8TjEkaKtIfJGO2T27LDa6q7dOKA 840UplkQg0cHbNwakMZezXazJzF4pgDyil+xFFiAAg9kAMEpjoNtMTv3L5p4KVa/KOP9 MCXceafRwI1WhaSYgTNGGhUrMmK7npQ0gwYU+L5N5PelGQnsMbBgKHvb56N/7sNMIVrU ErriqxQ8o9t0IuIY5iE6RJ3m2Gu1OW7mMXM28ASIrvjMMvs2Q/6/tY5JbHxNFIBlJNUU lPwLW5YY9zJJICfFaZbpFeefJj91ZOrG1iX2pDLAYwdnPGhs4CNtkB78uRYKxYEfPDDb Q/xg== 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=Mb03SItjo55ZPsSHbw8i8qIOyjBuu5+GBiaBDmefomY=; b=iDTQXjqzrueiqkKaWag7uPyezJysLsZc7PA/BKX8MSe/Q8A8hMCfM64Enf/tiM4Aqs kBTCcD6g/Vx21XjkgsmXfHn3FZzE8t2oq7yBhsm9WerUNSyGXlZ7wBvMI/t+l8Kmk62x IadTItOJ8a+sKeaGuU7P71D2qgORnZaBdV3q6re40S3LZVk9UmMp8Om1mOt9FtpF9W3J MxPg8LTnX+juXzgTE37w/T7FZGaWrfWf6HlQx6dTx73SSek/i4vR5Vjx1Cel/QywLibP 5Db08dQQHXsICPYgWf5+nseDq1meA01hQVUO85lw/fQKJ+RVxfMQRC8UAQrYFNiug1Uf b6Lg== X-Gm-Message-State: AOAM531BsPV7PvTII0wPECilPGbGhbsb01ZWZJHqr35hvOhtap4wGJHu XxRBYN8PZ1jAkzVNZXUV+6757nHk9JHZ3V0= X-Google-Smtp-Source: ABdhPJzJuwCMIj1ZCtdnJqpy1qx4IpKNvuKOlLqEcdg0qzUW3SzydnYH+A7CJMoRgW7eQyeXPkYWXVPHRT3X6YE= X-Received: from daviddunn-glinux.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:782]) (user=daviddunn job=sendgmr) by 2002:a17:903:230c:: with SMTP id d12mr3155012plh.74.1644427792733; Wed, 09 Feb 2022 09:29:52 -0800 (PST) Date: Wed, 9 Feb 2022 17:29:43 +0000 In-Reply-To: <20220209172945.1495014-1-daviddunn@google.com> Message-Id: <20220209172945.1495014-2-daviddunn@google.com> Mime-Version: 1.0 References: <20220209172945.1495014-1-daviddunn@google.com> X-Mailer: git-send-email 2.35.0.263.gb82422642f-goog Subject: [PATCH v6 1/3] KVM: x86: Provide per VM capability for disabling PMU virtualization From: David Dunn To: pbonzini@redhat.com Cc: seanjc@google.com, jmattson@google.com, like.xu.linux@gmail.com, kvm@vger.kernel.org, David Dunn 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, usermode will need to clear CPUID leaf 0xA to notify guests. Signed-off-by: David Dunn --- Documentation/virt/kvm/api.rst | 22 ++++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 1 + 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 ++++ 7 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index a4267104db50..df836965b347 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7561,3 +7561,25 @@ 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 +:Parameters: arg[0] is bitmask of PMU virtualization capabilities. +:Returns 0 on success, -EINVAL when arg[0] contains invalid bits + +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. Usermode +should adjust CPUID leaf 0xA to reflect that the PMU is disabled. diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index c371ee7e45f7..f832cd0f9b27 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/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 03fab48b149c..4e5b1eeeb77c 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 6f69f3e3635e..c35a6a193bf4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4329,6 +4329,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; @@ -6003,6 +6006,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; @@ -11630,6 +11641,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 5191b57e1562..cf6774cc18ef 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1134,6 +1134,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_VM_GPA_BITS 207 #define KVM_CAP_XSAVE2 208 #define KVM_CAP_SYS_ATTRIBUTES 209 +#define KVM_CAP_PMU_CAPABILITY 210 #ifdef KVM_CAP_IRQ_ROUTING @@ -1970,6 +1971,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 b46bcdb0cab1..00fb392e1742 100644 --- a/tools/include/uapi/linux/kvm.h +++ b/tools/include/uapi/linux/kvm.h @@ -1134,6 +1134,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_VM_GPA_BITS 207 #define KVM_CAP_XSAVE2 208 #define KVM_CAP_SYS_ATTRIBUTES 209 +#define KVM_CAP_PMU_CAPABILITY 210 #ifdef KVM_CAP_IRQ_ROUTING @@ -1973,6 +1974,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 Wed Feb 9 17:29:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Dunn X-Patchwork-Id: 12740605 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 1399FC433EF for ; Wed, 9 Feb 2022 17:30:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238322AbiBIR3z (ORCPT ); Wed, 9 Feb 2022 12:29:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232618AbiBIR3x (ORCPT ); Wed, 9 Feb 2022 12:29:53 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B564C05CB86 for ; Wed, 9 Feb 2022 09:29:56 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id ay10-20020a17090b030a00b001b8a4029ba0so4236314pjb.5 for ; Wed, 09 Feb 2022 09:29:56 -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=cE6nPhqUrdzaQ7066nYi569JLAMVfUJ5DNprAL1or7c=; b=LxvqmeRyjdn9uhoRdunTjzoeDd/+jD2yoE3Dg67uRUJpy7WeRNSJDVFpf7YTQOP37U loLFD2syK4/T4k9JHDAoRDWwcR9LQiXCLMFTWTL9excgLqYO9m6MaaGE7HrrBGWUL6iS 1NkWjojK3KySrNFfg4FzdkmZpA1Vl/xRLjABoY+tr9q09+w9p3tvZRU4wR618XSnhdUy 59NCIf72SAJxgQaBTG2ubyT0aWoUzp5sZYU4KAQ1MPyQALU8pyQY3y6TEYxiWBiEjby3 bl+mZbA0+0tb3KR085MEAngB+VqocUVFFEsMt0bPjUFKrhJGG0SxgprUZymrC4ekCQ9i MXdQ== 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=cE6nPhqUrdzaQ7066nYi569JLAMVfUJ5DNprAL1or7c=; b=mXQZJpqHzGmQV99fa61in8CBXJTW8vIpwuz5OkqBJ6aFp4tP/vJPXXaU59wGpgJdDU HvsBGfeY7qazP9ZwIQTYmO2H7qjjmyfmBgL7nc5tVvL27WCy7M5PfhkkKhXo9UTRNf3s rwJMMm/+kQYVbu8DIUR86lhuWZOZnLUMwk3zO6gLucRzxHLvfJ/rHlayubKh1iErT0SN N/za4NMdQagcu7BETn3iIyPztelExYab4twSe/Afb4fb2Af8CrGI7QKwEvdE10glUbgr tUeaM18BEGbJCx56BI2NYo+YsDz9jZF1KTeSwnPDAjruZPDHw0aD8cfzQXaQn2cm8CVQ JQyw== X-Gm-Message-State: AOAM533Z0ZIpFNqoB6QwF0lkxlmqLNvnGuKoxpmCewDFyQP11a0EK+cv mr/COhkrxSdE6vOE5XbUVLNlSA30mwZQGNk= X-Google-Smtp-Source: ABdhPJwXFc+eXwLS6S6SnOulElBTcAYiQHfaYFn8YwxFX2r/7x8cPSYtYX4P/0hs5Y6CcefBngtzprC+LlgEcsg= X-Received: from daviddunn-glinux.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:782]) (user=daviddunn job=sendgmr) by 2002:a05:6a00:1d9f:: with SMTP id z31mr3075554pfw.38.1644427795727; Wed, 09 Feb 2022 09:29:55 -0800 (PST) Date: Wed, 9 Feb 2022 17:29:44 +0000 In-Reply-To: <20220209172945.1495014-1-daviddunn@google.com> Message-Id: <20220209172945.1495014-3-daviddunn@google.com> Mime-Version: 1.0 References: <20220209172945.1495014-1-daviddunn@google.com> X-Mailer: git-send-email 2.35.0.263.gb82422642f-goog Subject: [PATCH v6 2/3] KVM: selftests: Allow creation of selftest VM without vcpus From: David Dunn To: pbonzini@redhat.com Cc: seanjc@google.com, jmattson@google.com, like.xu.linux@gmail.com, kvm@vger.kernel.org, David Dunn 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 | 35 +++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 4ed6aa049a91..2bdf96f520aa 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 d8cf851ab119..52f1f530564e 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -362,6 +362,34 @@ 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; + + 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 * @@ -412,13 +440,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 Wed Feb 9 17:29:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Dunn X-Patchwork-Id: 12740606 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 83B92C433F5 for ; Wed, 9 Feb 2022 17:30:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238321AbiBIR35 (ORCPT ); Wed, 9 Feb 2022 12:29:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238324AbiBIR3z (ORCPT ); Wed, 9 Feb 2022 12:29:55 -0500 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38195C0613C9 for ; Wed, 9 Feb 2022 09:29:59 -0800 (PST) Received: by mail-pj1-x104a.google.com with SMTP id a15-20020a17090ad80f00b001b8a1e1da50so2247241pjv.6 for ; Wed, 09 Feb 2022 09:29:59 -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=8OBoX65iKSlGteJ8KRsRO0AoOHBHi+mvW0Rr+JlImC4=; b=ZCL0lDmM040QdQ7x5ueHQEvbksPd0C2lX+TdG+eFbCZ5IUcF3N4Ret02g6XWp3235w T4EjHtjZfojCMqHj1BAxziiH3bjfWrh5ldjhXGmC0JdCZg1i5LfF4mfusyz+wwICbq4s 7rT4v1P3LqHzpojuXgM5ZSZ2A87suns5c8Ui5ywNvx6AxBiuU+UO/X+Wn+UCCOAW7CI7 JNR1dPAcd45MmZA/mDR5q6qS/F3cj7YQBej/qJqF1fA17D4D+xnQB8DbBwDulmV/saSa caYQ2Q7Q0cw+Q8ZCSA2W6qYVg0vw64q0O5J3kQ8mP5HJlW2ufhp+F+0B1TA8M8gKekbd bhpA== 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=8OBoX65iKSlGteJ8KRsRO0AoOHBHi+mvW0Rr+JlImC4=; b=kPYxeiLhCPp4opWbNiYamdNnGy0v/BictLCCR7oP+cbK/A/TiVCWMsU1Xgm1zZDfRj G7CcYM6pe/AEuZXgjX1dBIS1DcKhfdzOBr9aFOGXaopTBjW8C8SUk1YkOmgpx4MOZyCc Wabu999TrNtC1UGI1uY7mh4uoxKgSJfNsSudjafwcQnmsLvtrns99xsjgLCPjaQhQG8+ /SZjKySzmc+uR4CTPq/2UbdISkQ8tOoyCt+Wz7VFx5dD6kkHrcC+34f1zAbx0TWSqFuS 1oZNF1DSqUtvvGsOpzAZ48tBksKZpLKJRgu2RfVnVm2M+HuCrlhQs+pOBzMsuLAUm1ye 9Nnw== X-Gm-Message-State: AOAM531hTyLRRN3/HHwJKAUTzpMVyt/9E9BpRbgk1ozr3/9BOx1DaGKU pInWM2ZDTsrb/tVi7xpUXKFGzyGSRi/5mgg= X-Google-Smtp-Source: ABdhPJyOqWL9wvZd7gKHbxQ4sw4iacst1JTwmFMj1U2lPBHujiT31uwKyrJN5ab+OUD2GFl1XFC303JBYwGWHJE= X-Received: from daviddunn-glinux.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:782]) (user=daviddunn job=sendgmr) by 2002:a17:902:f786:: with SMTP id q6mr3101390pln.64.1644427798725; Wed, 09 Feb 2022 09:29:58 -0800 (PST) Date: Wed, 9 Feb 2022 17:29:45 +0000 In-Reply-To: <20220209172945.1495014-1-daviddunn@google.com> Message-Id: <20220209172945.1495014-4-daviddunn@google.com> Mime-Version: 1.0 References: <20220209172945.1495014-1-daviddunn@google.com> X-Mailer: git-send-email 2.35.0.263.gb82422642f-goog Subject: [PATCH v6 3/3] KVM: selftests: Verify disabling PMU virtualization via KVM_CAP_CONFIG_PMU From: David Dunn To: pbonzini@redhat.com Cc: seanjc@google.com, jmattson@google.com, like.xu.linux@gmail.com, kvm@vger.kernel.org, David Dunn 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 | 35 +++++++++++++++++++ 1 file changed, 35 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..7a4b99684d9d 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,39 @@ 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_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); + + 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 +463,7 @@ int main(int argc, char *argv[]) kvm_vm_free(vm); + test_pmu_config_disable(guest_code); + return 0; }