[RFC PATCH bpf-next 07/12] bpf: Record probe name in SDT map

From: Xu Kuohai

Date: Sat Jun 27 2026 - 10:56:18 EST


From: Xu Kuohai <xukuohai@xxxxxxxxxx>

When an SDT observer program is loaded, kernel needs to know the target
program and the target probe site. As user specifies probe site by probe
name, record probe name in SDT map for kernel to find the probe.

The max probe name length is set to 64. Programs with probe name longer
than 64 will be denied by libbpf.

Assisted-by: OpenCode:GLM-5.2
Signed-off-by: Xu Kuohai <xukuohai@xxxxxxxxxx>
---
include/uapi/linux/bpf.h | 2 ++
kernel/bpf/bpf_insn_array.c | 2 ++
tools/include/uapi/linux/bpf.h | 2 ++
tools/lib/bpf/libbpf.c | 7 +++++++
4 files changed, 13 insertions(+)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 773a7def0fbd..95ca41bf7501 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1521,6 +1521,7 @@ struct bpf_common_attr {
};

#define BPF_OBJ_NAME_LEN 16U
+#define BPF_SDT_MAX_NAME_LEN 64

enum {
BPF_STREAM_STDOUT = 1,
@@ -7743,6 +7744,7 @@ struct bpf_insn_array_value {
__u8 nargs; /* argument count (0..5) */
__u8 arg_reg[5]; /* BPF register for each argument */
__u8 pad[2];
+ char name[BPF_SDT_MAX_NAME_LEN]; /* probe name */
};

#endif /* _UAPI__LINUX_BPF_H__ */
diff --git a/kernel/bpf/bpf_insn_array.c b/kernel/bpf/bpf_insn_array.c
index fe30b08712ff..a9564ef4b2ef 100644
--- a/kernel/bpf/bpf_insn_array.c
+++ b/kernel/bpf/bpf_insn_array.c
@@ -3,6 +3,7 @@

#include <linux/bpf.h>
#include <linux/bpf_verifier.h>
+#include <linux/string.h>

struct bpf_insn_array {
struct bpf_map map;
@@ -107,6 +108,7 @@ static long insn_array_update_elem(struct bpf_map *map, void *key, void *value,
insn_array->values[index].btf_id = val.btf_id;
insn_array->values[index].nargs = val.nargs;
memcpy(insn_array->values[index].arg_reg, val.arg_reg, sizeof(val.arg_reg));
+ strscpy(insn_array->values[index].name, val.name, sizeof(val.name));
}

return 0;
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 773a7def0fbd..95ca41bf7501 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1521,6 +1521,7 @@ struct bpf_common_attr {
};

#define BPF_OBJ_NAME_LEN 16U
+#define BPF_SDT_MAX_NAME_LEN 64

enum {
BPF_STREAM_STDOUT = 1,
@@ -7743,6 +7744,7 @@ struct bpf_insn_array_value {
__u8 nargs; /* argument count (0..5) */
__u8 arg_reg[5]; /* BPF register for each argument */
__u8 pad[2];
+ char name[BPF_SDT_MAX_NAME_LEN]; /* probe name */
};

#endif /* _UAPI__LINUX_BPF_H__ */
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index badac128e8ad..dbc08a193101 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4146,6 +4146,12 @@ static int bpf_object__collect_sdt_notes(struct bpf_object *obj)
err = -ENOMEM;
goto out;
}
+ if (strlen(e->name) >= BPF_SDT_MAX_NAME_LEN) {
+ pr_warn("sdt: probe name '%s' too long (max %d chars)\n",
+ e->name, BPF_SDT_MAX_NAME_LEN - 1);
+ err = -EINVAL;
+ goto out;
+ }
e->sec_idx = sec_idx;
e->insn_idx = nop_idx;
e->nargs = nargs;
@@ -6843,6 +6849,7 @@ static int bpf_object__create_sdt_maps(struct bpf_object *obj)
val.nargs = e->nargs;
val.orig_off = e->insn_idx - prog->sec_insn_off;
memcpy(val.arg_reg, e->arg_reg, sizeof(val.arg_reg));
+ strncpy(val.name, e->name, sizeof(val.name) - 1);

err = bpf_map_update_elem(prog->sdt_map_fd, &key, &val, 0);
if (err)
--
2.47.3