Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 598c2fa

Browse files
sandip4nIngo Molnar
authored andcommitted
perf/x86/amd/lbr: Use freeze based on availability
Currently, the LBR code assumes that LBR Freeze is supported on all processors when X86_FEATURE_AMD_LBR_V2 is available i.e. CPUID leaf 0x80000022[EAX] bit 1 is set. This is incorrect as the availability of the feature is additionally dependent on CPUID leaf 0x80000022[EAX] bit 2 being set, which may not be set for all Zen 4 processors. Define a new feature bit for LBR and PMC freeze and set the freeze enable bit (FLBRI) in DebugCtl (MSR 0x1d9) conditionally. It should still be possible to use LBR without freeze for profile-guided optimization of user programs by using an user-only branch filter during profiling. When the user-only filter is enabled, branches are no longer recorded after the transition to CPL 0 upon PMI arrival. When branch entries are read in the PMI handler, the branch stack does not change. E.g. $ perf record -j any,u -e ex_ret_brn_tkn ./workload Since the feature bit is visible under flags in /proc/cpuinfo, it can be used to determine the feasibility of use-cases which require LBR Freeze to be supported by the hardware such as profile-guided optimization of kernels. Fixes: ca5b7c0 ("perf/x86/amd/lbr: Add LbrExtV2 branch record support") Signed-off-by: Sandipan Das <sandipan.das@amd.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/69a453c97cfd11c6f2584b19f937fe6df741510f.1711091584.git.sandipan.das@amd.com
1 parent 7f274e6 commit 598c2fa

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

arch/x86/events/amd/core.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,8 +904,8 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs)
904904
if (!status)
905905
goto done;
906906

907-
/* Read branch records before unfreezing */
908-
if (status & GLOBAL_STATUS_LBRS_FROZEN) {
907+
/* Read branch records */
908+
if (x86_pmu.lbr_nr) {
909909
amd_pmu_lbr_read();
910910
status &= ~GLOBAL_STATUS_LBRS_FROZEN;
911911
}

arch/x86/events/amd/lbr.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,12 @@ void amd_pmu_lbr_enable_all(void)
402402
wrmsrl(MSR_AMD64_LBR_SELECT, lbr_select);
403403
}
404404

405-
rdmsrl(MSR_IA32_DEBUGCTLMSR, dbg_ctl);
406-
rdmsrl(MSR_AMD_DBG_EXTN_CFG, dbg_extn_cfg);
405+
if (cpu_feature_enabled(X86_FEATURE_AMD_LBR_PMC_FREEZE)) {
406+
rdmsrl(MSR_IA32_DEBUGCTLMSR, dbg_ctl);
407+
wrmsrl(MSR_IA32_DEBUGCTLMSR, dbg_ctl | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
408+
}
407409

408-
wrmsrl(MSR_IA32_DEBUGCTLMSR, dbg_ctl | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
410+
rdmsrl(MSR_AMD_DBG_EXTN_CFG, dbg_extn_cfg);
409411
wrmsrl(MSR_AMD_DBG_EXTN_CFG, dbg_extn_cfg | DBG_EXTN_CFG_LBRV2EN);
410412
}
411413

@@ -418,10 +420,12 @@ void amd_pmu_lbr_disable_all(void)
418420
return;
419421

420422
rdmsrl(MSR_AMD_DBG_EXTN_CFG, dbg_extn_cfg);
421-
rdmsrl(MSR_IA32_DEBUGCTLMSR, dbg_ctl);
422-
423423
wrmsrl(MSR_AMD_DBG_EXTN_CFG, dbg_extn_cfg & ~DBG_EXTN_CFG_LBRV2EN);
424-
wrmsrl(MSR_IA32_DEBUGCTLMSR, dbg_ctl & ~DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
424+
425+
if (cpu_feature_enabled(X86_FEATURE_AMD_LBR_PMC_FREEZE)) {
426+
rdmsrl(MSR_IA32_DEBUGCTLMSR, dbg_ctl);
427+
wrmsrl(MSR_IA32_DEBUGCTLMSR, dbg_ctl & ~DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
428+
}
425429
}
426430

427431
__init int amd_pmu_lbr_init(void)

arch/x86/include/asm/cpufeatures.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,14 @@
459459
#define X86_FEATURE_IBPB_BRTYPE (20*32+28) /* "" MSR_PRED_CMD[IBPB] flushes all branch type predictions */
460460
#define X86_FEATURE_SRSO_NO (20*32+29) /* "" CPU is not affected by SRSO */
461461

462+
/*
463+
* Extended auxiliary flags: Linux defined - for features scattered in various
464+
* CPUID levels like 0x80000022, etc.
465+
*
466+
* Reuse free bits when adding new feature flags!
467+
*/
468+
#define X86_FEATURE_AMD_LBR_PMC_FREEZE (21*32+ 0) /* AMD LBR and PMC Freeze */
469+
462470
/*
463471
* BUG word(s)
464472
*/

arch/x86/kernel/cpu/scattered.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static const struct cpuid_bit cpuid_bits[] = {
4949
{ X86_FEATURE_BMEC, CPUID_EBX, 3, 0x80000020, 0 },
5050
{ X86_FEATURE_PERFMON_V2, CPUID_EAX, 0, 0x80000022, 0 },
5151
{ X86_FEATURE_AMD_LBR_V2, CPUID_EAX, 1, 0x80000022, 0 },
52+
{ X86_FEATURE_AMD_LBR_PMC_FREEZE, CPUID_EAX, 2, 0x80000022, 0 },
5253
{ 0, 0, 0, 0, 0 }
5354
};
5455

0 commit comments

Comments
 (0)