Skip to contentSkip to conversation

Add SBI v3.0 PMU enhancements

PATCH Applied Atish Patra 5 participants 15 messages Sep 9, 2025 – Oct 9, 2025

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

v6 cover Add SBI v3.0 PMU enhancements

SBI v3.0 specification[1] added two new improvements to the PMU chaper. The SBI v3.0 specification is frozen and under public review phase as per the RISC-V International guidelines.

1. Added an additional get_event_info function to query event availablity in bulk instead of individual SBI calls for each event. This helps in improving the boot time.

2. Raw event width allowed by the platform is widened to have 56 bits with RAW event v2 as per new clarification in the priv ISA[2].

Apart from implementing these new features, this series improves the gpa range check in KVM and updates the kvm SBI implementation to SBI v3.0.

The opensbi patches have been merged. This series can be found at [3].

[1] https://github.com/riscv-non-isa/riscv-sbi-doc/releases/download/v3.0-rc7/riscv-sbi.pdf
[2] https://github.com/riscv/riscv-isa-manual/issues/1578
[3] https://github.com/atishp04/linux/tree/b4/pmu_event_info_v6

---
Changes in v6:
- Dropped the helper function to check writable slot
- Updated PATCH 7 to return invalid address error if vcpu_write_guest fails
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Rebased on top of v6.17-rc3
- Updated PATCH 6 as per feedback to improve the generic helper function
- Adapted PATCH 7 & 8 as per PATCH 6.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Rebased on top of v6.16-rc7
- Fixed a potential compilation issue in PATCH5.
- Minor typos fixed PATCH2 and PATCH3.
- Fixed variable ordering in PATCH6
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Rebased on top of v6.15-rc7
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Dropped PATCH 2 to be taken during rcX.
- Improved gpa range check validation by introducing a helper function
and checking the entire range.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Atish Patra (8):
drivers/perf: riscv: Add SBI v3.0 flag drivers/perf: riscv: Add raw event v2 support
RISC-V: KVM: Add support for Raw event v2 drivers/perf: riscv: Implement PMU event info function drivers/perf: riscv: Export PMU event info function
RISC-V: KVM: No need of explicit writable slot check
RISC-V: KVM: Implement get event info function
RISC-V: KVM: Upgrade the supported SBI version to 3.0

 arch/riscv/include/asm/kvm_vcpu_pmu.h |   3 +
 arch/riscv/include/asm/kvm_vcpu_sbi.h |   2 +-
 arch/riscv/include/asm/sbi.h          |  13 +++
 arch/riscv/kvm/vcpu_pmu.c             |  74 +++++++++++--
 arch/riscv/kvm/vcpu_sbi_pmu.c         |   3 +
 arch/riscv/kvm/vcpu_sbi_sta.c         |   9 +-
 drivers/perf/riscv_pmu_sbi.c          | 191 +++++++++++++++++++++++++---------
 include/linux/perf/riscv_pmu.h        |   1 +
 8 files changed, 229 insertions(+), 67 deletions(-)

---
base-commit: e32a80927434907f973f38a88cd19d7e51991d24
change-id: 20241018-pmu_event_info-986e21ce6bd3 --
Regards,
Atish patra

#1 lore raw

PATCH v6 1/8 drivers/perf: riscv: Add SBI v3.0 flag

There are new PMU related features introduced in SBI v3.0.
1. Raw Event v2 which allows mhpmeventX value to be 56 bit wide.
2. Get Event info function to do a bulk query at one shot.

---
 drivers/perf/riscv_pmu_sbi.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/perf/riscv_pmu_sbi.c +4 -0
index 698de8ddf895..cfd6946fca42 100644
@@ -63,6 +63,7 @@ PMU_FORMAT_ATTR(event, "config:0-47");
 PMU_FORMAT_ATTR(firmware, "config:62-63");
 
 static bool sbi_v2_available;
+static bool sbi_v3_available;
 static DEFINE_STATIC_KEY_FALSE(sbi_pmu_snapshot_available);
 #define sbi_pmu_snapshot_available() \
 	static_branch_unlikely(&sbi_pmu_snapshot_available)
@@ -1452,6 +1453,9 @@ static int __init pmu_sbi_devinit(void)
 	if (sbi_spec_version >= sbi_mk_version(2, 0))
 		sbi_v2_available = true;
 
+	if (sbi_spec_version >= sbi_mk_version(3, 0))
+		sbi_v3_available = true;
+
 	ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_RISCV_STARTING,
 				      "perf/riscv/pmu:starting",
 				      pmu_sbi_starting_cpu, pmu_sbi_dying_cpu);
Signature · 1 line
2.43.0
#2 lore raw

PATCH v6 2/8 drivers/perf: riscv: Add raw event v2 support

In reply to Atish Patra

SBI v3.0 introduced a new raw event type that allows wider mhpmeventX width to be programmed via CFG_MATCH.

