Skip to contentSkip to conversation

kvm: x86/pmu: Introduce and test masked events

PATCH Superseded Aaron Lewis 3 participants 10 messages Jun 6, 2022 – Jul 9, 2022

Subsystems (5)
Documentation x86 KVM Kselftest +1 more subsystems

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v2 cover kvm: x86/pmu: Introduce and test masked events

This series introduces the concept of masked events to the pmu event filter. Masked events can help reduce the number of events needed in the events field of a pmu_event_filter by allowing a more generic matching method to be used for the unit mask when filtering guest events in the pmu. With masked events, if an eventsel should be restricted from the guest, instead of having to add a new eventsel for every unit mask, one encoded event can be added that matches all possible unit masks.

v1 -> v2
- Made has_invalid_event() static to fix warning.
- Fixed checkpatch.pl errors and warnings.
- Updated to account for KVM_X86_PMU_OP().

Aaron Lewis (4):
kvm: x86/pmu: Introduce masked events to the pmu event filter
selftests: kvm/x86: Add flags when creating a pmu event filter
selftests: kvm/x86: Add testing for masked events
selftests: kvm/x86: Add testing for KVM_SET_PMU_EVENT_FILTER

 Documentation/virt/kvm/api.rst                |  46 +++++-
 arch/x86/include/asm/kvm-x86-pmu-ops.h        |   1 +
 arch/x86/include/uapi/asm/kvm.h               |   8 +
 arch/x86/kvm/pmu.c                            | 128 +++++++++++++--
 arch/x86/kvm/pmu.h                            |   1 +
 arch/x86/kvm/svm/pmu.c                        |  12 ++
 arch/x86/kvm/vmx/pmu_intel.c                  |  12 ++
 .../kvm/x86_64/pmu_event_filter_test.c        | 147 +++++++++++++++++-
 8 files changed, 333 insertions(+), 22 deletions(-)
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#1 lore raw

PATCH v2 1/4 kvm: x86/pmu: Introduce masked events to the pmu event filter

When building an event list for the pmu event filter, fitting all the events in the limited space can be a challenge. It becomes particularly challenging when trying to include various unit mask combinations for a particular event the guest is allow to or not allow to program. Instead of increasing the size of the list to allow for these, add a new encoding in the pmu event filter's events field. These encoded events can then be used to test against the event the guest is attempting to program to determine if the guest should have access to it.

The encoded values are: mask, match, and invert. When filtering events the mask is applied to the guest's unit mask to see if it matches the match value (ie: unit_mask & mask == match). If it does and the pmu event filter is an allow list the event is allowed, and denied if it's a deny list. Additionally, the result is reversed if the invert flag is set in the encoded event.

This feature is enabled by setting the flags field to KVM_PMU_EVENT_FLAG_MASKED_EVENTS.

Events can be encoded by using KVM_PMU_EVENT_ENCODE_MASKED_EVENT().

It is an error to have a bit set outside valid encoded bits, and calls to KVM_SET_PMU_EVENT_FILTER will return -EINVAL in such cases, including bits that are set in the high nybble[1] for AMD if called on Intel.

[1] bits 35:32 in the event and bits 11:8 in the eventsel.

---
 Documentation/virt/kvm/api.rst         |  46 +++++++--
 arch/x86/include/asm/kvm-x86-pmu-ops.h |   1 +
 arch/x86/include/uapi/asm/kvm.h        |   8 ++
 arch/x86/kvm/pmu.c                     | 128 ++++++++++++++++++++++---
 arch/x86/kvm/pmu.h                     |   1 +
 arch/x86/kvm/svm/pmu.c                 |  12 +++
 arch/x86/kvm/vmx/pmu_intel.c           |  12 +++
 7 files changed, 190 insertions(+), 18 deletions(-)
Documentation/virt/kvm/api.rst +40 -6
index 11e00a46c610..4e904772da5b 100644
@@ -5017,7 +5017,13 @@ using this ioctl.
 :Architectures: x86
 :Type: vm ioctl
 :Parameters: struct kvm_pmu_event_filter (in)
-:Returns: 0 on success, -1 on error
+:Returns: 0 on success,
+    -EFAULT args[0] cannot be accessed.
+    -EINVAL args[0] contains invalid data in the filter or events field.
+                    Note: event validation is only done for modes where
+                    the flags field is non-zero.
+    -E2BIG nevents is too large.
+    -ENOMEM not enough memory to allocate the filter.
 
 ::
 
@@ -5030,14 +5036,42 @@ using this ioctl.
 	__u64 events[0];
   };
 
-This ioctl restricts the set of PMU events that the guest can program.
-The argument holds a list of events which will be allowed or denied.
-The eventsel+umask of each event the guest attempts to program is compared
-against the events field to determine whether the guest should have access.
+This ioctl restricts the set of PMU events the guest can program.  The
+argument holds a list of events which will be allowed or denied.
+
 The events field only controls general purpose counters; fixed purpose
 counters are controlled by the fixed_counter_bitmap.
 
-No flags are defined yet, the field must be zero.
+Valid values for 'flags'::
+
+``0``
+
+This is the default behavior for the pmu event filter, and used when the
+flags field is clear.  In this mode the eventsel+umask for the event the
+guest is attempting to program is compared against each event in the events
+field to determine whether the guest should have access to it.
+
+``KVM_PMU_EVENT_FLAG_MASKED_EVENTS``
+
+In this mode each event in the events field will be encoded with mask, match,
+and invert values in addition to an eventsel.  These encoded events will be
+matched against the event the guest is attempting to program to determine
+whether the guest should have access to it.  When matching an encoded event
+with a guest event these steps are followed:
+ 1. Match the encoded eventsel to the guest eventsel.
+ 2. If that matches, match the mask and match values from the encoded event to
+    the guest's unit mask (ie: unit_mask & mask == match).
+ 3. If that matches, the guest is allow to program the event if its an allow
+    list or the guest is not allow to program the event if its a deny list.
+ 4. If the invert value is set in the encoded event, reverse the meaning of #3
+    (ie: deny if its an allow list, allow if it's a deny list).
+
+To encode an event in the pmu_event_filter use
+KVM_PMU_EVENT_ENCODE_MASKED_EVENT().
+
+If a bit is set in an encoded event that is not apart of the bits used for
+eventsel, mask, match or invert a call to KVM_SET_PMU_EVENT_FILTER will
+return -EINVAL.
 
 Valid values for 'action'::
 
arch/x86/include/asm/kvm-x86-pmu-ops.h +1 -0
index fdfd8e06fee6..016713b583bf 100644
@@ -24,6 +24,7 @@ KVM_X86_PMU_OP(set_msr)
 KVM_X86_PMU_OP(refresh)
 KVM_X86_PMU_OP(init)
 KVM_X86_PMU_OP(reset)
+KVM_X86_PMU_OP(get_event_mask)
 KVM_X86_PMU_OP_OPTIONAL(deliver_pmi)
 KVM_X86_PMU_OP_OPTIONAL(cleanup)
 
arch/x86/include/uapi/asm/kvm.h +8 -0
index 21614807a2cb..2964f3f15fb5 100644
@@ -522,6 +522,14 @@ struct kvm_pmu_event_filter {
 #define KVM_PMU_EVENT_ALLOW 0
 #define KVM_PMU_EVENT_DENY 1
 
+#define KVM_PMU_EVENT_FLAG_MASKED_EVENTS (1u << 0)
+
+#define KVM_PMU_EVENT_ENCODE_MASKED_EVENT(select, mask, match, invert) \
+		(((select) & 0xfful) | (((select) & 0xf00ul) << 24) | \
+		(((mask) & 0xfful) << 24) | \
+		(((match) & 0xfful) << 8) | \
+		(((invert) & 0x1ul) << 23))
+
 /* for KVM_{GET,SET,HAS}_DEVICE_ATTR */
 #define KVM_VCPU_TSC_CTRL 0 /* control group for the timestamp counter (TSC) */
 #define   KVM_VCPU_TSC_OFFSET 0 /* attribute for the TSC offset */
arch/x86/kvm/pmu.c +116 -12

169 lines not on this page · raw message

arch/x86/kvm/pmu.h +1 -0
index e745f443b6a8..f13fcc692d04 100644
@@ -37,6 +37,7 @@ struct kvm_pmu_ops {
 	void (*reset)(struct kvm_vcpu *vcpu);
 	void (*deliver_pmi)(struct kvm_vcpu *vcpu);
 	void (*cleanup)(struct kvm_vcpu *vcpu);
+	u64 (*get_event_mask)(u32 flag);
 };
 
 void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops);
arch/x86/kvm/svm/pmu.c +12 -0
index 136039fc6d01..41b7bd51fd11 100644
@@ -342,6 +342,17 @@ static void amd_pmu_reset(struct kvm_vcpu *vcpu)
 	}
 }
 
+static u64 amd_pmu_get_event_mask(u32 flag)
+{
+	if (flag == KVM_PMU_EVENT_FLAG_MASKED_EVENTS)
+		return AMD64_EVENTSEL_EVENT |
+		       ARCH_PERFMON_EVENTSEL_UMASK |
+		       ARCH_PERFMON_EVENTSEL_INV |
+		       ARCH_PERFMON_EVENTSEL_CMASK;
+	return AMD64_EVENTSEL_EVENT |
+	       ARCH_PERFMON_EVENTSEL_UMASK;
+}
+
 struct kvm_pmu_ops amd_pmu_ops __initdata = {
 	.pmc_perf_hw_id = amd_pmc_perf_hw_id,
 	.pmc_is_enabled = amd_pmc_is_enabled,
@@ -355,4 +366,5 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = {
 	.refresh = amd_pmu_refresh,
 	.init = amd_pmu_init,
 	.reset = amd_pmu_reset,
+	.get_event_mask = amd_pmu_get_event_mask,
 };
arch/x86/kvm/vmx/pmu_intel.c +12 -0
index 37e9eb32e3d9..27c44105760d 100644
@@ -719,6 +719,17 @@ static void intel_pmu_cleanup(struct kvm_vcpu *vcpu)
 		intel_pmu_release_guest_lbr_event(vcpu);
 }
 
+static u64 intel_pmu_get_event_mask(u32 flag)
+{
+	if (flag == KVM_PMU_EVENT_FLAG_MASKED_EVENTS)
+		return ARCH_PERFMON_EVENTSEL_EVENT |
+		       ARCH_PERFMON_EVENTSEL_UMASK |
+		       ARCH_PERFMON_EVENTSEL_INV |
+		       ARCH_PERFMON_EVENTSEL_CMASK;
+	return ARCH_PERFMON_EVENTSEL_EVENT |
+	       ARCH_PERFMON_EVENTSEL_UMASK;
+}
+
 struct kvm_pmu_ops intel_pmu_ops __initdata = {
 	.pmc_perf_hw_id = intel_pmc_perf_hw_id,
 	.pmc_is_enabled = intel_pmc_is_enabled,
@@ -734,4 +745,5 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = {
 	.reset = intel_pmu_reset,
 	.deliver_pmi = intel_pmu_deliver_pmi,
 	.cleanup = intel_pmu_cleanup,
+	.get_event_mask = intel_pmu_get_event_mask,
 };
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#2 lore raw

Re: [PATCH v2 1/4] kvm: x86/pmu: Introduce masked events to the pmu event filter

Quoted 88 lines · Aaron Lewis

When building an event list for the pmu event filter, fitting all the events in the limited space can be a challenge. It becomes particularly challenging when trying to include various unit mask combinations for a particular event the guest is allow to or not allow to program. Instead of increasing the size of the list to allow for these, add a new encoding in the pmu event filter's events field. These encoded events can then be used to test against the event the guest is attempting to program to determine if the guest should have access to it.

The encoded values are: mask, match, and invert. When filtering events the mask is applied to the guest's unit mask to see if it matches the match value (ie: unit_mask & mask == match). If it does and the pmu event filter is an allow list the event is allowed, and denied if it's a deny list. Additionally, the result is reversed if the invert flag is set in the encoded event.

This feature is enabled by setting the flags field to KVM_PMU_EVENT_FLAG_MASKED_EVENTS.

Events can be encoded by using KVM_PMU_EVENT_ENCODE_MASKED_EVENT().

It is an error to have a bit set outside valid encoded bits, and calls to KVM_SET_PMU_EVENT_FILTER will return -EINVAL in such cases, including bits that are set in the high nybble[1] for AMD if called on Intel.

[1] bits 35:32 in the event and bits 11:8 in the eventsel.

---
 Documentation/virt/kvm/api.rst         |  46 +++++++--
 arch/x86/include/asm/kvm-x86-pmu-ops.h |   1 +
 arch/x86/include/uapi/asm/kvm.h        |   8 ++
 arch/x86/kvm/pmu.c                     | 128 ++++++++++++++++++++++---
 arch/x86/kvm/pmu.h                     |   1 +
 arch/x86/kvm/svm/pmu.c                 |  12 +++
 arch/x86/kvm/vmx/pmu_intel.c           |  12 +++
 7 files changed, 190 insertions(+), 18 deletions(-)
Documentation/virt/kvm/api.rst +23 -6
index 11e00a46c610..4e904772da5b 100644
@@ -5017,7 +5017,13 @@ using this ioctl.
 :Architectures: x86
 :Type: vm ioctl
 :Parameters: struct kvm_pmu_event_filter (in)
-:Returns: 0 on success, -1 on error
+:Returns: 0 on success,
+    -EFAULT args[0] cannot be accessed.
+    -EINVAL args[0] contains invalid data in the filter or events field.
+                    Note: event validation is only done for modes where
+                    the flags field is non-zero.
+    -E2BIG nevents is too large.
+    -ENOMEM not enough memory to allocate the filter.
 
 ::
 
@@ -5030,14 +5036,42 @@ using this ioctl.
        __u64 events[0];
   };
 
-This ioctl restricts the set of PMU events that the guest can program.
-The argument holds a list of events which will be allowed or denied.
-The eventsel+umask of each event the guest attempts to program is compared
-against the events field to determine whether the guest should have access.
+This ioctl restricts the set of PMU events the guest can program.  The
+argument holds a list of events which will be allowed or denied.
+
 The events field only controls general purpose counters; fixed purpose
 counters are controlled by the fixed_counter_bitmap.
 
-No flags are defined yet, the field must be zero.
+Valid values for 'flags'::
+
+``0``
+
+This is the default behavior for the pmu event filter, and used when the
+flags field is clear.  In this mode the eventsel+umask for the event the
+guest is attempting to program is compared against each event in the events
+field to determine whether the guest should have access to it.
+
+``KVM_PMU_EVENT_FLAG_MASKED_EVENTS``
+
+In this mode each event in the events field will be encoded with mask, match,
+and invert values in addition to an eventsel.  These encoded events will be

