public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] perf: riscv: Fix selecting counters in legacy mode
@ 2024-07-29 12:58 Shifrin Dmitry
  2024-07-31  7:37 ` Samuel Holland
  2024-08-01 16:40 ` patchwork-bot+linux-riscv
  0 siblings, 2 replies; 4+ messages in thread
From: Shifrin Dmitry @ 2024-07-29 12:58 UTC (permalink / raw)
  To: linux-riscv
  Cc: atishp, anup, will, mark.rutland, paul.walmsley, palmer, aou,
	linux-arm-kernel, linux-kernel, alexghiti, Shifrin Dmitry

It is required to check event type before checking event config.
Events with the different types can have the same config.
This check is missed for legacy mode code

For such perf usage:
    sysctl -w kernel.perf_user_access=2
    perf stat -e cycles,L1-dcache-loads --
driver will try to force both events to CYCLE counter.

This commit implements event type check before forcing
events on the special counters.

Signed-off-by: Shifrin Dmitry <dmitry.shifrin@syntacore.com>
---
 drivers/perf/riscv_pmu_sbi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
index 36d128ff166f..bf14ab282e11 100644
--- a/drivers/perf/riscv_pmu_sbi.c
+++ b/drivers/perf/riscv_pmu_sbi.c
@@ -416,7 +416,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event)
 	 * but not in the user access mode as we want to use the other counters
 	 * that support sampling/filtering.
 	 */
-	if (hwc->flags & PERF_EVENT_FLAG_LEGACY) {
+	if ((hwc->flags & PERF_EVENT_FLAG_LEGACY) && (event->attr.type == PERF_TYPE_HARDWARE)) {
 		if (event->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
 			cflags |= SBI_PMU_CFG_FLAG_SKIP_MATCH;
 			cmask = 1;
-- 
2.34.1


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

* Re: [PATCH] perf: riscv: Fix selecting counters in legacy mode
  2024-07-29 12:58 [PATCH] perf: riscv: Fix selecting counters in legacy mode Shifrin Dmitry
@ 2024-07-31  7:37 ` Samuel Holland
  2024-08-01  8:09   ` Dmitry Shifrin
  2024-08-01 16:40 ` patchwork-bot+linux-riscv
  1 sibling, 1 reply; 4+ messages in thread
From: Samuel Holland @ 2024-07-31  7:37 UTC (permalink / raw)
  To: Shifrin Dmitry, linux-riscv
  Cc: atishp, anup, will, mark.rutland, paul.walmsley, palmer, aou,
	linux-arm-kernel, linux-kernel, alexghiti

Hi Dmitry,

On 2024-07-29 7:58 AM, Shifrin Dmitry wrote:
> It is required to check event type before checking event config.
> Events with the different types can have the same config.
> This check is missed for legacy mode code
> 
> For such perf usage:
>     sysctl -w kernel.perf_user_access=2
>     perf stat -e cycles,L1-dcache-loads --
> driver will try to force both events to CYCLE counter.
> 
> This commit implements event type check before forcing
> events on the special counters.

It looks like pmu_sbi_event_mapped() and pmu_sbi_event_mapped() have a similar
bug where they do not check event->attr.type.

Regards,
Samuel

> 
> Signed-off-by: Shifrin Dmitry <dmitry.shifrin@syntacore.com>
> ---
>  drivers/perf/riscv_pmu_sbi.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
> index 36d128ff166f..bf14ab282e11 100644
> --- a/drivers/perf/riscv_pmu_sbi.c
> +++ b/drivers/perf/riscv_pmu_sbi.c
> @@ -416,7 +416,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event)
>  	 * but not in the user access mode as we want to use the other counters
>  	 * that support sampling/filtering.
>  	 */
> -	if (hwc->flags & PERF_EVENT_FLAG_LEGACY) {
> +	if ((hwc->flags & PERF_EVENT_FLAG_LEGACY) && (event->attr.type == PERF_TYPE_HARDWARE)) {
>  		if (event->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
>  			cflags |= SBI_PMU_CFG_FLAG_SKIP_MATCH;
>  			cmask = 1;


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

* Re: [PATCH] perf: riscv: Fix selecting counters in legacy mode
  2024-07-31  7:37 ` Samuel Holland
@ 2024-08-01  8:09   ` Dmitry Shifrin
  0 siblings, 0 replies; 4+ messages in thread
From: Dmitry Shifrin @ 2024-08-01  8:09 UTC (permalink / raw)
  To: linux-riscv@lists.infradead.org, samuel.holland@sifive.com
  Cc: alexghiti@rivosinc.com, linux-kernel@vger.kernel.org,
	mark.rutland@arm.com, paul.walmsley@sifive.com,
	atishp@atishpatra.org, palmer@dabbelt.com,
	linux-arm-kernel@lists.infradead.org, anup@brainfault.org,
	will@kernel.org, aou@eecs.berkeley.edu

Hi Samuel,

On Wed, 2024-07-31 at 02:37 -0500, Samuel Holland wrote:
> Hi Dmitry,
> 
> On 2024-07-29 7:58 AM, Shifrin Dmitry wrote:
> > It is required to check event type before checking event config.
> > Events with the different types can have the same config.
> > This check is missed for legacy mode code
> > 
> > For such perf usage:
> >     sysctl -w kernel.perf_user_access=2
> >     perf stat -e cycles,L1-dcache-loads --
> > driver will try to force both events to CYCLE counter.
> > 
> > This commit implements event type check before forcing
> > events on the special counters.
> 
> It looks like pmu_sbi_event_mapped() and pmu_sbi_event_mapped() have a similar
> bug where they do not check event->attr.type.

I am not sure that pmu_sbi_event_unmapped() and pmu_sbi_event_mapped() should be changed in the same way.
These functions in legacy mode just set/clear PERF_EVENT_FLAG_USER_READ_CNT for legacy events without any
mapping/unmapping. However in pmu_sbi_starting_cpu() CYCLE, TIME and INSTRET counters are always mapped to
user space in legacy mode. So it looks like in legacy mode we can just use

: if (event->hw.flags & PERF_EVENT_FLAG_LEGACY)
:	return;

Thanks, Dmitry

> 
> Regards,
> Samuel
> 
> > 
> > Signed-off-by: Shifrin Dmitry <dmitry.shifrin@syntacore.com>
> > ---
> >  drivers/perf/riscv_pmu_sbi.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
> > index 36d128ff166f..bf14ab282e11 100644
> > --- a/drivers/perf/riscv_pmu_sbi.c
> > +++ b/drivers/perf/riscv_pmu_sbi.c
> > @@ -416,7 +416,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event)
> >          * but not in the user access mode as we want to use the other counters
> >          * that support sampling/filtering.
> >          */
> > -       if (hwc->flags & PERF_EVENT_FLAG_LEGACY) {
> > +       if ((hwc->flags & PERF_EVENT_FLAG_LEGACY) && (event->attr.type == PERF_TYPE_HARDWARE)) {
> >                 if (event->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
> >                         cflags |= SBI_PMU_CFG_FLAG_SKIP_MATCH;
> >                         cmask = 1;
> 


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

* Re: [PATCH] perf: riscv: Fix selecting counters in legacy mode
  2024-07-29 12:58 [PATCH] perf: riscv: Fix selecting counters in legacy mode Shifrin Dmitry
  2024-07-31  7:37 ` Samuel Holland
@ 2024-08-01 16:40 ` patchwork-bot+linux-riscv
  1 sibling, 0 replies; 4+ messages in thread
From: patchwork-bot+linux-riscv @ 2024-08-01 16:40 UTC (permalink / raw)
  To: Dmitry Shifrin
  Cc: linux-riscv, atishp, anup, will, mark.rutland, paul.walmsley,
	palmer, aou, linux-arm-kernel, linux-kernel, alexghiti

Hello:

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

On Mon, 29 Jul 2024 15:58:58 +0300 you wrote:
> It is required to check event type before checking event config.
> Events with the different types can have the same config.
> This check is missed for legacy mode code
> 
> For such perf usage:
>     sysctl -w kernel.perf_user_access=2
>     perf stat -e cycles,L1-dcache-loads --
> driver will try to force both events to CYCLE counter.
> 
> [...]

Here is the summary with links:
  - perf: riscv: Fix selecting counters in legacy mode
    https://git.kernel.org/riscv/c/941a8e9b7a86

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] 4+ messages in thread

end of thread, other threads:[~2024-08-01 16:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-29 12:58 [PATCH] perf: riscv: Fix selecting counters in legacy mode Shifrin Dmitry
2024-07-31  7:37 ` Samuel Holland
2024-08-01  8:09   ` Dmitry Shifrin
2024-08-01 16:40 ` 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