netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next 1/2] libbpf: Fix ld_imm64 copy logic for ksym in light skeleton.
@ 2023-03-19 20:30 Alexei Starovoitov
  2023-03-19 20:30 ` [PATCH bpf-next 2/2] selftest/bpf: Add a test case for ld_imm64 copy logic Alexei Starovoitov
  2023-03-20 17:10 ` [PATCH bpf-next 1/2] libbpf: Fix ld_imm64 copy logic for ksym in light skeleton patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2023-03-19 20:30 UTC (permalink / raw)
  To: davem
  Cc: daniel, andrii, martin.lau, void, davemarchevsky, tj, memxor,
	netdev, bpf, kernel-team

From: Alexei Starovoitov <ast@kernel.org>

Unlike normal libbpf the light skeleton 'loader' program is doing
btf_find_by_name_kind() call at run-time to find ksym in the kernel and
populate its {btf_id, btf_obj_fd} pair in ld_imm64 insn. To avoid doing the
search multiple times for the same ksym it remembers the first patched ld_imm64
insn and copies {btf_id, btf_obj_fd} from it into subsequent ld_imm64 insn.
Fix a bug in copying logic, since it may incorrectly clear BPF_PSEUDO_BTF_ID flag.

Also replace always true if (btf_obj_fd >= 0) check with unconditional JMP_JA
to clarify the code.

Fixes: d995816b77eb ("libbpf: Avoid reload of imm for weak, unresolved, repeating ksym")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 tools/lib/bpf/gen_loader.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c
index 23f5c46708f8..b74c82bb831e 100644
--- a/tools/lib/bpf/gen_loader.c
+++ b/tools/lib/bpf/gen_loader.c
@@ -804,11 +804,13 @@ static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo,
 		return;
 	/* try to copy from existing ldimm64 insn */
 	if (kdesc->ref > 1) {
-		move_blob2blob(gen, insn + offsetof(struct bpf_insn, imm), 4,
-			       kdesc->insn + offsetof(struct bpf_insn, imm));
 		move_blob2blob(gen, insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm), 4,
 			       kdesc->insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm));
-		/* jump over src_reg adjustment if imm is not 0, reuse BPF_REG_0 from move_blob2blob */
+		move_blob2blob(gen, insn + offsetof(struct bpf_insn, imm), 4,
+			       kdesc->insn + offsetof(struct bpf_insn, imm));
+		/* jump over src_reg adjustment if imm (btf_id) is not 0, reuse BPF_REG_0 from move_blob2blob
+		 * If btf_id is zero, clear BPF_PSEUDO_BTF_ID flag in src_reg of ld_imm64 insn
+		 */
 		emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 3));
 		goto clear_src_reg;
 	}
@@ -831,7 +833,7 @@ static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo,
 	emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_7,
 			      sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm)));
 	/* skip src_reg adjustment */
-	emit(gen, BPF_JMP_IMM(BPF_JSGE, BPF_REG_7, 0, 3));
+	emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 3));
 clear_src_reg:
 	/* clear bpf_object__relocate_data's src_reg assignment, otherwise we get a verifier failure */
 	reg_mask = src_reg_mask();
-- 
2.34.1


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

* [PATCH bpf-next 2/2] selftest/bpf: Add a test case for ld_imm64 copy logic.
  2023-03-19 20:30 [PATCH bpf-next 1/2] libbpf: Fix ld_imm64 copy logic for ksym in light skeleton Alexei Starovoitov
@ 2023-03-19 20:30 ` Alexei Starovoitov
  2023-03-20 17:10 ` [PATCH bpf-next 1/2] libbpf: Fix ld_imm64 copy logic for ksym in light skeleton patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2023-03-19 20:30 UTC (permalink / raw)
  To: davem
  Cc: daniel, andrii, martin.lau, void, davemarchevsky, tj, memxor,
	netdev, bpf, kernel-team

From: Alexei Starovoitov <ast@kernel.org>

Add a test case to exercise {btf_id, btf_obj_fd} copy logic between ld_imm64 insns.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 tools/testing/selftests/bpf/progs/test_ksyms_weak.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/progs/test_ksyms_weak.c b/tools/testing/selftests/bpf/progs/test_ksyms_weak.c
index 5f8379aadb29..7003eef0c192 100644
--- a/tools/testing/selftests/bpf/progs/test_ksyms_weak.c
+++ b/tools/testing/selftests/bpf/progs/test_ksyms_weak.c
@@ -37,7 +37,7 @@ int pass_handler(const void *ctx)
 
 	/* tests existing symbols. */
 	rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, 0);
-	if (rq)
+	if (rq && bpf_ksym_exists(&runqueues))
 		out__existing_typed = rq->cpu;
 	out__existing_typeless = (__u64)&bpf_prog_active;
 
-- 
2.34.1


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

* Re: [PATCH bpf-next 1/2] libbpf: Fix ld_imm64 copy logic for ksym in light skeleton.
  2023-03-19 20:30 [PATCH bpf-next 1/2] libbpf: Fix ld_imm64 copy logic for ksym in light skeleton Alexei Starovoitov
  2023-03-19 20:30 ` [PATCH bpf-next 2/2] selftest/bpf: Add a test case for ld_imm64 copy logic Alexei Starovoitov
@ 2023-03-20 17:10 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-03-20 17:10 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: davem, daniel, andrii, martin.lau, void, davemarchevsky, tj,
	memxor, netdev, bpf, kernel-team

Hello:

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

On Sun, 19 Mar 2023 13:30:13 -0700 you wrote:
> From: Alexei Starovoitov <ast@kernel.org>
> 
> Unlike normal libbpf the light skeleton 'loader' program is doing
> btf_find_by_name_kind() call at run-time to find ksym in the kernel and
> populate its {btf_id, btf_obj_fd} pair in ld_imm64 insn. To avoid doing the
> search multiple times for the same ksym it remembers the first patched ld_imm64
> insn and copies {btf_id, btf_obj_fd} from it into subsequent ld_imm64 insn.
> Fix a bug in copying logic, since it may incorrectly clear BPF_PSEUDO_BTF_ID flag.
> 
> [...]

Here is the summary with links:
  - [bpf-next,1/2] libbpf: Fix ld_imm64 copy logic for ksym in light skeleton.
    https://git.kernel.org/bpf/bpf-next/c/a506d6ce1dd1
  - [bpf-next,2/2] selftest/bpf: Add a test case for ld_imm64 copy logic.
    https://git.kernel.org/bpf/bpf-next/c/bb4a6a923729

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] 3+ messages in thread

end of thread, other threads:[~2023-03-20 17:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-19 20:30 [PATCH bpf-next 1/2] libbpf: Fix ld_imm64 copy logic for ksym in light skeleton Alexei Starovoitov
2023-03-19 20:30 ` [PATCH bpf-next 2/2] selftest/bpf: Add a test case for ld_imm64 copy logic Alexei Starovoitov
2023-03-20 17:10 ` [PATCH bpf-next 1/2] libbpf: Fix ld_imm64 copy logic for ksym in light skeleton 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;
as well as URLs for NNTP newsgroup(s).