Use the raw event v2 if SBI v3.0 is available.

---
 arch/riscv/include/asm/sbi.h |  4 ++++
 drivers/perf/riscv_pmu_sbi.c | 16 +++++++++++-----
 2 files changed, 15 insertions(+), 5 deletions(-)
arch/riscv/include/asm/sbi.h +4 -0
index 341e74238aa0..b0c41ef56968 100644
@@ -161,7 +161,10 @@ struct riscv_pmu_snapshot_data {
 
 #define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(47, 0)
 #define RISCV_PMU_PLAT_FW_EVENT_MASK GENMASK_ULL(61, 0)
+/* SBI v3.0 allows extended hpmeventX width value */
+#define RISCV_PMU_RAW_EVENT_V2_MASK GENMASK_ULL(55, 0)
 #define RISCV_PMU_RAW_EVENT_IDX 0x20000
+#define RISCV_PMU_RAW_EVENT_V2_IDX 0x30000
 #define RISCV_PLAT_FW_EVENT	0xFFFF
 
 /** General pmu event codes specified in SBI PMU extension */
@@ -219,6 +222,7 @@ enum sbi_pmu_event_type {
 	SBI_PMU_EVENT_TYPE_HW = 0x0,
 	SBI_PMU_EVENT_TYPE_CACHE = 0x1,
 	SBI_PMU_EVENT_TYPE_RAW = 0x2,
+	SBI_PMU_EVENT_TYPE_RAW_V2 = 0x3,
 	SBI_PMU_EVENT_TYPE_FW = 0xf,
 };
 
drivers/perf/riscv_pmu_sbi.c +11 -5
index cfd6946fca42..3644bed4c8ab 100644
@@ -59,7 +59,7 @@ asm volatile(ALTERNATIVE(						\
 #define PERF_EVENT_FLAG_USER_ACCESS	BIT(SYSCTL_USER_ACCESS)
 #define PERF_EVENT_FLAG_LEGACY		BIT(SYSCTL_LEGACY)
 
-PMU_FORMAT_ATTR(event, "config:0-47");
+PMU_FORMAT_ATTR(event, "config:0-55");
 PMU_FORMAT_ATTR(firmware, "config:62-63");
 
 static bool sbi_v2_available;
@@ -527,8 +527,10 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
 		break;
 	case PERF_TYPE_RAW:
 		/*
-		 * As per SBI specification, the upper 16 bits must be unused
-		 * for a hardware raw event.
+		 * As per SBI v0.3 specification,
+		 *  -- the upper 16 bits must be unused for a hardware raw event.
+		 * As per SBI v2.0 specification,
+		 *  -- the upper 8 bits must be unused for a hardware raw event.
 		 * Bits 63:62 are used to distinguish between raw events
 		 * 00 - Hardware raw event
 		 * 10 - SBI firmware events
@@ -537,8 +539,12 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
 
 		switch (config >> 62) {
 		case 0:
-			/* Return error any bits [48-63] is set  as it is not allowed by the spec */
-			if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) {
+			if (sbi_v3_available) {
+				if (!(config & ~RISCV_PMU_RAW_EVENT_V2_MASK)) {
+					*econfig = config & RISCV_PMU_RAW_EVENT_V2_MASK;
+					ret = RISCV_PMU_RAW_EVENT_V2_IDX;
+				}
+			} else if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) {
 				*econfig = config & RISCV_PMU_RAW_EVENT_MASK;
 				ret = RISCV_PMU_RAW_EVENT_IDX;
 			}
Signature · 1 line
2.43.0
#3 lore raw

PATCH v6 3/8 RISC-V: KVM: Add support for Raw event v2

In reply to Atish Patra

SBI v3.0 introduced a new raw event type v2 for wider mhpmeventX programming. Add the support in kvm for that.

---
 arch/riscv/kvm/vcpu_pmu.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kvm/vcpu_pmu.c +4 -0
index 78ac3216a54d..15d71a7b75ba 100644
@@ -60,6 +60,7 @@ static u32 kvm_pmu_get_perf_event_type(unsigned long eidx)
 		type = PERF_TYPE_HW_CACHE;
 		break;
 	case SBI_PMU_EVENT_TYPE_RAW:
+	case SBI_PMU_EVENT_TYPE_RAW_V2:
 	case SBI_PMU_EVENT_TYPE_FW:
 		type = PERF_TYPE_RAW;
 		break;
@@ -128,6 +129,9 @@ static u64 kvm_pmu_get_perf_event_config(unsigned long eidx, uint64_t evt_data)
 	case SBI_PMU_EVENT_TYPE_RAW:
 		config = evt_data & RISCV_PMU_RAW_EVENT_MASK;
 		break;
+	case SBI_PMU_EVENT_TYPE_RAW_V2:
+		config = evt_data & RISCV_PMU_RAW_EVENT_V2_MASK;
+		break;
 	case SBI_PMU_EVENT_TYPE_FW:
 		if (ecode < SBI_PMU_FW_MAX)
 			config = (1ULL << 63) | ecode;
Signature · 1 line
2.43.0
#4 lore raw

PATCH v6 4/8 drivers/perf: riscv: Implement PMU event info function

In reply to Atish Patra

With the new SBI PMU event info function, we can query the availability of the all standard SBI PMU events at boot time with a single ecall. This improves the bootime by avoiding making an SBI call for each standard PMU event. Since this function is defined only in SBI v3.0, invoke this only if the underlying SBI implementation is v3.0 or higher.

---
 arch/riscv/include/asm/sbi.h |  9 ++++++
 drivers/perf/riscv_pmu_sbi.c | 69 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
arch/riscv/include/asm/sbi.h +9 -0
index b0c41ef56968..5ca7cebc13cc 100644
@@ -136,6 +136,7 @@ enum sbi_ext_pmu_fid {
 	SBI_EXT_PMU_COUNTER_FW_READ,
 	SBI_EXT_PMU_COUNTER_FW_READ_HI,
 	SBI_EXT_PMU_SNAPSHOT_SET_SHMEM,
+	SBI_EXT_PMU_EVENT_GET_INFO,
 };
 
 union sbi_pmu_ctr_info {
@@ -159,6 +160,14 @@ struct riscv_pmu_snapshot_data {
 	u64 reserved[447];
 };
 
+struct riscv_pmu_event_info {
+	u32 event_idx;
+	u32 output;
+	u64 event_data;
+};
+
+#define RISCV_PMU_EVENT_INFO_OUTPUT_MASK 0x01
+
 #define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(47, 0)
 #define RISCV_PMU_PLAT_FW_EVENT_MASK GENMASK_ULL(61, 0)
 /* SBI v3.0 allows extended hpmeventX width value */
drivers/perf/riscv_pmu_sbi.c +69 -0

81 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer Reviewed-by on v6 4/8
#5 lore raw

Re: [PATCH v6 4/8] drivers/perf: riscv: Implement PMU event info function

Quoted 8 lines · Atish Patra

With the new SBI PMU event info function, we can query the availability of the all standard SBI PMU events at boot time with a single ecall. This improves the bootime by avoiding making an SBI call for each standard PMU event. Since this function is defined only in SBI v3.0, invoke this only if the underlying SBI implementation is v3.0 or higher.

LGTM.

Regards,
Anup

Quoted 123 lines
---
 arch/riscv/include/asm/sbi.h |  9 ++++++
 drivers/perf/riscv_pmu_sbi.c | 69 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
arch/riscv/include/asm/sbi.h +9 -0
index b0c41ef56968..5ca7cebc13cc 100644
@@ -136,6 +136,7 @@ enum sbi_ext_pmu_fid {
        SBI_EXT_PMU_COUNTER_FW_READ,
        SBI_EXT_PMU_COUNTER_FW_READ_HI,
        SBI_EXT_PMU_SNAPSHOT_SET_SHMEM,
+       SBI_EXT_PMU_EVENT_GET_INFO,
 };
 
 union sbi_pmu_ctr_info {
@@ -159,6 +160,14 @@ struct riscv_pmu_snapshot_data {
        u64 reserved[447];
 };
 
+struct riscv_pmu_event_info {
+       u32 event_idx;
+       u32 output;
+       u64 event_data;
+};
+
+#define RISCV_PMU_EVENT_INFO_OUTPUT_MASK 0x01
+
 #define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(47, 0)
 #define RISCV_PMU_PLAT_FW_EVENT_MASK GENMASK_ULL(61, 0)
 /* SBI v3.0 allows extended hpmeventX width value */
drivers/perf/riscv_pmu_sbi.c +69 -1
index 3644bed4c8ab..a6c479f853e1 100644
@@ -299,6 +299,66 @@ static struct sbi_pmu_event_data pmu_cache_event_map[PERF_COUNT_HW_CACHE_MAX]
        },
 };
 
+static int pmu_sbi_check_event_info(void)
+{
+       int num_events = ARRAY_SIZE(pmu_hw_event_map) + PERF_COUNT_HW_CACHE_MAX *
+                        PERF_COUNT_HW_CACHE_OP_MAX * PERF_COUNT_HW_CACHE_RESULT_MAX;
+       struct riscv_pmu_event_info *event_info_shmem;
+       phys_addr_t base_addr;
+       int i, j, k, result = 0, count = 0;
+       struct sbiret ret;
+
+       event_info_shmem = kcalloc(num_events, sizeof(*event_info_shmem), GFP_KERNEL);
+       if (!event_info_shmem)
+               return -ENOMEM;
+
+       for (i = 0; i < ARRAY_SIZE(pmu_hw_event_map); i++)
+               event_info_shmem[count++].event_idx = pmu_hw_event_map[i].event_idx;
+
+       for (i = 0; i < ARRAY_SIZE(pmu_cache_event_map); i++) {
+               for (j = 0; j < ARRAY_SIZE(pmu_cache_event_map[i]); j++) {
+                       for (k = 0; k < ARRAY_SIZE(pmu_cache_event_map[i][j]); k++)
+                               event_info_shmem[count++].event_idx =
+                                                       pmu_cache_event_map[i][j][k].event_idx;
+               }
+       }
+
+       base_addr = __pa(event_info_shmem);
+       if (IS_ENABLED(CONFIG_32BIT))
+               ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_EVENT_GET_INFO, lower_32_bits(base_addr),
+                               upper_32_bits(base_addr), count, 0, 0, 0);
+       else
+               ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_EVENT_GET_INFO, base_addr, 0,
+                               count, 0, 0, 0);
+       if (ret.error) {
+               result = -EOPNOTSUPP;
+               goto free_mem;
+       }
+
+       for (i = 0; i < ARRAY_SIZE(pmu_hw_event_map); i++) {
+               if (!(event_info_shmem[i].output & RISCV_PMU_EVENT_INFO_OUTPUT_MASK))
+                       pmu_hw_event_map[i].event_idx = -ENOENT;
+       }
+
+       count = ARRAY_SIZE(pmu_hw_event_map);
+
+       for (i = 0; i < ARRAY_SIZE(pmu_cache_event_map); i++) {
+               for (j = 0; j < ARRAY_SIZE(pmu_cache_event_map[i]); j++) {
+                       for (k = 0; k < ARRAY_SIZE(pmu_cache_event_map[i][j]); k++) {
+                               if (!(event_info_shmem[count].output &
+                                     RISCV_PMU_EVENT_INFO_OUTPUT_MASK))
+                                       pmu_cache_event_map[i][j][k].event_idx = -ENOENT;
+                               count++;
+                       }
+               }
+       }
+
+free_mem:
+       kfree(event_info_shmem);
+
+       return result;
+}
+
 static void pmu_sbi_check_event(struct sbi_pmu_event_data *edata)
 {
        struct sbiret ret;
@@ -316,6 +376,15 @@ static void pmu_sbi_check_event(struct sbi_pmu_event_data *edata)
 
 static void pmu_sbi_check_std_events(struct work_struct *work)
 {
+       int ret;
+
+       if (sbi_v3_available) {
+               ret = pmu_sbi_check_event_info();
+               if (ret)
+                       pr_err("pmu_sbi_check_event_info failed with error %d\n", ret);
+               return;
+       }
+
        for (int i = 0; i < ARRAY_SIZE(pmu_hw_event_map); i++)
                pmu_sbi_check_event(&pmu_hw_event_map[i]);
 
 
--

2.43.0

#6 lore raw

PATCH v6 5/8 drivers/perf: riscv: Export PMU event info function

In reply to Atish Patra

The event mapping function can be used in event info function to find out the corresponding SBI PMU event encoding during the get_event_info function as well. Refactor and export it so that it can be invoked from kvm and internal driver.

---
 drivers/perf/riscv_pmu_sbi.c   | 122 ++++++++++++++++++++++-------------------
 include/linux/perf/riscv_pmu.h |   1 +
 2 files changed, 68 insertions(+), 55 deletions(-)
drivers/perf/riscv_pmu_sbi.c +67 -55
index a6c479f853e1..0392900d828e 100644
@@ -100,6 +100,7 @@ static unsigned int riscv_pmu_irq;
 /* Cache the available counters in a bitmask */
 static unsigned long cmask;
 
+static int pmu_event_find_cache(u64 config);
 struct sbi_pmu_event_data {
 	union {
 		union {
@@ -412,6 +413,71 @@ static bool pmu_sbi_ctr_is_fw(int cidx)
 	return (info->type == SBI_PMU_CTR_TYPE_FW) ? true : false;
 }
 
+int riscv_pmu_get_event_info(u32 type, u64 config, u64 *econfig)
+{
+	int ret = -ENOENT;
+
+	switch (type) {
+	case PERF_TYPE_HARDWARE:
+		if (config >= PERF_COUNT_HW_MAX)
+			return -EINVAL;
+		ret = pmu_hw_event_map[config].event_idx;
+		break;
+	case PERF_TYPE_HW_CACHE:
+		ret = pmu_event_find_cache(config);
+		break;
+	case PERF_TYPE_RAW:
+		/*
+		 * As per SBI v0.3 specification,
+		 *  -- the upper 16 bits must be unused for a hardware raw event.
+		 * As per SBI v2.0 specification,
+		 *  -- the upper 8 bits must be unused for a hardware raw event.
+		 * Bits 63:62 are used to distinguish between raw events
+		 * 00 - Hardware raw event
+		 * 10 - SBI firmware events
+		 * 11 - Risc-V platform specific firmware event
+		 */
+		switch (config >> 62) {
+		case 0:
+			if (sbi_v3_available) {
+			/* Return error any bits [56-63] is set  as it is not allowed by the spec */
+				if (!(config & ~RISCV_PMU_RAW_EVENT_V2_MASK)) {
+					if (econfig)
+						*econfig = config & RISCV_PMU_RAW_EVENT_V2_MASK;
+					ret = RISCV_PMU_RAW_EVENT_V2_IDX;
+				}
+			/* Return error any bits [48-63] is set  as it is not allowed by the spec */
+			} else if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) {
+				if (econfig)
+					*econfig = config & RISCV_PMU_RAW_EVENT_MASK;
+				ret = RISCV_PMU_RAW_EVENT_IDX;
+			}
+			break;
+		case 2:
+			ret = (config & 0xFFFF) | (SBI_PMU_EVENT_TYPE_FW << 16);
+			break;
+		case 3:
+			/*
+			 * For Risc-V platform specific firmware events
+			 * Event code - 0xFFFF
+			 * Event data - raw event encoding
+			 */
+			ret = SBI_PMU_EVENT_TYPE_FW << 16 | RISCV_PLAT_FW_EVENT;
+			if (econfig)
+				*econfig = config & RISCV_PMU_PLAT_FW_EVENT_MASK;
+			break;
+		default:
+			break;
+		}
+		break;
+	default:
+		break;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(riscv_pmu_get_event_info);
+
 /*
  * Returns the counter width of a programmable counter and number of hardware
  * counters. As we don't support heterogeneous CPUs yet, it is okay to just
@@ -577,7 +643,6 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
 {
 	u32 type = event->attr.type;
 	u64 config = event->attr.config;
-	int ret = -ENOENT;
 
 	/*
 	 * Ensure we are finished checking standard hardware events for
@@ -585,60 +650,7 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
 	 */
 	flush_work(&check_std_events_work);
 
-	switch (type) {
-	case PERF_TYPE_HARDWARE:
-		if (config >= PERF_COUNT_HW_MAX)
-			return -EINVAL;
-		ret = pmu_hw_event_map[event->attr.config].event_idx;
-		break;
-	case PERF_TYPE_HW_CACHE:
-		ret = pmu_event_find_cache(config);
-		break;
-	case PERF_TYPE_RAW:
-		/*
-		 * As per SBI v0.3 specification,
-		 *  -- the upper 16 bits must be unused for a hardware raw event.
-		 * As per SBI v2.0 specification,
-		 *  -- the upper 8 bits must be unused for a hardware raw event.
-		 * Bits 63:62 are used to distinguish between raw events
-		 * 00 - Hardware raw event
-		 * 10 - SBI firmware events
-		 * 11 - Risc-V platform specific firmware event
-		 */
-
-		switch (config >> 62) {
-		case 0:
-			if (sbi_v3_available) {
-				if (!(config & ~RISCV_PMU_RAW_EVENT_V2_MASK)) {
-					*econfig = config & RISCV_PMU_RAW_EVENT_V2_MASK;
-					ret = RISCV_PMU_RAW_EVENT_V2_IDX;
-				}
-			} else if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) {
-				*econfig = config & RISCV_PMU_RAW_EVENT_MASK;
-				ret = RISCV_PMU_RAW_EVENT_IDX;
-			}
-			break;
-		case 2:
-			ret = (config & 0xFFFF) | (SBI_PMU_EVENT_TYPE_FW << 16);
-			break;
-		case 3:
-			/*
-			 * For Risc-V platform specific firmware events
-			 * Event code - 0xFFFF
-			 * Event data - raw event encoding
-			 */
-			ret = SBI_PMU_EVENT_TYPE_FW << 16 | RISCV_PLAT_FW_EVENT;
-			*econfig = config & RISCV_PMU_PLAT_FW_EVENT_MASK;
-			break;
-		default:
-			break;
-		}
-		break;
-	default:
-		break;
-	}
-
-	return ret;
+	return riscv_pmu_get_event_info(type, config, econfig);
 }
 
 static void pmu_sbi_snapshot_free(struct riscv_pmu *pmu)
include/linux/perf/riscv_pmu.h +1 -0
index 701974639ff2..f82a28040594 100644
@@ -89,6 +89,7 @@ static inline void riscv_pmu_legacy_skip_init(void) {};
 struct riscv_pmu *riscv_pmu_alloc(void);
 #ifdef CONFIG_RISCV_PMU_SBI
 int riscv_pmu_get_hpm_info(u32 *hw_ctr_width, u32 *num_hw_ctr);
+int riscv_pmu_get_event_info(u32 type, u64 config, u64 *econfig);
 #endif
 
 #endif /* CONFIG_RISCV_PMU */
Signature · 1 line
2.43.0
#7 lore raw

PATCH v6 6/8 RISC-V: KVM: No need of explicit writable slot check

In reply to Atish Patra

There is not much value in checking if a memslot is writable explicitly before a write as it may change underneath after the check. Rather, return invalid address error when write_guest fails as it checks if the slot is writable anyways.

---
 arch/riscv/kvm/vcpu_pmu.c     | 11 ++---------
 arch/riscv/kvm/vcpu_sbi_sta.c |  9 ++-------
 2 files changed, 4 insertions(+), 16 deletions(-)
arch/riscv/kvm/vcpu_pmu.c +2 -9
index 15d71a7b75ba..f8514086bd6b 100644
@@ -409,8 +409,6 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long s
 	int snapshot_area_size = sizeof(struct riscv_pmu_snapshot_data);
 	int sbiret = 0;
 	gpa_t saddr;
-	unsigned long hva;
-	bool writable;
 
 	if (!kvpmu || flags) {
 		sbiret = SBI_ERR_INVALID_PARAM;
@@ -432,19 +430,14 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long s
 		goto out;
 	}
 
-	hva = kvm_vcpu_gfn_to_hva_prot(vcpu, saddr >> PAGE_SHIFT, &writable);
-	if (kvm_is_error_hva(hva) || !writable) {
-		sbiret = SBI_ERR_INVALID_ADDRESS;
-		goto out;
-	}
-
 	kvpmu->sdata = kzalloc(snapshot_area_size, GFP_ATOMIC);
 	if (!kvpmu->sdata)
 		return -ENOMEM;
 
+	/* No need to check writable slot explicitly as kvm_vcpu_write_guest does it internally */
 	if (kvm_vcpu_write_guest(vcpu, saddr, kvpmu->sdata, snapshot_area_size)) {
 		kfree(kvpmu->sdata);
-		sbiret = SBI_ERR_FAILURE;
+		sbiret = SBI_ERR_INVALID_ADDRESS;
 		goto out;
 	}
 
arch/riscv/kvm/vcpu_sbi_sta.c +2 -7
index cc6cb7c8f0e4..caaa28460ca4 100644
@@ -85,8 +85,6 @@ static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
 	unsigned long shmem_phys_hi = cp->a1;
 	u32 flags = cp->a2;
 	struct sbi_sta_struct zero_sta = {0};
-	unsigned long hva;
-	bool writable;
 	gpa_t shmem;
 	int ret;
 
@@ -111,13 +109,10 @@ static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
 			return SBI_ERR_INVALID_ADDRESS;
 	}
 
-	hva = kvm_vcpu_gfn_to_hva_prot(vcpu, shmem >> PAGE_SHIFT, &writable);
-	if (kvm_is_error_hva(hva) || !writable)
-		return SBI_ERR_INVALID_ADDRESS;
-
+	/* No need to check writable slot explicitly as kvm_vcpu_write_guest does it internally */
 	ret = kvm_vcpu_write_guest(vcpu, shmem, &zero_sta, sizeof(zero_sta));
 	if (ret)
-		return SBI_ERR_FAILURE;
+		return SBI_ERR_INVALID_ADDRESS;
 
 	vcpu->arch.sta.shmem = shmem;
 	vcpu->arch.sta.last_steal = current->sched_info.run_delay;
Signature · 1 line
2.43.0
maintainer Reviewed-by on v6 6/8
#8 lore raw

Re: [PATCH v6 6/8] RISC-V: KVM: No need of explicit writable slot check

Quoted 8 lines · Atish Patra

There is not much value in checking if a memslot is writable explicitly before a write as it may change underneath after the check. Rather, return invalid address error when write_guest fails as it checks if the slot is writable anyways.

LGTM.

Regards,
Anup

Quoted 73 lines
---
 arch/riscv/kvm/vcpu_pmu.c     | 11 ++---------
 arch/riscv/kvm/vcpu_sbi_sta.c |  9 ++-------
 2 files changed, 4 insertions(+), 16 deletions(-)
arch/riscv/kvm/vcpu_pmu.c +2 -9
index 15d71a7b75ba..f8514086bd6b 100644
@@ -409,8 +409,6 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long s
        int snapshot_area_size = sizeof(struct riscv_pmu_snapshot_data);
        int sbiret = 0;
        gpa_t saddr;
-       unsigned long hva;
-       bool writable;
 
        if (!kvpmu || flags) {
                sbiret = SBI_ERR_INVALID_PARAM;
@@ -432,19 +430,14 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long s
                goto out;
        }
 
-       hva = kvm_vcpu_gfn_to_hva_prot(vcpu, saddr >> PAGE_SHIFT, &writable);
-       if (kvm_is_error_hva(hva) || !writable) {
-               sbiret = SBI_ERR_INVALID_ADDRESS;
-               goto out;
-       }
-
        kvpmu->sdata = kzalloc(snapshot_area_size, GFP_ATOMIC);
        if (!kvpmu->sdata)
                return -ENOMEM;
 
+       /* No need to check writable slot explicitly as kvm_vcpu_write_guest does it internally */
        if (kvm_vcpu_write_guest(vcpu, saddr, kvpmu->sdata, snapshot_area_size)) {
                kfree(kvpmu->sdata);
-               sbiret = SBI_ERR_FAILURE;
+               sbiret = SBI_ERR_INVALID_ADDRESS;
                goto out;
        }
 
arch/riscv/kvm/vcpu_sbi_sta.c +2 -8
index cc6cb7c8f0e4..caaa28460ca4 100644
@@ -85,8 +85,6 @@ static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
        unsigned long shmem_phys_hi = cp->a1;
        u32 flags = cp->a2;
        struct sbi_sta_struct zero_sta = {0};
-       unsigned long hva;
-       bool writable;
        gpa_t shmem;
        int ret;
 
@@ -111,13 +109,10 @@ static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
                        return SBI_ERR_INVALID_ADDRESS;
        }
 
-       hva = kvm_vcpu_gfn_to_hva_prot(vcpu, shmem >> PAGE_SHIFT, &writable);
-       if (kvm_is_error_hva(hva) || !writable)
-               return SBI_ERR_INVALID_ADDRESS;
-
+       /* No need to check writable slot explicitly as kvm_vcpu_write_guest does it internally */
        ret = kvm_vcpu_write_guest(vcpu, shmem, &zero_sta, sizeof(zero_sta));
        if (ret)
-               return SBI_ERR_FAILURE;
+               return SBI_ERR_INVALID_ADDRESS;
 
        vcpu->arch.sta.shmem = shmem;
        vcpu->arch.sta.last_steal = current->sched_info.run_delay;
 
--

2.43.0

#9 lore raw

PATCH v6 7/8 RISC-V: KVM: Implement get event info function

In reply to Atish Patra

The new get_event_info funciton allows the guest to query the presence of multiple events with single SBI call. Currently, the perf driver in linux guest invokes it for all the standard SBI PMU events. Support the SBI function implementation in KVM as well.

---
 arch/riscv/include/asm/kvm_vcpu_pmu.h |  3 ++
 arch/riscv/kvm/vcpu_pmu.c             | 59 +++++++++++++++++++++++++++++++++++
 arch/riscv/kvm/vcpu_sbi_pmu.c         |  3 ++
 3 files changed, 65 insertions(+)
arch/riscv/include/asm/kvm_vcpu_pmu.h +3 -0
index 1d85b6617508..9a930afc8f57 100644
@@ -98,6 +98,9 @@ void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu);
 int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long saddr_low,
 				      unsigned long saddr_high, unsigned long flags,
 				      struct kvm_vcpu_sbi_return *retdata);
+int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_low,
+				  unsigned long saddr_high, unsigned long num_events,
+				  unsigned long flags, struct kvm_vcpu_sbi_return *retdata);
 void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu);
 void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu);
 
arch/riscv/kvm/vcpu_pmu.c +59 -0

65 lines not on this page · raw message

arch/riscv/kvm/vcpu_sbi_pmu.c +3 -0
index e4be34e03e83..a020d979d179 100644
@@ -73,6 +73,9 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
 	case SBI_EXT_PMU_SNAPSHOT_SET_SHMEM:
 		ret = kvm_riscv_vcpu_pmu_snapshot_set_shmem(vcpu, cp->a0, cp->a1, cp->a2, retdata);
 		break;
+	case SBI_EXT_PMU_EVENT_GET_INFO:
+		ret = kvm_riscv_vcpu_pmu_event_info(vcpu, cp->a0, cp->a1, cp->a2, cp->a3, retdata);
+		break;
 	default:
 		retdata->err_val = SBI_ERR_NOT_SUPPORTED;
 	}
Signature · 1 line
2.43.0
#10 lore raw

PATCH v6 8/8 RISC-V: KVM: Upgrade the supported SBI version to 3.0

In reply to Atish Patra

Upgrade the SBI version to v3.0 so that corresponding features can be enabled in the guest.

