Skip to content

Commit 3dec541

Browse files
Alexei Starovoitovborkmann
authored andcommitted
bpf: Add support for BTF pointers to x86 JIT
Pointer to BTF object is a pointer to kernel object or NULL. Such pointers can only be used by BPF_LDX instructions. The verifier changed their opcode from LDX|MEM|size to LDX|PROBE_MEM|size to make JITing easier. The number of entries in extable is the number of BPF_LDX insns that access kernel memory via "pointer to BTF type". Only these load instructions can fault. Since x86 extable is relative it has to be allocated in the same memory region as JITed code. Allocate it prior to last pass of JITing and let the last pass populate it. Pointer to extable in bpf_prog_aux is necessary to make page fault handling fast. Page fault handling is done in two steps: 1. bpf_prog_kallsyms_find() finds BPF program that page faulted. It's done by walking rb tree. 2. then extable for given bpf program is binary searched. This process is similar to how page faulting is done for kernel modules. The exception handler skips over faulting x86 instruction and initializes destination register with zero. This mimics exact behavior of bpf_probe_read (when probe_kernel_read faults dest is zeroed). JITs for other architectures can add support in similar way. Until then they will reject unknown opcode and fallback to interpreter. Since extable should be aligned and placed near JITed code make bpf_jit_binary_alloc() return 4 byte aligned image offset, so that extable aligning formula in bpf_int_jit_compile() doesn't need to rely on internal implementation of bpf_jit_binary_alloc(). On x86 gcc defaults to 16-byte alignment for regular kernel functions due to better performance. JITed code may be aligned to 16 in the future, but it will use 4 in the meantime. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Andrii Nakryiko <andriin@fb.com> Acked-by: Martin KaFai Lau <kafai@fb.com> Link: https://lore.kernel.org/bpf/20191016032505.2089704-10-ast@kernel.org
1 parent 2a02759 commit 3dec541

6 files changed

Lines changed: 128 additions & 5 deletions

File tree

‎arch/x86/net/bpf_jit_comp.c‎

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <linux/filter.h>
1010
#include <linux/if_vlan.h>
1111
#include <linux/bpf.h>
12-
12+
#include <asm/extable.h>
1313
#include <asm/set_memory.h>
1414
#include <asm/nospec-branch.h>
1515

@@ -123,6 +123,19 @@ static const int reg2hex[] = {
123123
[AUX_REG] = 3, /* R11 temp register */
124124
};
125125

126+
static const int reg2pt_regs[] = {
127+
[BPF_REG_0] = offsetof(struct pt_regs, ax),
128+
[BPF_REG_1] = offsetof(struct pt_regs, di),
129+
[BPF_REG_2] = offsetof(struct pt_regs, si),
130+
[BPF_REG_3] = offsetof(struct pt_regs, dx),
131+
[BPF_REG_4] = offsetof(struct pt_regs, cx),
132+
[BPF_REG_5] = offsetof(struct pt_regs, r8),
133+
[BPF_REG_6] = offsetof(struct pt_regs, bx),
134+
[BPF_REG_7] = offsetof(struct pt_regs, r13),
135+
[BPF_REG_8] = offsetof(struct pt_regs, r14),
136+
[BPF_REG_9] = offsetof(struct pt_regs, r15),
137+
};
138+
126139
/*
127140
* is_ereg() == true if BPF register 'reg' maps to x86-64 r8..r15
128141
* which need extra byte of encoding.
@@ -377,14 +390,27 @@ static void emit_mov_reg(u8 **pprog, bool is64, u32 dst_reg, u32 src_reg)
377390
*pprog = prog;
378391
}
379392

393+
394+
static bool ex_handler_bpf(const struct exception_table_entry *x,
395+
struct pt_regs *regs, int trapnr,
396+
unsigned long error_code, unsigned long fault_addr)
397+
{
398+
u32 reg = x->fixup >> 8;
399+
400+
/* jump over faulting load and clear dest register */
401+
*(unsigned long *)((void *)regs + reg) = 0;
402+
regs->ip += x->fixup & 0xff;
403+
return true;
404+
}
405+
380406
static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
381407
int oldproglen, struct jit_context *ctx)
382408
{
383409
struct bpf_insn *insn = bpf_prog->insnsi;
384410
int insn_cnt = bpf_prog->len;
385411
bool seen_exit = false;
386412
u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
387-
int i, cnt = 0;
413+
int i, cnt = 0, excnt = 0;
388414
int proglen = 0;
389415
u8 *prog = temp;
390416

@@ -778,21 +804,25 @@ stx: if (is_imm8(insn->off))
778804

779805
/* LDX: dst_reg = *(u8*)(src_reg + off) */
780806
case BPF_LDX | BPF_MEM | BPF_B:
807+
case BPF_LDX | BPF_PROBE_MEM | BPF_B:
781808
/* Emit 'movzx rax, byte ptr [rax + off]' */
782809
EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB6);
783810
goto ldx;
784811
case BPF_LDX | BPF_MEM | BPF_H:
812+
case BPF_LDX | BPF_PROBE_MEM | BPF_H:
785813
/* Emit 'movzx rax, word ptr [rax + off]' */
786814
EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB7);
787815
goto ldx;
788816
case BPF_LDX | BPF_MEM | BPF_W:
817+
case BPF_LDX | BPF_PROBE_MEM | BPF_W:
789818
/* Emit 'mov eax, dword ptr [rax+0x14]' */
790819
if (is_ereg(dst_reg) || is_ereg(src_reg))
791820
EMIT2(add_2mod(0x40, src_reg, dst_reg), 0x8B);
792821
else
793822
EMIT1(0x8B);
794823
goto ldx;
795824
case BPF_LDX | BPF_MEM | BPF_DW:
825+
case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
796826
/* Emit 'mov rax, qword ptr [rax+0x14]' */
797827
EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x8B);
798828
ldx: /*
@@ -805,6 +835,48 @@ stx: if (is_imm8(insn->off))
805835
else
806836
EMIT1_off32(add_2reg(0x80, src_reg, dst_reg),
807837
insn->off);
838+
if (BPF_MODE(insn->code) == BPF_PROBE_MEM) {
839+
struct exception_table_entry *ex;
840+
u8 *_insn = image + proglen;
841+
s64 delta;
842+
843+
if (!bpf_prog->aux->extable)
844+
break;
845+
846+
if (excnt >= bpf_prog->aux->num_exentries) {
847+
pr_err("ex gen bug\n");
848+
return -EFAULT;
849+
}
850+
ex = &bpf_prog->aux->extable[excnt++];
851+
852+
delta = _insn - (u8 *)&ex->insn;
853+
if (!is_simm32(delta)) {
854+
pr_err("extable->insn doesn't fit into 32-bit\n");
855+
return -EFAULT;
856+
}
857+
ex->insn = delta;
858+
859+
delta = (u8 *)ex_handler_bpf - (u8 *)&ex->handler;
860+
if (!is_simm32(delta)) {
861+
pr_err("extable->handler doesn't fit into 32-bit\n");
862+
return -EFAULT;
863+
}
864+
ex->handler = delta;
865+
866+
if (dst_reg > BPF_REG_9) {
867+
pr_err("verifier error\n");
868+
return -EFAULT;
869+
}
870+
/*
871+
* Compute size of x86 insn and its target dest x86 register.
872+
* ex_handler_bpf() will use lower 8 bits to adjust
873+
* pt_regs->ip to jump over this x86 instruction
874+
* and upper bits to figure out which pt_regs to zero out.
875+
* End result: x86 insn "mov rbx, qword ptr [rax+0x14]"
876+
* of 4 bytes will be ignored and rbx will be zero inited.
877+
*/
878+
ex->fixup = (prog - temp) | (reg2pt_regs[dst_reg] << 8);
879+
}
808880
break;
809881

810882
/* STX XADD: lock *(u32*)(dst_reg + off) += src_reg */
@@ -1058,6 +1130,11 @@ xadd: if (is_imm8(insn->off))
10581130
addrs[i] = proglen;
10591131
prog = temp;
10601132
}
1133+
1134+
if (image && excnt != bpf_prog->aux->num_exentries) {
1135+
pr_err("extable is not populated\n");
1136+
return -EFAULT;
1137+
}
10611138
return proglen;
10621139
}
10631140

@@ -1158,12 +1235,24 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
11581235
break;
11591236
}
11601237
if (proglen == oldproglen) {
1161-
header = bpf_jit_binary_alloc(proglen, &image,
1162-
1, jit_fill_hole);
1238+
/*
1239+
* The number of entries in extable is the number of BPF_LDX
1240+
* insns that access kernel memory via "pointer to BTF type".
1241+
* The verifier changed their opcode from LDX|MEM|size
1242+
* to LDX|PROBE_MEM|size to make JITing easier.
1243+
*/
1244+
u32 align = __alignof__(struct exception_table_entry);
1245+
u32 extable_size = prog->aux->num_exentries *
1246+
sizeof(struct exception_table_entry);
1247+
1248+
/* allocate module memory for x86 insns and extable */
1249+
header = bpf_jit_binary_alloc(roundup(proglen, align) + extable_size,
1250+
&image, align, jit_fill_hole);
11631251
if (!header) {
11641252
prog = orig_prog;
11651253
goto out_addrs;
11661254
}
1255+
prog->aux->extable = (void *) image + roundup(proglen, align);
11671256
}
11681257
oldproglen = proglen;
11691258
cond_resched();

‎include/linux/bpf.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct sock;
2424
struct seq_file;
2525
struct btf;
2626
struct btf_type;
27+
struct exception_table_entry;
2728

2829
extern struct idr btf_idr;
2930
extern spinlock_t btf_idr_lock;
@@ -423,6 +424,8 @@ struct bpf_prog_aux {
423424
* main prog always has linfo_idx == 0
424425
*/
425426
u32 linfo_idx;
427+
u32 num_exentries;
428+
struct exception_table_entry *extable;
426429
struct bpf_prog_stats __percpu *stats;
427430
union {
428431
struct work_struct work;

‎include/linux/extable.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,14 @@ search_module_extables(unsigned long addr)
3333
}
3434
#endif /*CONFIG_MODULES*/
3535

36+
#ifdef CONFIG_BPF_JIT
37+
const struct exception_table_entry *search_bpf_extables(unsigned long addr);
38+
#else
39+
static inline const struct exception_table_entry *
40+
search_bpf_extables(unsigned long addr)
41+
{
42+
return NULL;
43+
}
44+
#endif
45+
3646
#endif /* _LINUX_EXTABLE_H */

‎kernel/bpf/core.c‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <linux/kallsyms.h>
3131
#include <linux/rcupdate.h>
3232
#include <linux/perf_event.h>
33-
33+
#include <linux/extable.h>
3434
#include <asm/unaligned.h>
3535

3636
/* Registers */
@@ -712,6 +712,24 @@ bool is_bpf_text_address(unsigned long addr)
712712
return ret;
713713
}
714714

715+
const struct exception_table_entry *search_bpf_extables(unsigned long addr)
716+
{
717+
const struct exception_table_entry *e = NULL;
718+
struct bpf_prog *prog;
719+
720+
rcu_read_lock();
721+
prog = bpf_prog_kallsyms_find(addr);
722+
if (!prog)
723+
goto out;
724+
if (!prog->aux->num_exentries)
725+
goto out;
726+
727+
e = search_extable(prog->aux->extable, prog->aux->num_exentries, addr);
728+
out:
729+
rcu_read_unlock();
730+
return e;
731+
}
732+
715733
int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
716734
char *sym)
717735
{

‎kernel/bpf/verifier.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8729,6 +8729,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
87298729
return -EINVAL;
87308730
}
87318731
insn->code = BPF_LDX | BPF_PROBE_MEM | BPF_SIZE((insn)->code);
8732+
env->prog->aux->num_exentries++;
87328733
continue;
87338734
default:
87348735
continue;

‎kernel/extable.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr)
5656
e = search_kernel_exception_table(addr);
5757
if (!e)
5858
e = search_module_extables(addr);
59+
if (!e)
60+
e = search_bpf_extables(addr);
5961
return e;
6062
}
6163

0 commit comments

Comments
 (0)