Skip to contentSkip to conversation

introduce bpf_dynptr_copy kfunc

PATCH Superseded Mykyta Yatsenko 2 participants 8 messages Feb 21, 2025 – Feb 26, 2025

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 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                          |  75 +++++++++++-
 .../testing/selftests/bpf/prog_tests/dynptr.c |  21 ++++
 .../selftests/bpf/progs/dynptr_success.c      | 112 +++++++++++++++++-
 3 files changed, 199 insertions(+), 9 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v2 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 183298fc11ba..6600aa4492ec 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 v2 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 | 55 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
kernel/bpf/helpers.c +55 -0
index 6600aa4492ec..264afa0effb0 100644
@@ -2770,6 +2770,60 @@ __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p,
 	return 0;
 }
 
+/**
+ * bpf_dynptr_copy() - Copy data from one dynptr to another.
+ * @dst_ptr: Destination dynptr - where data should be copied to
+ * @dst_off: Offset into the destination dynptr
+ * @src_ptr: Source dynptr - where data should be copied from
+ * @src_off: Offset into the source dynptr
+ * @size: Length of the data to copy from source to destination
+ *
+ * Copies data from source dynptr to destination dynptr
+ */
+__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;
+	void *src_slice, *dst_slice;
+	char buf[256];
+	u32 off;
+
+	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;
+	}
+
+	if (src_slice)
+		return __bpf_dynptr_write(dst, dst_off, src_slice, size, 0);
+
+	if (dst_slice)
+		return __bpf_dynptr_read(dst_slice, size, src, src_off, 0);
+
+	if (bpf_dynptr_check_off_len(dst, dst_off, size) ||
+	    bpf_dynptr_check_off_len(src, src_off, size))
+		return -E2BIG;
+
+	off = 0;
+	while (off < size) {
+		u32 chunk_sz = min_t(u32, sizeof(buf), size - off);
+		int err = 0;
+
+		err = __bpf_dynptr_read(buf, chunk_sz, src, src_off + off, 0);
+		if (err)
+			return err;
+		err = __bpf_dynptr_write(dst, dst_off + off, buf, chunk_sz, 0);
+		if (err)
+			return err;
+
+		off += chunk_sz;
+	}
+	return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -3218,6 +3272,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
Signature · 1 line
2.48.1
Acked-by on v2 2/3
#3 lore raw

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

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 | 55 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

LGTM, a bit of unnecessary code I pointed out, but I like how minimal and clean all this looks, and completely reused pre-existing APIs.

Quoted 33 lines
kernel/bpf/helpers.c +25 -0
index 6600aa4492ec..264afa0effb0 100644
@@ -2770,6 +2770,60 @@ __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p,
        return 0;
 }
 
+/**
+ * bpf_dynptr_copy() - Copy data from one dynptr to another.
+ * @dst_ptr: Destination dynptr - where data should be copied to
+ * @dst_off: Offset into the destination dynptr
+ * @src_ptr: Source dynptr - where data should be copied from
+ * @src_off: Offset into the source dynptr
+ * @size: Length of the data to copy from source to destination
+ *
+ * Copies data from source dynptr to destination dynptr
+ */
+__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;
+       void *src_slice, *dst_slice;
+       char buf[256];
+       u32 off;
+
+       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 2807

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       }
+
+       if (src_slice)
+               return __bpf_dynptr_write(dst, dst_off, src_slice, size, 0);
+
+       if (dst_slice)
+               return __bpf_dynptr_read(dst_slice, size, src, src_off, 0);
+
+       if (bpf_dynptr_check_off_len(dst, dst_off, size) ||
+           bpf_dynptr_check_off_len(src, src_off, size))

__bpf_dynptr_read() and __bpf_dynptr_write() do these checks, so either it's unnecessary and we should keep all the sanity checking to dynptr_{read,write}, OR we ensure __bpf_dynptr_read/write don't do sanity checking every single time and we do full checking here, but then we'll need to also check !dst->data || __bpf_dynptr_is_rdonly(dst)

I think for now, I'd keep all the sanity checking to read/write and not over-optimize. So let's drop these checks?

pw-bot: cr

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

