Skip to contentSkip to conversation

libbpf: Fix expected_attach_type set when kernel not support

PATCH Superseded Tao Chen 4 participants 7 messages Sep 14, 2024 – Sep 25, 2024

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tao Chen

author
#0 lore raw

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

The commit "5902da6d8a52" set expected_attach_type again with field 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 | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

Change list:
- v2 -> v3:
- update BPF_TRACE_UPROBE_MULTI both in prog and opts suggedted by Andrri
- v1 -> v2:
- restore the original initialization way suggested by Jiri

tools/lib/bpf/libbpf.c +8 -4
index 219facd0e66e..a78e24ff354b 100644
@@ -7352,8 +7352,14 @@ static int libbpf_prepare_prog_load(struct bpf_program *prog,
 		opts->prog_flags |= BPF_F_XDP_HAS_FRAGS;
 
 	/* special check for usdt to use uprobe_multi link */
-	if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK))
+	if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) {
+		/* for BPF_TRACE_KPROBE_MULTI, user might want to query exected_attach_type
+		 * in prog, and expected_attach_type we set in kenrel is from opts, so we
+		 * update both.
+		 */
 		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 +7449,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 +7481,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,
Signature · 1 line
2.25.1
author
#1 lore raw
Quoted 48 lines · Tao Chen

The commit "5902da6d8a52" set expected_attach_type again with field 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 | 12 ++++++++----
  1 file changed, 8 insertions(+), 4 deletions(-)

Change list:
- v2 -> v3:
- update BPF_TRACE_UPROBE_MULTI both in prog and opts suggedted by Andrri
- v1 -> v2:
- restore the original initialization way suggested by Jiri

tools/lib/bpf/libbpf.c +8 -1
index 219facd0e66e..a78e24ff354b 100644
@@ -7352,8 +7352,14 @@ static int libbpf_prepare_prog_load(struct bpf_program *prog,
  		opts->prog_flags |= BPF_F_XDP_HAS_FRAGS;
 
  	/* special check for usdt to use uprobe_multi link */
-	if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK))
+	if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) {
+		/* for BPF_TRACE_KPROBE_MULTI, user might want to query exected_attach_type
+		 * in prog, and expected_attach_type we set in kenrel is from opts, so we
+		 * update both.
+		 */
  		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 +7449,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 7486

Quoted 9 lines
tools/lib/bpf/libbpf.c +0 -3
@@ -7474,9 +7481,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, guys, please review this patch again, the previous versions:
v1:
https://lore.kernel.org/bpf/[email protected]/
v2:
https://lore.kernel.org/bpf/[email protected]/

Signature · 2 lines
Best Regards
Dylane Chen
#2 lore raw
Quoted 53 lines · Tao Chen
Quoted 51 lines · Tao Chen

The commit "5902da6d8a52" set expected_attach_type again with field 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 | 12 ++++++++----
  1 file changed, 8 insertions(+), 4 deletions(-)

Change list:
- v2 -> v3:
- update BPF_TRACE_UPROBE_MULTI both in prog and opts suggedted by Andrri
- v1 -> v2:
- restore the original initialization way suggested by Jiri

tools/lib/bpf/libbpf.c +8 -1
index 219facd0e66e..a78e24ff354b 100644
@@ -7352,8 +7352,14 @@ static int libbpf_prepare_prog_load(struct bpf_program *prog,
              opts->prog_flags |= BPF_F_XDP_HAS_FRAGS;
 
      /* special check for usdt to use uprobe_multi link */
-     if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK))
+     if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) {
+             /* for BPF_TRACE_KPROBE_MULTI, user might want to query exected_attach_type
+              * in prog, and expected_attach_type we set in kenrel is from opts, so we
+              * update both.
+              */
              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 +7449,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 +7481,6 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
              insns_cnt = prog->insns_cnt;
      }
 
Quoted 8 lines

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

Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -3
-     /* 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, guys, please review this patch again, the previous versions:

It looks good, but bpf-next is closed right now due to merge window. I'll apply when the tree is open again.

Quoted 8 lines

v1:
https://lore.kernel.org/bpf/[email protected]/
v2:
https://lore.kernel.org/bpf/[email protected]/

--
Best Regards
Dylane Chen

author
#3 lore raw
Quoted 63 lines · Andrii Nakryiko
Quoted 53 lines · Tao Chen
Quoted 51 lines · Tao Chen

The commit "5902da6d8a52" set expected_attach_type again with field 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 | 12 ++++++++----
   1 file changed, 8 insertions(+), 4 deletions(-)

Change list:
- v2 -> v3:
- update BPF_TRACE_UPROBE_MULTI both in prog and opts suggedted by Andrri
- v1 -> v2:
- restore the original initialization way suggested by Jiri

tools/lib/bpf/libbpf.c +8 -1
index 219facd0e66e..a78e24ff354b 100644
@@ -7352,8 +7352,14 @@ static int libbpf_prepare_prog_load(struct bpf_program *prog,
               opts->prog_flags |= BPF_F_XDP_HAS_FRAGS;
 
       /* special check for usdt to use uprobe_multi link */
-     if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK))
+     if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) {
+             /* for BPF_TRACE_KPROBE_MULTI, user might want to query exected_attach_type
+              * in prog, and expected_attach_type we set in kenrel is from opts, so we
+              * update both.
+              */
               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 +7449,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 +7481,6 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
               insns_cnt = prog->insns_cnt;
       }
 
Quoted 8 lines

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

Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -3
-     /* 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, guys, please review this patch again, the previous versions:

Quoted 2 lines

It looks good, but bpf-next is closed right now due to merge window. I'll apply when the tree is open again.

Hi,Andrii, thank you for your response! I’ll wait for the next window.

Quoted 9 lines
Quoted 9 lines

v1:
https://lore.kernel.org/bpf/[email protected]/
v2:
https://lore.kernel.org/bpf/[email protected]/

--
Best Regards
Dylane Chen

Signature · 2 lines
Best Regards
Dylane Chen
Acked-by on v3 1/1
#4 lore raw
In reply to Tao Chen
Quoted 24 lines · Tao Chen

The commit "5902da6d8a52" set expected_attach_type again with field 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 | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

Change list:
- v2 -> v3:
- update BPF_TRACE_UPROBE_MULTI both in prog and opts suggedted by Andrri
- v1 -> v2:
- restore the original initialization way suggested by Jiri

tools/lib/bpf/libbpf.c +0 -0
index 219facd0e66e..a78e24ff354b 100644

tools/lib/bpf/libbpf.c static int libbpf_prepare_prog_load(struct bpf_program *prog, · line 7359

Quoted 9 lines
tools/lib/bpf/libbpf.c +5 -1
@@ -7352,8 +7352,14 @@ static int libbpf_prepare_prog_load(struct bpf_program *prog,
 		opts->prog_flags |= BPF_F_XDP_HAS_FRAGS;
 
 	/* special check for usdt to use uprobe_multi link */
-	if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK))
+	if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) {
+		/* for BPF_TRACE_KPROBE_MULTI, user might want to query exected_attach_type
+		 * in prog, and expected_attach_type we set in kenrel is from opts, so we
+		 * update both.
+		 */

s/K/U/ in BPF_TRACE_KPROBE_MULTI in above comment and 'kenrel' typo

thanks,
jirka

Quoted 27 lines
tools/lib/bpf/libbpf.c +3 -3
 		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 +7449,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 +7481,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,
Signature · 1 line
2.25.1
maintainer
#5 lore raw
Quoted 36 lines · Jiri Olsa
Quoted 24 lines · Tao Chen

The commit "5902da6d8a52" set expected_attach_type again with field 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 | 12 ++++++++----
  1 file changed, 8 insertions(+), 4 deletions(-)

Change list:
- v2 -> v3:
- update BPF_TRACE_UPROBE_MULTI both in prog and opts suggedted by Andrri
- v1 -> v2:
- restore the original initialization way suggested by Jiri

tools/lib/bpf/libbpf.c +0 -0
index 219facd0e66e..a78e24ff354b 100644

tools/lib/bpf/libbpf.c static int libbpf_prepare_prog_load(struct bpf_program *prog, · line 7359

Quoted 9 lines
tools/lib/bpf/libbpf.c +5 -1
@@ -7352,8 +7352,14 @@ static int libbpf_prepare_prog_load(struct bpf_program *prog,
  		opts->prog_flags |= BPF_F_XDP_HAS_FRAGS;
 
  	/* special check for usdt to use uprobe_multi link */
-	if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK))
+	if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) {
+		/* for BPF_TRACE_KPROBE_MULTI, user might want to query exected_attach_type
+		 * in prog, and expected_attach_type we set in kenrel is from opts, so we
+		 * update both.
+		 */

s/K/U/ in BPF_TRACE_KPROBE_MULTI in above comment and 'kenrel' typo

Quoted 1 line

Same typo is also in commit desc, would be good to improve the commit desc a bit if you spin v4 anyway. Thanks!

author
#6 lore raw
Quoted 41 lines · Daniel Borkmann
Quoted 40 lines · Jiri Olsa
Quoted 32 lines · Tao Chen

The commit "5902da6d8a52" set expected_attach_type again with field 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 | 12 ++++++++----

  1 file changed, 8 insertions(+), 4 deletions(-)

Change list:
- v2 -> v3:
     - update BPF_TRACE_UPROBE_MULTI both in prog and opts suggedted by Andrri
- v1 -> v2:
     - restore the original initialization way suggested by Jiri

tools/lib/bpf/libbpf.c +2 -1
index 219facd0e66e..a78e24ff354b 100644
@@ -7352,8 +7352,14 @@ static int libbpf_prepare_prog_load(struct 
 bpf_program *prog,
           opts->prog_flags |= BPF_F_XDP_HAS_FRAGS;
       /* special check for usdt to use uprobe_multi link */
-    if ((def & SEC_USDT) && kernel_supports(prog->obj, 
 FEAT_UPROBE_MULTI_LINK))
+    if ((def & SEC_USDT) && kernel_supports(prog->obj, 
 FEAT_UPROBE_MULTI_LINK)) {
+        /* for BPF_TRACE_KPROBE_MULTI, user might want to query 
Quoted 5 lines

exected_attach_type
+         * in prog, and expected_attach_type we set in kenrel is from opts, so we
+         * update both.
+         */

s/K/U/ in BPF_TRACE_KPROBE_MULTI in above comment and 'kenrel' typo

Quoted 3 lines
Quoted 1 line

Same typo is also in commit desc, would be good to improve the commit desc a bit if you spin v4 anyway. Thanks!

Hi,Dnaiel,my bad,i will fix it in v4

Signature · 2 lines
Best Regards
Dylane Chen