Skip to contentSkip to conversation

RISC-V: KVM: Add PMU event filter support

PATCH Superseded Yuhang.chen 2 participants 4 messages Aug 7

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover RISC-V: KVM: Add PMU event filter support

This series adds PMU event filter support to RISC-V KVM, allowing a userspace VMM to restrict which SBI PMU events a guest is permitted to program.

The KVM_SET_PMU_EVENT_FILTER ioctl and KVM_CAP_PMU_EVENT_FILTER already exist as generic KVM uAPI (include/uapi/linux/kvm.h); RISC-V KVM does not yet implement them. Wiring them up lets a VMM limit a guest's PMU event access, which is useful for sandboxing and for withholding host PMU events the host does not intend to expose.

Patch 1 implements the kernel side: the uapi struct, the ioctl handler with an SRCU-protected filter, enforcement in kvm_riscv_vcpu_pmu_ctr_cfg_match() (a disallowed event fails with SBI_ERR_NOT_SUPPORTED), and advertisement of KVM_CAP_PMU_EVENT_FILTER.

Patch 2 adds a selftest that covers the ALLOW/DENY semantics with the event in and out of the list, filter clearing, and ioctl argument rejection (EINVAL/E2BIG).

Testing: built and run under QEMU TCG with nested KVM (an L1 KVM host running an L2 guest). The selftest (patch 2) passes - all ALLOW/DENY and argument-validation cases behave as expected - and dmesg stays clean throughout the run.

Based on v7.2-rc2 (a635d6748234).

Assisted-by YuanSheng:deepseek-v4-pro

Yuhang.chen (2):
RISC-V: KVM: Add PMU event filter support
RISC-V: KVM: selftests: Add PMU event filter test

 arch/riscv/include/asm/kvm_host.h             |   3 +
 arch/riscv/include/uapi/asm/kvm.h             |  20 ++
 arch/riscv/kvm/vcpu_pmu.c                     |  28 +++
 arch/riscv/kvm/vm.c                           |  66 +++++-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../kvm/riscv/pmu_event_filter_test.c         | 199 ++++++++++++++++++
 6 files changed, 316 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/kvm/riscv/pmu_event_filter_test.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 RISC-V: KVM: Add PMU event filter support

Allow userspace to restrict which SBI PMU events a guest is permitted to program via the new VM ioctl KVM_SET_PMU_EVENT_FILTER. It takes a struct kvm_pmu_event_filter whose events[] array holds SBI PMU event indices encoded as (type << 16) | code. The action field selects ALLOW (only listed events may be programmed) or DENY (listed events are rejected); nevents == 0 clears any active filter.

The filter is enforced in kvm_riscv_vcpu_pmu_ctr_cfg_match(), where a disallowed event fails configuration with SBI_ERR_NOT_SUPPORTED. It governs new counter configuration only and is not retroactive. The filter lives in struct kvm_arch, read via SRCU on the vCPU run path and replaced under kvm->lock with synchronize_srcu_expedited().

Advertise the feature with KVM_CAP_PMU_EVENT_FILTER.

Assisted-by YuanSheng:deepseek-v4-pro
---
 arch/riscv/include/asm/kvm_host.h |  3 ++
 arch/riscv/include/uapi/asm/kvm.h | 20 ++++++++++
 arch/riscv/kvm/vcpu_pmu.c         | 28 +++++++++++++
 arch/riscv/kvm/vm.c               | 66 ++++++++++++++++++++++++++++++-
 4 files changed, 116 insertions(+), 1 deletion(-)
arch/riscv/include/asm/kvm_host.h +3 -0
index 60017ceec9d2..1cd3d6a11057 100644
@@ -95,6 +95,9 @@ struct kvm_arch {
 
 	/* KVM_CAP_RISCV_MP_STATE_RESET */
 	bool mp_state_reset;
+
+	/* KVM_SET_PMU_EVENT_FILTER */
+	struct kvm_pmu_event_filter __rcu *pmu_event_filter;
 };
 
 struct kvm_cpu_trap {
arch/riscv/include/uapi/asm/kvm.h +20 -0
index 504e73305343..da4f639fa89f 100644
@@ -12,6 +12,7 @@
 #ifndef __ASSEMBLER__
 
 #include <linux/types.h>
+#include <linux/stddef.h>
 #include <asm/bitsperlong.h>
 #include <asm/ptrace.h>
 
@@ -396,6 +397,25 @@ struct kvm_riscv_sbi_fwft {
 /* One single KVM irqchip, ie. the AIA */
 #define KVM_NR_IRQCHIPS			1
 
+/* for KVM_CAP_PMU_EVENT_FILTER */
+#define KVM_PMU_EVENT_ALLOW	0
+#define KVM_PMU_EVENT_DENY	1
+
+/*
+ * For KVM_SET_PMU_EVENT_FILTER: restrict which SBI PMU events a guest may
+ * configure.  Each @events entry is a SBI PMU event index (type in bits
+ * 19:16, code in bits 15:0).  %KVM_PMU_EVENT_ALLOW permits only listed
+ * events; %KVM_PMU_EVENT_DENY rejects them.  Enforced at counter
+ * configuration (SBI PMU COUNTER_CFG_MATCH), not retroactively.
+ */
+struct kvm_pmu_event_filter {
+	__u32 action;
+	__u32 nevents;
+	__u32 flags;
+	__u32 pad;
+	__DECLARE_FLEX_ARRAY(__u64, events);
+};
+
 #endif
 
 #endif /* __LINUX_KVM_RISCV_H */
arch/riscv/kvm/vcpu_pmu.c +28 -0
index bb46dcbfb24d..bc7d297a63ce 100644
@@ -733,6 +733,29 @@ int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base,
 	return 0;
 }
 
+static bool kvm_riscv_pmu_event_allowed(struct kvm *kvm, unsigned long eidx)
+{
+	struct kvm_pmu_event_filter *filter;
+	bool in_list = false;
+	unsigned int i;
+
+	filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu);
+	if (!filter)
+		return true;
+
+	for (i = 0; i < filter->nevents; i++) {
+		if ((unsigned long)filter->events[i] == eidx) {
+			in_list = true;
+			break;
+		}
+	}
+
+	/* ALLOW: permit only listed events; DENY: reject them. */
+	if (filter->action == KVM_PMU_EVENT_ALLOW)
+		return in_list;
+	return !in_list;
+}
+
 int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base,
 				     unsigned long ctr_mask, unsigned long flags,
 				     unsigned long eidx, u64 evtdata,
@@ -773,6 +796,11 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba
 		goto out;
 	}
 
+	if (!kvm_riscv_pmu_event_allowed(vcpu->kvm, eidx)) {
+		sbiret = SBI_ERR_NOT_SUPPORTED;
+		goto out;
+	}
+
 	/*
 	 * SKIP_MATCH flag indicates the caller is aware of the assigned counter
 	 * for this event. Just do a sanity check if it already marked used.
arch/riscv/kvm/vm.c +65 -1

84 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Guest can trivially bypass PMU event DENY filters by setting unmasked high bits in the event index…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Guest can trivially bypass PMU event DENY filters by setting unmasked high bits in the event index (eidx).
- [Medium] RISC-V defines a structurally incompatible struct kvm_pmu_event_filter compared to x86, which causes the KVM_SET_PMU_EVENT_FILTER ioctl number to diverge and breaks cross-architecture userspace code.
- [Medium] Missing validation for the UAPI padding field prevents safe future extensions.
--

--- Patch [1]: [PATCH 1/2] RISC-V: KVM: Add PMU event filter support --- commit 4331081d868d3aa2c618439fb21772bb580c0495
Author: Yuhang.chen <[email protected]>

RISC-V: KVM: Add PMU event filter support

Allow userspace to restrict which SBI PMU events a guest is permitted to program via the new VM ioctl KVM_SET_PMU_EVENT_FILTER. It takes a struct kvm_pmu_event_filter whose events[] array holds SBI PMU event indices encoded as (type << 16) | code.

Quoted 4 lines
arch/riscv/include/uapi/asm/kvm.h +0 -0
index 504e73305343a..da4f639fa89f8 100644

[ ... ]

Quoted 12 lines
arch/riscv/include/uapi/asm/kvm.h +8 -0
@@ -396,6 +397,25 @@ struct kvm_riscv_sbi_fwft {
 /* One single KVM irqchip, ie. the AIA */
 #define KVM_NR_IRQCHIPS			1
 
+/* for KVM_CAP_PMU_EVENT_FILTER */
+#define KVM_PMU_EVENT_ALLOW	0
+#define KVM_PMU_EVENT_DENY	1
+
+/*
+ * For KVM_SET_PMU_EVENT_FILTER: restrict which SBI PMU events a guest may
+ * configure.  Each @events entry is a SBI PMU event index (type in bits
+ * 19:16, code in bits 15:0).  %KVM_PMU_EVENT_ALLOW permits only listed

arch/riscv/include/uapi/asm/kvm.h struct kvm_riscv_sbi_fwft { · line 417

Quoted 10 lines
arch/riscv/include/uapi/asm/kvm.h +10 -0
+ * events; %KVM_PMU_EVENT_DENY rejects them.  Enforced at counter
+ * configuration (SBI PMU COUNTER_CFG_MATCH), not retroactively.
+ */
+struct kvm_pmu_event_filter {
+	__u32 action;
+	__u32 nevents;
+	__u32 flags;
+	__u32 pad;
+	__DECLARE_FLEX_ARRAY(__u64, events);
+};

[Severity: Medium]
Since KVM_SET_PMU_EVENT_FILTER is a generic KVM ioctl, does defining struct kvm_pmu_event_filter with a different size and layout than the existing x86 definition break cross-architecture userspace code?

The size difference (due to missing fields like fixed_counter_bitmap and different padding) causes the ioctl number to evaluate to a different numeric value on RISC-V. Could this be unified with the existing generic definition to maintain KVM UAPI consistency?

Quoted 7 lines
arch/riscv/include/uapi/asm/kvm.h +0 -0
 #endif
 
 #endif /* __LINUX_KVM_RISCV_H */
arch/riscv/kvm/vcpu_pmu.c +0 -0
index 2025b664961c8..189650b04c37d 100644

[ ... ]

Quoted 10 lines
arch/riscv/kvm/vcpu_pmu.c +6 -0
@@ -733,6 +733,29 @@ int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base,
 	return 0;
 }
 
+static bool kvm_riscv_pmu_event_allowed(struct kvm *kvm, unsigned long eidx)
+{
+	struct kvm_pmu_event_filter *filter;
+	bool in_list = false;
+	unsigned int i;
+

arch/riscv/kvm/vcpu_pmu.c int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, · line 751

Quoted 10 lines
arch/riscv/kvm/vcpu_pmu.c +10 -0
+	filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu);
+	if (!filter)
+		return true;
+
+	for (i = 0; i < filter->nevents; i++) {
+		if ((unsigned long)filter->events[i] == eidx) {
+			in_list = true;
+			break;
+		}
+	}

[Severity: High]
Can a malicious guest bypass the DENY filter by setting high unmasked bits in the event index?

In kvm_riscv_pmu_event_allowed(), the guest-controlled eidx is exact-matched against the filter entries. If the guest provides an eidx with bits above bit 19 set (e.g., bit 63), the equality check fails and a DENY filter will incorrectly allow it.

Because the subsequent configuration logic masks out the high bits using get_event_type() and get_event_code(), the blocked event is still successfully configured. Should eidx be properly masked before it is compared against the filter events?

Quoted 11 lines
arch/riscv/kvm/vcpu_pmu.c +7 -0
+
+	/* ALLOW: permit only listed events; DENY: reject them. */
+	if (filter->action == KVM_PMU_EVENT_ALLOW)
+		return in_list;
+	return !in_list;
+}
+
arch/riscv/kvm/vm.c +0 -0
index a9f083feeb767..6f822c43f1569 100644

[ ... ]

Quoted 16 lines
arch/riscv/kvm/vm.c +12 -0
@@ -265,7 +268,68 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
 	}
 }
 
+#define KVM_PMU_EVENT_FILTER_MAX_EVENTS	256
+
+static int kvm_riscv_vm_ioctl_set_pmu_event_filter(struct kvm *kvm,
+						   void __user *argp)
+{
+	struct kvm_pmu_event_filter __user *user_filter = argp;
+	struct kvm_pmu_event_filter *filter, tmp;
+	size_t size;
+	int r = 0;
+
+	if (copy_from_user(&tmp, user_filter, sizeof(tmp)))
+		return -EFAULT;

arch/riscv/kvm/vm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 292

Quoted 10 lines
arch/riscv/kvm/vm.c +10 -0
+
+	if (tmp.action != KVM_PMU_EVENT_ALLOW &&
+	    tmp.action != KVM_PMU_EVENT_DENY)
+		return -EINVAL;
+
+	if (tmp.flags)
+		return -EINVAL;
+
+	if (tmp.nevents > KVM_PMU_EVENT_FILTER_MAX_EVENTS)
+		return -E2BIG;

[Severity: Medium]
Does this code leave the tmp.pad field unvalidated?

If userspace passes uninitialized data or garbage in the padding field, the kernel will silently accept it because there is no check ensuring tmp.pad is zero. Will this prevent safely repurposing the padding field for new features or flags in the future without breaking ABI compatibility for existing applications?

Quoted 3 lines
arch/riscv/kvm/vm.c +3 -0
+
+	size = struct_size(filter, events, tmp.nevents);
+	filter = kzalloc(size, GFP_KERNEL_ACCOUNT);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH 2/2 RISC-V: KVM: selftests: Add PMU event filter test

In reply to Yuhang.chen

Add a selftest that exercises KVM_SET_PMU_EVENT_FILTER on RISC-V. The guest programs the CPU cycles and instructions SBI PMU events through SBI_EXT_PMU_COUNTER_CFG_MATCH while the host installs filters with the ALLOW and DENY actions, asserting that disallowed events return SBI_ERR_NOT_SUPPORTED and allowed events succeed.

The test also validates ioctl argument rejection: an invalid action, a non-zero flags field, and an over-large nevents value are each expected to fail with -EINVAL or -E2BIG. A baseline run verifies PMU availability and the test skips (KSFT_SKIP) when PMU or the filter capability is absent.

Assisted-by YuanSheng:deepseek-v4-pro
---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../kvm/riscv/pmu_event_filter_test.c         | 199 ++++++++++++++++++
 2 files changed, 200 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/riscv/pmu_event_filter_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index d28a057fa6c2..5d2dc3b25eac 100644
@@ -218,6 +218,7 @@ TEST_GEN_PROGS_s390 += pre_fault_memory_test
 TEST_GEN_PROGS_riscv = $(TEST_GEN_PROGS_COMMON)
 TEST_GEN_PROGS_riscv += riscv/sbi_pmu_test
 TEST_GEN_PROGS_riscv += riscv/ebreak_test
+TEST_GEN_PROGS_riscv += riscv/pmu_event_filter_test
 TEST_GEN_PROGS_riscv += access_tracking_perf_test
 TEST_GEN_PROGS_riscv += arch_timer
 TEST_GEN_PROGS_riscv += coalesced_io_test
tools/testing/selftests/kvm/riscv/pmu_event_filter_test.c new file +199 -0

199 lines not on this page · raw message

Signature · 1 line
2.34.1