BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/3] introduce bpf_dynptr_copy kfunc
@ 2025-02-18 19:00 Mykyta Yatsenko
  2025-02-18 19:00 ` [PATCH bpf-next 1/3] bpf/helpers: refactor bpf_dynptr_read and bpf_dynptr_write Mykyta Yatsenko
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Mykyta Yatsenko @ 2025-02-18 19:00 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87; +Cc: Mykyta Yatsenko

From: Mykyta Yatsenko <yatsenko@meta.com>

Introduce a new kfunc, bpf_dynptr_copy, which enables copying of
data from one dynptr to another. This functionality may be useful in
scenarios such as capturing XDP data to a ring buffer.
The patch set is split into 3 patches:
1. Refactor bpf_dynptr_read and bpf_dynptr_write by extracting code into
static functions, that allows calling them with no compiler warnings
2. Introduce bpf_dynptr_copy
3. Add tests for bpf_dynptr_copy

Mykyta Yatsenko (3):
  bpf/helpers: refactor bpf_dynptr_read and bpf_dynptr_write
  bpf/helpers: introduce bpf_dynptr_copy kfunc
  selftests/bpf: add tests for bpf_dynptr_copy

 kernel/bpf/helpers.c                          | 57 +++++++++++++-
 kernel/bpf/verifier.c                         |  3 +
 tools/testing/selftests/bpf/bpf_kfuncs.h      |  8 ++
 .../testing/selftests/bpf/prog_tests/dynptr.c | 25 ++++++
 .../selftests/bpf/progs/dynptr_success.c      | 77 +++++++++++++++++++
 5 files changed, 166 insertions(+), 4 deletions(-)

-- 
2.48.1


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH bpf-next 1/3] bpf/helpers: refactor bpf_dynptr_read and bpf_dynptr_write
  2025-02-18 19:00 [PATCH bpf-next 0/3] introduce bpf_dynptr_copy kfunc Mykyta Yatsenko
@ 2025-02-18 19:00 ` Mykyta Yatsenko
  2025-02-18 19:00 ` [PATCH bpf-next 2/3] bpf/helpers: introduce bpf_dynptr_copy kfunc Mykyta Yatsenko
  2025-02-18 19:00 ` [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy Mykyta Yatsenko
  2 siblings, 0 replies; 12+ messages in thread
From: Mykyta Yatsenko @ 2025-02-18 19:00 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87; +Cc: Mykyta Yatsenko

From: Mykyta Yatsenko <yatsenko@meta.com>

Refactor bpf_dynptr_read and bpf_dynptr_write helpers: extract code
into the static functions namely __bpf_dynptr_read and
__bpf_dynptr_write, this allows calling these without compiler warnings.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
 kernel/bpf/helpers.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index f27ce162427a..2833558c3009 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1759,8 +1759,8 @@ static const struct bpf_func_proto bpf_dynptr_from_mem_proto = {
 	.arg4_type	= ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL | MEM_UNINIT | MEM_WRITE,
 };
 
-BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, const struct bpf_dynptr_kern *, src,
-	   u32, offset, u64, flags)
+static int __bpf_dynptr_read(void *dst, u32 len, const struct bpf_dynptr_kern *src,
+			     u32 offset, u64 flags)
 {
 	enum bpf_dynptr_type type;
 	int err;
@@ -1793,6 +1793,12 @@ BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, const struct bpf_dynptr_kern
 	}
 }
 
+BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, const struct bpf_dynptr_kern *, src,
+	   u32, offset, u64, flags)
+{
+	return __bpf_dynptr_read(dst, len, src, offset, flags);
+}
+
 static const struct bpf_func_proto bpf_dynptr_read_proto = {
 	.func		= bpf_dynptr_read,
 	.gpl_only	= false,
@@ -1804,8 +1810,8 @@ static const struct bpf_func_proto bpf_dynptr_read_proto = {
 	.arg5_type	= ARG_ANYTHING,
 };
 
-BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u32, offset, void *, src,
-	   u32, len, u64, flags)
+static int __bpf_dynptr_write(const struct bpf_dynptr_kern *dst, u32 offset, void *src,
+			      u32 len, u64 flags)
 {
 	enum bpf_dynptr_type type;
 	int err;
@@ -1843,6 +1849,12 @@ BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u32, offset, v
 	}
 }
 
+BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u32, offset, void *, src,
+	   u32, len, u64, flags)
+{
+	return __bpf_dynptr_write(dst, offset, src, len, flags);
+}
+
 static const struct bpf_func_proto bpf_dynptr_write_proto = {
 	.func		= bpf_dynptr_write,
 	.gpl_only	= false,
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH bpf-next 2/3] bpf/helpers: introduce bpf_dynptr_copy kfunc
  2025-02-18 19:00 [PATCH bpf-next 0/3] introduce bpf_dynptr_copy kfunc Mykyta Yatsenko
  2025-02-18 19:00 ` [PATCH bpf-next 1/3] bpf/helpers: refactor bpf_dynptr_read and bpf_dynptr_write Mykyta Yatsenko
@ 2025-02-18 19:00 ` Mykyta Yatsenko
  2025-02-19  1:01   ` Eduard Zingerman
  2025-02-21  0:42   ` Andrii Nakryiko
  2025-02-18 19:00 ` [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy Mykyta Yatsenko
  2 siblings, 2 replies; 12+ messages in thread
From: Mykyta Yatsenko @ 2025-02-18 19:00 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87; +Cc: Mykyta Yatsenko

From: Mykyta Yatsenko <yatsenko@meta.com>

Introducing bpf_dynptr_copy kfunc allowing copying data from one dynptr to
another. This functionality is useful in scenarios such as capturing XDP
data to a ring buffer.
The implementation consists of 4 branches:
  * A fast branch for contiguous buffer capacity in both source and
destination dynptrs
  * 3 branches utilizing __bpf_dynptr_read and __bpf_dynptr_write to copy
data to/from non-contiguous buffer

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
 kernel/bpf/helpers.c  | 37 +++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c |  3 +++
 2 files changed, 40 insertions(+)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 2833558c3009..ac5fbdfc504d 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -2770,6 +2770,42 @@ __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p,
 	return 0;
 }
 
+__bpf_kfunc int bpf_dynptr_copy(struct bpf_dynptr *dst_ptr, u32 dst_off,
+				struct bpf_dynptr *src_ptr, u32 src_off, u32 size)
+{
+	struct bpf_dynptr_kern *dst = (struct bpf_dynptr_kern *)dst_ptr;
+	struct bpf_dynptr_kern *src = (struct bpf_dynptr_kern *)src_ptr;
+	__u8 *src_slice, *dst_slice;
+	int err = 0;
+
+	src_slice = bpf_dynptr_slice(src_ptr, src_off, NULL, size);
+	dst_slice = bpf_dynptr_slice_rdwr(dst_ptr, dst_off, NULL, size);
+
+	if (src_slice && dst_slice) {
+		memmove(dst_slice, src_slice, size);
+	} else if (src_slice) {
+		err = __bpf_dynptr_write(dst, dst_off, src_slice, size, 0);
+	} else if (dst_slice) {
+		err = __bpf_dynptr_read(dst_slice, size, src, src_off, 0);
+	} else {
+		u32 off = 0;
+		char buf[256];
+
+		if (bpf_dynptr_check_off_len(dst, dst_off, size) ||
+		    bpf_dynptr_check_off_len(src, src_off, size))
+			return -E2BIG;
+
+		while (err == 0 && off < size) {
+			u32 chunk_sz = min(sizeof(buf), size - off);
+
+			err = err ?: __bpf_dynptr_read(buf, chunk_sz, src, src_off + off, 0);
+			err = err ?: __bpf_dynptr_write(dst, dst_off + off, buf, chunk_sz, 0);
+			off += chunk_sz;
+		}
+	}
+	return err;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -3174,6 +3210,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null)
 BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
 BTF_ID_FLAGS(func, bpf_dynptr_size)
 BTF_ID_FLAGS(func, bpf_dynptr_clone)
+BTF_ID_FLAGS(func, bpf_dynptr_copy)
 #ifdef CONFIG_NET
 BTF_ID_FLAGS(func, bpf_modify_return_test_tp)
 #endif
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index e7bc74171c99..3c567bfcc582 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -11781,6 +11781,7 @@ enum special_kfunc_type {
 	KF_bpf_dynptr_slice,
 	KF_bpf_dynptr_slice_rdwr,
 	KF_bpf_dynptr_clone,
+	KF_bpf_dynptr_copy,
 	KF_bpf_percpu_obj_new_impl,
 	KF_bpf_percpu_obj_drop_impl,
 	KF_bpf_throw,
@@ -11819,6 +11820,7 @@ BTF_ID(func, bpf_dynptr_from_xdp)
 BTF_ID(func, bpf_dynptr_slice)
 BTF_ID(func, bpf_dynptr_slice_rdwr)
 BTF_ID(func, bpf_dynptr_clone)
+BTF_ID(func, bpf_dynptr_copy)
 BTF_ID(func, bpf_percpu_obj_new_impl)
 BTF_ID(func, bpf_percpu_obj_drop_impl)
 BTF_ID(func, bpf_throw)
@@ -11857,6 +11859,7 @@ BTF_ID_UNUSED
 BTF_ID(func, bpf_dynptr_slice)
 BTF_ID(func, bpf_dynptr_slice_rdwr)
 BTF_ID(func, bpf_dynptr_clone)
+BTF_ID(func, bpf_dynptr_copy)
 BTF_ID(func, bpf_percpu_obj_new_impl)
 BTF_ID(func, bpf_percpu_obj_drop_impl)
 BTF_ID(func, bpf_throw)
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy
  2025-02-18 19:00 [PATCH bpf-next 0/3] introduce bpf_dynptr_copy kfunc Mykyta Yatsenko
  2025-02-18 19:00 ` [PATCH bpf-next 1/3] bpf/helpers: refactor bpf_dynptr_read and bpf_dynptr_write Mykyta Yatsenko
  2025-02-18 19:00 ` [PATCH bpf-next 2/3] bpf/helpers: introduce bpf_dynptr_copy kfunc Mykyta Yatsenko
@ 2025-02-18 19:00 ` Mykyta Yatsenko
  2025-02-19  1:04   ` Eduard Zingerman
  2025-02-21  0:52   ` Andrii Nakryiko
  2 siblings, 2 replies; 12+ messages in thread
From: Mykyta Yatsenko @ 2025-02-18 19:00 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87; +Cc: Mykyta Yatsenko

From: Mykyta Yatsenko <yatsenko@meta.com>

Add XDP setup type for dynptr tests, enabling testing for
non-contiguous buffer.
Add 2 tests:
 - test_dynptr_copy - verify correctness for the fast (contiguous
 buffer) code path.
 - test_dynptr_copy_xdp - verifies code paths that handle
 non-contiguous buffer.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
 tools/testing/selftests/bpf/bpf_kfuncs.h      |  8 ++
 .../testing/selftests/bpf/prog_tests/dynptr.c | 25 ++++++
 .../selftests/bpf/progs/dynptr_success.c      | 77 +++++++++++++++++++
 3 files changed, 110 insertions(+)

diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
index 8215c9b3115e..e9c193036c82 100644
--- a/tools/testing/selftests/bpf/bpf_kfuncs.h
+++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
@@ -43,6 +43,14 @@ extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak;
 extern __u32 bpf_dynptr_size(const struct bpf_dynptr *ptr) __ksym __weak;
 extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym __weak;
 
+/* Description
+ *  Copy data from one dynptr to another
+ * Returns
+ *  Error code
+ */
+extern int bpf_dynptr_copy(struct bpf_dynptr *dst, __u32 dst_off,
+			   struct bpf_dynptr *src, __u32 src_off, __u32 size) __ksym __weak;
+
 /* Description
  *  Modify the address of a AF_UNIX sockaddr.
  * Returns
diff --git a/tools/testing/selftests/bpf/prog_tests/dynptr.c b/tools/testing/selftests/bpf/prog_tests/dynptr.c
index b614a5272dfd..247618958155 100644
--- a/tools/testing/selftests/bpf/prog_tests/dynptr.c
+++ b/tools/testing/selftests/bpf/prog_tests/dynptr.c
@@ -10,6 +10,7 @@ enum test_setup_type {
 	SETUP_SYSCALL_SLEEP,
 	SETUP_SKB_PROG,
 	SETUP_SKB_PROG_TP,
+	SETUP_XDP_PROG,
 };
 
 static struct {
@@ -18,6 +19,8 @@ static struct {
 } success_tests[] = {
 	{"test_read_write", SETUP_SYSCALL_SLEEP},
 	{"test_dynptr_data", SETUP_SYSCALL_SLEEP},
+	{"test_dynptr_copy", SETUP_SYSCALL_SLEEP},
+	{"test_dynptr_copy_xdp", SETUP_XDP_PROG},
 	{"test_ringbuf", SETUP_SYSCALL_SLEEP},
 	{"test_skb_readonly", SETUP_SKB_PROG},
 	{"test_dynptr_skb_data", SETUP_SKB_PROG},
@@ -120,6 +123,28 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typ
 
 		break;
 	}
+	case SETUP_XDP_PROG:
+	{
+		char data[5000];
+		int err, prog_fd;
+
+		LIBBPF_OPTS(bpf_test_run_opts, opts,
+			    .data_in = &data,
+			    .data_size_in = sizeof(data),
+			    .repeat = 1,
+		);
+
+		prog_fd = bpf_program__fd(prog);
+		if (!ASSERT_GE(prog_fd, 0, "prog_fd"))
+			goto cleanup;
+
+		err = bpf_prog_test_run_opts(prog_fd, &opts);
+
+		if (!ASSERT_OK(err, "test_run"))
+			goto cleanup;
+
+		break;
+	}
 	}
 
 	ASSERT_EQ(skel->bss->err, 0, "err");
diff --git a/tools/testing/selftests/bpf/progs/dynptr_success.c b/tools/testing/selftests/bpf/progs/dynptr_success.c
index bfcc85686cf0..8a6b35418e39 100644
--- a/tools/testing/selftests/bpf/progs/dynptr_success.c
+++ b/tools/testing/selftests/bpf/progs/dynptr_success.c
@@ -567,3 +567,80 @@ int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location)
 
 	return 1;
 }
+
+SEC("?tp/syscalls/sys_enter_nanosleep")
+int test_dynptr_copy(void *ctx)
+{
+	char *data = "hello there, world!!";
+	char buf[32] = {'\0'};
+	__u32 sz = strlen(data);
+	struct bpf_dynptr src, dst;
+
+	bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &src);
+	bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &dst);
+
+	err = bpf_dynptr_write(&src, 0, data, sz, 0);
+	err = err ?: bpf_dynptr_copy(&dst, 0, &src, 0, sz);
+	err = err ?: bpf_dynptr_read(buf, sz, &dst, 0, 0);
+	err = err ?: __builtin_memcmp(data, buf, sz);
+
+	err = err ?: bpf_dynptr_copy(&dst, 3, &src, 5, sz - 5);
+	err = err ?: bpf_dynptr_read(buf, sz - 5, &dst, 3, 0);
+	err = err ?: __builtin_memcmp(data + 5, buf, sz - 5);
+
+	bpf_ringbuf_discard_dynptr(&src, 0);
+	bpf_ringbuf_discard_dynptr(&dst, 0);
+	return 0;
+}
+
+SEC("xdp")
+int test_dynptr_copy_xdp(struct xdp_md *xdp)
+{
+	struct bpf_dynptr ptr_buf, ptr_xdp;
+	char *data = "qwertyuiopasdfghjkl;";
+	char buf[32] = {'\0'};
+	__u32 len = strlen(data);
+	int i, chunks = 200;
+
+	bpf_dynptr_from_xdp(xdp, 0, &ptr_xdp);
+	bpf_ringbuf_reserve_dynptr(&ringbuf, len * chunks, 0, &ptr_buf);
+
+	bpf_for(i, 0, chunks) {
+		err =  err ?: bpf_dynptr_write(&ptr_buf, i * len, data, len, 0);
+	}
+
+	err = err ?: bpf_dynptr_copy(&ptr_xdp, 0, &ptr_buf, 0, len * chunks);
+
+	bpf_for(i, 0, chunks) {
+		memset(buf, 0, sizeof(buf));
+		err = err ?: bpf_dynptr_read(&buf, len, &ptr_xdp, i * len, 0);
+		err = err ?: memcmp(data, buf, len);
+	}
+
+	memset(buf, 0, sizeof(buf));
+	bpf_for(i, 0, chunks) {
+		err = err ?: bpf_dynptr_write(&ptr_buf, i * len, buf, len, 0);
+	}
+
+	err = err ?: bpf_dynptr_copy(&ptr_buf, 0, &ptr_xdp, 0, len * chunks);
+
+	bpf_for(i, 0, chunks) {
+		memset(buf, 0, sizeof(buf));
+		err = err ?: bpf_dynptr_read(&buf, len, &ptr_buf, i * len, 0);
+		err = err ?: memcmp(data, buf, len);
+	}
+
+	bpf_ringbuf_discard_dynptr(&ptr_buf, 0);
+
+	err = err ?: bpf_dynptr_copy(&ptr_xdp, 2, &ptr_xdp, len, len * (chunks - 1));
+
+	bpf_for(i, 0, chunks - 1) {
+		memset(buf, 0, sizeof(buf));
+		err = err ?: bpf_dynptr_read(&buf, len, &ptr_xdp, 2 + i * len, 0);
+		err = err ?: memcmp(data, buf, len);
+	}
+
+	err = err ?: (bpf_dynptr_copy(&ptr_xdp, 2000, &ptr_xdp, 0, len * chunks) == -E2BIG ? 0 : 1);
+
+	return XDP_DROP;
+}
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH bpf-next 2/3] bpf/helpers: introduce bpf_dynptr_copy kfunc
  2025-02-18 19:00 ` [PATCH bpf-next 2/3] bpf/helpers: introduce bpf_dynptr_copy kfunc Mykyta Yatsenko
@ 2025-02-19  1:01   ` Eduard Zingerman
  2025-02-21  0:42   ` Andrii Nakryiko
  1 sibling, 0 replies; 12+ messages in thread
From: Eduard Zingerman @ 2025-02-19  1:01 UTC (permalink / raw)
  To: Mykyta Yatsenko, bpf, ast, andrii, daniel, kafai, kernel-team
  Cc: Mykyta Yatsenko

On Tue, 2025-02-18 at 19:00 +0000, Mykyta Yatsenko wrote:
> From: Mykyta Yatsenko <yatsenko@meta.com>
> 
> Introducing bpf_dynptr_copy kfunc allowing copying data from one dynptr to
> another. This functionality is useful in scenarios such as capturing XDP
> data to a ring buffer.
> The implementation consists of 4 branches:
>   * A fast branch for contiguous buffer capacity in both source and
> destination dynptrs
>   * 3 branches utilizing __bpf_dynptr_read and __bpf_dynptr_write to copy
> data to/from non-contiguous buffer
> 
> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
> ---

Acked-by: Eduard Zingerman <eddyz87@gmail.com>

>  kernel/bpf/helpers.c  | 37 +++++++++++++++++++++++++++++++++++++
>  kernel/bpf/verifier.c |  3 +++
>  2 files changed, 40 insertions(+)
> 
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 2833558c3009..ac5fbdfc504d 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -2770,6 +2770,42 @@ __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p,
>  	return 0;
>  }

Nit: it would be nice to have a docstring here, as for other dynptr functions.

> +__bpf_kfunc int bpf_dynptr_copy(struct bpf_dynptr *dst_ptr, u32 dst_off,
> +				struct bpf_dynptr *src_ptr, u32 src_off, u32 size)

[...]

> @@ -3174,6 +3210,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null)
>  BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
>  BTF_ID_FLAGS(func, bpf_dynptr_size)
>  BTF_ID_FLAGS(func, bpf_dynptr_clone)
> +BTF_ID_FLAGS(func, bpf_dynptr_copy)
>  #ifdef CONFIG_NET
>  BTF_ID_FLAGS(func, bpf_modify_return_test_tp)
>  #endif
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index e7bc74171c99..3c567bfcc582 100644

Nit: There is no need to add bpf_dynptr_copy to special kfuncs list.
     This list is maintained to get BTF ids of several kfuncs w/o lookup,
     like so: 'special_kfunc_list[KF_bpf_dynptr_slice]'.
     Which is useful when writing custom semantic rules for such functions.
     There are no special rules for bpf_dynptr_copy, hence entry in the
     special_kfunc_list is not needed.

> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -11781,6 +11781,7 @@ enum special_kfunc_type {
>  	KF_bpf_dynptr_slice,
>  	KF_bpf_dynptr_slice_rdwr,
>  	KF_bpf_dynptr_clone,
> +	KF_bpf_dynptr_copy,
>  	KF_bpf_percpu_obj_new_impl,
>  	KF_bpf_percpu_obj_drop_impl,
>  	KF_bpf_throw,
> @@ -11819,6 +11820,7 @@ BTF_ID(func, bpf_dynptr_from_xdp)
>  BTF_ID(func, bpf_dynptr_slice)
>  BTF_ID(func, bpf_dynptr_slice_rdwr)
>  BTF_ID(func, bpf_dynptr_clone)
> +BTF_ID(func, bpf_dynptr_copy)
>  BTF_ID(func, bpf_percpu_obj_new_impl)
>  BTF_ID(func, bpf_percpu_obj_drop_impl)
>  BTF_ID(func, bpf_throw)
> @@ -11857,6 +11859,7 @@ BTF_ID_UNUSED
>  BTF_ID(func, bpf_dynptr_slice)
>  BTF_ID(func, bpf_dynptr_slice_rdwr)
>  BTF_ID(func, bpf_dynptr_clone)
> +BTF_ID(func, bpf_dynptr_copy)
>  BTF_ID(func, bpf_percpu_obj_new_impl)
>  BTF_ID(func, bpf_percpu_obj_drop_impl)
>  BTF_ID(func, bpf_throw)



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy
  2025-02-18 19:00 ` [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy Mykyta Yatsenko
@ 2025-02-19  1:04   ` Eduard Zingerman
  2025-02-21  0:52   ` Andrii Nakryiko
  1 sibling, 0 replies; 12+ messages in thread
From: Eduard Zingerman @ 2025-02-19  1:04 UTC (permalink / raw)
  To: Mykyta Yatsenko, bpf, ast, andrii, daniel, kafai, kernel-team
  Cc: Mykyta Yatsenko

On Tue, 2025-02-18 at 19:00 +0000, Mykyta Yatsenko wrote:
> From: Mykyta Yatsenko <yatsenko@meta.com>
> 
> Add XDP setup type for dynptr tests, enabling testing for
> non-contiguous buffer.
> Add 2 tests:
>  - test_dynptr_copy - verify correctness for the fast (contiguous
>  buffer) code path.
>  - test_dynptr_copy_xdp - verifies code paths that handle
>  non-contiguous buffer.
> 
> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
> ---

Acked-by: Eduard Zingerman <eddyz87@gmail.com>

[...]

> +SEC("xdp")
> +int test_dynptr_copy_xdp(struct xdp_md *xdp)
> +{

Nit: it would be helpful if there were a few comments
     explaining which special cases are tested at each step.

> +	struct bpf_dynptr ptr_buf, ptr_xdp;
> +	char *data = "qwertyuiopasdfghjkl;";
> +	char buf[32] = {'\0'};
> +	__u32 len = strlen(data);
> +	int i, chunks = 200;
> +
> +	bpf_dynptr_from_xdp(xdp, 0, &ptr_xdp);
> +	bpf_ringbuf_reserve_dynptr(&ringbuf, len * chunks, 0, &ptr_buf);
> +
> +	bpf_for(i, 0, chunks) {
> +		err =  err ?: bpf_dynptr_write(&ptr_buf, i * len, data, len, 0);
> +	}
> +
> +	err = err ?: bpf_dynptr_copy(&ptr_xdp, 0, &ptr_buf, 0, len * chunks);
> +
> +	bpf_for(i, 0, chunks) {
> +		memset(buf, 0, sizeof(buf));
> +		err = err ?: bpf_dynptr_read(&buf, len, &ptr_xdp, i * len, 0);
> +		err = err ?: memcmp(data, buf, len);
> +	}
> +
> +	memset(buf, 0, sizeof(buf));
> +	bpf_for(i, 0, chunks) {
> +		err = err ?: bpf_dynptr_write(&ptr_buf, i * len, buf, len, 0);
> +	}
> +
> +	err = err ?: bpf_dynptr_copy(&ptr_buf, 0, &ptr_xdp, 0, len * chunks);
> +
> +	bpf_for(i, 0, chunks) {
> +		memset(buf, 0, sizeof(buf));
> +		err = err ?: bpf_dynptr_read(&buf, len, &ptr_buf, i * len, 0);
> +		err = err ?: memcmp(data, buf, len);
> +	}
> +
> +	bpf_ringbuf_discard_dynptr(&ptr_buf, 0);
> +
> +	err = err ?: bpf_dynptr_copy(&ptr_xdp, 2, &ptr_xdp, len, len * (chunks - 1));
> +
> +	bpf_for(i, 0, chunks - 1) {
> +		memset(buf, 0, sizeof(buf));
> +		err = err ?: bpf_dynptr_read(&buf, len, &ptr_xdp, 2 + i * len, 0);
> +		err = err ?: memcmp(data, buf, len);
> +	}
> +
> +	err = err ?: (bpf_dynptr_copy(&ptr_xdp, 2000, &ptr_xdp, 0, len * chunks) == -E2BIG ? 0 : 1);
> +
> +	return XDP_DROP;
> +}



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH bpf-next 2/3] bpf/helpers: introduce bpf_dynptr_copy kfunc
  2025-02-18 19:00 ` [PATCH bpf-next 2/3] bpf/helpers: introduce bpf_dynptr_copy kfunc Mykyta Yatsenko
  2025-02-19  1:01   ` Eduard Zingerman
@ 2025-02-21  0:42   ` Andrii Nakryiko
  1 sibling, 0 replies; 12+ messages in thread
From: Andrii Nakryiko @ 2025-02-21  0:42 UTC (permalink / raw)
  To: Mykyta Yatsenko
  Cc: bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87,
	Mykyta Yatsenko

On Tue, Feb 18, 2025 at 11:01 AM Mykyta Yatsenko
<mykyta.yatsenko5@gmail.com> wrote:
>
> From: Mykyta Yatsenko <yatsenko@meta.com>
>
> Introducing bpf_dynptr_copy kfunc allowing copying data from one dynptr to
> another. This functionality is useful in scenarios such as capturing XDP
> data to a ring buffer.
> The implementation consists of 4 branches:
>   * A fast branch for contiguous buffer capacity in both source and
> destination dynptrs
>   * 3 branches utilizing __bpf_dynptr_read and __bpf_dynptr_write to copy
> data to/from non-contiguous buffer
>
> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
> ---
>  kernel/bpf/helpers.c  | 37 +++++++++++++++++++++++++++++++++++++
>  kernel/bpf/verifier.c |  3 +++
>  2 files changed, 40 insertions(+)

This is surprisingly nicely compact and terse, I really like this! See
some nits below, and yes, let's add doc-comment as Eduard suggested.

pw-bot: cr

>
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 2833558c3009..ac5fbdfc504d 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -2770,6 +2770,42 @@ __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p,
>         return 0;
>  }
>
> +__bpf_kfunc int bpf_dynptr_copy(struct bpf_dynptr *dst_ptr, u32 dst_off,
> +                               struct bpf_dynptr *src_ptr, u32 src_off, u32 size)
> +{
> +       struct bpf_dynptr_kern *dst = (struct bpf_dynptr_kern *)dst_ptr;
> +       struct bpf_dynptr_kern *src = (struct bpf_dynptr_kern *)src_ptr;
> +       __u8 *src_slice, *dst_slice;

let's use `void *`, both memmove and bpf_dynptr_{write,read} uses
`void *` throughout

> +       int err = 0;

I'm not sure we want to thread through this err, see below

> +
> +       src_slice = bpf_dynptr_slice(src_ptr, src_off, NULL, size);
> +       dst_slice = bpf_dynptr_slice_rdwr(dst_ptr, dst_off, NULL, size);
> +
> +       if (src_slice && dst_slice) {
> +               memmove(dst_slice, src_slice, size);

return 0;

> +       } else if (src_slice) {
> +               err = __bpf_dynptr_write(dst, dst_off, src_slice, size, 0);

return __bpf_dynptr_write(...)

> +       } else if (dst_slice) {
> +               err = __bpf_dynptr_read(dst_slice, size, src, src_off, 0);

ditto, direct return

> +       } else {
> +               u32 off = 0;
> +               char buf[256];
> +
> +               if (bpf_dynptr_check_off_len(dst, dst_off, size) ||
> +                   bpf_dynptr_check_off_len(src, src_off, size))
> +                       return -E2BIG;

see, you are doing direct return here (but inconsistent with the rest of logic)

> +
> +               while (err == 0 && off < size) {

just while (off < size), because...

> +                       u32 chunk_sz = min(sizeof(buf), size - off);

this might trigger warning about type mismatch (size_t vs u32), so
probably best to be explicit: `min_t(u32, sizeof(buf), size - off);`

> +
> +                       err = err ?: __bpf_dynptr_read(buf, chunk_sz, src, src_off + off, 0);
> +                       err = err ?: __bpf_dynptr_write(dst, dst_off + off, buf, chunk_sz, 0);

I'd keep this a bit more conventional with just


err = __bpf_dynptr_read(...);
if (err)
    return err;
err = __bpf_dynptr_write(...);
if (err)
    return err;

off += chunk_sz;


Nothing wrong or incorrect with how you wrote it, but somehow feels a
bit more typical to do it this way.

> +                       off += chunk_sz;
> +               }
> +       }
> +       return err;
> +}
> +
>  __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
>  {
>         return obj;
> @@ -3174,6 +3210,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null)
>  BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
>  BTF_ID_FLAGS(func, bpf_dynptr_size)
>  BTF_ID_FLAGS(func, bpf_dynptr_clone)
> +BTF_ID_FLAGS(func, bpf_dynptr_copy)
>  #ifdef CONFIG_NET
>  BTF_ID_FLAGS(func, bpf_modify_return_test_tp)
>  #endif
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index e7bc74171c99..3c567bfcc582 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -11781,6 +11781,7 @@ enum special_kfunc_type {
>         KF_bpf_dynptr_slice,
>         KF_bpf_dynptr_slice_rdwr,
>         KF_bpf_dynptr_clone,
> +       KF_bpf_dynptr_copy,
>         KF_bpf_percpu_obj_new_impl,
>         KF_bpf_percpu_obj_drop_impl,
>         KF_bpf_throw,
> @@ -11819,6 +11820,7 @@ BTF_ID(func, bpf_dynptr_from_xdp)
>  BTF_ID(func, bpf_dynptr_slice)
>  BTF_ID(func, bpf_dynptr_slice_rdwr)
>  BTF_ID(func, bpf_dynptr_clone)
> +BTF_ID(func, bpf_dynptr_copy)
>  BTF_ID(func, bpf_percpu_obj_new_impl)
>  BTF_ID(func, bpf_percpu_obj_drop_impl)
>  BTF_ID(func, bpf_throw)
> @@ -11857,6 +11859,7 @@ BTF_ID_UNUSED
>  BTF_ID(func, bpf_dynptr_slice)
>  BTF_ID(func, bpf_dynptr_slice_rdwr)
>  BTF_ID(func, bpf_dynptr_clone)
> +BTF_ID(func, bpf_dynptr_copy)
>  BTF_ID(func, bpf_percpu_obj_new_impl)
>  BTF_ID(func, bpf_percpu_obj_drop_impl)
>  BTF_ID(func, bpf_throw)
> --
> 2.48.1
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy
  2025-02-18 19:00 ` [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy Mykyta Yatsenko
  2025-02-19  1:04   ` Eduard Zingerman
@ 2025-02-21  0:52   ` Andrii Nakryiko
  2025-02-21  0:56     ` Eduard Zingerman
  2025-02-21 12:08     ` Mykyta Yatsenko
  1 sibling, 2 replies; 12+ messages in thread
From: Andrii Nakryiko @ 2025-02-21  0:52 UTC (permalink / raw)
  To: Mykyta Yatsenko
  Cc: bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87,
	Mykyta Yatsenko

On Tue, Feb 18, 2025 at 11:01 AM Mykyta Yatsenko
<mykyta.yatsenko5@gmail.com> wrote:
>
> From: Mykyta Yatsenko <yatsenko@meta.com>
>
> Add XDP setup type for dynptr tests, enabling testing for
> non-contiguous buffer.
> Add 2 tests:
>  - test_dynptr_copy - verify correctness for the fast (contiguous
>  buffer) code path.
>  - test_dynptr_copy_xdp - verifies code paths that handle
>  non-contiguous buffer.
>
> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
> ---
>  tools/testing/selftests/bpf/bpf_kfuncs.h      |  8 ++
>  .../testing/selftests/bpf/prog_tests/dynptr.c | 25 ++++++
>  .../selftests/bpf/progs/dynptr_success.c      | 77 +++++++++++++++++++
>  3 files changed, 110 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
> index 8215c9b3115e..e9c193036c82 100644
> --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
> +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
> @@ -43,6 +43,14 @@ extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak;
>  extern __u32 bpf_dynptr_size(const struct bpf_dynptr *ptr) __ksym __weak;
>  extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym __weak;
>
> +/* Description
> + *  Copy data from one dynptr to another
> + * Returns
> + *  Error code
> + */
> +extern int bpf_dynptr_copy(struct bpf_dynptr *dst, __u32 dst_off,
> +                          struct bpf_dynptr *src, __u32 src_off, __u32 size) __ksym __weak;
> +

Do we *need* this? Doesn't all this come from vmlinux.h nowadays?

>  /* Description
>   *  Modify the address of a AF_UNIX sockaddr.
>   * Returns
> diff --git a/tools/testing/selftests/bpf/prog_tests/dynptr.c b/tools/testing/selftests/bpf/prog_tests/dynptr.c
> index b614a5272dfd..247618958155 100644
> --- a/tools/testing/selftests/bpf/prog_tests/dynptr.c
> +++ b/tools/testing/selftests/bpf/prog_tests/dynptr.c
> @@ -10,6 +10,7 @@ enum test_setup_type {
>         SETUP_SYSCALL_SLEEP,
>         SETUP_SKB_PROG,
>         SETUP_SKB_PROG_TP,
> +       SETUP_XDP_PROG,
>  };
>
>  static struct {
> @@ -18,6 +19,8 @@ static struct {
>  } success_tests[] = {
>         {"test_read_write", SETUP_SYSCALL_SLEEP},
>         {"test_dynptr_data", SETUP_SYSCALL_SLEEP},
> +       {"test_dynptr_copy", SETUP_SYSCALL_SLEEP},
> +       {"test_dynptr_copy_xdp", SETUP_XDP_PROG},
>         {"test_ringbuf", SETUP_SYSCALL_SLEEP},
>         {"test_skb_readonly", SETUP_SKB_PROG},
>         {"test_dynptr_skb_data", SETUP_SKB_PROG},
> @@ -120,6 +123,28 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typ
>
>                 break;
>         }
> +       case SETUP_XDP_PROG:
> +       {
> +               char data[5000];
> +               int err, prog_fd;
> +

no empty line here, opts is a variable

> +               LIBBPF_OPTS(bpf_test_run_opts, opts,
> +                           .data_in = &data,
> +                           .data_size_in = sizeof(data),
> +                           .repeat = 1,
> +               );
> +
> +               prog_fd = bpf_program__fd(prog);
> +               if (!ASSERT_GE(prog_fd, 0, "prog_fd"))
> +                       goto cleanup;


we shouldn't check this, if program loaded successfully this will
always be true (and yeah, I know that existing code does that, we
should remove or at least not duplicate this)

> +
> +               err = bpf_prog_test_run_opts(prog_fd, &opts);
> +
> +               if (!ASSERT_OK(err, "test_run"))
> +                       goto cleanup;
> +
> +               break;
> +       }
>         }
>
>         ASSERT_EQ(skel->bss->err, 0, "err");
> diff --git a/tools/testing/selftests/bpf/progs/dynptr_success.c b/tools/testing/selftests/bpf/progs/dynptr_success.c
> index bfcc85686cf0..8a6b35418e39 100644
> --- a/tools/testing/selftests/bpf/progs/dynptr_success.c
> +++ b/tools/testing/selftests/bpf/progs/dynptr_success.c
> @@ -567,3 +567,80 @@ int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location)
>
>         return 1;
>  }
> +
> +SEC("?tp/syscalls/sys_enter_nanosleep")
> +int test_dynptr_copy(void *ctx)
> +{
> +       char *data = "hello there, world!!";
> +       char buf[32] = {'\0'};
> +       __u32 sz = strlen(data);

this is fragile, this is not guaranteed to work (only if compiler just
substituted a constant value). maybe just use data[] = "hello
there..." and use sizeof(data) then?

> +       struct bpf_dynptr src, dst;
> +
> +       bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &src);
> +       bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &dst);
> +
> +       err = bpf_dynptr_write(&src, 0, data, sz, 0);
> +       err = err ?: bpf_dynptr_copy(&dst, 0, &src, 0, sz);
> +       err = err ?: bpf_dynptr_read(buf, sz, &dst, 0, 0);
> +       err = err ?: __builtin_memcmp(data, buf, sz);
> +
> +       err = err ?: bpf_dynptr_copy(&dst, 3, &src, 5, sz - 5);
> +       err = err ?: bpf_dynptr_read(buf, sz - 5, &dst, 3, 0);
> +       err = err ?: __builtin_memcmp(data + 5, buf, sz - 5);
> +
> +       bpf_ringbuf_discard_dynptr(&src, 0);
> +       bpf_ringbuf_discard_dynptr(&dst, 0);
> +       return 0;
> +}
> +
> +SEC("xdp")
> +int test_dynptr_copy_xdp(struct xdp_md *xdp)
> +{
> +       struct bpf_dynptr ptr_buf, ptr_xdp;
> +       char *data = "qwertyuiopasdfghjkl;";
> +       char buf[32] = {'\0'};
> +       __u32 len = strlen(data);

ditto

> +       int i, chunks = 200;
> +
> +       bpf_dynptr_from_xdp(xdp, 0, &ptr_xdp);
> +       bpf_ringbuf_reserve_dynptr(&ringbuf, len * chunks, 0, &ptr_buf);
> +
> +       bpf_for(i, 0, chunks) {
> +               err =  err ?: bpf_dynptr_write(&ptr_buf, i * len, data, len, 0);
> +       }
> +
> +       err = err ?: bpf_dynptr_copy(&ptr_xdp, 0, &ptr_buf, 0, len * chunks);
> +
> +       bpf_for(i, 0, chunks) {
> +               memset(buf, 0, sizeof(buf));

__builtin_memset(), memset() works only because compiler optimizes it
to built-in, but let's not rely on that

> +               err = err ?: bpf_dynptr_read(&buf, len, &ptr_xdp, i * len, 0);
> +               err = err ?: memcmp(data, buf, len);

__builtin_memcmp() and all the other cases below, please

> +       }
> +
> +       memset(buf, 0, sizeof(buf));
> +       bpf_for(i, 0, chunks) {
> +               err = err ?: bpf_dynptr_write(&ptr_buf, i * len, buf, len, 0);
> +       }
> +
> +       err = err ?: bpf_dynptr_copy(&ptr_buf, 0, &ptr_xdp, 0, len * chunks);
> +
> +       bpf_for(i, 0, chunks) {
> +               memset(buf, 0, sizeof(buf));
> +               err = err ?: bpf_dynptr_read(&buf, len, &ptr_buf, i * len, 0);
> +               err = err ?: memcmp(data, buf, len);
> +       }
> +
> +       bpf_ringbuf_discard_dynptr(&ptr_buf, 0);
> +
> +       err = err ?: bpf_dynptr_copy(&ptr_xdp, 2, &ptr_xdp, len, len * (chunks - 1));
> +
> +       bpf_for(i, 0, chunks - 1) {
> +               memset(buf, 0, sizeof(buf));
> +               err = err ?: bpf_dynptr_read(&buf, len, &ptr_xdp, 2 + i * len, 0);
> +               err = err ?: memcmp(data, buf, len);
> +       }
> +
> +       err = err ?: (bpf_dynptr_copy(&ptr_xdp, 2000, &ptr_xdp, 0, len * chunks) == -E2BIG ? 0 : 1);

overdoing it a bit with the whole `err ?: ` pattern, IMO


BTW, more questions to networking folks (maybe Martin knows). Is there
a way to setup SKB or XDP packet with a non-linear region for testing?

> +
> +       return XDP_DROP;
> +}
> --
> 2.48.1
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy
  2025-02-21  0:52   ` Andrii Nakryiko
@ 2025-02-21  0:56     ` Eduard Zingerman
  2025-02-21  1:03       ` Andrii Nakryiko
  2025-02-21 12:08     ` Mykyta Yatsenko
  1 sibling, 1 reply; 12+ messages in thread
From: Eduard Zingerman @ 2025-02-21  0:56 UTC (permalink / raw)
  To: Andrii Nakryiko, Mykyta Yatsenko
  Cc: bpf, ast, andrii, daniel, kafai, kernel-team, Mykyta Yatsenko

On Thu, 2025-02-20 at 16:52 -0800, Andrii Nakryiko wrote:
> On Tue, Feb 18, 2025 at 11:01 AM Mykyta Yatsenko
> <mykyta.yatsenko5@gmail.com> wrote:
> > 
> > From: Mykyta Yatsenko <yatsenko@meta.com>
> > 
> > Add XDP setup type for dynptr tests, enabling testing for
> > non-contiguous buffer.
> > Add 2 tests:
> >  - test_dynptr_copy - verify correctness for the fast (contiguous
> >  buffer) code path.
> >  - test_dynptr_copy_xdp - verifies code paths that handle
> >  non-contiguous buffer.
> > 
> > Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
> > ---
> >  tools/testing/selftests/bpf/bpf_kfuncs.h      |  8 ++
> >  .../testing/selftests/bpf/prog_tests/dynptr.c | 25 ++++++
> >  .../selftests/bpf/progs/dynptr_success.c      | 77 +++++++++++++++++++
> >  3 files changed, 110 insertions(+)
> > 
> > diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > index 8215c9b3115e..e9c193036c82 100644
> > --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
> > +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > @@ -43,6 +43,14 @@ extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak;
> >  extern __u32 bpf_dynptr_size(const struct bpf_dynptr *ptr) __ksym __weak;
> >  extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym __weak;
> > 
> > +/* Description
> > + *  Copy data from one dynptr to another
> > + * Returns
> > + *  Error code
> > + */
> > +extern int bpf_dynptr_copy(struct bpf_dynptr *dst, __u32 dst_off,
> > +                          struct bpf_dynptr *src, __u32 src_off, __u32 size) __ksym __weak;
> > +
> 
> Do we *need* this? Doesn't all this come from vmlinux.h nowadays?

It does come from vmlinux.h, but this test does not include vmlinux.h
(and compiles a bit faster because of that).

[...]


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy
  2025-02-21  0:56     ` Eduard Zingerman
@ 2025-02-21  1:03       ` Andrii Nakryiko
  0 siblings, 0 replies; 12+ messages in thread
From: Andrii Nakryiko @ 2025-02-21  1:03 UTC (permalink / raw)
  To: Eduard Zingerman
  Cc: Mykyta Yatsenko, bpf, ast, andrii, daniel, kafai, kernel-team,
	Mykyta Yatsenko

On Thu, Feb 20, 2025 at 4:56 PM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> On Thu, 2025-02-20 at 16:52 -0800, Andrii Nakryiko wrote:
> > On Tue, Feb 18, 2025 at 11:01 AM Mykyta Yatsenko
> > <mykyta.yatsenko5@gmail.com> wrote:
> > >
> > > From: Mykyta Yatsenko <yatsenko@meta.com>
> > >
> > > Add XDP setup type for dynptr tests, enabling testing for
> > > non-contiguous buffer.
> > > Add 2 tests:
> > >  - test_dynptr_copy - verify correctness for the fast (contiguous
> > >  buffer) code path.
> > >  - test_dynptr_copy_xdp - verifies code paths that handle
> > >  non-contiguous buffer.
> > >
> > > Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
> > > ---
> > >  tools/testing/selftests/bpf/bpf_kfuncs.h      |  8 ++
> > >  .../testing/selftests/bpf/prog_tests/dynptr.c | 25 ++++++
> > >  .../selftests/bpf/progs/dynptr_success.c      | 77 +++++++++++++++++++
> > >  3 files changed, 110 insertions(+)
> > >
> > > diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > index 8215c9b3115e..e9c193036c82 100644
> > > --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > @@ -43,6 +43,14 @@ extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak;
> > >  extern __u32 bpf_dynptr_size(const struct bpf_dynptr *ptr) __ksym __weak;
> > >  extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym __weak;
> > >
> > > +/* Description
> > > + *  Copy data from one dynptr to another
> > > + * Returns
> > > + *  Error code
> > > + */
> > > +extern int bpf_dynptr_copy(struct bpf_dynptr *dst, __u32 dst_off,
> > > +                          struct bpf_dynptr *src, __u32 src_off, __u32 size) __ksym __weak;
> > > +
> >
> > Do we *need* this? Doesn't all this come from vmlinux.h nowadays?
>
> It does come from vmlinux.h, but this test does not include vmlinux.h
> (and compiles a bit faster because of that).

let's switch to vmlinux.h then instead of manually adding kfunc declarations?

>
> [...]
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy
  2025-02-21  0:52   ` Andrii Nakryiko
  2025-02-21  0:56     ` Eduard Zingerman
@ 2025-02-21 12:08     ` Mykyta Yatsenko
  2025-02-21 17:16       ` Andrii Nakryiko
  1 sibling, 1 reply; 12+ messages in thread
From: Mykyta Yatsenko @ 2025-02-21 12:08 UTC (permalink / raw)
  To: Andrii Nakryiko, martin.lau
  Cc: bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87,
	Mykyta Yatsenko

On 21/02/2025 00:52, Andrii Nakryiko wrote:
> On Tue, Feb 18, 2025 at 11:01 AM Mykyta Yatsenko
> <mykyta.yatsenko5@gmail.com> wrote:
>> From: Mykyta Yatsenko <yatsenko@meta.com>
>>
>> Add XDP setup type for dynptr tests, enabling testing for
>> non-contiguous buffer.
>> Add 2 tests:
>>   - test_dynptr_copy - verify correctness for the fast (contiguous
>>   buffer) code path.
>>   - test_dynptr_copy_xdp - verifies code paths that handle
>>   non-contiguous buffer.
>>
>> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
>> ---
>>   tools/testing/selftests/bpf/bpf_kfuncs.h      |  8 ++
>>   .../testing/selftests/bpf/prog_tests/dynptr.c | 25 ++++++
>>   .../selftests/bpf/progs/dynptr_success.c      | 77 +++++++++++++++++++
>>   3 files changed, 110 insertions(+)
>>
>> diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
>> index 8215c9b3115e..e9c193036c82 100644
>> --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
>> +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
>> @@ -43,6 +43,14 @@ extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak;
>>   extern __u32 bpf_dynptr_size(const struct bpf_dynptr *ptr) __ksym __weak;
>>   extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym __weak;
>>
>> +/* Description
>> + *  Copy data from one dynptr to another
>> + * Returns
>> + *  Error code
>> + */
>> +extern int bpf_dynptr_copy(struct bpf_dynptr *dst, __u32 dst_off,
>> +                          struct bpf_dynptr *src, __u32 src_off, __u32 size) __ksym __weak;
>> +
> Do we *need* this? Doesn't all this come from vmlinux.h nowadays?
>
>>   /* Description
>>    *  Modify the address of a AF_UNIX sockaddr.
>>    * Returns
>> diff --git a/tools/testing/selftests/bpf/prog_tests/dynptr.c b/tools/testing/selftests/bpf/prog_tests/dynptr.c
>> index b614a5272dfd..247618958155 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/dynptr.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/dynptr.c
>> @@ -10,6 +10,7 @@ enum test_setup_type {
>>          SETUP_SYSCALL_SLEEP,
>>          SETUP_SKB_PROG,
>>          SETUP_SKB_PROG_TP,
>> +       SETUP_XDP_PROG,
>>   };
>>
>>   static struct {
>> @@ -18,6 +19,8 @@ static struct {
>>   } success_tests[] = {
>>          {"test_read_write", SETUP_SYSCALL_SLEEP},
>>          {"test_dynptr_data", SETUP_SYSCALL_SLEEP},
>> +       {"test_dynptr_copy", SETUP_SYSCALL_SLEEP},
>> +       {"test_dynptr_copy_xdp", SETUP_XDP_PROG},
>>          {"test_ringbuf", SETUP_SYSCALL_SLEEP},
>>          {"test_skb_readonly", SETUP_SKB_PROG},
>>          {"test_dynptr_skb_data", SETUP_SKB_PROG},
>> @@ -120,6 +123,28 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typ
>>
>>                  break;
>>          }
>> +       case SETUP_XDP_PROG:
>> +       {
>> +               char data[5000];
>> +               int err, prog_fd;
>> +
> no empty line here, opts is a variable
>
>> +               LIBBPF_OPTS(bpf_test_run_opts, opts,
>> +                           .data_in = &data,
>> +                           .data_size_in = sizeof(data),
>> +                           .repeat = 1,
>> +               );
>> +
>> +               prog_fd = bpf_program__fd(prog);
>> +               if (!ASSERT_GE(prog_fd, 0, "prog_fd"))
>> +                       goto cleanup;
>
> we shouldn't check this, if program loaded successfully this will
> always be true (and yeah, I know that existing code does that, we
> should remove or at least not duplicate this)
>
>> +
>> +               err = bpf_prog_test_run_opts(prog_fd, &opts);
>> +
>> +               if (!ASSERT_OK(err, "test_run"))
>> +                       goto cleanup;
>> +
>> +               break;
>> +       }
>>          }
>>
>>          ASSERT_EQ(skel->bss->err, 0, "err");
>> diff --git a/tools/testing/selftests/bpf/progs/dynptr_success.c b/tools/testing/selftests/bpf/progs/dynptr_success.c
>> index bfcc85686cf0..8a6b35418e39 100644
>> --- a/tools/testing/selftests/bpf/progs/dynptr_success.c
>> +++ b/tools/testing/selftests/bpf/progs/dynptr_success.c
>> @@ -567,3 +567,80 @@ int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location)
>>
>>          return 1;
>>   }
>> +
>> +SEC("?tp/syscalls/sys_enter_nanosleep")
>> +int test_dynptr_copy(void *ctx)
>> +{
>> +       char *data = "hello there, world!!";
>> +       char buf[32] = {'\0'};
>> +       __u32 sz = strlen(data);
> this is fragile, this is not guaranteed to work (only if compiler just
> substituted a constant value). maybe just use data[] = "hello
> there..." and use sizeof(data) then?
>
>> +       struct bpf_dynptr src, dst;
>> +
>> +       bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &src);
>> +       bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &dst);
>> +
>> +       err = bpf_dynptr_write(&src, 0, data, sz, 0);
>> +       err = err ?: bpf_dynptr_copy(&dst, 0, &src, 0, sz);
>> +       err = err ?: bpf_dynptr_read(buf, sz, &dst, 0, 0);
>> +       err = err ?: __builtin_memcmp(data, buf, sz);
>> +
>> +       err = err ?: bpf_dynptr_copy(&dst, 3, &src, 5, sz - 5);
>> +       err = err ?: bpf_dynptr_read(buf, sz - 5, &dst, 3, 0);
>> +       err = err ?: __builtin_memcmp(data + 5, buf, sz - 5);
>> +
>> +       bpf_ringbuf_discard_dynptr(&src, 0);
>> +       bpf_ringbuf_discard_dynptr(&dst, 0);
>> +       return 0;
>> +}
>> +
>> +SEC("xdp")
>> +int test_dynptr_copy_xdp(struct xdp_md *xdp)
>> +{
>> +       struct bpf_dynptr ptr_buf, ptr_xdp;
>> +       char *data = "qwertyuiopasdfghjkl;";
>> +       char buf[32] = {'\0'};
>> +       __u32 len = strlen(data);
> ditto
>
>> +       int i, chunks = 200;
>> +
>> +       bpf_dynptr_from_xdp(xdp, 0, &ptr_xdp);
>> +       bpf_ringbuf_reserve_dynptr(&ringbuf, len * chunks, 0, &ptr_buf);
>> +
>> +       bpf_for(i, 0, chunks) {
>> +               err =  err ?: bpf_dynptr_write(&ptr_buf, i * len, data, len, 0);
>> +       }
>> +
>> +       err = err ?: bpf_dynptr_copy(&ptr_xdp, 0, &ptr_buf, 0, len * chunks);
>> +
>> +       bpf_for(i, 0, chunks) {
>> +               memset(buf, 0, sizeof(buf));
> __builtin_memset(), memset() works only because compiler optimizes it
> to built-in, but let's not rely on that
>
>> +               err = err ?: bpf_dynptr_read(&buf, len, &ptr_xdp, i * len, 0);
>> +               err = err ?: memcmp(data, buf, len);
> __builtin_memcmp() and all the other cases below, please
>
>> +       }
>> +
>> +       memset(buf, 0, sizeof(buf));
>> +       bpf_for(i, 0, chunks) {
>> +               err = err ?: bpf_dynptr_write(&ptr_buf, i * len, buf, len, 0);
>> +       }
>> +
>> +       err = err ?: bpf_dynptr_copy(&ptr_buf, 0, &ptr_xdp, 0, len * chunks);
>> +
>> +       bpf_for(i, 0, chunks) {
>> +               memset(buf, 0, sizeof(buf));
>> +               err = err ?: bpf_dynptr_read(&buf, len, &ptr_buf, i * len, 0);
>> +               err = err ?: memcmp(data, buf, len);
>> +       }
>> +
>> +       bpf_ringbuf_discard_dynptr(&ptr_buf, 0);
>> +
>> +       err = err ?: bpf_dynptr_copy(&ptr_xdp, 2, &ptr_xdp, len, len * (chunks - 1));
>> +
>> +       bpf_for(i, 0, chunks - 1) {
>> +               memset(buf, 0, sizeof(buf));
>> +               err = err ?: bpf_dynptr_read(&buf, len, &ptr_xdp, 2 + i * len, 0);
>> +               err = err ?: memcmp(data, buf, len);
>> +       }
>> +
>> +       err = err ?: (bpf_dynptr_copy(&ptr_xdp, 2000, &ptr_xdp, 0, len * chunks) == -E2BIG ? 0 : 1);
> overdoing it a bit with the whole `err ?: ` pattern, IMO
>
>
> BTW, more questions to networking folks (maybe Martin knows). Is there
> a way to setup SKB or XDP packet with a non-linear region for testing?
The setup I made in dynptr.c for XDP makes non-linear region.
It looks like maximum linear mem size is 4k, so when going above that,
it creates multiple frags in xdp_buff:

```

char data[5000];
...
.data_in = &data,

```

I verified that in this test xdp_buff is non-linear, and all non-fast 
code paths of
the bpf_dynptr_copy are tested.

>
>> +
>> +       return XDP_DROP;
>> +}
>> --
>> 2.48.1
>>
Thanks for findings, fixing them in v2.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy
  2025-02-21 12:08     ` Mykyta Yatsenko
@ 2025-02-21 17:16       ` Andrii Nakryiko
  0 siblings, 0 replies; 12+ messages in thread
From: Andrii Nakryiko @ 2025-02-21 17:16 UTC (permalink / raw)
  To: Mykyta Yatsenko
  Cc: martin.lau, bpf, ast, andrii, daniel, kafai, kernel-team, eddyz87,
	Mykyta Yatsenko

On Fri, Feb 21, 2025 at 4:08 AM Mykyta Yatsenko
<mykyta.yatsenko5@gmail.com> wrote:
>
> On 21/02/2025 00:52, Andrii Nakryiko wrote:
> > On Tue, Feb 18, 2025 at 11:01 AM Mykyta Yatsenko
> > <mykyta.yatsenko5@gmail.com> wrote:
> >> From: Mykyta Yatsenko <yatsenko@meta.com>
> >>
> >> Add XDP setup type for dynptr tests, enabling testing for
> >> non-contiguous buffer.
> >> Add 2 tests:
> >>   - test_dynptr_copy - verify correctness for the fast (contiguous
> >>   buffer) code path.
> >>   - test_dynptr_copy_xdp - verifies code paths that handle
> >>   non-contiguous buffer.
> >>
> >> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
> >> ---
> >>   tools/testing/selftests/bpf/bpf_kfuncs.h      |  8 ++
> >>   .../testing/selftests/bpf/prog_tests/dynptr.c | 25 ++++++
> >>   .../selftests/bpf/progs/dynptr_success.c      | 77 +++++++++++++++++++
> >>   3 files changed, 110 insertions(+)
> >>

[...]

> >
> > BTW, more questions to networking folks (maybe Martin knows). Is there
> > a way to setup SKB or XDP packet with a non-linear region for testing?
> The setup I made in dynptr.c for XDP makes non-linear region.
> It looks like maximum linear mem size is 4k, so when going above that,
> it creates multiple frags in xdp_buff:
>
> ```
>
> char data[5000];
> ...
> .data_in = &data,
>
> ```
>
> I verified that in this test xdp_buff is non-linear, and all non-fast
> code paths of
> the bpf_dynptr_copy are tested.

Ah, awesome, great job!

>
> >
> >> +
> >> +       return XDP_DROP;
> >> +}
> >> --
> >> 2.48.1
> >>
> Thanks for findings, fixing them in v2.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2025-02-21 17:16 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-18 19:00 [PATCH bpf-next 0/3] introduce bpf_dynptr_copy kfunc Mykyta Yatsenko
2025-02-18 19:00 ` [PATCH bpf-next 1/3] bpf/helpers: refactor bpf_dynptr_read and bpf_dynptr_write Mykyta Yatsenko
2025-02-18 19:00 ` [PATCH bpf-next 2/3] bpf/helpers: introduce bpf_dynptr_copy kfunc Mykyta Yatsenko
2025-02-19  1:01   ` Eduard Zingerman
2025-02-21  0:42   ` Andrii Nakryiko
2025-02-18 19:00 ` [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy Mykyta Yatsenko
2025-02-19  1:04   ` Eduard Zingerman
2025-02-21  0:52   ` Andrii Nakryiko
2025-02-21  0:56     ` Eduard Zingerman
2025-02-21  1:03       ` Andrii Nakryiko
2025-02-21 12:08     ` Mykyta Yatsenko
2025-02-21 17:16       ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox