Skip to content

Commit bb23581

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Daniel Borkmann says: ==================== pull-request: bpf-next 2019-04-12 The following pull-request contains BPF updates for your *net-next* tree. The main changes are: 1) Improve BPF verifier scalability for large programs through two optimizations: i) remove verifier states that are not useful in pruning, ii) stop walking parentage chain once first LIVE_READ is seen. Combined gives approx 20x speedup. Increase limits for accepting large programs under root, and add various stress tests, from Alexei. 2) Implement global data support in BPF. This enables static global variables for .data, .rodata and .bss sections to be properly handled which allows for more natural program development. This also opens up the possibility to optimize program workflow by compiling ELFs only once and later only rewriting section data before reload, from Daniel and with test cases and libbpf refactoring from Joe. 3) Add config option to generate BTF type info for vmlinux as part of the kernel build process. DWARF debug info is converted via pahole to BTF. Latter relies on libbpf and makes use of BTF deduplication algorithm which results in 100x savings compared to DWARF data. Resulting .BTF section is typically about 2MB in size, from Andrii. 4) Add BPF verifier support for stack access with variable offset from helpers and add various test cases along with it, from Andrey. 5) Extend bpf_skb_adjust_room() growth BPF helper to mark inner MAC header so that L2 encapsulation can be used for tc tunnels, from Alan. 6) Add support for input __sk_buff context in BPF_PROG_TEST_RUN so that users can define a subset of allowed __sk_buff fields that get fed into the test program, from Stanislav. 7) Add bpf fs multi-dimensional array tests for BTF test suite and fix up various UBSAN warnings in bpftool, from Yonghong. 8) Generate a pkg-config file for libbpf, from Luca. 9) Dump program's BTF id in bpftool, from Prashant. 10) libbpf fix to use smaller BPF log buffer size for AF_XDP's XDP program, from Magnus. 11) kallsyms related fixes for the case when symbols are not present in BPF selftests and samples, from Daniel ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 78f07ad + 947e8b5 commit bb23581

75 files changed

Lines changed: 4603 additions & 425 deletions

Some content is hidden

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

‎.mailmap‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Alan Cox <alan@lxorguk.ukuu.org.uk>
1616
Alan Cox <root@hraefn.swansea.linux.org.uk>
1717
Aleksey Gorelov <aleksey_gorelov@phoenix.com>
1818
Aleksandar Markovic <aleksandar.markovic@mips.com> <aleksandar.markovic@imgtec.com>
19+
Alexei Starovoitov <ast@kernel.org> <ast@plumgrid.com>
20+
Alexei Starovoitov <ast@kernel.org> <alexei.starovoitov@gmail.com>
21+
Alexei Starovoitov <ast@kernel.org> <ast@fb.com>
1922
Al Viro <viro@ftp.linux.org.uk>
2023
Al Viro <viro@zenIV.linux.org.uk>
2124
Andi Shyti <andi@etezian.org> <andi.shyti@samsung.com>
@@ -46,6 +49,12 @@ Christoph Hellwig <hch@lst.de>
4649
Christophe Ricard <christophe.ricard@gmail.com>
4750
Corey Minyard <minyard@acm.org>
4851
Damian Hobson-Garcia <dhobsong@igel.co.jp>
52+
Daniel Borkmann <daniel@iogearbox.net> <dborkman@redhat.com>
53+
Daniel Borkmann <daniel@iogearbox.net> <dborkmann@redhat.com>
54+
Daniel Borkmann <daniel@iogearbox.net> <danborkmann@iogearbox.net>
55+
Daniel Borkmann <daniel@iogearbox.net> <daniel.borkmann@tik.ee.ethz.ch>
56+
Daniel Borkmann <daniel@iogearbox.net> <danborkmann@googlemail.com>
57+
Daniel Borkmann <daniel@iogearbox.net> <dxchgb@gmail.com>
4958
David Brownell <david-b@pacbell.net>
5059
David Woodhouse <dwmw2@shinybook.infradead.org>
5160
Dengcheng Zhu <dzhu@wavecomp.com> <dengcheng.zhu@mips.com>

‎Documentation/bpf/btf.rst‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ sequentially and type id is assigned to each recognized type starting from id
8282
#define BTF_KIND_RESTRICT 11 /* Restrict */
8383
#define BTF_KIND_FUNC 12 /* Function */
8484
#define BTF_KIND_FUNC_PROTO 13 /* Function Proto */
85+
#define BTF_KIND_VAR 14 /* Variable */
86+
#define BTF_KIND_DATASEC 15 /* Section */
8587

8688
Note that the type section encodes debug info, not just pure types.
8789
``BTF_KIND_FUNC`` is not a type, and it represents a defined subprogram.
@@ -393,6 +395,61 @@ refers to parameter type.
393395
If the function has variable arguments, the last parameter is encoded with
394396
``name_off = 0`` and ``type = 0``.
395397

398+
2.2.14 BTF_KIND_VAR
399+
~~~~~~~~~~~~~~~~~~~
400+
401+
``struct btf_type`` encoding requirement:
402+
* ``name_off``: offset to a valid C identifier
403+
* ``info.kind_flag``: 0
404+
* ``info.kind``: BTF_KIND_VAR
405+
* ``info.vlen``: 0
406+
* ``type``: the type of the variable
407+
408+
``btf_type`` is followed by a single ``struct btf_variable`` with the
409+
following data::
410+
411+
struct btf_var {
412+
__u32 linkage;
413+
};
414+
415+
``struct btf_var`` encoding:
416+
* ``linkage``: currently only static variable 0, or globally allocated
417+
variable in ELF sections 1
418+
419+
Not all type of global variables are supported by LLVM at this point.
420+
The following is currently available:
421+
422+
* static variables with or without section attributes
423+
* global variables with section attributes
424+
425+
The latter is for future extraction of map key/value type id's from a
426+
map definition.
427+
428+
2.2.15 BTF_KIND_DATASEC
429+
~~~~~~~~~~~~~~~~~~~~~~~
430+
431+
``struct btf_type`` encoding requirement:
432+
* ``name_off``: offset to a valid name associated with a variable or
433+
one of .data/.bss/.rodata
434+
* ``info.kind_flag``: 0
435+
* ``info.kind``: BTF_KIND_DATASEC
436+
* ``info.vlen``: # of variables
437+
* ``size``: total section size in bytes (0 at compilation time, patched
438+
to actual size by BPF loaders such as libbpf)
439+
440+
``btf_type`` is followed by ``info.vlen`` number of ``struct btf_var_secinfo``.::
441+
442+
struct btf_var_secinfo {
443+
__u32 type;
444+
__u32 offset;
445+
__u32 size;
446+
};
447+
448+
``struct btf_var_secinfo`` encoding:
449+
* ``type``: the type of the BTF_KIND_VAR variable
450+
* ``offset``: the in-section offset of the variable
451+
* ``size``: the size of the variable in bytes
452+
396453
3. BTF Kernel API
397454
*****************
398455

‎Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ NM = $(CROSS_COMPILE)nm
401401
STRIP = $(CROSS_COMPILE)strip
402402
OBJCOPY = $(CROSS_COMPILE)objcopy
403403
OBJDUMP = $(CROSS_COMPILE)objdump
404+
PAHOLE = pahole
404405
LEX = flex
405406
YACC = bison
406407
AWK = awk
@@ -455,7 +456,7 @@ KBUILD_LDFLAGS :=
455456
GCC_PLUGINS_CFLAGS :=
456457

457458
export ARCH SRCARCH CONFIG_SHELL HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE AS LD CC
458-
export CPP AR NM STRIP OBJCOPY OBJDUMP KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS
459+
export CPP AR NM STRIP OBJCOPY OBJDUMP PAHOLE KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS
459460
export MAKE LEX YACC AWK INSTALLKERNEL PERL PYTHON PYTHON2 PYTHON3 UTS_MACHINE
460461
export HOSTCXX KBUILD_HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS
461462

‎include/linux/bpf.h‎

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ struct bpf_map_ops {
5757
const struct btf *btf,
5858
const struct btf_type *key_type,
5959
const struct btf_type *value_type);
60+
61+
/* Direct value access helpers. */
62+
int (*map_direct_value_addr)(const struct bpf_map *map,
63+
u64 *imm, u32 off);
64+
int (*map_direct_value_meta)(const struct bpf_map *map,
65+
u64 imm, u32 *off);
6066
};
6167

6268
struct bpf_map {
@@ -81,7 +87,8 @@ struct bpf_map {
8187
struct btf *btf;
8288
u32 pages;
8389
bool unpriv_array;
84-
/* 51 bytes hole */
90+
bool frozen; /* write-once */
91+
/* 48 bytes hole */
8592

8693
/* The 3rd and 4th cacheline with misc members to avoid false sharing
8794
* particularly with refcounting.
@@ -421,8 +428,38 @@ struct bpf_array {
421428
};
422429
};
423430

431+
#define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
424432
#define MAX_TAIL_CALL_CNT 32
425433

434+
#define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
435+
BPF_F_RDONLY_PROG | \
436+
BPF_F_WRONLY | \
437+
BPF_F_WRONLY_PROG)
438+
439+
#define BPF_MAP_CAN_READ BIT(0)
440+
#define BPF_MAP_CAN_WRITE BIT(1)
441+
442+
static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
443+
{
444+
u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
445+
446+
/* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
447+
* not possible.
448+
*/
449+
if (access_flags & BPF_F_RDONLY_PROG)
450+
return BPF_MAP_CAN_READ;
451+
else if (access_flags & BPF_F_WRONLY_PROG)
452+
return BPF_MAP_CAN_WRITE;
453+
else
454+
return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
455+
}
456+
457+
static inline bool bpf_map_flags_access_ok(u32 access_flags)
458+
{
459+
return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
460+
(BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
461+
}
462+
426463
struct bpf_event_entry {
427464
struct perf_event *event;
428465
struct file *perf_file;
@@ -446,14 +483,6 @@ typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
446483
u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
447484
void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
448485

449-
int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
450-
union bpf_attr __user *uattr);
451-
int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
452-
union bpf_attr __user *uattr);
453-
int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
454-
const union bpf_attr *kattr,
455-
union bpf_attr __user *uattr);
456-
457486
/* an array of programs to be executed under rcu_lock.
458487
*
459488
* Typical usage:
@@ -644,6 +673,13 @@ static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
644673
struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
645674
int array_map_alloc_check(union bpf_attr *attr);
646675

676+
int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
677+
union bpf_attr __user *uattr);
678+
int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
679+
union bpf_attr __user *uattr);
680+
int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
681+
const union bpf_attr *kattr,
682+
union bpf_attr __user *uattr);
647683
#else /* !CONFIG_BPF_SYSCALL */
648684
static inline struct bpf_prog *bpf_prog_get(u32 ufd)
649685
{
@@ -755,6 +791,27 @@ static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
755791
{
756792
return ERR_PTR(-EOPNOTSUPP);
757793
}
794+
795+
static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
796+
const union bpf_attr *kattr,
797+
union bpf_attr __user *uattr)
798+
{
799+
return -ENOTSUPP;
800+
}
801+
802+
static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
803+
const union bpf_attr *kattr,
804+
union bpf_attr __user *uattr)
805+
{
806+
return -ENOTSUPP;
807+
}
808+
809+
static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
810+
const union bpf_attr *kattr,
811+
union bpf_attr __user *uattr)
812+
{
813+
return -ENOTSUPP;
814+
}
758815
#endif /* CONFIG_BPF_SYSCALL */
759816

