public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: Jakub Sitnicki <jakub@cloudflare•com>
To: Kumar Kartikeya Dwivedi <memxor@gmail•com>
Cc: bpf@vger•kernel.org, "Alexei Starovoitov" <ast@kernel•org>,
	"Daniel Borkmann" <daniel@iogearbox•net>,
	"Andrii Nakryiko" <andrii@kernel•org>,
	"Martin KaFai Lau" <kafai@fb•com>,
	"Song Liu" <songliubraving@fb•com>, "Yonghong Song" <yhs@fb•com>,
	"Jesper Dangaard Brouer" <brouer@redhat•com>,
	"Toke Høiland-Jørgensen" <toke@redhat•com>,
	netdev@vger•kernel.org
Subject: Re: [PATCH bpf-next v1 5/6] bpf: selftests: Fix fd cleanup in sk_lookup test
Date: Wed, 06 Oct 2021 08:49:32 +0200	[thread overview]
Message-ID: <87zgrmfy0z.fsf@cloudflare.com> (raw)
In-Reply-To: <20211006002853.308945-6-memxor@gmail.com>

On Wed, Oct 06, 2021 at 02:28 AM CEST, Kumar Kartikeya Dwivedi wrote:
> Similar to the fix in commit:
> e31eec77e4ab (bpf: selftests: Fix fd cleanup in get_branch_snapshot)
>
> We use memset to set fds to -1 without breaking on future changes to
> the array size (when MAX_SERVER constant is bumped).
>
> The particular close(0) occurs on non-reuseport tests, so it can be seen
> with -n 115/{2,3} but not 115/4. This can cause problems with future
> tests if they depend on BTF fd never being acquired as fd 0, breaking
> internal libbpf assumptions.
>
> Cc: Jakub Sitnicki <jakub@cloudflare•com>
> Fixes: 0ab5539f8584 (selftests/bpf: Tests for BPF_SK_LOOKUP attach point)
> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail•com>
> ---
>  .../selftests/bpf/prog_tests/sk_lookup.c      | 20 +++++++++++++------
>  1 file changed, 14 insertions(+), 6 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
> index aee41547e7f4..572220065bdf 100644
> --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
> +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
> @@ -598,11 +598,14 @@ static void query_lookup_prog(struct test_sk_lookup *skel)
>
>  static void run_lookup_prog(const struct test *t)
>  {
> -	int server_fds[MAX_SERVERS] = { -1 };
>  	int client_fd, reuse_conn_fd = -1;
>  	struct bpf_link *lookup_link;
> +	int server_fds[MAX_SERVERS];
>  	int i, err;
>
> +	/* set all fds to -1 */
> +	memset(server_fds, 0xFF, sizeof(server_fds));
> +
>  	lookup_link = attach_lookup_prog(t->lookup_prog);
>  	if (!lookup_link)
>  		return;
> @@ -663,8 +666,9 @@ static void run_lookup_prog(const struct test *t)
>  	if (reuse_conn_fd != -1)
>  		close(reuse_conn_fd);
>  	for (i = 0; i < ARRAY_SIZE(server_fds); i++) {
> -		if (server_fds[i] != -1)
> -			close(server_fds[i]);
> +		if (server_fds[i] == -1)
> +			break;
> +		close(server_fds[i]);
>  	}
>  	bpf_link__destroy(lookup_link);
>  }
> @@ -1053,11 +1057,14 @@ static void run_sk_assign(struct test_sk_lookup *skel,
>  			  struct bpf_program *lookup_prog,
>  			  const char *remote_ip, const char *local_ip)
>  {
> -	int server_fds[MAX_SERVERS] = { -1 };
> +	int server_fds[MAX_SERVERS];
>  	struct bpf_sk_lookup ctx;
>  	__u64 server_cookie;
>  	int i, err;
>
> +	/* set all fds to -1 */
> +	memset(server_fds, 0xFF, sizeof(server_fds));
> +
>  	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
>  		.ctx_in = &ctx,
>  		.ctx_size_in = sizeof(ctx),
> @@ -1097,8 +1104,9 @@ static void run_sk_assign(struct test_sk_lookup *skel,
>
>  close_servers:
>  	for (i = 0; i < ARRAY_SIZE(server_fds); i++) {
> -		if (server_fds[i] != -1)
> -			close(server_fds[i]);
> +		if (server_fds[i] == -1)
> +			break;
> +		close(server_fds[i]);
>  	}
>  }

My mistake. Thanks for the fix.

Alternatively we can use an extended designated initializer:

int server_fds[] = { [0 ... MAX_SERVERS] = -1 };

Reviewed-by: Jakub Sitnicki <jakub@cloudflare•com>

  reply	other threads:[~2021-10-06  6:49 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-06  0:28 [PATCH bpf-next v1 0/6] Typeless/weak ksym for gen_loader + misc fixups Kumar Kartikeya Dwivedi
2021-10-06  0:28 ` [PATCH bpf-next v1 1/6] bpf: Add bpf_kallsyms_lookup_name helper Kumar Kartikeya Dwivedi
2021-10-07 20:23   ` Song Liu
2021-10-06  0:28 ` [PATCH bpf-next v1 2/6] libbpf: Add typeless and weak ksym support to gen_loader Kumar Kartikeya Dwivedi
2021-10-07 21:45   ` Song Liu
2021-10-07 22:01     ` Kumar Kartikeya Dwivedi
2021-10-07 22:17       ` Song Liu
2021-10-06  0:28 ` [PATCH bpf-next v1 3/6] libbpf: Ensure that module BTF fd is never 0 Kumar Kartikeya Dwivedi
2021-10-06  4:41   ` Andrii Nakryiko
2021-10-06  5:24     ` Kumar Kartikeya Dwivedi
2021-10-06 16:43       ` Andrii Nakryiko
2021-10-06 19:09         ` Alexei Starovoitov
2021-10-06 19:38           ` Andrii Nakryiko
2021-10-07 10:24             ` Toke Høiland-Jørgensen
2021-10-07 18:44               ` Kumar Kartikeya Dwivedi
2021-10-07 21:29                 ` Andrii Nakryiko
2021-10-06  0:28 ` [PATCH bpf-next v1 4/6] bpf: selftests: Move test_ksyms_weak test to lskel, add libbpf test Kumar Kartikeya Dwivedi
2021-10-07 20:33   ` Song Liu
2021-10-07 20:46     ` Kumar Kartikeya Dwivedi
2021-10-07 20:55       ` Kumar Kartikeya Dwivedi
2021-10-07 20:57       ` Song Liu
2021-10-06  0:28 ` [PATCH bpf-next v1 5/6] bpf: selftests: Fix fd cleanup in sk_lookup test Kumar Kartikeya Dwivedi
2021-10-06  6:49   ` Jakub Sitnicki [this message]
2021-10-07 21:48     ` Song Liu
2021-10-06  0:28 ` [PATCH bpf-next v1 6/6] bpf: selftests: Fix memory leak in test_ima Kumar Kartikeya Dwivedi
2021-10-06  4:44   ` Andrii Nakryiko
2021-10-07 21:48     ` Song Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zgrmfy0z.fsf@cloudflare.com \
    --to=jakub@cloudflare$(echo .)com \
    --cc=andrii@kernel$(echo .)org \
    --cc=ast@kernel$(echo .)org \
    --cc=bpf@vger$(echo .)kernel.org \
    --cc=brouer@redhat$(echo .)com \
    --cc=daniel@iogearbox$(echo .)net \
    --cc=kafai@fb$(echo .)com \
    --cc=memxor@gmail$(echo .)com \
    --cc=netdev@vger$(echo .)kernel.org \
    --cc=songliubraving@fb$(echo .)com \
    --cc=toke@redhat$(echo .)com \
    --cc=yhs@fb$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox