public inbox for linux-riscv@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH 1/2] drivers: perf: ctr_get_width function for legacy is not defined
@ 2023-12-05  9:25 Vadim Shakirov
  2023-12-05  9:25 ` [PATCH 2/2] drivers: perf: added capabilities for legacy PMU Vadim Shakirov
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Vadim Shakirov @ 2023-12-05  9:25 UTC (permalink / raw)
  To: Atish Patra
  Cc: Vadim Shakirov, Anup Patel, Will Deacon, Mark Rutland,
	Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
	Andrew Jones, linux-riscv, linux-kernel

With parameters CONFIG_RISCV_PMU_LEGACY=y and CONFIG_RISCV_PMU_SBI=n
linux kernel crashes when you try perf record:

$ perf record ls
[   46.749286] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
[   46.750199] Oops [#1]
[   46.750342] Modules linked in:
[   46.750608] CPU: 0 PID: 107 Comm: perf-exec Not tainted 6.6.0 #2
[   46.750906] Hardware name: riscv-virtio,qemu (DT)
[   46.751184] epc : 0x0
[   46.751430]  ra : arch_perf_update_userpage+0x54/0x13e
[   46.751680] epc : 0000000000000000 ra : ffffffff8072ee52 sp : ff2000000022b8f0
[   46.751958]  gp : ffffffff81505988 tp : ff6000000290d400 t0 : ff2000000022b9c0
[   46.752229]  t1 : 0000000000000001 t2 : 0000000000000003 s0 : ff2000000022b930
[   46.752451]  s1 : ff600000028fb000 a0 : 0000000000000000 a1 : ff600000028fb000
[   46.752673]  a2 : 0000000ae2751268 a3 : 00000000004fb708 a4 : 0000000000000004
[   46.752895]  a5 : 0000000000000000 a6 : 000000000017ffe3 a7 : 00000000000000d2
[   46.753117]  s2 : ff600000028fb000 s3 : 0000000ae2751268 s4 : 0000000000000000
[   46.753338]  s5 : ffffffff8153e290 s6 : ff600000863b9000 s7 : ff60000002961078
[   46.753562]  s8 : ff60000002961048 s9 : ff60000002961058 s10: 0000000000000001
[   46.753783]  s11: 0000000000000018 t3 : ffffffffffffffff t4 : ffffffffffffffff
[   46.754005]  t5 : ff6000000292270c t6 : ff2000000022bb30
[   46.754179] status: 0000000200000100 badaddr: 0000000000000000 cause: 000000000000000c
[   46.754653] Code: Unable to access instruction at 0xffffffffffffffec.
[   46.754939] ---[ end trace 0000000000000000 ]---
[   46.755131] note: perf-exec[107] exited with irqs disabled
[   46.755546] note: perf-exec[107] exited with preempt_count 4

This happens because in the legacy case the ctr_get_width function was not
defined, but it is used in arch_perf_update_userpage.

Also remove extra check in riscv_pmu_ctr_get_width_mask

Fixes: cc4c07c89aad ("drivers: perf: Implement perf event mmap support in the SBI backend")
Signed-off-by: Vadim Shakirov <vadim.shakirov@syntacore.com>
---
 drivers/perf/riscv_pmu.c        | 18 +++++-------------
 drivers/perf/riscv_pmu_legacy.c |  8 +++++++-
 2 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/drivers/perf/riscv_pmu.c b/drivers/perf/riscv_pmu.c
index 0dda70e1ef90..c78a6fd6c57f 100644
--- a/drivers/perf/riscv_pmu.c
+++ b/drivers/perf/riscv_pmu.c
@@ -150,19 +150,11 @@ u64 riscv_pmu_ctr_get_width_mask(struct perf_event *event)
 	struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
 	struct hw_perf_event *hwc = &event->hw;
 
-	if (!rvpmu->ctr_get_width)
-	/**
-	 * If the pmu driver doesn't support counter width, set it to default
-	 * maximum allowed by the specification.
-	 */
-		cwidth = 63;
-	else {
-		if (hwc->idx == -1)
-			/* Handle init case where idx is not initialized yet */
-			cwidth = rvpmu->ctr_get_width(0);
-		else
-			cwidth = rvpmu->ctr_get_width(hwc->idx);
-	}
+	if (hwc->idx == -1)
+		/* Handle init case where idx is not initialized yet */
+		cwidth = rvpmu->ctr_get_width(0);
+	else
+		cwidth = rvpmu->ctr_get_width(hwc->idx);
 
 	return GENMASK_ULL(cwidth, 0);
 }
diff --git a/drivers/perf/riscv_pmu_legacy.c b/drivers/perf/riscv_pmu_legacy.c
index 79fdd667922e..a179ed6ac980 100644
--- a/drivers/perf/riscv_pmu_legacy.c
+++ b/drivers/perf/riscv_pmu_legacy.c
@@ -37,6 +37,12 @@ static int pmu_legacy_event_map(struct perf_event *event, u64 *config)
 	return pmu_legacy_ctr_get_idx(event);
 }
 
+/* cycle & instret are always 64 bit, one bit less according to SBI spec */
+static int pmu_legacy_ctr_get_width(int idx)
+{
+	return 63;
+}
+
 static u64 pmu_legacy_read_ctr(struct perf_event *event)
 {
 	struct hw_perf_event *hwc = &event->hw;
@@ -111,7 +117,7 @@ static void pmu_legacy_init(struct riscv_pmu *pmu)
 	pmu->ctr_stop = NULL;
 	pmu->event_map = pmu_legacy_event_map;
 	pmu->ctr_get_idx = pmu_legacy_ctr_get_idx;
-	pmu->ctr_get_width = NULL;
+	pmu->ctr_get_width = pmu_legacy_ctr_get_width;
 	pmu->ctr_clear_idx = NULL;
 	pmu->ctr_read = pmu_legacy_read_ctr;
 	pmu->event_mapped = pmu_legacy_event_mapped;
-- 
2.34.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* [PATCH 2/2] drivers: perf: added capabilities for legacy PMU
  2023-12-05  9:25 [PATCH 1/2] drivers: perf: ctr_get_width function for legacy is not defined Vadim Shakirov
@ 2023-12-05  9:25 ` Vadim Shakirov
  2023-12-08 21:06   ` Atish Patra
  2023-12-08 21:09 ` [PATCH 1/2] drivers: perf: ctr_get_width function for legacy is not defined Atish Patra
  2024-02-28 15:10 ` patchwork-bot+linux-riscv
  2 siblings, 1 reply; 5+ messages in thread
From: Vadim Shakirov @ 2023-12-05  9:25 UTC (permalink / raw)
  To: Atish Patra
  Cc: Vadim Shakirov, Anup Patel, Will Deacon, Mark Rutland,
	Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
	Andrew Jones, linux-riscv, linux-kernel

Added the PERF_PMU_CAP_NO_INTERRUPT flag because the legacy pmu driver
does not provide sampling capabilities

Added the PERF_PMU_CAP_NO_EXCLUDE flag because the legacy pmu driver
does not provide the ability to disable counter incrementation in
different privilege modes

Suggested-by: Atish Patra <atishp@atishpatra.org>
Signed-off-by: Vadim Shakirov <vadim.shakirov@syntacore.com>
---
 drivers/perf/riscv_pmu_legacy.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/perf/riscv_pmu_legacy.c b/drivers/perf/riscv_pmu_legacy.c
index a179ed6ac980..fa0bccf4edf2 100644
--- a/drivers/perf/riscv_pmu_legacy.c
+++ b/drivers/perf/riscv_pmu_legacy.c
@@ -123,6 +123,8 @@ static void pmu_legacy_init(struct riscv_pmu *pmu)
 	pmu->event_mapped = pmu_legacy_event_mapped;
 	pmu->event_unmapped = pmu_legacy_event_unmapped;
 	pmu->csr_index = pmu_legacy_csr_index;
+	pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
+	pmu->pmu.capabilities |= PERF_PMU_CAP_NO_EXCLUDE;
 
 	perf_pmu_register(&pmu->pmu, "cpu", PERF_TYPE_RAW);
 }
-- 
2.34.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH 2/2] drivers: perf: added capabilities for legacy PMU
  2023-12-05  9:25 ` [PATCH 2/2] drivers: perf: added capabilities for legacy PMU Vadim Shakirov
