0001 =================================
0002 HOWTO interact with BPF subsystem
0003 =================================
0004
0005 This document provides information for the BPF subsystem about various
0006 workflows related to reporting bugs, submitting patches, and queueing
0007 patches for stable kernels.
0008
0009 For general information about submitting patches, please refer to
0010 `Documentation/process/`_. This document only describes additional specifics
0011 related to BPF.
0012
0013 .. contents::
0014 :local:
0015 :depth: 2
0016
0017 Reporting bugs
0018 ==============
0019
0020 Q: How do I report bugs for BPF kernel code?
0021 --------------------------------------------
0022 A: Since all BPF kernel development as well as bpftool and iproute2 BPF
0023 loader development happens through the bpf kernel mailing list,
0024 please report any found issues around BPF to the following mailing
0025 list:
0026
0027 bpf@vger.kernel.org
0028
0029 This may also include issues related to XDP, BPF tracing, etc.
0030
0031 Given netdev has a high volume of traffic, please also add the BPF
0032 maintainers to Cc (from kernel ``MAINTAINERS`` file):
0033
0034 * Alexei Starovoitov <ast@kernel.org>
0035 * Daniel Borkmann <daniel@iogearbox.net>
0036
0037 In case a buggy commit has already been identified, make sure to keep
0038 the actual commit authors in Cc as well for the report. They can
0039 typically be identified through the kernel's git tree.
0040
0041 **Please do NOT report BPF issues to bugzilla.kernel.org since it
0042 is a guarantee that the reported issue will be overlooked.**
0043
0044 Submitting patches
0045 ==================
0046
0047 Q: To which mailing list do I need to submit my BPF patches?
0048 ------------------------------------------------------------
0049 A: Please submit your BPF patches to the bpf kernel mailing list:
0050
0051 bpf@vger.kernel.org
0052
0053 In case your patch has changes in various different subsystems (e.g.
0054 networking, tracing, security, etc), make sure to Cc the related kernel mailing
0055 lists and maintainers from there as well, so they are able to review
0056 the changes and provide their Acked-by's to the patches.
0057
0058 Q: Where can I find patches currently under discussion for BPF subsystem?
0059 -------------------------------------------------------------------------
0060 A: All patches that are Cc'ed to netdev are queued for review under netdev
0061 patchwork project:
0062
0063 https://patchwork.kernel.org/project/netdevbpf/list/
0064
0065 Those patches which target BPF, are assigned to a 'bpf' delegate for
0066 further processing from BPF maintainers. The current queue with
0067 patches under review can be found at:
0068
0069 https://patchwork.kernel.org/project/netdevbpf/list/?delegate=121173
0070
0071 Once the patches have been reviewed by the BPF community as a whole
0072 and approved by the BPF maintainers, their status in patchwork will be
0073 changed to 'Accepted' and the submitter will be notified by mail. This
0074 means that the patches look good from a BPF perspective and have been
0075 applied to one of the two BPF kernel trees.
0076
0077 In case feedback from the community requires a respin of the patches,
0078 their status in patchwork will be set to 'Changes Requested', and purged
0079 from the current review queue. Likewise for cases where patches would
0080 get rejected or are not applicable to the BPF trees (but assigned to
0081 the 'bpf' delegate).
0082
0083 Q: How do the changes make their way into Linux?
0084 ------------------------------------------------
0085 A: There are two BPF kernel trees (git repositories). Once patches have
0086 been accepted by the BPF maintainers, they will be applied to one
0087 of the two BPF trees:
0088
0089 * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git/
0090 * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/
0091
0092 The bpf tree itself is for fixes only, whereas bpf-next for features,
0093 cleanups or other kind of improvements ("next-like" content). This is
0094 analogous to net and net-next trees for networking. Both bpf and
0095 bpf-next will only have a master branch in order to simplify against
0096 which branch patches should get rebased to.
0097
0098 Accumulated BPF patches in the bpf tree will regularly get pulled
0099 into the net kernel tree. Likewise, accumulated BPF patches accepted
0100 into the bpf-next tree will make their way into net-next tree. net and
0101 net-next are both run by David S. Miller. From there, they will go
0102 into the kernel mainline tree run by Linus Torvalds. To read up on the
0103 process of net and net-next being merged into the mainline tree, see
0104 the :ref:`netdev-FAQ`
0105
0106
0107
0108 Occasionally, to prevent merge conflicts, we might send pull requests
0109 to other trees (e.g. tracing) with a small subset of the patches, but
0110 net and net-next are always the main trees targeted for integration.
0111
0112 The pull requests will contain a high-level summary of the accumulated
0113 patches and can be searched on netdev kernel mailing list through the
0114 following subject lines (``yyyy-mm-dd`` is the date of the pull
0115 request)::
0116
0117 pull-request: bpf yyyy-mm-dd
0118 pull-request: bpf-next yyyy-mm-dd
0119
0120 Q: How do I indicate which tree (bpf vs. bpf-next) my patch should be applied to?
0121 ---------------------------------------------------------------------------------
0122
0123 A: The process is the very same as described in the :ref:`netdev-FAQ`,
0124 so please read up on it. The subject line must indicate whether the
0125 patch is a fix or rather "next-like" content in order to let the
0126 maintainers know whether it is targeted at bpf or bpf-next.
0127
0128 For fixes eventually landing in bpf -> net tree, the subject must
0129 look like::
0130
0131 git format-patch --subject-prefix='PATCH bpf' start..finish
0132
0133 For features/improvements/etc that should eventually land in
0134 bpf-next -> net-next, the subject must look like::
0135
0136 git format-patch --subject-prefix='PATCH bpf-next' start..finish
0137
0138 If unsure whether the patch or patch series should go into bpf
0139 or net directly, or bpf-next or net-next directly, it is not a
0140 problem either if the subject line says net or net-next as target.
0141 It is eventually up to the maintainers to do the delegation of
0142 the patches.
0143
0144 If it is clear that patches should go into bpf or bpf-next tree,
0145 please make sure to rebase the patches against those trees in
0146 order to reduce potential conflicts.
0147
0148 In case the patch or patch series has to be reworked and sent out
0149 again in a second or later revision, it is also required to add a
0150 version number (``v2``, ``v3``, ...) into the subject prefix::
0151
0152 git format-patch --subject-prefix='PATCH bpf-next v2' start..finish
0153
0154 When changes have been requested to the patch series, always send the
0155 whole patch series again with the feedback incorporated (never send
0156 individual diffs on top of the old series).
0157
0158 Q: What does it mean when a patch gets applied to bpf or bpf-next tree?
0159 -----------------------------------------------------------------------
0160 A: It means that the patch looks good for mainline inclusion from
0161 a BPF point of view.
0162
0163 Be aware that this is not a final verdict that the patch will
0164 automatically get accepted into net or net-next trees eventually:
0165
0166 On the bpf kernel mailing list reviews can come in at any point
0167 in time. If discussions around a patch conclude that they cannot
0168 get included as-is, we will either apply a follow-up fix or drop
0169 them from the trees entirely. Therefore, we also reserve to rebase
0170 the trees when deemed necessary. After all, the purpose of the tree
0171 is to:
0172
0173 i) accumulate and stage BPF patches for integration into trees
0174 like net and net-next, and
0175
0176 ii) run extensive BPF test suite and
0177 workloads on the patches before they make their way any further.
0178
0179 Once the BPF pull request was accepted by David S. Miller, then
0180 the patches end up in net or net-next tree, respectively, and
0181 make their way from there further into mainline. Again, see the
0182 :ref:`netdev-FAQ` for additional information e.g. on how often they are
0183 merged to mainline.
0184
0185 Q: How long do I need to wait for feedback on my BPF patches?
0186 -------------------------------------------------------------
0187 A: We try to keep the latency low. The usual time to feedback will
0188 be around 2 or 3 business days. It may vary depending on the
0189 complexity of changes and current patch load.
0190
0191 Q: How often do you send pull requests to major kernel trees like net or net-next?
0192 ----------------------------------------------------------------------------------
0193
0194 A: Pull requests will be sent out rather often in order to not
0195 accumulate too many patches in bpf or bpf-next.
0196
0197 As a rule of thumb, expect pull requests for each tree regularly
0198 at the end of the week. In some cases pull requests could additionally
0199 come also in the middle of the week depending on the current patch
0200 load or urgency.
0201
0202 Q: Are patches applied to bpf-next when the merge window is open?
0203 -----------------------------------------------------------------
0204 A: For the time when the merge window is open, bpf-next will not be
0205 processed. This is roughly analogous to net-next patch processing,
0206 so feel free to read up on the :ref:`netdev-FAQ` about further details.
0207
0208 During those two weeks of merge window, we might ask you to resend
0209 your patch series once bpf-next is open again. Once Linus released
0210 a ``v*-rc1`` after the merge window, we continue processing of bpf-next.
0211
0212 For non-subscribers to kernel mailing lists, there is also a status
0213 page run by David S. Miller on net-next that provides guidance:
0214
0215 http://vger.kernel.org/~davem/net-next.html
0216
0217 Q: Verifier changes and test cases
0218 ----------------------------------
0219 Q: I made a BPF verifier change, do I need to add test cases for
0220 BPF kernel selftests_?
0221
0222 A: If the patch has changes to the behavior of the verifier, then yes,
0223 it is absolutely necessary to add test cases to the BPF kernel
0224 selftests_ suite. If they are not present and we think they are
0225 needed, then we might ask for them before accepting any changes.
0226
0227 In particular, test_verifier.c is tracking a high number of BPF test
0228 cases, including a lot of corner cases that LLVM BPF back end may
0229 generate out of the restricted C code. Thus, adding test cases is
0230 absolutely crucial to make sure future changes do not accidentally
0231 affect prior use-cases. Thus, treat those test cases as: verifier
0232 behavior that is not tracked in test_verifier.c could potentially
0233 be subject to change.
0234
0235 Q: samples/bpf preference vs selftests?
0236 ---------------------------------------
0237 Q: When should I add code to ``samples/bpf/`` and when to BPF kernel
0238 selftests_?
0239
0240 A: In general, we prefer additions to BPF kernel selftests_ rather than
0241 ``samples/bpf/``. The rationale is very simple: kernel selftests are
0242 regularly run by various bots to test for kernel regressions.
0243
0244 The more test cases we add to BPF selftests, the better the coverage
0245 and the less likely it is that those could accidentally break. It is
0246 not that BPF kernel selftests cannot demo how a specific feature can
0247 be used.
0248
0249 That said, ``samples/bpf/`` may be a good place for people to get started,
0250 so it might be advisable that simple demos of features could go into
0251 ``samples/bpf/``, but advanced functional and corner-case testing rather
0252 into kernel selftests.
0253
0254 If your sample looks like a test case, then go for BPF kernel selftests
0255 instead!
0256
0257 Q: When should I add code to the bpftool?
0258 -----------------------------------------
0259 A: The main purpose of bpftool (under tools/bpf/bpftool/) is to provide
0260 a central user space tool for debugging and introspection of BPF programs
0261 and maps that are active in the kernel. If UAPI changes related to BPF
0262 enable for dumping additional information of programs or maps, then
0263 bpftool should be extended as well to support dumping them.
0264
0265 Q: When should I add code to iproute2's BPF loader?
0266 ---------------------------------------------------
0267 A: For UAPI changes related to the XDP or tc layer (e.g. ``cls_bpf``),
0268 the convention is that those control-path related changes are added to
0269 iproute2's BPF loader as well from user space side. This is not only
0270 useful to have UAPI changes properly designed to be usable, but also
0271 to make those changes available to a wider user base of major
0272 downstream distributions.
0273
0274 Q: Do you accept patches as well for iproute2's BPF loader?
0275 -----------------------------------------------------------
0276 A: Patches for the iproute2's BPF loader have to be sent to:
0277
0278 netdev@vger.kernel.org
0279
0280 While those patches are not processed by the BPF kernel maintainers,
0281 please keep them in Cc as well, so they can be reviewed.
0282
0283 The official git repository for iproute2 is run by Stephen Hemminger
0284 and can be found at:
0285
0286 https://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git/
0287
0288 The patches need to have a subject prefix of '``[PATCH iproute2
0289 master]``' or '``[PATCH iproute2 net-next]``'. '``master``' or
0290 '``net-next``' describes the target branch where the patch should be
0291 applied to. Meaning, if kernel changes went into the net-next kernel
0292 tree, then the related iproute2 changes need to go into the iproute2
0293 net-next branch, otherwise they can be targeted at master branch. The
0294 iproute2 net-next branch will get merged into the master branch after
0295 the current iproute2 version from master has been released.
0296
0297 Like BPF, the patches end up in patchwork under the netdev project and
0298 are delegated to 'shemminger' for further processing:
0299
0300 http://patchwork.ozlabs.org/project/netdev/list/?delegate=389
0301
0302 Q: What is the minimum requirement before I submit my BPF patches?
0303 ------------------------------------------------------------------
0304 A: When submitting patches, always take the time and properly test your
0305 patches *prior* to submission. Never rush them! If maintainers find
0306 that your patches have not been properly tested, it is a good way to
0307 get them grumpy. Testing patch submissions is a hard requirement!
0308
0309 Note, fixes that go to bpf tree *must* have a ``Fixes:`` tag included.
0310 The same applies to fixes that target bpf-next, where the affected
0311 commit is in net-next (or in some cases bpf-next). The ``Fixes:`` tag is
0312 crucial in order to identify follow-up commits and tremendously helps
0313 for people having to do backporting, so it is a must have!
0314
0315 We also don't accept patches with an empty commit message. Take your
0316 time and properly write up a high quality commit message, it is
0317 essential!
0318
0319 Think about it this way: other developers looking at your code a month
0320 from now need to understand *why* a certain change has been done that
0321 way, and whether there have been flaws in the analysis or assumptions
0322 that the original author did. Thus providing a proper rationale and
0323 describing the use-case for the changes is a must.
0324
0325 Patch submissions with >1 patch must have a cover letter which includes
0326 a high level description of the series. This high level summary will
0327 then be placed into the merge commit by the BPF maintainers such that
0328 it is also accessible from the git log for future reference.
0329
0330 Q: Features changing BPF JIT and/or LLVM
0331 ----------------------------------------
0332 Q: What do I need to consider when adding a new instruction or feature
0333 that would require BPF JIT and/or LLVM integration as well?
0334
0335 A: We try hard to keep all BPF JITs up to date such that the same user
0336 experience can be guaranteed when running BPF programs on different
0337 architectures without having the program punt to the less efficient
0338 interpreter in case the in-kernel BPF JIT is enabled.
0339
0340 If you are unable to implement or test the required JIT changes for
0341 certain architectures, please work together with the related BPF JIT
0342 developers in order to get the feature implemented in a timely manner.
0343 Please refer to the git log (``arch/*/net/``) to locate the necessary
0344 people for helping out.
0345
0346 Also always make sure to add BPF test cases (e.g. test_bpf.c and
0347 test_verifier.c) for new instructions, so that they can receive
0348 broad test coverage and help run-time testing the various BPF JITs.
0349
0350 In case of new BPF instructions, once the changes have been accepted
0351 into the Linux kernel, please implement support into LLVM's BPF back
0352 end. See LLVM_ section below for further information.
0353
0354 Stable submission
0355 =================
0356
0357 Q: I need a specific BPF commit in stable kernels. What should I do?
0358 --------------------------------------------------------------------
0359 A: In case you need a specific fix in stable kernels, first check whether
0360 the commit has already been applied in the related ``linux-*.y`` branches:
0361
0362 https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/
0363
0364 If not the case, then drop an email to the BPF maintainers with the
0365 netdev kernel mailing list in Cc and ask for the fix to be queued up:
0366
0367 netdev@vger.kernel.org
0368
0369 The process in general is the same as on netdev itself, see also the
0370 :ref:`netdev-FAQ`.
0371
0372 Q: Do you also backport to kernels not currently maintained as stable?
0373 ----------------------------------------------------------------------
0374 A: No. If you need a specific BPF commit in kernels that are currently not
0375 maintained by the stable maintainers, then you are on your own.
0376
0377 The current stable and longterm stable kernels are all listed here:
0378
0379 https://www.kernel.org/
0380
0381 Q: The BPF patch I am about to submit needs to go to stable as well
0382 -------------------------------------------------------------------
0383 What should I do?
0384
0385 A: The same rules apply as with netdev patch submissions in general, see
0386 the :ref:`netdev-FAQ`.
0387
0388 Never add "``Cc: stable@vger.kernel.org``" to the patch description, but
0389 ask the BPF maintainers to queue the patches instead. This can be done
0390 with a note, for example, under the ``---`` part of the patch which does
0391 not go into the git log. Alternatively, this can be done as a simple
0392 request by mail instead.
0393
0394 Q: Queue stable patches
0395 -----------------------
0396 Q: Where do I find currently queued BPF patches that will be submitted
0397 to stable?
0398
0399 A: Once patches that fix critical bugs got applied into the bpf tree, they
0400 are queued up for stable submission under:
0401
0402 http://patchwork.ozlabs.org/bundle/bpf/stable/?state=*
0403
0404 They will be on hold there at minimum until the related commit made its
0405 way into the mainline kernel tree.
0406
0407 After having been under broader exposure, the queued patches will be
0408 submitted by the BPF maintainers to the stable maintainers.
0409
0410 Testing patches
0411 ===============
0412
0413 Q: How to run BPF selftests
0414 ---------------------------
0415 A: After you have booted into the newly compiled kernel, navigate to
0416 the BPF selftests_ suite in order to test BPF functionality (current
0417 working directory points to the root of the cloned git tree)::
0418
0419 $ cd tools/testing/selftests/bpf/
0420 $ make
0421
0422 To run the verifier tests::
0423
0424 $ sudo ./test_verifier
0425
0426 The verifier tests print out all the current checks being
0427 performed. The summary at the end of running all tests will dump
0428 information of test successes and failures::
0429
0430 Summary: 418 PASSED, 0 FAILED
0431
0432 In order to run through all BPF selftests, the following command is
0433 needed::
0434
0435 $ sudo make run_tests
0436
0437 See the kernels selftest `Documentation/dev-tools/kselftest.rst`_
0438 document for further documentation.
0439
0440 To maximize the number of tests passing, the .config of the kernel
0441 under test should match the config file fragment in
0442 tools/testing/selftests/bpf as closely as possible.
0443
0444 Finally to ensure support for latest BPF Type Format features -
0445 discussed in `Documentation/bpf/btf.rst`_ - pahole version 1.16
0446 is required for kernels built with CONFIG_DEBUG_INFO_BTF=y.
0447 pahole is delivered in the dwarves package or can be built
0448 from source at
0449
0450 https://github.com/acmel/dwarves
0451
0452 pahole starts to use libbpf definitions and APIs since v1.13 after the
0453 commit 21507cd3e97b ("pahole: add libbpf as submodule under lib/bpf").
0454 It works well with the git repository because the libbpf submodule will
0455 use "git submodule update --init --recursive" to update.
0456
0457 Unfortunately, the default github release source code does not contain
0458 libbpf submodule source code and this will cause build issues, the tarball
0459 from https://git.kernel.org/pub/scm/devel/pahole/pahole.git/ is same with
0460 github, you can get the source tarball with corresponding libbpf submodule
0461 codes from
0462
0463 https://fedorapeople.org/~acme/dwarves
0464
0465 Some distros have pahole version 1.16 packaged already, e.g.
0466 Fedora, Gentoo.
0467
0468 Q: Which BPF kernel selftests version should I run my kernel against?
0469 ---------------------------------------------------------------------
0470 A: If you run a kernel ``xyz``, then always run the BPF kernel selftests
0471 from that kernel ``xyz`` as well. Do not expect that the BPF selftest
0472 from the latest mainline tree will pass all the time.
0473
0474 In particular, test_bpf.c and test_verifier.c have a large number of
0475 test cases and are constantly updated with new BPF test sequences, or
0476 existing ones are adapted to verifier changes e.g. due to verifier
0477 becoming smarter and being able to better track certain things.
0478
0479 LLVM
0480 ====
0481
0482 Q: Where do I find LLVM with BPF support?
0483 -----------------------------------------
0484 A: The BPF back end for LLVM is upstream in LLVM since version 3.7.1.
0485
0486 All major distributions these days ship LLVM with BPF back end enabled,
0487 so for the majority of use-cases it is not required to compile LLVM by
0488 hand anymore, just install the distribution provided package.
0489
0490 LLVM's static compiler lists the supported targets through
0491 ``llc --version``, make sure BPF targets are listed. Example::
0492
0493 $ llc --version
0494 LLVM (http://llvm.org/):
0495 LLVM version 10.0.0
0496 Optimized build.
0497 Default target: x86_64-unknown-linux-gnu
0498 Host CPU: skylake
0499
0500 Registered Targets:
0501 aarch64 - AArch64 (little endian)
0502 bpf - BPF (host endian)
0503 bpfeb - BPF (big endian)
0504 bpfel - BPF (little endian)
0505 x86 - 32-bit X86: Pentium-Pro and above
0506 x86-64 - 64-bit X86: EM64T and AMD64
0507
0508 For developers in order to utilize the latest features added to LLVM's
0509 BPF back end, it is advisable to run the latest LLVM releases. Support
0510 for new BPF kernel features such as additions to the BPF instruction
0511 set are often developed together.
0512
0513 All LLVM releases can be found at: http://releases.llvm.org/
0514
0515 Q: Got it, so how do I build LLVM manually anyway?
0516 --------------------------------------------------
0517 A: We recommend that developers who want the fastest incremental builds
0518 use the Ninja build system, you can find it in your system's package
0519 manager, usually the package is ninja or ninja-build.
0520
0521 You need ninja, cmake and gcc-c++ as build requisites for LLVM. Once you
0522 have that set up, proceed with building the latest LLVM and clang version
0523 from the git repositories::
0524
0525 $ git clone https://github.com/llvm/llvm-project.git
0526 $ mkdir -p llvm-project/llvm/build
0527 $ cd llvm-project/llvm/build
0528 $ cmake .. -G "Ninja" -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
0529 -DLLVM_ENABLE_PROJECTS="clang" \
0530 -DCMAKE_BUILD_TYPE=Release \
0531 -DLLVM_BUILD_RUNTIME=OFF
0532 $ ninja
0533
0534 The built binaries can then be found in the build/bin/ directory, where
0535 you can point the PATH variable to.
0536
0537 Set ``-DLLVM_TARGETS_TO_BUILD`` equal to the target you wish to build, you
0538 will find a full list of targets within the llvm-project/llvm/lib/Target
0539 directory.
0540
0541 Q: Reporting LLVM BPF issues
0542 ----------------------------
0543 Q: Should I notify BPF kernel maintainers about issues in LLVM's BPF code
0544 generation back end or about LLVM generated code that the verifier
0545 refuses to accept?
0546
0547 A: Yes, please do!
0548
0549 LLVM's BPF back end is a key piece of the whole BPF
0550 infrastructure and it ties deeply into verification of programs from the
0551 kernel side. Therefore, any issues on either side need to be investigated
0552 and fixed whenever necessary.
0553
0554 Therefore, please make sure to bring them up at netdev kernel mailing
0555 list and Cc BPF maintainers for LLVM and kernel bits:
0556
0557 * Yonghong Song <yhs@fb.com>
0558 * Alexei Starovoitov <ast@kernel.org>
0559 * Daniel Borkmann <daniel@iogearbox.net>
0560
0561 LLVM also has an issue tracker where BPF related bugs can be found:
0562
0563 https://bugs.llvm.org/buglist.cgi?quicksearch=bpf
0564
0565 However, it is better to reach out through mailing lists with having
0566 maintainers in Cc.
0567
0568 Q: New BPF instruction for kernel and LLVM
0569 ------------------------------------------
0570 Q: I have added a new BPF instruction to the kernel, how can I integrate
0571 it into LLVM?
0572
0573 A: LLVM has a ``-mcpu`` selector for the BPF back end in order to allow
0574 the selection of BPF instruction set extensions. By default the
0575 ``generic`` processor target is used, which is the base instruction set
0576 (v1) of BPF.
0577
0578 LLVM has an option to select ``-mcpu=probe`` where it will probe the host
0579 kernel for supported BPF instruction set extensions and selects the
0580 optimal set automatically.
0581
0582 For cross-compilation, a specific version can be select manually as well ::
0583
0584 $ llc -march bpf -mcpu=help
0585 Available CPUs for this target:
0586
0587 generic - Select the generic processor.
0588 probe - Select the probe processor.
0589 v1 - Select the v1 processor.
0590 v2 - Select the v2 processor.
0591 [...]
0592
0593 Newly added BPF instructions to the Linux kernel need to follow the same
0594 scheme, bump the instruction set version and implement probing for the
0595 extensions such that ``-mcpu=probe`` users can benefit from the
0596 optimization transparently when upgrading their kernels.
0597
0598 If you are unable to implement support for the newly added BPF instruction
0599 please reach out to BPF developers for help.
0600
0601 By the way, the BPF kernel selftests run with ``-mcpu=probe`` for better
0602 test coverage.
0603
0604 Q: clang flag for target bpf?
0605 -----------------------------
0606 Q: In some cases clang flag ``-target bpf`` is used but in other cases the
0607 default clang target, which matches the underlying architecture, is used.
0608 What is the difference and when I should use which?
0609
0610 A: Although LLVM IR generation and optimization try to stay architecture
0611 independent, ``-target <arch>`` still has some impact on generated code:
0612
0613 - BPF program may recursively include header file(s) with file scope
0614 inline assembly codes. The default target can handle this well,
0615 while ``bpf`` target may fail if bpf backend assembler does not
0616 understand these assembly codes, which is true in most cases.
0617
0618 - When compiled without ``-g``, additional elf sections, e.g.,
0619 .eh_frame and .rela.eh_frame, may be present in the object file
0620 with default target, but not with ``bpf`` target.
0621
0622 - The default target may turn a C switch statement into a switch table
0623 lookup and jump operation. Since the switch table is placed
0624 in the global readonly section, the bpf program will fail to load.
0625 The bpf target does not support switch table optimization.
0626 The clang option ``-fno-jump-tables`` can be used to disable
0627 switch table generation.
0628
0629 - For clang ``-target bpf``, it is guaranteed that pointer or long /
0630 unsigned long types will always have a width of 64 bit, no matter
0631 whether underlying clang binary or default target (or kernel) is
0632 32 bit. However, when native clang target is used, then it will
0633 compile these types based on the underlying architecture's conventions,
0634 meaning in case of 32 bit architecture, pointer or long / unsigned
0635 long types e.g. in BPF context structure will have width of 32 bit
0636 while the BPF LLVM back end still operates in 64 bit. The native
0637 target is mostly needed in tracing for the case of walking ``pt_regs``
0638 or other kernel structures where CPU's register width matters.
0639 Otherwise, ``clang -target bpf`` is generally recommended.
0640
0641 You should use default target when:
0642
0643 - Your program includes a header file, e.g., ptrace.h, which eventually
0644 pulls in some header files containing file scope host assembly codes.
0645
0646 - You can add ``-fno-jump-tables`` to work around the switch table issue.
0647
0648 Otherwise, you can use ``bpf`` target. Additionally, you *must* use bpf target
0649 when:
0650
0651 - Your program uses data structures with pointer or long / unsigned long
0652 types that interface with BPF helpers or context data structures. Access
0653 into these structures is verified by the BPF verifier and may result
0654 in verification failures if the native architecture is not aligned with
0655 the BPF architecture, e.g. 64-bit. An example of this is
0656 BPF_PROG_TYPE_SK_MSG require ``-target bpf``
0657
0658
0659 .. Links
0660 .. _Documentation/process/: https://www.kernel.org/doc/html/latest/process/
0661 .. _netdev-FAQ: Documentation/process/maintainer-netdev.rst
0662 .. _selftests:
0663 https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/testing/selftests/bpf/
0664 .. _Documentation/dev-tools/kselftest.rst:
0665 https://www.kernel.org/doc/html/latest/dev-tools/kselftest.html
0666 .. _Documentation/bpf/btf.rst: btf.rst
0667
0668 Happy BPF hacking!