Skip to contentSkip to conversation

perf: RISC-V: fix access to the highest available counter

PATCH Superseded Sergey Matyukevich 2 participants 7 messages Jun 23, 2022

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover perf: RISC-V: fix access to the highest available counter

Hi all,

This patch series suggests some fixes and improvements for the bookkeeping of the PMU counter indexes. The first patch fixes pmu_ctr_list array size to prevent memory access beyond the allocated array. The second patch fixes access to the highest available PMU counter.

The accompanying OpenSBI fix is available for review here:
- https://github.com/riscv-software-src/opensbi/pull/260

Note that if custom SBI firmware does not support firmware events, then current driver behavior makes inaccessible the hardware counter with the highest index.

Finally, the third patch attempts to provide support for any gaps in PMU counter indexes. For this purpose pmu_ctr_list array is replaced by IDR.

Regards,
Sergey

Sergey Matyukevich (3):
perf: RISC-V: fix access beyond allocated array
perf: RISC-V: allow to use the highest available counter
perf: RISC-V: support noncontiguous pmu counter IDs

 drivers/perf/riscv_pmu_legacy.c |  4 +-
 drivers/perf/riscv_pmu_sbi.c    | 88 +++++++++++++++++++++++----------
 include/linux/perf/riscv_pmu.h  |  2 +-
 3 files changed, 65 insertions(+), 29 deletions(-)
Signature · 7 lines
2.36.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw

PATCH 1/3 perf: RISC-V: fix access beyond allocated array

From: Sergey Matyukevich <[email protected]>

Both OpenSBI and Linux driver explicitly assume that pmu counter IDs are not expected to be contiguous. Namely, there is no hardware counter with index 1: hardware uses that bit for TM control. However counter array is allocated without that assumption. As a result, memory beyond allocated array is accessed. Fix this by adding unused array element for index 1.

---
 drivers/perf/riscv_pmu_sbi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/perf/riscv_pmu_sbi.c +1 -1
index dca3537a8dcc..3e0ea564b9b8 100644
@@ -453,7 +453,7 @@ static int pmu_sbi_get_ctrinfo(int nctr)
 	int i, num_hw_ctr = 0, num_fw_ctr = 0;
 	union sbi_pmu_ctr_info cinfo;
 
-	pmu_ctr_list = kcalloc(nctr, sizeof(*pmu_ctr_list), GFP_KERNEL);
+	pmu_ctr_list = kcalloc(nctr + 1, sizeof(*pmu_ctr_list), GFP_KERNEL);
 	if (!pmu_ctr_list)
 		return -ENOMEM;
 
Signature · 7 lines
2.36.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#2 lore raw

Re: [PATCH 1/3] perf: RISC-V: fix access beyond allocated array

Quoted 28 lines · Sergey Matyukevich

From: Sergey Matyukevich <[email protected]>

Both OpenSBI and Linux driver explicitly assume that pmu counter IDs are not expected to be contiguous. Namely, there is no hardware counter with index 1: hardware uses that bit for TM control. However counter array is allocated without that assumption. As a result, memory beyond allocated array is accessed. Fix this by adding unused array element for index 1.

---
 drivers/perf/riscv_pmu_sbi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/perf/riscv_pmu_sbi.c +1 -2
index dca3537a8dcc..3e0ea564b9b8 100644
@@ -453,7 +453,7 @@ static int pmu_sbi_get_ctrinfo(int nctr)
        int i, num_hw_ctr = 0, num_fw_ctr = 0;
        union sbi_pmu_ctr_info cinfo;
 
-       pmu_ctr_list = kcalloc(nctr, sizeof(*pmu_ctr_list), GFP_KERNEL);
+       pmu_ctr_list = kcalloc(nctr + 1, sizeof(*pmu_ctr_list), GFP_KERNEL);
        if (!pmu_ctr_list)
                return -ENOMEM;
 
--
Quoted 2 lines

2.36.1

instead of this, get_info for loop should be restricted nctr as it should be zero indexed.

drivers/perf/riscv_pmu_sbi.c +1 -1
index f9cf6c62aaea..0722fe2869aa 100644
@@ -491,7 +491,7 @@ static int pmu_sbi_get_ctrinfo(int nctr, int *num_hw_ctrs)
        if (!pmu_ctr_list)
                return -ENOMEM;
 
