arm64/perf: Enable branch stack sampling
Subsystems (4)
v20 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 11.
Patches 1-7 are new clean-ups/prep which stand on their own. They were previously posted here[2]. Please pick them up if there's no issues with them.
Patches 8-11 add BRBE support with the actual support in patch 11.
A git branch is here[3].
[1] https://developer.arm.com/documentation/ddi0487/latest/
[2] https://lore.kernel.org/all/[email protected]/
[3] git://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git arm/brbe-v20
v20:
- 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...
---
Changes in v20:
- EDITME: describe what is new in this series revision.
- EDITME: use bulletpoints and terse descriptions.
- Link to v19: https://lore.kernel.org/r/[email protected]
---
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)
Mark Rutland (3):
perf: arm_pmu: Don't disable counter in armpmu_add()
perf: arm_pmuv3: Don't disable counter in armv8pmu_enable_event()
perf: arm_pmu: Move PMUv3-specific data
Rob Herring (Arm) (4):
perf: arm_pmuv3: Call kvm_vcpu_pmu_resync_el0() before enabling counters
perf: arm_v7_pmu: Drop obvious comments for enabling/disabling counters and interrupts
perf: arm_v7_pmu: Don't disable counter in (armv7|krait_|scorpion_)pmu_enable_event()
perf: apple_m1: Don't disable counter in m1_pmu_enable_event()
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/apple_m1_cpu_pmu.c | 4 - drivers/perf/arm_brbe.c | 802 +++++++++++++++++++++++++++++++++++ drivers/perf/arm_brbe.h | 47 ++ drivers/perf/arm_pmu.c | 23 +- drivers/perf/arm_pmuv3.c | 135 +++++- drivers/perf/arm_v7_pmu.c | 50 --- include/linux/perf/arm_pmu.h | 21 +- 17 files changed, 1297 insertions(+), 93 deletions(-)
---
base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b
change-id: 20250129-arm-brbe-v19-24d5d9e5e623
Best regards,
Signature · 1 line
Rob Herring (Arm) <[email protected]>
PATCH v20 1/11 perf: arm_pmuv3: Call kvm_vcpu_pmu_resync_el0() before enabling counters
Counting events related to setup of the PMU is not desired, but kvm_vcpu_pmu_resync_el0() is called just after the PMU counters have been enabled. Move the call to before enabling the counters.
--- drivers/perf/arm_pmuv3.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/perf/arm_pmuv3.c +2 -2
@@ -825,10 +825,10 @@ static void armv8pmu_start(struct arm_pmu *cpu_pmu)else armv8pmu_disable_user_access (); +kvm_vcpu_pmu_resync_el0 (); +/* Enable all counters */ armv8pmu_pmcr_write (armv8pmu_pmcr_read () |ARMV8_PMU_PMCR_E ); - -kvm_vcpu_pmu_resync_el0 (); }static void armv8pmu_stop (struct arm_pmu *cpu_pmu)
Signature · 1 line
2.47.2
PATCH v20 2/11 perf: arm_pmu: Don't disable counter in armpmu_add()
In reply to Rob Herring (Arm)From: Mark Rutland <[email protected]>
Currently armpmu_add() tries to handle a newly-allocated counter having a stale associated event, but this should not be possible, and if this were to happen the current mitigation is insufficient and potentially expensive. It would be better to warn if we encounter the impossible case.
Calls to pmu::add() and pmu::del() are serialized by the core perf code, and armpmu_del() clears the relevant slot in pmu_hw_events::events[] before clearing the bit in pmu_hw_events::used_mask such that the counter can be reallocated. Thus when armpmu_add() allocates a counter index from pmu_hw_events::used_mask, it should not be possible to observe a stale even in pmu_hw_events::events[] unless either pmu_hw_events::used_mask or pmu_hw_events::events[] have been corrupted.
If this were to happen, we'd end up with two events with the same event->hw.idx, which would clash with each other during reprogramming, deletion, etc, and produce bogus results. Add a WARN_ON_ONCE() for this case so that we can detect if this ever occurs in practice.
That possiblity aside, there's no need to call arm_pmu::disable(event) for the new event. The PMU reset code initialises the counter in a disabled state, and armpmu_del() will disable the counter before it can be reused. Remove the redundant disable.
--- drivers/perf/arm_pmu.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/perf/arm_pmu.c +3 -5
@@ -342,12 +342,10 @@ armpmu_add(struct perf_event *event, int flags)if (idx <0 )return idx; -/* -* If there is an event in the counter we are going to use then make -* sure it is disabled. -*/ +/* The newly-allocated counter should be empty */ +WARN_ON_ONCE (hw_events->events[idx]); + event->hw.idx = idx; - armpmu->disable (event); hw_events->events[idx] = event; hwc->state =PERF_HES_STOPPED |PERF_HES_UPTODATE ;
Signature · 1 line
2.47.2
PATCH v20 3/11 perf: arm_pmuv3: Don't disable counter in armv8pmu_enable_event()
In reply to Rob Herring (Arm)From: Mark Rutland <[email protected]>
Currently armv8pmu_enable_event() starts by disabling the event counter it has been asked to enable. This should not be necessary as the counter (and the PMU as a whole) should not be active when armv8pmu_enable_event() is called.
Remove the redundant call to armv8pmu_disable_event_counter(). At the same time, remove the comment immeditately above as everything it says is obvious from the function names below.
--- drivers/perf/arm_pmuv3.c | 5 ----- 1 file changed, 5 deletions(-)
drivers/perf/arm_pmuv3.c +0 -5
@@ -795,11 +795,6 @@ static void armv8pmu_enable_user_access(struct arm_pmu *cpu_pmu)static void armv8pmu_enable_event (struct perf_event *event) { -/* -* Enable counter and interrupt, and set the counter to count -* the event that we're interested in. -*/ -armv8pmu_disable_event_counter (event);armv8pmu_write_event_type (event);armv8pmu_enable_event_irq (event);armv8pmu_enable_event_counter (event);
Signature · 1 line
2.47.2
PATCH v20 4/11 perf: arm_v7_pmu: Drop obvious comments for enabling/disabling counters and interrupts
In reply to Rob Herring (Arm)The function calls for enabling/disabling counters and interrupts are pretty obvious as to what they are doing, and the comments don't add any additional value.
--- drivers/perf/arm_v7_pmu.c | 44 -------------------------------------------- 1 file changed, 44 deletions(-)
drivers/perf/arm_v7_pmu.c +0 -44
@@ -857,14 +857,6 @@ static void armv7pmu_enable_event(struct perf_event *event)return ; } -/* -* Enable counter and interrupt, and set the counter to count -* the event that we're interested in. -*/ - -/* -* Disable counter -*/ armv7_pmnc_disable_counter (idx);/*
@@ -875,14 +867,7 @@ static void armv7pmu_enable_event(struct perf_event *event)if (cpu_pmu->set_event_filter || idx !=ARMV7_IDX_CYCLE_COUNTER )armv7_pmnc_write_evtsel (idx, hwc->config_base); -/* -* Enable interrupt for this counter -*/ armv7_pmnc_enable_intens (idx); - -/* -* Enable counter -*/ armv7_pmnc_enable_counter (idx); }
@@ -898,18 +883,7 @@ static void armv7pmu_disable_event(struct perf_event *event)return ; } -/* -* Disable counter and interrupt -*/ - -/* -* Disable counter -*/ armv7_pmnc_disable_counter (idx); - -/* -* Disable interrupt for this counter -*/ armv7_pmnc_disable_intens (idx); }
@@ -1476,12 +1450,6 @@ static void krait_pmu_enable_event(struct perf_event *event)struct hw_perf_event *hwc = &event->hw;int idx = hwc->idx; -/* -* Enable counter and interrupt, and set the counter to count -* the event that we're interested in. -*/ - -/* Disable counter */ armv7_pmnc_disable_counter (idx);/*
@@ -1494,10 +1462,7 @@ static void krait_pmu_enable_event(struct perf_event *event)else armv7_pmnc_write_evtsel (idx, hwc->config_base); -/* Enable interrupt for this counter */ armv7_pmnc_enable_intens (idx); - -/* Enable counter */ armv7_pmnc_enable_counter (idx); }
@@ -1797,12 +1762,6 @@ static void scorpion_pmu_enable_event(struct perf_event *event)struct hw_perf_event *hwc = &event->hw;int idx = hwc->idx; -/* -* Enable counter and interrupt, and set the counter to count -* the event that we're interested in. -*/ - -/* Disable counter */ armv7_pmnc_disable_counter (idx);/*
@@ -1815,10 +1774,7 @@ static void scorpion_pmu_enable_event(struct perf_event *event)else if (idx !=ARMV7_IDX_CYCLE_COUNTER )armv7_pmnc_write_evtsel (idx, hwc->config_base); -/* Enable interrupt for this counter */ armv7_pmnc_enable_intens (idx); - -/* Enable counter */ armv7_pmnc_enable_counter (idx); }
Signature · 1 line
2.47.2
PATCH v20 5/11 perf: arm_v7_pmu: Don't disable counter in (armv7|krait_|scorpion_)pmu_enable_event()
In reply to Rob Herring (Arm)Currently (armv7|krait_|scorpion_)pmu_enable_event() start by disabling the event counter it has been asked to enable. This should not be necessary as the counter (and the PMU as a whole) should not be active when *_enable_event() is called.
--- drivers/perf/arm_v7_pmu.c | 6 ------ 1 file changed, 6 deletions(-)
drivers/perf/arm_v7_pmu.c +0 -6
@@ -857,8 +857,6 @@ static void armv7pmu_enable_event(struct perf_event *event)return ; } -armv7_pmnc_disable_counter (idx); -/* * Set event (if destined for PMNx counters) * We only need to set the event for the cycle counter if we
@@ -1450,8 +1448,6 @@ static void krait_pmu_enable_event(struct perf_event *event)struct hw_perf_event *hwc = &event->hw;int idx = hwc->idx; -armv7_pmnc_disable_counter (idx); -/* * Set event (if destined for PMNx counters) * We set the event for the cycle counter because we
@@ -1762,8 +1758,6 @@ static void scorpion_pmu_enable_event(struct perf_event *event)struct hw_perf_event *hwc = &event->hw;int idx = hwc->idx; -armv7_pmnc_disable_counter (idx); -/* * Set event (if destined for PMNx counters) * We don't set the event for the cycle counter because we
Signature · 1 line
2.47.2
PATCH v20 6/11 perf: apple_m1: Don't disable counter in m1_pmu_enable_event()
In reply to Rob Herring (Arm)Currently m1_pmu_enable_event() starts by disabling the event counter it has been asked to enable. This should not be necessary as the counter (and the PMU as a whole) should not be active when m1_pmu_enable_event() is called.
--- drivers/perf/apple_m1_cpu_pmu.c | 4 ---- 1 file changed, 4 deletions(-)
drivers/perf/apple_m1_cpu_pmu.c +0 -4
@@ -396,10 +396,6 @@ static void m1_pmu_enable_event(struct perf_event *event) user = event->hw.config_base &M1_PMU_CFG_COUNT_USER ; kernel = event->hw.config_base &M1_PMU_CFG_COUNT_KERNEL ; -m1_pmu_disable_counter_interrupt (event->hw.idx); -m1_pmu_disable_counter (event->hw.idx); -isb (); -m1_pmu_configure_counter (event->hw.idx, evt, user, kernel);m1_pmu_enable_counter (event->hw.idx);m1_pmu_enable_counter_interrupt (event->hw.idx);
Signature · 1 line
2.47.2
PATCH v20 7/11 perf: arm_pmu: Move PMUv3-specific data
In reply to Rob Herring (Arm)From: Mark Rutland <[email protected]>
A few fields in struct arm_pmu are only used with PMUv3, and soon we will need to add more for BRBE. Group the fields together so that we have a logical place to add more data in future.
At the same time, remove the comment for reg_pmmir as it doesn't convey anything useful.
There should be no functional change as a result of this patch.
--- include/linux/perf/arm_pmu.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
include/linux/perf/arm_pmu.h +7 -6
@@ -84,7 +84,6 @@ struct arm_pmu {struct pmu pmu;cpumask_t supported_cpus;char *name; -int pmuver;irqreturn_t (*handle_irq)(struct arm_pmu *pmu);void (*enable)(struct perf_event *event);void (*disable)(struct perf_event *event);
@@ -102,18 +101,20 @@ struct arm_pmu {int (*map_event)(struct perf_event *event);DECLARE_BITMAP (cntr_mask,ARMPMU_MAX_HWEVENTS );bool secure_access;/* 32-bit ARM only */ -#define ARMV8_PMUV3_MAX_COMMON_EVENTS 0x40 -DECLARE_BITMAP (pmceid_bitmap,ARMV8_PMUV3_MAX_COMMON_EVENTS ); -#define ARMV8_PMUV3_EXT_COMMON_EVENT_BASE 0x4000 -DECLARE_BITMAP (pmceid_ext_bitmap,ARMV8_PMUV3_MAX_COMMON_EVENTS );struct platform_device *plat_device;struct pmu_hw_events __percpu *hw_events;struct hlist_node node;struct notifier_block cpu_pm_nb;/* the attr_groups array must be NULL-terminated */ const struct attribute_group *attr_groups[ARMPMU_NR_ATTR_GROUPS +1 ]; -/* store the PMMIR_EL1 to expose slots */ + +/* PMUv3 only */ +int pmuver;u64 reg_pmmir; +#define ARMV8_PMUV3_MAX_COMMON_EVENTS 0x40 +DECLARE_BITMAP (pmceid_bitmap,ARMV8_PMUV3_MAX_COMMON_EVENTS ); +#define ARMV8_PMUV3_EXT_COMMON_EVENT_BASE 0x4000 +DECLARE_BITMAP (pmceid_ext_bitmap,ARMV8_PMUV3_MAX_COMMON_EVENTS );/* Only to be used by ACPI probing code */ unsigned long acpi_cpuid;
Signature · 1 line
2.47.2
PATCH v20 8/11 arm64/sysreg: Add BRBE registers and fields
In reply to 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.
---
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
@@ -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)
@@ -610,7 +600,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)
@@ -821,6 +810,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
PATCH v20 9/11 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.
---
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
@@ -352,6 +352,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
PATCH v20 10/11 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
@@ -619,6 +619,7 @@ struct kvm_host_data {#define KVM_HOST_DATA_FLAG_HOST_SME_ENABLED 3#define KVM_HOST_DATA_FLAG_TRBE_ENABLED 4#define KVM_HOST_DATA_FLAG_EL1_TRACING_CONFIGURED 5 +#define KVM_HOST_DATA_FLAG_HAS_BRBE 6unsigned long flags;struct kvm_cpu_context host_ctxt;
@@ -662,6 +663,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
@@ -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
@@ -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
@@ -318,7 +318,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
Re: [PATCH v20 10/11] KVM: arm64: nvhe: Disable branch generation in nVHE guests
Quoted 28 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_EL1is used instead ofBRBCR_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 4 lines
Signed-off-by Anshuman Khandual <[email protected]>Signed-off-by Mark Rutland <[email protected]>Co-developed-by Rob Herring (Arm) <[email protected]>Signed-off-by Rob Herring (Arm) <[email protected]>
PATCH v20 11/11 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.
---
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 | 128 ++++++- include/linux/perf/arm_pmu.h | 8 + 7 files changed, 1005 insertions(+), 7 deletions(-)
drivers/perf/Kconfig +11 -0
@@ -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
@@ -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
@@ -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 +124 -4
228 lines not on this page · raw message
include/linux/perf/arm_pmu.h +8 -0
@@ -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 {
@@ -111,6 +116,7 @@ struct arm_pmu {/* PMUv3 only */ int pmuver;u64 reg_pmmir; +u64 reg_brbidr;#define ARMV8_PMUV3_MAX_COMMON_EVENTS 0x40DECLARE_BITMAP (pmceid_bitmap,ARMV8_PMUV3_MAX_COMMON_EVENTS );#define ARMV8_PMUV3_EXT_COMMON_EVENT_BASE 0x4000
@@ -122,6 +128,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
Re: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
Quoted 2 lines · Rob Herring (Arm)
From: Anshuman Khandual <[email protected]>
[...]
Quoted 5 lines
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.
[...]
drivers/perf/arm_pmuv3.c static int armv8pmu_user_event_idx(struct perf_event *event) · line 1064
Quoted 11 lines
drivers/perf/arm_pmuv3.c +11
-0+static void armv8pmu_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) +{ +struct arm_pmu *armpmu = *this_cpu_ptr (&cpu_armpmu); +struct pmu_hw_events *hw_events =this_cpu_ptr (armpmu->hw_events); + +if (!hw_events->branch_users) +return ; + +if (sched_in) +brbe_invalidate (); +}
Just a minor concern. I don't see any handling for task migration. E.g., for a task is migrated from one CPU to another CPU, I expect we need to save and restore branch records based on BRBE injection. So far, the driver simply invalidates all records.
I think this topic is very likely discussed before. If this is the case, please ignore my comment. Except this, the code looks good to me.
Thanks,
Leo
Re: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
Quoted 32 lines · Leo Yan
Quoted 2 lines · Rob Herring (Arm)
From: Anshuman Khandual <[email protected]>
[...]
Quoted 5 lines
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.
[...]
drivers/perf/arm_pmuv3.c static int armv8pmu_user_event_idx(struct perf_event *event) · line 1064
Quoted 11 lines
drivers/perf/arm_pmuv3.c +11
-0+static void armv8pmu_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) +{ +struct arm_pmu *armpmu = *this_cpu_ptr (&cpu_armpmu); +struct pmu_hw_events *hw_events =this_cpu_ptr (armpmu->hw_events); + +if (!hw_events->branch_users) +return ; + +if (sched_in) +brbe_invalidate (); +}Just a minor concern. I don't see any handling for task migration. E.g., for a task is migrated from one CPU to another CPU, I expect we need to save and restore branch records based on BRBE injection. So far, the driver simply invalidates all records.
Quoted 3 lines
I think this topic is very likely discussed before. If this is the case, please ignore my comment. Except this, the code looks good to me.
Not really discussed on the list, but that was present in v18 (though not functional because .sched_task() hook wasn't actually enabled) and Mark removed it. His work is here[1].The only comment was:
Note: saving/restoring at context-switch doesn't interact well with event rotation (e.g. if filters change)
Rob
Re: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
Quoted 36 lines · Rob Herring
Quoted 31 lines · Leo Yan
Quoted 2 lines · Rob Herring (Arm)
From: Anshuman Khandual <[email protected]>
[...]
Quoted 5 lines
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.
[...]
drivers/perf/arm_pmuv3.c static int armv8pmu_user_event_idx(struct perf_event *event) · line 1064
Quoted 11 lines
drivers/perf/arm_pmuv3.c +11
-0+static void armv8pmu_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) +{ +struct arm_pmu *armpmu = *this_cpu_ptr (&cpu_armpmu); +struct pmu_hw_events *hw_events =this_cpu_ptr (armpmu->hw_events); + +if (!hw_events->branch_users) +return ; + +if (sched_in) +brbe_invalidate (); +}Just a minor concern. I don't see any handling for task migration. E.g., for a task is migrated from one CPU to another CPU, I expect we need to save and restore branch records based on BRBE injection. So far, the driver simply invalidates all records.
Quoted 3 lines
I think this topic is very likely discussed before. If this is the case, please ignore my comment. Except this, the code looks good to me.
Quoted 6 lines
Not really discussed on the list, but that was present in v18 (though not functional because .sched_task() hook wasn't actually enabled) and Mark removed it. His work is here[1].The only comment was:
Note: saving/restoring at context-switch doesn't interact well with event rotation (e.g. if filters change)
In the brbe_enable() function, it "Merge the permitted branch filters of all events". Based on current implementation, all events share the same branch filter.
When event rotation happens, if without context switch, in theory we should can directly use the branch record (no invalidation, no injection) for all events.
For a context-switch case, we need to save and re-inject branch record. BRBE record sticks to a process context, no matter what events have been enabled.
Thanks,
Leo
Mark Rutland
maintainerRe: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
Quoted 41 lines · Leo Yan
Quoted 40 lines · Rob Herring
Quoted 31 lines · Leo Yan
Quoted 2 lines · Rob Herring (Arm)
From: Anshuman Khandual <[email protected]>
[...]
Quoted 5 lines
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.
[...]
drivers/perf/arm_pmuv3.c static int armv8pmu_user_event_idx(struct perf_event *event) · line 1064
Quoted 11 lines
drivers/perf/arm_pmuv3.c +11
-0+static void armv8pmu_sched_task (struct perf_event_pmu_context *pmu_ctx,bool sched_in) +{ +struct arm_pmu *armpmu = *this_cpu_ptr (&cpu_armpmu); +struct pmu_hw_events *hw_events =this_cpu_ptr (armpmu->hw_events); + +if (!hw_events->branch_users) +return ; + +if (sched_in) +brbe_invalidate (); +}Just a minor concern. I don't see any handling for task migration. E.g., for a task is migrated from one CPU to another CPU, I expect we need to save and restore branch records based on BRBE injection. So far, the driver simply invalidates all records.
Quoted 3 lines
I think this topic is very likely discussed before. If this is the case, please ignore my comment. Except this, the code looks good to me.
Not really discussed on the list, but that was present in v18 (though not functional because .sched_task() hook wasn't actually enabled) and Mark removed it. His work is here[1].The only comment was:
Quoted 6 lines
Quoted 2 lines
Note: saving/restoring at context-switch doesn't interact well with event rotation (e.g. if filters change)
In the
brbe_enable()function, it "Merge the permitted branch filters of all events". Based on current implementation, all events share the same branch filter.
Critically, the brbe_enable() function merges the filters of all *active* events which have been installed into hardware. It does not track all events which can be rotated, and the resulting filter is not the same -- it can change as a result of rotation.
Quoted 3 lines
When event rotation happens, if without context switch, in theory we should can directly use the branch record (no invalidation, no injection) for all events.
No; that only works in *some* cases, and will produce incorrect results in others.
For example, consider filtering. Imagine a PMU with a single counter, and two events, where event-A filters for calls-and-returns and event-B filters for calls-only. When switching from event-A to event-B, it's theoretically possible to keep the existing records around, knowing that the returns can be filtered out later. When switching from event-B to event-A we cannot keep the existing records, since there are gaps whenever a return should have been recorded.
There are a number of cases of that shape given the set of configurable filters. In theory it's possible to retain those in some cases, but I don't think that the complexity is justified.
Similarly, whenever kernel branches are recorded it's necessary to drop the stale branches whenever branch recording is paused, as there's necessarily a blackout period and hence a gap in the records.
Do you think that you have a case where losing branches across rotation *really* matters?
Quoted 3 lines
For a context-switch case, we need to save and re-inject branch record. BRBE record sticks to a process context, no matter what events have been enabled.
I had originally wanted to keep per-event records around, but it doesn't work in all cases. One reason events get discarded at context-switch time is that CPU-bound events can sample branches, and would mis-attribute stale userspace branches to the wrong context when switching tasks. There are explicit comments about this in amd_pmu_brs_sched_task() and intel_pmu_lbr_sched_task().
Given we discard records when reprogramming events, we *could* try to preserve events in some cases, but I suspect that as with the rotation case this'll be a lot of complexity for little gain. Note that as we discard events when enabling the PMU, we'd throw some task-bound records away anyway, and practically the gain would be limited to cpu-bound records.
Do you have a reason why you think we *must* keep events around?
Mark.
Re: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
On Mon, Feb 24, 2025 at 04:05:43PM +0000, Mark Rutland wrote:
[...]
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 8 lines
Just a minor concern. I don't see any handling for task migration. E.g., for a task is migrated from one CPU to another CPU, I expect we need to save and restore branch records based on BRBE injection. So far, the driver simply invalidates all records.
I think this topic is very likely discussed before. If this is the case, please ignore my comment. Except this, the code looks good to me.
Not really discussed on the list, but that was present in v18 (though not functional because .sched_task() hook wasn't actually enabled) and Mark removed it. His work is here[1].The only comment was:
Note: saving/restoring at context-switch doesn't interact well with event rotation (e.g. if filters change)
Quoted 8 lines
Quoted 3 lines
In the
brbe_enable()function, it "Merge the permitted branch filters of all events". Based on current implementation, all events share the same branch filter.Critically, the
brbe_enable()function merges the filters of all *active* events which have been installed into hardware. It does not track all events which can be rotated, and the resulting filter is not the same -- it can change as a result of rotation.
In a perf session has multiple events, and events have different branch filters, seems to me, a simple way is to return error for this case.
I would argue BRBE is an IP for recording branches per CPU wise, it does not support recording for event wise. If we can unify branch filter within a perf session, would this be much easier for handling?
Quoted 7 lines
Quoted 3 lines
When event rotation happens, if without context switch, in theory we should can directly use the branch record (no invalidation, no injection) for all events.
No; that only works in *some* cases, and will produce incorrect results in others.
Quoted 7 lines
For example, consider filtering. Imagine a PMU with a single counter, and two events, where event-A filters for calls-and-returns and event-B filters for calls-only. When switching from event-A to event-B, it's theoretically possible to keep the existing records around, knowing that the returns can be filtered out later. When switching from event-B to event-A we cannot keep the existing records, since there are gaps whenever a return should have been recorded.
Seems to me, the problem is not caused by event rotation. We need to calculate a correct filter in the first place - the BRBE driver should calculate a superset for all filters of events for a session. Then, generate branch record based event's specific filter.
Quoted 7 lines
There are a number of cases of that shape given the set of configurable filters. In theory it's possible to retain those in some cases, but I don't think that the complexity is justified.
Similarly, whenever kernel branches are recorded it's necessary to drop the stale branches whenever branch recording is paused, as there's necessarily a blackout period and hence a gap in the records.
If we save BRBE record when a process is switched out and then restore the record when a process is switched in, should we can keep a decent branch record for performance profiling?
I understand it might be many things happen in the middle of a task switching or migration, but it is fine for not recording branches during the blackout period. The missed branch records are not very helpful for forming a flow for a profiled program itself, especially, if we consider we will optimize userspace program in many cases.
Quoted 2 lines
Do you think that you have a case where losing branches across rotation *really* matters?
I agreed that event rotation case might be rare and complex. Please see a comment below.
Quoted 11 lines
Quoted 3 lines
For a context-switch case, we need to save and re-inject branch record. BRBE record sticks to a process context, no matter what events have been enabled.
I had originally wanted to keep per-event records around, but it doesn't work in all cases. One reason events get discarded at context-switch time is that CPU-bound events can sample branches, and would mis-attribute stale userspace branches to the wrong context when switching tasks. There are explicit comments about this in
amd_pmu_brs_sched_task()andintel_pmu_lbr_sched_task().
Quoted 8 lines
Given we discard records when reprogramming events, we *could* try to preserve events in some cases, but I suspect that as with the rotation case this'll be a lot of complexity for little gain. Note that as we discard events when enabling the PMU, we'd throw some task-bound records away anyway, and practically the gain would be limited to cpu-bound records.
Do you have a reason why you think we *must* keep events around?
Here I am really concerned are cases when a process is preempted or migrated. The driver doesn't save and restore branch records for these cases, it just invalidates all records when a task is scheduled in.
As a result, if an event overflow is close to context switch, it is likely to capture incomplete branch records. For a userspace-only tracing, it is risk to capture empty branch record after preemption and migrations.
Thanks,
Leo
Re: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
Quoted 30 lines · Leo Yan
On Mon, Feb 24, 2025 at 04:05:43PM +0000, Mark Rutland wrote:
[...]
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 8 lines
Just a minor concern. I don't see any handling for task migration. E.g., for a task is migrated from one CPU to another CPU, I expect we need to save and restore branch records based on BRBE injection. So far, the driver simply invalidates all records.
I think this topic is very likely discussed before. If this is the case, please ignore my comment. Except this, the code looks good to me.
Not really discussed on the list, but that was present in v18 (though not functional because .sched_task() hook wasn't actually enabled) and Mark removed it. His work is here[1].The only comment was:
Note: saving/restoring at context-switch doesn't interact well with event rotation (e.g. if filters change)
Quoted 8 lines
Quoted 3 lines
In the
brbe_enable()function, it "Merge the permitted branch filters of all events". Based on current implementation, all events share the same branch filter.Critically, the
brbe_enable()function merges the filters of all *active* events which have been installed into hardware. It does not track all events which can be rotated, and the resulting filter is not the same -- it can change as a result of rotation.
Quoted 6 lines
In a perf session has multiple events, and events have different branch filters, seems to me, a simple way is to return error for this case.
I would argue BRBE is an IP for recording branches per CPU wise, it does not support recording for event wise. If we can unify branch filter within a perf session, would this be much easier for handling?
And the PMU is an IP for recording events per CPU, but that is not perf (only)...
Quoted 16 lines
Quoted 8 lines
Quoted 4 lines
When event rotation happens, if without context switch, in theory we should can directly use the branch record (no invalidation, no injection) for all events.
No; that only works in *some* cases, and will produce incorrect results in others.
Quoted 7 lines
For example, consider filtering. Imagine a PMU with a single counter, and two events, where event-A filters for calls-and-returns and event-B filters for calls-only. When switching from event-A to event-B, it's theoretically possible to keep the existing records around, knowing that the returns can be filtered out later. When switching from event-B to event-A we cannot keep the existing records, since there are gaps whenever a return should have been recorded.
Quoted 4 lines
Seems to me, the problem is not caused by event rotation. We need to calculate a correct filter in the first place - the BRBE driver should calculate a superset for all filters of events for a session. Then, generate branch record based event's specific filter.
The driver doesn't have enough information. If it is told to schedule event A, it doesn't know anything about event B. It could in theory try to remember event B if event B had already been scheduled, but it never knows when event B is gone.
Quoted 11 lines
Quoted 7 lines
There are a number of cases of that shape given the set of configurable filters. In theory it's possible to retain those in some cases, but I don't think that the complexity is justified.
Similarly, whenever kernel branches are recorded it's necessary to drop the stale branches whenever branch recording is paused, as there's necessarily a blackout period and hence a gap in the records.
If we save BRBE record when a process is switched out and then restore the record when a process is switched in, should we can keep a decent branch record for performance profiling?
Keep in mind that there's only 64 branches recorded at most. How many branches in a context switch plus reconfiguring the PMU? Not a small percentage of 64 I think. In traces where freeze on overflow was not working (there's an example in v18), just the interrupt entry until BRBE was stopped was a significant part of the trace. A context switch is going to be similar.
Quoted 5 lines
I understand it might be many things happen in the middle of a task switching or migration, but it is fine for not recording branches during the blackout period. The missed branch records are not very helpful for forming a flow for a profiled program itself, especially, if we consider we will optimize userspace program in many cases.
We can't really not record during a blackout period. The only way we can is with freeze on overflow.
Quoted 26 lines
Quoted 2 lines
Do you think that you have a case where losing branches across rotation *really* matters?
I agreed that event rotation case might be rare and complex. Please see a comment below.
Quoted 11 lines
Quoted 3 lines
For a context-switch case, we need to save and re-inject branch record. BRBE record sticks to a process context, no matter what events have been enabled.
I had originally wanted to keep per-event records around, but it doesn't work in all cases. One reason events get discarded at context-switch time is that CPU-bound events can sample branches, and would mis-attribute stale userspace branches to the wrong context when switching tasks. There are explicit comments about this in
amd_pmu_brs_sched_task()andintel_pmu_lbr_sched_task().Quoted 8 lines
Given we discard records when reprogramming events, we *could* try to preserve events in some cases, but I suspect that as with the rotation case this'll be a lot of complexity for little gain. Note that as we discard events when enabling the PMU, we'd throw some task-bound records away anyway, and practically the gain would be limited to cpu-bound records.
Do you have a reason why you think we *must* keep events around?
Quoted 8 lines
Here I am really concerned are cases when a process is preempted or migrated. The driver doesn't save and restore branch records for these cases, it just invalidates all records when a task is scheduled in.
As a result, if an event overflow is close to context switch, it is likely to capture incomplete branch records. For a userspace-only tracing, it is risk to capture empty branch record after preemption and migrations.
There's the same risk if something else is recording kernel branches when you are recording userspace only. I think the user has to be aware if other things like context switches are perturbing their data.
Rob
Re: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
On Mon, Feb 24, 2025 at 07:31:52PM -0600, Rob Herring wrote:
[...]
Quoted 20 lines
Quoted 15 lines
Quoted 7 lines
Quoted 3 lines
When event rotation happens, if without context switch, in theory we should can directly use the branch record (no invalidation, no injection) for all events.
No; that only works in *some* cases, and will produce incorrect results in others.
Quoted 7 lines
For example, consider filtering. Imagine a PMU with a single counter, and two events, where event-A filters for calls-and-returns and event-B filters for calls-only. When switching from event-A to event-B, it's theoretically possible to keep the existing records around, knowing that the returns can be filtered out later. When switching from event-B to event-A we cannot keep the existing records, since there are gaps whenever a return should have been recorded.
Quoted 4 lines
Seems to me, the problem is not caused by event rotation. We need to calculate a correct filter in the first place - the BRBE driver should calculate a superset for all filters of events for a session. Then, generate branch record based event's specific filter.
Quoted 4 lines
The driver doesn't have enough information. If it is told to schedule event A, it doesn't know anything about event B. It could in theory try to remember event B if event B had already been scheduled, but it never knows when event B is gone.
E.g., I tried below command for enabling 10 events in a perf session:
perf record -e armv9_nevis/r04/ -e armv9_nevis/r05/ \
-e armv9_nevis/r06/ -e armv9_nevis/r07/ \
-e armv9_nevis/r08/ -e armv9_nevis/r09/ \
-e armv9_nevis/r10/ -e armv9_nevis/r11/ \
-e armv9_nevis/r12/ -e armv9_nevis/r13/ \
-- sleep 1For Arm PMU, the flow below is invoked for every event on every affinied CPU in initialization phase:
armpmu_event_init() {
armv8pmu_set_event_filter();
}Shouldn't we calculate a superset branch filter for all events, store it into a per-CPU data structure and then apply the filter on BRBE?
Quoted 12 lines
Quoted 11 lines
Quoted 7 lines
There are a number of cases of that shape given the set of configurable filters. In theory it's possible to retain those in some cases, but I don't think that the complexity is justified.
Similarly, whenever kernel branches are recorded it's necessary to drop the stale branches whenever branch recording is paused, as there's necessarily a blackout period and hence a gap in the records.
If we save BRBE record when a process is switched out and then restore the record when a process is switched in, should we can keep a decent branch record for performance profiling?
Quoted 6 lines
Keep in mind that there's only 64 branches recorded at most. How many branches in a context switch plus reconfiguring the PMU? Not a small percentage of 64 I think. In traces where freeze on overflow was not working (there's an example in v18), just the interrupt entry until BRBE was stopped was a significant part of the trace. A context switch is going to be similar.
That is true for kernel mode enabled tracing. But we will have no such kind noises for userspace only mode tracing.
[...]
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Do you have a reason why you think we *must* keep events around?
Here I am really concerned are cases when a process is preempted or migrated. The driver doesn't save and restore branch records for these cases, it just invalidates all records when a task is scheduled in.
Quoted 8 lines
Quoted 4 lines
As a result, if an event overflow is close to context switch, it is likely to capture incomplete branch records. For a userspace-only tracing, it is risk to capture empty branch record after preemption and migrations.
There's the same risk if something else is recording kernel branches when you are recording userspace only. I think the user has to be aware if other things like context switches are perturbing their data.
I am confused for the decription above. Does it refer to branch recording cross different sessions? It is fine for me that the branch data is interleaved by different sessions (e.g. one is global tracing and another is only per-thread tracing).
We might need to consider an intact branch record for the single perf session case. E.g. if userspace program calls:
func_a -> func_b -> func_c
In a case for only userspace tracing, we will have no chance to preserve the call sequence of these functions after the program is switched out.
Thanks,
Leo
Re: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
Quoted 39 lines · Leo Yan
On Mon, Feb 24, 2025 at 07:31:52PM -0600, Rob Herring wrote:
[...]
Quoted 20 lines
Quoted 15 lines
Quoted 7 lines
Quoted 3 lines
When event rotation happens, if without context switch, in theory we should can directly use the branch record (no invalidation, no injection) for all events.
No; that only works in *some* cases, and will produce incorrect results in others.
Quoted 7 lines
For example, consider filtering. Imagine a PMU with a single counter, and two events, where event-A filters for calls-and-returns and event-B filters for calls-only. When switching from event-A to event-B, it's theoretically possible to keep the existing records around, knowing that the returns can be filtered out later. When switching from event-B to event-A we cannot keep the existing records, since there are gaps whenever a return should have been recorded.
Quoted 4 lines
Seems to me, the problem is not caused by event rotation. We need to calculate a correct filter in the first place - the BRBE driver should calculate a superset for all filters of events for a session. Then, generate branch record based event's specific filter.
Quoted 4 lines
The driver doesn't have enough information. If it is told to schedule event A, it doesn't know anything about event B. It could in theory try to remember event B if event B had already been scheduled, but it never knows when event B is gone.
E.g., I tried below command for enabling 10 events in a perf session:
perf record -e armv9_nevis/r04/ -e armv9_nevis/r05/ \ -e armv9_nevis/r06/ -e armv9_nevis/r07/ \ -e armv9_nevis/r08/ -e armv9_nevis/r09/ \ -e armv9_nevis/r10/ -e armv9_nevis/r11/ \ -e armv9_nevis/r12/ -e armv9_nevis/r13/ \ -- sleep 1
Quoted 6 lines
For Arm PMU, the flow below is invoked for every event on every affinied CPU in initialization phase:
armpmu_event_init() { armv8pmu_set_event_filter(); }
That function is passed *1* event. It is not intended to go looking at all events or muck with any global state. Could we go poking around all the data structures? Probably, it's C and data structures are often not opaque when they should be, so that wouldn't be a good idea. If you think it is, I'd recommend you stay away from Rust.
Furthermore, an event here may not actually be enabled. A user could open 2 events and handle them as mutually exclusive continuously disabling one and enabling the other. If the branch filters that they want are not overlapping, we'd be reducing our effective branch record size. Maximizing the size seems much more important to me than keeping some branches in a few corner cases.
Quoted 2 lines
Shouldn't we calculate a superset branch filter for all events, store it into a per-CPU data structure and then apply the filter on BRBE?
Suppose we do that, what happens when a 2nd session (as Mark pointed out, sessions only exist in the perf tool, not the kernel) adds more events. We configured the filters and now we have to change them again. So we have to invalidate the branch record. It's the same thing with event rotation (though less frequent (probably)).
Quoted 44 lines
Quoted 12 lines
Quoted 11 lines
Quoted 7 lines
There are a number of cases of that shape given the set of configurable filters. In theory it's possible to retain those in some cases, but I don't think that the complexity is justified.
Similarly, whenever kernel branches are recorded it's necessary to drop the stale branches whenever branch recording is paused, as there's necessarily a blackout period and hence a gap in the records.
If we save BRBE record when a process is switched out and then restore the record when a process is switched in, should we can keep a decent branch record for performance profiling?
Quoted 6 lines
Keep in mind that there's only 64 branches recorded at most. How many branches in a context switch plus reconfiguring the PMU? Not a small percentage of 64 I think. In traces where freeze on overflow was not working (there's an example in v18), just the interrupt entry until BRBE was stopped was a significant part of the trace. A context switch is going to be similar.
That is true for kernel mode enabled tracing. But we will have no such kind noises for userspace only mode tracing.
[...]
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Do you have a reason why you think we *must* keep events around?
Here I am really concerned are cases when a process is preempted or migrated. The driver doesn't save and restore branch records for these cases, it just invalidates all records when a task is scheduled in.
Quoted 8 lines
Quoted 4 lines
As a result, if an event overflow is close to context switch, it is likely to capture incomplete branch records. For a userspace-only tracing, it is risk to capture empty branch record after preemption and migrations.
There's the same risk if something else is recording kernel branches when you are recording userspace only. I think the user has to be aware if other things like context switches are perturbing their data.
I am confused for the decription above. Does it refer to branch recording cross different sessions? It is fine for me that the branch data is interleaved by different sessions (e.g. one is global tracing and another is only per-thread tracing).
Quoted 7 lines
We might need to consider an intact branch record for the single perf session case. E.g. if userspace program calls:
func_a -> func_b -> func_cIn a case for only userspace tracing, we will have no chance to preserve the call sequence of these functions after the program is switched out.
So you miss the few times that happens in a context switch. But we are sampling and all/most of the other samples are going to be fine. How much is that really going to affect your profile?
Rob
Mark Rutland
maintainerRe: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
In reply to Leo YanQuoted 41 lines · Leo Yan
On Mon, Feb 24, 2025 at 07:31:52PM -0600, Rob Herring wrote:
[...]
Quoted 20 lines
Quoted 15 lines
Quoted 7 lines
Quoted 3 lines
When event rotation happens, if without context switch, in theory we should can directly use the branch record (no invalidation, no injection) for all events.
No; that only works in *some* cases, and will produce incorrect results in others.
Quoted 7 lines
For example, consider filtering. Imagine a PMU with a single counter, and two events, where event-A filters for calls-and-returns and event-B filters for calls-only. When switching from event-A to event-B, it's theoretically possible to keep the existing records around, knowing that the returns can be filtered out later. When switching from event-B to event-A we cannot keep the existing records, since there are gaps whenever a return should have been recorded.
Quoted 4 lines
Seems to me, the problem is not caused by event rotation. We need to calculate a correct filter in the first place - the BRBE driver should calculate a superset for all filters of events for a session. Then, generate branch record based event's specific filter.
Quoted 4 lines
The driver doesn't have enough information. If it is told to schedule event A, it doesn't know anything about event B. It could in theory try to remember event B if event B had already been scheduled, but it never knows when event B is gone.
E.g., I tried below command for enabling 10 events in a perf session:
perf record -e armv9_nevis/r04/ -e armv9_nevis/r05/ \ -e armv9_nevis/r06/ -e armv9_nevis/r07/ \ -e armv9_nevis/r08/ -e armv9_nevis/r09/ \ -e armv9_nevis/r10/ -e armv9_nevis/r11/ \ -e armv9_nevis/r12/ -e armv9_nevis/r13/ \ -- sleep 1For Arm PMU, the flow below is invoked for every event on every affinied CPU in initialization phase:
Quoted 6 lines
armpmu_event_init() { armv8pmu_set_event_filter(); }Shouldn't we calculate a superset branch filter for all events, store it into a per-CPU data structure and then apply the filter on BRBE?
Should we? No.
*NONE* of the events in your example are CPU-bound, and the call to armpmu_event_init() can happen on an arbitrary CPU which the relevant event never actually runs on, while other unrelated events may run on that CPU.
It makes no sense for armv8pmu_set_event_filter() to write to a per-cpu structure. That's purely there to determine what the filters *should* be when *that specific event* is programmed into hardware.
As Rob and I have pointed out already, the *only* thing that can be relevant to deciding the configuration of HW filtering is the set of events which are *active* on that CPU.
Quoted 19 lines
Quoted 12 lines
Quoted 11 lines
Quoted 7 lines
There are a number of cases of that shape given the set of configurable filters. In theory it's possible to retain those in some cases, but I don't think that the complexity is justified.
Similarly, whenever kernel branches are recorded it's necessary to drop the stale branches whenever branch recording is paused, as there's necessarily a blackout period and hence a gap in the records.
If we save BRBE record when a process is switched out and then restore the record when a process is switched in, should we can keep a decent branch record for performance profiling?
Quoted 6 lines
Keep in mind that there's only 64 branches recorded at most. How many branches in a context switch plus reconfiguring the PMU? Not a small percentage of 64 I think. In traces where freeze on overflow was not working (there's an example in v18), just the interrupt entry until BRBE was stopped was a significant part of the trace. A context switch is going to be similar.
Quoted 2 lines
That is true for kernel mode enabled tracing. But we will have no such kind noises for userspace only mode tracing.
As mentioned elsewhere, it's not a problem for x86, so why is it magically a problem for arm64?
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 1 line
Do you have a reason why you think we *must* keep events around?
Here I am really concerned are cases when a process is preempted or migrated. The driver doesn't save and restore branch records for these cases, it just invalidates all records when a task is scheduled in.
As a result, if an event overflow is close to context switch, it is likely to capture incomplete branch records. For a userspace-only tracing, it is risk to capture empty branch record after preemption and migrations.
Quoted 8 lines
Quoted 3 lines
There's the same risk if something else is recording kernel branches when you are recording userspace only. I think the user has to be aware if other things like context switches are perturbing their data.
I am confused for the decription above. Does it refer to branch recording cross different sessions? It is fine for me that the branch data is interleaved by different sessions (e.g. one is global tracing and another is only per-thread tracing).
Imagine that there's an existing process with some pid ${PID}, and concurrently, the following commands are run, either by the same user or different users with appropriate permissions:
# Trying to record user branches only
perf record -j any,u -e cycles -p ${PID}
# Trying to record kernel branches only
perf record -j any,k -e cycles -p ${PID}Whatever you do, the task trying to record user branches only will lose some records:
* If we make the events mutually exclusive, the branches will only be recorded when the user event is installed.
* If we merge the HW filters and later apply a SW filter, it's very likely that kernel branches taken after exception entry have filled all the records, and there are no user branches left to sample.
Quoted 7 lines
We might need to consider an intact branch record for the single perf session case. E.g. if userspace program calls:
func_a -> func_b -> func_cIn a case for only userspace tracing, we will have no chance to preserve the call sequence of these functions after the program is switched out.
If those functions are small, it's very likely that they'll all be in the branch history. If they're so large that they're not executed in one scheduling quantum, do you expect them to fall within the same event period?
I think that you're making a big deal out of an edge case that doesn't matter much in practice.
Mark.
Re: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
Is this something we can try to test? I have the ability to set this up and test it on a server system in my lab.
If we can't write up a potential test case, it is not testable, and thus probably not something to worry about.
Quoted 133 lines · Mark Rutland
Quoted 37 lines · Leo Yan
On Mon, Feb 24, 2025 at 07:31:52PM -0600, Rob Herring wrote:
[...]
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 3 lines
When event rotation happens, if without context switch, in theory we should can directly use the branch record (no invalidation, no injection) for all events.
No; that only works in *some* cases, and will produce incorrect results in others.
For example, consider filtering. Imagine a PMU with a single counter, and two events, where event-A filters for calls-and-returns and event-B filters for calls-only. When switching from event-A to event-B, it's theoretically possible to keep the existing records around, knowing that the returns can be filtered out later. When switching from event-B to event-A we cannot keep the existing records, since there are gaps whenever a return should have been recorded.
Quoted 8 lines
Quoted 4 lines
Seems to me, the problem is not caused by event rotation. We need to calculate a correct filter in the first place - the BRBE driver should calculate a superset for all filters of events for a session. Then, generate branch record based event's specific filter.
The driver doesn't have enough information. If it is told to schedule event A, it doesn't know anything about event B. It could in theory try to remember event B if event B had already been scheduled, but it never knows when event B is gone.
E.g., I tried below command for enabling 10 events in a perf session:
perf record -e armv9_nevis/r04/ -e armv9_nevis/r05/ \ -e armv9_nevis/r06/ -e armv9_nevis/r07/ \ -e armv9_nevis/r08/ -e armv9_nevis/r09/ \ -e armv9_nevis/r10/ -e armv9_nevis/r11/ \ -e armv9_nevis/r12/ -e armv9_nevis/r13/ \ -- sleep 1For Arm PMU, the flow below is invoked for every event on every affinied CPU in initialization phase:
Quoted 6 lines
armpmu_event_init() { armv8pmu_set_event_filter(); }Shouldn't we calculate a superset branch filter for all events, store it into a per-CPU data structure and then apply the filter on BRBE?
Should we? No.
*NONE* of the events in your example are CPU-bound, and the call to
armpmu_event_init()can happen on an arbitrary CPU which the relevant event never actually runs on, while other unrelated events may run on that CPU.It makes no sense for
armv8pmu_set_event_filter()to write to a per-cpu structure. That's purely there to determine what the filters *should* be when *that specific event* is programmed into hardware.As Rob and I have pointed out already, the *only* thing that can be relevant to deciding the configuration of HW filtering is the set of events which are *active* on that CPU.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
There are a number of cases of that shape given the set of configurable filters. In theory it's possible to retain those in some cases, but I don't think that the complexity is justified.
Similarly, whenever kernel branches are recorded it's necessary to drop the stale branches whenever branch recording is paused, as there's necessarily a blackout period and hence a gap in the records.
If we save BRBE record when a process is switched out and then restore the record when a process is switched in, should we can keep a decent branch record for performance profiling?
Quoted 8 lines
Quoted 6 lines
Keep in mind that there's only 64 branches recorded at most. How many branches in a context switch plus reconfiguring the PMU? Not a small percentage of 64 I think. In traces where freeze on overflow was not working (there's an example in v18), just the interrupt entry until BRBE was stopped was a significant part of the trace. A context switch is going to be similar.
That is true for kernel mode enabled tracing. But we will have no such kind noises for userspace only mode tracing.
As mentioned elsewhere, it's not a problem for x86, so why is it magically a problem for arm64?
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 1 line
Do you have a reason why you think we *must* keep events around?
Here I am really concerned are cases when a process is preempted or migrated. The driver doesn't save and restore branch records for these cases, it just invalidates all records when a task is scheduled in.
As a result, if an event overflow is close to context switch, it is likely to capture incomplete branch records. For a userspace-only tracing, it is risk to capture empty branch record after preemption and migrations.
Quoted 7 lines
Quoted 3 lines
There's the same risk if something else is recording kernel branches when you are recording userspace only. I think the user has to be aware if other things like context switches are perturbing their data.
I am confused for the decription above. Does it refer to branch recording cross different sessions? It is fine for me that the branch data is interleaved by different sessions (e.g. one is global tracing and another is only per-thread tracing).
Imagine that there's an existing process with some pid ${PID}, and concurrently, the following commands are run, either by the same user or different users with appropriate permissions:
# Trying to record user branches only perf record -j any,u -e cycles -p ${PID} # Trying to record kernel branches only perf record -j any,k -e cycles -p ${PID}Whatever you do, the task trying to record user branches only will lose some records:
* If we make the events mutually exclusive, the branches will only be recorded when the user event is installed.
* If we merge the HW filters and later apply a SW filter, it's very likely that kernel branches taken after exception entry have filled all the records, and there are no user branches left to sample.
Quoted 7 lines
We might need to consider an intact branch record for the single perf session case. E.g. if userspace program calls:
func_a -> func_b -> func_cIn a case for only userspace tracing, we will have no chance to preserve the call sequence of these functions after the program is switched out.
If those functions are small, it's very likely that they'll all be in the branch history. If they're so large that they're not executed in one scheduling quantum, do you expect them to fall within the same event period?
I think that you're making a big deal out of an edge case that doesn't matter much in practice.
Mark.
Mark Rutland
maintainerRe: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
In reply to Leo YanQuoted 24 lines · Leo Yan
On Mon, Feb 24, 2025 at 04:05:43PM +0000, Mark Rutland wrote:
[...]
Quoted 20 lines
Quoted 19 lines
Quoted 9 lines
Quoted 8 lines
Just a minor concern. I don't see any handling for task migration. E.g., for a task is migrated from one CPU to another CPU, I expect we need to save and restore branch records based on BRBE injection. So far, the driver simply invalidates all records.
I think this topic is very likely discussed before. If this is the case, please ignore my comment. Except this, the code looks good to me.
Quoted 6 lines
Not really discussed on the list, but that was present in v18 (though not functional because .sched_task() hook wasn't actually enabled) and Mark removed it. His work is here[1].The only comment was:
Note: saving/restoring at context-switch doesn't interact well with event rotation (e.g. if filters change)
In the
brbe_enable()function, it "Merge the permitted branch filters of all events". Based on current implementation, all events share the same branch filter.
Quoted 7 lines
Quoted 4 lines
Critically, the
brbe_enable()function merges the filters of all *active* events which have been installed into hardware. It does not track all events which can be rotated, and the resulting filter is not the same -- it can change as a result of rotation.In a perf session has multiple events, and events have different branch filters, seems to me, a simple way is to return error for this case.
FWIW, I'd generally prefer to do that since it avoids a number of horrible edge-cases and gets rid of the need to do SW filtering, which falls somewhere between "tricky" and "not entirely possible". However, that's not what LBR and others do, which is why we went with filter merging.
If folk on the tools side are happy with the kernel rejecting conflicting events, then I'd be more than happy to do that. What I don't want is that we start off with that approach and people immediately start to complain that the BRBE driver rejects events that the LBR driver accepts.
See the last time this came up:
https://lore.kernel.org/linux-arm-kernel/Zli6Ot0TwK3Qy-ee@J2N7QTR9R3/
https://lore.kernel.org/linux-arm-kernel/ZlnKFFwv9612V81h@J2N7QTR9R3/
Quoted 2 lines
I would argue BRBE is an IP for recording branches per CPU wise, it does not support recording for event wise.
Yes, there is a mismatch between the hardware and the perf ABI.
Quoted 2 lines
If we can unify branch filter within a perf session, would this be much easier for handling?
Do you mean if the perf tool ensured that all events in a given session had the same filter? From the kernel's PoV there's no such thing as a "perf session", and I'm not sure whether you're suggesting doing that in userspace or withing the kernel.
Doing that in the perf tool would certianly make a stronger argument for the kernel taking the "reject conflicting branch filters" option.
Doing that within the kernel isn't really possible.
Quoted 15 lines
Quoted 7 lines
Quoted 3 lines
When event rotation happens, if without context switch, in theory we should can directly use the branch record (no invalidation, no injection) for all events.
No; that only works in *some* cases, and will produce incorrect results in others.
Quoted 7 lines
For example, consider filtering. Imagine a PMU with a single counter, and two events, where event-A filters for calls-and-returns and event-B filters for calls-only. When switching from event-A to event-B, it's theoretically possible to keep the existing records around, knowing that the returns can be filtered out later. When switching from event-B to event-A we cannot keep the existing records, since there are gaps whenever a return should have been recorded.
Quoted 4 lines
Seems to me, the problem is not caused by event rotation. We need to calculate a correct filter in the first place - the BRBE driver should calculate a superset for all filters of events for a session. Then, generate branch record based event's specific filter.
From the kernel's PoV there is no such thing as a perf session, and the branch filters are per-event per the perf ABI.
We only really have two options:
(1) Reject conflicting filters when scheduling events. At event open time we have ot check whether an entire group is actually schedulable.
(2) Merge filters when scheduling events, and then filter out branches which don't match a particular event's filters when reading the branches.
Quoted 11 lines
Quoted 7 lines
There are a number of cases of that shape given the set of configurable filters. In theory it's possible to retain those in some cases, but I don't think that the complexity is justified.
Similarly, whenever kernel branches are recorded it's necessary to drop the stale branches whenever branch recording is paused, as there's necessarily a blackout period and hence a gap in the records.
If we save BRBE record when a process is switched out and then restore the record when a process is switched in, should we can keep a decent branch record for performance profiling?
I cannot parse this question. What are you trying to suggest here?
Quoted 5 lines
I understand it might be many things happen in the middle of a task switching or migration, but it is fine for not recording branches during the blackout period. The missed branch records are not very helpful for forming a flow for a profiled program itself, especially, if we consider we will optimize userspace program in many cases.
Just to be clear, you're talking about userspace specifically, right?
There are users that want a contiguous set of branches, which is what hardware tries to guarantee, and that's what LBR tries to gaurantee today, so I don't think that we can say gaps are always fine.
If we want a "give me as many arbitrary samples branches as possible, with arbitrary potential gaps" option, I'm not necessarily opposed to that, but I do not think that should be the default behaviour.
Quoted 26 lines
Quoted 2 lines
Do you think that you have a case where losing branches across rotation *really* matters?
I agreed that event rotation case might be rare and complex. Please see a comment below.
Quoted 11 lines
Quoted 3 lines
For a context-switch case, we need to save and re-inject branch record. BRBE record sticks to a process context, no matter what events have been enabled.
I had originally wanted to keep per-event records around, but it doesn't work in all cases. One reason events get discarded at context-switch time is that CPU-bound events can sample branches, and would mis-attribute stale userspace branches to the wrong context when switching tasks. There are explicit comments about this in
amd_pmu_brs_sched_task()andintel_pmu_lbr_sched_task().Quoted 8 lines
Given we discard records when reprogramming events, we *could* try to preserve events in some cases, but I suspect that as with the rotation case this'll be a lot of complexity for little gain. Note that as we discard events when enabling the PMU, we'd throw some task-bound records away anyway, and practically the gain would be limited to cpu-bound records.
Do you have a reason why you think we *must* keep events around?
Quoted 8 lines
Here I am really concerned are cases when a process is preempted or migrated. The driver doesn't save and restore branch records for these cases, it just invalidates all records when a task is scheduled in.
As a result, if an event overflow is close to context switch, it is likely to capture incomplete branch records. For a userspace-only tracing, it is risk to capture empty branch record after preemption and migrations.
Yes, and I was initially concerned about that, but is that really a problem? So long as the event period doesn't *always* coincide with preemption you'll get records from other samples anyway.
While I agree this is a theoretical concern, I don't think its of practical importance. This discarding happens on x86 *today* with LBR, and (AFAICT) there hav been no complaints. Note that the LBR logic to save/restore records for task contexts is only done for user callstack recording, which BRBE does not support.
Regardless, there's still the problematic interaction with event rotation; you cannot save/restore the records safely here if events (and the relevant filters) can change between the save and restore.
Discarding here is significantly simpler, and correct.
Mark.
Re: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
On Tue, Feb 25, 2025 at 12:01:52PM +0000, Mark Rutland wrote:
[...]
Quoted 14 lines
Quoted 7 lines
Quoted 4 lines
Critically, the
brbe_enable()function merges the filters of all *active* events which have been installed into hardware. It does not track all events which can be rotated, and the resulting filter is not the same -- it can change as a result of rotation.In a perf session has multiple events, and events have different branch filters, seems to me, a simple way is to return error for this case.
FWIW, I'd generally prefer to do that since it avoids a number of horrible edge-cases and gets rid of the need to do SW filtering, which falls somewhere between "tricky" and "not entirely possible". However, that's not what LBR and others do, which is why we went with filter merging.
Quoted 7 lines
If folk on the tools side are happy with the kernel rejecting conflicting events, then I'd be more than happy to do that. What I don't want is that we start off with that approach and people immediately start to complain that the BRBE driver rejects events that the LBR driver accepts.
See the last time this came up.
Thanks for the shared links. Based on the info, let's say we can have two cases:
Case 1: set different branch filters in a single perf session:
perf record -e armv8_pmuv3_0/r03,branch_type=any_call/u \
-e armv8_pmuv3_0/r04,branch_type=any_ret/k ...Case 2: set different branch filters in multiple perf sessions:
perf record -e armv8_pmuv3_0/r03,branch_type=any_call/u ...
perf record -e armv8_pmuv3_0/r04,branch_type=any_ret/k ...In my previous reply, I was suggesting that we should reject the case 1. IMO, it is not quite useful to configure different filters for events in the same session, especially if this leads complexity in the driver due to the hardware limitation.
For case 2, when create a new session, if the perf tool can read out the current branch filter setting (e.g. via sysfs node) and give suggestion what branch filter is compabile with existed sessions, seems to me, this is a feasible solution. My understanding this is a rare case, and a clear guidance for users would be sufficient if this happens. (Maybe we can give recommendation for how to use BRBE in the perf doc).
To be clear, an important factor is the trace modes with modifier 'u' (user) and 'k' (kernel) should be supported for different events and for different sessions. In a mixed cases (some events are userspace only and some are kernel only), the BRBE driver needs to filter out branch records for specific mode when taking a sample.
Quoted 7 lines
Quoted 2 lines
If we can unify branch filter within a perf session, would this be much easier for handling?
Do you mean if the perf tool ensured that all events in a given session had the same filter? From the kernel's PoV there's no such thing as a "perf session", and I'm not sure whether you're suggesting doing that in userspace or withing the kernel.
My understanding is this would be not difficult to do such kind checking in the tool. E.g., the perf tool can iterate every event and check the branch filter and detect incompabile issue.
Quoted 4 lines
Doing that in the perf tool would certianly make a stronger argument for the kernel taking the "reject conflicting branch filters" option.
Doing that within the kernel isn't really possible.
As said above, if the BRBE driver can provide a knob in sysfs to indicate what is the current branch filter in the existed sessions, this would be helpful for the tool to do the checking and remind users.
I haven't done any experiments for this. If you think this is the way to move forward, I might do a prototype and get back to you to ensure we don't run into any unexpected issues.
[...]
To make the discussion easier, I would like reply separately regarding the branch record save and restore issue.
Thanks,
Leo
Re: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
Quoted 35 lines · Leo Yan
On Tue, Feb 25, 2025 at 12:01:52PM +0000, Mark Rutland wrote:
[...]
Quoted 14 lines
Quoted 7 lines
Quoted 4 lines
Critically, the
brbe_enable()function merges the filters of all *active* events which have been installed into hardware. It does not track all events which can be rotated, and the resulting filter is not the same -- it can change as a result of rotation.In a perf session has multiple events, and events have different branch filters, seems to me, a simple way is to return error for this case.
FWIW, I'd generally prefer to do that since it avoids a number of horrible edge-cases and gets rid of the need to do SW filtering, which falls somewhere between "tricky" and "not entirely possible". However, that's not what LBR and others do, which is why we went with filter merging.
Quoted 7 lines
If folk on the tools side are happy with the kernel rejecting conflicting events, then I'd be more than happy to do that. What I don't want is that we start off with that approach and people immediately start to complain that the BRBE driver rejects events that the LBR driver accepts.
See the last time this came up.
Thanks for the shared links. Based on the info, let's say we can have two cases:
Case 1: set different branch filters in a single perf session:
perf record -e armv8_pmuv3_0/r03,branch_type=any_call/u \ -e armv8_pmuv3_0/r04,branch_type=any_ret/k ...
Quoted 7 lines
Case 2: set different branch filters in multiple perf sessions:
perf record -e armv8_pmuv3_0/r03,branch_type=any_call/u ... perf record -e armv8_pmuv3_0/r04,branch_type=any_ret/k ...In my previous reply, I was suggesting that we should reject the case 1.
The driver cannot distinguish those 2 cases.
Quoted 10 lines
IMO, it is not quite useful to configure different filters for events in the same session, especially if this leads complexity in the driver due to the hardware limitation.
For case 2, when create a new session, if the perf tool can read out the current branch filter setting (e.g. via sysfs node) and give suggestion what branch filter is compabile with existed sessions, seems to me, this is a feasible solution. My understanding this is a rare case, and a clear guidance for users would be sufficient if this happens. (Maybe we can give recommendation for how to use BRBE in the perf doc).
First, I don't think anything currently in sysfs for PMU changes based on current PMU usage. It is all static features. So you just added a 2nd control interface in addition to the syscall/ioctl interface. It is also totally racy. As soon as you read sysfs, the information could be out of date because an event was added or removed.
Second, that is completely different from how x86 works. Folks don't want to know how to use BRBE. They want to do perf branch stack recording like they already do on existing platforms. That's what has been implemented here with the behavior as close as possible even for corner cases that seem questionable. For the userspace counter access support, folks were upset that it has to be explicitly enabled (in sysctl) and requested (in a configX bit) when you don't on x86. People notice and care if the behavior is different.
Quoted 9 lines
To be clear, an important factor is the trace modes with modifier 'u' (user) and 'k' (kernel) should be supported for different events and for different sessions. In a mixed cases (some events are userspace only and some are kernel only), the BRBE driver needs to filter out branch records for specific mode when taking a sample.
Quoted 3 lines
Quoted 2 lines
If we can unify branch filter within a perf session, would this be much easier for handling?
Quoted 8 lines
Quoted 4 lines
Do you mean if the perf tool ensured that all events in a given session had the same filter? From the kernel's PoV there's no such thing as a "perf session", and I'm not sure whether you're suggesting doing that in userspace or withing the kernel.
My understanding is this would be not difficult to do such kind checking in the tool. E.g., the perf tool can iterate every event and check the branch filter and detect incompabile issue.
You could detect that in perf tool, but you can never do it in every tool because anyone can write their own.
Quoted 5 lines
Quoted 4 lines
Doing that in the perf tool would certianly make a stronger argument for the kernel taking the "reject conflicting branch filters" option.
Doing that within the kernel isn't really possible.
Quoted 7 lines
As said above, if the BRBE driver can provide a knob in sysfs to indicate what is the current branch filter in the existed sessions, this would be helpful for the tool to do the checking and remind users.
I haven't done any experiments for this. If you think this is the way to move forward, I might do a prototype and get back to you to ensure we don't run into any unexpected issues.
I don't think anyone does. I think this whole discussion has gone into the weeds.
Rob
Mark Rutland
maintainerRe: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
In reply to Leo YanQuoted 34 lines · Leo Yan
On Tue, Feb 25, 2025 at 12:01:52PM +0000, Mark Rutland wrote:
[...]
Quoted 14 lines
Quoted 7 lines
Quoted 4 lines
Critically, the
brbe_enable()function merges the filters of all *active* events which have been installed into hardware. It does not track all events which can be rotated, and the resulting filter is not the same -- it can change as a result of rotation.In a perf session has multiple events, and events have different branch filters, seems to me, a simple way is to return error for this case.
FWIW, I'd generally prefer to do that since it avoids a number of horrible edge-cases and gets rid of the need to do SW filtering, which falls somewhere between "tricky" and "not entirely possible". However, that's not what LBR and others do, which is why we went with filter merging.
Quoted 7 lines
If folk on the tools side are happy with the kernel rejecting conflicting events, then I'd be more than happy to do that. What I don't want is that we start off with that approach and people immediately start to complain that the BRBE driver rejects events that the LBR driver accepts.
See the last time this came up.
Thanks for the shared links. Based on the info, let's say we can have two cases:
Case 1: set different branch filters in a single perf session:
perf record -e armv8_pmuv3_0/r03,branch_type=any_call/u \ -e armv8_pmuv3_0/r04,branch_type=any_ret/k ...
Quoted 7 lines
Case 2: set different branch filters in multiple perf sessions:
perf record -e armv8_pmuv3_0/r03,branch_type=any_call/u ... perf record -e armv8_pmuv3_0/r04,branch_type=any_ret/k ...In my previous reply, I was suggesting that we should reject the case 1.
Do you mean that the kernel should reject that, or that userspace should reject that.
As mentioned earlier, I am ok with the idea that we reject *scheduling* events with mismatched filters, as we do for other resource conflicts. That would necessarily mean rejecting *groups* of events with inconsistent filters at open time.
However, I do not think that we should reject indepenent events which happen to have mismatched filters, regardless of whether they're opened by the same "session".
Quoted 3 lines
IMO, it is not quite useful to configure different filters for events in the same session, especially if this leads complexity in the driver due to the hardware limitation.
I generally agree, but IIRC userspace does this today.
Quoted 6 lines
For case 2, when create a new session, if the perf tool can read out the current branch filter setting (e.g. via sysfs node) and give suggestion what branch filter is compabile with existed sessions, seems to me, this is a feasible solution. My understanding this is a rare case, and a clear guidance for users would be sufficient if this happens. (Maybe we can give recommendation for how to use BRBE in the perf doc).
No. We are not going to expose *dynamic* information about the PMU hardware via sysfs. This is not as simple as you make it out to be. At any point in time there can be an arbitrary number of events open, and some arbitrary subset currently scheduled.
I agree that ideally this should be rare, though, and hence either of the two options I have suggested thus far should handle that acceptably.
Quoted 5 lines
To be clear, an important factor is the trace modes with modifier 'u' (user) and 'k' (kernel) should be supported for different events and for different sessions. In a mixed cases (some events are userspace only and some are kernel only), the BRBE driver needs to filter out branch records for specific mode when taking a sample.
I hate to repeat myself, but the driver has *no concept whatsoever* of a "session". It only knows:
* Which events are currently active in hardware.
* Which events have been grouped together.
* Which events have been opened for a given task or CPU context.
... and *NONE* of those correspond directly to a "session" managed by the userspace perf tool.
Quoted 11 lines
Quoted 7 lines
Quoted 2 lines
If we can unify branch filter within a perf session, would this be much easier for handling?
Do you mean if the perf tool ensured that all events in a given session had the same filter? From the kernel's PoV there's no such thing as a "perf session", and I'm not sure whether you're suggesting doing that in userspace or withing the kernel.
My understanding is this would be not difficult to do such kind checking in the tool. E.g., the perf tool can iterate every event and check the branch filter and detect incompabile issue.
Cool, sounds like we could do that then!
Quoted 8 lines
Quoted 4 lines
Doing that in the perf tool would certianly make a stronger argument for the kernel taking the "reject conflicting branch filters" option.
Doing that within the kernel isn't really possible.
As said above, if the BRBE driver can provide a knob in sysfs to indicate what is the current branch filter in the existed sessions, this would be helpful for the tool to do the checking and remind users.
Sorry, as above, that is not going to happen. It is not practical and will cause many other problems.
Quoted 3 lines
I haven't done any experiments for this. If you think this is the way to move forward, I might do a prototype and get back to you to ensure we don't run into any unexpected issues.
What specifically are you proposing to prototype?
Mark.
Re: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
Quoted 37 lines · Mark Rutland
Quoted 36 lines · Leo Yan
On Tue, Feb 25, 2025 at 12:01:52PM +0000, Mark Rutland wrote:
[...]
Quoted 14 lines
Quoted 7 lines
Quoted 4 lines
Critically, the
brbe_enable()function merges the filters of all *active* events which have been installed into hardware. It does not track all events which can be rotated, and the resulting filter is not the same -- it can change as a result of rotation.In a perf session has multiple events, and events have different branch filters, seems to me, a simple way is to return error for this case.
FWIW, I'd generally prefer to do that since it avoids a number of horrible edge-cases and gets rid of the need to do SW filtering, which falls somewhere between "tricky" and "not entirely possible". However, that's not what LBR and others do, which is why we went with filter merging.
Quoted 7 lines
If folk on the tools side are happy with the kernel rejecting conflicting events, then I'd be more than happy to do that. What I don't want is that we start off with that approach and people immediately start to complain that the BRBE driver rejects events that the LBR driver accepts.
See the last time this came up.
Thanks for the shared links. Based on the info, let's say we can have two cases:
Case 1: set different branch filters in a single perf session:
perf record -e armv8_pmuv3_0/r03,branch_type=any_call/u \ -e armv8_pmuv3_0/r04,branch_type=any_ret/k ...Case 2: set different branch filters in multiple perf sessions:
Quoted 8 lines
Quoted 5 lines
perf record -e armv8_pmuv3_0/r03,branch_type=any_call/u ... perf record -e armv8_pmuv3_0/r04,branch_type=any_ret/k ...In my previous reply, I was suggesting that we should reject the case 1.
Do you mean that the kernel should reject that, or that userspace should reject that.
For the case 1, the userspace can reject it as it has sufficient info to detect mismatched filters by iterating event list.
Quoted 4 lines
As mentioned earlier, I am ok with the idea that we reject *scheduling* events with mismatched filters, as we do for other resource conflicts. That would necessarily mean rejecting *groups* of events with inconsistent filters at open time.
This rejects the case 2 in the PMU driver.
Quoted 3 lines
However, I do not think that we should reject indepenent events which happen to have mismatched filters, regardless of whether they're opened by the same "session".
Can I understand "independent events" are events that are not in a event group?
If independent events can have mismatched filters, the PMU driver can activate them simultaneously, this means the BRBE driver needs to merge the branch filters. If so, I still see the complexity caused by this case.
Quoted 13 lines
Quoted 3 lines
IMO, it is not quite useful to configure different filters for events in the same session, especially if this leads complexity in the driver due to the hardware limitation.
I generally agree, but IIRC userspace does this today.
Quoted 6 lines
For case 2, when create a new session, if the perf tool can read out the current branch filter setting (e.g. via sysfs node) and give suggestion what branch filter is compabile with existed sessions, seems to me, this is a feasible solution. My understanding this is a rare case, and a clear guidance for users would be sufficient if this happens. (Maybe we can give recommendation for how to use BRBE in the perf doc).
Quoted 4 lines
No. We are not going to expose *dynamic* information about the PMU hardware via sysfs. This is not as simple as you make it out to be. At any point in time there can be an arbitrary number of events open, and some arbitrary subset currently scheduled.
If so, the perf tool will miss info for checking the existed branch filter and defer to handle the mismatched filter error until the kernel reports the issue.
Quoted 12 lines
I agree that ideally this should be rare, though, and hence either of the two options I have suggested thus far should handle that acceptably.
Quoted 5 lines
To be clear, an important factor is the trace modes with modifier 'u' (user) and 'k' (kernel) should be supported for different events and for different sessions. In a mixed cases (some events are userspace only and some are kernel only), the BRBE driver needs to filter out branch records for specific mode when taking a sample.
I hate to repeat myself, but the driver has *no concept whatsoever* of a "session". It only knows:
Quoted 8 lines
* Which events are currently active in hardware.
* Which events have been grouped together.
* Which events have been opened for a given task or CPU context.
... and *NONE* of those correspond directly to a "session" managed by the userspace perf tool.
Sorry for that. After your reminder, I understood that the PMU driver has no knowledge for perf session.
The description above does not refer to perf session. As you said, the PMU driver has its own context to track active events, and it is possible that some active events might trace only user mode while others trace only kernel mode. In this case, BRBE needs to be enabled for both user and kernel modes. When capturing samples, the BRBE driver needs to filter out branch records based on a specific event for its corresponding mode.
For better understanding, though we don't allow mismatched filters, I still think we should set both E0BRE and ExBRE bits if there are multiple active events with different modes.
[...]
Quoted 5 lines
Quoted 3 lines
I haven't done any experiments for this. If you think this is the way to move forward, I might do a prototype and get back to you to ensure we don't run into any unexpected issues.
What specifically are you proposing to prototype?
I mean, in Linux perf, we can add code to check branch filters for opened events when creating a session and report an error for mismatching filters. I would suggest we implement it specifically for Arm64 to avoid dispute with other archs.
Thanks,
Leo
Re: [PATCH v20 11/11] perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)
Quoted 56 lines · Leo Yan
Quoted 37 lines · Mark Rutland
Quoted 36 lines · Leo Yan
On Tue, Feb 25, 2025 at 12:01:52PM +0000, Mark Rutland wrote:
[...]
Quoted 14 lines
Quoted 7 lines
Quoted 4 lines
Critically, the
brbe_enable()function merges the filters of all *active* events which have been installed into hardware. It does not track all events which can be rotated, and the resulting filter is not the same -- it can change as a result of rotation.In a perf session has multiple events, and events have different branch filters, seems to me, a simple way is to return error for this case.
FWIW, I'd generally prefer to do that since it avoids a number of horrible edge-cases and gets rid of the need to do SW filtering, which falls somewhere between "tricky" and "not entirely possible". However, that's not what LBR and others do, which is why we went with filter merging.
Quoted 7 lines
If folk on the tools side are happy with the kernel rejecting conflicting events, then I'd be more than happy to do that. What I don't want is that we start off with that approach and people immediately start to complain that the BRBE driver rejects events that the LBR driver accepts.
See the last time this came up.
Thanks for the shared links. Based on the info, let's say we can have two cases:
Case 1: set different branch filters in a single perf session:
perf record -e armv8_pmuv3_0/r03,branch_type=any_call/u \ -e armv8_pmuv3_0/r04,branch_type=any_ret/k ...Case 2: set different branch filters in multiple perf sessions:
Quoted 8 lines
Quoted 5 lines
perf record -e armv8_pmuv3_0/r03,branch_type=any_call/u ... perf record -e armv8_pmuv3_0/r04,branch_type=any_ret/k ...In my previous reply, I was suggesting that we should reject the case 1.
Do you mean that the kernel should reject that, or that userspace should reject that.
For the case 1, the userspace can reject it as it has sufficient info to detect mismatched filters by iterating event list.
Quoted 4 lines
As mentioned earlier, I am ok with the idea that we reject *scheduling* events with mismatched filters, as we do for other resource conflicts. That would necessarily mean rejecting *groups* of events with inconsistent filters at open time.
Quoted 8 lines
This rejects the case 2 in the PMU driver.
Quoted 3 lines
However, I do not think that we should reject indepenent events which happen to have mismatched filters, regardless of whether they're opened by the same "session".
Can I understand "independent events" are events that are not in a event group?
Yes. The BRBE code has no concept of groups either.
Quoted 4 lines
If independent events can have mismatched filters, the PMU driver can activate them simultaneously, this means the BRBE driver needs to merge the branch filters. If so, I still see the complexity caused by this case.
That's exactly what the driver does. It's not even that complex. We have the BRBFCR and BRBCR register values for each event and just have to OR them together for the enabled events. For filtering we just compare each branch record to a mask of the enabled branch types for the event.
Quoted 46 lines
Quoted 13 lines
Quoted 3 lines
IMO, it is not quite useful to configure different filters for events in the same session, especially if this leads complexity in the driver due to the hardware limitation.
I generally agree, but IIRC userspace does this today.
Quoted 6 lines
For case 2, when create a new session, if the perf tool can read out the current branch filter setting (e.g. via sysfs node) and give suggestion what branch filter is compabile with existed sessions, seems to me, this is a feasible solution. My understanding this is a rare case, and a clear guidance for users would be sufficient if this happens. (Maybe we can give recommendation for how to use BRBE in the perf doc).
Quoted 4 lines
No. We are not going to expose *dynamic* information about the PMU hardware via sysfs. This is not as simple as you make it out to be. At any point in time there can be an arbitrary number of events open, and some arbitrary subset currently scheduled.
If so, the perf tool will miss info for checking the existed branch filter and defer to handle the mismatched filter error until the kernel reports the issue.
Quoted 12 lines
I agree that ideally this should be rare, though, and hence either of the two options I have suggested thus far should handle that acceptably.
Quoted 5 lines
To be clear, an important factor is the trace modes with modifier 'u' (user) and 'k' (kernel) should be supported for different events and for different sessions. In a mixed cases (some events are userspace only and some are kernel only), the BRBE driver needs to filter out branch records for specific mode when taking a sample.
I hate to repeat myself, but the driver has *no concept whatsoever* of a "session". It only knows:
Quoted 8 lines
* Which events are currently active in hardware.
* Which events have been grouped together.
* Which events have been opened for a given task or CPU context.
... and *NONE* of those correspond directly to a "session" managed by the userspace perf tool.
Sorry for that. After your reminder, I understood that the PMU driver has no knowledge for perf session.
Quoted 7 lines
The description above does not refer to perf session. As you said, the PMU driver has its own context to track active events, and it is possible that some active events might trace only user mode while others trace only kernel mode. In this case, BRBE needs to be enabled for both user and kernel modes. When capturing samples, the BRBE driver needs to filter out branch records based on a specific event for its corresponding mode.
That's exactly what the driver does.
Quoted 3 lines
For better understanding, though we don't allow mismatched filters, I still think we should set both E0BRE and ExBRE bits if there are multiple active events with different modes.
That's exactly what the driver does.
Quoted 6 lines
[...]
Quoted 4 lines
Quoted 3 lines
I haven't done any experiments for this. If you think this is the way to move forward, I might do a prototype and get back to you to ensure we don't run into any unexpected issues.
Quoted 6 lines
Quoted 1 line
What specifically are you proposing to prototype?
I mean, in Linux perf, we can add code to check branch filters for opened events when creating a session and report an error for mismatching filters. I would suggest we implement it specifically for Arm64 to avoid dispute with other archs.
There's not really any such thing as mismatching filters. There's no mutually exclusive filters. The only effect having multiple events enabling branch stack is you might run out of branch records sooner. A CPU with a minimal number of branch records is going to have a similar problem as will just enabling all branches on a single event. There's not any way to fix that other than increasing the number of records or adding a mechanism to the architecture to avoid dropping records.
Rob
Quoted 29 lines · Rob Herring (Arm)
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 11.
Patches 1-7 are new clean-ups/prep which stand on their own. They were previously posted here[2]. Please pick them up if there's no issues with them.
Patches 8-11 add BRBE support with the actual support in patch 11.
A git branch is here[3].
[1] https://developer.arm.com/documentation/ddi0487/latest/
[2] https://lore.kernel.org/all/[email protected]/
[3]git://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.gitarm/brbe-v20v20:
- 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
Quoted 9 lines
- 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 clearBRBCR_ELxbits inbrbe_disable(). This is for KVM nVHE checks if BRBE is enabled.
- Document that MDCR_EL3.SBRBE can be 0b01 also
Will Deacon
maintainerQuoted 9 lines · Rob Herring (Arm)
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 11.
[...]
Applied cleanups to will (for-next/perf), thanks!
[01/11] perf: arm_pmuv3: Call kvm_vcpu_pmu_resync_el0() before enabling counters
https://git.kernel.org/will/c/04bd15c4cbc3 [02/11] perf: arm_pmu: Don't disable counter in armpmu_add() https://git.kernel.org/will/c/dcca27bc1ecc [03/11] perf: arm_pmuv3: Don't disable counter in armv8pmu_enable_event() https://git.kernel.org/will/c/4b0567ad0be5 [04/11] perf: arm_v7_pmu: Drop obvious comments for enabling/disabling counters and interrupts https://git.kernel.org/will/c/7a5387748215 [05/11] perf: arm_v7_pmu: Don't disable counter in (armv7|krait_|scorpion_)pmu_enable_event() https://git.kernel.org/will/c/7bf1001e0d91 [06/11] perf: apple_m1: Don't disable counter in m1_pmu_enable_event() https://git.kernel.org/will/c/c2e793da59fc [07/11] perf: arm_pmu: Move PMUv3-specific data https://git.kernel.org/will/c/dc4d58a752ea
Cheers,
Signature · 5 lines
Will https://fixes.arm64.dev https://next.arm64.dev https://will.arm64.dev
Quoted 15 lines · Will Deacon
Quoted 9 lines · Rob Herring (Arm)
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 11.
[...]
Applied cleanups to will (for-next/perf), thanks!
[01/11] perf:
arm_pmuv3: Callkvm_vcpu_pmu_resync_el0()before enabling counters
Quoted 13 lines
https://git.kernel.org/will/c/04bd15c4cbc3 [02/11] perf: arm_pmu: Don't disable counter in armpmu_add() https://git.kernel.org/will/c/dcca27bc1ecc [03/11] perf: arm_pmuv3: Don't disable counter in armv8pmu_enable_event() https://git.kernel.org/will/c/4b0567ad0be5 [04/11] perf: arm_v7_pmu: Drop obvious comments for enabling/disabling counters and interrupts https://git.kernel.org/will/c/7a5387748215 [05/11] perf: arm_v7_pmu: Don't disable counter in (armv7|krait_|scorpion_)pmu_enable_event() https://git.kernel.org/will/c/7bf1001e0d91 [06/11] perf: apple_m1: Don't disable counter in m1_pmu_enable_event() https://git.kernel.org/will/c/c2e793da59fc [07/11] perf: arm_pmu: Move PMUv3-specific data https://git.kernel.org/will/c/dc4d58a752ea
I don't know if you looked at the thread on patch 11 and said "long discussion, I'll assume a new version is coming. Next!" because that's what I would do. In this case though, there's not any changes.
Rob
Catalin Marinas
maintainerQuoted 29 lines · Rob Herring
Quoted 14 lines · Will Deacon
Quoted 9 lines · Rob Herring (Arm)
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 11.
[...]
Applied cleanups to will (for-next/perf), thanks!
[01/11] perf:
arm_pmuv3: Callkvm_vcpu_pmu_resync_el0()before enabling countersQuoted 13 lines
https://git.kernel.org/will/c/04bd15c4cbc3 [02/11] perf: arm_pmu: Don't disable counter in armpmu_add() https://git.kernel.org/will/c/dcca27bc1ecc [03/11] perf: arm_pmuv3: Don't disable counter in armv8pmu_enable_event() https://git.kernel.org/will/c/4b0567ad0be5 [04/11] perf: arm_v7_pmu: Drop obvious comments for enabling/disabling counters and interrupts https://git.kernel.org/will/c/7a5387748215 [05/11] perf: arm_v7_pmu: Don't disable counter in (armv7|krait_|scorpion_)pmu_enable_event() https://git.kernel.org/will/c/7bf1001e0d91 [06/11] perf: apple_m1: Don't disable counter in m1_pmu_enable_event() https://git.kernel.org/will/c/c2e793da59fc [07/11] perf: arm_pmu: Move PMUv3-specific data https://git.kernel.org/will/c/dc4d58a752ea
Quoted 3 lines
I don't know if you looked at the thread on patch 11 and said "long discussion, I'll assume a new version is coming. Next!" because that's what I would do. In this case though, there's not any changes.
I do this as well ;). But I think Will is waiting for Mark R to look at the rest of the series.
Signature · 1 line
Catalin
Mark Rutland
maintainerQuoted 30 lines · Catalin Marinas
Quoted 29 lines · Rob Herring
Quoted 27 lines · Will Deacon
Quoted 9 lines · Rob Herring (Arm)
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 11.
[...]
Applied cleanups to will (for-next/perf), thanks!
[01/11] perf:
arm_pmuv3: Callkvm_vcpu_pmu_resync_el0()before enabling countershttps://git.kernel.org/will/c/04bd15c4cbc3 [02/11] perf: arm_pmu: Don't disable counter in armpmu_add() https://git.kernel.org/will/c/dcca27bc1ecc [03/11] perf: arm_pmuv3: Don't disable counter in armv8pmu_enable_event() https://git.kernel.org/will/c/4b0567ad0be5 [04/11] perf: arm_v7_pmu: Drop obvious comments for enabling/disabling counters and interrupts https://git.kernel.org/will/c/7a5387748215 [05/11] perf: arm_v7_pmu: Don't disable counter in (armv7|krait_|scorpion_)pmu_enable_event() https://git.kernel.org/will/c/7bf1001e0d91 [06/11] perf: apple_m1: Don't disable counter in m1_pmu_enable_event() https://git.kernel.org/will/c/c2e793da59fc [07/11] perf: arm_pmu: Move PMUv3-specific data https://git.kernel.org/will/c/dc4d58a752ea
Quoted 6 lines
Quoted 3 lines
I don't know if you looked at the thread on patch 11 and said "long discussion, I'll assume a new version is coming. Next!" because that's what I would do. In this case though, there's not any changes.
I do this as well ;). But I think Will is waiting for Mark R to look at the rest of the series.
Sorry for the delay there.
I'm happy with the structure of this; my only remaining concern is the filtering logic, as it is surprisingly subtle, and when I was last working on that it wasn't clear to me whether we could alwways filter appropriately (or whether it'd be better to reject scheduling of events with mismatched filters).
I'll try to page that in and properly attack that soon, but aside from that concern the series as a whole looks good to me.
Thanks,
Mark.