---
 arch/riscv/include/asm/kvm_vcpu_sbi.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/include/asm/kvm_vcpu_sbi.h +1 -1
index d678fd7e5973..f9c350ab84d9 100644
@@ -11,7 +11,7 @@
 
 #define KVM_SBI_IMPID 3
 
-#define KVM_SBI_VERSION_MAJOR 2
+#define KVM_SBI_VERSION_MAJOR 3
 #define KVM_SBI_VERSION_MINOR 0
 
 enum kvm_riscv_sbi_ext_status {
Signature · 1 line
2.43.0
maintainer Acked-by on the v6 series
#11 lore raw
In reply to Atish Patra
Quoted 14 lines · Atish Patra

SBI v3.0 specification[1] added two new improvements to the PMU chaper. The SBI v3.0 specification is frozen and under public review phase as per the RISC-V International guidelines.

1. Added an additional get_event_info function to query event availablity in bulk instead of individual SBI calls for each event. This helps in improving the boot time.

2. Raw event width allowed by the platform is widened to have 56 bits with RAW event v2 as per new clarification in the priv ISA[2].

Apart from implementing these new features, this series improves the gpa range check in KVM and updates the kvm SBI implementation to SBI v3.0.

Quoted 7 lines

The opensbi patches have been merged. This series can be found at [3].

[1] https://github.com/riscv-non-isa/riscv-sbi-doc/releases/download/v3.0-rc7/riscv-sbi.pdf
[2] https://github.com/riscv/riscv-isa-manual/issues/1578
[3] https://github.com/atishp04/linux/tree/b4/pmu_event_info_v6

For the series:

- Paul

maintainer
#12 lore raw
Quoted 22 lines · Paul Walmsley
Quoted 20 lines · Atish Patra

SBI v3.0 specification[1] added two new improvements to the PMU chaper. The SBI v3.0 specification is frozen and under public review phase as per the RISC-V International guidelines.

1. Added an additional get_event_info function to query event availablity in bulk instead of individual SBI calls for each event. This helps in improving the boot time.

2. Raw event width allowed by the platform is widened to have 56 bits with RAW event v2 as per new clarification in the priv ISA[2].

Apart from implementing these new features, this series improves the gpa range check in KVM and updates the kvm SBI implementation to SBI v3.0.

The opensbi patches have been merged. This series can be found at [3].

[1] https://github.com/riscv-non-isa/riscv-sbi-doc/releases/download/v3.0-rc7/riscv-sbi.pdf
[2] https://github.com/riscv/riscv-isa-manual/issues/1578
[3] https://github.com/atishp04/linux/tree/b4/pmu_event_info_v6

Quoted 5 lines
Quoted 1 line

For the series:

I was assuming this series would go via the Risc-V arch tree so please shout if you were expecting me to take it via drivers/perf/!

Will

maintainer
#13 lore raw

Hi Will,

Quoted 24 lines · Will Deacon
Quoted 22 lines · Paul Walmsley
Quoted 20 lines · Atish Patra

SBI v3.0 specification[1] added two new improvements to the PMU chaper. The SBI v3.0 specification is frozen and under public review phase as per the RISC-V International guidelines.

1. Added an additional get_event_info function to query event availablity in bulk instead of individual SBI calls for each event. This helps in improving the boot time.

2. Raw event width allowed by the platform is widened to have 56 bits with RAW event v2 as per new clarification in the priv ISA[2].

Apart from implementing these new features, this series improves the gpa range check in KVM and updates the kvm SBI implementation to SBI v3.0.

The opensbi patches have been merged. This series can be found at [3].

[1] https://github.com/riscv-non-isa/riscv-sbi-doc/releases/download/v3.0-rc7/riscv-sbi.pdf
[2] https://github.com/riscv/riscv-isa-manual/issues/1578
[3] https://github.com/atishp04/linux/tree/b4/pmu_event_info_v6

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

For the series:

I was assuming this series would go via the Risc-V arch tree so please shout if you were expecting me to take it via drivers/perf/!

Based on offline discussion with Paul, I will take this series through the KVM RISC-V tree.

Regards,
Anup

#14 lore raw
In reply to Atish Patra
This series was applied to riscv/linux.git (for-next) by Anup Patel <[email protected]>:

Hello:

This series was applied to riscv/linux.git (for-next) by Anup Patel <[email protected]>:

Quoted 9 lines · On Tue, 09 Sep 2025 00:03:19 -0700 you wrote:

SBI v3.0 specification[1] added two new improvements to the PMU chaper. The SBI v3.0 specification is frozen and under public review phase as per the RISC-V International guidelines.

1. Added an additional get_event_info function to query event availablity in bulk instead of individual SBI calls for each event. This helps in improving the boot time.

[...]

Here is the summary with links:
- [v6,1/8] drivers/perf: riscv: Add SBI v3.0 flag

    https://git.kernel.org/riscv/c/8c8d0f002b76
  - [v6,2/8] drivers/perf: riscv: Add raw event v2 support
    https://git.kernel.org/riscv/c/656ef2ea30a9
  - [v6,3/8] RISC-V: KVM: Add support for Raw event v2
    https://git.kernel.org/riscv/c/190b74154299
  - [v6,4/8] drivers/perf: riscv: Implement PMU event info function
    https://git.kernel.org/riscv/c/adffbd06d003
  - [v6,5/8] drivers/perf: riscv: Export PMU event info function
    https://git.kernel.org/riscv/c/880fcc329e24
  - [v6,6/8] RISC-V: KVM: No need of explicit writable slot check
    https://git.kernel.org/riscv/c/41f4d0cc331a
  - [v6,7/8] RISC-V: KVM: Implement get event info function
    https://git.kernel.org/riscv/c/e309fd113b9f
  - [v6,8/8] RISC-V: KVM: Upgrade the supported SBI version to 3.0
    https://git.kernel.org/riscv/c/dbdadd943a27

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html