bpf: test_run: add xdp_shared_info pointer in bpf_test_finish signature

introduce xdp_shared_info pointer in bpf_test_finish signature in order
to copy back paged data from a xdp frags frame to userspace buffer

Acked-by: Toke Hoiland-Jorgensen <toke@redhat.com>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
Link: https://lore.kernel.org/r/c803673798c786f915bcdd6c9338edaa9740d3d6.1642758637.git.lorenzo@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Lorenzo Bianconi 2022-01-21 11:09:59 +01:00 committed by Alexei Starovoitov
parent 1c19499825
commit 7855e0db15

View File

@ -131,7 +131,8 @@ static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
static int bpf_test_finish(const union bpf_attr *kattr, static int bpf_test_finish(const union bpf_attr *kattr,
union bpf_attr __user *uattr, const void *data, union bpf_attr __user *uattr, const void *data,
u32 size, u32 retval, u32 duration) struct skb_shared_info *sinfo, u32 size,
u32 retval, u32 duration)
{ {
void __user *data_out = u64_to_user_ptr(kattr->test.data_out); void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
int err = -EFAULT; int err = -EFAULT;
@ -146,8 +147,36 @@ static int bpf_test_finish(const union bpf_attr *kattr,
err = -ENOSPC; err = -ENOSPC;
} }
if (data_out && copy_to_user(data_out, data, copy_size)) if (data_out) {
goto out; int len = sinfo ? copy_size - sinfo->xdp_frags_size : copy_size;
if (copy_to_user(data_out, data, len))
goto out;
if (sinfo) {
int i, offset = len, data_len;
for (i = 0; i < sinfo->nr_frags; i++) {
skb_frag_t *frag = &sinfo->frags[i];
if (offset >= copy_size) {
err = -ENOSPC;
break;
}
data_len = min_t(int, copy_size - offset,
skb_frag_size(frag));
if (copy_to_user(data_out + offset,
skb_frag_address(frag),
data_len))
goto out;
offset += data_len;
}
}
}
if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size))) if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
goto out; goto out;
if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
@ -801,7 +830,8 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
/* bpf program can never convert linear skb to non-linear */ /* bpf program can never convert linear skb to non-linear */
if (WARN_ON_ONCE(skb_is_nonlinear(skb))) if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
size = skb_headlen(skb); size = skb_headlen(skb);
ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration); ret = bpf_test_finish(kattr, uattr, skb->data, NULL, size, retval,
duration);
if (!ret) if (!ret)
ret = bpf_ctx_finish(kattr, uattr, ctx, ret = bpf_ctx_finish(kattr, uattr, ctx,
sizeof(struct __sk_buff)); sizeof(struct __sk_buff));
@ -969,8 +999,8 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
goto out; goto out;
size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size; size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size;
ret = bpf_test_finish(kattr, uattr, xdp.data_meta, size, retval, ret = bpf_test_finish(kattr, uattr, xdp.data_meta, sinfo, size,
duration); retval, duration);
if (!ret) if (!ret)
ret = bpf_ctx_finish(kattr, uattr, ctx, ret = bpf_ctx_finish(kattr, uattr, ctx,
sizeof(struct xdp_md)); sizeof(struct xdp_md));
@ -1062,8 +1092,8 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
if (ret < 0) if (ret < 0)
goto out; goto out;
ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys), ret = bpf_test_finish(kattr, uattr, &flow_keys, NULL,
retval, duration); sizeof(flow_keys), retval, duration);
if (!ret) if (!ret)
ret = bpf_ctx_finish(kattr, uattr, user_ctx, ret = bpf_ctx_finish(kattr, uattr, user_ctx,
sizeof(struct bpf_flow_keys)); sizeof(struct bpf_flow_keys));
@ -1167,7 +1197,7 @@ int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kat
user_ctx->cookie = sock_gen_cookie(ctx.selected_sk); user_ctx->cookie = sock_gen_cookie(ctx.selected_sk);
} }
ret = bpf_test_finish(kattr, uattr, NULL, 0, retval, duration); ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration);
if (!ret) if (!ret)
ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx)); ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx));