blob: 12a246fcf6cb99d4b68a7bb50f81422396b9127e [file] [log] [blame]
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +02001==============
2BPF Design Q&A
3==============
4
Alexei Starovoitov2e397482017-10-30 19:39:56 -07005BPF extensibility and applicability to networking, tracing, security
6in the linux kernel and several user space implementations of BPF
7virtual machine led to a number of misunderstanding on what BPF actually is.
8This short QA is an attempt to address that and outline a direction
9of where BPF is heading long term.
10
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020011.. contents::
12 :local:
13 :depth: 3
14
15Questions and Answers
16=====================
17
Alexei Starovoitov2e397482017-10-30 19:39:56 -070018Q: Is BPF a generic instruction set similar to x64 and arm64?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020019-------------------------------------------------------------
Alexei Starovoitov2e397482017-10-30 19:39:56 -070020A: NO.
21
22Q: Is BPF a generic virtual machine ?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020023-------------------------------------
Alexei Starovoitov2e397482017-10-30 19:39:56 -070024A: NO.
25
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020026BPF is generic instruction set *with* C calling convention.
27-----------------------------------------------------------
Alexei Starovoitov2e397482017-10-30 19:39:56 -070028
29Q: Why C calling convention was chosen?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020030~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
Alexei Starovoitov2e397482017-10-30 19:39:56 -070032A: Because BPF programs are designed to run in the linux kernel
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020033which is written in C, hence BPF defines instruction set compatible
34with two most used architectures x64 and arm64 (and takes into
35consideration important quirks of other architectures) and
36defines calling convention that is compatible with C calling
37convention of the linux kernel on those architectures.
Alexei Starovoitov2e397482017-10-30 19:39:56 -070038
Andrii Nakryiko466046762019-02-28 17:12:21 -080039Q: Can multiple return values be supported in the future?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020040~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alexei Starovoitov2e397482017-10-30 19:39:56 -070041A: NO. BPF allows only register R0 to be used as return value.
42
Andrii Nakryiko466046762019-02-28 17:12:21 -080043Q: Can more than 5 function arguments be supported in the future?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020044~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alexei Starovoitov2e397482017-10-30 19:39:56 -070045A: NO. BPF calling convention only allows registers R1-R5 to be used
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020046as arguments. BPF is not a standalone instruction set.
47(unlike x64 ISA that allows msft, cdecl and other conventions)
Alexei Starovoitov2e397482017-10-30 19:39:56 -070048
Andrii Nakryiko466046762019-02-28 17:12:21 -080049Q: Can BPF programs access instruction pointer or return address?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020050-----------------------------------------------------------------
Alexei Starovoitov2e397482017-10-30 19:39:56 -070051A: NO.
52
Andrii Nakryiko466046762019-02-28 17:12:21 -080053Q: Can BPF programs access stack pointer ?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020054------------------------------------------
55A: NO.
56
57Only frame pointer (register R10) is accessible.
58From compiler point of view it's necessary to have stack pointer.
Andrii Nakryiko466046762019-02-28 17:12:21 -080059For example, LLVM defines register R11 as stack pointer in its
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020060BPF backend, but it makes sure that generated code never uses it.
Alexei Starovoitov2e397482017-10-30 19:39:56 -070061
62Q: Does C-calling convention diminishes possible use cases?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020063-----------------------------------------------------------
64A: YES.
65
66BPF design forces addition of major functionality in the form
67of kernel helper functions and kernel objects like BPF maps with
68seamless interoperability between them. It lets kernel call into
Andrii Nakryiko466046762019-02-28 17:12:21 -080069BPF programs and programs call kernel helpers with zero overhead,
70as all of them were native C code. That is particularly the case
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020071for JITed BPF programs that are indistinguishable from
72native kernel C code.
Alexei Starovoitov2e397482017-10-30 19:39:56 -070073
74Q: Does it mean that 'innovative' extensions to BPF code are disallowed?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020075------------------------------------------------------------------------
76A: Soft yes.
77
Andrii Nakryiko466046762019-02-28 17:12:21 -080078At least for now, until BPF core has support for
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020079bpf-to-bpf calls, indirect calls, loops, global variables,
Andrii Nakryiko466046762019-02-28 17:12:21 -080080jump tables, read-only sections, and all other normal constructs
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020081that C code can produce.
Alexei Starovoitov2e397482017-10-30 19:39:56 -070082
83Q: Can loops be supported in a safe way?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +020084----------------------------------------
85A: It's not clear yet.
86
87BPF developers are trying to find a way to
Alexei Starovoitov3b880242019-04-17 18:27:01 -070088support bounded loops.
89
90Q: What are the verifier limits?
91--------------------------------
92A: The only limit known to the user space is BPF_MAXINSNS (4096).
93It's the maximum number of instructions that the unprivileged bpf
94program can have. The verifier has various internal limits.
95Like the maximum number of instructions that can be explored during
96program analysis. Currently, that limit is set to 1 million.
97Which essentially means that the largest program can consist
98of 1 million NOP instructions. There is a limit to the maximum number
99of subsequent branches, a limit to the number of nested bpf-to-bpf
100calls, a limit to the number of the verifier states per instruction,
101a limit to the number of maps used by the program.
102All these limits can be hit with a sufficiently complex program.
103There are also non-numerical limits that can cause the program
104to be rejected. The verifier used to recognize only pointer + constant
105expressions. Now it can recognize pointer + bounded_register.
106bpf_lookup_map_elem(key) had a requirement that 'key' must be
107a pointer to the stack. Now, 'key' can be a pointer to map value.
108The verifier is steadily getting 'smarter'. The limits are
109being removed. The only way to know that the program is going to
110be accepted by the verifier is to try to load it.
111The bpf development process guarantees that the future kernel
112versions will accept all bpf programs that were accepted by
113the earlier versions.
114
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200115
116Instruction level questions
117---------------------------
118
119Q: LD_ABS and LD_IND instructions vs C code
120~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700121
122Q: How come LD_ABS and LD_IND instruction are present in BPF whereas
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200123C code cannot express them and has to use builtin intrinsics?
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700124
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200125A: This is artifact of compatibility with classic BPF. Modern
126networking code in BPF performs better without them.
127See 'direct packet access'.
128
129Q: BPF instructions mapping not one-to-one to native CPU
130~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700131Q: It seems not all BPF instructions are one-to-one to native CPU.
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200132For example why BPF_JNE and other compare and jumps are not cpu-like?
133
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700134A: This was necessary to avoid introducing flags into ISA which are
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200135impossible to make generic and efficient across CPU architectures.
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700136
Andrii Nakryiko466046762019-02-28 17:12:21 -0800137Q: Why BPF_DIV instruction doesn't map to x64 div?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200138~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700139A: Because if we picked one-to-one relationship to x64 it would have made
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200140it more complicated to support on arm64 and other archs. Also it
141needs div-by-zero runtime check.
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700142
Andrii Nakryiko466046762019-02-28 17:12:21 -0800143Q: Why there is no BPF_SDIV for signed divide operation?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200144~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700145A: Because it would be rarely used. llvm errors in such case and
Andrii Nakryiko466046762019-02-28 17:12:21 -0800146prints a suggestion to use unsigned divide instead.
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700147
148Q: Why BPF has implicit prologue and epilogue?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200149~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700150A: Because architectures like sparc have register windows and in general
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200151there are enough subtle differences between architectures, so naive
152store return address into stack won't work. Another reason is BPF has
153to be safe from division by zero (and legacy exception path
154of LD_ABS insn). Those instructions need to invoke epilogue and
155return implicitly.
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700156
157Q: Why BPF_JLT and BPF_JLE instructions were not introduced in the beginning?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200158~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700159A: Because classic BPF didn't have them and BPF authors felt that compiler
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200160workaround would be acceptable. Turned out that programs lose performance
161due to lack of these compare instructions and they were added.
162These two instructions is a perfect example what kind of new BPF
163instructions are acceptable and can be added in the future.
164These two already had equivalent instructions in native CPUs.
165New instructions that don't have one-to-one mapping to HW instructions
166will not be accepted.
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700167
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200168Q: BPF 32-bit subregister requirements
169~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700170Q: BPF 32-bit subregisters have a requirement to zero upper 32-bits of BPF
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200171registers which makes BPF inefficient virtual machine for 32-bit
172CPU architectures and 32-bit HW accelerators. Can true 32-bit registers
173be added to BPF in the future?
174
Jiong Wangc231c222019-05-30 21:23:18 +0100175A: NO.
176
177But some optimizations on zero-ing the upper 32 bits for BPF registers are
178available, and can be leveraged to improve the performance of JITed BPF
179programs for 32-bit architectures.
180
181Starting with version 7, LLVM is able to generate instructions that operate
182on 32-bit subregisters, provided the option -mattr=+alu32 is passed for
183compiling a program. Furthermore, the verifier can now mark the
184instructions for which zero-ing the upper bits of the destination register
185is required, and insert an explicit zero-extension (zext) instruction
186(a mov32 variant). This means that for architectures without zext hardware
187support, the JIT back-ends do not need to clear the upper bits for
188subregisters written by alu32 instructions or narrow loads. Instead, the
189back-ends simply need to support code generation for that mov32 variant,
190and to overwrite bpf_jit_needs_zext() to make it return "true" (in order to
191enable zext insertion in the verifier).
192
193Note that it is possible for a JIT back-end to have partial hardware
194support for zext. In that case, if verifier zext insertion is enabled,
195it could lead to the insertion of unnecessary zext instructions. Such
196instructions could be removed by creating a simple peephole inside the JIT
197back-end: if one instruction has hardware support for zext and if the next
198instruction is an explicit zext, then the latter can be skipped when doing
199the code generation.
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700200
201Q: Does BPF have a stable ABI?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200202------------------------------
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700203A: YES. BPF instructions, arguments to BPF programs, set of helper
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200204functions and their arguments, recognized return codes are all part
Daniel Borkmanna769fa72019-01-07 22:57:17 +0100205of ABI. However there is one specific exception to tracing programs
206which are using helpers like bpf_probe_read() to walk kernel internal
207data structures and compile with kernel internal headers. Both of these
208kernel internals are subject to change and can break with newer kernels
209such that the program needs to be adapted accordingly.
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700210
211Q: How much stack space a BPF program uses?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200212-------------------------------------------
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700213A: Currently all program types are limited to 512 bytes of stack
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200214space, but the verifier computes the actual amount of stack used
215and both interpreter and most JITed code consume necessary amount.
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700216
217Q: Can BPF be offloaded to HW?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200218------------------------------
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700219A: YES. BPF HW offload is supported by NFP driver.
220
221Q: Does classic BPF interpreter still exist?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200222--------------------------------------------
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700223A: NO. Classic BPF programs are converted into extend BPF instructions.
224
225Q: Can BPF call arbitrary kernel functions?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200226-------------------------------------------
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700227A: NO. BPF programs can only call a set of helper functions which
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200228is defined for every program type.
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700229
230Q: Can BPF overwrite arbitrary kernel memory?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200231---------------------------------------------
232A: NO.
233
234Tracing bpf programs can *read* arbitrary memory with bpf_probe_read()
235and bpf_probe_read_str() helpers. Networking programs cannot read
236arbitrary memory, since they don't have access to these helpers.
237Programs can never read or write arbitrary memory directly.
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700238
239Q: Can BPF overwrite arbitrary user memory?
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200240-------------------------------------------
241A: Sort-of.
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700242
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200243Tracing BPF programs can overwrite the user memory
244of the current task with bpf_probe_write_user(). Every time such
245program is loaded the kernel will print warning message, so
246this helper is only useful for experiments and prototypes.
247Tracing BPF programs are root only.
248
249Q: bpf_trace_printk() helper warning
250------------------------------------
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700251Q: When bpf_trace_printk() helper is used the kernel prints nasty
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200252warning message. Why is that?
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700253
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200254A: This is done to nudge program authors into better interfaces when
255programs need to pass data to user space. Like bpf_perf_event_output()
256can be used to efficiently stream data via perf ring buffer.
257BPF maps can be used for asynchronous data sharing between kernel
258and user space. bpf_trace_printk() should only be used for debugging.
259
260Q: New functionality via kernel modules?
261----------------------------------------
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700262Q: Can BPF functionality such as new program or map types, new
Jesper Dangaard Brouer1a6ac1d2018-05-14 15:42:22 +0200263helpers, etc be added out of kernel module code?
264
Alexei Starovoitov2e397482017-10-30 19:39:56 -0700265A: NO.