RISC-V SBI v2.0 PMU improvements and Perf sampling in KVM guest
Subsystems (8)
v6 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_v6
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 v5->v6:
1. Added a patch for command line option for the sbi pmu tests.
2. Removed redundant prints and restructure the code little bit.
3. Added a patch for computing the sbi minor version correctly.
4. Addressed all other comments on v5.
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 (24):
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 definitions
RISC-V: KVM: Rename the SBI_STA_SHMEM_DISABLE to a generic name
RISC-V: Use the minor version mask while computing sbi version drivers/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: 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
KVM: riscv: selftests: Add commandline option for SBI PMU test
arch/riscv/include/asm/csr.h | 5 +- arch/riscv/include/asm/kvm_vcpu_pmu.h | 16 +- arch/riscv/include/asm/sbi.h | 38 +- 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 | 6 + 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 | 272 ++++++- 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 | 685 ++++++++++++++++++ tools/testing/selftests/kvm/steal_time.c | 4 +- 23 files changed, 1437 insertions(+), 114 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 v6 1/24 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 v6 01/24] 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 v6 2/24 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
PATCH v6 3/24 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: %ld\n" , + 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
PATCH v6 4/24 drivers/perf: riscv: Use BIT macro for shifting operations
In reply to Atish PatraIt is a good practice to use BIT() instead of (1 << x). Replace the current usages with BIT().
Take this opportunity to replace few (1UL << x) with BIT() as well for consistency.
--- 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
PATCH v6 5/24 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
PATCH v6 6/24 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
PATCH v6 7/24 RISC-V: Use the minor version mask while computing sbi version
In reply to Atish PatraAs per the SBI specification, minor version is encoded in the lower 24 bits only. Make sure that the SBI version is computed with the appropriate mask.
Currently, there is no minor version in use. Thus, it doesn't change anything functionality but it is good to be compliant with the specification.
--- arch/riscv/include/asm/sbi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/include/asm/sbi.h +2 -2
@@ -367,8 +367,8 @@ static inline unsigned long sbi_minor_version(void)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; +return ((major &SBI_SPEC_VERSION_MAJOR_MASK ) <<SBI_SPEC_VERSION_MAJOR_SHIFT + | (minor &SBI_SPEC_VERSION_MINOR_MASK )); }int sbi_err_map_linux_errno (int err);
Signature · 1 line
2.34.1
Re: [PATCH v6 07/24] RISC-V: Use the minor version mask while computing sbi version
Quoted 18 lines · Atish Patra
As per the SBI specification, minor version is encoded in the lower 24 bits only. Make sure that the SBI version is computed with the appropriate mask.
Currently, there is no minor version in use. Thus, it doesn't change anything functionality but it is good to be compliant with the specification.
Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/sbi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/riscv/include/asm/sbi.h +0
-0
arch/riscv/include/asm/sbi.h static inline unsigned long sbi_minor_version(void) · line 371
Quoted 7 lines
arch/riscv/include/asm/sbi.h +2
-2@@ -367,8 +367,8 @@ static inline unsigned long sbi_minor_version(void)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; +return ((major &SBI_SPEC_VERSION_MAJOR_MASK ) <<SBI_SPEC_VERSION_MAJOR_SHIFT + | (minor &SBI_SPEC_VERSION_MINOR_MASK ));
The previous version had ((major & major_mask) << major_shift) | minor (parentheses around all the major bits before the OR). Now we have parentheses around everything, which aren't necessary, and no longer have them around all the major bits before the OR. We don't need the parentheses around the major bits, since shift has higher precedence than OR, but I'd probably keep them.
Otherwise,
Quoted 6 lines
arch/riscv/include/asm/sbi.h +0
-0}int sbi_err_map_linux_errno (int err);Signature · 1 line
2.34.1
Re: [PATCH v6 07/24] RISC-V: Use the minor version mask while computing sbi version
Quoted 14 lines · Andrew Jones
Quoted 13 lines · Atish Patra
As per the SBI specification, minor version is encoded in the lower 24 bits only. Make sure that the SBI version is computed with the appropriate mask.
Currently, there is no minor version in use. Thus, it doesn't change anything functionality but it is good to be compliant with the specification.
Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/sbi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 16 lines
arch/riscv/include/asm/sbi.h static inline unsigned long sbi_minor_version(void) · line 371
Quoted 12 lines
arch/riscv/include/asm/sbi.h +2
-2@@ -367,8 +367,8 @@ static inline unsigned long sbi_minor_version(void)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; +return ((major &SBI_SPEC_VERSION_MAJOR_MASK ) <<SBI_SPEC_VERSION_MAJOR_SHIFT + | (minor &SBI_SPEC_VERSION_MINOR_MASK ));The previous version had ((major &
major_mask) <<major_shift) | minor (parentheses around all the major bits before the OR). Now we have parentheses around everything, which aren't necessary, and no longer
We have to use parentheses around | to avoid compiler warnings (-Wparentheses)
Are you only concerned about the outer parentheses ? I have removed it.
Quoted 4 lines
have them around all the major bits before the OR. We don't need the parentheses around the major bits, since shift has higher precedence than OR, but I'd probably keep them.
Is this what you prefer?
return ((major & SBI_SPEC_VERSION_MAJOR_MASK) << SBI_SPEC_VERSION_MAJOR_SHIFT) | (minor & SBI_SPEC_VERSION_MINOR_MASK);
Quoted 15 lines
Otherwise,
Reviewed-by Andrew Jones <[email protected]>Quoted 6 lines
arch/riscv/include/asm/sbi.h +0
-0}int sbi_err_map_linux_errno (int err);Signature · 1 line
2.34.1_______________________________________________
linux-riscv mailing list
linux-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v6 07/24] RISC-V: Use the minor version mask while computing sbi version
Quoted 42 lines · Atish Patra
Quoted 14 lines · Andrew Jones
Quoted 13 lines · Atish Patra
As per the SBI specification, minor version is encoded in the lower 24 bits only. Make sure that the SBI version is computed with the appropriate mask.
Currently, there is no minor version in use. Thus, it doesn't change anything functionality but it is good to be compliant with the specification.
Signed-off-by Atish Patra <[email protected]>--- arch/riscv/include/asm/sbi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Quoted 16 lines
arch/riscv/include/asm/sbi.h static inline unsigned long sbi_minor_version(void) · line 371
Quoted 12 lines
arch/riscv/include/asm/sbi.h +2
-2@@ -367,8 +367,8 @@ static inline unsigned long sbi_minor_version(void)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; +return ((major &SBI_SPEC_VERSION_MAJOR_MASK ) <<SBI_SPEC_VERSION_MAJOR_SHIFT + | (minor &SBI_SPEC_VERSION_MINOR_MASK ));The previous version had ((major &
major_mask) <<major_shift) | minor (parentheses around all the major bits before the OR). Now we have parentheses around everything, which aren't necessary, and no longerWe have to use parentheses around | to avoid compiler warnings (-Wparentheses)
Are you only concerned about the outer parentheses ? I have removed it.
Quoted 4 lines
have them around all the major bits before the OR. We don't need the parentheses around the major bits, since shift has higher precedence than OR, but I'd probably keep them.
Quoted 4 lines
Is this what you prefer?
return ((major &
SBI_SPEC_VERSION_MAJOR_MASK) <<SBI_SPEC_VERSION_MAJOR_SHIFT) | (minor &SBI_SPEC_VERSION_MINOR_MASK);
Yup
Thanks,
drew
Quoted 19 lines
Quoted 18 lines
Otherwise,
Reviewed-by Andrew Jones <[email protected]>Quoted 5 lines
arch/riscv/include/asm/sbi.h +0
-0}int sbi_err_map_linux_errno (int err);Signature · 1 line
2.34.1_______________________________________________
linux-riscv mailing list
linux-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v6 8/24 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 | 224 +++++++++++++++++++++++++++++++-- include/linux/perf/riscv_pmu.h | 6 + 3 files changed, 219 insertions(+), 12 deletions(-)
drivers/perf/riscv_pmu.c +1 -0
@@ -408,6 +408,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 +212 -12
331 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 v6 08/24] drivers/perf: riscv: Implement SBI PMU snapshot function
Quoted 93 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 | 224 +++++++++++++++++++++++++++++++-- include/linux/perf/riscv_pmu.h | 6 + 3 files changed, 219 insertions(+), 12 deletions(-)drivers/perf/riscv_pmu.c +1
-0@@ -408,6 +408,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 +38
-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,109 @@ 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); + }
drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig) · line 558
Quoted 10 lines
drivers/perf/riscv_pmu_sbi.c +10
-0+ +return 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 SBI_SHMEM_DISABLE patch got moved in front of this patch, but looks like it was forgotten to apply it.
Otherwise,
Thanks,
drew
Re: [PATCH v6 08/24] drivers/perf: riscv: Implement SBI PMU snapshot function
Quoted 99 lines · Andrew Jones
Quoted 98 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 | 224 +++++++++++++++++++++++++++++++-- include/linux/perf/riscv_pmu.h | 6 + 3 files changed, 219 insertions(+), 12 deletions(-)drivers/perf/riscv_pmu.c +1
-0@@ -408,6 +408,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 +43
-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,109 @@ 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 )
Quoted 9 lines
drivers/perf/riscv_pmu_sbi.c static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig) · line 558
Quoted 5 lines
drivers/perf/riscv_pmu_sbi.c +5
-0+{ +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_PMU ,SBI_EXT_PMU_SNAPSHOT_SET_SHMEM ,-1 , +-1 ,0 ,0 ,0 ,0 );The
SBI_SHMEM_DISABLEpatch got moved in front of this patch, but looks like it was forgotten to apply it.
Oops. My bad. Fixed it.
Quoted 11 lines
Otherwise,
Reviewed-by Andrew Jones <[email protected]>Thanks,
drew_______________________________________________
linux-riscv mailing list
linux-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v6 9/24 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
@@ -772,13 +772,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 ); }/*
@@ -790,7 +792,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 ;
@@ -798,11 +800,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 v6 10/24 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
PATCH v6 11/24 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 v6 12/24 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
PATCH v6 13/24 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 v6 13/24] RISC-V: KVM: Implement SBI PMU Snapshot feature
Quoted 8 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.
Quoted 8 lines
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(-)
PATCH v6 14/24 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 | 6 +++ arch/riscv/kvm/vcpu_pmu.c | 68 +++++++++++++++++++++++++-- 7 files changed, 93 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 +6 -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),
@@ -99,6 +100,9 @@ static bool kvm_riscv_vcpu_isa_enable_allowed(unsigned long ext)switch (ext) {case KVM_RISCV_ISA_EXT_H :return false; +case KVM_RISCV_ISA_EXT_SSCOFPMF : +/* Sscofpmf depends on interrupt filtering defined in ssaia */ +return __riscv_isa_extension_available (NULL ,RISCV_ISA_EXT_SSAIA );case KVM_RISCV_ISA_EXT_V :return riscv_v_vstate_ctrl_user_allowed ();default :
@@ -116,6 +120,8 @@ 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 : +/* There is not architectural config bit to disable sscofpmf completely */ +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 v6 14/24] RISC-V: KVM: Add perf sampling support for guests
Quoted 13 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]>
Quoted 10 lines
--- 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 | 6 +++ arch/riscv/kvm/vcpu_pmu.c | 68 +++++++++++++++++++++++++-- 7 files changed, 93 insertions(+), 8 deletions(-)
PATCH v6 15/24 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
PATCH v6 16/24 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
PATCH v6 17/24 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
PATCH v6 18/24 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
PATCH v6 19/24 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 v6 20/24 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
PATCH v6 21/24 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 | 369 ++++++++++++++++++ 2 files changed, 370 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 +369 -0
369 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v6 21/24] KVM: riscv: selftests: Add SBI PMU selftest
Quoted 11 lines · Atish Patra
This test implements basic sanity test and cycle/instret event counting tests.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/riscv/sbi_pmu_test.c | 369 ++++++++++++++++++ 2 files changed, 370 insertions(+) create mode 100644 tools/testing/selftests/kvm/riscv/sbi_pmu_test.c
PATCH v6 22/24 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 | 144 ++++++++++++++++++ 3 files changed, 181 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 &SBI_SPEC_VERSION_MINOR_MASK )); +} + +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 +144 -0
179 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v6 22/24] KVM: riscv: selftests: Add a test for PMU snapshot functionality
Quoted 54 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 Andrew Jones <[email protected]>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 | 144 ++++++++++++++++++ 3 files changed, 181 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 &SBI_SPEC_VERSION_MINOR_MASK ));
Same parentheses comment as the other patch.
Thanks,
drew
PATCH v6 23/24 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 | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+)
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +113 -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;
@@ -120,6 +124,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)
@@ -318,6 +347,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;
@@ -413,6 +469,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;
@@ -498,6 +582,32 @@ 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 ) {test_vm_basic_test (test_pmu_basic_sanity);
@@ -509,5 +619,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 v6 23/24] KVM: riscv: selftests: Add a test for counter overflow
Quoted 7 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 6 lines
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- .../selftests/kvm/riscv/sbi_pmu_test.c | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+)
PATCH v6 24/24 KVM: riscv: selftests: Add commandline option for SBI PMU test
In reply to Atish PatraSBI PMU test comprises of multiple tests and user may want to run only a subset depending on the platform. The most common case would be to run all to validate all the tests. However, some platform may not support all events or all ISA extensions.
The commandline option allows user to disable particular test if they want to.
--- .../selftests/kvm/riscv/sbi_pmu_test.c | 77 ++++++++++++++++--- 1 file changed, 68 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +68 -9
@@ -33,6 +33,16 @@ static unsigned long counter_mask_available;static bool illegal_handler_invoked; +enum sbi_pmu_test_id { +SBI_PMU_TEST_BASIC =0 , +SBI_PMU_TEST_EVENTS , +SBI_PMU_TEST_SNAPSHOT , +SBI_PMU_TEST_OVERFLOW , +SBI_PMU_TEST_MAX , +}; + +static int disabled_test_id =SBI_PMU_TEST_MAX ; +unsigned long pmu_csr_read_num (int csr_num) {#define switchcase_csr_read (__csr_num, __val) {\
@@ -608,19 +618,68 @@ static void test_vm_events_overflow(void *guest_code)test_vm_destroy (vm); } -int main (void ) +static void test_print_help (char *name) { -test_vm_basic_test (test_pmu_basic_sanity); -pr_info ("SBI PMU basic test : PASS\n" ); +pr_info ("Usage: %s [-h] [-d <test name>]\n" , name); +pr_info ("\t-d: Test to disable. Available tests are 'basic', 'events', 'snapshot', 'overflow'\n" ); +pr_info ("\t-h: print this help screen\n" ); +} -test_vm_events_test (test_pmu_events); -pr_info ("SBI PMU event verification test : PASS\n" ); +static bool parse_args (int argc,char *argv[]) +{ +int opt; + +while ((opt =getopt (argc, argv,"hd:" )) !=-1 ) { +switch (opt) { +case 'd' : +if (!strncmp ("basic" , optarg,5 )) + disabled_test_id =SBI_PMU_TEST_BASIC ; +else if (!strncmp ("events" , optarg,6 )) + disabled_test_id =SBI_PMU_TEST_EVENTS ; +else if (!strncmp ("snapshot" , optarg,8 )) + disabled_test_id =SBI_PMU_TEST_SNAPSHOT ; +else if (!strncmp ("overflow" , optarg,8 )) + disabled_test_id =SBI_PMU_TEST_OVERFLOW ; +else + goto done; +break ; +break ; +case 'h' : +default : + goto done; + } + } -test_vm_events_snapshot_test (test_pmu_events_snaphost); -pr_info ("SBI PMU event verification with snapshot test : PASS\n" ); +return true; +done: +test_print_help (argv[0 ]); +return false; +} -test_vm_events_overflow (test_pmu_events_overflow); -pr_info ("SBI PMU event verification with overflow test : PASS\n" ); +int main (int argc,char *argv[]) +{ +if (!parse_args (argc, argv)) +exit (KSFT_SKIP ); + +if (disabled_test_id !=SBI_PMU_TEST_BASIC ) { +test_vm_basic_test (test_pmu_basic_sanity); +pr_info ("SBI PMU basic test : PASS\n" ); + } + +if (disabled_test_id !=SBI_PMU_TEST_EVENTS ) { +test_vm_events_test (test_pmu_events); +pr_info ("SBI PMU event verification test : PASS\n" ); + } + +if (disabled_test_id !=SBI_PMU_TEST_SNAPSHOT ) { +test_vm_events_snapshot_test (test_pmu_events_snaphost); +pr_info ("SBI PMU event verification with snapshot test : PASS\n" ); + } + +if (disabled_test_id !=SBI_PMU_TEST_OVERFLOW ) { +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 v6 24/24] KVM: riscv: selftests: Add commandline option for SBI PMU test
Quoted 21 lines · Atish Patra
SBI PMU test comprises of multiple tests and user may want to run only a subset depending on the platform. The most common case would be to run all to validate all the tests. However, some platform may not support all events or all ISA extensions.
The commandline option allows user to disable particular test if they want to.
Suggested-by Andrew Jones <[email protected]>Signed-off-by Atish Patra <[email protected]>--- .../selftests/kvm/riscv/sbi_pmu_test.c | 77 ++++++++++++++++--- 1 file changed, 68 insertions(+), 9 deletions(-)tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +0
-0@@ -33,6 +33,16 @@ static unsigned long counter_mask_available;static bool illegal_handler_invoked;
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c static unsigned long counter_mask_available; · line 44
Quoted 10 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +9
-0+enum sbi_pmu_test_id { +SBI_PMU_TEST_BASIC =0 , +SBI_PMU_TEST_EVENTS , +SBI_PMU_TEST_SNAPSHOT , +SBI_PMU_TEST_OVERFLOW , +SBI_PMU_TEST_MAX , +}; + +static int disabled_test_id =SBI_PMU_TEST_MAX ;
I think we should allow specifying '-d' multiple times in case a user wants to disable more than one type of test. So we should set a bit in this variable instead of assigning it. We could also flip it around. If the user uses '-e' to enable test type then only type will be run, but then we'd probably want to allow multiple '-e' too so it doesn't gain anything.
Quoted 29 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +15
-5+unsigned long pmu_csr_read_num (int csr_num) {#define switchcase_csr_read (__csr_num, __val) {\@@ -608,19 +618,68 @@ static void test_vm_events_overflow(void *guest_code)test_vm_destroy (vm); } -int main (void ) +static void test_print_help (char *name) { -test_vm_basic_test (test_pmu_basic_sanity); -pr_info ("SBI PMU basic test : PASS\n" ); +pr_info ("Usage: %s [-h] [-d <test name>]\n" , name); +pr_info ("\t-d: Test to disable. Available tests are 'basic', 'events', 'snapshot', 'overflow'\n" ); +pr_info ("\t-h: print this help screen\n" ); +} -test_vm_events_test (test_pmu_events); -pr_info ("SBI PMU event verification test : PASS\n" ); +static bool parse_args (int argc,char *argv[]) +{ +int opt; + +while ((opt =getopt (argc, argv,"hd:" )) !=-1 ) { +switch (opt) { +case 'd' : +if (!strncmp ("basic" , optarg,5 )) + disabled_test_id =SBI_PMU_TEST_BASIC ;
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c static void test_vm_events_overflow(void *guest_code) · line 646
Quoted 10 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +10
-0+else if (!strncmp ("events" , optarg,6 )) + disabled_test_id =SBI_PMU_TEST_EVENTS ; +else if (!strncmp ("snapshot" , optarg,8 )) + disabled_test_id =SBI_PMU_TEST_SNAPSHOT ; +else if (!strncmp ("overflow" , optarg,8 )) + disabled_test_id =SBI_PMU_TEST_OVERFLOW ; +else + goto done; +break ; +break ;
Extra 'break'
Quoted 46 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +34
-4+case 'h' : +default : + goto done; + } + } -test_vm_events_snapshot_test (test_pmu_events_snaphost); -pr_info ("SBI PMU event verification with snapshot test : PASS\n" ); +return true; +done: +test_print_help (argv[0 ]); +return false; +} -test_vm_events_overflow (test_pmu_events_overflow); -pr_info ("SBI PMU event verification with overflow test : PASS\n" ); +int main (int argc,char *argv[]) +{ +if (!parse_args (argc, argv)) +exit (KSFT_SKIP ); + +if (disabled_test_id !=SBI_PMU_TEST_BASIC ) { +test_vm_basic_test (test_pmu_basic_sanity); +pr_info ("SBI PMU basic test : PASS\n" ); + } + +if (disabled_test_id !=SBI_PMU_TEST_EVENTS ) { +test_vm_events_test (test_pmu_events); +pr_info ("SBI PMU event verification test : PASS\n" ); + } + +if (disabled_test_id !=SBI_PMU_TEST_SNAPSHOT ) { +test_vm_events_snapshot_test (test_pmu_events_snaphost); +pr_info ("SBI PMU event verification with snapshot test : PASS\n" ); + } + +if (disabled_test_id !=SBI_PMU_TEST_OVERFLOW ) { +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
Thanks,
drew
Re: [PATCH v6 24/24] KVM: riscv: selftests: Add commandline option for SBI PMU test
Quoted 23 lines · Andrew Jones
Quoted 22 lines · Atish Patra
SBI PMU test comprises of multiple tests and user may want to run only a subset depending on the platform. The most common case would be to run all to validate all the tests. However, some platform may not support all events or all ISA extensions.
The commandline option allows user to disable particular test if they want to.
Suggested-by Andrew Jones <[email protected]>Signed-off-by Atish Patra <[email protected]>--- .../selftests/kvm/riscv/sbi_pmu_test.c | 77 ++++++++++++++++--- 1 file changed, 68 insertions(+), 9 deletions(-)tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +0
-0@@ -33,6 +33,16 @@ static unsigned long counter_mask_available;static bool illegal_handler_invoked;
Quoted 17 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c static unsigned long counter_mask_available; · line 44
Quoted 9 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +9
-0+enum sbi_pmu_test_id { +SBI_PMU_TEST_BASIC =0 , +SBI_PMU_TEST_EVENTS , +SBI_PMU_TEST_SNAPSHOT , +SBI_PMU_TEST_OVERFLOW , +SBI_PMU_TEST_MAX , +}; + +static int disabled_test_id =SBI_PMU_TEST_MAX ;I think we should allow specifying '-d' multiple times in case a user wants to disable more than one type of test. So we should set a bit in this variable instead of assigning it. We could also flip it around. If the user uses '-e' to enable test type then only type will be run, but then we'd probably want to allow multiple '-e' too so it doesn't gain anything.
Sure. Added multiple disable option.
Quoted 33 lines
Quoted 33 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +19
-5+unsigned long pmu_csr_read_num (int csr_num) {#define switchcase_csr_read (__csr_num, __val) {\@@ -608,19 +618,68 @@ static void test_vm_events_overflow(void *guest_code)test_vm_destroy (vm); } -int main (void ) +static void test_print_help (char *name) { -test_vm_basic_test (test_pmu_basic_sanity); -pr_info ("SBI PMU basic test : PASS\n" ); +pr_info ("Usage: %s [-h] [-d <test name>]\n" , name); +pr_info ("\t-d: Test to disable. Available tests are 'basic', 'events', 'snapshot', 'overflow'\n" ); +pr_info ("\t-h: print this help screen\n" ); +} -test_vm_events_test (test_pmu_events); -pr_info ("SBI PMU event verification test : PASS\n" ); +static bool parse_args (int argc,char *argv[]) +{ +int opt; + +while ((opt =getopt (argc, argv,"hd:" )) !=-1 ) { +switch (opt) { +case 'd' : +if (!strncmp ("basic" , optarg,5 )) + disabled_test_id =SBI_PMU_TEST_BASIC ; +else if (!strncmp ("events" , optarg,6 )) + disabled_test_id =SBI_PMU_TEST_EVENTS ; +else if (!strncmp ("snapshot" , optarg,8 )) + disabled_test_id =SBI_PMU_TEST_SNAPSHOT ;
Quoted 9 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c static void test_vm_events_overflow(void *guest_code) · line 646
Quoted 6 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +6
-0+else if (!strncmp ("overflow" , optarg,8 )) + disabled_test_id =SBI_PMU_TEST_OVERFLOW ; +else + goto done; +break ; +break ;Extra 'break'
Fixed. Thanks for catching this.
Quoted 54 lines
Quoted 46 lines
tools/testing/selftests/kvm/riscv/sbi_pmu_test.c +34
-4+case 'h' : +default : + goto done; + } + } -test_vm_events_snapshot_test (test_pmu_events_snaphost); -pr_info ("SBI PMU event verification with snapshot test : PASS\n" ); +return true; +done: +test_print_help (argv[0 ]); +return false; +} -test_vm_events_overflow (test_pmu_events_overflow); -pr_info ("SBI PMU event verification with overflow test : PASS\n" ); +int main (int argc,char *argv[]) +{ +if (!parse_args (argc, argv)) +exit (KSFT_SKIP ); + +if (disabled_test_id !=SBI_PMU_TEST_BASIC ) { +test_vm_basic_test (test_pmu_basic_sanity); +pr_info ("SBI PMU basic test : PASS\n" ); + } + +if (disabled_test_id !=SBI_PMU_TEST_EVENTS ) { +test_vm_events_test (test_pmu_events); +pr_info ("SBI PMU event verification test : PASS\n" ); + } + +if (disabled_test_id !=SBI_PMU_TEST_SNAPSHOT ) { +test_vm_events_snapshot_test (test_pmu_events_snaphost); +pr_info ("SBI PMU event verification with snapshot test : PASS\n" ); + } + +if (disabled_test_id !=SBI_PMU_TEST_OVERFLOW ) { +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.1Thanks,
drew_______________________________________________
linux-riscv mailing list
linux-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv