Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf: arm_pmuv3: Add support for the Branch Record Buffer Extension (BRBE)

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.

Cc: Catalin Marinas <catalin.marinas@arm.com>
Co-developed-by: Anshuman Khandual <anshuman.khandual@arm.com>
Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
Co-developed-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Tested-by: James Clark <james.clark@linaro.org>
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
tested-by: Adam Young <admiyo@os.amperecomputing.com>
Acked-by: Mark Rutland <mark.rutland@arm.com>
Link: https://lore.kernel.org/r/20250611-arm-brbe-v19-v23-4-e7775563036e@kernel.org
[will: Fix sparse warnings about mixed declarations and code.
Fix C99 comment syntax.]
Signed-off-by: Will Deacon <will@kernel.org>

authored by

Rob Herring (Arm) and committed by
Will Deacon
58074a0f d7567e9b

+990 -5
+11
drivers/perf/Kconfig
··· 223 223 Extension, which provides periodic sampling of operations in 224 224 the CPU pipeline and reports this via the perf AUX interface. 225 225 226 + config ARM64_BRBE 227 + bool "Enable support for branch stack sampling using FEAT_BRBE" 228 + depends on ARM_PMUV3 && ARM64 229 + default y 230 + help 231 + Enable perf support for Branch Record Buffer Extension (BRBE) which 232 + records all branches taken in an execution path. This supports some 233 + branch types and privilege based filtering. It captures additional 234 + relevant information such as cycle count, misprediction and branch 235 + type, branch privilege level etc. 236 + 226 237 config ARM_DMC620_PMU 227 238 tristate "Enable PMU support for the ARM DMC-620 memory controller" 228 239 depends on (ARM64 && ACPI) || COMPILE_TEST
+1
drivers/perf/Makefile
··· 23 23 obj-$(CONFIG_THUNDERX2_PMU) += thunderx2_pmu.o 24 24 obj-$(CONFIG_XGENE_PMU) += xgene_pmu.o 25 25 obj-$(CONFIG_ARM_SPE_PMU) += arm_spe_pmu.o 26 + obj-$(CONFIG_ARM64_BRBE) += arm_brbe.o 26 27 obj-$(CONFIG_ARM_DMC620_PMU) += arm_dmc620_pmu.o 27 28 obj-$(CONFIG_MARVELL_CN10K_TAD_PMU) += marvell_cn10k_tad_pmu.o 28 29 obj-$(CONFIG_MARVELL_CN10K_DDR_PMU) += marvell_cn10k_ddr_pmu.o
+805
drivers/perf/arm_brbe.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Branch Record Buffer Extension Driver. 4 + * 5 + * Copyright (C) 2022-2025 ARM Limited 6 + * 7 + * Author: Anshuman Khandual <anshuman.khandual@arm.com> 8 + */ 9 + #include <linux/types.h> 10 + #include <linux/bitmap.h> 11 + #include <linux/perf/arm_pmu.h> 12 + #include "arm_brbe.h" 13 + 14 + #define BRBFCR_EL1_BRANCH_FILTERS (BRBFCR_EL1_DIRECT | \ 15 + BRBFCR_EL1_INDIRECT | \ 16 + BRBFCR_EL1_RTN | \ 17 + BRBFCR_EL1_INDCALL | \ 18 + BRBFCR_EL1_DIRCALL | \ 19 + BRBFCR_EL1_CONDDIR) 20 + 21 + /* 22 + * BRBTS_EL1 is currently not used for branch stack implementation 23 + * purpose but BRBCR_ELx.TS needs to have a valid value from all 24 + * available options. BRBCR_ELx_TS_VIRTUAL is selected for this. 25 + */ 26 + #define BRBCR_ELx_DEFAULT_TS FIELD_PREP(BRBCR_ELx_TS_MASK, BRBCR_ELx_TS_VIRTUAL) 27 + 28 + /* 29 + * BRBE Buffer Organization 30 + * 31 + * BRBE buffer is arranged as multiple banks of 32 branch record 32 + * entries each. An individual branch record in a given bank could 33 + * be accessed, after selecting the bank in BRBFCR_EL1.BANK and 34 + * accessing the registers i.e [BRBSRC, BRBTGT, BRBINF] set with 35 + * indices [0..31]. 36 + * 37 + * Bank 0 38 + * 39 + * --------------------------------- ------ 40 + * | 00 | BRBSRC | BRBTGT | BRBINF | | 00 | 41 + * --------------------------------- ------ 42 + * | 01 | BRBSRC | BRBTGT | BRBINF | | 01 | 43 + * --------------------------------- ------ 44 + * | .. | BRBSRC | BRBTGT | BRBINF | | .. | 45 + * --------------------------------- ------ 46 + * | 31 | BRBSRC | BRBTGT | BRBINF | | 31 | 47 + * --------------------------------- ------ 48 + * 49 + * Bank 1 50 + * 51 + * --------------------------------- ------ 52 + * | 32 | BRBSRC | BRBTGT | BRBINF | | 00 | 53 + * --------------------------------- ------ 54 + * | 33 | BRBSRC | BRBTGT | BRBINF | | 01 | 55 + * --------------------------------- ------ 56 + * | .. | BRBSRC | BRBTGT | BRBINF | | .. | 57 + * --------------------------------- ------ 58 + * | 63 | BRBSRC | BRBTGT | BRBINF | | 31 | 59 + * --------------------------------- ------ 60 + */ 61 + #define BRBE_BANK_MAX_ENTRIES 32 62 + 63 + struct brbe_regset { 64 + u64 brbsrc; 65 + u64 brbtgt; 66 + u64 brbinf; 67 + }; 68 + 69 + #define PERF_BR_ARM64_MAX (PERF_BR_MAX + PERF_BR_NEW_MAX) 70 + 71 + struct brbe_hw_attr { 72 + int brbe_version; 73 + int brbe_cc; 74 + int brbe_nr; 75 + int brbe_format; 76 + }; 77 + 78 + #define BRBE_REGN_CASE(n, case_macro) \ 79 + case n: case_macro(n); break 80 + 81 + #define BRBE_REGN_SWITCH(x, case_macro) \ 82 + do { \ 83 + switch (x) { \ 84 + BRBE_REGN_CASE(0, case_macro); \ 85 + BRBE_REGN_CASE(1, case_macro); \ 86 + BRBE_REGN_CASE(2, case_macro); \ 87 + BRBE_REGN_CASE(3, case_macro); \ 88 + BRBE_REGN_CASE(4, case_macro); \ 89 + BRBE_REGN_CASE(5, case_macro); \ 90 + BRBE_REGN_CASE(6, case_macro); \ 91 + BRBE_REGN_CASE(7, case_macro); \ 92 + BRBE_REGN_CASE(8, case_macro); \ 93 + BRBE_REGN_CASE(9, case_macro); \ 94 + BRBE_REGN_CASE(10, case_macro); \ 95 + BRBE_REGN_CASE(11, case_macro); \ 96 + BRBE_REGN_CASE(12, case_macro); \ 97 + BRBE_REGN_CASE(13, case_macro); \ 98 + BRBE_REGN_CASE(14, case_macro); \ 99 + BRBE_REGN_CASE(15, case_macro); \ 100 + BRBE_REGN_CASE(16, case_macro); \ 101 + BRBE_REGN_CASE(17, case_macro); \ 102 + BRBE_REGN_CASE(18, case_macro); \ 103 + BRBE_REGN_CASE(19, case_macro); \ 104 + BRBE_REGN_CASE(20, case_macro); \ 105 + BRBE_REGN_CASE(21, case_macro); \ 106 + BRBE_REGN_CASE(22, case_macro); \ 107 + BRBE_REGN_CASE(23, case_macro); \ 108 + BRBE_REGN_CASE(24, case_macro); \ 109 + BRBE_REGN_CASE(25, case_macro); \ 110 + BRBE_REGN_CASE(26, case_macro); \ 111 + BRBE_REGN_CASE(27, case_macro); \ 112 + BRBE_REGN_CASE(28, case_macro); \ 113 + BRBE_REGN_CASE(29, case_macro); \ 114 + BRBE_REGN_CASE(30, case_macro); \ 115 + BRBE_REGN_CASE(31, case_macro); \ 116 + default: WARN(1, "Invalid BRB* index %d\n", x); \ 117 + } \ 118 + } while (0) 119 + 120 + #define RETURN_READ_BRBSRCN(n) \ 121 + return read_sysreg_s(SYS_BRBSRC_EL1(n)) 122 + static inline u64 get_brbsrc_reg(int idx) 123 + { 124 + BRBE_REGN_SWITCH(idx, RETURN_READ_BRBSRCN); 125 + return 0; 126 + } 127 + 128 + #define RETURN_READ_BRBTGTN(n) \ 129 + return read_sysreg_s(SYS_BRBTGT_EL1(n)) 130 + static u64 get_brbtgt_reg(int idx) 131 + { 132 + BRBE_REGN_SWITCH(idx, RETURN_READ_BRBTGTN); 133 + return 0; 134 + } 135 + 136 + #define RETURN_READ_BRBINFN(n) \ 137 + return read_sysreg_s(SYS_BRBINF_EL1(n)) 138 + static u64 get_brbinf_reg(int idx) 139 + { 140 + BRBE_REGN_SWITCH(idx, RETURN_READ_BRBINFN); 141 + return 0; 142 + } 143 + 144 + static u64 brbe_record_valid(u64 brbinf) 145 + { 146 + return FIELD_GET(BRBINFx_EL1_VALID_MASK, brbinf); 147 + } 148 + 149 + static bool brbe_invalid(u64 brbinf) 150 + { 151 + return brbe_record_valid(brbinf) == BRBINFx_EL1_VALID_NONE; 152 + } 153 + 154 + static bool brbe_record_is_complete(u64 brbinf) 155 + { 156 + return brbe_record_valid(brbinf) == BRBINFx_EL1_VALID_FULL; 157 + } 158 + 159 + static bool brbe_record_is_source_only(u64 brbinf) 160 + { 161 + return brbe_record_valid(brbinf) == BRBINFx_EL1_VALID_SOURCE; 162 + } 163 + 164 + static bool brbe_record_is_target_only(u64 brbinf) 165 + { 166 + return brbe_record_valid(brbinf) == BRBINFx_EL1_VALID_TARGET; 167 + } 168 + 169 + static int brbinf_get_in_tx(u64 brbinf) 170 + { 171 + return FIELD_GET(BRBINFx_EL1_T_MASK, brbinf); 172 + } 173 + 174 + static int brbinf_get_mispredict(u64 brbinf) 175 + { 176 + return FIELD_GET(BRBINFx_EL1_MPRED_MASK, brbinf); 177 + } 178 + 179 + static int brbinf_get_lastfailed(u64 brbinf) 180 + { 181 + return FIELD_GET(BRBINFx_EL1_LASTFAILED_MASK, brbinf); 182 + } 183 + 184 + static u16 brbinf_get_cycles(u64 brbinf) 185 + { 186 + u32 exp, mant, cycles; 187 + /* 188 + * Captured cycle count is unknown and hence 189 + * should not be passed on to userspace. 190 + */ 191 + if (brbinf & BRBINFx_EL1_CCU) 192 + return 0; 193 + 194 + exp = FIELD_GET(BRBINFx_EL1_CC_EXP_MASK, brbinf); 195 + mant = FIELD_GET(BRBINFx_EL1_CC_MANT_MASK, brbinf); 196 + 197 + if (!exp) 198 + return mant; 199 + 200 + cycles = (mant | 0x100) << (exp - 1); 201 + 202 + return min(cycles, U16_MAX); 203 + } 204 + 205 + static int brbinf_get_type(u64 brbinf) 206 + { 207 + return FIELD_GET(BRBINFx_EL1_TYPE_MASK, brbinf); 208 + } 209 + 210 + static int brbinf_get_el(u64 brbinf) 211 + { 212 + return FIELD_GET(BRBINFx_EL1_EL_MASK, brbinf); 213 + } 214 + 215 + void brbe_invalidate(void) 216 + { 217 + /* Ensure all branches before this point are recorded */ 218 + isb(); 219 + asm volatile(BRB_IALL_INSN); 220 + /* Ensure all branch records are invalidated after this point */ 221 + isb(); 222 + } 223 + 224 + static bool valid_brbe_nr(int brbe_nr) 225 + { 226 + return brbe_nr == BRBIDR0_EL1_NUMREC_8 || 227 + brbe_nr == BRBIDR0_EL1_NUMREC_16 || 228 + brbe_nr == BRBIDR0_EL1_NUMREC_32 || 229 + brbe_nr == BRBIDR0_EL1_NUMREC_64; 230 + } 231 + 232 + static bool valid_brbe_cc(int brbe_cc) 233 + { 234 + return brbe_cc == BRBIDR0_EL1_CC_20_BIT; 235 + } 236 + 237 + static bool valid_brbe_format(int brbe_format) 238 + { 239 + return brbe_format == BRBIDR0_EL1_FORMAT_FORMAT_0; 240 + } 241 + 242 + static bool valid_brbidr(u64 brbidr) 243 + { 244 + int brbe_format, brbe_cc, brbe_nr; 245 + 246 + brbe_format = FIELD_GET(BRBIDR0_EL1_FORMAT_MASK, brbidr); 247 + brbe_cc = FIELD_GET(BRBIDR0_EL1_CC_MASK, brbidr); 248 + brbe_nr = FIELD_GET(BRBIDR0_EL1_NUMREC_MASK, brbidr); 249 + 250 + return valid_brbe_format(brbe_format) && valid_brbe_cc(brbe_cc) && valid_brbe_nr(brbe_nr); 251 + } 252 + 253 + static bool valid_brbe_version(int brbe_version) 254 + { 255 + return brbe_version == ID_AA64DFR0_EL1_BRBE_IMP || 256 + brbe_version == ID_AA64DFR0_EL1_BRBE_BRBE_V1P1; 257 + } 258 + 259 + static void select_brbe_bank(int bank) 260 + { 261 + u64 brbfcr; 262 + 263 + brbfcr = read_sysreg_s(SYS_BRBFCR_EL1); 264 + brbfcr &= ~BRBFCR_EL1_BANK_MASK; 265 + brbfcr |= SYS_FIELD_PREP(BRBFCR_EL1, BANK, bank); 266 + write_sysreg_s(brbfcr, SYS_BRBFCR_EL1); 267 + /* 268 + * Arm ARM (DDI 0487K.a) D.18.4 rule PPBZP requires explicit sync 269 + * between setting BANK and accessing branch records. 270 + */ 271 + isb(); 272 + } 273 + 274 + static bool __read_brbe_regset(struct brbe_regset *entry, int idx) 275 + { 276 + entry->brbinf = get_brbinf_reg(idx); 277 + 278 + if (brbe_invalid(entry->brbinf)) 279 + return false; 280 + 281 + entry->brbsrc = get_brbsrc_reg(idx); 282 + entry->brbtgt = get_brbtgt_reg(idx); 283 + return true; 284 + } 285 + 286 + /* 287 + * Generic perf branch filters supported on BRBE 288 + * 289 + * New branch filters need to be evaluated whether they could be supported on 290 + * BRBE. This ensures that such branch filters would not just be accepted, to 291 + * fail silently. PERF_SAMPLE_BRANCH_HV is a special case that is selectively 292 + * supported only on platforms where kernel is in hyp mode. 293 + */ 294 + #define BRBE_EXCLUDE_BRANCH_FILTERS (PERF_SAMPLE_BRANCH_ABORT_TX | \ 295 + PERF_SAMPLE_BRANCH_IN_TX | \ 296 + PERF_SAMPLE_BRANCH_NO_TX | \ 297 + PERF_SAMPLE_BRANCH_CALL_STACK | \ 298 + PERF_SAMPLE_BRANCH_COUNTERS) 299 + 300 + #define BRBE_ALLOWED_BRANCH_TYPES (PERF_SAMPLE_BRANCH_ANY | \ 301 + PERF_SAMPLE_BRANCH_ANY_CALL | \ 302 + PERF_SAMPLE_BRANCH_ANY_RETURN | \ 303 + PERF_SAMPLE_BRANCH_IND_CALL | \ 304 + PERF_SAMPLE_BRANCH_COND | \ 305 + PERF_SAMPLE_BRANCH_IND_JUMP | \ 306 + PERF_SAMPLE_BRANCH_CALL) 307 + 308 + 309 + #define BRBE_ALLOWED_BRANCH_FILTERS (PERF_SAMPLE_BRANCH_USER | \ 310 + PERF_SAMPLE_BRANCH_KERNEL | \ 311 + PERF_SAMPLE_BRANCH_HV | \ 312 + BRBE_ALLOWED_BRANCH_TYPES | \ 313 + PERF_SAMPLE_BRANCH_NO_FLAGS | \ 314 + PERF_SAMPLE_BRANCH_NO_CYCLES | \ 315 + PERF_SAMPLE_BRANCH_TYPE_SAVE | \ 316 + PERF_SAMPLE_BRANCH_HW_INDEX | \ 317 + PERF_SAMPLE_BRANCH_PRIV_SAVE) 318 + 319 + #define BRBE_PERF_BRANCH_FILTERS (BRBE_ALLOWED_BRANCH_FILTERS | \ 320 + BRBE_EXCLUDE_BRANCH_FILTERS) 321 + 322 + /* 323 + * BRBE supports the following functional branch type filters while 324 + * generating branch records. These branch filters can be enabled, 325 + * either individually or as a group i.e ORing multiple filters 326 + * with each other. 327 + * 328 + * BRBFCR_EL1_CONDDIR - Conditional direct branch 329 + * BRBFCR_EL1_DIRCALL - Direct call 330 + * BRBFCR_EL1_INDCALL - Indirect call 331 + * BRBFCR_EL1_INDIRECT - Indirect branch 332 + * BRBFCR_EL1_DIRECT - Direct branch 333 + * BRBFCR_EL1_RTN - Subroutine return 334 + */ 335 + static u64 branch_type_to_brbfcr(int branch_type) 336 + { 337 + u64 brbfcr = 0; 338 + 339 + if (branch_type & PERF_SAMPLE_BRANCH_ANY) { 340 + brbfcr |= BRBFCR_EL1_BRANCH_FILTERS; 341 + return brbfcr; 342 + } 343 + 344 + if (branch_type & PERF_SAMPLE_BRANCH_ANY_CALL) { 345 + brbfcr |= BRBFCR_EL1_INDCALL; 346 + brbfcr |= BRBFCR_EL1_DIRCALL; 347 + } 348 + 349 + if (branch_type & PERF_SAMPLE_BRANCH_ANY_RETURN) 350 + brbfcr |= BRBFCR_EL1_RTN; 351 + 352 + if (branch_type & PERF_SAMPLE_BRANCH_IND_CALL) 353 + brbfcr |= BRBFCR_EL1_INDCALL; 354 + 355 + if (branch_type & PERF_SAMPLE_BRANCH_COND) 356 + brbfcr |= BRBFCR_EL1_CONDDIR; 357 + 358 + if (branch_type & PERF_SAMPLE_BRANCH_IND_JUMP) 359 + brbfcr |= BRBFCR_EL1_INDIRECT; 360 + 361 + if (branch_type & PERF_SAMPLE_BRANCH_CALL) 362 + brbfcr |= BRBFCR_EL1_DIRCALL; 363 + 364 + return brbfcr; 365 + } 366 + 367 + /* 368 + * BRBE supports the following privilege mode filters while generating 369 + * branch records. 370 + * 371 + * BRBCR_ELx_E0BRE - EL0 branch records 372 + * BRBCR_ELx_ExBRE - EL1/EL2 branch records 373 + * 374 + * BRBE also supports the following additional functional branch type 375 + * filters while generating branch records. 376 + * 377 + * BRBCR_ELx_EXCEPTION - Exception 378 + * BRBCR_ELx_ERTN - Exception return 379 + */ 380 + static u64 branch_type_to_brbcr(int branch_type) 381 + { 382 + u64 brbcr = BRBCR_ELx_FZP | BRBCR_ELx_DEFAULT_TS; 383 + 384 + if (branch_type & PERF_SAMPLE_BRANCH_USER) 385 + brbcr |= BRBCR_ELx_E0BRE; 386 + 387 + /* 388 + * When running in the hyp mode, writing into BRBCR_EL1 389 + * actually writes into BRBCR_EL2 instead. Field E2BRE 390 + * is also at the same position as E1BRE. 391 + */ 392 + if (branch_type & PERF_SAMPLE_BRANCH_KERNEL) 393 + brbcr |= BRBCR_ELx_ExBRE; 394 + 395 + if (branch_type & PERF_SAMPLE_BRANCH_HV) { 396 + if (is_kernel_in_hyp_mode()) 397 + brbcr |= BRBCR_ELx_ExBRE; 398 + } 399 + 400 + if (!(branch_type & PERF_SAMPLE_BRANCH_NO_CYCLES)) 401 + brbcr |= BRBCR_ELx_CC; 402 + 403 + if (!(branch_type & PERF_SAMPLE_BRANCH_NO_FLAGS)) 404 + brbcr |= BRBCR_ELx_MPRED; 405 + 406 + /* 407 + * The exception and exception return branches could be 408 + * captured, irrespective of the perf event's privilege. 409 + * If the perf event does not have enough privilege for 410 + * a given exception level, then addresses which falls 411 + * under that exception level will be reported as zero 412 + * for the captured branch record, creating source only 413 + * or target only records. 414 + */ 415 + if (branch_type & PERF_SAMPLE_BRANCH_KERNEL) { 416 + if (branch_type & PERF_SAMPLE_BRANCH_ANY) { 417 + brbcr |= BRBCR_ELx_EXCEPTION; 418 + brbcr |= BRBCR_ELx_ERTN; 419 + } 420 + 421 + if (branch_type & PERF_SAMPLE_BRANCH_ANY_CALL) 422 + brbcr |= BRBCR_ELx_EXCEPTION; 423 + 424 + if (branch_type & PERF_SAMPLE_BRANCH_ANY_RETURN) 425 + brbcr |= BRBCR_ELx_ERTN; 426 + } 427 + return brbcr; 428 + } 429 + 430 + bool brbe_branch_attr_valid(struct perf_event *event) 431 + { 432 + u64 branch_type = event->attr.branch_sample_type; 433 + 434 + /* 435 + * Ensure both perf branch filter allowed and exclude 436 + * masks are always in sync with the generic perf ABI. 437 + */ 438 + BUILD_BUG_ON(BRBE_PERF_BRANCH_FILTERS != (PERF_SAMPLE_BRANCH_MAX - 1)); 439 + 440 + if (branch_type & BRBE_EXCLUDE_BRANCH_FILTERS) { 441 + pr_debug("requested branch filter not supported 0x%llx\n", branch_type); 442 + return false; 443 + } 444 + 445 + /* Ensure at least 1 branch type is enabled */ 446 + if (!(branch_type & BRBE_ALLOWED_BRANCH_TYPES)) { 447 + pr_debug("no branch type enabled 0x%llx\n", branch_type); 448 + return false; 449 + } 450 + 451 + /* 452 + * No branches are recorded in guests nor nVHE hypervisors, so 453 + * excluding the host or both kernel and user is invalid. 454 + * 455 + * Ideally we'd just require exclude_guest and exclude_hv, but setting 456 + * event filters with perf for kernel or user don't set exclude_guest. 457 + * So effectively, exclude_guest and exclude_hv are ignored. 458 + */ 459 + if (event->attr.exclude_host || (event->attr.exclude_user && event->attr.exclude_kernel)) { 460 + pr_debug("branch filter in hypervisor or guest only not supported 0x%llx\n", branch_type); 461 + return false; 462 + } 463 + 464 + event->hw.branch_reg.config = branch_type_to_brbfcr(event->attr.branch_sample_type); 465 + event->hw.extra_reg.config = branch_type_to_brbcr(event->attr.branch_sample_type); 466 + 467 + return true; 468 + } 469 + 470 + unsigned int brbe_num_branch_records(const struct arm_pmu *armpmu) 471 + { 472 + return FIELD_GET(BRBIDR0_EL1_NUMREC_MASK, armpmu->reg_brbidr); 473 + } 474 + 475 + void brbe_probe(struct arm_pmu *armpmu) 476 + { 477 + u64 brbidr, aa64dfr0 = read_sysreg_s(SYS_ID_AA64DFR0_EL1); 478 + u32 brbe; 479 + 480 + brbe = cpuid_feature_extract_unsigned_field(aa64dfr0, ID_AA64DFR0_EL1_BRBE_SHIFT); 481 + if (!valid_brbe_version(brbe)) 482 + return; 483 + 484 + brbidr = read_sysreg_s(SYS_BRBIDR0_EL1); 485 + if (!valid_brbidr(brbidr)) 486 + return; 487 + 488 + armpmu->reg_brbidr = brbidr; 489 + } 490 + 491 + /* 492 + * BRBE is assumed to be disabled/paused on entry 493 + */ 494 + void brbe_enable(const struct arm_pmu *arm_pmu) 495 + { 496 + struct pmu_hw_events *cpuc = this_cpu_ptr(arm_pmu->hw_events); 497 + u64 brbfcr = 0, brbcr = 0; 498 + 499 + /* 500 + * Discard existing records to avoid a discontinuity, e.g. records 501 + * missed during handling an overflow. 502 + */ 503 + brbe_invalidate(); 504 + 505 + /* 506 + * Merge the permitted branch filters of all events. 507 + */ 508 + for (int i = 0; i < ARMPMU_MAX_HWEVENTS; i++) { 509 + struct perf_event *event = cpuc->events[i]; 510 + 511 + if (event && has_branch_stack(event)) { 512 + brbfcr |= event->hw.branch_reg.config; 513 + brbcr |= event->hw.extra_reg.config; 514 + } 515 + } 516 + 517 + /* 518 + * In VHE mode with MDCR_EL2.HPMN equal to PMCR_EL0.N, BRBCR_EL1.FZP 519 + * controls freezing the branch records on counter overflow rather than 520 + * BRBCR_EL2.FZP (which writes to BRBCR_EL1 are redirected to). 521 + * The exception levels are enabled/disabled in BRBCR_EL2, so keep EL1 522 + * and EL0 recording disabled for guests. 523 + * 524 + * As BRBCR_EL1 CC and MPRED bits also need to match, use the same 525 + * value for both registers just masking the exception levels. 526 + */ 527 + if (is_kernel_in_hyp_mode()) 528 + write_sysreg_s(brbcr & ~(BRBCR_ELx_ExBRE | BRBCR_ELx_E0BRE), SYS_BRBCR_EL12); 529 + write_sysreg_s(brbcr, SYS_BRBCR_EL1); 530 + /* Ensure BRBCR_ELx settings take effect before unpausing */ 531 + isb(); 532 + 533 + /* Finally write SYS_BRBFCR_EL to unpause BRBE */ 534 + write_sysreg_s(brbfcr, SYS_BRBFCR_EL1); 535 + /* Synchronization in PMCR write ensures ordering WRT PMU enabling */ 536 + } 537 + 538 + void brbe_disable(void) 539 + { 540 + /* 541 + * No need for synchronization here as synchronization in PMCR write 542 + * ensures ordering and in the interrupt handler this is a NOP as 543 + * we're already paused. 544 + */ 545 + write_sysreg_s(BRBFCR_EL1_PAUSED, SYS_BRBFCR_EL1); 546 + write_sysreg_s(0, SYS_BRBCR_EL1); 547 + } 548 + 549 + static const int brbe_type_to_perf_type_map[BRBINFx_EL1_TYPE_DEBUG_EXIT + 1][2] = { 550 + [BRBINFx_EL1_TYPE_DIRECT_UNCOND] = { PERF_BR_UNCOND, 0 }, 551 + [BRBINFx_EL1_TYPE_INDIRECT] = { PERF_BR_IND, 0 }, 552 + [BRBINFx_EL1_TYPE_DIRECT_LINK] = { PERF_BR_CALL, 0 }, 553 + [BRBINFx_EL1_TYPE_INDIRECT_LINK] = { PERF_BR_IND_CALL, 0 }, 554 + [BRBINFx_EL1_TYPE_RET] = { PERF_BR_RET, 0 }, 555 + [BRBINFx_EL1_TYPE_DIRECT_COND] = { PERF_BR_COND, 0 }, 556 + [BRBINFx_EL1_TYPE_CALL] = { PERF_BR_SYSCALL, 0 }, 557 + [BRBINFx_EL1_TYPE_ERET] = { PERF_BR_ERET, 0 }, 558 + [BRBINFx_EL1_TYPE_IRQ] = { PERF_BR_IRQ, 0 }, 559 + [BRBINFx_EL1_TYPE_TRAP] = { PERF_BR_IRQ, 0 }, 560 + [BRBINFx_EL1_TYPE_SERROR] = { PERF_BR_SERROR, 0 }, 561 + [BRBINFx_EL1_TYPE_ALIGN_FAULT] = { PERF_BR_EXTEND_ABI, PERF_BR_NEW_FAULT_ALGN }, 562 + [BRBINFx_EL1_TYPE_INSN_FAULT] = { PERF_BR_EXTEND_ABI, PERF_BR_NEW_FAULT_INST }, 563 + [BRBINFx_EL1_TYPE_DATA_FAULT] = { PERF_BR_EXTEND_ABI, PERF_BR_NEW_FAULT_DATA }, 564 + }; 565 + 566 + static void brbe_set_perf_entry_type(struct perf_branch_entry *entry, u64 brbinf) 567 + { 568 + int brbe_type = brbinf_get_type(brbinf); 569 + 570 + if (brbe_type <= BRBINFx_EL1_TYPE_DEBUG_EXIT) { 571 + const int *br_type = brbe_type_to_perf_type_map[brbe_type]; 572 + 573 + entry->type = br_type[0]; 574 + entry->new_type = br_type[1]; 575 + } 576 + } 577 + 578 + static int brbinf_get_perf_priv(u64 brbinf) 579 + { 580 + int brbe_el = brbinf_get_el(brbinf); 581 + 582 + switch (brbe_el) { 583 + case BRBINFx_EL1_EL_EL0: 584 + return PERF_BR_PRIV_USER; 585 + case BRBINFx_EL1_EL_EL1: 586 + return PERF_BR_PRIV_KERNEL; 587 + case BRBINFx_EL1_EL_EL2: 588 + if (is_kernel_in_hyp_mode()) 589 + return PERF_BR_PRIV_KERNEL; 590 + return PERF_BR_PRIV_HV; 591 + default: 592 + pr_warn_once("%d - unknown branch privilege captured\n", brbe_el); 593 + return PERF_BR_PRIV_UNKNOWN; 594 + } 595 + } 596 + 597 + static bool perf_entry_from_brbe_regset(int index, struct perf_branch_entry *entry, 598 + const struct perf_event *event) 599 + { 600 + struct brbe_regset bregs; 601 + u64 brbinf; 602 + 603 + if (!__read_brbe_regset(&bregs, index)) 604 + return false; 605 + 606 + brbinf = bregs.brbinf; 607 + perf_clear_branch_entry_bitfields(entry); 608 + if (brbe_record_is_complete(brbinf)) { 609 + entry->from = bregs.brbsrc; 610 + entry->to = bregs.brbtgt; 611 + } else if (brbe_record_is_source_only(brbinf)) { 612 + entry->from = bregs.brbsrc; 613 + entry->to = 0; 614 + } else if (brbe_record_is_target_only(brbinf)) { 615 + entry->from = 0; 616 + entry->to = bregs.brbtgt; 617 + } 618 + 619 + brbe_set_perf_entry_type(entry, brbinf); 620 + 621 + if (!branch_sample_no_cycles(event)) 622 + entry->cycles = brbinf_get_cycles(brbinf); 623 + 624 + if (!branch_sample_no_flags(event)) { 625 + /* Mispredict info is available for source only and complete branch records. */ 626 + if (!brbe_record_is_target_only(brbinf)) { 627 + entry->mispred = brbinf_get_mispredict(brbinf); 628 + entry->predicted = !entry->mispred; 629 + } 630 + 631 + /* 632 + * Currently TME feature is neither implemented in any hardware 633 + * nor it is being supported in the kernel. Just warn here once 634 + * if TME related information shows up rather unexpectedly. 635 + */ 636 + if (brbinf_get_lastfailed(brbinf) || brbinf_get_in_tx(brbinf)) 637 + pr_warn_once("Unknown transaction states\n"); 638 + } 639 + 640 + /* 641 + * Branch privilege level is available for target only and complete 642 + * branch records. 643 + */ 644 + if (!brbe_record_is_source_only(brbinf)) 645 + entry->priv = brbinf_get_perf_priv(brbinf); 646 + 647 + return true; 648 + } 649 + 650 + #define PERF_BR_ARM64_ALL ( \ 651 + BIT(PERF_BR_COND) | \ 652 + BIT(PERF_BR_UNCOND) | \ 653 + BIT(PERF_BR_IND) | \ 654 + BIT(PERF_BR_CALL) | \ 655 + BIT(PERF_BR_IND_CALL) | \ 656 + BIT(PERF_BR_RET)) 657 + 658 + #define PERF_BR_ARM64_ALL_KERNEL ( \ 659 + BIT(PERF_BR_SYSCALL) | \ 660 + BIT(PERF_BR_IRQ) | \ 661 + BIT(PERF_BR_SERROR) | \ 662 + BIT(PERF_BR_MAX + PERF_BR_NEW_FAULT_ALGN) | \ 663 + BIT(PERF_BR_MAX + PERF_BR_NEW_FAULT_DATA) | \ 664 + BIT(PERF_BR_MAX + PERF_BR_NEW_FAULT_INST)) 665 + 666 + static void prepare_event_branch_type_mask(u64 branch_sample, 667 + unsigned long *event_type_mask) 668 + { 669 + if (branch_sample & PERF_SAMPLE_BRANCH_ANY) { 670 + if (branch_sample & PERF_SAMPLE_BRANCH_KERNEL) 671 + bitmap_from_u64(event_type_mask, 672 + BIT(PERF_BR_ERET) | PERF_BR_ARM64_ALL | 673 + PERF_BR_ARM64_ALL_KERNEL); 674 + else 675 + bitmap_from_u64(event_type_mask, PERF_BR_ARM64_ALL); 676 + return; 677 + } 678 + 679 + bitmap_zero(event_type_mask, PERF_BR_ARM64_MAX); 680 + 681 + if (branch_sample & PERF_SAMPLE_BRANCH_ANY_CALL) { 682 + if (branch_sample & PERF_SAMPLE_BRANCH_KERNEL) 683 + bitmap_from_u64(event_type_mask, PERF_BR_ARM64_ALL_KERNEL); 684 + 685 + set_bit(PERF_BR_CALL, event_type_mask); 686 + set_bit(PERF_BR_IND_CALL, event_type_mask); 687 + } 688 + 689 + if (branch_sample & PERF_SAMPLE_BRANCH_IND_JUMP) 690 + set_bit(PERF_BR_IND, event_type_mask); 691 + 692 + if (branch_sample & PERF_SAMPLE_BRANCH_COND) 693 + set_bit(PERF_BR_COND, event_type_mask); 694 + 695 + if (branch_sample & PERF_SAMPLE_BRANCH_CALL) 696 + set_bit(PERF_BR_CALL, event_type_mask); 697 + 698 + if (branch_sample & PERF_SAMPLE_BRANCH_IND_CALL) 699 + set_bit(PERF_BR_IND_CALL, event_type_mask); 700 + 701 + if (branch_sample & PERF_SAMPLE_BRANCH_ANY_RETURN) { 702 + set_bit(PERF_BR_RET, event_type_mask); 703 + 704 + if (branch_sample & PERF_SAMPLE_BRANCH_KERNEL) 705 + set_bit(PERF_BR_ERET, event_type_mask); 706 + } 707 + } 708 + 709 + /* 710 + * BRBE is configured with an OR of permissions from all events, so there may 711 + * be events which have to be dropped or events where just the source or target 712 + * address has to be zeroed. 713 + */ 714 + static bool filter_branch_privilege(struct perf_branch_entry *entry, u64 branch_sample_type) 715 + { 716 + bool from_user = access_ok((void __user *)(unsigned long)entry->from, 4); 717 + bool to_user = access_ok((void __user *)(unsigned long)entry->to, 4); 718 + bool exclude_kernel = !((branch_sample_type & PERF_SAMPLE_BRANCH_KERNEL) || 719 + (is_kernel_in_hyp_mode() && (branch_sample_type & PERF_SAMPLE_BRANCH_HV))); 720 + 721 + /* We can only have a half record if permissions have not been expanded */ 722 + if (!entry->from || !entry->to) 723 + return true; 724 + 725 + /* 726 + * If record is within a single exception level, just need to either 727 + * drop or keep the entire record. 728 + */ 729 + if (from_user == to_user) 730 + return ((entry->priv == PERF_BR_PRIV_KERNEL) && !exclude_kernel) || 731 + ((entry->priv == PERF_BR_PRIV_USER) && 732 + (branch_sample_type & PERF_SAMPLE_BRANCH_USER)); 733 + 734 + /* 735 + * Record is across exception levels, mask addresses for the exception 736 + * level we're not capturing. 737 + */ 738 + if (!(branch_sample_type & PERF_SAMPLE_BRANCH_USER)) { 739 + if (from_user) 740 + entry->from = 0; 741 + if (to_user) 742 + entry->to = 0; 743 + } 744 + 745 + if (exclude_kernel) { 746 + if (!from_user) 747 + entry->from = 0; 748 + if (!to_user) 749 + entry->to = 0; 750 + } 751 + 752 + return true; 753 + } 754 + 755 + static bool filter_branch_type(struct perf_branch_entry *entry, 756 + const unsigned long *event_type_mask) 757 + { 758 + if (entry->type == PERF_BR_EXTEND_ABI) 759 + return test_bit(PERF_BR_MAX + entry->new_type, event_type_mask); 760 + else 761 + return test_bit(entry->type, event_type_mask); 762 + } 763 + 764 + static bool filter_branch_record(struct perf_branch_entry *entry, 765 + u64 branch_sample, 766 + const unsigned long *event_type_mask) 767 + { 768 + return filter_branch_type(entry, event_type_mask) && 769 + filter_branch_privilege(entry, branch_sample); 770 + } 771 + 772 + void brbe_read_filtered_entries(struct perf_branch_stack *branch_stack, 773 + const struct perf_event *event) 774 + { 775 + struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu); 776 + int nr_hw = brbe_num_branch_records(cpu_pmu); 777 + int nr_banks = DIV_ROUND_UP(nr_hw, BRBE_BANK_MAX_ENTRIES); 778 + int nr_filtered = 0; 779 + u64 branch_sample_type = event->attr.branch_sample_type; 780 + DECLARE_BITMAP(event_type_mask, PERF_BR_ARM64_MAX); 781 + 782 + prepare_event_branch_type_mask(branch_sample_type, event_type_mask); 783 + 784 + for (int bank = 0; bank < nr_banks; bank++) { 785 + int nr_remaining = nr_hw - (bank * BRBE_BANK_MAX_ENTRIES); 786 + int nr_this_bank = min(nr_remaining, BRBE_BANK_MAX_ENTRIES); 787 + 788 + select_brbe_bank(bank); 789 + 790 + for (int i = 0; i < nr_this_bank; i++) { 791 + struct perf_branch_entry *pbe = &branch_stack->entries[nr_filtered]; 792 + 793 + if (!perf_entry_from_brbe_regset(i, pbe, event)) 794 + goto done; 795 + 796 + if (!filter_branch_record(pbe, branch_sample_type, event_type_mask)) 797 + continue; 798 + 799 + nr_filtered++; 800 + } 801 + } 802 + 803 + done: 804 + branch_stack->nr = nr_filtered; 805 + }
+47
drivers/perf/arm_brbe.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + /* 3 + * Branch Record Buffer Extension Helpers. 4 + * 5 + * Copyright (C) 2022-2025 ARM Limited 6 + * 7 + * Author: Anshuman Khandual <anshuman.khandual@arm.com> 8 + */ 9 + 10 + struct arm_pmu; 11 + struct perf_branch_stack; 12 + struct perf_event; 13 + 14 + #ifdef CONFIG_ARM64_BRBE 15 + void brbe_probe(struct arm_pmu *arm_pmu); 16 + unsigned int brbe_num_branch_records(const struct arm_pmu *armpmu); 17 + void brbe_invalidate(void); 18 + 19 + void brbe_enable(const struct arm_pmu *arm_pmu); 20 + void brbe_disable(void); 21 + 22 + bool brbe_branch_attr_valid(struct perf_event *event); 23 + void brbe_read_filtered_entries(struct perf_branch_stack *branch_stack, 24 + const struct perf_event *event); 25 + #else 26 + static inline void brbe_probe(struct arm_pmu *arm_pmu) { } 27 + static inline unsigned int brbe_num_branch_records(const struct arm_pmu *armpmu) 28 + { 29 + return 0; 30 + } 31 + 32 + static inline void brbe_invalidate(void) { } 33 + 34 + static inline void brbe_enable(const struct arm_pmu *arm_pmu) { }; 35 + static inline void brbe_disable(void) { }; 36 + 37 + static inline bool brbe_branch_attr_valid(struct perf_event *event) 38 + { 39 + WARN_ON_ONCE(!has_branch_stack(event)); 40 + return false; 41 + } 42 + 43 + static void brbe_read_filtered_entries(struct perf_branch_stack *branch_stack, 44 + const struct perf_event *event) 45 + { 46 + } 47 + #endif
+13 -3
drivers/perf/arm_pmu.c
··· 99 99 .free_pmuirq = armpmu_free_percpu_pmunmi 100 100 }; 101 101 102 - static DEFINE_PER_CPU(struct arm_pmu *, cpu_armpmu); 102 + DEFINE_PER_CPU(struct arm_pmu *, cpu_armpmu); 103 103 static DEFINE_PER_CPU(int, cpu_irq); 104 104 static DEFINE_PER_CPU(const struct pmu_irq_ops *, cpu_irq_ops); 105 105 ··· 318 318 int idx = hwc->idx; 319 319 320 320 armpmu_stop(event, PERF_EF_UPDATE); 321 + 322 + if (has_branch_stack(event)) { 323 + hw_events->branch_users--; 324 + perf_sched_cb_dec(event->pmu); 325 + } 326 + 321 327 hw_events->events[idx] = NULL; 322 328 armpmu->clear_event_idx(hw_events, event); 323 329 perf_event_update_userpage(event); ··· 350 344 351 345 /* The newly-allocated counter should be empty */ 352 346 WARN_ON_ONCE(hw_events->events[idx]); 347 + 348 + if (has_branch_stack(event)) { 349 + hw_events->branch_users++; 350 + perf_sched_cb_inc(event->pmu); 351 + } 353 352 354 353 event->hw.idx = idx; 355 354 hw_events->events[idx] = event; ··· 520 509 !cpumask_test_cpu(event->cpu, &armpmu->supported_cpus)) 521 510 return -ENOENT; 522 511 523 - /* does not support taken branch sampling */ 524 - if (has_branch_stack(event)) 512 + if (has_branch_stack(event) && !armpmu->reg_brbidr) 525 513 return -EOPNOTSUPP; 526 514 527 515 return __hw_perf_event_init(event);
+105 -2
drivers/perf/arm_pmuv3.c
··· 25 25 #include <linux/smp.h> 26 26 #include <linux/nmi.h> 27 27 28 + #include "arm_brbe.h" 29 + 28 30 /* ARMv8 Cortex-A53 specific event types. */ 29 31 #define ARMV8_A53_PERFCTR_PREF_LINEFILL 0xC2 30 32 ··· 440 438 441 439 static DEVICE_ATTR_RO(threshold_max); 442 440 441 + static ssize_t branches_show(struct device *dev, 442 + struct device_attribute *attr, char *page) 443 + { 444 + struct pmu *pmu = dev_get_drvdata(dev); 445 + struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu); 446 + 447 + return sysfs_emit(page, "%d\n", brbe_num_branch_records(cpu_pmu)); 448 + } 449 + 450 + static DEVICE_ATTR_RO(branches); 451 + 443 452 static struct attribute *armv8_pmuv3_caps_attrs[] = { 453 + &dev_attr_branches.attr, 444 454 &dev_attr_slots.attr, 445 455 &dev_attr_bus_slots.attr, 446 456 &dev_attr_bus_width.attr, ··· 460 446 NULL, 461 447 }; 462 448 449 + static umode_t caps_is_visible(struct kobject *kobj, struct attribute *attr, int i) 450 + { 451 + struct device *dev = kobj_to_dev(kobj); 452 + struct pmu *pmu = dev_get_drvdata(dev); 453 + struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu); 454 + 455 + if (i == 0) 456 + return brbe_num_branch_records(cpu_pmu) ? attr->mode : 0; 457 + 458 + return attr->mode; 459 + } 460 + 463 461 static const struct attribute_group armv8_pmuv3_caps_attr_group = { 464 462 .name = "caps", 465 463 .attrs = armv8_pmuv3_caps_attrs, 464 + .is_visible = caps_is_visible, 466 465 }; 467 466 468 467 /* ··· 836 809 static void armv8pmu_start(struct arm_pmu *cpu_pmu) 837 810 { 838 811 struct perf_event_context *ctx; 812 + struct pmu_hw_events *hw_events = this_cpu_ptr(cpu_pmu->hw_events); 839 813 int nr_user = 0; 840 814 841 815 ctx = perf_cpu_task_ctx(); ··· 850 822 851 823 kvm_vcpu_pmu_resync_el0(); 852 824 825 + if (hw_events->branch_users) 826 + brbe_enable(cpu_pmu); 827 + 853 828 /* Enable all counters */ 854 829 armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E); 855 830 } 856 831 857 832 static void armv8pmu_stop(struct arm_pmu *cpu_pmu) 858 833 { 834 + struct pmu_hw_events *hw_events = this_cpu_ptr(cpu_pmu->hw_events); 835 + 836 + if (hw_events->branch_users) 837 + brbe_disable(); 838 + 859 839 /* Disable all counters */ 860 840 armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E); 841 + } 842 + 843 + static void read_branch_records(struct pmu_hw_events *cpuc, 844 + struct perf_event *event, 845 + struct perf_sample_data *data) 846 + { 847 + struct perf_branch_stack *branch_stack = cpuc->branch_stack; 848 + 849 + brbe_read_filtered_entries(branch_stack, event); 850 + perf_sample_save_brstack(data, event, branch_stack, NULL); 861 851 } 862 852 863 853 static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu) ··· 927 881 perf_sample_data_init(&data, 0, hwc->last_period); 928 882 if (!armpmu_event_set_period(event)) 929 883 continue; 884 + 885 + if (has_branch_stack(event)) 886 + read_branch_records(cpuc, event, &data); 930 887 931 888 /* 932 889 * Perf event overflow will queue the processing of the event as ··· 987 938 988 939 /* Always prefer to place a cycle counter into the cycle counter. */ 989 940 if ((evtype == ARMV8_PMUV3_PERFCTR_CPU_CYCLES) && 990 - !armv8pmu_event_get_threshold(&event->attr)) { 941 + !armv8pmu_event_get_threshold(&event->attr) && !has_branch_stack(event)) { 991 942 if (!test_and_set_bit(ARMV8_PMU_CYCLE_IDX, cpuc->used_mask)) 992 943 return ARMV8_PMU_CYCLE_IDX; 993 944 else if (armv8pmu_event_is_64bit(event) && ··· 1036 987 return event->hw.idx + 1; 1037 988 } 1038 989 990 + static void armv8pmu_sched_task(struct perf_event_pmu_context *pmu_ctx, 991 + struct task_struct *task, bool sched_in) 992 + { 993 + struct arm_pmu *armpmu = *this_cpu_ptr(&cpu_armpmu); 994 + struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events); 995 + 996 + if (!hw_events->branch_users) 997 + return; 998 + 999 + if (sched_in) 1000 + brbe_invalidate(); 1001 + } 1002 + 1039 1003 /* 1040 1004 * Add an event filter to a given event. 1041 1005 */ ··· 1064 1002 if (attr->exclude_idle) { 1065 1003 pr_debug("ARM performance counters do not support mode exclusion\n"); 1066 1004 return -EOPNOTSUPP; 1005 + } 1006 + 1007 + if (has_branch_stack(perf_event)) { 1008 + if (!brbe_num_branch_records(cpu_pmu) || !brbe_branch_attr_valid(perf_event)) 1009 + return -EOPNOTSUPP; 1010 + 1011 + perf_event->attach_state |= PERF_ATTACH_SCHED_CB; 1067 1012 } 1068 1013 1069 1014 /* ··· 1138 1069 1139 1070 /* Clear the counters we flip at guest entry/exit */ 1140 1071 kvm_clr_pmu_events(mask); 1072 + 1073 + if (brbe_num_branch_records(cpu_pmu)) { 1074 + brbe_disable(); 1075 + brbe_invalidate(); 1076 + } 1141 1077 1142 1078 /* 1143 1079 * Initialize & Reset PMNC. Request overflow interrupt for ··· 1312 1238 cpu_pmu->reg_pmmir = read_pmmir(); 1313 1239 else 1314 1240 cpu_pmu->reg_pmmir = 0; 1241 + 1242 + brbe_probe(cpu_pmu); 1243 + } 1244 + 1245 + static int branch_records_alloc(struct arm_pmu *armpmu) 1246 + { 1247 + size_t size = struct_size_t(struct perf_branch_stack, entries, 1248 + brbe_num_branch_records(armpmu)); 1249 + int cpu; 1250 + 1251 + for_each_cpu(cpu, &armpmu->supported_cpus) { 1252 + struct pmu_hw_events *events_cpu; 1253 + 1254 + events_cpu = per_cpu_ptr(armpmu->hw_events, cpu); 1255 + events_cpu->branch_stack = kmalloc(size, GFP_KERNEL); 1256 + if (!events_cpu->branch_stack) 1257 + return -ENOMEM; 1258 + } 1259 + return 0; 1315 1260 } 1316 1261 1317 1262 static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu) ··· 1347 1254 if (ret) 1348 1255 return ret; 1349 1256 1350 - return probe.present ? 0 : -ENODEV; 1257 + if (!probe.present) 1258 + return -ENODEV; 1259 + 1260 + if (brbe_num_branch_records(cpu_pmu)) { 1261 + ret = branch_records_alloc(cpu_pmu); 1262 + if (ret) 1263 + return ret; 1264 + } 1265 + return 0; 1351 1266 } 1352 1267 1353 1268 static void armv8pmu_disable_user_access_ipi(void *unused) ··· 1414 1313 cpu_pmu->set_event_filter = armv8pmu_set_event_filter; 1415 1314 1416 1315 cpu_pmu->pmu.event_idx = armv8pmu_user_event_idx; 1316 + if (brbe_num_branch_records(cpu_pmu)) 1317 + cpu_pmu->pmu.sched_task = armv8pmu_sched_task; 1417 1318 1418 1319 cpu_pmu->name = name; 1419 1320 cpu_pmu->map_event = map_event;
+8
include/linux/perf/arm_pmu.h
··· 70 70 struct arm_pmu *percpu_pmu; 71 71 72 72 int irq; 73 + 74 + struct perf_branch_stack *branch_stack; 75 + 76 + /* Active events requesting branch records */ 77 + unsigned int branch_users; 73 78 }; 74 79 75 80 enum armpmu_attr_groups { ··· 120 115 /* PMUv3 only */ 121 116 int pmuver; 122 117 u64 reg_pmmir; 118 + u64 reg_brbidr; 123 119 #define ARMV8_PMUV3_MAX_COMMON_EVENTS 0x40 124 120 DECLARE_BITMAP(pmceid_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS); 125 121 #define ARMV8_PMUV3_EXT_COMMON_EVENT_BASE 0x4000 ··· 131 125 }; 132 126 133 127 #define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu)) 128 + 129 + DECLARE_PER_CPU(struct arm_pmu *, cpu_armpmu); 134 130 135 131 u64 armpmu_event_update(struct perf_event *event); 136 132