public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] SBI PMU event related fixes
@ 2024-12-13  0:09 Atish Patra
  2024-12-13  0:09 ` [PATCH v2 1/3] drivers/perf: riscv: Fix Platform firmware event data Atish Patra
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Atish Patra @ 2024-12-13  0:09 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Atish Patra, Anup Patel,
	Will Deacon, Mark Rutland, Mayuresh Chitale, Samuel Holland
  Cc: Palmer Dabbelt, linux-riscv, linux-kernel, Atish Patra

Here are two minor improvement/fixes in the PMU event path. The first patch
was part of the series[1]. The 2nd patch was suggested during the series
review. 

While the series can only be merged once SBI v3.0 is frozen, these two
patches can be independent of SBI v3.0 and can be merged sooner. Hence, these
two patches are sent as a separate series.
 
[1] https://lore.kernel.org/kvm/20241119-pmu_event_info-v1-7-a4f9691421f8@rivosinc.com/T/#u

To: Paul Walmsley <paul.walmsley@sifive.com>
To: Palmer Dabbelt <palmer@dabbelt.com>
To: Atish Patra <atishp@atishpatra.org>
To: Anup Patel <anup@brainfault.org>
To: Will Deacon <will@kernel.org>
To: Mark Rutland <mark.rutland@arm.com>
To: Mayuresh Chitale <mchitale@ventanamicro.com>
To: Samuel Holland <samuel.holland@sifive.com>
Cc: Palmer Dabbelt <palmer@rivosinc.com>
Cc: linux-riscv@lists.infradead.org
Cc: linux-kernel@vger.kernel.org

Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
Changes in v2:
- Removed a drive-by fix from PATCH 1 and created a separate patch.
- Added fixes tag 
- Link to v1: https://lore.kernel.org/r/20241209-pmu_event_fixes-v1-0-d9525e90072c@rivosinc.com

---
Atish Patra (3):
      drivers/perf: riscv: Fix Platform firmware event data
      drivers/perf: riscv: Return error for default case
      drivers/perf: riscv: Do not allow invalid raw event config

 arch/riscv/include/asm/sbi.h |  1 +
 drivers/perf/riscv_pmu_sbi.c | 22 ++++++++++++----------
 2 files changed, 13 insertions(+), 10 deletions(-)
---
base-commit: fac04efc5c793dccbd07e2d59af9f90b7fc0dca4
change-id: 20241212-pmu_event_fixes_v2-c34bab91422d
--
Regards,
Atish patra


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/3] drivers/perf: riscv: Fix Platform firmware event data
  2024-12-13  0:09 [PATCH v2 0/3] SBI PMU event related fixes Atish Patra
@ 2024-12-13  0:09 ` Atish Patra
  2024-12-13  0:09 ` [PATCH v2 2/3] drivers/perf: riscv: Return error for default case Atish Patra
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Atish Patra @ 2024-12-13  0:09 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Atish Patra, Anup Patel,
	Will Deacon, Mark Rutland, Mayuresh Chitale, Samuel Holland
  Cc: Palmer Dabbelt, linux-riscv, linux-kernel, Atish Patra

Platform firmware event data field is allowed to be 62 bits for
Linux as uppper most two bits are reserved to indicate SBI fw or
platform specific firmware events.
However, the event data field is masked as per the hardware raw
event mask which is not correct.

Fix the platform firmware event data field with proper mask.

Fixes: f0c9363db2dd ("perf/riscv-sbi: Add platform specific firmware event handling")

Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
 arch/riscv/include/asm/sbi.h |  1 +
 drivers/perf/riscv_pmu_sbi.c | 12 +++++-------
 2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h
index 6c82318065cf..3d250824178b 100644
--- a/arch/riscv/include/asm/sbi.h
+++ b/arch/riscv/include/asm/sbi.h
@@ -159,6 +159,7 @@ 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)
 #define RISCV_PMU_RAW_EVENT_IDX 0x20000
 #define RISCV_PLAT_FW_EVENT	0xFFFF
 
diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
index 1aa303f76cc7..3473ba02abf3 100644
--- a/drivers/perf/riscv_pmu_sbi.c
+++ b/drivers/perf/riscv_pmu_sbi.c
@@ -507,7 +507,6 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
 {
 	u32 type = event->attr.type;
 	u64 config = event->attr.config;
-	u64 raw_config_val;
 	int ret;
 
 	/*
@@ -528,21 +527,20 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
 	case PERF_TYPE_RAW:
 		/*
 		 * As per SBI specification, the upper 16 bits must be unused
-		 * for a raw event.
+		 * 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
 		 */
-		raw_config_val = config & RISCV_PMU_RAW_EVENT_MASK;
+
 		switch (config >> 62) {
 		case 0:
 			ret = RISCV_PMU_RAW_EVENT_IDX;
-			*econfig = raw_config_val;
+			*econfig = config & RISCV_PMU_RAW_EVENT_MASK;
 			break;
 		case 2:
-			ret = (raw_config_val & 0xFFFF) |
-				(SBI_PMU_EVENT_TYPE_FW << 16);
+			ret = (config & 0xFFFF) | (SBI_PMU_EVENT_TYPE_FW << 16);
 			break;
 		case 3:
 			/*
@@ -551,7 +549,7 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
 			 * Event data - raw event encoding
 			 */
 			ret = SBI_PMU_EVENT_TYPE_FW << 16 | RISCV_PLAT_FW_EVENT;
-			*econfig = raw_config_val;
+			*econfig = config & RISCV_PMU_PLAT_FW_EVENT_MASK;
 			break;
 		}
 		break;

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/3] drivers/perf: riscv: Return error for default case
  2024-12-13  0:09 [PATCH v2 0/3] SBI PMU event related fixes Atish Patra
  2024-12-13  0:09 ` [PATCH v2 1/3] drivers/perf: riscv: Fix Platform firmware event data Atish Patra
@ 2024-12-13  0:09 ` Atish Patra
  2024-12-13  0:09 ` [PATCH v2 3/3] drivers/perf: riscv: Do not allow invalid raw event config Atish Patra
  2025-01-09 19:40 ` [PATCH v2 0/3] SBI PMU event related fixes patchwork-bot+linux-riscv
  3 siblings, 0 replies; 5+ messages in thread
From: Atish Patra @ 2024-12-13  0:09 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Atish Patra, Anup Patel,
	Will Deacon, Mark Rutland, Mayuresh Chitale, Samuel Holland
  Cc: Palmer Dabbelt, linux-riscv, linux-kernel, Atish Patra

If the upper two bits has an invalid valid (0x1), the event mapping
is not reliable as it returns an uninitialized variable.

Return appropriate value for the default case.

Fixes: f0c9363db2dd ("perf/riscv-sbi: Add platform specific firmware event handling")

Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
 drivers/perf/riscv_pmu_sbi.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
index 3473ba02abf3..da3651d32906 100644
--- a/drivers/perf/riscv_pmu_sbi.c
+++ b/drivers/perf/riscv_pmu_sbi.c
@@ -507,7 +507,7 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
 {
 	u32 type = event->attr.type;
 	u64 config = event->attr.config;
-	int ret;
+	int ret = -ENOENT;
 
 	/*
 	 * Ensure we are finished checking standard hardware events for
@@ -551,10 +551,11 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
 			ret = SBI_PMU_EVENT_TYPE_FW << 16 | RISCV_PLAT_FW_EVENT;
 			*econfig = config & RISCV_PMU_PLAT_FW_EVENT_MASK;
 			break;
+		default:
+			break;
 		}
 		break;
 	default:
-		ret = -ENOENT;
 		break;
 	}
 

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 3/3] drivers/perf: riscv: Do not allow invalid raw event config
  2024-12-13  0:09 [PATCH v2 0/3] SBI PMU event related fixes Atish Patra
  2024-12-13  0:09 ` [PATCH v2 1/3] drivers/perf: riscv: Fix Platform firmware event data Atish Patra
  2024-12-13  0:09 ` [PATCH v2 2/3] drivers/perf: riscv: Return error for default case Atish Patra
@ 2024-12-13  0:09 ` Atish Patra
  2025-01-09 19:40 ` [PATCH v2 0/3] SBI PMU event related fixes patchwork-bot+linux-riscv
  3 siblings, 0 replies; 5+ messages in thread
From: Atish Patra @ 2024-12-13  0:09 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Atish Patra, Anup Patel,
	Will Deacon, Mark Rutland, Mayuresh Chitale, Samuel Holland
  Cc: Palmer Dabbelt, linux-riscv, linux-kernel, Atish Patra

The SBI specification allows only lower 48bits of hpmeventX to be
configured via SBI PMU. Currently, the driver masks of the higher
bits but doesn't return an error. This will lead to an additional
SBI call for config matching which should return for an invalid
event error in most of the cases.

However, if a platform(i.e Rocket and sifive cores) implements a
bitmap of all bits in the event encoding this will lead to an
incorrect event being programmed leading to user confusion.

Report the error to the user if higher bits are set during the
event mapping itself to avoid the confusion and save an additional
SBI call.

Suggested-by: Samuel Holland <samuel.holland@sifive.com>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
 drivers/perf/riscv_pmu_sbi.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
index da3651d32906..194c153e5d71 100644
--- a/drivers/perf/riscv_pmu_sbi.c
+++ b/drivers/perf/riscv_pmu_sbi.c
@@ -536,8 +536,11 @@ static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
 
 		switch (config >> 62) {
 		case 0:
-			ret = RISCV_PMU_RAW_EVENT_IDX;
-			*econfig = config & RISCV_PMU_RAW_EVENT_MASK;
+			/* Return error any bits [48-63] is set  as it is not allowed by the spec */
+			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);

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 0/3] SBI PMU event related fixes
  2024-12-13  0:09 [PATCH v2 0/3] SBI PMU event related fixes Atish Patra
                   ` (2 preceding siblings ...)
  2024-12-13  0:09 ` [PATCH v2 3/3] drivers/perf: riscv: Do not allow invalid raw event config Atish Patra
@ 2025-01-09 19:40 ` patchwork-bot+linux-riscv
  3 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+linux-riscv @ 2025-01-09 19:40 UTC (permalink / raw)
  To: Atish Kumar Patra
  Cc: linux-riscv, paul.walmsley, palmer, atishp, anup, will,
	mark.rutland, mchitale, samuel.holland, palmer, linux-kernel

Hello:

This series was applied to riscv/linux.git (fixes)
by Palmer Dabbelt <palmer@rivosinc.com>:

On Thu, 12 Dec 2024 16:09:31 -0800 you wrote:
> Here are two minor improvement/fixes in the PMU event path. The first patch
> was part of the series[1]. The 2nd patch was suggested during the series
> review.
> 
> While the series can only be merged once SBI v3.0 is frozen, these two
> patches can be independent of SBI v3.0 and can be merged sooner. Hence, these
> two patches are sent as a separate series.
> 
> [...]

Here is the summary with links:
  - [v2,1/3] drivers/perf: riscv: Fix Platform firmware event data
    https://git.kernel.org/riscv/c/fc58db9aeb15
  - [v2,2/3] drivers/perf: riscv: Return error for default case
    https://git.kernel.org/riscv/c/2c206cdede56
  - [v2,3/3] drivers/perf: riscv: Do not allow invalid raw event config
    https://git.kernel.org/riscv/c/3aff4cdbe506

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-01-09 19:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-13  0:09 [PATCH v2 0/3] SBI PMU event related fixes Atish Patra
2024-12-13  0:09 ` [PATCH v2 1/3] drivers/perf: riscv: Fix Platform firmware event data Atish Patra
2024-12-13  0:09 ` [PATCH v2 2/3] drivers/perf: riscv: Return error for default case Atish Patra
2024-12-13  0:09 ` [PATCH v2 3/3] drivers/perf: riscv: Do not allow invalid raw event config Atish Patra
2025-01-09 19:40 ` [PATCH v2 0/3] SBI PMU event related fixes patchwork-bot+linux-riscv

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox