futex: Drop ROBUST_LIST_LIMIT
André Almeida
reviewerv2 cover futex: Drop ROBUST_LIST_LIMIT
As requested by Peter at [1], this patchset drops the ROBUST_LIST_LIMIT. This is achieve by simply rewriting the processed list element ->next to point to the head->list address, destroying the linked list to avoid any circular list.
Patches 2-4 create selftest for robust lists. Patch 2/4 creates helpful macros for futex selftests and 3/4 creates a bunch of tests for the interface (as I had submitted before at [2]).
Patch 4/4 is used to validate the changes made at 1/4:
- That the kernel can now handle a lock that is allocated in an index bigger than ROBUST_LIST_LIMIT
- That the kernel can still handle circular linked lists.
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
Changelog:
- Rebased on top of tip/locking/urgent (for 6.14-rc1)
v1: https://lore.kernel.org/lkml/[email protected]/#t
André Almeida (4):
futex: Drop ROBUST_LIST_LIMIT
selftests/futex: Add ASSERT_ macros
selftests/futex: Create test for robust list selftests/futex: Create tests for long and circular robust lists
include/uapi/linux/futex.h | 3 +- kernel/futex/core.c | 13 +- .../selftests/futex/functional/.gitignore | 1 + .../selftests/futex/functional/Makefile | 3 +- .../selftests/futex/functional/robust_list.c | 641 ++++++++++++++++++ .../testing/selftests/futex/include/logging.h | 38 ++ 6 files changed, 690 insertions(+), 9 deletions(-) create mode 100644 tools/testing/selftests/futex/functional/robust_list.c
Signature · 1 line
2.48.0
André Almeida
reviewerPATCH v2 1/4 futex: Drop ROBUST_LIST_LIMIT
ROBUST_LIST_LIMIT was introduced to avoid the kernel get stuck in circular lists, stopping to handle locks after the limit. This could cause starvation in applications that have very long lists with valid and non repetitive elements.
Instead of having a hard limit, rewrite the next pointer list while walking on it. In this way, if the kernel ever revisits a repetitive element (characterizing a circular list) the loop will stop.
--- include/uapi/linux/futex.h | 3 +-- kernel/futex/core.c | 13 +++++++------ 2 files changed, 8 insertions(+), 8 deletions(-)
include/uapi/linux/futex.h +1 -2
@@ -165,8 +165,7 @@ struct robust_list_head {#define FUTEX_TID_MASK 0x3fffffff/* -* This limit protects against a deliberately circular list. -* (Not worth introducing an rlimit for it) +* Deprecated, do not use. There is no limit of items in a list. */ #define ROBUST_LIST_LIMIT 2048
kernel/futex/core.c +7 -6
@@ -779,7 +779,7 @@ static void exit_robust_list(struct task_struct *curr) {struct robust_list_head __user *head = curr->robust_list;struct robust_list __user *entry, *next_entry, *pending; -unsigned int limit =ROBUST_LIST_LIMIT , pi, pip; +unsigned int pi, pip;unsigned int next_pi;unsigned long futex_offset;int rc;
@@ -820,13 +820,14 @@ static void exit_robust_list(struct task_struct *curr) }if (rc)return ; - entry = next_entry; - pi = next_pi; +/* -* Avoid excessively long or circular lists: +* Avoid circular lists: */ -if (!--limit) -break ; +put_user (&head->list, &entry->next); + + entry = next_entry; + pi = next_pi;cond_resched (); }
Signature · 1 line
2.48.0
André Almeida
reviewerPATCH v2 2/4 selftests/futex: Add ASSERT_ macros
In reply to André AlmeidaCreate ASSERT_{EQ, NE, TRUE, FALSE} macros to make test creation easier.
--- .../testing/selftests/futex/include/logging.h | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+)
tools/testing/selftests/futex/include/logging.h +38 -0
@@ -23,6 +23,44 @@#include <linux/futex.h> #include "kselftest.h" +#define ASSERT_EQ (var, value) \ +do { \ + if (var != value) { \ + ksft_test_result_fail("%s: expected %ld, but %s has %ld\n", \ + __func__, (long) value, #var, \ + (long) var); \ + return; \ + } \ +} while (0) + +#define ASSERT_NE (var, value) \ +do { \ + if (var == value) { \ + ksft_test_result_fail("%s: expected not %ld, but %s has %ld\n", \ + __func__, (long) value, #var, \ + (long) var); \ + return; \ + } \ +} while (0) + +#define ASSERT_TRUE (var) \ +do { \ + if ((var) == 0) { \ + ksft_test_result_fail("%s: expected %s to be true\n", \ + __func__, #var); \ + return; \ + } \ +} while (0) + +#define ASSERT_FALSE (var) \ +do { \ + if (var) { \ + ksft_test_result_fail("%s: expected %s to be false\n", \ + __func__, #var); \ + return; \ + } \ +} while (0) +/* * Define PASS, ERROR, and FAIL strings with and without color escape * sequences, default to no color.
Signature · 1 line
2.48.0
André Almeida
reviewerPATCH v2 3/4 selftests/futex: Create test for robust list
In reply to André AlmeidaCreate a test for the robust list mechanism. Test the following uAPI operations:
- Creating a robust mutex where the lock waiter is wake by the kernel when the lock owner died
- Setting a robust list to the current task
- Getting a robust list from the current task
- Getting a robust list from another task
- Using the list_op_pending field from robust_list_head struct to test robustness when the lock owner dies before completing the locking
- Setting a invalid size for syscall argument len
This is the expected output:
TAP version 13
1..6
ok 1 test_robustness
ok 2 test_set_robust_list_invalid_size
ok 3 test_get_robust_list_self
ok 4 test_get_robust_list_child
ok 5 test_set_list_op_pending
ok 6 test_robust_list_multiple_elements
# Totals: pass:6 fail:0 xfail:0 xpass:0 skip:0 error:0
--- .../selftests/futex/functional/.gitignore | 1 + .../selftests/futex/functional/Makefile | 3 +- .../selftests/futex/functional/robust_list.c | 516 ++++++++++++++++++ 3 files changed, 519 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/futex/functional/robust_list.c
tools/testing/selftests/futex/functional/.gitignore +1 -0
@@ -9,3 +9,4 @@ futex_wait_wouldblock futex_wait futex_requeue futex_waitv +robust_list
tools/testing/selftests/futex/functional/Makefile +2 -1
@@ -17,7 +17,8 @@ TEST_GEN_PROGS := \ futex_wait_private_mapped_file \ futex_wait \ futex_requeue \ - futex_waitv + futex_waitv \ + robust_list TEST_PROGS := run.sh
tools/testing/selftests/futex/functional/robust_list.c new file +516 -0
516 lines not on this page · raw message
Signature · 1 line
2.48.0
André Almeida
reviewerPATCH v2 4/4 selftests/futex: Create tests for long and circular robust lists
In reply to André AlmeidaAfter dropping ROBUST_LIST_LIMIT, create new robust list tests to ensure two conditions:
- That the kernel can correctly handle circular lists
- That the kernel can correctly wake up elements in very long lists, larger that the old ROBUST_LIST_LIMIT.
--- .../selftests/futex/functional/robust_list.c | 127 +++++++++++++++++- 1 file changed, 126 insertions(+), 1 deletion(-)
tools/testing/selftests/futex/functional/robust_list.c +126 -1
@@ -471,6 +471,128 @@ static void test_robust_list_multiple_elements(void)ksft_test_result_pass ("%s\n" , __func__); } +/* +* This is the old limit defined by the kernel +*/ +#define ROBUST_LIST_LIMIT 2048 +#define CHILD_LIST_LIMIT (ROBUST_LIST_LIMIT + 10) + +static int child_robust_list_limit (void *arg) +{ +struct lock_struct *locks; +struct robust_list *list; +struct robust_list_head head; +int ret, i; + + locks = (struct lock_struct *) arg; + + ret =set_list (&head); +if (ret) +ksft_test_result_fail ("set_list error\n" ); + +/* +* Create a very long list of locks +*/ + head.list.next = &locks[0 ].list; + + list = head.list.next; +for (i =0 ; i <CHILD_LIST_LIMIT -1 ; i++) { + list->next = &locks[i+1 ].list; + list = list->next; + } + list->next = &head.list; + +/* +* Grab the lock in the last one, and die without releasing it +*/ +mutex_lock (&locks[CHILD_LIST_LIMIT ], &head, false); +pthread_barrier_wait (&barrier); + +sleep (1 ); + +return 0 ; +} + +/* +* Robust list used to have a limit of 2048 items from the kernel side. After +* this limit the kernel would stop walking the list and ignore the other +* futexes, causing deadlocks. +* +* After this limit has been dropped, test if we can wait for a list of more +* than 2048 elements. +*/ +static void test_robust_list_limit (void ) +{ +struct lock_struct locks[CHILD_LIST_LIMIT +1 ]; + _Atomic(unsigned int) *futex = &locks[CHILD_LIST_LIMIT].futex; +struct robust_list_head head; +int ret; + + *futex =0 ; + + ret =set_list (&head); +ASSERT_EQ (ret,0 ); + + ret =pthread_barrier_init (&barrier,NULL ,2 ); +ASSERT_EQ (ret,0 ); + +create_child (child_robust_list_limit, locks); + +/* +* After the child thread creates the very long list of locks, wait on +* the last one. +*/ +pthread_barrier_wait (&barrier); + ret =mutex_lock (&locks[CHILD_LIST_LIMIT ], &head, false); + +if (ret !=0 ) +printf ("futex wait returned %d\n" , errno); +ASSERT_EQ (ret,0 ); + +ASSERT_TRUE (*futex |FUTEX_OWNER_DIED ); + +wait (NULL ); +pthread_barrier_destroy (&barrier); + +ksft_test_result_pass ("%s\n" , __func__); +} + +static int child_circular_list (void *arg) +{ +static struct robust_list_head head; +struct lock_struct a, b, c; +int ret; + + ret =set_list (&head); +if (ret) +ksft_test_result_fail ("set_list error\n" ); + + head.list.next = &a.list; + +/* +* The last element should point to head list, but we short circuit it +*/ + a.list.next = &b.list; + b.list.next = &c.list; + c.list.next = &a.list; + +return 0 ; +} + +/* +* Create a circular robust list. The kernel should be able to destroy the list +* while processing it so it won't be trapped in an infinite loop while handling +* a process exit +*/ +static void test_circular_list (void ) +{ +create_child (child_circular_list,NULL ); + +wait (NULL ); + +ksft_test_result_pass ("%s\n" , __func__); +} +void usage (char *prog) {printf ("Usage: %s\n" , prog);
@@ -502,14 +624,17 @@ int main(int argc, char *argv[]) }ksft_print_header (); -ksft_set_plan (6 ); +ksft_set_plan (8 );test_robustness (); +test_set_robust_list_invalid_size ();test_get_robust_list_self ();test_get_robust_list_child ();test_set_list_op_pending ();test_robust_list_multiple_elements (); +test_robust_list_limit (); +test_circular_list ();ksft_print_cnts ();return 0 ;
Signature · 1 line
2.48.0
* André Almeida:
Quoted 4 lines
As requested by Peter at [1], this patchset drops the
ROBUST_LIST_LIMIT. This is achieve by simply rewriting the processed list element ->next to point to thehead->listaddress, destroying the linked list to avoid any circular list.
Doesn't this turn a robust mutex overwrite or a TCB overwrite into a write-anything-anywhere primitive? Furthermore, I'm not entirely sure if this is entirely backwards-compatible.
Could you use the tortoise/hare approach instead?
Thanks,
Florian
André Almeida
reviewerHi Florian,
Em 28/01/2025 04:50, Florian Weimer escreveu:
Quoted 11 lines
* André Almeida:
Quoted 4 lines
As requested by Peter at [1], this patchset drops the
ROBUST_LIST_LIMIT. This is achieve by simply rewriting the processed list element ->next to point to thehead->listaddress, destroying the linked list to avoid any circular list.Doesn't this turn a robust mutex overwrite or a TCB overwrite into a write-anything-anywhere primitive? Furthermore, I'm not entirely sure if this is entirely backwards-compatible.
The robust list is meant to be a private resource, per-process, and this patch only rewrites it after the process exits, so I believe that any changes done in this memory should be safe given that the process will soon disappear anyway, right?
Do you think you can point out a scenario that wouldn't be backwards-compatible? I would like to try to test it.
Quoted 2 lines
Could you use the tortoise/hare approach instead?
I believe that you want the approach to be "slow and steady" but I'm not sure what you have in mind, if you could you please elaborate :)
Quoted 3 lines
Thanks,
Florian
* André Almeida:
Quoted 14 lines
Hi Florian,
Em 28/01/2025 04:50, Florian Weimer escreveu:
Quoted 10 lines
* André Almeida:
Quoted 4 lines
As requested by Peter at [1], this patchset drops the
ROBUST_LIST_LIMIT. This is achieve by simply rewriting the processed list element ->next to point to thehead->listaddress, destroying the linked list to avoid any circular list.Doesn't this turn a robust mutex overwrite or a TCB overwrite into a write-anything-anywhere primitive? Furthermore, I'm not entirely sure if this is entirely backwards-compatible.
Quoted 4 lines
The robust list is meant to be a private resource, per-process, and this patch only rewrites it after the process exits, so I believe that any changes done in this memory should be safe given that the process will soon disappear anyway, right?
At least in the glibc implementation, we let the kernel handle robust mutex notification on thread exit, and that's observable.
Beyond that, process-shared robust mutexes exist, too, and those updates will be observable, too.
Quoted 2 lines
Do you think you can point out a scenario that wouldn't be backwards-compatible? I would like to try to test it.
I think it should be okay for the glibc implementation. The robust list is libc-owned (at least in glibc implementation), so it should not matter, but the are other libs out there.
Quoted 4 lines
Quoted 1 line
Could you use the tortoise/hare approach instead?
I believe that you want the approach to be "slow and steady" but I'm not sure what you have in mind, if you could you please elaborate :)
I meant cycle detection using Floyd's algorithm.
Thanks,
Florian
Peter Zijlstra
reviewerQuoted 8 lines · Florian Weimer
Quoted 7 lines
Quoted 2 lines
Doesn't this turn a robust mutex overwrite or a TCB overwrite into a write-anything-anywhere primitive?
The robust list is meant to be a private resource, per-process, and this patch only rewrites it after the process exits, so I believe that any changes done in this memory should be safe given that the process will soon disappear anyway, right?
Quoted 5 lines
At least in the glibc implementation, we let the kernel handle robust mutex notification on thread exit, and that's observable.
Beyond that, process-shared robust mutexes exist, too, and those updates will be observable, too.
AFAICT we don't allow writing anywhere we couldn't already. The process shared things should be in shared memory, something we can already write to.
Notably, the kernel doesn't change address space while walking the robust list (the robust list doesn't even contain enough information to do this, even if we wanted to), it can only write to things the dying task could already write to.
So I don't think there is a security angle here. Yes userspace can shoot itself in the foot with this, but what else is new.
Peter Zijlstra
reviewerQuoted 6 lines · Florian Weimer
* André Almeida:
Quoted 4 lines
As requested by Peter at [1], this patchset drops the
ROBUST_LIST_LIMIT. This is achieve by simply rewriting the processed list element ->next to point to thehead->listaddress, destroying the linked list to avoid any circular list.
Well, I suggested we do this for a new robust list.
Quoted 2 lines
Furthermore, I'm not entirely sure
if this is entirely backwards-compatible.
I share Florian's concern about backward compat here. It might work, it might not.
I was just saying that if we're going to be doing new robust lists, we should try and fix all the known wrongs, and this one lets us get rid of the limit.
Quoted 1 line
Could you use the tortoise/hare approach instead?
That seems overly complicated for what we need.
André Almeida
reviewerEm 03/02/2025 10:29, Peter Zijlstra escreveu:
Quoted 10 lines · Florian Weimer
Quoted 6 lines
* André Almeida:
Quoted 4 lines
As requested by Peter at [1], this patchset drops the
ROBUST_LIST_LIMIT. This is achieve by simply rewriting the processed list element ->next to point to thehead->listaddress, destroying the linked list to avoid any circular list.Well, I suggested we do this for a new robust list.
Quoted 9 lines
Quoted 2 lines
Furthermore, I'm not entirely sure
if this is entirely backwards-compatible.I share Florian's concern about backward compat here. It might work, it might not.
I was just saying that if we're going to be doing new robust lists, we should try and fix all the known wrongs, and this one lets us get rid of the limit.
Oh, now I see, thanks for the clarification. I will add this for the new interface then.
In the meanwhile, if you can a look at patch 3/4 "selftests/futex: Create test for robust list" that would help to make sure the new interface don't mess with the old one.