bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Krzesimir Nowak <krzesimir@kinvolk.io>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: "open list" <linux-kernel@vger.kernel.org>,
	"Alban Crequy" <alban@kinvolk.io>,
	"Iago López Galeiras" <iago@kinvolk.io>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Martin KaFai Lau" <kafai@fb.com>,
	"Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>,
	"David S. Miller" <davem@davemloft.net>,
	"Jakub Kicinski" <jakub.kicinski@netronome.com>,
	"Jesper Dangaard Brouer" <hawk@kernel.org>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"Stanislav Fomichev" <sdf@google.com>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	xdp-newbies@vger.kernel.org
Subject: Re: [bpf-next v3 05/12] selftests/bpf: Allow passing more information to BPF prog test run
Date: Thu, 11 Jul 2019 14:17:44 +0200	[thread overview]
Message-ID: <CAGGp+cHoujaAF_DSFitwgV3sshjj6_q6CL_hKPZUDhZC825PUQ@mail.gmail.com> (raw)
In-Reply-To: <CAEf4BzYYdrcwJKg271ZL7kPJNYyZEGdxQeuUNbfPk=EjewuHeQ@mail.gmail.com>

On Thu, Jul 11, 2019 at 3:17 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Mon, Jul 8, 2019 at 3:42 PM Krzesimir Nowak <krzesimir@kinvolk.io> wrote:
> >
> > The test case can now specify a custom length of the data member,
> > context data and its length, which will be passed to
> > bpf_prog_test_run_xattr. For backward compatilibity, if the data
> > length is 0 (which is what will happen when the field is left
> > unspecified in the designated initializer of a struct), then the
> > length passed to the bpf_prog_test_run_xattr is TEST_DATA_LEN.
> >
> > Also for backward compatilibity, if context data length is 0, NULL is
> > passed as a context to bpf_prog_test_run_xattr. This is to avoid
> > breaking other tests, where context data being NULL and context data
> > length being 0 is handled differently from the case where context data
> > is not NULL and context data length is 0.
> >
> > Custom lengths still can't be greater than hardcoded 64 bytes for data
> > and 192 for context data.
> >
> > 192 for context data was picked to allow passing struct
> > bpf_perf_event_data as a context for perf event programs. The struct
> > is quite large, because it contains struct pt_regs.
> >
> > Test runs for perf event programs will not allow the copying the data
> > back to data_out buffer, so they require data_out_size to be zero and
> > data_out to be NULL. Since test_verifier hardcodes it, make it
> > possible to override the size. Overriding the size to zero will cause
> > the buffer to be NULL.
> >
> > Changes since v2:
> > - Allow overriding the data out size and buffer.
> >
> > Signed-off-by: Krzesimir Nowak <krzesimir@kinvolk.io>
> > ---
> >  tools/testing/selftests/bpf/test_verifier.c | 105 +++++++++++++++++---
> >  1 file changed, 93 insertions(+), 12 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/test_verifier.c b/tools/testing/selftests/bpf/test_verifier.c
> > index 1640ba9f12c1..6f124cc4ee34 100644
> > --- a/tools/testing/selftests/bpf/test_verifier.c
> > +++ b/tools/testing/selftests/bpf/test_verifier.c
> > @@ -54,6 +54,7 @@
> >  #define MAX_TEST_RUNS  8
> >  #define POINTER_VALUE  0xcafe4all
> >  #define TEST_DATA_LEN  64
> > +#define TEST_CTX_LEN   192
> >
> >  #define F_NEEDS_EFFICIENT_UNALIGNED_ACCESS     (1 << 0)
> >  #define F_LOAD_WITH_STRICT_ALIGNMENT           (1 << 1)
> > @@ -96,7 +97,12 @@ struct bpf_test {
> >         enum bpf_prog_type prog_type;
> >         uint8_t flags;
> >         __u8 data[TEST_DATA_LEN];
> > +       __u32 data_len;
> > +       __u8 ctx[TEST_CTX_LEN];
> > +       __u32 ctx_len;
> >         void (*fill_helper)(struct bpf_test *self);
> > +       bool override_data_out_len;
> > +       __u32 overridden_data_out_len;
> >         uint8_t runs;
> >         struct {
> >                 uint32_t retval, retval_unpriv;
> > @@ -104,6 +110,9 @@ struct bpf_test {
> >                         __u8 data[TEST_DATA_LEN];
> >                         __u64 data64[TEST_DATA_LEN / 8];
> >                 };
> > +               __u32 data_len;
> > +               __u8 ctx[TEST_CTX_LEN];
> > +               __u32 ctx_len;
> >         } retvals[MAX_TEST_RUNS];
> >  };
> >
> > @@ -818,21 +827,35 @@ static int set_admin(bool admin)
> >  }
> >
> >  static int do_prog_test_run(int fd_prog, bool unpriv, uint32_t expected_val,
> > -                           void *data, size_t size_data)
> > +                           void *data, size_t size_data, void *ctx,
> > +                           size_t size_ctx, u32 *overridden_data_out_size)
> >  {
> > -       __u8 tmp[TEST_DATA_LEN << 2];
> > -       __u32 size_tmp = sizeof(tmp);
> > -       int saved_errno;
> > -       int err;
> >         struct bpf_prog_test_run_attr attr = {
> >                 .prog_fd = fd_prog,
> >                 .repeat = 1,
> >                 .data_in = data,
> >                 .data_size_in = size_data,
> > -               .data_out = tmp,
> > -               .data_size_out = size_tmp,
> > +               .ctx_in = ctx,
> > +               .ctx_size_in = size_ctx,
> >         };
> > +       __u8 tmp[TEST_DATA_LEN << 2];
> > +       __u32 size_tmp = sizeof(tmp);
> > +       __u32 size_buf = size_tmp;
> > +       __u8 *buf = tmp;
> > +       int saved_errno;
> > +       int err;
> >
> > +       if (overridden_data_out_size)
> > +               size_buf = *overridden_data_out_size;
> > +       if (size_buf > size_tmp) {
> > +               printf("FAIL: out data size (%d) greater than a buffer size (%d) ",
> > +                      size_buf, size_tmp);
> > +               return -EINVAL;
> > +       }
> > +       if (!size_buf)
> > +               buf = NULL;
> > +       attr.data_size_out = size_buf;
> > +       attr.data_out = buf;
> >         if (unpriv)
> >                 set_admin(true);
> >         err = bpf_prog_test_run_xattr(&attr);
> > @@ -956,13 +979,45 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
> >         if (!alignment_prevented_execution && fd_prog >= 0) {
> >                 uint32_t expected_val;
> >                 int i;
> > +               __u32 size_data;
> > +               __u32 size_ctx;
> > +               bool bad_size;
> > +               void *ctx;
> > +               __u32 *overridden_data_out_size;
> >
> >                 if (!test->runs) {
> > +                       if (test->data_len > 0)
> > +                               size_data = test->data_len;
> > +                       else
> > +                               size_data = sizeof(test->data);
> > +                       if (test->override_data_out_len)
> > +                               overridden_data_out_size = &test->overridden_data_out_len;
> > +                       else
> > +                               overridden_data_out_size = NULL;
> > +                       size_ctx = test->ctx_len;
> > +                       bad_size = false;
>
> I hated all this duplication of logic, which with this patch becomes
> even more expansive, so I removed it. Please see [0]. Can you please
> apply that patch and add all this new logic only once?
>
>   [0] https://patchwork.ozlabs.org/patch/1130601/

Will do.

>
> >                         expected_val = unpriv && test->retval_unpriv ?
> >                                 test->retval_unpriv : test->retval;
> >
> > -                       err = do_prog_test_run(fd_prog, unpriv, expected_val,
> > -                                              test->data, sizeof(test->data));
> > +                       if (size_data > sizeof(test->data)) {
> > +                               printf("FAIL: data size (%u) greater than TEST_DATA_LEN (%lu) ", size_data, sizeof(test->data));
> > +                               bad_size = true;
> > +                       }
> > +                       if (size_ctx > sizeof(test->ctx)) {
> > +                               printf("FAIL: ctx size (%u) greater than TEST_CTX_LEN (%lu) ", size_ctx, sizeof(test->ctx));
>
> These look like way too long lines, wrap them?

Ah, yeah, these can be wrapped easily. Will do.

>
> > +                               bad_size = true;
> > +                       }
> > +                       if (size_ctx)
> > +                               ctx = test->ctx;
> > +                       else
> > +                               ctx = NULL;
>
> nit: single line:
>
> ctx = size_ctx ? test->ctx : NULL;
>
> > +                       if (bad_size)
> > +                               err = 1;
> > +                       else
> > +                               err = do_prog_test_run(fd_prog, unpriv, expected_val,
> > +                                                      test->data, size_data,
> > +                                                      ctx, size_ctx,
> > +                                                      overridden_data_out_size);
> >                         if (err)
> >                                 run_errs++;
> >                         else
> > @@ -970,14 +1025,40 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
> >                 }
> >
> >                 for (i = 0; i < test->runs; i++) {
> > +                       if (test->retvals[i].data_len > 0)
> > +                               size_data = test->retvals[i].data_len;
> > +                       else
> > +                               size_data = sizeof(test->retvals[i].data);
> > +                       if (test->override_data_out_len)
> > +                               overridden_data_out_size = &test->overridden_data_out_len;
> > +                       else
> > +                               overridden_data_out_size = NULL;
> > +                       size_ctx = test->retvals[i].ctx_len;
> > +                       bad_size = false;
> >                         if (unpriv && test->retvals[i].retval_unpriv)
> >                                 expected_val = test->retvals[i].retval_unpriv;
> >                         else
> >                                 expected_val = test->retvals[i].retval;
> >
> > -                       err = do_prog_test_run(fd_prog, unpriv, expected_val,
> > -                                              test->retvals[i].data,
> > -                                              sizeof(test->retvals[i].data));
> > +                       if (size_data > sizeof(test->retvals[i].data)) {
> > +                               printf("FAIL: data size (%u) at run %i greater than TEST_DATA_LEN (%lu) ", size_data, i + 1, sizeof(test->retvals[i].data));
> > +                               bad_size = true;
> > +                       }
> > +                       if (size_ctx > sizeof(test->retvals[i].ctx)) {
> > +                               printf("FAIL: ctx size (%u) at run %i greater than TEST_CTX_LEN (%lu) ", size_ctx, i + 1, sizeof(test->retvals[i].ctx));
> > +                               bad_size = true;
> > +                       }
> > +                       if (size_ctx)
> > +                               ctx = test->retvals[i].ctx;
> > +                       else
> > +                               ctx = NULL;
> > +                       if (bad_size)
> > +                               err = 1;
> > +                       else
> > +                               err = do_prog_test_run(fd_prog, unpriv, expected_val,
> > +                                                      test->retvals[i].data, size_data,
> > +                                                      ctx, size_ctx,
> > +                                                      overridden_data_out_size);
> >                         if (err) {
> >                                 printf("(run %d/%d) ", i + 1, test->runs);
> >                                 run_errs++;
> > --
> > 2.20.1
> >



-- 
Kinvolk GmbH | Adalbertstr.6a, 10999 Berlin | tel: +491755589364
Geschäftsführer/Directors: Alban Crequy, Chris Kühl, Iago López Galeiras
Registergericht/Court of registration: Amtsgericht Charlottenburg
Registernummer/Registration number: HRB 171414 B
Ust-ID-Nummer/VAT ID number: DE302207000

  reply	other threads:[~2019-07-11 12:18 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-08 16:31 [bpf-next v3 00/12] Test the 32bit narrow reads Krzesimir Nowak
2019-07-08 16:31 ` [bpf-next v3 01/12] selftests/bpf: Print a message when tester could not run a program Krzesimir Nowak
2019-07-10 23:44   ` Andrii Nakryiko
2019-07-11 11:36     ` Krzesimir Nowak
2019-07-12  0:10       ` Andrii Nakryiko
2019-07-08 16:31 ` [bpf-next v3 02/12] selftests/bpf: Avoid a clobbering of errno Krzesimir Nowak
2019-07-10 23:51   ` Andrii Nakryiko
2019-07-11 12:04     ` Krzesimir Nowak
2019-07-12  0:59       ` Andrii Nakryiko
2019-07-12 17:31         ` Krzesimir Nowak
2019-07-08 16:31 ` [bpf-next v3 03/12] selftests/bpf: Avoid another case of errno clobbering Krzesimir Nowak
2019-07-10 23:57   ` Andrii Nakryiko
2019-07-11 12:05     ` Krzesimir Nowak
2019-07-08 16:31 ` [bpf-next v3 04/12] selftests/bpf: Use bpf_prog_test_run_xattr Krzesimir Nowak
2019-07-11  0:03   ` Andrii Nakryiko
2019-07-11 12:07     ` Krzesimir Nowak
2019-07-08 16:31 ` [bpf-next v3 05/12] selftests/bpf: Allow passing more information to BPF prog test run Krzesimir Nowak
2019-07-11  1:17   ` Andrii Nakryiko
2019-07-11 12:17     ` Krzesimir Nowak [this message]
2019-07-08 16:31 ` [bpf-next v3 06/12] selftests/bpf: Make sure that preexisting tests for perf event work Krzesimir Nowak
2019-07-08 16:31 ` [bpf-next v3 07/12] tools headers: Adopt compiletime_assert from kernel sources Krzesimir Nowak
2019-07-12  0:19   ` Andrii Nakryiko
2019-07-08 16:31 ` [bpf-next v3 08/12] tools headers: Sync struct bpf_perf_event_data Krzesimir Nowak
2019-07-12  0:21   ` Andrii Nakryiko
2019-07-08 16:31 ` [bpf-next v3 09/12] bpf: Split out some helper functions Krzesimir Nowak
2019-07-08 16:40   ` Krzesimir Nowak
2019-07-11 20:25   ` Stanislav Fomichev
2019-07-08 16:31 ` [bpf-next v3 10/12] bpf: Implement bpf_prog_test_run for perf event programs Krzesimir Nowak
2019-07-11 20:30   ` Stanislav Fomichev
2019-07-08 16:31 ` [bpf-next v3 11/12] selftests/bpf: Add tests for bpf_prog_test_run for perf events progs Krzesimir Nowak
2019-07-12  0:37   ` Andrii Nakryiko
2019-07-12 17:37     ` Krzesimir Nowak
2019-07-12 17:49       ` Andrii Nakryiko
2019-07-08 16:31 ` [bpf-next v3 12/12] selftests/bpf: Test correctness of narrow 32bit read on 64bit field Krzesimir Nowak

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=CAGGp+cHoujaAF_DSFitwgV3sshjj6_q6CL_hKPZUDhZC825PUQ@mail.gmail.com \
    --to=krzesimir@kinvolk.io \
    --cc=alban@kinvolk.io \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=hawk@kernel.org \
    --cc=iago@kinvolk.io \
    --cc=jakub.kicinski@netronome.com \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=sdf@google.com \
    --cc=songliubraving@fb.com \
    --cc=xdp-newbies@vger.kernel.org \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
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;
as well as URLs for NNTP newsgroup(s).