Skip to contentSkip to conversation

libbpf: Fix expected_attach_type set when kernel not support

PATCH Superseded Tao Chen 2 participants 3 messages Sep 13, 2024

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tao Chen

author
#0 lore raw

PATCH 1/1 libbpf: Fix expected_attach_type set when kernel not support

The commit "5902da6d8a52" set expected_attach_type again with filed of bpf_program after libpf_prepare_prog_load, which makes expected_attach_type = 0 no sense when kenrel not support the attach_type feature, so fix it.

Fixes 5902da6d8a52 ("libbpf: Add uprobe multi link support to bpf_program__attach_usdt")
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
index 219facd0e66e..9035edf763a3 100644
@@ -7343,7 +7343,7 @@ static int libbpf_prepare_prog_load(struct bpf_program *prog,
 
 	/* old kernels might not support specifying expected_attach_type */
 	if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE))
-		opts->expected_attach_type = 0;
+		prog->expected_attach_type = 0;
 
 	if (def & SEC_SLEEPABLE)
 		opts->prog_flags |= BPF_F_SLEEPABLE;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 27 lines · Tao Chen

The commit "5902da6d8a52" set expected_attach_type again with filed of bpf_program after libpf_prepare_prog_load, which makes expected_attach_type = 0 no sense when kenrel not support the attach_type feature, so fix it.

Fixes 5902da6d8a52 ("libbpf: Add uprobe multi link support to bpf_program__attach_usdt")
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
index 219facd0e66e..9035edf763a3 100644
@@ -7343,7 +7343,7 @@ static int libbpf_prepare_prog_load(struct bpf_program *prog,
 
 	/* old kernels might not support specifying expected_attach_type */
 	if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE))
-		opts->expected_attach_type = 0;
+		prog->expected_attach_type = 0;
 
 	if (def & SEC_SLEEPABLE)
 		opts->prog_flags |= BPF_F_SLEEPABLE;
Signature · 1 line
2.25.1

good catch! thanks

I can't remember why it was needed, perhaps we should go back to where it was before?

I'm guessing prog->expected_attach_type might not get updated properly and that might cause issues, not sure

thanks,
jirka

---

tools/lib/bpf/libbpf.c +2 -4
index 219facd0e66e..df2244397ba1 100644
@@ -7353,7 +7353,7 @@ static int libbpf_prepare_prog_load(struct bpf_program *prog,
 
 	/* special check for usdt to use uprobe_multi link */
 	if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK))
-		prog->expected_attach_type = BPF_TRACE_UPROBE_MULTI;
+		opts->expected_attach_type = BPF_TRACE_UPROBE_MULTI;
 
 	if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) {
 		int btf_obj_fd = 0, btf_type_id = 0, err;
@@ -7443,6 +7443,7 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
 	load_attr.attach_btf_id = prog->attach_btf_id;
 	load_attr.kern_version = kern_version;
 	load_attr.prog_ifindex = prog->prog_ifindex;
+	load_attr.expected_attach_type = prog->expected_attach_type;
 
 	/* specify func_info/line_info only if kernel supports them */
 	if (obj->btf && btf__fd(obj->btf) >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) {
@@ -7474,9 +7475,6 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
 		insns_cnt = prog->insns_cnt;
 	}
 
-	/* allow prog_prepare_load_fn to change expected_attach_type */
-	load_attr.expected_attach_type = prog->expected_attach_type;
-
 	if (obj->gen_loader) {
 		bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name,
 				   license, insns, insns_cnt, &load_attr,
author
#2 lore raw
Quoted 64 lines · Jiri Olsa
Quoted 27 lines · Tao Chen

The commit "5902da6d8a52" set expected_attach_type again with filed of bpf_program after libpf_prepare_prog_load, which makes expected_attach_type = 0 no sense when kenrel not support the attach_type feature, so fix it.

Fixes 5902da6d8a52 ("libbpf: Add uprobe multi link support to bpf_program__attach_usdt")
---
  tools/lib/bpf/libbpf.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
index 219facd0e66e..9035edf763a3 100644
@@ -7343,7 +7343,7 @@ static int libbpf_prepare_prog_load(struct bpf_program *prog,
 
  	/* old kernels might not support specifying expected_attach_type */
  	if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE))
-		opts->expected_attach_type = 0;
+		prog->expected_attach_type = 0;
 
  	if (def & SEC_SLEEPABLE)
  		opts->prog_flags |= BPF_F_SLEEPABLE;
Signature · 1 line
2.25.1

good catch! thanks

I can't remember why it was needed, perhaps we should go back to where it was before?

I'm guessing prog->expected_attach_type might not get updated properly and that might cause issues, not sure

thanks,
jirka

---

tools/lib/bpf/libbpf.c +2 -1
index 219facd0e66e..df2244397ba1 100644
@@ -7353,7 +7353,7 @@ static int libbpf_prepare_prog_load(struct bpf_program *prog,
 
  	/* special check for usdt to use uprobe_multi link */
  	if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK))
-		prog->expected_attach_type = BPF_TRACE_UPROBE_MULTI;
+		opts->expected_attach_type = BPF_TRACE_UPROBE_MULTI;
 
  	if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) {
  		int btf_obj_fd = 0, btf_type_id = 0, err;
@@ -7443,6 +7443,7 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
  	load_attr.attach_btf_id = prog->attach_btf_id;
  	load_attr.kern_version = kern_version;
  	load_attr.prog_ifindex = prog->prog_ifindex;
+	load_attr.expected_attach_type = prog->expected_attach_type;
 
  	/* specify func_info/line_info only if kernel supports them */
  	if (obj->btf && btf__fd(obj->btf) >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) {

tools/lib/bpf/libbpf.c static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog · line 7480

Quoted 9 lines
tools/lib/bpf/libbpf.c +0 -3
@@ -7474,9 +7475,6 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
  		insns_cnt = prog->insns_cnt;
  	}
 
-	/* allow prog_prepare_load_fn to change expected_attach_type */
-	load_attr.expected_attach_type = prog->expected_attach_type;
-
  	if (obj->gen_loader) {
  		bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name,
  				   license, insns, insns_cnt, &load_attr,

Hi, Jiri, thank you for your reply.
It looks better your way, i will send it in v2.

Signature · 2 lines
Best Regards
Dylane Chen