Quoted 6 lines
kernel/bpf/helpers.c +6 -0
+               return -E2BIG;
+
+       off = 0;
+       while (off < size) {
+               u32 chunk_sz = min_t(u32, sizeof(buf), size - off);
+               int err = 0;

nit: unnecessary = 0 initialization, you are overwriting it immediately below

Quoted 27 lines
kernel/bpf/helpers.c +14 -1
+
+               err = __bpf_dynptr_read(buf, chunk_sz, src, src_off + off, 0);
+               if (err)
+                       return err;
+               err = __bpf_dynptr_write(dst, dst_off + off, buf, chunk_sz, 0);
+               if (err)
+                       return err;
+
+               off += chunk_sz;
+       }
+       return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
        return obj;
@@ -3218,6 +3272,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
--

2.48.1

#4 lore raw

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

Quoted 75 lines · Andrii Nakryiko
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 5 lines
---
  kernel/bpf/helpers.c | 55 ++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 55 insertions(+)

LGTM, a bit of unnecessary code I pointed out, but I like how minimal and clean all this looks, and completely reused pre-existing APIs.

Quoted 33 lines
kernel/bpf/helpers.c +25 -0
index 6600aa4492ec..264afa0effb0 100644
@@ -2770,6 +2770,60 @@ __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p,
         return 0;
  }
 
+/**
+ * bpf_dynptr_copy() - Copy data from one dynptr to another.
+ * @dst_ptr: Destination dynptr - where data should be copied to
+ * @dst_off: Offset into the destination dynptr
+ * @src_ptr: Source dynptr - where data should be copied from
+ * @src_off: Offset into the source dynptr
+ * @size: Length of the data to copy from source to destination
+ *
+ * Copies data from source dynptr to destination dynptr
+ */
+__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;
+       void *src_slice, *dst_slice;
+       char buf[256];
+       u32 off;
+
+       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 2807

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       }
+
+       if (src_slice)
+               return __bpf_dynptr_write(dst, dst_off, src_slice, size, 0);
+
+       if (dst_slice)
+               return __bpf_dynptr_read(dst_slice, size, src, src_off, 0);
+
+       if (bpf_dynptr_check_off_len(dst, dst_off, size) ||
+           bpf_dynptr_check_off_len(src, src_off, size))

__bpf_dynptr_read() and __bpf_dynptr_write() do these checks, so either it's unnecessary and we should keep all the sanity checking to dynptr_{read,write}, OR we ensure __bpf_dynptr_read/write don't do sanity checking every single time and we do full checking here, but then we'll need to also check !dst->data || __bpf_dynptr_is_rdonly(dst)

Quoted 2 lines

I think for now, I'd keep all the sanity checking to read/write and not over-optimize. So let's drop these checks?

I added this check to make the process of copying data chunk by chunk more transactional/atomic, trying to make the outcome of partial data copying less likely.
Other checks I assume would equally fail/pass for the 1st and last chunk. Of course, we don't give any atomicity guarantee here, but I thought size mismatch could be a common case to handle.

Quoted 38 lines

pw-bot: cr

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

Quoted 6 lines
kernel/bpf/helpers.c +6 -0
+               return -E2BIG;
+
+       off = 0;
+       while (off < size) {
+               u32 chunk_sz = min_t(u32, sizeof(buf), size - off);
+               int err = 0;

nit: unnecessary = 0 initialization, you are overwriting it immediately below

Quoted 27 lines
kernel/bpf/helpers.c +14 -1
+
+               err = __bpf_dynptr_read(buf, chunk_sz, src, src_off + off, 0);
+               if (err)
+                       return err;
+               err = __bpf_dynptr_write(dst, dst_off + off, buf, chunk_sz, 0);
+               if (err)
+                       return err;
+
+               off += chunk_sz;
+       }
+       return 0;
+}
+
  __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
  {
         return obj;
@@ -3218,6 +3272,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
--

2.48.1

#5 lore raw

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

Quoted 79 lines · Mykyta Yatsenko
Quoted 75 lines · Andrii Nakryiko
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 5 lines
---
  kernel/bpf/helpers.c | 55 ++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 55 insertions(+)

LGTM, a bit of unnecessary code I pointed out, but I like how minimal and clean all this looks, and completely reused pre-existing APIs.

Quoted 33 lines
kernel/bpf/helpers.c +25 -0
index 6600aa4492ec..264afa0effb0 100644
@@ -2770,6 +2770,60 @@ __bpf_kfunc int bpf_dynptr_clone(const struct bpf_dynptr *p,
         return 0;
  }
 
+/**
+ * bpf_dynptr_copy() - Copy data from one dynptr to another.
+ * @dst_ptr: Destination dynptr - where data should be copied to
+ * @dst_off: Offset into the destination dynptr
+ * @src_ptr: Source dynptr - where data should be copied from
+ * @src_off: Offset into the source dynptr
+ * @size: Length of the data to copy from source to destination
+ *
+ * Copies data from source dynptr to destination dynptr
+ */
+__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;
+       void *src_slice, *dst_slice;
+       char buf[256];
+       u32 off;
+
+       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 2807

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       }
+
+       if (src_slice)
+               return __bpf_dynptr_write(dst, dst_off, src_slice, size, 0);
+
+       if (dst_slice)
+               return __bpf_dynptr_read(dst_slice, size, src, src_off, 0);
+
+       if (bpf_dynptr_check_off_len(dst, dst_off, size) ||
+           bpf_dynptr_check_off_len(src, src_off, size))

__bpf_dynptr_read() and __bpf_dynptr_write() do these checks, so either it's unnecessary and we should keep all the sanity checking to dynptr_{read,write}, OR we ensure __bpf_dynptr_read/write don't do sanity checking every single time and we do full checking here, but then we'll need to also check !dst->data || __bpf_dynptr_is_rdonly(dst)

Quoted 2 lines

I think for now, I'd keep all the sanity checking to read/write and not over-optimize. So let's drop these checks?

Quoted 7 lines

I added this check to make the process of copying data chunk by chunk more transactional/atomic,
trying to make the outcome of partial data copying less likely. Other checks I assume would equally fail/pass for the 1st and last chunk. Of course, we don't give
any atomicity guarantee here, but I thought size mismatch could be a common case to handle.

Ah, makes sense. Because the generic read/write below will work on small chunks we won't detect violation until much later. Ok, then I guess this is fine as is.

Quoted 39 lines
Quoted 38 lines

pw-bot: cr

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

Quoted 6 lines
kernel/bpf/helpers.c +6 -0
+               return -E2BIG;
+
+       off = 0;
+       while (off < size) {
+               u32 chunk_sz = min_t(u32, sizeof(buf), size - off);
+               int err = 0;

nit: unnecessary = 0 initialization, you are overwriting it immediately below

Quoted 27 lines
kernel/bpf/helpers.c +14 -1
+
+               err = __bpf_dynptr_read(buf, chunk_sz, src, src_off + off, 0);
+               if (err)
+                       return err;
+               err = __bpf_dynptr_write(dst, dst_off + off, buf, chunk_sz, 0);
+               if (err)
+                       return err;
+
+               off += chunk_sz;
+       }
+       return 0;
+}
+
  __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
  {
         return obj;
@@ -3218,6 +3272,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
--

2.48.1

#6 lore raw

PATCH v2 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.

---
 .../testing/selftests/bpf/prog_tests/dynptr.c |  21 ++++
 .../selftests/bpf/progs/dynptr_success.c      | 112 +++++++++++++++++-
 2 files changed, 128 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/dynptr.c +21 -0
index b614a5272dfd..e29cc16124c2 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,24 @@ 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);
+		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 +107 -5

144 lines not on this page · raw message

Signature · 1 line
2.48.1
#7 lore raw

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

Quoted 160 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.

---
 .../testing/selftests/bpf/prog_tests/dynptr.c |  21 ++++
 .../selftests/bpf/progs/dynptr_success.c      | 112 +++++++++++++++++-
 2 files changed, 128 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/dynptr.c +21 -0
index b614a5272dfd..e29cc16124c2 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,24 @@ 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);
+               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 +52 -5
index bfcc85686cf0..dd10411d1c02 100644
@@ -1,20 +1,19 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2022 Facebook */
 
+#include <vmlinux.h>
 #include <string.h>
 #include <stdbool.h>
-#include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 #include "bpf_misc.h"
-#include "bpf_kfuncs.h"
 #include "errno.h"
 
 char _license[] SEC("license") = "GPL";
 
 int pid, err, val;
 
-struct sample {
+struct ringbuf_sample {
        int pid;
        int seq;
        long value;
@@ -121,7 +120,7 @@ int test_dynptr_data(void *ctx)
 
 static int ringbuf_callback(__u32 index, void *data)
 {
-       struct sample *sample;
+       struct ringbuf_sample *sample;
 
        struct bpf_dynptr *ptr = (struct bpf_dynptr *)data;
 
@@ -138,7 +137,7 @@ SEC("?tp/syscalls/sys_enter_nanosleep")
 int test_ringbuf(void *ctx)
 {
        struct bpf_dynptr ptr;
-       struct sample *sample;
+       struct ringbuf_sample *sample;
 
        if (bpf_get_current_pid_tgid() >> 32 != pid)
                return 0;
@@ -567,3 +566,106 @@ 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 = sizeof(data);
+       struct bpf_dynptr src, dst;
+
+       bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &src);
+       bpf_ringbuf_reserve_dynptr(&ringbuf, sz, 0, &dst);
+
+       /* Test basic case of copying contiguous memory backed dynptrs */
+       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);
+
+       /* Test that offsets are handled correctly */
+       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 = sizeof(data);
+       int i, chunks = 200;
+
+       /* ptr_xdp is backed by non-contiguous memory */
+       bpf_dynptr_from_xdp(xdp, 0, &ptr_xdp);
+       bpf_ringbuf_reserve_dynptr(&ringbuf, len * chunks, 0, &ptr_buf);
+
+       /* Destination dynptr is backed by non-contiguous memory */
+       bpf_for(i, 0, chunks) {
+               err = bpf_dynptr_write(&ptr_buf, i * len, data, len, 0);
+               if (err)
+                       goto out;
+       }
+

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

Quoted 10 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +10 -0
+       err = bpf_dynptr_copy(&ptr_xdp, 0, &ptr_buf, 0, len * chunks);
+       if (err)
+               goto out;
+
+       bpf_for(i, 0, chunks) {
+               __builtin_memset(buf, 0, sizeof(buf));
+               err = bpf_dynptr_read(&buf, len, &ptr_xdp, i * len, 0);
+               if (err)
+                       goto out;
+               if (__builtin_memcmp(data, buf, len) != 0)

Ok, so with GCC-BPF (see [0]) there is something wrong, seems like GCC doesn't "inline" __builtin_memcmp() and falls back to calling actual memcmp(). This is strange, as your buffer is pretty small (32 bytes), so it should have been inlined. Jose, can you please take a look from GCC-BPF side?

Meanwhile, Mykyta, can you please implement __builtin_memcp() using bpf_for() loop logic to unblock landing this? Thanks!

pw-bot: cr

[0] https://github.com/kernel-patches/bpf/actions/runs/13548082739/job/37865378205

Quoted 48 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +45 -1
+                       goto out;
+       }
+
+       /* Source dynptr is backed by non-contiguous memory */
+       __builtin_memset(buf, 0, sizeof(buf));
+       bpf_for(i, 0, chunks) {
+               err = bpf_dynptr_write(&ptr_buf, i * len, buf, len, 0);
+               if (err)
+                       goto out;
+       }
+
+       err = bpf_dynptr_copy(&ptr_buf, 0, &ptr_xdp, 0, len * chunks);
+       if (err)
+               goto out;
+
+       bpf_for(i, 0, chunks) {
+               __builtin_memset(buf, 0, sizeof(buf));
+               err = bpf_dynptr_read(&buf, len, &ptr_buf, i * len, 0);
+               if (err)
+                       goto out;
+               if (__builtin_memcmp(data, buf, len) != 0)
+                       goto out;
+       }
+
+       /* Both source and destination dynptrs are backed by non-contiguous memory */
+       err = bpf_dynptr_copy(&ptr_xdp, 2, &ptr_xdp, len, len * (chunks - 1));
+       if (err)
+               goto out;
+
+       bpf_for(i, 0, chunks - 1) {
+               __builtin_memset(buf, 0, sizeof(buf));
+               err = bpf_dynptr_read(&buf, len, &ptr_xdp, 2 + i * len, 0);
+               if (err)
+                       goto out;
+               if (__builtin_memcmp(data, buf, len) != 0)
+                       goto out;
+       }
+
+       if (bpf_dynptr_copy(&ptr_xdp, 2000, &ptr_xdp, 0, len * chunks) != -E2BIG)
+               err = 1;
+
+out:
+       bpf_ringbuf_discard_dynptr(&ptr_buf, 0);
+       return XDP_DROP;
+}
--

2.48.1