perf: RISC-V: Improve privilege mode filtering for perf
authorAtish Patra <atishp@rivosinc.com>
Sun, 5 Feb 2023 01:15:03 +0000 (17:15 -0800)
committerAnup Patel <anup@brainfault.org>
Tue, 7 Feb 2023 15:05:33 +0000 (20:35 +0530)
Currently, the host driver doesn't have any method to identify if the
requested perf event is from kvm or bare metal. As KVM runs in HS
mode, there are no separate hypervisor privilege mode to distinguish
between the attributes for guest/host.

Improve the privilege mode filtering by using the event specific
config1 field.

Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Signed-off-by: Anup Patel <anup@brainfault.org>
drivers/perf/riscv_pmu_sbi.c
include/linux/perf/riscv_pmu.h

index 6b53adc81f478729c902964921ba3f8b263ddc42..71174fa593f4bc727d3d2abf864018d7779d6fd3 100644 (file)
@@ -301,6 +301,27 @@ int riscv_pmu_get_hpm_info(u32 *hw_ctr_width, u32 *num_hw_ctr)
 }
 EXPORT_SYMBOL_GPL(riscv_pmu_get_hpm_info);
 
+static unsigned long pmu_sbi_get_filter_flags(struct perf_event *event)
+{
+       unsigned long cflags = 0;
+       bool guest_events = false;
+
+       if (event->attr.config1 & RISCV_PMU_CONFIG1_GUEST_EVENTS)
+               guest_events = true;
+       if (event->attr.exclude_kernel)
+               cflags |= guest_events ? SBI_PMU_CFG_FLAG_SET_VSINH : SBI_PMU_CFG_FLAG_SET_SINH;
+       if (event->attr.exclude_user)
+               cflags |= guest_events ? SBI_PMU_CFG_FLAG_SET_VUINH : SBI_PMU_CFG_FLAG_SET_UINH;
+       if (guest_events && event->attr.exclude_hv)
+               cflags |= SBI_PMU_CFG_FLAG_SET_SINH;
+       if (event->attr.exclude_host)
+               cflags |= SBI_PMU_CFG_FLAG_SET_UINH | SBI_PMU_CFG_FLAG_SET_SINH;
+       if (event->attr.exclude_guest)
+               cflags |= SBI_PMU_CFG_FLAG_SET_VSINH | SBI_PMU_CFG_FLAG_SET_VUINH;
+
+       return cflags;
+}
+
 static int pmu_sbi_ctr_get_idx(struct perf_event *event)
 {
        struct hw_perf_event *hwc = &event->hw;
@@ -311,11 +332,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event)
        uint64_t cbase = 0;
        unsigned long cflags = 0;
 
-       if (event->attr.exclude_kernel)
-               cflags |= SBI_PMU_CFG_FLAG_SET_SINH;
-       if (event->attr.exclude_user)
-               cflags |= SBI_PMU_CFG_FLAG_SET_UINH;
-
+       cflags = pmu_sbi_get_filter_flags(event);
        /* retrieve the available counter index */
 #if defined(CONFIG_32BIT)
        ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase,
index a1c3f777148101fae9143342b0cafe23ecaff4b6..43fc892aa7d992fbae0b871bcc7d9786fc1f9ebb 100644 (file)
@@ -26,6 +26,8 @@
 
 #define RISCV_PMU_STOP_FLAG_RESET 1
 
+#define RISCV_PMU_CONFIG1_GUEST_EVENTS 0x1
+
 struct cpu_hw_events {
        /* currently enabled events */
        int                     n_events;