Skip to content

Commit f5962ad

Browse files
mrutland-armwilldeacon
authored andcommitted
arm64: rework EL0 MRS emulation
On CPUs without FEAT_IDST, ID register emulation is slower than it needs to be, as all threads contend for the same lock to perform the emulation. This patch reworks the emulation to avoid this unnecessary contention. On CPUs with FEAT_IDST (which is mandatory from ARMv8.4 onwards), EL0 accesses to ID registers result in a SYS trap, and emulation of these is handled with a sys64_hook. These hooks are statically allocated, and no locking is required to iterate through the hooks and perform the emulation, allowing emulation to occur in parallel with no contention. On CPUs without FEAT_IDST, EL0 accesses to ID registers result in an UNDEFINED exception, and emulation of these accesses is handled with an undef_hook. When an EL0 MRS instruction is trapped to EL1, the kernel finds the relevant handler by iterating through all of the undef_hooks, requiring undef_lock to be held during this lookup. This locking is only required to safely traverse the list of undef_hooks (as it can be concurrently modified), and the actual emulation of the MRS does not require any mutual exclusion. This locking is an unfortunate bottleneck, especially given that MRS emulation is enabled unconditionally and is never disabled. This patch reworks the non-FEAT_IDST MRS emulation logic so that it can be invoked directly from do_el0_undef(). This removes the bottleneck, allowing MRS traps to be handled entirely in parallel, and is a stepping stone to making all of the undef_hooks lock-free. I've tested this in a 64-vCPU VM on a 64-CPU ThunderX2 host, with a benchmark which spawns a number of threads which each try to read ID_AA64ISAR0_EL1 1000000 times. This is vastly more contention than will ever be seen in realistic usage, but clearly demonstrates the removal of the bottleneck: | Threads || Time (seconds) | | || Before || After | | || Real | System || Real | System | |---------++--------+---------++--------+---------| | 1 || 0.29 | 0.20 || 0.24 | 0.12 | | 2 || 0.35 | 0.51 || 0.23 | 0.27 | | 4 || 1.08 | 3.87 || 0.24 | 0.56 | | 8 || 4.31 | 33.60 || 0.24 | 1.11 | | 16 || 9.47 | 149.39 || 0.23 | 2.15 | | 32 || 19.07 | 605.27 || 0.24 | 4.38 | | 64 || 65.40 | 3609.09 || 0.33 | 11.27 | Aside from the speedup, there should be no functional change as a result of this patch. Signed-off-by: Mark Rutland <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: James Morse <[email protected]> Cc: Joey Gouly <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Will Deacon <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent dbfbd87 commit f5962ad

File tree

3 files changed

+10
-19
lines changed

3 files changed

+10
-19
lines changed

arch/arm64/include/asm/cpufeature.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,8 @@ static inline bool system_supports_tlb_range(void)
832832
cpus_have_const_cap(ARM64_HAS_TLB_RANGE);
833833
}
834834

835-
extern int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt);
835+
int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt);
836+
bool try_emulate_mrs(struct pt_regs *regs, u32 isn);
836837

837838
static inline u32 id_aa64mmfr0_parange_to_phys_shift(int parange)
838839
{

arch/arm64/kernel/cpufeature.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3435,35 +3435,22 @@ int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt)
34353435
return rc;
34363436
}
34373437

3438-
static int emulate_mrs(struct pt_regs *regs, u32 insn)
3438+
bool try_emulate_mrs(struct pt_regs *regs, u32 insn)
34393439
{
34403440
u32 sys_reg, rt;
34413441

3442+
if (compat_user_mode(regs) || !aarch64_insn_is_mrs(insn))
3443+
return false;
3444+
34423445
/*
34433446
* sys_reg values are defined as used in mrs/msr instruction.
34443447
* shift the imm value to get the encoding.
34453448
*/
34463449
sys_reg = (u32)aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn) << 5;
34473450
rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
3448-
return do_emulate_mrs(regs, sys_reg, rt);
3451+
return do_emulate_mrs(regs, sys_reg, rt) == 0;
34493452
}
34503453

3451-
static struct undef_hook mrs_hook = {
3452-
.instr_mask = 0xffff0000,
3453-
.instr_val = 0xd5380000,
3454-
.pstate_mask = PSR_AA32_MODE_MASK,
3455-
.pstate_val = PSR_MODE_EL0t,
3456-
.fn = emulate_mrs,
3457-
};
3458-
3459-
static int __init enable_mrs_emulation(void)
3460-
{
3461-
register_undef_hook(&mrs_hook);
3462-
return 0;
3463-
}
3464-
3465-
core_initcall(enable_mrs_emulation);
3466-
34673454
enum mitigation_state arm64_get_meltdown_state(void)
34683455
{
34693456
if (__meltdown_safe)

arch/arm64/kernel/traps.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ void do_el0_undef(struct pt_regs *regs, unsigned long esr)
499499
if (user_insn_read(regs, &insn))
500500
goto out_err;
501501

502+
if (try_emulate_mrs(regs, insn))
503+
return;
504+
502505
if (call_undef_hook(regs, insn) == 0)
503506
return;
504507

0 commit comments

Comments
 (0)