xref: /linux/tools/testing/selftests/bpf/README.rst (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
199aaf53eSYonghong Song==================
299aaf53eSYonghong SongBPF Selftest Notes
399aaf53eSYonghong Song==================
46736aa79SAlan MaguireGeneral instructions on running selftests can be found in
505a98d76SAndrei Matei`Documentation/bpf/bpf_devel_QA.rst`__.
605a98d76SAndrei Matei
705a98d76SAndrei Matei__ /Documentation/bpf/bpf_devel_QA.rst#q-how-to-run-bpf-selftests
805a98d76SAndrei Matei
981bfcc3fSDaniel Müller=============
1081bfcc3fSDaniel MüllerBPF CI System
1181bfcc3fSDaniel Müller=============
1281bfcc3fSDaniel Müller
1381bfcc3fSDaniel MüllerBPF employs a continuous integration (CI) system to check patch submission in an
1481bfcc3fSDaniel Müllerautomated fashion. The system runs selftests for each patch in a series. Results
1581bfcc3fSDaniel Müllerare propagated to patchwork, where failures are highlighted similar to
1681bfcc3fSDaniel Müllerviolations of other checks (such as additional warnings being emitted or a
1781bfcc3fSDaniel Müller``scripts/checkpatch.pl`` reported deficiency):
1881bfcc3fSDaniel Müller
1981bfcc3fSDaniel Müller  https://patchwork.kernel.org/project/netdevbpf/list/?delegate=121173
2081bfcc3fSDaniel Müller
2181bfcc3fSDaniel MüllerThe CI system executes tests on multiple architectures. It uses a kernel
2281bfcc3fSDaniel Müllerconfiguration derived from both the generic and architecture specific config
2381bfcc3fSDaniel Müllerfile fragments below ``tools/testing/selftests/bpf/`` (e.g., ``config`` and
2481bfcc3fSDaniel Müller``config.x86_64``).
2581bfcc3fSDaniel Müller
2681bfcc3fSDaniel MüllerDenylisting Tests
2781bfcc3fSDaniel Müller=================
2881bfcc3fSDaniel Müller
2981bfcc3fSDaniel MüllerIt is possible for some architectures to not have support for all BPF features.
3081bfcc3fSDaniel MüllerIn such a case tests in CI may fail. An example of such a shortcoming is BPF
3181bfcc3fSDaniel Müllertrampoline support on IBM's s390x architecture. For cases like this, an in-tree
3281bfcc3fSDaniel Müllerdeny list file, located at ``tools/testing/selftests/bpf/DENYLIST.<arch>``, can
3381bfcc3fSDaniel Müllerbe used to prevent the test from running on such an architecture.
3481bfcc3fSDaniel Müller
3581bfcc3fSDaniel MüllerIn addition to that, the generic ``tools/testing/selftests/bpf/DENYLIST`` is
3681bfcc3fSDaniel Müllerhonored on every architecture running tests.
3781bfcc3fSDaniel Müller
3881bfcc3fSDaniel MüllerThese files are organized in three columns. The first column lists the test in
3981bfcc3fSDaniel Müllerquestion. This can be the name of a test suite or of an individual test. The
4081bfcc3fSDaniel Müllerremaining two columns provide additional meta data that helps identify and
4181bfcc3fSDaniel Müllerclassify the entry: column two is a copy and paste of the error being reported
4281bfcc3fSDaniel Müllerwhen running the test in the setting in question. The third column, if
4381bfcc3fSDaniel Mülleravailable, summarizes the underlying problem. A value of ``trampoline``, for
4481bfcc3fSDaniel Müllerexample, indicates that lack of trampoline support is causing the test to fail.
4581bfcc3fSDaniel MüllerThis last entry helps identify tests that can be re-enabled once such support is
4681bfcc3fSDaniel Mülleradded.
4781bfcc3fSDaniel Müller
48881949f7SKP Singh=========================
49881949f7SKP SinghRunning Selftests in a VM
50881949f7SKP Singh=========================
51881949f7SKP Singh
52881949f7SKP SinghIt's now possible to run the selftests using ``tools/testing/selftests/bpf/vmtest.sh``.
53881949f7SKP SinghThe script tries to ensure that the tests are run with the same environment as they
5481bfcc3fSDaniel Müllerwould be run post-submit in the CI used by the Maintainers, with the exception
5581bfcc3fSDaniel Müllerthat deny lists are not automatically honored.
56881949f7SKP Singh
576c4e777fSDaniel MüllerThis script uses the in-tree kernel configuration and downloads a VM userspace
586c4e777fSDaniel Müllerimage from the system used by the CI. It builds the kernel (without overwriting
596c4e777fSDaniel Mülleryour existing Kconfig), recompiles the bpf selftests, runs them (by default
606c4e777fSDaniel Müller``tools/testing/selftests/bpf/test_progs``) and saves the resulting output (by
616c4e777fSDaniel Müllerdefault in ``~/.bpf_selftests``).
62881949f7SKP Singh
637a188442SEvgeniy LitvinenkoScript dependencies:
647a188442SEvgeniy Litvinenko- clang (preferably built from sources, https://github.com/llvm/llvm-project);
657a188442SEvgeniy Litvinenko- pahole (preferably built from sources, https://git.kernel.org/pub/scm/devel/pahole/pahole.git/);
667a188442SEvgeniy Litvinenko- qemu;
677a188442SEvgeniy Litvinenko- docutils (for ``rst2man``);
687a188442SEvgeniy Litvinenko- libcap-devel.
697a188442SEvgeniy Litvinenko
706c4e777fSDaniel MüllerFor more information about using the script, run:
71881949f7SKP Singh
72881949f7SKP Singh.. code-block:: console
73881949f7SKP Singh
74881949f7SKP Singh  $ tools/testing/selftests/bpf/vmtest.sh -h
75881949f7SKP Singh
765ad0a415SKP SinghIn case of linker errors when running selftests, try using static linking:
775ad0a415SKP Singh
785ad0a415SKP Singh.. code-block:: console
795ad0a415SKP Singh
8018f6f9deSAkihiko Odaki  $ LDLIBS=-static PKG_CONFIG='pkg-config --static' vmtest.sh
815ad0a415SKP Singh
825ad0a415SKP Singh.. note:: Some distros may not support static linking.
835ad0a415SKP Singh
84881949f7SKP Singh.. note:: The script uses pahole and clang based on host environment setting.
85881949f7SKP Singh          If you want to change pahole and llvm, you can change `PATH` environment
86881949f7SKP Singh          variable in the beginning of script.
87881949f7SKP Singh
88e878ae2dSKP Singh.. note:: The script currently only supports x86_64 and s390x architectures.
8999aaf53eSYonghong Song
9099aaf53eSYonghong SongAdditional information about selftest failures are
9199aaf53eSYonghong Songdocumented here.
9299aaf53eSYonghong Song
9303d4d13fSAlexei Starovoitovprofiler[23] test failures with clang/llvm <12.0.0
9403d4d13fSAlexei Starovoitov==================================================
9503d4d13fSAlexei Starovoitov
9603d4d13fSAlexei StarovoitovWith clang/llvm <12.0.0, the profiler[23] test may fail.
9703d4d13fSAlexei StarovoitovThe symptom looks like
9803d4d13fSAlexei Starovoitov
9903d4d13fSAlexei Starovoitov.. code-block:: c
10003d4d13fSAlexei Starovoitov
10103d4d13fSAlexei Starovoitov  // r9 is a pointer to map_value
10203d4d13fSAlexei Starovoitov  // r7 is a scalar
10303d4d13fSAlexei Starovoitov  17:       bf 96 00 00 00 00 00 00 r6 = r9
10403d4d13fSAlexei Starovoitov  18:       0f 76 00 00 00 00 00 00 r6 += r7
10503d4d13fSAlexei Starovoitov  math between map_value pointer and register with unbounded min value is not allowed
10603d4d13fSAlexei Starovoitov
10703d4d13fSAlexei Starovoitov  // the instructions below will not be seen in the verifier log
10803d4d13fSAlexei Starovoitov  19:       a5 07 01 00 01 01 00 00 if r7 < 257 goto +1
10903d4d13fSAlexei Starovoitov  20:       bf 96 00 00 00 00 00 00 r6 = r9
11003d4d13fSAlexei Starovoitov  // r6 is used here
11103d4d13fSAlexei Starovoitov
11203d4d13fSAlexei StarovoitovThe verifier will reject such code with above error.
11303d4d13fSAlexei StarovoitovAt insn 18 the r7 is indeed unbounded. The later insn 19 checks the bounds and
11403d4d13fSAlexei Starovoitovthe insn 20 undoes map_value addition. It is currently impossible for the
11503d4d13fSAlexei Starovoitovverifier to understand such speculative pointer arithmetic.
1161c26ac6aSAndrei MateiHence `this patch`__ addresses it on the compiler side. It was committed on llvm 12.
1171c26ac6aSAndrei Matei
118*f067074bSNathan Chancellor__ https://github.com/llvm/llvm-project/commit/ddf1864ace484035e3cde5e83b3a31ac81e059c6
11903d4d13fSAlexei Starovoitov
12003d4d13fSAlexei StarovoitovThe corresponding C code
1211c26ac6aSAndrei Matei
12203d4d13fSAlexei Starovoitov.. code-block:: c
12303d4d13fSAlexei Starovoitov
12403d4d13fSAlexei Starovoitov  for (int i = 0; i < MAX_CGROUPS_PATH_DEPTH; i++) {
12503d4d13fSAlexei Starovoitov          filepart_length = bpf_probe_read_str(payload, ...);
12603d4d13fSAlexei Starovoitov          if (filepart_length <= MAX_PATH) {
12703d4d13fSAlexei Starovoitov                  barrier_var(filepart_length); // workaround
12803d4d13fSAlexei Starovoitov                  payload += filepart_length;
12903d4d13fSAlexei Starovoitov          }
13003d4d13fSAlexei Starovoitov  }
13103d4d13fSAlexei Starovoitov
13299aaf53eSYonghong Songbpf_iter test failures with clang/llvm 10.0.0
13399aaf53eSYonghong Song=============================================
13499aaf53eSYonghong Song
13599aaf53eSYonghong SongWith clang/llvm 10.0.0, the following two bpf_iter tests failed:
13699aaf53eSYonghong Song  * ``bpf_iter/ipv6_route``
13799aaf53eSYonghong Song  * ``bpf_iter/netlink``
13899aaf53eSYonghong Song
13999aaf53eSYonghong SongThe symptom for ``bpf_iter/ipv6_route`` looks like
14099aaf53eSYonghong Song
14199aaf53eSYonghong Song.. code-block:: c
14299aaf53eSYonghong Song
14399aaf53eSYonghong Song  2: (79) r8 = *(u64 *)(r1 +8)
14499aaf53eSYonghong Song  ...
14599aaf53eSYonghong Song  14: (bf) r2 = r8
14699aaf53eSYonghong Song  15: (0f) r2 += r1
14799aaf53eSYonghong Song  ; BPF_SEQ_PRINTF(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
14899aaf53eSYonghong Song  16: (7b) *(u64 *)(r8 +64) = r2
14999aaf53eSYonghong Song  only read is supported
15099aaf53eSYonghong Song
15199aaf53eSYonghong SongThe symptom for ``bpf_iter/netlink`` looks like
15299aaf53eSYonghong Song
15399aaf53eSYonghong Song.. code-block:: c
15499aaf53eSYonghong Song
15599aaf53eSYonghong Song  ; struct netlink_sock *nlk = ctx->sk;
15699aaf53eSYonghong Song  2: (79) r7 = *(u64 *)(r1 +8)
15799aaf53eSYonghong Song  ...
15899aaf53eSYonghong Song  15: (bf) r2 = r7
15999aaf53eSYonghong Song  16: (0f) r2 += r1
16099aaf53eSYonghong Song  ; BPF_SEQ_PRINTF(seq, "%pK %-3d ", s, s->sk_protocol);
16199aaf53eSYonghong Song  17: (7b) *(u64 *)(r7 +0) = r2
16299aaf53eSYonghong Song  only read is supported
16399aaf53eSYonghong Song
1641c26ac6aSAndrei MateiThis is due to a llvm BPF backend bug. `The fix`__
16599aaf53eSYonghong Songhas been pushed to llvm 10.x release branch and will be
1661c26ac6aSAndrei Mateiavailable in 10.0.1. The patch is available in llvm 11.0.0 trunk.
1671c26ac6aSAndrei Matei
168*f067074bSNathan Chancellor__  https://github.com/llvm/llvm-project/commit/3cb7e7bf959dcd3b8080986c62e10a75c7af43f0
169149cb339SAndrii Nakryiko
170afef88e6SDaniel Müllerbpf_verif_scale/loop6.bpf.o test failure with Clang 12
171afef88e6SDaniel Müller======================================================
17286a35af6SYonghong Song
17386a35af6SYonghong SongWith Clang 12, the following bpf_verif_scale test failed:
174afef88e6SDaniel Müller  * ``bpf_verif_scale/loop6.bpf.o``
17586a35af6SYonghong Song
17686a35af6SYonghong SongThe verifier output looks like
17786a35af6SYonghong Song
17886a35af6SYonghong Song.. code-block:: c
17986a35af6SYonghong Song
18086a35af6SYonghong Song  R1 type=ctx expected=fp
18186a35af6SYonghong Song  The sequence of 8193 jumps is too complex.
18286a35af6SYonghong Song
18386a35af6SYonghong SongThe reason is compiler generating the following code
18486a35af6SYonghong Song
18586a35af6SYonghong Song.. code-block:: c
18686a35af6SYonghong Song
18786a35af6SYonghong Song  ;       for (i = 0; (i < VIRTIO_MAX_SGS) && (i < num); i++) {
18886a35af6SYonghong Song      14:       16 05 40 00 00 00 00 00 if w5 == 0 goto +64 <LBB0_6>
18986a35af6SYonghong Song      15:       bc 51 00 00 00 00 00 00 w1 = w5
19086a35af6SYonghong Song      16:       04 01 00 00 ff ff ff ff w1 += -1
19186a35af6SYonghong Song      17:       67 05 00 00 20 00 00 00 r5 <<= 32
19286a35af6SYonghong Song      18:       77 05 00 00 20 00 00 00 r5 >>= 32
19386a35af6SYonghong Song      19:       a6 01 01 00 05 00 00 00 if w1 < 5 goto +1 <LBB0_4>
19486a35af6SYonghong Song      20:       b7 05 00 00 06 00 00 00 r5 = 6
19586a35af6SYonghong Song  00000000000000a8 <LBB0_4>:
19686a35af6SYonghong Song      21:       b7 02 00 00 00 00 00 00 r2 = 0
19786a35af6SYonghong Song      22:       b7 01 00 00 00 00 00 00 r1 = 0
19886a35af6SYonghong Song  ;       for (i = 0; (i < VIRTIO_MAX_SGS) && (i < num); i++) {
19986a35af6SYonghong Song      23:       7b 1a e0 ff 00 00 00 00 *(u64 *)(r10 - 32) = r1
20086a35af6SYonghong Song      24:       7b 5a c0 ff 00 00 00 00 *(u64 *)(r10 - 64) = r5
20186a35af6SYonghong Song
20286a35af6SYonghong SongNote that insn #15 has w1 = w5 and w1 is refined later but
20386a35af6SYonghong Songr5(w5) is eventually saved on stack at insn #24 for later use.
20486a35af6SYonghong SongThis cause later verifier failure. The bug has been `fixed`__ in
20586a35af6SYonghong SongClang 13.
20686a35af6SYonghong Song
207*f067074bSNathan Chancellor__  https://github.com/llvm/llvm-project/commit/1959ead525b8830cc8a345f45e1c3ef9902d3229
20886a35af6SYonghong Song
209149cb339SAndrii NakryikoBPF CO-RE-based tests and Clang version
210149cb339SAndrii Nakryiko=======================================
211149cb339SAndrii Nakryiko
212149cb339SAndrii NakryikoA set of selftests use BPF target-specific built-ins, which might require
213149cb339SAndrii Nakryikobleeding-edge Clang versions (Clang 12 nightly at this time).
214149cb339SAndrii Nakryiko
215149cb339SAndrii NakryikoFew sub-tests of core_reloc test suit (part of test_progs test runner) require
216149cb339SAndrii Nakryikothe following built-ins, listed with corresponding Clang diffs introducing
217149cb339SAndrii Nakryikothem to Clang/LLVM. These sub-tests are going to be skipped if Clang is too
218149cb339SAndrii Nakryikoold to support them, they shouldn't cause build failures or runtime test
219149cb339SAndrii Nakryikofailures:
220149cb339SAndrii Nakryiko
2211c26ac6aSAndrei Matei- __builtin_btf_type_id() [0_, 1_, 2_];
2221c26ac6aSAndrei Matei- __builtin_preserve_type_info(), __builtin_preserve_enum_value() [3_, 4_].
223149cb339SAndrii Nakryiko
224*f067074bSNathan Chancellor.. _0: https://github.com/llvm/llvm-project/commit/6b01b465388b204d543da3cf49efd6080db094a9
225*f067074bSNathan Chancellor.. _1: https://github.com/llvm/llvm-project/commit/072cde03aaa13a2c57acf62d79876bf79aa1919f
226*f067074bSNathan Chancellor.. _2: https://github.com/llvm/llvm-project/commit/00602ee7ef0bf6c68d690a2bd729c12b95c95c99
227*f067074bSNathan Chancellor.. _3: https://github.com/llvm/llvm-project/commit/6d218b4adb093ff2e9764febbbc89f429412006c
228*f067074bSNathan Chancellor.. _4: https://github.com/llvm/llvm-project/commit/6d6750696400e7ce988d66a1a00e1d0cb32815f8
2293fcd50d6SIlya Leoshkevich
2303fcd50d6SIlya LeoshkevichFloating-point tests and Clang version
2313fcd50d6SIlya Leoshkevich======================================
2323fcd50d6SIlya Leoshkevich
2333fcd50d6SIlya LeoshkevichCertain selftests, e.g. core_reloc, require support for the floating-point
2343fcd50d6SIlya Leoshkevichtypes, which was introduced in `Clang 13`__. The older Clang versions will
2353fcd50d6SIlya Leoshkevicheither crash when compiling these tests, or generate an incorrect BTF.
2363fcd50d6SIlya Leoshkevich
237*f067074bSNathan Chancellor__  https://github.com/llvm/llvm-project/commit/a7137b238a07d9399d3ae96c0b461571bd5aa8b2
2382ba4badcSMartin KaFai Lau
2392ba4badcSMartin KaFai LauKernel function call test and Clang version
2402ba4badcSMartin KaFai Lau===========================================
2412ba4badcSMartin KaFai Lau
2422ba4badcSMartin KaFai LauSome selftests (e.g. kfunc_call and bpf_tcp_ca) require a LLVM support
2432ba4badcSMartin KaFai Lauto generate extern function in BTF.  It was introduced in `Clang 13`__.
2442ba4badcSMartin KaFai Lau
2452ba4badcSMartin KaFai LauWithout it, the error from compiling bpf selftests looks like:
2462ba4badcSMartin KaFai Lau
2472ba4badcSMartin KaFai Lau.. code-block:: console
2482ba4badcSMartin KaFai Lau
2492ba4badcSMartin KaFai Lau  libbpf: failed to find BTF for extern 'tcp_slow_start' [25] section: -2
2502ba4badcSMartin KaFai Lau
251*f067074bSNathan Chancellor__ https://github.com/llvm/llvm-project/commit/886f9ff53155075bd5f1e994f17b85d1e1b7470c
252a9dab4e4SAndrii Nakryiko
2532220ecf5SYonghong Songbtf_tag test and Clang version
2542220ecf5SYonghong Song==============================
2552220ecf5SYonghong Song
2563f1d0dc0SYonghong SongThe btf_tag selftest requires LLVM support to recognize the btf_decl_tag and
2573f1d0dc0SYonghong Songbtf_type_tag attributes. They are introduced in `Clang 14` [0_, 1_].
25867ef7e1aSYonghong SongThe subtests ``btf_type_tag_user_{mod1, mod2, vmlinux}`` also requires
25967ef7e1aSYonghong Songpahole version ``1.23``.
2602220ecf5SYonghong Song
2613f1d0dc0SYonghong SongWithout them, the btf_tag selftest will be skipped and you will observe:
2622220ecf5SYonghong Song
2632220ecf5SYonghong Song.. code-block:: console
2642220ecf5SYonghong Song
2652220ecf5SYonghong Song  #<test_num> btf_tag:SKIP
2662220ecf5SYonghong Song
267*f067074bSNathan Chancellor.. _0: https://github.com/llvm/llvm-project/commit/a162b67c98066218d0d00aa13b99afb95d9bb5e6
268*f067074bSNathan Chancellor.. _1: https://github.com/llvm/llvm-project/commit/3466e00716e12e32fdb100e3fcfca5c2b3e8d784
2692220ecf5SYonghong Song
270a9dab4e4SAndrii NakryikoClang dependencies for static linking tests
271a9dab4e4SAndrii Nakryiko===========================================
272a9dab4e4SAndrii Nakryiko
273a9dab4e4SAndrii Nakryikolinked_vars, linked_maps, and linked_funcs tests depend on `Clang fix`__ to
274a9dab4e4SAndrii Nakryikogenerate valid BTF information for weak variables. Please make sure you use
275a9dab4e4SAndrii NakryikoClang that contains the fix.
276a9dab4e4SAndrii Nakryiko
277*f067074bSNathan Chancellor__ https://github.com/llvm/llvm-project/commit/968292cb93198442138128d850fd54dc7edc0035
278fc8c262eSYonghong Song
279fc8c262eSYonghong SongClang relocation changes
280fc8c262eSYonghong Song========================
281fc8c262eSYonghong Song
282fc8c262eSYonghong SongClang 13 patch `clang reloc patch`_  made some changes on relocations such
283fc8c262eSYonghong Songthat existing relocation types are broken into more types and
284fc8c262eSYonghong Songeach new type corresponds to only one way to resolve relocation.
285fc8c262eSYonghong SongSee `kernel llvm reloc`_ for more explanation and some examples.
286fc8c262eSYonghong SongUsing clang 13 to compile old libbpf which has static linker support,
287fc8c262eSYonghong Songthere will be a compilation failure::
288fc8c262eSYonghong Song
289afef88e6SDaniel Müller  libbpf: ELF relo #0 in section #6 has unexpected type 2 in .../bpf_tcp_nogpl.bpf.o
290fc8c262eSYonghong Song
291fc8c262eSYonghong SongHere, ``type 2`` refers to new relocation type ``R_BPF_64_ABS64``.
292fc8c262eSYonghong SongTo fix this issue, user newer libbpf.
293fc8c262eSYonghong Song
294fc8c262eSYonghong Song.. Links
295*f067074bSNathan Chancellor.. _clang reloc patch: https://github.com/llvm/llvm-project/commit/6a2ea84600ba4bd3b2733bd8f08f5115eb32164b
296fc8c262eSYonghong Song.. _kernel llvm reloc: /Documentation/bpf/llvm_reloc.rst
29754ea6079SMartin KaFai Lau
29854ea6079SMartin KaFai LauClang dependencies for the u32 spill test (xdpwall)
29954ea6079SMartin KaFai Lau===================================================
30054ea6079SMartin KaFai LauThe xdpwall selftest requires a change in `Clang 14`__.
30154ea6079SMartin KaFai Lau
30254ea6079SMartin KaFai LauWithout it, the xdpwall selftest will fail and the error message
30354ea6079SMartin KaFai Laufrom running test_progs will look like:
30454ea6079SMartin KaFai Lau
30554ea6079SMartin KaFai Lau.. code-block:: console
30654ea6079SMartin KaFai Lau
307*f067074bSNathan Chancellor  test_xdpwall:FAIL:Does LLVM have https://github.com/llvm/llvm-project/commit/ea72b0319d7b0f0c2fcf41d121afa5d031b319d5? unexpected error: -4007
30854ea6079SMartin KaFai Lau
309*f067074bSNathan Chancellor__ https://github.com/llvm/llvm-project/commit/ea72b0319d7b0f0c2fcf41d121afa5d031b319d5
310