bpf_devel_QA.rst 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. =================================
  2. HOWTO interact with BPF subsystem
  3. =================================
  4. This document provides information for the BPF subsystem about various
  5. workflows related to reporting bugs, submitting patches, and queueing
  6. patches for stable kernels.
  7. For general information about submitting patches, please refer to
  8. `Documentation/process/`_. This document only describes additional specifics
  9. related to BPF.
  10. .. contents::
  11. :local:
  12. :depth: 2
  13. Reporting bugs
  14. ==============
  15. Q: How do I report bugs for BPF kernel code?
  16. --------------------------------------------
  17. A: Since all BPF kernel development as well as bpftool and iproute2 BPF
  18. loader development happens through the netdev kernel mailing list,
  19. please report any found issues around BPF to the following mailing
  20. list:
  21. netdev@vger.kernel.org
  22. This may also include issues related to XDP, BPF tracing, etc.
  23. Given netdev has a high volume of traffic, please also add the BPF
  24. maintainers to Cc (from kernel MAINTAINERS_ file):
  25. * Alexei Starovoitov <ast@kernel.org>
  26. * Daniel Borkmann <daniel@iogearbox.net>
  27. In case a buggy commit has already been identified, make sure to keep
  28. the actual commit authors in Cc as well for the report. They can
  29. typically be identified through the kernel's git tree.
  30. **Please do NOT report BPF issues to bugzilla.kernel.org since it
  31. is a guarantee that the reported issue will be overlooked.**
  32. Submitting patches
  33. ==================
  34. Q: To which mailing list do I need to submit my BPF patches?
  35. ------------------------------------------------------------
  36. A: Please submit your BPF patches to the netdev kernel mailing list:
  37. netdev@vger.kernel.org
  38. Historically, BPF came out of networking and has always been maintained
  39. by the kernel networking community. Although these days BPF touches
  40. many other subsystems as well, the patches are still routed mainly
  41. through the networking community.
  42. In case your patch has changes in various different subsystems (e.g.
  43. tracing, security, etc), make sure to Cc the related kernel mailing
  44. lists and maintainers from there as well, so they are able to review
  45. the changes and provide their Acked-by's to the patches.
  46. Q: Where can I find patches currently under discussion for BPF subsystem?
  47. -------------------------------------------------------------------------
  48. A: All patches that are Cc'ed to netdev are queued for review under netdev
  49. patchwork project:
  50. http://patchwork.ozlabs.org/project/netdev/list/
  51. Those patches which target BPF, are assigned to a 'bpf' delegate for
  52. further processing from BPF maintainers. The current queue with
  53. patches under review can be found at:
  54. https://patchwork.ozlabs.org/project/netdev/list/?delegate=77147
  55. Once the patches have been reviewed by the BPF community as a whole
  56. and approved by the BPF maintainers, their status in patchwork will be
  57. changed to 'Accepted' and the submitter will be notified by mail. This
  58. means that the patches look good from a BPF perspective and have been
  59. applied to one of the two BPF kernel trees.
  60. In case feedback from the community requires a respin of the patches,
  61. their status in patchwork will be set to 'Changes Requested', and purged
  62. from the current review queue. Likewise for cases where patches would
  63. get rejected or are not applicable to the BPF trees (but assigned to
  64. the 'bpf' delegate).
  65. Q: How do the changes make their way into Linux?
  66. ------------------------------------------------
  67. A: There are two BPF kernel trees (git repositories). Once patches have
  68. been accepted by the BPF maintainers, they will be applied to one
  69. of the two BPF trees:
  70. * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git/
  71. * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/
  72. The bpf tree itself is for fixes only, whereas bpf-next for features,
  73. cleanups or other kind of improvements ("next-like" content). This is
  74. analogous to net and net-next trees for networking. Both bpf and
  75. bpf-next will only have a master branch in order to simplify against
  76. which branch patches should get rebased to.
  77. Accumulated BPF patches in the bpf tree will regularly get pulled
  78. into the net kernel tree. Likewise, accumulated BPF patches accepted
  79. into the bpf-next tree will make their way into net-next tree. net and
  80. net-next are both run by David S. Miller. From there, they will go
  81. into the kernel mainline tree run by Linus Torvalds. To read up on the
  82. process of net and net-next being merged into the mainline tree, see
  83. the :ref:`netdev-FAQ`
  84. Occasionally, to prevent merge conflicts, we might send pull requests
  85. to other trees (e.g. tracing) with a small subset of the patches, but
  86. net and net-next are always the main trees targeted for integration.
  87. The pull requests will contain a high-level summary of the accumulated
  88. patches and can be searched on netdev kernel mailing list through the
  89. following subject lines (``yyyy-mm-dd`` is the date of the pull
  90. request)::
  91. pull-request: bpf yyyy-mm-dd
  92. pull-request: bpf-next yyyy-mm-dd
  93. Q: How do I indicate which tree (bpf vs. bpf-next) my patch should be applied to?
  94. ---------------------------------------------------------------------------------
  95. A: The process is the very same as described in the :ref:`netdev-FAQ`,
  96. so please read up on it. The subject line must indicate whether the
  97. patch is a fix or rather "next-like" content in order to let the
  98. maintainers know whether it is targeted at bpf or bpf-next.
  99. For fixes eventually landing in bpf -> net tree, the subject must
  100. look like::
  101. git format-patch --subject-prefix='PATCH bpf' start..finish
  102. For features/improvements/etc that should eventually land in
  103. bpf-next -> net-next, the subject must look like::
  104. git format-patch --subject-prefix='PATCH bpf-next' start..finish
  105. If unsure whether the patch or patch series should go into bpf
  106. or net directly, or bpf-next or net-next directly, it is not a
  107. problem either if the subject line says net or net-next as target.
  108. It is eventually up to the maintainers to do the delegation of
  109. the patches.
  110. If it is clear that patches should go into bpf or bpf-next tree,
  111. please make sure to rebase the patches against those trees in
  112. order to reduce potential conflicts.
  113. In case the patch or patch series has to be reworked and sent out
  114. again in a second or later revision, it is also required to add a
  115. version number (``v2``, ``v3``, ...) into the subject prefix::
  116. git format-patch --subject-prefix='PATCH net-next v2' start..finish
  117. When changes have been requested to the patch series, always send the
  118. whole patch series again with the feedback incorporated (never send
  119. individual diffs on top of the old series).
  120. Q: What does it mean when a patch gets applied to bpf or bpf-next tree?
  121. -----------------------------------------------------------------------
  122. A: It means that the patch looks good for mainline inclusion from
  123. a BPF point of view.
  124. Be aware that this is not a final verdict that the patch will
  125. automatically get accepted into net or net-next trees eventually:
  126. On the netdev kernel mailing list reviews can come in at any point
  127. in time. If discussions around a patch conclude that they cannot
  128. get included as-is, we will either apply a follow-up fix or drop
  129. them from the trees entirely. Therefore, we also reserve to rebase
  130. the trees when deemed necessary. After all, the purpose of the tree
  131. is to:
  132. i) accumulate and stage BPF patches for integration into trees
  133. like net and net-next, and
  134. ii) run extensive BPF test suite and
  135. workloads on the patches before they make their way any further.
  136. Once the BPF pull request was accepted by David S. Miller, then
  137. the patches end up in net or net-next tree, respectively, and
  138. make their way from there further into mainline. Again, see the
  139. :ref:`netdev-FAQ` for additional information e.g. on how often they are
  140. merged to mainline.
  141. Q: How long do I need to wait for feedback on my BPF patches?
  142. -------------------------------------------------------------
  143. A: We try to keep the latency low. The usual time to feedback will
  144. be around 2 or 3 business days. It may vary depending on the
  145. complexity of changes and current patch load.
  146. Q: How often do you send pull requests to major kernel trees like net or net-next?
  147. ----------------------------------------------------------------------------------
  148. A: Pull requests will be sent out rather often in order to not
  149. accumulate too many patches in bpf or bpf-next.
  150. As a rule of thumb, expect pull requests for each tree regularly
  151. at the end of the week. In some cases pull requests could additionally
  152. come also in the middle of the week depending on the current patch
  153. load or urgency.
  154. Q: Are patches applied to bpf-next when the merge window is open?
  155. -----------------------------------------------------------------
  156. A: For the time when the merge window is open, bpf-next will not be
  157. processed. This is roughly analogous to net-next patch processing,
  158. so feel free to read up on the :ref:`netdev-FAQ` about further details.
  159. During those two weeks of merge window, we might ask you to resend
  160. your patch series once bpf-next is open again. Once Linus released
  161. a ``v*-rc1`` after the merge window, we continue processing of bpf-next.
  162. For non-subscribers to kernel mailing lists, there is also a status
  163. page run by David S. Miller on net-next that provides guidance:
  164. http://vger.kernel.org/~davem/net-next.html
  165. Q: Verifier changes and test cases
  166. ----------------------------------
  167. Q: I made a BPF verifier change, do I need to add test cases for
  168. BPF kernel selftests_?
  169. A: If the patch has changes to the behavior of the verifier, then yes,
  170. it is absolutely necessary to add test cases to the BPF kernel
  171. selftests_ suite. If they are not present and we think they are
  172. needed, then we might ask for them before accepting any changes.
  173. In particular, test_verifier.c is tracking a high number of BPF test
  174. cases, including a lot of corner cases that LLVM BPF back end may
  175. generate out of the restricted C code. Thus, adding test cases is
  176. absolutely crucial to make sure future changes do not accidentally
  177. affect prior use-cases. Thus, treat those test cases as: verifier
  178. behavior that is not tracked in test_verifier.c could potentially
  179. be subject to change.
  180. Q: samples/bpf preference vs selftests?
  181. ---------------------------------------
  182. Q: When should I add code to `samples/bpf/`_ and when to BPF kernel
  183. selftests_ ?
  184. A: In general, we prefer additions to BPF kernel selftests_ rather than
  185. `samples/bpf/`_. The rationale is very simple: kernel selftests are
  186. regularly run by various bots to test for kernel regressions.
  187. The more test cases we add to BPF selftests, the better the coverage
  188. and the less likely it is that those could accidentally break. It is
  189. not that BPF kernel selftests cannot demo how a specific feature can
  190. be used.
  191. That said, `samples/bpf/`_ may be a good place for people to get started,
  192. so it might be advisable that simple demos of features could go into
  193. `samples/bpf/`_, but advanced functional and corner-case testing rather
  194. into kernel selftests.
  195. If your sample looks like a test case, then go for BPF kernel selftests
  196. instead!
  197. Q: When should I add code to the bpftool?
  198. -----------------------------------------
  199. A: The main purpose of bpftool (under tools/bpf/bpftool/) is to provide
  200. a central user space tool for debugging and introspection of BPF programs
  201. and maps that are active in the kernel. If UAPI changes related to BPF
  202. enable for dumping additional information of programs or maps, then
  203. bpftool should be extended as well to support dumping them.
  204. Q: When should I add code to iproute2's BPF loader?
  205. ---------------------------------------------------
  206. A: For UAPI changes related to the XDP or tc layer (e.g. ``cls_bpf``),
  207. the convention is that those control-path related changes are added to
  208. iproute2's BPF loader as well from user space side. This is not only
  209. useful to have UAPI changes properly designed to be usable, but also
  210. to make those changes available to a wider user base of major
  211. downstream distributions.
  212. Q: Do you accept patches as well for iproute2's BPF loader?
  213. -----------------------------------------------------------
  214. A: Patches for the iproute2's BPF loader have to be sent to:
  215. netdev@vger.kernel.org
  216. While those patches are not processed by the BPF kernel maintainers,
  217. please keep them in Cc as well, so they can be reviewed.
  218. The official git repository for iproute2 is run by Stephen Hemminger
  219. and can be found at:
  220. https://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git/
  221. The patches need to have a subject prefix of '``[PATCH iproute2
  222. master]``' or '``[PATCH iproute2 net-next]``'. '``master``' or
  223. '``net-next``' describes the target branch where the patch should be
  224. applied to. Meaning, if kernel changes went into the net-next kernel
  225. tree, then the related iproute2 changes need to go into the iproute2
  226. net-next branch, otherwise they can be targeted at master branch. The
  227. iproute2 net-next branch will get merged into the master branch after
  228. the current iproute2 version from master has been released.
  229. Like BPF, the patches end up in patchwork under the netdev project and
  230. are delegated to 'shemminger' for further processing:
  231. http://patchwork.ozlabs.org/project/netdev/list/?delegate=389
  232. Q: What is the minimum requirement before I submit my BPF patches?
  233. ------------------------------------------------------------------
  234. A: When submitting patches, always take the time and properly test your
  235. patches *prior* to submission. Never rush them! If maintainers find
  236. that your patches have not been properly tested, it is a good way to
  237. get them grumpy. Testing patch submissions is a hard requirement!
  238. Note, fixes that go to bpf tree *must* have a ``Fixes:`` tag included.
  239. The same applies to fixes that target bpf-next, where the affected
  240. commit is in net-next (or in some cases bpf-next). The ``Fixes:`` tag is
  241. crucial in order to identify follow-up commits and tremendously helps
  242. for people having to do backporting, so it is a must have!
  243. We also don't accept patches with an empty commit message. Take your
  244. time and properly write up a high quality commit message, it is
  245. essential!
  246. Think about it this way: other developers looking at your code a month
  247. from now need to understand *why* a certain change has been done that
  248. way, and whether there have been flaws in the analysis or assumptions
  249. that the original author did. Thus providing a proper rationale and
  250. describing the use-case for the changes is a must.
  251. Patch submissions with >1 patch must have a cover letter which includes
  252. a high level description of the series. This high level summary will
  253. then be placed into the merge commit by the BPF maintainers such that
  254. it is also accessible from the git log for future reference.
  255. Q: Features changing BPF JIT and/or LLVM
  256. ----------------------------------------
  257. Q: What do I need to consider when adding a new instruction or feature
  258. that would require BPF JIT and/or LLVM integration as well?
  259. A: We try hard to keep all BPF JITs up to date such that the same user
  260. experience can be guaranteed when running BPF programs on different
  261. architectures without having the program punt to the less efficient
  262. interpreter in case the in-kernel BPF JIT is enabled.
  263. If you are unable to implement or test the required JIT changes for
  264. certain architectures, please work together with the related BPF JIT
  265. developers in order to get the feature implemented in a timely manner.
  266. Please refer to the git log (``arch/*/net/``) to locate the necessary
  267. people for helping out.
  268. Also always make sure to add BPF test cases (e.g. test_bpf.c and
  269. test_verifier.c) for new instructions, so that they can receive
  270. broad test coverage and help run-time testing the various BPF JITs.
  271. In case of new BPF instructions, once the changes have been accepted
  272. into the Linux kernel, please implement support into LLVM's BPF back
  273. end. See LLVM_ section below for further information.
  274. Stable submission
  275. =================
  276. Q: I need a specific BPF commit in stable kernels. What should I do?
  277. --------------------------------------------------------------------
  278. A: In case you need a specific fix in stable kernels, first check whether
  279. the commit has already been applied in the related ``linux-*.y`` branches:
  280. https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/
  281. If not the case, then drop an email to the BPF maintainers with the
  282. netdev kernel mailing list in Cc and ask for the fix to be queued up:
  283. netdev@vger.kernel.org
  284. The process in general is the same as on netdev itself, see also the
  285. :ref:`netdev-FAQ`.
  286. Q: Do you also backport to kernels not currently maintained as stable?
  287. ----------------------------------------------------------------------
  288. A: No. If you need a specific BPF commit in kernels that are currently not
  289. maintained by the stable maintainers, then you are on your own.
  290. The current stable and longterm stable kernels are all listed here:
  291. https://www.kernel.org/
  292. Q: The BPF patch I am about to submit needs to go to stable as well
  293. -------------------------------------------------------------------
  294. What should I do?
  295. A: The same rules apply as with netdev patch submissions in general, see
  296. the :ref:`netdev-FAQ`.
  297. Never add "``Cc: stable@vger.kernel.org``" to the patch description, but
  298. ask the BPF maintainers to queue the patches instead. This can be done
  299. with a note, for example, under the ``---`` part of the patch which does
  300. not go into the git log. Alternatively, this can be done as a simple
  301. request by mail instead.
  302. Q: Queue stable patches
  303. -----------------------
  304. Q: Where do I find currently queued BPF patches that will be submitted
  305. to stable?
  306. A: Once patches that fix critical bugs got applied into the bpf tree, they
  307. are queued up for stable submission under:
  308. http://patchwork.ozlabs.org/bundle/bpf/stable/?state=*
  309. They will be on hold there at minimum until the related commit made its
  310. way into the mainline kernel tree.
  311. After having been under broader exposure, the queued patches will be
  312. submitted by the BPF maintainers to the stable maintainers.
  313. Testing patches
  314. ===============
  315. Q: How to run BPF selftests
  316. ---------------------------
  317. A: After you have booted into the newly compiled kernel, navigate to
  318. the BPF selftests_ suite in order to test BPF functionality (current
  319. working directory points to the root of the cloned git tree)::
  320. $ cd tools/testing/selftests/bpf/
  321. $ make
  322. To run the verifier tests::
  323. $ sudo ./test_verifier
  324. The verifier tests print out all the current checks being
  325. performed. The summary at the end of running all tests will dump
  326. information of test successes and failures::
  327. Summary: 418 PASSED, 0 FAILED
  328. In order to run through all BPF selftests, the following command is
  329. needed::
  330. $ sudo make run_tests
  331. See the kernels selftest `Documentation/dev-tools/kselftest.rst`_
  332. document for further documentation.
  333. Q: Which BPF kernel selftests version should I run my kernel against?
  334. ---------------------------------------------------------------------
  335. A: If you run a kernel ``xyz``, then always run the BPF kernel selftests
  336. from that kernel ``xyz`` as well. Do not expect that the BPF selftest
  337. from the latest mainline tree will pass all the time.
  338. In particular, test_bpf.c and test_verifier.c have a large number of
  339. test cases and are constantly updated with new BPF test sequences, or
  340. existing ones are adapted to verifier changes e.g. due to verifier
  341. becoming smarter and being able to better track certain things.
  342. LLVM
  343. ====
  344. Q: Where do I find LLVM with BPF support?
  345. -----------------------------------------
  346. A: The BPF back end for LLVM is upstream in LLVM since version 3.7.1.
  347. All major distributions these days ship LLVM with BPF back end enabled,
  348. so for the majority of use-cases it is not required to compile LLVM by
  349. hand anymore, just install the distribution provided package.
  350. LLVM's static compiler lists the supported targets through
  351. ``llc --version``, make sure BPF targets are listed. Example::
  352. $ llc --version
  353. LLVM (http://llvm.org/):
  354. LLVM version 6.0.0svn
  355. Optimized build.
  356. Default target: x86_64-unknown-linux-gnu
  357. Host CPU: skylake
  358. Registered Targets:
  359. bpf - BPF (host endian)
  360. bpfeb - BPF (big endian)
  361. bpfel - BPF (little endian)
  362. x86 - 32-bit X86: Pentium-Pro and above
  363. x86-64 - 64-bit X86: EM64T and AMD64
  364. For developers in order to utilize the latest features added to LLVM's
  365. BPF back end, it is advisable to run the latest LLVM releases. Support
  366. for new BPF kernel features such as additions to the BPF instruction
  367. set are often developed together.
  368. All LLVM releases can be found at: http://releases.llvm.org/
  369. Q: Got it, so how do I build LLVM manually anyway?
  370. --------------------------------------------------
  371. A: You need cmake and gcc-c++ as build requisites for LLVM. Once you have
  372. that set up, proceed with building the latest LLVM and clang version
  373. from the git repositories::
  374. $ git clone http://llvm.org/git/llvm.git
  375. $ cd llvm/tools
  376. $ git clone --depth 1 http://llvm.org/git/clang.git
  377. $ cd ..; mkdir build; cd build
  378. $ cmake .. -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
  379. -DBUILD_SHARED_LIBS=OFF \
  380. -DCMAKE_BUILD_TYPE=Release \
  381. -DLLVM_BUILD_RUNTIME=OFF
  382. $ make -j $(getconf _NPROCESSORS_ONLN)
  383. The built binaries can then be found in the build/bin/ directory, where
  384. you can point the PATH variable to.
  385. Q: Reporting LLVM BPF issues
  386. ----------------------------
  387. Q: Should I notify BPF kernel maintainers about issues in LLVM's BPF code
  388. generation back end or about LLVM generated code that the verifier
  389. refuses to accept?
  390. A: Yes, please do!
  391. LLVM's BPF back end is a key piece of the whole BPF
  392. infrastructure and it ties deeply into verification of programs from the
  393. kernel side. Therefore, any issues on either side need to be investigated
  394. and fixed whenever necessary.
  395. Therefore, please make sure to bring them up at netdev kernel mailing
  396. list and Cc BPF maintainers for LLVM and kernel bits:
  397. * Yonghong Song <yhs@fb.com>
  398. * Alexei Starovoitov <ast@kernel.org>
  399. * Daniel Borkmann <daniel@iogearbox.net>
  400. LLVM also has an issue tracker where BPF related bugs can be found:
  401. https://bugs.llvm.org/buglist.cgi?quicksearch=bpf
  402. However, it is better to reach out through mailing lists with having
  403. maintainers in Cc.
  404. Q: New BPF instruction for kernel and LLVM
  405. ------------------------------------------
  406. Q: I have added a new BPF instruction to the kernel, how can I integrate
  407. it into LLVM?
  408. A: LLVM has a ``-mcpu`` selector for the BPF back end in order to allow
  409. the selection of BPF instruction set extensions. By default the
  410. ``generic`` processor target is used, which is the base instruction set
  411. (v1) of BPF.
  412. LLVM has an option to select ``-mcpu=probe`` where it will probe the host
  413. kernel for supported BPF instruction set extensions and selects the
  414. optimal set automatically.
  415. For cross-compilation, a specific version can be select manually as well ::
  416. $ llc -march bpf -mcpu=help
  417. Available CPUs for this target:
  418. generic - Select the generic processor.
  419. probe - Select the probe processor.
  420. v1 - Select the v1 processor.
  421. v2 - Select the v2 processor.
  422. [...]
  423. Newly added BPF instructions to the Linux kernel need to follow the same
  424. scheme, bump the instruction set version and implement probing for the
  425. extensions such that ``-mcpu=probe`` users can benefit from the
  426. optimization transparently when upgrading their kernels.
  427. If you are unable to implement support for the newly added BPF instruction
  428. please reach out to BPF developers for help.
  429. By the way, the BPF kernel selftests run with ``-mcpu=probe`` for better
  430. test coverage.
  431. Q: clang flag for target bpf?
  432. -----------------------------
  433. Q: In some cases clang flag ``-target bpf`` is used but in other cases the
  434. default clang target, which matches the underlying architecture, is used.
  435. What is the difference and when I should use which?
  436. A: Although LLVM IR generation and optimization try to stay architecture
  437. independent, ``-target <arch>`` still has some impact on generated code:
  438. - BPF program may recursively include header file(s) with file scope
  439. inline assembly codes. The default target can handle this well,
  440. while ``bpf`` target may fail if bpf backend assembler does not
  441. understand these assembly codes, which is true in most cases.
  442. - When compiled without ``-g``, additional elf sections, e.g.,
  443. .eh_frame and .rela.eh_frame, may be present in the object file
  444. with default target, but not with ``bpf`` target.
  445. - The default target may turn a C switch statement into a switch table
  446. lookup and jump operation. Since the switch table is placed
  447. in the global readonly section, the bpf program will fail to load.
  448. The bpf target does not support switch table optimization.
  449. The clang option ``-fno-jump-tables`` can be used to disable
  450. switch table generation.
  451. - For clang ``-target bpf``, it is guaranteed that pointer or long /
  452. unsigned long types will always have a width of 64 bit, no matter
  453. whether underlying clang binary or default target (or kernel) is
  454. 32 bit. However, when native clang target is used, then it will
  455. compile these types based on the underlying architecture's conventions,
  456. meaning in case of 32 bit architecture, pointer or long / unsigned
  457. long types e.g. in BPF context structure will have width of 32 bit
  458. while the BPF LLVM back end still operates in 64 bit. The native
  459. target is mostly needed in tracing for the case of walking ``pt_regs``
  460. or other kernel structures where CPU's register width matters.
  461. Otherwise, ``clang -target bpf`` is generally recommended.
  462. You should use default target when:
  463. - Your program includes a header file, e.g., ptrace.h, which eventually
  464. pulls in some header files containing file scope host assembly codes.
  465. - You can add ``-fno-jump-tables`` to work around the switch table issue.
  466. Otherwise, you can use ``bpf`` target. Additionally, you *must* use bpf target
  467. when:
  468. - Your program uses data structures with pointer or long / unsigned long
  469. types that interface with BPF helpers or context data structures. Access
  470. into these structures is verified by the BPF verifier and may result
  471. in verification failures if the native architecture is not aligned with
  472. the BPF architecture, e.g. 64-bit. An example of this is
  473. BPF_PROG_TYPE_SK_MSG require ``-target bpf``
  474. .. Links
  475. .. _Documentation/process/: https://www.kernel.org/doc/html/latest/process/
  476. .. _MAINTAINERS: ../../MAINTAINERS
  477. .. _netdev-FAQ: ../networking/netdev-FAQ.rst
  478. .. _samples/bpf/: ../../samples/bpf/
  479. .. _selftests: ../../tools/testing/selftests/bpf/
  480. .. _Documentation/dev-tools/kselftest.rst:
  481. https://www.kernel.org/doc/html/latest/dev-tools/kselftest.html
  482. Happy BPF hacking!