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