bpf_devel_QA.rst 29 KB

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