bpf_design_QA.rst 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 where the verifier can guarantee that
  70. the program terminates in less than 4096 instructions.
  71. Instruction level questions
  72. ---------------------------
  73. Q: LD_ABS and LD_IND instructions vs C code
  74. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  75. Q: How come LD_ABS and LD_IND instruction are present in BPF whereas
  76. C code cannot express them and has to use builtin intrinsics?
  77. A: This is artifact of compatibility with classic BPF. Modern
  78. networking code in BPF performs better without them.
  79. See 'direct packet access'.
  80. Q: BPF instructions mapping not one-to-one to native CPU
  81. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  82. Q: It seems not all BPF instructions are one-to-one to native CPU.
  83. For example why BPF_JNE and other compare and jumps are not cpu-like?
  84. A: This was necessary to avoid introducing flags into ISA which are
  85. impossible to make generic and efficient across CPU architectures.
  86. Q: why BPF_DIV instruction doesn't map to x64 div?
  87. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. A: Because if we picked one-to-one relationship to x64 it would have made
  89. it more complicated to support on arm64 and other archs. Also it
  90. needs div-by-zero runtime check.
  91. Q: why there is no BPF_SDIV for signed divide operation?
  92. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. A: Because it would be rarely used. llvm errors in such case and
  94. prints a suggestion to use unsigned divide instead
  95. Q: Why BPF has implicit prologue and epilogue?
  96. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  97. A: Because architectures like sparc have register windows and in general
  98. there are enough subtle differences between architectures, so naive
  99. store return address into stack won't work. Another reason is BPF has
  100. to be safe from division by zero (and legacy exception path
  101. of LD_ABS insn). Those instructions need to invoke epilogue and
  102. return implicitly.
  103. Q: Why BPF_JLT and BPF_JLE instructions were not introduced in the beginning?
  104. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  105. A: Because classic BPF didn't have them and BPF authors felt that compiler
  106. workaround would be acceptable. Turned out that programs lose performance
  107. due to lack of these compare instructions and they were added.
  108. These two instructions is a perfect example what kind of new BPF
  109. instructions are acceptable and can be added in the future.
  110. These two already had equivalent instructions in native CPUs.
  111. New instructions that don't have one-to-one mapping to HW instructions
  112. will not be accepted.
  113. Q: BPF 32-bit subregister requirements
  114. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  115. Q: BPF 32-bit subregisters have a requirement to zero upper 32-bits of BPF
  116. registers which makes BPF inefficient virtual machine for 32-bit
  117. CPU architectures and 32-bit HW accelerators. Can true 32-bit registers
  118. be added to BPF in the future?
  119. A: NO. The first thing to improve performance on 32-bit archs is to teach
  120. LLVM to generate code that uses 32-bit subregisters. Then second step
  121. is to teach verifier to mark operations where zero-ing upper bits
  122. is unnecessary. Then JITs can take advantage of those markings and
  123. drastically reduce size of generated code and improve performance.
  124. Q: Does BPF have a stable ABI?
  125. ------------------------------
  126. A: YES. BPF instructions, arguments to BPF programs, set of helper
  127. functions and their arguments, recognized return codes are all part
  128. of ABI. However when tracing programs are using bpf_probe_read() helper
  129. to walk kernel internal datastructures and compile with kernel
  130. internal headers these accesses can and will break with newer
  131. kernels. The union bpf_attr -> kern_version is checked at load time
  132. to prevent accidentally loading kprobe-based bpf programs written
  133. for a different kernel. Networking programs don't do kern_version check.
  134. Q: How much stack space a BPF program uses?
  135. -------------------------------------------
  136. A: Currently all program types are limited to 512 bytes of stack
  137. space, but the verifier computes the actual amount of stack used
  138. and both interpreter and most JITed code consume necessary amount.
  139. Q: Can BPF be offloaded to HW?
  140. ------------------------------
  141. A: YES. BPF HW offload is supported by NFP driver.
  142. Q: Does classic BPF interpreter still exist?
  143. --------------------------------------------
  144. A: NO. Classic BPF programs are converted into extend BPF instructions.
  145. Q: Can BPF call arbitrary kernel functions?
  146. -------------------------------------------
  147. A: NO. BPF programs can only call a set of helper functions which
  148. is defined for every program type.
  149. Q: Can BPF overwrite arbitrary kernel memory?
  150. ---------------------------------------------
  151. A: NO.
  152. Tracing bpf programs can *read* arbitrary memory with bpf_probe_read()
  153. and bpf_probe_read_str() helpers. Networking programs cannot read
  154. arbitrary memory, since they don't have access to these helpers.
  155. Programs can never read or write arbitrary memory directly.
  156. Q: Can BPF overwrite arbitrary user memory?
  157. -------------------------------------------
  158. A: Sort-of.
  159. Tracing BPF programs can overwrite the user memory
  160. of the current task with bpf_probe_write_user(). Every time such
  161. program is loaded the kernel will print warning message, so
  162. this helper is only useful for experiments and prototypes.
  163. Tracing BPF programs are root only.
  164. Q: bpf_trace_printk() helper warning
  165. ------------------------------------
  166. Q: When bpf_trace_printk() helper is used the kernel prints nasty
  167. warning message. Why is that?
  168. A: This is done to nudge program authors into better interfaces when
  169. programs need to pass data to user space. Like bpf_perf_event_output()
  170. can be used to efficiently stream data via perf ring buffer.
  171. BPF maps can be used for asynchronous data sharing between kernel
  172. and user space. bpf_trace_printk() should only be used for debugging.
  173. Q: New functionality via kernel modules?
  174. ----------------------------------------
  175. Q: Can BPF functionality such as new program or map types, new
  176. helpers, etc be added out of kernel module code?
  177. A: NO.