Skip to contentSkip to conversation

perf: arm64: Tools support for Dwarf unwinding through SVE functions

PATCH Applied James Clark 4 participants 10 messages May 25, 2022 – Sep 23, 2022

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

v3 cover perf: arm64: Tools support for Dwarf unwinding through SVE functions

Changes since v2:
* Add commit to fix header include path to use Perf's copy
* Apply Leo's review tags

When SVE registers are pushed onto the stack the VG register is required to unwind because the stack offsets would vary by the SVE register width at the time when the sample was taken.

The patches ("[PATCH v2 0/2] perf: arm64: Kernel support for Dwarf unwinding through SVE functions") add support for sampling the VG register to the kernel and the docs. This is the patchset to add support to userspace perf.

A small change is also required to libunwind or libdw depending on which unwinder is used, and these will be published later. Without these changes Perf continues to work with both libraries, although the VG register is still not used for unwinding.

Thanks
James

James Clark (5):
perf tools: arm64: Use perf's copy of kernel headers perf tools: arm64: Copy perf_regs.h from the kernel perf tools: Use dynamic register set for Dwarf unwind perf tools: arm64: Decouple Libunwind register names from Perf perf tools: arm64: Add support for VG register

 tools/arch/arm64/include/uapi/asm/perf_regs.h |  7 +-
 tools/perf/arch/arm64/util/perf_regs.c        | 34 +++++++++
 tools/perf/arch/arm64/util/unwind-libunwind.c | 73 +------------------
 tools/perf/util/evsel.c                       |  2 +-
 tools/perf/util/libunwind/arm64.c             |  2 +-
 tools/perf/util/perf_regs.c                   |  2 +
 6 files changed, 46 insertions(+), 74 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH v3 1/5 perf tools: arm64: Use perf's copy of kernel headers

Fix this include path to use perf's copy of the kernel header rather than the one from the root of the repo.

This fixes build errors when only applying the perf tools part of a patchset rather than both sides.

---
 tools/perf/util/libunwind/arm64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/libunwind/arm64.c +1 -1
index 15f60fd09424..014d82159656 100644
@@ -24,7 +24,7 @@
 #include "unwind.h"
 #include "libunwind-aarch64.h"
 #define perf_event_arm_regs perf_event_arm64_regs
-#include <../../../../arch/arm64/include/uapi/asm/perf_regs.h>
+#include <../../../arch/arm64/include/uapi/asm/perf_regs.h>
 #undef perf_event_arm_regs
 #include "../../arch/arm64/util/unwind-libunwind.c"
 
Signature · 1 line
2.28.0
Tested-by on v3 1/5
#2 lore raw

Re: [PATCH v3 1/5] perf tools: arm64: Use perf's copy of kernel headers

Quoted 8 lines · James Clark

Fix this include path to use perf's copy of the kernel header rather than the one from the root of the repo.

This fixes build errors when only applying the perf tools part of a patchset rather than both sides.

Without this change "make -C tools/perf" was failing if kernel-side changes weren't applied

Thanks,
German

Quoted 17 lines
---
 tools/perf/util/libunwind/arm64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/libunwind/arm64.c +1 -1
index 15f60fd09424..014d82159656 100644
@@ -24,7 +24,7 @@
 #include "unwind.h"
 #include "libunwind-aarch64.h"
 #define perf_event_arm_regs perf_event_arm64_regs
-#include <../../../../arch/arm64/include/uapi/asm/perf_regs.h>
+#include <../../../arch/arm64/include/uapi/asm/perf_regs.h>
 #undef perf_event_arm_regs
 #include "../../arch/arm64/util/unwind-libunwind.c"
 
reviewer Acked-by
#3 lore raw

Re: [PATCH v3 1/5] perf tools: arm64: Use perf's copy of kernel headers

Quoted 12 lines · German Gomez
Quoted 8 lines · James Clark

Fix this include path to use perf's copy of the kernel header rather than the one from the root of the repo.

This fixes build errors when only applying the perf tools part of a patchset rather than both sides.

Quoted 6 lines

Without this change "make -C tools/perf" was failing if kernel-side changes weren't applied

Thanks,
German

Thanks,
Ian

Quoted 17 lines
Quoted 17 lines
---
 tools/perf/util/libunwind/arm64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/libunwind/arm64.c +1 -1
index 15f60fd09424..014d82159656 100644
@@ -24,7 +24,7 @@
 #include "unwind.h"
 #include "libunwind-aarch64.h"
 #define perf_event_arm_regs perf_event_arm64_regs
-#include <../../../../arch/arm64/include/uapi/asm/perf_regs.h>
+#include <../../../arch/arm64/include/uapi/asm/perf_regs.h>
 #undef perf_event_arm_regs
 #include "../../arch/arm64/util/unwind-libunwind.c"
 
#4 lore raw

PATCH v3 2/5 perf tools: arm64: Copy perf_regs.h from the kernel

In reply to James Clark

Get the updated header for the newly added VG register.

---
 tools/arch/arm64/include/uapi/asm/perf_regs.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/arch/arm64/include/uapi/asm/perf_regs.h +6 -1
index d54daafa89e3..fd157f46727e 100644
@@ -36,6 +36,11 @@ enum perf_event_arm_regs {
 	PERF_REG_ARM64_LR,
 	PERF_REG_ARM64_SP,
 	PERF_REG_ARM64_PC,
-	PERF_REG_ARM64_MAX,
+
+	/* Extended/pseudo registers */
+	PERF_REG_ARM64_VG = 46, // SVE Vector Granule
+
+	PERF_REG_ARM64_MAX = PERF_REG_ARM64_PC + 1,
+	PERF_REG_ARM64_EXTENDED_MAX = PERF_REG_ARM64_VG + 1
 };
 #endif /* _ASM_ARM64_PERF_REGS_H */
Signature · 1 line
2.28.0
#5 lore raw

PATCH v3 3/5 perf tools: Use dynamic register set for Dwarf unwind

In reply to James Clark

Architectures can detect availability of extra registers at runtime so use this more complete set for unwinding. This will include the VG register on arm64 in a later commit.

If the function isn't implemented then PERF_REGS_MASK is returned and there is no change.

---
 tools/perf/util/evsel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/evsel.c +1 -1
index ef169ad15236..020836a4c4bc 100644
@@ -896,7 +896,7 @@ static void __evsel__config_callchain(struct evsel *evsel, struct record_opts *o
 					   "specifying a subset with --user-regs may render DWARF unwinding unreliable, "
 					   "so the minimal registers set (IP, SP) is explicitly forced.\n");
 			} else {
-				attr->sample_regs_user |= PERF_REGS_MASK;
+				attr->sample_regs_user |= arch__user_reg_mask();
 			}
 			attr->sample_stack_user = param->dump_size;
 			attr->exclude_callchain_user = 1;
Signature · 1 line
2.28.0
#6 lore raw

PATCH v3 4/5 perf tools: arm64: Decouple Libunwind register names from Perf

In reply to James Clark

Dwarf register numbers and real register numbers on aarch64 are equivalent. Remove the references to the register names from Libunwind so that new registers are supported without having to add build time feature checks for each new register.

The unwinder won't ask for a register that it doesn't know about and Perf will already report an error for an unknown or unrecorded register in the perf_reg_value() function so extra validation isn't needed.

After this change the new VG register can be read by libunwind.

---
 tools/perf/arch/arm64/util/unwind-libunwind.c | 73 +------------------
 1 file changed, 2 insertions(+), 71 deletions(-)
tools/perf/arch/arm64/util/unwind-libunwind.c +2 -71
index 5aecf88e3de6..871af5992298 100644
@@ -10,77 +10,8 @@
 
 int LIBUNWIND__ARCH_REG_ID(int regnum)
 {
-	switch (regnum) {
-	case UNW_AARCH64_X0:
-		return PERF_REG_ARM64_X0;
-	case UNW_AARCH64_X1:
-		return PERF_REG_ARM64_X1;
-	case UNW_AARCH64_X2:
-		return PERF_REG_ARM64_X2;
-	case UNW_AARCH64_X3:
-		return PERF_REG_ARM64_X3;
-	case UNW_AARCH64_X4:
-		return PERF_REG_ARM64_X4;
-	case UNW_AARCH64_X5:
-		return PERF_REG_ARM64_X5;
-	case UNW_AARCH64_X6:
-		return PERF_REG_ARM64_X6;
-	case UNW_AARCH64_X7:
-		return PERF_REG_ARM64_X7;
-	case UNW_AARCH64_X8:
-		return PERF_REG_ARM64_X8;
-	case UNW_AARCH64_X9:
-		return PERF_REG_ARM64_X9;
-	case UNW_AARCH64_X10:
-		return PERF_REG_ARM64_X10;
-	case UNW_AARCH64_X11:
-		return PERF_REG_ARM64_X11;
-	case UNW_AARCH64_X12:
-		return PERF_REG_ARM64_X12;
-	case UNW_AARCH64_X13:
-		return PERF_REG_ARM64_X13;
-	case UNW_AARCH64_X14:
-		return PERF_REG_ARM64_X14;
-	case UNW_AARCH64_X15:
-		return PERF_REG_ARM64_X15;
-	case UNW_AARCH64_X16:
-		return PERF_REG_ARM64_X16;
-	case UNW_AARCH64_X17:
-		return PERF_REG_ARM64_X17;
-	case UNW_AARCH64_X18:
-		return PERF_REG_ARM64_X18;
-	case UNW_AARCH64_X19:
-		return PERF_REG_ARM64_X19;
-	case UNW_AARCH64_X20:
-		return PERF_REG_ARM64_X20;
-	case UNW_AARCH64_X21:
-		return PERF_REG_ARM64_X21;
-	case UNW_AARCH64_X22:
-		return PERF_REG_ARM64_X22;
-	case UNW_AARCH64_X23:
-		return PERF_REG_ARM64_X23;
-	case UNW_AARCH64_X24:
-		return PERF_REG_ARM64_X24;
-	case UNW_AARCH64_X25:
-		return PERF_REG_ARM64_X25;
-	case UNW_AARCH64_X26:
-		return PERF_REG_ARM64_X26;
-	case UNW_AARCH64_X27:
-		return PERF_REG_ARM64_X27;
-	case UNW_AARCH64_X28:
-		return PERF_REG_ARM64_X28;
-	case UNW_AARCH64_X29:
-		return PERF_REG_ARM64_X29;
-	case UNW_AARCH64_X30:
-		return PERF_REG_ARM64_LR;
-	case UNW_AARCH64_SP:
-		return PERF_REG_ARM64_SP;
-	case UNW_AARCH64_PC:
-		return PERF_REG_ARM64_PC;
-	default:
-		pr_err("unwind: invalid reg id %d\n", regnum);
+	if (regnum < 0 || regnum >= PERF_REG_ARM64_EXTENDED_MAX)
 		return -EINVAL;
-	}
 
-	return -EINVAL;
+	return regnum;
 }
Signature · 1 line
2.28.0
#7 lore raw

PATCH v3 5/5 perf tools: arm64: Add support for VG register

In reply to James Clark

Add the name of the VG register so it can be used in --user-regs

The event will fail to open if the register is requested but not available so only add it to the mask if the kernel supports sve and also if it supports that specific register.

---
 tools/perf/arch/arm64/util/perf_regs.c | 34 ++++++++++++++++++++++++++
 tools/perf/util/perf_regs.c            |  2 ++
 2 files changed, 36 insertions(+)
tools/perf/arch/arm64/util/perf_regs.c +34 -0
index 476b037eea1c..c0a921512a90 100644
@@ -2,9 +2,11 @@
 #include <errno.h>
 #include <regex.h>
 #include <string.h>
+#include <sys/auxv.h>
 #include <linux/kernel.h>
 #include <linux/zalloc.h>
 
+#include "../../../perf-sys.h"
 #include "../../../util/debug.h"
 #include "../../../util/event.h"
 #include "../../../util/perf_regs.h"
@@ -43,6 +45,7 @@ const struct sample_reg sample_reg_masks[] = {
 	SMPL_REG(lr, PERF_REG_ARM64_LR),
 	SMPL_REG(sp, PERF_REG_ARM64_SP),
 	SMPL_REG(pc, PERF_REG_ARM64_PC),
+	SMPL_REG(vg, PERF_REG_ARM64_VG),
 	SMPL_REG_END
 };
 
@@ -131,3 +134,34 @@ int arch_sdt_arg_parse_op(char *old_op, char **new_op)
 
 	return SDT_ARG_VALID;
 }
+
+uint64_t arch__user_reg_mask(void)
+{
+	struct perf_event_attr attr = {
+		.type                   = PERF_TYPE_HARDWARE,
+		.config                 = PERF_COUNT_HW_CPU_CYCLES,
+		.sample_type            = PERF_SAMPLE_REGS_USER,
+		.disabled               = 1,
+		.exclude_kernel         = 1,
+		.sample_period		= 1,
+		.sample_regs_user	= PERF_REGS_MASK
+	};
+	int fd;
+
+	if (getauxval(AT_HWCAP) & HWCAP_SVE)
+		attr.sample_regs_user |= SMPL_REG_MASK(PERF_REG_ARM64_VG);
+
+	/*
+	 * Check if the pmu supports perf extended regs, before
+	 * returning the register mask to sample.
+	 */
+	if (attr.sample_regs_user != PERF_REGS_MASK) {
+		event_attr_init(&attr);
+		fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
+		if (fd != -1) {
+			close(fd);
+			return attr.sample_regs_user;
+		}
+	}
+	return PERF_REGS_MASK;
+}
tools/perf/util/perf_regs.c +2 -0
index a982e40ee5a9..872dd3d38782 100644
@@ -103,6 +103,8 @@ static const char *__perf_reg_name_arm64(int id)
 		return "lr";
 	case PERF_REG_ARM64_PC:
 		return "pc";
+	case PERF_REG_ARM64_VG:
+		return "vg";
 	default:
 		return NULL;
 	}
Signature · 1 line
2.28.0
maintainer Applied
#8 lore raw
In reply to James Clark

Em Wed, May 25, 2022 at 04:41:09PM +0100, James Clark escreveu:

Quoted 8 lines

Changes since v2:
* Add commit to fix header include path to use Perf's copy
* Apply Leo's review tags

When SVE registers are pushed onto the stack the VG register is required to unwind because the stack offsets would vary by the SVE register width at the time when the sample was taken.

Quoted 8 lines

The patches ("[PATCH v2 0/2] perf: arm64: Kernel support for Dwarf unwinding through SVE functions") add support for sampling the VG register to the kernel and the docs. This is the patchset to add support to userspace perf.

A small change is also required to libunwind or libdw depending on which unwinder is used, and these will be published later. Without these changes Perf continues to work with both libraries, although the VG register is still not used for unwinding.

Thanks, applied.

- Arnaldo

Quoted 20 lines

Thanks
James

James Clark (5):
perf tools: arm64: Use perf's copy of kernel headers perf tools: arm64: Copy perf_regs.h from the kernel perf tools: Use dynamic register set for Dwarf unwind perf tools: arm64: Decouple Libunwind register names from Perf perf tools: arm64: Add support for VG register

 tools/arch/arm64/include/uapi/asm/perf_regs.h |  7 +-
 tools/perf/arch/arm64/util/perf_regs.c        | 34 +++++++++
 tools/perf/arch/arm64/util/unwind-libunwind.c | 73 +------------------
 tools/perf/util/evsel.c                       |  2 +-
 tools/perf/util/libunwind/arm64.c             |  2 +-
 tools/perf/util/perf_regs.c                   |  2 +
 6 files changed, 46 insertions(+), 74 deletions(-)
Signature · 1 line
2.28.0
Signature · 1 line
- Arnaldo
#9 lore raw
Quoted 13 lines · Arnaldo Carvalho de Melo

Em Wed, May 25, 2022 at 04:41:09PM +0100, James Clark escreveu:

Quoted 12 lines

Changes since v2:
* Add commit to fix header include path to use Perf's copy
* Apply Leo's review tags

When SVE registers are pushed onto the stack the VG register is required to unwind because the stack offsets would vary by the SVE register width at the time when the sample was taken.

The patches ("[PATCH v2 0/2] perf: arm64: Kernel support for Dwarf unwinding through SVE functions") add support for sampling the VG register to the kernel and the docs. This is the patchset to add support to userspace perf.

Quoted 8 lines
Quoted 4 lines

A small change is also required to libunwind or libdw depending on which unwinder is used, and these will be published later. Without these changes Perf continues to work with both libraries, although the VG register is still not used for unwinding.

Thanks, applied.

- Arnaldo

Hi Arnaldo,

Will has applied the kernel side here:
https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git/commit/?h=for-next/perf&id=cbb0c02caf4bd98b9e0cd6d7420734b8e9a35703

There was a small change in the formatting in perf_regs.h, but the values are all the same. So in theory it's fine to leave Perf's copy until the next update, although it is a bit weird.

Thanks
James

Quoted 23 lines
Quoted 22 lines

Thanks
James

James Clark (5):
perf tools: arm64: Use perf's copy of kernel headers perf tools: arm64: Copy perf_regs.h from the kernel perf tools: Use dynamic register set for Dwarf unwind perf tools: arm64: Decouple Libunwind register names from Perf perf tools: arm64: Add support for VG register

 tools/arch/arm64/include/uapi/asm/perf_regs.h |  7 +-
 tools/perf/arch/arm64/util/perf_regs.c        | 34 +++++++++
 tools/perf/arch/arm64/util/unwind-libunwind.c | 73 +------------------
 tools/perf/util/evsel.c                       |  2 +-
 tools/perf/util/libunwind/arm64.c             |  2 +-
 tools/perf/util/perf_regs.c                   |  2 +
 6 files changed, 46 insertions(+), 74 deletions(-)
Signature · 1 line
2.28.0