Skip to content

Commit a2c0b0f

Browse files
jgoulywilldeacon
authored andcommitted
arm64: alternatives: mark patch_alternative() as noinstr
The alternatives code must be `noinstr` such that it does not patch itself, as the cache invalidation is only performed after all the alternatives have been applied. Mark patch_alternative() as `noinstr`. Mark branch_insn_requires_update() and get_alt_insn() with `__always_inline` since they are both only called through patch_alternative(). Booting a kernel in QEMU TCG with KCSAN=y and ARM64_USE_LSE_ATOMICS=y caused a boot hang: [ 0.241121] CPU: All CPU(s) started at EL2 The alternatives code was patching the atomics in __tsan_read4() from LL/SC atomics to LSE atomics. The following fragment is using LL/SC atomics in the .text section: | <__tsan_unaligned_read4+304>: ldxr x6, [x2] | <__tsan_unaligned_read4+308>: add x6, x6, x5 | <__tsan_unaligned_read4+312>: stxr w7, x6, [x2] | <__tsan_unaligned_read4+316>: cbnz w7, <__tsan_unaligned_read4+304> This LL/SC atomic sequence was to be replaced with LSE atomics. However since the alternatives code was instrumentable, __tsan_read4() was being called after only the first instruction was replaced, which led to the following code in memory: | <__tsan_unaligned_read4+304>: ldadd x5, x6, [x2] | <__tsan_unaligned_read4+308>: add x6, x6, x5 | <__tsan_unaligned_read4+312>: stxr w7, x6, [x2] | <__tsan_unaligned_read4+316>: cbnz w7, <__tsan_unaligned_read4+304> This caused an infinite loop as the `stxr` instruction never completed successfully, so `w7` was always 0. Signed-off-by: Joey Gouly <joey.gouly@arm.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will@kernel.org> Link: https://lore.kernel.org/r/20220405104733.11476-1-joey.gouly@arm.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent 1d8e926 commit a2c0b0f

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

arch/arm64/kernel/alternative.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ bool alternative_is_applied(u16 cpufeature)
4242
/*
4343
* Check if the target PC is within an alternative block.
4444
*/
45-
static bool branch_insn_requires_update(struct alt_instr *alt, unsigned long pc)
45+
static __always_inline bool branch_insn_requires_update(struct alt_instr *alt, unsigned long pc)
4646
{
4747
unsigned long replptr = (unsigned long)ALT_REPL_PTR(alt);
4848
return !(pc >= replptr && pc <= (replptr + alt->alt_len));
4949
}
5050

5151
#define align_down(x, a) ((unsigned long)(x) & ~(((unsigned long)(a)) - 1))
5252

53-
static u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, __le32 *altinsnptr)
53+
static __always_inline u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, __le32 *altinsnptr)
5454
{
5555
u32 insn;
5656

@@ -95,7 +95,7 @@ static u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, __le32 *altinsnp
9595
return insn;
9696
}
9797

98-
static void patch_alternative(struct alt_instr *alt,
98+
static noinstr void patch_alternative(struct alt_instr *alt,
9999
__le32 *origptr, __le32 *updptr, int nr_inst)
100100
{
101101
__le32 *replptr;

0 commit comments

Comments
 (0)