f2fs: Provide a splice-read wrapper
[linux-block.git] / Documentation / bpf / bpf_design_QA.rst
CommitLineData
1a6ac1d5
JDB
1==============
2BPF Design Q&A
3==============
4
2e39748a
AS
5BPF 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
1a6ac1d5
JDB
11.. contents::
12 :local:
13 :depth: 3
14
15Questions and Answers
16=====================
17
2e39748a 18Q: Is BPF a generic instruction set similar to x64 and arm64?
1a6ac1d5 19-------------------------------------------------------------
2e39748a
AS
20A: NO.
21
22Q: Is BPF a generic virtual machine ?
1a6ac1d5 23-------------------------------------
2e39748a
AS
24A: NO.
25
1a6ac1d5
JDB
26BPF is generic instruction set *with* C calling convention.
27-----------------------------------------------------------
2e39748a
AS
28
29Q: Why C calling convention was chosen?
1a6ac1d5
JDB
30~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
2e39748a 32A: Because BPF programs are designed to run in the linux kernel
1a6ac1d5
JDB
33which 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.
2e39748a 38
46604676 39Q: Can multiple return values be supported in the future?
1a6ac1d5 40~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2e39748a
AS
41A: NO. BPF allows only register R0 to be used as return value.
42
46604676 43Q: Can more than 5 function arguments be supported in the future?
1a6ac1d5 44~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2e39748a 45A: NO. BPF calling convention only allows registers R1-R5 to be used
1a6ac1d5
JDB
46as arguments. BPF is not a standalone instruction set.
47(unlike x64 ISA that allows msft, cdecl and other conventions)
2e39748a 48
46604676 49Q: Can BPF programs access instruction pointer or return address?
1a6ac1d5 50-----------------------------------------------------------------
2e39748a
AS
51A: NO.
52
46604676 53Q: Can BPF programs access stack pointer ?
1a6ac1d5
JDB
54------------------------------------------
55A: NO.
56
57Only frame pointer (register R10) is accessible.
58From compiler point of view it's necessary to have stack pointer.
46604676 59For example, LLVM defines register R11 as stack pointer in its
1a6ac1d5 60BPF backend, but it makes sure that generated code never uses it.
2e39748a
AS
61
62Q: Does C-calling convention diminishes possible use cases?
1a6ac1d5
JDB
63-----------------------------------------------------------
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
46604676
AN
69BPF programs and programs call kernel helpers with zero overhead,
70as all of them were native C code. That is particularly the case
1a6ac1d5
JDB
71for JITed BPF programs that are indistinguishable from
72native kernel C code.
2e39748a
AS
73
74Q: Does it mean that 'innovative' extensions to BPF code are disallowed?
1a6ac1d5
JDB
75------------------------------------------------------------------------
76A: Soft yes.
77
46604676 78At least for now, until BPF core has support for
1a6ac1d5 79bpf-to-bpf calls, indirect calls, loops, global variables,
46604676 80jump tables, read-only sections, and all other normal constructs
1a6ac1d5 81that C code can produce.
2e39748a
AS
82
83Q: Can loops be supported in a safe way?
1a6ac1d5
JDB
84----------------------------------------
85A: It's not clear yet.
86
87BPF developers are trying to find a way to
3b880244
AS
88support 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
1a6ac1d5
JDB
115
116Instruction level questions
117---------------------------
118
119Q: LD_ABS and LD_IND instructions vs C code
120~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2e39748a
AS
121
122Q: How come LD_ABS and LD_IND instruction are present in BPF whereas
1a6ac1d5
JDB
123C code cannot express them and has to use builtin intrinsics?
124
2e39748a 125A: This is artifact of compatibility with classic BPF. Modern
1a6ac1d5
JDB
126networking code in BPF performs better without them.
127See 'direct packet access'.
2e39748a 128
1a6ac1d5
JDB
129Q: BPF instructions mapping not one-to-one to native CPU
130~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2e39748a 131Q: It seems not all BPF instructions are one-to-one to native CPU.
1a6ac1d5
JDB
132For example why BPF_JNE and other compare and jumps are not cpu-like?
133
2e39748a 134A: This was necessary to avoid introducing flags into ISA which are
1a6ac1d5 135impossible to make generic and efficient across CPU architectures.
2e39748a 136
46604676 137Q: Why BPF_DIV instruction doesn't map to x64 div?
1a6ac1d5 138~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2e39748a 139A: Because if we picked one-to-one relationship to x64 it would have made
1a6ac1d5
JDB
140it more complicated to support on arm64 and other archs. Also it
141needs div-by-zero runtime check.
2e39748a 142
46604676 143Q: Why there is no BPF_SDIV for signed divide operation?
1a6ac1d5 144~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2e39748a 145A: Because it would be rarely used. llvm errors in such case and
46604676 146prints a suggestion to use unsigned divide instead.
2e39748a
AS
147
148Q: Why BPF has implicit prologue and epilogue?
1a6ac1d5 149~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2e39748a 150A: Because architectures like sparc have register windows and in general
1a6ac1d5
JDB
151there 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.
2e39748a
AS
156
157Q: Why BPF_JLT and BPF_JLE instructions were not introduced in the beginning?
1a6ac1d5 158~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2e39748a 159A: Because classic BPF didn't have them and BPF authors felt that compiler
1a6ac1d5
JDB
160workaround 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.
167
168Q: BPF 32-bit subregister requirements
169~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2e39748a 170Q: BPF 32-bit subregisters have a requirement to zero upper 32-bits of BPF
1a6ac1d5
JDB
171registers 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
c231c22a
JW
175A: 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.
2e39748a
AS
200
201Q: Does BPF have a stable ABI?
1a6ac1d5 202------------------------------
2e39748a 203A: YES. BPF instructions, arguments to BPF programs, set of helper
1a6ac1d5 204functions and their arguments, recognized return codes are all part
a769fa72
DB
205of 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.
2e39748a 210
27b53b73
THJ
211New BPF functionality is generally added through the use of kfuncs instead of
212new helpers. Kfuncs are not considered part of the stable API, and have their own
213lifecycle expectations as described in :ref:`BPF_kfunc_lifecycle_expectations`.
214
6939f4ef
QY
215Q: Are tracepoints part of the stable ABI?
216------------------------------------------
217A: NO. Tracepoints are tied to internal implementation details hence they are
218subject to change and can break with newer kernels. BPF programs need to change
219accordingly when this happens.
220
b9b738ee
PM
221Q: Are places where kprobes can attach part of the stable ABI?
222--------------------------------------------------------------
223A: NO. The places to which kprobes can attach are internal implementation
224details, which means that they are subject to change and can break with
225newer kernels. BPF programs need to change accordingly when this happens.
226
2e39748a 227Q: How much stack space a BPF program uses?
1a6ac1d5 228-------------------------------------------
2e39748a 229A: Currently all program types are limited to 512 bytes of stack
1a6ac1d5
JDB
230space, but the verifier computes the actual amount of stack used
231and both interpreter and most JITed code consume necessary amount.
2e39748a
AS
232
233Q: Can BPF be offloaded to HW?
1a6ac1d5 234------------------------------
2e39748a
AS
235A: YES. BPF HW offload is supported by NFP driver.
236
237Q: Does classic BPF interpreter still exist?
1a6ac1d5 238--------------------------------------------
2e39748a
AS
239A: NO. Classic BPF programs are converted into extend BPF instructions.
240
241Q: Can BPF call arbitrary kernel functions?
1a6ac1d5 242-------------------------------------------
27b53b73
THJ
243A: NO. BPF programs can only call specific functions exposed as BPF helpers or
244kfuncs. The set of available functions is defined for every program type.
2e39748a
AS
245
246Q: Can BPF overwrite arbitrary kernel memory?
1a6ac1d5
JDB
247---------------------------------------------
248A: NO.
249
250Tracing bpf programs can *read* arbitrary memory with bpf_probe_read()
251and bpf_probe_read_str() helpers. Networking programs cannot read
252arbitrary memory, since they don't have access to these helpers.
253Programs can never read or write arbitrary memory directly.
2e39748a
AS
254
255Q: Can BPF overwrite arbitrary user memory?
1a6ac1d5
JDB
256-------------------------------------------
257A: Sort-of.
258
259Tracing BPF programs can overwrite the user memory
260of the current task with bpf_probe_write_user(). Every time such
261program is loaded the kernel will print warning message, so
262this helper is only useful for experiments and prototypes.
263Tracing BPF programs are root only.
2e39748a 264
1a6ac1d5
JDB
265Q: New functionality via kernel modules?
266----------------------------------------
2e39748a 267Q: Can BPF functionality such as new program or map types, new
1a6ac1d5
JDB
268helpers, etc be added out of kernel module code?
269
27b53b73
THJ
270A: Yes, through kfuncs and kptrs
271
272The core BPF functionality such as program types, maps and helpers cannot be
273added to by modules. However, modules can expose functionality to BPF programs
274by exporting kfuncs (which may return pointers to module-internal data
275structures as kptrs).
5bdca94f
MKL
276
277Q: Directly calling kernel function is an ABI?
278----------------------------------------------
279Q: Some kernel functions (e.g. tcp_slow_start) can be called
280by BPF programs. Do these kernel functions become an ABI?
281
282A: NO.
283
284The kernel function protos will change and the bpf programs will be
285rejected by the verifier. Also, for example, some of the bpf-callable
286kernel functions have already been used by other kernel tcp
287cc (congestion-control) implementations. If any of these kernel
288functions has changed, both the in-tree and out-of-tree kernel tcp cc
289implementations have to be changed. The same goes for the bpf
27b53b73
THJ
290programs and they have to be adjusted accordingly. See
291:ref:`BPF_kfunc_lifecycle_expectations` for details.
62fc770d
PM
292
293Q: Attaching to arbitrary kernel functions is an ABI?
294-----------------------------------------------------
295Q: BPF programs can be attached to many kernel functions. Do these
296kernel functions become part of the ABI?
297
298A: NO.
299
300The kernel function prototypes will change, and BPF programs attaching to
301them will need to change. The BPF compile-once-run-everywhere (CO-RE)
302should be used in order to make it easier to adapt your BPF programs to
303different versions of the kernel.
8fcf1969
PM
304
305Q: Marking a function with BTF_ID makes that function an ABI?
306-------------------------------------------------------------
307A: NO.
308
309The BTF_ID macro does not cause a function to become part of the ABI
310any more than does the EXPORT_SYMBOL_GPL macro.
9805af8d
KKD
311
312Q: What is the compatibility story for special BPF types in map values?
313-----------------------------------------------------------------------
314Q: Users are allowed to embed bpf_spin_lock, bpf_timer fields in their BPF map
315values (when using BTF support for BPF maps). This allows to use helpers for
316such objects on these fields inside map values. Users are also allowed to embed
03b77e17 317pointers to some kernel types (with __kptr_untrusted and __kptr BTF tags). Will the
9805af8d
KKD
318kernel preserve backwards compatibility for these features?
319
320A: It depends. For bpf_spin_lock, bpf_timer: YES, for kptr and everything else:
321NO, but see below.
322
323For struct types that have been added already, like bpf_spin_lock and bpf_timer,
324the kernel will preserve backwards compatibility, as they are part of UAPI.
325
326For kptrs, they are also part of UAPI, but only with respect to the kptr
03b77e17 327mechanism. The types that you can use with a __kptr_untrusted and __kptr tagged
9805af8d
KKD
328pointer in your struct are NOT part of the UAPI contract. The supported types can
329and will change across kernel releases. However, operations like accessing kptr
330fields and bpf_kptr_xchg() helper will continue to be supported across kernel
331releases for the supported types.
332
333For any other supported struct type, unless explicitly stated in this document
334and added to bpf.h UAPI header, such types can and will arbitrarily change their
335size, type, and alignment, or any other user visible API or ABI detail across
336kernel releases. The users must adapt their BPF programs to the new changes and
337update them to make sure their programs continue to work correctly.
338
25906092 339NOTE: BPF subsystem specially reserves the 'bpf\_' prefix for type names, in
9805af8d 340order to introduce more special fields in the future. Hence, user programs must
25906092
BS
341avoid defining types with 'bpf\_' prefix to not be broken in future releases.
342In other words, no backwards compatibility is guaranteed if one using a type
343in BTF with 'bpf\_' prefix.
9805af8d 344
1f6d52f1
KKD
345Q: What is the compatibility story for special BPF types in allocated objects?
346------------------------------------------------------------------------------
347Q: Same as above, but for allocated objects (i.e. objects allocated using
348bpf_obj_new for user defined types). Will the kernel preserve backwards
9805af8d
KKD
349compatibility for these features?
350
351A: NO.
352
27b53b73
THJ
353Unlike map value types, the API to work with allocated objects and any support
354for special fields inside them is exposed through kfuncs, and thus has the same
355lifecycle expectations as the kfuncs themselves. See
356:ref:`BPF_kfunc_lifecycle_expectations` for details.