bpf_design_QA.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 there is no BPF_SDIV for signed divide operation?
  115. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  116. A: Because it would be rarely used. llvm errors in such case and
  117. prints a suggestion to use unsigned divide instead.
  118. Q: Why BPF has implicit prologue and epilogue?
  119. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  120. A: Because architectures like sparc have register windows and in general
  121. there are enough subtle differences between architectures, so naive
  122. store return address into stack won't work. Another reason is BPF has
  123. to be safe from division by zero (and legacy exception path
  124. of LD_ABS insn). Those instructions need to invoke epilogue and
  125. return implicitly.
  126. Q: Why BPF_JLT and BPF_JLE instructions were not introduced in the beginning?
  127. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  128. A: Because classic BPF didn't have them and BPF authors felt that compiler
  129. workaround would be acceptable. Turned out that programs lose performance
  130. due to lack of these compare instructions and they were added.
  131. These two instructions is a perfect example what kind of new BPF
  132. instructions are acceptable and can be added in the future.
  133. These two already had equivalent instructions in native CPUs.
  134. New instructions that don't have one-to-one mapping to HW instructions
  135. will not be accepted.
  136. Q: BPF 32-bit subregister requirements
  137. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  138. Q: BPF 32-bit subregisters have a requirement to zero upper 32-bits of BPF
  139. registers which makes BPF inefficient virtual machine for 32-bit
  140. CPU architectures and 32-bit HW accelerators. Can true 32-bit registers
  141. be added to BPF in the future?
  142. A: NO.
  143. But some optimizations on zero-ing the upper 32 bits for BPF registers are
  144. available, and can be leveraged to improve the performance of JITed BPF
  145. programs for 32-bit architectures.
  146. Starting with version 7, LLVM is able to generate instructions that operate
  147. on 32-bit subregisters, provided the option -mattr=+alu32 is passed for
  148. compiling a program. Furthermore, the verifier can now mark the
  149. instructions for which zero-ing the upper bits of the destination register
  150. is required, and insert an explicit zero-extension (zext) instruction
  151. (a mov32 variant). This means that for architectures without zext hardware
  152. support, the JIT back-ends do not need to clear the upper bits for
  153. subregisters written by alu32 instructions or narrow loads. Instead, the
  154. back-ends simply need to support code generation for that mov32 variant,
  155. and to overwrite bpf_jit_needs_zext() to make it return "true" (in order to
  156. enable zext insertion in the verifier).
  157. Note that it is possible for a JIT back-end to have partial hardware
  158. support for zext. In that case, if verifier zext insertion is enabled,
  159. it could lead to the insertion of unnecessary zext instructions. Such
  160. instructions could be removed by creating a simple peephole inside the JIT
  161. back-end: if one instruction has hardware support for zext and if the next
  162. instruction is an explicit zext, then the latter can be skipped when doing
  163. the code generation.
  164. Q: Does BPF have a stable ABI?
  165. ------------------------------
  166. A: YES. BPF instructions, arguments to BPF programs, set of helper
  167. functions and their arguments, recognized return codes are all part
  168. of ABI. However there is one specific exception to tracing programs
  169. which are using helpers like bpf_probe_read() to walk kernel internal
  170. data structures and compile with kernel internal headers. Both of these
  171. kernel internals are subject to change and can break with newer kernels
  172. such that the program needs to be adapted accordingly.
  173. Q: Are tracepoints part of the stable ABI?
  174. ------------------------------------------
  175. A: NO. Tracepoints are tied to internal implementation details hence they are
  176. subject to change and can break with newer kernels. BPF programs need to change
  177. accordingly when this happens.
  178. Q: Are places where kprobes can attach part of the stable ABI?
  179. --------------------------------------------------------------
  180. A: NO. The places to which kprobes can attach are internal implementation
  181. details, which means that they are subject to change and can break with
  182. newer kernels. BPF programs need to change accordingly when this happens.
  183. Q: How much stack space a BPF program uses?
  184. -------------------------------------------
  185. A: Currently all program types are limited to 512 bytes of stack
  186. space, but the verifier computes the actual amount of stack used
  187. and both interpreter and most JITed code consume necessary amount.
  188. Q: Can BPF be offloaded to HW?
  189. ------------------------------
  190. A: YES. BPF HW offload is supported by NFP driver.
  191. Q: Does classic BPF interpreter still exist?
  192. --------------------------------------------
  193. A: NO. Classic BPF programs are converted into extend BPF instructions.
  194. Q: Can BPF call arbitrary kernel functions?
  195. -------------------------------------------
  196. A: NO. BPF programs can only call a set of helper functions which
  197. is defined for every program type.
  198. Q: Can BPF overwrite arbitrary kernel memory?
  199. ---------------------------------------------
  200. A: NO.
  201. Tracing bpf programs can *read* arbitrary memory with bpf_probe_read()
  202. and bpf_probe_read_str() helpers. Networking programs cannot read
  203. arbitrary memory, since they don't have access to these helpers.
  204. Programs can never read or write arbitrary memory directly.
  205. Q: Can BPF overwrite arbitrary user memory?
  206. -------------------------------------------
  207. A: Sort-of.
  208. Tracing BPF programs can overwrite the user memory
  209. of the current task with bpf_probe_write_user(). Every time such
  210. program is loaded the kernel will print warning message, so
  211. this helper is only useful for experiments and prototypes.
  212. Tracing BPF programs are root only.
  213. Q: New functionality via kernel modules?
  214. ----------------------------------------
  215. Q: Can BPF functionality such as new program or map types, new
  216. helpers, etc be added out of kernel module code?
  217. A: NO.
  218. Q: Directly calling kernel function is an ABI?
  219. ----------------------------------------------
  220. Q: Some kernel functions (e.g. tcp_slow_start) can be called
  221. by BPF programs. Do these kernel functions become an ABI?
  222. A: NO.
  223. The kernel function protos will change and the bpf programs will be
  224. rejected by the verifier. Also, for example, some of the bpf-callable
  225. kernel functions have already been used by other kernel tcp
  226. cc (congestion-control) implementations. If any of these kernel
  227. functions has changed, both the in-tree and out-of-tree kernel tcp cc
  228. implementations have to be changed. The same goes for the bpf
  229. programs and they have to be adjusted accordingly.
  230. Q: Attaching to arbitrary kernel functions is an ABI?
  231. -----------------------------------------------------
  232. Q: BPF programs can be attached to many kernel functions. Do these
  233. kernel functions become part of the ABI?
  234. A: NO.
  235. The kernel function prototypes will change, and BPF programs attaching to
  236. them will need to change. The BPF compile-once-run-everywhere (CO-RE)
  237. should be used in order to make it easier to adapt your BPF programs to
  238. different versions of the kernel.
  239. Q: Marking a function with BTF_ID makes that function an ABI?
  240. -------------------------------------------------------------
  241. A: NO.
  242. The BTF_ID macro does not cause a function to become part of the ABI
  243. any more than does the EXPORT_SYMBOL_GPL macro.