brintos

brintos / linux-shallow public Read only

0
0
Text · 15.7 KiB · eb19c94 Raw
352 lines · plain
1==============2BPF Design Q&A3==============4 5BPF extensibility and applicability to networking, tracing, security6in the linux kernel and several user space implementations of BPF7virtual machine led to a number of misunderstanding on what BPF actually is.8This short QA is an attempt to address that and outline a direction9of where BPF is heading long term.10 11.. contents::12    :local:13    :depth: 314 15Questions and Answers16=====================17 18Q: Is BPF a generic instruction set similar to x64 and arm64?19-------------------------------------------------------------20A: NO.21 22Q: Is BPF a generic virtual machine ?23-------------------------------------24A: NO.25 26BPF is generic instruction set *with* C calling convention.27-----------------------------------------------------------28 29Q: Why C calling convention was chosen?30~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~31 32A: Because BPF programs are designed to run in the linux kernel33which is written in C, hence BPF defines instruction set compatible34with two most used architectures x64 and arm64 (and takes into35consideration important quirks of other architectures) and36defines calling convention that is compatible with C calling37convention of the linux kernel on those architectures.38 39Q: Can multiple return values be supported in the future?40~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~41A: NO. BPF allows only register R0 to be used as return value.42 43Q: Can more than 5 function arguments be supported in the future?44~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~45A: NO. BPF calling convention only allows registers R1-R5 to be used46as arguments. BPF is not a standalone instruction set.47(unlike x64 ISA that allows msft, cdecl and other conventions)48 49Q: Can BPF programs access instruction pointer or return address?50-----------------------------------------------------------------51A: NO.52 53Q: Can BPF programs access stack pointer ?54------------------------------------------55A: NO.56 57Only frame pointer (register R10) is accessible.58From compiler point of view it's necessary to have stack pointer.59For example, LLVM defines register R11 as stack pointer in its60BPF backend, but it makes sure that generated code never uses it.61 62Q: Does C-calling convention diminishes possible use cases?63-----------------------------------------------------------64A: YES.65 66BPF design forces addition of major functionality in the form67of kernel helper functions and kernel objects like BPF maps with68seamless interoperability between them. It lets kernel call into69BPF programs and programs call kernel helpers with zero overhead,70as all of them were native C code. That is particularly the case71for JITed BPF programs that are indistinguishable from72native kernel C code.73 74Q: Does it mean that 'innovative' extensions to BPF code are disallowed?75------------------------------------------------------------------------76A: Soft yes.77 78At least for now, until BPF core has support for79bpf-to-bpf calls, indirect calls, loops, global variables,80jump tables, read-only sections, and all other normal constructs81that C code can produce.82 83Q: Can loops be supported in a safe way?84----------------------------------------85A: It's not clear yet.86 87BPF developers are trying to find a way to88support 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 bpf94program can have. The verifier has various internal limits.95Like the maximum number of instructions that can be explored during96program analysis. Currently, that limit is set to 1 million.97Which essentially means that the largest program can consist98of 1 million NOP instructions. There is a limit to the maximum number99of subsequent branches, a limit to the number of nested bpf-to-bpf100calls, 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 program104to be rejected. The verifier used to recognize only pointer + constant105expressions. Now it can recognize pointer + bounded_register.106bpf_lookup_map_elem(key) had a requirement that 'key' must be107a pointer to the stack. Now, 'key' can be a pointer to map value.108The verifier is steadily getting 'smarter'. The limits are109being removed. The only way to know that the program is going to110be accepted by the verifier is to try to load it.111The bpf development process guarantees that the future kernel112versions will accept all bpf programs that were accepted by113the earlier versions.114 115 116Instruction level questions117---------------------------118 119Q: LD_ABS and LD_IND instructions vs C code120~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~121 122Q: How come LD_ABS and LD_IND instruction are present in BPF whereas123C code cannot express them and has to use builtin intrinsics?124 125A: This is artifact of compatibility with classic BPF. Modern126networking code in BPF performs better without them.127See 'direct packet access'.128 129Q: BPF instructions mapping not one-to-one to native CPU130~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~131Q: It seems not all BPF instructions are one-to-one to native CPU.132For example why BPF_JNE and other compare and jumps are not cpu-like?133 134A: This was necessary to avoid introducing flags into ISA which are135impossible to make generic and efficient across CPU architectures.136 137Q: Why BPF_DIV instruction doesn't map to x64 div?138~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~139A: Because if we picked one-to-one relationship to x64 it would have made140it more complicated to support on arm64 and other archs. Also it141needs div-by-zero runtime check.142 143Q: Why BPF has implicit prologue and epilogue?144~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~145A: Because architectures like sparc have register windows and in general146there are enough subtle differences between architectures, so naive147store return address into stack won't work. Another reason is BPF has148to be safe from division by zero (and legacy exception path149of LD_ABS insn). Those instructions need to invoke epilogue and150return implicitly.151 152Q: Why BPF_JLT and BPF_JLE instructions were not introduced in the beginning?153~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~154A: Because classic BPF didn't have them and BPF authors felt that compiler155workaround would be acceptable. Turned out that programs lose performance156due to lack of these compare instructions and they were added.157These two instructions is a perfect example what kind of new BPF158instructions are acceptable and can be added in the future.159These two already had equivalent instructions in native CPUs.160New instructions that don't have one-to-one mapping to HW instructions161will not be accepted.162 163Q: BPF 32-bit subregister requirements164~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~165Q: BPF 32-bit subregisters have a requirement to zero upper 32-bits of BPF166registers which makes BPF inefficient virtual machine for 32-bit167CPU architectures and 32-bit HW accelerators. Can true 32-bit registers168be added to BPF in the future?169 170A: NO.171 172But some optimizations on zero-ing the upper 32 bits for BPF registers are173available, and can be leveraged to improve the performance of JITed BPF174programs for 32-bit architectures.175 176Starting with version 7, LLVM is able to generate instructions that operate177on 32-bit subregisters, provided the option -mattr=+alu32 is passed for178compiling a program. Furthermore, the verifier can now mark the179instructions for which zero-ing the upper bits of the destination register180is required, and insert an explicit zero-extension (zext) instruction181(a mov32 variant). This means that for architectures without zext hardware182support, the JIT back-ends do not need to clear the upper bits for183subregisters written by alu32 instructions or narrow loads. Instead, the184back-ends simply need to support code generation for that mov32 variant,185and to overwrite bpf_jit_needs_zext() to make it return "true" (in order to186enable zext insertion in the verifier).187 188Note that it is possible for a JIT back-end to have partial hardware189support for zext. In that case, if verifier zext insertion is enabled,190it could lead to the insertion of unnecessary zext instructions. Such191instructions could be removed by creating a simple peephole inside the JIT192back-end: if one instruction has hardware support for zext and if the next193instruction is an explicit zext, then the latter can be skipped when doing194the code generation.195 196Q: Does BPF have a stable ABI?197------------------------------198A: YES. BPF instructions, arguments to BPF programs, set of helper199functions and their arguments, recognized return codes are all part200of ABI. However there is one specific exception to tracing programs201which are using helpers like bpf_probe_read() to walk kernel internal202data structures and compile with kernel internal headers. Both of these203kernel internals are subject to change and can break with newer kernels204such that the program needs to be adapted accordingly.205 206New BPF functionality is generally added through the use of kfuncs instead of207new helpers. Kfuncs are not considered part of the stable API, and have their own208lifecycle expectations as described in :ref:`BPF_kfunc_lifecycle_expectations`.209 210Q: Are tracepoints part of the stable ABI?211------------------------------------------212A: NO. Tracepoints are tied to internal implementation details hence they are213subject to change and can break with newer kernels. BPF programs need to change214accordingly when this happens.215 216Q: Are places where kprobes can attach part of the stable ABI?217--------------------------------------------------------------218A: NO. The places to which kprobes can attach are internal implementation219details, which means that they are subject to change and can break with220newer kernels. BPF programs need to change accordingly when this happens.221 222Q: How much stack space a BPF program uses?223-------------------------------------------224A: Currently all program types are limited to 512 bytes of stack225space, but the verifier computes the actual amount of stack used226and both interpreter and most JITed code consume necessary amount.227 228Q: Can BPF be offloaded to HW?229------------------------------230A: YES. BPF HW offload is supported by NFP driver.231 232Q: Does classic BPF interpreter still exist?233--------------------------------------------234A: NO. Classic BPF programs are converted into extend BPF instructions.235 236Q: Can BPF call arbitrary kernel functions?237-------------------------------------------238A: NO. BPF programs can only call specific functions exposed as BPF helpers or239kfuncs. The set of available functions is defined for every program type.240 241Q: Can BPF overwrite arbitrary kernel memory?242---------------------------------------------243A: NO.244 245Tracing bpf programs can *read* arbitrary memory with bpf_probe_read()246and bpf_probe_read_str() helpers. Networking programs cannot read247arbitrary memory, since they don't have access to these helpers.248Programs can never read or write arbitrary memory directly.249 250Q: Can BPF overwrite arbitrary user memory?251-------------------------------------------252A: Sort-of.253 254Tracing BPF programs can overwrite the user memory255of the current task with bpf_probe_write_user(). Every time such256program is loaded the kernel will print warning message, so257this helper is only useful for experiments and prototypes.258Tracing BPF programs are root only.259 260Q: New functionality via kernel modules?261----------------------------------------262Q: Can BPF functionality such as new program or map types, new263helpers, etc be added out of kernel module code?264 265A: Yes, through kfuncs and kptrs266 267The core BPF functionality such as program types, maps and helpers cannot be268added to by modules. However, modules can expose functionality to BPF programs269by exporting kfuncs (which may return pointers to module-internal data270structures as kptrs).271 272Q: Directly calling kernel function is an ABI?273----------------------------------------------274Q: Some kernel functions (e.g. tcp_slow_start) can be called275by BPF programs.  Do these kernel functions become an ABI?276 277A: NO.278 279The kernel function protos will change and the bpf programs will be280rejected by the verifier.  Also, for example, some of the bpf-callable281kernel functions have already been used by other kernel tcp282cc (congestion-control) implementations.  If any of these kernel283functions has changed, both the in-tree and out-of-tree kernel tcp cc284implementations have to be changed.  The same goes for the bpf285programs and they have to be adjusted accordingly. See286:ref:`BPF_kfunc_lifecycle_expectations` for details.287 288Q: Attaching to arbitrary kernel functions is an ABI?289-----------------------------------------------------290Q: BPF programs can be attached to many kernel functions.  Do these291kernel functions become part of the ABI?292 293A: NO.294 295The kernel function prototypes will change, and BPF programs attaching to296them will need to change.  The BPF compile-once-run-everywhere (CO-RE)297should be used in order to make it easier to adapt your BPF programs to298different versions of the kernel.299 300Q: Marking a function with BTF_ID makes that function an ABI?301-------------------------------------------------------------302A: NO.303 304The BTF_ID macro does not cause a function to become part of the ABI305any more than does the EXPORT_SYMBOL_GPL macro.306 307Q: What is the compatibility story for special BPF types in map values?308-----------------------------------------------------------------------309Q: Users are allowed to embed bpf_spin_lock, bpf_timer fields in their BPF map310values (when using BTF support for BPF maps). This allows to use helpers for311such objects on these fields inside map values. Users are also allowed to embed312pointers to some kernel types (with __kptr_untrusted and __kptr BTF tags). Will the313kernel preserve backwards compatibility for these features?314 315A: It depends. For bpf_spin_lock, bpf_timer: YES, for kptr and everything else:316NO, but see below.317 318For struct types that have been added already, like bpf_spin_lock and bpf_timer,319the kernel will preserve backwards compatibility, as they are part of UAPI.320 321For kptrs, they are also part of UAPI, but only with respect to the kptr322mechanism. The types that you can use with a __kptr_untrusted and __kptr tagged323pointer in your struct are NOT part of the UAPI contract. The supported types can324and will change across kernel releases. However, operations like accessing kptr325fields and bpf_kptr_xchg() helper will continue to be supported across kernel326releases for the supported types.327 328For any other supported struct type, unless explicitly stated in this document329and added to bpf.h UAPI header, such types can and will arbitrarily change their330size, type, and alignment, or any other user visible API or ABI detail across331kernel releases. The users must adapt their BPF programs to the new changes and332update them to make sure their programs continue to work correctly.333 334NOTE: BPF subsystem specially reserves the 'bpf\_' prefix for type names, in335order to introduce more special fields in the future. Hence, user programs must336avoid defining types with 'bpf\_' prefix to not be broken in future releases.337In other words, no backwards compatibility is guaranteed if one using a type338in BTF with 'bpf\_' prefix.339 340Q: What is the compatibility story for special BPF types in allocated objects?341------------------------------------------------------------------------------342Q: Same as above, but for allocated objects (i.e. objects allocated using343bpf_obj_new for user defined types). Will the kernel preserve backwards344compatibility for these features?345 346A: NO.347 348Unlike map value types, the API to work with allocated objects and any support349for special fields inside them is exposed through kfuncs, and thus has the same350lifecycle expectations as the kfuncs themselves. See351:ref:`BPF_kfunc_lifecycle_expectations` for details.352