Skip to contentSkip to conversation

libbpf: Add bpf_program__set_section_name api

RFC PATCH In review Tao Chen 2 participants 3 messages Mar 25, 2024 – Mar 26, 2024

Subsystems (2)
lore
Share & tools
lore

Tao Chen

author
#0 lore raw

RFC PATCH 1/1 libbpf: Add bpf_program__set_section_name api

The new api can be used to reset the function we want to trace in runtime. So we need not to change the code again when we just do minor changes to the trace function in kprobe or other ebpf event type. We can replace the old section with new section passed in via parameter. Maybe the following scenario we can use the api:

1. solve "*.isra.o" issue caused by compiler in new kernel obj = offcputime_bpf__open(); bpf_program__set_section_name(*(obj->skeleton->obj), "kprobe/finish_task_switch", "kprobe/finish_task_switch.isra.0");

2. dynamic adjustment for trace function offset
obj = offcputime_bpf__open(); bpf_program__set_section_name(*(obj->skeleton->obj), "kprobe/finish_task_switch+23", "kprobe/finish_task_switch+45");

---
 tools/lib/bpf/libbpf.c   | 24 ++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  2 ++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 27 insertions(+)
tools/lib/bpf/libbpf.c +24 -0
index 96ff1aa4bf6a..94f32e845c61 100644
@@ -8576,6 +8576,30 @@ const char *bpf_program__section_name(const struct bpf_program *prog)
 	return prog->sec_name;
 }
 
+int bpf_program__set_section_name(struct bpf_object *obj, const char *old_sec,
+				  const char *new_sec)
+{
+	struct bpf_program *prog = NULL;
+
+	if (obj == NULL || old_sec == NULL || new_sec == NULL)
+		return libbpf_err(-EINVAL);
+
+	do {
+		prog = bpf_object__next_program(obj, NULL);
+		if (prog) {
+			if (strcmp(prog->sec_name, old_sec) == 0) {
+				free(prog->sec_name);
+				prog->sec_name = strdup(new_sec);
+				if (!prog->sec_name)
+					return libbpf_err(-ENOMEM);
+				return 0;
+			}
+		}
+	} while (prog);
+
+	return libbpf_err(-ENOENT);
+}
+
 bool bpf_program__autoload(const struct bpf_program *prog)
 {
 	return prog->autoload;
tools/lib/bpf/libbpf.h +2 -0
index 0e52621cba43..eb620a9c8b8a 100644
@@ -307,6 +307,8 @@ LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
 
 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
+LIBBPF_API int bpf_program__set_section_name(struct bpf_object *obj, const char *old_sec,
+					     const char *new_sec);
 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
 LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);
tools/lib/bpf/libbpf.map +1 -0
index 57712321490f..14ac62cff605 100644
@@ -400,4 +400,5 @@ LIBBPF_1.3.0 {
 		bpf_program__attach_netfilter;
 		bpf_program__attach_tcx;
 		bpf_program__attach_uprobe_multi;
+		bpf_program__set_section_name;
 } LIBBPF_1.2.0;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 17 lines · Tao Chen

The new api can be used to reset the function we want to trace in runtime. So we need not to change the code again when we just do minor changes to the trace function in kprobe or other ebpf event type. We can replace the old section with new section passed in via parameter. Maybe the following scenario we can use the api:

1. solve "*.isra.o" issue caused by compiler in new kernel obj = offcputime_bpf__open(); bpf_program__set_section_name(*(obj->skeleton->obj), "kprobe/finish_task_switch", "kprobe/finish_task_switch.isra.0");

2. dynamic adjustment for trace function offset
obj = offcputime_bpf__open(); bpf_program__set_section_name(*(obj->skeleton->obj), "kprobe/finish_task_switch+23", "kprobe/finish_task_switch+45");

Quoted 7 lines
---
 tools/lib/bpf/libbpf.c   | 24 ++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  2 ++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 27 insertions(+)

This is a wrong approach. SEC() is immutable and serves as a hint to libbpf on program type and possibly some attach parameters. But after that libbpf allows to override all of them though APIs like bpf_program__set_type() and others. Attach APIs always allow to specify all the target parameters, including kprobe function name and so on.

Please check all of the libbpf APIs in libbpf.h.

pw-bot: cr

Quoted 8 lines
tools/lib/bpf/libbpf.c +0 -0
index 96ff1aa4bf6a..94f32e845c61 100644
@@ -8576,6 +8576,30 @@ const char *bpf_program__section_name(const struct bpf_program *prog)
        return prog->sec_name;
 }
 

[...]

author
#2 lore raw

Hi, Andrii, thank you for your reply. I read the apis again in libbpf.h, as you say, it seems that this patch no need. Thanks again.

Quoted 47 lines · Andrii Nakryiko
Quoted 17 lines · Tao Chen

The new api can be used to reset the function we want to trace in runtime. So we need not to change the code again when we just do minor changes to the trace function in kprobe or other ebpf event type. We can replace the old section with new section passed in via parameter. Maybe the following scenario we can use the api:

1. solve "*.isra.o" issue caused by compiler in new kernel obj = offcputime_bpf__open(); bpf_program__set_section_name(*(obj->skeleton->obj), "kprobe/finish_task_switch", "kprobe/finish_task_switch.isra.0");

2. dynamic adjustment for trace function offset
obj = offcputime_bpf__open(); bpf_program__set_section_name(*(obj->skeleton->obj), "kprobe/finish_task_switch+23", "kprobe/finish_task_switch+45");

Quoted 7 lines
---
  tools/lib/bpf/libbpf.c   | 24 ++++++++++++++++++++++++
  tools/lib/bpf/libbpf.h   |  2 ++
  tools/lib/bpf/libbpf.map |  1 +
  3 files changed, 27 insertions(+)

This is a wrong approach. SEC() is immutable and serves as a hint to libbpf on program type and possibly some attach parameters. But after that libbpf allows to override all of them though APIs like bpf_program__set_type() and others. Attach APIs always allow to specify all the target parameters, including kprobe function name and so on.

Please check all of the libbpf APIs in libbpf.h.

pw-bot: cr

Quoted 8 lines
tools/lib/bpf/libbpf.c +0 -0
index 96ff1aa4bf6a..94f32e845c61 100644
@@ -8576,6 +8576,30 @@ const char *bpf_program__section_name(const struct bpf_program *prog)
         return prog->sec_name;
  }
 

[...]