1. 05 Sep, 2024 19 commits
  2. 04 Sep, 2024 9 commits
    • Yonghong Song's avatar
      selftests/bpf: Add a selftest for x86 jit convergence issues · eff5b5ff
      Yonghong Song authored
      The core part of the selftest, i.e., the je <-> jmp cycle, mimics the
      original sched-ext bpf program. The test will fail without the
      previous patch.
      
      I tried to create some cases for other potential cycles
      (je <-> je, jmp <-> je and jmp <-> jmp) with similar pattern
      to the test in this patch, but failed. So this patch
      only contains one test for je <-> jmp cycle.
      Signed-off-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/r/20240904221256.37389-1-yonghong.song@linux.devSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      eff5b5ff
    • Yonghong Song's avatar
      bpf, x64: Fix a jit convergence issue · c8831bdb
      Yonghong Song authored
      Daniel Hodges reported a jit error when playing with a sched-ext program.
      The error message is:
        unexpected jmp_cond padding: -4 bytes
      
      But further investigation shows the error is actual due to failed
      convergence. The following are some analysis:
      
        ...
        pass4, final_proglen=4391:
          ...
          20e:    48 85 ff                test   rdi,rdi
          211:    74 7d                   je     0x290
          213:    48 8b 77 00             mov    rsi,QWORD PTR [rdi+0x0]
          ...
          289:    48 85 ff                test   rdi,rdi
          28c:    74 17                   je     0x2a5
          28e:    e9 7f ff ff ff          jmp    0x212
          293:    bf 03 00 00 00          mov    edi,0x3
      
      Note that insn at 0x211 is 2-byte cond jump insn for offset 0x7d (-125)
      and insn at 0x28e is 5-byte jmp insn with offset -129.
      
        pass5, final_proglen=4392:
          ...
          20e:    48 85 ff                test   rdi,rdi
          211:    0f 84 80 00 00 00       je     0x297
          217:    48 8b 77 00             mov    rsi,QWORD PTR [rdi+0x0]
          ...
          28d:    48 85 ff                test   rdi,rdi
          290:    74 1a                   je     0x2ac
          292:    eb 84                   jmp    0x218
          294:    bf 03 00 00 00          mov    edi,0x3
      
      Note that insn at 0x211 is 6-byte cond jump insn now since its offset
      becomes 0x80 based on previous round (0x293 - 0x213 = 0x80). At the same
      time, insn at 0x292 is a 2-byte insn since its offset is -124.
      
      pass6 will repeat the same code as in pass4. pass7 will repeat the same
      code as in pass5, and so on. This will prevent eventual convergence.
      
      Passes 1-14 are with padding = 0. At pass15, padding is 1 and related
      insn looks like:
      
          211:    0f 84 80 00 00 00       je     0x297
          217:    48 8b 77 00             mov    rsi,QWORD PTR [rdi+0x0]
          ...
          24d:    48 85 d2                test   rdx,rdx
      
      The similar code in pass14:
          211:    74 7d                   je     0x290
          213:    48 8b 77 00             mov    rsi,QWORD PTR [rdi+0x0]
          ...
          249:    48 85 d2                test   rdx,rdx
          24c:    74 21                   je     0x26f
          24e:    48 01 f7                add    rdi,rsi
          ...
      
      Before generating the following insn,
        250:    74 21                   je     0x273
      "padding = 1" enables some checking to ensure nops is either 0 or 4
      where
        #define INSN_SZ_DIFF (((addrs[i] - addrs[i - 1]) - (prog - temp)))
        nops = INSN_SZ_DIFF - 2
      
      In this specific case,
        addrs[i] = 0x24e // from pass14
        addrs[i-1] = 0x24d // from pass15
        prog - temp = 3 // from 'test rdx,rdx' in pass15
      so
        nops = -4
      and this triggers the failure.
      
      To fix the issue, we need to break cycles of je <-> jmp. For example,
      in the above case, we have
        211:    74 7d                   je     0x290
      the offset is 0x7d. If 2-byte je insn is generated only if
      the offset is less than 0x7d (<= 0x7c), the cycle can be
      break and we can achieve the convergence.
      
      I did some study on other cases like je <-> je, jmp <-> je and
      jmp <-> jmp which may cause cycles. Those cases are not from actual
      reproducible cases since it is pretty hard to construct a test case
      for them. the results show that the offset <= 0x7b (0x7b = 123) should
      be enough to cover all cases. This patch added a new helper to generate 8-bit
      cond/uncond jmp insns only if the offset range is [-128, 123].
      Reported-by: default avatarDaniel Hodges <hodgesd@meta.com>
      Signed-off-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/r/20240904221251.37109-1-yonghong.song@linux.devSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      c8831bdb
    • Feng Yang's avatar
      selftests: bpf: Replace sizeof(arr)/sizeof(arr[0]) with ARRAY_SIZE · 23457b37
      Feng Yang authored
      The ARRAY_SIZE macro is more compact and more formal in linux source.
      Signed-off-by: default avatarFeng Yang <yangfeng@kylinos.cn>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20240903072559.292607-1-yangfeng59949@163.com
      23457b37
    • Alexei Starovoitov's avatar
      Merge branch 'bpf-follow-up-on-gen_epilogue' · 6fee7a7e
      Alexei Starovoitov authored
      Martin KaFai Lau says:
      
      ====================
      bpf: Follow up on gen_epilogue
      
      From: Martin KaFai Lau <martin.lau@kernel.org>
      
      The set addresses some follow ups on the earlier gen_epilogue
      patch set.
      ====================
      Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Link: https://lore.kernel.org/r/20240904180847.56947-1-martin.lau@linux.devSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      6fee7a7e
    • Martin KaFai Lau's avatar
      bpf: Fix indentation issue in epilogue_idx · 00750788
      Martin KaFai Lau authored
      There is a report on new indentation issue in epilogue_idx.
      This patch fixed it.
      
      Fixes: 169c3176 ("bpf: Add gen_epilogue to bpf_verifier_ops")
      Reported-by: default avatarkernel test robot <lkp@intel.com>
      Closes: https://lore.kernel.org/oe-kbuild-all/202408311622.4GzlzN33-lkp@intel.com/Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      Link: https://lore.kernel.org/r/20240904180847.56947-3-martin.lau@linux.devSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      00750788
    • Martin KaFai Lau's avatar
      bpf: Remove the insn_buf array stack usage from the inline_bpf_loop() · 940ce73b
      Martin KaFai Lau authored
      This patch removes the insn_buf array stack usage from the
      inline_bpf_loop(). Instead, the env->insn_buf is used. The
      usage in inline_bpf_loop() needs more than 16 insn, so the
      INSN_BUF_SIZE needs to be increased from 16 to 32.
      The compiler stack size warning on the verifier is gone
      after this change.
      
      Cc: Eduard Zingerman <eddyz87@gmail.com>
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      Link: https://lore.kernel.org/r/20240904180847.56947-2-martin.lau@linux.devSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      940ce73b
    • Rong Tao's avatar
      samples/bpf: Remove sample tracex2 · 46f4ea04
      Rong Tao authored
      In commit ba8de796 ("net: introduce sk_skb_reason_drop function")
      kfree_skb_reason() becomes an inline function and cannot be traced.
      
      samples/bpf is abandonware by now, and we should slowly but surely
      convert whatever makes sense into BPF selftests under
      tools/testing/selftests/bpf and just get rid of the rest.
      
      Link: https://github.com/torvalds/linux/commit/ba8de796baf4bdc03530774fb284fe3c97875566Signed-off-by: default avatarRong Tao <rongtao@cestc.cn>
      Link: https://lore.kernel.org/r/tencent_30ADAC88CB2915CA57E9512D4460035BA107@qq.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      46f4ea04
    • Yuan Chen's avatar
      selftests/bpf: Fix procmap_query()'s params mismatch and compilation warning · 02baa0a2
      Yuan Chen authored
      When the PROCMAP_QUERY is not defined, a compilation error occurs due to the
      mismatch of the procmap_query()'s params, procmap_query() only be called in
      the file where the function is defined, modify the params so they can match.
      
      We get a warning when build samples/bpf:
          trace_helpers.c:252:5: warning: no previous prototype for ‘procmap_query’ [-Wmissing-prototypes]
            252 | int procmap_query(int fd, const void *addr, __u32 query_flags, size_t *start, size_t *offset, int *flags)
                |     ^~~~~~~~~~~~~
      As this function is only used in the file, mark it as 'static'.
      
      Fixes: 4e9e0760 ("selftests/bpf: make use of PROCMAP_QUERY ioctl if available")
      Signed-off-by: default avatarYuan Chen <chenyuan@kylinos.cn>
      Link: https://lore.kernel.org/r/20240903012839.3178-1-chenyuan_fl@163.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      02baa0a2
    • Xu Kuohai's avatar
      bpf, arm64: Jit BPF_CALL to direct call when possible · ddbe9ec5
      Xu Kuohai authored
      Currently, BPF_CALL is always jited to indirect call. When target is
      within the range of direct call, BPF_CALL can be jited to direct call.
      
      For example, the following BPF_CALL
      
          call __htab_map_lookup_elem
      
      is always jited to indirect call:
      
          mov     x10, #0xffffffffffff18f4
          movk    x10, #0x821, lsl #16
          movk    x10, #0x8000, lsl #32
          blr     x10
      
      When the address of target __htab_map_lookup_elem is within the range of
      direct call, the BPF_CALL can be jited to:
      
          bl      0xfffffffffd33bc98
      
      This patch does such jit optimization by emitting arm64 direct calls for
      BPF_CALL when possible, indirect calls otherwise.
      
      Without this patch, the jit works as follows.
      
      1. First pass
         A. Determine jited position and size for each bpf instruction.
         B. Computed the jited image size.
      
      2. Allocate jited image with size computed in step 1.
      
      3. Second pass
         A. Adjust jump offset for jump instructions
         B. Write the final image.
      
      This works because, for a given bpf prog, regardless of where the jited
      image is allocated, the jited result for each instruction is fixed. The
      second pass differs from the first only in adjusting the jump offsets,
      like changing "jmp imm1" to "jmp imm2", while the position and size of
      the "jmp" instruction remain unchanged.
      
      Now considering whether to jit BPF_CALL to arm64 direct or indirect call
      instruction. The choice depends solely on the jump offset: direct call
      if the jump offset is within 128MB, indirect call otherwise.
      
      For a given BPF_CALL, the target address is known, so the jump offset is
      decided by the jited address of the BPF_CALL instruction. In other words,
      for a given bpf prog, the jited result for each BPF_CALL is determined
      by its jited address.
      
      The jited address for a BPF_CALL is the jited image address plus the
      total jited size of all preceding instructions. For a given bpf prog,
      there are clearly no BPF_CALL instructions before the first BPF_CALL
      instruction. Since the jited result for all other instructions other
      than BPF_CALL are fixed, the total jited size preceding the first
      BPF_CALL is also fixed. Therefore, once the jited image is allocated,
      the jited address for the first BPF_CALL is fixed.
      
      Now that the jited result for the first BPF_CALL is fixed, the jited
      results for all instructions preceding the second BPF_CALL are fixed.
      So the jited address and result for the second BPF_CALL are also fixed.
      
      Similarly, we can conclude that the jited addresses and results for all
      subsequent BPF_CALL instructions are fixed.
      
      This means that, for a given bpf prog, once the jited image is allocated,
      the jited address and result for all instructions, including all BPF_CALL
      instructions, are fixed.
      
      Based on the observation, with this patch, the jit works as follows.
      
      1. First pass
         Estimate the maximum jited image size. In this pass, all BPF_CALLs
         are jited to arm64 indirect calls since the jump offsets are unknown
         because the jited image is not allocated.
      
      2. Allocate jited image with size estimated in step 1.
      
      3. Second pass
         A. Determine the jited result for each BPF_CALL.
         B. Determine jited address and size for each bpf instruction.
      
      4. Third pass
         A. Adjust jump offset for jump instructions.
         B. Write the final image.
      Signed-off-by: default avatarXu Kuohai <xukuohai@huawei.com>
      Reviewed-by: default avatarPuranjay Mohan <puranjay@kernel.org>
      Link: https://lore.kernel.org/r/20240903094407.601107-1-xukuohai@huaweicloud.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      ddbe9ec5
  3. 02 Sep, 2024 2 commits
  4. 30 Aug, 2024 10 commits