Skip to contentSkip to conversation

Dynptr convenience helpers

PATCH Superseded Joanne Koong 3 participants 22 messages Apr 9, 2023 – Apr 20, 2023

Subsystems (7)
Kselftest BPF Tracing BPF selftests +3 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Dynptr convenience helpers

This patchset is the 3rd in the dynptr series. The 1st (dynptr fundamentals) can be found here [0] and the second (skb + xdp dynptrs) can be found here [1].

This patchset adds the following convenience helpers for interacting with dynptrs:

int bpf_dynptr_trim(struct bpf_dynptr *ptr, __u32 len) __ksym;
int bpf_dynptr_advance(struct bpf_dynptr *ptr, __u32 len) __ksym;
int bpf_dynptr_is_null(const struct bpf_dynptr *ptr) __ksym;
int bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym;
__u32 bpf_dynptr_get_size(const struct bpf_dynptr *ptr) __ksym;
__u32 bpf_dynptr_get_offset(const struct bpf_dynptr *ptr) __ksym;
int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym;

[0] https://lore.kernel.org/bpf/[email protected]/
[1] https://lore.kernel.org/bpf/[email protected]/

Joanne Koong (5):
bpf: Add bpf_dynptr_trim and bpf_dynptr_advance
bpf: Add bpf_dynptr_is_null and bpf_dynptr_is_rdonly
bpf: Add bpf_dynptr_get_size and bpf_dynptr_get_offset
bpf: Add bpf_dynptr_clone
selftests/bpf: add tests for dynptr convenience helpers

 include/linux/bpf.h                           |   2 +-
 kernel/bpf/helpers.c                          | 108 +++++-
 kernel/bpf/verifier.c                         | 125 ++++++-
 kernel/trace/bpf_trace.c                      |   4 +-
 tools/testing/selftests/bpf/bpf_kfuncs.h      |   8 +
 .../testing/selftests/bpf/prog_tests/dynptr.c |   6 +
 .../testing/selftests/bpf/progs/dynptr_fail.c | 313 +++++++++++++++++
 .../selftests/bpf/progs/dynptr_success.c      | 320 ++++++++++++++++++
 8 files changed, 864 insertions(+), 22 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/5 bpf: Add bpf_dynptr_trim and bpf_dynptr_advance

bpf_dynptr_trim decreases the size of a dynptr by the specified number of bytes (offset remains the same). bpf_dynptr_advance advances the offset of the dynptr by the specified number of bytes (size decreases correspondingly).

Trimming or advancing the dynptr may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, advance/trim can be used to narrow in on the specific region to hash.

---
 kernel/bpf/helpers.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
kernel/bpf/helpers.c +49 -0
index b6a5cda5bb59..51b4c4b5dbed 100644
@@ -1448,6 +1448,13 @@ u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
 	return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+	u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+	ptr->size = new_size | metadata;
+}
+
 int bpf_dynptr_check_size(u32 size)
 {
 	return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -2275,6 +2282,46 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
 	return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
 }
 
+/* For dynptrs, the offset may only be advanced and the size may only be decremented */
+static int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 off_inc, u32 sz_dec)
+{
+	u32 size;
+
+	if (!ptr->data)
+		return -EINVAL;
+
+	size = bpf_dynptr_get_size(ptr);
+
+	if (sz_dec > size)
+		return -ERANGE;
+
+	if (off_inc) {
+		u32 new_off;
+
+		if (off_inc > size)
+			return -ERANGE;
+
+		if (check_add_overflow(ptr->offset, off_inc, &new_off))
+			return -ERANGE;
+
+		ptr->offset = new_off;
+	}
+
+	bpf_dynptr_set_size(ptr, size - sz_dec);
+
+	return 0;
+}
+
+__bpf_kfunc int bpf_dynptr_advance(struct bpf_dynptr_kern *ptr, u32 len)
+{
+	return bpf_dynptr_adjust(ptr, len, len);
+}
+
+__bpf_kfunc int bpf_dynptr_trim(struct bpf_dynptr_kern *ptr, u32 len)
+{
+	return bpf_dynptr_adjust(ptr, 0, len);
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -2347,6 +2394,8 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_trim)
+BTF_ID_FLAGS(func, bpf_dynptr_advance)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v1 bpf-next 1/5] bpf: Add bpf_dynptr_trim and bpf_dynptr_advance

Quoted 36 lines · Joanne Koong

bpf_dynptr_trim decreases the size of a dynptr by the specified number of bytes (offset remains the same). bpf_dynptr_advance advances the offset of the dynptr by the specified number of bytes (size decreases correspondingly).

Trimming or advancing the dynptr may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, advance/trim can be used to narrow in on the specific region to hash.

---
 kernel/bpf/helpers.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
kernel/bpf/helpers.c +7 -0
index b6a5cda5bb59..51b4c4b5dbed 100644
@@ -1448,6 +1448,13 @@ u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
        return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+       u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+       ptr->size = new_size | metadata;
+}
+
 int bpf_dynptr_check_size(u32 size)
 {
        return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2286

Quoted 5 lines
kernel/bpf/helpers.c +2 -0
@@ -2275,6 +2282,46 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
        return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
 }
 
+/* For dynptrs, the offset may only be advanced and the size may only be decremented */
+static int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 off_inc, u32 sz_dec)

it feels like this helper just makes it a bit harder to follow what's going on. Half of this function isn't actually executed for bpf_dynptr_trim, so I don't think we are saving all that much code, maybe let's code each of advance and trim explicitly?

Quoted 5 lines
kernel/bpf/helpers.c +5 -0
+{
+       u32 size;
+
+       if (!ptr->data)
+               return -EINVAL;

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2301

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+
+       size = bpf_dynptr_get_size(ptr);
+
+       if (sz_dec > size)
+               return -ERANGE;
+
+       if (off_inc) {
+               u32 new_off;
+
+               if (off_inc > size)

like here it becomes confusing if off_inc includes sz_dec, or they should be added to each other. I think it's convoluted as is.

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2304

Quoted 3 lines
kernel/bpf/helpers.c +3 -0
+                       return -ERANGE;
+
+               if (check_add_overflow(ptr->offset, off_inc, &new_off))

why do we need to worry about overflow, we checked all the error conditions above?..

Quoted 6 lines
kernel/bpf/helpers.c +6 -0
+                       return -ERANGE;
+
+               ptr->offset = new_off;
+       }
+
+       bpf_dynptr_set_size(ptr, size - sz_dec);

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2320

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+
+       return 0;
+}
+
+__bpf_kfunc int bpf_dynptr_advance(struct bpf_dynptr_kern *ptr, u32 len)
+{
+       return bpf_dynptr_adjust(ptr, len, len);
+}
+
+__bpf_kfunc int bpf_dynptr_trim(struct bpf_dynptr_kern *ptr, u32 len)

I'm also wondering if trim operation is a bit unusual for dealing ranges? Instead of a relative size decrement, maybe it's more straightforward to have bpf_dynptr_resize() to set new desired size? So if someone has original dynptr with 100 bytes but wants to have dynptr for bytes [10, 30), they'd do a pretty natural:

bpf_dynptr_advance(&dynptr, 10);
bpf_dynptr_resize(&dynptr, 20);

?

Quoted 19 lines
kernel/bpf/helpers.c +6 -1
+{
+       return bpf_dynptr_adjust(ptr, 0, len);
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
        return obj;
@@ -2347,6 +2394,8 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_trim)
+BTF_ID_FLAGS(func, bpf_dynptr_advance)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
--

2.34.1

#3 lore raw

Re: [PATCH v1 bpf-next 1/5] bpf: Add bpf_dynptr_trim and bpf_dynptr_advance

Quoted 41 lines · Andrii Nakryiko
Quoted 39 lines · Joanne Koong

bpf_dynptr_trim decreases the size of a dynptr by the specified number of bytes (offset remains the same). bpf_dynptr_advance advances the offset of the dynptr by the specified number of bytes (size decreases correspondingly).

Trimming or advancing the dynptr may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, advance/trim can be used to narrow in on the specific region to hash.

---
 kernel/bpf/helpers.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
kernel/bpf/helpers.c +7 -0
index b6a5cda5bb59..51b4c4b5dbed 100644
@@ -1448,6 +1448,13 @@ u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
        return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+       u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+       ptr->size = new_size | metadata;
+}
+
 int bpf_dynptr_check_size(u32 size)
 {
        return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -2275,6 +2282,46 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
        return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
 }
 
Quoted 8 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2286

Quoted 2 lines
kernel/bpf/helpers.c +2 -0
+/* For dynptrs, the offset may only be advanced and the size may only be decremented */
+static int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 off_inc, u32 sz_dec)

it feels like this helper just makes it a bit harder to follow what's going on. Half of this function isn't actually executed for bpf_dynptr_trim, so I don't think we are saving all that much code, maybe let's code each of advance and trim explicitly?

Sounds good, I will change this in v2 to handle advance and trim separately

Quoted 20 lines
Quoted 5 lines
kernel/bpf/helpers.c +5 -0
+{
+       u32 size;
+
+       if (!ptr->data)
+               return -EINVAL;

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2301

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+
+       size = bpf_dynptr_get_size(ptr);
+
+       if (sz_dec > size)
+               return -ERANGE;
+
+       if (off_inc) {
+               u32 new_off;
+
+               if (off_inc > size)

like here it becomes confusing if off_inc includes sz_dec, or they should be added to each other. I think it's convoluted as is.

Quoted 6 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2304

Quoted 3 lines
kernel/bpf/helpers.c +3 -0
+                       return -ERANGE;
+
+               if (check_add_overflow(ptr->offset, off_inc, &new_off))

why do we need to worry about overflow, we checked all the error conditions above?..

Ahh you're right, this cant overflow u32. The dynptr max supported size is 2^24 - 1 as well

Quoted 24 lines
Quoted 7 lines
kernel/bpf/helpers.c +6 -0
+                       return -ERANGE;
+
+               ptr->offset = new_off;
+       }
+
+       bpf_dynptr_set_size(ptr, size - sz_dec);

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2320

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+
+       return 0;
+}
+
+__bpf_kfunc int bpf_dynptr_advance(struct bpf_dynptr_kern *ptr, u32 len)
+{
+       return bpf_dynptr_adjust(ptr, len, len);
+}
+
+__bpf_kfunc int bpf_dynptr_trim(struct bpf_dynptr_kern *ptr, u32 len)

I'm also wondering if trim operation is a bit unusual for dealing ranges? Instead of a relative size decrement, maybe it's more straightforward to have bpf_dynptr_resize() to set new desired size? So if someone has original dynptr with 100 bytes but wants to have dynptr for bytes [10, 30), they'd do a pretty natural:

Quoted 5 lines

bpf_dynptr_advance(&dynptr, 10);
bpf_dynptr_resize(&dynptr, 20);

?

Yeah! I like this idea a lot, that way they dont' need to know the current size of the dynptr before they trim. This seems a lot more ergonomic

Quoted 19 lines
Quoted 19 lines
kernel/bpf/helpers.c +6 -1
+{
+       return bpf_dynptr_adjust(ptr, 0, len);
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
        return obj;
@@ -2347,6 +2394,8 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_trim)
+BTF_ID_FLAGS(func, bpf_dynptr_advance)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
--

2.34.1

#4 lore raw

Re: [PATCH v1 bpf-next 1/5] bpf: Add bpf_dynptr_trim and bpf_dynptr_advance

Quoted 112 lines · Joanne Koong
Quoted 41 lines · Andrii Nakryiko
Quoted 39 lines · Joanne Koong

bpf_dynptr_trim decreases the size of a dynptr by the specified number of bytes (offset remains the same). bpf_dynptr_advance advances the offset of the dynptr by the specified number of bytes (size decreases correspondingly).

Trimming or advancing the dynptr may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, advance/trim can be used to narrow in on the specific region to hash.

---
 kernel/bpf/helpers.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
kernel/bpf/helpers.c +7 -0
index b6a5cda5bb59..51b4c4b5dbed 100644
@@ -1448,6 +1448,13 @@ u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
        return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+       u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+       ptr->size = new_size | metadata;
+}
+
 int bpf_dynptr_check_size(u32 size)
 {
        return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -2275,6 +2282,46 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
        return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
 }
 
Quoted 8 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2286

Quoted 2 lines
kernel/bpf/helpers.c +2 -0
+/* For dynptrs, the offset may only be advanced and the size may only be decremented */
+static int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 off_inc, u32 sz_dec)

it feels like this helper just makes it a bit harder to follow what's going on. Half of this function isn't actually executed for bpf_dynptr_trim, so I don't think we are saving all that much code, maybe let's code each of advance and trim explicitly?

Sounds good, I will change this in v2 to handle advance and trim separately

