]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
riscv: Use 64-bit variable for output in __get_user_asm
authorNathan Chancellor <nathan@kernel.org>
Fri, 16 Jan 2026 22:44:34 +0000 (15:44 -0700)
committerPaul Walmsley <pjw@kernel.org>
Sat, 17 Jan 2026 01:05:47 +0000 (18:05 -0700)
After commit f6bff7827a48 ("riscv: uaccess: use 'asm_goto_output' for
get_user()"), which was the first commit that started using asm goto
with outputs on RISC-V, builds of clang built with assertions enabled
start crashing in certain files that use get_user() with:

  clang: llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:12743: Register FollowCopyChain(MachineRegisterInfo &, Register): Assertion `MI->getOpcode() == TargetOpcode::COPY && "start of copy chain MUST be COPY"' failed.

Internally, LLVM generates an addiw instruction when the output of the
inline asm (which may be any scalar type) needs to be sign extended for
ABI reasons, such as a later function call, so that basic block does not
have to do it.

Use a temporary 64-bit variable as the output of the inline assembly in
__get_user_asm() and explicitly cast it to truncate it if necessary,
avoiding the addiw that triggers the assertion.

Link: https://github.com/ClangBuiltLinux/linux/issues/2092
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Link: https://patch.msgid.link/20260116-riscv-wa-llvm-asm-goto-outputs-assertion-failure-v3-1-55b5775f989b@kernel.org
Signed-off-by: Paul Walmsley <pjw@kernel.org>
arch/riscv/include/asm/uaccess.h

index 36bba6720c26ae963c9dc4571285fb28b0727239..53a643c0f6feeb1bcb4bbb1d19d44e5bfd68805a 100644 (file)
@@ -97,13 +97,23 @@ static inline unsigned long __untagged_addr_remote(struct mm_struct *mm, unsigne
  */
 
 #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
+/*
+ * Use a temporary variable for the output of the asm goto to avoid a
+ * triggering an LLVM assertion due to sign extending the output when
+ * it is used in later function calls:
+ * https://github.com/llvm/llvm-project/issues/143795
+ */
 #define __get_user_asm(insn, x, ptr, label)                    \
+do {                                                           \
+       u64 __tmp;                                              \
        asm_goto_output(                                        \
                "1:\n"                                          \
                "       " insn " %0, %1\n"                      \
                _ASM_EXTABLE_UACCESS_ERR(1b, %l2, %0)           \
-               : "=&r" (x)                                     \
-               : "m" (*(ptr)) : : label)
+               : "=&r" (__tmp)                                 \
+               : "m" (*(ptr)) : : label);                      \
+       (x) = (__typeof__(x))__tmp;                             \
+} while (0)
 #else /* !CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
 #define __get_user_asm(insn, x, ptr, label)                    \
 do {                                                           \