From: Zhao Liu <zhao1.liu@intel.com>
To: "Paolo Bonzini" <pbonzini@redhat.com>,
"Eric Blake" <eblake@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Michael Roth" <michael.roth@amd.com>,
"Daniel P . Berrangé" <berrange@redhat.com>,
"Eduardo Habkost" <eduardo@habkost.net>,
"Marcelo Tosatti" <mtosatti@redhat.com>,
"Shaoqin Huang" <shahuang@redhat.com>,
"Eric Auger" <eauger@redhat.com>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Laurent Vivier" <lvivier@redhat.com>,
"Thomas Huth" <thuth@redhat.com>,
"Sebastian Ott" <sebott@redhat.com>,
"Gavin Shan" <gshan@redhat.com>
Cc: qemu-devel@nongnu.org, kvm@vger.kernel.org, qemu-arm@nongnu.org,
Zhenyu Wang <zhenyu.z.wang@intel.com>,
Dapeng Mi <dapeng1.mi@intel.com>, Yuan Yao <yuan.yao@intel.com>,
Xiong Zhang <xiong.y.zhang@intel.com>,
Mingwei Zhang <mizhang@google.com>,
Jim Mattson <jmattson@google.com>, Zhao Liu <zhao1.liu@intel.com>
Subject: [RFC 5/5] i386/kvm: Support fixed counter in KVM PMU filter
Date: Wed, 10 Jul 2024 12:51:17 +0800 [thread overview]
Message-ID: <20240710045117.3164577-6-zhao1.liu@intel.com> (raw)
In-Reply-To: <20240710045117.3164577-1-zhao1.liu@intel.com>
KVM_SET_PMU_EVENT_FILTER of x86 KVM allows user to configure x86 fixed
function counters by a bitmap.
Add the support of x86-fixed-counter in kvm-pmu-filter object and handle
this in i386 kvm codes.
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
accel/kvm/kvm-pmu.c | 71 ++++++++++++++++++++++++++++++++++++++++
include/sysemu/kvm-pmu.h | 1 +
qapi/kvm.json | 36 +++++++++++++++++++-
target/i386/kvm/kvm.c | 69 +++++++++++++++++++++++++-------------
4 files changed, 153 insertions(+), 24 deletions(-)
diff --git a/accel/kvm/kvm-pmu.c b/accel/kvm/kvm-pmu.c
index 7a1720c68f8f..bad76482f426 100644
--- a/accel/kvm/kvm-pmu.c
+++ b/accel/kvm/kvm-pmu.c
@@ -256,6 +256,68 @@ fail:
qapi_free_KVMPMUFilterEventList(head);
}
+static void
+kvm_pmu_filter_get_fixed_counter(Object *obj, Visitor *v, const char *name,
+ void *opaque, Error **errp)
+{
+ KVMPMUFilter *filter = KVM_PMU_FILTER(obj);
+ KVMPMUX86FixedCounterVariant *str_counter;
+
+ str_counter = g_new(KVMPMUX86FixedCounterVariant, 1);
+ str_counter->action = filter->x86_fixed_counter->action;
+ str_counter->bitmap = g_strdup_printf("0x%x",
+ filter->x86_fixed_counter->bitmap);
+
+ visit_type_KVMPMUX86FixedCounterVariant(v, name, &str_counter, errp);
+ qapi_free_KVMPMUX86FixedCounterVariant(str_counter);
+}
+
+static void
+kvm_pmu_filter_set_fixed_counter(Object *obj, Visitor *v, const char *name,
+ void *opaque, Error **errp)
+{
+ KVMPMUFilter *filter = KVM_PMU_FILTER(obj);
+ KVMPMUX86FixedCounterVariant *str_counter;
+ KVMPMUX86FixedCounter *new_counter, *old_counter;
+ uint64_t bitmap;
+ int ret;
+
+ old_counter = filter->x86_fixed_counter;
+ if (!visit_type_KVMPMUX86FixedCounterVariant(v, name,
+ &str_counter, errp)) {
+ return;
+ }
+
+ new_counter = g_new(KVMPMUX86FixedCounter, 1);
+ new_counter->action = str_counter->action;
+
+ ret = qemu_strtou64(str_counter->bitmap, NULL,
+ 0, &bitmap);
+ if (ret < 0) {
+ error_setg(errp,
+ "Invalid x86 fixed counter (bitmap: %s): %s. "
+ "The bitmap must be a uint32 string.",
+ str_counter->bitmap, strerror(-ret));
+ g_free(new_counter);
+ goto fail;
+ }
+ if (bitmap > UINT32_MAX) {
+ error_setg(errp,
+ "Invalid x86 fixed counter (bitmap: %s): "
+ "Numerical result out of range. "
+ "The bitmap must be a uint32 string.",
+ str_counter->bitmap);
+ g_free(new_counter);
+ goto fail;
+ }
+ new_counter->bitmap = bitmap;
+ filter->x86_fixed_counter = new_counter;
+ qapi_free_KVMPMUX86FixedCounter(old_counter);
+
+fail:
+ qapi_free_KVMPMUX86FixedCounterVariant(str_counter);
+}
+
static void
kvm_pmu_filter_class_init(ObjectClass *oc, void *data)
{
@@ -266,6 +328,15 @@ kvm_pmu_filter_class_init(ObjectClass *oc, void *data)
NULL, NULL);
object_class_property_set_description(oc, "events",
"KVM PMU event list");
+
+ object_class_property_add(oc, "x86-fixed-counter",
+ "KVMPMUX86FixedCounter",
+ kvm_pmu_filter_get_fixed_counter,
+ kvm_pmu_filter_set_fixed_counter,
+ NULL, NULL);
+ object_class_property_set_description(oc, "x86-fixed-counter",
+ "Enablement bitmap of "
+ "x86 PMU fixed counter");
}
static void kvm_pmu_filter_instance_init(Object *obj)
diff --git a/include/sysemu/kvm-pmu.h b/include/sysemu/kvm-pmu.h
index 707f33d604fd..e74a6a665565 100644
--- a/include/sysemu/kvm-pmu.h
+++ b/include/sysemu/kvm-pmu.h
@@ -24,6 +24,7 @@ struct KVMPMUFilter {
uint32_t nevents;
KVMPMUFilterEventList *events;
+ KVMPMUX86FixedCounter *x86_fixed_counter;
};
/*
diff --git a/qapi/kvm.json b/qapi/kvm.json
index f4e8854fa6c6..fe9a9ec940be 100644
--- a/qapi/kvm.json
+++ b/qapi/kvm.json
@@ -123,6 +123,21 @@
'x86-default': 'KVMPMUX86DefalutEvent',
'x86-masked-entry': 'KVMPMUX86MaskedEntry' } }
+##
+# @KVMPMUX86FixedCounter:
+#
+# x86 fixed counter that KVM PMU filter supports.
+#
+# @action: action that KVM PMU filter will take.
+#
+# @bitmap: x86 fixed counter bitmap.
+#
+# Since 9.1
+##
+{ 'struct': 'KVMPMUX86FixedCounter',
+ 'data': { 'action': 'KVMPMUFilterAction',
+ 'bitmap': 'uint32' } }
+
##
# @KVMPMUFilterProperty:
#
@@ -208,6 +223,22 @@
'x86-default': 'KVMPMUX86DefalutEventVariant',
'x86-masked-entry': 'KVMPMUX86MaskedEntryVariant' } }
+##
+# @KVMPMUX86FixedCounterVariant:
+#
+# The variant of KVMPMUX86FixedCounter with the string, rather than
+# the numeric value.
+#
+# @action: action that KVM PMU filter will take.
+#
+# @bitmap: x86 fixed counter bitmap. This field is a uint32 string.
+#
+# Since 9.1
+##
+{ 'struct': 'KVMPMUX86FixedCounterVariant',
+ 'data': { 'action': 'KVMPMUFilterAction',
+ 'bitmap': 'str' } }
+
##
# @KVMPMUFilterPropertyVariant:
#
@@ -215,7 +246,10 @@
#
# @events: the KVMPMUFilterEventVariant list.
#
+# @x86-fixed-counter: enablement bitmap of x86 fixed counters.
+#
# Since 9.1
##
{ 'struct': 'KVMPMUFilterPropertyVariant',
- 'data': { '*events': ['KVMPMUFilterEventVariant'] } }
+ 'data': { '*events': ['KVMPMUFilterEventVariant'],
+ '*x86-fixed-counter': 'KVMPMUX86FixedCounterVariant' } }
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index 396a93efe745..b350c4123ea2 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -5419,6 +5419,7 @@ static bool kvm_install_pmu_event_filter(KVMState *s)
{
struct kvm_pmu_event_filter *kvm_filter;
KVMPMUFilter *filter = s->pmu_filter;
+ KVMPMUFilterAction action;
int ret;
kvm_filter = g_malloc0(sizeof(struct kvm_pmu_event_filter) +
@@ -5426,9 +5427,14 @@ static bool kvm_install_pmu_event_filter(KVMState *s)
/*
* Currently, kvm-pmu-filter only supports configuring the same
- * action for PMU events.
+ * action for PMU fixed-counters/events.
*/
- switch (filter->events->value->action) {
+ if (filter->x86_fixed_counter) {
+ action = filter->x86_fixed_counter->action;
+ } else {
+ action = filter->events->value->action;
+ }
+ switch (action) {
case KVM_PMU_FILTER_ACTION_ALLOW:
kvm_filter->action = KVM_PMU_EVENT_ALLOW;
break;
@@ -5439,23 +5445,29 @@ static bool kvm_install_pmu_event_filter(KVMState *s)
g_assert_not_reached();
}
- /*
- * The check in kvm_arch_check_pmu_filter() ensures masked entry
- * format won't be mixed with other formats.
- */
- kvm_filter->flags = filter->events->value->format ==
- KVM_PMU_EVENT_FMT_X86_MASKED_ENTRY ?
- KVM_PMU_EVENT_FLAG_MASKED_EVENTS : 0;
-
- if (kvm_filter->flags == KVM_PMU_EVENT_FLAG_MASKED_EVENTS &&
- !kvm_vm_check_extension(s, KVM_CAP_PMU_EVENT_MASKED_EVENTS)) {
- error_report("Masked entry format of PMU event "
- "is not supported by Host.");
- goto fail;
+ if (filter->x86_fixed_counter) {
+ kvm_filter->fixed_counter_bitmap = filter->x86_fixed_counter->bitmap;
}
- if (!kvm_config_pmu_event(filter, kvm_filter)) {
- goto fail;
+ if (filter->nevents) {
+ /*
+ * The check in kvm_arch_check_pmu_filter() ensures masked entry
+ * format won't be mixed with other formats.
+ */
+ kvm_filter->flags = filter->events->value->format ==
+ KVM_PMU_EVENT_FMT_X86_MASKED_ENTRY ?
+ KVM_PMU_EVENT_FLAG_MASKED_EVENTS : 0;
+
+ if (kvm_filter->flags == KVM_PMU_EVENT_FLAG_MASKED_EVENTS &&
+ !kvm_vm_check_extension(s, KVM_CAP_PMU_EVENT_MASKED_EVENTS)) {
+ error_report("Masked entry format of PMU event "
+ "is not supported by Host.");
+ goto fail;
+ }
+
+ if (!kvm_config_pmu_event(filter, kvm_filter)) {
+ goto fail;
+ }
}
ret = kvm_vm_ioctl(s, KVM_SET_PMU_EVENT_FILTER, kvm_filter);
@@ -6088,17 +6100,24 @@ static void kvm_arch_check_pmu_filter(const Object *obj, const char *name,
KVMPMUFilterAction action;
uint32_t base_flag;
- if (!filter->nevents) {
+ if (!filter->x86_fixed_counter && !filter->nevents) {
error_setg(errp,
"Empty KVM PMU filter.");
return;
}
/* Pick the first event's flag as the base one. */
- base_flag = events->value->format ==
- KVM_PMU_EVENT_FMT_X86_MASKED_ENTRY ?
- KVM_PMU_EVENT_FLAG_MASKED_EVENTS : 0;
+ base_flag = 0;
+ if (filter->nevents &&
+ events->value->format == KVM_PMU_EVENT_FMT_X86_MASKED_ENTRY) {
+ base_flag = KVM_PMU_EVENT_FLAG_MASKED_EVENTS;
+ }
+
action = KVM_PMU_FILTER_ACTION__MAX;
+ if (filter->x86_fixed_counter) {
+ action = filter->x86_fixed_counter->action;
+ }
+
while (events) {
KVMPMUFilterEvent *event = events->value;
uint32_t flag;
@@ -6128,9 +6147,13 @@ static void kvm_arch_check_pmu_filter(const Object *obj, const char *name,
if (action == KVM_PMU_FILTER_ACTION__MAX) {
action = event->action;
} else if (action != event->action) {
- /* TODO: Support events with different actions if necessary. */
+ /*
+ * TODO: Support fixed-counters/events with different actions
+ * if necessary.
+ */
error_setg(errp,
- "Only support PMU events with the same action");
+ "Only support PMU fixed-counters/events "
+ "with the same action");
return;
}
--
2.34.1
next prev parent reply other threads:[~2024-07-10 4:37 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-10 4:51 [RFC 0/5] accel/kvm: Support KVM PMU filter Zhao Liu
2024-07-10 4:51 ` [RFC 1/5] qapi/qom: Introduce kvm-pmu-filter object Zhao Liu
2024-07-10 4:51 ` [RFC 2/5] i386/kvm: Support initial KVM PMU filter Zhao Liu
2024-07-10 4:51 ` [RFC 3/5] i386/kvm: Support event with select&umask format in " Zhao Liu
2024-07-18 5:28 ` Mi, Dapeng
2024-07-19 8:40 ` Zhao Liu
2024-07-23 5:36 ` Mi, Dapeng
2024-07-10 4:51 ` [RFC 4/5] i386/kvm: Support event with masked entry " Zhao Liu
2024-07-10 4:51 ` Zhao Liu [this message]
2024-07-18 5:27 ` [RFC 0/5] accel/kvm: Support " Mi, Dapeng
2024-07-19 8:05 ` Zhao Liu
2024-08-02 9:01 ` Shaoqin Huang
2024-08-02 9:37 ` Zhao Liu
2024-08-02 9:41 ` Shaoqin Huang
2024-08-02 10:03 ` Zhao Liu
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=20240710045117.3164577-6-zhao1.liu@intel.com \
--to=zhao1.liu@intel.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=dapeng1.mi@intel.com \
--cc=eauger@redhat.com \
--cc=eblake@redhat.com \
--cc=eduardo@habkost.net \
--cc=gshan@redhat.com \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=lvivier@redhat.com \
--cc=michael.roth@amd.com \
--cc=mizhang@google.com \
--cc=mtosatti@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=sebott@redhat.com \
--cc=shahuang@redhat.com \
--cc=thuth@redhat.com \
--cc=xiong.y.zhang@intel.com \
--cc=yuan.yao@intel.com \
--cc=zhenyu.z.wang@intel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).