Skip to content

Commit 8ab9da5

Browse files
anakryikoborkmann
authored andcommitted
libbpf: Support CO-RE relocations for LDX/ST/STX instructions
Clang patch [0] enables emitting relocatable generic ALU/ALU64 instructions (i.e, shifts and arithmetic operations), as well as generic load/store instructions. The former ones are already supported by libbpf as is. This patch adds further support for load/store instructions. Relocatable field offset is encoded in BPF instruction's 16-bit offset section and are adjusted by libbpf based on target kernel BTF. These Clang changes and corresponding libbpf changes allow for more succinct generated BPF code by encoding relocatable field reads as a single ST/LDX/STX instruction. It also enables relocatable access to BPF context. Previously, if context struct (e.g., __sk_buff) was accessed with CO-RE relocations (e.g., due to preserve_access_index attribute), it would be rejected by BPF verifier due to modified context pointer dereference. With Clang patch, such context accesses are both relocatable and have a fixed offset from the point of view of BPF verifier. [0] https://reviews.llvm.org/D71790 Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent f9e6bfd commit 8ab9da5

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdarg.h>
1919
#include <libgen.h>
2020
#include <inttypes.h>
21+
#include <limits.h>
2122
#include <string.h>
2223
#include <unistd.h>
2324
#include <endian.h>
@@ -3810,11 +3811,13 @@ static int bpf_core_reloc_insn(struct bpf_program *prog,
38103811
insn = &prog->insns[insn_idx];
38113812
class = BPF_CLASS(insn->code);
38123813

3813-
if (class == BPF_ALU || class == BPF_ALU64) {
3814+
switch (class) {
3815+
case BPF_ALU:
3816+
case BPF_ALU64:
38143817
if (BPF_SRC(insn->code) != BPF_K)
38153818
return -EINVAL;
38163819
if (!failed && validate && insn->imm != orig_val) {
3817-
pr_warn("prog '%s': unexpected insn #%d value: got %u, exp %u -> %u\n",
3820+
pr_warn("prog '%s': unexpected insn #%d (ALU/ALU64) value: got %u, exp %u -> %u\n",
38183821
bpf_program__title(prog, false), insn_idx,
38193822
insn->imm, orig_val, new_val);
38203823
return -EINVAL;
@@ -3824,7 +3827,29 @@ static int bpf_core_reloc_insn(struct bpf_program *prog,
38243827
pr_debug("prog '%s': patched insn #%d (ALU/ALU64)%s imm %u -> %u\n",
38253828
bpf_program__title(prog, false), insn_idx,
38263829
failed ? " w/ failed reloc" : "", orig_val, new_val);
3827-
} else {
3830+
break;
3831+
case BPF_LDX:
3832+
case BPF_ST:
3833+
case BPF_STX:
3834+
if (!failed && validate && insn->off != orig_val) {
3835+
pr_warn("prog '%s': unexpected insn #%d (LD/LDX/ST/STX) value: got %u, exp %u -> %u\n",
3836+
bpf_program__title(prog, false), insn_idx,
3837+
insn->off, orig_val, new_val);
3838+
return -EINVAL;
3839+
}
3840+
if (new_val > SHRT_MAX) {
3841+
pr_warn("prog '%s': insn #%d (LD/LDX/ST/STX) value too big: %u\n",
3842+
bpf_program__title(prog, false), insn_idx,
3843+
new_val);
3844+
return -ERANGE;
3845+
}
3846+
orig_val = insn->off;
3847+
insn->off = new_val;
3848+
pr_debug("prog '%s': patched insn #%d (LD/LDX/ST/STX)%s off %u -> %u\n",
3849+
bpf_program__title(prog, false), insn_idx,
3850+
failed ? " w/ failed reloc" : "", orig_val, new_val);
3851+
break;
3852+
default:
38283853
pr_warn("prog '%s': trying to relocate unrecognized insn #%d, code:%x, src:%x, dst:%x, off:%x, imm:%x\n",
38293854
bpf_program__title(prog, false),
38303855
insn_idx, insn->code, insn->src_reg, insn->dst_reg,

0 commit comments

Comments
 (0)