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

ARM: 9176/1: avoid literal references in inline assembly

Nathan reports that the new get_current() and per-CPU offset accessors
may cause problems at build time due to the use of a literal to hold the
address of the respective variables. This is due to the fact that LLD
before v14 does not support the PC-relative group relocations that are
normally used for this, and the fallback relies on literals but does not
emit the literal pools explictly using the .ltorg directive.

./arch/arm/include/asm/current.h:53:6: error: out of range pc-relative fixup value
asm(LOAD_SYM_ARMV6(%0, __current) : "=r"(cur));
^
./arch/arm/include/asm/insn.h:25:2: note: expanded from macro 'LOAD_SYM_ARMV6'
" ldr " #reg ", =" #sym " nt"
^
<inline asm>:1:3: note: instantiated into assembly here
ldr r0, =__current
^

Since emitting a literal pool in this particular case is not possible,
let's avoid the LOAD_SYM_ARMV6() entirely, and use the ordinary C
assigment instead.

As it turns out, there are other such cases, and here, using .ltorg to
emit the literal pool within range of the LDR instruction would be
possible due to the presence of an unconditional branch right after it.
Unfortunately, putting .ltorg directives in subsections appears to
confuse the Clang inline assembler, resulting in similar errors even
though the .ltorg is most definitely within range.

So let's fix this by emitting the literal explicitly, and not rely on
the assembler to figure this out. This means we have move the fallback
out of the LOAD_SYM_ARMV6() macro and into the callers.

Link: https://github.com/ClangBuiltLinux/linux/issues/1551

Fixes: 9c46929e7989 ("ARM: implement THREAD_INFO_IN_TASK for uniprocessor systems")
Reported-by: Nathan Chancellor <natechancellor@gmail.com>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>

authored by

Ard Biesheuvel and committed by
Russell King (Oracle)
5fe41793 23d9a928

+19 -9
+11 -2
arch/arm/include/asm/current.h
··· 37 37 #ifdef CONFIG_CPU_V6 38 38 "1: \n\t" 39 39 " .subsection 1 \n\t" 40 + #if !(defined(MODULE) && defined(CONFIG_ARM_MODULE_PLTS)) && \ 41 + !(defined(CONFIG_LD_IS_LLD) && CONFIG_LLD_VERSION < 140000) 40 42 "2: " LOAD_SYM_ARMV6(%0, __current) " \n\t" 41 43 " b 1b \n\t" 44 + #else 45 + "2: ldr %0, 3f \n\t" 46 + " ldr %0, [%0] \n\t" 47 + " b 1b \n\t" 48 + "3: .long __current \n\t" 49 + #endif 42 50 " .previous \n\t" 43 51 " .pushsection \".alt.smp.init\", \"a\" \n\t" 44 52 " .long 0b - . \n\t" ··· 54 46 " .popsection \n\t" 55 47 #endif 56 48 : "=r"(cur)); 57 - #elif __LINUX_ARM_ARCH__>=7 || \ 58 - (defined(MODULE) && defined(CONFIG_ARM_MODULE_PLTS)) 49 + #elif __LINUX_ARM_ARCH__>= 7 || \ 50 + (defined(MODULE) && defined(CONFIG_ARM_MODULE_PLTS)) || \ 51 + (defined(CONFIG_LD_IS_LLD) && CONFIG_LLD_VERSION < 140000) 59 52 cur = __current; 60 53 #else 61 54 asm(LOAD_SYM_ARMV6(%0, __current) : "=r"(cur));
-7
arch/arm/include/asm/insn.h
··· 10 10 * which should be sufficient for the core kernel as well as modules loaded 11 11 * into the module region. (Not supported by LLD before release 14) 12 12 */ 13 - #if !(defined(MODULE) && defined(CONFIG_ARM_MODULE_PLTS)) && \ 14 - !(defined(CONFIG_LD_IS_LLD) && CONFIG_LLD_VERSION < 140000) 15 13 #define LOAD_SYM_ARMV6(reg, sym) \ 16 14 " .globl " #sym " \n\t" \ 17 15 " .reloc 10f, R_ARM_ALU_PC_G0_NC, " #sym " \n\t" \ ··· 18 20 "10: sub " #reg ", pc, #8 \n\t" \ 19 21 "11: sub " #reg ", " #reg ", #4 \n\t" \ 20 22 "12: ldr " #reg ", [" #reg ", #0] \n\t" 21 - #else 22 - #define LOAD_SYM_ARMV6(reg, sym) \ 23 - " ldr " #reg ", =" #sym " \n\t" \ 24 - " ldr " #reg ", [" #reg "] \n\t" 25 - #endif 26 23 27 24 static inline unsigned long 28 25 arm_gen_nop(void)
+8
arch/arm/include/asm/percpu.h
··· 38 38 #ifdef CONFIG_CPU_V6 39 39 "1: \n\t" 40 40 " .subsection 1 \n\t" 41 + #if !(defined(MODULE) && defined(CONFIG_ARM_MODULE_PLTS)) && \ 42 + !(defined(CONFIG_LD_IS_LLD) && CONFIG_LLD_VERSION < 140000) 41 43 "2: " LOAD_SYM_ARMV6(%0, __per_cpu_offset) " \n\t" 42 44 " b 1b \n\t" 45 + #else 46 + "2: ldr %0, 3f \n\t" 47 + " ldr %0, [%0] \n\t" 48 + " b 1b \n\t" 49 + "3: .long __per_cpu_offset \n\t" 50 + #endif 43 51 " .previous \n\t" 44 52 " .pushsection \".alt.smp.init\", \"a\" \n\t" 45 53 " .long 0b - . \n\t"