test-ww_mutex: Miscellaneous fixes
v2 cover test-ww_mutex: Miscellaneous fixes
This series fixes:
- module cleanup
- error propagation from stress workers
- transient -EDEADLK handling in test_cycle_work()
- timeout-related false negatives in stress_inorder_work()
The series is re-based on v7.2-rc2 and has been tested on a 96-CPU x86_64 bare-metal system and a 160-CPU arm64 bare-metal system. Both systems completed 100 iterations of tools/testing/selftests/locking/ww_mutex.sh successfully with the self-test as a module. With the self-test as a built-in, both systems survived:
for N in {1..100}; do
echo 1 > /sys/kernel/test_ww_mutex/run_tests
doneThe main differences between the original contribution and this v2, is that the first patch retains the sysfs file when the test is built-in, even in case of error (suggested by John Stultz). There is no change in the second commit, except for John's a-b. In the third commit, the endless spinning acquiring the mutex has been replaced by a modest for-loop, which follows the WW big-rules wrt. contention.
The last patch has an improved commit message.
Håkon Bugge (4):
test-ww_mutex: Fix module cleanup
test-ww_mutex: Report errors from stress workers
test-ww_mutex: Handle transient -EDEADLK in test_cycle_work
test-ww_mutex: Retry lock acquisition after timeout if deadlocked
kernel/locking/test-ww_mutex.c | 68 +++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 14 deletions(-)
--
2.43.5
PATCH v2 1/4 test-ww_mutex: Fix module cleanup
When test-ww_mutex is compiled as a module, there are two bugs. One is related to the case where the module fails to load. In that case, some or all of the allocated objects are not released. Also, if loaded OK and thereafter unloaded, the sysfs file is not removed.
The latter bug gives the following splat (trimmed for better brevity):
sysfs: cannot create duplicate filename '/kernel/test_ww_mutex'
dump_stack_lvl+0x5d/0x90 sysfs_warn_dup.cold+0x17/0x23 sysfs_create_dir_ns+0xe2/0x100 kobject_add_internal+0xba/0x270 kobject_add+0x95/0xd0 kobject_create_and_add+0xa3/0xd0 test_ww_mutex_init+0x90/0xff0 [test_ww_mutex] do_one_initcall+0x5c/0x330 do_init_module+0x90/0x290 __do_sys_init_module+0x195/0x1d0 do_syscall_64+0xe8/0x550 entry_SYSCALL_64_after_hwframe+0x76/0x7e
---
v1 -> v2:
* Keep the sysfs file if module is built-in even when the test fails. This in order to be able to run the test multiple times even in case of errors. Suggested by John Stultz
--- kernel/locking/test-ww_mutex.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-)
kernel/locking/test-ww_mutex.c +17 -10
@@ -734,7 +734,7 @@ static struct kobject *test_ww_mutex_kobj;static int __init test_ww_mutex_init (void ) { -int ret; +int ret = -ENOMEM ;prandom_seed_state (&rng,get_random_u64 ());
@@ -743,28 +743,35 @@ static int __init test_ww_mutex_init(void)return -ENOMEM ; test_ww_mutex_kobj =kobject_create_and_add ("test_ww_mutex" , kernel_kobj); -if (!test_ww_mutex_kobj) { -destroy_workqueue (wq); -return -ENOMEM ; - } +if (!test_ww_mutex_kobj) + goto kobj_err;/* Create the files associated with this kobject */ ret =sysfs_create_group (test_ww_mutex_kobj, &attr_group); -if (ret) { -kobject_put (test_ww_mutex_kobj); -destroy_workqueue (wq); -return ret; - } +if (ret) + goto sysfs_err;mutex_lock (&run_lock); ret =run_test_classes ();mutex_unlock (&run_lock); +if (!ret ||IS_BUILTIN (CONFIG_WW_MUTEX_SELFTEST )) +return 0 ; + +sysfs_remove_group (test_ww_mutex_kobj, &attr_group); + +sysfs_err: +kobject_put (test_ww_mutex_kobj); + +kobj_err: +destroy_workqueue (wq); +return ret; } static void __exit test_ww_mutex_exit(void) { +sysfs_remove_group (test_ww_mutex_kobj, &attr_group);kobject_put (test_ww_mutex_kobj);destroy_workqueue (wq); }
Signature · 1 line
2.43.5
PATCH v2 2/4 test-ww_mutex: Report errors from stress workers
In reply to Håkon BuggeIf any of stress_one_work(), stress_reorder_work(), or stress_inorder_work() fails, the error is not conveyed back to the test harness function stress().
We may then see in the log:
stress (stress_inorder_work) failed with -35
Beginning ww (die) mutex selftests
All ww mutex selftests passed
Besides being a contradiction by terms, the failure to return error from said worker functions leads to false-positive results.
We fix that by adding a *result* variable to *struct stress*, update it in said worker functions, and finally, in stress(), scan the *stress_array* and report the first error if any.
---
v1 -> v2:
* Added John's a-b
--- kernel/locking/test-ww_mutex.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
kernel/locking/test-ww_mutex.c +19 -2
@@ -392,6 +392,7 @@ struct stress {struct ww_class *class;unsigned long timeout;int nlocks; +int result; };struct rnd_state rng;
@@ -440,6 +441,9 @@ static void stress_inorder_work(struct work_struct *work)struct ww_mutex *locks = stress->locks;struct ww_acquire_ctx ctx;int *order; +int err; + + stress->result = -ENOMEM ; order =get_random_order (nlocks);if (!order)
@@ -447,7 +451,7 @@ static void stress_inorder_work(struct work_struct *work) do { int contended = -1; - int n, err; + int n; ww_acquire_init(&ctx, stress->class); retry:
@@ -485,6 +489,7 @@ static void stress_inorder_work(struct work_struct *work) }while (!time_after (jiffies, stress->timeout));kfree (order); + stress->result = err; }struct reorder_lock {
@@ -501,6 +506,8 @@ static void stress_reorder_work(struct work_struct *work)int *order;int n, err; + stress->result = -ENOMEM ; + order =get_random_order (stress->nlocks);if (!order)return ;
@@ -545,6 +552,8 @@ static void stress_reorder_work(struct work_struct *work)ww_acquire_fini (&ctx); }while (!time_after (jiffies, stress->timeout)); + stress->result = err; + out:list_for_each_entry_safe (ll, ln, &locks, link)kfree (ll);
@@ -569,6 +578,8 @@ static void stress_one_work(struct work_struct *work)break ; } }while (!time_after (jiffies, stress->timeout)); + + stress->result = err; }#define STRESS_INORDER BIT(0)
@@ -581,6 +592,7 @@ static int stress(struct ww_class *class, int nlocks, int nthreads, unsigned intstruct ww_mutex *locks;struct stress *stress_array;int n, count; +int res =0 ; locks =kmalloc_objs (*locks, nlocks);if (!locks)
@@ -635,10 +647,15 @@ static int stress(struct ww_class *class, int nlocks, int nthreads, unsigned intfor (n =0 ; n < nlocks; n++)ww_mutex_destroy (&locks[n]); + +/* Report the first error */ +for (n =0 ; !res && n < count; n++) + res = stress_array[n].result; +kfree (stress_array);kfree (locks); -return 0 ; +return res; }static int run_tests (struct ww_class *class)
Signature · 1 line
2.43.5
PATCH v2 3/4 test-ww_mutex: Handle transient -EDEADLK in test_cycle_work
In reply to Håkon BuggeThere is a timing issue in test_cycle_work(), in the sense that acquiring *a_mutex* after deadlock has been detected on the *b_mutex*, may not succeed immediately. This may lead to false negatives, which shows up in the log as:
cyclic deadlock not resolved, ret[77/93] = -35
We re-factor the inner part test_cycle_work(), where we loop a few times attempting to acquire the mutexes in BAAB order. If the first A succeeds or the last B succeeds, we break out of the loop.
---
v1 -> v2:
* Removed the end-less lock acquisition attempts, as that strategy did not obey the big-rules of the WW mutexes. Instead, re-factor the test, and loop a handful times.
--- kernel/locking/test-ww_mutex.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
kernel/locking/test-ww_mutex.c +16 -1
@@ -294,6 +294,8 @@ static void test_cycle_work(struct work_struct *work)struct test_cycle *cycle =container_of (work,typeof (*cycle), work);struct ww_acquire_ctx ctx;int err, erra =0 ; +const int max_attempts =5 ; +int attempt;ww_acquire_init_noinject (&ctx, cycle->class);ww_mutex_lock (&cycle->a_mutex, &ctx);
@@ -302,13 +304,26 @@ static void test_cycle_work(struct work_struct *work)wait_for_completion (&cycle->b_signal); err =ww_mutex_lock (cycle->b_mutex, &ctx); -if (err == -EDEADLK ) { +if (err != -EDEADLK ) + goto ok; + +for (attempt =0 ; attempt < max_attempts; ++attempt) { err =0 ;ww_mutex_unlock (&cycle->a_mutex);ww_mutex_lock_slow (cycle->b_mutex, &ctx); erra =ww_mutex_lock (&cycle->a_mutex, &ctx); +if (erra != -EDEADLK ) +break ; + + erra =0 ; +ww_mutex_unlock (cycle->b_mutex); +ww_mutex_lock_slow (&cycle->a_mutex, &ctx); + err =ww_mutex_lock (cycle->b_mutex, &ctx); +if (err != -EDEADLK ) +break ; } +ok:if (!err)ww_mutex_unlock (cycle->b_mutex);if (!erra)
Signature · 1 line
2.43.5
Re: [PATCH v2 3/4] test-ww_mutex: Handle transient -EDEADLK in test_cycle_work
Quoted 8 lines · Håkon Bugge
There is a timing issue in
test_cycle_work(), in the sense that acquiring *a_mutex* after deadlock has been detected on the *b_mutex*, may not succeed immediately. This may lead to false negatives, which shows up in the log as:cyclic deadlock not resolved, ret[77/93] = -35
Quoted 8 lines
We re-factor the inner part
test_cycle_work(), where we loop a few times attempting to acquire the mutexes in BAAB order. If the first A succeeds or the last B succeeds, we break out of the loop.Fixes d1b42b800e5d ("locking/ww_mutex: Add kselftests for resolving ww_mutex cyclic deadlocks")Fixes e4a02ed2aaf4 ("locking/ww_mutex: Fix runtime warning in the WW mutex selftest")Signed-off-by Håkon Bugge <[email protected]>
I've been running stress testing with this for a while as part of my proxy-exec v31 submission, and I've not run into any issues so far.
This version definitely avoids my concern with the earlier one, so thanks for the update.
One tiny nit below, but I wouldn't be upset if this went in unchanged.
Quoted 25 lines
---
v1 -> v2:
* Removed the end-less lock acquisition attempts, as that strategy did not obey the big-rules of the WW mutexes. Instead, re-factor the test, and loop a handful times.--- kernel/locking/test-ww_mutex.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)kernel/locking/test-ww_mutex.c +2
-0@@ -294,6 +294,8 @@ static void test_cycle_work(struct work_struct *work)struct test_cycle *cycle =container_of (work,typeof (*cycle), work);struct ww_acquire_ctx ctx;int err, erra =0 ; +const int max_attempts =5 ; +int attempt;ww_acquire_init_noinject (&ctx, cycle->class);ww_mutex_lock (&cycle->a_mutex, &ctx);
kernel/locking/test-ww_mutex.c static void test_cycle_work(struct work_struct *work) · line 308
Quoted 6 lines
kernel/locking/test-ww_mutex.c +2
-1@@ -302,13 +304,26 @@ static void test_cycle_work(struct work_struct *work)wait_for_completion (&cycle->b_signal); err =ww_mutex_lock (cycle->b_mutex, &ctx); -if (err == -EDEADLK ) { +if (err != -EDEADLK ) + goto ok;
Nit: I'm not sure if "ok" is a great label for ww_mutex_lock returning an error. "out" might be more intuitive?
thanks
-john
Re: [PATCH v2 3/4] test-ww_mutex: Handle transient -EDEADLK in test_cycle_work
Quoted 25 lines · John Stultz
Quoted 8 lines · Håkon Bugge
There is a timing issue in
test_cycle_work(), in the sense that acquiring *a_mutex* after deadlock has been detected on the *b_mutex*, may not succeed immediately. This may lead to false negatives, which shows up in the log as:cyclic deadlock not resolved, ret[77/93] = -35
Quoted 8 lines
We re-factor the inner part
test_cycle_work(), where we loop a few times attempting to acquire the mutexes in BAAB order. If the first A succeeds or the last B succeeds, we break out of the loop.Fixes d1b42b800e5d ("locking/ww_mutex: Add kselftests for resolving ww_mutex cyclic deadlocks")Fixes e4a02ed2aaf4 ("locking/ww_mutex: Fix runtime warning in the WW mutex selftest")Signed-off-by Håkon Bugge <[email protected]>I've been running stress testing with this for a while as part of my proxy-exec v31 submission, and I've not run into any issues so far.
Quoted 6 lines
This version definitely avoids my concern with the earlier one, so thanks for the update.
One tiny nit below, but I wouldn't be upset if this went in unchanged.
Acked-by John Stultz <[email protected]>
Thanks for the a-b. Yes, my idea of a recursive solution, which became a tail recursion, could then be translated into this simple loop. I agree, much cleaner!
As to the "ok:" label, I chose that name because we did not have deadlock. But of course, it could be other errors, so will fix in v3.
I found another error here, that when running with N + 1 threads (where N is number of CPUs online) and N is 256 or 512, the N+1'th thread never executes. Related to the default unbound-workqueue active budget. I have a fix, but will send that separate from this series.
Thxs, Håkon
Re: [PATCH v2 3/4] test-ww_mutex: Handle transient -EDEADLK in test_cycle_work
In reply to John StultzQuoted 6 lines · John Stultz
I've been running stress testing with this for a while as part of my proxy-exec v31 submission, and I've not run into any issues so far.
I did a test with your proxy-exec-v31-7.2-rc4 tag, and I did not see anything wrong. One nit though, the kernel does not compile without CONFIG_SCHED_PROXY_EXEC, because you do not have the pre-decl for find_proxy_task() in that case.
Thxs, Håkon
Re: [PATCH v2 3/4] test-ww_mutex: Handle transient -EDEADLK in test_cycle_work
Quoted 9 lines · Haakon Bugge
Quoted 4 lines · John Stultz
I've been running stress testing with this for a while as part of my proxy-exec v31 submission, and I've not run into any issues so far.
I did a test with your proxy-exec-v31-7.2-rc4 tag, and I did not see anything wrong. One nit though, the kernel does not compile without
CONFIG_SCHED_PROXY_EXEC, because you do not have the pre-decl forfind_proxy_task()in that case.
Ah, thanks for the heads up!
Yeah, I missed !CONFIG_SCHED_PROXY_EXEC + CONFIG_CORE_SCHED in my test build coverage. The issue isn't in the series I was submitting, but is from the new (and very cool) patches from K Prateek to get core scheduling working better with proxy-exec that I included in my full stack. I've added a quick fix to the top of that branch and will get it folded down properly in my WIP tree for the next version.
thanks
-john
Re: [PATCH v2 3/4] test-ww_mutex: Handle transient -EDEADLK in test_cycle_work
Quoted 5 lines · John Stultz
Ah, thanks for the heads up!
I just realised, that you are not in the maintainers list for test-ww_mutex.c, so both my v3 series [1] and the fix for the deadlock in test_cycle_work() [2] was sent without CCing you.
Let me know if I shall resend or if you can pick'm up from LKML.
Thanks, Håkon
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
PATCH v2 4/4 test-ww_mutex: Retry lock acquisition after timeout if deadlocked
In reply to Håkon Buggestress_inorder_work() terminates when its timeout expires. If the final lock acquisition attempt returns -EDEADLK at that point, the test may report a false failure.
With a high number of threads and few locks, there is a very high probability that a thread will be subject to lock contention. That implies, when the loop times out, we will most probably have been exposed to a -EDEADLK return.
Therefore, we retry a finite number of times after timeout before reporting failure.
We retry up to 10,000 loops after timeout. This value was determined
empirically: 10 and 100 retries were insufficient on the test systems, while 10,000 provided stable results and avoided false negatives.
Without this commit, we may see in the log:
Beginning ww (wound) mutex selftests
stress (stress_inorder_work) failed with -35
---
v1 -> v2:
* Reworded commit message
--- kernel/locking/test-ww_mutex.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/locking/test-ww_mutex.c +2 -1
@@ -457,6 +457,7 @@ static void stress_inorder_work(struct work_struct *work)struct ww_acquire_ctx ctx;int *order;int err; +int attempts_after_tmout =10000 ; stress->result = -ENOMEM ;
@@ -489,7 +490,7 @@ static void stress_inorder_work(struct work_struct *work)ww_mutex_unlock (&locks[order[n]]);if (err == -EDEADLK ) { -if (!time_after (jiffies, stress->timeout)) { +if ((!time_after (jiffies, stress->timeout)) || attempts_after_tmout--) {ww_mutex_lock_slow (&locks[order[contended]], &ctx); goto retry; }
Signature · 1 line
2.43.5
Re: [PATCH v2 4/4] test-ww_mutex: Retry lock acquisition after timeout if deadlocked
Quoted 19 lines · Håkon Bugge
stress_inorder_work()terminates when its timeout expires. If the final lock acquisition attempt returns-EDEADLKat that point, the test may report a false failure.With a high number of threads and few locks, there is a very high probability that a thread will be subject to lock contention. That implies, when the loop times out, we will most probably have been exposed to a
-EDEADLKreturn.Therefore, we retry a finite number of times after timeout before reporting failure.
We retry up to 10,000 loops after timeout. This value was determined
empirically: 10 and 100 retries were insufficient on the test systems, while 10,000 provided stable results and avoided false negatives.
Quoted 7 lines
Without this commit, we may see in the log:
Beginning ww (wound) mutex selftests
stress (stress_inorder_work) failed with -35Fixes cfa92b6d5207 ("locking/ww_mutex/test: Make sure we bail out instead of livelock")Signed-off-by Håkon Bugge <[email protected]>
A gentle ping on this v2 of the series.
Thxs, Håkon
Quoted 32 lines
---
v1 -> v2:
* Reworded commit message
---kernel/locking/test-ww_mutex.c| 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)kernel/locking/test-ww_mutex.c +2
-2@@ -457,6 +457,7 @@ static void stress_inorder_work(struct work_struct *work)struct ww_acquire_ctx ctx;int *order;int err; +int attempts_after_tmout =10000 ; stress->result = -ENOMEM ;@@ -489,7 +490,7 @@ static void stress_inorder_work(struct work_struct *work)ww_mutex_unlock (&locks[order[n]]);if (err == -EDEADLK ) { -if (!time_after (jiffies, stress->timeout)) { +if ((!time_after (jiffies, stress->timeout)) || attempts_after_tmout--) {ww_mutex_lock_slow (&locks[order[contended]], &ctx); goto retry; } --2.43.5
Re: [PATCH v2 4/4] test-ww_mutex: Retry lock acquisition after timeout if deadlocked
In reply to Håkon BuggeQuoted 17 lines · Håkon Bugge
stress_inorder_work()terminates when its timeout expires. If the final lock acquisition attempt returns-EDEADLKat that point, the test may report a false failure.With a high number of threads and few locks, there is a very high probability that a thread will be subject to lock contention. That implies, when the loop times out, we will most probably have been exposed to a
-EDEADLKreturn.Therefore, we retry a finite number of times after timeout before reporting failure.
We retry up to 10,000 loops after timeout. This value was determined
empirically: 10 and 100 retries were insufficient on the test systems, while 10,000 provided stable results and avoided false negatives.
Quoted 7 lines
Without this commit, we may see in the log:
Beginning ww (wound) mutex selftests
stress (stress_inorder_work) failed with -35Fixes cfa92b6d5207 ("locking/ww_mutex/test: Make sure we bail out instead of livelock")Signed-off-by Håkon Bugge <[email protected]>
So I've utilized this in my testing with proxy-exec v31 and have not seen any practical troubles with it.
However, I still am not very happy with the post-timeout retrys, particularly as it is such a high retry value.
It still feels like we're papering over a problem we don't understand well, and it might be better to leave it failing until we can better articulate what's going on and why this is the right solution.
The rest of the patchset looks great, but I'd maybe drop this one from the series for now.
thanks
-john
Re: [PATCH v2 4/4] test-ww_mutex: Retry lock acquisition after timeout if deadlocked
Quoted 36 lines · John Stultz
Quoted 17 lines · Håkon Bugge
stress_inorder_work()terminates when its timeout expires. If the final lock acquisition attempt returns-EDEADLKat that point, the test may report a false failure.With a high number of threads and few locks, there is a very high probability that a thread will be subject to lock contention. That implies, when the loop times out, we will most probably have been exposed to a
-EDEADLKreturn.Therefore, we retry a finite number of times after timeout before reporting failure.
We retry up to 10,000 loops after timeout. This value was determined
empirically: 10 and 100 retries were insufficient on the test systems, while 10,000 provided stable results and avoided false negatives.Quoted 7 lines
Without this commit, we may see in the log:
Beginning ww (wound) mutex selftests
stress (stress_inorder_work) failed with -35Fixes cfa92b6d5207 ("locking/ww_mutex/test: Make sure we bail out instead of livelock")Signed-off-by Håkon Bugge <[email protected]>So I've utilized this in my testing with proxy-exec v31 and have not seen any practical troubles with it.
However, I still am not very happy with the post-timeout retrys, particularly as it is such a high retry value.
Quoted 6 lines
It still feels like we're papering over a problem we don't understand well, and it might be better to leave it failing until we can better articulate what's going on and why this is the right solution.
The rest of the patchset looks great, but I'd maybe drop this one from the series for now.
I agree. This is not a real fix. On a larger 512 CPU BM system, it still fails. I would suggest to disable this test for now, so that the other stable tests can be regularly executed to avoid regressions.
Thxs, Håkon