Skip to content

Commit a506d6c

Browse files
Alexei Starovoitovanakryiko
authored andcommitted
libbpf: Fix ld_imm64 copy logic for ksym in light skeleton.
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: d995816 ("libbpf: Avoid reload of imm for weak, unresolved, repeating ksym") Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 08ff1c9 commit a506d6c

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

tools/lib/bpf/gen_loader.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -804,11 +804,13 @@ static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo,
804804
return;
805805
/* try to copy from existing ldimm64 insn */
806806
if (kdesc->ref > 1) {
807-
move_blob2blob(gen, insn + offsetof(struct bpf_insn, imm), 4,
808-
kdesc->insn + offsetof(struct bpf_insn, imm));
809807
move_blob2blob(gen, insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm), 4,
810808
kdesc->insn + sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm));
811-
/* jump over src_reg adjustment if imm is not 0, reuse BPF_REG_0 from move_blob2blob */
809+
move_blob2blob(gen, insn + offsetof(struct bpf_insn, imm), 4,
810+
kdesc->insn + offsetof(struct bpf_insn, imm));
811+
/* jump over src_reg adjustment if imm (btf_id) is not 0, reuse BPF_REG_0 from move_blob2blob
812+
* If btf_id is zero, clear BPF_PSEUDO_BTF_ID flag in src_reg of ld_imm64 insn
813+
*/
812814
emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 3));
813815
goto clear_src_reg;
814816
}
@@ -831,7 +833,7 @@ static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo,
831833
emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_7,
832834
sizeof(struct bpf_insn) + offsetof(struct bpf_insn, imm)));
833835
/* skip src_reg adjustment */
834-
emit(gen, BPF_JMP_IMM(BPF_JSGE, BPF_REG_7, 0, 3));
836+
emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 3));
835837
clear_src_reg:
836838
/* clear bpf_object__relocate_data's src_reg assignment, otherwise we get a verifier failure */
837839
reg_mask = src_reg_mask();

0 commit comments

Comments
 (0)