RISC-V: KVM: Add PMU event filter support
Subsystems (4)
v2 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. Like the other arch-specific KVM uAPI structs, struct kvm_pmu_event_filter is defined per-architecture; its layout reflects RISC-V PMU events and the ioctl number encodes the struct size.
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 and dmesg stays clean throughout the run.
Based on v7.2-rc2 (a635d6748234).
Changes in v2:
- Mask reserved high bits of the SBI PMU event index before comparing against the filter, so a guest cannot bypass a DENY filter by setting bits above bit 19. Noted by sashiko-bot review.
- Reject non-zero pad in the ioctl argument to keep the field usable for future extensions.
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 | 31 +++ arch/riscv/kvm/vm.c | 69 +++++- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../kvm/riscv/pmu_event_filter_test.c | 199 ++++++++++++++++++ 6 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/kvm/riscv/pmu_event_filter_test.c
--
2.34.1
PATCH v2 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(). Event indices are masked to their valid bits before comparison, so reserved high bits cannot bypass a DENY filter.
Advertise the feature with KVM_CAP_PMU_EVENT_FILTER.
--- arch/riscv/include/asm/kvm_host.h | 3 ++ arch/riscv/include/uapi/asm/kvm.h | 20 +++++++++ arch/riscv/kvm/vcpu_pmu.c | 31 ++++++++++++++ arch/riscv/kvm/vm.c | 69 ++++++++++++++++++++++++++++++- 4 files changed, 122 insertions(+), 1 deletion(-)
arch/riscv/include/asm/kvm_host.h +3 -0
@@ -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
@@ -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 +31 -0
@@ -733,6 +733,32 @@ 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; + +/* Reserved high bits must not bypass the filter. */ + eidx &=SBI_PMU_EVENT_IDX_MASK ; + + 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 +799,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 +68 -1
87 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The implementation fails to clear the active filter when `nevents == 0` as documented in the…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The implementation fails to clear the active filter when nevents == 0 as documented in the commit message. --
--- Patch [1]: [PATCH v2 1/2] RISC-V: KVM: Add PMU event filter support --- commit f5fab5adc86a94658a09438bfb27fb33aa49c8df
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.
Quoted 4 lines
arch/riscv/kvm/vm.c +0
-0
[ ... ]
Quoted 1 line
arch/riscv/kvm/vm.c +0
-0@@ -265,7 +268,71 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
[ ... ]
arch/riscv/kvm/vm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 300
Quoted 7 lines
arch/riscv/kvm/vm.c +7
-0+if (tmp.nevents >KVM_PMU_EVENT_FILTER_MAX_EVENTS ) +return -E2BIG ; + + size =struct_size (filter, events, tmp.nevents); + filter =kzalloc (size,GFP_KERNEL_ACCOUNT ); +if (!filter) +return -ENOMEM ;
[Severity: Medium]
The commit message states that "nevents == 0 clears any active filter". However, this code unconditionally allocates and installs a new filter even when nevents == 0, instead of skipping the allocation and setting the pointer to NULL.
If userspace passes a zeroed structure to clear the filter (meaning nevents is 0 and action defaults to 0, which is KVM_PMU_EVENT_ALLOW), won't this install a 0-event ALLOW filter?
Quoted 4 lines
arch/riscv/kvm/vcpu_pmu.c +0
-0
[ ... ]
Quoted 1 line
arch/riscv/kvm/vcpu_pmu.c +0
-0@@ -733,6 +733,32 @@ int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base,
[ ... ]
arch/riscv/kvm/vcpu_pmu.c int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, · line 760
Quoted 12 lines
arch/riscv/kvm/vcpu_pmu.c +12
-0+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; +}
[Severity: Medium]
If a 0-event ALLOW filter was installed because nevents was 0, in_list will be false here.
Does this result in kvm_riscv_pmu_event_allowed() returning false and rejecting all guest PMU events, breaking PMU functionality rather than restoring the default allow-all behavior?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Anup Patel
maintainerQuoted 68 lines · Yuhang.chen
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 structkvm_pmu_event_filterwhose 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 withSBI_ERR_NOT_SUPPORTED. It governs new counter configuration only and is not retroactive. The filter lives in structkvm_arch, read via SRCU on the vCPU run path and replaced underkvm->lockwithsynchronize_srcu_expedited(). Event indices are masked to their valid bits before comparison, so reserved high bits cannot bypass a DENY filter.Advertise the feature with
KVM_CAP_PMU_EVENT_FILTER.Assisted-by YuanSheng:deepseek-v4-proCo-developed-by Quan Zhou <[email protected]>Signed-off-by Quan Zhou <[email protected]>Signed-off-by Yuhang.chen <[email protected]>--- arch/riscv/include/asm/kvm_host.h | 3 ++ arch/riscv/include/uapi/asm/kvm.h | 20 +++++++++ arch/riscv/kvm/vcpu_pmu.c | 31 ++++++++++++++ arch/riscv/kvm/vm.c | 69 ++++++++++++++++++++++++++++++- 4 files changed, 122 insertions(+), 1 deletion(-)arch/riscv/include/asm/kvm_host.h +3
-0@@ -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 +10
-0@@ -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
arch/riscv/include/uapi/asm/kvm.h struct kvm_riscv_sbi_fwft { · line 418
Quoted 10 lines
arch/riscv/include/uapi/asm/kvm.h +10
-0+* 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); +}; +
This definition of kvm_pmu_event_filter does not cover SBI PMU RAW and RAWv2 events.
Maybe you can go with something like below:
struct kvm_pmu_event {
__u32 pad;
__u32 event_idx;
__u64 event_data;
};struct kvm_pmu_event_filter {
#define KVM_PMU_EVENT_ACTION_ALLOW 0
#define KVM_PMU_EVENT_ACTION_DENY 1
__u32 action;
__u32 nevents;
__u32 pad[2];
__DECLARE_FLEX_ARRAY(struct kvm_pmu_event, events);
};Quoted 147 lines
arch/riscv/include/uapi/asm/kvm.h +0
-0#endif #endif /* __LINUX_KVM_RISCV_H */ arch/riscv/kvm/vcpu_pmu.c +31
-0@@ -733,6 +733,32 @@ 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; + +/* Reserved high bits must not bypass the filter. */ + eidx &=SBI_PMU_EVENT_IDX_MASK ; + + 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 +799,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 +68
-2@@ -53,6 +53,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm) {kvm_destroy_vcpus (kvm); +kfree (srcu_dereference_check (kvm->arch.pmu_event_filter, &kvm->srcu,1 )); +kvm_riscv_aia_destroy_vm (kvm); }@@ -187,6 +189,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_MP_STATE :case KVM_CAP_IMMEDIATE_EXIT :case KVM_CAP_SET_GUEST_DEBUG : +case KVM_CAP_PMU_EVENT_FILTER : r =1 ;break ;case KVM_CAP_NR_VCPUS :@@ -265,7 +268,71 @@ 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 ; + +if (tmp.action !=KVM_PMU_EVENT_ALLOW && + tmp.action !=KVM_PMU_EVENT_DENY ) +return -EINVAL ; + +if (tmp.flags) +return -EINVAL ; + +if (tmp.pad) +return -EINVAL ; + +if (tmp.nevents >KVM_PMU_EVENT_FILTER_MAX_EVENTS ) +return -E2BIG ; + + size =struct_size (filter, events, tmp.nevents); + filter =kzalloc (size,GFP_KERNEL_ACCOUNT ); +if (!filter) +return -ENOMEM ; + + filter->action = tmp.action; + filter->nevents = tmp.nevents; + filter->flags = tmp.flags; + +if (copy_from_user (filter->events, user_filter->events, +flex_array_size (filter, events, filter->nevents))) { + r = -EFAULT ; + goto cleanup; + } + +mutex_lock (&kvm->lock); + filter =rcu_replace_pointer (kvm->arch.pmu_event_filter, filter, +mutex_is_locked (&kvm->lock)); +mutex_unlock (&kvm->lock); +synchronize_srcu_expedited (&kvm->srcu); + +cleanup: +kfree (filter); +return r; +} +int kvm_arch_vm_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg) { -return -EINVAL ; +struct kvm *kvm = filp->private_data; +void __user *argp = (void __user *)arg; +int r; + +switch (ioctl) { +case KVM_SET_PMU_EVENT_FILTER : + r =kvm_riscv_vm_ioctl_set_pmu_event_filter (kvm, argp); +break ; +default : + r = -EINVAL ; + } + +return r; } --
Quoted 2 lines
2.34.1
Regards,
Anup
PATCH v2 2/2 RISC-V: KVM: selftests: Add PMU event filter test
In reply to Yuhang.chenAdd 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.
--- 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
@@ -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
[email protected]
AI reviewRe: [PATCH v2 2/2] RISC-V: KVM: selftests: Add PMU event filter test
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The commit message falsely claims that the guest programs the 'instructions' SBI PMU event, but…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The commit message falsely claims that the guest programs the 'instructions' SBI PMU event, but the implementation only ever programs the 'cycles' event. --
--- Patch [2]: [PATCH v2 2/2] RISC-V: KVM: selftests: Add PMU event filter test --- commit 828aa8f61ee14a213c9f6491df6c9b1b9ddbd337
Author: Yuhang.chen <[email protected]>
RISC-V: KVM: selftests: Add PMU event filter test
This patch adds a selftest to verify KVM_SET_PMU_EVENT_FILTER on RISC-V. The host applies ALLOW and DENY filters, and asserts that the guest receives the expected success or failure when configuring PMU counters.
Quoted 5 lines
Add a selftest that exercises
KVM_SET_PMU_EVENT_FILTERon RISC-V. The guest programs the CPU cycles and instructions SBI PMU events throughSBI_EXT_PMU_COUNTER_CFG_MATCHwhile the host installs filters with the ALLOW and DENY actions, asserting that disallowed events returnSBI_ERR_NOT_SUPPORTEDand allowed events succeed.
[Severity: Medium]
Is this description accurate?
The commit message states that the guest programs both the CPU cycles and instructions SBI PMU events.
However, looking at guest_code(), it appears to only ever program EV_CYCLES via the SBI_EXT_PMU_COUNTER_CFG_MATCH call:
guest_code() {
...
ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH,
0, 1, 0, EV_CYCLES, 0, 0);
...
}An inline comment in the test code also explicitly notes that the instructions event is never programmed by the guest: "Instructions itself is never programmed, so host support for it is irrelevant."
Could the commit message be updated to reflect that only the CPU cycles event is actually programmed by the guest code?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2