-       for (i = 0; i <= nctr; i++) {
+       for (i = 0; i < nctr; i++) {
                ret = sbi_ecall(SBI_EXT_PMU,
SBI_EXT_PMU_COUNTER_GET_INFO, i, 0, 0, 0, 0, 0);
                if (ret.error)
                        /* The logical counter ids are not expected to

be contiguous */

Signature · 7 lines
Regards,
Atish

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#3 lore raw

Re: [PATCH 1/3] perf: RISC-V: fix access beyond allocated array

Quoted 45 lines
Quoted 25 lines

Both OpenSBI and Linux driver explicitly assume that pmu counter IDs are not expected to be contiguous. Namely, there is no hardware counter with index 1: hardware uses that bit for TM control. However counter array is allocated without that assumption. As a result, memory beyond allocated array is accessed. Fix this by adding unused array element for index 1.

---
 drivers/perf/riscv_pmu_sbi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/perf/riscv_pmu_sbi.c +1 -2
index dca3537a8dcc..3e0ea564b9b8 100644
@@ -453,7 +453,7 @@ static int pmu_sbi_get_ctrinfo(int nctr)
        int i, num_hw_ctr = 0, num_fw_ctr = 0;
        union sbi_pmu_ctr_info cinfo;
 
-       pmu_ctr_list = kcalloc(nctr, sizeof(*pmu_ctr_list), GFP_KERNEL);
+       pmu_ctr_list = kcalloc(nctr + 1, sizeof(*pmu_ctr_list), GFP_KERNEL);
        if (!pmu_ctr_list)
                return -ENOMEM;
 
--
Quoted 2 lines

2.36.1

instead of this, get_info for loop should be restricted nctr as it should be zero indexed.

drivers/perf/riscv_pmu_sbi.c +1 -1
index f9cf6c62aaea..0722fe2869aa 100644
@@ -491,7 +491,7 @@ static int pmu_sbi_get_ctrinfo(int nctr, int *num_hw_ctrs)
        if (!pmu_ctr_list)
                return -ENOMEM;
 
-       for (i = 0; i <= nctr; i++) {
+       for (i = 0; i < nctr; i++) {
                ret = sbi_ecall(SBI_EXT_PMU,
 SBI_EXT_PMU_COUNTER_GET_INFO, i, 0, 0, 0, 0, 0);
                if (ret.error)
                        /* The logical counter ids are not expected to
Quoted 1 line

be contiguous */

Well, this is going to fix immediate issue. But array size will have to be increased by one to enable access to the highest index counter (see the 2nd patch).

Regards,
Sergey

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#4 lore raw

PATCH 2/3 perf: RISC-V: allow to use the highest available counter

In reply to Sergey Matyukevich

From: Sergey Matyukevich <[email protected]>

Both OpenSBI and Linux explicitly assume that there is no hardware counter with index 1: hardware uses that bit for TM control. So OpenSBI filters out that index in sanity checks. However its range sanity checks do not treat that index in a special way. As a result, OpenSBI does not allow to use the firmware counter with the highest index. Linux perf RISC-V SBI driver is adapted for the current OpenSBI behavior: it excludes the highest valid index from the counter mask passed to OpenSBI.

This patch fixes ranges to re-enable the highest available counter.

Accompanying OpenSBI fix to accept full mask:
- https://github.com/riscv-software-src/opensbi/pull/260

---
 drivers/perf/riscv_pmu_sbi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/perf/riscv_pmu_sbi.c +3 -3
index 3e0ea564b9b8..294d4bded59e 100644
@@ -265,7 +265,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event)
 	struct sbiret ret;
 	int idx;
 	uint64_t cbase = 0;
-	uint64_t cmask = GENMASK_ULL(rvpmu->num_counters - 1, 0);
+	uint64_t cmask = GENMASK_ULL(rvpmu->num_counters, 0);
 	unsigned long cflags = 0;
 
 	if (event->attr.exclude_kernel)
@@ -283,7 +283,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event)
 	}
 
 	idx = ret.value;
-	if (idx >= rvpmu->num_counters || !pmu_ctr_list[idx].value)
+	if (idx > rvpmu->num_counters || !pmu_ctr_list[idx].value)
 		return -ENOENT;
 
 	/* Additional sanity check for the counter id */
@@ -482,7 +482,7 @@ static inline void pmu_sbi_stop_all(struct riscv_pmu *pmu)
 	 * which may include counters that are not enabled yet.
 	 */
 	sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP,
-		  0, GENMASK_ULL(pmu->num_counters - 1, 0), 0, 0, 0, 0);
+		  0, GENMASK_ULL(pmu->num_counters, 0), 0, 0, 0, 0);
 }
 
 static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu)
Signature · 7 lines
2.36.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#5 lore raw

Re: [PATCH 2/3] perf: RISC-V: allow to use the highest available counter

Quoted 53 lines · Sergey Matyukevich

From: Sergey Matyukevich <[email protected]>

