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

Signed-off-by: Lorenzo Bianconi <lore...@kernel.org>
---
 net/bpf/test_run.c | 48 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 40 insertions(+), 8 deletions(-)

diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index 8a4cc15b89b7..bc575fd64e06 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -123,7 +123,8 @@ static int bpf_test_run(struct bpf_prog *prog, void *ctx, 
u32 repeat,
 
 static int bpf_test_finish(const union bpf_attr *kattr,
                           union bpf_attr __user *uattr, const void *data,
-                          u32 size, u32 retval, u32 duration)
+                          struct xdp_shared_info *xdp_sinfo, u32 size,
+                          u32 retval, u32 duration)
 {
        void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
        int err = -EFAULT;
@@ -138,8 +139,37 @@ static int bpf_test_finish(const union bpf_attr *kattr,
                err = -ENOSPC;
        }
 
-       if (data_out && copy_to_user(data_out, data, copy_size))
-               goto out;
+       if (data_out) {
+               int len = xdp_sinfo ? copy_size - xdp_sinfo->data_length
+                                   : copy_size;
+
+               if (copy_to_user(data_out, data, len))
+                       goto out;
+
+               if (xdp_sinfo) {
+                       int i, offset = len, data_len;
+
+                       for (i = 0; i < xdp_sinfo->nr_frags; i++) {
+                               skb_frag_t *frag = &xdp_sinfo->frags[i];
+
+                               if (offset >= copy_size) {
+                                       err = -ENOSPC;
+                                       break;
+                               }
+
+                               data_len = min_t(int, copy_size - offset,
+                                                xdp_get_frag_size(frag));
+
+                               if (copy_to_user(data_out + offset,
+                                                xdp_get_frag_address(frag),
+                                                data_len))
+                                       goto out;
+
+                               offset += data_len;
+                       }
+               }
+       }
+
        if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
                goto out;
        if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
@@ -641,7 +671,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 */
        if (WARN_ON_ONCE(skb_is_nonlinear(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)
                ret = bpf_ctx_finish(kattr, uattr, ctx,
                                     sizeof(struct __sk_buff));
@@ -723,7 +754,8 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const 
union bpf_attr *kattr,
                goto out;
 
        size = xdp.data_end - xdp.data + xdp_sinfo->data_length;
-       ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration);
+       ret = bpf_test_finish(kattr, uattr, xdp.data, xdp_sinfo, size, retval,
+                             duration);
 
 out:
        bpf_prog_change_xdp(prog, NULL);
@@ -809,8 +841,8 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
        if (ret < 0)
                goto out;
 
-       ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
-                             retval, duration);
+       ret = bpf_test_finish(kattr, uattr, &flow_keys, NULL,
+                             sizeof(flow_keys), retval, duration);
        if (!ret)
                ret = bpf_ctx_finish(kattr, uattr, user_ctx,
                                     sizeof(struct bpf_flow_keys));
@@ -914,7 +946,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);
        }
 
-       ret = bpf_test_finish(kattr, uattr, NULL, 0, retval, duration);
+       ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration);
        if (!ret)
                ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx));
 
-- 
2.30.2

Reply via email to