mbox series

[bpf-next,0/3] enable BPF_PROG_TEST_RUN for raw_tp

Message ID 20200923012841.2701378-1-songliubraving@fb.com
Headers show
Series enable BPF_PROG_TEST_RUN for raw_tp | expand

Message

Song Liu Sept. 23, 2020, 1:28 a.m. UTC
This set enables BPF_PROG_TEST_RUN for raw_tracepoint type programs. This
set also enables running the raw_tp program on a specific CPU. This feature
can be used by user space to trigger programs that access percpu resources,
e.g. perf_event, percpu variables.

Song Liu (3):
  bpf: enable BPF_PROG_TEST_RUN for raw_tracepoint
  libbpf: introduce bpf_prog_test_run_xattr_opts
  selftests/bpf: add raw_tp_test_run

 include/linux/bpf.h                           |  3 +
 include/uapi/linux/bpf.h                      |  5 ++
 kernel/bpf/syscall.c                          |  2 +-
 kernel/trace/bpf_trace.c                      |  1 +
 net/bpf/test_run.c                            | 90 +++++++++++++++++++
 tools/include/uapi/linux/bpf.h                |  5 ++
 tools/lib/bpf/bpf.c                           | 13 ++-
 tools/lib/bpf/bpf.h                           | 11 +++
 tools/lib/bpf/libbpf.map                      |  1 +
 .../bpf/prog_tests/raw_tp_test_run.c          | 68 ++++++++++++++
 .../bpf/progs/test_raw_tp_test_run.c          | 26 ++++++
 11 files changed, 223 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/raw_tp_test_run.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_raw_tp_test_run.c

--
2.24.1

Comments

John Fastabend Sept. 23, 2020, 4:23 a.m. UTC | #1
Song Liu wrote:
> Add .test_run for raw_tracepoint. Also, introduce a new feature that runs

> the target program on a specific CPU. This is achieved by a new flag in

> bpf_attr.test, cpu_plus. For compatibility, cpu_plus == 0 means run the

> program on current cpu, cpu_plus > 0 means run the program on cpu with id

> (cpu_plus - 1). This feature is needed for BPF programs that handle

> perf_event and other percpu resources, as the program can access these

> resource locally.

> 

> Signed-off-by: Song Liu <songliubraving@fb.com>

> ---


Acked-by: John Fastabend <john.fastabend@gmail.com>


[...]

> +

> +int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,

> +			     const union bpf_attr *kattr,

> +			     union bpf_attr __user *uattr)

> +{

> +	void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);

> +	__u32 ctx_size_in = kattr->test.ctx_size_in;

> +	struct bpf_raw_tp_test_run_info info;

> +	int cpu, err = 0;

> +

> +	/* doesn't support data_in/out, ctx_out, duration, or repeat */

> +	if (kattr->test.data_in || kattr->test.data_out ||

> +	    kattr->test.ctx_out || kattr->test.duration ||

> +	    kattr->test.repeat)

> +		return -EINVAL;

> +

> +	if (ctx_size_in < prog->aux->max_ctx_offset)

> +		return -EINVAL;

> +

> +	if (ctx_size_in) {

> +		info.ctx = kzalloc(ctx_size_in, GFP_USER);

> +		if (!info.ctx)

> +			return -ENOMEM;

> +		if (copy_from_user(info.ctx, ctx_in, ctx_size_in)) {

> +			err = -EFAULT;

> +			goto out;

> +		}

> +	} else {

> +		info.ctx = NULL;

> +	}

> +

> +	info.prog = prog;

> +	cpu = kattr->test.cpu_plus - 1;

> +

> +	if (!kattr->test.cpu_plus || cpu == smp_processor_id()) {

> +		__bpf_prog_test_run_raw_tp(&info);

> +	} else {

> +		/* smp_call_function_single() also checks cpu_online()

> +		 * after csd_lock(). However, since cpu_plus is from user

> +		 * space, let's do an extra quick check to filter out

> +		 * invalid value before smp_call_function_single().

> +		 */

> +		if (!cpu_online(cpu)) {

> +			err = -ENXIO;

> +			goto out;

> +		}

> +

> +		err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp,

> +					       &info, 1);

> +		if (err)

> +			goto out;

> +	}

> +

> +	if (copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32))) {

> +		err = -EFAULT;

> +		goto out;

> +	}


This goto is not needed. I don't mind it though.

> +

> +out:

> +	kfree(info.ctx);

> +	return err;

> +}

> +
John Fastabend Sept. 23, 2020, 4:26 a.m. UTC | #2
Song Liu wrote:
> This API supports new field cpu_plus in bpf_attr.test.
> 
> Signed-off-by: Song Liu <songliubraving@fb.com>
> ---

Acked-by: John Fastabend <john.fastabend@gmail.com>