netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf] selftests/bpf: test_progs: fix client/server race in tcp_rtt
@ 2019-09-20 23:30 Stanislav Fomichev
  2019-09-22 21:03 ` Andrii Nakryiko
  0 siblings, 1 reply; 4+ messages in thread
From: Stanislav Fomichev @ 2019-09-20 23:30 UTC (permalink / raw)
  To: netdev, bpf; +Cc: davem, ast, daniel, 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")
Signed-off-by: Stanislav Fomichev <sdf@google.com>
---
 tools/testing/selftests/bpf/prog_tests/tcp_rtt.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
index fdc0b3614a9e..e64058906bcd 100644
--- a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
+++ b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
@@ -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:
-- 
2.23.0.351.gc4317032e6-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] selftests/bpf: test_progs: fix client/server race in tcp_rtt
  2019-09-20 23:30 [PATCH bpf] selftests/bpf: test_progs: fix client/server race in tcp_rtt Stanislav Fomichev
@ 2019-09-22 21:03 ` Andrii Nakryiko
  2019-09-23 15:38   ` Stanislav Fomichev
  0 siblings, 1 reply; 4+ messages in thread
From: Andrii Nakryiko @ 2019-09-22 21:03 UTC (permalink / raw)
  To: Stanislav Fomichev
  Cc: Networking, bpf, David S. Miller, Alexei Starovoitov, Daniel Borkmann

On Sun, Sep 22, 2019 at 12:10 PM Stanislav Fomichev <sdf@google.com> wrote:
>
> 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")
> Signed-off-by: Stanislav Fomichev <sdf@google.com>
> ---
>  tools/testing/selftests/bpf/prog_tests/tcp_rtt.c | 16 +++++++++++++++-
>  1 file changed, 15 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
> index fdc0b3614a9e..e64058906bcd 100644
> --- a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
> +++ b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
> @@ -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);


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?


> +
>         CHECK_FAIL(run_test(cgroup_fd, server_fd));
>         close(server_fd);
>  close_cgroup_fd:
> --
> 2.23.0.351.gc4317032e6-goog
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] selftests/bpf: test_progs: fix client/server race in tcp_rtt
  2019-09-22 21:03 ` Andrii Nakryiko
@ 2019-09-23 15:38   ` Stanislav Fomichev
  2019-09-23 16:41     ` Andrii Nakryiko
  0 siblings, 1 reply; 4+ messages in thread
From: Stanislav Fomichev @ 2019-09-23 15:38 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Stanislav Fomichev, Networking, bpf, David S. Miller,
	Alexei Starovoitov, Daniel Borkmann

On 09/22, Andrii Nakryiko wrote:
> On Sun, Sep 22, 2019 at 12:10 PM Stanislav Fomichev <sdf@google.com> wrote:
> >
> > 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")
> > Signed-off-by: Stanislav Fomichev <sdf@google.com>
> > ---
> >  tools/testing/selftests/bpf/prog_tests/tcp_rtt.c | 16 +++++++++++++++-
> >  1 file changed, 15 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
> > index fdc0b3614a9e..e64058906bcd 100644
> > --- a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
> > +++ b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
> > @@ -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);
> 
> 
> 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.

> > +
> >         CHECK_FAIL(run_test(cgroup_fd, server_fd));
> >         close(server_fd);
> >  close_cgroup_fd:
> > --
> > 2.23.0.351.gc4317032e6-goog
> >

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf] selftests/bpf: test_progs: fix client/server race in tcp_rtt
  2019-09-23 15:38   ` Stanislav Fomichev
@ 2019-09-23 16:41     ` Andrii Nakryiko
  0 siblings, 0 replies; 4+ messages in thread
From: Andrii Nakryiko @ 2019-09-23 16:41 UTC (permalink / raw)
  To: Stanislav Fomichev
  Cc: Stanislav Fomichev, Networking, bpf, David S. Miller,
	Alexei Starovoitov, Daniel Borkmann

On Mon, Sep 23, 2019 at 8:38 AM Stanislav Fomichev <sdf@fomichev.me> wrote:
>
> On 09/22, Andrii Nakryiko wrote:
> > On Sun, Sep 22, 2019 at 12:10 PM Stanislav Fomichev <sdf@google.com> wrote:
> > >
> > > 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")
> > > Signed-off-by: Stanislav Fomichev <sdf@google.com>
> > > ---
> > >  tools/testing/selftests/bpf/prog_tests/tcp_rtt.c | 16 +++++++++++++++-
> > >  1 file changed, 15 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
> > > index fdc0b3614a9e..e64058906bcd 100644
> > > --- a/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
> > > +++ b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.c
> > > @@ -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);
> >
> >
> > 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.

Sounds good.


>
> > > +
> > >         CHECK_FAIL(run_test(cgroup_fd, server_fd));
> > >         close(server_fd);
> > >  close_cgroup_fd:
> > > --
> > > 2.23.0.351.gc4317032e6-goog
> > >

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-09-23 16:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-20 23:30 [PATCH bpf] selftests/bpf: test_progs: fix client/server race in tcp_rtt Stanislav Fomichev
2019-09-22 21:03 ` Andrii Nakryiko
2019-09-23 15:38   ` Stanislav Fomichev
2019-09-23 16:41     ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).