Quoted 20 lines
Quoted 5 lines
kernel/bpf/helpers.c +5 -0
+{
+       u32 size;
+
+       if (!ptr->data)
+               return -EINVAL;

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2301

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+
+       size = bpf_dynptr_get_size(ptr);
+
+       if (sz_dec > size)
+               return -ERANGE;
+
+       if (off_inc) {
+               u32 new_off;
+
+               if (off_inc > size)

like here it becomes confusing if off_inc includes sz_dec, or they should be added to each other. I think it's convoluted as is.

Quoted 6 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2304

Quoted 3 lines
kernel/bpf/helpers.c +3 -0
+                       return -ERANGE;
+
+               if (check_add_overflow(ptr->offset, off_inc, &new_off))

why do we need to worry about overflow, we checked all the error conditions above?..

Ahh you're right, this cant overflow u32. The dynptr max supported size is 2^24 - 1 as well

Quoted 27 lines
Quoted 7 lines
kernel/bpf/helpers.c +6 -0
+                       return -ERANGE;
+
+               ptr->offset = new_off;
+       }
+
+       bpf_dynptr_set_size(ptr, size - sz_dec);

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2320

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+
+       return 0;
+}
+
+__bpf_kfunc int bpf_dynptr_advance(struct bpf_dynptr_kern *ptr, u32 len)
+{
+       return bpf_dynptr_adjust(ptr, len, len);
+}
+
+__bpf_kfunc int bpf_dynptr_trim(struct bpf_dynptr_kern *ptr, u32 len)

I'm also wondering if trim operation is a bit unusual for dealing ranges? Instead of a relative size decrement, maybe it's more straightforward to have bpf_dynptr_resize() to set new desired size? So if someone has original dynptr with 100 bytes but wants to have dynptr for bytes [10, 30), they'd do a pretty natural:

bpf_dynptr_advance(&dynptr, 10);
bpf_dynptr_resize(&dynptr, 20);

Quoted 6 lines
Quoted 2 lines

?

Yeah! I like this idea a lot, that way they dont' need to know the current size of the dynptr before they trim. This seems a lot more ergonomic

Thinking a bit more, I'm now wondering if we should actually merge those two into one API to allow adjust both at the same time. Similarly how langauges like Go and Rust allow to adjust array slices by specifying new [start, end) offsets, should we have just one:

bpf_dynptr_adjust(&dynptr, 10, 30);

bpf_dynptr_advance() could be expressed as:

bpf_dynptr_adjust(&dynptr, 10, bpf_dynptr_size(&dynptr) - 10);

I suspect full adjust with custom [start, end) will be actually more common than just advancing offset.

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
kernel/bpf/helpers.c +6 -1
+{
+       return bpf_dynptr_adjust(ptr, 0, len);
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
        return obj;
@@ -2347,6 +2394,8 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_trim)
+BTF_ID_FLAGS(func, bpf_dynptr_advance)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
--

2.34.1

#5 lore raw

Re: [PATCH v1 bpf-next 1/5] bpf: Add bpf_dynptr_trim and bpf_dynptr_advance

Quoted 126 lines · Andrii Nakryiko
Quoted 112 lines · Joanne Koong
Quoted 41 lines · Andrii Nakryiko
Quoted 39 lines · Joanne Koong

bpf_dynptr_trim decreases the size of a dynptr by the specified number of bytes (offset remains the same). bpf_dynptr_advance advances the offset of the dynptr by the specified number of bytes (size decreases correspondingly).

Trimming or advancing the dynptr may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, advance/trim can be used to narrow in on the specific region to hash.

---
 kernel/bpf/helpers.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
kernel/bpf/helpers.c +7 -0
index b6a5cda5bb59..51b4c4b5dbed 100644
@@ -1448,6 +1448,13 @@ u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
        return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+       u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+       ptr->size = new_size | metadata;
+}
+
 int bpf_dynptr_check_size(u32 size)
 {
        return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -2275,6 +2282,46 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
        return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
 }
 
Quoted 8 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2286

Quoted 2 lines
kernel/bpf/helpers.c +2 -0
+/* For dynptrs, the offset may only be advanced and the size may only be decremented */
+static int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 off_inc, u32 sz_dec)

it feels like this helper just makes it a bit harder to follow what's going on. Half of this function isn't actually executed for bpf_dynptr_trim, so I don't think we are saving all that much code, maybe let's code each of advance and trim explicitly?

Sounds good, I will change this in v2 to handle advance and trim separately

Quoted 20 lines
Quoted 5 lines
kernel/bpf/helpers.c +5 -0
+{
+       u32 size;
+
+       if (!ptr->data)
+               return -EINVAL;

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2301

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+
+       size = bpf_dynptr_get_size(ptr);
+
+       if (sz_dec > size)
+               return -ERANGE;
+
+       if (off_inc) {
+               u32 new_off;
+
+               if (off_inc > size)

like here it becomes confusing if off_inc includes sz_dec, or they should be added to each other. I think it's convoluted as is.

Quoted 6 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2304

Quoted 3 lines
kernel/bpf/helpers.c +3 -0
+                       return -ERANGE;
+
+               if (check_add_overflow(ptr->offset, off_inc, &new_off))

why do we need to worry about overflow, we checked all the error conditions above?..

Ahh you're right, this cant overflow u32. The dynptr max supported size is 2^24 - 1 as well

Quoted 27 lines
Quoted 7 lines
kernel/bpf/helpers.c +6 -0
+                       return -ERANGE;
+
+               ptr->offset = new_off;
+       }
+
+       bpf_dynptr_set_size(ptr, size - sz_dec);

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2320

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+
+       return 0;
+}
+
+__bpf_kfunc int bpf_dynptr_advance(struct bpf_dynptr_kern *ptr, u32 len)
+{
+       return bpf_dynptr_adjust(ptr, len, len);
+}
+
+__bpf_kfunc int bpf_dynptr_trim(struct bpf_dynptr_kern *ptr, u32 len)

I'm also wondering if trim operation is a bit unusual for dealing ranges? Instead of a relative size decrement, maybe it's more straightforward to have bpf_dynptr_resize() to set new desired size? So if someone has original dynptr with 100 bytes but wants to have dynptr for bytes [10, 30), they'd do a pretty natural:

bpf_dynptr_advance(&dynptr, 10);
bpf_dynptr_resize(&dynptr, 20);

Quoted 6 lines
Quoted 2 lines

?

Yeah! I like this idea a lot, that way they dont' need to know the current size of the dynptr before they trim. This seems a lot more ergonomic

Thinking a bit more, I'm now wondering if we should actually merge those two into one API to allow adjust both at the same time. Similarly how langauges like Go and Rust allow to adjust array slices by specifying new [start, end) offsets, should we have just one:

Quoted 6 lines

bpf_dynptr_adjust(&dynptr, 10, 30);

bpf_dynptr_advance() could be expressed as:

bpf_dynptr_adjust(&dynptr, 10, bpf_dynptr_size(&dynptr) - 10);

I think for expressing advance where only start offset changes, end needs to be "bpf_dynptr_size(&dynptr)" (no minus 10) here?

Quoted 3 lines

I suspect full adjust with custom [start, end) will be actually more common than just advancing offset.

I think this might get quickly cumbersome for the use cases where the user just wants to parse through the data with only adjusting start offset, for example parsing an skb's header options. maybe there's some way to combine the two?:

bpf_dynptr_adjust(&dynptr, start, end);
where if end is -1 or some #define macro set to u32_max or something like that then that signifies dont' modify the end offset, just modify the start? That way the user can just advance instead of needing to know its size every time. I don't know if that makes the interface uglier / more confusing though. WDYT?

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
kernel/bpf/helpers.c +6 -1
+{
+       return bpf_dynptr_adjust(ptr, 0, len);
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
        return obj;
@@ -2347,6 +2394,8 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_trim)
+BTF_ID_FLAGS(func, bpf_dynptr_advance)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
--

2.34.1

#6 lore raw

Re: [PATCH v1 bpf-next 1/5] bpf: Add bpf_dynptr_trim and bpf_dynptr_advance

Quoted 129 lines · Joanne Koong
Quoted 126 lines · Andrii Nakryiko
Quoted 112 lines · Joanne Koong
Quoted 41 lines · Andrii Nakryiko
Quoted 39 lines · Joanne Koong

bpf_dynptr_trim decreases the size of a dynptr by the specified number of bytes (offset remains the same). bpf_dynptr_advance advances the offset of the dynptr by the specified number of bytes (size decreases correspondingly).

Trimming or advancing the dynptr may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, advance/trim can be used to narrow in on the specific region to hash.

---
 kernel/bpf/helpers.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
kernel/bpf/helpers.c +7 -0
index b6a5cda5bb59..51b4c4b5dbed 100644
@@ -1448,6 +1448,13 @@ u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
        return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+       u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+       ptr->size = new_size | metadata;
+}
+
 int bpf_dynptr_check_size(u32 size)
 {
        return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -2275,6 +2282,46 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
        return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
 }
 
Quoted 8 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2286

Quoted 2 lines
kernel/bpf/helpers.c +2 -0
+/* For dynptrs, the offset may only be advanced and the size may only be decremented */
+static int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 off_inc, u32 sz_dec)

it feels like this helper just makes it a bit harder to follow what's going on. Half of this function isn't actually executed for bpf_dynptr_trim, so I don't think we are saving all that much code, maybe let's code each of advance and trim explicitly?

Sounds good, I will change this in v2 to handle advance and trim separately

Quoted 20 lines
Quoted 5 lines
kernel/bpf/helpers.c +5 -0
+{
+       u32 size;
+
+       if (!ptr->data)
+               return -EINVAL;

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2301

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+
+       size = bpf_dynptr_get_size(ptr);
+
+       if (sz_dec > size)
+               return -ERANGE;
+
+       if (off_inc) {
+               u32 new_off;
+
+               if (off_inc > size)

like here it becomes confusing if off_inc includes sz_dec, or they should be added to each other. I think it's convoluted as is.

Quoted 6 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2304

Quoted 3 lines
kernel/bpf/helpers.c +3 -0
+                       return -ERANGE;
+
+               if (check_add_overflow(ptr->offset, off_inc, &new_off))

why do we need to worry about overflow, we checked all the error conditions above?..

Ahh you're right, this cant overflow u32. The dynptr max supported size is 2^24 - 1 as well

Quoted 27 lines
Quoted 7 lines
kernel/bpf/helpers.c +6 -0
+                       return -ERANGE;
+
+               ptr->offset = new_off;
+       }
+
+       bpf_dynptr_set_size(ptr, size - sz_dec);

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2320

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+
+       return 0;
+}
+
+__bpf_kfunc int bpf_dynptr_advance(struct bpf_dynptr_kern *ptr, u32 len)
+{
+       return bpf_dynptr_adjust(ptr, len, len);
+}
+
+__bpf_kfunc int bpf_dynptr_trim(struct bpf_dynptr_kern *ptr, u32 len)

I'm also wondering if trim operation is a bit unusual for dealing ranges? Instead of a relative size decrement, maybe it's more straightforward to have bpf_dynptr_resize() to set new desired size? So if someone has original dynptr with 100 bytes but wants to have dynptr for bytes [10, 30), they'd do a pretty natural:

bpf_dynptr_advance(&dynptr, 10);
bpf_dynptr_resize(&dynptr, 20);

Quoted 6 lines
Quoted 2 lines

?

Yeah! I like this idea a lot, that way they dont' need to know the current size of the dynptr before they trim. This seems a lot more ergonomic

Thinking a bit more, I'm now wondering if we should actually merge those two into one API to allow adjust both at the same time. Similarly how langauges like Go and Rust allow to adjust array slices by specifying new [start, end) offsets, should we have just one:

Quoted 8 lines
Quoted 6 lines

bpf_dynptr_adjust(&dynptr, 10, 30);

bpf_dynptr_advance() could be expressed as:

bpf_dynptr_adjust(&dynptr, 10, bpf_dynptr_size(&dynptr) - 10);

I think for expressing advance where only start offset changes, end needs to be "bpf_dynptr_size(&dynptr)" (no minus 10) here?

yep, you are right! it's end offset, so no need to adjust for 10. So even better:

bpf_dynptr_adjust(&dynptr, 10, bpf_dynptr_size(&dynptr));

Quoted 10 lines
Quoted 4 lines

I suspect full adjust with custom [start, end) will be actually more common than just advancing offset.

I think this might get quickly cumbersome for the use cases where the user just wants to parse through the data with only adjusting start offset, for example parsing an skb's header options. maybe there's some way to combine the two?:

Quoted 6 lines

bpf_dynptr_adjust(&dynptr, start, end);
where if end is -1 or some #define macro set to u32_max or something like that then that signifies dont' modify the end offset, just modify the start? That way the user can just advance instead of needing to know its size every time. I don't know if that makes the interface uglier / more confusing though. WDYT?

I think it does make it more cumbersome, I'd keep it as [start, end) offset always. We can inline bpf_dynptr_size() if there is a performance concern.

At least I'd start there, and if there is demand we can also add -1 as a special case later.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
kernel/bpf/helpers.c +6 -1
+{
+       return bpf_dynptr_adjust(ptr, 0, len);
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
        return obj;
@@ -2347,6 +2394,8 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_trim)
+BTF_ID_FLAGS(func, bpf_dynptr_advance)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
--

2.34.1

#7 lore raw

PATCH 2/5 bpf: Add bpf_dynptr_is_null and bpf_dynptr_is_rdonly

In reply to Joanne Koong

bpf_dynptr_is_null returns true if the dynptr is null / invalid (determined by whether ptr->data is NULL), else false if the dynptr is a valid dynptr.

bpf_dynptr_is_rdonly returns true if the dynptr is read-only, else false if the dynptr is read-writable.

---
 kernel/bpf/helpers.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
