Skip to contentSkip to conversation

Miscallenous improvement & fixes for the PMU driver

PATCH In review Atish Patra 4 participants 10 messages Jul 11, 2022 – Aug 12, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover Miscallenous improvement & fixes for the PMU driver

This series fixes issues PMU driver code.
PATCH 1 & 3 are fixes for rv32 while PATCH 2 fixes a redundant user page update issue during counter start.

PATCH 4 & 5 improves the SBI PMU definition.

Changes from v2->v3:
1. Added two more patches that fixes few typos and enable support for perf support in KVM.

Changes from v1->v2:
1. Add proper compile time rv32 checks.

Atish Patra (5):
RISC-V: Fix counter restart during overflow for RV32
RISC-V: Update user page mapping only once during start
RISC-V: Fix SBI PMU calls for RV32
RISC-V: Move counter info definition to sbi header file
RISC-V: Improve SBI definitions

arch/riscv/include/asm/sbi.h | 32 ++++++++++++++++++++++++++++++--
drivers/perf/riscv_pmu.c     |  1 -
drivers/perf/riscv_pmu_sbi.c | 30 ++++++++++++++++--------------
3 files changed, 46 insertions(+), 17 deletions(-)

--
2.25.1

#1 lore raw

PATCH v3 1/5 RISC-V: Fix counter restart during overflow for RV32

Pass the upper half of the initial value of the counter correctly for RV32.

Fixes 4905ec2fb7e6 ("RISC-V: Add sscofpmf extension support")
---
 drivers/perf/riscv_pmu_sbi.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/perf/riscv_pmu_sbi.c +5 -0
index dca3537a8dcc..0cb694b794ae 100644
@@ -525,8 +525,13 @@ static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
 			hwc = &event->hw;
 			max_period = riscv_pmu_ctr_get_width_mask(event);
 			init_val = local64_read(&hwc->prev_count) & max_period;
+#if defined(CONFIG_32BIT)
+			sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
+				  flag, init_val, init_val >> 32, 0);
+#else
 			sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
 				  flag, init_val, 0, 0);
+#endif
 		}
 		ctr_ovf_mask = ctr_ovf_mask >> 1;
 		idx++;
Signature · 1 line
2.25.1
Reviewed-by on v3 1/5
#2 lore raw

Re: [v3 1/5] RISC-V: Fix counter restart during overflow for RV32

Quoted 32 lines · Atish Patra

Pass the upper half of the initial value of the counter correctly for RV32.

Fixes 4905ec2fb7e6 ("RISC-V: Add sscofpmf extension support")
---
 drivers/perf/riscv_pmu_sbi.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/perf/riscv_pmu_sbi.c +5 -1
index dca3537a8dcc..0cb694b794ae 100644
@@ -525,8 +525,13 @@ static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
                        hwc = &event->hw;
                        max_period = riscv_pmu_ctr_get_width_mask(event);
                        init_val = local64_read(&hwc->prev_count) & max_period;
+#if defined(CONFIG_32BIT)
+                       sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
+                                 flag, init_val, init_val >> 32, 0);
+#else
                        sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
                                  flag, init_val, 0, 0);
+#endif
                }
                ctr_ovf_mask = ctr_ovf_mask >> 1;
                idx++;
--

2.25.1

Signature · 2 lines
Best Regards
 Guo Ren
#3 lore raw

PATCH v3 2/5 RISC-V: Update user page mapping only once during start

In reply to Atish Patra

Currently, riscv_pmu_event_set_period updates the userpage mapping. However, the caller of riscv_pmu_event_set_period should update the userpage mapping because the counter can not be updated/started from set_period function in counter overflow path.

Invoke the perf_event_update_userpage at the caller so that it doesn't get invoked twice during counter start path.

Fixes f5bfa23f576f ("RISC-V: Add a perf core library for pmu drivers")
---
 drivers/perf/riscv_pmu.c     | 1 -
 drivers/perf/riscv_pmu_sbi.c | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
