Skip to contentSkip to conversation

libbpf: never interpret subprogs in .text as entry programs

PATCH Applied Andrii Nakryiko 3 participants 4 messages Oct 10, 2024 – Oct 11, 2024

Subsystems (4)
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 1/2 libbpf: never interpret subprogs in .text as entry programs

Libbpf pre-1.0 had a legacy logic of allowing singular non-annotated (i.e., not having explicit SEC() annotation) function to be treated as sole entry BPF program (unless there were other explicit entry programs).

This behavior was dropped during libbpf 1.0 transition period (unless LIBBPF_STRICT_SEC_NAME flag was unset in libbpf_mode). When 1.0 was released and all the legacy behavior was removed, the bug slipped through leaving this legacy behavior around.

Fix this for good, as it actually causes very confusing behavior if BPF object file only has subprograms, but no entry programs.

Fixes bd054102a8c7 ("libbpf: enforce strict libbpf 1.0 behaviors")
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
index 05ad264ff09b..7c40286c3948 100644
@@ -4417,7 +4417,7 @@ static int bpf_object__collect_externs(struct bpf_object *obj)
 
 static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog)
 {
-	return prog->sec_idx == obj->efile.text_shndx && obj->nr_programs > 1;
+	return prog->sec_idx == obj->efile.text_shndx;
 }
 
 struct bpf_program *
Signature · 1 line
2.43.5
author maintainer
#1 lore raw

PATCH 2/2 selftests/bpf: add subprog to BPF object file with no entry programs

Add a subprogram to BPF object file that otherwise has no entry BPF programs to validate that libbpf can still load this correctly.

Until this was fixed, user could expect this very confusing error message:

libbpf: prog 'dangling_subprog': missing BPF prog type, check ELF section name '.text'
libbpf: prog 'dangling_subprog': failed to load: -22
libbpf: failed to load object 'struct_ops_detach'
libbpf: failed to load BPF skeleton 'struct_ops_detach': -22

---
 tools/testing/selftests/bpf/progs/struct_ops_detach.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/testing/selftests/bpf/progs/struct_ops_detach.c +6 -0
index 56b787a89876..5222d58592a7 100644
@@ -6,5 +6,11 @@
 
 char _license[] SEC("license") = "GPL";
 
+int dangling_subprog(void)
+{
+	/* do nothing, just be here */
+	return 0;
+}
+
 SEC(".struct_ops.link")
 struct bpf_testmod_ops testmod_do_detach;
Signature · 1 line
2.43.5
#2 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: add subprog to BPF object file with no entry programs

Quoted 21 lines · Andrii Nakryiko

Add a subprogram to BPF object file that otherwise has no entry BPF programs to validate that libbpf can still load this correctly.

Until this was fixed, user could expect this very confusing error message:

libbpf: prog 'dangling_subprog': missing BPF prog type, check ELF section name '.text'
libbpf: prog 'dangling_subprog': failed to load: -22
libbpf: failed to load object 'struct_ops_detach'
libbpf: failed to load BPF skeleton 'struct_ops_detach': -22

---
 tools/testing/selftests/bpf/progs/struct_ops_detach.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/testing/selftests/bpf/progs/struct_ops_detach.c +0 -0
index 56b787a89876..5222d58592a7 100644

tools/testing/selftests/bpf/progs/struct_ops_detach.c line 8

Quoted 3 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c +0 -0
@@ -6,5 +6,11 @@
 
 char _license[] SEC("license") = "GPL";
 

argh, I had this comment here, which I missed to amend into this patch, maybe whoever applies can just add it so I don't spam the mailing list:

/* This subprogram validates that libbpf handles the situation in which BPF
* object has subprograms in .text section, but has no entry BPF programs.
* At some point that was causing issues due to legacy logic of treating such
* subprogram as entry program (with unknown program type, which would fail). */

Quoted 11 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c +6 -1
+int dangling_subprog(void)
+{
+       /* do nothing, just be here */
+       return 0;
+}
+
 SEC(".struct_ops.link")
 struct bpf_testmod_ops testmod_do_detach;
--

2.43.5

#3 lore raw
In reply to Andrii Nakryiko
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 11 lines · On Thu, 10 Oct 2024 14:17:30 -0700 you wrote:

Libbpf pre-1.0 had a legacy logic of allowing singular non-annotated (i.e., not having explicit SEC() annotation) function to be treated as sole entry BPF program (unless there were other explicit entry programs).

This behavior was dropped during libbpf 1.0 transition period (unless LIBBPF_STRICT_SEC_NAME flag was unset in libbpf_mode). When 1.0 was released and all the legacy behavior was removed, the bug slipped through leaving this legacy behavior around.

[...]

Here is the summary with links:
- [bpf-next,1/2] libbpf: never interpret subprogs in .text as entry programs

    https://git.kernel.org/bpf/bpf-next/c/db089c9158c1
  - [bpf-next,2/2] selftests/bpf: add subprog to BPF object file with no entry programs
    https://git.kernel.org/bpf/bpf-next/c/82370ed5ade5

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html