kernel/bpf/helpers.c +19 -4
index 51b4c4b5dbed..e4e84e92a4c6 100644
@@ -1423,7 +1423,7 @@ static const struct bpf_func_proto bpf_kptr_xchg_proto = {
 #define DYNPTR_SIZE_MASK	0xFFFFFF
 #define DYNPTR_RDONLY_BIT	BIT(31)
 
-static bool bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr)
+static bool __bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr)
 {
 	return ptr->size & DYNPTR_RDONLY_BIT;
 }
@@ -1570,7 +1570,7 @@ BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u32, offset, v
 	enum bpf_dynptr_type type;
 	int err;
 
-	if (!dst->data || bpf_dynptr_is_rdonly(dst))
+	if (!dst->data || __bpf_dynptr_is_rdonly(dst))
 		return -EINVAL;
 
 	err = bpf_dynptr_check_off_len(dst, offset, len);
@@ -1626,7 +1626,7 @@ BPF_CALL_3(bpf_dynptr_data, const struct bpf_dynptr_kern *, ptr, u32, offset, u3
 	if (err)
 		return 0;
 
-	if (bpf_dynptr_is_rdonly(ptr))
+	if (__bpf_dynptr_is_rdonly(ptr))
 		return 0;
 
 	type = bpf_dynptr_get_type(ptr);
@@ -2254,7 +2254,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
 __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset,
 					void *buffer, u32 buffer__szk)
 {
-	if (!ptr->data || bpf_dynptr_is_rdonly(ptr))
+	if (!ptr->data || __bpf_dynptr_is_rdonly(ptr))
 		return NULL;
 
 	/* bpf_dynptr_slice_rdwr is the same logic as bpf_dynptr_slice.
@@ -2322,6 +2322,19 @@ __bpf_kfunc int bpf_dynptr_trim(struct bpf_dynptr_kern *ptr, u32 len)
 	return bpf_dynptr_adjust(ptr, 0, len);
 }
 
+__bpf_kfunc bool bpf_dynptr_is_null(struct bpf_dynptr_kern *ptr)
+{
+	return !ptr->data;
+}
+
+__bpf_kfunc bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
+{
+	if (!ptr->data)
+		return false;
+
+	return __bpf_dynptr_is_rdonly(ptr);
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -2396,6 +2409,8 @@ BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
 BTF_ID_FLAGS(func, bpf_dynptr_trim)
 BTF_ID_FLAGS(func, bpf_dynptr_advance)
+BTF_ID_FLAGS(func, bpf_dynptr_is_null)
+BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
Signature · 1 line
2.34.1
Acked-by on v1 2/5
#8 lore raw

Re: [PATCH v1 bpf-next 2/5] bpf: Add bpf_dynptr_is_null and bpf_dynptr_is_rdonly

Quoted 45 lines · Joanne Koong

bpf_dynptr_is_null returns true if the dynptr is null / invalid (determined by whether ptr->data is NULL), else false if the dynptr is a valid dynptr.

bpf_dynptr_is_rdonly returns true if the dynptr is read-only, else false if the dynptr is read-writable.

---
 kernel/bpf/helpers.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
kernel/bpf/helpers.c +3 -3
index 51b4c4b5dbed..e4e84e92a4c6 100644
@@ -1423,7 +1423,7 @@ static const struct bpf_func_proto bpf_kptr_xchg_proto = {
 #define DYNPTR_SIZE_MASK       0xFFFFFF
 #define DYNPTR_RDONLY_BIT      BIT(31)
 
-static bool bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr)
+static bool __bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr)
 {
        return ptr->size & DYNPTR_RDONLY_BIT;
 }
@@ -1570,7 +1570,7 @@ BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u32, offset, v
        enum bpf_dynptr_type type;
        int err;
 
-       if (!dst->data || bpf_dynptr_is_rdonly(dst))
+       if (!dst->data || __bpf_dynptr_is_rdonly(dst))
                return -EINVAL;
 
        err = bpf_dynptr_check_off_len(dst, offset, len);
@@ -1626,7 +1626,7 @@ BPF_CALL_3(bpf_dynptr_data, const struct bpf_dynptr_kern *, ptr, u32, offset, u3
        if (err)
                return 0;
 
-       if (bpf_dynptr_is_rdonly(ptr))
+       if (__bpf_dynptr_is_rdonly(ptr))
                return 0;
 
        type = bpf_dynptr_get_type(ptr);

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset · line 2257

Quoted 5 lines
kernel/bpf/helpers.c +1 -1
@@ -2254,7 +2254,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
 __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset,
                                        void *buffer, u32 buffer__szk)
 {
-       if (!ptr->data || bpf_dynptr_is_rdonly(ptr))
+       if (!ptr->data || __bpf_dynptr_is_rdonly(ptr))

seems like all the uses of __bpf_dynptr_is_rdonly check !ptr->data explicitly, so maybe move that ptr->data check inside and simplify all the callers?

Regardless, looks good:

Quoted 35 lines
kernel/bpf/helpers.c +15 -1
                return NULL;
 
        /* bpf_dynptr_slice_rdwr is the same logic as bpf_dynptr_slice.
@@ -2322,6 +2322,19 @@ __bpf_kfunc int bpf_dynptr_trim(struct bpf_dynptr_kern *ptr, u32 len)
        return bpf_dynptr_adjust(ptr, 0, len);
 }
 
+__bpf_kfunc bool bpf_dynptr_is_null(struct bpf_dynptr_kern *ptr)
+{
+       return !ptr->data;
+}
+
+__bpf_kfunc bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
+{
+       if (!ptr->data)
+               return false;
+
+       return __bpf_dynptr_is_rdonly(ptr);
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
        return obj;
@@ -2396,6 +2409,8 @@ BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
 BTF_ID_FLAGS(func, bpf_dynptr_trim)
 BTF_ID_FLAGS(func, bpf_dynptr_advance)
+BTF_ID_FLAGS(func, bpf_dynptr_is_null)
+BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
--

2.34.1

#9 lore raw

Re: [PATCH v1 bpf-next 2/5] bpf: Add bpf_dynptr_is_null and bpf_dynptr_is_rdonly

Quoted 45 lines · Andrii Nakryiko
Quoted 43 lines · Joanne Koong

bpf_dynptr_is_null returns true if the dynptr is null / invalid (determined by whether ptr->data is NULL), else false if the dynptr is a valid dynptr.

bpf_dynptr_is_rdonly returns true if the dynptr is read-only, else false if the dynptr is read-writable.

---
 kernel/bpf/helpers.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
kernel/bpf/helpers.c +3 -3
index 51b4c4b5dbed..e4e84e92a4c6 100644
@@ -1423,7 +1423,7 @@ static const struct bpf_func_proto bpf_kptr_xchg_proto = {
 #define DYNPTR_SIZE_MASK       0xFFFFFF
 #define DYNPTR_RDONLY_BIT      BIT(31)
 
-static bool bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr)
+static bool __bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr)
 {
        return ptr->size & DYNPTR_RDONLY_BIT;
 }
@@ -1570,7 +1570,7 @@ BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u32, offset, v
        enum bpf_dynptr_type type;
        int err;
 
-       if (!dst->data || bpf_dynptr_is_rdonly(dst))
+       if (!dst->data || __bpf_dynptr_is_rdonly(dst))
                return -EINVAL;
 
        err = bpf_dynptr_check_off_len(dst, offset, len);
@@ -1626,7 +1626,7 @@ BPF_CALL_3(bpf_dynptr_data, const struct bpf_dynptr_kern *, ptr, u32, offset, u3
        if (err)
                return 0;
 
-       if (bpf_dynptr_is_rdonly(ptr))
+       if (__bpf_dynptr_is_rdonly(ptr))
                return 0;
 
Quoted 11 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset · line 2257

Quoted 7 lines
kernel/bpf/helpers.c +1 -1
        type = bpf_dynptr_get_type(ptr);
@@ -2254,7 +2254,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
 __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset,
                                        void *buffer, u32 buffer__szk)
 {
-       if (!ptr->data || bpf_dynptr_is_rdonly(ptr))
+       if (!ptr->data || __bpf_dynptr_is_rdonly(ptr))

seems like all the uses of __bpf_dynptr_is_rdonly check !ptr->data explicitly, so maybe move that ptr->data check inside and simplify all the callers?

i think combining it gets confusing in the case where ptr->data is null, as to how the invoked places interpret the return value. I think having the check spelled out more explicitly in the invoked places (eg bpf_dynptr_write, bpf_dynptr_data, ...) makes it more clear as well where the check for null is happening. for v2 I will leave this as is, but also happy to change it if you prefer the two be combined

Quoted 40 lines

Regardless, looks good:

Quoted 35 lines
kernel/bpf/helpers.c +15 -1
                return NULL;
 
        /* bpf_dynptr_slice_rdwr is the same logic as bpf_dynptr_slice.
@@ -2322,6 +2322,19 @@ __bpf_kfunc int bpf_dynptr_trim(struct bpf_dynptr_kern *ptr, u32 len)
        return bpf_dynptr_adjust(ptr, 0, len);
 }
 
+__bpf_kfunc bool bpf_dynptr_is_null(struct bpf_dynptr_kern *ptr)
+{
+       return !ptr->data;
+}
+
+__bpf_kfunc bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
+{
+       if (!ptr->data)
+               return false;
+
+       return __bpf_dynptr_is_rdonly(ptr);
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
        return obj;
@@ -2396,6 +2409,8 @@ BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
 BTF_ID_FLAGS(func, bpf_dynptr_trim)
 BTF_ID_FLAGS(func, bpf_dynptr_advance)
+BTF_ID_FLAGS(func, bpf_dynptr_is_null)
+BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
--

2.34.1

#10 lore raw

PATCH 3/5 bpf: Add bpf_dynptr_get_size and bpf_dynptr_get_offset

In reply to Joanne Koong

bpf_dynptr_get_size returns the number of useable bytes in a dynptr and bpf_dynptr_get_offset returns the current offset into the dynptr.

---
 include/linux/bpf.h      |  2 +-
 kernel/bpf/helpers.c     | 24 +++++++++++++++++++++---
 kernel/trace/bpf_trace.c |  4 ++--
 3 files changed, 24 insertions(+), 6 deletions(-)
include/linux/bpf.h +1 -1
index 002a811b6b90..2a73ddd06e55 100644
@@ -1167,7 +1167,7 @@ enum bpf_dynptr_type {
 };
 
 int bpf_dynptr_check_size(u32 size);
-u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr);
+u32 __bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr);
 
 #ifdef CONFIG_BPF_JIT
 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
kernel/bpf/helpers.c +21 -3
index e4e84e92a4c6..bac4c6fe49f0 100644
@@ -1443,7 +1443,7 @@ static enum bpf_dynptr_type bpf_dynptr_get_type(const struct bpf_dynptr_kern *pt
 	return (ptr->size & ~(DYNPTR_RDONLY_BIT)) >> DYNPTR_TYPE_SHIFT;
 }
 
-u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
+u32 __bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
 {
 	return ptr->size & DYNPTR_SIZE_MASK;
 }
@@ -1476,7 +1476,7 @@ void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)
 
 static int bpf_dynptr_check_off_len(const struct bpf_dynptr_kern *ptr, u32 offset, u32 len)
 {
-	u32 size = bpf_dynptr_get_size(ptr);
+	u32 size = __bpf_dynptr_get_size(ptr);
 
 	if (len > size || offset > size - len)
 		return -E2BIG;
@@ -2290,7 +2290,7 @@ static int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 off_inc, u32 sz_de
 	if (!ptr->data)
 		return -EINVAL;
 
-	size = bpf_dynptr_get_size(ptr);
+	size = __bpf_dynptr_get_size(ptr);
 
 	if (sz_dec > size)
 		return -ERANGE;
@@ -2335,6 +2335,22 @@ __bpf_kfunc bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
 	return __bpf_dynptr_is_rdonly(ptr);
 }
 
+__bpf_kfunc __u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
+{
+	if (!ptr->data)
+		return -EINVAL;
+
+	return __bpf_dynptr_get_size(ptr);
+}
+
+__bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr)
+{
+	if (!ptr->data)
+		return -EINVAL;
+
+	return ptr->offset;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -2411,6 +2427,8 @@ BTF_ID_FLAGS(func, bpf_dynptr_trim)
 BTF_ID_FLAGS(func, bpf_dynptr_advance)
 BTF_ID_FLAGS(func, bpf_dynptr_is_null)
 BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
+BTF_ID_FLAGS(func, bpf_dynptr_get_size)
+BTF_ID_FLAGS(func, bpf_dynptr_get_offset)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/trace/bpf_trace.c +2 -2
index bcf91bc7bf71..f30bdc72d26c 100644
@@ -1349,9 +1349,9 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
 	}
 
 	return verify_pkcs7_signature(data_ptr->data,
-				      bpf_dynptr_get_size(data_ptr),
+				      __bpf_dynptr_get_size(data_ptr),
 				      sig_ptr->data,
-				      bpf_dynptr_get_size(sig_ptr),
+				      __bpf_dynptr_get_size(sig_ptr),
 				      trusted_keyring->key,
 				      VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
 				      NULL);
Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH v1 bpf-next 3/5] bpf: Add bpf_dynptr_get_size and bpf_dynptr_get_offset

Quoted 11 lines · Joanne Koong

bpf_dynptr_get_size returns the number of useable bytes in a dynptr and bpf_dynptr_get_offset returns the current offset into the dynptr.

---
 include/linux/bpf.h      |  2 +-
 kernel/bpf/helpers.c     | 24 +++++++++++++++++++++---
 kernel/trace/bpf_trace.c |  4 ++--
 3 files changed, 24 insertions(+), 6 deletions(-)

[...]

kernel/bpf/helpers.c __bpf_kfunc bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr) · line 2346

Quoted 9 lines
kernel/bpf/helpers.c +9 -0
+__bpf_kfunc __u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
+{
+       if (!ptr->data)
+               return -EINVAL;
+
+       return __bpf_dynptr_get_size(ptr);
+}
+
+__bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr)

I think get_offset is actually not essential and it's hard to think about the case where this is going to be really necessary. Let's keep only get_size for now?

Quoted 7 lines
kernel/bpf/helpers.c +7 -0
+{
+       if (!ptr->data)
+               return -EINVAL;
+
+       return ptr->offset;
+}
+

[...]

#12 lore raw

Re: [PATCH v1 bpf-next 3/5] bpf: Add bpf_dynptr_get_size and bpf_dynptr_get_offset

Quoted 16 lines · Andrii Nakryiko
Quoted 11 lines · Joanne Koong

bpf_dynptr_get_size returns the number of useable bytes in a dynptr and bpf_dynptr_get_offset returns the current offset into the dynptr.

---
 include/linux/bpf.h      |  2 +-
 kernel/bpf/helpers.c     | 24 +++++++++++++++++++++---
 kernel/trace/bpf_trace.c |  4 ++--
 3 files changed, 24 insertions(+), 6 deletions(-)

[...]

Quoted 14 lines

kernel/bpf/helpers.c __bpf_kfunc bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr) · line 2346

Quoted 9 lines
kernel/bpf/helpers.c +9 -0
+__bpf_kfunc __u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
+{
+       if (!ptr->data)
+               return -EINVAL;
+
+       return __bpf_dynptr_get_size(ptr);
+}
+
+__bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr)

I think get_offset is actually not essential and it's hard to think about the case where this is going to be really necessary. Let's keep only get_size for now?

Sounds good, I will remove this from v2 (i'll try to send v2 out next week). Thanks for reviewing this patchset!

Quoted 10 lines
Quoted 8 lines
kernel/bpf/helpers.c +7 -0
+{
+       if (!ptr->data)
+               return -EINVAL;
+
+       return ptr->offset;
+}
+

[...]

#13 lore raw

PATCH 4/5 bpf: Add bpf_dynptr_clone

In reply to Joanne Koong

The cloned dynptr will point to the same data as its parent dynptr, with the same type, offset, size and read-only properties.

Any writes to a dynptr will be reflected across all instances (by 'instance', this means any dynptrs that point to the same underlying data).

Please note that data slice and dynptr invalidations will affect all instances as well. For example, if bpf_dynptr_write() is called on an skb-type dynptr, all data slices of dynptr instances to that skb will be invalidated as well (eg data slices of any clones, parents, grandparents, ...). Another example is if a ringbuf dynptr is submitted, any instance of that dynptr will be invalidated.

Changing the view of the dynptr (eg advancing the offset or trimming the size) will only affect that dynptr and not affect any other instances.

One example use case where cloning may be helpful is for hashing or iterating through dynptr data. Cloning will allow the user to maintain the original view of the dynptr for future use, while also allowing views to smaller subsets of the data after the offset is advanced or the size is trimmed.

---
 kernel/bpf/helpers.c  |  14 +++++
 kernel/bpf/verifier.c | 125 +++++++++++++++++++++++++++++++++++++-----
 2 files changed, 126 insertions(+), 13 deletions(-)
kernel/bpf/helpers.c +14 -0
index bac4c6fe49f0..108f3bcfa6da 100644
@@ -2351,6 +2351,19 @@ __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr)
 	return ptr->offset;
 }
 
+__bpf_kfunc int bpf_dynptr_clone(struct bpf_dynptr_kern *ptr,
+				 struct bpf_dynptr_kern *clone__uninit)
+{
+	if (!ptr->data) {
+		bpf_dynptr_set_null(clone__uninit);
+		return -EINVAL;
+	}
+
+	memcpy(clone__uninit, ptr, sizeof(*clone__uninit));
+
+	return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -2429,6 +2442,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null)
 BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
 BTF_ID_FLAGS(func, bpf_dynptr_get_size)
 BTF_ID_FLAGS(func, bpf_dynptr_get_offset)
+BTF_ID_FLAGS(func, bpf_dynptr_clone)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/verifier.c +112 -13

188 lines not on this page · raw message

Signature · 1 line
2.34.1
#14 lore raw

Re: [PATCH v1 bpf-next 4/5] bpf: Add bpf_dynptr_clone

Quoted 36 lines · Joanne Koong

The cloned dynptr will point to the same data as its parent dynptr, with the same type, offset, size and read-only properties.

Any writes to a dynptr will be reflected across all instances (by 'instance', this means any dynptrs that point to the same underlying data).

Please note that data slice and dynptr invalidations will affect all instances as well. For example, if bpf_dynptr_write() is called on an skb-type dynptr, all data slices of dynptr instances to that skb will be invalidated as well (eg data slices of any clones, parents, grandparents, ...). Another example is if a ringbuf dynptr is submitted, any instance of that dynptr will be invalidated.

Changing the view of the dynptr (eg advancing the offset or trimming the size) will only affect that dynptr and not affect any other instances.

One example use case where cloning may be helpful is for hashing or iterating through dynptr data. Cloning will allow the user to maintain the original view of the dynptr for future use, while also allowing views to smaller subsets of the data after the offset is advanced or the size is trimmed.

---
 kernel/bpf/helpers.c  |  14 +++++
 kernel/bpf/verifier.c | 125 +++++++++++++++++++++++++++++++++++++-----
 2 files changed, 126 insertions(+), 13 deletions(-)
kernel/bpf/helpers.c +0 -0
index bac4c6fe49f0..108f3bcfa6da 100644

kernel/bpf/helpers.c __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr) · line 2355

Quoted 5 lines
kernel/bpf/helpers.c +2 -0
@@ -2351,6 +2351,19 @@ __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr)
        return ptr->offset;
 }
 
+__bpf_kfunc int bpf_dynptr_clone(struct bpf_dynptr_kern *ptr,
+                                struct bpf_dynptr_kern *clone__uninit)

I think most of uses for bpf_dynptr_clone() would be to get a partial view (like you mentioned above, to, e.g., do a hashing of a part of the memory range). So it feels it would be best UX if clone would allow you to define a new range in one go. So e.g., to create a "sub-dynptr" for range of bytes [10, 30), it should be enough to:

struct bpf_dynptr orig_ptr, new_ptr;

bpf_dynptr_clone(&orig_ptr, 10, 30, &new_ptr);

Instead of:

bpf_dynptr_clone(&orig_ptr, &new_ptr); bpf_dynptr_advance(&new_ptr, 10); bpf_dynptr_trim(&new_ptr, bpf_dynptr_get_size(&orig_ptr) - 30);

This, btw, shows the awkwardness of the bpf_dynptr_trim() approach.

If someone really wanted an exact full-sized copy, it's trivial:

bpf_dynptr_clone(&orig_ptr, 0, bpf_dynptr_get_size(&orig_ptr), &new_ptr);

BTW, let's rename bpf_dynptr_get_size -> bpf_dynptr_size()? That "get_" is a sore in the eye, IMO.

kernel/bpf/helpers.c __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr) · line 2362

Quoted 7 lines
kernel/bpf/helpers.c +7 -0
+{
+       if (!ptr->data) {
+               bpf_dynptr_set_null(clone__uninit);
+               return -EINVAL;
+       }
+
+       memcpy(clone__uninit, ptr, sizeof(*clone__uninit));

why memcpy instead of *clone__uninit = *ptr?

Quoted 64 lines
kernel/bpf/helpers.c +5 -0
+
+       return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
        return obj;
@@ -2429,6 +2442,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null)
 BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
 BTF_ID_FLAGS(func, bpf_dynptr_get_size)
 BTF_ID_FLAGS(func, bpf_dynptr_get_offset)
+BTF_ID_FLAGS(func, bpf_dynptr_clone)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/verifier.c +9 -11
index 3660b573048a..804cb50050f9 100644
@@ -302,6 +302,7 @@ struct bpf_kfunc_call_arg_meta {
        struct {
                enum bpf_dynptr_type type;
                u32 id;
+               u32 ref_obj_id;
        } initialized_dynptr;
        struct {
                u8 spi;
@@ -963,24 +964,15 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
        return 0;
 }
 
-static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
+static void invalidate_dynptr(struct bpf_verifier_env *env, struct bpf_func_state *state, int spi)
 {
-       struct bpf_func_state *state = func(env, reg);
-       int spi, i;
-
-       spi = dynptr_get_spi(env, reg);
-       if (spi < 0)
-               return spi;
+       int i;
 
        for (i = 0; i < BPF_REG_SIZE; i++) {
                state->stack[spi].slot_type[i] = STACK_INVALID;
                state->stack[spi - 1].slot_type[i] = STACK_INVALID;
        }
 
-       /* Invalidate any slices associated with this dynptr */
-       if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type))
-               WARN_ON_ONCE(release_reference(env, state->stack[spi].spilled_ptr.ref_obj_id));
-
        __mark_reg_not_init(env, &state->stack[spi].spilled_ptr);
        __mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr);
 
