Skip to content

Commit 5b7fe93

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Daniel Borkmann says: ==================== pull-request: bpf-next 2019-10-27 The following pull-request contains BPF updates for your *net-next* tree. We've added 52 non-merge commits during the last 11 day(s) which contain a total of 65 files changed, 2604 insertions(+), 1100 deletions(-). The main changes are: 1) Revolutionize BPF tracing by using in-kernel BTF to type check BPF assembly code. The work here teaches BPF verifier to recognize kfree_skb()'s first argument as 'struct sk_buff *' in tracepoints such that verifier allows direct use of bpf_skb_event_output() helper used in tc BPF et al (w/o probing memory access) that dumps skb data into perf ring buffer. Also add direct loads to probe memory in order to speed up/replace bpf_probe_read() calls, from Alexei Starovoitov. 2) Big batch of changes to improve libbpf and BPF kselftests. Besides others: generalization of libbpf's CO-RE relocation support to now also include field existence relocations, revamp the BPF kselftest Makefile to add test runner concept allowing to exercise various ways to build BPF programs, and teach bpf_object__open() and friends to automatically derive BPF program type/expected attach type from section names to ease their use, from Andrii Nakryiko. 3) Fix deadlock in stackmap's build-id lookup on rq_lock(), from Song Liu. 4) Allow to read BTF as raw data from bpftool. Most notable use case is to dump /sys/kernel/btf/vmlinux through this, from Jiri Olsa. 5) Use bpf_redirect_map() helper in libbpf's AF_XDP helper prog which manages to improve "rx_drop" performance by ~4%., from Björn Töpel. 6) Fix to restore the flow dissector after reattach BPF test and also fix error handling in bpf_helper_defs.h generation, from Jakub Sitnicki. 7) Improve verifier's BTF ctx access for use outside of raw_tp, from Martin KaFai Lau. 8) Improve documentation for AF_XDP with new sections and to reflect latest features, from Magnus Karlsson. 9) Add back 'version' section parsing to libbpf for old kernels, from John Fastabend. 10) Fix strncat bounds error in libbpf's libbpf_prog_type_by_name(), from KP Singh. 11) Turn on -mattr=+alu32 in LLVM by default for BPF kselftests in order to improve insn coverage for built BPF progs, from Yonghong Song. 12) Misc minor cleanups and fixes, from various others. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents b951248 + 027cbaa commit 5b7fe93

65 files changed

Lines changed: 2604 additions & 1100 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Documentation/networking/af_xdp.rst‎

Lines changed: 231 additions & 28 deletions
Large diffs are not rendered by default.

‎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: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
#include <linux/u64_stats_sync.h>
1717

1818
struct bpf_verifier_env;
19+
struct bpf_verifier_log;
1920
struct perf_event;
2021
struct bpf_prog;
2122
struct bpf_map;
2223
struct sock;
2324
struct seq_file;
2425
struct btf;
2526
struct btf_type;
27+
struct exception_table_entry;
2628

2729
extern struct idr btf_idr;
2830
extern spinlock_t btf_idr_lock;
@@ -211,6 +213,7 @@ enum bpf_arg_type {
211213
ARG_PTR_TO_INT, /* pointer to int */
212214
ARG_PTR_TO_LONG, /* pointer to long */
213215
ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
216+
ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
214217
};
215218

216219
/* type of values returned from helper functions */
@@ -233,11 +236,17 @@ struct bpf_func_proto {
233236
bool gpl_only;
234237
bool pkt_access;
235238
enum bpf_return_type ret_type;
236-
enum bpf_arg_type arg1_type;
237-
enum bpf_arg_type arg2_type;
238-
enum bpf_arg_type arg3_type;
239-
enum bpf_arg_type arg4_type;
240-
enum bpf_arg_type arg5_type;
239+
union {
240+
struct {
241+
enum bpf_arg_type arg1_type;
242+
enum bpf_arg_type arg2_type;
243+
enum bpf_arg_type arg3_type;
244+
enum bpf_arg_type arg4_type;
245+
enum bpf_arg_type arg5_type;
246+
};
247+
enum bpf_arg_type arg_type[5];
248+
};
249+
u32 *btf_id; /* BTF ids of arguments */
241250
};
242251

243252
/* bpf_context is intentionally undefined structure. Pointer to bpf_context is
@@ -281,14 +290,19 @@ enum bpf_reg_type {
281290
PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
282291
PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
283292
PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
293+
PTR_TO_BTF_ID, /* reg points to kernel struct */
284294
};
285295

286296
/* The information passed from prog-specific *_is_valid_access
287297
* back to the verifier.
288298
*/
289299
struct bpf_insn_access_aux {
290300
enum bpf_reg_type reg_type;
291-
int ctx_field_size;
301+
union {
302+
int ctx_field_size;
303+
u32 btf_id;
304+
};
305+
struct bpf_verifier_log *log; /* for verbose logs */
292306
};
293307

294308
static inline void
@@ -375,8 +389,14 @@ struct bpf_prog_aux {
375389
u32 id;
376390
u32 func_cnt; /* used by non-func prog as the number of func progs */
377391
u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
392+
u32 attach_btf_id; /* in-kernel BTF type id to attach to */
378393
bool verifier_zext; /* Zero extensions has been inserted by verifier. */
379394
bool offload_requested;
395+
bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
396+
/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
397+
const struct btf_type *attach_func_proto;
398+
/* function name for valid attach_btf_id */
399+
const char *attach_func_name;
380400
struct bpf_prog **func;
381401
void *jit_data; /* JIT specific data. arch dependent */
382402
struct latch_tree_node ksym_tnode;
@@ -416,6 +436,8 @@ struct bpf_prog_aux {
416436
* main prog always has linfo_idx == 0
417437
*/
418438
u32 linfo_idx;
439+
u32 num_exentries;
440+
struct exception_table_entry *extable;
419441
struct bpf_prog_stats __percpu *stats;
420442
union {
421443
struct work_struct work;
@@ -482,6 +504,7 @@ struct bpf_event_entry {
482504

483505
bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
484506
int bpf_prog_calc_tag(struct bpf_prog *fp);
507+
const char *kernel_type_name(u32 btf_type_id);
485508

486509
const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
487510

@@ -747,6 +770,15 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
747770
int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
748771
const union bpf_attr *kattr,
749772
union bpf_attr __user *uattr);
773+
bool btf_ctx_access(int off, int size, enum bpf_access_type type,
774+
const struct bpf_prog *prog,
775+
struct bpf_insn_access_aux *info);
776+
int btf_struct_access(struct bpf_verifier_log *log,
777+
const struct btf_type *t, int off, int size,
778+
enum bpf_access_type atype,
779+
u32 *next_btf_id);
780+
u32 btf_resolve_helper_id(struct bpf_verifier_log *log, void *, int);
781+
750782
#else /* !CONFIG_BPF_SYSCALL */
751783
static inline struct bpf_prog *bpf_prog_get(u32 ufd)
752784
{

‎include/linux/bpf_verifier.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ struct bpf_reg_state {
5252
*/
5353
struct bpf_map *map_ptr;
5454

55+
u32 btf_id; /* for PTR_TO_BTF_ID */
56+
5557
/* Max size from any of the above. */
5658
unsigned long raw;
5759
};
@@ -330,10 +332,12 @@ static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log)
330332
#define BPF_LOG_STATS 4
331333
#define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2)
332334
#define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS)
335+
#define BPF_LOG_KERNEL (BPF_LOG_MASK + 1) /* kernel internal flag */
333336

334337
static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
335338
{
336-
return log->level && log->ubuf && !bpf_verifier_log_full(log);
339+
return (log->level && log->ubuf && !bpf_verifier_log_full(log)) ||
340+
log->level == BPF_LOG_KERNEL;
337341
}
338342

339343
#define BPF_MAX_SUBPROGS 256
@@ -397,6 +401,8 @@ __printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log,
397401
const char *fmt, va_list args);
398402
__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
399403
const char *fmt, ...);
404+
__printf(2, 3) void bpf_log(struct bpf_verifier_log *log,
405+
const char *fmt, ...);
400406

401407
static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env)
402408
{

‎include/linux/btf.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define _LINUX_BTF_H 1
66

77
#include <linux/types.h>
8+
#include <uapi/linux/btf.h>
89

910
struct btf;
1011
struct btf_member;
@@ -53,9 +54,40 @@ bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
5354
int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
5455
bool btf_type_is_void(const struct btf_type *t);
5556

57+
static inline bool btf_type_is_ptr(const struct btf_type *t)
58+
{
59+
return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
60+
}
61+
62+
static inline bool btf_type_is_int(const struct btf_type *t)
63+
{
64+
return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
65+
}
66+
67+
static inline bool btf_type_is_enum(const struct btf_type *t)
68+
{
69+
return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
70+
}
71+
72+
static inline bool btf_type_is_typedef(const struct btf_type *t)
73+
{
74+
return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
75+
}
76+
77+
static inline bool btf_type_is_func(const struct btf_type *t)
78+
{
79+
return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
80+
}
81+
82+
static inline bool btf_type_is_func_proto(const struct btf_type *t)
83+
{
84+
return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
85+
}
86+
5687
#ifdef CONFIG_BPF_SYSCALL
5788
const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
5889
const char *btf_name_by_offset(const struct btf *btf, u32 offset);
90+
struct btf *btf_parse_vmlinux(void);
5991
#else
6092
static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
6193
u32 type_id)

‎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 */

0 commit comments

Comments
 (0)