@ 2023-12-08 21:06   ` Atish Patra
  0 siblings, 0 replies; 5+ messages in thread
From: Atish Patra @ 2023-12-08 21:06 UTC (permalink / raw)
  To: Vadim Shakirov
  Cc: Anup Patel, Will Deacon, Mark Rutland, Paul Walmsley,
	Palmer Dabbelt, Albert Ou, Alexandre Ghiti, Andrew Jones,
	linux-riscv, linux-kernel

On Tue, Dec 5, 2023 at 1:26 AM Vadim Shakirov
<vadim.shakirov@syntacore.com> wrote:
>
> Added the PERF_PMU_CAP_NO_INTERRUPT flag because the legacy pmu driver
> does not provide sampling capabilities
>
> Added the PERF_PMU_CAP_NO_EXCLUDE flag because the legacy pmu driver
> does not provide the ability to disable counter incrementation in
> different privilege modes
>
> Suggested-by: Atish Patra <atishp@atishpatra.org>
> Signed-off-by: Vadim Shakirov <vadim.shakirov@syntacore.com>
> ---
>  drivers/perf/riscv_pmu_legacy.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/drivers/perf/riscv_pmu_legacy.c b/drivers/perf/riscv_pmu_legacy.c
> index a179ed6ac980..fa0bccf4edf2 100644
> --- a/drivers/perf/riscv_pmu_legacy.c
> +++ b/drivers/perf/riscv_pmu_legacy.c
> @@ -123,6 +123,8 @@ static void pmu_legacy_init(struct riscv_pmu *pmu)
>         pmu->event_mapped = pmu_legacy_event_mapped;
>         pmu->event_unmapped = pmu_legacy_event_unmapped;
>         pmu->csr_index = pmu_legacy_csr_index;
> +       pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
> +       pmu->pmu.capabilities |= PERF_PMU_CAP_NO_EXCLUDE;
>
>         perf_pmu_register(&pmu->pmu, "cpu", PERF_TYPE_RAW);
>  }
> --
> 2.34.1
>

Reviewed-by: Atish Patra <atishp@rivosinc.com>

-- 
Regards,
Atish

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH 1/2] drivers: perf: ctr_get_width function for legacy is not defined
  2023-12-05  9:25 [PATCH 1/2] drivers: perf: ctr_get_width function for legacy is not defined Vadim Shakirov
  2023-12-05  9:25 ` [PATCH 2/2] drivers: perf: added capabilities for legacy PMU Vadim Shakirov
