From 9f37e25f8035f05bcadccd17517f89d9e684f8ad Mon Sep 17 00:00:00 2001 From: Pengpeng Hou Date: Thu, 27 Aug 2026 19:57:18 +0800 Subject: [PATCH] lib: sbi: reject PMU skip-match counter class mismatches 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 Tested-by: Sebastian Alba Vives Reviewed-by: Anup Patel Link: https://lore.kernel.org/r/20260827115718.50124-1-pengpeng@iscas.ac.cn Signed-off-by: Anup Patel --- 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 e79f6b60..653b8f69 100644 --- a/lib/sbi/sbi_pmu.c +++ b/lib/sbi/sbi_pmu.c @@ -939,6 +939,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;