riscv: vdso: Implement __vdso_futex_robust_try_unlock()
cover riscv: vdso: Implement __vdso_futex_robust_try_unlock()
This series adds new vdso futex entries, allowing user to address the robust futex race condition regarding clearing the robust_list_head::list_op_pending pointer.
Thomas wrote a summary of the problem:
https://lore.kernel.org/lkml/[email protected]/
The core infrastructure has been implemented along with x86 support. This series adds RISC-V support.
The first 4 patches prepare compat vdso Makefile. The final patch is the main show.
Nam Cao (5):
riscv: compat_vdso: switch to standard kbuild rule
riscv: compat_vdso: Allow *.c source files
riscv: compat_vdso: Introduce COMPAT_MARCH variable
riscv: compat_vdso: Build with zacas if available
riscv: vdso: Implement __vdso_futex_robust_try_unlock()
arch/riscv/Kconfig | 1 + arch/riscv/include/asm/cpufeature-macros.h | 2 + arch/riscv/include/asm/futex_robust.h | 16 +++++ arch/riscv/include/asm/vdso/futex.h | 9 +++ arch/riscv/kernel/compat_vdso/Makefile | 26 ++++--- arch/riscv/kernel/compat_vdso/futex.c | 3 + arch/riscv/kernel/vdso.c | 56 +++++++++++++++ arch/riscv/kernel/vdso/Makefile | 14 +++- arch/riscv/kernel/vdso/futex.c | 84 ++++++++++++++++++++++ arch/riscv/kernel/vdso/vdso.lds.S | 8 +++ 10 files changed, 209 insertions(+), 10 deletions(-) create mode 100644 arch/riscv/include/asm/futex_robust.h create mode 100644 arch/riscv/include/asm/vdso/futex.h create mode 100644 arch/riscv/kernel/compat_vdso/futex.c create mode 100644 arch/riscv/kernel/vdso/futex.c
Signature · 1 line
2.47.3
PATCH 1/5 riscv: compat_vdso: switch to standard kbuild rule
The compat_vdso Makefile has a custom build rule for *.S files. This is unnecessary, and makes it hard to extend this Makefile to support building *.c files.
Switch to standard kbuild rule instead.
--- arch/riscv/kernel/compat_vdso/Makefile | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
arch/riscv/kernel/compat_vdso/Makefile +1 -6
@@ -43,8 +43,7 @@ $(obj)/compat_vdso.so.dbg: $(obj)/compat_vdso.lds $(obj-compat_vdso) FORCE LDFLAGS_compat_vdso.so.dbg = -shared -S -soname=linux-compat_vdso.so.1 \ --build-id=sha1 --hash-style=both --eh-frame-hdr -$(obj-compat_vdso): %.o: %.S FORCE - $(call if_changed_dep,compat_vdsoas) +$(obj-compat_vdso) : KBUILD_AFLAGS += $(COMPAT_CC_FLAGS) # strip rule for the .so file $(obj)/%.so: OBJCOPYFLAGS := -S
@@ -66,7 +65,3 @@ quiet_cmd_compat_vdsold = VDSOLD $@ cmd_compat_vdsold = $(COMPAT_LD) $(ld_flags) $(COMPAT_LD_FLAGS) -T $(filter-out FORCE,$^) -o [email protected] && \ $(OBJCOPY) $(patsubst %, -G __compat_vdso_%, $(compat_vdso-syms)) [email protected] $@ && \ rm [email protected] - -# actual build commands -quiet_cmd_compat_vdsoas = VDSOAS $@ - cmd_compat_vdsoas = $(COMPAT_CC) $(a_flags) $(COMPAT_CC_FLAGS) -c -o $@ $<
Signature · 1 line
2.47.3
PATCH 2/5 riscv: compat_vdso: Allow *.c source files
In reply to Nam CaoExtend to support building *.c files. This allows adding futex.c in a follow-up commit.
--- arch/riscv/kernel/compat_vdso/Makefile | 1 + 1 file changed, 1 insertion(+)
arch/riscv/kernel/compat_vdso/Makefile +1 -0
@@ -44,6 +44,7 @@ LDFLAGS_compat_vdso.so.dbg = -shared -S -soname=linux-compat_vdso.so.1 \ --build-id=sha1 --hash-style=both --eh-frame-hdr $(obj-compat_vdso) : KBUILD_AFLAGS += $(COMPAT_CC_FLAGS) +$(obj-compat_vdso) : KBUILD_CFLAGS += $(COMPAT_CC_FLAGS) # strip rule for the .so file $(obj)/%.so: OBJCOPYFLAGS := -S
Signature · 1 line
2.47.3
PATCH 3/5 riscv: compat_vdso: Introduce COMPAT_MARCH variable
In reply to Nam CaoThe -march option is inside COMPAT_CC_FLAGS, making it difficult to extend.
Introduce COMPAT_MARCH variable to allow extension. A follow-up commit will take advantage of this to add _zacas to -march.
--- arch/riscv/kernel/compat_vdso/Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
arch/riscv/kernel/compat_vdso/Makefile +5 -2
@@ -14,10 +14,13 @@ COMPAT_LD := $(LD) # binutils 2.35 does not support the zifencei extension, but in the ISA # spec 20191213, G stands for IMAFD_ZICSR_ZIFENCEI. ifdef CONFIG_TOOLCHAIN_NEEDS_EXPLICIT_ZICSR_ZIFENCEI - COMPAT_CC_FLAGS := -march=rv32g -mabi=ilp32 + COMPAT_MARCH := rv32g else - COMPAT_CC_FLAGS := -march=rv32imafd -mabi=ilp32 + COMPAT_MARCH := rv32imafd endif + +COMPAT_CC_FLAGS := -march=$(COMPAT_MARCH) -mabi=ilp32 + COMPAT_LD_FLAGS := -melf32lriscv # Disable attributes, as they're useless and break the build.
Signature · 1 line
2.47.3
PATCH 4/5 riscv: compat_vdso: Build with zacas if available
In reply to Nam CaoThe compat vdso Makefile does not enable zacas, disallowing source files from using zacas instructions. This is an obstacle to adding robust __vdso_futex_robust_list32_try_unlock() in a follow-up commit.
Build with zacas, if compiler supports it.
--- arch/riscv/kernel/compat_vdso/Makefile | 4 ++++ 1 file changed, 4 insertions(+)
arch/riscv/kernel/compat_vdso/Makefile +4 -0
@@ -19,6 +19,10 @@ else COMPAT_MARCH := rv32imafd endif +ifneq ($(call cc-option,-mabi=ilp32 -march=rv32ima_zacas),) +COMPAT_MARCH := $(COMPAT_MARCH)_zacas +endif + COMPAT_CC_FLAGS := -march=$(COMPAT_MARCH) -mabi=ilp32 COMPAT_LD_FLAGS := -melf32lriscv
Signature · 1 line
2.47.3
PATCH 5/5 riscv: vdso: Implement __vdso_futex_robust_try_unlock()
In reply to Nam CaoThis is the RISC-V port for __vdso_futex_robust_try_unlock(). It is based on the x86's implementation in commit 61cfc8e372d1 ("x86/vdso: Prepare for robust futex unlock support") and commit a2274cc0091e ("x86/vdso: Implement __vdso_futex_robust_try_unlock()").
When the FUTEX_ROBUST_UNLOCK mechanism is used for unlocking (PI-)futexes, the unlock sequence in userspace looks like this:
1) robust_list_set_op_pending(mutex);
2) robust_list_remove(mutex);
lval = gettid();
3) if (atomic_try_cmpxchg(&mutex->lock, lval, 0))
4) robust_list_clear_op_pending();
else
5) sys_futex(OP,...FUTEX_ROBUST_UNLOCK);
That still leaves a minimal race window between #3 and #4 where the mutex could be acquired by some other task which observes that it is the last user and:
1) unmaps the mutex memory
2) maps a different file, which ends up covering the same address
When then the original task exits before reaching #4 then the kernel robust list handling observes the pending op entry and tries to fix up user space.
In case that the newly mapped data contains the TID of the exiting thread at the address of the mutex/futex the kernel will set the owner died bit in that memory and therefore corrupt unrelated data.
Provide a VDSO function which exposes the critical section window in the VDSO symbol table. The resulting addresses are updated in the task's mm when the VDSO is (re)map()'ed.
The core code detects when a task was interrupted within the critical section and is about to deliver a signal. It then invokes an architecture specific function which determines whether the pending op pointer has to be cleared or not. The unlock assembly sequence on 64-bit is:
mv a5, a0 ; move LOCK to a5
.Lretry
lr.w a0, (a5) ; load *LOCK
bne a0, a1, 24 .Lend ; if (*LOCK != TID) then exit
sc.w.rl t0, zero, (a5) ; try changing *LOCK from TID to zero
.Lstart
bnez t0, .Lretry ; retry if store-conditional failed
sd zero, 0(a2) ; clear POP
.Lend
sext.w a0,a0
ret
So if the kernel sees that the user task is between .Lstart and .Lend, t0 can be checked to determine if the pending op pointer (POP) should be cleared.
When the kernel is built with ZACAS, another unlock assembly sequence is provided using ZACAS instructions. If the CPU supports ZACAS, that alternative sequence is used and the critical section is also changed to that.
There are two entry points to handle the different robust list pending op pointer sizes:
__vdso_futex_robust_list64_try_unlock()
__vdso_futex_robust_list32_try_unlock()The 32-bit VDSO and compat VDSO provides__vdso_futex_robust_list32_try_unlock().
Unlike x86, the RISC-V 64-bit VDSO provides only provides __vdso_futex_robust_list64_try_unlock(). Because RISC-V threads can only have one list_op_pending pointer type.
--- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/cpufeature-macros.h | 2 + arch/riscv/include/asm/futex_robust.h | 16 +++++ arch/riscv/include/asm/vdso/futex.h | 9 +++ arch/riscv/kernel/compat_vdso/Makefile | 7 +- arch/riscv/kernel/compat_vdso/futex.c | 3 + arch/riscv/kernel/vdso.c | 56 +++++++++++++++ arch/riscv/kernel/vdso/Makefile | 14 +++- arch/riscv/kernel/vdso/futex.c | 84 ++++++++++++++++++++++ arch/riscv/kernel/vdso/vdso.lds.S | 8 +++ 10 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/include/asm/futex_robust.h create mode 100644 arch/riscv/include/asm/vdso/futex.h create mode 100644 arch/riscv/kernel/compat_vdso/futex.c create mode 100644 arch/riscv/kernel/vdso/futex.c
arch/riscv/Kconfig +1 -0
@@ -163,6 +163,7 @@ config RISCV select HAVE_FUNCTION_GRAPH_TRACER if HAVE_DYNAMIC_FTRACE_WITH_ARGS select HAVE_FUNCTION_GRAPH_FREGS select HAVE_FUNCTION_TRACER if HAVE_DYNAMIC_FTRACE + select HAVE_FUTEX_ROBUST_UNLOCK select HAVE_EBPF_JIT if MMU select HAVE_GENERIC_TIF_BITS select HAVE_GUP_FAST if MMU
arch/riscv/include/asm/cpufeature-macros.h +2 -0
@@ -6,6 +6,8 @@#ifndef _ASM_CPUFEATURE_MACROS_H#define _ASM_CPUFEATURE_MACROS_H +#include <linux/types.h> +#include <asm/hwcap.h> #include <asm/alternative-macros.h>
arch/riscv/include/asm/futex_robust.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_RISCV_FUTEX_ROBUST_H +#define _ASM_RISCV_FUTEX_ROBUST_H + +#include <asm/ptrace.h> +#include <asm/vdso/futex.h> + +static inline void __user *arch_futex_robust_unlock_get_pop (struct pt_regs *regs) +{ +if (cpu_supports_zacas ()) +return (regs->a0 == regs->a1) ? (void __user *)regs->a2 :NULL ; +else +return (regs->t0 ==0 ) ? (void __user *)regs->a2 :NULL ; +} + +#endif /* _ASM_RISCV_FUTEX_ROBUST_H */
arch/riscv/include/asm/vdso/futex.h new file +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include <asm/cpufeature-macros.h> + +static inline bool cpu_supports_zacas (void ) +{ +return IS_ENABLED (CONFIG_RISCV_ISA_ZACAS ) &&IS_ENABLED (CONFIG_TOOLCHAIN_HAS_ZACAS ) && +riscv_has_extension_unlikely (RISCV_ISA_EXT_ZACAS ); +}
arch/riscv/kernel/compat_vdso/Makefile +6 -1
@@ -32,7 +32,12 @@ COMPAT_CC_FLAGS += $(call cc-option,-mno-riscv-attribute) COMPAT_CC_FLAGS += $(call as-option,-Wa$(comma)-mno-arch-attr) # Files to link into the compat_vdso -obj-compat_vdso = $(patsubst %, %.o, $(compat_vdso-syms)) note.o +obj-compat_vdso := $(patsubst %, %.o, $(compat_vdso-syms)) note.o + +ifdef CONFIG_FUTEX_ROBUST_UNLOCK +compat_vdso-syms += __vdso_futex_robust_list32_try_unlock +obj-compat_vdso += futex.o +endif # Build rules targets := $(obj-compat_vdso) compat_vdso.so compat_vdso.so.dbg compat_vdso.lds
arch/riscv/kernel/compat_vdso/futex.c new file +3 -0
@@ -0,0 +1,3 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "../vdso/futex.c"
arch/riscv/kernel/vdso.c +56 -0
79 lines not on this page · raw message
arch/riscv/kernel/vdso/Makefile +13 -1
@@ -27,12 +27,24 @@ asflags-y += -DVDSO_CFI=1 endif # Files to link into the vdso -obj-vdso = $(patsubst %, %.o, $(vdso-syms)) note.o +obj-vdso := $(patsubst %, %.o, $(vdso-syms)) note.o ifdef CONFIG_VDSO_GETRANDOM obj-vdso += vgetrandom-chacha.o endif +ifdef CONFIG_FUTEX_ROBUST_UNLOCK + +obj-vdso += futex.o + +ifdef CONFIG_64BIT +vdso-syms += __vdso_futex_robust_list64_try_unlock +else +vdso-syms += __vdso_futex_robust_list32_try_unlock +endif + +endif + ccflags-y := -fno-stack-protector ccflags-y += -DDISABLE_BRANCH_PROFILING ccflags-y += -fno-builtin
arch/riscv/kernel/vdso/futex.c new file +84 -0
84 lines not on this page · raw message
arch/riscv/kernel/vdso/vdso.lds.S +8 -0
@@ -82,6 +82,14 @@ VERSION #endif #if defined(CONFIG_VDSO_GETRANDOM) && !defined(COMPAT_VDSO) __vdso_getrandom; +#endif + +#ifdef CONFIG_FUTEX_ROBUST_UNLOCK +#if defined(CONFIG_32BIT) || defined(COMPAT_VDSO) + __vdso_futex_robust_list32_try_unlock; +#else + __vdso_futex_robust_list64_try_unlock; +#endif #endif local: *; };
Signature · 1 line
2.47.3
On Fri, Jun 19, 2026 at 04:11:21PM +0200, Nam Cao wrote:
(...)
Quoted 18 lines
arch/riscv/kernel/vdso/Makefile +6
-1@@ -27,12 +27,24 @@ asflags-y += -DVDSO_CFI=1 endif # Files to link into the vdso -obj-vdso = $(patsubst %, %.o, $(vdso-syms)) note.o +obj-vdso := $(patsubst %, %.o, $(vdso-syms)) note.o ifdef CONFIG_VDSO_GETRANDOM obj-vdso += vgetrandom-chacha.o endif +ifdef CONFIG_FUTEX_ROBUST_UNLOCK + +obj-vdso += futex.o + +ifdef CONFIG_64BIT
arch/riscv/kernel/vdso/Makefile asflags-y += -DVDSO_CFI=1 · line 50
Quoted 10 lines
arch/riscv/kernel/vdso/Makefile +7
-0+vdso-syms += __vdso_futex_robust_list64_try_unlock +else +vdso-syms += __vdso_futex_robust_list32_try_unlock +endif + +endif + ccflags-y := -fno-stack-protector ccflags-y += -DDISABLE_BRANCH_PROFILING ccflags-y += -fno-builtin
This is missing a
CFLAGS_REMOVE_futex.o = $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS)
Otherwise we get this:
https://lore.kernel.org/oe-kbuild-all/[email protected]/
Or add a preparatory commit which uses ccflags-remove-y instead of listing all the objects manually.
(...)
Hi Nam Cao, thanks for your patch!
Em 19/06/2026 11:11, Nam Cao escreveu:
Quoted 5 lines
This is the RISC-V port for
__vdso_futex_robust_try_unlock(). It is based on the x86's implementation in commit61cfc8e372d1("x86/vdso: Prepare for robust futex unlock support") and commita2274cc0091e("x86/vdso: Implement__vdso_futex_robust_try_unlock()").
[...]
Quoted 3 lines
Signed-off-by Nam Cao <[email protected]>---
How have you tested it?
[...]
arch/riscv/include/asm/vdso/futex.h line 2
Quoted 8 lines
arch/riscv/include/asm/vdso/futex.h new file +2
-0@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +
I believe this is missing the ifdef header guard
#ifndef __ASM_VDSO_FUTEX_H
#define __ASM_VDSO_FUTEX_H
[...]
Quoted 13 lines
arch/riscv/include/asm/vdso/futex.h new file +13
-0+#if defined(CONFIG_RISCV_ISA_ZACAS ) && defined(CONFIG_TOOLCHAIN_HAS_ZACAS ) +#define FUTEX_CAS_OVERWRITE_VDSO_CS_RANGE (vdso, fd, idx, xlen, symbol) \ +{ \ + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZACAS)) { \ + void *start = symbol(vdso, CONCAT3(futex_list, xlen, _try_unlock_cs_cas_start));\ + void *end = symbol(vdso, CONCAT3(futex_list, xlen, _try_unlock_cs_cas_end)); \ + \ + futex_set_vdso_cs_range(fd, idx, (uintptr_t)start, (uintptr_t)end, xlen == 32); \ + } \ +} +#else +#define FUTEX_CAS_OVERWRITE_VDSO_CS_RANGE (...) +#endif
arch/riscv/include/asm/vdso/futex.h
Quoted 10 lines
arch/riscv/include/asm/vdso/futex.h new file +10
-0+ +#define FUTEX_SET_VDSO_CS_RANGE (vdso, fd, idx, xlen, symbol) \ +{ \ + void *start = symbol(vdso, CONCAT3(futex_list, xlen, _try_unlock_cs_lrsc_start)); \ + void *end = symbol(vdso, CONCAT3(futex_list, xlen, _try_unlock_cs_lrsc_end)); \ + \ + futex_set_vdso_cs_range(fd, idx, (uintptr_t)start, (uintptr_t)end, xlen == 32); \ + \ + FUTEX_CAS_OVERWRITE_VDSO_CS_RANGE(vdso, fd, idx, xlen, symbol); \ + \
So if the build has support for CAS, the function overwrite what it had just set. Why do it even writes it in the first place then, can't this be an if/else?
arch/riscv/include/asm/vdso/futex.h
Quoted 2 lines
arch/riscv/include/asm/vdso/futex.h new file +2
-0+} +
Thanks,
André
Quoted 1 line · André Almeida
Hi Nam Cao, thanks for your patch!
Hi,
Quoted 1 line
How have you tested it?
With a gdb breakpoint in the critical session.
Quoted 4 lines
I believe this is missing the ifdef header guard
#ifndef
__ASM_VDSO_FUTEX_H
#define__ASM_VDSO_FUTEX_H
Right, thanks!
Quoted 19 lines
Quoted 19 lines
arch/riscv/kernel/vdso.c +19
-0+#if defined(CONFIG_RISCV_ISA_ZACAS ) && defined(CONFIG_TOOLCHAIN_HAS_ZACAS ) +#define FUTEX_CAS_OVERWRITE_VDSO_CS_RANGE (vdso, fd, idx, xlen, symbol) \ +{ \ + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZACAS)) { \ + void *start = symbol(vdso, CONCAT3(futex_list, xlen, _try_unlock_cs_cas_start));\ + void *end = symbol(vdso, CONCAT3(futex_list, xlen, _try_unlock_cs_cas_end)); \ + \ + futex_set_vdso_cs_range(fd, idx, (uintptr_t)start, (uintptr_t)end, xlen == 32); \ + } \ +} +#else +#define FUTEX_CAS_OVERWRITE_VDSO_CS_RANGE (...) +#endif + +#define FUTEX_SET_VDSO_CS_RANGE (vdso, fd, idx, xlen, symbol) \ +{ \ + void *start = symbol(vdso, CONCAT3(futex_list, xlen, _try_unlock_cs_lrsc_start)); \ + void *end = symbol(vdso, CONCAT3(futex_list, xlen, _try_unlock_cs_lrsc_end)); \ + \
Quoted 8 lines
arch/riscv/kernel/vdso.c static struct __vdso_info vdso_info; · line 62
Quoted 4 lines
arch/riscv/kernel/vdso.c +4
-0+futex_set_vdso_cs_range (fd, idx, (uintptr_t )start, (uintptr_t )end, xlen ==32 ); \ + \ +FUTEX_CAS_OVERWRITE_VDSO_CS_RANGE (vdso, fd, idx, xlen, symbol); \ + \So if the build has support for CAS, the function overwrite what it had just set. Why do it even writes it in the first place then, can't this be an if/else?
Functionally speaking, an if/else makes more sense. However, I cannot figure out how to do that without creating a complete mess, since *_try_unlock_cs_cas_start and *_try_unlock_cs_cas_end are not always available. If you have a suggestion, please do let me know. But doing it this way only costs us a few more instructions.
Nam
Quoted 10 lines · Nam Cao
Quoted 3 lines · André Almeida
So if the build has support for CAS, the function overwrite what it had just set. Why do it even writes it in the first place then, can't this be an if/else?
Functionally speaking, an if/else makes more sense. However, I cannot figure out how to do that without creating a complete mess, since
*_try_unlock_cs_cas_startand*_try_unlock_cs_cas_endare not always available. If you have a suggestion, please do let me know. But doing it this way only costs us a few more instructions.
Look at this again, we can do something like below. Let me send v3.
#if defined(CONFIG_RISCV_ISA_ZACAS) && defined(CONFIG_TOOLCHAIN_HAS_ZACAS)
#define FUTEX_CAS_SET_VDSO_CS_RANGE(vdso, fd, idx, xlen, symbol) \
{ \
void *start = symbol(vdso, CONCAT3(futex_list, xlen, _try_unlock_cs_cas_start)); \
void *end = symbol(vdso, CONCAT3(futex_list, xlen, _try_unlock_cs_cas_end)); \
\
futex_set_vdso_cs_range(fd, idx, (uintptr_t)start, (uintptr_t)end, xlen == 32); \
}#else
#define FUTEX_CAS_SET_VDSO_CS_RANGE(...) BUILD_BUG() #endif
#define FUTEX_LRSC_SET_VDSO_CS_RANGE(vdso, fd, idx, xlen, symbol) \
{ \
void *start = symbol(vdso, CONCAT3(futex_list, xlen, _try_unlock_cs_lrsc_start)); \
void *end = symbol(vdso, CONCAT3(futex_list, xlen, _try_unlock_cs_lrsc_end)); \
\
futex_set_vdso_cs_range(fd, idx, (uintptr_t)start, (uintptr_t)end, xlen == 32); \
}#define FUTEX_SET_VDSO_CS_RANGE(vdso, fd, idx, xlen, symbol) \
{ \
if (cpu_supports_zacas()) { \
FUTEX_CAS_SET_VDSO_CS_RANGE(vdso, fd, idx, xlen, symbol); \
} else { \
FUTEX_LRSC_SET_VDSO_CS_RANGE(vdso, fd, idx, xlen, symbol); \
} \}