Skip to contentSkip to conversation

arm64/perf: Enable branch stack sampling

PATCH Superseded Rob Herring (Arm) 5 participants 18 messages Apr 7, 2025 – May 28, 2025

Subsystems (4)

v21 of 23 Superseded Latest is v23 →

lore
Share & tools
lore
#0 lore raw

v21 cover arm64/perf: Enable branch stack sampling

This series enables perf branch stack sampling support on arm64 via a v9.2 arch feature called Branch Record Buffer Extension (BRBE). Details on BRBE can be found in the Arm ARM[1] chapter D18.

I've picked up this series from Anshuman. v19 and v20 versions have been reworked quite a bit by Mark and myself. The bulk of those changes are in patch 4.

A git branch is here[2].

[1] https://developer.arm.com/documentation/ddi0487/latest/
[2] git://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git arm/brbe-v21

v21:
- Drop clean-up patches 1-7 already applied
- Rebase on v6.15-rc1

v20:
- https://lore.kernel.org/r/[email protected]
- Added back some of the arm64 specific exception types. The x86 IRQ branches also include other exceptions like page faults. On arm64, we can distinguish the exception types, so we do. Also, to better align with x86, we convert 'call' branches which are user to kernel to 'syscall'.
- Only enable exceptions and exception returns if recording kernel branches (matching x86)
- Drop requiring event and branch privileges to match
- Add "branches" caps sysfs attribute like x86
- Reword comment about FZP and MDCR_EL2.HPMN interaction
- Rework BRBE invalidation to avoid invalidating in interrupt handler when no handled events capture the branch stack (i.e. when there are multiple users).
- Also clear BRBCR_ELx bits in brbe_disable(). This is for KVM nVHE checks if BRBE is enabled.
- Document that MDCR_EL3.SBRBE can be 0b01 also

v19:
- https://lore.kernel.org/all/[email protected]/
- Drop saving of branch records when task scheduled out (Mark). Make sched_task() callback actually get called. Enabling requires a call to perf_sched_cb_inc(). So the saving of branch records never happened.
- Got rid of added armpmu ops. All BRBE support is contained within pmuv3 code.
- Fix freeze on overflow for VHE
- The cycle counter doesn't freeze BRBE on overflow, so avoid assigning it when BRBE is enabled.
- Drop all the Arm specific exception branches. Not a clear need for them.
- Fix handling of branch 'cycles' reading. CC field is mantissa/exponent, not an integer.
- Rework s/w filtering to better match h/w filtering
- Reject events with disjoint event filter and branch filter or with exclude_host set
- Dropped perf test patch which has been applied for 6.14
- Dropped patch "KVM: arm64: Explicitly handle BRBE traps as UNDEFINED" which has been applied for 6.14

v18:
- https://lore.kernel.org/all/[email protected]/

For v1-v17, see the above link. Not going to duplicate it all here...

---
Anshuman Khandual (4):
arm64/sysreg: Add BRBE registers and fields
arm64: Handle BRBE booting requirements
KVM: arm64: nvhe: Disable branch generation in nVHE guests
perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)

 Documentation/arch/arm64/booting.rst |  21 +
 arch/arm64/include/asm/el2_setup.h   |  86 +++-
 arch/arm64/include/asm/kvm_host.h    |   2 +
 arch/arm64/include/asm/sysreg.h      |  17 +-
 arch/arm64/kvm/debug.c               |   4 +
 arch/arm64/kvm/hyp/nvhe/debug-sr.c   |  32 ++
 arch/arm64/kvm/hyp/nvhe/switch.c     |   2 +-
 arch/arm64/tools/sysreg              | 132 ++++++
 drivers/perf/Kconfig                 |  11 +
 drivers/perf/Makefile                |   1 +
 drivers/perf/arm_brbe.c              | 802 +++++++++++++++++++++++++++++++++++
 drivers/perf/arm_brbe.h              |  47 ++
 drivers/perf/arm_pmu.c               |  15 +-
 drivers/perf/arm_pmuv3.c             | 129 +++++-
 include/linux/perf/arm_pmu.h         |   8 +
 15 files changed, 1287 insertions(+), 22 deletions(-)

---
base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8
change-id: 20250129-arm-brbe-v19-24d5d9e5e623

Best regards,

Signature · 1 line
Rob Herring (Arm) <[email protected]>
#1 lore raw

PATCH v21 1/4 arm64/sysreg: Add BRBE registers and fields

From: Anshuman Khandual <[email protected]>

This patch adds definitions related to the Branch Record Buffer Extension (BRBE) as per ARM DDI 0487K.a. These will be used by KVM and a BRBE driver in subsequent patches.

Some existing BRBE definitions in asm/sysreg.h are replaced with equivalent generated definitions.

---
v19:
- split BRBINF.CC field into mantissa and exponent

---
 arch/arm64/include/asm/sysreg.h |  17 ++----
 arch/arm64/tools/sysreg         | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+), 11 deletions(-)
arch/arm64/include/asm/sysreg.h +6 -11
index 2639d3633073..67e96494749e 100644
@@ -198,16 +198,8 @@
 #define SYS_DBGVCR32_EL2		sys_reg(2, 4, 0, 7, 0)
 
 #define SYS_BRBINF_EL1(n)		sys_reg(2, 1, 8, (n & 15), (((n & 16) >> 2) | 0))
-#define SYS_BRBINFINJ_EL1		sys_reg(2, 1, 9, 1, 0)
 #define SYS_BRBSRC_EL1(n)		sys_reg(2, 1, 8, (n & 15), (((n & 16) >> 2) | 1))
-#define SYS_BRBSRCINJ_EL1		sys_reg(2, 1, 9, 1, 1)
 #define SYS_BRBTGT_EL1(n)		sys_reg(2, 1, 8, (n & 15), (((n & 16) >> 2) | 2))
-#define SYS_BRBTGTINJ_EL1		sys_reg(2, 1, 9, 1, 2)
-#define SYS_BRBTS_EL1			sys_reg(2, 1, 9, 0, 2)
-
-#define SYS_BRBCR_EL1			sys_reg(2, 1, 9, 0, 0)
-#define SYS_BRBFCR_EL1			sys_reg(2, 1, 9, 0, 1)
-#define SYS_BRBIDR0_EL1			sys_reg(2, 1, 9, 2, 0)
 
 #define SYS_TRCITECR_EL1		sys_reg(3, 0, 1, 2, 3)
 #define SYS_TRCACATR(m)			sys_reg(2, 1, 2, ((m & 7) << 1), (2 | (m >> 3)))
@@ -273,8 +265,6 @@
 /* ETM */
 #define SYS_TRCOSLAR			sys_reg(2, 1, 1, 0, 4)
 
-#define SYS_BRBCR_EL2			sys_reg(2, 4, 9, 0, 0)
-
 #define SYS_MIDR_EL1			sys_reg(3, 0, 0, 0, 0)
 #define SYS_MPIDR_EL1			sys_reg(3, 0, 0, 0, 5)
 #define SYS_REVIDR_EL1			sys_reg(3, 0, 0, 0, 6)
@@ -607,7 +597,6 @@
 #define SYS_CNTHV_CVAL_EL2		sys_reg(3, 4, 14, 3, 2)
 
 /* VHE encodings for architectural EL0/1 system registers */
-#define SYS_BRBCR_EL12			sys_reg(2, 5, 9, 0, 0)
 #define SYS_SCTLR_EL12			sys_reg(3, 5, 1, 0, 0)
 #define SYS_CPACR_EL12			sys_reg(3, 5, 1, 0, 2)
 #define SYS_SCTLR2_EL12			sys_reg(3, 5, 1, 0, 3)
@@ -818,6 +807,12 @@
 #define OP_COSP_RCTX			sys_insn(1, 3, 7, 3, 6)
 #define OP_CPP_RCTX			sys_insn(1, 3, 7, 3, 7)
 
+/*
+ * BRBE Instructions
+ */
+#define BRB_IALL_INSN	__emit_inst(0xd5000000 | OP_BRB_IALL | (0x1f))
+#define BRB_INJ_INSN	__emit_inst(0xd5000000 | OP_BRB_INJ  | (0x1f))
+
 /* Common SCTLR_ELx flags. */
 #define SCTLR_ELx_ENTP2	(BIT(60))
 #define SCTLR_ELx_DSSBS	(BIT(44))
arch/arm64/tools/sysreg +132 -0

138 lines not on this page · raw message

Signature · 1 line
2.47.2
maintainer Acked-by on v21 1/4
#2 lore raw

Re: [PATCH v21 1/4] arm64/sysreg: Add BRBE registers and fields

Quoted 15 lines · Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

This patch adds definitions related to the Branch Record Buffer Extension (BRBE) as per ARM DDI 0487K.a. These will be used by KVM and a BRBE driver in subsequent patches.

Some existing BRBE definitions in asm/sysreg.h are replaced with equivalent generated definitions.

Quoted 7 lines

---
v19:
- split BRBINF.CC field into mantissa and exponent

---
 arch/arm64/include/asm/sysreg.h |  17 ++----
 arch/arm64/tools/sysreg         | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+), 11 deletions(-)

Some of the field names here are made up, but overall they seem to correspond to the architecture, so:

Will

#3 lore raw

PATCH v21 2/4 arm64: Handle BRBE booting requirements

In reply to Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

To use the Branch Record Buffer Extension (BRBE), some configuration is necessary at EL3 and EL2. This patch documents the requirements and adds the initial EL2 setup code, which largely consists of configuring the fine-grained traps and initializing a couple of BRBE control registers.

Before this patch, __init_el2_fgt() would initialize HDFGRTR_EL2 and HDFGWTR_EL2 with the same value, relying on the read/write trap controls for a register occupying the same bit position in either register. The 'nBRBIDR' trap control only exists in bit 59 of HDFGRTR_EL2, while bit 59 of HDFGWTR_EL2 is RES0, and so this assumption no longer holds.

To handle HDFGRTR_EL2 and HDFGWTR_EL2 having (slightly) different bit layouts, __init_el2_fgt() is changed to accumulate the HDFGRTR_EL2 and HDFGWTR_EL2 control bits separately. While making this change the open-coded value (1 << 62) is replaced with HDFG{R,W}TR_EL2_nPMSNEVFR_EL1_MASK.

The BRBCR_EL1 and BRBCR_EL2 registers are unusual and require special
initialisation: even though they are subject to E2H renaming, both have an effect regardless of HCR_EL2.TGE, even when running at EL2, and consequently both need to be initialised. This is handled in __init_el2_brbe() with a comment to explain the situation.

Cc Marc Zyngier <[email protected]>
Cc Oliver Upton <[email protected]>
[Mark: rewrite commit message, fix typo in comment]

---
v20:
- Document that MDCR_EL3.SBRBE can be 0b01 also
- Fix "HDFGWTR_EL2 is RES0" in commit msg

---
 Documentation/arch/arm64/booting.rst | 21 +++++++++
 arch/arm64/include/asm/el2_setup.h   | 86 ++++++++++++++++++++++++++++++++++--
 2 files changed, 104 insertions(+), 3 deletions(-)
Documentation/arch/arm64/booting.rst +21 -0
index dee7b6de864f..a627c1e0e4a0 100644
@@ -358,6 +358,27 @@ Before jumping into the kernel, the following conditions must be met:
 
     - HWFGWTR_EL2.nSMPRI_EL1 (bit 54) must be initialised to 0b01.
 
+  For CPUs with feature Branch Record Buffer Extension (FEAT_BRBE):
+
+  - If EL3 is present:
+
+    - MDCR_EL3.SBRBE (bits 33:32) must be initialised to 0b01 or 0b11.
+
+  - If the kernel is entered at EL1 and EL2 is present:
+
+    - BRBCR_EL2.CC (bit 3) must be initialised to 0b1.
+    - BRBCR_EL2.MPRED (bit 4) must be initialised to 0b1.
+
+    - HDFGRTR_EL2.nBRBDATA (bit 61) must be initialised to 0b1.
+    - HDFGRTR_EL2.nBRBCTL  (bit 60) must be initialised to 0b1.
+    - HDFGRTR_EL2.nBRBIDR  (bit 59) must be initialised to 0b1.
+
+    - HDFGWTR_EL2.nBRBDATA (bit 61) must be initialised to 0b1.
+    - HDFGWTR_EL2.nBRBCTL  (bit 60) must be initialised to 0b1.
+
+    - HFGITR_EL2.nBRBIALL (bit 56) must be initialised to 0b1.
+    - HFGITR_EL2.nBRBINJ  (bit 55) must be initialised to 0b1.
+
   For CPUs with the Scalable Matrix Extension FA64 feature (FEAT_SME_FA64):
 
   - If EL3 is present:
arch/arm64/include/asm/el2_setup.h +83 -3

118 lines not on this page · raw message

Signature · 1 line
2.47.2
maintainer
#4 lore raw

Re: [PATCH v21 2/4] arm64: Handle BRBE booting requirements

Quoted 33 lines · Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

To use the Branch Record Buffer Extension (BRBE), some configuration is necessary at EL3 and EL2. This patch documents the requirements and adds the initial EL2 setup code, which largely consists of configuring the fine-grained traps and initializing a couple of BRBE control registers.

Before this patch, __init_el2_fgt() would initialize HDFGRTR_EL2 and HDFGWTR_EL2 with the same value, relying on the read/write trap controls for a register occupying the same bit position in either register. The 'nBRBIDR' trap control only exists in bit 59 of HDFGRTR_EL2, while bit 59 of HDFGWTR_EL2 is RES0, and so this assumption no longer holds.

To handle HDFGRTR_EL2 and HDFGWTR_EL2 having (slightly) different bit layouts, __init_el2_fgt() is changed to accumulate the HDFGRTR_EL2 and HDFGWTR_EL2 control bits separately. While making this change the open-coded value (1 << 62) is replaced with HDFG{R,W}TR_EL2_nPMSNEVFR_EL1_MASK.

The BRBCR_EL1 and BRBCR_EL2 registers are unusual and require special
initialisation: even though they are subject to E2H renaming, both have an effect regardless of HCR_EL2.TGE, even when running at EL2, and consequently both need to be initialised. This is handled in __init_el2_brbe() with a comment to explain the situation.

Cc Marc Zyngier <[email protected]>
Cc Oliver Upton <[email protected]>
[Mark: rewrite commit message, fix typo in comment]
Quoted 8 lines

---
v20:
- Document that MDCR_EL3.SBRBE can be 0b01 also
- Fix "HDFGWTR_EL2 is RES0" in commit msg

---
 Documentation/arch/arm64/booting.rst | 21 +++++++++
 arch/arm64/include/asm/el2_setup.h   | 86 ++++++++++++++++++++++++++++++++++--
 2 files changed, 104 insertions(+), 3 deletions(-)

It would be good to have an Ack from the kvm/arm64 side on this, but in the meantime I've left some comments inline.

Documentation/arch/arm64/booting.rst Before jumping into the kernel, the following conditions must be met: · line 361

Quoted 9 lines
Documentation/arch/arm64/booting.rst +1 -0
index dee7b6de864f..a627c1e0e4a0 100644
@@ -358,6 +358,27 @@ Before jumping into the kernel, the following conditions must be met:
 
     - HWFGWTR_EL2.nSMPRI_EL1 (bit 54) must be initialised to 0b01.
 
+  For CPUs with feature Branch Record Buffer Extension (FEAT_BRBE):

This doesn't make sense ^^^

Quoted 29 lines
arch/arm64/include/asm/el2_setup.h +21 -0
index ebceaae3c749..e7fcba1e7d8e 100644
@@ -189,6 +189,39 @@
 .Lskip_set_cptr_\@:
 .endm
 
+/*
+ * Configure BRBE to permit recording cycle counts and branch mispredicts.
+ *
+ * At any EL, to record cycle counts BRBE requires that both BRBCR_EL2.CC=1 and
+ * BRBCR_EL1.CC=1.
+ *
+ * At any EL, to record branch mispredicts BRBE requires that both
+ * BRBCR_EL2.MPRED=1 and BRBCR_EL1.MPRED=1.
+ *
+ * When HCR_EL2.E2H=1, the BRBCR_EL1 encoding is redirected to BRBCR_EL2, but
+ * the {CC,MPRED} bits in the real BRBCR_EL1 register still apply.
+ *
+ * Set {CC,MPRED} in both BRBCR_EL2 and BRBCR_EL1 so that at runtime we only
+ * need to enable/disable these in BRBCR_EL1 regardless of whether the kernel
+ * ends up executing in EL1 or EL2.
+ */
+.macro __init_el2_brbe
+	mrs	x1, id_aa64dfr0_el1
+	ubfx	x1, x1, #ID_AA64DFR0_EL1_BRBE_SHIFT, #4
+	cbz	x1, .Lskip_brbe_\@
+

arch/arm64/include/asm/el2_setup.h line 222

Quoted 10 lines
arch/arm64/include/asm/el2_setup.h +10 -0
+	mov_q	x0, BRBCR_ELx_CC | BRBCR_ELx_MPRED
+	msr_s	SYS_BRBCR_EL2, x0
+
+	__check_hvhe .Lset_brbe_nvhe_\@, x1
+	msr_s	SYS_BRBCR_EL12, x0	// VHE
+	b	.Lskip_brbe_\@
+
+.Lset_brbe_nvhe_\@:
+	msr_s	SYS_BRBCR_EL1, x0	// NVHE
+.Lskip_brbe_\@:

Why do we have to poke BRBCR_EL12/BRBCR_EL1 here rather than in the BRBE driver code?

Quoted 12 lines
arch/arm64/include/asm/el2_setup.h +3 -0
+.endm
+
 /* Disable any fine grained traps */
 .macro __init_el2_fgt
 	mrs	x1, id_aa64mmfr0_el1
@@ -196,16 +229,48 @@
 	cbz	x1, .Lskip_fgt_\@
 
 	mov	x0, xzr
+	mov	x2, xzr
 	mrs	x1, id_aa64dfr0_el1
 	ubfx	x1, x1, #ID_AA64DFR0_EL1_PMSVer_SHIFT, #4

arch/arm64/include/asm/el2_setup.h line 243

Quoted 10 lines
arch/arm64/include/asm/el2_setup.h +4 -1
 	cmp	x1, #3
 	b.lt	.Lskip_spe_fgt_\@
+
 	/* Disable PMSNEVFR_EL1 read and write traps */
-	orr	x0, x0, #(1 << 62)
+	orr	x0, x0, #HDFGRTR_EL2_nPMSNEVFR_EL1_MASK
+	orr	x2, x2, #HDFGWTR_EL2_nPMSNEVFR_EL1_MASK
 
 .Lskip_spe_fgt_\@:
+#ifdef CONFIG_ARM64_BRBE

Why is this gated on CONFIG_ARM64_BRBE?

Quoted 17 lines
arch/arm64/include/asm/el2_setup.h +17 -0
+	mrs	x1, id_aa64dfr0_el1
+	ubfx	x1, x1, #ID_AA64DFR0_EL1_BRBE_SHIFT, #4
+	cbz	x1, .Lskip_brbe_reg_fgt_\@
+
+	/*
+	 * Disable read traps for the following registers
+	 *
+	 * [BRBSRC|BRBTGT|RBINF]_EL1
+	 * [BRBSRCINJ|BRBTGTINJ|BRBINFINJ|BRBTS]_EL1
+	 */
+	orr	x0, x0, #HDFGRTR_EL2_nBRBDATA_MASK
+
+	/*
+	 * Disable write traps for the following registers
+	 *
+	 * [BRBSRCINJ|BRBTGTINJ|BRBINFINJ|BRBTS]_EL1
+	 */

arch/arm64/include/asm/el2_setup.h line 270

Quoted 10 lines
arch/arm64/include/asm/el2_setup.h +10 -0
+	orr	x2, x2, #HDFGWTR_EL2_nBRBDATA_MASK
+
+	/* Disable read and write traps for [BRBCR|BRBFCR]_EL1 */
+	orr	x0, x0, #HDFGRTR_EL2_nBRBCTL_MASK
+	orr	x2, x2, #HDFGWTR_EL2_nBRBCTL_MASK
+
+	/* Disable read traps for BRBIDR_EL1 */
+	orr	x0, x0, #HDFGRTR_EL2_nBRBIDR_MASK
+
+.Lskip_brbe_reg_fgt_\@:

I think this label should become .Lset_debug_fgt_\@:. That way, we have a clear point at which we're done with HDFG*TR_EL2. We can zero x0 and x2 and from then on we can focus on HFG*TR + HFGITR.

nit: the existing .Lskip_debug_fgt_\@ label looks to be misnamed -- it should probably be .Lskip_sme_fgt_\@ ?

arch/arm64/include/asm/el2_setup.h line 273

Quoted 4 lines
arch/arm64/include/asm/el2_setup.h +2 -1
+#endif /* CONFIG_ARM64_BRBE */
 	msr_s	SYS_HDFGRTR_EL2, x0
-	msr_s	SYS_HDFGWTR_EL2, x0
+	msr_s	SYS_HDFGWTR_EL2, x2

nit: It would be cleaner to use x0/x1 for the pair of trap registers but I can see that would be a more invasive change.

arch/arm64/include/asm/el2_setup.h line 318

Quoted 12 lines
arch/arm64/include/asm/el2_setup.h +5 -1
 	mov	x0, xzr
 	mrs	x1, id_aa64pfr1_el1
@@ -246,7 +311,21 @@
 .Lset_fgt_\@:
 	msr_s	SYS_HFGRTR_EL2, x0
 	msr_s	SYS_HFGWTR_EL2, x0
-	msr_s	SYS_HFGITR_EL2, xzr
+	mov	x0, xzr
+#ifdef CONFIG_ARM64_BRBE
+	mrs	x1, id_aa64dfr0_el1
+	ubfx	x1, x1, #ID_AA64DFR0_EL1_BRBE_SHIFT, #4
+	cbz	x1, .Lskip_brbe_insn_fgt_\@

It would probably scale better if we unconditionally stick x2 in HFGITR_EL2 and zero that register in '.Lset_debug_fgt_\@'. We still have two checks for BRBE, but at least '.Lset_fgt_\@' could just stick to writing the registers.

Will

#5 lore raw

Re: [PATCH v21 2/4] arm64: Handle BRBE booting requirements

Quoted 52 lines · Will Deacon
Quoted 33 lines · Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

To use the Branch Record Buffer Extension (BRBE), some configuration is necessary at EL3 and EL2. This patch documents the requirements and adds the initial EL2 setup code, which largely consists of configuring the fine-grained traps and initializing a couple of BRBE control registers.

Before this patch, __init_el2_fgt() would initialize HDFGRTR_EL2 and HDFGWTR_EL2 with the same value, relying on the read/write trap controls for a register occupying the same bit position in either register. The 'nBRBIDR' trap control only exists in bit 59 of HDFGRTR_EL2, while bit 59 of HDFGWTR_EL2 is RES0, and so this assumption no longer holds.

To handle HDFGRTR_EL2 and HDFGWTR_EL2 having (slightly) different bit layouts, __init_el2_fgt() is changed to accumulate the HDFGRTR_EL2 and HDFGWTR_EL2 control bits separately. While making this change the open-coded value (1 << 62) is replaced with HDFG{R,W}TR_EL2_nPMSNEVFR_EL1_MASK.

The BRBCR_EL1 and BRBCR_EL2 registers are unusual and require special
initialisation: even though they are subject to E2H renaming, both have an effect regardless of HCR_EL2.TGE, even when running at EL2, and consequently both need to be initialised. This is handled in __init_el2_brbe() with a comment to explain the situation.

Cc Marc Zyngier <[email protected]>
Cc Oliver Upton <[email protected]>
[Mark: rewrite commit message, fix typo in comment]
Quoted 8 lines

---
v20:
- Document that MDCR_EL3.SBRBE can be 0b01 also
- Fix "HDFGWTR_EL2 is RES0" in commit msg

---
 Documentation/arch/arm64/booting.rst | 21 +++++++++
 arch/arm64/include/asm/el2_setup.h   | 86 ++++++++++++++++++++++++++++++++++--
 2 files changed, 104 insertions(+), 3 deletions(-)

It would be good to have an Ack from the kvm/arm64 side on this, but in the meantime I've left some comments inline.

Quoted 6 lines
Documentation/arch/arm64/booting.rst +0 -0
index dee7b6de864f..a627c1e0e4a0 100644
@@ -358,6 +358,27 @@ Before jumping into the kernel, the following conditions must be met:
 
Quoted 5 lines

Documentation/arch/arm64/booting.rst Before jumping into the kernel, the following conditions must be met: · line 361

Quoted 3 lines
Documentation/arch/arm64/booting.rst +1 -0
     - HWFGWTR_EL2.nSMPRI_EL1 (bit 54) must be initialised to 0b01.
 
+  For CPUs with feature Branch Record Buffer Extension (FEAT_BRBE):

This doesn't make sense ^^^

You mean it should be "with the Branch Record Buffer Extension feature" instead?

Quoted 35 lines
Quoted 35 lines
arch/arm64/include/asm/el2_setup.h +26 -0
index ebceaae3c749..e7fcba1e7d8e 100644
@@ -189,6 +189,39 @@
 .Lskip_set_cptr_\@:
 .endm
 
+/*
+ * Configure BRBE to permit recording cycle counts and branch mispredicts.
+ *
+ * At any EL, to record cycle counts BRBE requires that both BRBCR_EL2.CC=1 and
+ * BRBCR_EL1.CC=1.
+ *
+ * At any EL, to record branch mispredicts BRBE requires that both
+ * BRBCR_EL2.MPRED=1 and BRBCR_EL1.MPRED=1.
+ *
+ * When HCR_EL2.E2H=1, the BRBCR_EL1 encoding is redirected to BRBCR_EL2, but
+ * the {CC,MPRED} bits in the real BRBCR_EL1 register still apply.
+ *
+ * Set {CC,MPRED} in both BRBCR_EL2 and BRBCR_EL1 so that at runtime we only
+ * need to enable/disable these in BRBCR_EL1 regardless of whether the kernel
+ * ends up executing in EL1 or EL2.
+ */
+.macro __init_el2_brbe
+	mrs	x1, id_aa64dfr0_el1
+	ubfx	x1, x1, #ID_AA64DFR0_EL1_BRBE_SHIFT, #4
+	cbz	x1, .Lskip_brbe_\@
+
+	mov_q	x0, BRBCR_ELx_CC | BRBCR_ELx_MPRED
+	msr_s	SYS_BRBCR_EL2, x0
+
+	__check_hvhe .Lset_brbe_nvhe_\@, x1
+	msr_s	SYS_BRBCR_EL12, x0	// VHE
Quoted 8 lines

arch/arm64/include/asm/el2_setup.h line 222

Quoted 5 lines
arch/arm64/include/asm/el2_setup.h +5 -0
+	b	.Lskip_brbe_\@
+
+.Lset_brbe_nvhe_\@:
+	msr_s	SYS_BRBCR_EL1, x0	// NVHE
+.Lskip_brbe_\@:

Why do we have to poke BRBCR_EL12/BRBCR_EL1 here rather than in the BRBE driver code?

Yeah, I think we can drop this. Originally, the driver did not touch SYS_BRBCR_EL12, but it turns out we need to for freeze on overflow to work correctly with VHE (see the comment in the driver for SYS_BRBCR_EL12 access).

The only other reason I can come up with is some fields reset to UNKNOWN and there may be no driver. However, the important ones, ExBRE, reset to 0, so we should be fine.

Quoted 20 lines
Quoted 20 lines
arch/arm64/include/asm/el2_setup.h +6 -1
+.endm
+
 /* Disable any fine grained traps */
 .macro __init_el2_fgt
 	mrs	x1, id_aa64mmfr0_el1
@@ -196,16 +229,48 @@
 	cbz	x1, .Lskip_fgt_\@
 
 	mov	x0, xzr
+	mov	x2, xzr
 	mrs	x1, id_aa64dfr0_el1
 	ubfx	x1, x1, #ID_AA64DFR0_EL1_PMSVer_SHIFT, #4
 	cmp	x1, #3
 	b.lt	.Lskip_spe_fgt_\@
+
 	/* Disable PMSNEVFR_EL1 read and write traps */
-	orr	x0, x0, #(1 << 62)
+	orr	x0, x0, #HDFGRTR_EL2_nPMSNEVFR_EL1_MASK
+	orr	x2, x2, #HDFGWTR_EL2_nPMSNEVFR_EL1_MASK
 
Quoted 4 lines

arch/arm64/include/asm/el2_setup.h line 243

Quoted 2 lines
arch/arm64/include/asm/el2_setup.h +1 -0
 .Lskip_spe_fgt_\@:
+#ifdef CONFIG_ARM64_BRBE

Why is this gated on CONFIG_ARM64_BRBE?

Shrug. We don't do that anywhere else, so I'll drop it.

Quoted 35 lines
Quoted 17 lines
arch/arm64/include/asm/el2_setup.h +17 -0
+	mrs	x1, id_aa64dfr0_el1
+	ubfx	x1, x1, #ID_AA64DFR0_EL1_BRBE_SHIFT, #4
+	cbz	x1, .Lskip_brbe_reg_fgt_\@
+
+	/*
+	 * Disable read traps for the following registers
+	 *
+	 * [BRBSRC|BRBTGT|RBINF]_EL1
+	 * [BRBSRCINJ|BRBTGTINJ|BRBINFINJ|BRBTS]_EL1
+	 */
+	orr	x0, x0, #HDFGRTR_EL2_nBRBDATA_MASK
+
+	/*
+	 * Disable write traps for the following registers
+	 *
+	 * [BRBSRCINJ|BRBTGTINJ|BRBINFINJ|BRBTS]_EL1
+	 */

arch/arm64/include/asm/el2_setup.h line 270

Quoted 10 lines
arch/arm64/include/asm/el2_setup.h +10 -0
+	orr	x2, x2, #HDFGWTR_EL2_nBRBDATA_MASK
+
+	/* Disable read and write traps for [BRBCR|BRBFCR]_EL1 */
+	orr	x0, x0, #HDFGRTR_EL2_nBRBCTL_MASK
+	orr	x2, x2, #HDFGWTR_EL2_nBRBCTL_MASK
+
+	/* Disable read traps for BRBIDR_EL1 */
+	orr	x0, x0, #HDFGRTR_EL2_nBRBIDR_MASK
+
+.Lskip_brbe_reg_fgt_\@:

I think this label should become .Lset_debug_fgt_\@:. That way, we have a clear point at which we're done with HDFG*TR_EL2. We can zero x0 and x2 and from then on we can focus on HFG*TR + HFGITR.

nit: the existing .Lskip_debug_fgt_\@ label looks to be misnamed -- it should probably be .Lskip_sme_fgt_\@ ?

Quoted 7 lines

arch/arm64/include/asm/el2_setup.h line 273

Quoted 4 lines
arch/arm64/include/asm/el2_setup.h +2 -1
+#endif /* CONFIG_ARM64_BRBE */
 	msr_s	SYS_HDFGRTR_EL2, x0
-	msr_s	SYS_HDFGWTR_EL2, x0
+	msr_s	SYS_HDFGWTR_EL2, x2

nit: It would be cleaner to use x0/x1 for the pair of trap registers but I can see that would be a more invasive change.

That would not be worth the churn IMO. We'd want to just swap x1 and x2 everywhere so we consistently use x2 for ID registers.

And then I'd be to blame for *all* this wonderful code. ;)

Quoted 10 lines
Quoted 10 lines
arch/arm64/include/asm/el2_setup.h +2 -1
 	mov	x0, xzr
 	mrs	x1, id_aa64pfr1_el1
@@ -246,7 +311,21 @@
 .Lset_fgt_\@:
 	msr_s	SYS_HFGRTR_EL2, x0
 	msr_s	SYS_HFGWTR_EL2, x0
-	msr_s	SYS_HFGITR_EL2, xzr
+	mov	x0, xzr
+#ifdef CONFIG_ARM64_BRBE
Quoted 8 lines

arch/arm64/include/asm/el2_setup.h line 318

Quoted 3 lines
arch/arm64/include/asm/el2_setup.h +3 -0
+	mrs	x1, id_aa64dfr0_el1
+	ubfx	x1, x1, #ID_AA64DFR0_EL1_BRBE_SHIFT, #4
+	cbz	x1, .Lskip_brbe_insn_fgt_\@

It would probably scale better if we unconditionally stick x2 in HFGITR_EL2 and zero that register in '.Lset_debug_fgt_\@'. We still have two checks for BRBE, but at least '.Lset_fgt_\@' could just stick to writing the registers.

All that looks doable.

Rob

#6 lore raw

PATCH v21 3/4 KVM: arm64: nvhe: Disable branch generation in nVHE guests

In reply to Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

While BRBE can record branches within guests, the host recording branches in guests is not supported by perf (though events are). Support for BRBE in guests will supported by providing direct access to BRBE within the guests. That is how x86 LBR works for guests. Therefore, BRBE needs to be disabled on guest entry and restored on exit.

For nVHE, this requires explicit handling for guests. Before entering a guest, save the BRBE state and disable the it. When returning to the host, restore the state.

For VHE, it is not necessary. We initialize BRBCR_EL1.{E1BRE,E0BRE}=={0,0} at boot time, and HCR_EL2.TGE==1 while running in the host. We configure BRBCR_EL2.{E2BRE,E0HBRE} to enable branch recording in the host. When entering the guest, we set HCR_EL2.TGE==0 which means BRBCR_EL1 is used instead of BRBCR_EL2. Consequently for VHE, BRBE recording is disabled at EL1 and EL0 when running a guest.

Should recording in guests (by the host) ever be desired, the perf ABI will need to be extended to distinguish guest addresses (struct perf_branch_entry.priv) for starters. BRBE records would also need to be invalidated on guest entry/exit as guest/host EL1 and EL0 records can't be distinguished.

---
v20:
- Reword commit message about no guest recording.
- Add BRBE to __kvm_vcpu_run() synchronization comment

v19:
- Rework due to v6.14 debug flag changes
- Redo commit message

---
 arch/arm64/include/asm/kvm_host.h  |  2 ++
 arch/arm64/kvm/debug.c             |  4 ++++
 arch/arm64/kvm/hyp/nvhe/debug-sr.c | 32 ++++++++++++++++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/switch.c   |  2 +-
 4 files changed, 39 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +2 -0
index e98cfe7855a6..e3f1e7b5ce52 100644
@@ -654,6 +654,7 @@ struct kvm_host_data {
 #define KVM_HOST_DATA_FLAG_HAS_TRBE			1
 #define KVM_HOST_DATA_FLAG_TRBE_ENABLED			4
 #define KVM_HOST_DATA_FLAG_EL1_TRACING_CONFIGURED	5
+#define KVM_HOST_DATA_FLAG_HAS_BRBE			6
 	unsigned long flags;
 
 	struct kvm_cpu_context host_ctxt;
@@ -687,6 +688,7 @@ struct kvm_host_data {
 		u64 trfcr_el1;
 		/* Values of trap registers for the host before guest entry. */
 		u64 mdcr_el2;
+		u64 brbcr_el1;
 	} host_debug_state;
 
 	/* Guest trace filter value */
arch/arm64/kvm/debug.c +4 -0
index 0e4c805e7e89..bc6015108a68 100644
@@ -81,6 +81,10 @@ void kvm_init_host_debug_data(void)
 	    !(read_sysreg_s(SYS_PMBIDR_EL1) & PMBIDR_EL1_P))
 		host_data_set_flag(HAS_SPE);
 
+	/* Check if we have BRBE implemented and available at the host */
+	if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_BRBE_SHIFT))
+		host_data_set_flag(HAS_BRBE);
+
 	if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceFilt_SHIFT)) {
 		/* Force disable trace in protected mode in case of no TRBE */
 		if (is_protected_kvm_enabled())
arch/arm64/kvm/hyp/nvhe/debug-sr.c +32 -0
index 2f4a4f5036bb..2a1c0f49792b 100644
@@ -92,12 +92,42 @@ static void __trace_switch_to_host(void)
 			  *host_data_ptr(host_debug_state.trfcr_el1));
 }
 
+static void __debug_save_brbe(u64 *brbcr_el1)
+{
+	*brbcr_el1 = 0;
+
+	/* Check if the BRBE is enabled */
+	if (!(read_sysreg_el1(SYS_BRBCR) & (BRBCR_ELx_E0BRE | BRBCR_ELx_ExBRE)))
+		return;
+
+	/*
+	 * Prohibit branch record generation while we are in guest.
+	 * Since access to BRBCR_EL1 is trapped, the guest can't
+	 * modify the filtering set by the host.
+	 */
+	*brbcr_el1 = read_sysreg_el1(SYS_BRBCR);
+	write_sysreg_el1(0, SYS_BRBCR);
+}
+
+static void __debug_restore_brbe(u64 brbcr_el1)
+{
+	if (!brbcr_el1)
+		return;
+
+	/* Restore BRBE controls */
+	write_sysreg_el1(brbcr_el1, SYS_BRBCR);
+}
+
 void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
 {
 	/* Disable and flush SPE data generation */
 	if (host_data_test_flag(HAS_SPE))
 		__debug_save_spe(host_data_ptr(host_debug_state.pmscr_el1));
 
+	/* Disable BRBE branch records */
+	if (host_data_test_flag(HAS_BRBE))
+		__debug_save_brbe(host_data_ptr(host_debug_state.brbcr_el1));
+
 	if (__trace_needs_switch())
 		__trace_switch_to_guest();
 }
@@ -111,6 +141,8 @@ void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
 {
 	if (host_data_test_flag(HAS_SPE))
 		__debug_restore_spe(*host_data_ptr(host_debug_state.pmscr_el1));
+	if (host_data_test_flag(HAS_BRBE))
+		__debug_restore_brbe(*host_data_ptr(host_debug_state.brbcr_el1));
 	if (__trace_needs_switch())
 		__trace_switch_to_host();
 }
arch/arm64/kvm/hyp/nvhe/switch.c +1 -1
index 7d2ba6ef0261..0d66d3b54800 100644
@@ -319,7 +319,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 	 * We're about to restore some new MMU state. Make sure
 	 * ongoing page-table walks that have started before we
 	 * trapped to EL2 have completed. This also synchronises the
-	 * above disabling of SPE and TRBE.
+	 * above disabling of BRBE, SPE and TRBE.
 	 *
 	 * See DDI0487I.a D8.1.5 "Out-of-context translation regimes",
 	 * rule R_LFHQG and subsequent information statements.
Signature · 1 line
2.47.2
maintainer
#7 lore raw

Re: [PATCH v21 3/4] KVM: arm64: nvhe: Disable branch generation in nVHE guests

Quoted 40 lines · Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

While BRBE can record branches within guests, the host recording branches in guests is not supported by perf (though events are). Support for BRBE in guests will supported by providing direct access to BRBE within the guests. That is how x86 LBR works for guests. Therefore, BRBE needs to be disabled on guest entry and restored on exit.

For nVHE, this requires explicit handling for guests. Before entering a guest, save the BRBE state and disable the it. When returning to the host, restore the state.

For VHE, it is not necessary. We initialize BRBCR_EL1.{E1BRE,E0BRE}=={0,0} at boot time, and HCR_EL2.TGE==1 while running in the host. We configure BRBCR_EL2.{E2BRE,E0HBRE} to enable branch recording in the host. When entering the guest, we set HCR_EL2.TGE==0 which means BRBCR_EL1 is used instead of BRBCR_EL2. Consequently for VHE, BRBE recording is disabled at EL1 and EL0 when running a guest.

Should recording in guests (by the host) ever be desired, the perf ABI will need to be extended to distinguish guest addresses (struct perf_branch_entry.priv) for starters. BRBE records would also need to be invalidated on guest entry/exit as guest/host EL1 and EL0 records can't be distinguished.

---
v20:
- Reword commit message about no guest recording.
- Add BRBE to __kvm_vcpu_run() synchronization comment

v19:
- Rework due to v6.14 debug flag changes
- Redo commit message

Quoted 6 lines
---
 arch/arm64/include/asm/kvm_host.h  |  2 ++
 arch/arm64/kvm/debug.c             |  4 ++++
 arch/arm64/kvm/hyp/nvhe/debug-sr.c | 32 ++++++++++++++++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/switch.c   |  2 +-
 4 files changed, 39 insertions(+), 1 deletion(-)

This one also needs reviewing from the KVM/arm64 side...

Will

reviewer Reviewed-by on v21 3/4
#8 lore raw

Re: [PATCH v21 3/4] KVM: arm64: nvhe: Disable branch generation in nVHE guests

In reply to Rob Herring (Arm)
Quoted 27 lines · Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

While BRBE can record branches within guests, the host recording branches in guests is not supported by perf (though events are). Support for BRBE in guests will supported by providing direct access to BRBE within the guests. That is how x86 LBR works for guests. Therefore, BRBE needs to be disabled on guest entry and restored on exit.

For nVHE, this requires explicit handling for guests. Before entering a guest, save the BRBE state and disable the it. When returning to the host, restore the state.

For VHE, it is not necessary. We initialize BRBCR_EL1.{E1BRE,E0BRE}=={0,0} at boot time, and HCR_EL2.TGE==1 while running in the host. We configure BRBCR_EL2.{E2BRE,E0HBRE} to enable branch recording in the host. When entering the guest, we set HCR_EL2.TGE==0 which means BRBCR_EL1 is used instead of BRBCR_EL2. Consequently for VHE, BRBE recording is disabled at EL1 and EL0 when running a guest.

Should recording in guests (by the host) ever be desired, the perf ABI will need to be extended to distinguish guest addresses (struct perf_branch_entry.priv) for starters. BRBE records would also need to be invalidated on guest entry/exit as guest/host EL1 and EL0 records can't be distinguished.

Quoted 5 lines
#9 lore raw

PATCH v21 4/4 perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)

In reply to Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

The ARMv9.2 architecture introduces the optional Branch Record Buffer Extension (BRBE), which records information about branches as they are executed into set of branch record registers. BRBE is similar to x86's Last Branch Record (LBR) and PowerPC's Branch History Rolling Buffer (BHRB).

BRBE supports filtering by exception level and can filter just the source or target address if excluded to avoid leaking privileged addresses. The h/w filter would be sufficient except when there are multiple events with disjoint filtering requirements. In this case, BRBE is configured with a union of all the events' desired branches, and then the recorded branches are filtered based on each event's filter. For example, with one event capturing kernel events and another event capturing user events, BRBE will be configured to capture both kernel and user branches. When handling event overflow, the branch records have to be filtered by software to only include kernel or user branch addresses for that event. In contrast, x86 simply configures LBR using the last installed event which seems broken.

It is possible on x86 to configure branch filter such that no branches are ever recorded (e.g. -j save_type). For BRBE, events with a configuration that will result in no samples are rejected.

Recording branches in KVM guests is not supported like x86. However, perf on x86 allows requesting branch recording in guests. The guest events are recorded, but the resulting branches are all from the host. For BRBE, events with branch recording and "exclude_host" set are rejected. Requiring "exclude_guest" to be set did not work. The default for the perf tool does set "exclude_guest" if no exception level options are specified. However, specifying kernel or user events defaults to including both host and guest. In this case, only host branches are recorded.

BRBE can support some additional exception branch types compared to x86. On x86, all exceptions other than syscalls are recorded as IRQ. With BRBE, it is possible to better categorize these exceptions. One limitation relative to x86 is we cannot distinguish a syscall return from other exception returns. So all exception returns are recorded as ERET type. The FIQ branch type is omitted as the only FIQ user is Apple platforms which don't support BRBE. The debug branch types are omitted as there is no clear need for them.

BRBE records are invalidated whenever events are reconfigured, a new task is scheduled in, or after recording is paused (and the records have been recorded for the event). The architecture allows branch records to be invalidated by the PE under implementation defined conditions. It is expected that these conditions are rare.

---
v21:
- update .sched_task() parameters. task_struct added in v6.15

v20:
- Fix sparse percpu warning in branch_records_alloc()
- Use pr_debug instead of pr_debug_once. Add debug print on all error cases when event.attr is rejected.
- Add back some arm64 specific exception types
- Convert 'call' from user to kernel to syscall as appropriate
- Drop requiring event and branch privileges to match
- Only enable exceptions and exception returns if recording kernel branches
- Add "branches" caps sysfs attribute like x86
- Drop some unused defines
- Use u64 instead of unsigned long for branch record fields
- use min() in brbinf_get_cycles()
- Reword comment about FZP and MDCR_EL2.HPMN interaction
- Add comments on assumptions about calling brbe_enable()
- Merge capture_brbe_flags() into perf_entry_from_brbe_regset()
- Rework BRBE invalidation to avoid invalidating in interrupt handler when no handled events capture the branch stack.
- Also clear BRBCR_ELx in brbe_disable(). This is for KVM nVHE checks if BRBE is enabled.

v19:
- Drop saving of branch records when task scheduled out. (Mark)
- Got rid of added armpmu ops. All BRBE support contained within pmuv3 code.
- Dropped armpmu.num_branch_records as reg_brbidr has same info.
- Make sched_task() callback actually get called. Enabling requires a call to perf_sched_cb_inc().
- Fix freeze on overflow for VHE
- The cycle counter doesn't freeze BRBE on overflow, so avoid assigning it when BRBE is enabled.
- Drop all the Arm specific exception branches. Not a clear need for them.
- Simplify enable/disable to avoid RMW and document ISBs needed
- Fix handling of branch 'cycles' reading. CC field is mantissa/exponent, not an integer.
- Save BRBFCR and BRBCR settings in event->hw.branch_reg.config and event->hw.extra_reg.config to avoid recalculating the register value each time the event is installed.
- Rework s/w filtering to better match h/w filtering
- Reject events with disjoint event filter and branch filter
- Reject events if exclude_host is set

v18: https://lore.kernel.org/all/[email protected]/

---
 drivers/perf/Kconfig         |  11 +
 drivers/perf/Makefile        |   1 +
 drivers/perf/arm_brbe.c      | 802 +++++++++++++++++++++++++++++++++++++++++++
 drivers/perf/arm_brbe.h      |  47 +++
 drivers/perf/arm_pmu.c       |  15 +-
 drivers/perf/arm_pmuv3.c     | 129 ++++++-
 include/linux/perf/arm_pmu.h |   8 +
 7 files changed, 1006 insertions(+), 7 deletions(-)
drivers/perf/Kconfig +11 -0
index 4e268de351c4..3be60ff4236d 100644
@@ -223,6 +223,17 @@ config ARM_SPE_PMU
 	  Extension, which provides periodic sampling of operations in
 	  the CPU pipeline and reports this via the perf AUX interface.
 
+config ARM64_BRBE
+	bool "Enable support for branch stack sampling using FEAT_BRBE"
+	depends on ARM_PMUV3 && ARM64
+	default y
+	help
+	  Enable perf support for Branch Record Buffer Extension (BRBE) which
+	  records all branches taken in an execution path. This supports some
+	  branch types and privilege based filtering. It captures additional
+	  relevant information such as cycle count, misprediction and branch
+	  type, branch privilege level etc.
+
 config ARM_DMC620_PMU
 	tristate "Enable PMU support for the ARM DMC-620 memory controller"
 	depends on (ARM64 && ACPI) || COMPILE_TEST
drivers/perf/Makefile +1 -0
index de71d2574857..192fc8b16204 100644
@@ -23,6 +23,7 @@ obj-$(CONFIG_STARFIVE_STARLINK_PMU) += starfive_starlink_pmu.o
 obj-$(CONFIG_THUNDERX2_PMU) += thunderx2_pmu.o
 obj-$(CONFIG_XGENE_PMU) += xgene_pmu.o
 obj-$(CONFIG_ARM_SPE_PMU) += arm_spe_pmu.o
+obj-$(CONFIG_ARM64_BRBE) += arm_brbe.o
 obj-$(CONFIG_ARM_DMC620_PMU) += arm_dmc620_pmu.o
 obj-$(CONFIG_MARVELL_CN10K_TAD_PMU) += marvell_cn10k_tad_pmu.o
 obj-$(CONFIG_MARVELL_CN10K_DDR_PMU) += marvell_cn10k_ddr_pmu.o
drivers/perf/arm_brbe.c new file +802 -0

802 lines not on this page · raw message

drivers/perf/arm_brbe.h new file +47 -0

47 lines not on this page · raw message

drivers/perf/arm_pmu.c +12 -3
index 2f33e69a8caf..df9867c0dc57 100644
@@ -99,7 +99,7 @@ static const struct pmu_irq_ops percpu_pmunmi_ops = {
 	.free_pmuirq = armpmu_free_percpu_pmunmi
 };
 
-static DEFINE_PER_CPU(struct arm_pmu *, cpu_armpmu);
+DEFINE_PER_CPU(struct arm_pmu *, cpu_armpmu);
 static DEFINE_PER_CPU(int, cpu_irq);
 static DEFINE_PER_CPU(const struct pmu_irq_ops *, cpu_irq_ops);
 
@@ -317,6 +317,11 @@ armpmu_del(struct perf_event *event, int flags)
 	struct hw_perf_event *hwc = &event->hw;
 	int idx = hwc->idx;
 
+	if (has_branch_stack(event)) {
+		hw_events->branch_users--;
+		perf_sched_cb_dec(event->pmu);
+	}
+
 	armpmu_stop(event, PERF_EF_UPDATE);
 	hw_events->events[idx] = NULL;
 	armpmu->clear_event_idx(hw_events, event);
@@ -345,6 +350,11 @@ armpmu_add(struct perf_event *event, int flags)
 	/* The newly-allocated counter should be empty */
 	WARN_ON_ONCE(hw_events->events[idx]);
 
+	if (has_branch_stack(event)) {
+		hw_events->branch_users++;
+		perf_sched_cb_inc(event->pmu);
+	}
+
 	event->hw.idx = idx;
 	hw_events->events[idx] = event;
 
@@ -509,8 +519,7 @@ static int armpmu_event_init(struct perf_event *event)
 		!cpumask_test_cpu(event->cpu, &armpmu->supported_cpus))
 		return -ENOENT;
 
-	/* does not support taken branch sampling */
-	if (has_branch_stack(event))
+	if (has_branch_stack(event) && !armpmu->reg_brbidr)
 		return -EOPNOTSUPP;
 
 	return __hw_perf_event_init(event);
drivers/perf/arm_pmuv3.c +125 -4

229 lines not on this page · raw message

include/linux/perf/arm_pmu.h +8 -0
index 6dc5e0cd76ca..93c9a26492fc 100644
@@ -70,6 +70,11 @@ struct pmu_hw_events {
 	struct arm_pmu		*percpu_pmu;
 
 	int irq;
+
+	struct perf_branch_stack	*branch_stack;
+
+	/* Active events requesting branch records */
+	unsigned int		branch_users;
 };
 
 enum armpmu_attr_groups {
@@ -115,6 +120,7 @@ struct arm_pmu {
 	/* PMUv3 only */
 	int		pmuver;
 	u64		reg_pmmir;
+	u64		reg_brbidr;
 #define ARMV8_PMUV3_MAX_COMMON_EVENTS		0x40
 	DECLARE_BITMAP(pmceid_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS);
 #define ARMV8_PMUV3_EXT_COMMON_EVENT_BASE	0x4000
@@ -126,6 +132,8 @@ struct arm_pmu {
 
 #define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu))
 
+DECLARE_PER_CPU(struct arm_pmu *, cpu_armpmu);
+
 u64 armpmu_event_update(struct perf_event *event);
 
 int armpmu_event_set_period(struct perf_event *event);
Signature · 1 line
2.47.2
maintainer
#10 lore raw

Re: [PATCH v21 4/4] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)

Hey Rob,

Quoted 7 lines · Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

The ARMv9.2 architecture introduces the optional Branch Record Buffer Extension (BRBE), which records information about branches as they are executed into set of branch record registers. BRBE is similar to x86's Last Branch Record (LBR) and PowerPC's Branch History Rolling Buffer (BHRB).

Since you picked this up from v19, the driver has changed considerably and I presume you will be continuing to extend it in future as the architecture progresses. Perhaps having you listed as Author (and crucially, in git blame :p) with Anshuman as a Co-developed-by: would be more appropriate?

Quoted 9 lines
---
 drivers/perf/Kconfig         |  11 +
 drivers/perf/Makefile        |   1 +
 drivers/perf/arm_brbe.c      | 802 +++++++++++++++++++++++++++++++++++++++++++
 drivers/perf/arm_brbe.h      |  47 +++
 drivers/perf/arm_pmu.c       |  15 +-
 drivers/perf/arm_pmuv3.c     | 129 ++++++-
 include/linux/perf/arm_pmu.h |   8 +
 7 files changed, 1006 insertions(+), 7 deletions(-)

Do you know if James Clark's tests [1] are going to be respun for the perf tool? It would be handy to have some way to test this new functionality.

Quoted 8 lines
drivers/perf/Kconfig +0 -0
index 4e268de351c4..3be60ff4236d 100644
@@ -223,6 +223,17 @@ config ARM_SPE_PMU
 	  Extension, which provides periodic sampling of operations in
 	  the CPU pipeline and reports this via the perf AUX interface.
 

drivers/perf/Kconfig config ARM_SPE_PMU · line 235

Quoted 10 lines
drivers/perf/Kconfig +10 -0
+config ARM64_BRBE
+	bool "Enable support for branch stack sampling using FEAT_BRBE"
+	depends on ARM_PMUV3 && ARM64
+	default y
+	help
+	  Enable perf support for Branch Record Buffer Extension (BRBE) which
+	  records all branches taken in an execution path. This supports some
+	  branch types and privilege based filtering. It captures additional
+	  relevant information such as cycle count, misprediction and branch
+	  type, branch privilege level etc.

It's a shame that this can't be modular, but I suppose the tight integration with the CPU PMU driver precludes that. Oh well.

drivers/perf/arm_brbe.c

Quoted 5 lines
drivers/perf/arm_brbe.c new file +0 -0
new file mode 100644
index 000000000000..2f254bd40af3

(The driver code looks fine to me but I'd like an Ack from Mark on the UAPI).

Quoted 14 lines
drivers/perf/arm_pmu.c +1 -1
index 2f33e69a8caf..df9867c0dc57 100644
@@ -99,7 +99,7 @@ static const struct pmu_irq_ops percpu_pmunmi_ops = {
 	.free_pmuirq = armpmu_free_percpu_pmunmi
 };
 
-static DEFINE_PER_CPU(struct arm_pmu *, cpu_armpmu);
+DEFINE_PER_CPU(struct arm_pmu *, cpu_armpmu);
 static DEFINE_PER_CPU(int, cpu_irq);
 static DEFINE_PER_CPU(const struct pmu_irq_ops *, cpu_irq_ops);
 

drivers/perf/arm_pmu.c armpmu_del(struct perf_event *event, int flags) · line 323

Quoted 7 lines
drivers/perf/arm_pmu.c +4 -0
@@ -317,6 +317,11 @@ armpmu_del(struct perf_event *event, int flags)
 	struct hw_perf_event *hwc = &event->hw;
 	int idx = hwc->idx;
 
+	if (has_branch_stack(event)) {
+		hw_events->branch_users--;
+		perf_sched_cb_dec(event->pmu);
+	}

Shouldn't we decrement this *after* calling armpmu_stop()?

Quoted 4 lines
drivers/perf/arm_pmu.c +1 -0
+
 	armpmu_stop(event, PERF_EF_UPDATE);
 	hw_events->events[idx] = NULL;
 	armpmu->clear_event_idx(hw_events, event);

[...]

Quoted 11 lines
drivers/perf/arm_pmu.c +11 -0
+static int branch_records_alloc(struct arm_pmu *armpmu)
+{
+	struct perf_branch_stack *branch_stack_cpu;
+	struct perf_branch_stack __percpu *branch_stack;
+	size_t size = struct_size(branch_stack_cpu, entries, brbe_num_branch_records(armpmu));
+	int cpu;
+
+	branch_stack = __alloc_percpu_gfp(size, __alignof__(*branch_stack_cpu),
+					  GFP_KERNEL);
+	if (!branch_stack)
+		return -ENOMEM;

drivers/perf/arm_pmu.c

Quoted 10 lines
drivers/perf/arm_pmu.c +9 -0
+
+	for_each_possible_cpu(cpu) {
+		struct pmu_hw_events *events_cpu;
+
+		events_cpu = per_cpu_ptr(armpmu->hw_events, cpu);
+		branch_stack_cpu = per_cpu_ptr(branch_stack, cpu);
+		events_cpu->branch_stack = branch_stack_cpu;
+	}
+	return 0;
 }

How does this work in a heterogeneous system? Shouldn't we at least scope the allocation to the CPUs associated with this PMU?

Quoted 19 lines

static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)

drivers/perf/arm_pmu.c +9 -1
@@ -1255,7 +1367,15 @@ static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
 	if (ret)
 		return ret;
 
-	return probe.present ? 0 : -ENODEV;
+	if (!probe.present)
+		return -ENODEV;
+
+	if (brbe_num_branch_records(cpu_pmu)) {
+		ret = branch_records_alloc(cpu_pmu);
+		if (ret)
+			return ret;
+	}
+	return 0;
 }
 
 static void armv8pmu_disable_user_access_ipi(void *unused)

drivers/perf/arm_pmu.c static int armv8_pmu_init(struct arm_pmu *cpu_pmu, char *name, · line 1437

Quoted 4 lines
drivers/perf/arm_pmu.c +1 -0
@@ -1314,6 +1434,7 @@ static int armv8_pmu_init(struct arm_pmu *cpu_pmu, char *name,
 	cpu_pmu->set_event_filter	= armv8pmu_set_event_filter;
 
 	cpu_pmu->pmu.event_idx		= armv8pmu_user_event_idx;
+	cpu_pmu->pmu.sched_task		= armv8pmu_sched_task;

Can we avoid assigning this unless BRBE actually probed?

Will

[1] https://gitlab.arm.com/linux-arm/linux-jc/-/commit/3a7ddce70c2daadb63fcc511de0a89055ca48b32

#11 lore raw

Re: [PATCH v21 4/4] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)

Quoted 11 lines · Will Deacon

Hey Rob,

Quoted 7 lines · Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

The ARMv9.2 architecture introduces the optional Branch Record Buffer Extension (BRBE), which records information about branches as they are executed into set of branch record registers. BRBE is similar to x86's Last Branch Record (LBR) and PowerPC's Branch History Rolling Buffer (BHRB).

Quoted 5 lines

Since you picked this up from v19, the driver has changed considerably and I presume you will be continuing to extend it in future as the architecture progresses. Perhaps having you listed as Author (and crucially, in git blame :p) with Anshuman as a Co-developed-by: would be more appropriate?

Shrug.

Quoted 10 lines
Quoted 9 lines
---
 drivers/perf/Kconfig         |  11 +
 drivers/perf/Makefile        |   1 +
 drivers/perf/arm_brbe.c      | 802 +++++++++++++++++++++++++++++++++++++++++++
 drivers/perf/arm_brbe.h      |  47 +++
 drivers/perf/arm_pmu.c       |  15 +-
 drivers/perf/arm_pmuv3.c     | 129 ++++++-
 include/linux/perf/arm_pmu.h |   8 +
 7 files changed, 1006 insertions(+), 7 deletions(-)
Quoted 3 lines

Do you know if James Clark's tests [1] are going to be respun for the perf tool? It would be handy to have some way to test this new functionality.

Yes. I dropped them here because I've been told by Arnaldo in the past to send userspace stuff separately.

Quoted 48 lines
Quoted 8 lines
drivers/perf/Kconfig +0 -0
index 4e268de351c4..3be60ff4236d 100644
@@ -223,6 +223,17 @@ config ARM_SPE_PMU
 	  Extension, which provides periodic sampling of operations in
 	  the CPU pipeline and reports this via the perf AUX interface.
 

drivers/perf/Kconfig config ARM_SPE_PMU · line 235

Quoted 10 lines
drivers/perf/Kconfig +10 -0
+config ARM64_BRBE
+	bool "Enable support for branch stack sampling using FEAT_BRBE"
+	depends on ARM_PMUV3 && ARM64
+	default y
+	help
+	  Enable perf support for Branch Record Buffer Extension (BRBE) which
+	  records all branches taken in an execution path. This supports some
+	  branch types and privilege based filtering. It captures additional
+	  relevant information such as cycle count, misprediction and branch
+	  type, branch privilege level etc.

It's a shame that this can't be modular, but I suppose the tight integration with the CPU PMU driver precludes that. Oh well.

drivers/perf/arm_brbe.c

Quoted 5 lines
drivers/perf/arm_brbe.c new file +0 -0
new file mode 100644
index 000000000000..2f254bd40af3

(The driver code looks fine to me but I'd like an Ack from Mark on the UAPI).

Quoted 17 lines
drivers/perf/arm_pmu.c +1 -1
index 2f33e69a8caf..df9867c0dc57 100644
@@ -99,7 +99,7 @@ static const struct pmu_irq_ops percpu_pmunmi_ops = {
 	.free_pmuirq = armpmu_free_percpu_pmunmi
 };
 
-static DEFINE_PER_CPU(struct arm_pmu *, cpu_armpmu);
+DEFINE_PER_CPU(struct arm_pmu *, cpu_armpmu);
 static DEFINE_PER_CPU(int, cpu_irq);
 static DEFINE_PER_CPU(const struct pmu_irq_ops *, cpu_irq_ops);
 
@@ -317,6 +317,11 @@ armpmu_del(struct perf_event *event, int flags)
 	struct hw_perf_event *hwc = &event->hw;
 	int idx = hwc->idx;
 
Quoted 6 lines

drivers/perf/arm_pmu.c armpmu_del(struct perf_event *event, int flags) · line 323

Quoted 4 lines
drivers/perf/arm_pmu.c +4 -0
+	if (has_branch_stack(event)) {
+		hw_events->branch_users--;
+		perf_sched_cb_dec(event->pmu);
+	}

Shouldn't we decrement this *after* calling armpmu_stop()?

Logically, that would make more sense. It's all serialized by the perf core though, so we can't get .sched_task() during this.

Quoted 23 lines
Quoted 4 lines
drivers/perf/arm_pmu.c +1 -0
+
 	armpmu_stop(event, PERF_EF_UPDATE);
 	hw_events->events[idx] = NULL;
 	armpmu->clear_event_idx(hw_events, event);

[...]

Quoted 16 lines
drivers/perf/arm_pmu.c +16 -0
+static int branch_records_alloc(struct arm_pmu *armpmu)
+{
+	struct perf_branch_stack *branch_stack_cpu;
+	struct perf_branch_stack __percpu *branch_stack;
+	size_t size = struct_size(branch_stack_cpu, entries, brbe_num_branch_records(armpmu));
+	int cpu;
+
+	branch_stack = __alloc_percpu_gfp(size, __alignof__(*branch_stack_cpu),
+					  GFP_KERNEL);
+	if (!branch_stack)
+		return -ENOMEM;
+
+	for_each_possible_cpu(cpu) {
+		struct pmu_hw_events *events_cpu;
+
+		events_cpu = per_cpu_ptr(armpmu->hw_events, cpu);
Quoted 8 lines

drivers/perf/arm_pmu.c

Quoted 5 lines
drivers/perf/arm_pmu.c +4 -0
+		branch_stack_cpu = per_cpu_ptr(branch_stack, cpu);
+		events_cpu->branch_stack = branch_stack_cpu;
+	}
+	return 0;
 }

How does this work in a heterogeneous system? Shouldn't we at least scope the allocation to the CPUs associated with this PMU?

Leaks memory, that's how.

As a bonus, it could overrun some memory too if the record sizes are different though mostly overrunning into other BRBE buffers...

I think we just need to loop over cpu_pmu->supported_cpus and call kmalloc(). Since events_cpu is already percpu, we don't need a percpu allocation of the branch stack buffers.

I'm assuming it is safe to assume all CPUs either have BRBE or they don't? Different record sizes at least should work fine (other than the above issue).

BTW, I was scratching my head how armpmu_alloc() works with for_each_possible_cpu(), but I guess the hotplug callbacks overwrite the events->percpu_pmu value. I think we could just remove the loop there. I'll investigate that.

Rob

#12 lore raw

Re: [PATCH v21 4/4] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)

Quoted 13 lines · Rob Herring
Quoted 11 lines · Will Deacon

Hey Rob,

Quoted 7 lines · Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

The ARMv9.2 architecture introduces the optional Branch Record Buffer Extension (BRBE), which records information about branches as they are executed into set of branch record registers. BRBE is similar to x86's Last Branch Record (LBR) and PowerPC's Branch History Rolling Buffer (BHRB).

Quoted 7 lines
Quoted 5 lines

Since you picked this up from v19, the driver has changed considerably and I presume you will be continuing to extend it in future as the architecture progresses. Perhaps having you listed as Author (and crucially, in git blame :p) with Anshuman as a Co-developed-by: would be more appropriate?

Shrug.

I did a comparison to v18 arm_brbe.c. It was 1134 lines. I (and Mark) have removed 332 lines and changed 369 lines. So a little more than half remains, but the unchanged parts are a lot of 1 line helper functions, brackets and whitespace. So given the more complex parts have changed, I will change the authorship.

Rob

#13 lore raw

Re: [PATCH v21 4/4] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)

In reply to Rob Herring
Quoted 30 lines · Rob Herring
Quoted 11 lines · Will Deacon

Hey Rob,

Quoted 7 lines · Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

The ARMv9.2 architecture introduces the optional Branch Record Buffer Extension (BRBE), which records information about branches as they are executed into set of branch record registers. BRBE is similar to x86's Last Branch Record (LBR) and PowerPC's Branch History Rolling Buffer (BHRB).

Quoted 5 lines

Since you picked this up from v19, the driver has changed considerably and I presume you will be continuing to extend it in future as the architecture progresses. Perhaps having you listed as Author (and crucially, in git blame :p) with Anshuman as a Co-developed-by: would be more appropriate?

Shrug.

Quoted 10 lines
Quoted 9 lines
---
  drivers/perf/Kconfig         |  11 +
  drivers/perf/Makefile        |   1 +
  drivers/perf/arm_brbe.c      | 802 +++++++++++++++++++++++++++++++++++++++++++
  drivers/perf/arm_brbe.h      |  47 +++
  drivers/perf/arm_pmu.c       |  15 +-
  drivers/perf/arm_pmuv3.c     | 129 ++++++-
  include/linux/perf/arm_pmu.h |   8 +
  7 files changed, 1006 insertions(+), 7 deletions(-)
Quoted 7 lines
Quoted 3 lines

Do you know if James Clark's tests [1] are going to be respun for the perf tool? It would be handy to have some way to test this new functionality.

Yes. I dropped them here because I've been told by Arnaldo in the past to send userspace stuff separately.

That version of the test was out of date so I've pushed the new version here: https://git.linaro.org/plugins/gitiles/people/james.clark/linux.git/+/16e4a18c2d5fc53736f05c9052b1d11d74909707

But I'll wait for the driver changes to be finalised before posting it. Or Rob can take it back into the patchset.

maintainer
#14 lore raw

Re: [PATCH v21 4/4] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)

Quoted 41 lines · James Clark
Quoted 30 lines · Rob Herring
Quoted 11 lines · Will Deacon

Hey Rob,

Quoted 7 lines · Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

The ARMv9.2 architecture introduces the optional Branch Record Buffer Extension (BRBE), which records information about branches as they are executed into set of branch record registers. BRBE is similar to x86's Last Branch Record (LBR) and PowerPC's Branch History Rolling Buffer (BHRB).

Quoted 5 lines

Since you picked this up from v19, the driver has changed considerably and I presume you will be continuing to extend it in future as the architecture progresses. Perhaps having you listed as Author (and crucially, in git blame :p) with Anshuman as a Co-developed-by: would be more appropriate?

Shrug.

Quoted 10 lines
Quoted 9 lines
---
  drivers/perf/Kconfig         |  11 +
  drivers/perf/Makefile        |   1 +
  drivers/perf/arm_brbe.c      | 802 +++++++++++++++++++++++++++++++++++++++++++
  drivers/perf/arm_brbe.h      |  47 +++
  drivers/perf/arm_pmu.c       |  15 +-
  drivers/perf/arm_pmuv3.c     | 129 ++++++-
  include/linux/perf/arm_pmu.h |   8 +
  7 files changed, 1006 insertions(+), 7 deletions(-)
Quoted 7 lines
Quoted 3 lines

Do you know if James Clark's tests [1] are going to be respun for the perf tool? It would be handy to have some way to test this new functionality.

Yes. I dropped them here because I've been told by Arnaldo in the past to send userspace stuff separately.

Quoted 5 lines

That version of the test was out of date so I've pushed the new version
here: https://git.linaro.org/plugins/gitiles/people/james.clark/linux.git/+/16e4a18c2d5fc53736f05c9052b1d11d74909707

But I'll wait for the driver changes to be finalised before posting it. Or Rob can take it back into the patchset.

Thanks, James. I just wanted to make sure that there was still a plan for upstreaming these separately from the driver. Have you had a chance to run them against this v21?

Will

#15 lore raw

Re: [PATCH v21 4/4] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)

Quoted 45 lines · Will Deacon
Quoted 44 lines · James Clark
Quoted 30 lines · Rob Herring
Quoted 11 lines · Will Deacon

Hey Rob,

Quoted 7 lines · Rob Herring (Arm)

From: Anshuman Khandual <[email protected]>

The ARMv9.2 architecture introduces the optional Branch Record Buffer Extension (BRBE), which records information about branches as they are executed into set of branch record registers. BRBE is similar to x86's Last Branch Record (LBR) and PowerPC's Branch History Rolling Buffer (BHRB).

Quoted 5 lines

Since you picked this up from v19, the driver has changed considerably and I presume you will be continuing to extend it in future as the architecture progresses. Perhaps having you listed as Author (and crucially, in git blame :p) with Anshuman as a Co-developed-by: would be more appropriate?

Shrug.

Quoted 10 lines
Quoted 9 lines
---
   drivers/perf/Kconfig         |  11 +
   drivers/perf/Makefile        |   1 +
   drivers/perf/arm_brbe.c      | 802 +++++++++++++++++++++++++++++++++++++++++++
   drivers/perf/arm_brbe.h      |  47 +++
   drivers/perf/arm_pmu.c       |  15 +-
   drivers/perf/arm_pmuv3.c     | 129 ++++++-
   include/linux/perf/arm_pmu.h |   8 +
   7 files changed, 1006 insertions(+), 7 deletions(-)
Quoted 7 lines
Quoted 3 lines

Do you know if James Clark's tests [1] are going to be respun for the perf tool? It would be handy to have some way to test this new functionality.

Yes. I dropped them here because I've been told by Arnaldo in the past to send userspace stuff separately.

That version of the test was out of date so I've pushed the new version
here: https://git.linaro.org/plugins/gitiles/people/james.clark/linux.git/+/16e4a18c2d5fc53736f05c9052b1d11d74909707

Quoted 8 lines
Quoted 2 lines

But I'll wait for the driver changes to be finalised before posting it. Or Rob can take it back into the patchset.

Thanks, James. I just wanted to make sure that there was still a plan for upstreaming these separately from the driver. Have you had a chance to run them against this v21?

Will

Yes I'll send it once the driver is merged. And yes they're passing against v21, but I'll retest v22 as well.

James

#16 lore raw
In reply to Rob Herring (Arm)
Quoted 4 lines · On Mon, 07 Apr 2025 12:41:29 -0500 "Rob Herring (Arm)" <[email protected]> wrote:

This series enables perf branch stack sampling support on arm64 via a v9.2 arch feature called Branch Record Buffer Extension (BRBE). Details on BRBE can be found in the Arm ARM[1] chapter D18.

Quoted 8 lines

I've picked up this series from Anshuman. v19 and v20 versions have been reworked quite a bit by Mark and myself. The bulk of those changes are in patch 4.

A git branch is here[2].

[1] https://developer.arm.com/documentation/ddi0487/latest/
[2] git://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git arm/brbe-v21

Hi Rob,

Expecting any changes on this? Anything specific we can do to help then shout.

We need to pull in the reworked version into openeuler that is carrying older code and would rather not do it more than once.

Jonathan

Quoted 80 lines

v21:
- Drop clean-up patches 1-7 already applied
- Rebase on v6.15-rc1

v20:
- https://lore.kernel.org/r/[email protected]
- Added back some of the arm64 specific exception types. The x86 IRQ branches also include other exceptions like page faults. On arm64, we can distinguish the exception types, so we do. Also, to better align with x86, we convert 'call' branches which are user to kernel to 'syscall'.
- Only enable exceptions and exception returns if recording kernel branches (matching x86)
- Drop requiring event and branch privileges to match
- Add "branches" caps sysfs attribute like x86
- Reword comment about FZP and MDCR_EL2.HPMN interaction
- Rework BRBE invalidation to avoid invalidating in interrupt handler when no handled events capture the branch stack (i.e. when there are multiple users).
- Also clear BRBCR_ELx bits in brbe_disable(). This is for KVM nVHE checks if BRBE is enabled.
- Document that MDCR_EL3.SBRBE can be 0b01 also

v19:
- https://lore.kernel.org/all/[email protected]/
- Drop saving of branch records when task scheduled out (Mark). Make sched_task() callback actually get called. Enabling requires a call to perf_sched_cb_inc(). So the saving of branch records never happened.
- Got rid of added armpmu ops. All BRBE support is contained within pmuv3 code.
- Fix freeze on overflow for VHE
- The cycle counter doesn't freeze BRBE on overflow, so avoid assigning it when BRBE is enabled.
- Drop all the Arm specific exception branches. Not a clear need for them.
- Fix handling of branch 'cycles' reading. CC field is mantissa/exponent, not an integer.
- Rework s/w filtering to better match h/w filtering
- Reject events with disjoint event filter and branch filter or with exclude_host set
- Dropped perf test patch which has been applied for 6.14
- Dropped patch "KVM: arm64: Explicitly handle BRBE traps as UNDEFINED" which has been applied for 6.14

v18:
- https://lore.kernel.org/all/[email protected]/

For v1-v17, see the above link. Not going to duplicate it all here...

---
Anshuman Khandual (4):
arm64/sysreg: Add BRBE registers and fields
arm64: Handle BRBE booting requirements
KVM: arm64: nvhe: Disable branch generation in nVHE guests
perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)

 Documentation/arch/arm64/booting.rst |  21 +
 arch/arm64/include/asm/el2_setup.h   |  86 +++-
 arch/arm64/include/asm/kvm_host.h    |   2 +
 arch/arm64/include/asm/sysreg.h      |  17 +-
 arch/arm64/kvm/debug.c               |   4 +
 arch/arm64/kvm/hyp/nvhe/debug-sr.c   |  32 ++
 arch/arm64/kvm/hyp/nvhe/switch.c     |   2 +-
 arch/arm64/tools/sysreg              | 132 ++++++
 drivers/perf/Kconfig                 |  11 +
 drivers/perf/Makefile                |   1 +
 drivers/perf/arm_brbe.c              | 802 +++++++++++++++++++++++++++++++++++
 drivers/perf/arm_brbe.h              |  47 ++
 drivers/perf/arm_pmu.c               |  15 +-
 drivers/perf/arm_pmuv3.c             | 129 +++++-
 include/linux/perf/arm_pmu.h         |   8 +
 15 files changed, 1287 insertions(+), 22 deletions(-)

---
base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8
change-id: 20250129-arm-brbe-v19-24d5d9e5e623

Best regards,

#17 lore raw
Quoted 12 lines · Jonathan Cameron
Quoted 8 lines · On Mon, 07 Apr 2025 12:41:29 -0500 "Rob Herring (Arm)" <[email protected]> wrote:

This series enables perf branch stack sampling support on arm64 via a v9.2 arch feature called Branch Record Buffer Extension (BRBE). Details on BRBE can be found in the Arm ARM[1] chapter D18.

I've picked up this series from Anshuman. v19 and v20 versions have been reworked quite a bit by Mark and myself. The bulk of those changes are in patch 4.

Quoted 8 lines
Quoted 4 lines

A git branch is here[2].

[1] https://developer.arm.com/documentation/ddi0487/latest/
[2] git://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git arm/brbe-v21

Hi Rob,

Expecting any changes on this? Anything specific we can do to help then shout.

I hope not. Still waiting on Mark R's review though... I'm sure there will be something and since perf drivers seemingly only get reviewed once a cycle, I guess it will be another cycle... Sigh.

Rob