lib: sbi_pmu: propagate hardware counter errors
v2 cover lib: sbi_pmu: propagate hardware counter errors
The two patches Anup asked for in the thread for Pengpeng Hou's SKIP_MATCH class check. v1 is at
https://lore.kernel.org/r/[email protected]
Changes in v2:
- patch 1: propagate SBI_EALREADY_STARTED as well, per Anup's review. An already running counter may have been configured for a different event previously, so the configure must not report success. The commit message was reworked around that and the special case is gone.
- patch 2: unchanged except for the context line that patch 1 touches. Picked up Anup's Reviewed-by.
They stay a series because both touch the same if block in sbi_pmu_ctr_cfg_match() and the second does not apply without the first.
Built clean for rv64 and rv32, PLATFORM=generic, no new warnings.
Sebastian Alba Vives (2):
lib: sbi_pmu: do not ignore pmu_ctr_start_hw() error in cfg_match
lib: sbi_pmu: reject out-of-bounds counter index in pmu_ctr_write_hw()
lib/sbi/sbi_pmu.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)
Signature · 7 lines
2.43.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v2 1/2 lib: sbi_pmu: do not ignore pmu_ctr_start_hw() error in cfg_match
sbi_pmu_ctr_cfg_match() calls pmu_ctr_start_hw() for the AUTO_START flag and drops the result. pmu_ctr_start_hw() carries its own index guard
if (cidx >= num_hw_ctrs || cidx == 1) return SBI_EINVAL;
and can also return SBI_EALREADY_STARTED when the counter is already running. Both are reported to the caller as a successful configure.
Neither is benign. A rejected index means nothing was configured, and an already running counter may have been configured for a different event previously, so the supervisor must not be told its configure succeeded.
This is also the only one of the three start paths in the file that discards the error. sbi_pmu_ctr_start() propagates it:
ret = pmu_ctr_start_hw(cidx, ival, bUpdate); if (ret) return ret;
and the firmware branch directly below this call site propagates the error from pmu_dev->fw_counter_start() as well.
--- lib/sbi/sbi_pmu.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
lib/sbi/sbi_pmu.c +5 -2
@@ -982,8 +982,11 @@ skip_match: event_type == SBI_PMU_EVENT_TYPE_HW_RAW_V2) {if (flags &SBI_PMU_CFG_FLAG_CLEAR_VALUE )pmu_ctr_write_hw (ctr_idx,0 ); -if (flags &SBI_PMU_CFG_FLAG_AUTO_START ) -pmu_ctr_start_hw (ctr_idx,0 , false); +if (flags &SBI_PMU_CFG_FLAG_AUTO_START ) { + ret =pmu_ctr_start_hw (ctr_idx,0 , false); +if (ret) +return ret; + } } else if (event_type == SBI_PMU_EVENT_TYPE_FW) {if (flags &SBI_PMU_CFG_FLAG_CLEAR_VALUE ) phs->fw_counters_data[ctr_idx - num_hw_ctrs] =0 ;
Signature · 7 lines
2.43.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v2 2/2 lib: sbi_pmu: reject out-of-bounds counter index in pmu_ctr_write_hw()
In reply to Sebastian Alba Vivespmu_ctr_write_hw() writes CSR_MCYCLE + cidx with no check on cidx, while its sibling pmu_ctr_start_hw() guards the same index space:
if (cidx >= num_hw_ctrs || cidx == 1) return SBI_EINVAL;
A cidx at or above num_hw_ctrs addresses an mhpmcounter the platform need not implement, and cidx == 1 addresses 0xb01, which is not assigned. Either one traps in M-mode instead of returning an error.
Give the function the same guard and a return value. The guard is copied from pmu_ctr_start_hw() rather than limited to the upper bound, so that the two functions accept exactly the same index space.
Only the call site in sbi_pmu_ctr_cfg_match() propagates the error. The two call sites inside pmu_ctr_start_hw() run after the identical guard in that function, so they cannot fail and are left unchanged.
--- lib/sbi/sbi_pmu.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
lib/sbi/sbi_pmu.c +12 -3
@@ -389,8 +389,12 @@ static int pmu_ctr_enable_irq_hw(int ctr_idx)return 0 ; } -static void pmu_ctr_write_hw (uint32_t cidx,uint64_t ival) +static int pmu_ctr_write_hw (uint32_t cidx,uint64_t ival) { +/* Make sure the counter index lies within the range and is not TM bit */ +if (cidx >= num_hw_ctrs || cidx ==1 ) +return SBI_EINVAL ; +#if __riscv_xlen ==32 csr_write_num (CSR_MCYCLE + cidx,0 );csr_write_num (CSR_MCYCLEH + cidx, ival >>BITS_PER_LONG );
@@ -398,6 +402,8 @@ static void pmu_ctr_write_hw(uint32_t cidx, uint64_t ival)#else csr_write_num (CSR_MCYCLE + cidx, ival);#endif + +return 0 ; }static int pmu_ctr_start_hw (uint32_t cidx,uint64_t ival,bool ival_update)
@@ -980,8 +986,11 @@ skip_match: event_type == SBI_PMU_EVENT_TYPE_HW_CACHE || event_type == SBI_PMU_EVENT_TYPE_HW_RAW || event_type == SBI_PMU_EVENT_TYPE_HW_RAW_V2) { -if (flags &SBI_PMU_CFG_FLAG_CLEAR_VALUE ) -pmu_ctr_write_hw (ctr_idx,0 ); +if (flags &SBI_PMU_CFG_FLAG_CLEAR_VALUE ) { + ret =pmu_ctr_write_hw (ctr_idx,0 ); +if (ret) +return ret; + }if (flags &SBI_PMU_CFG_FLAG_AUTO_START ) { ret =pmu_ctr_start_hw (ctr_idx,0 , false);if (ret)
Signature · 7 lines
2.43.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Anup Patel
AppliedQuoted 14 lines · Sebastian Alba Vives
The two patches Anup asked for in the thread for Pengpeng Hou's
SKIP_MATCHclass check. v1 is athttps://lore.kernel.org/r/[email protected]
Changes in v2:
- patch 1: propagateSBI_EALREADY_STARTEDas well, per Anup's review. An already running counter may have been configured for a different event previously, so the configure must not report success. The commit message was reworked around that and the special case is gone.
- patch 2: unchanged except for the context line that patch 1 touches. Picked up Anup's Reviewed-by.
Quoted 8 lines
They stay a series because both touch the same if block in
sbi_pmu_ctr_cfg_match()and the second does not apply without the first.Built clean for rv64 and rv32, PLATFORM=generic, no new warnings.
Sebastian Alba Vives (2):
lib:sbi_pmu: do not ignorepmu_ctr_start_hw()error incfg_match
lib:sbi_pmu: reject out-of-bounds counter index inpmu_ctr_write_hw()
Applied this series to the riscv/opensbi repo.
Thanks,
Anup
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi