selftests/bpf: fix possible hang in sockopt_inherit
Subsystems (5)
PATCH 1/1 selftests/bpf: fix possible hang in sockopt_inherit
Andrii reported that sockopt_inherit occasionally hangs up on 5.5 kernel [0]. This can happen if server_thread runs faster than the main thread. In that case, pthread_cond_wait will wait forever because pthread_cond_signal was executed before the main thread was blocking. Let's move pthread_mutex_lock up a bit to make sure server_thread runs strictly after the main thread goes to sleep.
(Not sure why this is 5.5 specific, maybe scheduling is less deterministic? But I was able to confirm that it does indeed happen in a VM.)
[0] https://lore.kernel.org/bpf/CAEf4BzY0-bVNHmCkMFPgObs=isUAyg-dFzGDY7QWYkmm7rmTSg@mail.gmail.com/
--- tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c +1 -2
@@ -193,11 +193,10 @@ static void run_test(int cgroup_fd)if (CHECK_FAIL (server_fd <0 )) goto close_bpf_object; +pthread_mutex_lock (&server_started_mtx);if (CHECK_FAIL (pthread_create (&tid,NULL , server_thread, (void *)&server_fd))) goto close_server_fd; - -pthread_mutex_lock (&server_started_mtx);pthread_cond_wait (&server_started, &server_started_mtx);pthread_mutex_unlock (&server_started_mtx);
Signature · 1 line
2.27.0.389.gc38d7665816-goog
Quoted 12 lines · Stanislav Fomichev
Andrii reported that
sockopt_inheritoccasionally hangs up on 5.5 kernel [0]. This can happen ifserver_threadruns faster than the main thread. In that case,pthread_cond_waitwill wait forever becausepthread_cond_signalwas executed before the main thread was blocking. Let's movepthread_mutex_lockup a bit to make sureserver_threadruns strictly after the main thread goes to sleep.(Not sure why this is 5.5 specific, maybe scheduling is less deterministic? But I was able to confirm that it does indeed happen in a VM.)
Quoted 5 lines
[0] https://lore.kernel.org/bpf/CAEf4BzY0-bVNHmCkMFPgObs=isUAyg-dFzGDY7QWYkmm7rmTSg@mail.gmail.com/
Reported-by Andrii Nakryiko <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>---
Great, thanks for figuring this out! Hopefully this is it.
Quoted 23 lines
tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c +1
-3@@ -193,11 +193,10 @@ static void run_test(int cgroup_fd)if (CHECK_FAIL (server_fd <0 )) goto close_bpf_object; +pthread_mutex_lock (&server_started_mtx);if (CHECK_FAIL (pthread_create (&tid,NULL , server_thread, (void *)&server_fd))) goto close_server_fd; - -pthread_mutex_lock (&server_started_mtx);pthread_cond_wait (&server_started, &server_started_mtx);pthread_mutex_unlock (&server_started_mtx); --2.27.0.389.gc38d7665816-goog
Quoted 7 lines · Stanislav Fomichev
Andrii reported that
sockopt_inheritoccasionally hangs up on 5.5 kernel [0]. This can happen ifserver_threadruns faster than the main thread. In that case,pthread_cond_waitwill wait forever becausepthread_cond_signalwas executed before the main thread was blocking. Let's movepthread_mutex_lockup a bit to make sureserver_threadruns strictly after the main thread goes to sleep.
Quoted 8 lines
(Not sure why this is 5.5 specific, maybe scheduling is less deterministic? But I was able to confirm that it does indeed happen in a VM.)
[0] https://lore.kernel.org/bpf/CAEf4BzY0-bVNHmCkMFPgObs=isUAyg-dFzGDY7QWYkmm7rmTSg@mail.gmail.com/
Reported-by Andrii Nakryiko <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>
Applied, thanks!