lib/sbi_pmu: Don't fallback to fixed counters when sscofpmf && !smcntrpmf
PATCH 1/1 lib/sbi_pmu: Don't fallback to fixed counters when sscofpmf && !smcntrpmf
Currently when searching for a hardware counter for an event, if no programmable counter is available, the code falls back to using a fixed counter (mcycle/minstret) if one matches the event.
However the fallback is incorrect when sscofpmf is present but smcntrpmf is not. That's because with sscofpmf, programmable counters support mode filtering, but the fixed counters do not (without smcntrpmf). Even if the caller didn't configure mode filtering, by default programmable counters don't count M mode when sscofpmf is present, whereas mcycle/minstret do.
Fix the logic to not fallback to a fixed counter if sscofpmf is present but smcntrpmf is not.
---
Hi folks,
I noticed this while testing Linux perf with more events than programmable counters.
Sometimes cycles gets counted using a mhpmcounter, and sometimes with mcycle, and the cycle count differs between the two.
The count differs because mcycle doesn't support filtering when smcntrpmf is not present.
It can be reproduced by running perf stat and specifying cycles:u N+1 times where N is the number of mhpmcounters, eg:
$ perf stat -e cycles:u,cycles:u,...
...
392860 cycles:u
61101120 cycles:uThe first cycles got an mhpmcounter and only counted userspace cycles. The second used mcycle and counted everything.
cheers
--- lib/sbi/sbi_pmu.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
lib/sbi/sbi_pmu.c +12 -5
@@ -830,13 +830,20 @@ static int pmu_ctr_find_hw(struct sbi_pmu_hart_state *phs,if (ctr_idx ==SBI_ENOTSUPP ) {/** -* We can't find any programmable counters for cycle/instret. -* Return the fixed counter as they are mandatory anyways. +* We can't find a programmable counter, see if we can use a +* fixed counter instead if one was found for this event. +* +* If sscofpmf is present but smcntrpmf is not, we can't +* fallback to a fixed counter, because the fixed counter +* doesn't support filtering whereas a programmable counter +* would. */ -if (fixed_ctr >=0 ) -return pmu_fixed_ctr_update_inhibit_bits (fixed_ctr, flags); -else +if (fixed_ctr <0 || + ((sbi_hart_has_extension (scratch,SBI_HART_EXT_SSCOFPMF ) && + !sbi_hart_has_extension (scratch,SBI_HART_EXT_SMCNTRPMF ))))return SBI_EFAIL ; + +return pmu_fixed_ctr_update_inhibit_bits (fixed_ctr, flags); } ret =pmu_update_hw_mhpmevent (temp, ctr_idx, flags, event_idx, data);
---
base-commit: 4813a2042096b7860655761aad973723293a552e
change-id: 20260324-mcycle-fix-3fc5682eee9c
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Quoted 12 lines · Michael Ellerman
Currently when searching for a hardware counter for an event, if no programmable counter is available, the code falls back to using a fixed counter (mcycle/minstret) if one matches the event.
However the fallback is incorrect when sscofpmf is present but smcntrpmf is not. That's because with sscofpmf, programmable counters support mode filtering, but the fixed counters do not (without smcntrpmf). Even if the caller didn't configure mode filtering, by default programmable counters don't count M mode when sscofpmf is present, whereas mcycle/minstret do.
Quoted 4 lines
Fix the logic to not fallback to a fixed counter if sscofpmf is present but smcntrpmf is not.
Signed-off-by Michael Ellerman <[email protected]>
Fixes tag is missing but I will add this at the time of merging.
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 68 lines
---
Hi folks,I noticed this while testing Linux perf with more events than programmable counters.
Sometimes cycles gets counted using a mhpmcounter, and sometimes with mcycle, and the cycle count differs between the two.
The count differs because mcycle doesn't support filtering when smcntrpmf is not present.
It can be reproduced by running perf stat and specifying
cycles:uN+1 times where N is the number of mhpmcounters, eg:$ perf stat -e cycles:u,cycles:u,... ... 392860 cycles:u 61101120 cycles:uThe first cycles got an mhpmcounter and only counted userspace cycles. The second used mcycle and counted everything.
cheers
--- lib/sbi/sbi_pmu.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)lib/sbi/sbi_pmu.c +12
-6@@ -830,13 +830,20 @@ static int pmu_ctr_find_hw(struct sbi_pmu_hart_state *phs,if (ctr_idx ==SBI_ENOTSUPP ) {/** -* We can't find any programmable counters for cycle/instret. -* Return the fixed counter as they are mandatory anyways. +* We can't find a programmable counter, see if we can use a +* fixed counter instead if one was found for this event. +* +* If sscofpmf is present but smcntrpmf is not, we can't +* fallback to a fixed counter, because the fixed counter +* doesn't support filtering whereas a programmable counter +* would. */ -if (fixed_ctr >=0 ) -return pmu_fixed_ctr_update_inhibit_bits (fixed_ctr, flags); -else +if (fixed_ctr <0 || + ((sbi_hart_has_extension (scratch,SBI_HART_EXT_SSCOFPMF ) && + !sbi_hart_has_extension (scratch,SBI_HART_EXT_SMCNTRPMF ))))return SBI_EFAIL ; + +return pmu_fixed_ctr_update_inhibit_bits (fixed_ctr, flags); } ret =pmu_update_hw_mhpmevent (temp, ctr_idx, flags, event_idx, data); ---base-commit:
4813a2042096b7860655761aad973723293a552e
change-id: 20260324-mcycle-fix-3fc5682eee9c--
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
Quoted 13 lines · Anup Patel
Quoted 12 lines · Michael Ellerman
Currently when searching for a hardware counter for an event, if no programmable counter is available, the code falls back to using a fixed counter (mcycle/minstret) if one matches the event.
However the fallback is incorrect when sscofpmf is present but smcntrpmf is not. That's because with sscofpmf, programmable counters support mode filtering, but the fixed counters do not (without smcntrpmf). Even if the caller didn't configure mode filtering, by default programmable counters don't count M mode when sscofpmf is present, whereas mcycle/minstret do.
Quoted 6 lines
Quoted 4 lines
Fix the logic to not fallback to a fixed counter if sscofpmf is present but smcntrpmf is not.
Signed-off-by Michael Ellerman <[email protected]>Fixes tag is missing but I will add this at the time of merging.
Thanks.
cheers
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi