[PATCH v9 08/14] futex: Create syscall set_robust_list2()
From: André Almeida
Date: Fri Dec 04 2026 - 11:16:31 EST
Emulators (like FEX-Emu, to run x86 apps on top of Aarch64) have two
special needs about robust lists: to be able to register more than one
robust list, one of the app being emulated and one list for the emulator
itself; and to be able to walk 32-bit robust lists on a 64-bit
platform without compat entry points.
The current syscall allows one robust list per task (on x86-64, it
can have two if compat is enabled) and on Aarch64 there's no way to
parse a 32-bit robust list. The current syscall cannot be expanded to
solve both needs due to the lack of `flags` argument, so create a new
syscall, set_robust_list2() with the following signature:
sys_set_robust_list2(struct robust_list_head __user *head,
unsigned int index, unsigned int flags)
The new syscall allows setting multiple lists per task, of 64-bit or
32-bit types. These are the arguments:
- `*head` is the same structure used in the current syscall.
- `index` is used to set which index of task array of heads to be changed.
It should be between zero and the number of elements in the robust list
array, [0, robust_list2_list_limit).
- `flags` can be used for special operations:
- `FUTEX_ROBUST_LIST2_FLAG_NEW_ENTRY`: to avoid conflicts between the
app logic and libc, the kernel provides this special flag to get an
unused index. `index` must be zero and `head` must point to a valid
address. The kernel looks for an empty index, sets `head` there and
returns such index.
- `FUTEX_ROBUST_LIST2_FLAG_32BIT_ENTRY`: to specify that a given `head`
is a 32-bit pointer. The kernel will later call the appropriate
function to walk the list in 64-bit kernels. `head` should point to
a `struct robust_list_head32`, defined in the uAPI header. Using this
flag from a compat app is a no-op, given that the kernel will flag
every entry as a 32-bit struct anyway.
- Both flags can be combined and used together.
Kernel is responsible for always providing valid indexes, but userspace is
responsible for managing such indexes afterwards. Userspace can rewrite any
index at any given time. Using `head` as `NULL` to a given index makes it
free to be later returned by the kernel. However, it's invalid to ask for a
new entry while setting `head` as `NULL`.
And those are the error return values:
- `EINVAL`: for invalid flags, unaligned `head` address, `index` out of
range, when `head` is NULL or `index` is not zero when using
`FUTEX_ROBUST_LIST2_FLAG_NEW_ENTRY`.
- `ENOMEM`: the allocation of the array of robust list heads failed.
- `EFAULT`: `head` address can't be accessed.
- `ENOSPC`: when `FUTEX_ROBUST_LIST2_FLAG_NEW_ENTRY` is used but there's
no empty index available at the moment.
The array of lists is dynamically allocated on first use, but has a fixed
size determined by the kernel. Userspace should use sysctl interfaces
to gather how many `head` indexes are allocated
(`futex.robust_list2_list_limit`) and how many elements each list can
handle (`futex.robust_list2_elements_per_list`).
8 slots cover the target use case with headroom. However, userspace should
always check this number and not make assumptions.
The first two slots are internally reserved for the kernel, to store the
original syscall robust_list_head's (the native and compat one). Map the
indexes in such a way that userspace can't touch those indexes with the new
syscall and needs to use the original syscalls to manipulate such
addresses.
The array of lists is destroyed only during task exit. There are no
guarantees about the order of the lists.
Signed-off-by: André Almeida <andrealmeid@xxxxxxxxxx>
---
include/uapi/linux/futex.h | 7 +++++++
kernel/futex/core.c | 32 ++++++++++++++++++++++++++++++++
kernel/futex/futex.h | 2 ++
kernel/futex/syscalls.c | 30 ++++++++++++++++++++++++++++++
4 files changed, 71 insertions(+)
diff --git a/include/uapi/linux/futex.h b/include/uapi/linux/futex.h
index b39f8a23a84b..a114a19c53f1 100644
--- a/include/uapi/linux/futex.h
+++ b/include/uapi/linux/futex.h
@@ -131,6 +131,13 @@ struct futex_waitv {
* thread exit time.
*/
+/*
+ * User flags for set/get_robust_list2() syscalls
+ */
+#define FUTEX_ROBUST_LIST2_FLAG_NEW_ENTRY 0x1
+#define FUTEX_ROBUST_LIST2_FLAG_32BIT_ENTRY 0x2
+#define FUTEX_ROBUST_LIST2_FLAGS 0x3
+
/*
* Per-lock list entry - embedded in user-space locks, somewhere close
* to the futex field. (Note: user-space uses a double-linked list to
diff --git a/kernel/futex/core.c b/kernel/futex/core.c
index 0dd2f5594061..b01b3501df55 100644
--- a/kernel/futex/core.c
+++ b/kernel/futex/core.c
@@ -1137,6 +1137,38 @@ uintptr_t *futex_get_robust_lists(void)
return rl;
}
+static unsigned int __futex_robust_list_install(uintptr_t *rl, unsigned int idx, uintptr_t entry)
+{
+ rl[idx] = entry;
+ return idx - FUTEX_ROBUST_LIST2_IDX;
+}
+
+int futex_robust_list_install(uintptr_t entry, unsigned int idx, bool new, bool compat)
+{
+ uintptr_t *rl = futex_get_robust_lists();
+
+ if (!rl)
+ return -ENOMEM;
+
+ if (compat && entry)
+ entry |= FUTEX_ROBUST_LIST2_ENTRY_32BIT;
+
+ if (!new) {
+ if (idx >= FUTEX_ROBUST_LIST2_PER_USER)
+ return -EINVAL;
+
+ return __futex_robust_list_install(rl, idx + FUTEX_ROBUST_LIST2_IDX, entry);
+ }
+
+ /* Search for an empty slot */
+ for (idx = FUTEX_ROBUST_LIST2_IDX; idx < FUTEX_ROBUST_LIST2_MAX_IDX; idx++) {
+ if (!rl[idx])
+ return __futex_robust_list_install(rl, idx, entry);
+ }
+
+ return -ENOSPC;
+}
+
/*
* Fetch a robust-list pointer. Bit 0 signals PI futexes:
*/
diff --git a/kernel/futex/futex.h b/kernel/futex/futex.h
index c1da53fd7187..8258d6fda227 100644
--- a/kernel/futex/futex.h
+++ b/kernel/futex/futex.h
@@ -492,4 +492,6 @@ bool futex_robust_list_clear_pending(void __user *pop, unsigned int flags);
uintptr_t *futex_get_robust_lists(void);
+int futex_robust_list_install(uintptr_t entry, unsigned int idx, bool new, bool compat);
+
#endif /* _FUTEX_H */
diff --git a/kernel/futex/syscalls.c b/kernel/futex/syscalls.c
index 8e7aec8fe03b..af75ffb70956 100644
--- a/kernel/futex/syscalls.c
+++ b/kernel/futex/syscalls.c
@@ -115,6 +115,36 @@ SYSCALL_DEFINE3(get_robust_list, int, pid,
return put_user(head, head_ptr);
}
+SYSCALL_DEFINE3(set_robust_list2, struct robust_list_head * __user, head,
+ unsigned int, index, unsigned int, flags)
+{
+ size_t align = sizeof(u64), head_size = sizeof(*head);
+ bool new = false, compat = false;
+
+ if (flags & ~FUTEX_ROBUST_LIST2_FLAGS)
+ return -EINVAL;
+
+ if (flags & FUTEX_ROBUST_LIST2_FLAG_NEW_ENTRY) {
+ if (index || !head)
+ return -EINVAL;
+ new = true;
+ }
+
+ if (flags & FUTEX_ROBUST_LIST2_FLAG_32BIT_ENTRY || in_compat_syscall()) {
+ compat = true;
+ align = sizeof(u32);
+ head_size = sizeof(struct robust_list_head32);
+ }
+
+ if (!IS_ALIGNED((uintptr_t)head, align))
+ return -EINVAL;
+
+ if (head && unlikely(!access_ok(head, head_size)))
+ return -EFAULT;
+
+ return futex_robust_list_install((uintptr_t) head, index, new, compat);
+}
+
long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
u32 __user *uaddr2, u32 val2, u32 val3)
{
--
2.56.1