futex: Drop ROBUST_LIST_LIMIT
André Almeida
reviewercover futex: Drop ROBUST_LIST_LIMIT
Hey,
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.
Thanks,
André
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
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.47.1
André Almeida
reviewerPATCH 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
@@ -189,8 +189,7 @@ struct robust_list2_entry {#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_list64(struct task_struct *curr, struct robust_list_head __user *head) {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_list64(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.47.1
André Almeida
reviewerPATCH 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.47.1
André Almeida
reviewerPATCH 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
---
Changes from v2:
- Dropped kselftest_harness include, using just futex test API
- 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
Changes from v1:
- Change futex type from int to _Atomic(unsigned int)
- Use old futex(FUTEX_WAIT) instead of the new sys_futex_wait()
--- .../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.47.1
André Almeida
reviewerPATCH 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.47.1