From: Thomas Gleixner <tglx@kernel.org>
To: "André Almeida" <andrealmeid@igalia.com>,
"Ingo Molnar" <mingo@redhat.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Darren Hart" <dvhart@infradead.org>,
"Davidlohr Bueso" <dave@stgolabs.net>,
"Arnd Bergmann" <arnd@arndb.de>,
"Sebastian Andrzej Siewior" <bigeasy@linutronix.de>,
"Waiman Long" <longman@redhat.com>,
"Ryan Houdek" <Sonicadvance1@gmail.com>
Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-api@vger.kernel.org, kernel-dev@igalia.com,
"André Almeida" <andrealmeid@igalia.com>
Subject: Re: [PATCH v8 04/10] futex: Create set_robust_list2() syscall
Date: Tue, 01 Dec 2026 02:27:24 +0100 [thread overview]
Message-ID: <8733shrds3.ffs@fw13> (raw)
In-Reply-To: <20261115-tonyk-robust_futex-v8-4-a4fe47333a28@igalia.com>
On Sun, Nov 15 2026 at 14:31, André Almeida wrote:
> +enum robust_list2_cmd {
> + FUTEX_ROBUST_LIST_CMD_CREATE_64,
> + FUTEX_ROBUST_LIST_CMD_CREATE_32,
This is a complete misnomer. It does not create a robust list. It
installs a robust list pointer which is handed in from user space.
> + FUTEX_ROBUST_LIST_CMD_MODIFY_64,
> + FUTEX_ROBUST_LIST_CMD_MODIFY_32,
> + FUTEX_ROBUST_LIST_CMD_LIST_LIMIT,
> + FUTEX_ROBUST_LIST_CMD_USER_MAX,
> +int futex_robust_list_create(uintptr_t head, enum robust_list2_cmd cmd)
> +{
> + uintptr_t *rl = current->futex.robust_lists, entry = FUTEX_ROBUST_LIST_ENTRY_INUSE;
> + unsigned int index;
> +
> + if (!rl) {
> + rl = kcalloc(FUTEX_ROBUST_LIST2_MAX_IDX, sizeof(*rl), GFP_KERNEL);
> + if (!rl)
> + return -ENOMEM;
> +
> + scoped_guard(mutex, ¤t->futex.exit_mutex) {
> + /* check if another thread set the list before us */
How so?
task::futex.robust_lists is strictly per thread and there is no way for
another thread to set it. This whole mutex dance is pointless voodoo
programming all over the place.
> + if (current->futex.robust_lists) {
> + kfree(rl);
> + rl = current->futex.robust_lists;
> + } else {
> + current->futex.robust_lists = rl;
> + }
> + }
> + }
> +
> + switch (cmd) {
> + case FUTEX_ROBUST_LIST_CMD_CREATE_32:
> + if (head)
> + entry |= FUTEX_ROBUST_LIST_ENTRY_32BIT;
> + fallthrough;
> + case FUTEX_ROBUST_LIST_CMD_CREATE_64:
> + /* Search for an empty slot */
> + for (index = FUTEX_ROBUST_LIST2_IDX;
> + index < FUTEX_ROBUST_LIST2_MAX_IDX; index++) {
> + if (rl[index] == 0)
> + goto exit;
> + }
> + return -ENOSPC;
> + default:
> + return -EINVAL;
So if this is invoked with a NULL entry it will happily give out an
unused index to user space.
As a consequence: If this is a FUTEX_ROBUST_LIST_CMD_CREATE_32 command,
then it will return a valid index, which means success for user space
and then a subsequent MODIFY for that index will fail the bitness
check. That does not make any sense at all.
That aside, this switch case is overengineered and tasteless. There is
zero reason to hand in @cmd. All what this function needs to know is
whether this is a 32 bit or a 64bit list pointer.
So you can make this:
int robust_list_install(uintptr_t entry)
{
if (!(entry & FUTEX_ROBUST_LIST_ENTRY_MASK))
return;
uintptr_t *rl = current->futex.robust_lists;
if (!rl) {
rl = kcalloc(FUTEX_ROBUST_LIST2_MAX_IDX, sizeof(*rl), GFP_KERNEL);
if (!rl)
return -ENOMEM;
current->futex.robust_lists = rl;
}
/* Search for an empty slot */
for (unsigned int idx = FUTEX_ROBUST_LIST2_IDX; idx < FUTEX_ROBUST_LIST2_MAX_IDX; idx++) {
if (rl[idx])
continue;
rl[idx] = head;
return idx - FUTEX_ROBUST_LIST2_IDX;
}
return -ENOSPC;
}
And let the caller do:
case FUTEX_ROBUST_LIST_CMD_INSTALL_32:
entry |= FUTEX_ROBUST_LIST_ENTRY_32BIT;
fallthrough;
case FUTEX_ROBUST_LIST_CMD_INSTALL_64:
....
ret = robust_list_install(entry);
No?
But see below ...
> +int futex_robust_list_modify(uintptr_t head, enum robust_list2_cmd cmd,
> + unsigned int index)
> +{
> + uintptr_t *rl = current->futex.robust_lists, entry = head;
> +
> + if (index >= FUTEX_ROBUST_LIST2_MAX_IDX - FUTEX_ROBUST_LIST2_IDX)
> + return -EINVAL;
> +
> + /* See comment about index mapping at sys_set_robust_list2() */
> + index += FUTEX_ROBUST_LIST2_IDX;
> +
> + if (!rl || !rl[index])
> + return -ENOENT;
> +
> + switch (cmd) {
> + case FUTEX_ROBUST_LIST_CMD_MODIFY_64:
> + if (rl[index] & FUTEX_ROBUST_LIST_ENTRY_32BIT)
> + return -EINVAL;
> + if (futex_in_32bit_syscall())
> + return -EINVAL;
A 32bit syscall cannot even reach this code ...
> + break;
> + case FUTEX_ROBUST_LIST_CMD_MODIFY_32:
> + if (!(rl[index] & FUTEX_ROBUST_LIST_ENTRY_32BIT))
> + return -EINVAL;
> + if (entry)
> + entry |= FUTEX_ROBUST_LIST_ENTRY_32BIT;
> + break;
> + default:
> + return -EINVAL;
> + }
> +
> + scoped_guard(mutex, ¤t->futex.exit_mutex)
> + rl[index] = entry;
Again this whole thing can be simplified.
int futex_robust_list_modify(uintptr_t entry, unsigned int idx)
{
uintptr_t tmp, *rl = current->futex.robust_lists;
if (idx >= FUTEX_ROBUST_LIST2_MAX_USER_IDX)
return -EINVAL;
idx += FUTEX_ROBUST_LIST2_IDX;
if (!rl || !rl[idx])
return -ENOENT;
// Validate that bitness matches
if ((rl[idx] ^ entry) & FUTEX_ROBUST_LIST_ENTRY_32BIT)
return -EINVAL;
rl[idx] = entry & FUTEX_ROBUST_LIST_ENTRY_MASK ? : NULL;
return 0;
}
But see below ...
> +static void exit_robust_lists(struct task_struct *tsk)
Unused function
> +{
> + uintptr_t *rl = tsk->futex.robust_lists;
What guarantees that rl is not NULL?
> + tsk->futex.robust_lists = NULL;
> +
> + for (unsigned int idx = 0; idx < FUTEX_ROBUST_LIST2_MAX_IDX; idx++) {
> + uintptr_t entry = rl[idx];
> +
> + if (!(entry & FUTEX_ROBUST_LIST_ENTRY_MASK))
> + continue;
> +
> + /*
> + * If the list type is the same as the kernel bitness, always
> + * calls exit_robust_list(). exit_robust_list32() is only for
> + * 32-bit lists in a 64-bit kernel.
> + */
You really have a tendency to comment the obvious...
> + if (IS_ENABLED(CONFIG_64BIT) && (entry & FUTEX_ROBUST_LIST_ENTRY_32BIT)) {
> + struct robust_list_head32 __user *head;
> +
> + entry &= FUTEX_ROBUST_LIST_ENTRY_MASK;
> +
> + head = (__force struct robust_list_head32 __user *)entry;
> + exit_robust_list32(tsk, head);
> + } else {
> + struct robust_list_head __user *head;
> +
> + entry &= FUTEX_ROBUST_LIST_ENTRY_MASK;
> +
> + head = (__force struct robust_list_head __user *)entry;
> + exit_robust_list(tsk, head);
> + }
> + }
> +
> + kfree(rl);
> +}
> +
> static void futex_cleanup(struct task_struct *tsk)
> {
> if (unlikely(tsk->futex.robust_list)) {
> - exit_robust_list(tsk);
> + exit_robust_list(tsk, tsk->futex.robust_list);
This want's to be a preparatory change and not something in the middle
of introducing a new syscall...
> tsk->futex.robust_list = NULL;
> }
>
> #ifdef CONFIG_64BIT
> if (unlikely(tsk->futex.robust_list32)) {
> - exit_robust_list32(tsk);
> + exit_robust_list32(tsk, tsk->futex.robust_list32);
Ditto.
> tsk->futex.robust_list32 = NULL;
> }
> #endif
> +SYSCALL_DEFINE4(set_robust_list2, struct robust_list_head * __user, head, unsigned int,
> + cmd, unsigned int, index, unsigned int, flags)
> +{
> + uintptr_t entry = (__force uintptr_t)head;
> + size_t align = sizeof(u32);
> + int ret;
> +
> + if (flags)
> + return -EINVAL;
> +
> + if (cmd >= FUTEX_ROBUST_LIST_CMD_USER_MAX)
> + return -EINVAL;
These two checks can be combined in one conditional.
> + if (cmd == FUTEX_ROBUST_LIST_CMD_CREATE_64 ||
> + cmd == FUTEX_ROBUST_LIST_CMD_MODIFY_64) {
No line break required.
> + if (futex_in_32bit_syscall())
> + return -EOPNOTSUPP;
> + align = sizeof(u64);
> + }
> +
> + switch (cmd) {
> + case FUTEX_ROBUST_LIST_CMD_CREATE_64:
> + case FUTEX_ROBUST_LIST_CMD_CREATE_32:
> + if (index)
> + return -EINVAL;
This check is pretty pointless because 0 is also a valid index, no?
> + if (entry % align)
> + return -EINVAL;
This should also validate that the head pointer is a valid user space
address. If not, return -EFAULT.
> + /*
> + * The first two indexes are reserved for the kernel to be used
> + * with the legacy syscall, so we hide them from userspace.
> + *
> + * We map [0, FUTEX_ROBUST_LISTS_PER_USER) to
> + * [FUTEX_ROBUST_LIST2_IDX, FUTEX_ROBUST_LIST2_MAX_IDX)
> + */
This comment is not really helpful here.
> + ret = futex_robust_list_create(entry, cmd);
> +
> + if (ret >= 0)
> + ret -= FUTEX_ROBUST_LIST2_IDX;
See above.
> + return ret;
> +
> + case FUTEX_ROBUST_LIST_CMD_MODIFY_64:
> + case FUTEX_ROBUST_LIST_CMD_MODIFY_32:
> + if (entry % align)
> + return -EINVAL;
See above.
> + return futex_robust_list_modify(entry, cmd, index);
> + case FUTEX_ROBUST_LIST_CMD_LIST_LIMIT:
> + return FUTEX_ROBUST_LISTS_PER_USER;
This does not make sense at all because that's a compile time constant
so it should be read from a sysctl and not be multiplexed into this
syscall just because, no?
> + }
> +
> + return -EINVAL;
> +}
This whole thing can be simplified so it becomes comprehensible.
SYSCALL_DEFINE4(set_robust_list2, struct robust_list_head * __user, head, unsigned int,
cmd, unsigned int, index, unsigned int, flags)
{
if (flags || cmd >= FUTEX_ROBUST_LIST_CMD_USER_MAX)
return -EINVAL;
size_t align = sizeof(u32);
switch (cmd) {
case FUTEX_ROBUST_LIST_CMD_INSTALL_64:
case FUTEX_ROBUST_LIST_CMD_MODIFY_64:
if (futex_in_32bit_syscall())
return -EOPNOTSUPP;
align = sizeof(u64);
}
if (unlikely(!access_ok(uaddr, align)))
return -EFAULT;
uintptr_t entry = (__force uintptr_t)head;
if (entry % align)
return -EINVAL;
switch (cmd) {
case FUTEX_ROBUST_LIST_CMD_INSTALL_32:
entry |= FUTEX_ROBUST_LIST_ENTRY_32BIT;
fallthrough;
case FUTEX_ROBUST_LIST_CMD_INSTALL_64:
return futex_robust_list_create(entry);
case FUTEX_ROBUST_LIST_CMD_MODIFY_32:
entry |= FUTEX_ROBUST_LIST_ENTRY_32BIT;
fallthrough;
case FUTEX_ROBUST_LIST_CMD_MODIFY_64:
return futex_robust_list_modify(entry, index);
}
return -EINVAL;
}
No?
But I really have to ask how the distinction between INSTALL and MODIFY
is actually valuable.
This whole thing can be made _ONE_ operation, which removes boatloads of
pointless code and solves the install NULL and then modify issue I
pointed out above:
static int __robust_list_install(uintptr_t *rl, , unsigned int idx, uintptr_t entry)
{
rl[idx] = entry & FUTEX_ROBUST_LIST_ENTRY_MASK ? : NULL;
return idx - FUTEX_ROBUST_LIST2_IDX;
}
int robust_list_install(uintptr_t entry, unsigned int idx)
{
uintptr_t *rl = robust_get_lists();
if (!rl)
return -ENOMEM;
// Update an distinct entry?
if (idx != FUTEX_ROBUST_LIST2_NEW_ENTRY) {
if (idx >= FUTEX_ROBUST_LIST2_MAX_USER_IDX)
return -EINVAL;
return __robust_list_install(rl, idx, entry);
}
/* Search for an empty slot */
for (idx = FUTEX_ROBUST_LIST2_IDX; idx < FUTEX_ROBUST_LIST2_MAX_IDX; idx++) {
if (!rl[idx])
return __robust_list_install(rl, idx, entry);
}
return -ENOSPC;
}
Note that this does not care whether there is a bitness mismatch in case
of updating a particular index. Why?
The kernel is not responsible for user space to get its act together
and there is zero impact for the kernel if user space screws that up.
No?
That said, the whole ordering approach of this patch set is backwards
and unreviewable.
What you want to do is:
1) Preperatory patches
Patch 02/03 plus patches which change the exit_robust_list()
signature and add missing sanity checks to the existing syscalls.
2) Introduce robust_get_lists() which either returns the existing
robust lists pointer or allocates the robust list array
uintptr_t *robust_get_lists(void)
{
uintptr_t *rl = current->futex.robust_lists;
if (!rl) {
rl = kcalloc(FUTEX_ROBUST_LIST2_MAX_IDX, sizeof(*rl), GFP_KERNEL);
current->futex.robust_lists = rl;
}
return rl;
}
3) Provide the exit() walker for this new thing and invoke it which
will be a NOOP because the list is empty.
4) Use #2/#3 for the existing syscalls and remove the then unused leftovers
SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head, size_t, len)
{
uintptr_t *rl = robust_get_lists();
if (!rl)
return -ENOMEM;
rl[FUTEX_ROBUST_LIST_NATIVE_IDX] = head;
return 0;
}
...
5/6) Add the new syscalls
7/8) Wire up the syscalls
Thanks,
tglx
next prev parent reply other threads:[~2026-12-01 1:27 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-15 17:31 [PATCH v8 00/10] futex: Create {set,get}_robust_list2() syscalls André Almeida
2026-11-15 17:31 ` [PATCH v8 01/10] futex: Skip test_futex_robust_unlock() if not supported André Almeida
2026-11-30 21:06 ` [tip: locking/futex] " tip-bot2 for André Almeida
2026-11-15 17:31 ` [PATCH v8 02/10] futex: Use explicit sizes for compat_robust_list structs André Almeida
2026-11-15 17:31 ` [PATCH v8 03/10] futex: Make exit_robust_list32() unconditionally available for 64-bit kernels André Almeida
2026-11-15 17:31 ` [PATCH v8 04/10] futex: Create set_robust_list2() syscall André Almeida
2026-11-21 0:14 ` kernel test robot
2026-12-01 1:27 ` Thomas Gleixner [this message]
2026-11-15 17:31 ` [PATCH v8 05/10] futex: Create get_robust_list2() syscall André Almeida
2026-11-21 0:35 ` kernel test robot
2026-11-21 0:46 ` kernel test robot
2026-11-15 17:31 ` [PATCH v8 06/10] futex: Wire up set_robust_list2 syscall André Almeida
2026-11-15 20:55 ` kernel test robot
2026-11-15 21:30 ` kernel test robot
2026-11-15 17:31 ` [PATCH v8 07/10] futex: Wire up get_robust_list2 syscall André Almeida
2026-11-15 17:31 ` [PATCH v8 08/10] selftests/futex: Expand for set_robust_list2() André Almeida
2026-11-15 17:31 ` [PATCH v8 09/10] selftests/futex: Expand for get_robust_list2() André Almeida
2026-11-15 17:31 ` [PATCH v8 10/10] futex: Use new robust list API internally André Almeida
2026-11-15 20:44 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8733shrds3.ffs@fw13 \
--to=tglx@kernel.org \
--cc=Sonicadvance1@gmail.com \
--cc=andrealmeid@igalia.com \
--cc=arnd@arndb.de \
--cc=bigeasy@linutronix.de \
--cc=dave@stgolabs.net \
--cc=dvhart@infradead.org \
--cc=kernel-dev@igalia.com \
--cc=linux-api@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=longman@redhat.com \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®