Skip to contentSkip to conversation

introduce bpf_dynptr_copy kfunc

PATCH Superseded Mykyta Yatsenko 3 participants 12 messages Feb 18, 2025 – Feb 21, 2025

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover introduce bpf_dynptr_copy kfunc

From: Mykyta Yatsenko <[email protected]>

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(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/3 bpf/helpers: refactor bpf_dynptr_read and bpf_dynptr_write

From: Mykyta Yatsenko <[email protected]>

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.

---
 kernel/bpf/helpers.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
kernel/bpf/helpers.c +16 -4
index f27ce162427a..2833558c3009 100644
@@ -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,
Signature · 1 line
2.48.1
#2 lore raw

PATCH 2/3 bpf/helpers: introduce bpf_dynptr_copy kfunc

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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

---
 kernel/bpf/helpers.c  | 37 +++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c |  3 +++
 2 files changed, 40 insertions(+)
kernel/bpf/helpers.c +37 -0
index 2833558c3009..ac5fbdfc504d 100644
@@ -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
kernel/bpf/verifier.c +3 -0
index e7bc74171c99..3c567bfcc582 100644
@@ -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)
Signature · 1 line
2.48.1
maintainer Acked-by on v1 2/3
#3 lore raw

Re: [PATCH bpf-next 2/3] bpf/helpers: introduce bpf_dynptr_copy kfunc

Quoted 11 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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

Quoted 2 lines

---

kernel/bpf/helpers.c __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p, · line 2771

Quoted 11 lines
 kernel/bpf/helpers.c  | 37 +++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c |  3 +++
 2 files changed, 40 insertions(+)
kernel/bpf/helpers.c +0 -0
index 2833558c3009..ac5fbdfc504d 100644
@@ -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.

Quoted 2 lines
kernel/bpf/helpers.c +2 -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)

[...]

kernel/bpf/verifier.c

Quoted 10 lines
kernel/bpf/helpers.c +1 -0
@@ -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
kernel/bpf/verifier.c +0 -0
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.

Quoted 26 lines
kernel/bpf/verifier.c +3 -0
@@ -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)
#4 lore raw

Re: [PATCH bpf-next 2/3] bpf/helpers: introduce bpf_dynptr_copy kfunc

In reply to Mykyta Yatsenko
Quoted 12 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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

Quoted 5 lines
---
 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

Quoted 6 lines
kernel/bpf/helpers.c +0 -0
index 2833558c3009..ac5fbdfc504d 100644

kernel/bpf/helpers.c __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p, · line 2778

Quoted 9 lines
kernel/bpf/helpers.c +6 -0
@@ -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

kernel/bpf/helpers.c __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p, · line 2779

Quoted 1 line
kernel/bpf/helpers.c +1 -0
+       int err = 0;

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

kernel/bpf/helpers.c __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p, · line 2785

Quoted 6 lines
kernel/bpf/helpers.c +6 -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);

return 0;

kernel/bpf/helpers.c __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p, · line 2787

Quoted 2 lines
kernel/bpf/helpers.c +2 -0
+       } else if (src_slice) {
+               err = __bpf_dynptr_write(dst, dst_off, src_slice, size, 0);

return __bpf_dynptr_write(...)

kernel/bpf/helpers.c __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p, · line 2789

Quoted 2 lines
kernel/bpf/helpers.c +2 -0
+       } else if (dst_slice) {
+               err = __bpf_dynptr_read(dst_slice, size, src, src_off, 0);

ditto, direct return

kernel/bpf/helpers.c __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p, · line 2796

Quoted 7 lines
kernel/bpf/helpers.c +7 -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;

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

kernel/bpf/helpers.c __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p, · line 2798

Quoted 2 lines
kernel/bpf/helpers.c +2 -0
+
+               while (err == 0 && off < size) {

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

kernel/bpf/helpers.c __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p, · line 2799

Quoted 1 line
kernel/bpf/helpers.c +1 -0
+                       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);

kernel/bpf/helpers.c __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p, · line 2802

Quoted 3 lines
kernel/bpf/helpers.c +3 -0
+
+                       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.

Quoted 48 lines
kernel/bpf/helpers.c +7 -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
kernel/bpf/verifier.c +3 -1
index e7bc74171c99..3c567bfcc582 100644
@@ -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

#5 lore raw

PATCH 3/3 selftests/bpf: add tests for bpf_dynptr_copy

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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.

---
 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(+)
tools/testing/selftests/bpf/bpf_kfuncs.h +8 -0
index 8215c9b3115e..e9c193036c82 100644
@@ -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
tools/testing/selftests/bpf/prog_tests/dynptr.c +25 -0
index b614a5272dfd..247618958155 100644
@@ -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");
tools/testing/selftests/bpf/progs/dynptr_success.c +77 -0

80 lines not on this page · raw message

Signature · 1 line
2.48.1
maintainer Acked-by on v1 3/3
#6 lore raw

Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy

Quoted 10 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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.

Quoted 2 lines

---

[...]

tools/testing/selftests/bpf/progs/dynptr_success.c int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location) · line 598

Quoted 3 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +3 -0
+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.

Quoted 48 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +48 -0
+	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;
+}
#7 lore raw

Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy

In reply to Mykyta Yatsenko
Quoted 24 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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.

---
 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(+)
tools/testing/selftests/bpf/bpf_kfuncs.h +0 -0
index 8215c9b3115e..e9c193036c82 100644
@@ -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;

tools/testing/selftests/bpf/bpf_kfuncs.h extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak; · line 53

Quoted 10 lines
tools/testing/selftests/bpf/bpf_kfuncs.h +8 -0
 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?

Quoted 25 lines
tools/testing/selftests/bpf/bpf_kfuncs.h +0 -0
 /* Description
  *  Modify the address of a AF_UNIX sockaddr.
  * Returns
tools/testing/selftests/bpf/prog_tests/dynptr.c +3 -0
index b614a5272dfd..247618958155 100644
@@ -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},

tools/testing/selftests/bpf/prog_tests/dynptr.c static void verify_success(const char *prog_name, enum test_setup_type setup_typ · line 130

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/dynptr.c +5 -0
@@ -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

tools/testing/selftests/bpf/prog_tests/dynptr.c static void verify_success(const char *prog_name, enum test_setup_type setup_typ · line 139

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/dynptr.c +9 -0
+               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)

Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/dynptr.c +8 -0
+
+               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");
tools/testing/selftests/bpf/progs/dynptr_success.c +0 -0
index bfcc85686cf0..8a6b35418e39 100644

tools/testing/selftests/bpf/progs/dynptr_success.c int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location) · line 576

Quoted 10 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +7 -0
@@ -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?

Quoted 16 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +16 -0
+       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);

tools/testing/selftests/bpf/progs/dynptr_success.c int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location) · line 602

Quoted 10 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +10 -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

tools/testing/selftests/bpf/progs/dynptr_success.c int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location) · line 615

Quoted 13 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +13 -0
+       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

tools/testing/selftests/bpf/progs/dynptr_success.c int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location) · line 617

Quoted 2 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +2 -0
+               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

Quoted 16 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +16 -0
+       }
+
+       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);

tools/testing/selftests/bpf/progs/dynptr_success.c int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location) · line 643

Quoted 10 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +10 -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?

Quoted 6 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +3 -1
+
+       return XDP_DROP;
+}
--

2.48.1

maintainer
#8 lore raw

Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy

Quoted 28 lines · Andrii Nakryiko
Quoted 26 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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.

---
 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(+)
tools/testing/selftests/bpf/bpf_kfuncs.h +0 -0
index 8215c9b3115e..e9c193036c82 100644
@@ -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;
 
Quoted 10 lines

tools/testing/selftests/bpf/bpf_kfuncs.h extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak; · line 53

Quoted 8 lines
tools/testing/selftests/bpf/bpf_kfuncs.h +8 -0
+/* 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).

[...]

#9 lore raw

Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy

Quoted 41 lines · Eduard Zingerman
Quoted 28 lines · Andrii Nakryiko
Quoted 26 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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.

---
 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(+)
tools/testing/selftests/bpf/bpf_kfuncs.h +0 -0
index 8215c9b3115e..e9c193036c82 100644
@@ -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;
 
Quoted 10 lines

tools/testing/selftests/bpf/bpf_kfuncs.h extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak; · line 53

Quoted 8 lines
tools/testing/selftests/bpf/bpf_kfuncs.h +8 -0
+/* 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?

Quoted 2 lines

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?

Quoted 3 lines

[...]

#10 lore raw

Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy

In reply to Andrii Nakryiko
Quoted 193 lines · Andrii Nakryiko
Quoted 23 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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.

---
  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(+)
tools/testing/selftests/bpf/bpf_kfuncs.h +0 -0
index 8215c9b3115e..e9c193036c82 100644
@@ -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;

tools/testing/selftests/bpf/bpf_kfuncs.h extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak; · line 53

Quoted 10 lines
tools/testing/selftests/bpf/bpf_kfuncs.h +8 -0
  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?

Quoted 25 lines
tools/testing/selftests/bpf/bpf_kfuncs.h +0 -0
  /* Description
   *  Modify the address of a AF_UNIX sockaddr.
   * Returns
tools/testing/selftests/bpf/prog_tests/dynptr.c +3 -0
index b614a5272dfd..247618958155 100644
@@ -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},

tools/testing/selftests/bpf/prog_tests/dynptr.c static void verify_success(const char *prog_name, enum test_setup_type setup_typ · line 130

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/dynptr.c +5 -0
@@ -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

tools/testing/selftests/bpf/prog_tests/dynptr.c static void verify_success(const char *prog_name, enum test_setup_type setup_typ · line 139

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/dynptr.c +9 -0
+               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)

Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/dynptr.c +8 -0
+
+               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");
tools/testing/selftests/bpf/progs/dynptr_success.c +0 -0
index bfcc85686cf0..8a6b35418e39 100644

tools/testing/selftests/bpf/progs/dynptr_success.c int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location) · line 576

Quoted 10 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +7 -0
@@ -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?

Quoted 16 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +16 -0
+       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);

tools/testing/selftests/bpf/progs/dynptr_success.c int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location) · line 602

Quoted 10 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +10 -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

tools/testing/selftests/bpf/progs/dynptr_success.c int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location) · line 615

Quoted 13 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +13 -0
+       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

tools/testing/selftests/bpf/progs/dynptr_success.c int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location) · line 617

Quoted 2 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +2 -0
+               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

Quoted 16 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +16 -0
+       }
+
+       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);

tools/testing/selftests/bpf/progs/dynptr_success.c int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location) · line 643

Quoted 10 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +10 -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

Quoted 2 lines

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.

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +3 -1
+
+       return XDP_DROP;
+}
--

2.48.1

Thanks for findings, fixing them in v2.

#11 lore raw

Re: [PATCH bpf-next 3/3] selftests/bpf: add tests for bpf_dynptr_copy

Quoted 21 lines · Mykyta Yatsenko
Quoted 19 lines · Andrii Nakryiko
Quoted 17 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

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.

---
  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(+)

[...]

Quoted 15 lines
Quoted 3 lines

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,

```
Quoted 3 lines

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!

Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +3 -1
+
+       return XDP_DROP;
+}
--

2.48.1

Thanks for findings, fixing them in v2.