1================== 2BPF Selftest Notes 3================== 4General instructions on running selftests can be found in 5`Documentation/bpf/bpf_devel_QA.rst`__. 6 7__ /Documentation/bpf/bpf_devel_QA.rst#q-how-to-run-bpf-selftests 8 9========================= 10Running Selftests in a VM 11========================= 12 13It's now possible to run the selftests using ``tools/testing/selftests/bpf/vmtest.sh``. 14The script tries to ensure that the tests are run with the same environment as they 15would be run post-submit in the CI used by the Maintainers. 16 17This script uses the in-tree kernel configuration and downloads a VM userspace 18image from the system used by the CI. It builds the kernel (without overwriting 19your existing Kconfig), recompiles the bpf selftests, runs them (by default 20``tools/testing/selftests/bpf/test_progs``) and saves the resulting output (by 21default in ``~/.bpf_selftests``). 22 23Script dependencies: 24- clang (preferably built from sources, https://github.com/llvm/llvm-project); 25- pahole (preferably built from sources, https://git.kernel.org/pub/scm/devel/pahole/pahole.git/); 26- qemu; 27- docutils (for ``rst2man``); 28- libcap-devel. 29 30For more information about using the script, run: 31 32.. code-block:: console 33 34 $ tools/testing/selftests/bpf/vmtest.sh -h 35 36In case of linker errors when running selftests, try using static linking: 37 38.. code-block:: console 39 40 $ LDLIBS=-static vmtest.sh 41 42.. note:: Some distros may not support static linking. 43 44.. note:: The script uses pahole and clang based on host environment setting. 45 If you want to change pahole and llvm, you can change `PATH` environment 46 variable in the beginning of script. 47 48.. note:: The script currently only supports x86_64 and s390x architectures. 49 50Additional information about selftest failures are 51documented here. 52 53profiler[23] test failures with clang/llvm <12.0.0 54================================================== 55 56With clang/llvm <12.0.0, the profiler[23] test may fail. 57The symptom looks like 58 59.. code-block:: c 60 61 // r9 is a pointer to map_value 62 // r7 is a scalar 63 17: bf 96 00 00 00 00 00 00 r6 = r9 64 18: 0f 76 00 00 00 00 00 00 r6 += r7 65 math between map_value pointer and register with unbounded min value is not allowed 66 67 // the instructions below will not be seen in the verifier log 68 19: a5 07 01 00 01 01 00 00 if r7 < 257 goto +1 69 20: bf 96 00 00 00 00 00 00 r6 = r9 70 // r6 is used here 71 72The verifier will reject such code with above error. 73At insn 18 the r7 is indeed unbounded. The later insn 19 checks the bounds and 74the insn 20 undoes map_value addition. It is currently impossible for the 75verifier to understand such speculative pointer arithmetic. 76Hence `this patch`__ addresses it on the compiler side. It was committed on llvm 12. 77 78__ https://reviews.llvm.org/D85570 79 80The corresponding C code 81 82.. code-block:: c 83 84 for (int i = 0; i < MAX_CGROUPS_PATH_DEPTH; i++) { 85 filepart_length = bpf_probe_read_str(payload, ...); 86 if (filepart_length <= MAX_PATH) { 87 barrier_var(filepart_length); // workaround 88 payload += filepart_length; 89 } 90 } 91 92bpf_iter test failures with clang/llvm 10.0.0 93============================================= 94 95With clang/llvm 10.0.0, the following two bpf_iter tests failed: 96 * ``bpf_iter/ipv6_route`` 97 * ``bpf_iter/netlink`` 98 99The symptom for ``bpf_iter/ipv6_route`` looks like 100 101.. code-block:: c 102 103 2: (79) r8 = *(u64 *)(r1 +8) 104 ... 105 14: (bf) r2 = r8 106 15: (0f) r2 += r1 107 ; BPF_SEQ_PRINTF(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen); 108 16: (7b) *(u64 *)(r8 +64) = r2 109 only read is supported 110 111The symptom for ``bpf_iter/netlink`` looks like 112 113.. code-block:: c 114 115 ; struct netlink_sock *nlk = ctx->sk; 116 2: (79) r7 = *(u64 *)(r1 +8) 117 ... 118 15: (bf) r2 = r7 119 16: (0f) r2 += r1 120 ; BPF_SEQ_PRINTF(seq, "%pK %-3d ", s, s->sk_protocol); 121 17: (7b) *(u64 *)(r7 +0) = r2 122 only read is supported 123 124This is due to a llvm BPF backend bug. `The fix`__ 125has been pushed to llvm 10.x release branch and will be 126available in 10.0.1. The patch is available in llvm 11.0.0 trunk. 127 128__ https://reviews.llvm.org/D78466 129 130bpf_verif_scale/loop6.bpf.o test failure with Clang 12 131====================================================== 132 133With Clang 12, the following bpf_verif_scale test failed: 134 * ``bpf_verif_scale/loop6.bpf.o`` 135 136The verifier output looks like 137 138.. code-block:: c 139 140 R1 type=ctx expected=fp 141 The sequence of 8193 jumps is too complex. 142 143The reason is compiler generating the following code 144 145.. code-block:: c 146 147 ; for (i = 0; (i < VIRTIO_MAX_SGS) && (i < num); i++) { 148 14: 16 05 40 00 00 00 00 00 if w5 == 0 goto +64 <LBB0_6> 149 15: bc 51 00 00 00 00 00 00 w1 = w5 150 16: 04 01 00 00 ff ff ff ff w1 += -1 151 17: 67 05 00 00 20 00 00 00 r5 <<= 32 152 18: 77 05 00 00 20 00 00 00 r5 >>= 32 153 19: a6 01 01 00 05 00 00 00 if w1 < 5 goto +1 <LBB0_4> 154 20: b7 05 00 00 06 00 00 00 r5 = 6 155 00000000000000a8 <LBB0_4>: 156 21: b7 02 00 00 00 00 00 00 r2 = 0 157 22: b7 01 00 00 00 00 00 00 r1 = 0 158 ; for (i = 0; (i < VIRTIO_MAX_SGS) && (i < num); i++) { 159 23: 7b 1a e0 ff 00 00 00 00 *(u64 *)(r10 - 32) = r1 160 24: 7b 5a c0 ff 00 00 00 00 *(u64 *)(r10 - 64) = r5 161 162Note that insn #15 has w1 = w5 and w1 is refined later but 163r5(w5) is eventually saved on stack at insn #24 for later use. 164This cause later verifier failure. The bug has been `fixed`__ in 165Clang 13. 166 167__ https://reviews.llvm.org/D97479 168 169BPF CO-RE-based tests and Clang version 170======================================= 171 172A set of selftests use BPF target-specific built-ins, which might require 173bleeding-edge Clang versions (Clang 12 nightly at this time). 174 175Few sub-tests of core_reloc test suit (part of test_progs test runner) require 176the following built-ins, listed with corresponding Clang diffs introducing 177them to Clang/LLVM. These sub-tests are going to be skipped if Clang is too 178old to support them, they shouldn't cause build failures or runtime test 179failures: 180 181- __builtin_btf_type_id() [0_, 1_, 2_]; 182- __builtin_preserve_type_info(), __builtin_preserve_enum_value() [3_, 4_]. 183 184.. _0: https://reviews.llvm.org/D74572 185.. _1: https://reviews.llvm.org/D74668 186.. _2: https://reviews.llvm.org/D85174 187.. _3: https://reviews.llvm.org/D83878 188.. _4: https://reviews.llvm.org/D83242 189 190Floating-point tests and Clang version 191====================================== 192 193Certain selftests, e.g. core_reloc, require support for the floating-point 194types, which was introduced in `Clang 13`__. The older Clang versions will 195either crash when compiling these tests, or generate an incorrect BTF. 196 197__ https://reviews.llvm.org/D83289 198 199Kernel function call test and Clang version 200=========================================== 201 202Some selftests (e.g. kfunc_call and bpf_tcp_ca) require a LLVM support 203to generate extern function in BTF. It was introduced in `Clang 13`__. 204 205Without it, the error from compiling bpf selftests looks like: 206 207.. code-block:: console 208 209 libbpf: failed to find BTF for extern 'tcp_slow_start' [25] section: -2 210 211__ https://reviews.llvm.org/D93563 212 213btf_tag test and Clang version 214============================== 215 216The btf_tag selftest requires LLVM support to recognize the btf_decl_tag and 217btf_type_tag attributes. They are introduced in `Clang 14` [0_, 1_]. 218The subtests ``btf_type_tag_user_{mod1, mod2, vmlinux}`` also requires 219pahole version ``1.23``. 220 221Without them, the btf_tag selftest will be skipped and you will observe: 222 223.. code-block:: console 224 225 #<test_num> btf_tag:SKIP 226 227.. _0: https://reviews.llvm.org/D111588 228.. _1: https://reviews.llvm.org/D111199 229 230Clang dependencies for static linking tests 231=========================================== 232 233linked_vars, linked_maps, and linked_funcs tests depend on `Clang fix`__ to 234generate valid BTF information for weak variables. Please make sure you use 235Clang that contains the fix. 236 237__ https://reviews.llvm.org/D100362 238 239Clang relocation changes 240======================== 241 242Clang 13 patch `clang reloc patch`_ made some changes on relocations such 243that existing relocation types are broken into more types and 244each new type corresponds to only one way to resolve relocation. 245See `kernel llvm reloc`_ for more explanation and some examples. 246Using clang 13 to compile old libbpf which has static linker support, 247there will be a compilation failure:: 248 249 libbpf: ELF relo #0 in section #6 has unexpected type 2 in .../bpf_tcp_nogpl.bpf.o 250 251Here, ``type 2`` refers to new relocation type ``R_BPF_64_ABS64``. 252To fix this issue, user newer libbpf. 253 254.. Links 255.. _clang reloc patch: https://reviews.llvm.org/D102712 256.. _kernel llvm reloc: /Documentation/bpf/llvm_reloc.rst 257 258Clang dependencies for the u32 spill test (xdpwall) 259=================================================== 260The xdpwall selftest requires a change in `Clang 14`__. 261 262Without it, the xdpwall selftest will fail and the error message 263from running test_progs will look like: 264 265.. code-block:: console 266 267 test_xdpwall:FAIL:Does LLVM have https://reviews.llvm.org/D109073? unexpected error: -4007 268 269__ https://reviews.llvm.org/D109073 270