Skip to contentSkip to conversation

selftests/bpf: test_progs: fix client/server race in tcp_rtt

PATCH Superseded Stanislav Fomichev 3 participants 4 messages Sep 20, 2019 – Sep 23, 2019

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: test_progs: fix client/server race in tcp_rtt

This is the same problem I found earlier in test_sockopt_inherit:
there is a race between server thread doing accept() and client thread doing connect(). Let's explicitly synchronize them via pthread conditional variable.

Fixes b55873984dab ("selftests/bpf: test BPF_SOCK_OPS_RTT_CB")
---
 tools/testing/selftests/bpf/prog_tests/tcp_rtt.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +15 -1
index fdc0b3614a9e..e64058906bcd 100644
@@ -203,6 +203,9 @@ static int start_server(void)
 	return fd;
 }
 
+static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
+
 static void *server_thread(void *arg)
 {
 	struct sockaddr_storage addr;
@@ -215,6 +218,10 @@ static void *server_thread(void *arg)
 		return NULL;
 	}
 
+	pthread_mutex_lock(&server_started_mtx);
+	pthread_cond_signal(&server_started);
+	pthread_mutex_unlock(&server_started_mtx);
+
 	client_fd = accept(fd, (struct sockaddr *)&addr, &len);
 	if (CHECK_FAIL(client_fd < 0)) {
 		perror("Failed to accept client");
@@ -248,7 +255,14 @@ void test_tcp_rtt(void)
 	if (CHECK_FAIL(server_fd < 0))
 		goto close_cgroup_fd;
 
-	pthread_create(&tid, NULL, server_thread, (void *)&server_fd);
+	if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread,
+				      (void *)&server_fd)))
+		goto close_cgroup_fd;
+
+	pthread_mutex_lock(&server_started_mtx);
+	pthread_cond_wait(&server_started, &server_started_mtx);
+	pthread_mutex_unlock(&server_started_mtx);
+
 	CHECK_FAIL(run_test(cgroup_fd, server_fd));
 	close(server_fd);
 close_cgroup_fd:
Signature · 1 line
2.23.0.351.gc4317032e6-goog
#1 lore raw
Quoted 39 lines · Stanislav Fomichev

This is the same problem I found earlier in test_sockopt_inherit:
there is a race between server thread doing accept() and client thread doing connect(). Let's explicitly synchronize them via pthread conditional variable.

Fixes b55873984dab ("selftests/bpf: test BPF_SOCK_OPS_RTT_CB")
---
 tools/testing/selftests/bpf/prog_tests/tcp_rtt.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +7 -0
index fdc0b3614a9e..e64058906bcd 100644
@@ -203,6 +203,9 @@ static int start_server(void)
        return fd;
 }
 
+static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
+
 static void *server_thread(void *arg)
 {
        struct sockaddr_storage addr;
@@ -215,6 +218,10 @@ static void *server_thread(void *arg)
                return NULL;
        }
 
+       pthread_mutex_lock(&server_started_mtx);
+       pthread_cond_signal(&server_started);
+       pthread_mutex_unlock(&server_started_mtx);
+
        client_fd = accept(fd, (struct sockaddr *)&addr, &len);
        if (CHECK_FAIL(client_fd < 0)) {
                perror("Failed to accept client");
@@ -248,7 +255,14 @@ void test_tcp_rtt(void)
        if (CHECK_FAIL(server_fd < 0))

tools/testing/selftests/bpf/prog_tests/tcp_rtt.c void test_tcp_rtt(void) · line 264

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +7 -1
                goto close_cgroup_fd;
 
-       pthread_create(&tid, NULL, server_thread, (void *)&server_fd);
+       if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread,
+                                     (void *)&server_fd)))
+               goto close_cgroup_fd;
+
+       pthread_mutex_lock(&server_started_mtx);
+       pthread_cond_wait(&server_started, &server_started_mtx);
+       pthread_mutex_unlock(&server_started_mtx);

If the server fails to listen, then we'll never get a signal, right? Let's use timedwait instead to avoid test getting stuck forever in such cases?

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +1 -1
+
        CHECK_FAIL(run_test(cgroup_fd, server_fd));
        close(server_fd);
 close_cgroup_fd:
--

2.23.0.351.gc4317032e6-goog

#2 lore raw
Quoted 42 lines · Andrii Nakryiko
Quoted 41 lines · Stanislav Fomichev

This is the same problem I found earlier in test_sockopt_inherit:
there is a race between server thread doing accept() and client thread doing connect(). Let's explicitly synchronize them via pthread conditional variable.

Fixes b55873984dab ("selftests/bpf: test BPF_SOCK_OPS_RTT_CB")
---
 tools/testing/selftests/bpf/prog_tests/tcp_rtt.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +7 -0
index fdc0b3614a9e..e64058906bcd 100644
@@ -203,6 +203,9 @@ static int start_server(void)
        return fd;
 }
 
+static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
+
 static void *server_thread(void *arg)
 {
        struct sockaddr_storage addr;
@@ -215,6 +218,10 @@ static void *server_thread(void *arg)
                return NULL;
        }
 
+       pthread_mutex_lock(&server_started_mtx);
+       pthread_cond_signal(&server_started);
+       pthread_mutex_unlock(&server_started_mtx);
+
        client_fd = accept(fd, (struct sockaddr *)&addr, &len);
        if (CHECK_FAIL(client_fd < 0)) {
                perror("Failed to accept client");
@@ -248,7 +255,14 @@ void test_tcp_rtt(void)
        if (CHECK_FAIL(server_fd < 0))
                goto close_cgroup_fd;
 
Quoted 13 lines

tools/testing/selftests/bpf/prog_tests/tcp_rtt.c void test_tcp_rtt(void) · line 264

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +7 -1
-       pthread_create(&tid, NULL, server_thread, (void *)&server_fd);
+       if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread,
+                                     (void *)&server_fd)))
+               goto close_cgroup_fd;
+
+       pthread_mutex_lock(&server_started_mtx);
+       pthread_cond_wait(&server_started, &server_started_mtx);
+       pthread_mutex_unlock(&server_started_mtx);

If the server fails to listen, then we'll never get a signal, right? Let's use timedwait instead to avoid test getting stuck forever in such cases?

Good point. How about I do the same thing I do in sockopt_inherit tests:
https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c#n73

	err = listen()
	pthread_cond_signal()
	if (CHECK_FAIL(err)) {
		return;
	}

Should fix the problem of getting stuck forever without any timeouts. I'll send a v2 later today.

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +1 -1
+
        CHECK_FAIL(run_test(cgroup_fd, server_fd));
        close(server_fd);
 close_cgroup_fd:
--

2.23.0.351.gc4317032e6-goog

#3 lore raw
Quoted 60 lines · Stanislav Fomichev
Quoted 42 lines · Andrii Nakryiko
Quoted 41 lines · Stanislav Fomichev

This is the same problem I found earlier in test_sockopt_inherit:
there is a race between server thread doing accept() and client thread doing connect(). Let's explicitly synchronize them via pthread conditional variable.

Fixes b55873984dab ("selftests/bpf: test BPF_SOCK_OPS_RTT_CB")
---
 tools/testing/selftests/bpf/prog_tests/tcp_rtt.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +7 -0
index fdc0b3614a9e..e64058906bcd 100644
@@ -203,6 +203,9 @@ static int start_server(void)
        return fd;
 }
 
+static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
+
 static void *server_thread(void *arg)
 {
        struct sockaddr_storage addr;
@@ -215,6 +218,10 @@ static void *server_thread(void *arg)
                return NULL;
        }
 
+       pthread_mutex_lock(&server_started_mtx);
+       pthread_cond_signal(&server_started);
+       pthread_mutex_unlock(&server_started_mtx);
+
        client_fd = accept(fd, (struct sockaddr *)&addr, &len);
        if (CHECK_FAIL(client_fd < 0)) {
                perror("Failed to accept client");
@@ -248,7 +255,14 @@ void test_tcp_rtt(void)
        if (CHECK_FAIL(server_fd < 0))
                goto close_cgroup_fd;
 
Quoted 13 lines

tools/testing/selftests/bpf/prog_tests/tcp_rtt.c void test_tcp_rtt(void) · line 264

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +7 -1
-       pthread_create(&tid, NULL, server_thread, (void *)&server_fd);
+       if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread,
+                                     (void *)&server_fd)))
+               goto close_cgroup_fd;
+
+       pthread_mutex_lock(&server_started_mtx);
+       pthread_cond_wait(&server_started, &server_started_mtx);
+       pthread_mutex_unlock(&server_started_mtx);

If the server fails to listen, then we'll never get a signal, right? Let's use timedwait instead to avoid test getting stuck forever in such cases?

Good point. How about I do the same thing I do in sockopt_inherit tests:
https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c#n73

Quoted 8 lines
        err = listen()
        pthread_cond_signal()
        if (CHECK_FAIL(err)) {
                return;
        }

Should fix the problem of getting stuck forever without any timeouts. I'll send a v2 later today.

Sounds good.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +1 -1
+
        CHECK_FAIL(run_test(cgroup_fd, server_fd));
        close(server_fd);
 close_cgroup_fd:
--

2.23.0.351.gc4317032e6-goog