drivers/perf/riscv_pmu.c +0 -1
index b2b8d2074ed0..130b9f1a40e0 100644
@@ -170,7 +170,6 @@ int riscv_pmu_event_set_period(struct perf_event *event)
 		left = (max_period >> 1);
 
 	local64_set(&hwc->prev_count, (u64)-left);
-	perf_event_update_userpage(event);
 
 	return overflow;
 }
drivers/perf/riscv_pmu_sbi.c +1 -0
index 0cb694b794ae..3735337a4cfb 100644
@@ -532,6 +532,7 @@ static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
 			sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
 				  flag, init_val, 0, 0);
 #endif
+			perf_event_update_userpage(event);
 		}
 		ctr_ovf_mask = ctr_ovf_mask >> 1;
 		idx++;
Signature · 1 line
2.25.1
Reviewed-by on v3 2/5
#4 lore raw

Re: [v3 2/5] RISC-V: Update user page mapping only once during start

Good catch, Is there any bug report?

Anyway:

Quoted 43 lines · Atish Patra

Currently, riscv_pmu_event_set_period updates the userpage mapping. However, the caller of riscv_pmu_event_set_period should update the userpage mapping because the counter can not be updated/started from set_period function in counter overflow path.

Invoke the perf_event_update_userpage at the caller so that it doesn't get invoked twice during counter start path.

Fixes f5bfa23f576f ("RISC-V: Add a perf core library for pmu drivers")
---
 drivers/perf/riscv_pmu.c     | 1 -
 drivers/perf/riscv_pmu_sbi.c | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
drivers/perf/riscv_pmu.c +0 -1
index b2b8d2074ed0..130b9f1a40e0 100644
@@ -170,7 +170,6 @@ int riscv_pmu_event_set_period(struct perf_event *event)
                left = (max_period >> 1);
 
        local64_set(&hwc->prev_count, (u64)-left);
-       perf_event_update_userpage(event);
 
        return overflow;
 }
drivers/perf/riscv_pmu_sbi.c +1 -1
index 0cb694b794ae..3735337a4cfb 100644
@@ -532,6 +532,7 @@ static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
                        sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
                                  flag, init_val, 0, 0);
 #endif
+                       perf_event_update_userpage(event);
                }
                ctr_ovf_mask = ctr_ovf_mask >> 1;
                idx++;
--
Quoted 2 lines

2.25.1

--
Best Regards
Guo Ren

#5 lore raw

Re: [v3 2/5] RISC-V: Update user page mapping only once during start

Quoted 3 lines · Guo Ren

Good catch, Is there any bug report?

Not that I know of.

Quoted 55 lines

Anyway:

Quoted 43 lines · Atish Patra

Currently, riscv_pmu_event_set_period updates the userpage mapping. However, the caller of riscv_pmu_event_set_period should update the userpage mapping because the counter can not be updated/started from set_period function in counter overflow path.

Invoke the perf_event_update_userpage at the caller so that it doesn't get invoked twice during counter start path.

Fixes f5bfa23f576f ("RISC-V: Add a perf core library for pmu drivers")
---
 drivers/perf/riscv_pmu.c     | 1 -
 drivers/perf/riscv_pmu_sbi.c | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
drivers/perf/riscv_pmu.c +0 -1
index b2b8d2074ed0..130b9f1a40e0 100644
@@ -170,7 +170,6 @@ int riscv_pmu_event_set_period(struct perf_event *event)
                left = (max_period >> 1);
 
        local64_set(&hwc->prev_count, (u64)-left);
-       perf_event_update_userpage(event);
 
        return overflow;
 }
drivers/perf/riscv_pmu_sbi.c +1 -1
index 0cb694b794ae..3735337a4cfb 100644
@@ -532,6 +532,7 @@ static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
                        sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
                                  flag, init_val, 0, 0);
 #endif
+                       perf_event_update_userpage(event);
                }
                ctr_ovf_mask = ctr_ovf_mask >> 1;
                idx++;
