1. Mar 08, 2023
  2. Mar 07, 2023
    • Jakub Kicinski's avatar
      Merge tag 'for-netdev' of https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next · 36e5e391
      Jakub Kicinski authored
      Daniel Borkmann says:
      
      ====================
      pull-request: bpf-next 2023-03-06
      
      We've added 85 non-merge commits during the last 13 day(s) which contain
      a total of 131 files changed, 7102 insertions(+), 1792 deletions(-).
      
      The main changes are:
      
      1) Add skb and XDP typed dynptrs which allow BPF programs for more
         ergonomic and less brittle iteration through data and variable-sized
         accesses, from Joanne Koong.
      
      2) Bigger batch of BPF verifier improvements to prepare for upcoming BPF
         open-coded iterators allowing for less restrictive looping capabilities,
         from Andrii Nakryiko.
      
      3) Rework RCU enforcement in the verifier, add kptr_rcu and enforce BPF
         programs to NULL-check before passing such pointers into kfunc,
         from Alexei Starovoitov.
      
      4) Add support for kptrs in percpu hashmaps, percpu LRU hashmaps and in
         local storage maps, from Kumar Kartikeya Dwivedi.
      
      5) Add BPF verifier support for ST instructions in convert_ctx_access()
         which will help new -mcpu=v4 clang flag to start emitting them,
         from Eduard Zingerman.
      
      6) Make uprobe attachment Android APK aware by supporting attachment
         to functions inside ELF objects contained in APKs via function names,
         from Daniel Müller.
      
      7) Add a new flag BPF_F_TIMER_ABS flag for bpf_timer_start() helper
         to start the timer with absolute expiration value instead of relative
         one, from Tero Kristo.
      
      8) Add a new kfunc bpf_cgroup_from_id() to look up cgroups via id,
         from Tejun Heo.
      
      9) Extend libbpf to support users manually attaching kprobes/uprobes
         in the legacy/perf/link mode, from Menglong Dong.
      
      10) Implement workarounds in the mips BPF JIT for DADDI/R4000,
         from Jiaxun Yang.
      
      11) Enable mixing bpf2bpf and tailcalls for the loongarch BPF JIT,
          from Hengqi Chen.
      
      12) Extend BPF instruction set doc with describing the encoding of BPF
          instructions in terms of how bytes are stored under big/little endian,
          from Jose E. Marchesi.
      
      13) Follow-up to enable kfunc support for riscv BPF JIT, from Pu Lehui.
      
      14) Fix bpf_xdp_query() backwards compatibility on old kernels,
          from Yonghong Song.
      
      15) Fix BPF selftest cross compilation with CLANG_CROSS_FLAGS,
          from Florent Revest.
      
      16) Improve bpf_cpumask_ma to only allocate one bpf_mem_cache,
          from Hou Tao.
      
      17) Fix BPF verifier's check_subprogs to not unnecessarily mark
          a subprogram with has_tail_call, from Ilya Leoshkevich.
      
      18) Fix arm syscall regs spec in libbpf's bpf_tracing.h, from Puranjay Mohan.
      
      * tag 'for-netdev' of https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next: (85 commits)
        selftests/bpf: Add test for legacy/perf kprobe/uprobe attach mode
        selftests/bpf: Split test_attach_probe into multi subtests
        libbpf: Add support to set kprobe/uprobe attach mode
        tools/resolve_btfids: Add /libsubcmd to .gitignore
        bpf: add support for fixed-size memory pointer returns for kfuncs
        bpf: generalize dynptr_get_spi to be usable for iters
        bpf: mark PTR_TO_MEM as non-null register type
        bpf: move kfunc_call_arg_meta higher in the file
        bpf: ensure that r0 is marked scratched after any function call
        bpf: fix visit_insn()'s detection of BPF_FUNC_timer_set_callback helper
        bpf: clean up visit_insn()'s instruction processing
        selftests/bpf: adjust log_fixup's buffer size for proper truncation
        bpf: honor env->test_state_freq flag in is_state_visited()
        selftests/bpf: enhance align selftest's expected log matching
        bpf: improve regsafe() checks for PTR_TO_{MEM,BUF,TP_BUFFER}
        bpf: improve stack slot state printing
        selftests/bpf: Disassembler tests for verifier.c:convert_ctx_access()
        selftests/bpf: test if pointer type is tracked for BPF_ST_MEM
        bpf: allow ctx writes using BPF_ST_MEM instruction
        bpf: Use separate RCU callbacks for freeing selem
        ...
      ====================
      
      Link: https://lore.kernel.org/r/20230307004346.27578-1-daniel@iogearbox.net
      
      
      Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
      36e5e391
    • Andrii Nakryiko's avatar
      Merge branch 'libbpf: allow users to set kprobe/uprobe attach mode' · 8f4c92f0
      Andrii Nakryiko authored
      
      
      Menglong Dong says:
      
      ====================
      
      From: Menglong Dong <imagedong@tencent.com>
      
      By default, libbpf will attach the kprobe/uprobe BPF program in the
      latest mode that supported by kernel. In this series, we add the support
      to let users manually attach kprobe/uprobe in legacy/perf/link mode in
      the 1th patch.
      
      And in the 2th patch, we split the testing 'attach_probe' into multi
      subtests, as Andrii suggested.
      
      In the 3th patch, we add the testings for loading kprobe/uprobe in
      different mode.
      
      Changes since v3:
      - rename eBPF to BPF in the doc
      - use OPTS_GET() to get the value of 'force_ioctl_attach'
      - error out on attach mode is not supported
      - use test_attach_probe_manual__open_and_load() directly
      
      Changes since v2:
      - fix the typo in the 2th patch
      
      Changes since v1:
      - some small changes in the 1th patch, as Andrii suggested
      - split 'attach_probe' into multi subtests
      ====================
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      8f4c92f0
    • Menglong Dong's avatar
      selftests/bpf: Add test for legacy/perf kprobe/uprobe attach mode · c7aec81b
      Menglong Dong authored
      
      
      Add the testing for kprobe/uprobe attaching in default, legacy, perf and
      link mode. And the testing passed:
      
      ./test_progs -t attach_probe
      $5/1     attach_probe/manual-default:OK
      $5/2     attach_probe/manual-legacy:OK
      $5/3     attach_probe/manual-perf:OK
      $5/4     attach_probe/manual-link:OK
      $5/5     attach_probe/auto:OK
      $5/6     attach_probe/kprobe-sleepable:OK
      $5/7     attach_probe/uprobe-lib:OK
      $5/8     attach_probe/uprobe-sleepable:OK
      $5/9     attach_probe/uprobe-ref_ctr:OK
      $5       attach_probe:OK
      Summary: 1/9 PASSED, 0 SKIPPED, 0 FAILED
      
      Signed-off-by: default avatarMenglong Dong <imagedong@tencent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Reviewed-by: default avatarBiao Jiang <benbjiang@tencent.com>
      Link: https://lore.kernel.org/bpf/20230306064833.7932-4-imagedong@tencent.com
      c7aec81b
    • Menglong Dong's avatar
      selftests/bpf: Split test_attach_probe into multi subtests · 7391ec63
      Menglong Dong authored
      
      
      In order to adapt to the older kernel, now we split the "attach_probe"
      testing into multi subtests:
      
        manual // manual attach tests for kprobe/uprobe
        auto // auto-attach tests for kprobe and uprobe
        kprobe-sleepable // kprobe sleepable test
        uprobe-lib // uprobe tests for library function by name
        uprobe-sleepable // uprobe sleepable test
        uprobe-ref_ctr // uprobe ref_ctr test
      
      As sleepable kprobe needs to set BPF_F_SLEEPABLE flag before loading,
      we need to move it to a stand alone skel file, in case of it is not
      supported by kernel and make the whole loading fail.
      
      Therefore, we can only enable part of the subtests for older kernel.
      
      Signed-off-by: default avatarMenglong Dong <imagedong@tencent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Reviewed-by: default avatarBiao Jiang <benbjiang@tencent.com>
      Link: https://lore.kernel.org/bpf/20230306064833.7932-3-imagedong@tencent.com
      7391ec63
    • Menglong Dong's avatar
      libbpf: Add support to set kprobe/uprobe attach mode · f8b299bc
      Menglong Dong authored
      
      
      By default, libbpf will attach the kprobe/uprobe BPF program in the
      latest mode that supported by kernel. In this patch, we add the support
      to let users manually attach kprobe/uprobe in legacy or perf mode.
      
      There are 3 mode that supported by the kernel to attach kprobe/uprobe:
      
        LEGACY: create perf event in legacy way and don't use bpf_link
        PERF: create perf event with perf_event_open() and don't use bpf_link
      
      Signed-off-by: default avatarMenglong Dong <imagedong@tencent.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Reviewed-by: default avatarBiao Jiang <benbjiang@tencent.com>
      Link: create perf event with perf_event_open() and use bpf_link
      Link: https://lore.kernel.org/bpf/20230113093427.1666466-1-imagedong@tencent.com/
      Link: https://lore.kernel.org/bpf/20230306064833.7932-2-imagedong@tencent.com
      
      Users now can manually choose the mode with
      bpf_program__attach_uprobe_opts()/bpf_program__attach_kprobe_opts().
      f8b299bc
  3. Mar 06, 2023
  4. Mar 05, 2023
  5. Mar 04, 2023
    • Alexei Starovoitov's avatar
      Merge branch 'bpf: allow ctx writes using BPF_ST_MEM instruction' · 2564a031
      Alexei Starovoitov authored
      Eduard Zingerman says:
      
      ====================
      
      Changes v1 -> v2, suggested by Alexei:
      - Resolved conflict with recent commit:
        6fcd486b ("bpf: Refactor RCU enforcement in the verifier");
      - Variable `ctx_access` removed in function `convert_ctx_accesses()`;
      - Macro `BPF_COPY_STORE` renamed to `BPF_EMIT_STORE` and fixed to
        correctly extract original store instruction class from code.
      
      Original message follows:
      
      The function verifier.c:convert_ctx_access() applies some rewrites to BPF
      instructions that read from or write to the BPF program context.
      For example, the write instruction for the `struct bpf_sockopt::retval`
      field:
      
          *(u32 *)(r1 + offsetof(struct bpf_sockopt, retval)) = r2
      
      Is transformed to:
      
          *(u64 *)(r1 + offsetof(struct bpf_sockopt_kern, tmp_reg)) = r9
          r9 = *(u64 *)(r1 + offsetof(struct bpf_sockopt_kern, current_task))
          r9 = *(u64 *)(r9 + offsetof(struct task_struct, bpf_ctx))
          *(u32 *)(r9 + offsetof(struct bpf_cg_run_ctx, retval)) = r2
          r9 = *(u64 *)(r1 + offsetof(struct bpf_sockopt_kern, tmp_reg))
      
      Currently, the verifier only supports such transformations for LDX
      (memory-to-register read) and STX (register-to-memory write) instructions.
      Error is reported for ST instructions (immediate-to-memory write).
      This is fine because clang does not currently emit ST instructions.
      
      However, new `-mcpu=v4` clang flag is planned, which would allow to emit
      ST instructions (discussed in [1]).
      
      This patch-set adjusts the verifier to support ST instructions in
      `verifier.c:convert_ctx_access()`.
      
      The patches #1 and #2 were previously shared as part of RFC [2]. The
      changes compared to that RFC are:
      - In patch #1, a bug in the handling of the
        `struct __sk_buff::queue_mapping` field was fixed.
      - Patch #3 is added, which is a set of disassembler-based test cases for
        context access rewrites. The test cases cover all fields for which the
        handling code is modified in patch #1.
      
      [1] Propose some new instructions for -mcpu=v4
          https://lore.kernel.org/bpf/4bfe98be-5333-1c7e-2f6d-42486c8ec039@meta.com/
      [2] RFC Support for BPF_ST instruction in LLVM C compiler
          https://lore.kernel.org/bpf/20221231163122.1360813-1-eddyz87@gmail.com/
      [3] v1
          https://lore.kernel.org/bpf/20230302225507.3413720-1-eddyz87@gmail.com/
      
      
      ====================
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      2564a031
    • Eduard Zingerman's avatar
      selftests/bpf: Disassembler tests for verifier.c:convert_ctx_access() · 71cf4d02
      Eduard Zingerman authored
      
      
      Function verifier.c:convert_ctx_access() applies some rewrites to BPF
      instructions that read or write BPF program context. This commit adds
      machinery to allow test cases that inspect BPF program after these
      rewrites are applied.
      
      An example of a test case:
      
        {
              // Shorthand for field offset and size specification
      	N(CGROUP_SOCKOPT, struct bpf_sockopt, retval),
      
              // Pattern generated for field read
      	.read  = "$dst = *(u64 *)($ctx + bpf_sockopt_kern::current_task);"
      		 "$dst = *(u64 *)($dst + task_struct::bpf_ctx);"
      		 "$dst = *(u32 *)($dst + bpf_cg_run_ctx::retval);",
      
              // Pattern generated for field write
      	.write = "*(u64 *)($ctx + bpf_sockopt_kern::tmp_reg) = r9;"
      		 "r9 = *(u64 *)($ctx + bpf_sockopt_kern::current_task);"
      		 "r9 = *(u64 *)(r9 + task_struct::bpf_ctx);"
      		 "*(u32 *)(r9 + bpf_cg_run_ctx::retval) = $src;"
      		 "r9 = *(u64 *)($ctx + bpf_sockopt_kern::tmp_reg);" ,
        },
      
      For each test case, up to three programs are created:
      - One that uses BPF_LDX_MEM to read the context field.
      - One that uses BPF_STX_MEM to write to the context field.
      - One that uses BPF_ST_MEM to write to the context field.
      
      The disassembly of each program is compared with the pattern specified
      in the test case.
      
      Kernel code for disassembly is reused (as is in the bpftool).
      To keep Makefile changes to the minimum, symbolic links to
      `kernel/bpf/disasm.c` and `kernel/bpf/disasm.h ` are added.
      
      Signed-off-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Link: https://lore.kernel.org/r/20230304011247.566040-4-eddyz87@gmail.com
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      71cf4d02
    • Eduard Zingerman's avatar
      selftests/bpf: test if pointer type is tracked for BPF_ST_MEM · 806f81cd
      Eduard Zingerman authored
      
      
      Check that verifier tracks pointer types for BPF_ST_MEM instructions
      and reports error if pointer types do not match for different
      execution branches.
      
      Signed-off-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Link: https://lore.kernel.org/r/20230304011247.566040-3-eddyz87@gmail.com
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      806f81cd
    • Eduard Zingerman's avatar
      bpf: allow ctx writes using BPF_ST_MEM instruction · 0d80a619
      Eduard Zingerman authored
      
      
      Lift verifier restriction to use BPF_ST_MEM instructions to write to
      context data structures. This requires the following changes:
       - verifier.c:do_check() for BPF_ST updated to:
         - no longer forbid writes to registers of type PTR_TO_CTX;
         - track dst_reg type in the env->insn_aux_data[...].ptr_type field
           (same way it is done for BPF_STX and BPF_LDX instructions).
       - verifier.c:convert_ctx_access() and various callbacks invoked by
         it are updated to handled BPF_ST instruction alongside BPF_STX.
      
      Signed-off-by: default avatarEduard Zingerman <eddyz87@gmail.com>
      Link: https://lore.kernel.org/r/20230304011247.566040-2-eddyz87@gmail.com
      
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      0d80a619