Documentation/virt/kvm/api.rst using this ioctl. · line 5067

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+matched against the event the guest is attempting to program to determine
+whether the guest should have access to it.  When matching an encoded event
+with a guest event these steps are followed:
+ 1. Match the encoded eventsel to the guest eventsel.
+ 2. If that matches, match the mask and match values from the encoded event to
+    the guest's unit mask (ie: unit_mask & mask == match).
+ 3. If that matches, the guest is allow to program the event if its an allow
+    list or the guest is not allow to program the event if its a deny list.
+ 4. If the invert value is set in the encoded event, reverse the meaning of #3
+    (ie: deny if its an allow list, allow if it's a deny list).

The invert flag introduces some ambiguity. What if a particular event matches two of the masked filter entries: one with an invert flag and one without?

Documentation/virt/kvm/api.rst using this ioctl. · line 5072

Quoted 4 lines
Documentation/virt/kvm/api.rst +4 -0
+To encode an event in the pmu_event_filter use
+KVM_PMU_EVENT_ENCODE_MASKED_EVENT().
+
+If a bit is set in an encoded event that is not apart of the bits used for

Nit: "a part"?

#3 lore raw

Re: [PATCH v2 1/4] kvm: x86/pmu: Introduce masked events to the pmu event filter

Quoted 8 lines
Quoted 8 lines
Documentation/virt/kvm/api.rst +8 -0
+In this mode each event in the events field will be encoded with mask, match,
+and invert values in addition to an eventsel.  These encoded events will be
+matched against the event the guest is attempting to program to determine
+whether the guest should have access to it.  When matching an encoded event
+with a guest event these steps are followed:
+ 1. Match the encoded eventsel to the guest eventsel.
+ 2. If that matches, match the mask and match values from the encoded event to
+    the guest's unit mask (ie: unit_mask & mask == match).
Quoted 9 lines

Documentation/virt/kvm/api.rst using this ioctl. · line 5067

Quoted 4 lines

+ 3. If that matches, the guest is allow to program the event if its an allow

Documentation/virt/kvm/api.rst +3 -0
+    list or the guest is not allow to program the event if its a deny list.
+ 4. If the invert value is set in the encoded event, reverse the meaning of #3
+    (ie: deny if its an allow list, allow if it's a deny list).

The invert flag introduces some ambiguity. What if a particular event matches two of the masked filter entries: one with an invert flag and one without?

That's a good point! I think I can deal with that by validating the events when they are being set to ensure that for a particular event the invert flags are all set the same way and return EINVAL if they're not.

#4 lore raw

Re: [PATCH v2 1/4] kvm: x86/pmu: Introduce masked events to the pmu event filter

Quoted 19 lines · Aaron Lewis
Quoted 18 lines

Documentation/virt/kvm/api.rst using this ioctl. · line 5067

Quoted 13 lines
Documentation/virt/kvm/api.rst +12 -0
+In this mode each event in the events field will be encoded with mask, match,
+and invert values in addition to an eventsel.  These encoded events will be
+matched against the event the guest is attempting to program to determine
+whether the guest should have access to it.  When matching an encoded event
+with a guest event these steps are followed:
+ 1. Match the encoded eventsel to the guest eventsel.
+ 2. If that matches, match the mask and match values from the encoded event to
+    the guest's unit mask (ie: unit_mask & mask == match).
+ 3. If that matches, the guest is allow to program the event if its an allow
+    list or the guest is not allow to program the event if its a deny list.
+ 4. If the invert value is set in the encoded event, reverse the meaning of #3
+    (ie: deny if its an allow list, allow if it's a deny list).

The invert flag introduces some ambiguity. What if a particular event matches two of the masked filter entries: one with an invert flag and one without?

Quoted 4 lines

That's a good point! I think I can deal with that by validating the events when they are being set to ensure that for a particular event the invert flags are all set the same way and return EINVAL if they're not.

Once conflicts are disallowed, how is the behavior changed by an 'invert' entry? Isn't the behavior the same as not including the entry at all?

#5 lore raw

Re: [PATCH v2 1/4] kvm: x86/pmu: Introduce masked events to the pmu event filter

Quoted 4 lines

Once conflicts are disallowed, how is the behavior changed by an 'invert' entry? Isn't the behavior the same as not including the entry at all?

Another good point. I think it should work if I do two passes. The first pass to find if the event should be filtered. Iff it should, do a second pass to see if a masked event exists to negate that (ie: a match is found with the invert bit set).

#6 lore raw

Re: [PATCH v2 1/4] kvm: x86/pmu: Introduce masked events to the pmu event filter

In reply to Aaron Lewis
Thank you for the patch! Perhaps something to improve:

Hi Aaron,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on mst-vhost/linux-next]
[also build test WARNING on v5.19-rc5]
[cannot apply to kvm/queue next-20220701]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Aaron-Lewis/kvm-x86-pmu-Introduce-and-test-masked-events/20220607-020408
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next
reproduce: make htmldocs

If you fix the issue, kindly add following tag where applicable

All warnings (new ones prefixed by >>):

>> Documentation/virt/kvm/api.rst:5023: WARNING: Unexpected indentation.
>> Documentation/virt/kvm/api.rst:5025: WARNING: Block quote ends without a blank line; unexpected unindent.

vim +5023 Documentation/virt/kvm/api.rst

5015
5016 :Capability: KVM_CAP_PMU_EVENT_FILTER
5017 :Architectures: x86
5018 :Type: vm ioctl
5019 :Parameters: struct kvm_pmu_event_filter (in)
5020 :Returns: 0 on success,
5021 -EFAULT args[0] cannot be accessed.
5022 -EINVAL args[0] contains invalid data in the filter or events field.

> 5023	                    Note: event validation is only done for modes where
  5024	                    the flags field is non-zero.
> 5025	    -E2BIG nevents is too large.

5026 -ENOMEM not enough memory to allocate the filter.
5027

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#7 lore raw

PATCH v2 2/4 selftests: kvm/x86: Add flags when creating a pmu event filter

In reply to Aaron Lewis

Now that the flags field can be non-zero, pass it in when creating a pmu event filter.

This is needed in preparation for testing masked events.

No functional change intended.

---
 .../testing/selftests/kvm/x86_64/pmu_event_filter_test.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c +5 -4
index 93d77574b255..4bff4c71ac45 100644
@@ -222,14 +222,15 @@ static struct kvm_pmu_event_filter *alloc_pmu_event_filter(uint32_t nevents)
 
 
 static struct kvm_pmu_event_filter *
-create_pmu_event_filter(const uint64_t event_list[],
-			int nevents, uint32_t action)
+create_pmu_event_filter(const uint64_t event_list[], int nevents,
+			uint32_t action, uint32_t flags)
 {
 	struct kvm_pmu_event_filter *f;
 	int i;
 
 	f = alloc_pmu_event_filter(nevents);
 	f->action = action;
+	f->flags = flags;
 	for (i = 0; i < nevents; i++)
 		f->events[i] = event_list[i];
 
@@ -240,7 +241,7 @@ static struct kvm_pmu_event_filter *event_filter(uint32_t action)
 {
 	return create_pmu_event_filter(event_list,
 				       ARRAY_SIZE(event_list),
-				       action);
+				       action, 0);
 }
 
 /*
@@ -287,7 +288,7 @@ static void test_amd_deny_list(struct kvm_vm *vm)
 	struct kvm_pmu_event_filter *f;
 	uint64_t count;
 
-	f = create_pmu_event_filter(&event, 1, KVM_PMU_EVENT_DENY);
+	f = create_pmu_event_filter(&event, 1, KVM_PMU_EVENT_DENY, 0);
 	count = test_with_filter(vm, f);
 
 	free(f);
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#8 lore raw

PATCH v2 3/4 selftests: kvm/x86: Add testing for masked events

In reply to Aaron Lewis

Add testing for the pmu event filter's masked events. These tests run through different ways of finding an event the guest is attempting to program in an event list. For any given eventsel, there may be multiple instances of it in an event list. These tests try different ways of looking up a match to force the matching algorithm to walk the relevant eventsel's and ensure it is able to a) find a match, b) stays within its bounds.

---
 .../kvm/x86_64/pmu_event_filter_test.c        | 107 ++++++++++++++++++
 1 file changed, 107 insertions(+)
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c +107 -0
index 4bff4c71ac45..5b0163f9ba84 100644
@@ -18,8 +18,12 @@
 /*
  * In lieu of copying perf_event.h into tools...
  */
+#define ARCH_PERFMON_EVENTSEL_EVENT			0x000000FFULL
 #define ARCH_PERFMON_EVENTSEL_OS			(1ULL << 17)
 #define ARCH_PERFMON_EVENTSEL_ENABLE			(1ULL << 22)
+#define AMD64_EVENTSEL_EVENT	\
+	(ARCH_PERFMON_EVENTSEL_EVENT | (0x0FULL << 32))
+
 
 union cpuid10_eax {
 	struct {
@@ -445,6 +449,107 @@ static bool use_amd_pmu(void)
 		 is_zen3(entry->eax));
 }
 
+#define ENCODE_MASKED_EVENT(select, mask, match, invert) \
+		KVM_PMU_EVENT_ENCODE_MASKED_EVENT(select, mask, match, invert)
+
+static void expect_success(uint64_t count)
+{
+	if (count != NUM_BRANCHES)
+		pr_info("masked filter: Branch instructions retired = %lu (expected %u)\n",
+			count, NUM_BRANCHES);
+	TEST_ASSERT(count, "Allowed PMU event is not counting");
+}
+
+static void expect_failure(uint64_t count)
+{
+	if (count)
+		pr_info("masked filter: Branch instructions retired = %lu (expected 0)\n",
+			count);
+	TEST_ASSERT(!count, "Disallowed PMU Event is counting");
+}
+
+static void run_masked_filter_test(struct kvm_vm *vm, uint64_t masked_events[],
+				   const int nmasked_events, uint64_t event,
+				   uint32_t action, bool invert,
+				   void (*expected_func)(uint64_t))
+{
+	struct kvm_pmu_event_filter *f;
+	uint64_t old_event;
+	uint64_t count;
+	int i;
+
+	for (i = 0; i < nmasked_events; i++) {
+		if ((masked_events[i] & AMD64_EVENTSEL_EVENT) != EVENT(event, 0))
+			continue;
+
+		old_event = masked_events[i];
+
+		masked_events[i] =
+			ENCODE_MASKED_EVENT(event, ~0x00, 0x00, invert);
+
+		f = create_pmu_event_filter(masked_events, nmasked_events, action,
+				   KVM_PMU_EVENT_FLAG_MASKED_EVENTS);
+
+		count = test_with_filter(vm, f);
+		free(f);
+
+		expected_func(count);
+
+		masked_events[i] = old_event;
+	}
+}
+
+static void run_masked_filter_tests(struct kvm_vm *vm, uint64_t masked_events[],
+				    const int nmasked_events, uint64_t event)
+{
+	run_masked_filter_test(vm, masked_events, nmasked_events, event,
+			       KVM_PMU_EVENT_ALLOW, /*invert=*/false,
+			       expect_success);
+	run_masked_filter_test(vm, masked_events, nmasked_events, event,
+			       KVM_PMU_EVENT_ALLOW, /*invert=*/true,
+			       expect_failure);
+	run_masked_filter_test(vm, masked_events, nmasked_events, event,
+			       KVM_PMU_EVENT_DENY, /*invert=*/false,
+			       expect_failure);
+	run_masked_filter_test(vm, masked_events, nmasked_events, event,
+			       KVM_PMU_EVENT_DENY, /*invert=*/true,
+			       expect_success);
+}
+
+static void test_masked_filters(struct kvm_vm *vm)
+{
+	uint64_t masked_events[11];
+	const int nmasked_events = ARRAY_SIZE(masked_events);
+	uint64_t prev_event, event, next_event;
+	int i;
+
+	if (use_intel_pmu()) {
+		/* Instructions retired */
+		prev_event = 0xc0;
+		event = INTEL_BR_RETIRED;
+		/* Branch misses retired */
+		next_event = 0xc5;
+	} else {
+		TEST_ASSERT(use_amd_pmu(), "Unknown platform");
+		/* Retired instructions */
+		prev_event = 0xc0;
+		event = AMD_ZEN_BR_RETIRED;
+		/* Retired branch instructions mispredicted */
+		next_event = 0xc3;
+	}
+
+	for (i = 0; i < nmasked_events; i++)
+		masked_events[i] =
+			ENCODE_MASKED_EVENT(event, ~0x00, i+1, 0);
+
+	run_masked_filter_tests(vm, masked_events, nmasked_events, event);
+
+	masked_events[0] = ENCODE_MASKED_EVENT(prev_event, ~0x00, 0, 0);
+	masked_events[1] = ENCODE_MASKED_EVENT(next_event, ~0x00, 0, 0);
+
+	run_masked_filter_tests(vm, masked_events, nmasked_events, event);
+}
+
 int main(int argc, char *argv[])
 {
 	void (*guest_code)(void) = NULL;
@@ -489,6 +594,8 @@ int main(int argc, char *argv[])
 	test_not_member_deny_list(vm);
 	test_not_member_allow_list(vm);
 
+	test_masked_filters(vm);
+
 	kvm_vm_free(vm);
 
 	test_pmu_config_disable(guest_code);
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#9 lore raw

PATCH v2 4/4 selftests: kvm/x86: Add testing for KVM_SET_PMU_EVENT_FILTER

In reply to Aaron Lewis

Test that masked events are not using invalid bits, and if they are, ensure the pmu event filter is not accepted by KVM_SET_PMU_EVENT_FILTER. The only valid bits that can be used for masked events are set when using KVM_PMU_EVENT_ENCODE_MASKED_EVENT() with one caveat. If any bits in the high nybble[1] of the eventsel for AMD are used on Intel setting the pmu event filter with KVM_SET_PMU_EVENT_FILTER will fail.

Also, because no validation was being done on the event list prior to the introduction of masked events, verify that this continues for the original event type (flags == 0). If invalid bits are set (bits other than eventsel+umask) the pmu event filter will be accepted by KVM_SET_PMU_EVENT_FILTER.

[1] bits 35:32 in the event and bits 11:8 in the eventsel.

---
 .../kvm/x86_64/pmu_event_filter_test.c        | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c +31 -0
index 5b0163f9ba84..1fe1cbd36146 100644
@@ -550,6 +550,36 @@ static void test_masked_filters(struct kvm_vm *vm)
 	run_masked_filter_tests(vm, masked_events, nmasked_events, event);
 }
 
+static void test_filter_ioctl(struct kvm_vm *vm)
+{
+	struct kvm_pmu_event_filter *f;
+	uint64_t e = ~0ul;
+	int r;
+
+	/*
+	 * Unfortunately having invalid bits set in event data is expected to
+	 * pass when flags == 0 (bits other than eventsel+umask).
+	 */
+	f = create_pmu_event_filter(&e, 1, KVM_PMU_EVENT_ALLOW, 0);
+	r = _vm_ioctl(vm, KVM_SET_PMU_EVENT_FILTER, (void *)f);
+	TEST_ASSERT(r == 0, "Valid PMU Event Filter is failing");
+	free(f);
+
+	f = create_pmu_event_filter(&e, 1, KVM_PMU_EVENT_ALLOW,
+				    KVM_PMU_EVENT_FLAG_MASKED_EVENTS);
+	r = _vm_ioctl(vm, KVM_SET_PMU_EVENT_FILTER, (void *)f);
+	TEST_ASSERT(r != 0, "Invalid PMU Event Filter is expected to fail");
+	free(f);
+
+	e = ENCODE_MASKED_EVENT(0xff, 0xff, 0xff, 0xf);
+
+	f = create_pmu_event_filter(&e, 1, KVM_PMU_EVENT_ALLOW,
+				    KVM_PMU_EVENT_FLAG_MASKED_EVENTS);
+	r = _vm_ioctl(vm, KVM_SET_PMU_EVENT_FILTER, (void *)f);
+	TEST_ASSERT(r == 0, "Valid PMU Event Filter is failing");
+	free(f);
+}
+
 int main(int argc, char *argv[])
 {
 	void (*guest_code)(void) = NULL;
@@ -595,6 +625,7 @@ int main(int argc, char *argv[])
 	test_not_member_allow_list(vm);
 
 	test_masked_filters(vm);
+	test_filter_ioctl(vm);
 
 	kvm_vm_free(vm);
 
Signature · 1 line
2.36.1.255.ge46751e96f-goog