lib: sbi_pmu: propagate hardware counter errors
cover lib: sbi_pmu: propagate hardware counter errors
Anup asked for these two as separate patches in the thread for Pengpeng Hou's SKIP_MATCH class check:
https://lore.kernel.org/r/[email protected]
They go as a series rather than two independent patches because both touch the same if block in sbi_pmu_ctr_cfg_match(), and the second does not apply without the first.
Both are hardening of a latent defect rather than a fix for a reachable one. Since 9f37e25f landed I could not find a route that gets a bad counter index into either call site on current master; details are in the note under patch 2. The Fixes tag points at the commit that introduced the missing checks.
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 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;
so a rejected index is reported to the caller as a successful configure.
This is 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.
Propagate the error, except SBI_EALREADY_STARTED. That value only says the mcountinhibit bit was already clear, which is a benign condition for a configure call, and failing the whole call because the counter was already running would be a behaviour change for existing supervisors.
---
Two things I kept out of the commit message because they are design questions rather than part of the fix.
I filtered SBI_EALREADY_STARTED on the assumption that a configure call should not fail just because the counter was already running. If you would rather have every value propagated, the change is to drop the second half of the condition.
The other is partial success. By the time this call site runs, phs->active_events[ctr_idx] has already been written, so returning an error leaves the counter configured while reporting a failed configure to the supervisor. I did not add a rollback because that goes beyond what you asked for, but I can send that instead if you would rather the call be all-or-nothing.
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 && ret !=SBI_EALREADY_STARTED ) +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
Re: [PATCH 1/2] lib: sbi_pmu: do not ignore pmu_ctr_start_hw() error in cfg_match
Quoted 18 lines · Sebastian Alba Vives
sbi_pmu_ctr_cfg_match()callspmu_ctr_start_hw()for theAUTO_STARTflag and drops the result.pmu_ctr_start_hw()carries its own index guardif (cidx >= num_hw_ctrs || cidx == 1) return SBI_EINVAL;so a rejected index is reported to the caller as a successful configure.
This is 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;
Quoted 8 lines
and the firmware branch directly below this call site propagates the error from
pmu_dev->fw_counter_start()as well.Propagate the error, except
SBI_EALREADY_STARTED. That value only says the mcountinhibit bit was already clear, which is a benign condition for a configure call, and failing the whole call because the counter was already running would be a behaviour change for existing supervisors.
On the contrary, the configure should fail if the counter was already running because the counter might have been configured for a different event previously.
Otherwise, this looks good.
Regards,
Anup
Quoted 49 lines
Fixes 13d40f21d588 ("lib: sbi: Add PMU support")Signed-off-by Sebastian Alba Vives <[email protected]>---
Two things I kept out of the commit message because they are design questions rather than part of the fix.
I filtered
SBI_EALREADY_STARTEDon the assumption that a configure call should not fail just because the counter was already running. If you would rather have every value propagated, the change is to drop the second half of the condition.The other is partial success. By the time this call site runs, phs->active_events[ctr_idx] has already been written, so returning an error leaves the counter configured while reporting a failed configure to the supervisor. I did not add a rollback because that goes beyond what you asked for, but I can send that instead if you would rather the call be all-or-nothing.
lib/sbi/sbi_pmu.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)lib/sbi/sbi_pmu.c +5
-3@@ -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 && ret !=SBI_EALREADY_STARTED ) +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 ; --2.43.0
--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
PATCH 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.
---
A note on reachability, so the Fixes tag is not read as more than it is.
Since 9f37e25f ("lib: sbi: reject PMU skip-match counter class mismatches") the SKIP_MATCH path rejects a hardware event on a firmware counter. Index 1 cannot reach this call site either:
active_events[1] is initialised to SBI_PMU_EVENT_IDX_INVALID and pmu_ctr_find_hw() masks out the fixed counters with ~SBI_PMU_FIXED_CTR_MASK. I could not find a route that gets a bad index into pmu_ctr_write_hw() on current master, so this is hardening of a latent defect, not a live one.
I included cidx == 1 to match pmu_ctr_start_hw() exactly, which is slightly more than the out-of-bounds check you asked for. If you would rather the two functions guard different index spaces, I will resend with only the upper bound.
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_MCYCLE + cidx, ival &0xFFFFFFFF );
@@ -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 && ret !=SBI_EALREADY_STARTED )
Signature · 7 lines
2.43.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [PATCH 2/2] lib: sbi_pmu: reject out-of-bounds counter index in pmu_ctr_write_hw()
Quoted 15 lines · Sebastian Alba Vives
pmu_ctr_write_hw()writesCSR_MCYCLE+ cidx with no check on cidx, while its siblingpmu_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_ctrsaddresses an mhpmcounter the platform need not implement, and cidx == 1 addresses0xb01, 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.
Quoted 6 lines
Only the call site in
sbi_pmu_ctr_cfg_match()propagates the error. The two call sites insidepmu_ctr_start_hw()run after the identical guard in that function, so they cannot fail and are left unchanged.Fixes 13d40f21d588 ("lib: sbi: Add PMU support")Signed-off-by Sebastian Alba Vives <[email protected]>
LGTM.
Regards,
Anup
Quoted 71 lines
---
A note on reachability, so the Fixes tag is not read as more than it is.
Since
9f37e25f("lib: sbi: reject PMU skip-match counter class mismatches") theSKIP_MATCHpath rejects a hardware event on a firmware counter. Index 1 cannot reach this call site either:
active_events[1] is initialised toSBI_PMU_EVENT_IDX_INVALIDandpmu_ctr_find_hw()masks out the fixed counters with ~SBI_PMU_FIXED_CTR_MASK. I could not find a route that gets a bad index intopmu_ctr_write_hw()on current master, so this is hardening of a latent defect, not a live one.I included cidx == 1 to match
pmu_ctr_start_hw()exactly, which is slightly more than the out-of-bounds check you asked for. If you would rather the two functions guard different index spaces, I will resend with only the upper bound.lib/sbi/sbi_pmu.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)lib/sbi/sbi_pmu.c +12
-4@@ -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_MCYCLE + cidx, ival &0xFFFFFFFF );@@ -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 && ret !=SBI_EALREADY_STARTED ) --2.43.0
--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi