Skip to contentSkip to conversation

Miscellaneous PMU fixes

PATCH New Atish Patra 3 participants 10 messages Jul 20, 2022 – Jul 30, 2022

lore
Share & tools
lore
#0 lore raw

v3 cover Miscellaneous PMU fixes

This series have a set of smaller PMU related fixes/improvements. Most of the fixes are around possible buffer overrun errors in case of corrupt supervisor software.

Changes from v2->v3:
1. Improved patch 2.
2. Added RB tags.

Changes from v1->v2:
1. Split patch 1 to two patches with further improvements.
2. Added a fix for the issue pointed by Andrew.

Atish Patra (4):
lib: sbi: Fix counter index sanity check
lib: sbi: Add the bound check for events during config match
lib: sbi: Fix possible buffer overrun in counter validation
lib: sbi: Fix fw_event_map initialization

lib/sbi/sbi_pmu.c | 87 +++++++++++++++++++++++++++++++++++------------

1 file changed, 65 insertions(+), 22 deletions(-)

--
2.25.1

#1 lore raw

PATCH v3 1/4 lib: sbi: Fix counter index sanity check

The current implementation computes the possible counter range by doing a left shift of counter base. However, this may overflow depending on the counter base value. In case of overflow, the highest counter id may be computed incorrectly. As per the SBI specification, the respective function should return an error if any of the counter is not valid.

Fix the counter index check by avoiding left shifting while doing the sanity checks. Without the shift, the implementation just iterates over the counter mask and computes the correct counter index by adding the base to it.

---
 lib/sbi/sbi_pmu.c | 33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)
