diff mbox series

bpf_program__set_attach_target(prog, 0, "foo") cannot possibly work ?

Message ID CA+hQ2+hqMpmwHRxbeu20QBzC67BQ4UK+JvFV16JtnZ66+sHpCA@mail.gmail.com (mailing list archive)
State Not Applicable
Headers show
Series bpf_program__set_attach_target(prog, 0, "foo") cannot possibly work ? | expand

Commit Message

Luigi Rizzo Oct. 4, 2020, 7:02 p.m. UTC
Hi,
I am trying to use

   bpf_program__set_attach_target(prog,  0, "foo")

to override the attach point for a bpf program, and it seems that
it cannot possibly work because the function depends on
prog->obj->btf_vmlinux being !NULL.

The only place in libbpf that sets btf_vmlinux is this:

  2495 static int bpf_object__load_vmlinux_btf(struct bpf_object *obj)
  ...
  2517         obj->btf_vmlinux = libbpf_find_kernel_btf();

and this is only called within the function below, which also clears
the field once done.

  5890 int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
  ...
  5911         err = err ? : bpf_object__load_vmlinux_btf(obj);
  ...
  5917         btf__free(obj->btf_vmlinux);

I don't know exactly what is the plan with that field, hence what
is the best way to fix the problem. I can suggest a couple below:


index 7253b833576c..28288d4c992b 100644
                return btf_id;


---

Does the above make sense ?

cheers
luigi
diff mbox series

Patch

--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9128,10 +9128,18 @@  int bpf_program__set_attach_target(struct
bpf_program *prog,
        if (attach_prog_fd)
                btf_id = libbpf_find_prog_btf_id(attach_func_name,
                                                 attach_prog_fd);
-       else
-               btf_id = __find_vmlinux_btf_id(prog->obj->btf_vmlinux,
+       else {
+               struct btf *btf = prog->obj->btf_vmlinux;
+
+               if (!btf)
+                       btf = libbpf_find_kernel_btf();
+
+               btf_id = __find_vmlinux_btf_id(btf,
                                               attach_func_name,
                                               prog->expected_attach_type);
+               if (!prog->obj->btf_vmlinux)
+                       btf_free(btf);
+       }

        if (btf_id < 0)
                return btf_id;


or possibly even simpler

index 7253b833576c..a9870e9dc67a 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9128,10 +9128,14 @@  int bpf_program__set_attach_target(struct
bpf_program *prog,
        if (attach_prog_fd)
                btf_id = libbpf_find_prog_btf_id(attach_func_name,
                                                 attach_prog_fd);
-       else
-               btf_id = __find_vmlinux_btf_id(prog->obj->btf_vmlinux,
-                                              attach_func_name,
+       else {
+               struct btf *btf = libbpf_find_kernel_btf();
+
+               btf_id = __find_vmlinux_btf_id(btf, attach_func_name,
                                               prog->expected_attach_type);
+               if (btf)
+                       btf_free(btf);
+       }

        if (btf_id < 0)