[PATCH] lib: sbi: reject PMU skip-match counter class mismatches

Pengpeng Hou pengpeng at iscas.ac.cn
Thu Aug 27 04:57:18 PDT 2026


SKIP_MATCH selects the first counter in the caller-provided set, but the
current path checks only that the index exists and is active.  A supervisor
can select an active hardware counter for a firmware event.  CLEAR_VALUE or
AUTO_START then indexes firmware-counter state with
ctr_idx - num_hw_ctrs, which underflows.  The inverse mismatch sends a
firmware-counter index to hardware-counter operations.

Reject the selected counter when its hardware/firmware class cannot monitor
the requested event.  Return SBI_ENOTSUPP, matching the PMU specification's
case where the selected counter cannot monitor that event.

Fixes: 13d40f21d588 ("lib: sbi: Add PMU support")

Signed-off-by: Pengpeng Hou <pengpeng at iscas.ac.cn>
---
Base-commit: 4e79fd7de59f1b2899092c1a84ce68c8ebc68f93

 lib/sbi/sbi_pmu.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/lib/sbi/sbi_pmu.c b/lib/sbi/sbi_pmu.c
index a0f6d2f..040db61 100644
--- a/lib/sbi/sbi_pmu.c
+++ b/lib/sbi/sbi_pmu.c
@@ -926,6 +926,12 @@ int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,
 		if (cidx_first >= total_ctrs)
 			return SBI_EINVAL;
 
+		if ((event_type == SBI_PMU_EVENT_TYPE_FW &&
+		     cidx_first < num_hw_ctrs) ||
+		    (event_type != SBI_PMU_EVENT_TYPE_FW &&
+		     cidx_first >= num_hw_ctrs))
+			return SBI_ENOTSUPP;
+
 		if (phs->active_events[cidx_first] == SBI_PMU_EVENT_IDX_INVALID)
 			return SBI_EINVAL;
 		ctr_idx = cidx_first;
-- 
2.50.1 (Apple Git-155)




More information about the opensbi mailing list