760817
static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,

‎include/linux/bpf_verifier.h‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ struct bpf_verifier_state {
207207
struct bpf_verifier_state_list {
208208
struct bpf_verifier_state state;
209209
struct bpf_verifier_state_list *next;
210+
int miss_cnt, hit_cnt;
210211
};
211212

212213
/* Possible states for alu_state member. */
@@ -223,6 +224,10 @@ struct bpf_insn_aux_data {
223224
unsigned long map_state; /* pointer/poison value for maps */
224225
s32 call_imm; /* saved imm field of call insn */
225226
u32 alu_limit; /* limit for add/sub register with pointer */
227+
struct {
228+
u32 map_index; /* index into used_maps[] */
229+
u32 map_off; /* offset from value base address */
230+
};
226231
};
227232
int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
228233
int sanitize_stack_off; /* stack slot to be cleared */
@@ -248,6 +253,12 @@ static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log)
248253
return log->len_used >= log->len_total - 1;
249254
}
250255

256+
#define BPF_LOG_LEVEL1 1
257+
#define BPF_LOG_LEVEL2 2
258+
#define BPF_LOG_STATS 4
259+
#define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2)
260+
#define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS)
261+
251262
static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
252263
{
253264
return log->level && log->ubuf && !bpf_verifier_log_full(log);
@@ -274,6 +285,7 @@ struct bpf_verifier_env {
274285
bool strict_alignment; /* perform strict pointer alignment checks */
275286
struct bpf_verifier_state *cur_state; /* current verifier state */
276287
struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
288+
struct bpf_verifier_state_list *free_list;
277289
struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
278290
u32 used_map_cnt; /* number of used maps */
279291
u32 id_gen; /* used to generate unique reg IDs */
@@ -284,6 +296,21 @@ struct bpf_verifier_env {
284296
struct bpf_verifier_log log;
285297
struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1];
286298
u32 subprog_cnt;
299+
/* number of instructions analyzed by the verifier */
300+
u32 insn_processed;
301+
/* total verification time */
302+
u64 verification_time;
303+
/* maximum number of verifier states kept in 'branching' instructions */
304+
u32 max_states_per_insn;
305+
/* total number of allocated verifier states */
306+
u32 total_states;
307+
/* some states are freed during program analysis.
308+
* this is peak number of states. this number dominates kernel
309+
* memory consumption during verification
310+
*/
311+
u32 peak_states;
312+
/* longest register parentage chain walked for liveness marking */
313+
u32 longest_mark_read_walk;
287314
};
288315

289316
__printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log,

‎include/linux/btf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
5151
const struct btf_member *m,
5252
u32 expected_offset, u32 expected_size);
5353
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);
5455

5556
#ifdef CONFIG_BPF_SYSCALL
5657
const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);

‎include/uapi/linux/bpf.h‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ enum bpf_cmd {
105105
BPF_BTF_GET_FD_BY_ID,
106106
BPF_TASK_FD_QUERY,
107107
BPF_MAP_LOOKUP_AND_DELETE_ELEM,
108+
BPF_MAP_FREEZE,
108109
};
109110

110111
enum bpf_map_type {
@@ -255,8 +256,19 @@ enum bpf_attach_type {
255256
*/
256257
#define BPF_F_ANY_ALIGNMENT (1U << 1)
257258

258-
/* when bpf_ldimm64->src_reg == BPF_PSEUDO_MAP_FD, bpf_ldimm64->imm == fd */
259+
/* When BPF ldimm64's insn[0].src_reg != 0 then this can have
260+
* two extensions:
261+
*
262+
* insn[0].src_reg: BPF_PSEUDO_MAP_FD BPF_PSEUDO_MAP_VALUE
263+
* insn[0].imm: map fd map fd
264+
* insn[1].imm: 0 offset into value
265+
* insn[0].off: 0 0
266+
* insn[1].off: 0 0
267+
* ldimm64 rewrite: address of map address of map[0]+offset
268+
* verifier type: CONST_PTR_TO_MAP PTR_TO_MAP_VALUE
269+
*/
259270
#define BPF_PSEUDO_MAP_FD 1
271+
#define BPF_PSEUDO_MAP_VALUE 2
260272

261273
/* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative
262274
* offset to another bpf function
@@ -283,7 +295,7 @@ enum bpf_attach_type {
283295

284296
#define BPF_OBJ_NAME_LEN 16U
285297

286-
/* Flags for accessing BPF object */
298+
/* Flags for accessing BPF object from syscall side. */
287299
#define BPF_F_RDONLY (1U << 3)
288300
#define BPF_F_WRONLY (1U << 4)
289301

@@ -293,6 +305,10 @@ enum bpf_attach_type {
293305
/* Zero-initialize hash function seed. This should only be used for testing. */
294306
#define BPF_F_ZERO_SEED (1U << 6)
295307

308+
/* Flags for accessing BPF object from program side. */
309+
#define BPF_F_RDONLY_PROG (1U << 7)
310+
#define BPF_F_WRONLY_PROG (1U << 8)
311+
296312
/* flags for BPF_PROG_QUERY */
297313
#define BPF_F_QUERY_EFFECTIVE (1U << 0)
298314

@@ -396,6 +412,13 @@ union bpf_attr {
396412
__aligned_u64 data_out;
397413
__u32 repeat;
398414
__u32 duration;
415+
__u32 ctx_size_in; /* input: len of ctx_in */
416+
__u32 ctx_size_out; /* input/output: len of ctx_out
417+
* returns ENOSPC if ctx_out
418+
* is too small.
419+
*/
420+
__aligned_u64 ctx_in;
421+
__aligned_u64 ctx_out;
399422
} test;
400423

401424
struct { /* anonymous struct used by BPF_*_GET_*_ID */
@@ -1500,6 +1523,10 @@ union bpf_attr {
15001523
* * **BPF_F_ADJ_ROOM_ENCAP_L4_UDP **:
15011524
* Use with ENCAP_L3 flags to further specify the tunnel type.
15021525
*
1526+
* * **BPF_F_ADJ_ROOM_ENCAP_L2(len) **:
1527+
* Use with ENCAP_L3/L4 flags to further specify the tunnel
1528+
* type; **len** is the length of the inner MAC header.
1529+
*
15031530
* A call to this helper is susceptible to change the underlaying
15041531
* packet buffer. Therefore, at load time, all checks on pointers
15051532
* previously done by the verifier are invalidated and must be
@@ -2641,10 +2668,16 @@ enum bpf_func_id {
26412668
/* BPF_FUNC_skb_adjust_room flags. */
26422669
#define BPF_F_ADJ_ROOM_FIXED_GSO (1ULL << 0)
26432670

2671+
#define BPF_ADJ_ROOM_ENCAP_L2_MASK 0xff
2672+
#define BPF_ADJ_ROOM_ENCAP_L2_SHIFT 56
2673+
26442674
#define BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 (1ULL << 1)
26452675
#define BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 (1ULL << 2)
26462676
#define BPF_F_ADJ_ROOM_ENCAP_L4_GRE (1ULL << 3)
26472677
#define BPF_F_ADJ_ROOM_ENCAP_L4_UDP (1ULL << 4)
2678+
#define BPF_F_ADJ_ROOM_ENCAP_L2(len) (((__u64)len & \
2679+
BPF_ADJ_ROOM_ENCAP_L2_MASK) \
2680+
<< BPF_ADJ_ROOM_ENCAP_L2_SHIFT)
26482681

26492682
/* Mode for BPF_FUNC_skb_adjust_room helper. */
26502683
enum bpf_adj_room_mode {

0 commit comments

Comments
 (0)