lib/sbi/sbi_pmu.c +17 -16
index 3f5fd1031b25..31631a2fab80 100644
@@ -343,25 +343,26 @@ int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask,
 {
 	int event_idx_type;
 	uint32_t event_code;
-	unsigned long ctr_mask = cmask << cbase;
 	int ret = SBI_EINVAL;
 	bool bUpdate = FALSE;
+	int i, cidx;
 
-	if (sbi_fls(ctr_mask) >= total_ctrs)
+	if ((cbase + sbi_fls(cmask)) >= total_ctrs)
 		return ret;
 
 	if (flags & SBI_PMU_START_FLAG_SET_INIT_VALUE)
 		bUpdate = TRUE;
 
-	for_each_set_bit_from(cbase, &ctr_mask, total_ctrs) {
-		event_idx_type = pmu_ctr_validate(cbase, &event_code);
+	for_each_set_bit(i, &cmask, total_ctrs) {
+		cidx = i + cbase;
+		event_idx_type = pmu_ctr_validate(cidx, &event_code);
 		if (event_idx_type < 0)
 			/* Continue the start operation for other counters */
 			continue;
 		else if (event_idx_type == SBI_PMU_EVENT_TYPE_FW)
-			ret = pmu_ctr_start_fw(cbase, event_code, ival, bUpdate);
+			ret = pmu_ctr_start_fw(cidx, event_code, ival, bUpdate);
 		else
-			ret = pmu_ctr_start_hw(cbase, ival, bUpdate);
+			ret = pmu_ctr_start_hw(cidx, ival, bUpdate);
 	}
 
 	return ret;
@@ -421,25 +422,26 @@ int sbi_pmu_ctr_stop(unsigned long cbase, unsigned long cmask,
 	int ret = SBI_EINVAL;
 	int event_idx_type;
 	uint32_t event_code;
-	unsigned long ctr_mask = cmask << cbase;
+	int i, cidx;
 
-	if (sbi_fls(ctr_mask) >= total_ctrs)
+	if ((cbase + sbi_fls(cmask)) >= total_ctrs)
 		return SBI_EINVAL;
 
-	for_each_set_bit_from(cbase, &ctr_mask, total_ctrs) {
-		event_idx_type = pmu_ctr_validate(cbase, &event_code);
+	for_each_set_bit(i, &cmask, total_ctrs) {
+		cidx = i + cbase;
+		event_idx_type = pmu_ctr_validate(cidx, &event_code);
 		if (event_idx_type < 0)
 			/* Continue the stop operation for other counters */
 			continue;
 
 		else if (event_idx_type == SBI_PMU_EVENT_TYPE_FW)
-			ret = pmu_ctr_stop_fw(cbase, event_code);
+			ret = pmu_ctr_stop_fw(cidx, event_code);
 		else
-			ret = pmu_ctr_stop_hw(cbase);
+			ret = pmu_ctr_stop_hw(cidx);
 
 		if (flag & SBI_PMU_STOP_FLAG_RESET) {
-			active_events[hartid][cbase] = SBI_PMU_EVENT_IDX_INVALID;
-			pmu_reset_hw_mhpmevent(cbase);
+			active_events[hartid][cidx] = SBI_PMU_EVENT_IDX_INVALID;
+			pmu_reset_hw_mhpmevent(cidx);
 		}
 	}
 
@@ -615,10 +617,9 @@ int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,
 	int event_type = get_cidx_type(event_idx);
 	struct sbi_pmu_fw_event *fevent;
 	uint32_t fw_evt_code;
-	unsigned long tmp = cidx_mask << cidx_base;
 
 	/* Do a basic sanity check of counter base & mask */
-	if (sbi_fls(tmp) >= total_ctrs || event_type >= SBI_PMU_EVENT_TYPE_MAX)
+	if ((cidx_base + sbi_fls(cidx_mask)) >= total_ctrs || event_type >= SBI_PMU_EVENT_TYPE_MAX)
 		return SBI_EINVAL;
 
 	if (flags & SBI_PMU_CFG_FLAG_SKIP_MATCH) {
Signature · 1 line
2.25.1
Applied
#2 lore raw

PATCH v3 1/4 lib: sbi: Fix counter index sanity check

Quoted 7 lines · Atish Patra

The current implementation computes the possible counter range by doing a left shift of counter base. However, this may overflow depending on the counter base value. In case of overflow, the highest counter id may be computed incorrectly. As per the SBI specification, the respective function should return an error if any of the counter is not valid.

Quoted 7 lines

Fix the counter index check by avoiding left shifting while doing the sanity checks. Without the shift, the implementation just iterates over the counter mask and computes the correct counter index by adding the base to it.

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 91 lines
---
 lib/sbi/sbi_pmu.c | 33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)
lib/sbi/sbi_pmu.c +17 -17
index 3f5fd1031b25..31631a2fab80 100644
@@ -343,25 +343,26 @@ int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask,
 {
        int event_idx_type;
        uint32_t event_code;
-       unsigned long ctr_mask = cmask << cbase;
        int ret = SBI_EINVAL;
        bool bUpdate = FALSE;
+       int i, cidx;
 
-       if (sbi_fls(ctr_mask) >= total_ctrs)
+       if ((cbase + sbi_fls(cmask)) >= total_ctrs)
                return ret;
 
        if (flags & SBI_PMU_START_FLAG_SET_INIT_VALUE)
                bUpdate = TRUE;
 
-       for_each_set_bit_from(cbase, &ctr_mask, total_ctrs) {
-               event_idx_type = pmu_ctr_validate(cbase, &event_code);
+       for_each_set_bit(i, &cmask, total_ctrs) {
+               cidx = i + cbase;
+               event_idx_type = pmu_ctr_validate(cidx, &event_code);
                if (event_idx_type < 0)
                        /* Continue the start operation for other counters */
                        continue;
                else if (event_idx_type == SBI_PMU_EVENT_TYPE_FW)
-                       ret = pmu_ctr_start_fw(cbase, event_code, ival, bUpdate);
+                       ret = pmu_ctr_start_fw(cidx, event_code, ival, bUpdate);
                else
-                       ret = pmu_ctr_start_hw(cbase, ival, bUpdate);
+                       ret = pmu_ctr_start_hw(cidx, ival, bUpdate);
        }
 
        return ret;
@@ -421,25 +422,26 @@ int sbi_pmu_ctr_stop(unsigned long cbase, unsigned long cmask,
        int ret = SBI_EINVAL;
        int event_idx_type;
        uint32_t event_code;
-       unsigned long ctr_mask = cmask << cbase;
+       int i, cidx;
 
-       if (sbi_fls(ctr_mask) >= total_ctrs)
+       if ((cbase + sbi_fls(cmask)) >= total_ctrs)
                return SBI_EINVAL;
 
-       for_each_set_bit_from(cbase, &ctr_mask, total_ctrs) {
-               event_idx_type = pmu_ctr_validate(cbase, &event_code);
+       for_each_set_bit(i, &cmask, total_ctrs) {
+               cidx = i + cbase;
+               event_idx_type = pmu_ctr_validate(cidx, &event_code);
                if (event_idx_type < 0)
                        /* Continue the stop operation for other counters */
                        continue;
 
                else if (event_idx_type == SBI_PMU_EVENT_TYPE_FW)
-                       ret = pmu_ctr_stop_fw(cbase, event_code);
+                       ret = pmu_ctr_stop_fw(cidx, event_code);
                else
-                       ret = pmu_ctr_stop_hw(cbase);
+                       ret = pmu_ctr_stop_hw(cidx);
 
                if (flag & SBI_PMU_STOP_FLAG_RESET) {
-                       active_events[hartid][cbase] = SBI_PMU_EVENT_IDX_INVALID;
-                       pmu_reset_hw_mhpmevent(cbase);
+                       active_events[hartid][cidx] = SBI_PMU_EVENT_IDX_INVALID;
+                       pmu_reset_hw_mhpmevent(cidx);
                }
        }
 
@@ -615,10 +617,9 @@ int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,
        int event_type = get_cidx_type(event_idx);
        struct sbi_pmu_fw_event *fevent;
        uint32_t fw_evt_code;
-       unsigned long tmp = cidx_mask << cidx_base;
 
        /* Do a basic sanity check of counter base & mask */
-       if (sbi_fls(tmp) >= total_ctrs || event_type >= SBI_PMU_EVENT_TYPE_MAX)
+       if ((cidx_base + sbi_fls(cidx_mask)) >= total_ctrs || event_type >= SBI_PMU_EVENT_TYPE_MAX)
                return SBI_EINVAL;
 
        if (flags & SBI_PMU_CFG_FLAG_SKIP_MATCH) {
--

2.25.1

#3 lore raw

PATCH v3 2/4 lib: sbi: Add the bound check for events during config match

In reply to Atish Patra

Currently, there is no sanity check for firmware event code. We don't see any issue as Linux kernel driver does a bound check on firmware events already. However, OpenSBI can't assume sane supervisor mode software always. Thus, an invalid event idx can cause a buffer overflow error. For hardware events, the match will fail for invalid event code anyways. However, a search is unecessary if event code is invalid.

Add a event ID validation function to solve the issue.

---
 lib/sbi/sbi_pmu.c | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
lib/sbi/sbi_pmu.c +44 -2
index 31631a2fab80..bd1ef8665a70 100644
@@ -107,6 +107,44 @@ static bool pmu_event_select_overlap(struct sbi_pmu_hw_event *evt,
 	return FALSE;
 }
 
+static int pmu_event_validate(unsigned long event_idx)
+{
+	uint32_t event_idx_type = get_cidx_type(event_idx);
+	uint32_t event_idx_code = get_cidx_code(event_idx);
+	uint32_t event_idx_code_max = -1;
+	uint32_t cache_ops_result, cache_ops_id, cache_id;
+
+	switch(event_idx_type) {
+	case SBI_PMU_EVENT_TYPE_HW:
+		event_idx_code_max = SBI_PMU_HW_GENERAL_MAX;
+		break;
+	case SBI_PMU_EVENT_TYPE_FW:
+		event_idx_code_max = SBI_PMU_FW_MAX;
+		break;
+	case SBI_PMU_EVENT_TYPE_HW_CACHE:
+		cache_ops_result = event_idx_code & 0x1;
+		cache_ops_id = (event_idx_code & 0x06) >> 1;
+		cache_id = (event_idx_code & 0xfff8) >> 3;
+		if ((cache_ops_result < SBI_PMU_HW_CACHE_RESULT_MAX) &&
+		    (cache_ops_id < SBI_PMU_HW_CACHE_OP_MAX) &&
+		    (cache_id < SBI_PMU_HW_CACHE_MAX))
+			return event_idx_type;
+		else
+			return SBI_EINVAL;
+		break;
+	case SBI_PMU_EVENT_TYPE_HW_RAW:
+		event_idx_code_max = 1; // event_idx.code should be zero
+		break;
+	default:
+		return SBI_EINVAL;
+	}
+
+	if (event_idx_code < event_idx_code_max)
+		return event_idx_type;
+
+	return SBI_EINVAL;
+}
+
 static int pmu_ctr_validate(uint32_t cidx, uint32_t *event_idx_code)
 {
 	uint32_t event_idx_val;
@@ -614,12 +652,16 @@ int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,
 {
 	int ctr_idx = SBI_ENOTSUPP;
 	u32 hartid = current_hartid();
-	int event_type = get_cidx_type(event_idx);
+	int event_type;
 	struct sbi_pmu_fw_event *fevent;
 	uint32_t fw_evt_code;
 
 	/* Do a basic sanity check of counter base & mask */
-	if ((cidx_base + sbi_fls(cidx_mask)) >= total_ctrs || event_type >= SBI_PMU_EVENT_TYPE_MAX)
+	if ((cidx_base + sbi_fls(cidx_mask)) >= total_ctrs)
+		return SBI_EINVAL;
+
+	event_type = pmu_event_validate(event_idx);
+	if (event_type < 0)
 		return SBI_EINVAL;
 
 	if (flags & SBI_PMU_CFG_FLAG_SKIP_MATCH) {
Signature · 1 line
2.25.1
#4 lore raw

PATCH v3 2/4 lib: sbi: Add the bound check for events during config match

Quoted 30 lines · Atish Patra

Currently, there is no sanity check for firmware event code. We don't see any issue as Linux kernel driver does a bound check on firmware events already. However, OpenSBI can't assume sane supervisor mode software always. Thus, an invalid event idx can cause a buffer overflow error. For hardware events, the match will fail for invalid event code anyways. However, a search is unecessary if event code is invalid.

Add a event ID validation function to solve the issue.

---
 lib/sbi/sbi_pmu.c | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
lib/sbi/sbi_pmu.c +8 -0
index 31631a2fab80..bd1ef8665a70 100644
@@ -107,6 +107,44 @@ static bool pmu_event_select_overlap(struct sbi_pmu_hw_event *evt,
 	return FALSE;
 }
 
+static int pmu_event_validate(unsigned long event_idx)
+{
+	uint32_t event_idx_type = get_cidx_type(event_idx);
+	uint32_t event_idx_code = get_cidx_code(event_idx);
+	uint32_t event_idx_code_max = -1;
+	uint32_t cache_ops_result, cache_ops_id, cache_id;
+
+	switch(event_idx_type) {

lib/sbi/sbi_pmu.c static bool pmu_event_select_overlap(struct sbi_pmu_hw_event *evt, · line 127

Quoted 10 lines
lib/sbi/sbi_pmu.c +10 -0
+	case SBI_PMU_EVENT_TYPE_HW:
+		event_idx_code_max = SBI_PMU_HW_GENERAL_MAX;
+		break;
+	case SBI_PMU_EVENT_TYPE_FW:
+		event_idx_code_max = SBI_PMU_FW_MAX;
+		break;
+	case SBI_PMU_EVENT_TYPE_HW_CACHE:
+		cache_ops_result = event_idx_code & 0x1;
+		cache_ops_id = (event_idx_code & 0x06) >> 1;
+		cache_id = (event_idx_code & 0xfff8) >> 3;

It'd be nice to introduce helper macros for these.

Quoted 45 lines
lib/sbi/sbi_pmu.c +26 -2
+		if ((cache_ops_result < SBI_PMU_HW_CACHE_RESULT_MAX) &&
+		    (cache_ops_id < SBI_PMU_HW_CACHE_OP_MAX) &&
+		    (cache_id < SBI_PMU_HW_CACHE_MAX))
+			return event_idx_type;
+		else
+			return SBI_EINVAL;
+		break;
+	case SBI_PMU_EVENT_TYPE_HW_RAW:
+		event_idx_code_max = 1; // event_idx.code should be zero
+		break;
+	default:
+		return SBI_EINVAL;
+	}
+
+	if (event_idx_code < event_idx_code_max)
+		return event_idx_type;
+
+	return SBI_EINVAL;
+}
+
 static int pmu_ctr_validate(uint32_t cidx, uint32_t *event_idx_code)
 {
 	uint32_t event_idx_val;
@@ -614,12 +652,16 @@ int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,
 {
 	int ctr_idx = SBI_ENOTSUPP;
 	u32 hartid = current_hartid();
-	int event_type = get_cidx_type(event_idx);
+	int event_type;
 	struct sbi_pmu_fw_event *fevent;
 	uint32_t fw_evt_code;
 
 	/* Do a basic sanity check of counter base & mask */
-	if ((cidx_base + sbi_fls(cidx_mask)) >= total_ctrs || event_type >= SBI_PMU_EVENT_TYPE_MAX)
+	if ((cidx_base + sbi_fls(cidx_mask)) >= total_ctrs)
+		return SBI_EINVAL;
+
+	event_type = pmu_event_validate(event_idx);
+	if (event_type < 0)
 		return SBI_EINVAL;
 
 	if (flags & SBI_PMU_CFG_FLAG_SKIP_MATCH) {
Signature · 1 line
2.25.1

Otherwise,

Applied
#5 lore raw

PATCH v3 2/4 lib: sbi: Add the bound check for events during config match

Quoted 36 lines · Andrew Jones
Quoted 34 lines · Atish Patra

Currently, there is no sanity check for firmware event code. We don't see any issue as Linux kernel driver does a bound check on firmware events already. However, OpenSBI can't assume sane supervisor mode software always. Thus, an invalid event idx can cause a buffer overflow error. For hardware events, the match will fail for invalid event code anyways. However, a search is unecessary if event code is invalid.

Add a event ID validation function to solve the issue.

---
 lib/sbi/sbi_pmu.c | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
lib/sbi/sbi_pmu.c +12 -0
index 31631a2fab80..bd1ef8665a70 100644
@@ -107,6 +107,44 @@ static bool pmu_event_select_overlap(struct sbi_pmu_hw_event *evt,
      return FALSE;
 }
 
+static int pmu_event_validate(unsigned long event_idx)
+{
+     uint32_t event_idx_type = get_cidx_type(event_idx);
+     uint32_t event_idx_code = get_cidx_code(event_idx);
+     uint32_t event_idx_code_max = -1;
+     uint32_t cache_ops_result, cache_ops_id, cache_id;
+
+     switch(event_idx_type) {
+     case SBI_PMU_EVENT_TYPE_HW:
+             event_idx_code_max = SBI_PMU_HW_GENERAL_MAX;
+             break;
+     case SBI_PMU_EVENT_TYPE_FW:
Quoted 8 lines

lib/sbi/sbi_pmu.c static bool pmu_event_select_overlap(struct sbi_pmu_hw_event *evt, · line 127

Quoted 6 lines
lib/sbi/sbi_pmu.c +6 -0
+             event_idx_code_max = SBI_PMU_FW_MAX;
+             break;
+     case SBI_PMU_EVENT_TYPE_HW_CACHE:
+             cache_ops_result = event_idx_code & 0x1;
+             cache_ops_id = (event_idx_code & 0x06) >> 1;
+             cache_id = (event_idx_code & 0xfff8) >> 3;

It'd be nice to introduce helper macros for these.

I have added appropriate defines in include/sbi/sbi_ecall_interface.h and used it here at time of merging this patch.

Quoted 42 lines
Quoted 42 lines
lib/sbi/sbi_pmu.c +26 -2
+             if ((cache_ops_result < SBI_PMU_HW_CACHE_RESULT_MAX) &&
+                 (cache_ops_id < SBI_PMU_HW_CACHE_OP_MAX) &&
+                 (cache_id < SBI_PMU_HW_CACHE_MAX))
+                     return event_idx_type;
+             else
+                     return SBI_EINVAL;
+             break;
+     case SBI_PMU_EVENT_TYPE_HW_RAW:
+             event_idx_code_max = 1; // event_idx.code should be zero
+             break;
+     default:
+             return SBI_EINVAL;
+     }
+
+     if (event_idx_code < event_idx_code_max)
+             return event_idx_type;
+
+     return SBI_EINVAL;
+}
+
 static int pmu_ctr_validate(uint32_t cidx, uint32_t *event_idx_code)
 {
      uint32_t event_idx_val;
@@ -614,12 +652,16 @@ int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,
 {
      int ctr_idx = SBI_ENOTSUPP;
      u32 hartid = current_hartid();
-     int event_type = get_cidx_type(event_idx);
+     int event_type;
      struct sbi_pmu_fw_event *fevent;
      uint32_t fw_evt_code;
 
      /* Do a basic sanity check of counter base & mask */
-     if ((cidx_base + sbi_fls(cidx_mask)) >= total_ctrs || event_type >= SBI_PMU_EVENT_TYPE_MAX)
+     if ((cidx_base + sbi_fls(cidx_mask)) >= total_ctrs)
+             return SBI_EINVAL;
+
+     event_type = pmu_event_validate(event_idx);
+     if (event_type < 0)
              return SBI_EINVAL;
 
Quoted 8 lines
Quoted 4 lines
lib/sbi/sbi_pmu.c +0 -1
      if (flags & SBI_PMU_CFG_FLAG_SKIP_MATCH) {
--

2.25.1

Otherwise,

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

#6 lore raw

PATCH v3 3/4 lib: sbi: Fix possible buffer overrun in counter validation

In reply to Atish Patra

The active_events array is accessed with counter ID passed from the supervisor software before the counter ID bound check. This may cause a buffer overrun if a supervisor passes an invalid counter ID.

Fix this by moving the access part after the bound check.

---
 lib/sbi/sbi_pmu.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
lib/sbi/sbi_pmu.c +4 -4
index bd1ef8665a70..ae3c00374058 100644
@@ -151,13 +151,13 @@ static int pmu_ctr_validate(uint32_t cidx, uint32_t *event_idx_code)
 	uint32_t event_idx_type;
 	u32 hartid = current_hartid();
 
-	event_idx_val = active_events[hartid][cidx];
-
-	if (cidx >= total_ctrs || (event_idx_val == SBI_PMU_EVENT_IDX_INVALID))
+	if (cidx >= total_ctrs)
 		return SBI_EINVAL;
 
+	event_idx_val = active_events[hartid][cidx];
 	event_idx_type = get_cidx_type(event_idx_val);
-	if (event_idx_type >= SBI_PMU_EVENT_TYPE_MAX)
+	if (event_idx_val == SBI_PMU_EVENT_IDX_INVALID ||
+	    event_idx_type >= SBI_PMU_EVENT_TYPE_MAX)
 		return SBI_EINVAL;
 
 	*event_idx_code = get_cidx_code(event_idx_val);
Signature · 1 line
2.25.1
Applied
#7 lore raw

PATCH v3 3/4 lib: sbi: Fix possible buffer overrun in counter validation

Quoted 10 lines · Atish Patra

The active_events array is accessed with counter ID passed from the supervisor software before the counter ID bound check. This may cause a buffer overrun if a supervisor passes an invalid counter ID.

Fix this by moving the access part after the bound check.

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 29 lines
---
 lib/sbi/sbi_pmu.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
lib/sbi/sbi_pmu.c +4 -5
index bd1ef8665a70..ae3c00374058 100644
@@ -151,13 +151,13 @@ static int pmu_ctr_validate(uint32_t cidx, uint32_t *event_idx_code)
        uint32_t event_idx_type;
        u32 hartid = current_hartid();
 
-       event_idx_val = active_events[hartid][cidx];
-
-       if (cidx >= total_ctrs || (event_idx_val == SBI_PMU_EVENT_IDX_INVALID))
+       if (cidx >= total_ctrs)
                return SBI_EINVAL;
 
+       event_idx_val = active_events[hartid][cidx];
        event_idx_type = get_cidx_type(event_idx_val);
-       if (event_idx_type >= SBI_PMU_EVENT_TYPE_MAX)
+       if (event_idx_val == SBI_PMU_EVENT_IDX_INVALID ||
+           event_idx_type >= SBI_PMU_EVENT_TYPE_MAX)
                return SBI_EINVAL;
 
        *event_idx_code = get_cidx_code(event_idx_val);
--

2.25.1

#8 lore raw

PATCH v3 4/4 lib: sbi: Fix fw_event_map initialization

In reply to Atish Patra

fw_event_map represents array of firmware events. It should initialized for maximum number of firmware events not counters.

---
 lib/sbi/sbi_pmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +1 -1
index ae3c00374058..b4342971bcf1 100644
@@ -764,7 +764,7 @@ static void pmu_reset_event_map(u32 hartid)
 	/* Initialize the counter to event mapping table */
 	for (j = 3; j < total_ctrs; j++)
 		active_events[hartid][j] = SBI_PMU_EVENT_IDX_INVALID;
-	for (j = 0; j < SBI_PMU_FW_CTR_MAX; j++)
+	for (j = 0; j < SBI_PMU_FW_EVENT_MAX; j++)
 		sbi_memset(&fw_event_map[hartid][j], 0,
 			   sizeof(struct sbi_pmu_fw_event));
 }
Signature · 1 line
2.25.1
Applied
#9 lore raw

PATCH v3 4/4 lib: sbi: Fix fw_event_map initialization

Quoted 6 lines · Atish Patra

fw_event_map represents array of firmware events. It should initialized for maximum number of firmware events not counters.

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 20 lines
---
 lib/sbi/sbi_pmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +1 -2
index ae3c00374058..b4342971bcf1 100644
@@ -764,7 +764,7 @@ static void pmu_reset_event_map(u32 hartid)
        /* Initialize the counter to event mapping table */
        for (j = 3; j < total_ctrs; j++)
                active_events[hartid][j] = SBI_PMU_EVENT_IDX_INVALID;
-       for (j = 0; j < SBI_PMU_FW_CTR_MAX; j++)
+       for (j = 0; j < SBI_PMU_FW_EVENT_MAX; j++)
                sbi_memset(&fw_event_map[hartid][j], 0,
                           sizeof(struct sbi_pmu_fw_event));
 }
--

2.25.1