Skip to contentSkip to conversation

futex: Introduce __vdso_robust_futex_unlock_u32 and __vdso_robust_pi_futex_try_unlock_u32

RFC PATCH New Mathieu Desnoyers 2 participants 3 messages Mar 12 – Mar 13

Subsystems (6)
x86 x86 entry code Intel SGX Futex +2 more subsystems
lore
Share & tools
lore
#0 lore raw

RFC PATCH v3 1/1 futex: Introduce __vdso_robust_futex_unlock_u32 and __vdso_robust_pi_futex_try_unlock_u32

Fix a long standing data corruption race condition with robust futexes, as pointed out here:

"File corruption race condition in robust mutex unlocking"
https://sourceware.org/bugzilla/show_bug.cgi?id=14485

The __vdso_robust_futex_unlock_u32 vDSO unlocks the robust futex by exchanging the content of uaddr with val with a store-release semantic. If the futex has waiters, it sets bit 1 of *robust_list_head->list_op_pending, else it clears *robust_list_head->list_op_pending. Those operations are within a code region known by the kernel, making them safe with respect to asynchronous program termination either from thread context or from a nested signal handler.

Expected use of this vDSO:

if ((__vdso_robust_futex_unlock_u32((u32 *) &mutex->__data.__lock, 0, robust_list_head)

    & FUTEX_WAITERS) != 0)
        futex_wake((u32 *) &mutex->__data.__lock, 1, private);

WRITE_ONCE(robust_list_head->list_op_pending, 0);

Also introduce __vdso_robust_pi_futex_try_unlock_u32 to fix a similar unlock race with robust PI futexes.

The __vdso_robust_pi_futex_try_unlock_u32 vDSO try to perform a compare-and-exchange with release semantic to set the expected *uaddr content to val. If the futex has waiters, it fails, and userspace needs to call futex_unlock_pi(). Before exiting the critical section, if the cmpxchg fails, it sets bit 1 of *robust_list_head->list_op_pending. If the cmpxchg succeeds, it clears *@robust_list_head->list_op_pending. Those operations are within a code region known by the kernel, making them safe with respect to asynchronous program termination either from thread context or from a nested signal handler.

Expected use of this vDSO:

int l = atomic_load_relaxed(&mutex->__data.__lock);

do {
        if (((l & FUTEX_WAITERS) != 0) || (l != READ_ONCE(pd->tid))) {
                futex_unlock_pi((unsigned int *) &mutex->__data.__lock, private);
                break;
        }
} while (!__vdso_robust_pi_futex_try_unlock_u32(&mutex->__data.__lock,
                                                &l, 0, robust_list_head));

WRITE_ONCE(robust_list_head->list_op_pending, 0);

The approach taken by these vDSO is to extend the x86 vDSO exception table to track the relevant ip ranges. The four kernel execution paths impacted by this change are:

1) exit_robust_list/compat_exit_robust_list (process exit)
2) setup_rt_frame (signal delivery)
3) futex_wake
4) futex_unlock_pi

Bit 1 of the robust_list_head->list_op_pending pointer is used to flag whether there is either a pending wakeup or futex_unlock_pi action (FUTEX_UADDR_NEED_ACTION). This allows extending the "need action" state beyond the vDSO and lets the caller issue futex_wake and futex_unlock_pi system calls. This "need action" flag is cleared by the caller when zeroing robust_list_head->list_op_pending.

futex_wake now clears the robust_list_head->list_op_pending to close the race between call to futex_wake and clearing of the robust_list_head->list_op_pending by the application. This prevents multiple calls to futex_wake in case a crash happens within that window.

futex_unlock_pi now clears the robust_list_head->list_op_pending to close the race between call to futex_unlock_pi and clearing of the robust_list_head->list_op_pending by the application. This prevents multiple calls to futex_unlock_pi in case a crash happens within that window.

[ This patch is lightly compiled tested on x86-64 only, submitted for feedback.
It implements the vDSO for x86-32 and x86-64.
It is based on v7.0-rc3. ]

Cc "André Almeida" <[email protected]>
Cc Carlos O'Donell <[email protected]>
Cc Sebastian Andrzej Siewior <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Florian Weimer <[email protected]>
Cc Rich Felker <[email protected]>
Cc Torvald Riegel <[email protected]>
Cc Darren Hart <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc "Liam R . Howlett" <[email protected]>

---
Changes since v2:
- Pass robust_list_head as vdso argument.
- Add "val" parameter to each vdso.
- Add _u32 suffix to each vdso.
- Introduce ARCH_HAS_VDSO_FUTEX to provide a futex_vdso_exception stub when not implemented by the architecture.
- Wire up x86 vdso32 vfutex.o.

Changes since v1:
- Remove unlock_store_done leftover code from handle_futex_death.
- Handle robust PI futexes.

---
 arch/Kconfig                        |   3 +
 arch/x86/Kconfig                    |   1 +
 arch/x86/entry/vdso/common/vfutex.c |  88 +++++++++++++
 arch/x86/entry/vdso/extable.c       |  59 ++++++++-
 arch/x86/entry/vdso/extable.h       |  37 ++++--
 arch/x86/entry/vdso/vdso32/Makefile |   1 +
 arch/x86/entry/vdso/vdso32/vfutex.c |   1 +
 arch/x86/entry/vdso/vdso64/Makefile |   1 +
 arch/x86/entry/vdso/vdso64/vfutex.c |   1 +
 arch/x86/entry/vdso/vdso64/vsgx.S   |   2 +-
 arch/x86/include/asm/vdso.h         |   3 +
 arch/x86/kernel/signal.c            |   4 +
 include/linux/futex.h               |   1 +
 include/vdso/futex.h                |  74 +++++++++++
 kernel/futex/core.c                 | 188 ++++++++++++++++++++++++----
 kernel/futex/futex.h                |   2 +
 kernel/futex/pi.c                   |   3 +
 kernel/futex/waitwake.c             |   3 +
 18 files changed, 439 insertions(+), 33 deletions(-)
 create mode 100644 arch/x86/entry/vdso/common/vfutex.c
 create mode 100644 arch/x86/entry/vdso/vdso32/vfutex.c
 create mode 100644 arch/x86/entry/vdso/vdso64/vfutex.c
 create mode 100644 include/vdso/futex.h
arch/Kconfig +3 -0
index 102ddbd4298e..4f3e1be29af1 100644
@@ -1670,6 +1670,9 @@ config ARCH_HAS_VDSO_ARCH_DATA
 config ARCH_HAS_VDSO_TIME_DATA
 	bool
 
+config ARCH_HAS_VDSO_FUTEX
+	bool
+
 config HAVE_STATIC_CALL
 	bool
 
arch/x86/Kconfig +1 -0
index e2df1b147184..957d5d9209a1 100644
@@ -111,6 +111,7 @@ config X86
 	select ARCH_HAS_SYSCALL_WRAPPER
 	select ARCH_HAS_UBSAN
 	select ARCH_HAS_DEBUG_WX
+	select ARCH_HAS_VDSO_FUTEX
 	select ARCH_HAS_ZONE_DMA_SET if EXPERT
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG
 	select ARCH_HAVE_EXTRA_ELF_NOTES
arch/x86/entry/vdso/common/vfutex.c new file +88 -0

88 lines not on this page · raw message

arch/x86/entry/vdso/extable.c +56 -3

79 lines not on this page · raw message

arch/x86/entry/vdso/extable.h +30 -7
index baba612b832c..5dfbde724065 100644
@@ -8,21 +8,44 @@
  * exception table, not each individual entry.
  */
 #ifdef __ASSEMBLER__
-#define _ASM_VDSO_EXTABLE_HANDLE(from, to)	\
-	ASM_VDSO_EXTABLE_HANDLE from to
+#define _ASM_VDSO_EXTABLE_FIXUP_HANDLE(from, to)	\
+	ASM_VDSO_EXTABLE_FIXUP_HANDLE from to
 
-.macro ASM_VDSO_EXTABLE_HANDLE from:req to:req
+.macro ASM_VDSO_EXTABLE_FIXUP_HANDLE from:req to:req
 	.pushsection __ex_table, "a"
+	.long 0		/* type: fixup */
 	.long (\from) - __ex_table
 	.long (\to) - __ex_table
 	.popsection
 .endm
 #else
-#define _ASM_VDSO_EXTABLE_HANDLE(from, to)	\
-	".pushsection __ex_table, \"a\"\n"      \
-	".long (" #from ") - __ex_table\n"      \
-	".long (" #to ") - __ex_table\n"        \
+#define _ASM_VDSO_EXTABLE_FIXUP_HANDLE(from, to)	\
+	".pushsection __ex_table, \"a\"\n"		\
+	".long 0\n"	/* type: fixup */		\
+	".long (" #from ") - __ex_table\n"		\
+	".long (" #to ") - __ex_table\n"		\
 	".popsection\n"
+
+/*
+ * Identify robust futex unlock critical section.
+ */
+#define _ASM_VDSO_EXTABLE_FUTEX_HANDLE(start, end)	\
+	".pushsection __ex_table, \"a\"\n"		\
+	".long 1\n"	/* type: futex */		\
+	".long (" #start ") - __ex_table\n"		\
+	".long (" #end ") - __ex_table\n"		\
+	".popsection\n"
+
+/*
+ * Identify robust PI futex unlock critical section.
+ */
+#define _ASM_VDSO_EXTABLE_PI_FUTEX_HANDLE(start, end)	\
+	".pushsection __ex_table, \"a\"\n"		\
+	".long 2\n"	/* type: pi_futex */		\
+	".long (" #start ") - __ex_table\n"		\
+	".long (" #end ") - __ex_table\n"		\
+	".popsection\n"
+
 #endif
 
 #endif /* __VDSO_EXTABLE_H */
arch/x86/entry/vdso/vdso32/Makefile +1 -0
index add6afb484ba..acf4f990be98 100644
@@ -9,6 +9,7 @@ vdsos-y			:= 32
 # Files to link into the vDSO:
 vobjs-y			:= note.o vclock_gettime.o vgetcpu.o
 vobjs-y			+= system_call.o sigreturn.o
+vobjs-y			+= vfutex.o
 
 # Compilation flags
 flags-y			:= -DBUILD_VDSO32 -m32 -mregparm=0
arch/x86/entry/vdso/vdso32/vfutex.c new file +1 -0
new file mode 100644
index 000000000000..940a6ee30026
@@ -0,0 +1 @@
+#include "common/vfutex.c"
arch/x86/entry/vdso/vdso64/Makefile +1 -0
index bfffaf1aeecc..df53c2d0037d 100644
@@ -10,6 +10,7 @@ vdsos-$(CONFIG_X86_X32_ABI)	+= x32
 # Files to link into the vDSO:
 vobjs-y				:= note.o vclock_gettime.o vgetcpu.o
 vobjs-y				+= vgetrandom.o vgetrandom-chacha.o
+vobjs-y				+= vfutex.o
 vobjs-$(CONFIG_X86_SGX)		+= vsgx.o
 
 # Compilation flags
arch/x86/entry/vdso/vdso64/vfutex.c new file +1 -0
new file mode 100644
index 000000000000..940a6ee30026
@@ -0,0 +1 @@
+#include "common/vfutex.c"
arch/x86/entry/vdso/vdso64/vsgx.S +1 -1
index 37a3d4c02366..0ea5a1ebd455 100644
@@ -145,6 +145,6 @@ SYM_FUNC_START(__vdso_sgx_enter_enclave)
 
 	.cfi_endproc
 
-_ASM_VDSO_EXTABLE_HANDLE(.Lenclu_eenter_eresume, .Lhandle_exception)
+_ASM_VDSO_EXTABLE_FIXUP_HANDLE(.Lenclu_eenter_eresume, .Lhandle_exception)
 
 SYM_FUNC_END(__vdso_sgx_enter_enclave)
arch/x86/include/asm/vdso.h +3 -0
index e8afbe9faa5b..9ac7af34cdc4 100644
@@ -38,6 +38,9 @@ extern int map_vdso_once(const struct vdso_image *image, unsigned long addr);
 extern bool fixup_vdso_exception(struct pt_regs *regs, int trapnr,
 				 unsigned long error_code,
 				 unsigned long fault_addr);
+extern void futex_vdso_exception(struct pt_regs *regs,
+				 bool *in_futex_vdso,
+				 bool *need_action);
 #endif /* __ASSEMBLER__ */
 
 #endif /* _ASM_X86_VDSO_H */
arch/x86/kernel/signal.c +4 -0
index 2404233336ab..c2e4db89f16d 100644
@@ -28,6 +28,7 @@
 #include <linux/entry-common.h>
 #include <linux/syscalls.h>
 #include <linux/rseq.h>
+#include <linux/futex.h>
 
 #include <asm/processor.h>
 #include <asm/ucontext.h>
@@ -235,6 +236,9 @@ unsigned long get_sigframe_size(void)
 static int
 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
 {
+	/* Handle futex robust list fixup. */
+	futex_signal_deliver(ksig, regs);
+
 	/* Perform fixup for the pre-signal frame. */
 	rseq_signal_deliver(ksig, regs);
 
include/linux/futex.h +1 -0
index 9e9750f04980..6c274c79e176 100644
@@ -81,6 +81,7 @@ void futex_exec_release(struct task_struct *tsk);
 long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
 	      u32 __user *uaddr2, u32 val2, u32 val3);
 int futex_hash_prctl(unsigned long arg2, unsigned long arg3, unsigned long arg4);
+void futex_signal_deliver(struct ksignal *ksig, struct pt_regs *regs);
 
 #ifdef CONFIG_FUTEX_PRIVATE_HASH
 int futex_hash_allocate_default(void);
include/vdso/futex.h new file +74 -0

74 lines not on this page · raw message

kernel/futex/core.c +166 -22

311 lines not on this page · raw message

kernel/futex/futex.h +2 -0
index 30c2afa03889..f64ed00463ca 100644
@@ -396,6 +396,8 @@ double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
 		spin_unlock(&hb2->lock);
 }
 
+extern void clear_robust_list_pending_op(struct task_struct *curr);
+
 /* syscalls */
 
 extern int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags, u32
kernel/futex/pi.c +3 -0
index bc1f7e83a37e..3b889dfbcdd5 100644
@@ -1148,6 +1148,9 @@ int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
 	if ((uval & FUTEX_TID_MASK) != vpid)
 		return -EPERM;
 
+	/* Clear the pending_op_list. */
+	clear_robust_list_pending_op(current);
+
 	ret = get_futex_key(uaddr, flags, &key, FUTEX_WRITE);
 	if (ret)
 		return ret;
kernel/futex/waitwake.c +3 -0
index 1c2dd03f11ec..7752ed8c6dc1 100644
@@ -162,6 +162,9 @@ int futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
 	if (!bitset)
 		return -EINVAL;
 
+	/* Clear the pending_op_list. */
+	clear_robust_list_pending_op(current);
+
 	ret = get_futex_key(uaddr, flags, &key, FUTEX_READ);
 	if (unlikely(ret != 0))
 		return ret;
Signature · 1 line
2.39.5
#1 lore raw
kernel test robot noticed the following build errors:

Hi Mathieu,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on tip/locking/core]
[also build test ERROR on linus/master v7.0-rc3 next-20260311] [cannot apply to tip/x86/core tip/x86/vdso]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mathieu-Desnoyers/futex-Introduce-__vdso_robust_futex_unlock_u32-and-__vdso_robust_pi_futex_try_unlock_u32/20260313-201027
base:   tip/locking/core
patch link:    https://lore.kernel.org/r/20260312181850.2156985-1-mathieu.desnoyers%40efficios.com
patch subject: [RFC PATCH v3] futex: Introduce __vdso_robust_futex_unlock_u32 and __vdso_robust_pi_futex_try_unlock_u32
config: arm64-allnoconfig (https://download.01.org/0day-ci/archive/20260314/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 15.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260314/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from include/linux/sched.h:13,

                    from include/linux/percpu.h:12,
                    from arch/arm64/include/asm/arch_timer.h:18,
                    from arch/arm64/include/asm/timex.h:8,
                    from include/linux/timex.h:67,
                    from include/linux/time32.h:13,
                    from include/linux/time.h:60,
                    from include/linux/compat.h:10,
                    from kernel/futex/core.c:34:
   kernel/futex/core.c: In function 'handle_futex_death':
>> arch/arm64/include/asm/processor.h:384:43: error: implicit declaration of function 'task_stack_page'; did you mean 'task_stack_vm_area'? [-Wimplicit-function-declaration]
     384 |         ((struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1)
         |                                           ^~~~~~~~~~~~~~~
   kernel/futex/core.c:1148:38: note: in expansion of macro 'task_pt_regs'
    1148 |                 futex_vdso_exception(task_pt_regs(curr), &in_futex_vdso, &vdso_need_action);
         |                                      ^~~~~~~~~~~~

vim +384 arch/arm64/include/asm/processor.h

2f79d2fc391e4ba Peter Collingbourne 2021-03-18  378  
9cce7a435f89c9e Catalin Marinas     2012-03-05  379  /* Thread switching */
9cce7a435f89c9e Catalin Marinas     2012-03-05  380  extern struct task_struct *cpu_switch_to(struct task_struct *prev,
9cce7a435f89c9e Catalin Marinas     2012-03-05  381  					 struct task_struct *next);
9cce7a435f89c9e Catalin Marinas     2012-03-05  382  
9cce7a435f89c9e Catalin Marinas     2012-03-05  383  #define task_pt_regs(p) \

34be98f4944f990 Ard Biesheuvel 2017-07-20 @384 ((struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1)
9cce7a435f89c9e Catalin Marinas 2012-03-05 385

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Mathieu Desnoyers
kernel test robot noticed the following build errors:

Hi Mathieu,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on tip/locking/core]
[also build test ERROR on linus/master v7.0-rc3 next-20260311] [cannot apply to tip/x86/core tip/x86/vdso]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mathieu-Desnoyers/futex-Introduce-__vdso_robust_futex_unlock_u32-and-__vdso_robust_pi_futex_try_unlock_u32/20260313-201027
base:   tip/locking/core
patch link:    https://lore.kernel.org/r/20260312181850.2156985-1-mathieu.desnoyers%40efficios.com
patch subject: [RFC PATCH v3] futex: Introduce __vdso_robust_futex_unlock_u32 and __vdso_robust_pi_futex_try_unlock_u32
config: csky-allnoconfig (https://download.01.org/0day-ci/archive/20260314/[email protected]/config)
compiler: csky-linux-gcc (GCC) 15.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260314/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from arch/csky/include/asm/thread_info.h:10,
from include/linux/thread_info.h:62, from include/asm-generic/preempt.h:5, from ./arch/csky/include/generated/asm/preempt.h:1, from include/linux/preempt.h:79,
from include/linux/alloc_tag.h:11,
from include/linux/rhashtable-types.h:12, from include/linux/ipc.h:7,
from include/uapi/linux/sem.h:5,
from include/linux/sem.h:5,
from include/linux/compat.h:14,
from kernel/futex/core.c:34:
kernel/futex/core.c: In function 'handle_futex_death':

>> arch/csky/include/asm/processor.h:81:43: error: implicit declaration of function 'task_stack_page'; did you mean 'task_stack_vm_area'? [-Wimplicit-function-declaration]
      81 |         ((struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1)
         |                                           ^~~~~~~~~~~~~~~
   kernel/futex/core.c:1148:38: note: in expansion of macro 'task_pt_regs'
    1148 |                 futex_vdso_exception(task_pt_regs(curr), &in_futex_vdso, &vdso_need_action);
         |                                      ^~~~~~~~~~~~

vim +81 arch/csky/include/asm/processor.h

e9564df753fd54 Guo Ren 2018-09-05 79
e9564df753fd54 Guo Ren 2018-09-05 80 #define task_pt_regs(p) \
0f231dcfc664aa Guo Ren 2019-01-24 @81 ((struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1)
e9564df753fd54 Guo Ren 2018-09-05 82

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki