diff mbox series

[bpf-next,1/3] selftests/bpf: fix race in tcp_rtt test

Message ID 20200314013932.4035712-1-andriin@fb.com
State Accepted
Delegated to: BPF Maintainers
Headers show
Series [bpf-next,1/3] selftests/bpf: fix race in tcp_rtt test | expand

Commit Message

Andrii Nakryiko March 14, 2020, 1:39 a.m. UTC
Previous attempt to make tcp_rtt more robust introduced a new race, in which
server_done might be set to true before server can actually accept any
connection. Fix this by unconditionally waiting for accept(). Given socket is
non-blocking, if there are any problems with client side, it should eventually
close listening FD and let server thread exit with failure.

Fixes: 4cd729fa022c ("selftests/bpf: Make tcp_rtt test more robust to failures")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/testing/selftests/bpf/prog_tests/tcp_rtt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Martin KaFai Lau March 17, 2020, 5:27 a.m. UTC | #1
On Fri, Mar 13, 2020 at 06:39:30PM -0700, Andrii Nakryiko wrote:
> Previous attempt to make tcp_rtt more robust introduced a new race, in which
> server_done might be set to true before server can actually accept any
> connection. Fix this by unconditionally waiting for accept(). Given socket is
> non-blocking, if there are any problems with client side, it should eventually
> close listening FD and let server thread exit with failure.
> 
> Fixes: 4cd729fa022c ("selftests/bpf: Make tcp_rtt test more robust to failures")
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>
> ---
>  tools/testing/selftests/bpf/prog_tests/tcp_rtt.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
> index e08f6bb17700..e56b52ab41da 100644
> --- a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
> +++ b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
> @@ -226,7 +226,7 @@ static void *server_thread(void *arg)
>  		return ERR_PTR(err);
>  	}
>  
> -	while (!server_done) {
> +	while (true) {
>  		client_fd = accept(fd, (struct sockaddr *)&addr, &len);
>  		if (client_fd == -1 && errno == EAGAIN) {
>  			usleep(50);
> @@ -272,7 +272,7 @@ void test_tcp_rtt(void)
>  	CHECK_FAIL(run_test(cgroup_fd, server_fd));
>  
>  	server_done = true;
> -	pthread_join(tid, &server_res);
> +	CHECK_FAIL(pthread_join(tid, &server_res));
From looking at run_test(),
I suspect without accept and server_thread, this may also work:

listen(server_fd, 1);
run_test(cgroup_fd, server_fd);
close(server_fd);

This change lgtm since it is NONBLOCK.  Ideally, may be it should
also be time limited by epoll or setsockopt(SO_RCVTIMEO) in the future.

Acked-by: Martin KaFai Lau <kafai@fb.com>
Daniel Borkmann March 17, 2020, 6:58 p.m. UTC | #2
On 3/14/20 2:39 AM, Andrii Nakryiko wrote:
> Previous attempt to make tcp_rtt more robust introduced a new race, in which
> server_done might be set to true before server can actually accept any
> connection. Fix this by unconditionally waiting for accept(). Given socket is
> non-blocking, if there are any problems with client side, it should eventually
> close listening FD and let server thread exit with failure.
> 
> Fixes: 4cd729fa022c ("selftests/bpf: Make tcp_rtt test more robust to failures")
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>

Series applied, thanks!
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
index e08f6bb17700..e56b52ab41da 100644
--- a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
+++ b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
@@ -226,7 +226,7 @@  static void *server_thread(void *arg)
 		return ERR_PTR(err);
 	}
 
-	while (!server_done) {
+	while (true) {
 		client_fd = accept(fd, (struct sockaddr *)&addr, &len);
 		if (client_fd == -1 && errno == EAGAIN) {
 			usleep(50);
@@ -272,7 +272,7 @@  void test_tcp_rtt(void)
 	CHECK_FAIL(run_test(cgroup_fd, server_fd));
 
 	server_done = true;
-	pthread_join(tid, &server_res);
+	CHECK_FAIL(pthread_join(tid, &server_res));
 	CHECK_FAIL(IS_ERR(server_res));
 
 close_server_fd: