[PATCH] lib: sbi_pmu: Allow resetting an already stopped counter

Mayuresh Chitale mayuresh.chitale at oss.qualcomm.com
Thu Oct 29 05:43:07 PDT 2026


Kernel sbi_pmu driver issues a counter stop with SBI_PMU_STOP_FLAG_RESET
while removing a perf event. This is done after the counter is stopped
which causes pmu_ctr_stop_hw() to fail with SBI_EALREADY_STOPPED.

Allow pmu_ctr_stop_hw to complete the requested reset and return
success, while still reporting error in other cases.

Fixes: c5077d497d0f ("lib: sbi_pmu: Fix counter and event info error codes as per SBI v3.0 spec")
Signed-off-by: Mayuresh Chitale <mayuresh.chitale at oss.qualcomm.com>
---
 lib/sbi/sbi_pmu.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/lib/sbi/sbi_pmu.c b/lib/sbi/sbi_pmu.c
index a043dc7c..01c45f7a 100644
--- a/lib/sbi/sbi_pmu.c
+++ b/lib/sbi/sbi_pmu.c
@@ -721,16 +721,21 @@ int sbi_pmu_ctr_stop(unsigned long cbase, unsigned long cmask,
 
 		else if (event_idx_type == SBI_PMU_EVENT_TYPE_FW)
 			ret = pmu_ctr_stop_fw(phs, cidx, event_code);
-		else
+		else {
 			ret = pmu_ctr_stop_hw(cidx);
+			if (ret && ret != SBI_EALREADY_STOPPED)
+				return ret;
 
-		if(ret)
-			return ret;
-
-		if (cidx > (CSR_INSTRET - CSR_CYCLE) && flag & SBI_PMU_STOP_FLAG_RESET) {
-			phs->active_events[cidx] = SBI_PMU_EVENT_IDX_INVALID;
-			pmu_reset_hw_mhpmevent(cidx);
+			if (flag & SBI_PMU_STOP_FLAG_RESET &&
+			    cidx > (CSR_INSTRET - CSR_CYCLE)) {
+				phs->active_events[cidx] = SBI_PMU_EVENT_IDX_INVALID;
+				pmu_reset_hw_mhpmevent(cidx);
+				ret = 0;
+			}
 		}
+
+		if (ret)
+			return ret;
 	}
 
 	/* Clear PMU overflow interrupt to avoid spurious ones */
-- 
2.43.0




More information about the opensbi mailing list