Skip to content

Commit 7914695

Browse files
aikbp3tk0v
authored andcommitted
x86/amd: Cache debug register values in percpu variables
Reading DR[0-3]_ADDR_MASK MSRs takes about 250 cycles which is going to be noticeable with the AMD KVM SEV-ES DebugSwap feature enabled. KVM is going to store host's DR[0-3] and DR[0-3]_ADDR_MASK before switching to a guest; the hardware is going to swap these on VMRUN and VMEXIT. Store MSR values passed to set_dr_addr_mask() in percpu variables (when changed) and return them via new amd_get_dr_addr_mask(). The gain here is about 10x. As set_dr_addr_mask() uses the array too, change the @dr type to unsigned to avoid checking for <0. And give it the amd_ prefix to match the new helper as the whole DR_ADDR_MASK feature is AMD-specific anyway. While at it, replace deprecated boot_cpu_has() with cpu_feature_enabled() in set_dr_addr_mask(). Signed-off-by: Alexey Kardashevskiy <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 8c19b6f commit 7914695

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

arch/x86/include/asm/debugreg.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,14 @@ static __always_inline void local_db_restore(unsigned long dr7)
126126
}
127127

128128
#ifdef CONFIG_CPU_SUP_AMD
129-
extern void set_dr_addr_mask(unsigned long mask, int dr);
129+
extern void amd_set_dr_addr_mask(unsigned long mask, unsigned int dr);
130+
extern unsigned long amd_get_dr_addr_mask(unsigned int dr);
130131
#else
131-
static inline void set_dr_addr_mask(unsigned long mask, int dr) { }
132+
static inline void amd_set_dr_addr_mask(unsigned long mask, unsigned int dr) { }
133+
static inline unsigned long amd_get_dr_addr_mask(unsigned int dr)
134+
{
135+
return 0;
136+
}
132137
#endif
133138

134139
#endif /* _ASM_X86_DEBUGREG_H */

arch/x86/kernel/cpu/amd.c

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,24 +1158,43 @@ static bool cpu_has_amd_erratum(struct cpuinfo_x86 *cpu, const int *erratum)
11581158
return false;
11591159
}
11601160

1161-
void set_dr_addr_mask(unsigned long mask, int dr)
1161+
static DEFINE_PER_CPU_READ_MOSTLY(unsigned long[4], amd_dr_addr_mask);
1162+
1163+
static unsigned int amd_msr_dr_addr_masks[] = {
1164+
MSR_F16H_DR0_ADDR_MASK,
1165+
MSR_F16H_DR1_ADDR_MASK,
1166+
MSR_F16H_DR1_ADDR_MASK + 1,
1167+
MSR_F16H_DR1_ADDR_MASK + 2
1168+
};
1169+
1170+
void amd_set_dr_addr_mask(unsigned long mask, unsigned int dr)
11621171
{
1163-
if (!boot_cpu_has(X86_FEATURE_BPEXT))
1172+
int cpu = smp_processor_id();
1173+
1174+
if (!cpu_feature_enabled(X86_FEATURE_BPEXT))
11641175
return;
11651176

1166-
switch (dr) {
1167-
case 0:
1168-
wrmsr(MSR_F16H_DR0_ADDR_MASK, mask, 0);
1169-
break;
1170-
case 1:
1171-
case 2:
1172-
case 3:
1173-
wrmsr(MSR_F16H_DR1_ADDR_MASK - 1 + dr, mask, 0);
1174-
break;
1175-
default:
1176-
break;
1177-
}
1177+
if (WARN_ON_ONCE(dr >= ARRAY_SIZE(amd_msr_dr_addr_masks)))
1178+
return;
1179+
1180+
if (per_cpu(amd_dr_addr_mask, cpu)[dr] == mask)
1181+
return;
1182+
1183+
wrmsr(amd_msr_dr_addr_masks[dr], mask, 0);
1184+
per_cpu(amd_dr_addr_mask, cpu)[dr] = mask;
1185+
}
1186+
1187+
unsigned long amd_get_dr_addr_mask(unsigned int dr)
1188+
{
1189+
if (!cpu_feature_enabled(X86_FEATURE_BPEXT))
1190+
return 0;
1191+
1192+
if (WARN_ON_ONCE(dr >= ARRAY_SIZE(amd_msr_dr_addr_masks)))
1193+
return 0;
1194+
1195+
return per_cpu(amd_dr_addr_mask[dr], smp_processor_id());
11781196
}
1197+
EXPORT_SYMBOL_GPL(amd_get_dr_addr_mask);
11791198

11801199
u32 amd_get_highest_perf(void)
11811200
{

arch/x86/kernel/hw_breakpoint.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ int arch_install_hw_breakpoint(struct perf_event *bp)
127127

128128
set_debugreg(*dr7, 7);
129129
if (info->mask)
130-
set_dr_addr_mask(info->mask, i);
130+
amd_set_dr_addr_mask(info->mask, i);
131131

132132
return 0;
133133
}
@@ -166,7 +166,7 @@ void arch_uninstall_hw_breakpoint(struct perf_event *bp)
166166

167167
set_debugreg(dr7, 7);
168168
if (info->mask)
169-
set_dr_addr_mask(0, i);
169+
amd_set_dr_addr_mask(0, i);
170170

171171
/*
172172
* Ensure the write to cpu_dr7 is after we've set the DR7 register.

0 commit comments

Comments
 (0)