Both OpenSBI and Linux explicitly assume that there is no hardware counter with index 1: hardware uses that bit for TM control. So OpenSBI filters out that index in sanity checks. However its range sanity checks do not treat that index in a special way. As a result, OpenSBI does not allow to use the firmware counter with the highest index. Linux perf RISC-V SBI driver is adapted for the current OpenSBI behavior: it excludes the highest valid index from the counter mask passed to OpenSBI.

This patch fixes ranges to re-enable the highest available counter.

Accompanying OpenSBI fix to accept full mask:
- https://github.com/riscv-software-src/opensbi/pull/260

---
 drivers/perf/riscv_pmu_sbi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/perf/riscv_pmu_sbi.c +3 -4
index 3e0ea564b9b8..294d4bded59e 100644
@@ -265,7 +265,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event)
        struct sbiret ret;
        int idx;
        uint64_t cbase = 0;
-       uint64_t cmask = GENMASK_ULL(rvpmu->num_counters - 1, 0);
+       uint64_t cmask = GENMASK_ULL(rvpmu->num_counters, 0);
        unsigned long cflags = 0;
 
        if (event->attr.exclude_kernel)
@@ -283,7 +283,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event)
        }
 
        idx = ret.value;
-       if (idx >= rvpmu->num_counters || !pmu_ctr_list[idx].value)
+       if (idx > rvpmu->num_counters || !pmu_ctr_list[idx].value)
                return -ENOENT;
 
        /* Additional sanity check for the counter id */
@@ -482,7 +482,7 @@ static inline void pmu_sbi_stop_all(struct riscv_pmu *pmu)
         * which may include counters that are not enabled yet.
         */
        sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP,
-                 0, GENMASK_ULL(pmu->num_counters - 1, 0), 0, 0, 0, 0);
+                 0, GENMASK_ULL(pmu->num_counters, 0), 0, 0, 0, 0);
 }
 
 static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu)
--
Quoted 2 lines

2.36.1

This won't be required to support maximum firmware counter index. With the fix, I proposed in your first patch in kernel
and OpenSBI, it should be doable. Both kernel & OpenSBI should use zero indexed counter schemes to avoid confusion.

--
Regards,
Atish

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#6 lore raw

PATCH 3/3 perf: RISC-V: support noncontiguous pmu counter IDs

In reply to Sergey Matyukevich

From: Sergey Matyukevich <[email protected]>

OpenSBI and Linux driver assume that pmu counter IDs are not expected to be contiguous. Current support for noncontiguous IDs is limited by the special treatment of the index 1 used by hardware for TM control. Replace counter array by IDR to support gaps in hardware counter IDs.

---
 drivers/perf/riscv_pmu_legacy.c |  4 +-
 drivers/perf/riscv_pmu_sbi.c    | 88 +++++++++++++++++++++++----------
 include/linux/perf/riscv_pmu.h  |  2 +-
 3 files changed, 65 insertions(+), 29 deletions(-)
drivers/perf/riscv_pmu_legacy.c +2 -2
index 342778782359..7d7131c47bc0 100644
@@ -14,7 +14,6 @@
 
 #define RISCV_PMU_LEGACY_CYCLE		0
 #define RISCV_PMU_LEGACY_INSTRET	1
-#define RISCV_PMU_LEGACY_NUM_CTR	2
 
 static bool pmu_init_done;
 
@@ -83,7 +82,8 @@ static void pmu_legacy_init(struct riscv_pmu *pmu)
 {
 	pr_info("Legacy PMU implementation is available\n");
 
-	pmu->num_counters = RISCV_PMU_LEGACY_NUM_CTR;
+	pmu->cmask = BIT(RISCV_PMU_LEGACY_CYCLE) |
+		BIT(RISCV_PMU_LEGACY_INSTRET);
 	pmu->ctr_start = pmu_legacy_ctr_start;
 	pmu->ctr_stop = NULL;
 	pmu->event_map = pmu_legacy_event_map;
drivers/perf/riscv_pmu_sbi.c +62 -26

190 lines not on this page · raw message

include/linux/perf/riscv_pmu.h +1 -1
index 46f9b6fe306e..b46e7e6d3209 100644
@@ -45,7 +45,7 @@ struct riscv_pmu {
 
 	irqreturn_t	(*handle_irq)(int irq_num, void *dev);
 
-	int		num_counters;
+	u64		cmask;
 	u64		(*ctr_read)(struct perf_event *event);
 	int		(*ctr_get_idx)(struct perf_event *event);
 	int		(*ctr_get_width)(int idx);
Signature · 7 lines
2.36.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv