[PATCH v9 06/14] futex: Use the multi-robust list infrastructure for current syscalls
From: André Almeida
Date: Fri Dec 04 2026 - 11:17:02 EST
Use the multi-robust list infrastructure for the current system calls and
drop robust_list and robust_list32 from struct futex_sched_data, relying
solely on the robust_lists member. Make use of the reserved array positions
for the current system call and the compat entry point.
For the robust_list_clear_pending() functions, walk the array of robust
list heads checking which one, if any, matches the pop address.
Syscall set_robust_list() now can return -ENOMEM in case the allocation
fails.
Signed-off-by: André Almeida <andrealmeid@xxxxxxxxxx>
---
include/linux/futex.h | 4 ----
include/linux/futex_types.h | 4 ----
kernel/futex/core.c | 58 +++++++++++++++++++++++++++++++--------------
kernel/futex/syscalls.c | 24 +++++++++++++++----
4 files changed, 59 insertions(+), 31 deletions(-)
diff --git a/include/linux/futex.h b/include/linux/futex.h
index 0da95f94cfe9..75a83f262d1c 100644
--- a/include/linux/futex.h
+++ b/include/linux/futex.h
@@ -72,10 +72,6 @@ static inline void futex_init_task(struct task_struct *tsk)
INIT_LIST_HEAD(&tsk->futex.pi_state_list);
tsk->futex.state = FUTEX_STATE_OK;
mutex_init(&tsk->futex.exit_mutex);
- tsk->futex.robust_list = NULL;
-#ifdef CONFIG_COMPAT
- tsk->futex.robust_list32 = NULL;
-#endif
tsk->futex.robust_lists = NULL;
}
diff --git a/include/linux/futex_types.h b/include/linux/futex_types.h
index 20123110c80d..a2b4ec0cb125 100644
--- a/include/linux/futex_types.h
+++ b/include/linux/futex_types.h
@@ -21,10 +21,6 @@ struct robust_list_head;
* @state: Futex handling state to handle exit races correctly
*/
struct futex_sched_data {
- struct robust_list_head __user *robust_list;
-#ifdef CONFIG_COMPAT
- struct robust_list_head32 __user *robust_list32;
-#endif
uintptr_t *robust_lists;
struct list_head pi_state_list;
struct futex_pi_state *pi_state_cache;
diff --git a/kernel/futex/core.c b/kernel/futex/core.c
index d15906f3c80c..a0a391330408 100644
--- a/kernel/futex/core.c
+++ b/kernel/futex/core.c
@@ -1223,7 +1223,8 @@ static void exit_robust_list(struct task_struct *curr,
static bool robust_list_clear_pending(unsigned long __user *pop)
{
- struct robust_list_head __user *head = current->futex.robust_list;
+ uintptr_t *rl = current->futex.robust_lists;
+ unsigned int i;
if (!put_user(0UL, pop))
return true;
@@ -1237,8 +1238,24 @@ static bool robust_list_clear_pending(unsigned long __user *pop)
* task exits. Better a few stale futexes than corrupted memory. But
* that's mostly an academic exercise.
*/
- if (pop == (unsigned long __user *)&head->list_op_pending)
- current->futex.robust_list = NULL;
+ if (!rl)
+ return false;
+
+ for (i = 0; i < FUTEX_ROBUST_LIST2_MAX_IDX; i++) {
+ struct robust_list_head __user *head;
+
+ if (!rl[i] || rl[i] & FUTEX_ROBUST_LIST2_ENTRY_32BIT)
+ continue;
+
+ head = (struct robust_list_head __user *)
+ (rl[i] & FUTEX_ROBUST_LIST2_ENTRY_MASK);
+
+ if (pop == (unsigned long __user *)&head->list_op_pending) {
+ rl[i] = (uintptr_t) NULL;
+ break;
+ }
+ }
+
return false;
}
@@ -1342,14 +1359,31 @@ static void exit_robust_list32(struct task_struct *curr,
static bool robust_list_clear_pending32(u32 __user *pop)
{
- struct robust_list_head32 __user *head = current->futex.robust_list32;
+ uintptr_t *rl = current->futex.robust_lists;
+ unsigned int i;
if (!put_user(0U, pop))
return true;
/* See comment in robust_list_clear_pending(). */
- if (pop == &head->list_op_pending)
- current->futex.robust_list32 = NULL;
+ if (!rl)
+ return false;
+
+ for (i = 0; i < FUTEX_ROBUST_LIST2_MAX_IDX; i++) {
+ struct robust_list_head32 __user *head;
+
+ if (!rl[i] || !(rl[i] & FUTEX_ROBUST_LIST2_ENTRY_32BIT))
+ continue;
+
+ head = (struct robust_list_head32 __user *)
+ (rl[i] & FUTEX_ROBUST_LIST2_ENTRY_MASK);
+
+ if (pop == (unsigned __user *)&head->list_op_pending) {
+ rl[i] = (uintptr_t) NULL;
+ break;
+ }
+ }
+
return false;
}
#else
@@ -1514,18 +1548,6 @@ static void futex_cleanup(struct task_struct *tsk)
if (unlikely(tsk->futex.robust_lists))
exit_robust_lists(tsk);
- if (unlikely(tsk->futex.robust_list)) {
- exit_robust_list(tsk, tsk->futex.robust_list);
- tsk->futex.robust_list = NULL;
- }
-
-#ifdef CONFIG_64BIT
- if (unlikely(tsk->futex.robust_list32)) {
- exit_robust_list32(tsk, tsk->futex.robust_list32);
- tsk->futex.robust_list32 = NULL;
- }
-#endif
-
if (unlikely(!list_empty(&tsk->futex.pi_state_list)))
exit_pi_state_list(tsk);
}
diff --git a/kernel/futex/syscalls.c b/kernel/futex/syscalls.c
index 4a45bace75fc..8e7aec8fe03b 100644
--- a/kernel/futex/syscalls.c
+++ b/kernel/futex/syscalls.c
@@ -27,21 +27,31 @@
*/
SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head, size_t, len)
{
+ uintptr_t *rl = futex_get_robust_lists();
+
/* The kernel knows only one size for now. */
if (unlikely(len != sizeof(*head)))
return -EINVAL;
- current->futex.robust_list = head;
+ if (!rl)
+ return -ENOMEM;
+
+ rl[FUTEX_ROBUST_LIST2_NATIVE_IDX] = (uintptr_t)head;
return 0;
}
static inline void __user *futex_task_robust_list(struct task_struct *p, bool compat)
{
+ uintptr_t *rl = p->futex.robust_lists;
+
+ if (!rl)
+ return NULL;
+
#ifdef CONFIG_COMPAT
if (compat)
- return p->futex.robust_list32;
+ return (void __user *)rl[FUTEX_ROBUST_LIST2_COMPAT_IDX];
#endif
- return p->futex.robust_list;
+ return (void __user *)rl[FUTEX_ROBUST_LIST2_NATIVE_IDX];
}
static void __user *futex_get_robust_list_common(int pid, bool compat)
@@ -76,7 +86,7 @@ static void __user *futex_get_robust_list_common(int pid, bool compat)
up_read(&p->signal->exec_update_lock);
put_task_struct(p);
- return head;
+ return (void __user *)((uintptr_t) head & FUTEX_ROBUST_LIST2_ENTRY_MASK);
err_unlock:
up_read(&p->signal->exec_update_lock);
@@ -481,11 +491,15 @@ SYSCALL_DEFINE4(futex_requeue,
COMPAT_SYSCALL_DEFINE2(set_robust_list, struct robust_list_head32 __user *, head,
compat_size_t, len)
{
+ uintptr_t *rl = futex_get_robust_lists();
+
if (unlikely(len != sizeof(*head)))
return -EINVAL;
- current->futex.robust_list32 = head;
+ if (!rl)
+ return -ENOMEM;
+ rl[FUTEX_ROBUST_LIST2_COMPAT_IDX] = (uintptr_t)head | FUTEX_ROBUST_LIST2_ENTRY_32BIT;
return 0;
}
--
2.56.1