All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Dunn <daviddunn@google.com>
To: kvm@vger.kernel.org, pbonzini@redhat.com,
	like.xu.linux@gmail.com, jmattson@google.com,
	cloudliang@tencent.com
Cc: daviddunn@google.com
Subject: [PATCH 3/3] Verify KVM_CAP_ENABLE_PMU in kvm pmu_event_filter_test selftest.
Date: Wed, 19 Jan 2022 18:28:18 +0000	[thread overview]
Message-ID: <20220119182818.3641304-3-daviddunn@google.com> (raw)
In-Reply-To: <20220119182818.3641304-1-daviddunn@google.com>

After disabling PMU using KVM_CAP_ENABLE_PMU, the PMU should no longer
be visible to the guest.  On Intel, this causes a #GP and on AMD the
counters are no longer functional.

Signed-off-by: David Dunn <daviddunn@google.com>
---
 .../kvm/x86_64/pmu_event_filter_test.c        | 29 +++++++++++++++++++
 1 file changed, 29 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 aa104946e6e0..0bd502d3055c 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,34 @@ static void test_not_member_allow_list(struct kvm_vm *vm)
 	TEST_ASSERT(!count, "Disallowed PMU Event is counting");
 }
 
+/*
+ * Verify that disabling PMU using KVM_CAP_ENABLE_PMU does not allow PMU.
+ *
+ * After every change to CAP_ENABLE_PMU, SET_CPUID2 is required to refresh
+ * KVM PMU state on existing VCPU.
+ */
+static void test_cap_enable_pmu(struct kvm_vm *vm)
+{
+	int r;
+	struct kvm_cpuid2 *cpuid2;
+	struct kvm_enable_cap cap = { .cap = KVM_CAP_ENABLE_PMU };
+	bool sane;
+
+	r = kvm_check_cap(KVM_CAP_ENABLE_PMU);
+	if (!r)
+		return;
+
+	cpuid2 = vcpu_get_cpuid(vm, VCPU_ID);
+
+	cap.args[0] = 0;
+	r = vm_enable_cap(vm, &cap);
+	vcpu_set_cpuid(vm, VCPU_ID, cpuid2);
+
+	sane = sanity_check_pmu(vm);
+
+	TEST_ASSERT(!sane, "Guest should not see PMU when disabled.");
+}
+
 /*
  * Check for a non-zero PMU version, at least one general-purpose
  * counter per logical processor, an EBX bit vector of length greater
@@ -431,6 +459,7 @@ int main(int argc, char *argv[])
 	test_member_allow_list(vm);
 	test_not_member_deny_list(vm);
 	test_not_member_allow_list(vm);
+	test_cap_enable_pmu(vm);
 
 	kvm_vm_free(vm);
 
-- 
2.34.1.703.g22d0c6ccf7-goog


  parent reply	other threads:[~2022-01-19 18:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-19 18:28 [PATCH 1/3] Provide VM capability to disable PMU virtualization for individual VMs David Dunn
2022-01-19 18:28 ` [PATCH 2/3] Verify that the PMU event filter works as expected David Dunn
2022-01-19 18:28 ` David Dunn [this message]
2022-01-20  1:15 ` [PATCH 1/3] Provide VM capability to disable PMU virtualization for individual VMs Sean Christopherson
2022-01-20  3:00   ` David Dunn
2022-01-20  3:02 ` Like Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220119182818.3641304-3-daviddunn@google.com \
    --to=daviddunn@google.com \
    --cc=cloudliang@tencent.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=like.xu.linux@gmail.com \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.