Re: [PATCH bpf-next 6/7] bpf: Add test ops for BPF_PROG_TYPE_TRACING

From: KP Singh
Date: Tue Mar 03 2020 - 17:57:56 EST


On 03-Mär 14:51, Andrii Nakryiko wrote:
> On Tue, Mar 3, 2020 at 6:12 AM KP Singh <kpsingh@xxxxxxxxxxxx> wrote:
> >
> > From: KP Singh <kpsingh@xxxxxxxxxx>
> >
> > The current fexit and fentry tests rely on a different program to
> > exercise the functions they attach to. Instead of doing this, implement
> > the test operations for tracing which will also be used for
> > BPF_OVERRIDE_RETURN in a subsequent patch.
>
> typo: BPF_OVERRIDE_RETURN -> BPF_MODIFY_RETURN?

Oops :) Fixed. Thanks! Artifacts of renaming.

>
> >
> > Also, clean up the fexit test to use the generated skeleton.
> >
> > Signed-off-by: KP Singh <kpsingh@xxxxxxxxxx>
> > ---
>
> Nice clean up for fexit_test, thank you!

It was very satisfying :)

>
> > include/linux/bpf.h | 10 +++
> > kernel/trace/bpf_trace.c | 1 +
> > net/bpf/test_run.c | 38 +++++++---
> > .../selftests/bpf/prog_tests/fentry_fexit.c | 12 +---
> > .../selftests/bpf/prog_tests/fentry_test.c | 14 ++--
> > .../selftests/bpf/prog_tests/fexit_test.c | 69 ++++++-------------
> > 6 files changed, 68 insertions(+), 76 deletions(-)
> >
> > diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> > index 3cfdc216a2f4..c00919025532 100644
> > --- a/include/linux/bpf.h
> > +++ b/include/linux/bpf.h
> > @@ -1156,6 +1156,9 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
> > union bpf_attr __user *uattr);
> > int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
> > union bpf_attr __user *uattr);
> > +int bpf_prog_test_run_tracing(struct bpf_prog *prog,
> > + const union bpf_attr *kattr,
> > + union bpf_attr __user *uattr);
> > int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
> > const union bpf_attr *kattr,
> > union bpf_attr __user *uattr);
> > @@ -1313,6 +1316,13 @@ static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
> > return -ENOTSUPP;
> > }
> >
> > +static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
> > + const union bpf_attr *kattr,
> > + union bpf_attr __user *uattr)
> > +{
> > + return -ENOTSUPP;
> > +}
> > +
> > static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
> > const union bpf_attr *kattr,
> > union bpf_attr __user *uattr)
> > diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> > index 07764c761073..363e0a2c75cf 100644
> > --- a/kernel/trace/bpf_trace.c
> > +++ b/kernel/trace/bpf_trace.c
> > @@ -1266,6 +1266,7 @@ const struct bpf_verifier_ops tracing_verifier_ops = {
> > };
> >
> > const struct bpf_prog_ops tracing_prog_ops = {
> > + .test_run = bpf_prog_test_run_tracing,
> > };
> >
> > static bool raw_tp_writable_prog_is_valid_access(int off, int size,
> > diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
> > index 562443f94133..fb54b45285b4 100644
> > --- a/net/bpf/test_run.c
> > +++ b/net/bpf/test_run.c
> > @@ -160,18 +160,38 @@ static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
> > kfree(data);
> > return ERR_PTR(-EFAULT);
> > }
> > - if (bpf_fentry_test1(1) != 2 ||
> > - bpf_fentry_test2(2, 3) != 5 ||
> > - bpf_fentry_test3(4, 5, 6) != 15 ||
> > - bpf_fentry_test4((void *)7, 8, 9, 10) != 34 ||
> > - bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 ||
> > - bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111) {
> > - kfree(data);
> > - return ERR_PTR(-EFAULT);
> > - }
> > +
> > return data;
> > }
> >
> > +int bpf_prog_test_run_tracing(struct bpf_prog *prog,
> > + const union bpf_attr *kattr,
> > + union bpf_attr __user *uattr)
> > +{
> > + int err = -EFAULT;
> > +
> > + switch (prog->expected_attach_type) {
> > + case BPF_TRACE_FENTRY:
> > + case BPF_TRACE_FEXIT:
> > + if (bpf_fentry_test1(1) != 2 ||
> > + bpf_fentry_test2(2, 3) != 5 ||
> > + bpf_fentry_test3(4, 5, 6) != 15 ||
> > + bpf_fentry_test4((void *)7, 8, 9, 10) != 34 ||
> > + bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 ||
> > + bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111)
> > + goto out;
> > + break;
> > + default:
> > + goto out;
> > + }
>
> No trace_bpf_test_finish here?

Ah yes, we trace it not ony for erroneous cases. Changed it to
setting err = 0 and falling through to the trace_bpf_test_finish.

- KP

>
> > +
> > + return 0;
> > +
> > +out:
> > + trace_bpf_test_finish(&err);
> > + return err;
> > +}
> > +
> > 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);
>
> [...]