Skip to contentSkip to conversation

libperf and arm64 userspace counter access support

PATCH Superseded Rob Herring 4 participants 23 messages Sep 11, 2020 – Sep 29, 2020 1 message not in the archive

Subsystems (5)

v3 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v3 cover libperf and arm64 userspace counter access support

This is resurrecting Raphael's series[1] to enable userspace counter access on arm64. My previous versions are here[2][3].

Changes in v3:
- Dropped removing x86 rdpmc test until libperf tests can run via 'perf test'
- Added verbose prints for tests
- Split adding perf_evsel__mmap() to separate patch

The following changes to the arm64 support have been made compared to Raphael's last version:

The major change is support for heterogeneous systems with some restrictions. Specifically, userspace must pin itself to like CPUs, open a specific PMU by type, and use h/w specific events. The tests have been reworked to demonstrate this.

Chained events are not supported. The problem with supporting chained events was there's no way to distinguish between a chained event and a native 64-bit counter. We could add some flag, but do self monitoring processes really need that? Native 64-bit counters are supported if the PMU h/w has support. As there's already an explicit ABI to request 64-bit counters, userspace can request 64-bit counters and if user access is not enabled, then it must retry with 32-bit counters.

Prior versions broke the build on arm32 (surprisingly never caught by 0-day). As a result, event_mapped and event_unmapped implementations have been moved into the arm64 code.

There was a bug in that pmc_width was not set in the user page. The tests now check for this.

The documentation has been converted to rST. I've added sections on chained events and heterogeneous.

The tests have been expanded to test the cycle counter access.

Rob

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[3] https://lore.kernel.org/linux-arm-kernel/[email protected]/

Raphael Gault (4):
arm64: pmu: Add hook to handle pmu-related undefined instructions
arm64: pmu: Add function implementation to update event index in
userpage
arm64: perf: Enable pmu counter direct access for perf event on armv8
Documentation: arm64: Document PMU counters access from userspace

Rob Herring (6):
tools/include: Add an initial math64.h
libperf: Add libperf_evsel__mmap()
libperf: tests: Add support for verbose printing
libperf: Add support for user space counter access
libperf: Add arm64 support to perf_mmap__read_self()
perf: arm64: Add test for userspace counter access on heterogeneous
systems

 Documentation/arm64/index.rst                 |   1 +
 .../arm64/perf_counter_user_access.rst        |  56 ++++++
 arch/arm64/include/asm/mmu.h                  |   5 +
 arch/arm64/include/asm/mmu_context.h          |   2 +
 arch/arm64/include/asm/perf_event.h           |  14 ++
 arch/arm64/kernel/cpufeature.c                |   4 +-
 arch/arm64/kernel/perf_event.c                | 116 +++++++++++
 include/linux/perf/arm_pmu.h                  |   2 +
 tools/include/linux/math64.h                  |  75 +++++++
 tools/lib/perf/Documentation/libperf.txt      |   1 +
 tools/lib/perf/evsel.c                        |  34 ++++
 tools/lib/perf/include/internal/evsel.h       |   2 +
 tools/lib/perf/include/internal/mmap.h        |   3 +
 tools/lib/perf/include/internal/tests.h       |  32 +++
 tools/lib/perf/include/perf/evsel.h           |   2 +
 tools/lib/perf/libperf.map                    |   1 +
 tools/lib/perf/mmap.c                         | 188 ++++++++++++++++++
 tools/lib/perf/tests/Makefile                 |   4 +-
 tools/lib/perf/tests/test-evsel.c             |  63 ++++++
 tools/perf/arch/arm64/include/arch-tests.h    |   7 +
 tools/perf/arch/arm64/tests/Build             |   1 +
 tools/perf/arch/arm64/tests/arch-tests.c      |   4 +
 tools/perf/arch/arm64/tests/user-events.c     | 170 ++++++++++++++++
 23 files changed, 784 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/arm64/perf_counter_user_access.rst
 create mode 100644 tools/include/linux/math64.h
 create mode 100644 tools/perf/arch/arm64/tests/user-events.c

--
2.25.1

#1 lore raw

PATCH v3 1/10 arm64: pmu: Add hook to handle pmu-related undefined instructions

From: Raphael Gault <[email protected]>

This patch introduces a protection for the userspace processes which are trying to access the registers from the pmu registers on a big.LITTLE environment. It introduces a hook to handle undefined instructions.

The goal here is to prevent the process to be interrupted by a signal when the error is caused by the task being scheduled while accessing a counter, causing the counter access to be invalid. As we are not able to know efficiently the number of counters available physically on both pmu in that context we consider that any faulting access to a counter which is architecturally correct should not cause a SIGILL signal if the permissions are set accordingly.

This commit also modifies the mask of the mrs_hook declared in arch/arm64/kernel/cpufeatures.c which emulates only feature register access. This is necessary because this hook's mask was too large and thus masking any mrs instruction, even if not related to the emulated registers which made the pmu emulation inefficient.

---
v2:
- Fix warning for set but unused sys_reg

---
 arch/arm64/kernel/cpufeature.c |  4 +--
 arch/arm64/kernel/perf_event.c | 54 ++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 2 deletions(-)
arch/arm64/kernel/cpufeature.c +2 -2
index a389b999482e..00bf53ffd9b0 100644
@@ -2811,8 +2811,8 @@ static int emulate_mrs(struct pt_regs *regs, u32 insn)
 }
 
 static struct undef_hook mrs_hook = {
-	.instr_mask = 0xfff00000,
-	.instr_val  = 0xd5300000,
+	.instr_mask = 0xffff0000,
+	.instr_val  = 0xd5380000,
 	.pstate_mask = PSR_AA32_MODE_MASK,
 	.pstate_val = PSR_MODE_EL0t,
 	.fn = emulate_mrs,
arch/arm64/kernel/perf_event.c +54 -0

69 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#2 lore raw

Re: [PATCH v3 01/10] arm64: pmu: Add hook to handle pmu-related undefined instructions

Quoted 82 lines · Rob Herring

From: Raphael Gault <[email protected]>

This patch introduces a protection for the userspace processes which are trying to access the registers from the pmu registers on a big.LITTLE environment. It introduces a hook to handle undefined instructions.

The goal here is to prevent the process to be interrupted by a signal when the error is caused by the task being scheduled while accessing a counter, causing the counter access to be invalid. As we are not able to know efficiently the number of counters available physically on both pmu in that context we consider that any faulting access to a counter which is architecturally correct should not cause a SIGILL signal if the permissions are set accordingly.

This commit also modifies the mask of the mrs_hook declared in arch/arm64/kernel/cpufeatures.c which emulates only feature register access. This is necessary because this hook's mask was too large and thus masking any mrs instruction, even if not related to the emulated registers which made the pmu emulation inefficient.

---
v2:
- Fix warning for set but unused sys_reg

---
 arch/arm64/kernel/cpufeature.c |  4 +--
 arch/arm64/kernel/perf_event.c | 54 ++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 2 deletions(-)
arch/arm64/kernel/cpufeature.c +2 -2
index a389b999482e..00bf53ffd9b0 100644
@@ -2811,8 +2811,8 @@ static int emulate_mrs(struct pt_regs *regs, u32 insn)
 }
 
 static struct undef_hook mrs_hook = {
-	.instr_mask = 0xfff00000,
-	.instr_val  = 0xd5300000,
+	.instr_mask = 0xffff0000,
+	.instr_val  = 0xd5380000,
 	.pstate_mask = PSR_AA32_MODE_MASK,
 	.pstate_val = PSR_MODE_EL0t,
 	.fn = emulate_mrs,
arch/arm64/kernel/perf_event.c +19 -0
index 462f9a9cc44b..70538ae684da 100644
@@ -8,9 +8,11 @@
  * This code is based heavily on the ARMv7 perf event code.
  */
 
+#include <asm/cpu.h>
 #include <asm/irq_regs.h>
 #include <asm/perf_event.h>
 #include <asm/sysreg.h>
+#include <asm/traps.h>
 #include <asm/virt.h>
 
 #include <clocksource/arm_arch_timer.h>
@@ -1016,6 +1018,58 @@ static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
 	return probe.present ? 0 : -ENODEV;
 }
 
+static int emulate_pmu(struct pt_regs *regs, u32 insn)
+{
+	u32 rt;
+	u32 pmuserenr;
+
+	rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
+	pmuserenr = read_sysreg(pmuserenr_el0);
+
+	if ((pmuserenr & (ARMV8_PMU_USERENR_ER|ARMV8_PMU_USERENR_CR)) !=
+	    (ARMV8_PMU_USERENR_ER|ARMV8_PMU_USERENR_CR))
+		return -EINVAL;
+
+
+	/*
+	 * Userspace is expected to only use this in the context of the scheme
+	 * described in the struct perf_event_mmap_page comments.
+	 *

arch/arm64/kernel/perf_event.c static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu) · line 1047

Quoted 10 lines
arch/arm64/kernel/perf_event.c +10 -0
+	 * Given that context, we can only get here if we got migrated between
+	 * getting the register index and doing the MSR read.  This in turn
+	 * implies we'll fail the sequence and retry, so any value returned is
+	 * 'good', all we need is to be non-fatal.
+	 *
+	 * The choice of the value 0 is comming from the fact that when
+	 * accessing a register which is not counting events but is accessible,
+	 * we get 0.
+	 */
+	pt_regs_write_reg(regs, rt, 0);

Hmm... this feels pretty fragile since, although we may expect userspace only to trigger this in the context of the specific perf use-case, we don't have a way to detect that, so the ABI we're exposing is that EL0 accesses to non-existent counters will return 0. I don't really think that's something we want to commit to.

When restartable sequences were added to the kernel, one of the proposed use-cases was to allow PMU access on big/little systems, because the sequence will abort on preemption. Taking that approach removes the need for this emulation hook entirely. Is that something we can rely on instead of this emulation hook?

Cheers,

Will

#3 lore raw

Re: [PATCH v3 01/10] arm64: pmu: Add hook to handle pmu-related undefined instructions

Quoted 101 lines · Will Deacon
Quoted 82 lines · Rob Herring

From: Raphael Gault <[email protected]>

This patch introduces a protection for the userspace processes which are trying to access the registers from the pmu registers on a big.LITTLE environment. It introduces a hook to handle undefined instructions.

The goal here is to prevent the process to be interrupted by a signal when the error is caused by the task being scheduled while accessing a counter, causing the counter access to be invalid. As we are not able to know efficiently the number of counters available physically on both pmu in that context we consider that any faulting access to a counter which is architecturally correct should not cause a SIGILL signal if the permissions are set accordingly.

This commit also modifies the mask of the mrs_hook declared in arch/arm64/kernel/cpufeatures.c which emulates only feature register access. This is necessary because this hook's mask was too large and thus masking any mrs instruction, even if not related to the emulated registers which made the pmu emulation inefficient.

---
v2:
- Fix warning for set but unused sys_reg

---
 arch/arm64/kernel/cpufeature.c |  4 +--
 arch/arm64/kernel/perf_event.c | 54 ++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 2 deletions(-)
arch/arm64/kernel/cpufeature.c +2 -2
index a389b999482e..00bf53ffd9b0 100644
@@ -2811,8 +2811,8 @@ static int emulate_mrs(struct pt_regs *regs, u32 insn)
 }
 
 static struct undef_hook mrs_hook = {
-     .instr_mask = 0xfff00000,
-     .instr_val  = 0xd5300000,
+     .instr_mask = 0xffff0000,
+     .instr_val  = 0xd5380000,
      .pstate_mask = PSR_AA32_MODE_MASK,
      .pstate_val = PSR_MODE_EL0t,
      .fn = emulate_mrs,
arch/arm64/kernel/perf_event.c +19 -0
index 462f9a9cc44b..70538ae684da 100644
@@ -8,9 +8,11 @@
  * This code is based heavily on the ARMv7 perf event code.
  */
 
+#include <asm/cpu.h>
 #include <asm/irq_regs.h>
 #include <asm/perf_event.h>
 #include <asm/sysreg.h>
+#include <asm/traps.h>
 #include <asm/virt.h>
 
 #include <clocksource/arm_arch_timer.h>
@@ -1016,6 +1018,58 @@ static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
      return probe.present ? 0 : -ENODEV;
 }
 
+static int emulate_pmu(struct pt_regs *regs, u32 insn)
+{
+     u32 rt;
+     u32 pmuserenr;
+
+     rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
+     pmuserenr = read_sysreg(pmuserenr_el0);
+
+     if ((pmuserenr & (ARMV8_PMU_USERENR_ER|ARMV8_PMU_USERENR_CR)) !=
+         (ARMV8_PMU_USERENR_ER|ARMV8_PMU_USERENR_CR))
+             return -EINVAL;
+
+
+     /*
+      * Userspace is expected to only use this in the context of the scheme
+      * described in the struct perf_event_mmap_page comments.
+      *

arch/arm64/kernel/perf_event.c static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu) · line 1047

Quoted 10 lines
arch/arm64/kernel/perf_event.c +10 -0
+      * Given that context, we can only get here if we got migrated between
+      * getting the register index and doing the MSR read.  This in turn
+      * implies we'll fail the sequence and retry, so any value returned is
+      * 'good', all we need is to be non-fatal.
+      *
+      * The choice of the value 0 is comming from the fact that when
+      * accessing a register which is not counting events but is accessible,
+      * we get 0.
+      */
+     pt_regs_write_reg(regs, rt, 0);

Hmm... this feels pretty fragile since, although we may expect userspace only to trigger this in the context of the specific perf use-case, we don't have a way to detect that, so the ABI we're exposing is that EL0 accesses to non-existent counters will return 0. I don't really think that's something we want to commit to.

Quoted 5 lines

When restartable sequences were added to the kernel, one of the proposed use-cases was to allow PMU access on big/little systems, because the sequence will abort on preemption. Taking that approach removes the need for this emulation hook entirely. Is that something we can rely on instead of this emulation hook?

So back to the RFC version[1]!? That would mean pulling librseq into the kernel based on the prior discussion. It doesn't look like that has happened yet.

Why not just drop the undef hook? For heterogeneous systems, we require userspace to pin itself to cores for a specific PMU. See patch
9. If userspace fails to do that, then it gets to keep the pieces.

Rob

[1] https://lore.kernel.org/linux-arm-kernel/20190625134117.r3gysn7mvzzdrytj@willie-the-truck/

maintainer
#4 lore raw

Re: [PATCH v3 01/10] arm64: pmu: Add hook to handle pmu-related undefined instructions

Quoted 42 lines · Rob Herring
Quoted 35 lines · Will Deacon
Quoted 17 lines · Rob Herring
arch/arm64/kernel/perf_event.c +17 -0
+static int emulate_pmu(struct pt_regs *regs, u32 insn)
+{
+     u32 rt;
+     u32 pmuserenr;
+
+     rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
+     pmuserenr = read_sysreg(pmuserenr_el0);
+
+     if ((pmuserenr & (ARMV8_PMU_USERENR_ER|ARMV8_PMU_USERENR_CR)) !=
+         (ARMV8_PMU_USERENR_ER|ARMV8_PMU_USERENR_CR))
+             return -EINVAL;
+
+
+     /*
+      * Userspace is expected to only use this in the context of the scheme
+      * described in the struct perf_event_mmap_page comments.
+      *

arch/arm64/kernel/perf_event.c static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu) · line 1047

Quoted 10 lines
arch/arm64/kernel/perf_event.c +10 -0
+      * Given that context, we can only get here if we got migrated between
+      * getting the register index and doing the MSR read.  This in turn
+      * implies we'll fail the sequence and retry, so any value returned is
+      * 'good', all we need is to be non-fatal.
+      *
+      * The choice of the value 0 is comming from the fact that when
+      * accessing a register which is not counting events but is accessible,
+      * we get 0.
+      */
+     pt_regs_write_reg(regs, rt, 0);

Hmm... this feels pretty fragile since, although we may expect userspace only to trigger this in the context of the specific perf use-case, we don't have a way to detect that, so the ABI we're exposing is that EL0 accesses to non-existent counters will return 0. I don't really think that's something we want to commit to.

Quoted 5 lines

When restartable sequences were added to the kernel, one of the proposed use-cases was to allow PMU access on big/little systems, because the sequence will abort on preemption. Taking that approach removes the need for this emulation hook entirely. Is that something we can rely on instead of this emulation hook?

Quoted 3 lines

So back to the RFC version[1]!? That would mean pulling librseq into the kernel based on the prior discussion. It doesn't look like that has happened yet.

Yeah, or just don't bother supporting heterogeneous systems with this for now.

Quoted 3 lines

Why not just drop the undef hook? For heterogeneous systems, we require userspace to pin itself to cores for a specific PMU. See patch
9. If userspace fails to do that, then it gets to keep the pieces.

Dropping it works too!

Will

#5 lore raw

Re: [PATCH v3 01/10] arm64: pmu: Add hook to handle pmu-related undefined instructions

Quoted 44 lines · Will Deacon
Quoted 42 lines · Rob Herring
Quoted 40 lines · Will Deacon
Quoted 17 lines · Rob Herring
arch/arm64/kernel/perf_event.c +17 -0
+static int emulate_pmu(struct pt_regs *regs, u32 insn)
+{
+     u32 rt;
+     u32 pmuserenr;
+
+     rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
+     pmuserenr = read_sysreg(pmuserenr_el0);
+
+     if ((pmuserenr & (ARMV8_PMU_USERENR_ER|ARMV8_PMU_USERENR_CR)) !=
+         (ARMV8_PMU_USERENR_ER|ARMV8_PMU_USERENR_CR))
+             return -EINVAL;
+
+
+     /*
+      * Userspace is expected to only use this in the context of the scheme
+      * described in the struct perf_event_mmap_page comments.
+      *

arch/arm64/kernel/perf_event.c static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu) · line 1047

Quoted 10 lines
arch/arm64/kernel/perf_event.c +10 -0
+      * Given that context, we can only get here if we got migrated between
+      * getting the register index and doing the MSR read.  This in turn
+      * implies we'll fail the sequence and retry, so any value returned is
+      * 'good', all we need is to be non-fatal.
+      *
+      * The choice of the value 0 is comming from the fact that when
+      * accessing a register which is not counting events but is accessible,
+      * we get 0.
+      */
+     pt_regs_write_reg(regs, rt, 0);

Hmm... this feels pretty fragile since, although we may expect userspace only to trigger this in the context of the specific perf use-case, we don't have a way to detect that, so the ABI we're exposing is that EL0 accesses to non-existent counters will return 0. I don't really think that's something we want to commit to.

When restartable sequences were added to the kernel, one of the proposed use-cases was to allow PMU access on big/little systems, because the sequence will abort on preemption. Taking that approach removes the need for this emulation hook entirely. Is that something we can rely on instead of this emulation hook?

Quoted 6 lines
Quoted 3 lines

So back to the RFC version[1]!? That would mean pulling librseq into the kernel based on the prior discussion. It doesn't look like that has happened yet.

Yeah, or just don't bother supporting heterogeneous systems with this for now.

But the people are asking for it. :)

Quoted 5 lines
Quoted 3 lines

Why not just drop the undef hook? For heterogeneous systems, we require userspace to pin itself to cores for a specific PMU. See patch
9. If userspace fails to do that, then it gets to keep the pieces.

Dropping it works too!

Great. I asked Mark R to comment in case I'm forgetting some other reason.

Rob

#6 lore raw

PATCH v3 2/10 arm64: pmu: Add function implementation to update event index in userpage

In reply to Rob Herring

From: Raphael Gault <[email protected]>

In order to be able to access the counter directly for userspace, we need to provide the index of the counter using the userpage. We thus need to override the event_idx function to retrieve and convert the perf_event index to armv8 hardware index.

Since the arm_pmu driver can be used by any implementation, even if not armv8, two components play a role into making sure the behaviour is correct and consistent with the PMU capabilities:

* the ARMPMU_EL0_RD_CNTR flag which denotes the capability to access counter from userspace.
* the event_idx call back, which is implemented and initialized by the PMU implementation: if no callback is provided, the default behaviour applies, returning 0 as index value.

---
 arch/arm64/kernel/perf_event.c | 21 +++++++++++++++++++++
 include/linux/perf/arm_pmu.h   |  2 ++
 2 files changed, 23 insertions(+)
arch/arm64/kernel/perf_event.c +21 -0
index 70538ae684da..2727d126cecd 100644
@@ -820,6 +820,22 @@ static void armv8pmu_clear_event_idx(struct pmu_hw_events *cpuc,
 		clear_bit(idx - 1, cpuc->used_mask);
 }
 
+static int armv8pmu_access_event_idx(struct perf_event *event)
+{
+	if (!(event->hw.flags & ARMPMU_EL0_RD_CNTR))
+		return 0;
+
+	/*
+	 * We remap the cycle counter index to 32 to
+	 * match the offset applied to the rest of
+	 * the counter indices.
+	 */
+	if (event->hw.idx == ARMV8_IDX_CYCLE_COUNTER)
+		return 32;
+
+	return event->hw.idx;
+}
+
 /*
  * Add an event filter to a given event.
  */
@@ -916,6 +932,9 @@ static int __armv8_pmuv3_map_event(struct perf_event *event,
 	if (armv8pmu_event_is_64bit(event))
 		event->hw.flags |= ARMPMU_EVT_64BIT;
 
+	if (!armv8pmu_event_is_chained(event))
+		event->hw.flags |= ARMPMU_EL0_RD_CNTR;
+
 	/* Only expose micro/arch events supported by this PMU */
 	if ((hw_event_id > 0) && (hw_event_id < ARMV8_PMUV3_MAX_COMMON_EVENTS)
 	    && test_bit(hw_event_id, armpmu->pmceid_bitmap)) {
@@ -1092,6 +1111,8 @@ static int armv8_pmu_init(struct arm_pmu *cpu_pmu, char *name,
 	cpu_pmu->set_event_filter	= armv8pmu_set_event_filter;
 	cpu_pmu->filter_match		= armv8pmu_filter_match;
 
+	cpu_pmu->pmu.event_idx		= armv8pmu_access_event_idx;
+
 	cpu_pmu->name			= name;
 	cpu_pmu->map_event		= map_event;
 	cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] = events ?
include/linux/perf/arm_pmu.h +2 -0
index 5b616dde9a4c..74fbbbd29dc7 100644
@@ -26,6 +26,8 @@
  */
 /* Event uses a 64bit counter */
 #define ARMPMU_EVT_64BIT		1
+/* Allow access to hardware counter from userspace */
+#define ARMPMU_EL0_RD_CNTR		2
 
 #define HW_OP_UNSUPPORTED		0xFFFF
 #define C(_x)				PERF_COUNT_HW_CACHE_##_x
Signature · 1 line
2.25.1
#7 lore raw

PATCH v3 3/10 arm64: perf: Enable pmu counter direct access for perf event on armv8

In reply to Rob Herring

From: Raphael Gault <[email protected]>

Keep track of event opened with direct access to the hardware counters and modify permissions while they are open.

The strategy used here is the same which x86 uses: everytime an event is mapped, the permissions are set if required. The atomic field added in the mm_context helps keep track of the different event opened and de-activate the permissions when all are unmapped.
We also need to update the permissions in the context switch code so that tasks keep the right permissions.

---
v2:
- Move mapped/unmapped into arm64 code. Fixes arm32.
- Rebase on cap_user_time_short changes

Changes from Raphael's v4:
- Drop homogeneous check
- Disable access for chained counters
- Set pmc_width in user page

---
 arch/arm64/include/asm/mmu.h         |  5 ++++
 arch/arm64/include/asm/mmu_context.h |  2 ++
 arch/arm64/include/asm/perf_event.h  | 14 ++++++++++
 arch/arm64/kernel/perf_event.c       | 41 ++++++++++++++++++++++++++++
 4 files changed, 62 insertions(+)
arch/arm64/include/asm/mmu.h +5 -0
index a7a5ecaa2e83..52cfdb676f06 100644
@@ -19,6 +19,11 @@
 
 typedef struct {
 	atomic64_t	id;
+	/*
+	 * non-zero if userspace have access to hardware
+	 * counters directly.
+	 */
+	atomic_t	pmu_direct_access;
 #ifdef CONFIG_COMPAT
 	void		*sigpage;
 #endif
arch/arm64/include/asm/mmu_context.h +2 -0
index f2d7537d6f83..d24589ecb07a 100644
@@ -21,6 +21,7 @@
 #include <asm/proc-fns.h>
 #include <asm-generic/mm_hooks.h>
 #include <asm/cputype.h>
+#include <asm/perf_event.h>
 #include <asm/sysreg.h>
 #include <asm/tlbflush.h>
 
@@ -224,6 +225,7 @@ static inline void __switch_mm(struct mm_struct *next)
 	}
 
 	check_and_switch_context(next);
+	perf_switch_user_access(next);
 }
 
 static inline void
arch/arm64/include/asm/perf_event.h +14 -0
index 2c2d7dbe8a02..a025d9595d51 100644
@@ -8,6 +8,7 @@
 
 #include <asm/stack_pointer.h>
 #include <asm/ptrace.h>
+#include <linux/mm_types.h>
 
 #define	ARMV8_PMU_MAX_COUNTERS	32
 #define	ARMV8_PMU_COUNTER_MASK	(ARMV8_PMU_MAX_COUNTERS - 1)
@@ -251,4 +252,17 @@ extern unsigned long perf_misc_flags(struct pt_regs *regs);
 	(regs)->pstate = PSR_MODE_EL1h;	\
 }
 
+static inline void perf_switch_user_access(struct mm_struct *mm)
+{
+	if (!IS_ENABLED(CONFIG_PERF_EVENTS))
+		return;
+
+	if (atomic_read(&mm->context.pmu_direct_access)) {
+		write_sysreg(ARMV8_PMU_USERENR_ER|ARMV8_PMU_USERENR_CR,
+			     pmuserenr_el0);
+	} else {
+		write_sysreg(0, pmuserenr_el0);
+	}
+}
+
 #endif
arch/arm64/kernel/perf_event.c +41 -0

59 lines not on this page · raw message

Signature · 1 line
2.25.1
#8 lore raw

PATCH v3 4/10 tools/include: Add an initial math64.h

In reply to Rob Herring

Add an initial math64.h similar to linux/math64.h with functions mul_u64_u64_div64() and mul_u64_u32_shr(). This isn't a direct copy of include/linux/math64.h as that doesn't define mul_u64_u64_div64().

Implementation was written by Peter Zilkstra based on linux/math64.h and div64.h[1]. The original implementation was not optimal on arm64 as __int128 division is not optimal with a call out to __udivti3, so I dropped the __int128 variant of mul_u64_u64_div64().

[1] https://lore.kernel.org/lkml/[email protected]/

Cc Peter Zijlstra <[email protected]>
---
 tools/include/linux/math64.h | 75 ++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
 create mode 100644 tools/include/linux/math64.h
tools/include/linux/math64.h new file +75 -0
new file mode 100644
index 000000000000..4ad45d5943dc
@@ -0,0 +1,75 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_MATH64_H
+#define _LINUX_MATH64_H
+
+#include <linux/types.h>
+
+#ifdef __x86_64__
+static inline u64 mul_u64_u64_div64(u64 a, u64 b, u64 c)
+{
+	u64 q;
+
+	asm ("mulq %2; divq %3" : "=a" (q)
+				: "a" (a), "rm" (b), "rm" (c)
+				: "rdx");
+
+	return q;
+}
+#define mul_u64_u64_div64 mul_u64_u64_div64
+#endif
+
+#ifdef __SIZEOF_INT128__
+static inline u64 mul_u64_u32_shr(u64 a, u32 b, unsigned int shift)
+{
+	return (u64)(((unsigned __int128)a * b) >> shift);
+}
+
+#else
+
+#ifdef __i386__
+static inline u64 mul_u32_u32(u32 a, u32 b)
+{
+	u32 high, low;
+
+	asm ("mull %[b]" : "=a" (low), "=d" (high)
+			 : [a] "a" (a), [b] "rm" (b) );
+
+	return low | ((u64)high) << 32;
+}
+#else
+static inline u64 mul_u32_u32(u32 a, u32 b)
+{
+	return (u64)a * b;
+}
+#endif
+
+static inline u64 mul_u64_u32_shr(u64 a, u32 b, unsigned int shift)
+{
+	u32 ah, al;
+	u64 ret;
+
+	al = a;
+	ah = a >> 32;
+
+	ret = mul_u32_u32(al, b) >> shift;
+	if (ah)
+		ret += mul_u32_u32(ah, b) << (32 - shift);
+
+	return ret;
+}
+
+#endif	/* __SIZEOF_INT128__ */
+
+#ifndef mul_u64_u64_div64
+static inline u64 mul_u64_u64_div64(u64 a, u64 b, u64 c)
+{
+	u64 quot, rem;
+
+	quot = a / c;
+	rem = a % c;
+
+	return quot * b + (rem * b) / c;
+}
+#endif
+
+#endif /* _LINUX_MATH64_H */
Signature · 1 line
2.25.1
#9 lore raw

PATCH v3 5/10 libperf: Add libperf_evsel__mmap()

In reply to Rob Herring

In order to support usersapce access, an event must be mmapped. While there's already mmap support for evlist, the usecase is a bit different than the self monitoring with userspace access. So let's add a new perf_evsel__mmap() function to mmap an evsel. This allows implementing userspace access as a fastpath for perf_evsel__read().

The mmapped address is returned by perf_evsel__mmap() primarily for users/tests to check if userspace access is enabled.

---
v3:
- New patch split out from user access patch

---
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/evsel.c                   | 31 ++++++++++++++++++++++++
 tools/lib/perf/include/internal/evsel.h  |  2 ++
 tools/lib/perf/include/perf/evsel.h      |  2 ++
 tools/lib/perf/libperf.map               |  1 +
 5 files changed, 37 insertions(+)
tools/lib/perf/Documentation/libperf.txt +1 -0
index 0c74c30ed23a..0b4694ce42b9 100644
@@ -136,6 +136,7 @@ SYNOPSIS
                        struct perf_thread_map *threads);
   void perf_evsel__close(struct perf_evsel *evsel);
   void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu);
+  void *perf_evsel__mmap(struct perf_evsel *evsel, size_t length);
   int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
                        struct perf_counts_values *count);
   int perf_evsel__enable(struct perf_evsel *evsel);
tools/lib/perf/evsel.c +31 -0
index 4dc06289f4c7..99fa53dc0887 100644
@@ -11,10 +11,12 @@
 #include <stdlib.h>
 #include <internal/xyarray.h>
 #include <internal/cpumap.h>
+#include <internal/mmap.h>
 #include <internal/threadmap.h>
 #include <internal/lib.h>
 #include <linux/string.h>
 #include <sys/ioctl.h>
+#include <sys/mman.h>
 
 void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr)
 {
@@ -156,6 +158,35 @@ void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu)
 	perf_evsel__close_fd_cpu(evsel, cpu);
 }
 
+void *perf_evsel__mmap(struct perf_evsel *evsel, size_t length)
+{
+	int ret;
+	struct perf_mmap *map;
+	struct perf_mmap_param mp = {
+		.prot = PROT_READ | PROT_WRITE,
+	};
+
+	if (FD(evsel, 0, 0) < 0)
+		return NULL;
+
+	mp.mask = length - page_size - 1;
+
+	map = zalloc(sizeof(*map));
+	if (!map)
+		return NULL;
+
+	perf_mmap__init(map, NULL, false, NULL);
+
+	ret = perf_mmap__mmap(map, &mp, FD(evsel, 0, 0), 0);
+	if (ret) {
+		free(map);
+		return NULL;
+	}
+
+	evsel->mmap = map;
+	return map->base;
+}
+
 int perf_evsel__read_size(struct perf_evsel *evsel)
 {
 	u64 read_format = evsel->attr.read_format;
tools/lib/perf/include/internal/evsel.h +2 -0
index 1ffd083b235e..a7985dbb68ff 100644
@@ -9,6 +9,7 @@
 
 struct perf_cpu_map;
 struct perf_thread_map;
+struct perf_mmap;
 struct xyarray;
 
 /*
@@ -40,6 +41,7 @@ struct perf_evsel {
 	struct perf_cpu_map	*cpus;
 	struct perf_cpu_map	*own_cpus;
 	struct perf_thread_map	*threads;
+	struct perf_mmap	*mmap;
 	struct xyarray		*fd;
 	struct xyarray		*sample_id;
 	u64			*id;
tools/lib/perf/include/perf/evsel.h +2 -0
index c82ec39a4ad0..28f1354e52df 100644
@@ -3,6 +3,7 @@
 #define __LIBPERF_EVSEL_H
 
 #include <stdint.h>
+#include <stddef.h>
 #include <perf/core.h>
 
 struct perf_evsel;
@@ -27,6 +28,7 @@ LIBPERF_API int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *
 				 struct perf_thread_map *threads);
 LIBPERF_API void perf_evsel__close(struct perf_evsel *evsel);
 LIBPERF_API void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu);
+LIBPERF_API void *perf_evsel__mmap(struct perf_evsel *evsel, size_t length);
 LIBPERF_API int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
 				 struct perf_counts_values *count);
 LIBPERF_API int perf_evsel__enable(struct perf_evsel *evsel);
tools/lib/perf/libperf.map +1 -0
index 7be1af8a546c..733a0647be8b 100644
@@ -23,6 +23,7 @@ LIBPERF_0.0.1 {
 		perf_evsel__disable;
 		perf_evsel__open;
 		perf_evsel__close;
+		perf_evsel__mmap;
 		perf_evsel__read;
 		perf_evsel__cpus;
 		perf_evsel__threads;
Signature · 1 line
2.25.1
#10 lore raw

Re: [PATCH v3 05/10] libperf: Add libperf_evsel__mmap()

Quoted 51 lines · Rob Herring

In order to support usersapce access, an event must be mmapped. While there's already mmap support for evlist, the usecase is a bit different than the self monitoring with userspace access. So let's add a new perf_evsel__mmap() function to mmap an evsel. This allows implementing userspace access as a fastpath for perf_evsel__read().

The mmapped address is returned by perf_evsel__mmap() primarily for users/tests to check if userspace access is enabled.

---
v3:
- New patch split out from user access patch

---
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/evsel.c                   | 31 ++++++++++++++++++++++++
 tools/lib/perf/include/internal/evsel.h  |  2 ++
 tools/lib/perf/include/perf/evsel.h      |  2 ++
 tools/lib/perf/libperf.map               |  1 +
 5 files changed, 37 insertions(+)
tools/lib/perf/Documentation/libperf.txt +1 -0
index 0c74c30ed23a..0b4694ce42b9 100644
@@ -136,6 +136,7 @@ SYNOPSIS
                        struct perf_thread_map *threads);
   void perf_evsel__close(struct perf_evsel *evsel);
   void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu);
+  void *perf_evsel__mmap(struct perf_evsel *evsel, size_t length);
   int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
                        struct perf_counts_values *count);
   int perf_evsel__enable(struct perf_evsel *evsel);
tools/lib/perf/evsel.c +2 -0
index 4dc06289f4c7..99fa53dc0887 100644
@@ -11,10 +11,12 @@
 #include <stdlib.h>
 #include <internal/xyarray.h>
 #include <internal/cpumap.h>
+#include <internal/mmap.h>
 #include <internal/threadmap.h>
 #include <internal/lib.h>
 #include <linux/string.h>
 #include <sys/ioctl.h>
+#include <sys/mman.h>
 
 void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr)
 {

tools/lib/perf/evsel.c void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu) · line 162

Quoted 5 lines
tools/lib/perf/evsel.c +2 -0
@@ -156,6 +158,35 @@ void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu)
 	perf_evsel__close_fd_cpu(evsel, cpu);
 }
 
+void *perf_evsel__mmap(struct perf_evsel *evsel, size_t length)
+{

could you rather use 'int pages' insted of length as we do in perf_evlist__mmap, so we keep the same interface

len = (pages + 1) * page_size

thanks,
jirka

#11 lore raw

Re: [PATCH v3 05/10] libperf: Add libperf_evsel__mmap()

Quoted 56 lines · Jiri Olsa
Quoted 54 lines · Rob Herring

In order to support usersapce access, an event must be mmapped. While there's already mmap support for evlist, the usecase is a bit different than the self monitoring with userspace access. So let's add a new perf_evsel__mmap() function to mmap an evsel. This allows implementing userspace access as a fastpath for perf_evsel__read().

The mmapped address is returned by perf_evsel__mmap() primarily for users/tests to check if userspace access is enabled.

---
v3:
- New patch split out from user access patch

---
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/evsel.c                   | 31 ++++++++++++++++++++++++
 tools/lib/perf/include/internal/evsel.h  |  2 ++
 tools/lib/perf/include/perf/evsel.h      |  2 ++
 tools/lib/perf/libperf.map               |  1 +
 5 files changed, 37 insertions(+)
tools/lib/perf/Documentation/libperf.txt +1 -0
index 0c74c30ed23a..0b4694ce42b9 100644
@@ -136,6 +136,7 @@ SYNOPSIS
                        struct perf_thread_map *threads);
   void perf_evsel__close(struct perf_evsel *evsel);
   void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu);
+  void *perf_evsel__mmap(struct perf_evsel *evsel, size_t length);
   int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
                        struct perf_counts_values *count);
   int perf_evsel__enable(struct perf_evsel *evsel);
tools/lib/perf/evsel.c +2 -0
index 4dc06289f4c7..99fa53dc0887 100644
@@ -11,10 +11,12 @@
 #include <stdlib.h>
 #include <internal/xyarray.h>
 #include <internal/cpumap.h>
+#include <internal/mmap.h>
 #include <internal/threadmap.h>
 #include <internal/lib.h>
 #include <linux/string.h>
 #include <sys/ioctl.h>
+#include <sys/mman.h>
 
 void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr)
 {
@@ -156,6 +158,35 @@ void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu)
      perf_evsel__close_fd_cpu(evsel, cpu);
 }
 
Quoted 7 lines

tools/lib/perf/evsel.c void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu) · line 162

Quoted 2 lines
tools/lib/perf/evsel.c +2 -0
+void *perf_evsel__mmap(struct perf_evsel *evsel, size_t length)
+{

could you rather use 'int pages' insted of length as we do in perf_evlist__mmap, so we keep the same interface

len = (pages + 1) * page_size

Sure, but what's the reason it is pages minus 1? It seems a bit odd to pass in 0 here to get 1 page which would be the typical usage.

Rob

#12 lore raw

Re: [PATCH v3 05/10] libperf: Add libperf_evsel__mmap()

Quoted 63 lines · Rob Herring
Quoted 62 lines · Jiri Olsa
Quoted 51 lines · Rob Herring

In order to support usersapce access, an event must be mmapped. While there's already mmap support for evlist, the usecase is a bit different than the self monitoring with userspace access. So let's add a new perf_evsel__mmap() function to mmap an evsel. This allows implementing userspace access as a fastpath for perf_evsel__read().

The mmapped address is returned by perf_evsel__mmap() primarily for users/tests to check if userspace access is enabled.

---
v3:
- New patch split out from user access patch

---
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/evsel.c                   | 31 ++++++++++++++++++++++++
 tools/lib/perf/include/internal/evsel.h  |  2 ++
 tools/lib/perf/include/perf/evsel.h      |  2 ++
 tools/lib/perf/libperf.map               |  1 +
 5 files changed, 37 insertions(+)
tools/lib/perf/Documentation/libperf.txt +1 -0
index 0c74c30ed23a..0b4694ce42b9 100644
@@ -136,6 +136,7 @@ SYNOPSIS
                        struct perf_thread_map *threads);
   void perf_evsel__close(struct perf_evsel *evsel);
   void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu);
+  void *perf_evsel__mmap(struct perf_evsel *evsel, size_t length);
   int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
                        struct perf_counts_values *count);
   int perf_evsel__enable(struct perf_evsel *evsel);
tools/lib/perf/evsel.c +2 -0
index 4dc06289f4c7..99fa53dc0887 100644
@@ -11,10 +11,12 @@
 #include <stdlib.h>
 #include <internal/xyarray.h>
 #include <internal/cpumap.h>
+#include <internal/mmap.h>
 #include <internal/threadmap.h>
 #include <internal/lib.h>
 #include <linux/string.h>
 #include <sys/ioctl.h>
+#include <sys/mman.h>
 
 void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr)
 {

tools/lib/perf/evsel.c void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu) · line 162

Quoted 5 lines
tools/lib/perf/evsel.c +2 -0
@@ -156,6 +158,35 @@ void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu)
      perf_evsel__close_fd_cpu(evsel, cpu);
 }
 
+void *perf_evsel__mmap(struct perf_evsel *evsel, size_t length)
+{

could you rather use 'int pages' insted of length as we do in perf_evlist__mmap, so we keep the same interface

Quoted 4 lines
Quoted 1 line

len = (pages + 1) * page_size

Sure, but what's the reason it is pages minus 1? It seems a bit odd to pass in 0 here to get 1 page which would be the typical usage.

the user mmap page is extra, so when you map 2 pages you actualy map 3, but we want the size of what's available for storing events in the API

jirka

#13 lore raw

PATCH v3 6/10 libperf: tests: Add support for verbose printing

In reply to Rob Herring

Add __T_VERBOSE() so tests can add verbose output. The verbose output is enabled with the '-v' command line option.

---
v3:
- New patch

---
 tools/lib/perf/include/internal/tests.h | 32 +++++++++++++++++++++++++
 tools/lib/perf/tests/Makefile           |  4 +++-
 2 files changed, 35 insertions(+), 1 deletion(-)
tools/lib/perf/include/internal/tests.h +32 -0
index 2093e8868a67..27b6e64299e2 100644
@@ -3,11 +3,32 @@
 #define __LIBPERF_INTERNAL_TESTS_H
 
 #include <stdio.h>
+#include <unistd.h>
 
 int tests_failed;
+int tests_verbose;
+
+static inline int get_verbose(char **argv, int argc)
+{
+	char c;
+	int verbose = 0;
+
+	while ((c = getopt(argc, argv, "v")) != -1) {
+		switch (c)
+		{
+		case 'v':
+			verbose = 1;
+			break;
+		default:
+			break;
+		}
+	}
+	return verbose;
+}
 
 #define __T_START					\
 do {							\
+	tests_verbose = get_verbose(argv, argc);	\
 	fprintf(stdout, "- running %s...", __FILE__);	\
 	fflush(NULL);					\
 	tests_failed = 0;				\
@@ -30,4 +51,15 @@ do {
 	}                                                                        \
 } while (0)
 
+#define __T_VERBOSE(...)						\
+do {									\
+	if (tests_verbose) {						\
+		if (tests_verbose == 1) {				\
+			fputc('\n', stderr);				\
+			tests_verbose++;				\
+		}							\
+		fprintf(stderr, ##__VA_ARGS__);				\
+	}								\
+} while (0)
+
 #endif /* __LIBPERF_INTERNAL_TESTS_H */
tools/lib/perf/tests/Makefile +3 -1
index 96841775feaf..9438b385d489 100644
@@ -5,6 +5,8 @@ TESTS = test-cpumap test-threadmap test-evlist test-evsel
 TESTS_SO := $(addsuffix -so,$(TESTS))
 TESTS_A  := $(addsuffix -a,$(TESTS))
 
+TEST_ARGS := $(if $(V),-v)
+
 # Set compile option CFLAGS
 ifdef EXTRA_CFLAGS
   CFLAGS := $(EXTRA_CFLAGS)
@@ -30,7 +32,7 @@ run:
 	@echo "running static:"
 	@for i in $(TESTS_A); do ./$$i; done
 	@echo "running dynamic:"
-	@for i in $(TESTS_SO); do LD_LIBRARY_PATH=../ ./$$i; done
+	@for i in $(TESTS_SO); do LD_LIBRARY_PATH=../ ./$$i $(TEST_ARGS); done
 
 clean:
 	$(call QUIET_CLEAN, tests)$(RM) $(TESTS_A) $(TESTS_SO)
Signature · 1 line
2.25.1
#14 lore raw

PATCH v3 7/10 libperf: Add support for user space counter access

In reply to Rob Herring

x86 and arm64 can both support direct access of event counters in userspace. The access sequence is less than trivial and currently exists in perf test code (tools/perf/arch/x86/tests/rdpmc.c) with copies in projects such as PAPI and libpfm4.

In order to support usersapce access, an event must be mmapped first with perf_evsel__mmap(). Then subsequent calls to perf_evsel__read() will use the fast path (assuming the arch supports it).

---
v3:
- Split out perf_evsel__mmap() to separate patch

---
 tools/lib/perf/evsel.c                 |  3 +
 tools/lib/perf/include/internal/mmap.h |  3 +
 tools/lib/perf/mmap.c                  | 90 ++++++++++++++++++++++++++
 tools/lib/perf/tests/test-evsel.c      | 63 ++++++++++++++++++
 4 files changed, 159 insertions(+)
tools/lib/perf/evsel.c +3 -0
index 99fa53dc0887..4164e6524453 100644
@@ -222,6 +222,9 @@ int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
 	if (FD(evsel, cpu, thread) < 0)
 		return -EINVAL;
 
+	if (evsel->mmap && !perf_mmap__read_self(evsel->mmap, count))
+		return 0;
+
 	if (readn(FD(evsel, cpu, thread), count->values, size) <= 0)
 		return -errno;
 
tools/lib/perf/include/internal/mmap.h +3 -0
index be7556e0a2b2..5e3422f40ed5 100644
@@ -11,6 +11,7 @@
 #define PERF_SAMPLE_MAX_SIZE (1 << 16)
 
 struct perf_mmap;
+struct perf_counts_values;
 
 typedef void (*libperf_unmap_cb_t)(struct perf_mmap *map);
 
@@ -52,4 +53,6 @@ void perf_mmap__put(struct perf_mmap *map);
 
 u64 perf_mmap__read_head(struct perf_mmap *map);
 
+int perf_mmap__read_self(struct perf_mmap *map, struct perf_counts_values *count);
+
 #endif /* __LIBPERF_INTERNAL_MMAP_H */
tools/lib/perf/mmap.c +90 -0

102 lines not on this page · raw message

tools/lib/perf/tests/test-evsel.c +63 -0

75 lines not on this page · raw message

Signature · 1 line
2.25.1
#15 lore raw

PATCH v3 8/10 libperf: Add arm64 support to perf_mmap__read_self()

In reply to Rob Herring

Add the arm64 variants for read_perf_counter() and read_timestamp(). Unfortunately the counter number is encoded into the instruction, so the code is a bit verbose to enumerate all possible counters.

---
 tools/lib/perf/mmap.c | 98 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
tools/lib/perf/mmap.c +98 -0
index cb07969cfdbf..6cf939aae6b6 100644
@@ -13,6 +13,7 @@
 #include <internal/lib.h>
 #include <linux/kernel.h>
 #include <linux/math64.h>
+#include <linux/stringify.h>
 #include "internal.h"
 
 void perf_mmap__init(struct perf_mmap *map, struct perf_mmap *prev,
@@ -294,6 +295,103 @@ static u64 read_timestamp(void)
 
 	return low | ((u64)high) << 32;
 }
+#elif defined(__aarch64__)
+#define read_sysreg(r) ({						\
+	u64 __val;							\
+	asm volatile("mrs %0, " __stringify(r) : "=r" (__val));		\
+	__val;								\
+})
+
+static u64 read_pmccntr(void)
+{
+	return read_sysreg(pmccntr_el0);
+}
+
+#define PMEVCNTR_READ(idx)					\
+	static u64 read_pmevcntr_##idx(void) {			\
+		return read_sysreg(pmevcntr##idx##_el0);	\
+	}
+
+PMEVCNTR_READ(0);
+PMEVCNTR_READ(1);
+PMEVCNTR_READ(2);
+PMEVCNTR_READ(3);
+PMEVCNTR_READ(4);
+PMEVCNTR_READ(5);
+PMEVCNTR_READ(6);
+PMEVCNTR_READ(7);
+PMEVCNTR_READ(8);
+PMEVCNTR_READ(9);
+PMEVCNTR_READ(10);
+PMEVCNTR_READ(11);
+PMEVCNTR_READ(12);
+PMEVCNTR_READ(13);
+PMEVCNTR_READ(14);
+PMEVCNTR_READ(15);
+PMEVCNTR_READ(16);
+PMEVCNTR_READ(17);
+PMEVCNTR_READ(18);
+PMEVCNTR_READ(19);
+PMEVCNTR_READ(20);
+PMEVCNTR_READ(21);
+PMEVCNTR_READ(22);
+PMEVCNTR_READ(23);
+PMEVCNTR_READ(24);
+PMEVCNTR_READ(25);
+PMEVCNTR_READ(26);
+PMEVCNTR_READ(27);
+PMEVCNTR_READ(28);
+PMEVCNTR_READ(29);
+PMEVCNTR_READ(30);
+
+/*
+ * Read a value direct from PMEVCNTR<idx>
+ */
+static u64 read_perf_counter(unsigned int counter)
+{
+	static u64 (* const read_f[])(void) = {
+		read_pmevcntr_0,
+		read_pmevcntr_1,
+		read_pmevcntr_2,
+		read_pmevcntr_3,
+		read_pmevcntr_4,
+		read_pmevcntr_5,
+		read_pmevcntr_6,
+		read_pmevcntr_7,
+		read_pmevcntr_8,
+		read_pmevcntr_9,
+		read_pmevcntr_10,
+		read_pmevcntr_11,
+		read_pmevcntr_13,
+		read_pmevcntr_12,
+		read_pmevcntr_14,
+		read_pmevcntr_15,
+		read_pmevcntr_16,
+		read_pmevcntr_17,
+		read_pmevcntr_18,
+		read_pmevcntr_19,
+		read_pmevcntr_20,
+		read_pmevcntr_21,
+		read_pmevcntr_22,
+		read_pmevcntr_23,
+		read_pmevcntr_24,
+		read_pmevcntr_25,
+		read_pmevcntr_26,
+		read_pmevcntr_27,
+		read_pmevcntr_28,
+		read_pmevcntr_29,
+		read_pmevcntr_30,
+		read_pmccntr
+	};
+
+	if (counter < ARRAY_SIZE(read_f))
+		return (read_f[counter])();
+
+	return 0;
+}
+
+static u64 read_timestamp(void) { return read_sysreg(cntvct_el0); }
+
 #else
 static u64 read_perf_counter(unsigned int counter) { return 0; }
 static u64 read_timestamp(void) { return 0; }
Signature · 1 line
2.25.1
#16 lore raw

PATCH v3 9/10 perf: arm64: Add test for userspace counter access on heterogeneous systems

In reply to Rob Herring

Userspace counter access only works on heterogeneous systems with some restrictions. The userspace process must be pinned to a homogeneous subset of CPUs and must open the corresponding PMU for those CPUs. This commit adds a test implementing these requirements.

---
v2:
- Drop all but heterogeneous test as others covered by libperf tests
- Rework to use libperf

---
 tools/perf/arch/arm64/include/arch-tests.h |   7 +
 tools/perf/arch/arm64/tests/Build          |   1 +
 tools/perf/arch/arm64/tests/arch-tests.c   |   4 +
 tools/perf/arch/arm64/tests/user-events.c  | 170 +++++++++++++++++++++
 4 files changed, 182 insertions(+)
 create mode 100644 tools/perf/arch/arm64/tests/user-events.c
tools/perf/arch/arm64/include/arch-tests.h +7 -0
index 90ec4c8cb880..380ad34a3f09 100644
@@ -2,11 +2,18 @@
 #ifndef ARCH_TESTS_H
 #define ARCH_TESTS_H
 
+#include <linux/compiler.h>
+
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
 struct thread;
 struct perf_sample;
+int test__arch_unwind_sample(struct perf_sample *sample,
+			     struct thread *thread);
 #endif
 
 extern struct test arch_tests[];
+int test__rd_pinned(struct test __maybe_unused *test,
+		       int __maybe_unused subtest);
+
 
 #endif
tools/perf/arch/arm64/tests/Build +1 -0
index a61c06bdb757..3f9a20c17fc6 100644
@@ -1,4 +1,5 @@
 perf-y += regs_load.o
 perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
 
+perf-y += user-events.o
 perf-y += arch-tests.o
tools/perf/arch/arm64/tests/arch-tests.c +4 -0
index 5b1543c98022..80ce7bd3c16d 100644
@@ -10,6 +10,10 @@ struct test arch_tests[] = {
 		.func = test__dwarf_unwind,
 	},
 #endif
+	{
+		.desc = "Pinned CPU user counter access",
+		.func = test__rd_pinned,
+	},
 	{
 		.func = NULL,
 	},
tools/perf/arch/arm64/tests/user-events.c new file +170 -0

170 lines not on this page · raw message

Signature · 1 line
2.25.1
#17 lore raw

PATCH v3 10/10 Documentation: arm64: Document PMU counters access from userspace

In reply to Rob Herring

From: Raphael Gault <[email protected]>

Add a documentation file to describe the access to the pmu hardware counters from userspace

---
v2:
- Update links to test examples

Changes from Raphael's v4:
- Convert to rSt
- Update chained event status
- Add section for heterogeneous systems

---
 Documentation/arm64/index.rst                 |  1 +
 .../arm64/perf_counter_user_access.rst        | 56 +++++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 Documentation/arm64/perf_counter_user_access.rst
Documentation/arm64/index.rst +1 -0
index d9665d83c53a..c712a08e7627 100644
@@ -15,6 +15,7 @@ ARM64 Architecture
     legacy_instructions
     memory
     perf
+    perf_counter_user_access
     pointer-authentication
     silicon-errata
     sve
Documentation/arm64/perf_counter_user_access.rst new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.25.1
#18 lore raw
In reply to Rob Herring
Quoted 7 lines · Rob Herring

This is resurrecting Raphael's series[1] to enable userspace counter access on arm64. My previous versions are here[2][3].

Changes in v3:
- Dropped removing x86 rdpmc test until libperf tests can run via 'perf test'
- Added verbose prints for tests
- Split adding perf_evsel__mmap() to separate patch

hi,
which commit/branch did you base on? I can't apply arm bits:

Applying: arm64: pmu: Add hook to handle pmu-related undefined instructions
Applying: arm64: perf: Enable pmu counter direct access for perf event on armv8
error: patch failed: arch/arm64/kernel/perf_event.c:836
error: arch/arm64/kernel/perf_event.c: patch does not apply
Patch failed at 0002 arm64: perf: Enable pmu counter direct access for perf event on armv8

thanks,
jirka

#19 lore raw
Quoted 10 lines · Jiri Olsa
Quoted 7 lines · Rob Herring

This is resurrecting Raphael's series[1] to enable userspace counter access on arm64. My previous versions are here[2][3].

Changes in v3:
- Dropped removing x86 rdpmc test until libperf tests can run via 'perf test'
- Added verbose prints for tests
- Split adding perf_evsel__mmap() to separate patch

Quoted 8 lines

hi,
which commit/branch did you base on? I can't apply arm bits:

Applying: arm64: pmu: Add hook to handle pmu-related undefined instructions
Applying: arm64: perf: Enable pmu counter direct access for perf event on armv8
error: patch failed: arch/arm64/kernel/perf_event.c:836
error: arch/arm64/kernel/perf_event.c: patch does not apply
Patch failed at 0002 arm64: perf: Enable pmu counter direct access for perf event on armv8

It's on v5.9-rc2.

Rob

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

A message that is not in the archive (sent off-list or not yet received): CANW9uytmafiNb_8oua9QY7L9O5BQTBFQBOMS3ZgjQ7aWj8CD2Q@mail.gmail.com

#21 lore raw
Quoted 2 lines · Itaru Kitayama

Could you point us to your repo too?

Sure, pushed a branch here:

git://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git user-perf-event-v3

Rob

#22 lore raw

Rob,
V3 fails to build on ThunderX2:

$ make
  BUILD:   Doing 'make -j224' parallel build

Auto-detecting system features:

...                         dwarf: [ on  ]
...            dwarf_getlocations: [ on  ]
...                         glibc: [ on  ]
...                          gtk2: [ OFF ]
...                        libbfd: [ on  ]
...                        libcap: [ on  ]
...                        libelf: [ on  ]
...                       libnuma: [ on  ]
...        numa_num_possible_cpus: [ on  ]
...                       libperl: [ on  ]
...                     libpython: [ on  ]
...                     libcrypto: [ on  ]
...                     libunwind: [ OFF ]
...            libdw-dwarf-unwind: [ on  ]
...                          zlib: [ on  ]
...                          lzma: [ on  ]
...                     get_cpuid: [ OFF ]
...                           bpf: [ on  ]
...                        libaio: [ on  ]
...                       libzstd: [ on  ]
...        disassembler-four-args: [ on  ]

Makefile.config:604: No libunwind found. Please install
libunwind-dev[el] >= 1.1 and/or set LIBUNWIND_DIR
Makefile.config:730: GTK2 not found, disables GTK2 support. Please
install gtk2-devel or libgtk2.0-dev
Makefile.config:965: No libbabeltrace found, disables 'perf data' CTF format support, please install
libbabeltrace-dev[el]/libbabeltrace-ctf-dev
Makefile.config:1009: No openjdk development package found, please
install JDK package, e.g. openjdk-8-jdk, java-1.8.0-openjdk-devel
CC arch/arm64/tests/user-events.o
CC arch/arm64/tests/arch-tests.o
In file included from arch/arm64/tests/arch-tests.c:4:
/home/users/itaru.kitayama/projects/linux/tools/perf/arch/arm64/include/arch-tests.h:10:5:
error: redundant redeclaration of ‘test__arch_unwind_sample’ [-Werror=redundant-decls]
int test__arch_unwind_sample(struct perf_sample *sample,

     ^~~~~~~~~~~~~~~~~~~~~~~~

In file included from arch/arm64/tests/arch-tests.c:3:
/home/users/itaru.kitayama/projects/linux/tools/perf/tests/tests.h:134:5:
note: previous declaration of ‘test__arch_unwind_sample’ was here
int test__arch_unwind_sample(struct perf_sample *sample,

     ^~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
make[6]: *** [/home/users/itaru.kitayama/projects/linux/tools/build/Makefile.build:97:
arch/arm64/tests/arch-tests.o] Error 1
make[6]: *** Waiting for unfinished jobs....
In file included from arch/arm64/tests/user-events.c:13:
/home/users/itaru.kitayama/projects/linux/tools/perf/arch/arm64/include/arch-tests.h:10:5:
error: redundant redeclaration of ‘test__arch_unwind_sample’ [-Werror=redundant-decls]
int test__arch_unwind_sample(struct perf_sample *sample,

     ^~~~~~~~~~~~~~~~~~~~~~~~

In file included from arch/arm64/tests/user-events.c:12:
/home/users/itaru.kitayama/projects/linux/tools/perf/tests/tests.h:134:5:
note: previous declaration of ‘test__arch_unwind_sample’ was here
int test__arch_unwind_sample(struct perf_sample *sample,

     ^~~~~~~~~~~~~~~~~~~~~~~~
arch/arm64/tests/user-events.c: In function ‘perf_init’:
arch/arm64/tests/user-events.c:106:7: error: too few arguments to
function ‘perf_evsel__mmap’
  if (!perf_evsel__mmap(evsel)) {
       ^~~~~~~~~~~~~~~~
In file included from arch/arm64/tests/user-events.c:8:
/home/users/itaru.kitayama/projects/linux/tools/lib/perf/include/perf/evsel.h:31:19:
note: declared here
 LIBPERF_API void *perf_evsel__mmap(struct perf_evsel *evsel, size_t length);
                   ^~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
make[6]: *** [/home/users/itaru.kitayama/projects/linux/tools/build/Makefile.build:97:
arch/arm64/tests/user-events.o] Error 1
make[5]: *** [/home/users/itaru.kitayama/projects/linux/tools/build/Makefile.build:139:
tests] Error 2
make[4]: *** [/home/users/itaru.kitayama/projects/linux/tools/build/Makefile.build:139:
arm64] Error 2
make[3]: *** [/home/users/itaru.kitayama/projects/linux/tools/build/Makefile.build:139:
arch] Error 2
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [Makefile.perf:634: perf-in.o] Error 2
make[1]: *** [Makefile.perf:233: sub-make] Error 2
make: *** [Makefile:70: all] Error 2

Quoted 10 lines · Rob Herring
Quoted 2 lines · Itaru Kitayama

Could you point us to your repo too?

Sure, pushed a branch here:

git://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git user-perf-event-v3

Rob

#23 lore raw
Quoted 30 lines · Itaru Kitayama

Rob,
V3 fails to build on ThunderX2:

$ make
  BUILD:   Doing 'make -j224' parallel build

Auto-detecting system features:

...                         dwarf: [ on  ]
...            dwarf_getlocations: [ on  ]
...                         glibc: [ on  ]
...                          gtk2: [ OFF ]
...                        libbfd: [ on  ]
...                        libcap: [ on  ]
...                        libelf: [ on  ]
...                       libnuma: [ on  ]
...        numa_num_possible_cpus: [ on  ]
...                       libperl: [ on  ]
...                     libpython: [ on  ]
...                     libcrypto: [ on  ]
...                     libunwind: [ OFF ]
...            libdw-dwarf-unwind: [ on  ]
...                          zlib: [ on  ]
...                          lzma: [ on  ]
...                     get_cpuid: [ OFF ]
...                           bpf: [ on  ]
...                        libaio: [ on  ]
...                       libzstd: [ on  ]
...        disassembler-four-args: [ on  ]
Quoted 1 line

Makefile.config:604: No libunwind found. Please install

I believe this is an existing problem with your setup?

Quoted 30 lines

libunwind-dev[el] >= 1.1 and/or set LIBUNWIND_DIR
Makefile.config:730: GTK2 not found, disables GTK2 support. Please
install gtk2-devel or libgtk2.0-dev
Makefile.config:965: No libbabeltrace found, disables 'perf data' CTF format support, please install
libbabeltrace-dev[el]/libbabeltrace-ctf-dev
Makefile.config:1009: No openjdk development package found, please
install JDK package, e.g. openjdk-8-jdk, java-1.8.0-openjdk-devel
CC arch/arm64/tests/user-events.o
CC arch/arm64/tests/arch-tests.o
In file included from arch/arm64/tests/arch-tests.c:4:
/home/users/itaru.kitayama/projects/linux/tools/perf/arch/arm64/include/arch-tests.h:10:5:
error: redundant redeclaration of ‘test__arch_unwind_sample’ [-Werror=redundant-decls]
int test__arch_unwind_sample(struct perf_sample *sample,

     ^~~~~~~~~~~~~~~~~~~~~~~~

In file included from arch/arm64/tests/arch-tests.c:3:
/home/users/itaru.kitayama/projects/linux/tools/perf/tests/tests.h:134:5:
note: previous declaration of ‘test__arch_unwind_sample’ was here
int test__arch_unwind_sample(struct perf_sample *sample,

     ^~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
make[6]: *** [/home/users/itaru.kitayama/projects/linux/tools/build/Makefile.build:97:
arch/arm64/tests/arch-tests.o] Error 1
make[6]: *** Waiting for unfinished jobs....
In file included from arch/arm64/tests/user-events.c:13:
/home/users/itaru.kitayama/projects/linux/tools/perf/arch/arm64/include/arch-tests.h:10:5:
error: redundant redeclaration of ‘test__arch_unwind_sample’ [-Werror=redundant-decls]
int test__arch_unwind_sample(struct perf_sample *sample,

Quoted 6 lines
     ^~~~~~~~~~~~~~~~~~~~~~~~

In file included from arch/arm64/tests/user-events.c:12:
/home/users/itaru.kitayama/projects/linux/tools/perf/tests/tests.h:134:5:
note: previous declaration of ‘test__arch_unwind_sample’ was here
int test__arch_unwind_sample(struct perf_sample *sample,

     ^~~~~~~~~~~~~~~~~~~~~~~~

These are due to the above issue.

Quoted 4 lines

arch/arm64/tests/user-events.c: In function ‘perf_init’:
arch/arm64/tests/user-events.c:106:7: error: too few arguments to function ‘perf_evsel__mmap’
if (!perf_evsel__mmap(evsel)) {

I forgot to update this test. Should be 'perf_evsel__mmap(evsel, SZ_4K)' here, but that's going to change to 'perf_evsel__mmap(evsel, 0)' with it changing from bytes to pages (minus 1).

Rob