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. ====================
Showing the changes to include/linux (5 of 65 files). Show all
Unified · Split
| @@ -16,6 +16,7 @@ · 15 unmodified lines | |||
| 16 | #include <linux/u64_stats_sync.h> | 16 | #include <linux/u64_stats_sync.h> |
| 17 | 17 | ||
| 18 | struct bpf_verifier_env; | 18 | struct bpf_verifier_env; |
| 19 | struct bpf_verifier_log; | ||
| 19 | struct perf_event; | 20 | struct perf_event; |
| 20 | struct bpf_prog; | 21 | struct bpf_prog; |
| 21 | struct bpf_map; | 22 | struct bpf_map; |
| @@ -23,6 +24,7 @@ struct sock; · 1 unmodified line | |||
| 23 | struct seq_file; | 24 | struct seq_file; |
| 24 | struct btf; | 25 | struct btf; |
| 25 | struct btf_type; | 26 | struct btf_type; |
| 27 | struct exception_table_entry; | ||
| 26 | 28 | ||
| 27 | extern struct idr btf_idr; | 29 | extern struct idr btf_idr; |
| 28 | extern spinlock_t btf_idr_lock; | 30 | extern spinlock_t btf_idr_lock; |
| @@ -211,6 +213,7 @@ enum bpf_arg_type { · 182 unmodified lines | |||
| 211 | ARG_PTR_TO_INT, /* pointer to int */ | 213 | ARG_PTR_TO_INT, /* pointer to int */ |
| 212 | ARG_PTR_TO_LONG, /* pointer to long */ | 214 | ARG_PTR_TO_LONG, /* pointer to long */ |
| 213 | ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */ | 215 | ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */ |
| 216 | ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */ | ||
| 214 | }; | 217 | }; |
| 215 | 218 | ||
| 216 | /* type of values returned from helper functions */ | 219 | /* type of values returned from helper functions */ |
| @@ -233,11 +236,17 @@ struct bpf_func_proto { · 16 unmodified lines | |||
| 233 | bool gpl_only; | 236 | bool gpl_only; |
| 234 | bool pkt_access; | 237 | bool pkt_access; |
| 235 | enum bpf_return_type ret_type; | 238 | enum bpf_return_type ret_type; |
| 236 | enum bpf_arg_type arg1_type; | 239 | union { |
| 237 | enum bpf_arg_type arg2_type; | 240 | struct { |
| 238 | enum bpf_arg_type arg3_type; | 241 | enum bpf_arg_type arg1_type; |
| 239 | enum bpf_arg_type arg4_type; | 242 | enum bpf_arg_type arg2_type; |
| 240 | enum bpf_arg_type arg5_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 */ | ||
| 241 | }; | 250 | }; |
| 242 | 251 | ||
| 243 | /* bpf_context is intentionally undefined structure. Pointer to bpf_context is | 252 | /* bpf_context is intentionally undefined structure. Pointer to bpf_context is |
| @@ -281,6 +290,7 @@ enum bpf_reg_type { · 37 unmodified lines | |||
| 281 | PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */ | 290 | PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */ |
| 282 | PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */ | 291 | PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */ |
| 283 | PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */ | 292 | PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */ |
| 293 | PTR_TO_BTF_ID, /* reg points to kernel struct */ | ||
| 284 | }; | 294 | }; |
| 285 | 295 | ||
| 286 | /* The information passed from prog-specific *_is_valid_access | 296 | /* The information passed from prog-specific *_is_valid_access |
| @@ -288,7 +298,11 @@ enum bpf_reg_type { · 1 unmodified line | |||
| 288 | */ | 298 | */ |
| 289 | struct bpf_insn_access_aux { | 299 | struct bpf_insn_access_aux { |
| 290 | enum bpf_reg_type reg_type; | 300 | 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 */ | ||
| 292 | }; | 306 | }; |
| 293 | 307 | ||
| 294 | static inline void | 308 | static inline void |
| @@ -375,8 +389,14 @@ struct bpf_prog_aux { · 80 unmodified lines | |||
| 375 | u32 id; | 389 | u32 id; |
| 376 | u32 func_cnt; /* used by non-func prog as the number of func progs */ | 390 | u32 func_cnt; /* used by non-func prog as the number of func progs */ |
| 377 | u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */ | 391 | 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 */ | ||
| 378 | bool verifier_zext; /* Zero extensions has been inserted by verifier. */ | 393 | bool verifier_zext; /* Zero extensions has been inserted by verifier. */ |
| 379 | bool offload_requested; | 394 | 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; | ||
| 380 | struct bpf_prog **func; | 400 | struct bpf_prog **func; |
| 381 | void *jit_data; /* JIT specific data. arch dependent */ | 401 | void *jit_data; /* JIT specific data. arch dependent */ |
| 382 | struct latch_tree_node ksym_tnode; | 402 | struct latch_tree_node ksym_tnode; |
| @@ -416,6 +436,8 @@ struct bpf_prog_aux { · 33 unmodified lines | |||
| 416 | * main prog always has linfo_idx == 0 | 436 | * main prog always has linfo_idx == 0 |
| 417 | */ | 437 | */ |
| 418 | u32 linfo_idx; | 438 | u32 linfo_idx; |
| 439 | u32 num_exentries; | ||
| 440 | struct exception_table_entry *extable; | ||
| 419 | struct bpf_prog_stats __percpu *stats; | 441 | struct bpf_prog_stats __percpu *stats; |
| 420 | union { | 442 | union { |
| 421 | struct work_struct work; | 443 | struct work_struct work; |
| @@ -482,6 +504,7 @@ struct bpf_event_entry { · 60 unmodified lines | |||
| 482 | 504 | ||
| 483 | bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp); | 505 | bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp); |
| 484 | int bpf_prog_calc_tag(struct bpf_prog *fp); | 506 | int bpf_prog_calc_tag(struct bpf_prog *fp); |
| 507 | const char *kernel_type_name(u32 btf_type_id); | ||
| 485 | 508 | ||
| 486 | const struct bpf_func_proto *bpf_get_trace_printk_proto(void); | 509 | const struct bpf_func_proto *bpf_get_trace_printk_proto(void); |
| 487 | 510 | ||
| @@ -747,6 +770,15 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, · 259 unmodified lines | |||
| 747 | int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, | 770 | int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, |
| 748 | const union bpf_attr *kattr, | 771 | const union bpf_attr *kattr, |
| 749 | union bpf_attr __user *uattr); | 772 | 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 | |||
| 750 | #else /* !CONFIG_BPF_SYSCALL */ | 782 | #else /* !CONFIG_BPF_SYSCALL */ |
| 751 | static inline struct bpf_prog *bpf_prog_get(u32 ufd) | 783 | static inline struct bpf_prog *bpf_prog_get(u32 ufd) |
| 752 | { | 784 | { |
| @@ -52,6 +52,8 @@ struct bpf_reg_state { · 51 unmodified lines | |||
| 52 | */ | 52 | */ |
| 53 | struct bpf_map *map_ptr; | 53 | struct bpf_map *map_ptr; |
| 54 | 54 | ||
| 55 | u32 btf_id; /* for PTR_TO_BTF_ID */ | ||
| 56 | |||
| 55 | /* Max size from any of the above. */ | 57 | /* Max size from any of the above. */ |
| 56 | unsigned long raw; | 58 | unsigned long raw; |
| 57 | }; | 59 | }; |
| @@ -330,10 +332,12 @@ static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log) · 272 unmodified lines | |||
| 330 | #define BPF_LOG_STATS 4 | 332 | #define BPF_LOG_STATS 4 |
| 331 | #define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2) | 333 | #define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2) |
| 332 | #define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS) | 334 | #define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS) |
| 335 | #define BPF_LOG_KERNEL (BPF_LOG_MASK + 1) /* kernel internal flag */ | ||
| 333 | 336 | ||
| 334 | static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log) | 337 | static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log) |
| 335 | { | 338 | { |
| 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; | ||
| 337 | } | 341 | } |
| 338 | 342 | ||
| 339 | #define BPF_MAX_SUBPROGS 256 | 343 | #define BPF_MAX_SUBPROGS 256 |
| @@ -397,6 +401,8 @@ __printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log, · 57 unmodified lines | |||
| 397 | const char *fmt, va_list args); | 401 | const char *fmt, va_list args); |
| 398 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, | 402 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 399 | const char *fmt, ...); | 403 | const char *fmt, ...); |
| 404 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, | ||
| 405 | const char *fmt, ...); | ||
| 400 | 406 | ||
| 401 | static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env) | 407 | static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env) |
| 402 | { | 408 | { |
| @@ -5,6 +5,7 @@ · 4 unmodified lines | |||
| 5 | #define _LINUX_BTF_H 1 | 5 | #define _LINUX_BTF_H 1 |
| 6 | 6 | ||
| 7 | #include <linux/types.h> | 7 | #include <linux/types.h> |
| 8 | #include <uapi/linux/btf.h> | ||
| 8 | 9 | ||
| 9 | struct btf; | 10 | struct btf; |
| 10 | struct btf_member; | 11 | struct btf_member; |
| @@ -53,9 +54,40 @@ bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, · 42 unmodified lines | |||
| 53 | int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t); | 54 | int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t); |
| 54 | bool btf_type_is_void(const struct btf_type *t); | 55 | bool btf_type_is_void(const struct btf_type *t); |
| 55 | 56 | ||
| 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 | |||
| 56 | #ifdef CONFIG_BPF_SYSCALL | 87 | #ifdef CONFIG_BPF_SYSCALL |
| 57 | const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); | 88 | const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); |
| 58 | const char *btf_name_by_offset(const struct btf *btf, u32 offset); | 89 | const char *btf_name_by_offset(const struct btf *btf, u32 offset); |
| 90 | struct btf *btf_parse_vmlinux(void); | ||
| 59 | #else | 91 | #else |
| 60 | static inline const struct btf_type *btf_type_by_id(const struct btf *btf, | 92 | static inline const struct btf_type *btf_type_by_id(const struct btf *btf, |
| 61 | u32 type_id) | 93 | u32 type_id) |
| @@ -33,4 +33,14 @@ search_module_extables(unsigned long addr) · 32 unmodified lines | |||
| 33 | } | 33 | } |
| 34 | #endif /*CONFIG_MODULES*/ | 34 | #endif /*CONFIG_MODULES*/ |
| 35 | 35 | ||
| 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 | |||
| 36 | #endif /* _LINUX_EXTABLE_H */ | 46 | #endif /* _LINUX_EXTABLE_H */ |
| @@ -65,6 +65,9 @@ struct ctl_table_header; · 64 unmodified lines | |||
| 65 | /* unused opcode to mark special call to bpf_tail_call() helper */ | 65 | /* unused opcode to mark special call to bpf_tail_call() helper */ |
| 66 | #define BPF_TAIL_CALL 0xf0 | 66 | #define BPF_TAIL_CALL 0xf0 |
| 67 | 67 | ||
| 68 | /* unused opcode to mark special load instruction. Same as BPF_ABS */ | ||
| 69 | #define BPF_PROBE_MEM 0x20 | ||
| 70 | |||
| 68 | /* unused opcode to mark call to interpreter with arguments */ | 71 | /* unused opcode to mark call to interpreter with arguments */ |
| 69 | #define BPF_CALL_ARGS 0xe0 | 72 | #define BPF_CALL_ARGS 0xe0 |
| 70 | 73 | ||
| @@ -464,10 +467,11 @@ static inline bool insn_is_zext(const struct bpf_insn *insn) · 393 unmodified lines | |||
| 464 | #define BPF_CALL_x(x, name, ...) \ | 467 | #define BPF_CALL_x(x, name, ...) \ |
| 465 | static __always_inline \ | 468 | static __always_inline \ |
| 466 | u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \ | 469 | u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \ |
| 470 | typedef u64 (*btf_##name)(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \ | ||
| 467 | u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)); \ | 471 | u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)); \ |
| 468 | u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)) \ | 472 | u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)) \ |
| 469 | { \ | 473 | { \ |
| 470 | return ____##name(__BPF_MAP(x,__BPF_CAST,__BPF_N,__VA_ARGS__));\ | 474 | return ((btf_##name)____##name)(__BPF_MAP(x,__BPF_CAST,__BPF_N,__VA_ARGS__));\ |
| 471 | } \ | 475 | } \ |
| 472 | static __always_inline \ | 476 | static __always_inline \ |
| 473 | u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)) | 477 | u64 ____##name(__BPF_MAP(x, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)) |