RISC-V: vDSO: Correct inline assembly constraints in the getrandom syscall wrapper
authorXi Ruoyao <xry111@xry111.site>
Fri, 6 Jun 2025 09:24:44 +0000 (17:24 +0800)
committerPalmer Dabbelt <palmer@dabbelt.com>
Thu, 12 Jun 2025 19:13:57 +0000 (12:13 -0700)
As recently pointed out by Thomas, if a register is forced for two
different register variables, among them one is used as "+" (both input
and output) and another is only used as input, Clang would treat the
conflicting input parameters as undefined behaviour and optimize away
the argument assignment.

Instead use "=r" (only output) for the output parameter and "r" (only
input) for the input parameter.
While the example from the GCC documentation uses "0" for the input
parameter, this is not necessary as confirmed by the GCC developers and "r"
matches what the other architectures' vDSO implementations are using.

[ alex: Update log to match v2 (Thomas) ]

Link: https://lore.kernel.org/all/20250603-loongarch-vdso-syscall-v1-1-6d12d6dfbdd0@linutronix.de/
Link: https://gcc.gnu.org/onlinedocs/gcc-15.1.0/gcc/Local-Register-Variables.html
Link: https://gcc.gnu.org/pipermail/gcc-help/2025-June/144266.html
Cc: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Cc: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Reviewed-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Fixes: ee0d03053e70 ("RISC-V: vDSO: Wire up getrandom() vDSO")
Link: https://lore.kernel.org/r/20250606092443.73650-2-xry111@xry111.site
Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Palmer Dabbelt <palmer@dabbelt.com>
arch/riscv/include/asm/vdso/getrandom.h

index 8dc92441702a9391a4dd1363ecb14e8bb129cf35..c6d66895c1f585c7b8e670bc206049b985c20ab1 100644 (file)
@@ -18,7 +18,7 @@ static __always_inline ssize_t getrandom_syscall(void *_buffer, size_t _len, uns
        register unsigned int flags asm("a2") = _flags;
 
        asm volatile ("ecall\n"
-                     : "+r" (ret)
+                     : "=r" (ret)
                      : "r" (nr), "r" (buffer), "r" (len), "r" (flags)
                      : "memory");