BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v4 0/2] BPF static linker: fix linking duplicate extern functions
@ 2024-10-02  6:25 Eric Long via B4 Relay
  2024-10-02  6:25 ` [PATCH bpf-next v4 1/2] libbpf: do not resolve size on duplicate FUNCs Eric Long via B4 Relay
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Eric Long via B4 Relay @ 2024-10-02  6:25 UTC (permalink / raw)
  To: bpf; +Cc: Andrii Nakryiko, Alexei Starovoitov, netdev, Eric Long

Currently, if `bpftool gen object` tries to link two objects that
contains the same extern function prototype, libbpf will try to get
their (non-existent) size by calling bpf__resolve_size like extern
variables and fail with:

	libbpf: global 'whatever': failed to resolve size of underlying type: -22

This should not be the case, and this series adds conditions to update
size only when the BTF kind is not function.

Fixes: a46349227cd8 ("libbpf: Add linker extern resolution support for functions and global variables")
Signed-off-by: Eric Long <i@hack3r.moe>
---
Changes in v4:
- Remove redundant FUNC_PROTO check.
- Merge tests into linked_funcs.
- Link to v3: https://lore.kernel.org/r/20241001-libbpf-dup-extern-funcs-v3-0-42f7774efbf3@hack3r.moe

Changes in v3:
- Simplifiy changes and shorten subjects, according to reviews.
- Remove unused includes in selftests.
- Link to v2: https://lore.kernel.org/r/20240929-libbpf-dup-extern-funcs-v2-0-0cc81de3f79f@hack3r.moe

Changes in v2:
- Fix compile errors. Oops!
- Link to v1: https://lore.kernel.org/r/20240929-libbpf-dup-extern-funcs-v1-0-df15fbd6525b@hack3r.moe

---
Eric Long (2):
      libbpf: do not resolve size on duplicate FUNCs
      selftests/bpf: test linking with duplicate extern functions

 tools/lib/bpf/linker.c                            | 4 ++++
 tools/testing/selftests/bpf/progs/linked_funcs1.c | 8 ++++++++
 tools/testing/selftests/bpf/progs/linked_funcs2.c | 8 ++++++++
 3 files changed, 20 insertions(+)
---
base-commit: 93eeaab4563cc7fc0309bc1c4d301139762bbd60
change-id: 20240929-libbpf-dup-extern-funcs-871f4bad2122

Best regards,
-- 
Eric Long <i@hack3r.moe>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf-next v4 1/2] libbpf: do not resolve size on duplicate FUNCs
  2024-10-02  6:25 [PATCH bpf-next v4 0/2] BPF static linker: fix linking duplicate extern functions Eric Long via B4 Relay
@ 2024-10-02  6:25 ` Eric Long via B4 Relay
  2024-10-02  6:25 ` [PATCH bpf-next v4 2/2] selftests/bpf: test linking with duplicate extern functions Eric Long via B4 Relay
  2024-10-08  3:30 ` [PATCH bpf-next v4 0/2] BPF static linker: fix linking " patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Long via B4 Relay @ 2024-10-02  6:25 UTC (permalink / raw)
  To: bpf; +Cc: Andrii Nakryiko, Alexei Starovoitov, netdev, Eric Long

From: Eric Long <i@hack3r.moe>

FUNCs do not have sizes, thus currently btf__resolve_size will fail
with -EINVAL. Add conditions so that we only update size when the BTF
object is not function or function prototype.

Signed-off-by: Eric Long <i@hack3r.moe>
---
 tools/lib/bpf/linker.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index 81dbbdd79a7c65a4b048b85e1dba99cb5f7cb56b..f83c1c29982c3d9d7f6775cd98a6f3387a4e9c50 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -2451,6 +2451,10 @@ static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj)
 			if (glob_sym && glob_sym->var_idx >= 0) {
 				__s64 sz;
 
+				/* FUNCs don't have size, nothing to update */
+				if (btf_is_func(t))
+					continue;
+
 				dst_var = &dst_sec->sec_vars[glob_sym->var_idx];
 				/* Because underlying BTF type might have
 				 * changed, so might its size have changed, so

-- 
2.46.2



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH bpf-next v4 2/2] selftests/bpf: test linking with duplicate extern functions
  2024-10-02  6:25 [PATCH bpf-next v4 0/2] BPF static linker: fix linking duplicate extern functions Eric Long via B4 Relay
  2024-10-02  6:25 ` [PATCH bpf-next v4 1/2] libbpf: do not resolve size on duplicate FUNCs Eric Long via B4 Relay
@ 2024-10-02  6:25 ` Eric Long via B4 Relay
  2024-10-08  3:30 ` [PATCH bpf-next v4 0/2] BPF static linker: fix linking " patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Long via B4 Relay @ 2024-10-02  6:25 UTC (permalink / raw)
  To: bpf; +Cc: Andrii Nakryiko, Alexei Starovoitov, netdev, Eric Long

From: Eric Long <i@hack3r.moe>

Previously when multiple BPF object files referencing the same extern
function (usually kfunc) are statically linked using `bpftool gen
object`, libbpf tries to get the nonexistent size of BTF_KIND_FUNC_PROTO
and fails. This test ensures it is fixed.

Signed-off-by: Eric Long <i@hack3r.moe>
---
 tools/testing/selftests/bpf/progs/linked_funcs1.c | 8 ++++++++
 tools/testing/selftests/bpf/progs/linked_funcs2.c | 8 ++++++++
 2 files changed, 16 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/linked_funcs1.c b/tools/testing/selftests/bpf/progs/linked_funcs1.c
index cc79dddac182c20da69a1a57fa39bc81004184f1..049a1f78de3f835e7658dde6f2d03161b6a5a07f 100644
--- a/tools/testing/selftests/bpf/progs/linked_funcs1.c
+++ b/tools/testing/selftests/bpf/progs/linked_funcs1.c
@@ -63,6 +63,8 @@ extern int set_output_val2(int x);
 /* here we'll force set_output_ctx2() to be __hidden in the final obj file */
 __hidden extern void set_output_ctx2(__u64 *ctx);
 
+void *bpf_cast_to_kern_ctx(void *obj) __ksym;
+
 SEC("?raw_tp/sys_enter")
 int BPF_PROG(handler1, struct pt_regs *regs, long id)
 {
@@ -86,4 +88,10 @@ int BPF_PROG(handler1, struct pt_regs *regs, long id)
 	return 0;
 }
 
+/* Generate BTF FUNC record and test linking with duplicate extern functions */
+void kfunc_gen1(void)
+{
+	bpf_cast_to_kern_ctx(0);
+}
+
 char LICENSE[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/linked_funcs2.c b/tools/testing/selftests/bpf/progs/linked_funcs2.c
index 942cc5526ddf02004bf82d1f72c74cfba6eaf486..96850759fd8d0074249bbf1f743aab08e20de0fc 100644
--- a/tools/testing/selftests/bpf/progs/linked_funcs2.c
+++ b/tools/testing/selftests/bpf/progs/linked_funcs2.c
@@ -63,6 +63,8 @@ extern int set_output_val1(int x);
 /* here we'll force set_output_ctx1() to be __hidden in the final obj file */
 __hidden extern void set_output_ctx1(__u64 *ctx);
 
+void *bpf_cast_to_kern_ctx(void *obj) __ksym;
+
 SEC("?raw_tp/sys_enter")
 int BPF_PROG(handler2, struct pt_regs *regs, long id)
 {
@@ -86,4 +88,10 @@ int BPF_PROG(handler2, struct pt_regs *regs, long id)
 	return 0;
 }
 
+/* Generate BTF FUNC record and test linking with duplicate extern functions */
+void kfunc_gen2(void)
+{
+	bpf_cast_to_kern_ctx(0);
+}
+
 char LICENSE[] SEC("license") = "GPL";

-- 
2.46.2



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next v4 0/2] BPF static linker: fix linking duplicate extern functions
  2024-10-02  6:25 [PATCH bpf-next v4 0/2] BPF static linker: fix linking duplicate extern functions Eric Long via B4 Relay
  2024-10-02  6:25 ` [PATCH bpf-next v4 1/2] libbpf: do not resolve size on duplicate FUNCs Eric Long via B4 Relay
  2024-10-02  6:25 ` [PATCH bpf-next v4 2/2] selftests/bpf: test linking with duplicate extern functions Eric Long via B4 Relay
@ 2024-10-08  3:30 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-10-08  3:30 UTC (permalink / raw)
  To: Eric Long via B4 Relay; +Cc: bpf, andrii, ast, netdev, i

Hello:

This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Wed, 02 Oct 2024 14:25:05 +0800 you wrote:
> Currently, if `bpftool gen object` tries to link two objects that
> contains the same extern function prototype, libbpf will try to get
> their (non-existent) size by calling bpf__resolve_size like extern
> variables and fail with:
> 
> 	libbpf: global 'whatever': failed to resolve size of underlying type: -22
> 
> [...]

Here is the summary with links:
  - [bpf-next,v4,1/2] libbpf: do not resolve size on duplicate FUNCs
    https://git.kernel.org/bpf/bpf-next/c/4b146e95da87
  - [bpf-next,v4,2/2] selftests/bpf: test linking with duplicate extern functions
    https://git.kernel.org/bpf/bpf-next/c/3c591de28543

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-10-08  3:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-02  6:25 [PATCH bpf-next v4 0/2] BPF static linker: fix linking duplicate extern functions Eric Long via B4 Relay
2024-10-02  6:25 ` [PATCH bpf-next v4 1/2] libbpf: do not resolve size on duplicate FUNCs Eric Long via B4 Relay
2024-10-02  6:25 ` [PATCH bpf-next v4 2/2] selftests/bpf: test linking with duplicate extern functions Eric Long via B4 Relay
2024-10-08  3:30 ` [PATCH bpf-next v4 0/2] BPF static linker: fix linking " patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox