bpf: add bpf_dynptr_memset() kfunc
Subsystems (3)
v3 cover bpf: add bpf_dynptr_memset() kfunc
Implement bpf_dynptr_memset() kfunc and add tests for it.
v2->v3:
* nits and slow-path loop rewrite (Andrii)
* simplify xdp chunks test (Mykyta)v1->v2:
* handle non-linear buffers with bpf_dynptr_write()
* change function signature to include offset arg
* add more test cases
v2: https://lore.kernel.org/bpf/[email protected]/
v1: https://lore.kernel.org/bpf/[email protected]/
Ihor Solodrai (2):
bpf: add bpf_dynptr_memset() kfunc selftests/bpf: add test cases for bpf_dynptr_memset()
kernel/bpf/helpers.c | 47 ++++++ .../testing/selftests/bpf/prog_tests/dynptr.c | 8 + .../selftests/bpf/progs/dynptr_success.c | 158 ++++++++++++++++++ 3 files changed, 213 insertions(+)
Signature · 1 line
2.47.1
PATCH v3 1/2 bpf: add bpf_dynptr_memset() kfunc
Currently there is no straightforward way to fill dynptr memory with a value (most commonly zero). One can do it with bpf_dynptr_write(), but a temporary buffer is necessary for that.
Implement bpf_dynptr_memset() - an analogue of memset() from libc.
--- kernel/bpf/helpers.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
kernel/bpf/helpers.c +47 -0
@@ -2907,6 +2907,52 @@ __bpf_kfunc int bpf_dynptr_copy(struct bpf_dynptr *dst_ptr, u32 dst_off,return 0 ; } +/** +* bpf_dynptr_memset() - Fill dynptr memory with a constant byte. +* @ptr: Destination dynptr - where data will be filled +* @offset: Offset into the dynptr to start filling from +* @size: Number of bytes to fill +* @val: Constant byte to fill the memory with +* +* Fills the size bytes of the memory area pointed to by ptr +* at offset with the constant byte val. +* Returns 0 on success; negative error, otherwise. +*/ + __bpf_kfunc int bpf_dynptr_memset(struct bpf_dynptr *ptr, u32 offset, u32 size, u8 val) + { +struct bpf_dynptr_kern *p = (struct bpf_dynptr_kern *)ptr; +u32 chunk_sz, write_off; +char buf[256 ]; +void * slice; +int err; + +if (__bpf_dynptr_is_rdonly (p)) +return -EINVAL ; + + err =bpf_dynptr_check_off_len (p, offset, size); +if (err) +return err; + + slice =bpf_dynptr_slice_rdwr (ptr, offset,NULL , size); +if (likely (slice)) { +memset (slice, val, size); +return 0 ; + } + +/* Non-linear data under the dynptr, write from a local buffer */ + chunk_sz =min_t (u32,sizeof (buf), size); +memset (buf, val, chunk_sz); + +for (write_off =0 ; write_off < size; write_off += chunk_sz) { + chunk_sz =min_t (u32,sizeof (buf), size - write_off); + err =__bpf_dynptr_write (p, offset + write_off, buf, chunk_sz,0 ); +if (err) +return err; + } + +return 0 ; +} + __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj) {return obj;
@@ -3735,6 +3781,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) BTF_ID_FLAGS(func, bpf_dynptr_size) BTF_ID_FLAGS(func, bpf_dynptr_clone) BTF_ID_FLAGS(func, bpf_dynptr_copy) +BTF_ID_FLAGS(func, bpf_dynptr_memset)#ifdef CONFIG_NET BTF_ID_FLAGS(func, bpf_modify_return_test_tp)#endif
Signature · 1 line
2.47.1
Quoted 18 lines · Ihor Solodrai
Currently there is no straightforward way to fill dynptr memory with a value (most commonly zero). One can do it with
bpf_dynptr_write(), but a temporary buffer is necessary for that.Implement
bpf_dynptr_memset()- an analogue ofmemset()from libc.Signed-off-by Ihor Solodrai <[email protected]>--- kernel/bpf/helpers.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)kernel/bpf/helpers.c +0
-0@@ -2907,6 +2907,52 @@ __bpf_kfunc int bpf_dynptr_copy(struct bpf_dynptr *dst_ptr, u32 dst_off,return 0 ;
kernel/bpf/helpers.c __bpf_kfunc int bpf_dynptr_copy(struct bpf_dynptr *dst_ptr, u32 dst_off, · line 2917
Quoted 10 lines
kernel/bpf/helpers.c +8
-0} +/** +* bpf_dynptr_memset() - Fill dynptr memory with a constant byte. +* @ptr: Destination dynptr - where data will be filled +* @offset: Offset into the dynptr to start filling from +* @size: Number of bytes to fill +* @val: Constant byte to fill the memory with +* +* Fills the size bytes of the memory area pointed to by ptr
nit: looking at other doc comments, you should use @size when referring to parameter (same for @ptr, though nit-within-nit: we seem to be using "p" for similar functions)
Quoted 13 lines
kernel/bpf/helpers.c +13
-0+* at offset with the constant byte val. +* Returns 0 on success; negative error, otherwise. +*/ + __bpf_kfunc int bpf_dynptr_memset(struct bpf_dynptr *ptr, u32 offset, u32 size, u8 val) + { +struct bpf_dynptr_kern *p = (struct bpf_dynptr_kern *)ptr; +u32 chunk_sz, write_off; +char buf[256 ]; +void * slice; +int err; + +if (__bpf_dynptr_is_rdonly (p)) +return -EINVAL ;
kernel/bpf/helpers.c __bpf_kfunc int bpf_dynptr_copy(struct bpf_dynptr *dst_ptr, u32 dst_off, · line 2940
Quoted 10 lines
kernel/bpf/helpers.c +10
-0+ + err =bpf_dynptr_check_off_len (p, offset, size); +if (err) +return err; + + slice =bpf_dynptr_slice_rdwr (ptr, offset,NULL , size); +if (likely (slice)) { +memset (slice, val, size); +return 0 ; + }
I think what (I believe, Mykyta) was saying is that by doing
slice = bpf_dynptr_slice_rdwr(...);
if (likely(slice)) {
memset(...);
return 0;
}if (__bpf_dynptr_is_rdonly(p))
return -EINVAL;
err = bpf_dynptr_check_off_len(...);
if (err)
return err;
we have the fastest possible expected happy case, and we'll do all the sanity checking if happy path fails either due to non-contiguity of memory or due to invalid size/offset/read-write permission
pw-bot: cr
Quoted 29 lines
kernel/bpf/helpers.c +16
-1+ +/* Non-linear data under the dynptr, write from a local buffer */ + chunk_sz =min_t (u32,sizeof (buf), size); +memset (buf, val, chunk_sz); + +for (write_off =0 ; write_off < size; write_off += chunk_sz) { + chunk_sz =min_t (u32,sizeof (buf), size - write_off); + err =__bpf_dynptr_write (p, offset + write_off, buf, chunk_sz,0 ); +if (err) +return err; + } + +return 0 ; +} + __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj) {return obj;@@ -3735,6 +3781,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) BTF_ID_FLAGS(func, bpf_dynptr_size) BTF_ID_FLAGS(func, bpf_dynptr_clone) BTF_ID_FLAGS(func, bpf_dynptr_copy) +BTF_ID_FLAGS(func, bpf_dynptr_memset)#ifdef CONFIG_NET BTF_ID_FLAGS(func, bpf_modify_return_test_tp)#endif --2.47.1
PATCH v3 2/2 selftests/bpf: add test cases for bpf_dynptr_memset()
In reply to Ihor SolodraiAdd tests to verify the behavior of bpf_dynptr_memset():
* normal memset 0
* normal memset non-0
* memset with an offset
* memset in dynptr that was adjusted
* error: size overflow
* error: offset+size overflow
* error: readonly dynptr
* memset into non-linear xdp dynptr
--- .../testing/selftests/bpf/prog_tests/dynptr.c | 8 + .../selftests/bpf/progs/dynptr_success.c | 158 ++++++++++++++++++ 2 files changed, 166 insertions(+)
tools/testing/selftests/bpf/prog_tests/dynptr.c +8 -0
@@ -21,6 +21,14 @@ static struct { {"test_dynptr_data", SETUP_SYSCALL_SLEEP}, {"test_dynptr_copy", SETUP_SYSCALL_SLEEP}, {"test_dynptr_copy_xdp", SETUP_XDP_PROG}, + {"test_dynptr_memset_zero", SETUP_SYSCALL_SLEEP}, + {"test_dynptr_memset_notzero", SETUP_SYSCALL_SLEEP}, + {"test_dynptr_memset_zero_offset", SETUP_SYSCALL_SLEEP}, + {"test_dynptr_memset_zero_adjusted", SETUP_SYSCALL_SLEEP}, + {"test_dynptr_memset_overflow", SETUP_SYSCALL_SLEEP}, + {"test_dynptr_memset_overflow_offset", SETUP_SYSCALL_SLEEP}, + {"test_dynptr_memset_readonly", SETUP_SKB_PROG}, + {"test_dynptr_memset_xdp_chunks", SETUP_XDP_PROG}, {"test_ringbuf", SETUP_SYSCALL_SLEEP}, {"test_skb_readonly", SETUP_SKB_PROG}, {"test_dynptr_skb_data", SETUP_SKB_PROG},
tools/testing/selftests/bpf/progs/dynptr_success.c +158 -0
164 lines not on this page · raw message
Signature · 1 line
2.47.1