From: Stanislav Fomichev <sdf@fomichev.me>
To: Martin Lau <kafai@fb.com>
Cc: Stanislav Fomichev <sdf@google.com>,
"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
"bpf@vger.kernel.org" <bpf@vger.kernel.org>,
"davem@davemloft.net" <davem@davemloft.net>,
"ast@kernel.org" <ast@kernel.org>,
"daniel@iogearbox.net" <daniel@iogearbox.net>
Subject: Re: [PATCH bpf-next v3 1/3] bpf: support input __sk_buff context in BPF_PROG_TEST_RUN
Date: Tue, 9 Apr 2019 11:40:50 -0700 [thread overview]
Message-ID: <20190409184050.GM7431@mini-arch.hsd1.ca.comcast.net> (raw)
In-Reply-To: <20190409182500.ezvhvgfrcbdvhd6o@kafai-mbp>
On 04/09, Martin Lau wrote:
> On Tue, Apr 09, 2019 at 10:27:37AM -0700, Stanislav Fomichev wrote:
> > Add new set of arguments to bpf_attr for BPF_PROG_TEST_RUN:
> > * ctx_in/ctx_size_in - input context
> > * ctx_out/ctx_size_out - output context
> >
> > The intended use case is to pass some meta data to the test runs that
> > operate on skb (this has being brought up on recent LPC).
> >
> > For programs that use bpf_prog_test_run_skb, support __sk_buff input and
> > output. Initially, from input __sk_buff, copy _only_ cb and priority into
> > skb, all other non-zero fields are prohibited (with EINVAL).
> > If the user has set ctx_out/ctx_size_out, copy the potentially modified
> > __sk_buff back to the userspace.
> >
> > We require all fields of input __sk_buff except the ones we explicitly
> > support to be set to zero. The expectation is that in the future we might
> > add support for more fields and we want to fail explicitly if the user
> > runs the program on the kernel where we don't yet support them.
> >
> > The API is intentionally vague (i.e. we don't explicitly add __sk_buff
> > to bpf_attr, but ctx_in) to potentially let other test_run types use
> > this interface in the future (this can be xdp_md for xdp types for
> > example).
> >
> > v3:
> > * handle case where ctx_in is NULL, but ctx_out is not [Martin]
> > * convert size==0 checks to ptr==NULL checks and add some extra ptr
> > checks [Martin]
> >
> > v2:
> > * Addressed comments from Martin Lau
> >
> > Cc: Martin Lau <kafai@fb.com>
> > Signed-off-by: Stanislav Fomichev <sdf@google.com>
> > ---
> > include/uapi/linux/bpf.h | 7 ++
> > kernel/bpf/syscall.c | 10 ++-
> > net/bpf/test_run.c | 142 ++++++++++++++++++++++++++++++++++++---
> > 3 files changed, 150 insertions(+), 9 deletions(-)
> >
> > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> > index 837024512baf..8e96f99cebf8 100644
> > --- a/include/uapi/linux/bpf.h
> > +++ b/include/uapi/linux/bpf.h
> > @@ -396,6 +396,13 @@ union bpf_attr {
> > __aligned_u64 data_out;
> > __u32 repeat;
> > __u32 duration;
> > + __u32 ctx_size_in; /* input: len of ctx_in */
> > + __u32 ctx_size_out; /* input/output: len of ctx_out
> > + * returns ENOSPC if ctx_out
> > + * is too small.
> > + */
> > + __aligned_u64 ctx_in;
> > + __aligned_u64 ctx_out;
> > } test;
> >
> > struct { /* anonymous struct used by BPF_*_GET_*_ID */
> > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> > index 1d65e56594db..5bb963e8f9b0 100644
> > --- a/kernel/bpf/syscall.c
> > +++ b/kernel/bpf/syscall.c
> > @@ -1949,7 +1949,7 @@ static int bpf_prog_query(const union bpf_attr *attr,
> > return cgroup_bpf_prog_query(attr, uattr);
> > }
> >
> > -#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
> > +#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
> >
> > static int bpf_prog_test_run(const union bpf_attr *attr,
> > union bpf_attr __user *uattr)
> > @@ -1962,6 +1962,14 @@ static int bpf_prog_test_run(const union bpf_attr *attr,
> > if (CHECK_ATTR(BPF_PROG_TEST_RUN))
> > return -EINVAL;
> >
> > + if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
> > + (!attr->test.ctx_size_in && attr->test.ctx_in))
> > + return -EINVAL;
> > +
> > + if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
> > + (!attr->test.ctx_size_out && attr->test.ctx_out))
> > + return -EINVAL;
> > +
> > prog = bpf_prog_get(attr->test.prog_fd);
> > if (IS_ERR(prog))
> > return PTR_ERR(prog);
> > diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
> > index fab142b796ef..2023d8841c6d 100644
> > --- a/net/bpf/test_run.c
> > +++ b/net/bpf/test_run.c
> > @@ -123,12 +123,125 @@ static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
> > return data;
> > }
> >
> > +static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size)
> > +{
> > + void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in);
> > + void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
> > + u32 size = kattr->test.ctx_size_in;
> > + void *data;
> > + int err;
> > +
> > + if (!data_in && !data_out)
> > + return NULL;
> > +
> > + data = kzalloc(max_size, GFP_USER);
> > + if (!data)
> > + return ERR_PTR(-ENOMEM);
> > +
> > + if (data_in) {
> > + err = bpf_check_uarg_tail_zero(data_in, max_size, size);
> > + if (err) {
> > + kfree(data);
> > + return ERR_PTR(err);
> > + }
> > +
> > + if (copy_from_user(data, data_in, size)) {
> A min_t is needed:
> size = min_t(u32, max_size, size);
> if (copy_from_user(data, data_in, size)) {
Ah, sorry about that, good catch! Will respin shortly.
> Others lgtm,
>
> Acked-by: Martin KaFai Lau <kafai@fb.com>
Thank you for a review!
> > + kfree(data);
> > + return ERR_PTR(-EFAULT);
> > + }
> > + }
> > + return data;
> > +}
prev parent reply other threads:[~2019-04-09 18:40 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-09 17:27 [PATCH bpf-next v3 1/3] bpf: support input __sk_buff context in BPF_PROG_TEST_RUN Stanislav Fomichev
2019-04-09 17:27 ` [PATCH bpf-next v3 2/3] libbpf: add support for ctx_{size,}_{in,out} " Stanislav Fomichev
2019-04-09 18:28 ` Martin Lau
2019-04-09 17:27 ` [PATCH bpf-next v3 3/3] selftests: bpf: add selftest for __sk_buff context " Stanislav Fomichev
2019-04-09 18:29 ` Martin Lau
2019-04-09 18:25 ` [PATCH bpf-next v3 1/3] bpf: support input " Martin Lau
2019-04-09 18:40 ` Stanislav Fomichev [this message]
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=20190409184050.GM7431@mini-arch.hsd1.ca.comcast.net \
--to=sdf@fomichev.me \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=kafai@fb.com \
--cc=netdev@vger.kernel.org \
--cc=sdf@google.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).