RISC-V SBI v2.0 PMU improvements and Perf sampling in KVM guest
Subsystems (8)
v5 cover RISC-V SBI v2.0 PMU improvements and Perf sampling in KVM guest
This series implements SBI PMU improvements done in SBI v2.0[1] i.e. PMU snapshot and fw_read_hi() functions.
SBI v2.0 introduced PMU snapshot feature which allows the SBI implementation to provide counter information (i.e. values/overflow status) via a shared memory between the SBI implementation and supervisor OS. This allows to minimize the number of traps in when perf being used inside a kvm guest as it relies on SBI PMU + trap/emulation of the counters.
The current set of ratified RISC-V specification also doesn't allow scountovf to be trap/emulated by the hypervisor. The SBI PMU snapshot bridges the gap in ISA as well and enables perf sampling in the guest. However, LCOFI in the guest only works via IRQ filtering in AIA specification. That's why, AIA has to be enabled in the hardware (at least the Ssaia extension) in order to use the sampling support in the perf.
Here are the patch wise implementation details.
PATCH 1,4,7,8,9,10,11,15 : Generic cleanups/improvements. PATCH 2,3,14 : FW_READ_HI function implementation
PATCH 5-6: Add PMU snapshot feature in sbi pmu driver
PATCH 12-13: KVM implementation for snapshot and sampling in kvm guests PATCH 16-17: Generic improvements for kvm selftests
PATCH 18-22: KVM selftests for SBI PMU extension
The series is based on v6.9-rc1 and is available at:
https://github.com/atishp04/linux/tree/kvm_pmu_snapshot_v5
The kvmtool patch is also available at:
https://github.com/atishp04/kvmtool/tree/sscofpmf
It also requires Ssaia ISA extension to be present in the hardware in order to get perf sampling support in the guest. In Qemu virt machine, it can be done by the following config.
``` -cpu rv64,sscofpmf=true,x-ssaia=true ```
There is no other dependencies on AIA apart from that. Thus, Ssaia must be disabled for the guest if AIA patches are not available. Here is the example command.
``` ./lkvm-static run -m 256 -c2 --console serial -p "console=ttyS0 earlycon" --disable-ssaia -k ./Image --debug ```
The series has been tested only in Qemu.
Here is the snippet of the perf running inside a kvm guest.
===================================================
$ perf record -e cycles -e instructions perf bench sched messaging -g 5 ... $ Running 'sched/messaging' benchmark: ... [ 45.928723] perf_duration_warn: 2 callbacks suppressed [ 45.929000] perf: interrupt took too long (484426 > 483186), lowering kernel.perf_event_max_sample_rate to 250 $ 20 sender and receiver processes per group $ 5 groups == 200 processes run
Total time: 14.220 [sec]
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.117 MB perf.data (1942 samples) ]
$ perf report --stdio
$ To display the perf.data header info, please use --header/--header-only optio>
$
$
$ Total Lost Samples: 0
$
$ Samples: 943 of event 'cycles'
$ Event count (approx.): 5128976844
$
$ Overhead Command Shared Object Symbol >
$ ........ ............... ........................... .....................>
$
7.59% sched-messaging [kernel.kallsyms] [k] memcpy
5.48% sched-messaging [kernel.kallsyms] [k] percpu_counter_ad>
5.24% sched-messaging [kernel.kallsyms] [k] __sbi_rfence_v02_>
4.00% sched-messaging [kernel.kallsyms] [k] _raw_spin_unlock_>
3.79% sched-messaging [kernel.kallsyms] [k] set_pte_range
3.72% sched-messaging [kernel.kallsyms] [k] next_uptodate_fol>
3.46% sched-messaging [kernel.kallsyms] [k] filemap_map_pages
3.31% sched-messaging [kernel.kallsyms] [k] handle_mm_fault
3.20% sched-messaging [kernel.kallsyms] [k] finish_task_switc>
3.16% sched-messaging [kernel.kallsyms] [k] clear_page
3.03% sched-messaging [kernel.kallsyms] [k] mtree_range_walk
2.42% sched-messaging [kernel.kallsyms] [k] flush_icache_pte===================================================
[1] https://github.com/riscv-non-isa/riscv-sbi-doc
Changes from v4->v5:
1. Moved sbi related definitions to its own header file from processor.h
2. Added few helper functions for selftests.
3. Improved firmware counter read and RV32 start/stop functions.
4. Converted all the shifting operations to use BIT macro
5. Addressed all other comments on v4.
Changes from v3->v4:
1. Added selftests.
2. Fixed an issue to clear the interrupt pending bits.
3. Fixed the counter index in snapshot memory start function.
Changes from v2->v3:
1. Fixed a patchwork warning on patch6.
2. Fixed a comment formatting & nit fix in PATCH 3 & 5.
3. Moved the hvien update and sscofpmf enabling to PATCH 9 from PATCH 8.
Changes from v1->v2:
1. Fixed warning/errors from patchwork CI.
2. Rebased on top of kvm-next.
3. Added Acked-by tags.
Changes from RFC->v1:
1. Addressed all the comments on RFC series.
2. Removed PATCH2 and merged into later patches.
3. Added 2 more patches for minor fixes.
4. Fixed KVM boot issue without Ssaia and made sscofpmf in guest dependent on Ssaia in the host.
Atish Patra (22):
RISC-V: Fix the typo in Scountovf CSR name
RISC-V: Add FIRMWARE_READ_HI definitiondrivers/perf: riscv: Read upper bits of a firmware counter drivers/perf: riscv: Use BIT macro for shifting operations
RISC-V: Add SBI PMU snapshot definitionsdrivers/perf: riscv: Implement SBI PMU snapshot function drivers/perf: riscv: Fix counter mask iteration for RV32
RISC-V: KVM: Fix the initial sample period value
RISC-V: KVM: Rename the SBI_STA_SHMEM_DISABLE to a generic name
RISC-V: KVM: No need to update the counter value during reset
RISC-V: KVM: No need to exit to the user space if perf event failed
RISC-V: KVM: Implement SBI PMU Snapshot feature
RISC-V: KVM: Add perf sampling support for guests
RISC-V: KVM: Support 64 bit firmware counters on RV32
RISC-V: KVM: Improve firmware counter read function
KVM: riscv: selftests: Move sbi definitions to its own header file
KVM: riscv: selftests: Add helper functions for extension checks
KVM: riscv: selftests: Add Sscofpmf to get-reg-list test
KVM: riscv: selftests: Add SBI PMU extension definitions
KVM: riscv: selftests: Add SBI PMU selftest
KVM: riscv: selftests: Add a test for PMU snapshot functionality
KVM: riscv: selftests: Add a test for counter overflow
arch/riscv/include/asm/csr.h | 5 +- arch/riscv/include/asm/kvm_vcpu_pmu.h | 16 +- arch/riscv/include/asm/sbi.h | 34 +- arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kernel/paravirt.c | 6 +- arch/riscv/kvm/aia.c | 5 + arch/riscv/kvm/vcpu.c | 15 +- arch/riscv/kvm/vcpu_onereg.c | 5 + arch/riscv/kvm/vcpu_pmu.c | 260 +++++++- arch/riscv/kvm/vcpu_sbi_pmu.c | 17 +- arch/riscv/kvm/vcpu_sbi_sta.c | 4 +- drivers/perf/riscv_pmu.c | 1 + drivers/perf/riscv_pmu_sbi.c | 264 +++++++- include/linux/perf/riscv_pmu.h | 6 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/riscv/processor.h | 49 +- .../testing/selftests/kvm/include/riscv/sbi.h | 141 +++++ .../selftests/kvm/include/riscv/ucall.h | 1 + .../selftests/kvm/lib/riscv/processor.c | 12 + .../testing/selftests/kvm/riscv/arch_timer.c | 2 +- .../selftests/kvm/riscv/get-reg-list.c | 4 + .../selftests/kvm/riscv/sbi_pmu_test.c | 581 ++++++++++++++++++ tools/testing/selftests/kvm/steal_time.c | 4 +- 23 files changed, 1322 insertions(+), 112 deletions(-)
create mode 100644 tools/testing/selftests/kvm/include/riscv/sbi.h create mode 100644 tools/testing/selftests/kvm/riscv/sbi_pmu_test.c
--
2.34.1
PATCH v5 1/22 RISC-V: Fix the typo in Scountovf CSR name
The counter overflow CSR name is "scountovf" not "sscountovf".
Fix the csr name.
--- arch/riscv/include/asm/csr.h | 2 +- drivers/perf/riscv_pmu_sbi.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/riscv/include/asm/csr.h +1 -1
@@ -281,7 +281,7 @@#define CSR_HPMCOUNTER30H 0xc9e#define CSR_HPMCOUNTER31H 0xc9f -#define CSR_SSCOUNTOVF 0xda0 +#define CSR_SCOUNTOVF 0xda0#define CSR_SSTATUS 0x100#define CSR_SIE 0x104
drivers/perf/riscv_pmu_sbi.c +1 -1
@@ -27,7 +27,7 @@#define ALT_SBI_PMU_OVERFLOW (__ovl) \ asm volatile(ALTERNATIVE_2( \ - "csrr %0, " __stringify(CSR_SSCOUNTOVF), \ + "csrr %0, " __stringify(CSR_SCOUNTOVF), \ "csrr %0, " __stringify(THEAD_C9XX_CSR_SCOUNTEROF), \ THEAD_VENDOR_ID, ERRATA_THEAD_PMU, \ CONFIG_ERRATA_THEAD_PMU, \
Signature · 1 line
2.34.1
Re: [PATCH v5 01/22] RISC-V: Fix the typo in Scountovf CSR name
Quoted 43 lines · Atish Patra
The counter overflow CSR name is "scountovf" not "sscountovf".
Fix the csr name.
Fixes 4905ec2fb7e6 ("RISC-V: Add sscofpmf extension support")Reviewed-by Cl?ment L?ger <[email protected]>Reviewed-by Conor Dooley <[email protected]>Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/csr.h | 2 +- drivers/perf/riscv_pmu_sbi.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)arch/riscv/include/asm/csr.h +1
-1@@ -281,7 +281,7 @@#define CSR_HPMCOUNTER30H 0xc9e#define CSR_HPMCOUNTER31H 0xc9f -#define CSR_SSCOUNTOVF 0xda0 +#define CSR_SCOUNTOVF 0xda0#define CSR_SSTATUS 0x100#define CSR_SIE 0x104drivers/perf/riscv_pmu_sbi.c +1
-1@@ -27,7 +27,7 @@#define ALT_SBI_PMU_OVERFLOW (__ovl) \ asm volatile(ALTERNATIVE_2( \ - "csrr %0, " __stringify(CSR_SSCOUNTOVF), \ + "csrr %0, " __stringify(CSR_SCOUNTOVF), \ "csrr %0, " __stringify(THEAD_C9XX_CSR_SCOUNTEROF), \ THEAD_VENDOR_ID, ERRATA_THEAD_PMU, \ CONFIG_ERRATA_THEAD_PMU, \Signature · 1 line
2.34.1
PATCH v5 2/22 RISC-V: Add FIRMWARE_READ_HI definition
In reply to Atish PatraSBI v2.0 added another function to SBI PMU extension to read the upper bits of a counter with width larger than XLEN.
Add the definition for that function.
--- arch/riscv/include/asm/sbi.h | 1 + 1 file changed, 1 insertion(+)
arch/riscv/include/asm/sbi.h +1 -0
@@ -131,6 +131,7 @@ enum sbi_ext_pmu_fid {SBI_EXT_PMU_COUNTER_START ,SBI_EXT_PMU_COUNTER_STOP ,SBI_EXT_PMU_COUNTER_FW_READ , + SBI_EXT_PMU_COUNTER_FW_READ_HI, }; union sbi_pmu_ctr_info {
Signature · 1 line
2.34.1
Re: [PATCH v5 02/22] RISC-V: Add FIRMWARE_READ_HI definition
Quoted 28 lines · Atish Patra
SBI v2.0 added another function to SBI PMU extension to read the upper bits of a counter with width larger than XLEN.
Add the definition for that function.
Reviewed-by Cl?ment L?ger <[email protected]>Acked-by Conor Dooley <[email protected]>Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/sbi.h | 1 + 1 file changed, 1 insertion(+)arch/riscv/include/asm/sbi.h +1
-0@@ -131,6 +131,7 @@ enum sbi_ext_pmu_fid {SBI_EXT_PMU_COUNTER_START ,SBI_EXT_PMU_COUNTER_STOP ,SBI_EXT_PMU_COUNTER_FW_READ , + SBI_EXT_PMU_COUNTER_FW_READ_HI, }; union sbi_pmu_ctr_info {Signature · 1 line
2.34.1
PATCH v5 3/22 drivers/perf: riscv: Read upper bits of a firmware counter
In reply to Atish PatraSBI v2.0 introduced a explicit function to read the upper 32 bits for any firmware counter width that is longer than 32bits. This is only applicable for RV32 where firmware counter can be 64 bit.
--- drivers/perf/riscv_pmu_sbi.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
drivers/perf/riscv_pmu_sbi.c +20 -5
@@ -57,6 +57,8 @@ asm volatile(ALTERNATIVE( \PMU_FORMAT_ATTR (event,"config:0-47" );PMU_FORMAT_ATTR (firmware,"config:63" ); +static bool sbi_v2_available; +static struct attribute *riscv_arch_formats_attr[] = { &format_attr_event.attr, &format_attr_firmware.attr,
@@ -511,19 +513,29 @@ static u64 pmu_sbi_ctr_read(struct perf_event *event)struct hw_perf_event *hwc = &event->hw;int idx = hwc->idx;struct sbiret ret; -union sbi_pmu_ctr_info info;u64 val =0 ; +union sbi_pmu_ctr_info info = pmu_ctr_list[idx];if (pmu_sbi_is_fw_event (event)) { ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_FW_READ , hwc->idx,0 ,0 ,0 ,0 ,0 ); -if (!ret.error) - val = ret.value; +if (ret.error) +return 0 ; + + val = ret.value; +if (IS_ENABLED (CONFIG_32BIT ) && sbi_v2_available && info.width >=32 ) { + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_FW_READ_HI , + hwc->idx,0 ,0 ,0 ,0 ,0 ); +if (!ret.error) + val |= ((u64 )ret.value <<32 ); +else +WARN_ONCE (1 ,"Unable to read upper 32 bits of firmware counter error: %d\n" , +sbi_err_map_linux_errno (ret.error)); + } }else { - info = pmu_ctr_list[idx]; val =riscv_pmu_ctr_read_csr (info.csr);if (IS_ENABLED (CONFIG_32BIT )) - val = ((u64 )riscv_pmu_ctr_read_csr (info.csr +0x80 )) <<31 | val; + val |= ((u64 )riscv_pmu_ctr_read_csr (info.csr +0x80 )) <<32 ; }return val;
@@ -1135,6 +1147,9 @@ static int __init pmu_sbi_devinit(void)return 0 ; } +if (sbi_spec_version >=sbi_mk_version (2 ,0 )) + sbi_v2_available = true; + ret =cpuhp_setup_state_multi (CPUHP_AP_PERF_RISCV_STARTING ,"perf/riscv/pmu:starting" , pmu_sbi_starting_cpu, pmu_sbi_dying_cpu);
Signature · 1 line
2.34.1
Re: [PATCH v5 03/22] drivers/perf: riscv: Read upper bits of a firmware counter
Quoted 42 lines · Atish Patra
SBI v2.0 introduced a explicit function to read the upper 32 bits for any firmware counter width that is longer than 32bits. This is only applicable for RV32 where firmware counter can be 64 bit.
Reviewed-by Andrew Jones <[email protected]>Acked-by Palmer Dabbelt <[email protected]>Reviewed-by Conor Dooley <[email protected]>Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- drivers/perf/riscv_pmu_sbi.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)drivers/perf/riscv_pmu_sbi.c +5
-3@@ -57,6 +57,8 @@ asm volatile(ALTERNATIVE( \PMU_FORMAT_ATTR (event,"config:0-47" );PMU_FORMAT_ATTR (firmware,"config:63" ); +static bool sbi_v2_available; +static struct attribute *riscv_arch_formats_attr[] = { &format_attr_event.attr, &format_attr_firmware.attr,@@ -511,19 +513,29 @@ static u64 pmu_sbi_ctr_read(struct perf_event *event)struct hw_perf_event *hwc = &event->hw;int idx = hwc->idx;struct sbiret ret; -union sbi_pmu_ctr_info info;u64 val =0 ; +union sbi_pmu_ctr_info info = pmu_ctr_list[idx];if (pmu_sbi_is_fw_event (event)) { ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_FW_READ , hwc->idx,0 ,0 ,0 ,0 ,0 ); -if (!ret.error) - val = ret.value; +if (ret.error) +return 0 ;
drivers/perf/riscv_pmu_sbi.c static u64 pmu_sbi_ctr_read(struct perf_event *event) · line 533
Quoted 10 lines
drivers/perf/riscv_pmu_sbi.c +10
-0+ + val = ret.value; +if (IS_ENABLED (CONFIG_32BIT ) && sbi_v2_available && info.width >=32 ) { + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_FW_READ_HI , + hwc->idx,0 ,0 ,0 ,0 ,0 ); +if (!ret.error) + val |= ((u64 )ret.value <<32 ); +else +WARN_ONCE (1 ,"Unable to read upper 32 bits of firmware counter error: %d\n" , +sbi_err_map_linux_errno (ret.error));
I don't think we should use sbi_err_map_linux_errno() in this case since we don't have a 1:1 mapping of SBI errors to Linux errors and we don't propagate the error as a Linux error. For warnings, it's better to output the exact SBI error.
Quoted 23 lines
drivers/perf/riscv_pmu_sbi.c +5
-2+ } }else { - info = pmu_ctr_list[idx]; val =riscv_pmu_ctr_read_csr (info.csr);if (IS_ENABLED (CONFIG_32BIT )) - val = ((u64 )riscv_pmu_ctr_read_csr (info.csr +0x80 )) <<31 | val; + val |= ((u64 )riscv_pmu_ctr_read_csr (info.csr +0x80 )) <<32 ; }return val;@@ -1135,6 +1147,9 @@ static int __init pmu_sbi_devinit(void)return 0 ; } +if (sbi_spec_version >=sbi_mk_version (2 ,0 )) + sbi_v2_available = true; + ret =cpuhp_setup_state_multi (CPUHP_AP_PERF_RISCV_STARTING ,"perf/riscv/pmu:starting" , pmu_sbi_starting_cpu, pmu_sbi_dying_cpu);Signature · 1 line
2.34.1
Thanks,
drew
Re: [PATCH v5 03/22] drivers/perf: riscv: Read upper bits of a firmware counter
Quoted 50 lines · Andrew Jones
Quoted 49 lines · Atish Patra
SBI v2.0 introduced a explicit function to read the upper 32 bits for any firmware counter width that is longer than 32bits. This is only applicable for RV32 where firmware counter can be 64 bit.
Reviewed-by Andrew Jones <[email protected]>Acked-by Palmer Dabbelt <[email protected]>Reviewed-by Conor Dooley <[email protected]>Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- drivers/perf/riscv_pmu_sbi.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)drivers/perf/riscv_pmu_sbi.c +12
-3@@ -57,6 +57,8 @@ asm volatile(ALTERNATIVE( \PMU_FORMAT_ATTR (event,"config:0-47" );PMU_FORMAT_ATTR (firmware,"config:63" ); +static bool sbi_v2_available; +static struct attribute *riscv_arch_formats_attr[] = { &format_attr_event.attr, &format_attr_firmware.attr,@@ -511,19 +513,29 @@ static u64 pmu_sbi_ctr_read(struct perf_event *event)struct hw_perf_event *hwc = &event->hw;int idx = hwc->idx;struct sbiret ret; -union sbi_pmu_ctr_info info;u64 val =0 ; +union sbi_pmu_ctr_info info = pmu_ctr_list[idx];if (pmu_sbi_is_fw_event (event)) { ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_FW_READ , hwc->idx,0 ,0 ,0 ,0 ,0 ); -if (!ret.error) - val = ret.value; +if (ret.error) +return 0 ; + + val = ret.value; +if (IS_ENABLED (CONFIG_32BIT ) && sbi_v2_available && info.width >=32 ) { + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_FW_READ_HI , + hwc->idx,0 ,0 ,0 ,0 ,0 ); +if (!ret.error) + val |= ((u64 )ret.value <<32 );
Quoted 9 lines
drivers/perf/riscv_pmu_sbi.c static u64 pmu_sbi_ctr_read(struct perf_event *event) · line 533
Quoted 3 lines
drivers/perf/riscv_pmu_sbi.c +3
-0+else +WARN_ONCE (1 ,"Unable to read upper 32 bits of firmware counter error: %d\n" , +sbi_err_map_linux_errno (ret.error));I don't think we should use
sbi_err_map_linux_errno()in this case since we don't have a 1:1 mapping of SBI errors to Linux errors and we don't propagate the error as a Linux error. For warnings, it's better to output the exact SBI error.
Sure. Fixed it.
Quoted 26 lines
Quoted 23 lines
drivers/perf/riscv_pmu_sbi.c +5
-2+ } }else { - info = pmu_ctr_list[idx]; val =riscv_pmu_ctr_read_csr (info.csr);if (IS_ENABLED (CONFIG_32BIT )) - val = ((u64 )riscv_pmu_ctr_read_csr (info.csr +0x80 )) <<31 | val; + val |= ((u64 )riscv_pmu_ctr_read_csr (info.csr +0x80 )) <<32 ; }return val;@@ -1135,6 +1147,9 @@ static int __init pmu_sbi_devinit(void)return 0 ; } +if (sbi_spec_version >=sbi_mk_version (2 ,0 )) + sbi_v2_available = true; + ret =cpuhp_setup_state_multi (CPUHP_AP_PERF_RISCV_STARTING ,"perf/riscv/pmu:starting" , pmu_sbi_starting_cpu, pmu_sbi_dying_cpu);Signature · 1 line
2.34.1Thanks,
drew
PATCH v5 4/22 drivers/perf: riscv: Use BIT macro for shifting operations
In reply to Atish PatraIt is a good practice to use BIT() instead of (1UL << x). Replace the current usages with BIT().
--- arch/riscv/include/asm/sbi.h | 20 ++++++++++---------- drivers/perf/riscv_pmu_sbi.c | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-)
arch/riscv/include/asm/sbi.h +10 -10
@@ -233,20 +233,20 @@ enum sbi_pmu_ctr_type {#define SBI_PMU_EVENT_IDX_INVALID 0xFFFFFFFF/* Flags defined for config matching function */ -#define SBI_PMU_CFG_FLAG_SKIP_MATCH (1 << 0) -#define SBI_PMU_CFG_FLAG_CLEAR_VALUE (1 << 1) -#define SBI_PMU_CFG_FLAG_AUTO_START (1 << 2) -#define SBI_PMU_CFG_FLAG_SET_VUINH (1 << 3) -#define SBI_PMU_CFG_FLAG_SET_VSINH (1 << 4) -#define SBI_PMU_CFG_FLAG_SET_UINH (1 << 5) -#define SBI_PMU_CFG_FLAG_SET_SINH (1 << 6) -#define SBI_PMU_CFG_FLAG_SET_MINH (1 << 7) +#define SBI_PMU_CFG_FLAG_SKIP_MATCH BIT(0) +#define SBI_PMU_CFG_FLAG_CLEAR_VALUE BIT(1) +#define SBI_PMU_CFG_FLAG_AUTO_START BIT(2) +#define SBI_PMU_CFG_FLAG_SET_VUINH BIT(3) +#define SBI_PMU_CFG_FLAG_SET_VSINH BIT(4) +#define SBI_PMU_CFG_FLAG_SET_UINH BIT(5) +#define SBI_PMU_CFG_FLAG_SET_SINH BIT(6) +#define SBI_PMU_CFG_FLAG_SET_MINH BIT(7)/* Flags defined for counter start function */ -#define SBI_PMU_START_FLAG_SET_INIT_VALUE (1 << 0) +#define SBI_PMU_START_FLAG_SET_INIT_VALUE BIT(0)/* Flags defined for counter stop function */ -#define SBI_PMU_STOP_FLAG_RESET (1 << 0) +#define SBI_PMU_STOP_FLAG_RESET BIT(0)enum sbi_ext_dbcn_fid {SBI_EXT_DBCN_CONSOLE_WRITE =0 ,
drivers/perf/riscv_pmu_sbi.c +1 -1
@@ -386,7 +386,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event) cmask =1 ; }else if (event->attr.config ==PERF_COUNT_HW_INSTRUCTIONS ) { cflags |=SBI_PMU_CFG_FLAG_SKIP_MATCH ; - cmask =1UL << (CSR_INSTRET -CSR_CYCLE ); + cmask =BIT (CSR_INSTRET -CSR_CYCLE ); } }
Signature · 1 line
2.34.1
unsubscribe
unsubscribe
Jonathan Oleson
Talent Acquisition | Seattle
linkedin.com/in/jonathanoleson/
[email protected]
-----Original Message-----
From: Atish Patra <[email protected]>
Sent: Wednesday, April 3, 2024 1:05 AM
To: [email protected]
Cc: Atish Patra <[email protected]>; Ajay Kaher <[email protected]>; Alexandre Ghiti <[email protected]>; Alexey Makhalov <[email protected]>; Andrew Jones <[email protected]>; Anup Patel <[email protected]>; Conor Dooley <[email protected]>; Juergen Gross <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Mark Rutland <[email protected]>; Palmer Dabbelt <[email protected]>; Paolo Bonzini <[email protected]>; Paul Walmsley <[email protected]>; Shuah Khan <[email protected]>; [email protected]; VMware PV-Drivers Reviewers <[email protected]>; Will Deacon <[email protected]>; [email protected]
Subject: [External] [PATCH v5 04/22] drivers/perf: riscv: Use BIT macro for shifting operations
It is a good practice to use BIT() instead of (1UL << x). Replace the current usages with BIT().
--- arch/riscv/include/asm/sbi.h | 20 ++++++++++---------- drivers/perf/riscv_pmu_sbi.c | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-)
arch/riscv/include/asm/sbi.h +0 -0
@@ -233,20 +233,20 @@ enum sbi_pmu_ctr_type { #define
SBI_PMU_EVENT_IDX_INVALID 0xFFFFFFFF
/* Flags defined for config matching function */
-#define SBI_PMU_CFG_FLAG_SKIP_MATCH (1 << 0)
-#define SBI_PMU_CFG_FLAG_CLEAR_VALUE (1 << 1)
-#define SBI_PMU_CFG_FLAG_AUTO_START (1 << 2)
-#define SBI_PMU_CFG_FLAG_SET_VUINH (1 << 3)
-#define SBI_PMU_CFG_FLAG_SET_VSINH (1 << 4)
-#define SBI_PMU_CFG_FLAG_SET_UINH (1 << 5)
-#define SBI_PMU_CFG_FLAG_SET_SINH (1 << 6)
-#define SBI_PMU_CFG_FLAG_SET_MINH (1 << 7)
+#define SBI_PMU_CFG_FLAG_SKIP_MATCH BIT(0)
+#define SBI_PMU_CFG_FLAG_CLEAR_VALUE BIT(1)
+#define SBI_PMU_CFG_FLAG_AUTO_START BIT(2)
+#define SBI_PMU_CFG_FLAG_SET_VUINH BIT(3)
+#define SBI_PMU_CFG_FLAG_SET_VSINH BIT(4)
+#define SBI_PMU_CFG_FLAG_SET_UINH BIT(5)
+#define SBI_PMU_CFG_FLAG_SET_SINH BIT(6)
+#define SBI_PMU_CFG_FLAG_SET_MINH BIT(7)
/* Flags defined for counter start function */ -#defineSBI_PMU_START_FLAG_SET_INIT_VALUE (1 << 0) +#define SBI_PMU_START_FLAG_SET_INIT_VALUE BIT(0)
/* Flags defined for counter stop function */ -#defineSBI_PMU_STOP_FLAG_RESET (1 << 0)
+#define SBI_PMU_STOP_FLAG_RESET BIT(0)
enum sbi_ext_dbcn_fid {
SBI_EXT_DBCN_CONSOLE_WRITE = 0,
drivers/perf/riscv_pmu_sbi.c +0 -0
@@ -386,7 +386,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event) cmask =1 ; }else if (event->attr.config ==PERF_COUNT_HW_INSTRUCTIONS )
{
cflags |= SBI_PMU_CFG_FLAG_SKIP_MATCH;
- cmask = 1UL << (CSR_INSTRET - CSR_CYCLE);
+ cmask = BIT(CSR_INSTRET - CSR_CYCLE);
}
}
--
2.34.1
Re: [PATCH v5 04/22] drivers/perf: riscv: Use BIT macro for shifting operations
In reply to Atish PatraQuoted 1 line · Atish Patra
It is a good practice to use
BIT()instead of (1UL << x).
(1UL << x) isn't generally a problem. The problem is with (1 << x).
Quoted 59 lines
Replace the current usages with
BIT().Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/sbi.h | 20 ++++++++++---------- drivers/perf/riscv_pmu_sbi.c | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-)arch/riscv/include/asm/sbi.h +10
-10@@ -233,20 +233,20 @@ enum sbi_pmu_ctr_type {#define SBI_PMU_EVENT_IDX_INVALID 0xFFFFFFFF/* Flags defined for config matching function */ -#define SBI_PMU_CFG_FLAG_SKIP_MATCH (1 << 0) -#define SBI_PMU_CFG_FLAG_CLEAR_VALUE (1 << 1) -#define SBI_PMU_CFG_FLAG_AUTO_START (1 << 2) -#define SBI_PMU_CFG_FLAG_SET_VUINH (1 << 3) -#define SBI_PMU_CFG_FLAG_SET_VSINH (1 << 4) -#define SBI_PMU_CFG_FLAG_SET_UINH (1 << 5) -#define SBI_PMU_CFG_FLAG_SET_SINH (1 << 6) -#define SBI_PMU_CFG_FLAG_SET_MINH (1 << 7) +#define SBI_PMU_CFG_FLAG_SKIP_MATCH BIT(0) +#define SBI_PMU_CFG_FLAG_CLEAR_VALUE BIT(1) +#define SBI_PMU_CFG_FLAG_AUTO_START BIT(2) +#define SBI_PMU_CFG_FLAG_SET_VUINH BIT(3) +#define SBI_PMU_CFG_FLAG_SET_VSINH BIT(4) +#define SBI_PMU_CFG_FLAG_SET_UINH BIT(5) +#define SBI_PMU_CFG_FLAG_SET_SINH BIT(6) +#define SBI_PMU_CFG_FLAG_SET_MINH BIT(7)/* Flags defined for counter start function */ -#define SBI_PMU_START_FLAG_SET_INIT_VALUE (1 << 0) +#define SBI_PMU_START_FLAG_SET_INIT_VALUE BIT(0)/* Flags defined for counter stop function */ -#define SBI_PMU_STOP_FLAG_RESET (1 << 0) +#define SBI_PMU_STOP_FLAG_RESET BIT(0)enum sbi_ext_dbcn_fid {SBI_EXT_DBCN_CONSOLE_WRITE =0 ,drivers/perf/riscv_pmu_sbi.c +1
-1@@ -386,7 +386,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event) cmask =1 ; }else if (event->attr.config ==PERF_COUNT_HW_INSTRUCTIONS ) { cflags |=SBI_PMU_CFG_FLAG_SKIP_MATCH ; - cmask =1UL << (CSR_INSTRET -CSR_CYCLE ); + cmask =BIT (CSR_INSTRET -CSR_CYCLE ); } }Signature · 1 line
2.34.1
Other than the commit message,
Thanks,
drew
Re: [PATCH v5 04/22] drivers/perf: riscv: Use BIT macro for shifting operations
Quoted 5 lines · Andrew Jones
Quoted 1 line · Atish Patra
It is a good practice to use
BIT()instead of (1UL << x).(1UL << x) isn't generally a problem. The problem is with (1 << x).
Yes. That's why, the commit message said it's good to have :) I improved the commit message to specify about 1 << x as well.
Quoted 66 lines
Quoted 59 lines
Replace the current usages with
BIT().Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/sbi.h | 20 ++++++++++---------- drivers/perf/riscv_pmu_sbi.c | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-)arch/riscv/include/asm/sbi.h +10
-10@@ -233,20 +233,20 @@ enum sbi_pmu_ctr_type {#define SBI_PMU_EVENT_IDX_INVALID 0xFFFFFFFF/* Flags defined for config matching function */ -#define SBI_PMU_CFG_FLAG_SKIP_MATCH (1 << 0) -#define SBI_PMU_CFG_FLAG_CLEAR_VALUE (1 << 1) -#define SBI_PMU_CFG_FLAG_AUTO_START (1 << 2) -#define SBI_PMU_CFG_FLAG_SET_VUINH (1 << 3) -#define SBI_PMU_CFG_FLAG_SET_VSINH (1 << 4) -#define SBI_PMU_CFG_FLAG_SET_UINH (1 << 5) -#define SBI_PMU_CFG_FLAG_SET_SINH (1 << 6) -#define SBI_PMU_CFG_FLAG_SET_MINH (1 << 7) +#define SBI_PMU_CFG_FLAG_SKIP_MATCH BIT(0) +#define SBI_PMU_CFG_FLAG_CLEAR_VALUE BIT(1) +#define SBI_PMU_CFG_FLAG_AUTO_START BIT(2) +#define SBI_PMU_CFG_FLAG_SET_VUINH BIT(3) +#define SBI_PMU_CFG_FLAG_SET_VSINH BIT(4) +#define SBI_PMU_CFG_FLAG_SET_UINH BIT(5) +#define SBI_PMU_CFG_FLAG_SET_SINH BIT(6) +#define SBI_PMU_CFG_FLAG_SET_MINH BIT(7)/* Flags defined for counter start function */ -#define SBI_PMU_START_FLAG_SET_INIT_VALUE (1 << 0) +#define SBI_PMU_START_FLAG_SET_INIT_VALUE BIT(0)/* Flags defined for counter stop function */ -#define SBI_PMU_STOP_FLAG_RESET (1 << 0) +#define SBI_PMU_STOP_FLAG_RESET BIT(0)enum sbi_ext_dbcn_fid {SBI_EXT_DBCN_CONSOLE_WRITE =0 ,drivers/perf/riscv_pmu_sbi.c +1
-1@@ -386,7 +386,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event) cmask =1 ; }else if (event->attr.config ==PERF_COUNT_HW_INSTRUCTIONS ) { cflags |=SBI_PMU_CFG_FLAG_SKIP_MATCH ; - cmask =1UL << (CSR_INSTRET -CSR_CYCLE ); + cmask =BIT (CSR_INSTRET -CSR_CYCLE ); } }Signature · 1 line
2.34.1Other than the commit message,
Reviewed-by Andrew Jones <[email protected]>Thanks,
drew
PATCH v5 5/22 RISC-V: Add SBI PMU snapshot definitions
In reply to Atish PatraSBI PMU Snapshot function optimizes the number of traps to higher privilege mode by leveraging a shared memory between the S/VS-mode and the M/HS mode. Add the definitions for that extension and new error codes.
--- arch/riscv/include/asm/sbi.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
arch/riscv/include/asm/sbi.h +11 -0
@@ -132,6 +132,7 @@ enum sbi_ext_pmu_fid {SBI_EXT_PMU_COUNTER_STOP ,SBI_EXT_PMU_COUNTER_FW_READ ,SBI_EXT_PMU_COUNTER_FW_READ_HI , + SBI_EXT_PMU_SNAPSHOT_SET_SHMEM, }; union sbi_pmu_ctr_info {
@@ -148,6 +149,13 @@ union sbi_pmu_ctr_info { }; }; +/* Data structure to contain the pmu snapshot data */ +struct riscv_pmu_snapshot_data { +u64 ctr_overflow_mask; +u64 ctr_values[64 ]; +u64 reserved[447 ]; +}; +#define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(47, 0)#define RISCV_PMU_RAW_EVENT_IDX 0x20000
@@ -244,9 +252,11 @@ enum sbi_pmu_ctr_type {/* Flags defined for counter start function */ #define SBI_PMU_START_FLAG_SET_INIT_VALUE BIT(0) +#define SBI_PMU_START_FLAG_INIT_SNAPSHOT BIT(1)/* Flags defined for counter stop function */ #define SBI_PMU_STOP_FLAG_RESET BIT(0) +#define SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT BIT(1)enum sbi_ext_dbcn_fid {SBI_EXT_DBCN_CONSOLE_WRITE =0 ,
@@ -285,6 +295,7 @@ struct sbi_sta_struct {#define SBI_ERR_ALREADY_AVAILABLE -6#define SBI_ERR_ALREADY_STARTED -7#define SBI_ERR_ALREADY_STOPPED -8 +#define SBI_ERR_NO_SHMEM -9extern unsigned long sbi_spec_version;struct sbiret {
Signature · 1 line
2.34.1
Re: [PATCH v5 05/22] RISC-V: Add SBI PMU snapshot definitions
Quoted 61 lines · Atish Patra
SBI PMU Snapshot function optimizes the number of traps to higher privilege mode by leveraging a shared memory between the S/VS-mode and the M/HS mode. Add the definitions for that extension and new error codes.
Reviewed-by Anup Patel <[email protected]>Acked-by Palmer Dabbelt <[email protected]>Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/sbi.h | 11 +++++++++++ 1 file changed, 11 insertions(+)arch/riscv/include/asm/sbi.h +11
-0@@ -132,6 +132,7 @@ enum sbi_ext_pmu_fid {SBI_EXT_PMU_COUNTER_STOP ,SBI_EXT_PMU_COUNTER_FW_READ ,SBI_EXT_PMU_COUNTER_FW_READ_HI , + SBI_EXT_PMU_SNAPSHOT_SET_SHMEM, }; union sbi_pmu_ctr_info {@@ -148,6 +149,13 @@ union sbi_pmu_ctr_info { }; }; +/* Data structure to contain the pmu snapshot data */ +struct riscv_pmu_snapshot_data { +u64 ctr_overflow_mask; +u64 ctr_values[64 ]; +u64 reserved[447 ]; +}; +#define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(47, 0)#define RISCV_PMU_RAW_EVENT_IDX 0x20000@@ -244,9 +252,11 @@ enum sbi_pmu_ctr_type {/* Flags defined for counter start function */ #define SBI_PMU_START_FLAG_SET_INIT_VALUE BIT(0) +#define SBI_PMU_START_FLAG_INIT_SNAPSHOT BIT(1)/* Flags defined for counter stop function */ #define SBI_PMU_STOP_FLAG_RESET BIT(0) +#define SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT BIT(1)enum sbi_ext_dbcn_fid {SBI_EXT_DBCN_CONSOLE_WRITE =0 ,@@ -285,6 +295,7 @@ struct sbi_sta_struct {#define SBI_ERR_ALREADY_AVAILABLE -6#define SBI_ERR_ALREADY_STARTED -7#define SBI_ERR_ALREADY_STOPPED -8 +#define SBI_ERR_NO_SHMEM -9extern unsigned long sbi_spec_version;struct sbiret {Signature · 1 line
2.34.1
PATCH v5 6/22 drivers/perf: riscv: Implement SBI PMU snapshot function
In reply to Atish PatraSBI v2.0 SBI introduced PMU snapshot feature which adds the following features.
1. Read counter values directly from the shared memory instead of csr read.
2. Start multiple counters with initial values with one SBI call.
These functionalities optimizes the number of traps to the higher privilege mode. If the kernel is in VS mode while the hypervisor deploy trap & emulate method, this would minimize all the hpmcounter CSR read traps. If the kernel is running in S-mode, the benefits reduced to CSR latency vs DRAM/cache latency as there is no trap involved while accessing the hpmcounter CSRs.
In both modes, it does saves the number of ecalls while starting multiple counter together with an initial values. This is a likely scenario if multiple counters overflow at the same time.
--- drivers/perf/riscv_pmu.c | 1 + drivers/perf/riscv_pmu_sbi.c | 216 +++++++++++++++++++++++++++++++-- include/linux/perf/riscv_pmu.h | 6 + 3 files changed, 211 insertions(+), 12 deletions(-)
drivers/perf/riscv_pmu.c +1 -0
@@ -404,6 +404,7 @@ struct riscv_pmu *riscv_pmu_alloc(void) cpuc->n_events =0 ;for (i =0 ; i <RISCV_MAX_COUNTERS ; i++) cpuc->events[i] =NULL ; + cpuc->snapshot_addr =NULL ; } pmu->pmu = (struct pmu ) { .event_init = riscv_pmu_event_init,
drivers/perf/riscv_pmu_sbi.c +204 -12
317 lines not on this page · raw message
include/linux/perf/riscv_pmu.h +6 -0
@@ -39,6 +39,12 @@ struct cpu_hw_events {DECLARE_BITMAP (used_hw_ctrs,RISCV_MAX_COUNTERS );/* currently enabled firmware counters */ DECLARE_BITMAP (used_fw_ctrs,RISCV_MAX_COUNTERS ); +/* The virtual address of the shared memory where counter snapshot will be taken */ +void *snapshot_addr; +/* The physical address of the shared memory where counter snapshot will be taken */ +phys_addr_t snapshot_addr_phys; +/* Boolean flag to indicate setup is already done */ +bool snapshot_set_done; };struct riscv_pmu {
Signature · 1 line
2.34.1
Re: [PATCH v5 06/22] drivers/perf: riscv: Implement SBI PMU snapshot function
Quoted 97 lines · Atish Patra
SBI v2.0 SBI introduced PMU snapshot feature which adds the following features.
1. Read counter values directly from the shared memory instead of csr read.
2. Start multiple counters with initial values with one SBI call.These functionalities optimizes the number of traps to the higher privilege mode. If the kernel is in VS mode while the hypervisor deploy trap & emulate method, this would minimize all the hpmcounter CSR read traps. If the kernel is running in S-mode, the benefits reduced to CSR latency vs DRAM/cache latency as there is no trap involved while accessing the hpmcounter CSRs.
In both modes, it does saves the number of ecalls while starting multiple counter together with an initial values. This is a likely scenario if multiple counters overflow at the same time.
Acked-by Palmer Dabbelt <[email protected]>Reviewed-by Anup Patel <[email protected]>Reviewed-by Conor Dooley <[email protected]>Signed-off-by Atish Patra <[email protected]>--- drivers/perf/riscv_pmu.c | 1 + drivers/perf/riscv_pmu_sbi.c | 216 +++++++++++++++++++++++++++++++-- include/linux/perf/riscv_pmu.h | 6 + 3 files changed, 211 insertions(+), 12 deletions(-)drivers/perf/riscv_pmu.c +1
-0@@ -404,6 +404,7 @@ struct riscv_pmu *riscv_pmu_alloc(void) cpuc->n_events =0 ;for (i =0 ; i <RISCV_MAX_COUNTERS ; i++) cpuc->events[i] =NULL ; + cpuc->snapshot_addr =NULL ; } pmu->pmu = (struct pmu ) { .event_init = riscv_pmu_event_init,drivers/perf/riscv_pmu_sbi.c +42
-0@@ -58,6 +58,9 @@ PMU_FORMAT_ATTR(event, "config:0-47");PMU_FORMAT_ATTR (firmware,"config:63" );static bool sbi_v2_available; +static DEFINE_STATIC_KEY_FALSE (sbi_pmu_snapshot_available); +#define sbi_pmu_snapshot_available () \ + static_branch_unlikely(&sbi_pmu_snapshot_available)static struct attribute *riscv_arch_formats_attr[] = { &format_attr_event.attr,@@ -508,14 +511,108 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)return ret; } +static void pmu_sbi_snapshot_free (struct riscv_pmu *pmu) +{ +int cpu; + +for_each_possible_cpu (cpu) { +struct cpu_hw_events *cpu_hw_evt =per_cpu_ptr (pmu->hw_events, cpu); + +if (!cpu_hw_evt->snapshot_addr) +continue ; + +free_page ((unsigned long )cpu_hw_evt->snapshot_addr); + cpu_hw_evt->snapshot_addr =NULL ; + cpu_hw_evt->snapshot_addr_phys =0 ; + } +} + +static int pmu_sbi_snapshot_alloc (struct riscv_pmu *pmu) +{ +int cpu; +struct page *snapshot_page; + +for_each_possible_cpu (cpu) { +struct cpu_hw_events *cpu_hw_evt =per_cpu_ptr (pmu->hw_events, cpu); + +if (cpu_hw_evt->snapshot_addr) +continue ; + + snapshot_page =alloc_page (GFP_ATOMIC | __GFP_ZERO); +if (!snapshot_page) { +pmu_sbi_snapshot_free (pmu); +return -ENOMEM ; + } + cpu_hw_evt->snapshot_addr =page_to_virt (snapshot_page); + cpu_hw_evt->snapshot_addr_phys =page_to_phys (snapshot_page); + } + +return 0 ; +} +
drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig) · line 562
Quoted 10 lines
drivers/perf/riscv_pmu_sbi.c +10
-0+static int pmu_sbi_snapshot_disable (void ) +{ +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM ,-1 , +-1 ,0 ,0 ,0 ,0 ); +if (ret.error) { +pr_warn ("failed to disable snapshot shared memory\n" ); +return sbi_err_map_linux_errno (ret.error); + }
Also need to set snapshot_set_done to false, but I'm not yet convinced that we need snapshot_set_done, especially if we don't allow snapshot_addr_phys to be zero, since zero can then mean set-not-done, but ~0UL is probably a better invalid physical address choice than zero.
Quoted 139 lines
drivers/perf/riscv_pmu_sbi.c +77
-6+ +return 0 ; +} + +static int pmu_sbi_snapshot_setup (struct riscv_pmu *pmu,int cpu) +{ +struct cpu_hw_events *cpu_hw_evt; +struct sbiret ret = {0 }; + + cpu_hw_evt =per_cpu_ptr (pmu->hw_events, cpu); +if (!cpu_hw_evt->snapshot_addr_phys) +return -EINVAL ; + +if (cpu_hw_evt->snapshot_set_done) +return 0 ; + +if (IS_ENABLED (CONFIG_32BIT )) + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM , + cpu_hw_evt->snapshot_addr_phys, + (u64)(cpu_hw_evt->snapshot_addr_phys) >>32 ,0 ,0 ,0 ,0 ); +else + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM , + cpu_hw_evt->snapshot_addr_phys,0 ,0 ,0 ,0 ,0 ); + +/* Free up the snapshot area memory and fall back to SBI PMU calls without snapshot */ +if (ret.error) { +if (ret.error !=SBI_ERR_NOT_SUPPORTED ) +pr_warn ("pmu snapshot setup failed with error %ld\n" , ret.error); +return sbi_err_map_linux_errno (ret.error); + } + + cpu_hw_evt->snapshot_set_done = true; + +return 0 ; +} +static u64 pmu_sbi_ctr_read (struct perf_event *event) {struct hw_perf_event *hwc = &event->hw;int idx = hwc->idx;struct sbiret ret;u64 val =0 ; +struct riscv_pmu *pmu =to_riscv_pmu (event->pmu); +struct cpu_hw_events *cpu_hw_evt =this_cpu_ptr (pmu->hw_events); +struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;union sbi_pmu_ctr_info info = pmu_ctr_list[idx]; +/* Read the value from the shared memory directly */ +if (sbi_pmu_snapshot_available ()) { + val = sdata->ctr_values[idx]; +return val; + } +if (pmu_sbi_is_fw_event (event)) { ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_FW_READ , hwc->idx,0 ,0 ,0 ,0 ,0 );@@ -565,6 +662,7 @@ static void pmu_sbi_ctr_start(struct perf_event *event, u64 ival)struct hw_perf_event *hwc = &event->hw;unsigned long flag =SBI_PMU_START_FLAG_SET_INIT_VALUE ; +/* There is no benefit setting SNAPSHOT FLAG for a single counter */ #if defined(CONFIG_32BIT ) ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_START , hwc->idx,1 , flag, ival, ival >>32 ,0 );@@ -585,16 +683,36 @@ static void pmu_sbi_ctr_stop(struct perf_event *event, unsigned long flag) {struct sbiret ret;struct hw_perf_event *hwc = &event->hw; +struct riscv_pmu *pmu =to_riscv_pmu (event->pmu); +struct cpu_hw_events *cpu_hw_evt =this_cpu_ptr (pmu->hw_events); +struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;if ((hwc->flags &PERF_EVENT_FLAG_USER_ACCESS ) && (hwc->flags &PERF_EVENT_FLAG_USER_READ_CNT ))pmu_sbi_reset_scounteren ((void *)event); +if (sbi_pmu_snapshot_available ()) + flag |=SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ; + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_STOP , hwc->idx,1 , flag,0 ,0 ,0 ); -if (ret.error && (ret.error !=SBI_ERR_ALREADY_STOPPED ) && - flag !=SBI_PMU_STOP_FLAG_RESET ) +if (!ret.error &&sbi_pmu_snapshot_available ()) { +/* +* The counter snapshot is based on the index base specified by hwc->idx. +* The actual counter value is updated in shared memory at index 0 when counter +* mask is 0x01. To ensure accurate counter values, it's necessary to transfer +* the counter value to shared memory. However, if hwc->idx is zero, the counter +* value is already correctly updated in shared memory, requiring no further +* adjustment. +*/ +if (hwc->idx >0 ) { + sdata->ctr_values[hwc->idx] = sdata->ctr_values[0 ]; + sdata->ctr_values[0 ] =0 ; + } + }else if (ret.error && (ret.error !=SBI_ERR_ALREADY_STOPPED ) && + flag !=SBI_PMU_STOP_FLAG_RESET ) {pr_err ("Stopping counter idx %d failed with error %d\n" , hwc->idx,sbi_err_map_linux_errno (ret.error)); + } }static int pmu_sbi_find_num_ctrs (void )@@ -652,10 +770,14 @@ static inline void pmu_sbi_stop_all(struct riscv_pmu *pmu)static inline void pmu_sbi_stop_hw_ctrs (struct riscv_pmu *pmu) {struct cpu_hw_events *cpu_hw_evt =this_cpu_ptr (pmu->hw_events); +unsigned long flag =0 ; + +if (sbi_pmu_snapshot_available ()) + flag =SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ;/* No need to check the error here as we can't do anything about the error */ sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_STOP ,0 , - cpu_hw_evt->used_hw_ctrs[0 ],0 ,0 ,0 ,0 ); + cpu_hw_evt->used_hw_ctrs[0 ], flag,0 ,0 ,0 ); }/* @@ -664,11 +786,10 @@ static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu)* while the overflowed counters need to be started with updated initialization * value. */ -static inline void pmu_sbi_start_overflow_mask (struct riscv_pmu *pmu, -unsigned long ctr_ovf_mask) +static noinline void pmu_sbi_start_ovf_ctrs_sbi(struct cpu_hw_events *cpu_hw_evt, +unsigned long ctr_ovf_mask) {int idx =0 ; -struct cpu_hw_events *cpu_hw_evt =this_cpu_ptr (pmu->hw_events);struct perf_event *event;unsigned long flag =SBI_PMU_START_FLAG_SET_INIT_VALUE ;unsigned long ctr_start_mask =0 ;@@ -703,6 +824,48 @@ static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu, } } +static noinline void pmu_sbi_start_ovf_ctrs_snapshot(struct cpu_hw_events *cpu_hw_evt, +unsigned long ctr_ovf_mask)
drivers/perf/riscv_pmu_sbi.c static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu, · line 838
Quoted 10 lines
drivers/perf/riscv_pmu_sbi.c +10
-0+{ +int idx =0 ; +struct perf_event *event; +unsigned long flag =SBI_PMU_START_FLAG_INIT_SNAPSHOT ; +u64 max_period, init_val =0 ; +struct hw_perf_event *hwc; +struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr; + +for_each_set_bit (idx, cpu_hw_evt->used_hw_ctrs,RISCV_MAX_COUNTERS ) { +if (ctr_ovf_mask & (BIT (idx))) {
Unnecessary () around BIT()
Quoted 104 lines
drivers/perf/riscv_pmu_sbi.c +56
-6+ event = cpu_hw_evt->events[idx]; + hwc = &event->hw; + max_period =riscv_pmu_ctr_get_width_mask (event); + init_val =local64_read (&hwc->prev_count) & max_period; + sdata->ctr_values[idx] = init_val; + } +/* +* We do not need to update the non-overflow counters the previous +* value should have been there already. +*/ + } + +for (idx =0 ; idx <BITS_TO_LONGS (RISCV_MAX_COUNTERS ); idx++) { +/* Start all the counters in a single shot */ +sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_START , idx *BITS_PER_LONG , + cpu_hw_evt->used_hw_ctrs[idx], flag,0 ,0 ,0 ); + } +} + +static void pmu_sbi_start_overflow_mask (struct riscv_pmu *pmu, +unsigned long ctr_ovf_mask) +{ +struct cpu_hw_events *cpu_hw_evt =this_cpu_ptr (pmu->hw_events); + +if (sbi_pmu_snapshot_available ()) +pmu_sbi_start_ovf_ctrs_snapshot (cpu_hw_evt, ctr_ovf_mask); +else +pmu_sbi_start_ovf_ctrs_sbi (cpu_hw_evt, ctr_ovf_mask); +} +static irqreturn_t pmu_sbi_ovf_handler (int irq,void *dev) {struct perf_sample_data data;@@ -716,6 +879,7 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)unsigned long overflowed_ctrs =0 ;struct cpu_hw_events *cpu_hw_evt = dev;u64 start_clock =sched_clock (); +struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;if (WARN_ON_ONCE (!cpu_hw_evt))return IRQ_NONE ;@@ -737,8 +901,10 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)pmu_sbi_stop_hw_ctrs (pmu);/* Overflow status register should only be read after counter are stopped */ -ALT_SBI_PMU_OVERFLOW (overflow); - +if (sbi_pmu_snapshot_available ()) + overflow = sdata->ctr_overflow_mask; +else +ALT_SBI_PMU_OVERFLOW (overflow);/* * Overflow interrupt pending bit should only be cleared after stopping * all the counters to avoid any race condition. @@ -819,6 +985,9 @@ static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)enable_percpu_irq (riscv_pmu_irq,IRQ_TYPE_NONE ); } +if (sbi_pmu_snapshot_available ()) +return pmu_sbi_snapshot_setup (pmu, cpu); +return 0 ; }@@ -831,6 +1000,9 @@ static int pmu_sbi_dying_cpu(unsigned int cpu, struct hlist_node *node)/* Disable all counters access for user mode now */ csr_write (CSR_SCOUNTEREN ,0x0 ); +if (sbi_pmu_snapshot_available ()) +return pmu_sbi_snapshot_disable (); +return 0 ; }@@ -1106,10 +1278,6 @@ static int pmu_sbi_device_probe(struct platform_device *pdev) pmu->event_unmapped = pmu_sbi_event_unmapped; pmu->csr_index = pmu_sbi_csr_index; - ret =cpuhp_state_add_instance (CPUHP_AP_PERF_RISCV_STARTING , &pmu->node); -if (ret) -return ret; - ret =riscv_pm_pmu_register (pmu);if (ret) goto out_unregister;@@ -1118,8 +1286,32 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)if (ret) goto out_unregister; +/* SBI PMU Snapsphot is only available in SBI v2.0 */ +if (sbi_v2_available) { + ret =pmu_sbi_snapshot_alloc (pmu); +if (ret) + goto out_unregister; + + ret =pmu_sbi_snapshot_setup (pmu,smp_processor_id ()); +if (!ret) { +pr_info ("SBI PMU snapshot detected\n" ); +/* +* We enable it once here for the boot cpu. If snapshot shmem setup +* fails during cpu hotplug process, it will fail to start the cpu +* as we can not handle hetergenous PMUs with different snapshot +* capability. +*/
drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_device_probe(struct platform_device *pdev) · line 1313
Quoted 10 lines
drivers/perf/riscv_pmu_sbi.c +8
-0+static_branch_enable (&sbi_pmu_snapshot_available); + } +/* Snapshot is an optional feature. Continue if not available */ + } +register_sysctl ("kernel" , sbi_pmu_sysctl_table); + ret =cpuhp_state_add_instance (CPUHP_AP_PERF_RISCV_STARTING , &pmu->node); +if (ret) +return ret;
This should be goto out_unregister, and in the case of sbi_v2_available we also need a pmu_sbi_snapshot_free() and a pmu_sbi_snapshot_disable().
Quoted 24 lines
drivers/perf/riscv_pmu_sbi.c +1
-0+return 0 ; out_unregister:include/linux/perf/riscv_pmu.h +6
-0@@ -39,6 +39,12 @@ struct cpu_hw_events {DECLARE_BITMAP (used_hw_ctrs,RISCV_MAX_COUNTERS );/* currently enabled firmware counters */ DECLARE_BITMAP (used_fw_ctrs,RISCV_MAX_COUNTERS ); +/* The virtual address of the shared memory where counter snapshot will be taken */ +void *snapshot_addr; +/* The physical address of the shared memory where counter snapshot will be taken */ +phys_addr_t snapshot_addr_phys; +/* Boolean flag to indicate setup is already done */ +bool snapshot_set_done; };struct riscv_pmu {Signature · 1 line
2.34.1
Thanks,
drew
Re: [PATCH v5 06/22] drivers/perf: riscv: Implement SBI PMU snapshot function
Quoted 102 lines · Andrew Jones
Quoted 101 lines · Atish Patra
SBI v2.0 SBI introduced PMU snapshot feature which adds the following features.
1. Read counter values directly from the shared memory instead of csr read.
2. Start multiple counters with initial values with one SBI call.These functionalities optimizes the number of traps to the higher privilege mode. If the kernel is in VS mode while the hypervisor deploy trap & emulate method, this would minimize all the hpmcounter CSR read traps. If the kernel is running in S-mode, the benefits reduced to CSR latency vs DRAM/cache latency as there is no trap involved while accessing the hpmcounter CSRs.
In both modes, it does saves the number of ecalls while starting multiple counter together with an initial values. This is a likely scenario if multiple counters overflow at the same time.
Acked-by Palmer Dabbelt <[email protected]>Reviewed-by Anup Patel <[email protected]>Reviewed-by Conor Dooley <[email protected]>Signed-off-by Atish Patra <[email protected]>--- drivers/perf/riscv_pmu.c | 1 + drivers/perf/riscv_pmu_sbi.c | 216 +++++++++++++++++++++++++++++++-- include/linux/perf/riscv_pmu.h | 6 + 3 files changed, 211 insertions(+), 12 deletions(-)drivers/perf/riscv_pmu.c +1
-0@@ -404,6 +404,7 @@ struct riscv_pmu *riscv_pmu_alloc(void) cpuc->n_events =0 ;for (i =0 ; i <RISCV_MAX_COUNTERS ; i++) cpuc->events[i] =NULL ; + cpuc->snapshot_addr =NULL ; } pmu->pmu = (struct pmu ) { .event_init = riscv_pmu_event_init,drivers/perf/riscv_pmu_sbi.c +46
-0@@ -58,6 +58,9 @@ PMU_FORMAT_ATTR(event, "config:0-47");PMU_FORMAT_ATTR (firmware,"config:63" );static bool sbi_v2_available; +static DEFINE_STATIC_KEY_FALSE (sbi_pmu_snapshot_available); +#define sbi_pmu_snapshot_available () \ + static_branch_unlikely(&sbi_pmu_snapshot_available)static struct attribute *riscv_arch_formats_attr[] = { &format_attr_event.attr,@@ -508,14 +511,108 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)return ret; } +static void pmu_sbi_snapshot_free (struct riscv_pmu *pmu) +{ +int cpu; + +for_each_possible_cpu (cpu) { +struct cpu_hw_events *cpu_hw_evt =per_cpu_ptr (pmu->hw_events, cpu); + +if (!cpu_hw_evt->snapshot_addr) +continue ; + +free_page ((unsigned long )cpu_hw_evt->snapshot_addr); + cpu_hw_evt->snapshot_addr =NULL ; + cpu_hw_evt->snapshot_addr_phys =0 ; + } +} + +static int pmu_sbi_snapshot_alloc (struct riscv_pmu *pmu) +{ +int cpu; +struct page *snapshot_page; + +for_each_possible_cpu (cpu) { +struct cpu_hw_events *cpu_hw_evt =per_cpu_ptr (pmu->hw_events, cpu); + +if (cpu_hw_evt->snapshot_addr) +continue ; + + snapshot_page =alloc_page (GFP_ATOMIC | __GFP_ZERO); +if (!snapshot_page) { +pmu_sbi_snapshot_free (pmu); +return -ENOMEM ; + } + cpu_hw_evt->snapshot_addr =page_to_virt (snapshot_page); + cpu_hw_evt->snapshot_addr_phys =page_to_phys (snapshot_page); + } + +return 0 ; +} + +static int pmu_sbi_snapshot_disable (void ) +{ +struct sbiret ret; +
Quoted 8 lines
drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig) · line 562
Quoted 6 lines
drivers/perf/riscv_pmu_sbi.c +6
-0+ ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM ,-1 , +-1 ,0 ,0 ,0 ,0 ); +if (ret.error) { +pr_warn ("failed to disable snapshot shared memory\n" ); +return sbi_err_map_linux_errno (ret.error); + }Also need to set
snapshot_set_doneto false, but I'm not yet convinced
Done.
Quoted 4 lines
that we need
snapshot_set_done, especially if we don't allowsnapshot_addr_physto be zero, since zero can then mean set-not-done, but ~0UL is probably a better invalid physical address choice than zero.
Agreed. But I don't see any benefit either way. snapshot_set_done is just more explicit way of doing the same thing without interpreting what zero means.
If you think there is a benefit or you feel storngly about it, I can change it you suggested approach.
Btw, I just realized that you had a comment about this in v4 as well. Sorry I missed that and did not reply there.
Quoted 137 lines
Quoted 137 lines
drivers/perf/riscv_pmu_sbi.c +75
-6+ +return 0 ; +} + +static int pmu_sbi_snapshot_setup (struct riscv_pmu *pmu,int cpu) +{ +struct cpu_hw_events *cpu_hw_evt; +struct sbiret ret = {0 }; + + cpu_hw_evt =per_cpu_ptr (pmu->hw_events, cpu); +if (!cpu_hw_evt->snapshot_addr_phys) +return -EINVAL ; + +if (cpu_hw_evt->snapshot_set_done) +return 0 ; + +if (IS_ENABLED (CONFIG_32BIT )) + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM , + cpu_hw_evt->snapshot_addr_phys, + (u64)(cpu_hw_evt->snapshot_addr_phys) >>32 ,0 ,0 ,0 ,0 ); +else + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM , + cpu_hw_evt->snapshot_addr_phys,0 ,0 ,0 ,0 ,0 ); + +/* Free up the snapshot area memory and fall back to SBI PMU calls without snapshot */ +if (ret.error) { +if (ret.error !=SBI_ERR_NOT_SUPPORTED ) +pr_warn ("pmu snapshot setup failed with error %ld\n" , ret.error); +return sbi_err_map_linux_errno (ret.error); + } + + cpu_hw_evt->snapshot_set_done = true; + +return 0 ; +} +static u64 pmu_sbi_ctr_read (struct perf_event *event) {struct hw_perf_event *hwc = &event->hw;int idx = hwc->idx;struct sbiret ret;u64 val =0 ; +struct riscv_pmu *pmu =to_riscv_pmu (event->pmu); +struct cpu_hw_events *cpu_hw_evt =this_cpu_ptr (pmu->hw_events); +struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;union sbi_pmu_ctr_info info = pmu_ctr_list[idx]; +/* Read the value from the shared memory directly */ +if (sbi_pmu_snapshot_available ()) { + val = sdata->ctr_values[idx]; +return val; + } +if (pmu_sbi_is_fw_event (event)) { ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_FW_READ , hwc->idx,0 ,0 ,0 ,0 ,0 );@@ -565,6 +662,7 @@ static void pmu_sbi_ctr_start(struct perf_event *event, u64 ival)struct hw_perf_event *hwc = &event->hw;unsigned long flag =SBI_PMU_START_FLAG_SET_INIT_VALUE ; +/* There is no benefit setting SNAPSHOT FLAG for a single counter */ #if defined(CONFIG_32BIT ) ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_START , hwc->idx,1 , flag, ival, ival >>32 ,0 );@@ -585,16 +683,36 @@ static void pmu_sbi_ctr_stop(struct perf_event *event, unsigned long flag) {struct sbiret ret;struct hw_perf_event *hwc = &event->hw; +struct riscv_pmu *pmu =to_riscv_pmu (event->pmu); +struct cpu_hw_events *cpu_hw_evt =this_cpu_ptr (pmu->hw_events); +struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;if ((hwc->flags &PERF_EVENT_FLAG_USER_ACCESS ) && (hwc->flags &PERF_EVENT_FLAG_USER_READ_CNT ))pmu_sbi_reset_scounteren ((void *)event); +if (sbi_pmu_snapshot_available ()) + flag |=SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ; + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_STOP , hwc->idx,1 , flag,0 ,0 ,0 ); -if (ret.error && (ret.error !=SBI_ERR_ALREADY_STOPPED ) && - flag !=SBI_PMU_STOP_FLAG_RESET ) +if (!ret.error &&sbi_pmu_snapshot_available ()) { +/* +* The counter snapshot is based on the index base specified by hwc->idx. +* The actual counter value is updated in shared memory at index 0 when counter +* mask is 0x01. To ensure accurate counter values, it's necessary to transfer +* the counter value to shared memory. However, if hwc->idx is zero, the counter +* value is already correctly updated in shared memory, requiring no further +* adjustment. +*/ +if (hwc->idx >0 ) { + sdata->ctr_values[hwc->idx] = sdata->ctr_values[0 ]; + sdata->ctr_values[0 ] =0 ; + } + }else if (ret.error && (ret.error !=SBI_ERR_ALREADY_STOPPED ) && + flag !=SBI_PMU_STOP_FLAG_RESET ) {pr_err ("Stopping counter idx %d failed with error %d\n" , hwc->idx,sbi_err_map_linux_errno (ret.error)); + } }static int pmu_sbi_find_num_ctrs (void )@@ -652,10 +770,14 @@ static inline void pmu_sbi_stop_all(struct riscv_pmu *pmu)static inline void pmu_sbi_stop_hw_ctrs (struct riscv_pmu *pmu) {struct cpu_hw_events *cpu_hw_evt =this_cpu_ptr (pmu->hw_events); +unsigned long flag =0 ; + +if (sbi_pmu_snapshot_available ()) + flag =SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ;/* No need to check the error here as we can't do anything about the error */ sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_STOP ,0 , - cpu_hw_evt->used_hw_ctrs[0 ],0 ,0 ,0 ,0 ); + cpu_hw_evt->used_hw_ctrs[0 ], flag,0 ,0 ,0 ); }/* @@ -664,11 +786,10 @@ static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu)* while the overflowed counters need to be started with updated initialization * value. */ -static inline void pmu_sbi_start_overflow_mask (struct riscv_pmu *pmu, -unsigned long ctr_ovf_mask) +static noinline void pmu_sbi_start_ovf_ctrs_sbi(struct cpu_hw_events *cpu_hw_evt, +unsigned long ctr_ovf_mask) {int idx =0 ; -struct cpu_hw_events *cpu_hw_evt =this_cpu_ptr (pmu->hw_events);struct perf_event *event;unsigned long flag =SBI_PMU_START_FLAG_SET_INIT_VALUE ;unsigned long ctr_start_mask =0 ;@@ -703,6 +824,48 @@ static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu, } }
Quoted 15 lines
drivers/perf/riscv_pmu_sbi.c static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu, · line 838
Quoted 12 lines
drivers/perf/riscv_pmu_sbi.c +12
-0+static noinline void pmu_sbi_start_ovf_ctrs_snapshot(struct cpu_hw_events *cpu_hw_evt, +unsigned long ctr_ovf_mask) +{ +int idx =0 ; +struct perf_event *event; +unsigned long flag =SBI_PMU_START_FLAG_INIT_SNAPSHOT ; +u64 max_period, init_val =0 ; +struct hw_perf_event *hwc; +struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr; + +for_each_set_bit (idx, cpu_hw_evt->used_hw_ctrs,RISCV_MAX_COUNTERS ) { +if (ctr_ovf_mask & (BIT (idx))) {Unnecessary () around
BIT()
Fixed.
Quoted 111 lines
Quoted 111 lines
drivers/perf/riscv_pmu_sbi.c +61
-6+ event = cpu_hw_evt->events[idx]; + hwc = &event->hw; + max_period =riscv_pmu_ctr_get_width_mask (event); + init_val =local64_read (&hwc->prev_count) & max_period; + sdata->ctr_values[idx] = init_val; + } +/* +* We do not need to update the non-overflow counters the previous +* value should have been there already. +*/ + } + +for (idx =0 ; idx <BITS_TO_LONGS (RISCV_MAX_COUNTERS ); idx++) { +/* Start all the counters in a single shot */ +sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_START , idx *BITS_PER_LONG , + cpu_hw_evt->used_hw_ctrs[idx], flag,0 ,0 ,0 ); + } +} + +static void pmu_sbi_start_overflow_mask (struct riscv_pmu *pmu, +unsigned long ctr_ovf_mask) +{ +struct cpu_hw_events *cpu_hw_evt =this_cpu_ptr (pmu->hw_events); + +if (sbi_pmu_snapshot_available ()) +pmu_sbi_start_ovf_ctrs_snapshot (cpu_hw_evt, ctr_ovf_mask); +else +pmu_sbi_start_ovf_ctrs_sbi (cpu_hw_evt, ctr_ovf_mask); +} +static irqreturn_t pmu_sbi_ovf_handler (int irq,void *dev) {struct perf_sample_data data;@@ -716,6 +879,7 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)unsigned long overflowed_ctrs =0 ;struct cpu_hw_events *cpu_hw_evt = dev;u64 start_clock =sched_clock (); +struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;if (WARN_ON_ONCE (!cpu_hw_evt))return IRQ_NONE ;@@ -737,8 +901,10 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)pmu_sbi_stop_hw_ctrs (pmu);/* Overflow status register should only be read after counter are stopped */ -ALT_SBI_PMU_OVERFLOW (overflow); - +if (sbi_pmu_snapshot_available ()) + overflow = sdata->ctr_overflow_mask; +else +ALT_SBI_PMU_OVERFLOW (overflow);/* * Overflow interrupt pending bit should only be cleared after stopping * all the counters to avoid any race condition. @@ -819,6 +985,9 @@ static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)enable_percpu_irq (riscv_pmu_irq,IRQ_TYPE_NONE ); } +if (sbi_pmu_snapshot_available ()) +return pmu_sbi_snapshot_setup (pmu, cpu); +return 0 ; }@@ -831,6 +1000,9 @@ static int pmu_sbi_dying_cpu(unsigned int cpu, struct hlist_node *node)/* Disable all counters access for user mode now */ csr_write (CSR_SCOUNTEREN ,0x0 ); +if (sbi_pmu_snapshot_available ()) +return pmu_sbi_snapshot_disable (); +return 0 ; }@@ -1106,10 +1278,6 @@ static int pmu_sbi_device_probe(struct platform_device *pdev) pmu->event_unmapped = pmu_sbi_event_unmapped; pmu->csr_index = pmu_sbi_csr_index; - ret =cpuhp_state_add_instance (CPUHP_AP_PERF_RISCV_STARTING , &pmu->node); -if (ret) -return ret; - ret =riscv_pm_pmu_register (pmu);if (ret) goto out_unregister;@@ -1118,8 +1286,32 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)if (ret) goto out_unregister; +/* SBI PMU Snapsphot is only available in SBI v2.0 */ +if (sbi_v2_available) { + ret =pmu_sbi_snapshot_alloc (pmu); +if (ret) + goto out_unregister; + + ret =pmu_sbi_snapshot_setup (pmu,smp_processor_id ()); +if (!ret) { +pr_info ("SBI PMU snapshot detected\n" ); +/* +* We enable it once here for the boot cpu. If snapshot shmem setup +* fails during cpu hotplug process, it will fail to start the cpu +* as we can not handle hetergenous PMUs with different snapshot +* capability. +*/ +static_branch_enable (&sbi_pmu_snapshot_available); + } +/* Snapshot is an optional feature. Continue if not available */ + } +register_sysctl ("kernel" , sbi_pmu_sysctl_table);
Quoted 7 lines
drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_device_probe(struct platform_device *pdev) · line 1313
Quoted 3 lines
drivers/perf/riscv_pmu_sbi.c +3
-0+ ret =cpuhp_state_add_instance (CPUHP_AP_PERF_RISCV_STARTING , &pmu->node); +if (ret) +return ret;This should be goto
out_unregister, and in the case ofsbi_v2_availablewe also need apmu_sbi_snapshot_free()and apmu_sbi_snapshot_disable().
Yes. Thanks for catching it. Fixed
Quoted 27 lines
Quoted 24 lines
drivers/perf/riscv_pmu_sbi.c +1
-0+return 0 ; out_unregister:include/linux/perf/riscv_pmu.h +6
-0@@ -39,6 +39,12 @@ struct cpu_hw_events {DECLARE_BITMAP (used_hw_ctrs,RISCV_MAX_COUNTERS );/* currently enabled firmware counters */ DECLARE_BITMAP (used_fw_ctrs,RISCV_MAX_COUNTERS ); +/* The virtual address of the shared memory where counter snapshot will be taken */ +void *snapshot_addr; +/* The physical address of the shared memory where counter snapshot will be taken */ +phys_addr_t snapshot_addr_phys; +/* Boolean flag to indicate setup is already done */ +bool snapshot_set_done; };struct riscv_pmu {Signature · 1 line
2.34.1Thanks,
drew
Re: [PATCH v5 06/22] drivers/perf: riscv: Implement SBI PMU snapshot function
Quoted 2 lines · Atish Patra
Quoted 1 line · Andrew Jones
On Wed, Apr 03, 2024 at 01:04:35AM -0700, Atish Patra wrote:
...
Quoted 20 lines
Quoted 4 lines
Quoted 4 lines
drivers/perf/riscv_pmu_sbi.c +4
-0+static int pmu_sbi_snapshot_disable (void ) +{ +struct sbiret ret; +Quoted 8 lines
drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig) · line 562
Quoted 6 lines
drivers/perf/riscv_pmu_sbi.c +6
-0+ ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM ,-1 , +-1 ,0 ,0 ,0 ,0 ); +if (ret.error) { +pr_warn ("failed to disable snapshot shared memory\n" ); +return sbi_err_map_linux_errno (ret.error); + }Also need to set
snapshot_set_doneto false, but I'm not yet convincedDone.
Quoted 4 lines
that we need
snapshot_set_done, especially if we don't allowsnapshot_addr_physto be zero, since zero can then mean set-not-done, but ~0UL is probably a better invalid physical address choice than zero.
Quoted 7 lines
Agreed. But I don't see any benefit either way.
snapshot_set_doneis just more explicit way of doing the same thing without interpreting what zero means.If you think there is a benefit or you feel storngly about it, I can change it you suggested approach.
I don't have a strong opinion on it. I'm just reluctant to add redundant state, not only because it increases size, but also because we have to keep track of it, like in the example above, where we needed to remember to reset the extra state to false.
Of course, giving invalid addresses additional meanings also comes with its own code maintenance trade-offs, so either way...
Thanks,
drew
Re: [PATCH v5 06/22] drivers/perf: riscv: Implement SBI PMU snapshot function
In reply to Atish PatraOn Wed, Apr 03, 2024 at 01:04:35AM -0700, Atish Patra wrote:
...
drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig) · line 558
Quoted 6 lines
drivers/perf/riscv_pmu_sbi.c +6
-0+static int pmu_sbi_snapshot_disable (void ) +{ +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM ,-1 , +-1 ,0 ,0 ,0 ,0 );
The "Rename the SBI_STA_SHMEM_DISABLE" patch should come before this patch so SBI_SHMEM_DISABLE can be used instead of the -1's here.
Thanks,
drew
Re: [PATCH v5 06/22] drivers/perf: riscv: Implement SBI PMU snapshot function
Quoted 12 lines · Andrew Jones
On Wed, Apr 03, 2024 at 01:04:35AM -0700, Atish Patra wrote:
...drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig) · line 558
Quoted 6 lines
drivers/perf/riscv_pmu_sbi.c +6
-0+static int pmu_sbi_snapshot_disable (void ) +{ +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM ,-1 , +-1 ,0 ,0 ,0 ,0 );The "Rename the
SBI_STA_SHMEM_DISABLE" patch should come before this patch soSBI_SHMEM_DISABLEcan be used instead of the -1's here.
Sure. Done.
Quoted 2 lines
Thanks,
drew
PATCH v5 7/22 drivers/perf: riscv: Fix counter mask iteration for RV32
In reply to Atish PatraFor RV32, used_hw_ctrs can have more than 1 word if the firmware chooses to interleave firmware/hardware counters indicies. Even though it's a unlikely scenario, handle that case by iterating over all the words instead of just using the first word.
--- drivers/perf/riscv_pmu_sbi.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/perf/riscv_pmu_sbi.c +12 -9
@@ -771,13 +771,15 @@ static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu) {struct cpu_hw_events *cpu_hw_evt =this_cpu_ptr (pmu->hw_events);unsigned long flag =0 ; +int i;if (sbi_pmu_snapshot_available ()) flag =SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ; -/* No need to check the error here as we can't do anything about the error */ -sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_STOP ,0 , - cpu_hw_evt->used_hw_ctrs[0 ], flag,0 ,0 ,0 ); +for (i =0 ; i <BITS_TO_LONGS (RISCV_MAX_COUNTERS ); i++) +/* No need to check the error here as we can't do anything about the error */ +sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_STOP , i *BITS_PER_LONG , + cpu_hw_evt->used_hw_ctrs[i], flag,0 ,0 ,0 ); }/*
@@ -789,7 +791,7 @@ static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu) static noinline void pmu_sbi_start_ovf_ctrs_sbi(struct cpu_hw_events *cpu_hw_evt,unsigned long ctr_ovf_mask) { -int idx =0 ; +int idx =0 , i;struct perf_event *event;unsigned long flag =SBI_PMU_START_FLAG_SET_INIT_VALUE ;unsigned long ctr_start_mask =0 ;
@@ -797,11 +799,12 @@ static noinline void pmu_sbi_start_ovf_ctrs_sbi(struct cpu_hw_events *cpu_hw_evtstruct hw_perf_event *hwc;u64 init_val =0 ; - ctr_start_mask = cpu_hw_evt->used_hw_ctrs[0 ] & ~ctr_ovf_mask; - -/* Start all the counters that did not overflow in a single shot */ -sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_START ,0 , ctr_start_mask, -0 ,0 ,0 ,0 ); +for (i =0 ; i <BITS_TO_LONGS (RISCV_MAX_COUNTERS ); i++) { + ctr_start_mask = cpu_hw_evt->used_hw_ctrs[i] & ~ctr_ovf_mask; +/* Start all the counters that did not overflow in a single shot */ +sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_START , i *BITS_PER_LONG , ctr_start_mask, +0 ,0 ,0 ,0 ); + }/* Reinitialize and start all the counter that overflowed */ while (ctr_ovf_mask) {
Signature · 1 line
2.34.1
Re: [PATCH v5 07/22] drivers/perf: riscv: Fix counter mask iteration for RV32
Quoted 63 lines · Atish Patra
For RV32,
used_hw_ctrscan have more than 1 word if the firmware chooses to interleavefirmware/hardwarecounters indicies. Even though it's a unlikely scenario, handle that case by iterating over all the words instead of just using the first word.Signed-off-by Atish Patra <[email protected]>--- drivers/perf/riscv_pmu_sbi.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)drivers/perf/riscv_pmu_sbi.c +12
-9@@ -771,13 +771,15 @@ static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu) {struct cpu_hw_events *cpu_hw_evt =this_cpu_ptr (pmu->hw_events);unsigned long flag =0 ; +int i;if (sbi_pmu_snapshot_available ()) flag =SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ; -/* No need to check the error here as we can't do anything about the error */ -sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_STOP ,0 , - cpu_hw_evt->used_hw_ctrs[0 ], flag,0 ,0 ,0 ); +for (i =0 ; i <BITS_TO_LONGS (RISCV_MAX_COUNTERS ); i++) +/* No need to check the error here as we can't do anything about the error */ +sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_STOP , i *BITS_PER_LONG , + cpu_hw_evt->used_hw_ctrs[i], flag,0 ,0 ,0 ); }/* @@ -789,7 +791,7 @@ static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu) static noinline void pmu_sbi_start_ovf_ctrs_sbi(struct cpu_hw_events *cpu_hw_evt,unsigned long ctr_ovf_mask) { -int idx =0 ; +int idx =0 , i;struct perf_event *event;unsigned long flag =SBI_PMU_START_FLAG_SET_INIT_VALUE ;unsigned long ctr_start_mask =0 ;@@ -797,11 +799,12 @@ static noinline void pmu_sbi_start_ovf_ctrs_sbi(struct cpu_hw_events *cpu_hw_evtstruct hw_perf_event *hwc;u64 init_val =0 ; - ctr_start_mask = cpu_hw_evt->used_hw_ctrs[0 ] & ~ctr_ovf_mask; - -/* Start all the counters that did not overflow in a single shot */ -sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_START ,0 , ctr_start_mask, -0 ,0 ,0 ,0 ); +for (i =0 ; i <BITS_TO_LONGS (RISCV_MAX_COUNTERS ); i++) { + ctr_start_mask = cpu_hw_evt->used_hw_ctrs[i] & ~ctr_ovf_mask; +/* Start all the counters that did not overflow in a single shot */ +sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_START , i *BITS_PER_LONG , ctr_start_mask, +0 ,0 ,0 ,0 ); + }/* Reinitialize and start all the counter that overflowed */ while (ctr_ovf_mask) {Signature · 1 line
2.34.1
PATCH v5 8/22 RISC-V: KVM: Fix the initial sample period value
In reply to Atish PatraThe initial sample period value when counter value is not assigned should be set to maximum value supported by the counter width. Otherwise, it may result in spurious interrupts.
--- arch/riscv/kvm/vcpu_pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kvm/vcpu_pmu.c +1 -1
@@ -39,7 +39,7 @@ static u64 kvm_pmu_get_sample_period(struct kvm_pmc *pmc)u64 sample_period;if (!pmc->counter_val) - sample_period = counter_val_mask +1 ; + sample_period = counter_val_mask;else sample_period = (-pmc->counter_val) & counter_val_mask;
Signature · 1 line
2.34.1
Re: [PATCH v5 08/22] RISC-V: KVM: Fix the initial sample period value
Quoted 25 lines · Atish Patra
The initial sample period value when counter value is not assigned should be set to maximum value supported by the counter width. Otherwise, it may result in spurious interrupts.
Signed-off-by Atish Patra <[email protected]>--- arch/riscv/kvm/vcpu_pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/riscv/kvm/vcpu_pmu.c +1
-1@@ -39,7 +39,7 @@ static u64 kvm_pmu_get_sample_period(struct kvm_pmc *pmc)u64 sample_period;if (!pmc->counter_val) - sample_period = counter_val_mask +1 ; + sample_period = counter_val_mask;else sample_period = (-pmc->counter_val) & counter_val_mask;Signature · 1 line
2.34.1
PATCH v5 9/22 RISC-V: KVM: Rename the SBI_STA_SHMEM_DISABLE to a generic name
In reply to Atish PatraSBI_STA_SHMEM_DISABLE is a macro to invoke disable shared memory commands. As this can be invoked from other SBI extension context as well, rename it to more generic name as SBI_SHMEM_DISABLE.
--- arch/riscv/include/asm/sbi.h | 2 +- arch/riscv/kernel/paravirt.c | 6 +++--- arch/riscv/kvm/vcpu_sbi_sta.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
arch/riscv/include/asm/sbi.h +1 -1
@@ -277,7 +277,7 @@ struct sbi_sta_struct {u8 pad[47 ]; } __packed; -#define SBI_STA_SHMEM_DISABLE -1 +#define SBI_SHMEM_DISABLE -1/* SBI spec version fields */ #define SBI_SPEC_VERSION_DEFAULT 0x1
arch/riscv/kernel/paravirt.c +3 -3
@@ -62,7 +62,7 @@ static int sbi_sta_steal_time_set_shmem(unsigned long lo, unsigned long hi, ret =sbi_ecall (SBI_EXT_STA ,SBI_EXT_STA_STEAL_TIME_SET_SHMEM , lo, hi, flags,0 ,0 ,0 );if (ret.error) { -if (lo ==SBI_STA_SHMEM_DISABLE && hi ==SBI_STA_SHMEM_DISABLE ) +if (lo ==SBI_SHMEM_DISABLE && hi ==SBI_SHMEM_DISABLE )pr_warn ("Failed to disable steal-time shmem" );else pr_warn ("Failed to set steal-time shmem" );
@@ -84,8 +84,8 @@ static int pv_time_cpu_online(unsigned int cpu)static int pv_time_cpu_down_prepare (unsigned int cpu) { -return sbi_sta_steal_time_set_shmem (SBI_STA_SHMEM_DISABLE , -SBI_STA_SHMEM_DISABLE ,0 ); +return sbi_sta_steal_time_set_shmem (SBI_SHMEM_DISABLE , +SBI_SHMEM_DISABLE ,0 ); }static u64 pv_time_steal_clock (int cpu)
arch/riscv/kvm/vcpu_sbi_sta.c +2 -2
@@ -93,8 +93,8 @@ static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)if (flags !=0 )return SBI_ERR_INVALID_PARAM ; -if (shmem_phys_lo ==SBI_STA_SHMEM_DISABLE && - shmem_phys_hi ==SBI_STA_SHMEM_DISABLE ) { +if (shmem_phys_lo ==SBI_SHMEM_DISABLE && + shmem_phys_hi ==SBI_SHMEM_DISABLE ) { vcpu->arch.sta.shmem =INVALID_GPA ;return 0 ; }
Signature · 1 line
2.34.1
Re: [PATCH v5 09/22] RISC-V: KVM: Rename the SBI_STA_SHMEM_DISABLE to a generic name
Quoted 66 lines · Atish Patra
SBI_STA_SHMEM_DISABLEis a macro to invoke disable shared memory commands. As this can be invoked from other SBI extension context as well, rename it to more generic name asSBI_SHMEM_DISABLE.Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/sbi.h | 2 +- arch/riscv/kernel/paravirt.c | 6 +++--- arch/riscv/kvm/vcpu_sbi_sta.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)arch/riscv/include/asm/sbi.h +1
-1@@ -277,7 +277,7 @@ struct sbi_sta_struct {u8 pad[47 ]; } __packed; -#define SBI_STA_SHMEM_DISABLE -1 +#define SBI_SHMEM_DISABLE -1/* SBI spec version fields */ #define SBI_SPEC_VERSION_DEFAULT 0x1arch/riscv/kernel/paravirt.c +3
-3@@ -62,7 +62,7 @@ static int sbi_sta_steal_time_set_shmem(unsigned long lo, unsigned long hi, ret =sbi_ecall (SBI_EXT_STA ,SBI_EXT_STA_STEAL_TIME_SET_SHMEM , lo, hi, flags,0 ,0 ,0 );if (ret.error) { -if (lo ==SBI_STA_SHMEM_DISABLE && hi ==SBI_STA_SHMEM_DISABLE ) +if (lo ==SBI_SHMEM_DISABLE && hi ==SBI_SHMEM_DISABLE )pr_warn ("Failed to disable steal-time shmem" );else pr_warn ("Failed to set steal-time shmem" );@@ -84,8 +84,8 @@ static int pv_time_cpu_online(unsigned int cpu)static int pv_time_cpu_down_prepare (unsigned int cpu) { -return sbi_sta_steal_time_set_shmem (SBI_STA_SHMEM_DISABLE , -SBI_STA_SHMEM_DISABLE ,0 ); +return sbi_sta_steal_time_set_shmem (SBI_SHMEM_DISABLE , +SBI_SHMEM_DISABLE ,0 ); }static u64 pv_time_steal_clock (int cpu)arch/riscv/kvm/vcpu_sbi_sta.c +2
-2@@ -93,8 +93,8 @@ static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)if (flags !=0 )return SBI_ERR_INVALID_PARAM ; -if (shmem_phys_lo ==SBI_STA_SHMEM_DISABLE && - shmem_phys_hi ==SBI_STA_SHMEM_DISABLE ) { +if (shmem_phys_lo ==SBI_SHMEM_DISABLE && + shmem_phys_hi ==SBI_SHMEM_DISABLE ) { vcpu->arch.sta.shmem =INVALID_GPA ;return 0 ; }Signature · 1 line
2.34.1
PATCH v5 10/22 RISC-V: KVM: No need to update the counter value during reset
In reply to Atish PatraThe virtual counter value is updated during pmu_ctr_read. There is no need to update it in reset case. Otherwise, it will be counted twice which is incorrect.
--- arch/riscv/kvm/vcpu_pmu.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
arch/riscv/kvm/vcpu_pmu.c +2 -6
@@ -397,7 +397,6 @@ int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, {struct kvm_pmu *kvpmu =vcpu_to_pmu (vcpu);int i, pmc_index, sbiret =0 ; -u64 enabled, running;struct kvm_pmc *pmc;int fevent_code;
@@ -432,12 +431,9 @@ int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base, sbiret =SBI_ERR_ALREADY_STOPPED ; } -if (flags &SBI_PMU_STOP_FLAG_RESET ) { -/* Relase the counter if this is a reset request */ - pmc->counter_val +=perf_event_read_value (pmc->perf_event, - &enabled, &running); +if (flags &SBI_PMU_STOP_FLAG_RESET ) +/* Release the counter if this is a reset request */ kvm_pmu_release_perf_event (pmc); - } }else { sbiret =SBI_ERR_INVALID_PARAM ; }
Signature · 1 line
2.34.1
PATCH v5 11/22 RISC-V: KVM: No need to exit to the user space if perf event failed
In reply to Atish PatraCurrently, we return a linux error code if creating a perf event failed in kvm. That shouldn't be necessary as guest can continue to operate without perf profiling or profiling with firmware counters.
Return appropriate SBI error code to indicate that PMU configuration failed. An error message in kvm already describes the reason for failure.
--- arch/riscv/kvm/vcpu_pmu.c | 14 +++++++++----- arch/riscv/kvm/vcpu_sbi_pmu.c | 6 +++--- 2 files changed, 12 insertions(+), 8 deletions(-)
arch/riscv/kvm/vcpu_pmu.c +9 -5
@@ -229,8 +229,9 @@ static int kvm_pmu_validate_counter_mask(struct kvm_pmu *kvpmu, unsigned long ctreturn 0 ; } -static int kvm_pmu_create_perf_event (struct kvm_pmc *pmc,struct perf_event_attr *attr, -unsigned long flags,unsigned long eidx,unsigned long evtdata) +static long kvm_pmu_create_perf_event (struct kvm_pmc *pmc,struct perf_event_attr *attr, +unsigned long flags,unsigned long eidx, +unsigned long evtdata) {struct perf_event *event;
@@ -454,7 +455,8 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba unsigned long eidx, u64 evtdata, struct kvm_vcpu_sbi_return *retdata) { - int ctr_idx, ret, sbiret = 0; + int ctr_idx, sbiret = 0; +long ret;bool is_fevent;unsigned long event_code;u32 etype =kvm_pmu_get_perf_event_type (eidx);
@@ -513,8 +515,10 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba kvpmu->fw_event[event_code].started = true; }else { ret =kvm_pmu_create_perf_event (pmc, &attr, flags, eidx, evtdata); -if (ret) -return ret; +if (ret) { + sbiret =SBI_ERR_NOT_SUPPORTED ; + goto out; + } }set_bit (ctr_idx, kvpmu->pmc_in_use);
arch/riscv/kvm/vcpu_sbi_pmu.c +3 -3
@@ -42,9 +42,9 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,#endif /* * This can fail if perf core framework fails to create an event. -* Forward the error to userspace because it's an error which -* happened within the host kernel. The other option would be -* to convert to an SBI error and forward to the guest. +* No need to forward the error to userspace and exit the guest. +* The operation can continue without profiling. Forward the +* appropriate SBI error to the guest. */ ret =kvm_riscv_vcpu_pmu_ctr_cfg_match (vcpu, cp->a0, cp->a1, cp->a2, cp->a3, temp, retdata);
Signature · 1 line
2.34.1
Re: [PATCH v5 11/22] RISC-V: KVM: No need to exit to the user space if perf event failed
Quoted 42 lines · Atish Patra
Currently, we return a linux error code if creating a perf event failed in kvm. That shouldn't be necessary as guest can continue to operate without perf profiling or profiling with firmware counters.
Return appropriate SBI error code to indicate that PMU configuration failed. An error message in kvm already describes the reason for failure.
Fixes 0cb74b65d2e5 ("RISC-V: KVM: Implement perf support without sampling")Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- arch/riscv/kvm/vcpu_pmu.c | 14 +++++++++----- arch/riscv/kvm/vcpu_sbi_pmu.c | 6 +++--- 2 files changed, 12 insertions(+), 8 deletions(-)arch/riscv/kvm/vcpu_pmu.c +5
-3@@ -229,8 +229,9 @@ static int kvm_pmu_validate_counter_mask(struct kvm_pmu *kvpmu, unsigned long ctreturn 0 ; } -static int kvm_pmu_create_perf_event (struct kvm_pmc *pmc,struct perf_event_attr *attr, -unsigned long flags,unsigned long eidx,unsigned long evtdata) +static long kvm_pmu_create_perf_event (struct kvm_pmc *pmc,struct perf_event_attr *attr, +unsigned long flags,unsigned long eidx, +unsigned long evtdata) {struct perf_event *event;@@ -454,7 +455,8 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba unsigned long eidx, u64 evtdata, struct kvm_vcpu_sbi_return *retdata) { - int ctr_idx, ret, sbiret = 0; + int ctr_idx, sbiret = 0; +long ret;bool is_fevent;unsigned long event_code;u32 etype =kvm_pmu_get_perf_event_type (eidx);
arch/riscv/kvm/vcpu_pmu.c int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba · line 519
Quoted 7 lines
arch/riscv/kvm/vcpu_pmu.c +2
-2@@ -513,8 +515,10 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba kvpmu->fw_event[event_code].started = true; }else { ret =kvm_pmu_create_perf_event (pmc, &attr, flags, eidx, evtdata); -if (ret) -return ret; +if (ret) { + sbiret =SBI_ERR_NOT_SUPPORTED ;
I'm still not sure about this. I replied in the v4 thread about it.
Thanks,
drew
Quoted 25 lines
arch/riscv/kvm/vcpu_pmu.c +2
-0+ goto out; + } }set_bit (ctr_idx, kvpmu->pmc_in_use);arch/riscv/kvm/vcpu_sbi_pmu.c +3
-3@@ -42,9 +42,9 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,#endif /* * This can fail if perf core framework fails to create an event. -* Forward the error to userspace because it's an error which -* happened within the host kernel. The other option would be -* to convert to an SBI error and forward to the guest. +* No need to forward the error to userspace and exit the guest. +* The operation can continue without profiling. Forward the +* appropriate SBI error to the guest. */ ret =kvm_riscv_vcpu_pmu_ctr_cfg_match (vcpu, cp->a0, cp->a1, cp->a2, cp->a3, temp, retdata);Signature · 1 line
2.34.1
PATCH v5 12/22 RISC-V: KVM: Implement SBI PMU Snapshot feature
In reply to Atish PatraPMU Snapshot function allows to minimize the number of traps when the guest access configures/access the hpmcounters. If the snapshot feature is enabled, the hypervisor updates the shared memory with counter data and state of overflown counters. The guest can just read the shared memory instead of trap & emulate done by the hypervisor.
This patch doesn't implement the counter overflow yet.
--- arch/riscv/include/asm/kvm_vcpu_pmu.h | 7 ++ arch/riscv/kvm/vcpu_pmu.c | 121 +++++++++++++++++++++++++- arch/riscv/kvm/vcpu_sbi_pmu.c | 3 + 3 files changed, 130 insertions(+), 1 deletion(-)
arch/riscv/include/asm/kvm_vcpu_pmu.h +7 -0
@@ -50,6 +50,10 @@ struct kvm_pmu {bool init_done;/* Bit map of all the virtual counter used */ DECLARE_BITMAP (pmc_in_use,RISCV_KVM_MAX_COUNTERS ); +/* The address of the counter snapshot area (guest physical address) */ +gpa_t snapshot_addr; +/* The actual data of the snapshot */ +struct riscv_pmu_snapshot_data *sdata; };#define vcpu_to_pmu (vcpu) (&(vcpu)->arch.pmu_context)
@@ -85,6 +89,9 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_baint kvm_riscv_vcpu_pmu_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx,struct kvm_vcpu_sbi_return *retdata);void kvm_riscv_vcpu_pmu_init (struct kvm_vcpu *vcpu); +int kvm_riscv_vcpu_pmu_snapshot_set_shmem (struct kvm_vcpu *vcpu,unsigned long saddr_low, +unsigned long saddr_high,unsigned long flags, +struct kvm_vcpu_sbi_return *retdata);void kvm_riscv_vcpu_pmu_deinit (struct kvm_vcpu *vcpu);void kvm_riscv_vcpu_pmu_reset (struct kvm_vcpu *vcpu);
arch/riscv/kvm/vcpu_pmu.c +120 -1
190 lines not on this page · raw message
arch/riscv/kvm/vcpu_sbi_pmu.c +3 -0
@@ -64,6 +64,9 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,case SBI_EXT_PMU_COUNTER_FW_READ : ret =kvm_riscv_vcpu_pmu_ctr_read (vcpu, cp->a0, retdata);break ; +case SBI_EXT_PMU_SNAPSHOT_SET_SHMEM : + ret =kvm_riscv_vcpu_pmu_snapshot_set_shmem (vcpu, cp->a0, cp->a1, cp->a2, retdata); +break ;default : retdata->err_val =SBI_ERR_NOT_SUPPORTED ; }
Signature · 1 line
2.34.1
Re: [PATCH v5 12/22] RISC-V: KVM: Implement SBI PMU Snapshot feature
Quoted 92 lines · Atish Patra
PMU Snapshot function allows to minimize the number of traps when the guest access configures/access the hpmcounters. If the snapshot feature is enabled, the hypervisor updates the shared memory with counter data and state of overflown counters. The guest can just read the shared memory instead of trap & emulate done by the hypervisor.
This patch doesn't implement the counter overflow yet.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/kvm_vcpu_pmu.h | 7 ++ arch/riscv/kvm/vcpu_pmu.c | 121 +++++++++++++++++++++++++- arch/riscv/kvm/vcpu_sbi_pmu.c | 3 + 3 files changed, 130 insertions(+), 1 deletion(-)arch/riscv/include/asm/kvm_vcpu_pmu.h +7
-0@@ -50,6 +50,10 @@ struct kvm_pmu {bool init_done;/* Bit map of all the virtual counter used */ DECLARE_BITMAP (pmc_in_use,RISCV_KVM_MAX_COUNTERS ); +/* The address of the counter snapshot area (guest physical address) */ +gpa_t snapshot_addr; +/* The actual data of the snapshot */ +struct riscv_pmu_snapshot_data *sdata; };#define vcpu_to_pmu (vcpu) (&(vcpu)->arch.pmu_context)@@ -85,6 +89,9 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_baint kvm_riscv_vcpu_pmu_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx,struct kvm_vcpu_sbi_return *retdata);void kvm_riscv_vcpu_pmu_init (struct kvm_vcpu *vcpu); +int kvm_riscv_vcpu_pmu_snapshot_set_shmem (struct kvm_vcpu *vcpu,unsigned long saddr_low, +unsigned long saddr_high,unsigned long flags, +struct kvm_vcpu_sbi_return *retdata);void kvm_riscv_vcpu_pmu_deinit (struct kvm_vcpu *vcpu);void kvm_riscv_vcpu_pmu_reset (struct kvm_vcpu *vcpu);arch/riscv/kvm/vcpu_pmu.c +36
-0@@ -14,6 +14,7 @@#include <asm/csr.h> #include <asm/kvm_vcpu_sbi.h> #include <asm/kvm_vcpu_pmu.h> +#include <asm/sbi.h> #include <linux/bitops.h> #define kvm_pmu_num_counters (pmu) ((pmu)->num_hw_ctrs + (pmu)->num_fw_ctrs)@@ -311,6 +312,80 @@ int kvm_riscv_vcpu_pmu_read_hpm(struct kvm_vcpu *vcpu, unsigned int csr_num,return ret; } +static void kvm_pmu_clear_snapshot_area (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *kvpmu =vcpu_to_pmu (vcpu); +int snapshot_area_size =sizeof (struct riscv_pmu_snapshot_data ); + +if (kvpmu->sdata) { +if (kvpmu->snapshot_addr !=INVALID_GPA ) { +memset (kvpmu->sdata,0 , snapshot_area_size); +kvm_vcpu_write_guest (vcpu, kvpmu->snapshot_addr, + kvpmu->sdata, snapshot_area_size); + }else { +pr_warn ("snapshot address invalid\n" ); + } +kfree (kvpmu->sdata); + kvpmu->sdata =NULL ; + } + kvpmu->snapshot_addr =INVALID_GPA ; +} + +int kvm_riscv_vcpu_pmu_snapshot_set_shmem (struct kvm_vcpu *vcpu,unsigned long saddr_low, +unsigned long saddr_high,unsigned long flags, +struct kvm_vcpu_sbi_return *retdata) +{ +struct kvm_pmu *kvpmu =vcpu_to_pmu (vcpu); +int snapshot_area_size =sizeof (struct riscv_pmu_snapshot_data ); +int sbiret =0 ; +gpa_t saddr; +unsigned long hva; +bool writable; + +if (!kvpmu || flags) { + sbiret =SBI_ERR_INVALID_PARAM ; + goto out; + } +
arch/riscv/kvm/vcpu_pmu.c int kvm_riscv_vcpu_pmu_read_hpm(struct kvm_vcpu *vcpu, unsigned int csr_num, · line 359
Quoted 10 lines
arch/riscv/kvm/vcpu_pmu.c +10
-0+if (saddr_low ==SBI_SHMEM_DISABLE && saddr_high ==SBI_SHMEM_DISABLE ) { +kvm_pmu_clear_snapshot_area (vcpu); +return 0 ; + } + + saddr = saddr_low; + +if (saddr_high !=0 ) { +if (IS_ENABLED (CONFIG_32BIT )) + saddr |= ((gpa_t )saddr <<32 );
saddr |= ((gpa_t)saddr_high << 32)
arch/riscv/kvm/vcpu_pmu.c int kvm_riscv_vcpu_pmu_read_hpm(struct kvm_vcpu *vcpu, unsigned int csr_num, · line 364
Quoted 5 lines
arch/riscv/kvm/vcpu_pmu.c +5
-0+else + sbiret =SBI_ERR_INVALID_ADDRESS ; + goto out; + } +
Thanks,
drew
Re: [PATCH v5 12/22] RISC-V: KVM: Implement SBI PMU Snapshot feature
Quoted 97 lines · Andrew Jones
Quoted 96 lines · Atish Patra
PMU Snapshot function allows to minimize the number of traps when the guest access configures/access the hpmcounters. If the snapshot feature is enabled, the hypervisor updates the shared memory with counter data and state of overflown counters. The guest can just read the shared memory instead of trap & emulate done by the hypervisor.
This patch doesn't implement the counter overflow yet.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/kvm_vcpu_pmu.h | 7 ++ arch/riscv/kvm/vcpu_pmu.c | 121 +++++++++++++++++++++++++- arch/riscv/kvm/vcpu_sbi_pmu.c | 3 + 3 files changed, 130 insertions(+), 1 deletion(-)arch/riscv/include/asm/kvm_vcpu_pmu.h +7
-0@@ -50,6 +50,10 @@ struct kvm_pmu {bool init_done;/* Bit map of all the virtual counter used */ DECLARE_BITMAP (pmc_in_use,RISCV_KVM_MAX_COUNTERS ); +/* The address of the counter snapshot area (guest physical address) */ +gpa_t snapshot_addr; +/* The actual data of the snapshot */ +struct riscv_pmu_snapshot_data *sdata; };#define vcpu_to_pmu (vcpu) (&(vcpu)->arch.pmu_context)@@ -85,6 +89,9 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_baint kvm_riscv_vcpu_pmu_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx,struct kvm_vcpu_sbi_return *retdata);void kvm_riscv_vcpu_pmu_init (struct kvm_vcpu *vcpu); +int kvm_riscv_vcpu_pmu_snapshot_set_shmem (struct kvm_vcpu *vcpu,unsigned long saddr_low, +unsigned long saddr_high,unsigned long flags, +struct kvm_vcpu_sbi_return *retdata);void kvm_riscv_vcpu_pmu_deinit (struct kvm_vcpu *vcpu);void kvm_riscv_vcpu_pmu_reset (struct kvm_vcpu *vcpu);arch/riscv/kvm/vcpu_pmu.c +40
-0@@ -14,6 +14,7 @@#include <asm/csr.h> #include <asm/kvm_vcpu_sbi.h> #include <asm/kvm_vcpu_pmu.h> +#include <asm/sbi.h> #include <linux/bitops.h> #define kvm_pmu_num_counters (pmu) ((pmu)->num_hw_ctrs + (pmu)->num_fw_ctrs)@@ -311,6 +312,80 @@ int kvm_riscv_vcpu_pmu_read_hpm(struct kvm_vcpu *vcpu, unsigned int csr_num,return ret; } +static void kvm_pmu_clear_snapshot_area (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *kvpmu =vcpu_to_pmu (vcpu); +int snapshot_area_size =sizeof (struct riscv_pmu_snapshot_data ); + +if (kvpmu->sdata) { +if (kvpmu->snapshot_addr !=INVALID_GPA ) { +memset (kvpmu->sdata,0 , snapshot_area_size); +kvm_vcpu_write_guest (vcpu, kvpmu->snapshot_addr, + kvpmu->sdata, snapshot_area_size); + }else { +pr_warn ("snapshot address invalid\n" ); + } +kfree (kvpmu->sdata); + kvpmu->sdata =NULL ; + } + kvpmu->snapshot_addr =INVALID_GPA ; +} + +int kvm_riscv_vcpu_pmu_snapshot_set_shmem (struct kvm_vcpu *vcpu,unsigned long saddr_low, +unsigned long saddr_high,unsigned long flags, +struct kvm_vcpu_sbi_return *retdata) +{ +struct kvm_pmu *kvpmu =vcpu_to_pmu (vcpu); +int snapshot_area_size =sizeof (struct riscv_pmu_snapshot_data ); +int sbiret =0 ; +gpa_t saddr; +unsigned long hva; +bool writable; + +if (!kvpmu || flags) { + sbiret =SBI_ERR_INVALID_PARAM ; + goto out; + } + +if (saddr_low ==SBI_SHMEM_DISABLE && saddr_high ==SBI_SHMEM_DISABLE ) { +kvm_pmu_clear_snapshot_area (vcpu); +return 0 ; + }
Quoted 9 lines
arch/riscv/kvm/vcpu_pmu.c int kvm_riscv_vcpu_pmu_read_hpm(struct kvm_vcpu *vcpu, unsigned int csr_num, · line 359
Quoted 6 lines
arch/riscv/kvm/vcpu_pmu.c +6
-0+ + saddr = saddr_low; + +if (saddr_high !=0 ) { +if (IS_ENABLED (CONFIG_32BIT )) + saddr |= ((gpa_t )saddr <<32 );saddr |= ((gpa_t)saddr_high << 32)
Oops. Thanks for catching it. Fixed.
Quoted 8 lines
arch/riscv/kvm/vcpu_pmu.c int kvm_riscv_vcpu_pmu_read_hpm(struct kvm_vcpu *vcpu, unsigned int csr_num, · line 364
Quoted 5 lines
arch/riscv/kvm/vcpu_pmu.c +5
-0+else + sbiret =SBI_ERR_INVALID_ADDRESS ; + goto out; + } +Thanks,
drew
PATCH v5 13/22 RISC-V: KVM: Add perf sampling support for guests
In reply to Atish PatraKVM enables perf for guest via counter virtualization. However, the sampling can not be supported as there is no mechanism to enabled trap/emulate scountovf in ISA yet. Rely on the SBI PMU snapshot to provide the counter overflow data via the shared memory.
In case of sampling event, the host first sets the guest's LCOFI interrupt and injects to the guest via irq filtering mechanism defined in AIA specification. Thus, ssaia must be enabled in the host in order to use perf sampling in the guest. No other AIA dependency w.r.t kernel is required.
--- arch/riscv/include/asm/csr.h | 3 +- arch/riscv/include/asm/kvm_vcpu_pmu.h | 3 ++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/aia.c | 5 ++ arch/riscv/kvm/vcpu.c | 15 ++++-- arch/riscv/kvm/vcpu_onereg.c | 5 ++ arch/riscv/kvm/vcpu_pmu.c | 68 +++++++++++++++++++++++++-- 7 files changed, 92 insertions(+), 8 deletions(-)
arch/riscv/include/asm/csr.h +2 -1
@@ -168,7 +168,8 @@#define VSIP_TO_HVIP_SHIFT (IRQ_VS_SOFT - IRQ_S_SOFT)#define VSIP_VALID_MASK ((_AC(1, UL) << IRQ_S_SOFT) | \ (_AC(1, UL) << IRQ_S_TIMER) | \ - (_AC(1, UL) << IRQ_S_EXT)) + (_AC(1, UL) << IRQ_S_EXT) | \ + (_AC(1, UL) << IRQ_PMU_OVF))/* AIA CSR bits */ #define TOPI_IID_SHIFT 16
arch/riscv/include/asm/kvm_vcpu_pmu.h +3 -0
@@ -36,6 +36,7 @@ struct kvm_pmc {bool started;/* Monitoring event ID */ unsigned long event_idx; +struct kvm_vcpu *vcpu; };/* PMU data structure per vcpu */
@@ -50,6 +51,8 @@ struct kvm_pmu {bool init_done;/* Bit map of all the virtual counter used */ DECLARE_BITMAP (pmc_in_use,RISCV_KVM_MAX_COUNTERS ); +/* Bit map of all the virtual counter overflown */ +DECLARE_BITMAP (pmc_overflown,RISCV_KVM_MAX_COUNTERS );/* The address of the counter snapshot area (guest physical address) */ gpa_t snapshot_addr;/* The actual data of the snapshot */
arch/riscv/include/uapi/asm/kvm.h +1 -0
@@ -167,6 +167,7 @@ enum KVM_RISCV_ISA_EXT_ID {KVM_RISCV_ISA_EXT_ZFA ,KVM_RISCV_ISA_EXT_ZTSO ,KVM_RISCV_ISA_EXT_ZACAS , +KVM_RISCV_ISA_EXT_SSCOFPMF , KVM_RISCV_ISA_EXT_MAX, };
arch/riscv/kvm/aia.c +5 -0
@@ -545,6 +545,9 @@ void kvm_riscv_aia_enable(void)enable_percpu_irq (hgei_parent_irq,irq_get_trigger_type (hgei_parent_irq));csr_set (CSR_HIE ,BIT (IRQ_S_GEXT )); +/* Enable IRQ filtering for overflow interrupt only if sscofpmf is present */ +if (__riscv_isa_extension_available (NULL ,RISCV_ISA_EXT_SSCOFPMF )) +csr_write (CSR_HVIEN ,BIT (IRQ_PMU_OVF )); }void kvm_riscv_aia_disable (void )
@@ -558,6 +561,8 @@ void kvm_riscv_aia_disable(void)return ; hgctrl =get_cpu_ptr (&aia_hgei); +if (__riscv_isa_extension_available (NULL ,RISCV_ISA_EXT_SSCOFPMF )) +csr_clear (CSR_HVIEN ,BIT (IRQ_PMU_OVF ));/* Disable per-CPU SGEI interrupt */ csr_clear (CSR_HIE ,BIT (IRQ_S_GEXT ));disable_percpu_irq (hgei_parent_irq);
arch/riscv/kvm/vcpu.c +12 -3
@@ -365,6 +365,13 @@ void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu) } } +/* Sync up the HVIP.LCOFIP bit changes (only clear) by the guest */ +if ((csr->hvip ^ hvip) & (1UL <<IRQ_PMU_OVF )) { +if (!(hvip & (1UL <<IRQ_PMU_OVF )) && + !test_and_set_bit (IRQ_PMU_OVF , v->irqs_pending_mask)) +clear_bit (IRQ_PMU_OVF , v->irqs_pending); + } +/* Sync-up AIA high interrupts */ kvm_riscv_vcpu_aia_sync_interrupts (vcpu);
@@ -382,7 +389,8 @@ int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq)if (irq <IRQ_LOCAL_MAX && irq !=IRQ_VS_SOFT && irq !=IRQ_VS_TIMER && - irq !=IRQ_VS_EXT ) + irq !=IRQ_VS_EXT && + irq !=IRQ_PMU_OVF )return -EINVAL ;set_bit (irq, vcpu->arch.irqs_pending);
@@ -397,14 +405,15 @@ int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq)int kvm_riscv_vcpu_unset_interrupt (struct kvm_vcpu *vcpu,unsigned int irq) {/* -* We only allow VS-mode software, timer, and external +* We only allow VS-mode software, timer, counter overflow and external * interrupts when irq is one of the local interrupts * defined by RISC-V privilege specification. */ if (irq <IRQ_LOCAL_MAX && irq !=IRQ_VS_SOFT && irq !=IRQ_VS_TIMER && - irq !=IRQ_VS_EXT ) + irq !=IRQ_VS_EXT && + irq !=IRQ_PMU_OVF )return -EINVAL ;clear_bit (irq, vcpu->arch.irqs_pending);
arch/riscv/kvm/vcpu_onereg.c +5 -0
@@ -36,6 +36,7 @@ static const unsigned long kvm_isa_ext_arr[] = {/* Multi letter extensions (alphabetically sorted) */ KVM_ISA_EXT_ARR (SMSTATEEN ),KVM_ISA_EXT_ARR (SSAIA ), +KVM_ISA_EXT_ARR (SSCOFPMF ),KVM_ISA_EXT_ARR (SSTC ),KVM_ISA_EXT_ARR (SVINVAL ), KVM_ISA_EXT_ARR(SVNAPOT),
@@ -101,6 +102,9 @@ static bool kvm_riscv_vcpu_isa_enable_allowed(unsigned long ext)return false;case KVM_RISCV_ISA_EXT_V :return riscv_v_vstate_ctrl_user_allowed (); +case KVM_RISCV_ISA_EXT_SSCOFPMF : +/* Sscofpmf depends on interrupt filtering defined in ssaia */ +return __riscv_isa_extension_available (NULL ,RISCV_ISA_EXT_SSAIA );default :break ; }
@@ -116,6 +120,7 @@ static bool kvm_riscv_vcpu_isa_disable_allowed(unsigned long ext)case KVM_RISCV_ISA_EXT_C :case KVM_RISCV_ISA_EXT_I :case KVM_RISCV_ISA_EXT_M : +case KVM_RISCV_ISA_EXT_SSCOFPMF :case KVM_RISCV_ISA_EXT_SSTC :case KVM_RISCV_ISA_EXT_SVINVAL :case KVM_RISCV_ISA_EXT_SVNAPOT :
arch/riscv/kvm/vcpu_pmu.c +64 -4
115 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v5 13/22] RISC-V: KVM: Add perf sampling support for guests
Quoted 152 lines · Atish Patra
KVM enables perf for guest via counter virtualization. However, the sampling can not be supported as there is no mechanism to enabled trap/emulate scountovf in ISA yet. Rely on the SBI PMU snapshot to provide the counter overflow data via the shared memory.
In case of sampling event, the host first sets the guest's LCOFI interrupt and injects to the guest via irq filtering mechanism defined in AIA specification. Thus, ssaia must be enabled in the host in order to use perf sampling in the guest. No other AIA dependency w.r.t kernel is required.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/csr.h | 3 +- arch/riscv/include/asm/kvm_vcpu_pmu.h | 3 ++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/aia.c | 5 ++ arch/riscv/kvm/vcpu.c | 15 ++++-- arch/riscv/kvm/vcpu_onereg.c | 5 ++ arch/riscv/kvm/vcpu_pmu.c | 68 +++++++++++++++++++++++++-- 7 files changed, 92 insertions(+), 8 deletions(-)arch/riscv/include/asm/csr.h +2
-1@@ -168,7 +168,8 @@#define VSIP_TO_HVIP_SHIFT (IRQ_VS_SOFT - IRQ_S_SOFT)#define VSIP_VALID_MASK ((_AC(1, UL) << IRQ_S_SOFT) | \ (_AC(1, UL) << IRQ_S_TIMER) | \ - (_AC(1, UL) << IRQ_S_EXT)) + (_AC(1, UL) << IRQ_S_EXT) | \ + (_AC(1, UL) << IRQ_PMU_OVF))/* AIA CSR bits */ #define TOPI_IID_SHIFT 16arch/riscv/include/asm/kvm_vcpu_pmu.h +3
-0@@ -36,6 +36,7 @@ struct kvm_pmc {bool started;/* Monitoring event ID */ unsigned long event_idx; +struct kvm_vcpu *vcpu; };/* PMU data structure per vcpu */ @@ -50,6 +51,8 @@ struct kvm_pmu {bool init_done;/* Bit map of all the virtual counter used */ DECLARE_BITMAP (pmc_in_use,RISCV_KVM_MAX_COUNTERS ); +/* Bit map of all the virtual counter overflown */ +DECLARE_BITMAP (pmc_overflown,RISCV_KVM_MAX_COUNTERS );/* The address of the counter snapshot area (guest physical address) */ gpa_t snapshot_addr;/* The actual data of the snapshot */ arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -167,6 +167,7 @@ enum KVM_RISCV_ISA_EXT_ID {KVM_RISCV_ISA_EXT_ZFA ,KVM_RISCV_ISA_EXT_ZTSO ,KVM_RISCV_ISA_EXT_ZACAS , +KVM_RISCV_ISA_EXT_SSCOFPMF , KVM_RISCV_ISA_EXT_MAX, };arch/riscv/kvm/aia.c +5
-0@@ -545,6 +545,9 @@ void kvm_riscv_aia_enable(void)enable_percpu_irq (hgei_parent_irq,irq_get_trigger_type (hgei_parent_irq));csr_set (CSR_HIE ,BIT (IRQ_S_GEXT )); +/* Enable IRQ filtering for overflow interrupt only if sscofpmf is present */ +if (__riscv_isa_extension_available (NULL ,RISCV_ISA_EXT_SSCOFPMF )) +csr_write (CSR_HVIEN ,BIT (IRQ_PMU_OVF )); }void kvm_riscv_aia_disable (void )@@ -558,6 +561,8 @@ void kvm_riscv_aia_disable(void)return ; hgctrl =get_cpu_ptr (&aia_hgei); +if (__riscv_isa_extension_available (NULL ,RISCV_ISA_EXT_SSCOFPMF )) +csr_clear (CSR_HVIEN ,BIT (IRQ_PMU_OVF ));/* Disable per-CPU SGEI interrupt */ csr_clear (CSR_HIE ,BIT (IRQ_S_GEXT ));disable_percpu_irq (hgei_parent_irq);arch/riscv/kvm/vcpu.c +12
-3@@ -365,6 +365,13 @@ void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu) } } +/* Sync up the HVIP.LCOFIP bit changes (only clear) by the guest */ +if ((csr->hvip ^ hvip) & (1UL <<IRQ_PMU_OVF )) { +if (!(hvip & (1UL <<IRQ_PMU_OVF )) && + !test_and_set_bit (IRQ_PMU_OVF , v->irqs_pending_mask)) +clear_bit (IRQ_PMU_OVF , v->irqs_pending); + } +/* Sync-up AIA high interrupts */ kvm_riscv_vcpu_aia_sync_interrupts (vcpu);@@ -382,7 +389,8 @@ int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq)if (irq <IRQ_LOCAL_MAX && irq !=IRQ_VS_SOFT && irq !=IRQ_VS_TIMER && - irq !=IRQ_VS_EXT ) + irq !=IRQ_VS_EXT && + irq !=IRQ_PMU_OVF )return -EINVAL ;set_bit (irq, vcpu->arch.irqs_pending);@@ -397,14 +405,15 @@ int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq)int kvm_riscv_vcpu_unset_interrupt (struct kvm_vcpu *vcpu,unsigned int irq) {/* -* We only allow VS-mode software, timer, and external +* We only allow VS-mode software, timer, counter overflow and external * interrupts when irq is one of the local interrupts * defined by RISC-V privilege specification. */ if (irq <IRQ_LOCAL_MAX && irq !=IRQ_VS_SOFT && irq !=IRQ_VS_TIMER && - irq !=IRQ_VS_EXT ) + irq !=IRQ_VS_EXT && + irq !=IRQ_PMU_OVF )return -EINVAL ;clear_bit (irq, vcpu->arch.irqs_pending);arch/riscv/kvm/vcpu_onereg.c +1
-0@@ -36,6 +36,7 @@ static const unsigned long kvm_isa_ext_arr[] = {/* Multi letter extensions (alphabetically sorted) */ KVM_ISA_EXT_ARR (SMSTATEEN ),KVM_ISA_EXT_ARR (SSAIA ), +KVM_ISA_EXT_ARR (SSCOFPMF ),KVM_ISA_EXT_ARR (SSTC ),KVM_ISA_EXT_ARR (SVINVAL ), KVM_ISA_EXT_ARR(SVNAPOT),
arch/riscv/kvm/vcpu_onereg.c static bool kvm_riscv_vcpu_isa_enable_allowed(unsigned long ext) · line 105
Quoted 4 lines
arch/riscv/kvm/vcpu_onereg.c +1
-0@@ -101,6 +102,9 @@ static bool kvm_riscv_vcpu_isa_enable_allowed(unsigned long ext)return false;case KVM_RISCV_ISA_EXT_V :return riscv_v_vstate_ctrl_user_allowed (); +case KVM_RISCV_ISA_EXT_SSCOFPMF :
nit: this case which starts with 'S' should come before the 'V' case since we tend to alphabetize these things.
arch/riscv/kvm/vcpu_onereg.c static bool kvm_riscv_vcpu_isa_disable_allowed(unsigned long ext) · line 123
Quoted 10 lines
arch/riscv/kvm/vcpu_onereg.c +3
-0+/* Sscofpmf depends on interrupt filtering defined in ssaia */ +return __riscv_isa_extension_available (NULL ,RISCV_ISA_EXT_SSAIA );default :break ; }@@ -116,6 +120,7 @@ static bool kvm_riscv_vcpu_isa_disable_allowed(unsigned long ext)case KVM_RISCV_ISA_EXT_C :case KVM_RISCV_ISA_EXT_I :case KVM_RISCV_ISA_EXT_M : +case KVM_RISCV_ISA_EXT_SSCOFPMF :
Since we can choose not to inject overflow interrupts for the guest, then the VMM could be allowed to disable this. Returning false from this function means that there's no way for KVM to turn off the behavior (or that KVM doesn't want to maintain code allowing the behavior to be turned off). Extensions that provides instructions which are unconditionally exposed to VS-mode can't be disabled, but anything KVM emulates, like this overflow can be. Is disabling Sscofpmf something that KVM would rather not maintain?
Thanks,
drew
PATCH v5 14/22 RISC-V: KVM: Support 64 bit firmware counters on RV32
In reply to Atish PatraThe SBI v2.0 introduced a fw_read_hi function to read 64 bit firmware counters for RV32 based systems.
Add infrastructure to support that.
--- arch/riscv/include/asm/kvm_vcpu_pmu.h | 4 ++- arch/riscv/kvm/vcpu_pmu.c | 44 ++++++++++++++++++++++++++- arch/riscv/kvm/vcpu_sbi_pmu.c | 6 ++++ 3 files changed, 52 insertions(+), 2 deletions(-)
arch/riscv/include/asm/kvm_vcpu_pmu.h +3 -1
@@ -20,7 +20,7 @@ static_assert(RISCV_KVM_MAX_COUNTERS <= 64);struct kvm_fw_event {/* Current value of the event */ -unsigned long value; +u64 value;/* Event monitoring status */ bool started;
@@ -91,6 +91,8 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba struct kvm_vcpu_sbi_return *retdata);int kvm_riscv_vcpu_pmu_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx,struct kvm_vcpu_sbi_return *retdata); +int kvm_riscv_vcpu_pmu_fw_ctr_read_hi (struct kvm_vcpu *vcpu,unsigned long cidx, +struct kvm_vcpu_sbi_return *retdata);void kvm_riscv_vcpu_pmu_init (struct kvm_vcpu *vcpu); int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long saddr_low, unsigned long saddr_high, unsigned long flags,
arch/riscv/kvm/vcpu_pmu.c +43 -1
62 lines not on this page · raw message
arch/riscv/kvm/vcpu_sbi_pmu.c +6 -0
@@ -64,6 +64,12 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,case SBI_EXT_PMU_COUNTER_FW_READ : ret =kvm_riscv_vcpu_pmu_ctr_read (vcpu, cp->a0, retdata);break ; +case SBI_EXT_PMU_COUNTER_FW_READ_HI : +if (IS_ENABLED (CONFIG_32BIT )) + ret =kvm_riscv_vcpu_pmu_fw_ctr_read_hi (vcpu, cp->a0, retdata); +else + retdata->out_val =0 ; +break ;case SBI_EXT_PMU_SNAPSHOT_SET_SHMEM : ret =kvm_riscv_vcpu_pmu_snapshot_set_shmem (vcpu, cp->a0, cp->a1, cp->a2, retdata);break ;
Signature · 1 line
2.34.1
Re: [PATCH v5 14/22] RISC-V: KVM: Support 64 bit firmware counters on RV32
Quoted 124 lines · Atish Patra
The SBI v2.0 introduced a
fw_read_hifunction to read 64 bit firmware counters for RV32 based systems.Add infrastructure to support that.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/kvm_vcpu_pmu.h | 4 ++- arch/riscv/kvm/vcpu_pmu.c | 44 ++++++++++++++++++++++++++- arch/riscv/kvm/vcpu_sbi_pmu.c | 6 ++++ 3 files changed, 52 insertions(+), 2 deletions(-)arch/riscv/include/asm/kvm_vcpu_pmu.h +3
-1@@ -20,7 +20,7 @@ static_assert(RISCV_KVM_MAX_COUNTERS <= 64);struct kvm_fw_event {/* Current value of the event */ -unsigned long value; +u64 value;/* Event monitoring status */ bool started;@@ -91,6 +91,8 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba struct kvm_vcpu_sbi_return *retdata);int kvm_riscv_vcpu_pmu_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx,struct kvm_vcpu_sbi_return *retdata); +int kvm_riscv_vcpu_pmu_fw_ctr_read_hi (struct kvm_vcpu *vcpu,unsigned long cidx, +struct kvm_vcpu_sbi_return *retdata);void kvm_riscv_vcpu_pmu_init (struct kvm_vcpu *vcpu); int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long saddr_low, unsigned long saddr_high, unsigned long flags,arch/riscv/kvm/vcpu_pmu.c +43
-1@@ -197,6 +197,36 @@ static int pmu_get_pmc_index(struct kvm_pmu *pmu, unsigned long eidx,return kvm_pmu_get_programmable_pmc_index (pmu, eidx, cbase, cmask); } +static int pmu_fw_ctr_read_hi (struct kvm_vcpu *vcpu,unsigned long cidx, +unsigned long *out_val) +{ +struct kvm_pmu *kvpmu =vcpu_to_pmu (vcpu); +struct kvm_pmc *pmc; +int fevent_code; + +if (!IS_ENABLED (CONFIG_32BIT )) { +pr_warn ("%s: should be invoked for only RV32\n" , __func__); +return -EINVAL ; + } + +if (cidx >=kvm_pmu_num_counters (kvpmu) || cidx ==1 ) { +pr_warn ("Invalid counter id [%ld]during read\n" , cidx); +return -EINVAL ; + } + + pmc = &kvpmu->pmc[cidx]; + +if (pmc->cinfo.type !=SBI_PMU_CTR_TYPE_FW ) +return -EINVAL ; + + fevent_code =get_event_code (pmc->event_idx); + pmc->counter_val = kvpmu->fw_event[fevent_code].value; + + *out_val = pmc->counter_val >>32 ; + +return 0 ; +} +static int pmu_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx,unsigned long *out_val) {@@ -705,6 +735,18 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_bareturn 0 ; } +int kvm_riscv_vcpu_pmu_fw_ctr_read_hi (struct kvm_vcpu *vcpu,unsigned long cidx, +struct kvm_vcpu_sbi_return *retdata) +{ +int ret; + + ret =pmu_fw_ctr_read_hi (vcpu, cidx, &retdata->out_val); +if (ret == -EINVAL ) + retdata->err_val =SBI_ERR_INVALID_PARAM ; + +return 0 ; +} +int kvm_riscv_vcpu_pmu_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx,struct kvm_vcpu_sbi_return *retdata) {@@ -778,7 +820,7 @@ void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) pmc->cinfo.csr =CSR_CYCLE + i; }else { pmc->cinfo.type =SBI_PMU_CTR_TYPE_FW ; - pmc->cinfo.width =BITS_PER_LONG -1 ; + pmc->cinfo.width =63 ; } }arch/riscv/kvm/vcpu_sbi_pmu.c +6
-0@@ -64,6 +64,12 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,case SBI_EXT_PMU_COUNTER_FW_READ : ret =kvm_riscv_vcpu_pmu_ctr_read (vcpu, cp->a0, retdata);break ; +case SBI_EXT_PMU_COUNTER_FW_READ_HI : +if (IS_ENABLED (CONFIG_32BIT )) + ret =kvm_riscv_vcpu_pmu_fw_ctr_read_hi (vcpu, cp->a0, retdata); +else + retdata->out_val =0 ; +break ;case SBI_EXT_PMU_SNAPSHOT_SET_SHMEM : ret =kvm_riscv_vcpu_pmu_snapshot_set_shmem (vcpu, cp->a0, cp->a1, cp->a2, retdata);break ;Signature · 1 line
2.34.1
PATCH v5 15/22 RISC-V: KVM: Improve firmware counter read function
In reply to Atish PatraRename the function to indicate that it is meant for firmware counter read. While at it, add a range sanity check for it as well.
--- arch/riscv/include/asm/kvm_vcpu_pmu.h | 2 +- arch/riscv/kvm/vcpu_pmu.c | 7 ++++++- arch/riscv/kvm/vcpu_sbi_pmu.c | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-)
arch/riscv/include/asm/kvm_vcpu_pmu.h +1 -1
@@ -89,7 +89,7 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba unsigned long ctr_mask, unsigned long flags, unsigned long eidx, u64 evtdata, struct kvm_vcpu_sbi_return *retdata); -int kvm_riscv_vcpu_pmu_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx, +int kvm_riscv_vcpu_pmu_fw_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx,struct kvm_vcpu_sbi_return *retdata);int kvm_riscv_vcpu_pmu_fw_ctr_read_hi (struct kvm_vcpu *vcpu,unsigned long cidx,struct kvm_vcpu_sbi_return *retdata);
arch/riscv/kvm/vcpu_pmu.c +6 -1
@@ -235,6 +235,11 @@ static int pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx,u64 enabled, running;int fevent_code; +if (cidx >=kvm_pmu_num_counters (kvpmu) || cidx ==1 ) { +pr_warn ("Invalid counter id [%ld] during read\n" , cidx); +return -EINVAL ; + } + pmc = &kvpmu->pmc[cidx];if (pmc->cinfo.type ==SBI_PMU_CTR_TYPE_FW ) {
@@ -747,7 +752,7 @@ int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx,return 0 ; } -int kvm_riscv_vcpu_pmu_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx, +int kvm_riscv_vcpu_pmu_fw_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx,struct kvm_vcpu_sbi_return *retdata) {int ret;
arch/riscv/kvm/vcpu_sbi_pmu.c +1 -1
@@ -62,7 +62,7 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, ret =kvm_riscv_vcpu_pmu_ctr_stop (vcpu, cp->a0, cp->a1, cp->a2, retdata);break ;case SBI_EXT_PMU_COUNTER_FW_READ : - ret =kvm_riscv_vcpu_pmu_ctr_read (vcpu, cp->a0, retdata); + ret =kvm_riscv_vcpu_pmu_fw_ctr_read (vcpu, cp->a0, retdata);break ;case SBI_EXT_PMU_COUNTER_FW_READ_HI :if (IS_ENABLED (CONFIG_32BIT ))
Signature · 1 line
2.34.1
Re: [PATCH v5 15/22] RISC-V: KVM: Improve firmware counter read function
Quoted 65 lines · Atish Patra
Rename the function to indicate that it is meant for firmware counter read. While at it, add a range sanity check for it as well.
Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/kvm_vcpu_pmu.h | 2 +- arch/riscv/kvm/vcpu_pmu.c | 7 ++++++- arch/riscv/kvm/vcpu_sbi_pmu.c | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-)arch/riscv/include/asm/kvm_vcpu_pmu.h +1
-1@@ -89,7 +89,7 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba unsigned long ctr_mask, unsigned long flags, unsigned long eidx, u64 evtdata, struct kvm_vcpu_sbi_return *retdata); -int kvm_riscv_vcpu_pmu_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx, +int kvm_riscv_vcpu_pmu_fw_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx,struct kvm_vcpu_sbi_return *retdata);int kvm_riscv_vcpu_pmu_fw_ctr_read_hi (struct kvm_vcpu *vcpu,unsigned long cidx,struct kvm_vcpu_sbi_return *retdata);arch/riscv/kvm/vcpu_pmu.c +6
-1@@ -235,6 +235,11 @@ static int pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx,u64 enabled, running;int fevent_code; +if (cidx >=kvm_pmu_num_counters (kvpmu) || cidx ==1 ) { +pr_warn ("Invalid counter id [%ld] during read\n" , cidx); +return -EINVAL ; + } + pmc = &kvpmu->pmc[cidx];if (pmc->cinfo.type ==SBI_PMU_CTR_TYPE_FW ) {@@ -747,7 +752,7 @@ int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx,return 0 ; } -int kvm_riscv_vcpu_pmu_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx, +int kvm_riscv_vcpu_pmu_fw_ctr_read (struct kvm_vcpu *vcpu,unsigned long cidx,struct kvm_vcpu_sbi_return *retdata) {int ret;arch/riscv/kvm/vcpu_sbi_pmu.c +1
-1@@ -62,7 +62,7 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, ret =kvm_riscv_vcpu_pmu_ctr_stop (vcpu, cp->a0, cp->a1, cp->a2, retdata);break ;case SBI_EXT_PMU_COUNTER_FW_READ : - ret =kvm_riscv_vcpu_pmu_ctr_read (vcpu, cp->a0, retdata); + ret =kvm_riscv_vcpu_pmu_fw_ctr_read (vcpu, cp->a0, retdata);break ;case SBI_EXT_PMU_COUNTER_FW_READ_HI :if (IS_ENABLED (CONFIG_32BIT ))Signature · 1 line
2.34.1
PATCH v5 16/22 KVM: riscv: selftests: Move sbi definitions to its own header file
In reply to Atish PatraThe SBI definitions will continue to grow. Move the sbi related definitions to its own header file from processor.h
--- .../selftests/kvm/include/riscv/processor.h | 39 --------------- .../testing/selftests/kvm/include/riscv/sbi.h | 50 +++++++++++++++++++ .../selftests/kvm/include/riscv/ucall.h | 1 + tools/testing/selftests/kvm/steal_time.c | 4 +- 4 files changed, 54 insertions(+), 40 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/riscv/sbi.h
tools/testing/selftests/kvm/include/riscv/processor.h +0 -39
@@ -154,45 +154,6 @@ void vm_install_interrupt_handler(struct kvm_vm *vm, exception_handler_fn handle#define PGTBL_PAGE_SIZE PGTBL_L0_BLOCK_SIZE#define PGTBL_PAGE_SIZE_SHIFT PGTBL_L0_BLOCK_SHIFT -/* SBI return error codes */ -#define SBI_SUCCESS 0 -#define SBI_ERR_FAILURE -1 -#define SBI_ERR_NOT_SUPPORTED -2 -#define SBI_ERR_INVALID_PARAM -3 -#define SBI_ERR_DENIED -4 -#define SBI_ERR_INVALID_ADDRESS -5 -#define SBI_ERR_ALREADY_AVAILABLE -6 -#define SBI_ERR_ALREADY_STARTED -7 -#define SBI_ERR_ALREADY_STOPPED -8 - -#define SBI_EXT_EXPERIMENTAL_START 0x08000000 -#define SBI_EXT_EXPERIMENTAL_END 0x08FFFFFF - -#define KVM_RISCV_SELFTESTS_SBI_EXT SBI_EXT_EXPERIMENTAL_END -#define KVM_RISCV_SELFTESTS_SBI_UCALL 0 -#define KVM_RISCV_SELFTESTS_SBI_UNEXP 1 - -enum sbi_ext_id { -SBI_EXT_BASE =0x10 , -SBI_EXT_STA =0x535441 , -}; - -enum sbi_ext_base_fid { -SBI_EXT_BASE_PROBE_EXT =3 , -}; - -struct sbiret { -long error; -long value; -}; - -struct sbiret sbi_ecall (int ext,int fid,unsigned long arg0, -unsigned long arg1,unsigned long arg2, -unsigned long arg3,unsigned long arg4, -unsigned long arg5); - -bool guest_sbi_probe_extension (int extid,long *out_val); -static inline void local_irq_enable (void ) {csr_set (CSR_SSTATUS ,SR_SIE );
tools/testing/selftests/kvm/include/riscv/sbi.h new file +50 -0
50 lines not on this page · raw message
tools/testing/selftests/kvm/include/riscv/ucall.h +1 -0
@@ -3,6 +3,7 @@#define SELFTEST_KVM_UCALL_H #include "processor.h" +#include "sbi.h" #define UCALL_EXIT_REASON KVM_EXIT_RISCV_SBI
tools/testing/selftests/kvm/steal_time.c +3 -1
@@ -11,7 +11,9 @@#include <pthread.h> #include <linux/kernel.h> #include <asm/kvm.h> -#ifndef __riscv +#ifdef __riscv +#include "sbi.h" +#else #include <asm/kvm_para.h> #endif
Signature · 1 line
2.34.1
Re: [PATCH v5 16/22] KVM: riscv: selftests: Move sbi definitions to its own header file
Quoted 149 lines · Atish Patra
The SBI definitions will continue to grow. Move the sbi related definitions to its own header file from
processor.hSuggested-by Andrew Jones <[email protected]>Signed-off-by Atish Patra <[email protected]>--- .../selftests/kvm/include/riscv/processor.h | 39 --------------- .../testing/selftests/kvm/include/riscv/sbi.h | 50 +++++++++++++++++++ .../selftests/kvm/include/riscv/ucall.h | 1 + tools/testing/selftests/kvm/steal_time.c | 4 +- 4 files changed, 54 insertions(+), 40 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/riscv/sbi.htools/testing/selftests/kvm/include/riscv/processor.h +0
-39@@ -154,45 +154,6 @@ void vm_install_interrupt_handler(struct kvm_vm *vm, exception_handler_fn handle#define PGTBL_PAGE_SIZE PGTBL_L0_BLOCK_SIZE#define PGTBL_PAGE_SIZE_SHIFT PGTBL_L0_BLOCK_SHIFT -/* SBI return error codes */ -#define SBI_SUCCESS 0 -#define SBI_ERR_FAILURE -1 -#define SBI_ERR_NOT_SUPPORTED -2 -#define SBI_ERR_INVALID_PARAM -3 -#define SBI_ERR_DENIED -4 -#define SBI_ERR_INVALID_ADDRESS -5 -#define SBI_ERR_ALREADY_AVAILABLE -6 -#define SBI_ERR_ALREADY_STARTED -7 -#define SBI_ERR_ALREADY_STOPPED -8 - -#define SBI_EXT_EXPERIMENTAL_START 0x08000000 -#define SBI_EXT_EXPERIMENTAL_END 0x08FFFFFF - -#define KVM_RISCV_SELFTESTS_SBI_EXT SBI_EXT_EXPERIMENTAL_END -#define KVM_RISCV_SELFTESTS_SBI_UCALL 0 -#define KVM_RISCV_SELFTESTS_SBI_UNEXP 1 - -enum sbi_ext_id { -SBI_EXT_BASE =0x10 , -SBI_EXT_STA =0x535441 , -}; - -enum sbi_ext_base_fid { -SBI_EXT_BASE_PROBE_EXT =3 , -}; - -struct sbiret { -long error; -long value; -}; - -struct sbiret sbi_ecall (int ext,int fid,unsigned long arg0, -unsigned long arg1,unsigned long arg2, -unsigned long arg3,unsigned long arg4, -unsigned long arg5); - -bool guest_sbi_probe_extension (int extid,long *out_val); -static inline void local_irq_enable (void ) {csr_set (CSR_SSTATUS ,SR_SIE );tools/testing/selftests/kvm/include/riscv/sbi.h new file +50
-0@@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* RISC-V SBI specific definitions +* +* Copyright (C) 2024 Rivos Inc. +*/ + +#ifndef SELFTEST_KVM_SBI_H +#define SELFTEST_KVM_SBI_H + +/* SBI return error codes */ +#define SBI_SUCCESS 0 +#define SBI_ERR_FAILURE -1 +#define SBI_ERR_NOT_SUPPORTED -2 +#define SBI_ERR_INVALID_PARAM -3 +#define SBI_ERR_DENIED -4 +#define SBI_ERR_INVALID_ADDRESS -5 +#define SBI_ERR_ALREADY_AVAILABLE -6 +#define SBI_ERR_ALREADY_STARTED -7 +#define SBI_ERR_ALREADY_STOPPED -8 + +#define SBI_EXT_EXPERIMENTAL_START 0x08000000 +#define SBI_EXT_EXPERIMENTAL_END 0x08FFFFFF + +#define KVM_RISCV_SELFTESTS_SBI_EXT SBI_EXT_EXPERIMENTAL_END +#define KVM_RISCV_SELFTESTS_SBI_UCALL 0 +#define KVM_RISCV_SELFTESTS_SBI_UNEXP 1 + +enum sbi_ext_id { +SBI_EXT_BASE =0x10 , +SBI_EXT_STA =0x535441 , +}; + +enum sbi_ext_base_fid { +SBI_EXT_BASE_PROBE_EXT =3 , +}; + +struct sbiret { +long error; +long value; +}; + +struct sbiret sbi_ecall (int ext,int fid,unsigned long arg0, +unsigned long arg1,unsigned long arg2, +unsigned long arg3,unsigned long arg4, +unsigned long arg5); + +bool guest_sbi_probe_extension (int extid,long *out_val); + +#endif /* SELFTEST_KVM_SBI_H */ tools/testing/selftests/kvm/include/riscv/ucall.h +1
-0@@ -3,6 +3,7 @@#define SELFTEST_KVM_UCALL_H #include "processor.h" +#include "sbi.h" #define UCALL_EXIT_REASON KVM_EXIT_RISCV_SBItools/testing/selftests/kvm/steal_time.c +3
-1@@ -11,7 +11,9 @@#include <pthread.h> #include <linux/kernel.h> #include <asm/kvm.h> -#ifndef __riscv +#ifdef __riscv +#include "sbi.h" +#else #include <asm/kvm_para.h> #endif Signature · 1 line
2.34.1
PATCH v5 17/22 KVM: riscv: selftests: Add helper functions for extension checks
In reply to Atish Patra__vcpu_has_ext can check both SBI and ISA extensions when the first argument is properly converted to SBI/ISA extension IDs. Introduce two helper functions to make life easier for developers so they don't have to worry about the conversions.
Replace the current usages as well with new helpers.
--- tools/testing/selftests/kvm/include/riscv/processor.h | 10 ++++++++++ tools/testing/selftests/kvm/riscv/arch_timer.c | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/riscv/processor.h +10 -0
@@ -50,6 +50,16 @@ static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype,bool __vcpu_has_ext (struct kvm_vcpu *vcpu,uint64_t ext); +static inline bool __vcpu_has_isa_ext (struct kvm_vcpu *vcpu,uint64_t isa_ext) +{ +return __vcpu_has_ext (vcpu,RISCV_ISA_EXT_REG (isa_ext)); +} + +static inline bool __vcpu_has_sbi_ext (struct kvm_vcpu *vcpu,uint64_t sbi_ext) +{ +return __vcpu_has_ext (vcpu,RISCV_SBI_EXT_REG (sbi_ext)); +} +struct ex_regs {unsigned long ra;unsigned long sp;
tools/testing/selftests/kvm/riscv/arch_timer.c +1 -1
@@ -85,7 +85,7 @@ struct kvm_vm *test_vm_create(void)int nr_vcpus = test_args.nr_vcpus; vm =vm_create_with_vcpus (nr_vcpus, guest_code, vcpus); -__TEST_REQUIRE (__vcpu_has_ext (vcpus[0 ],RISCV_ISA_EXT_REG (KVM_RISCV_ISA_EXT_SSTC )), +__TEST_REQUIRE (__vcpu_has_isa_ext (vcpus[0 ],KVM_RISCV_ISA_EXT_SSTC ),"SSTC not available, skipping test\n" );vm_init_vector_tables (vm);
Signature · 1 line
2.34.1
Re: [PATCH v5 17/22] KVM: riscv: selftests: Add helper functions for extension checks
Quoted 50 lines · Atish Patra
__vcpu_has_extcan check both SBI and ISA extensions when the first argument is properly converted to SBI/ISA extension IDs. Introduce two helper functions to make life easier for developers so they don't have to worry about the conversions.Replace the current usages as well with new helpers.
Signed-off-by Atish Patra <[email protected]>--- tools/testing/selftests/kvm/include/riscv/processor.h | 10 ++++++++++ tools/testing/selftests/kvm/riscv/arch_timer.c | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-)tools/testing/selftests/kvm/include/riscv/processor.h +10
-0@@ -50,6 +50,16 @@ static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype,bool __vcpu_has_ext (struct kvm_vcpu *vcpu,uint64_t ext); +static inline bool __vcpu_has_isa_ext (struct kvm_vcpu *vcpu,uint64_t isa_ext) +{ +return __vcpu_has_ext (vcpu,RISCV_ISA_EXT_REG (isa_ext)); +} + +static inline bool __vcpu_has_sbi_ext (struct kvm_vcpu *vcpu,uint64_t sbi_ext) +{ +return __vcpu_has_ext (vcpu,RISCV_SBI_EXT_REG (sbi_ext)); +} +struct ex_regs {unsigned long ra;unsigned long sp;tools/testing/selftests/kvm/riscv/arch_timer.c +1
-1@@ -85,7 +85,7 @@ struct kvm_vm *test_vm_create(void)int nr_vcpus = test_args.nr_vcpus; vm =vm_create_with_vcpus (nr_vcpus, guest_code, vcpus); -__TEST_REQUIRE (__vcpu_has_ext (vcpus[0 ],RISCV_ISA_EXT_REG (KVM_RISCV_ISA_EXT_SSTC )), +__TEST_REQUIRE (__vcpu_has_isa_ext (vcpus[0 ],KVM_RISCV_ISA_EXT_SSTC ),"SSTC not available, skipping test\n" );vm_init_vector_tables (vm);Signature · 1 line
2.34.1
PATCH v5 18/22 KVM: riscv: selftests: Add Sscofpmf to get-reg-list test
In reply to Atish PatraThe KVM RISC-V allows Sscofpmf extension for Guest/VM so let us add this extension to get-reg-list test.
--- tools/testing/selftests/kvm/riscv/get-reg-list.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/testing/selftests/kvm/riscv/get-reg-list.c +4 -0
@@ -43,6 +43,7 @@ bool filter_reg(__u64 reg)case KVM_REG_RISCV_ISA_EXT |KVM_REG_RISCV_ISA_SINGLE |KVM_RISCV_ISA_EXT_V :case KVM_REG_RISCV_ISA_EXT |KVM_REG_RISCV_ISA_SINGLE |KVM_RISCV_ISA_EXT_SMSTATEEN :case KVM_REG_RISCV_ISA_EXT |KVM_REG_RISCV_ISA_SINGLE |KVM_RISCV_ISA_EXT_SSAIA : +case KVM_REG_RISCV_ISA_EXT |KVM_REG_RISCV_ISA_SINGLE |KVM_RISCV_ISA_EXT_SSCOFPMF :case KVM_REG_RISCV_ISA_EXT |KVM_REG_RISCV_ISA_SINGLE |KVM_RISCV_ISA_EXT_SSTC :case KVM_REG_RISCV_ISA_EXT |KVM_REG_RISCV_ISA_SINGLE |KVM_RISCV_ISA_EXT_SVINVAL :case KVM_REG_RISCV_ISA_EXT |KVM_REG_RISCV_ISA_SINGLE |KVM_RISCV_ISA_EXT_SVNAPOT :
@@ -408,6 +409,7 @@ static const char *isa_ext_single_id_to_str(__u64 reg_off)KVM_ISA_EXT_ARR (V ),KVM_ISA_EXT_ARR (SMSTATEEN ),KVM_ISA_EXT_ARR (SSAIA ), +KVM_ISA_EXT_ARR (SSCOFPMF ),KVM_ISA_EXT_ARR (SSTC ),KVM_ISA_EXT_ARR (SVINVAL ), KVM_ISA_EXT_ARR(SVNAPOT),
@@ -931,6 +933,7 @@ KVM_ISA_EXT_SUBLIST_CONFIG(fp_f, FP_F);KVM_ISA_EXT_SUBLIST_CONFIG (fp_d,FP_D );KVM_ISA_EXT_SIMPLE_CONFIG (h,H );KVM_ISA_EXT_SUBLIST_CONFIG (smstateen,SMSTATEEN ); +KVM_ISA_EXT_SIMPLE_CONFIG (sscofpmf,SSCOFPMF );KVM_ISA_EXT_SIMPLE_CONFIG (sstc,SSTC );KVM_ISA_EXT_SIMPLE_CONFIG (svinval,SVINVAL );KVM_ISA_EXT_SIMPLE_CONFIG (svnapot,SVNAPOT );
@@ -986,6 +989,7 @@ struct vcpu_reg_list *vcpu_configs[] = { &config_fp_d, &config_h, &config_smstateen, + &config_sscofpmf, &config_sstc, &config_svinval, &config_svnapot,
Signature · 1 line
2.34.1
PATCH v5 19/22 KVM: riscv: selftests: Add SBI PMU extension definitions
In reply to Atish PatraThe SBI PMU extension definition is required for upcoming SBI PMU selftests.
--- .../testing/selftests/kvm/include/riscv/sbi.h | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+)
tools/testing/selftests/kvm/include/riscv/sbi.h +66 -0
@@ -29,17 +29,83 @@enum sbi_ext_id {SBI_EXT_BASE =0x10 ,SBI_EXT_STA =0x535441 , +SBI_EXT_PMU =0x504D55 , };enum sbi_ext_base_fid {SBI_EXT_BASE_PROBE_EXT =3 , }; +enum sbi_ext_pmu_fid { +SBI_EXT_PMU_NUM_COUNTERS =0 , +SBI_EXT_PMU_COUNTER_GET_INFO , +SBI_EXT_PMU_COUNTER_CFG_MATCH , +SBI_EXT_PMU_COUNTER_START , +SBI_EXT_PMU_COUNTER_STOP , +SBI_EXT_PMU_COUNTER_FW_READ , +SBI_EXT_PMU_COUNTER_FW_READ_HI , +SBI_EXT_PMU_SNAPSHOT_SET_SHMEM , +}; + +union sbi_pmu_ctr_info { +unsigned long value; +struct { +unsigned long csr:12 ; +unsigned long width:6 ; +#if __riscv_xlen ==32 +unsigned long reserved:13 ; +#else +unsigned long reserved:45 ; +#endif +unsigned long type:1 ; + }; +};struct sbiret {long error;long value; }; +/** General pmu event codes specified in SBI PMU extension */ +enum sbi_pmu_hw_generic_events_t { +SBI_PMU_HW_NO_EVENT =0 , +SBI_PMU_HW_CPU_CYCLES =1 , +SBI_PMU_HW_INSTRUCTIONS =2 , +SBI_PMU_HW_CACHE_REFERENCES =3 , +SBI_PMU_HW_CACHE_MISSES =4 , +SBI_PMU_HW_BRANCH_INSTRUCTIONS =5 , +SBI_PMU_HW_BRANCH_MISSES =6 , +SBI_PMU_HW_BUS_CYCLES =7 , +SBI_PMU_HW_STALLED_CYCLES_FRONTEND =8 , +SBI_PMU_HW_STALLED_CYCLES_BACKEND =9 , +SBI_PMU_HW_REF_CPU_CYCLES =10 , + +SBI_PMU_HW_GENERAL_MAX , +}; + +/* SBI PMU counter types */ +enum sbi_pmu_ctr_type { +SBI_PMU_CTR_TYPE_HW =0x0 , +SBI_PMU_CTR_TYPE_FW , +}; + +/* Flags defined for config matching function */ +#define SBI_PMU_CFG_FLAG_SKIP_MATCH BIT(0) +#define SBI_PMU_CFG_FLAG_CLEAR_VALUE BIT(1) +#define SBI_PMU_CFG_FLAG_AUTO_START BIT(2) +#define SBI_PMU_CFG_FLAG_SET_VUINH BIT(3) +#define SBI_PMU_CFG_FLAG_SET_VSINH BIT(4) +#define SBI_PMU_CFG_FLAG_SET_UINH BIT(5) +#define SBI_PMU_CFG_FLAG_SET_SINH BIT(6) +#define SBI_PMU_CFG_FLAG_SET_MINH BIT(7) + +/* Flags defined for counter start function */ +#define SBI_PMU_START_FLAG_SET_INIT_VALUE BIT(0) +#define SBI_PMU_START_FLAG_INIT_SNAPSHOT BIT(1) + +/* Flags defined for counter stop function */ +#define SBI_PMU_STOP_FLAG_RESET BIT(0) +#define SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT BIT(1) + struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4,
Signature · 1 line
2.34.1
Re: [PATCH v5 19/22] KVM: riscv: selftests: Add SBI PMU extension definitions
Quoted 100 lines · Atish Patra
The SBI PMU extension definition is required for upcoming SBI PMU selftests.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- .../testing/selftests/kvm/include/riscv/sbi.h | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+)tools/testing/selftests/kvm/include/riscv/sbi.h +66
-0@@ -29,17 +29,83 @@enum sbi_ext_id {SBI_EXT_BASE =0x10 ,SBI_EXT_STA =0x535441 , +SBI_EXT_PMU =0x504D55 , };enum sbi_ext_base_fid {SBI_EXT_BASE_PROBE_EXT =3 , }; +enum sbi_ext_pmu_fid { +SBI_EXT_PMU_NUM_COUNTERS =0 , +SBI_EXT_PMU_COUNTER_GET_INFO , +SBI_EXT_PMU_COUNTER_CFG_MATCH , +SBI_EXT_PMU_COUNTER_START , +SBI_EXT_PMU_COUNTER_STOP , +SBI_EXT_PMU_COUNTER_FW_READ , +SBI_EXT_PMU_COUNTER_FW_READ_HI , +SBI_EXT_PMU_SNAPSHOT_SET_SHMEM , +}; + +union sbi_pmu_ctr_info { +unsigned long value; +struct { +unsigned long csr:12 ; +unsigned long width:6 ; +#if __riscv_xlen ==32 +unsigned long reserved:13 ; +#else +unsigned long reserved:45 ; +#endif +unsigned long type:1 ; + }; +};struct sbiret {long error;long value; }; +/** General pmu event codes specified in SBI PMU extension */ +enum sbi_pmu_hw_generic_events_t { +SBI_PMU_HW_NO_EVENT =0 , +SBI_PMU_HW_CPU_CYCLES =1 , +SBI_PMU_HW_INSTRUCTIONS =2 , +SBI_PMU_HW_CACHE_REFERENCES =3 , +SBI_PMU_HW_CACHE_MISSES =4 , +SBI_PMU_HW_BRANCH_INSTRUCTIONS =5 , +SBI_PMU_HW_BRANCH_MISSES =6 , +SBI_PMU_HW_BUS_CYCLES =7 , +SBI_PMU_HW_STALLED_CYCLES_FRONTEND =8 , +SBI_PMU_HW_STALLED_CYCLES_BACKEND =9 , +SBI_PMU_HW_REF_CPU_CYCLES =10 , + +SBI_PMU_HW_GENERAL_MAX , +}; + +/* SBI PMU counter types */ +enum sbi_pmu_ctr_type { +SBI_PMU_CTR_TYPE_HW =0x0 , +SBI_PMU_CTR_TYPE_FW , +}; + +/* Flags defined for config matching function */ +#define SBI_PMU_CFG_FLAG_SKIP_MATCH BIT(0) +#define SBI_PMU_CFG_FLAG_CLEAR_VALUE BIT(1) +#define SBI_PMU_CFG_FLAG_AUTO_START BIT(2) +#define SBI_PMU_CFG_FLAG_SET_VUINH BIT(3) +#define SBI_PMU_CFG_FLAG_SET_VSINH BIT(4) +#define SBI_PMU_CFG_FLAG_SET_UINH BIT(5) +#define SBI_PMU_CFG_FLAG_SET_SINH BIT(6) +#define SBI_PMU_CFG_FLAG_SET_MINH BIT(7) + +/* Flags defined for counter start function */ +#define SBI_PMU_START_FLAG_SET_INIT_VALUE BIT(0) +#define SBI_PMU_START_FLAG_INIT_SNAPSHOT BIT(1) + +/* Flags defined for counter stop function */ +#define SBI_PMU_STOP_FLAG_RESET BIT(0) +#define SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT BIT(1) + struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4,Signature · 1 line
2.34.1
PATCH v5 20/22 KVM: riscv: selftests: Add SBI PMU selftest
In reply to Atish PatraThis test implements basic sanity test and cycle/instret event counting tests.
--- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/riscv/sbi_pmu_test.c | 340 ++++++++++++++++++ 2 files changed, 341 insertions(+) create mode 100644 tools/testing/selftests/kvm/riscv/sbi_pmu_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -189,6 +189,7 @@ TEST_GEN_PROGS_s390x += rseq_test TEST_GEN_PROGS_s390x += set_memory_region_test TEST_GEN_PROGS_s390x += kvm_binary_stats_test +TEST_GEN_PROGS_riscv += riscv/sbi_pmu_test TEST_GEN_PROGS_riscv += arch_timer TEST_GEN_PROGS_riscv += demand_paging_test TEST_GEN_PROGS_riscv += dirty_log_test
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +340 -0
340 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v5 20/22] KVM: riscv: selftests: Add SBI PMU selftest
On Wed, Apr 03, 2024 at 01:04:49AM -0700, Atish Patra wrote:
...
Quoted 16 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +16
-0+static void test_pmu_basic_sanity (void ) +{ +long out_val =0 ; +bool probe; +struct sbiret ret; +int num_counters =0 , i; +union sbi_pmu_ctr_info ctrinfo; + + probe =guest_sbi_probe_extension (SBI_EXT_PMU , &out_val); +GUEST_ASSERT (probe && out_val ==1 ); + + num_counters =get_num_counters (); + +for (i =0 ; i < num_counters; i++) { + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_GET_INFO , i, +0 ,0 ,0 ,0 ,0 );
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c line 266
Quoted 10 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +10
-0+ +/* There can be gaps in logical counter indicies*/ +if (ret.error) +continue ; +GUEST_ASSERT_NE (ret.value,0 ); + + ctrinfo.value = ret.value; + +/** +* Accesibillity check of hardware and read capability of firmware counters.
Accessibility
Quoted 34 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +34
-0+* The spec doesn't mandate any initial value. No need to check any value. +*/ +read_counter (i, ctrinfo); + } + +GUEST_DONE (); +} + +static void run_vcpu (struct kvm_vcpu *vcpu) +{ +struct ucall uc; + +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +case UCALL_DONE : +case UCALL_SYNC : +break ; +default : +TEST_FAIL ("Unknown ucall %lu" , uc.cmd); +break ; + } +} + +void test_vm_destroy (struct kvm_vm *vm) +{ +memset (ctrinfo_arr,0 ,sizeof (union sbi_pmu_ctr_info ) *RISCV_MAX_PMU_COUNTERS ); + counter_mask_available =0 ; +kvm_vm_free (vm); +} + +static void test_vm_basic_test (void *guest_code)
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c line 310
Quoted 10 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +10
-0+{ +struct kvm_vm *vm; +struct kvm_vcpu *vcpu; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); +__TEST_REQUIRE (__vcpu_has_sbi_ext (vcpu,KVM_RISCV_SBI_EXT_PMU ), +"SBI PMU not available, skipping test" ); +vm_init_vector_tables (vm); +/* Illegal instruction handler is required to verify read access without configuration */ +vm_install_exception_handler (vm,EXC_INST_ILLEGAL , guest_illegal_exception_handler);
I still don't see where the "verify" part is. The handler doesn't record that it had to handle anything.
Quoted 33 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +30
-0+ +vcpu_init_vector_tables (vcpu); +run_vcpu (vcpu); + +test_vm_destroy (vm); +} + +static void test_vm_events_test (void *guest_code) +{ +struct kvm_vm *vm =NULL ; +struct kvm_vcpu *vcpu =NULL ; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); +__TEST_REQUIRE (__vcpu_has_sbi_ext (vcpu,KVM_RISCV_SBI_EXT_PMU ), +"SBI PMU not available, skipping test" ); +run_vcpu (vcpu); + +test_vm_destroy (vm); +} + +int main (void ) +{ +test_vm_basic_test (test_pmu_basic_sanity); +pr_info ("SBI PMU basic test : PASS\n" ); + +test_vm_events_test (test_pmu_events); +pr_info ("SBI PMU event verification test : PASS\n" ); + +return 0 ; +}Signature · 1 line
2.34.1
Thanks,
drew
Re: [PATCH v5 20/22] KVM: riscv: selftests: Add SBI PMU selftest
Quoted 22 lines · Andrew Jones
On Wed, Apr 03, 2024 at 01:04:49AM -0700, Atish Patra wrote:
...Quoted 20 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +20
-0+static void test_pmu_basic_sanity (void ) +{ +long out_val =0 ; +bool probe; +struct sbiret ret; +int num_counters =0 , i; +union sbi_pmu_ctr_info ctrinfo; + + probe =guest_sbi_probe_extension (SBI_EXT_PMU , &out_val); +GUEST_ASSERT (probe && out_val ==1 ); + + num_counters =get_num_counters (); + +for (i =0 ; i < num_counters; i++) { + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_GET_INFO , i, +0 ,0 ,0 ,0 ,0 ); + +/* There can be gaps in logical counter indicies*/ +if (ret.error) +continue ;
Quoted 9 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c line 266
Quoted 6 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +6
-0+GUEST_ASSERT_NE (ret.value,0 ); + + ctrinfo.value = ret.value; + +/** +* Accesibillity check of hardware and read capability of firmware counters. Accessibility
Fixed it.
Quoted 39 lines
Quoted 39 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +39
-0+* The spec doesn't mandate any initial value. No need to check any value. +*/ +read_counter (i, ctrinfo); + } + +GUEST_DONE (); +} + +static void run_vcpu (struct kvm_vcpu *vcpu) +{ +struct ucall uc; + +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +case UCALL_DONE : +case UCALL_SYNC : +break ; +default : +TEST_FAIL ("Unknown ucall %lu" , uc.cmd); +break ; + } +} + +void test_vm_destroy (struct kvm_vm *vm) +{ +memset (ctrinfo_arr,0 ,sizeof (union sbi_pmu_ctr_info ) *RISCV_MAX_PMU_COUNTERS ); + counter_mask_available =0 ; +kvm_vm_free (vm); +} + +static void test_vm_basic_test (void *guest_code) +{ +struct kvm_vm *vm; +struct kvm_vcpu *vcpu; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code);
Quoted 9 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c line 310
Quoted 5 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +5
-0+__TEST_REQUIRE (__vcpu_has_sbi_ext (vcpu,KVM_RISCV_SBI_EXT_PMU ), +"SBI PMU not available, skipping test" ); +vm_init_vector_tables (vm); +/* Illegal instruction handler is required to verify read access without configuration */ +vm_install_exception_handler (vm,EXC_INST_ILLEGAL , guest_illegal_exception_handler);I still don't see where the "verify" part is. The handler doesn't record that it had to handle anything.
The objective of the test is to ensure that we get an illegal instruction without configuration. The presence of the registered exception handler is sufficient for that.
The verify part is that the test doesn't end up in a illegal instruction exception when you try to access a counter without configuring.
Let me know if you think we should more verbose comment to explain the scenario.
Quoted 36 lines
Quoted 33 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +30
-0+ +vcpu_init_vector_tables (vcpu); +run_vcpu (vcpu); + +test_vm_destroy (vm); +} + +static void test_vm_events_test (void *guest_code) +{ +struct kvm_vm *vm =NULL ; +struct kvm_vcpu *vcpu =NULL ; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); +__TEST_REQUIRE (__vcpu_has_sbi_ext (vcpu,KVM_RISCV_SBI_EXT_PMU ), +"SBI PMU not available, skipping test" ); +run_vcpu (vcpu); + +test_vm_destroy (vm); +} + +int main (void ) +{ +test_vm_basic_test (test_pmu_basic_sanity); +pr_info ("SBI PMU basic test : PASS\n" ); + +test_vm_events_test (test_pmu_events); +pr_info ("SBI PMU event verification test : PASS\n" ); + +return 0 ; +}Signature · 1 line
2.34.1Thanks,
drew
Re: [PATCH v5 20/22] KVM: riscv: selftests: Add SBI PMU selftest
Quoted 84 lines · Atish Patra
Quoted 22 lines · Andrew Jones
On Wed, Apr 03, 2024 at 01:04:49AM -0700, Atish Patra wrote:
...Quoted 20 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +20
-0+static void test_pmu_basic_sanity (void ) +{ +long out_val =0 ; +bool probe; +struct sbiret ret; +int num_counters =0 , i; +union sbi_pmu_ctr_info ctrinfo; + + probe =guest_sbi_probe_extension (SBI_EXT_PMU , &out_val); +GUEST_ASSERT (probe && out_val ==1 ); + + num_counters =get_num_counters (); + +for (i =0 ; i < num_counters; i++) { + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_GET_INFO , i, +0 ,0 ,0 ,0 ,0 ); + +/* There can be gaps in logical counter indicies*/ +if (ret.error) +continue ;Quoted 9 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c line 266
Quoted 6 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +6
-0+GUEST_ASSERT_NE (ret.value,0 ); + + ctrinfo.value = ret.value; + +/** +* Accesibillity check of hardware and read capability of firmware counters. Accessibility
Fixed it.
Quoted 39 lines
Quoted 39 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +39
-0+* The spec doesn't mandate any initial value. No need to check any value. +*/ +read_counter (i, ctrinfo); + } + +GUEST_DONE (); +} + +static void run_vcpu (struct kvm_vcpu *vcpu) +{ +struct ucall uc; + +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +case UCALL_DONE : +case UCALL_SYNC : +break ; +default : +TEST_FAIL ("Unknown ucall %lu" , uc.cmd); +break ; + } +} + +void test_vm_destroy (struct kvm_vm *vm) +{ +memset (ctrinfo_arr,0 ,sizeof (union sbi_pmu_ctr_info ) *RISCV_MAX_PMU_COUNTERS ); + counter_mask_available =0 ; +kvm_vm_free (vm); +} + +static void test_vm_basic_test (void *guest_code) +{ +struct kvm_vm *vm; +struct kvm_vcpu *vcpu; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code);Quoted 9 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c line 310
Quoted 5 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +5
-0+__TEST_REQUIRE (__vcpu_has_sbi_ext (vcpu,KVM_RISCV_SBI_EXT_PMU ), +"SBI PMU not available, skipping test" ); +vm_init_vector_tables (vm); +/* Illegal instruction handler is required to verify read access without configuration */ +vm_install_exception_handler (vm,EXC_INST_ILLEGAL , guest_illegal_exception_handler);I still don't see where the "verify" part is. The handler doesn't record that it had to handle anything.
Quoted 2 lines
The objective of the test is to ensure that we get an illegal instruction without configuration.
This part I guessed.
Quoted 2 lines
The presence of the registered exception handler is sufficient for that.
This part I disagree with. The handler may not be necessary and not run if we don't get the ILL. Usually when I write tests like these I set a boolean in the handler and check it after the instruction which should have sent us there to make sure we did indeed go there.
Quoted 7 lines
The verify part is that the test doesn't end up in a illegal instruction exception when you try to access a counter without configuring.
Let me know if you think we should more verbose comment to explain the scenario.
With a boolean the test code will be mostly self documenting, but a short comment saying why we expect the boolean to be set would be good too.
Thanks,
drew
Quoted 38 lines
Quoted 37 lines
Quoted 34 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +30
-0+ +vcpu_init_vector_tables (vcpu); +run_vcpu (vcpu); + +test_vm_destroy (vm); +} + +static void test_vm_events_test (void *guest_code) +{ +struct kvm_vm *vm =NULL ; +struct kvm_vcpu *vcpu =NULL ; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); +__TEST_REQUIRE (__vcpu_has_sbi_ext (vcpu,KVM_RISCV_SBI_EXT_PMU ), +"SBI PMU not available, skipping test" ); +run_vcpu (vcpu); + +test_vm_destroy (vm); +} + +int main (void ) +{ +test_vm_basic_test (test_pmu_basic_sanity); +pr_info ("SBI PMU basic test : PASS\n" ); + +test_vm_events_test (test_pmu_events); +pr_info ("SBI PMU event verification test : PASS\n" ); + +return 0 ; +}Signature · 1 line
2.34.1Thanks,
drew
Re: [PATCH v5 20/22] KVM: riscv: selftests: Add SBI PMU selftest
Quoted 91 lines · On Tue, Apr 9, 2024 at 1:01?AM Andrew Jones <[email protected]> wrote:
Quoted 84 lines · Atish Patra
Quoted 22 lines · Andrew Jones
On Wed, Apr 03, 2024 at 01:04:49AM -0700, Atish Patra wrote:
...Quoted 20 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +20
-0+static void test_pmu_basic_sanity (void ) +{ +long out_val =0 ; +bool probe; +struct sbiret ret; +int num_counters =0 , i; +union sbi_pmu_ctr_info ctrinfo; + + probe =guest_sbi_probe_extension (SBI_EXT_PMU , &out_val); +GUEST_ASSERT (probe && out_val ==1 ); + + num_counters =get_num_counters (); + +for (i =0 ; i < num_counters; i++) { + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_COUNTER_GET_INFO , i, +0 ,0 ,0 ,0 ,0 ); + +/* There can be gaps in logical counter indicies*/ +if (ret.error) +continue ;Quoted 9 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c line 266
Quoted 6 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +6
-0+GUEST_ASSERT_NE (ret.value,0 ); + + ctrinfo.value = ret.value; + +/** +* Accesibillity check of hardware and read capability of firmware counters. Accessibility
Fixed it.
Quoted 39 lines
Quoted 39 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +39
-0+* The spec doesn't mandate any initial value. No need to check any value. +*/ +read_counter (i, ctrinfo); + } + +GUEST_DONE (); +} + +static void run_vcpu (struct kvm_vcpu *vcpu) +{ +struct ucall uc; + +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +case UCALL_DONE : +case UCALL_SYNC : +break ; +default : +TEST_FAIL ("Unknown ucall %lu" , uc.cmd); +break ; + } +} + +void test_vm_destroy (struct kvm_vm *vm) +{ +memset (ctrinfo_arr,0 ,sizeof (union sbi_pmu_ctr_info ) *RISCV_MAX_PMU_COUNTERS ); + counter_mask_available =0 ; +kvm_vm_free (vm); +} + +static void test_vm_basic_test (void *guest_code) +{ +struct kvm_vm *vm; +struct kvm_vcpu *vcpu; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code);Quoted 9 lines
Quoted 5 lines
+ __TEST_REQUIRE(__vcpu_has_sbi_ext(vcpu,
KVM_RISCV_SBI_EXT_PMU),
+ "SBI PMU not available, skipping test");
+vm_init_vector_tables(vm);
+ /* Illegal instruction handler is required to verify read access without configuration */
+vm_install_exception_handler(vm, EXC_INST_ILLEGAL, guest_illegal_exception_handler);I still don't see where the "verify" part is. The handler doesn't record that it had to handle anything.
Quoted 2 lines
The objective of the test is to ensure that we get an illegal instruction without configuration.
This part I guessed.
Quoted 8 lines
Quoted 2 lines
The presence of the registered exception handler is sufficient for that.
This part I disagree with. The handler may not be necessary and not run if we don't get the ILL. Usually when I write tests like these I set a boolean in the handler and check it after the instruction which should have sent us there to make sure we did indeed go there.
Ahh I got your point now. That makes sense. Since it was just a sanity test, I hadn't put the boolean check earlier. But you are correct about bugs in kvm code which wouldn't
generate an expected ILL .
I have added it. Thanks for the suggestion :)
Quoted 52 lines
Quoted 7 lines
The verify part is that the test doesn't end up in a illegal instruction exception when you try to access a counter without configuring.
Let me know if you think we should more verbose comment to explain the scenario.
With a boolean the test code will be mostly self documenting, but a short comment saying why we expect the boolean to be set would be good too.
Thanks,
drewQuoted 38 lines
Quoted 37 lines
Quoted 32 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c new file +30
-1+ +vcpu_init_vector_tables (vcpu); +run_vcpu (vcpu); + +test_vm_destroy (vm); +} + +static void test_vm_events_test (void *guest_code) +{ +struct kvm_vm *vm =NULL ; +struct kvm_vcpu *vcpu =NULL ; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); +__TEST_REQUIRE (__vcpu_has_sbi_ext (vcpu,KVM_RISCV_SBI_EXT_PMU ), +"SBI PMU not available, skipping test" ); +run_vcpu (vcpu); + +test_vm_destroy (vm); +} + +int main (void ) +{ +test_vm_basic_test (test_pmu_basic_sanity); +pr_info ("SBI PMU basic test : PASS\n" ); + +test_vm_events_test (test_pmu_events); +pr_info ("SBI PMU event verification test : PASS\n" ); + +return 0 ; +} --Quoted 2 lines
2.34.1
Thanks,
drew
PATCH v5 21/22 KVM: riscv: selftests: Add a test for PMU snapshot functionality
In reply to Atish PatraVerify PMU snapshot functionality by setting up the shared memory correctly and reading the counter values from the shared memory instead of the CSR.
--- .../testing/selftests/kvm/include/riscv/sbi.h | 25 ++++ .../selftests/kvm/lib/riscv/processor.c | 12 ++ .../selftests/kvm/riscv/sbi_pmu_test.c | 127 ++++++++++++++++++ 3 files changed, 164 insertions(+)
tools/testing/selftests/kvm/include/riscv/sbi.h +25 -0
@@ -8,6 +8,12 @@#ifndef SELFTEST_KVM_SBI_H #define SELFTEST_KVM_SBI_H +/* SBI spec version fields */ +#define SBI_SPEC_VERSION_DEFAULT 0x1 +#define SBI_SPEC_VERSION_MAJOR_SHIFT 24 +#define SBI_SPEC_VERSION_MAJOR_MASK 0x7f +#define SBI_SPEC_VERSION_MINOR_MASK 0xffffff +/* SBI return error codes */ #define SBI_SUCCESS 0#define SBI_ERR_FAILURE -1
@@ -33,6 +39,9 @@ enum sbi_ext_id { };enum sbi_ext_base_fid { +SBI_EXT_BASE_GET_SPEC_VERSION =0 , +SBI_EXT_BASE_GET_IMP_ID , +SBI_EXT_BASE_GET_IMP_VERSION ,SBI_EXT_BASE_PROBE_EXT =3 , };enum sbi_ext_pmu_fid {
@@ -60,6 +69,12 @@ union sbi_pmu_ctr_info { }; }; +struct riscv_pmu_snapshot_data { +u64 ctr_overflow_mask; +u64 ctr_values[64 ]; +u64 reserved[447 ]; +}; +struct sbiret {long error;long value;
@@ -113,4 +128,14 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,bool guest_sbi_probe_extension (int extid,long *out_val); +/* Make SBI version */ +static inline unsigned long sbi_mk_version (unsigned long major, +unsigned long minor) +{ +return ((major &SBI_SPEC_VERSION_MAJOR_MASK ) << +SBI_SPEC_VERSION_MAJOR_SHIFT ) | minor; +} + +unsigned long get_host_sbi_spec_version (void ); +#endif /* SELFTEST_KVM_SBI_H */
tools/testing/selftests/kvm/lib/riscv/processor.c +12 -0
@@ -502,3 +502,15 @@ bool guest_sbi_probe_extension(int extid, long *out_val)return true; } + +unsigned long get_host_sbi_spec_version (void ) +{ +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_BASE ,SBI_EXT_BASE_GET_SPEC_VERSION ,0 , +0 ,0 ,0 ,0 ,0 ); + +GUEST_ASSERT (!ret.error); + +return ret.value; +}
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +127 -0
164 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v5 21/22] KVM: riscv: selftests: Add a test for PMU snapshot functionality
Quoted 53 lines · Atish Patra
Verify PMU snapshot functionality by setting up the shared memory correctly and reading the counter values from the shared memory instead of the CSR.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- .../testing/selftests/kvm/include/riscv/sbi.h | 25 ++++ .../selftests/kvm/lib/riscv/processor.c | 12 ++ .../selftests/kvm/riscv/sbi_pmu_test.c | 127 ++++++++++++++++++ 3 files changed, 164 insertions(+)tools/testing/selftests/kvm/include/riscv/sbi.h +15
-0@@ -8,6 +8,12 @@#ifndef SELFTEST_KVM_SBI_H #define SELFTEST_KVM_SBI_H +/* SBI spec version fields */ +#define SBI_SPEC_VERSION_DEFAULT 0x1 +#define SBI_SPEC_VERSION_MAJOR_SHIFT 24 +#define SBI_SPEC_VERSION_MAJOR_MASK 0x7f +#define SBI_SPEC_VERSION_MINOR_MASK 0xffffff +/* SBI return error codes */ #define SBI_SUCCESS 0#define SBI_ERR_FAILURE -1@@ -33,6 +39,9 @@ enum sbi_ext_id { };enum sbi_ext_base_fid { +SBI_EXT_BASE_GET_SPEC_VERSION =0 , +SBI_EXT_BASE_GET_IMP_ID , +SBI_EXT_BASE_GET_IMP_VERSION ,SBI_EXT_BASE_PROBE_EXT =3 , };enum sbi_ext_pmu_fid {@@ -60,6 +69,12 @@ union sbi_pmu_ctr_info { }; }; +struct riscv_pmu_snapshot_data { +u64 ctr_overflow_mask; +u64 ctr_values[64 ]; +u64 reserved[447 ]; +}; +struct sbiret {long error;long value;
tools/testing/selftests/kvm/include/riscv/sbi.h struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, · line 136
Quoted 9 lines
tools/testing/selftests/kvm/include/riscv/sbi.h +6
-0@@ -113,4 +128,14 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,bool guest_sbi_probe_extension (int extid,long *out_val); +/* Make SBI version */ +static inline unsigned long sbi_mk_version (unsigned long major, +unsigned long minor) +{ +return ((major &SBI_SPEC_VERSION_MAJOR_MASK ) << +SBI_SPEC_VERSION_MAJOR_SHIFT ) | minor;
Should also also mask 'minor'. I see this matches what we have in the kernel so we should fix it there too.
Quoted 46 lines
tools/testing/selftests/kvm/include/riscv/sbi.h +4
-0+} + +unsigned long get_host_sbi_spec_version (void ); +#endif /* SELFTEST_KVM_SBI_H */ tools/testing/selftests/kvm/lib/riscv/processor.c +12
-0@@ -502,3 +502,15 @@ bool guest_sbi_probe_extension(int extid, long *out_val)return true; } + +unsigned long get_host_sbi_spec_version (void ) +{ +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_BASE ,SBI_EXT_BASE_GET_SPEC_VERSION ,0 , +0 ,0 ,0 ,0 ,0 ); + +GUEST_ASSERT (!ret.error); + +return ret.value; +}tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +6
-0@@ -19,6 +19,11 @@#define RISCV_MAX_PMU_COUNTERS 64union sbi_pmu_ctr_info ctrinfo_arr[RISCV_MAX_PMU_COUNTERS ]; +/* Snapshot shared memory data */ +#define PMU_SNAPSHOT_GPA_BASE BIT(30) +static void *snapshot_gva; +static vm_paddr_t snapshot_gpa; +/* Cache the available counters in a bitmask */ static unsigned long counter_mask_available;@@ -178,6 +183,32 @@ static unsigned long read_counter(int idx, union sbi_pmu_ctr_info ctrinfo)return counter_val; } +static inline void verify_sbi_requirement_assert (void )
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c static unsigned long read_counter(int idx, union sbi_pmu_ctr_info ctrinfo) · line 196
Quoted 10 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +10
-0+{ +long out_val =0 ; +bool probe; + + probe =guest_sbi_probe_extension (SBI_EXT_PMU , &out_val); +GUEST_ASSERT (probe && out_val ==1 ); + +if (get_host_sbi_spec_version () <sbi_mk_version (2 ,0 )) +__GUEST_ASSERT (0 ,"SBI implementation version doesn't support PMU Snapshot" ); +}
It's a pity we can't check the SBI spec version that KVM is advertising from KVM userspace. Normally we'd want to check something like this at the start of the test with TEST_REQUIRE() before running a VCPU in order to generate a skip exit.
(We probably should allow reading and even writing the SBI spec version from the VMM in order to better support migration.)
Quoted 145 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +111
-0+ +static void snapshot_set_shmem (vm_paddr_t gpa,unsigned long flags) +{ +unsigned long lo = (unsigned long )gpa; +#if __riscv_xlen ==32 +unsigned long hi = (unsigned long )(gpa >>32 ); +#else +unsigned long hi = gpa ==-1 ?-1 :0 ; +#endif +struct sbiret ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM , + lo, hi, flags,0 ,0 ,0 ); + +GUEST_ASSERT (ret.value ==0 && ret.error ==0 ); +} +static void test_pmu_event (unsigned long event) {unsigned long counter;@@ -210,6 +241,41 @@ static void test_pmu_event(unsigned long event)stop_reset_counter (counter,0 ); } +static void test_pmu_event_snapshot (unsigned long event) +{ +unsigned long counter; +unsigned long counter_value_pre, counter_value_post; +unsigned long counter_init_value =100 ; +struct riscv_pmu_snapshot_data *snapshot_data = snapshot_gva; + + counter =get_counter_index (0 , counter_mask_available,0 , event); + counter_value_pre =read_counter (counter, ctrinfo_arr[counter]); + +/* Do not set the initial value */ +start_counter (counter,0 ,0 ); +dummy_func_loop (10000 ); +stop_counter (counter,SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ); + +/* The counter value is updated w.r.t relative index of cbase */ + counter_value_post =READ_ONCE (snapshot_data->ctr_values[0 ]); +__GUEST_ASSERT (counter_value_post > counter_value_pre, +"counter_value_post %lx counter_value_pre %lx\n" , + counter_value_post, counter_value_pre); + +/* Now set the initial value and compare */ +WRITE_ONCE (snapshot_data->ctr_values[0 ], counter_init_value); +start_counter (counter,SBI_PMU_START_FLAG_INIT_SNAPSHOT ,0 ); +dummy_func_loop (10000 ); +stop_counter (counter,SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ); + + counter_value_post =READ_ONCE (snapshot_data->ctr_values[0 ]); +__GUEST_ASSERT (counter_value_post > counter_init_value, +"counter_value_post %lx counter_init_value %lx for counter\n" , + counter_value_post, counter_init_value); + +stop_reset_counter (counter,0 ); +} +static void test_invalid_event (void ) {struct sbiret ret;@@ -272,6 +338,34 @@ static void test_pmu_basic_sanity(void)GUEST_DONE (); } +static void test_pmu_events_snaphost (void ) +{ +int num_counters =0 ; +struct riscv_pmu_snapshot_data *snapshot_data = snapshot_gva; +int i; + +/* Verify presence of SBI PMU and minimum requrired SBI version */ +verify_sbi_requirement_assert (); + +snapshot_set_shmem (snapshot_gpa,0 ); + +/* Get the counter details */ + num_counters =get_num_counters (); +update_counter_info (num_counters); + +/* Validate shared memory access */ +GUEST_ASSERT_EQ (READ_ONCE (snapshot_data->ctr_overflow_mask),0 ); +for (i =0 ; i < num_counters; i++) { +if (counter_mask_available & (BIT (i))) +GUEST_ASSERT_EQ (READ_ONCE (snapshot_data->ctr_values[i]),0 ); + } +/* Only these two events are guranteed to be present */ +test_pmu_event_snapshot (SBI_PMU_HW_CPU_CYCLES ); +test_pmu_event_snapshot (SBI_PMU_HW_INSTRUCTIONS ); + +GUEST_DONE (); +} +static void run_vcpu (struct kvm_vcpu *vcpu) {struct ucall uc;@@ -328,13 +422,46 @@ static void test_vm_events_test(void *guest_code)test_vm_destroy (vm); } +static void test_vm_setup_snapshot_mem (struct kvm_vm *vm,struct kvm_vcpu *vcpu) +{ +/* PMU Snapshot requires single page only */ +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS ,PMU_SNAPSHOT_GPA_BASE ,1 ,1 ,0 ); +/* PMU_SNAPSHOT_GPA_BASE is identity mapped */ +virt_map (vm,PMU_SNAPSHOT_GPA_BASE ,PMU_SNAPSHOT_GPA_BASE ,1 ); + + snapshot_gva = (void *)(PMU_SNAPSHOT_GPA_BASE ); + snapshot_gpa =addr_gva2gpa (vcpu->vm, (vm_vaddr_t )snapshot_gva); +sync_global_to_guest (vcpu->vm, snapshot_gva); +sync_global_to_guest (vcpu->vm, snapshot_gpa); +} + +static void test_vm_events_snapshot_test (void *guest_code) +{ +struct kvm_vm *vm =NULL ; +struct kvm_vcpu *vcpu; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); +__TEST_REQUIRE (__vcpu_has_sbi_ext (vcpu,KVM_RISCV_SBI_EXT_PMU ), +"SBI PMU not available, skipping test" ); + +test_vm_setup_snapshot_mem (vm, vcpu); + +run_vcpu (vcpu); + +test_vm_destroy (vm); +} +int main (void ) { +pr_info ("SBI PMU basic test : starting\n" );test_vm_basic_test (test_pmu_basic_sanity);pr_info ("SBI PMU basic test : PASS\n" );test_vm_events_test (test_pmu_events);pr_info ("SBI PMU event verification test : PASS\n" ); +test_vm_events_snapshot_test (test_pmu_events_snaphost); +pr_info ("SBI PMU event verification with snapshot test : PASS\n" ); +return 0 ; }Signature · 1 line
2.34.1
Since my comments are a bit out-of-scope for this patch,
Thanks,
drew
Re: [PATCH v5 21/22] KVM: riscv: selftests: Add a test for PMU snapshot functionality
Quoted 57 lines · Andrew Jones
Quoted 56 lines · Atish Patra
Verify PMU snapshot functionality by setting up the shared memory correctly and reading the counter values from the shared memory instead of the CSR.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- .../testing/selftests/kvm/include/riscv/sbi.h | 25 ++++ .../selftests/kvm/lib/riscv/processor.c | 12 ++ .../selftests/kvm/riscv/sbi_pmu_test.c | 127 ++++++++++++++++++ 3 files changed, 164 insertions(+)tools/testing/selftests/kvm/include/riscv/sbi.h +15
-0@@ -8,6 +8,12 @@#ifndef SELFTEST_KVM_SBI_H #define SELFTEST_KVM_SBI_H +/* SBI spec version fields */ +#define SBI_SPEC_VERSION_DEFAULT 0x1 +#define SBI_SPEC_VERSION_MAJOR_SHIFT 24 +#define SBI_SPEC_VERSION_MAJOR_MASK 0x7f +#define SBI_SPEC_VERSION_MINOR_MASK 0xffffff +/* SBI return error codes */ #define SBI_SUCCESS 0#define SBI_ERR_FAILURE -1@@ -33,6 +39,9 @@ enum sbi_ext_id { };enum sbi_ext_base_fid { +SBI_EXT_BASE_GET_SPEC_VERSION =0 , +SBI_EXT_BASE_GET_IMP_ID , +SBI_EXT_BASE_GET_IMP_VERSION ,SBI_EXT_BASE_PROBE_EXT =3 , };enum sbi_ext_pmu_fid {@@ -60,6 +69,12 @@ union sbi_pmu_ctr_info { }; }; +struct riscv_pmu_snapshot_data { +u64 ctr_overflow_mask; +u64 ctr_values[64 ]; +u64 reserved[447 ]; +}; +struct sbiret {long error;long value;@@ -113,4 +128,14 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,bool guest_sbi_probe_extension (int extid,long *out_val);
Quoted 10 lines
tools/testing/selftests/kvm/include/riscv/sbi.h struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, · line 136
Quoted 6 lines
tools/testing/selftests/kvm/include/riscv/sbi.h +6
-0+/* Make SBI version */ +static inline unsigned long sbi_mk_version (unsigned long major, +unsigned long minor) +{ +return ((major &SBI_SPEC_VERSION_MAJOR_MASK ) << +SBI_SPEC_VERSION_MAJOR_SHIFT ) | minor;Should also also mask 'minor'. I see this matches what we have in the kernel so we should fix it there too.
Nice catch. I fixed it in both places.
Quoted 45 lines
Quoted 45 lines
tools/testing/selftests/kvm/include/riscv/sbi.h +4
-0+} + +unsigned long get_host_sbi_spec_version (void ); +#endif /* SELFTEST_KVM_SBI_H */ tools/testing/selftests/kvm/lib/riscv/processor.c +12
-0@@ -502,3 +502,15 @@ bool guest_sbi_probe_extension(int extid, long *out_val)return true; } + +unsigned long get_host_sbi_spec_version (void ) +{ +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_BASE ,SBI_EXT_BASE_GET_SPEC_VERSION ,0 , +0 ,0 ,0 ,0 ,0 ); + +GUEST_ASSERT (!ret.error); + +return ret.value; +}tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +5
-0@@ -19,6 +19,11 @@#define RISCV_MAX_PMU_COUNTERS 64union sbi_pmu_ctr_info ctrinfo_arr[RISCV_MAX_PMU_COUNTERS ]; +/* Snapshot shared memory data */ +#define PMU_SNAPSHOT_GPA_BASE BIT(30) +static void *snapshot_gva; +static vm_paddr_t snapshot_gpa; +/* Cache the available counters in a bitmask */ static unsigned long counter_mask_available;@@ -178,6 +183,32 @@ static unsigned long read_counter(int idx, union sbi_pmu_ctr_info ctrinfo)return counter_val; }
Quoted 17 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c static unsigned long read_counter(int idx, union sbi_pmu_ctr_info ctrinfo) · line 196
Quoted 11 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +11
-0+static inline void verify_sbi_requirement_assert (void ) +{ +long out_val =0 ; +bool probe; + + probe =guest_sbi_probe_extension (SBI_EXT_PMU , &out_val); +GUEST_ASSERT (probe && out_val ==1 ); + +if (get_host_sbi_spec_version () <sbi_mk_version (2 ,0 )) +__GUEST_ASSERT (0 ,"SBI implementation version doesn't support PMU Snapshot" ); +}It's a pity we can't check the SBI spec version that KVM is advertising from KVM userspace. Normally we'd want to check something like this at the start of the test with
TEST_REQUIRE()before running a VCPU in order to generate a skip exit.
Agreed. I will send a separate series for that as it is an ABI change.
Quoted 3 lines
(We probably should allow reading and even writing the SBI spec version from the VMM in order to better support migration.)
How that would work for SBI spec version write use case ? For migraiton, you can't go back to older SBI versions in the host. Isn't it ?
Considering this case your VM is running with PMU snapshot as the host has SBI v2.0. It can't be migrated to v1.0 and expecting it work. Correct ?
Quoted 152 lines
Quoted 145 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +111
-0+ +static void snapshot_set_shmem (vm_paddr_t gpa,unsigned long flags) +{ +unsigned long lo = (unsigned long )gpa; +#if __riscv_xlen ==32 +unsigned long hi = (unsigned long )(gpa >>32 ); +#else +unsigned long hi = gpa ==-1 ?-1 :0 ; +#endif +struct sbiret ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM , + lo, hi, flags,0 ,0 ,0 ); + +GUEST_ASSERT (ret.value ==0 && ret.error ==0 ); +} +static void test_pmu_event (unsigned long event) {unsigned long counter;@@ -210,6 +241,41 @@ static void test_pmu_event(unsigned long event)stop_reset_counter (counter,0 ); } +static void test_pmu_event_snapshot (unsigned long event) +{ +unsigned long counter; +unsigned long counter_value_pre, counter_value_post; +unsigned long counter_init_value =100 ; +struct riscv_pmu_snapshot_data *snapshot_data = snapshot_gva; + + counter =get_counter_index (0 , counter_mask_available,0 , event); + counter_value_pre =read_counter (counter, ctrinfo_arr[counter]); + +/* Do not set the initial value */ +start_counter (counter,0 ,0 ); +dummy_func_loop (10000 ); +stop_counter (counter,SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ); + +/* The counter value is updated w.r.t relative index of cbase */ + counter_value_post =READ_ONCE (snapshot_data->ctr_values[0 ]); +__GUEST_ASSERT (counter_value_post > counter_value_pre, +"counter_value_post %lx counter_value_pre %lx\n" , + counter_value_post, counter_value_pre); + +/* Now set the initial value and compare */ +WRITE_ONCE (snapshot_data->ctr_values[0 ], counter_init_value); +start_counter (counter,SBI_PMU_START_FLAG_INIT_SNAPSHOT ,0 ); +dummy_func_loop (10000 ); +stop_counter (counter,SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ); + + counter_value_post =READ_ONCE (snapshot_data->ctr_values[0 ]); +__GUEST_ASSERT (counter_value_post > counter_init_value, +"counter_value_post %lx counter_init_value %lx for counter\n" , + counter_value_post, counter_init_value); + +stop_reset_counter (counter,0 ); +} +static void test_invalid_event (void ) {struct sbiret ret;@@ -272,6 +338,34 @@ static void test_pmu_basic_sanity(void)GUEST_DONE (); } +static void test_pmu_events_snaphost (void ) +{ +int num_counters =0 ; +struct riscv_pmu_snapshot_data *snapshot_data = snapshot_gva; +int i; + +/* Verify presence of SBI PMU and minimum requrired SBI version */ +verify_sbi_requirement_assert (); + +snapshot_set_shmem (snapshot_gpa,0 ); + +/* Get the counter details */ + num_counters =get_num_counters (); +update_counter_info (num_counters); + +/* Validate shared memory access */ +GUEST_ASSERT_EQ (READ_ONCE (snapshot_data->ctr_overflow_mask),0 ); +for (i =0 ; i < num_counters; i++) { +if (counter_mask_available & (BIT (i))) +GUEST_ASSERT_EQ (READ_ONCE (snapshot_data->ctr_values[i]),0 ); + } +/* Only these two events are guranteed to be present */ +test_pmu_event_snapshot (SBI_PMU_HW_CPU_CYCLES ); +test_pmu_event_snapshot (SBI_PMU_HW_INSTRUCTIONS ); + +GUEST_DONE (); +} +static void run_vcpu (struct kvm_vcpu *vcpu) {struct ucall uc;@@ -328,13 +422,46 @@ static void test_vm_events_test(void *guest_code)test_vm_destroy (vm); } +static void test_vm_setup_snapshot_mem (struct kvm_vm *vm,struct kvm_vcpu *vcpu) +{ +/* PMU Snapshot requires single page only */ +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS ,PMU_SNAPSHOT_GPA_BASE ,1 ,1 ,0 ); +/* PMU_SNAPSHOT_GPA_BASE is identity mapped */ +virt_map (vm,PMU_SNAPSHOT_GPA_BASE ,PMU_SNAPSHOT_GPA_BASE ,1 ); + + snapshot_gva = (void *)(PMU_SNAPSHOT_GPA_BASE ); + snapshot_gpa =addr_gva2gpa (vcpu->vm, (vm_vaddr_t )snapshot_gva); +sync_global_to_guest (vcpu->vm, snapshot_gva); +sync_global_to_guest (vcpu->vm, snapshot_gpa); +} + +static void test_vm_events_snapshot_test (void *guest_code) +{ +struct kvm_vm *vm =NULL ; +struct kvm_vcpu *vcpu; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); +__TEST_REQUIRE (__vcpu_has_sbi_ext (vcpu,KVM_RISCV_SBI_EXT_PMU ), +"SBI PMU not available, skipping test" ); + +test_vm_setup_snapshot_mem (vm, vcpu); + +run_vcpu (vcpu); + +test_vm_destroy (vm); +} +int main (void ) { +pr_info ("SBI PMU basic test : starting\n" );test_vm_basic_test (test_pmu_basic_sanity);pr_info ("SBI PMU basic test : PASS\n" );test_vm_events_test (test_pmu_events);pr_info ("SBI PMU event verification test : PASS\n" ); +test_vm_events_snapshot_test (test_pmu_events_snaphost); +pr_info ("SBI PMU event verification with snapshot test : PASS\n" ); +return 0 ; }Signature · 1 line
2.34.1Since my comments are a bit out-of-scope for this patch,
Reviewed-by Andrew Jones <[email protected]>Thanks,
drew
Re: [PATCH v5 21/22] KVM: riscv: selftests: Add a test for PMU snapshot functionality
Quoted 2 lines · Atish Patra
Quoted 1 line · Andrew Jones
On Wed, Apr 03, 2024 at 01:04:50AM -0700, Atish Patra wrote:
...
Quoted 19 lines
Quoted 12 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c static unsigned long read_counter(int idx, union sbi_pmu_ctr_info ctrinfo) · line 196
Quoted 6 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +6
-0+ probe =guest_sbi_probe_extension (SBI_EXT_PMU , &out_val); +GUEST_ASSERT (probe && out_val ==1 ); + +if (get_host_sbi_spec_version () <sbi_mk_version (2 ,0 )) +__GUEST_ASSERT (0 ,"SBI implementation version doesn't support PMU Snapshot" ); +}It's a pity we can't check the SBI spec version that KVM is advertising from KVM userspace. Normally we'd want to check something like this at the start of the test with
TEST_REQUIRE()before running a VCPU in order to generate a skip exit.Agreed. I will send a separate series for that as it is an ABI change.
Quoted 3 lines
(We probably should allow reading and even writing the SBI spec version from the VMM in order to better support migration.)
Quoted 6 lines
How that would work for SBI spec version write use case ? For migraiton, you can't go back to older SBI versions in the host. Isn't it ?
Considering this case your VM is running with PMU snapshot as the host has SBI v2.0. It can't be migrated to v1.0 and expecting it work. Correct ?
We can start a VM on a host with SBI v2.0, but tell KVM to tell the VM that it has v1.0. Then, the guest shouldn't use any features from SBI that appear after v1.0 and it should be safe to migrate to a host with v1.0.
A more likely scenario might be this though:
1. KVM userspace checks and captures the SBI version of the host where the VM is first being launched, e.g. v2.0
2. The VM gets migrated to another host which supports something later, e.g. v3.0, but to
- avoid possibly confusing the guest we tell the destination host that it should expose v2.0 as the SBI version
- allow rollback to the source host without concern that the guest has already seen v3.0 and started to use something that the source can't provide
Thanks,
drew
Re: [PATCH v5 21/22] KVM: riscv: selftests: Add a test for PMU snapshot functionality
Quoted 23 lines · Andrew Jones
Quoted 2 lines · Atish Patra
Quoted 1 line · Andrew Jones
On Wed, Apr 03, 2024 at 01:04:50AM -0700, Atish Patra wrote:
...
Quoted 19 lines
Quoted 11 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c static unsigned long read_counter(int idx, union sbi_pmu_ctr_info ctrinfo) · line 196
Quoted 6 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +6
-0+ probe =guest_sbi_probe_extension (SBI_EXT_PMU , &out_val); +GUEST_ASSERT (probe && out_val ==1 ); + +if (get_host_sbi_spec_version () <sbi_mk_version (2 ,0 )) +__GUEST_ASSERT (0 ,"SBI implementation version doesn't support PMU Snapshot" ); +}It's a pity we can't check the SBI spec version that KVM is advertising from KVM userspace. Normally we'd want to check something like this at the start of the test with
TEST_REQUIRE()before running a VCPU in order to generate a skip exit.Agreed. I will send a separate series for that as it is an ABI change.
Quoted 3 lines
(We probably should allow reading and even writing the SBI spec version from the VMM in order to better support migration.)
How that would work for SBI spec version write use case ? For migraiton, you can't go back to older SBI versions in the host. Isn't it ?
Quoted 7 lines
Quoted 3 lines
Considering this case your VM is running with PMU snapshot as the host has SBI v2.0. It can't be migrated to v1.0 and expecting it work. Correct ?
We can start a VM on a host with SBI v2.0, but tell KVM to tell the VM that it has v1.0. Then, the guest shouldn't use any features from SBI that appear after v1.0 and it should be safe to migrate to a host with v1.0.
That depends on when the VMM request to KVM to change the version. Most of SBI implementation checks the SBI version at the boot and enable/disable
feature based on the SBI version available. If the SBI version supported by KVM changes
to an older one, the calls from VM will fail unexpectedly.
Quoted 11 lines
A more likely scenario might be this though:
1. KVM userspace checks and captures the SBI version of the host where the VM is first being launched, e.g. v2.0
2. The VM gets migrated to another host which supports something later, e.g. v3.0, but to
- avoid possibly confusing the guest we tell the destination host that it should expose v2.0 as the SBI version
- allow rollback to the source host without concern that the guest has already seen v3.0 and started to use something that the source can't provide
This makes sense though. As per my understanding, we should not allow modifying
the SBI version that is less that the version VM already boot with. However, we can allow modifying the SBI version that is higher or same as the VM booted with.
I can't think of a use case for the higher version though.
Quoted 2 lines
Thanks,
drew
Re: [PATCH v5 21/22] KVM: riscv: selftests: Add a test for PMU snapshot functionality
Quoted 33 lines · Atish Patra
Quoted 23 lines · Andrew Jones
Quoted 2 lines · Atish Patra
Quoted 1 line · Andrew Jones
On Wed, Apr 03, 2024 at 01:04:50AM -0700, Atish Patra wrote:
...
Quoted 19 lines
Quoted 11 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c static unsigned long read_counter(int idx, union sbi_pmu_ctr_info ctrinfo) · line 196
Quoted 6 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +6
-0+ probe =guest_sbi_probe_extension (SBI_EXT_PMU , &out_val); +GUEST_ASSERT (probe && out_val ==1 ); + +if (get_host_sbi_spec_version () <sbi_mk_version (2 ,0 )) +__GUEST_ASSERT (0 ,"SBI implementation version doesn't support PMU Snapshot" ); +}It's a pity we can't check the SBI spec version that KVM is advertising from KVM userspace. Normally we'd want to check something like this at the start of the test with
TEST_REQUIRE()before running a VCPU in order to generate a skip exit.Agreed. I will send a separate series for that as it is an ABI change.
Quoted 3 lines
(We probably should allow reading and even writing the SBI spec version from the VMM in order to better support migration.)
How that would work for SBI spec version write use case ? For migraiton, you can't go back to older SBI versions in the host. Isn't it ?
Quoted 7 lines
Quoted 3 lines
Considering this case your VM is running with PMU snapshot as the host has SBI v2.0. It can't be migrated to v1.0 and expecting it work. Correct ?
We can start a VM on a host with SBI v2.0, but tell KVM to tell the VM that it has v1.0. Then, the guest shouldn't use any features from SBI that appear after v1.0 and it should be safe to migrate to a host with v1.0.
Quoted 6 lines
That depends on when the VMM request to KVM to change the version. Most of SBI implementation checks the SBI version at the boot and enable/disable
feature based on the SBI version available. If the SBI version supported by KVM changes
to an older one, the calls from VM will fail unexpectedly.
We have to configure KVM's SBI version before the first run of VCPUs, just like we should make sure ISA/SBI extensions are configured first.
Quoted 13 lines
Quoted 12 lines
A more likely scenario might be this though:
1. KVM userspace checks and captures the SBI version of the host where the VM is first being launched, e.g. v2.0
2. The VM gets migrated to another host which supports something later, e.g. v3.0, but to
- avoid possibly confusing the guest we tell the destination host that it should expose v2.0 as the SBI version
- allow rollback to the source host without concern that the guest has already seen v3.0 and started to use something that the source can't provide
Quoted 5 lines
This makes sense though. As per my understanding, we should not allow modifying
the SBI version that is less that the version VM already boot with. However, we can allow modifying the SBI version that is higher or same as the VM booted with.
Mostly only 'the same as'. Higher might work, but it's also risky since there could be guests out there which capture the version on boot and then for whatever reason do sanity checks against that later on and freak out when there's a change, even if the change went higher.
Quoted 2 lines
I can't think of a use case for the higher version though.
Maybe only for a coordinated update which uses kexec rather than a full shutdown+boot cycle, but I'm reaching...
Regarding a full shutdown+boot cycle, in those cases, we're usually free to make changes as that's the same as a host kernel being shutdown and then being boot again after a firmware update.
Thanks,
drew
PATCH v5 22/22 KVM: riscv: selftests: Add a test for counter overflow
In reply to Atish PatraAdd a test for verifying overflow interrupt. Currently, it relies on overflow support on cycle/instret events. This test works for cycle/ instret events which support sampling via hpmcounters on the platform. There are no ISA extensions to detect if a platform supports that. Thus, this test will fail on platform with virtualization but doesn't support overflow on these two events.
--- .../selftests/kvm/riscv/sbi_pmu_test.c | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+)
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +114 -0
@@ -14,6 +14,7 @@#include "test_util.h" #include "processor.h" #include "sbi.h" +#include "arch_timer.h" /* Maximum counters(firmware + hardware) */ #define RISCV_MAX_PMU_COUNTERS 64
@@ -24,6 +25,9 @@ union sbi_pmu_ctr_info ctrinfo_arr[RISCV_MAX_PMU_COUNTERS];static void *snapshot_gva;static vm_paddr_t snapshot_gpa; +static int vcpu_shared_irq_count; +static int counter_in_use; +/* Cache the available counters in a bitmask */ static unsigned long counter_mask_available;
@@ -117,6 +121,31 @@ static void guest_illegal_exception_handler(struct ex_regs *regs) regs->epc +=4 ; } +static void guest_irq_handler (struct ex_regs *regs) +{ +unsigned int irq_num = regs->cause & ~CAUSE_IRQ_FLAG ; +struct riscv_pmu_snapshot_data *snapshot_data = snapshot_gva; +unsigned long overflown_mask; +unsigned long counter_val =0 ; + +/* Validate that we are in the correct irq handler */ +GUEST_ASSERT_EQ (irq_num,IRQ_PMU_OVF ); + +/* Stop all counters first to avoid further interrupts */ +stop_counter (counter_in_use,SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ); + +csr_clear (CSR_SIP ,BIT (IRQ_PMU_OVF )); + + overflown_mask =READ_ONCE (snapshot_data->ctr_overflow_mask); +GUEST_ASSERT (overflown_mask &0x01 ); + +WRITE_ONCE (vcpu_shared_irq_count, vcpu_shared_irq_count+1 ); + + counter_val =READ_ONCE (snapshot_data->ctr_values[0 ]); +/* Now start the counter to mimick the real driver behavior */ +start_counter (counter_in_use,SBI_PMU_START_FLAG_SET_INIT_VALUE , counter_val); +} +static unsigned long get_counter_index (unsigned long cbase,unsigned long cmask,unsigned long cflags,unsigned long event)
@@ -276,6 +305,33 @@ static void test_pmu_event_snapshot(unsigned long event)stop_reset_counter (counter,0 ); } +static void test_pmu_event_overflow (unsigned long event) +{ +unsigned long counter; +unsigned long counter_value_post; +unsigned long counter_init_value =ULONG_MAX -10000 ; +struct riscv_pmu_snapshot_data *snapshot_data = snapshot_gva; + + counter =get_counter_index (0 , counter_mask_available,0 , event); + counter_in_use = counter; + +/* The counter value is updated w.r.t relative index of cbase passed to start/stop */ +WRITE_ONCE (snapshot_data->ctr_values[0 ], counter_init_value); +start_counter (counter,SBI_PMU_START_FLAG_INIT_SNAPSHOT ,0 ); +dummy_func_loop (10000 ); +udelay (msecs_to_usecs (2000 )); +/* irq handler should have stopped the counter */ +stop_counter (counter,SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ); + + counter_value_post =READ_ONCE (snapshot_data->ctr_values[0 ]); +/* The counter value after stopping should be less the init value due to overflow */ +__GUEST_ASSERT (counter_value_post < counter_init_value, +"counter_value_post %lx counter_init_value %lx for counter\n" , + counter_value_post, counter_init_value); + +stop_reset_counter (counter,0 ); +} +static void test_invalid_event (void ) {struct sbiret ret;
@@ -366,6 +422,34 @@ static void test_pmu_events_snaphost(void)GUEST_DONE (); } +static void test_pmu_events_overflow (void ) +{ +int num_counters =0 ; + +/* Verify presence of SBI PMU and minimum requrired SBI version */ +verify_sbi_requirement_assert (); + +snapshot_set_shmem (snapshot_gpa,0 ); +csr_set (CSR_IE ,BIT (IRQ_PMU_OVF )); +local_irq_enable (); + +/* Get the counter details */ + num_counters =get_num_counters (); +update_counter_info (num_counters); + +/* +* Qemu supports overflow for cycle/instruction. +* This test may fail on any platform that do not support overflow for these two events. +*/ +test_pmu_event_overflow (SBI_PMU_HW_CPU_CYCLES ); +GUEST_ASSERT_EQ (vcpu_shared_irq_count,1 ); + +test_pmu_event_overflow (SBI_PMU_HW_INSTRUCTIONS ); +GUEST_ASSERT_EQ (vcpu_shared_irq_count,2 ); + +GUEST_DONE (); +} +static void run_vcpu (struct kvm_vcpu *vcpu) {struct ucall uc;
@@ -451,6 +535,33 @@ static void test_vm_events_snapshot_test(void *guest_code)test_vm_destroy (vm); } +static void test_vm_events_overflow (void *guest_code) +{ +struct kvm_vm *vm =NULL ; +struct kvm_vcpu *vcpu; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); +__TEST_REQUIRE (__vcpu_has_sbi_ext (vcpu,KVM_RISCV_SBI_EXT_PMU ), +"SBI PMU not available, skipping test" ); + +__TEST_REQUIRE (__vcpu_has_isa_ext (vcpu,KVM_RISCV_ISA_EXT_SSCOFPMF ), +"Sscofpmf is not available, skipping overflow test" ); + + +test_vm_setup_snapshot_mem (vm, vcpu); +vm_init_vector_tables (vm); +vm_install_interrupt_handler (vm, guest_irq_handler); + +vcpu_init_vector_tables (vcpu); +/* Initialize guest timer frequency. */ +vcpu_get_reg (vcpu,RISCV_TIMER_REG (frequency), &timer_freq); +sync_global_to_guest (vm, timer_freq); + +run_vcpu (vcpu); + +test_vm_destroy (vm); +} +int main (void ) {pr_info ("SBI PMU basic test : starting\n" );
@@ -463,5 +574,8 @@ int main(void)test_vm_events_snapshot_test (test_pmu_events_snaphost);pr_info ("SBI PMU event verification with snapshot test : PASS\n" ); +test_vm_events_overflow (test_pmu_events_overflow); +pr_info ("SBI PMU event verification with overflow test : PASS\n" ); +return 0 ; }
Signature · 1 line
2.34.1
Re: [PATCH v5 22/22] KVM: riscv: selftests: Add a test for counter overflow
Quoted 6 lines · Atish Patra
Add a test for verifying overflow interrupt. Currently, it relies on overflow support on cycle/instret events. This test works for cycle/ instret events which support sampling via hpmcounters on the platform. There are no ISA extensions to detect if a platform supports that. Thus, this test will fail on platform with virtualization but doesn't support overflow on these two events.
Maybe we should give the user a command line option to disable this test in case the platform they're testing doesn't support it but they want to run the rest of the tests without getting a FAIL.
Quoted 137 lines
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- .../selftests/kvm/riscv/sbi_pmu_test.c | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+)tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +87
-0@@ -14,6 +14,7 @@#include "test_util.h" #include "processor.h" #include "sbi.h" +#include "arch_timer.h" /* Maximum counters(firmware + hardware) */ #define RISCV_MAX_PMU_COUNTERS 64@@ -24,6 +25,9 @@ union sbi_pmu_ctr_info ctrinfo_arr[RISCV_MAX_PMU_COUNTERS];static void *snapshot_gva;static vm_paddr_t snapshot_gpa; +static int vcpu_shared_irq_count; +static int counter_in_use; +/* Cache the available counters in a bitmask */ static unsigned long counter_mask_available;@@ -117,6 +121,31 @@ static void guest_illegal_exception_handler(struct ex_regs *regs) regs->epc +=4 ; } +static void guest_irq_handler (struct ex_regs *regs) +{ +unsigned int irq_num = regs->cause & ~CAUSE_IRQ_FLAG ; +struct riscv_pmu_snapshot_data *snapshot_data = snapshot_gva; +unsigned long overflown_mask; +unsigned long counter_val =0 ; + +/* Validate that we are in the correct irq handler */ +GUEST_ASSERT_EQ (irq_num,IRQ_PMU_OVF ); + +/* Stop all counters first to avoid further interrupts */ +stop_counter (counter_in_use,SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ); + +csr_clear (CSR_SIP ,BIT (IRQ_PMU_OVF )); + + overflown_mask =READ_ONCE (snapshot_data->ctr_overflow_mask); +GUEST_ASSERT (overflown_mask &0x01 ); + +WRITE_ONCE (vcpu_shared_irq_count, vcpu_shared_irq_count+1 ); + + counter_val =READ_ONCE (snapshot_data->ctr_values[0 ]); +/* Now start the counter to mimick the real driver behavior */ +start_counter (counter_in_use,SBI_PMU_START_FLAG_SET_INIT_VALUE , counter_val); +} +static unsigned long get_counter_index (unsigned long cbase,unsigned long cmask,unsigned long cflags,unsigned long event)@@ -276,6 +305,33 @@ static void test_pmu_event_snapshot(unsigned long event)stop_reset_counter (counter,0 ); } +static void test_pmu_event_overflow (unsigned long event) +{ +unsigned long counter; +unsigned long counter_value_post; +unsigned long counter_init_value =ULONG_MAX -10000 ; +struct riscv_pmu_snapshot_data *snapshot_data = snapshot_gva; + + counter =get_counter_index (0 , counter_mask_available,0 , event); + counter_in_use = counter; + +/* The counter value is updated w.r.t relative index of cbase passed to start/stop */ +WRITE_ONCE (snapshot_data->ctr_values[0 ], counter_init_value); +start_counter (counter,SBI_PMU_START_FLAG_INIT_SNAPSHOT ,0 ); +dummy_func_loop (10000 ); +udelay (msecs_to_usecs (2000 )); +/* irq handler should have stopped the counter */ +stop_counter (counter,SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ); + + counter_value_post =READ_ONCE (snapshot_data->ctr_values[0 ]); +/* The counter value after stopping should be less the init value due to overflow */ +__GUEST_ASSERT (counter_value_post < counter_init_value, +"counter_value_post %lx counter_init_value %lx for counter\n" , + counter_value_post, counter_init_value); + +stop_reset_counter (counter,0 ); +} +static void test_invalid_event (void ) {struct sbiret ret;@@ -366,6 +422,34 @@ static void test_pmu_events_snaphost(void)GUEST_DONE (); } +static void test_pmu_events_overflow (void ) +{ +int num_counters =0 ; + +/* Verify presence of SBI PMU and minimum requrired SBI version */ +verify_sbi_requirement_assert (); + +snapshot_set_shmem (snapshot_gpa,0 ); +csr_set (CSR_IE ,BIT (IRQ_PMU_OVF )); +local_irq_enable (); + +/* Get the counter details */ + num_counters =get_num_counters (); +update_counter_info (num_counters); + +/* +* Qemu supports overflow for cycle/instruction. +* This test may fail on any platform that do not support overflow for these two events. +*/ +test_pmu_event_overflow (SBI_PMU_HW_CPU_CYCLES ); +GUEST_ASSERT_EQ (vcpu_shared_irq_count,1 ); + +test_pmu_event_overflow (SBI_PMU_HW_INSTRUCTIONS ); +GUEST_ASSERT_EQ (vcpu_shared_irq_count,2 ); + +GUEST_DONE (); +} +static void run_vcpu (struct kvm_vcpu *vcpu) {struct ucall uc;@@ -451,6 +535,33 @@ static void test_vm_events_snapshot_test(void *guest_code)test_vm_destroy (vm); } +static void test_vm_events_overflow (void *guest_code) +{ +struct kvm_vm *vm =NULL ;
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c static void test_vm_events_snapshot_test(void *guest_code) · line 550
Quoted 10 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +10
-0+struct kvm_vcpu *vcpu; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); +__TEST_REQUIRE (__vcpu_has_sbi_ext (vcpu,KVM_RISCV_SBI_EXT_PMU ), +"SBI PMU not available, skipping test" ); + +__TEST_REQUIRE (__vcpu_has_isa_ext (vcpu,KVM_RISCV_ISA_EXT_SSCOFPMF ), +"Sscofpmf is not available, skipping overflow test" ); + +
extra blank line here
Quoted 29 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +17
-0+test_vm_setup_snapshot_mem (vm, vcpu); +vm_init_vector_tables (vm); +vm_install_interrupt_handler (vm, guest_irq_handler); + +vcpu_init_vector_tables (vcpu); +/* Initialize guest timer frequency. */ +vcpu_get_reg (vcpu,RISCV_TIMER_REG (frequency), &timer_freq); +sync_global_to_guest (vm, timer_freq); + +run_vcpu (vcpu); + +test_vm_destroy (vm); +} +int main (void ) {pr_info ("SBI PMU basic test : starting\n" );@@ -463,5 +574,8 @@ int main(void)test_vm_events_snapshot_test (test_pmu_events_snaphost);pr_info ("SBI PMU event verification with snapshot test : PASS\n" ); +test_vm_events_overflow (test_pmu_events_overflow); +pr_info ("SBI PMU event verification with overflow test : PASS\n" ); +return 0 ; }Signature · 1 line
2.34.1
Other than the command line option idea,
Thanks,
drew
Re: [PATCH v5 22/22] KVM: riscv: selftests: Add a test for counter overflow
Quoted 8 lines · Andrew Jones
Quoted 6 lines · Atish Patra
Add a test for verifying overflow interrupt. Currently, it relies on overflow support on cycle/instret events. This test works for cycle/ instret events which support sampling via hpmcounters on the platform. There are no ISA extensions to detect if a platform supports that. Thus, this test will fail on platform with virtualization but doesn't support overflow on these two events.
Quoted 4 lines
Maybe we should give the user a command line option to disable this test in case the platform they're testing doesn't support it but they want to run the rest of the tests without getting a FAIL.
Sure. I will add that option.
Quoted 134 lines
Quoted 134 lines
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- .../selftests/kvm/riscv/sbi_pmu_test.c | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+)tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +84
-0@@ -14,6 +14,7 @@#include "test_util.h" #include "processor.h" #include "sbi.h" +#include "arch_timer.h" /* Maximum counters(firmware + hardware) */ #define RISCV_MAX_PMU_COUNTERS 64@@ -24,6 +25,9 @@ union sbi_pmu_ctr_info ctrinfo_arr[RISCV_MAX_PMU_COUNTERS];static void *snapshot_gva;static vm_paddr_t snapshot_gpa; +static int vcpu_shared_irq_count; +static int counter_in_use; +/* Cache the available counters in a bitmask */ static unsigned long counter_mask_available;@@ -117,6 +121,31 @@ static void guest_illegal_exception_handler(struct ex_regs *regs) regs->epc +=4 ; } +static void guest_irq_handler (struct ex_regs *regs) +{ +unsigned int irq_num = regs->cause & ~CAUSE_IRQ_FLAG ; +struct riscv_pmu_snapshot_data *snapshot_data = snapshot_gva; +unsigned long overflown_mask; +unsigned long counter_val =0 ; + +/* Validate that we are in the correct irq handler */ +GUEST_ASSERT_EQ (irq_num,IRQ_PMU_OVF ); + +/* Stop all counters first to avoid further interrupts */ +stop_counter (counter_in_use,SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ); + +csr_clear (CSR_SIP ,BIT (IRQ_PMU_OVF )); + + overflown_mask =READ_ONCE (snapshot_data->ctr_overflow_mask); +GUEST_ASSERT (overflown_mask &0x01 ); + +WRITE_ONCE (vcpu_shared_irq_count, vcpu_shared_irq_count+1 ); + + counter_val =READ_ONCE (snapshot_data->ctr_values[0 ]); +/* Now start the counter to mimick the real driver behavior */ +start_counter (counter_in_use,SBI_PMU_START_FLAG_SET_INIT_VALUE , counter_val); +} +static unsigned long get_counter_index (unsigned long cbase,unsigned long cmask,unsigned long cflags,unsigned long event)@@ -276,6 +305,33 @@ static void test_pmu_event_snapshot(unsigned long event)stop_reset_counter (counter,0 ); } +static void test_pmu_event_overflow (unsigned long event) +{ +unsigned long counter; +unsigned long counter_value_post; +unsigned long counter_init_value =ULONG_MAX -10000 ; +struct riscv_pmu_snapshot_data *snapshot_data = snapshot_gva; + + counter =get_counter_index (0 , counter_mask_available,0 , event); + counter_in_use = counter; + +/* The counter value is updated w.r.t relative index of cbase passed to start/stop */ +WRITE_ONCE (snapshot_data->ctr_values[0 ], counter_init_value); +start_counter (counter,SBI_PMU_START_FLAG_INIT_SNAPSHOT ,0 ); +dummy_func_loop (10000 ); +udelay (msecs_to_usecs (2000 )); +/* irq handler should have stopped the counter */ +stop_counter (counter,SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT ); + + counter_value_post =READ_ONCE (snapshot_data->ctr_values[0 ]); +/* The counter value after stopping should be less the init value due to overflow */ +__GUEST_ASSERT (counter_value_post < counter_init_value, +"counter_value_post %lx counter_init_value %lx for counter\n" , + counter_value_post, counter_init_value); + +stop_reset_counter (counter,0 ); +} +static void test_invalid_event (void ) {struct sbiret ret;@@ -366,6 +422,34 @@ static void test_pmu_events_snaphost(void)GUEST_DONE (); } +static void test_pmu_events_overflow (void ) +{ +int num_counters =0 ; + +/* Verify presence of SBI PMU and minimum requrired SBI version */ +verify_sbi_requirement_assert (); + +snapshot_set_shmem (snapshot_gpa,0 ); +csr_set (CSR_IE ,BIT (IRQ_PMU_OVF )); +local_irq_enable (); + +/* Get the counter details */ + num_counters =get_num_counters (); +update_counter_info (num_counters); + +/* +* Qemu supports overflow for cycle/instruction. +* This test may fail on any platform that do not support overflow for these two events. +*/ +test_pmu_event_overflow (SBI_PMU_HW_CPU_CYCLES ); +GUEST_ASSERT_EQ (vcpu_shared_irq_count,1 ); + +test_pmu_event_overflow (SBI_PMU_HW_INSTRUCTIONS ); +GUEST_ASSERT_EQ (vcpu_shared_irq_count,2 ); + +GUEST_DONE (); +} +static void run_vcpu (struct kvm_vcpu *vcpu) {struct ucall uc;@@ -451,6 +535,33 @@ static void test_vm_events_snapshot_test(void *guest_code)test_vm_destroy (vm); }
Quoted 16 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c static void test_vm_events_snapshot_test(void *guest_code) · line 550
Quoted 13 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +13
-0+static void test_vm_events_overflow (void *guest_code) +{ +struct kvm_vm *vm =NULL ; +struct kvm_vcpu *vcpu; + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); +__TEST_REQUIRE (__vcpu_has_sbi_ext (vcpu,KVM_RISCV_SBI_EXT_PMU ), +"SBI PMU not available, skipping test" ); + +__TEST_REQUIRE (__vcpu_has_isa_ext (vcpu,KVM_RISCV_ISA_EXT_SSCOFPMF ), +"Sscofpmf is not available, skipping overflow test" ); + +extra blank line here
Fixed.
Quoted 36 lines
Quoted 29 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +17
-0+test_vm_setup_snapshot_mem (vm, vcpu); +vm_init_vector_tables (vm); +vm_install_interrupt_handler (vm, guest_irq_handler); + +vcpu_init_vector_tables (vcpu); +/* Initialize guest timer frequency. */ +vcpu_get_reg (vcpu,RISCV_TIMER_REG (frequency), &timer_freq); +sync_global_to_guest (vm, timer_freq); + +run_vcpu (vcpu); + +test_vm_destroy (vm); +} +int main (void ) {pr_info ("SBI PMU basic test : starting\n" );@@ -463,5 +574,8 @@ int main(void)test_vm_events_snapshot_test (test_pmu_events_snaphost);pr_info ("SBI PMU event verification with snapshot test : PASS\n" ); +test_vm_events_overflow (test_pmu_events_overflow); +pr_info ("SBI PMU event verification with overflow test : PASS\n" ); +return 0 ; }Signature · 1 line
2.34.1Other than the command line option idea,
Reviewed-by Andrew Jones <[email protected]>Thanks,
drew