--
Quoted 2 lines

2.25.1

--
Best Regards
Guo Ren

Signature · 2 lines
Regards,
Atish
#6 lore raw

PATCH v3 3/5 RISC-V: Fix SBI PMU calls for RV32

In reply to Atish Patra

Some of the SBI PMU calls does not pass 64bit arguments correctly and not under RV32 compile time flags. Currently, this doesn't create any incorrect results as RV64 ignores any value in the additional register and qemu doesn't support raw events.

Fix those SBI calls in order to set correct values for RV32.

Fixes e9991434596f ("RISC-V: Add perf platform driver based on SBI PMU extension")
---
 drivers/perf/riscv_pmu_sbi.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/perf/riscv_pmu_sbi.c +10 -0
index 3735337a4cfb..bae614c73b14 100644
@@ -274,8 +274,13 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event)
 		cflags |= SBI_PMU_CFG_FLAG_SET_UINH;
 
 	/* retrieve the available counter index */
+#if defined(CONFIG_32BIT)
+	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase, cmask,
+			cflags, hwc->event_base, hwc->config, hwc->config >> 32);
+#else
 	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase, cmask,
 			cflags, hwc->event_base, hwc->config, 0);
+#endif
 	if (ret.error) {
 		pr_debug("Not able to find a counter for event %lx config %llx\n",
 			hwc->event_base, hwc->config);
@@ -417,8 +422,13 @@ static void pmu_sbi_ctr_start(struct perf_event *event, u64 ival)
 	struct hw_perf_event *hwc = &event->hw;
 	unsigned long flag = SBI_PMU_START_FLAG_SET_INIT_VALUE;
 
+#if defined(CONFIG_32BIT)
 	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
 			1, flag, ival, ival >> 32, 0);
+#else
+	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
+			1, flag, ival, 0, 0);
+#endif
 	if (ret.error && (ret.error != SBI_ERR_ALREADY_STARTED))
 		pr_err("Starting counter idx %d failed with error %d\n",
 			hwc->idx, sbi_err_map_linux_errno(ret.error));
Signature · 1 line
2.25.1
#7 lore raw

PATCH v3 4/5 RISC-V: Move counter info definition to sbi header file

In reply to Atish Patra

Counter info encoding format is defined by the SBI specificaiton. KVM implementation of SBI PMU extension will also leverage this definition. Move the definition to common sbi header file from the sbi pmu driver.

---
 arch/riscv/include/asm/sbi.h | 14 ++++++++++++++
 drivers/perf/riscv_pmu_sbi.c | 14 --------------
 2 files changed, 14 insertions(+), 14 deletions(-)
arch/riscv/include/asm/sbi.h +14 -0
index 9e3c2cf1edaf..d633ac0f5a32 100644
@@ -122,6 +122,20 @@ enum sbi_ext_pmu_fid {
 	SBI_EXT_PMU_COUNTER_FW_READ,
 };
 
+union sbi_pmu_ctr_info {
+	unsigned long value;
+	struct {
+		unsigned long csr:12;
+		unsigned long width:6;
+#if __riscv_xlen == 32
+		unsigned long reserved:13;
+#else
+		unsigned long reserved:45;
+#endif
+		unsigned long type:1;
+	};
+};
+
 #define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(55, 0)
 #define RISCV_PMU_RAW_EVENT_IDX 0x20000
 
drivers/perf/riscv_pmu_sbi.c +0 -14
index bae614c73b14..24124546844c 100644
@@ -21,20 +21,6 @@
 #include <asm/sbi.h>
 #include <asm/hwcap.h>
 
-union sbi_pmu_ctr_info {
-	unsigned long value;
-	struct {
-		unsigned long csr:12;
-		unsigned long width:6;
-#if __riscv_xlen == 32
-		unsigned long reserved:13;
-#else
-		unsigned long reserved:45;
-#endif
-		unsigned long type:1;
-	};
-};
-
 /*
  * RISC-V doesn't have hetergenous harts yet. This need to be part of
  * per_cpu in case of harts with different pmu counters
Signature · 7 lines
2.25.1


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

PATCH v3 5/5 RISC-V: Improve SBI definitions

In reply to Atish Patra

Fixed few typos and bit fields not aligned with the spec. Define other related macros that will be useful in the future.

---
 arch/riscv/include/asm/sbi.h | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
arch/riscv/include/asm/sbi.h +16 -2
index d633ac0f5a32..2a0ef738695e 100644
@@ -136,7 +136,7 @@ union sbi_pmu_ctr_info {
 	};
 };
 
-#define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(55, 0)
+#define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(47, 0)
 #define RISCV_PMU_RAW_EVENT_IDX 0x20000
 
 /** General pmu event codes specified in SBI PMU extension */
@@ -203,12 +203,26 @@ enum sbi_pmu_ctr_type {
 	SBI_PMU_CTR_TYPE_FW,
 };
 
+/* Helper macros to decode event idx */
+#define SBI_PMU_EVENT_IDX_OFFSET 20
+#define SBI_PMU_EVENT_IDX_MASK 0xFFFFF
+#define SBI_PMU_EVENT_IDX_CODE_MASK 0xFFFF
+#define SBI_PMU_EVENT_IDX_TYPE_MASK 0xF0000
+#define SBI_PMU_EVENT_RAW_IDX 0x20000
+#define SBI_PMU_FIXED_CTR_MASK 0x07
+
+#define SBI_PMU_EVENT_CACHE_ID_CODE_MASK 0xFFF8
+#define SBI_PMU_EVENT_CACHE_OP_ID_CODE_MASK 0x06
+#define SBI_PMU_EVENT_CACHE_RESULT_ID_CODE_MASK 0x01
+
+#define SBI_PMU_EVENT_IDX_INVALID 0xFFFFFFFF
+
 /* Flags defined for config matching function */
 #define SBI_PMU_CFG_FLAG_SKIP_MATCH	(1 << 0)
 #define SBI_PMU_CFG_FLAG_CLEAR_VALUE	(1 << 1)
 #define SBI_PMU_CFG_FLAG_AUTO_START	(1 << 2)
 #define SBI_PMU_CFG_FLAG_SET_VUINH	(1 << 3)
-#define SBI_PMU_CFG_FLAG_SET_VSNH	(1 << 4)
+#define SBI_PMU_CFG_FLAG_SET_VSINH	(1 << 4)
 #define SBI_PMU_CFG_FLAG_SET_UINH	(1 << 5)
 #define SBI_PMU_CFG_FLAG_SET_SINH	(1 << 6)
 #define SBI_PMU_CFG_FLAG_SET_MINH	(1 << 7)
Signature · 7 lines
2.25.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
maintainer
#9 lore raw
In reply to Atish Patra
Quoted 20 lines · Atish Patra

This series fixes issues PMU driver code.
PATCH 1 & 3 are fixes for rv32 while PATCH 2 fixes a redundant user page update issue during counter start.

PATCH 4 & 5 improves the SBI PMU definition.

Changes from v2->v3:
1. Added two more patches that fixes few typos and enable support for perf support in KVM.

Changes from v1->v2:
1. Add proper compile time rv32 checks.

Atish Patra (5):
RISC-V: Fix counter restart during overflow for RV32
RISC-V: Update user page mapping only once during start
RISC-V: Fix SBI PMU calls for RV32
RISC-V: Move counter info definition to sbi header file
RISC-V: Improve SBI definitions

Quoted 4 lines
arch/riscv/include/asm/sbi.h | 32 ++++++++++++++++++++++++++++++--
drivers/perf/riscv_pmu.c     |  1 -
drivers/perf/riscv_pmu_sbi.c | 30 ++++++++++++++++--------------
3 files changed, 46 insertions(+), 17 deletions(-)

Sorry, I guess I'd forgotten that Will asked me to merge this one. Thanks to Atish for reminding me, this is on for-next.