@ 2023-12-08 21:09 ` Atish Patra
  2024-02-28 15:10 ` patchwork-bot+linux-riscv
  2 siblings, 0 replies; 5+ messages in thread
From: Atish Patra @ 2023-12-08 21:09 UTC (permalink / raw)
  To: Vadim Shakirov
  Cc: Anup Patel, Will Deacon, Mark Rutland, Paul Walmsley,
	Palmer Dabbelt, Albert Ou, Alexandre Ghiti, Andrew Jones,
	linux-riscv, linux-kernel

On Tue, Dec 5, 2023 at 1:26 AM Vadim Shakirov
<vadim.shakirov@syntacore.com> wrote:
>
> With parameters CONFIG_RISCV_PMU_LEGACY=y and CONFIG_RISCV_PMU_SBI=n
> linux kernel crashes when you try perf record:
>
> $ perf record ls
> [   46.749286] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
> [   46.750199] Oops [#1]
> [   46.750342] Modules linked in:
> [   46.750608] CPU: 0 PID: 107 Comm: perf-exec Not tainted 6.6.0 #2
> [   46.750906] Hardware name: riscv-virtio,qemu (DT)
> [   46.751184] epc : 0x0
> [   46.751430]  ra : arch_perf_update_userpage+0x54/0x13e
> [   46.751680] epc : 0000000000000000 ra : ffffffff8072ee52 sp : ff2000000022b8f0
> [   46.751958]  gp : ffffffff81505988 tp : ff6000000290d400 t0 : ff2000000022b9c0
> [   46.752229]  t1 : 0000000000000001 t2 : 0000000000000003 s0 : ff2000000022b930
> [   46.752451]  s1 : ff600000028fb000 a0 : 0000000000000000 a1 : ff600000028fb000
> [   46.752673]  a2 : 0000000ae2751268 a3 : 00000000004fb708 a4 : 0000000000000004
> [   46.752895]  a5 : 0000000000000000 a6 : 000000000017ffe3 a7 : 00000000000000d2
> [   46.753117]  s2 : ff600000028fb000 s3 : 0000000ae2751268 s4 : 0000000000000000
> [   46.753338]  s5 : ffffffff8153e290 s6 : ff600000863b9000 s7 : ff60000002961078
> [   46.753562]  s8 : ff60000002961048 s9 : ff60000002961058 s10: 0000000000000001
> [   46.753783]  s11: 0000000000000018 t3 : ffffffffffffffff t4 : ffffffffffffffff
> [   46.754005]  t5 : ff6000000292270c t6 : ff2000000022bb30
> [   46.754179] status: 0000000200000100 badaddr: 0000000000000000 cause: 000000000000000c
> [   46.754653] Code: Unable to access instruction at 0xffffffffffffffec.
> [   46.754939] ---[ end trace 0000000000000000 ]---
> [   46.755131] note: perf-exec[107] exited with irqs disabled
> [   46.755546] note: perf-exec[107] exited with preempt_count 4
>
> This happens because in the legacy case the ctr_get_width function was not
> defined, but it is used in arch_perf_update_userpage.
>
> Also remove extra check in riscv_pmu_ctr_get_width_mask
>
> Fixes: cc4c07c89aad ("drivers: perf: Implement perf event mmap support in the SBI backend")
> Signed-off-by: Vadim Shakirov <vadim.shakirov@syntacore.com>
> ---
>  drivers/perf/riscv_pmu.c        | 18 +++++-------------
>  drivers/perf/riscv_pmu_legacy.c |  8 +++++++-
>  2 files changed, 12 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/perf/riscv_pmu.c b/drivers/perf/riscv_pmu.c
> index 0dda70e1ef90..c78a6fd6c57f 100644
> --- a/drivers/perf/riscv_pmu.c
> +++ b/drivers/perf/riscv_pmu.c
> @@ -150,19 +150,11 @@ u64 riscv_pmu_ctr_get_width_mask(struct perf_event *event)
>         struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
>         struct hw_perf_event *hwc = &event->hw;
>
> -       if (!rvpmu->ctr_get_width)
> -       /**
> -        * If the pmu driver doesn't support counter width, set it to default
> -        * maximum allowed by the specification.
> -        */
> -               cwidth = 63;
> -       else {
> -               if (hwc->idx == -1)
> -                       /* Handle init case where idx is not initialized yet */
> -                       cwidth = rvpmu->ctr_get_width(0);
> -               else
> -                       cwidth = rvpmu->ctr_get_width(hwc->idx);
> -       }
> +       if (hwc->idx == -1)
> +               /* Handle init case where idx is not initialized yet */
> +               cwidth = rvpmu->ctr_get_width(0);
> +       else
> +               cwidth = rvpmu->ctr_get_width(hwc->idx);
>
>         return GENMASK_ULL(cwidth, 0);
>  }
> diff --git a/drivers/perf/riscv_pmu_legacy.c b/drivers/perf/riscv_pmu_legacy.c
> index 79fdd667922e..a179ed6ac980 100644
> --- a/drivers/perf/riscv_pmu_legacy.c
> +++ b/drivers/perf/riscv_pmu_legacy.c
> @@ -37,6 +37,12 @@ static int pmu_legacy_event_map(struct perf_event *event, u64 *config)
>         return pmu_legacy_ctr_get_idx(event);
>  }
>
> +/* cycle & instret are always 64 bit, one bit less according to SBI spec */
> +static int pmu_legacy_ctr_get_width(int idx)
> +{
> +       return 63;
> +}
> +
>  static u64 pmu_legacy_read_ctr(struct perf_event *event)
>  {
>         struct hw_perf_event *hwc = &event->hw;
> @@ -111,7 +117,7 @@ static void pmu_legacy_init(struct riscv_pmu *pmu)
>         pmu->ctr_stop = NULL;
>         pmu->event_map = pmu_legacy_event_map;
>         pmu->ctr_get_idx = pmu_legacy_ctr_get_idx;
> -       pmu->ctr_get_width = NULL;
> +       pmu->ctr_get_width = pmu_legacy_ctr_get_width;
>         pmu->ctr_clear_idx = NULL;
>         pmu->ctr_read = pmu_legacy_read_ctr;
>         pmu->event_mapped = pmu_legacy_event_mapped;
> --
> 2.34.1
>


Reviewed-by: Atish Patra <atishp@rivosinc.com>
-- 
Regards,
Atish

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH 1/2] drivers: perf: ctr_get_width function for legacy is not defined
  2023-12-05  9:25 [PATCH 1/2] drivers: perf: ctr_get_width function for legacy is not defined Vadim Shakirov
  2023-12-05  9:25 ` [PATCH 2/2] drivers: perf: added capabilities for legacy PMU Vadim Shakirov
  2023-12-08 21:09 ` [PATCH 1/2] drivers: perf: ctr_get_width function for legacy is not defined Atish Patra
@ 2024-02-28 15:10 ` patchwork-bot+linux-riscv
  2 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+linux-riscv @ 2024-02-28 15:10 UTC (permalink / raw)
  To: Vadim Shakirov
  Cc: linux-riscv, atishp, anup, will, mark.rutland, paul.walmsley,
	palmer, aou, alexghiti, ajones, linux-kernel

Hello:

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

On Tue, 5 Dec 2023 12:25:54 +0300 you wrote:
> With parameters CONFIG_RISCV_PMU_LEGACY=y and CONFIG_RISCV_PMU_SBI=n
> linux kernel crashes when you try perf record:
> 
> $ perf record ls
> [   46.749286] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
> [   46.750199] Oops [#1]
> [   46.750342] Modules linked in:
> [   46.750608] CPU: 0 PID: 107 Comm: perf-exec Not tainted 6.6.0 #2
> [   46.750906] Hardware name: riscv-virtio,qemu (DT)
> [   46.751184] epc : 0x0
> [   46.751430]  ra : arch_perf_update_userpage+0x54/0x13e
> [   46.751680] epc : 0000000000000000 ra : ffffffff8072ee52 sp : ff2000000022b8f0
> [   46.751958]  gp : ffffffff81505988 tp : ff6000000290d400 t0 : ff2000000022b9c0
> [   46.752229]  t1 : 0000000000000001 t2 : 0000000000000003 s0 : ff2000000022b930
> [   46.752451]  s1 : ff600000028fb000 a0 : 0000000000000000 a1 : ff600000028fb000
> [   46.752673]  a2 : 0000000ae2751268 a3 : 00000000004fb708 a4 : 0000000000000004
> [   46.752895]  a5 : 0000000000000000 a6 : 000000000017ffe3 a7 : 00000000000000d2
> [   46.753117]  s2 : ff600000028fb000 s3 : 0000000ae2751268 s4 : 0000000000000000
> [   46.753338]  s5 : ffffffff8153e290 s6 : ff600000863b9000 s7 : ff60000002961078
> [   46.753562]  s8 : ff60000002961048 s9 : ff60000002961058 s10: 0000000000000001
> [   46.753783]  s11: 0000000000000018 t3 : ffffffffffffffff t4 : ffffffffffffffff
> [   46.754005]  t5 : ff6000000292270c t6 : ff2000000022bb30
> [   46.754179] status: 0000000200000100 badaddr: 0000000000000000 cause: 000000000000000c
> [   46.754653] Code: Unable to access instruction at 0xffffffffffffffec.
> [   46.754939] ---[ end trace 0000000000000000 ]---
> [   46.755131] note: perf-exec[107] exited with irqs disabled
> [   46.755546] note: perf-exec[107] exited with preempt_count 4
> 
> [...]

Here is the summary with links:
  - [1/2] drivers: perf: ctr_get_width function for legacy is not defined
    https://git.kernel.org/riscv/c/682dc133f83e
  - [2/2] drivers: perf: added capabilities for legacy PMU
    https://git.kernel.org/riscv/c/65730fe8f4fb

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



_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

end of thread, other threads:[~2024-02-28 15:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-05  9:25 [PATCH 1/2] drivers: perf: ctr_get_width function for legacy is not defined Vadim Shakirov
2023-12-05  9:25 ` [PATCH 2/2] drivers: perf: added capabilities for legacy PMU Vadim Shakirov
2023-12-08 21:06   ` Atish Patra
2023-12-08 21:09 ` [PATCH 1/2] drivers: perf: ctr_get_width function for legacy is not defined Atish Patra
2024-02-28 15:10 ` 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