Re: [PATCH bpf-next 1/2] bpf: add bpf_dynptr_memset() kfunc

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 6/18/25 4:44 PM, Mykyta Yatsenko wrote:
On 6/18/25 23:33, Ihor Solodrai wrote:
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.

Signed-off-by: Ihor Solodrai <isolodrai@xxxxxxxx>
---
  kernel/bpf/helpers.c | 28 ++++++++++++++++++++++++++++
  1 file changed, 28 insertions(+)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index b71e428ad936..dfd04628a522 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -2906,6 +2906,33 @@ __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
+ * @val: Constant byte to fill the memory with
+ * @n: Number of bytes to fill
+ *
+ * Fills the first n bytes of the memory area pointed to by ptr
+ * with the constant byte val.
+ * Returns 0 on success; negative error, otherwise.
+ */
+ __bpf_kfunc int bpf_dynptr_memset(struct bpf_dynptr *ptr, u8 val, u32 n)
+ {
+    struct bpf_dynptr_kern *p = (struct bpf_dynptr_kern *)ptr;
+    int err;
+
+    if (__bpf_dynptr_is_rdonly(p))
+        return -EINVAL;
+
+    err = bpf_dynptr_check_off_len(p, 0, n);
+    if (err)
+        return err;
+
+    memset(p->data + p->offset, val, n);
Do we need to handle non-contiguous buffers, similarly to bpf_dynptr_write (BPF_DYNPTR_TYPE_XDP case)?

It appears so, yes. I am a little lost on how exactly to do it though.

Looking at bpf_xdp_store_bytes [1]:

  BPF_CALL_4(bpf_xdp_store_bytes, struct xdp_buff *, xdp, u32, offset,
  	   void *, buf, u32, len)
  {
  	void *ptr;

  	ptr = bpf_xdp_pointer(xdp, offset, len);
  	if (IS_ERR(ptr))
  		return PTR_ERR(ptr);

  	if (!ptr)
  		bpf_xdp_copy_buf(xdp, offset, buf, len, true);
  	else
  		memcpy(ptr, buf, len);

  	return 0;
  }

  ...

int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len)
  {
  	return ____bpf_xdp_store_bytes(xdp, offset, buf, len);
  }

It looks like I'd need to implement similar logic for memset.

But then, does memset even make sense for xdp/skb buffers?
Maybe -ENOTSUPP is more appropriate?

I'd appreciate any hints.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/net/core/filter.c#n4084

+
+    return 0;
+}
+
  __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
  {
      return obj;
@@ -3364,6 +3391,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






[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux