bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next 1/2] bpf: add bpf_dynptr_memset() kfunc
@ 2025-06-18 22:33 Ihor Solodrai
  2025-06-18 22:33 ` [PATCH bpf-next 2/2] selftests/bpf: add test cases for bpf_dynptr_memset() Ihor Solodrai
  2025-06-18 23:44 ` [PATCH bpf-next 1/2] bpf: add bpf_dynptr_memset() kfunc Mykyta Yatsenko
  0 siblings, 2 replies; 14+ messages in thread
From: Ihor Solodrai @ 2025-06-18 22:33 UTC (permalink / raw)
  To: andrii; +Cc: bpf, ast, daniel, eddyz87, mykolal, kernel-team

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@meta.com>
---
 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);
+
+	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
-- 
2.47.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2025-06-23 22:12 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-18 22:33 [PATCH bpf-next 1/2] bpf: add bpf_dynptr_memset() kfunc Ihor Solodrai
2025-06-18 22:33 ` [PATCH bpf-next 2/2] selftests/bpf: add test cases for bpf_dynptr_memset() Ihor Solodrai
2025-06-18 23:44 ` [PATCH bpf-next 1/2] bpf: add bpf_dynptr_memset() kfunc Mykyta Yatsenko
2025-06-19 17:09   ` Ihor Solodrai
2025-06-19 17:19     ` Eduard Zingerman
2025-06-19 17:55       ` Ihor Solodrai
2025-06-19 17:57         ` Eduard Zingerman
2025-06-19 18:04           ` Ihor Solodrai
2025-06-19 18:13             ` Eduard Zingerman
2025-06-19 18:17               ` Eduard Zingerman
2025-06-23 21:38                 ` Andrii Nakryiko
2025-06-23 21:45                   ` Eduard Zingerman
2025-06-23 22:12                     ` Andrii Nakryiko
2025-06-20 15:09       ` Mykyta Yatsenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).