From patchwork Fri Jan 21 00:29:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Dunn X-Patchwork-Id: 12719185 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 206C4C433F5 for ; Fri, 21 Jan 2022 00:30:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378401AbiAUAaV (ORCPT ); Thu, 20 Jan 2022 19:30:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347824AbiAUAaT (ORCPT ); Thu, 20 Jan 2022 19:30:19 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73EF5C061574 for ; Thu, 20 Jan 2022 16:30:19 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id m200-20020a628cd1000000b004c7473d8cb5so209586pfd.5 for ; Thu, 20 Jan 2022 16:30:19 -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=39yPp9RoXGDts+zygPxTJfWP0AtmMcXoUZ2TJ95lwms=; b=BYnwiV2u7Bmm9SG+BxxiKqw+fK+xa1I2gkPyQ7cO5OpBQMcfaBs5hyrGgVkf4l89K8 yUqIh9DpHkXcnFWzrq4y+Da8BQ3FdoxukhYccUApM43zithPh+DpMTDMGHxqCnTUWO4X sV2xhwhfIwpAaBS/ulIxFBelY1CJR9HoL3YLnxUqmT2+Rs75JTRPrEqHC7Y7ayBRV5W3 S5xzOTB9GKSt88kiOW8burqDpJB1imJ+inX1vlTcvjlUP937XDioq9xGDqHaWjGIWjsc XOTv1UqqO8w8XR5iI9vLfEzEf15ONuSm31xM9CRDex5sSJ8nmzgn4KH+2O+erh/f7UbD RahA== 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=39yPp9RoXGDts+zygPxTJfWP0AtmMcXoUZ2TJ95lwms=; b=7J35S09E0G3pOoYQ1+7mSraTs9Rg76YYqSflIO2NM3pA+1nIcCV7V4+jUafN4dy9Yf mqvYY6FyvcgjzvW4rHsVehvCkVc1IJioFQfDJW/q14Ta1ep0CNdBAD5ZPiWREChTtjr/ sFLf49z2dPQQ8b3WSoGQ1PIlyGI3v4iolQjMnLd0GsjJhd4psRji1HMSsFOCkC/8TdZU FcuBxAL71aprLSS/YIYkPXFFVveQSKjQWn8FHvWyIbGpBNutdkW43nWK+41sOJqXb5MV 2Nz9IMXNWy67F+1BL6Vt3RfnoJnH+QxTOTO1kzqted0y0Z0V0jzYXxeBJwMV7Lkdl/aV nfzg== X-Gm-Message-State: AOAM530KTDstQiTZ3vX70DKJtqAc/uFPAPPPJTpO9l9ve8sVRRp+CC5h dUVUiOL5IqF0/DjDrAIlHuo08mQiqTjY76MV0B9KEWaBt4AbSw8EDASH7c5X0PUVlOEkpFtwzcR 4H+x+4EEsvIdx7LJzcmXA0PMaRloKp0RwfPMhLnXy0BHGBNVw63gdHyNJrnW/VUb7UQ== X-Google-Smtp-Source: ABdhPJzCLBeCtgb5GweHQQLf5y01BOZqcDQEhr1DlYZh6P8iFuDLVy+Zyc4XveNb5JpOc7/cRnPAPuOHzFwkwGc= X-Received: from daviddunn-glinux.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:782]) (user=daviddunn job=sendgmr) by 2002:a17:902:ecca:b0:14a:e540:6c76 with SMTP id a10-20020a170902ecca00b0014ae5406c76mr1369427plh.20.1642725018840; Thu, 20 Jan 2022 16:30:18 -0800 (PST) Date: Fri, 21 Jan 2022 00:29:50 +0000 In-Reply-To: <20220121002952.241015-1-daviddunn@google.com> Message-Id: <20220121002952.241015-2-daviddunn@google.com> Mime-Version: 1.0 References: <20220121002952.241015-1-daviddunn@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v2 1/3] Provide VM capability to disable PMU virtualization for individual VMs From: David Dunn To: kvm@vger.kernel.org, pbonzini@redhat.com, like.xu.linux@gmail.com, jmattson@google.com, cloudliang@tencent.com Cc: daviddunn@google.com Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org KVM_PMU_CONFIG_DISABLE can be 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_CONFIG reports available settings when queried via check_extension. Since KVM_GET_SUPPORTED_CPUID reports the maximal CPUID information based on module parameters, usermode will need to adjust CPUID when disabling PMU virtualization on individual VMs. Signed-off-by: David Dunn --- 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 ++++ 6 files changed, 23 insertions(+), 2 deletions(-) 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/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..42a98635bea5 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_CONFIG: + r = enable_pmu ? KVM_PMU_CONFIG_VALID : 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_CONFIG: + r = -EINVAL; + if (!enable_pmu || kvm->created_vcpus > 0 || + cap->args[0] & ~KVM_PMU_CONFIG_VALID) + break; + kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CONFIG_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..57a1280fa43b 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_CONFIG 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_PMU_CONFIG_DISABLE (1 << 0) +#define KVM_PMU_CONFIG_VALID (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. diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h index 9563d294f181..57a1280fa43b 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_CONFIG 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_PMU_CONFIG_DISABLE (1 << 0) +#define KVM_PMU_CONFIG_VALID (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 00:29:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Dunn X-Patchwork-Id: 12719186 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 7B31FC433F5 for ; Fri, 21 Jan 2022 00:30:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378404AbiAUAaZ (ORCPT ); Thu, 20 Jan 2022 19:30:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378408AbiAUAaY (ORCPT ); Thu, 20 Jan 2022 19:30:24 -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 1A690C061748 for ; Thu, 20 Jan 2022 16:30:24 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id h9-20020a170902680900b0014adffcba60so1238487plk.7 for ; Thu, 20 Jan 2022 16:30:24 -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=erq9giT+nz2vof3ybLlfjog6bGmIW7l1hEFH/EpVmtcxFwp1vSjBfSF8KdD7Nj6qL2 KBD28MC5m8SOiK4YheM3fE1zoa4O3CBtFlxortyM9Mssm/SAuQhrjaC+X7nVJonXHu8C vkQpwZKqLx78neOesCuzslqkIiFdN4TSsXFs8/f9n3kO80OWgco2vgaRUbHbErGjKB7B UR9mEoEUGwgMl3HwC7OXgWWHxxGfvWfIcDdHXLqBcni8hq+qZHMS/vm+A45E+WQk5iwZ K6pPklS2hWvah6gLxPGcDMMV8LtL1qhVuYO0yfZvaWH4V5aIpRU8oaNtYsLUVOX551Uh AYqQ== 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=rY7Z1K6/jewCwTKihGePKUKP8rkaMX5+w5qRqJaf6DD2XIjFVuMOZzMfALV4igMXAt gauUEDHw6l+OI6VZEP5B9s3iu6mIN+iZsLnYIx84/LX1yzZ8t0O8AVl92NrqbDt87QMl cXe1DA6sZ0ykO3T/+PA5Ge+/SupEwfLz4d1iTUTKUR+jnUlAil5YscG2Fau+LMymem9R jBK6o9o18402v6Stwi75jipDPAayqI60Y8qIPdjSQiyEWVCgaqnYkg3bLL91PEanTQul Jfv8l7kvbvPQeZncg7SMm+zjDRpxWoAfZ0ypPvaa2jEPm3kTHByfkeyboqnEN8ESpc64 gMzQ== X-Gm-Message-State: AOAM530gedlSZt+JAsgIlzjGFcCbKH3uTGZdbLLLsupQl9vAkmx5noxX JPW4MzYFHCGY94Jln2lSKkAGH8szGC+NszRG3hizpiQF30jcKuzDwrT3NjavuBY8/9nCb2Zn54T lGi+xYJmq7ikuf5ZmapKc2iWWNH/Fm1MEWTzyRU/gT4QnIxn1NR2MQj6HR/IDTG9Rww== X-Google-Smtp-Source: ABdhPJwcDJszuCRohcH0AQWDfbTMTmDn/EwZVT1DcoXbjS3PwV9sQQN/KR8bRWUhemH3q0EPBckDtP0jI3qz9ZI= X-Received: from daviddunn-glinux.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:782]) (user=daviddunn job=sendgmr) by 2002:a05:6a00:b8e:b0:4c2:860c:1cea with SMTP id g14-20020a056a000b8e00b004c2860c1ceamr1489996pfj.22.1642725023490; Thu, 20 Jan 2022 16:30:23 -0800 (PST) Date: Fri, 21 Jan 2022 00:29:51 +0000 In-Reply-To: <20220121002952.241015-1-daviddunn@google.com> Message-Id: <20220121002952.241015-3-daviddunn@google.com> Mime-Version: 1.0 References: <20220121002952.241015-1-daviddunn@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v2 2/3] selftests: introduce function to create test 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 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 00:29:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Dunn X-Patchwork-Id: 12719187 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 6C753C433EF for ; Fri, 21 Jan 2022 00:30:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378408AbiAUAab (ORCPT ); Thu, 20 Jan 2022 19:30:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378402AbiAUAaa (ORCPT ); Thu, 20 Jan 2022 19:30:30 -0500 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD6D8C061574 for ; Thu, 20 Jan 2022 16:30:29 -0800 (PST) Received: by mail-qk1-x74a.google.com with SMTP id b141-20020a376793000000b0047910c74c9dso5514717qkc.4 for ; Thu, 20 Jan 2022 16:30:29 -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=q/IAOHN4sH5IHagVn8dkrSpgnhYRJyTC5Xd3XT8xJb8=; b=HWm9tHoOK5Sxkc0pwiMzjeHFtqcLQo2o/ZlOpfyFIOzEoNQenIgIJEY/0aP0MM4COH tC90fKnehsoBojGM6DPJJoluL09XFBMBGtfahi5HJdBEFRwEFeOY5H+SiPl9R7PzxXAG 9Vegqxm9CiHstHi7xV4NzpQayMWGKoE/yEJBA9lGFjYpBTl81BsABZrBeO8t9SM9Xda/ qgUTVRNokcIKzNiJJZTmxDjC/KG9IJTsuuMrXOK4+wq2O+PAtX/jExbpkelPUk6V6THh XlB8lNDMv8hZRASJ5RHo6gC/NPgkZzHlEqkbwBWUh41sYVFlcJdqw9myBgvBqOIejaiY UxXw== 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=q/IAOHN4sH5IHagVn8dkrSpgnhYRJyTC5Xd3XT8xJb8=; b=662GipFO0fOdHUEf2KV87Fttg1RLauq4ECf3BeC+24L+5G4YgxyoQrDokeHmY+RAZb nqnO6/0fX3j38kVOaxv9NxRALaE8qKrnNYCYryuX09fX6AxPzY5xMEyutCaGz+xmm7nB K5bpCUjDZzxPOygvnHiUF4ZhRkARNRGx0qpAexqziWWQLr3HFqqC3CGj+qDAIcMiORS2 Lfo6fCFQegdIHzZgzgt4nwtGZzwr8Y0TS2/nwYXkGobwfmQ3XuViOFnVFJjyv1DVQIUs BjgOHCWHfGalYZATyDYeHSoCr99USFKMuVKIB2vkqU23yv9p84YIQ4tOuO0ZfoIex7h7 iAxw== X-Gm-Message-State: AOAM5303wlhmlGy+Ou9IrdwnPZckE5mGinbit+5xaRBCZz+kUe9JAW+1 n6LaXyf9vTglCvSEM1I/mWrN1hPzHkvyR2gP7bU1rHodUUyawgp5ni/Qbfub8aHliBHCPpf3XH/ xUclsT7d5iu9Vh5c24YnH3gPXT50wsfr/mGrKoGn40HOjhNk5usQ5MXVKvFg3mXGXFg== X-Google-Smtp-Source: ABdhPJwCNyM8n/5wunHDtnKFo3aEmYpOTJpLZ1aY2mHjNX+sGJPJ8KevzsOjUDOHMj8VzXN/9bCxOHM2speh62Y= X-Received: from daviddunn-glinux.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:782]) (user=daviddunn job=sendgmr) by 2002:ac8:5802:: with SMTP id g2mr1511554qtg.100.1642725028811; Thu, 20 Jan 2022 16:30:28 -0800 (PST) Date: Fri, 21 Jan 2022 00:29:52 +0000 In-Reply-To: <20220121002952.241015-1-daviddunn@google.com> Message-Id: <20220121002952.241015-4-daviddunn@google.com> Mime-Version: 1.0 References: <20220121002952.241015-1-daviddunn@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [PATCH v2 3/3] selftests: Verify disabling PMU 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 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 | 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..40fecaa4ea9f 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_PMU_CONFIG_DISABLE prevents the use of the PMU. + * + * Note that KVM_CAP_PMU_CONFIG 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_CONFIG); + if ((r & KVM_PMU_CONFIG_DISABLE) == 0) + return; + + vm = vm_create_without_vcpus(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES); + + cap.cap = KVM_CAP_PMU_CONFIG; + cap.args[0] = KVM_PMU_CONFIG_DISABLE; + r = vm_enable_cap(vm, &cap); + TEST_ASSERT(r == 0, "Failed KVM_PMU_CONFIG_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 see PMU when disabled."); + + 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; }