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, seanjc@google.com
Cc: daviddunn@google.com
Subject: [PATCH v3 3/3] KVM: selftests: Verify disabling PMU virtualization via KVM_CAP_CONFIG_PMU
Date: Fri, 21 Jan 2022 21:07:02 +0000 [thread overview]
Message-ID: <20220121210702.635477-4-daviddunn@google.com> (raw)
In-Reply-To: <20220121210702.635477-1-daviddunn@google.com>
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 <daviddunn@google.com>
---
.../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..9e38730d6f87 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 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;
}
--
2.35.0.rc0.227.g00780c9af4-goog
next prev parent reply other threads:[~2022-01-21 21:07 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-21 21:06 [PATCH v3 0/3] KVM: x86: Provide per VM capability for disabling PMU virtualization David Dunn
2022-01-21 21:07 ` [PATCH v3 1/3] " David Dunn
2022-01-21 21:07 ` [PATCH v3 2/3] KVM: selftests: Allow creation of selftest VM without vcpus David Dunn
2022-01-21 21:07 ` David Dunn [this message]
2022-01-21 22:20 ` [PATCH v3 0/3] KVM: x86: Provide per VM capability for disabling PMU virtualization David Dunn
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=20220121210702.635477-4-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 \
--cc=seanjc@google.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.