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