bpf_design_QA.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. ==============
  2. BPF Design Q&A
  3. ==============
  4. BPF extensibility and applicability to networking, tracing, security
  5. in the linux kernel and several user space implementations of BPF
  6. virtual machine led to a number of misunderstanding on what BPF actually is.
  7. This short QA is an attempt to address that and outline a direction
  8. of where BPF is heading long term.
  9. .. contents::
  10. :local:
  11. :depth: 3
  12. Questions and Answers
  13. =====================
  14. Q: Is BPF a generic instruction set similar to x64 and arm64?
  15. -------------------------------------------------------------
  16. A: NO.
  17. Q: Is BPF a generic virtual machine ?
  18. -------------------------------------
  19. A: NO.
  20. BPF is generic instruction set *with* C calling convention.
  21. -----------------------------------------------------------
  22. Q: Why C calling convention was chosen?
  23. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. A: Because BPF programs are designed to run in the linux kernel
  25. which is written in C, hence BPF defines instruction set compatible
  26. with two most used architectures x64 and arm64 (and takes into
  27. consideration important quirks of other architectures) and
  28. defines calling convention that is compatible with C calling
  29. convention of the linux kernel on those architectures.
  30. Q: Can multiple return values be supported in the future?
  31. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  32. A: NO. BPF allows only register R0 to be used as return value.
  33. Q: Can more than 5 function arguments be supported in the future?
  34. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. A: NO. BPF calling convention only allows registers R1-R5 to be used
  36. as arguments. BPF is not a standalone instruction set.
  37. (unlike x64 ISA that allows msft, cdecl and other conventions)
  38. Q: Can BPF programs access instruction pointer or return address?
  39. -----------------------------------------------------------------
  40. A: NO.
  41. Q: Can BPF programs access stack pointer ?
  42. ------------------------------------------
  43. A: NO.
  44. Only frame pointer (register R10) is accessible.
  45. From compiler point of view it's necessary to have stack pointer.
  46. For example, LLVM defines register R11 as stack pointer in its
  47. BPF backend, but it makes sure that generated code never uses it.
  48. Q: Does C-calling convention diminishes possible use cases?
  49. -----------------------------------------------------------
  50. A: YES.
  51. BPF design forces addition of major functionality in the form
  52. of kernel helper functions and kernel objects like BPF maps with
  53. seamless interoperability between them. It lets kernel call into
  54. BPF programs and programs call kernel helpers with zero overhead,
  55. as all of them were native C code. That is particularly the case
  56. for JITed BPF programs that are indistinguishable from
  57. native kernel C code.
  58. Q: Does it mean that 'innovative' extensions to BPF code are disallowed?
  59. ------------------------------------------------------------------------
  60. A: Soft yes.
  61. At least for now, until BPF core has support for
  62. bpf-to-bpf calls, indirect calls, loops, global variables,
  63. jump tables, read-only sections, and all other normal constructs
  64. that C code can produce.
  65. Q: Can loops be supported in a safe way?
  66. ----------------------------------------
  67. A: It's not clear yet.
  68. BPF developers are trying to find a way to
  69. support bounded loops.
  70. Q: What are the verifier limits?
  71. --------------------------------
  72. A: The only limit known to the user space is BPF_MAXINSNS (4096).
  73. It's the maximum number of instructions that the unprivileged bpf
  74. program can have. The verifier has various internal limits.
  75. Like the maximum number of instructions that can be explored during
  76. program analysis. Currently, that limit is set to 1 million.
  77. Which essentially means that the largest program can consist
  78. of 1 million NOP instructions. There is a limit to the maximum number
  79. of subsequent branches, a limit to the number of nested bpf-to-bpf
  80. calls, a limit to the number of the verifier states per instruction,
  81. a limit to the number of maps used by the program.
  82. All these limits can be hit with a sufficiently complex program.
  83. There are also non-numerical limits that can cause the program
  84. to be rejected. The verifier used to recognize only pointer + constant
  85. expressions. Now it can recognize pointer + bounded_register.
  86. bpf_lookup_map_elem(key) had a requirement that 'key' must be
  87. a pointer to the stack. Now, 'key' can be a pointer to map value.
  88. The verifier is steadily getting 'smarter'. The limits are
  89. being removed. The only way to know that the program is going to
  90. be accepted by the verifier is to try to load it.
  91. The bpf development process guarantees that the future kernel
  92. versions will accept all bpf programs that were accepted by
  93. the earlier versions.
  94. Instruction level questions
  95. ---------------------------
  96. Q: LD_ABS and LD_IND instructions vs C code
  97. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  98. Q: How come LD_ABS and LD_IND instruction are present in BPF whereas
  99. C code cannot express them and has to use builtin intrinsics?
  100. A: This is artifact of compatibility with classic BPF. Modern
  101. networking code in BPF performs better without them.
  102. See 'direct packet access'.
  103. Q: BPF instructions mapping not one-to-one to native CPU
  104. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  105. Q: It seems not all BPF instructions are one-to-one to native CPU.
  106. For example why BPF_JNE and other compare and jumps are not cpu-like?
  107. A: This was necessary to avoid introducing flags into ISA which are
  108. impossible to make generic and efficient across CPU architectures.
  109. Q: Why BPF_DIV instruction doesn't map to x64 div?
  110. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  111. A: Because if we picked one-to-one relationship to x64 it would have made
  112. it more complicated to support on arm64 and other archs. Also it
  113. needs div-by-zero runtime check.
  114. Q: Why BPF has implicit prologue and epilogue?
  115. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  116. A: Because architectures like sparc have register windows and in general
  117. there are enough subtle differences between architectures, so naive
  118. store return address into stack won't work. Another reason is BPF has
  119. to be safe from division by zero (and legacy exception path
  120. of LD_ABS insn). Those instructions need to invoke epilogue and
  121. return implicitly.
  122. Q: Why BPF_JLT and BPF_JLE instructions were not introduced in the beginning?
  123. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  124. A: Because classic BPF didn't have them and BPF authors felt that compiler
  125. workaround would be acceptable. Turned out that programs lose performance
  126. due to lack of these compare instructions and they were added.
  127. These two instructions is a perfect example what kind of new BPF
  128. instructions are acceptable and can be added in the future.
  129. These two already had equivalent instructions in native CPUs.
  130. New instructions that don't have one-to-one mapping to HW instructions
  131. will not be accepted.
  132. Q: BPF 32-bit subregister requirements
  133. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  134. Q: BPF 32-bit subregisters have a requirement to zero upper 32-bits of BPF
  135. registers which makes BPF inefficient virtual machine for 32-bit
  136. CPU architectures and 32-bit HW accelerators. Can true 32-bit registers
  137. be added to BPF in the future?
  138. A: NO.
  139. But some optimizations on zero-ing the upper 32 bits for BPF registers are
  140. available, and can be leveraged to improve the performance of JITed BPF
  141. programs for 32-bit architectures.
  142. Starting with version 7, LLVM is able to generate instructions that operate
  143. on 32-bit subregisters, provided the option -mattr=+alu32 is passed for
  144. compiling a program. Furthermore, the verifier can now mark the
  145. instructions for which zero-ing the upper bits of the destination register
  146. is required, and insert an explicit zero-extension (zext) instruction
  147. (a mov32 variant). This means that for architectures without zext hardware
  148. support, the JIT back-ends do not need to clear the upper bits for
  149. subregisters written by alu32 instructions or narrow loads. Instead, the
  150. back-ends simply need to support code generation for that mov32 variant,
  151. and to overwrite bpf_jit_needs_zext() to make it return "true" (in order to
  152. enable zext insertion in the verifier).
  153. Note that it is possible for a JIT back-end to have partial hardware
  154. support for zext. In that case, if verifier zext insertion is enabled,
  155. it could lead to the insertion of unnecessary zext instructions. Such
  156. instructions could be removed by creating a simple peephole inside the JIT
  157. back-end: if one instruction has hardware support for zext and if the next
  158. instruction is an explicit zext, then the latter can be skipped when doing
  159. the code generation.
  160. Q: Does BPF have a stable ABI?
  161. ------------------------------
  162. A: YES. BPF instructions, arguments to BPF programs, set of helper
  163. functions and their arguments, recognized return codes are all part
  164. of ABI. However there is one specific exception to tracing programs
  165. which are using helpers like bpf_probe_read() to walk kernel internal
  166. data structures and compile with kernel internal headers. Both of these
  167. kernel internals are subject to change and can break with newer kernels
  168. such that the program needs to be adapted accordingly.
  169. New BPF functionality is generally added through the use of kfuncs instead of
  170. new helpers. Kfuncs are not considered part of the stable API, and have their own
  171. lifecycle expectations as described in :ref:`BPF_kfunc_lifecycle_expectations`.
  172. Q: Are tracepoints part of the stable ABI?
  173. ------------------------------------------
  174. A: NO. Tracepoints are tied to internal implementation details hence they are
  175. subject to change and can break with newer kernels. BPF programs need to change
  176. accordingly when this happens.
  177. Q: Are places where kprobes can attach part of the stable ABI?
  178. --------------------------------------------------------------
  179. A: NO. The places to which kprobes can attach are internal implementation
  180. details, which means that they are subject to change and can break with
  181. newer kernels. BPF programs need to change accordingly when this happens.
  182. Q: How much stack space a BPF program uses?
  183. -------------------------------------------
  184. A: Currently all program types are limited to 512 bytes of stack
  185. space, but the verifier computes the actual amount of stack used
  186. and both interpreter and most JITed code consume necessary amount.
  187. Q: Can BPF be offloaded to HW?
  188. ------------------------------
  189. A: YES. BPF HW offload is supported by NFP driver.
  190. Q: Does classic BPF interpreter still exist?
  191. --------------------------------------------
  192. A: NO. Classic BPF programs are converted into extend BPF instructions.
  193. Q: Can BPF call arbitrary kernel functions?
  194. -------------------------------------------
  195. A: NO. BPF programs can only call specific functions exposed as BPF helpers or
  196. kfuncs. The set of available functions is defined for every program type.
  197. Q: Can BPF overwrite arbitrary kernel memory?
  198. ---------------------------------------------
  199. A: NO.
  200. Tracing bpf programs can *read* arbitrary memory with bpf_probe_read()
  201. and bpf_probe_read_str() helpers. Networking programs cannot read
  202. arbitrary memory, since they don't have access to these helpers.
  203. Programs can never read or write arbitrary memory directly.
  204. Q: Can BPF overwrite arbitrary user memory?
  205. -------------------------------------------
  206. A: Sort-of.
  207. Tracing BPF programs can overwrite the user memory
  208. of the current task with bpf_probe_write_user(). Every time such
  209. program is loaded the kernel will print warning message, so
  210. this helper is only useful for experiments and prototypes.
  211. Tracing BPF programs are root only.
  212. Q: New functionality via kernel modules?
  213. ----------------------------------------
  214. Q: Can BPF functionality such as new program or map types, new
  215. helpers, etc be added out of kernel module code?
  216. A: Yes, through kfuncs and kptrs
  217. The core BPF functionality such as program types, maps and helpers cannot be
  218. added to by modules. However, modules can expose functionality to BPF programs
  219. by exporting kfuncs (which may return pointers to module-internal data
  220. structures as kptrs).
  221. Q: Directly calling kernel function is an ABI?
  222. ----------------------------------------------
  223. Q: Some kernel functions (e.g. tcp_slow_start) can be called
  224. by BPF programs. Do these kernel functions become an ABI?
  225. A: NO.
  226. The kernel function protos will change and the bpf programs will be
  227. rejected by the verifier. Also, for example, some of the bpf-callable
  228. kernel functions have already been used by other kernel tcp
  229. cc (congestion-control) implementations. If any of these kernel
  230. functions has changed, both the in-tree and out-of-tree kernel tcp cc
  231. implementations have to be changed. The same goes for the bpf
  232. programs and they have to be adjusted accordingly. See
  233. :ref:`BPF_kfunc_lifecycle_expectations` for details.
  234. Q: Attaching to arbitrary kernel functions is an ABI?
  235. -----------------------------------------------------
  236. Q: BPF programs can be attached to many kernel functions. Do these
  237. kernel functions become part of the ABI?
  238. A: NO.
  239. The kernel function prototypes will change, and BPF programs attaching to
  240. them will need to change. The BPF compile-once-run-everywhere (CO-RE)
  241. should be used in order to make it easier to adapt your BPF programs to
  242. different versions of the kernel.
  243. Q: Marking a function with BTF_ID makes that function an ABI?
  244. -------------------------------------------------------------
  245. A: NO.
  246. The BTF_ID macro does not cause a function to become part of the ABI
  247. any more than does the EXPORT_SYMBOL_GPL macro.
  248. Q: What is the compatibility story for special BPF types in map values?
  249. -----------------------------------------------------------------------
  250. Q: Users are allowed to embed bpf_spin_lock, bpf_timer fields in their BPF map
  251. values (when using BTF support for BPF maps). This allows to use helpers for
  252. such objects on these fields inside map values. Users are also allowed to embed
  253. pointers to some kernel types (with __kptr_untrusted and __kptr BTF tags). Will the
  254. kernel preserve backwards compatibility for these features?
  255. A: It depends. For bpf_spin_lock, bpf_timer: YES, for kptr and everything else:
  256. NO, but see below.
  257. For struct types that have been added already, like bpf_spin_lock and bpf_timer,
  258. the kernel will preserve backwards compatibility, as they are part of UAPI.
  259. For kptrs, they are also part of UAPI, but only with respect to the kptr
  260. mechanism. The types that you can use with a __kptr_untrusted and __kptr tagged
  261. pointer in your struct are NOT part of the UAPI contract. The supported types can
  262. and will change across kernel releases. However, operations like accessing kptr
  263. fields and bpf_kptr_xchg() helper will continue to be supported across kernel
  264. releases for the supported types.
  265. For any other supported struct type, unless explicitly stated in this document
  266. and added to bpf.h UAPI header, such types can and will arbitrarily change their
  267. size, type, and alignment, or any other user visible API or ABI detail across
  268. kernel releases. The users must adapt their BPF programs to the new changes and
  269. update them to make sure their programs continue to work correctly.
  270. NOTE: BPF subsystem specially reserves the 'bpf\_' prefix for type names, in
  271. order to introduce more special fields in the future. Hence, user programs must
  272. avoid defining types with 'bpf\_' prefix to not be broken in future releases.
  273. In other words, no backwards compatibility is guaranteed if one using a type
  274. in BTF with 'bpf\_' prefix.
  275. Q: What is the compatibility story for special BPF types in allocated objects?
  276. ------------------------------------------------------------------------------
  277. Q: Same as above, but for allocated objects (i.e. objects allocated using
  278. bpf_obj_new for user defined types). Will the kernel preserve backwards
  279. compatibility for these features?
  280. A: NO.
  281. Unlike map value types, the API to work with allocated objects and any support
  282. for special fields inside them is exposed through kfuncs, and thus has the same
  283. lifecycle expectations as the kfuncs themselves. See
  284. :ref:`BPF_kfunc_lifecycle_expectations` for details.