@@ -1007,6 +999,51 @@ static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re
         */
        state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
        state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN;
+}
+
+static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
+{
+       struct bpf_func_state *state = func(env, reg);
+       int spi;

kernel/bpf/verifier.c static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re · line 1017

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+
+       spi = dynptr_get_spi(env, reg);
+       if (spi < 0)
+               return spi;
+
+       if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) {
+               int ref_obj_id = state->stack[spi].spilled_ptr.ref_obj_id;
+               int i;
+
+               /* If the dynptr has a ref_obj_id, then we need to invaldiate

typo: invalidate

kernel/bpf/verifier.c static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re · line 1021

Quoted 4 lines
kernel/bpf/verifier.c +4 -0
+                * two things:
+                *
+                * 1) Any dynptrs with a matching ref_obj_id (clones)
+                * 2) Any slices associated with the ref_obj_id

I think this is where this dynptr_id confusion comes from. The rule should be "any slices derived from this dynptr". But as mentioned on another thread, it's a separate topic which we can address later.

kernel/bpf/verifier.c static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re · line 1029

Quoted 8 lines
kernel/bpf/verifier.c +8 -0
+                */
+
+               /* Invalidate any slices associated with this dynptr */
+               WARN_ON_ONCE(release_reference(env, ref_obj_id));
+
+               /* Invalidate any dynptr clones */
+               for (i = 1; i < state->allocated_stack / BPF_REG_SIZE; i++) {
+                       if (state->stack[i].spilled_ptr.ref_obj_id == ref_obj_id) {

nit: invert if condition and continue to reduce nestedness of the rest the loop body

Quoted 28 lines
kernel/bpf/verifier.c +21 -0
+                               /* it should always be the case that if the ref obj id
+                                * matches then the stack slot also belongs to a
+                                * dynptr
+                                */
+                               if (state->stack[i].slot_type[0] != STACK_DYNPTR) {
+                                       verbose(env, "verifier internal error: misconfigured ref_obj_id\n");
+                                       return -EFAULT;
+                               }
+                               if (state->stack[i].spilled_ptr.dynptr.first_slot)
+                                       invalidate_dynptr(env, state, i);
+                       }
+               }
+
+               return 0;
+       }
+
+       invalidate_dynptr(env, state, spi);
 
        return 0;
 }
@@ -6967,6 +7004,50 @@ static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx,
        return 0;
 }
 
+static int handle_dynptr_clone(struct bpf_verifier_env *env, enum bpf_arg_type arg_type,
+                              int regno, int insn_idx, struct bpf_kfunc_call_arg_meta *meta)
+{
+       struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];

kernel/bpf/verifier.c static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, · line 7020

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+       struct bpf_reg_state *first_reg_state, *second_reg_state;
+       struct bpf_func_state *state = func(env, reg);
+       enum bpf_dynptr_type dynptr_type = meta->initialized_dynptr.type;
+       int err, spi, ref_obj_id;
+
+       if (!dynptr_type) {
+               verbose(env, "verifier internal error: no dynptr type for bpf_dynptr_clone\n");
+               return -EFAULT;
+       }
+       arg_type |= get_dynptr_type_flag(dynptr_type);

what about MEM_RDONLY and MEM_UNINIT flags, do we need to derive and add them?

Quoted 6 lines
kernel/bpf/verifier.c +6 -0
+
+       err = process_dynptr_func(env, regno, insn_idx, arg_type);
+       if (err < 0)
+               return err;
+
+       spi = dynptr_get_spi(env, reg);

kernel/bpf/verifier.c static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, · line 7036

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+       if (spi < 0)
+               return spi;
+
+       first_reg_state = &state->stack[spi].spilled_ptr;
+       second_reg_state = &state->stack[spi - 1].spilled_ptr;
+       ref_obj_id = first_reg_state->ref_obj_id;
+
+       /* reassign the clone the same dynptr id as the original */
+       __mark_dynptr_reg(first_reg_state, dynptr_type, true, meta->initialized_dynptr.id);
+       __mark_dynptr_reg(second_reg_state, dynptr_type, false, meta->initialized_dynptr.id);

I'm not sure why clone should have the same dynptr_id? Isn't it a new instance of a dynptr? I get preserving ref_obj_id (if refcounted), but why reusing dynptr_id?..

kernel/bpf/verifier.c static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, · line 7042

Quoted 6 lines
kernel/bpf/verifier.c +6 -0
+
+       if (meta->initialized_dynptr.ref_obj_id) {
+               /* release the new ref obj id assigned during process_dynptr_func */
+               err = release_reference_state(cur_func(env), ref_obj_id);
+               if (err)
+                       return err;

ugh... this is not good to create reference just to release. If we need to reuse logic, let's reuse the logic without parts that shouldn't happen. Please check if we can split process_dynptr_func() appropriately to allow this.

Quoted 8 lines
kernel/bpf/verifier.c +8 -0
+               /* reassign the clone the same ref obj id as the original */
+               first_reg_state->ref_obj_id = meta->initialized_dynptr.ref_obj_id;
+               second_reg_state->ref_obj_id = meta->initialized_dynptr.ref_obj_id;
+       }
+
+       return 0;
+}
+

[...]

#15 lore raw

Re: [PATCH v1 bpf-next 4/5] bpf: Add bpf_dynptr_clone

Quoted 52 lines · Andrii Nakryiko
Quoted 36 lines · Joanne Koong

The cloned dynptr will point to the same data as its parent dynptr, with the same type, offset, size and read-only properties.

Any writes to a dynptr will be reflected across all instances (by 'instance', this means any dynptrs that point to the same underlying data).

Please note that data slice and dynptr invalidations will affect all instances as well. For example, if bpf_dynptr_write() is called on an skb-type dynptr, all data slices of dynptr instances to that skb will be invalidated as well (eg data slices of any clones, parents, grandparents, ...). Another example is if a ringbuf dynptr is submitted, any instance of that dynptr will be invalidated.

Changing the view of the dynptr (eg advancing the offset or trimming the size) will only affect that dynptr and not affect any other instances.

One example use case where cloning may be helpful is for hashing or iterating through dynptr data. Cloning will allow the user to maintain the original view of the dynptr for future use, while also allowing views to smaller subsets of the data after the offset is advanced or the size is trimmed.

---
 kernel/bpf/helpers.c  |  14 +++++
 kernel/bpf/verifier.c | 125 +++++++++++++++++++++++++++++++++++++-----
 2 files changed, 126 insertions(+), 13 deletions(-)
kernel/bpf/helpers.c +0 -0
index bac4c6fe49f0..108f3bcfa6da 100644

kernel/bpf/helpers.c __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr) · line 2355

Quoted 5 lines
kernel/bpf/helpers.c +2 -0
@@ -2351,6 +2351,19 @@ __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr)
        return ptr->offset;
 }
 
+__bpf_kfunc int bpf_dynptr_clone(struct bpf_dynptr_kern *ptr,
+                                struct bpf_dynptr_kern *clone__uninit)

I think most of uses for bpf_dynptr_clone() would be to get a partial view (like you mentioned above, to, e.g., do a hashing of a part of the memory range). So it feels it would be best UX if clone would allow you to define a new range in one go. So e.g., to create a "sub-dynptr" for range of bytes [10, 30), it should be enough to:

struct bpf_dynptr orig_ptr, new_ptr;

Quoted 8 lines

bpf_dynptr_clone(&orig_ptr, 10, 30, &new_ptr);

Instead of:

bpf_dynptr_clone(&orig_ptr, &new_ptr); bpf_dynptr_advance(&new_ptr, 10); bpf_dynptr_trim(&new_ptr, bpf_dynptr_get_size(&orig_ptr) - 30);

I don't think we can do this because we can't have bpf_dynptr_clone() fail (which might happen if we take in a range, if the range is invalid). This is because in the case where the clone is of a reference-counted dynptr (eg like a ringbuf-type dynptr), the clone even if it's invalid must be treated by the verifier as a legit dynptr (since the verifier can't know ahead of time whether the clone call will succeed or not) which means if the invalid clone dynptr is then passed into a reference-releasing function, the verifier will release the reference but the actual reference won't be released at runtime (since the clone dynptr is invalid), which would leak the reference. An example is something like:

// invalid range is passed, error is returned and new_ptr is invalid
bpf_dynptr_clone(&ringbuf_ptr, 9999999, 9999999, &new_ptr);
// this releases the reference and invalidates both new_ptr and ringbuf_ptr bpf_ringbuf_discard_dynptr(&new_ptr, 0);

At runtime, bpf_ringbuf_discard_dynptr() will be a no-op since new_ptr is invalid - the ringbuf record still needs to be submitted/discarded, but the verifier will think this already happened

Quoted 10 lines

This, btw, shows the awkwardness of the bpf_dynptr_trim() approach.

If someone really wanted an exact full-sized copy, it's trivial:

bpf_dynptr_clone(&orig_ptr, 0, bpf_dynptr_get_size(&orig_ptr), &new_ptr);

BTW, let's rename bpf_dynptr_get_size -> bpf_dynptr_size()? That "get_" is a sore in the eye, IMO.

Will do!

Quoted 12 lines

kernel/bpf/helpers.c __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr) · line 2362

Quoted 9 lines
kernel/bpf/helpers.c +7 -0
+{
+       if (!ptr->data) {
+               bpf_dynptr_set_null(clone__uninit);
+               return -EINVAL;
+       }
+
+       memcpy(clone__uninit, ptr, sizeof(*clone__uninit));

why memcpy instead of *clone__uninit = *ptr?

No good reason :) I'll change this for v2

Quoted 77 lines
Quoted 64 lines
kernel/bpf/helpers.c +5 -0
+
+       return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
        return obj;
@@ -2429,6 +2442,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null)
 BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
 BTF_ID_FLAGS(func, bpf_dynptr_get_size)
 BTF_ID_FLAGS(func, bpf_dynptr_get_offset)
+BTF_ID_FLAGS(func, bpf_dynptr_clone)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/verifier.c +9 -11
index 3660b573048a..804cb50050f9 100644
@@ -302,6 +302,7 @@ struct bpf_kfunc_call_arg_meta {
        struct {
                enum bpf_dynptr_type type;
                u32 id;
+               u32 ref_obj_id;
        } initialized_dynptr;
        struct {
                u8 spi;
@@ -963,24 +964,15 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
        return 0;
 }
 
-static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
+static void invalidate_dynptr(struct bpf_verifier_env *env, struct bpf_func_state *state, int spi)
 {
-       struct bpf_func_state *state = func(env, reg);
-       int spi, i;
-
-       spi = dynptr_get_spi(env, reg);
-       if (spi < 0)
-               return spi;
+       int i;
 
        for (i = 0; i < BPF_REG_SIZE; i++) {
                state->stack[spi].slot_type[i] = STACK_INVALID;
                state->stack[spi - 1].slot_type[i] = STACK_INVALID;
        }
 
-       /* Invalidate any slices associated with this dynptr */
-       if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type))
-               WARN_ON_ONCE(release_reference(env, state->stack[spi].spilled_ptr.ref_obj_id));
-
        __mark_reg_not_init(env, &state->stack[spi].spilled_ptr);
        __mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr);
 
@@ -1007,6 +999,51 @@ static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re
         */
        state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
        state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN;
+}
+
+static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
+{
+       struct bpf_func_state *state = func(env, reg);
+       int spi;

kernel/bpf/verifier.c static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re · line 1017

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+
+       spi = dynptr_get_spi(env, reg);
+       if (spi < 0)
+               return spi;
+
+       if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) {
+               int ref_obj_id = state->stack[spi].spilled_ptr.ref_obj_id;
+               int i;
+
+               /* If the dynptr has a ref_obj_id, then we need to invaldiate

typo: invalidate

Quoted 9 lines

kernel/bpf/verifier.c static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re · line 1021

Quoted 4 lines
kernel/bpf/verifier.c +4 -0
+                * two things:
+                *
+                * 1) Any dynptrs with a matching ref_obj_id (clones)
+                * 2) Any slices associated with the ref_obj_id

I think this is where this dynptr_id confusion comes from. The rule should be "any slices derived from this dynptr". But as mentioned on another thread, it's a separate topic which we can address later.

If there's a parent and a clone and slices derived from the parent and slices derived from the clone, if the clone is invalidated then we need to invalidate slices associated with the parent as well. So shouldn't it be "any slices associated with the ref obj id" not "any slices derived from this dynptr"? (also just a note, parent/clone slices will share the same ref obj id and the same dynptr id, so checking against either does the same thing)

Quoted 11 lines

kernel/bpf/verifier.c static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re · line 1029

Quoted 8 lines
kernel/bpf/verifier.c +8 -0
+                */
+
+               /* Invalidate any slices associated with this dynptr */
+               WARN_ON_ONCE(release_reference(env, ref_obj_id));
+
+               /* Invalidate any dynptr clones */
+               for (i = 1; i < state->allocated_stack / BPF_REG_SIZE; i++) {
+                       if (state->stack[i].spilled_ptr.ref_obj_id == ref_obj_id) {

nit: invert if condition and continue to reduce nestedness of the rest the loop body

Ooh good idea

Quoted 34 lines
Quoted 34 lines
kernel/bpf/verifier.c +26 -0
+                               /* it should always be the case that if the ref obj id
+                                * matches then the stack slot also belongs to a
+                                * dynptr
+                                */
+                               if (state->stack[i].slot_type[0] != STACK_DYNPTR) {
+                                       verbose(env, "verifier internal error: misconfigured ref_obj_id\n");
+                                       return -EFAULT;
+                               }
+                               if (state->stack[i].spilled_ptr.dynptr.first_slot)
+                                       invalidate_dynptr(env, state, i);
+                       }
+               }
+
+               return 0;
+       }
+
+       invalidate_dynptr(env, state, spi);
 
        return 0;
 }
@@ -6967,6 +7004,50 @@ static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx,
        return 0;
 }
 
+static int handle_dynptr_clone(struct bpf_verifier_env *env, enum bpf_arg_type arg_type,
+                              int regno, int insn_idx, struct bpf_kfunc_call_arg_meta *meta)
+{
+       struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
+       struct bpf_reg_state *first_reg_state, *second_reg_state;
+       struct bpf_func_state *state = func(env, reg);
+       enum bpf_dynptr_type dynptr_type = meta->initialized_dynptr.type;
+       int err, spi, ref_obj_id;
+
Quoted 8 lines

kernel/bpf/verifier.c static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, · line 7020

Quoted 5 lines
kernel/bpf/verifier.c +5 -0
+       if (!dynptr_type) {
+               verbose(env, "verifier internal error: no dynptr type for bpf_dynptr_clone\n");
+               return -EFAULT;
+       }
+       arg_type |= get_dynptr_type_flag(dynptr_type);

what about MEM_RDONLY and MEM_UNINIT flags, do we need to derive and add them?

I don't think we need MEM_UNINIT because we can't clone an uninitialized dynptr. But yes, definitely MEM_RDONLY. I missed that, I will add it in in v2

Quoted 13 lines
Quoted 13 lines
kernel/bpf/verifier.c +12 -0
+
+       err = process_dynptr_func(env, regno, insn_idx, arg_type);
+       if (err < 0)
+               return err;
+
+       spi = dynptr_get_spi(env, reg);
+       if (spi < 0)
+               return spi;
+
+       first_reg_state = &state->stack[spi].spilled_ptr;
+       second_reg_state = &state->stack[spi - 1].spilled_ptr;
+       ref_obj_id = first_reg_state->ref_obj_id;
Quoted 9 lines

kernel/bpf/verifier.c static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, · line 7036

Quoted 4 lines
kernel/bpf/verifier.c +4 -0
+
+       /* reassign the clone the same dynptr id as the original */
+       __mark_dynptr_reg(first_reg_state, dynptr_type, true, meta->initialized_dynptr.id);
+       __mark_dynptr_reg(second_reg_state, dynptr_type, false, meta->initialized_dynptr.id);

I'm not sure why clone should have the same dynptr_id? Isn't it a new instance of a dynptr? I get preserving ref_obj_id (if refcounted), but why reusing dynptr_id?..

I think we need to also copy over the dynptr id because in the case of a non-reference counted dynptr, if the parent (or clone) is invalidated (eg overwriting bytes of the dynptr on the stack), we must also invalidate the slices of the clone (or parent)

Quoted 8 lines

kernel/bpf/verifier.c static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, · line 7042

Quoted 7 lines
kernel/bpf/verifier.c +6 -0
+
+       if (meta->initialized_dynptr.ref_obj_id) {
+               /* release the new ref obj id assigned during process_dynptr_func */
+               err = release_reference_state(cur_func(env), ref_obj_id);
+               if (err)
+                       return err;
Quoted 4 lines

ugh... this is not good to create reference just to release. If we need to reuse logic, let's reuse the logic without parts that shouldn't happen. Please check if we can split process_dynptr_func() appropriately to allow this.

I'll change this for v2. I think the simplest approach would be having mark_stack_slots_dynptr() take in a boolean or something that'll indicate whether it should acquire a new reference state or not

Quoted 11 lines
Quoted 9 lines
kernel/bpf/verifier.c +8 -0
+               /* reassign the clone the same ref obj id as the original */
+               first_reg_state->ref_obj_id = meta->initialized_dynptr.ref_obj_id;
+               second_reg_state->ref_obj_id = meta->initialized_dynptr.ref_obj_id;
+       }
+
+       return 0;
+}
+

[...]

#16 lore raw

Re: [PATCH v1 bpf-next 4/5] bpf: Add bpf_dynptr_clone

Quoted 76 lines · Joanne Koong
Quoted 52 lines · Andrii Nakryiko
Quoted 36 lines · Joanne Koong

The cloned dynptr will point to the same data as its parent dynptr, with the same type, offset, size and read-only properties.

Any writes to a dynptr will be reflected across all instances (by 'instance', this means any dynptrs that point to the same underlying data).

Please note that data slice and dynptr invalidations will affect all instances as well. For example, if bpf_dynptr_write() is called on an skb-type dynptr, all data slices of dynptr instances to that skb will be invalidated as well (eg data slices of any clones, parents, grandparents, ...). Another example is if a ringbuf dynptr is submitted, any instance of that dynptr will be invalidated.

Changing the view of the dynptr (eg advancing the offset or trimming the size) will only affect that dynptr and not affect any other instances.

One example use case where cloning may be helpful is for hashing or iterating through dynptr data. Cloning will allow the user to maintain the original view of the dynptr for future use, while also allowing views to smaller subsets of the data after the offset is advanced or the size is trimmed.

---
 kernel/bpf/helpers.c  |  14 +++++
 kernel/bpf/verifier.c | 125 +++++++++++++++++++++++++++++++++++++-----
 2 files changed, 126 insertions(+), 13 deletions(-)
kernel/bpf/helpers.c +0 -0
index bac4c6fe49f0..108f3bcfa6da 100644

kernel/bpf/helpers.c __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr) · line 2355

Quoted 5 lines
kernel/bpf/helpers.c +2 -0
@@ -2351,6 +2351,19 @@ __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr)
        return ptr->offset;
 }
 
+__bpf_kfunc int bpf_dynptr_clone(struct bpf_dynptr_kern *ptr,
+                                struct bpf_dynptr_kern *clone__uninit)

I think most of uses for bpf_dynptr_clone() would be to get a partial view (like you mentioned above, to, e.g., do a hashing of a part of the memory range). So it feels it would be best UX if clone would allow you to define a new range in one go. So e.g., to create a "sub-dynptr" for range of bytes [10, 30), it should be enough to:

struct bpf_dynptr orig_ptr, new_ptr;

Quoted 8 lines

bpf_dynptr_clone(&orig_ptr, 10, 30, &new_ptr);

Instead of:

bpf_dynptr_clone(&orig_ptr, &new_ptr); bpf_dynptr_advance(&new_ptr, 10); bpf_dynptr_trim(&new_ptr, bpf_dynptr_get_size(&orig_ptr) - 30);

I don't think we can do this because we can't have bpf_dynptr_clone() fail (which might happen if we take in a range, if the range is invalid). This is because in the case where the clone is of a reference-counted dynptr (eg like a ringbuf-type dynptr), the clone even if it's invalid must be treated by the verifier as a legit dynptr (since the verifier can't know ahead of time whether the clone call will succeed or not) which means if the invalid clone dynptr is then passed into a reference-releasing function, the verifier will release the reference but the actual reference won't be released at runtime (since the clone dynptr is invalid), which would leak the reference. An example is something like:

Quoted 8 lines

// invalid range is passed, error is returned and new_ptr is invalid
bpf_dynptr_clone(&ringbuf_ptr, 9999999, 9999999, &new_ptr);
// this releases the reference and invalidates both new_ptr and ringbuf_ptr bpf_ringbuf_discard_dynptr(&new_ptr, 0);

At runtime, bpf_ringbuf_discard_dynptr() will be a no-op since new_ptr is invalid - the ringbuf record still needs to be submitted/discarded, but the verifier will think this already happened

Ah, tricky, good point. But ok, I guess with bpf_dynptr_adjust() proposal in another email this would be ok:

bpf_dynptr_clone(..); /* always succeeds */ bpf_dynptr_adjust(&new_ptr, 10, 30); /* could fail to adjust, but dynptr is left valid */

Right?

Quoted 112 lines
Quoted 11 lines

This, btw, shows the awkwardness of the bpf_dynptr_trim() approach.

If someone really wanted an exact full-sized copy, it's trivial:

bpf_dynptr_clone(&orig_ptr, 0, bpf_dynptr_get_size(&orig_ptr), &new_ptr);

BTW, let's rename bpf_dynptr_get_size -> bpf_dynptr_size()? That "get_" is a sore in the eye, IMO.

Will do!

Quoted 12 lines

kernel/bpf/helpers.c __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr) · line 2362

Quoted 9 lines
kernel/bpf/helpers.c +7 -0
+{
+       if (!ptr->data) {
+               bpf_dynptr_set_null(clone__uninit);
+               return -EINVAL;
+       }
+
+       memcpy(clone__uninit, ptr, sizeof(*clone__uninit));

why memcpy instead of *clone__uninit = *ptr?

No good reason :) I'll change this for v2

Quoted 77 lines
Quoted 64 lines
kernel/bpf/helpers.c +5 -0
+
+       return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
        return obj;
@@ -2429,6 +2442,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null)
 BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
 BTF_ID_FLAGS(func, bpf_dynptr_get_size)
 BTF_ID_FLAGS(func, bpf_dynptr_get_offset)
+BTF_ID_FLAGS(func, bpf_dynptr_clone)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/verifier.c +9 -11
index 3660b573048a..804cb50050f9 100644
@@ -302,6 +302,7 @@ struct bpf_kfunc_call_arg_meta {
        struct {
                enum bpf_dynptr_type type;
                u32 id;
+               u32 ref_obj_id;
        } initialized_dynptr;
        struct {
                u8 spi;
@@ -963,24 +964,15 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
        return 0;
 }
 
-static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
+static void invalidate_dynptr(struct bpf_verifier_env *env, struct bpf_func_state *state, int spi)
 {
-       struct bpf_func_state *state = func(env, reg);
-       int spi, i;
-
-       spi = dynptr_get_spi(env, reg);
-       if (spi < 0)
-               return spi;
+       int i;
 
        for (i = 0; i < BPF_REG_SIZE; i++) {
                state->stack[spi].slot_type[i] = STACK_INVALID;
                state->stack[spi - 1].slot_type[i] = STACK_INVALID;
        }
 
-       /* Invalidate any slices associated with this dynptr */
-       if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type))
-               WARN_ON_ONCE(release_reference(env, state->stack[spi].spilled_ptr.ref_obj_id));
-
        __mark_reg_not_init(env, &state->stack[spi].spilled_ptr);
        __mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr);
 
@@ -1007,6 +999,51 @@ static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re
         */
        state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
        state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN;
+}
+
+static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
+{
+       struct bpf_func_state *state = func(env, reg);
+       int spi;

kernel/bpf/verifier.c static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re · line 1017

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+
+       spi = dynptr_get_spi(env, reg);
+       if (spi < 0)
+               return spi;
+
+       if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) {
+               int ref_obj_id = state->stack[spi].spilled_ptr.ref_obj_id;
+               int i;
+
+               /* If the dynptr has a ref_obj_id, then we need to invaldiate

typo: invalidate

Quoted 9 lines

kernel/bpf/verifier.c static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re · line 1021

Quoted 4 lines
kernel/bpf/verifier.c +4 -0
+                * two things:
+                *
+                * 1) Any dynptrs with a matching ref_obj_id (clones)
+                * 2) Any slices associated with the ref_obj_id

I think this is where this dynptr_id confusion comes from. The rule should be "any slices derived from this dynptr". But as mentioned on another thread, it's a separate topic which we can address later.

Quoted 7 lines

If there's a parent and a clone and slices derived from the parent and slices derived from the clone, if the clone is invalidated then we need to invalidate slices associated with the parent as well. So shouldn't it be "any slices associated with the ref obj id" not "any slices derived from this dynptr"? (also just a note, parent/clone slices will share the same ref obj id and the same dynptr id, so checking against either does the same thing)

So, we have a ringbuf dynptr with ref_obj_id=1, id=2, ok? We clone it, clone gets ref_obj_id=1, id=3. If either original dynptr or clone dynptr is released due to bpf_ringbuf_discard_dynptr(), we invalidate all the dynptrs with ref_obj_id=1. During invalidation of each dynptr, we invalidate all the slices with ref_obj_id==dynptr's id. So we'll invalidate slices derived from dynptr with id=2 (original dynptr), and then all the slices derived from dynptr with id=3?

Quoted 83 lines
Quoted 12 lines

kernel/bpf/verifier.c static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re · line 1029

Quoted 9 lines
kernel/bpf/verifier.c +8 -0
+                */
+
+               /* Invalidate any slices associated with this dynptr */
+               WARN_ON_ONCE(release_reference(env, ref_obj_id));
+
+               /* Invalidate any dynptr clones */
+               for (i = 1; i < state->allocated_stack / BPF_REG_SIZE; i++) {
+                       if (state->stack[i].spilled_ptr.ref_obj_id == ref_obj_id) {

nit: invert if condition and continue to reduce nestedness of the rest the loop body

Ooh good idea

Quoted 34 lines
Quoted 34 lines
kernel/bpf/verifier.c +26 -0
+                               /* it should always be the case that if the ref obj id
+                                * matches then the stack slot also belongs to a
+                                * dynptr
+                                */
+                               if (state->stack[i].slot_type[0] != STACK_DYNPTR) {
+                                       verbose(env, "verifier internal error: misconfigured ref_obj_id\n");
+                                       return -EFAULT;
+                               }
+                               if (state->stack[i].spilled_ptr.dynptr.first_slot)
+                                       invalidate_dynptr(env, state, i);
+                       }
+               }
+
+               return 0;
+       }
+
+       invalidate_dynptr(env, state, spi);
 
        return 0;
 }
@@ -6967,6 +7004,50 @@ static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx,
        return 0;
 }
 
+static int handle_dynptr_clone(struct bpf_verifier_env *env, enum bpf_arg_type arg_type,
+                              int regno, int insn_idx, struct bpf_kfunc_call_arg_meta *meta)
+{
+       struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
+       struct bpf_reg_state *first_reg_state, *second_reg_state;
+       struct bpf_func_state *state = func(env, reg);
+       enum bpf_dynptr_type dynptr_type = meta->initialized_dynptr.type;
+       int err, spi, ref_obj_id;
+
Quoted 8 lines

kernel/bpf/verifier.c static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, · line 7020

Quoted 5 lines
kernel/bpf/verifier.c +5 -0
+       if (!dynptr_type) {
+               verbose(env, "verifier internal error: no dynptr type for bpf_dynptr_clone\n");
+               return -EFAULT;
+       }
+       arg_type |= get_dynptr_type_flag(dynptr_type);

what about MEM_RDONLY and MEM_UNINIT flags, do we need to derive and add them?

I don't think we need MEM_UNINIT because we can't clone an uninitialized dynptr. But yes, definitely MEM_RDONLY. I missed that, I will add it in in v2

Quoted 13 lines
Quoted 13 lines
kernel/bpf/verifier.c +12 -0
+
+       err = process_dynptr_func(env, regno, insn_idx, arg_type);
+       if (err < 0)
+               return err;
+
+       spi = dynptr_get_spi(env, reg);
+       if (spi < 0)
+               return spi;
+
+       first_reg_state = &state->stack[spi].spilled_ptr;
+       second_reg_state = &state->stack[spi - 1].spilled_ptr;
+       ref_obj_id = first_reg_state->ref_obj_id;
Quoted 9 lines

kernel/bpf/verifier.c static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, · line 7036

Quoted 4 lines
kernel/bpf/verifier.c +4 -0
+
+       /* reassign the clone the same dynptr id as the original */
+       __mark_dynptr_reg(first_reg_state, dynptr_type, true, meta->initialized_dynptr.id);
+       __mark_dynptr_reg(second_reg_state, dynptr_type, false, meta->initialized_dynptr.id);

I'm not sure why clone should have the same dynptr_id? Isn't it a new instance of a dynptr? I get preserving ref_obj_id (if refcounted), but why reusing dynptr_id?..

Quoted 4 lines

I think we need to also copy over the dynptr id because in the case of a non-reference counted dynptr, if the parent (or clone) is invalidated (eg overwriting bytes of the dynptr on the stack), we must also invalidate the slices of the clone (or parent)

yep, right now we'll have to do that because we have dynptr_id. But if we get rid of it and stick to ref_obj_id and id, then clone would need to get a new id, but keep ref_obj_id, right?

Quoted 27 lines
Quoted 8 lines

kernel/bpf/verifier.c static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, · line 7042

Quoted 7 lines
kernel/bpf/verifier.c +6 -0
+
+       if (meta->initialized_dynptr.ref_obj_id) {
+               /* release the new ref obj id assigned during process_dynptr_func */
+               err = release_reference_state(cur_func(env), ref_obj_id);
+               if (err)
+                       return err;
Quoted 4 lines

ugh... this is not good to create reference just to release. If we need to reuse logic, let's reuse the logic without parts that shouldn't happen. Please check if we can split process_dynptr_func() appropriately to allow this.

I'll change this for v2. I think the simplest approach would be having mark_stack_slots_dynptr() take in a boolean or something that'll indicate whether it should acquire a new reference state or not

Quoted 11 lines
Quoted 9 lines
kernel/bpf/verifier.c +8 -0
+               /* reassign the clone the same ref obj id as the original */
+               first_reg_state->ref_obj_id = meta->initialized_dynptr.ref_obj_id;
+               second_reg_state->ref_obj_id = meta->initialized_dynptr.ref_obj_id;
+       }
+
+       return 0;
+}
+

[...]

#17 lore raw

Re: [PATCH v1 bpf-next 4/5] bpf: Add bpf_dynptr_clone

Quoted 87 lines · Andrii Nakryiko
Quoted 76 lines · Joanne Koong
Quoted 52 lines · Andrii Nakryiko
Quoted 36 lines · Joanne Koong

The cloned dynptr will point to the same data as its parent dynptr, with the same type, offset, size and read-only properties.

Any writes to a dynptr will be reflected across all instances (by 'instance', this means any dynptrs that point to the same underlying data).

Please note that data slice and dynptr invalidations will affect all instances as well. For example, if bpf_dynptr_write() is called on an skb-type dynptr, all data slices of dynptr instances to that skb will be invalidated as well (eg data slices of any clones, parents, grandparents, ...). Another example is if a ringbuf dynptr is submitted, any instance of that dynptr will be invalidated.

Changing the view of the dynptr (eg advancing the offset or trimming the size) will only affect that dynptr and not affect any other instances.

One example use case where cloning may be helpful is for hashing or iterating through dynptr data. Cloning will allow the user to maintain the original view of the dynptr for future use, while also allowing views to smaller subsets of the data after the offset is advanced or the size is trimmed.

---
 kernel/bpf/helpers.c  |  14 +++++
 kernel/bpf/verifier.c | 125 +++++++++++++++++++++++++++++++++++++-----
 2 files changed, 126 insertions(+), 13 deletions(-)
kernel/bpf/helpers.c +0 -0
index bac4c6fe49f0..108f3bcfa6da 100644

kernel/bpf/helpers.c __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr) · line 2355

Quoted 5 lines
kernel/bpf/helpers.c +2 -0
@@ -2351,6 +2351,19 @@ __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr)
        return ptr->offset;
 }
 
+__bpf_kfunc int bpf_dynptr_clone(struct bpf_dynptr_kern *ptr,
+                                struct bpf_dynptr_kern *clone__uninit)

I think most of uses for bpf_dynptr_clone() would be to get a partial view (like you mentioned above, to, e.g., do a hashing of a part of the memory range). So it feels it would be best UX if clone would allow you to define a new range in one go. So e.g., to create a "sub-dynptr" for range of bytes [10, 30), it should be enough to:

struct bpf_dynptr orig_ptr, new_ptr;

Quoted 8 lines

bpf_dynptr_clone(&orig_ptr, 10, 30, &new_ptr);

Instead of:

bpf_dynptr_clone(&orig_ptr, &new_ptr); bpf_dynptr_advance(&new_ptr, 10); bpf_dynptr_trim(&new_ptr, bpf_dynptr_get_size(&orig_ptr) - 30);

I don't think we can do this because we can't have bpf_dynptr_clone() fail (which might happen if we take in a range, if the range is invalid). This is because in the case where the clone is of a reference-counted dynptr (eg like a ringbuf-type dynptr), the clone even if it's invalid must be treated by the verifier as a legit dynptr (since the verifier can't know ahead of time whether the clone call will succeed or not) which means if the invalid clone dynptr is then passed into a reference-releasing function, the verifier will release the reference but the actual reference won't be released at runtime (since the clone dynptr is invalid), which would leak the reference. An example is something like:

Quoted 8 lines

// invalid range is passed, error is returned and new_ptr is invalid
bpf_dynptr_clone(&ringbuf_ptr, 9999999, 9999999, &new_ptr);
// this releases the reference and invalidates both new_ptr and ringbuf_ptr bpf_ringbuf_discard_dynptr(&new_ptr, 0);

At runtime, bpf_ringbuf_discard_dynptr() will be a no-op since new_ptr is invalid - the ringbuf record still needs to be submitted/discarded, but the verifier will think this already happened

Quoted 8 lines

Ah, tricky, good point. But ok, I guess with bpf_dynptr_adjust() proposal in another email this would be ok:

bpf_dynptr_clone(..); /* always succeeds */ bpf_dynptr_adjust(&new_ptr, 10, 30); /* could fail to adjust, but dynptr is left valid */

Right?

Yes, this would be okay because if bpf_dynptr_adjust fails, the clone is valid (it was unaffected) so submitting/discarding it later on would correctly release the reference

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

This, btw, shows the awkwardness of the bpf_dynptr_trim() approach.

If someone really wanted an exact full-sized copy, it's trivial:

bpf_dynptr_clone(&orig_ptr, 0, bpf_dynptr_get_size(&orig_ptr), &new_ptr);

[...]

Quoted 34 lines
Quoted 26 lines
Quoted 17 lines
Quoted 4 lines
kernel/bpf/helpers.c +4 -0
+static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
+{
+       struct bpf_func_state *state = func(env, reg);
+       int spi;

kernel/bpf/helpers.c

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+
+       spi = dynptr_get_spi(env, reg);
+       if (spi < 0)
+               return spi;
+
+       if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) {
+               int ref_obj_id = state->stack[spi].spilled_ptr.ref_obj_id;
+               int i;
+
+               /* If the dynptr has a ref_obj_id, then we need to invaldiate

typo: invalidate

Quoted 9 lines

kernel/bpf/helpers.c

Quoted 4 lines
kernel/bpf/helpers.c +4 -0
+                * two things:
+                *
+                * 1) Any dynptrs with a matching ref_obj_id (clones)
+                * 2) Any slices associated with the ref_obj_id

I think this is where this dynptr_id confusion comes from. The rule should be "any slices derived from this dynptr". But as mentioned on another thread, it's a separate topic which we can address later.

Quoted 7 lines

If there's a parent and a clone and slices derived from the parent and slices derived from the clone, if the clone is invalidated then we need to invalidate slices associated with the parent as well. So shouldn't it be "any slices associated with the ref obj id" not "any slices derived from this dynptr"? (also just a note, parent/clone slices will share the same ref obj id and the same dynptr id, so checking against either does the same thing)

Quoted 8 lines

So, we have a ringbuf dynptr with ref_obj_id=1, id=2, ok? We clone it, clone gets ref_obj_id=1, id=3. If either original dynptr or clone dynptr is released due to bpf_ringbuf_discard_dynptr(), we invalidate all the dynptrs with ref_obj_id=1. During invalidation of each dynptr, we invalidate all the slices with ref_obj_id==dynptr's id. So we'll invalidate slices derived from dynptr with id=2 (original dynptr), and then all the slices derived from dynptr with id=3?

When we create a slice for a dynptr (through bpf_dynptr_data()), the slice's reg->ref_obj_id is set to the dynptr's ref_obj_id (not dynptr's id). During invalidation of the dynptr, we invalidate all the slices with that ref obj id, which means we invalidate all slices for any parents/clones.
[...]

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 6 lines
kernel/bpf/helpers.c +6 -0
+
+       err = process_dynptr_func(env, regno, insn_idx, arg_type);
+       if (err < 0)
+               return err;
+
+       spi = dynptr_get_spi(env, reg);

kernel/bpf/helpers.c

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       if (spi < 0)
+               return spi;
+
+       first_reg_state = &state->stack[spi].spilled_ptr;
+       second_reg_state = &state->stack[spi - 1].spilled_ptr;
+       ref_obj_id = first_reg_state->ref_obj_id;
+
+       /* reassign the clone the same dynptr id as the original */
+       __mark_dynptr_reg(first_reg_state, dynptr_type, true, meta->initialized_dynptr.id);
+       __mark_dynptr_reg(second_reg_state, dynptr_type, false, meta->initialized_dynptr.id);

I'm not sure why clone should have the same dynptr_id? Isn't it a new instance of a dynptr? I get preserving ref_obj_id (if refcounted), but why reusing dynptr_id?..

Quoted 9 lines
Quoted 4 lines

I think we need to also copy over the dynptr id because in the case of a non-reference counted dynptr, if the parent (or clone) is invalidated (eg overwriting bytes of the dynptr on the stack), we must also invalidate the slices of the clone (or parent)

yep, right now we'll have to do that because we have dynptr_id. But if we get rid of it and stick to ref_obj_id and id, then clone would need to get a new id, but keep ref_obj_id, right?

Thinking about this some more, I think you're right that we shouldn't reuse the dynptr id. in fact, i think reusing it would lead to incorrect behavior - in the example of a non-reference counted dynptr, if the parent dynptr is overwritten on the stack (and thus invalidated), that shouldn't invalidate the slices of the clone at all. I'll change this in the next version

Quoted 1 line
Quoted 1 line
Quoted 1 line

[...]

#18 lore raw

Re: [PATCH v1 bpf-next 4/5] bpf: Add bpf_dynptr_clone

Quoted 97 lines · Joanne Koong
Quoted 94 lines · Andrii Nakryiko
Quoted 76 lines · Joanne Koong
Quoted 52 lines · Andrii Nakryiko
Quoted 36 lines · Joanne Koong

The cloned dynptr will point to the same data as its parent dynptr, with the same type, offset, size and read-only properties.

Any writes to a dynptr will be reflected across all instances (by 'instance', this means any dynptrs that point to the same underlying data).

Please note that data slice and dynptr invalidations will affect all instances as well. For example, if bpf_dynptr_write() is called on an skb-type dynptr, all data slices of dynptr instances to that skb will be invalidated as well (eg data slices of any clones, parents, grandparents, ...). Another example is if a ringbuf dynptr is submitted, any instance of that dynptr will be invalidated.

Changing the view of the dynptr (eg advancing the offset or trimming the size) will only affect that dynptr and not affect any other instances.

One example use case where cloning may be helpful is for hashing or iterating through dynptr data. Cloning will allow the user to maintain the original view of the dynptr for future use, while also allowing views to smaller subsets of the data after the offset is advanced or the size is trimmed.

---
 kernel/bpf/helpers.c  |  14 +++++
 kernel/bpf/verifier.c | 125 +++++++++++++++++++++++++++++++++++++-----
 2 files changed, 126 insertions(+), 13 deletions(-)
kernel/bpf/helpers.c +0 -0
index bac4c6fe49f0..108f3bcfa6da 100644

kernel/bpf/helpers.c __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr) · line 2355

Quoted 5 lines
kernel/bpf/helpers.c +2 -0
@@ -2351,6 +2351,19 @@ __bpf_kfunc __u32 bpf_dynptr_get_offset(const struct bpf_dynptr_kern *ptr)
        return ptr->offset;
 }
 
+__bpf_kfunc int bpf_dynptr_clone(struct bpf_dynptr_kern *ptr,
+                                struct bpf_dynptr_kern *clone__uninit)

I think most of uses for bpf_dynptr_clone() would be to get a partial view (like you mentioned above, to, e.g., do a hashing of a part of the memory range). So it feels it would be best UX if clone would allow you to define a new range in one go. So e.g., to create a "sub-dynptr" for range of bytes [10, 30), it should be enough to:

struct bpf_dynptr orig_ptr, new_ptr;

Quoted 8 lines

bpf_dynptr_clone(&orig_ptr, 10, 30, &new_ptr);

Instead of:

bpf_dynptr_clone(&orig_ptr, &new_ptr); bpf_dynptr_advance(&new_ptr, 10); bpf_dynptr_trim(&new_ptr, bpf_dynptr_get_size(&orig_ptr) - 30);

I don't think we can do this because we can't have bpf_dynptr_clone() fail (which might happen if we take in a range, if the range is invalid). This is because in the case where the clone is of a reference-counted dynptr (eg like a ringbuf-type dynptr), the clone even if it's invalid must be treated by the verifier as a legit dynptr (since the verifier can't know ahead of time whether the clone call will succeed or not) which means if the invalid clone dynptr is then passed into a reference-releasing function, the verifier will release the reference but the actual reference won't be released at runtime (since the clone dynptr is invalid), which would leak the reference. An example is something like:

Quoted 8 lines

// invalid range is passed, error is returned and new_ptr is invalid
bpf_dynptr_clone(&ringbuf_ptr, 9999999, 9999999, &new_ptr);
// this releases the reference and invalidates both new_ptr and ringbuf_ptr bpf_ringbuf_discard_dynptr(&new_ptr, 0);

At runtime, bpf_ringbuf_discard_dynptr() will be a no-op since new_ptr is invalid - the ringbuf record still needs to be submitted/discarded, but the verifier will think this already happened

Ah, tricky, good point. But ok, I guess with bpf_dynptr_adjust() proposal in another email this would be ok:

bpf_dynptr_clone(..); /* always succeeds */ bpf_dynptr_adjust(&new_ptr, 10, 30); /* could fail to adjust, but dynptr is left valid */

Quoted 5 lines
Quoted 1 line

Right?

Yes, this would be okay because if bpf_dynptr_adjust fails, the clone is valid (it was unaffected) so submitting/discarding it later on would correctly release the reference

ok, cool, let's do that and keep things simple

Quoted 53 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

This, btw, shows the awkwardness of the bpf_dynptr_trim() approach.

If someone really wanted an exact full-sized copy, it's trivial:

bpf_dynptr_clone(&orig_ptr, 0, bpf_dynptr_get_size(&orig_ptr), &new_ptr);

[...]

Quoted 34 lines
Quoted 26 lines
Quoted 17 lines
Quoted 4 lines
kernel/bpf/helpers.c +4 -0
+static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
+{
+       struct bpf_func_state *state = func(env, reg);
+       int spi;

kernel/bpf/helpers.c

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+
+       spi = dynptr_get_spi(env, reg);
+       if (spi < 0)
+               return spi;
+
+       if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) {
+               int ref_obj_id = state->stack[spi].spilled_ptr.ref_obj_id;
+               int i;
+
+               /* If the dynptr has a ref_obj_id, then we need to invaldiate

typo: invalidate

Quoted 9 lines

kernel/bpf/helpers.c

Quoted 4 lines
kernel/bpf/helpers.c +4 -0
+                * two things:
+                *
+                * 1) Any dynptrs with a matching ref_obj_id (clones)
+                * 2) Any slices associated with the ref_obj_id

I think this is where this dynptr_id confusion comes from. The rule should be "any slices derived from this dynptr". But as mentioned on another thread, it's a separate topic which we can address later.

Quoted 7 lines

If there's a parent and a clone and slices derived from the parent and slices derived from the clone, if the clone is invalidated then we need to invalidate slices associated with the parent as well. So shouldn't it be "any slices associated with the ref obj id" not "any slices derived from this dynptr"? (also just a note, parent/clone slices will share the same ref obj id and the same dynptr id, so checking against either does the same thing)

Quoted 8 lines

So, we have a ringbuf dynptr with ref_obj_id=1, id=2, ok? We clone it, clone gets ref_obj_id=1, id=3. If either original dynptr or clone dynptr is released due to bpf_ringbuf_discard_dynptr(), we invalidate all the dynptrs with ref_obj_id=1. During invalidation of each dynptr, we invalidate all the slices with ref_obj_id==dynptr's id. So we'll invalidate slices derived from dynptr with id=2 (original dynptr), and then all the slices derived from dynptr with id=3?

Quoted 6 lines

When we create a slice for a dynptr (through bpf_dynptr_data()), the slice's reg->ref_obj_id is set to the dynptr's ref_obj_id (not dynptr's id). During invalidation of the dynptr, we invalidate all the slices with that ref obj id, which means we invalidate all slices for any parents/clones.
[...]

Yep, because of how we define that ref_obj_id should be in refs array. What I'm saying is that we should probably change that to be more general "ID of an object which lifetime we are associated with". That would need some verifier internals adjustments.

So let's wrap up this particular ref_obj_id revamp discussion for now, it's a separate topic that will be just distracting us.

Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 6 lines
kernel/bpf/helpers.c +6 -0
+
+       err = process_dynptr_func(env, regno, insn_idx, arg_type);
+       if (err < 0)
+               return err;
+
+       spi = dynptr_get_spi(env, reg);

kernel/bpf/helpers.c

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       if (spi < 0)
+               return spi;
+
+       first_reg_state = &state->stack[spi].spilled_ptr;
+       second_reg_state = &state->stack[spi - 1].spilled_ptr;
+       ref_obj_id = first_reg_state->ref_obj_id;
+
+       /* reassign the clone the same dynptr id as the original */
+       __mark_dynptr_reg(first_reg_state, dynptr_type, true, meta->initialized_dynptr.id);
+       __mark_dynptr_reg(second_reg_state, dynptr_type, false, meta->initialized_dynptr.id);

I'm not sure why clone should have the same dynptr_id? Isn't it a new instance of a dynptr? I get preserving ref_obj_id (if refcounted), but why reusing dynptr_id?..

Quoted 9 lines
Quoted 4 lines

I think we need to also copy over the dynptr id because in the case of a non-reference counted dynptr, if the parent (or clone) is invalidated (eg overwriting bytes of the dynptr on the stack), we must also invalidate the slices of the clone (or parent)

yep, right now we'll have to do that because we have dynptr_id. But if we get rid of it and stick to ref_obj_id and id, then clone would need to get a new id, but keep ref_obj_id, right?

Quoted 6 lines

Thinking about this some more, I think you're right that we shouldn't reuse the dynptr id. in fact, i think reusing it would lead to incorrect behavior - in the example of a non-reference counted dynptr, if the parent dynptr is overwritten on the stack (and thus invalidated), that shouldn't invalidate the slices of the clone at all. I'll change this in the next version

ok

Quoted 2 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

[...]

#19 lore raw

Re: [PATCH v1 bpf-next 4/5] bpf: Add bpf_dynptr_clone

In reply to Joanne Koong
kernel test robot noticed the following build warnings:

Hi Joanne,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Joanne-Koong/bpf-Add-bpf_dynptr_trim-and-bpf_dynptr_advance/20230409-113652
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230409033431.3992432-5-joannelkoong%40gmail.com
patch subject: [PATCH v1 bpf-next 4/5] bpf: Add bpf_dynptr_clone
config: m68k-randconfig-s032-20230416 (https://download.01.org/0day-ci/archive/20230418/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 12.1.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-39-gce1a6720-dirty
# https://github.com/intel-lab-lkp/linux/commit/d7830addcc26375f56b68655ddbfb44116b3e7f6
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Joanne-Koong/bpf-Add-bpf_dynptr_trim-and-bpf_dynptr_advance/20230409-113652
git checkout d7830addcc26375f56b68655ddbfb44116b3e7f6
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=m68k olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=m68k SHELL=/bin/bash kernel/bpf/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> kernel/bpf/verifier.c:7020:41: sparse: sparse: mixing different enum types:
>> kernel/bpf/verifier.c:7020:41: sparse:    unsigned int enum bpf_arg_type
>> kernel/bpf/verifier.c:7020:41: sparse:    unsigned int enum bpf_type_flag

kernel/bpf/verifier.c:18042:38: sparse: sparse: subtraction of functions? Share your drugs kernel/bpf/verifier.c: note: in included file (through include/linux/bpf.h, include/linux/bpf-cgroup.h):
include/linux/bpfptr.h:65:40: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:65:40: sparse: sparse: cast from non-scalar include/linux/bpfptr.h:65:40: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:65:40: sparse: sparse: cast from non-scalar include/linux/bpfptr.h:65:40: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:65:40: sparse: sparse: cast from non-scalar

vim +7020 kernel/bpf/verifier.c

7006
7007 static int handle_dynptr_clone(struct bpf_verifier_env *env, enum bpf_arg_type arg_type,
7008 int regno, int insn_idx, struct bpf_kfunc_call_arg_meta *meta)
7009 {
7010 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
7011 struct bpf_reg_state *first_reg_state, *second_reg_state;
7012 struct bpf_func_state *state = func(env, reg);
7013 enum bpf_dynptr_type dynptr_type = meta->initialized_dynptr.type;
7014 int err, spi, ref_obj_id;
7015
7016 if (!dynptr_type) {
7017 verbose(env, "verifier internal error: no dynptr type for bpf_dynptr_clone\n");
7018 return -EFAULT;
7019 }

> 7020		arg_type |= get_dynptr_type_flag(dynptr_type);

7021
7022 err = process_dynptr_func(env, regno, insn_idx, arg_type);
7023 if (err < 0)
7024 return err;
7025
7026 spi = dynptr_get_spi(env, reg);
7027 if (spi < 0)
7028 return spi;
7029
7030 first_reg_state = &state->stack[spi].spilled_ptr;
7031 second_reg_state = &state->stack[spi - 1].spilled_ptr;
7032 ref_obj_id = first_reg_state->ref_obj_id;
7033
7034 /* reassign the clone the same dynptr id as the original */
7035 __mark_dynptr_reg(first_reg_state, dynptr_type, true, meta->initialized_dynptr.id);
7036 __mark_dynptr_reg(second_reg_state, dynptr_type, false, meta->initialized_dynptr.id);
7037
7038 if (meta->initialized_dynptr.ref_obj_id) {
7039 /* release the new ref obj id assigned during process_dynptr_func */
7040 err = release_reference_state(cur_func(env), ref_obj_id);
7041 if (err)
7042 return err;
7043 /* reassign the clone the same ref obj id as the original */
7044 first_reg_state->ref_obj_id = meta->initialized_dynptr.ref_obj_id;
7045 second_reg_state->ref_obj_id = meta->initialized_dynptr.ref_obj_id;
7046 }
7047
7048 return 0;
7049 }
7050

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#20 lore raw

PATCH 5/5 selftests/bpf: add tests for dynptr convenience helpers

In reply to Joanne Koong

Add various tests for the added dynptr convenience helpers.

---
 tools/testing/selftests/bpf/bpf_kfuncs.h      |   8 +
 .../testing/selftests/bpf/prog_tests/dynptr.c |   6 +
 .../testing/selftests/bpf/progs/dynptr_fail.c | 313 +++++++++++++++++
 .../selftests/bpf/progs/dynptr_success.c      | 320 ++++++++++++++++++
 4 files changed, 647 insertions(+)
tools/testing/selftests/bpf/bpf_kfuncs.h +8 -0
index 8c993ec8ceea..f62f5acde611 100644
@@ -35,4 +35,12 @@ extern void *bpf_dynptr_slice(const struct bpf_dynptr *ptr, __u32 offset,
 extern void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr *ptr, __u32 offset,
 			      void *buffer, __u32 buffer__szk) __ksym;
 
+extern int bpf_dynptr_trim(struct bpf_dynptr *ptr, __u32 len) __ksym;
+extern int bpf_dynptr_advance(struct bpf_dynptr *ptr, __u32 len) __ksym;
+extern int bpf_dynptr_is_null(const struct bpf_dynptr *ptr) __ksym;
+extern int bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym;
+extern __u32 bpf_dynptr_get_size(const struct bpf_dynptr *ptr) __ksym;
+extern __u32 bpf_dynptr_get_offset(const struct bpf_dynptr *ptr) __ksym;
+extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym;
+
 #endif
tools/testing/selftests/bpf/prog_tests/dynptr.c +6 -0
index d176c34a7d2e..198850fc8982 100644
@@ -20,6 +20,12 @@ static struct {
 	{"test_ringbuf", SETUP_SYSCALL_SLEEP},
 	{"test_skb_readonly", SETUP_SKB_PROG},
 	{"test_dynptr_skb_data", SETUP_SKB_PROG},
+	{"test_advance_trim", SETUP_SYSCALL_SLEEP},
+	{"test_advance_trim_err", SETUP_SYSCALL_SLEEP},
+	{"test_zero_size_dynptr", SETUP_SYSCALL_SLEEP},
+	{"test_dynptr_is_null", SETUP_SYSCALL_SLEEP},
+	{"test_dynptr_is_rdonly", SETUP_SKB_PROG},
+	{"test_dynptr_clone", SETUP_SKB_PROG},
 };
 
 static void verify_success(const char *prog_name, enum test_setup_type setup_type)
tools/testing/selftests/bpf/progs/dynptr_fail.c +313 -0

316 lines not on this page · raw message

tools/testing/selftests/bpf/progs/dynptr_success.c +320 -0

323 lines not on this page · raw message

Signature · 1 line
2.34.1
#21 lore raw
In reply to Joanne Koong
Quoted 7 lines · Joanne Koong

This patchset is the 3rd in the dynptr series. The 1st (dynptr fundamentals) can be found here [0] and the second (skb + xdp dynptrs) can be found here [1].

This patchset adds the following convenience helpers for interacting with dynptrs:

"convenience helpers" doesn't do these APIs justice. They are necessary APIs to make dynptrs a generic interface for working with memory ranges. So it's dynptr cloning and adjustment helpers, but not just a convenience APIs.

Quoted 32 lines

int bpf_dynptr_trim(struct bpf_dynptr *ptr, __u32 len) __ksym;
int bpf_dynptr_advance(struct bpf_dynptr *ptr, __u32 len) __ksym;
int bpf_dynptr_is_null(const struct bpf_dynptr *ptr) __ksym;
int bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym;
__u32 bpf_dynptr_get_size(const struct bpf_dynptr *ptr) __ksym;
__u32 bpf_dynptr_get_offset(const struct bpf_dynptr *ptr) __ksym;
int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym;

[0] https://lore.kernel.org/bpf/[email protected]/
[1] https://lore.kernel.org/bpf/[email protected]/

Joanne Koong (5):
bpf: Add bpf_dynptr_trim and bpf_dynptr_advance
bpf: Add bpf_dynptr_is_null and bpf_dynptr_is_rdonly
bpf: Add bpf_dynptr_get_size and bpf_dynptr_get_offset
bpf: Add bpf_dynptr_clone
selftests/bpf: add tests for dynptr convenience helpers

 include/linux/bpf.h                           |   2 +-
 kernel/bpf/helpers.c                          | 108 +++++-
 kernel/bpf/verifier.c                         | 125 ++++++-
 kernel/trace/bpf_trace.c                      |   4 +-
 tools/testing/selftests/bpf/bpf_kfuncs.h      |   8 +
 .../testing/selftests/bpf/prog_tests/dynptr.c |   6 +
 .../testing/selftests/bpf/progs/dynptr_fail.c | 313 +++++++++++++++++
 .../selftests/bpf/progs/dynptr_success.c      | 320 ++++++++++++++++++
 8 files changed, 864 insertions(+), 22 deletions(-)

--
2.34.1