Skip to content

bpf: Support calling kernel function

Martin KaFai Lau bpf netdev 2 versions – Applied

Status from this message: This series was applied to bpf/bpf-next.git (refs/heads/master):

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest14 patches 24 repliesApplied 14/14
  2. v115 patches 32 repliesSuperseded

Changes in v2 from the cover letter

- Patch 2 in v1 is removed. No need to support extern func in kernel. Changed libbpf to adjust the .ksyms datasec for extern func in patch 11. (Andrii)
- Name change: btf_check_func_arg_match() and btf_check_subprog_arg_match() in patch 2. (Andrii)
- Always set unreliable on any error in patch 2 since it does not matter. (Andrii)
- s/kern_func/kfunc/ and s/descriptor/desc/ in this set. (Andrii)
- Remove some unnecessary changes in disasm.h and disasm.c
in patch 3. In particular, no need to change the function signature in bpf_insn_revmap_call_t. Also, removed the changes in print_bpf_insn().
- Fixed an issue in check_kfunc_call() when the calling kernel function returns a pointer in patch 3. Added a selftest.
- Adjusted the verifier selftests due to the changes in the verifier log in patch 3.
- Fixed a comparison issue in kfunc_desc_cmp_by_imm() in patch 3. (Andrii)
- Name change: is_ldimm64_insn(),
new helper: is_call_insn() in patch 10 (Andrii)
- Move btf_func_linkage() from btf.h to libbpf.c in patch 11. (Andrii)
- Fixed the linker error when CONFIG_BPF_SYSCALL is not defined. Moved the check_kfunc_call from filter.c to test_run.c in patch 14. (kernel test robot)

Patches in v2 All 14 applied

#Subject and filesSizeRepliesApplied
1/14bpf: Simplify freeing logic in linfo and jited_linfoinclude/linux/filter.h kernel/bpf/core.c kernel/bpf/syscall.c +1 more+17 -280 repliesApplied
2/14bpf: Refactor btf_check_func_arg_matchinclude/linux/bpf.h include/linux/btf.h kernel/bpf/btf.c +1 more+95 -770 repliesApplied
3/14bpf: Support bpf program calling kernel functionarch/x86/net/bpf_jit_comp.c include/linux/bpf.h include/linux/btf.h +10 more+480 -466 repliesApplied
4/14bpf: Support kernel function call in x86-32arch/x86/net/bpf_jit_comp32.c+198 -00 repliesApplied
5/14tcp: Rename bictcp function prefix to cubictcpnet/ipv4/tcp_cubic.c+12 -120 repliesApplied
6/14bpf: tcp: Put some tcp cong functions in allowlist for bpf-tcp-ccnet/ipv4/bpf_tcp_ca.c+41 -00 repliesApplied
7/14libbpf: Refactor bpf_object__resolve_ksyms_btf_idtools/lib/bpf/libbpf.c+67 -570 repliesApplied
8/14libbpf: Refactor codes for finding btf id of a kernel symboltools/lib/bpf/libbpf.c+33 -110 repliesApplied
9/14libbpf: Rename RELO_EXTERN to RELO_EXTERN_VARtools/lib/bpf/libbpf.c+3 -30 repliesApplied
10/14libbpf: Record extern sym relocation firsttools/lib/bpf/libbpf.c+34 -290 repliesApplied
11/14libbpf: Support extern kernel functiontools/lib/bpf/libbpf.c+162 -120 repliesApplied
12/14bpf: selftests: Rename bictcp to bpf_cubictools/testing/selftests/bpf/progs/bpf_cubic.c+15 -150 repliesApplied
13/14bpf: selftests: bpf_cubic and bpf_dctcp calling kernel functionstools/testing/selftests/bpf/bpf_tcp_helpers.h tools/testing/selftests/bpf/progs/bpf_cubic.c tools/testing/selftests/bpf/progs/bpf_dctcp.c+11 -460 repliesApplied
14/14bpf: selftests: Add kfunc_call testinclude/linux/bpf.h net/bpf/test_run.c net/core/filter.c +3 more+183 -00 repliesApplied

Reviews trailers per patch

7 of 14 patches have a Reviewed-by or Acked-by.

PatchAndrii Nakryiko
1/14
2/14
3/14
4/14
5/14
6/14A-b
7/14A-b
8/14A-b
9/14A-b
10/14A-b
11/14
12/14A-b
13/14A-b
14/14

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 bpf-next 00/14] bpf: Support calling kernel function

This series adds support to allow bpf program calling kernel function.

The use case included in this set is to allow bpf-tcp-cc to directly call some tcp-cc helper functions (e.g. "tcp_cong_avoid_ai()"). Those functions have already been used by some kernel tcp-cc implementations.

This set will also allow the bpf-tcp-cc program to directly call the kernel tcp-cc implementation, For example, a bpf_dctcp may only want to implement its own dctcp_cwnd_event() and reuse other dctcp_*() directly from the kernel tcp_dctcp.c instead of reimplementing (or copy-and-pasting) them.

The tcp-cc kernel functions mentioned above will be white listed for the struct_ops bpf-tcp-cc programs to use in a later patch. The white listed functions are not bounded to a fixed ABI contract. Those functions have already been used by the existing kernel tcp-cc. If any of them has changed, both in-tree and out-of-tree kernel tcp-cc implementations have to be changed. The same goes for the struct_ops bpf-tcp-cc programs which have to be adjusted accordingly.

Please see individual patch for details.

v2:
- Patch 2 in v1 is removed. No need to support extern func in kernel. Changed libbpf to adjust the .ksyms datasec for extern func in patch 11. (Andrii)
- Name change: btf_check_func_arg_match() and btf_check_subprog_arg_match() in patch 2. (Andrii)
- Always set unreliable on any error in patch 2 since it does not matter. (Andrii)
- s/kern_func/kfunc/ and s/descriptor/desc/ in this set. (Andrii)
- Remove some unnecessary changes in disasm.h and disasm.c
in patch 3. In particular, no need to change the function signature in bpf_insn_revmap_call_t. Also, removed the changes in print_bpf_insn().
- Fixed an issue in check_kfunc_call() when the calling kernel function returns a pointer in patch 3. Added a selftest.
- Adjusted the verifier selftests due to the changes in the verifier log in patch 3.
- Fixed a comparison issue in kfunc_desc_cmp_by_imm() in patch 3. (Andrii)
- Name change: is_ldimm64_insn(),
new helper: is_call_insn() in patch 10 (Andrii)
- Move btf_func_linkage() from btf.h to libbpf.c in patch 11. (Andrii)
- Fixed the linker error when CONFIG_BPF_SYSCALL is not defined. Moved the check_kfunc_call from filter.c to test_run.c in patch 14. (kernel test robot)

Martin KaFai Lau (14):
bpf: Simplify freeing logic in linfo and jited_linfo
bpf: Refactor btf_check_func_arg_match
bpf: Support bpf program calling kernel function
bpf: Support kernel function call in x86-32
tcp: Rename bictcp function prefix to cubictcp
bpf: tcp: Put some tcp cong functions in allowlist for bpf-tcp-cc
libbpf: Refactor bpf_object__resolve_ksyms_btf_id
libbpf: Refactor codes for finding btf id of a kernel symbol
libbpf: Rename RELO_EXTERN to RELO_EXTERN_VAR
libbpf: Record extern sym relocation first
libbpf: Support extern kernel function
bpf: selftests: Rename bictcp to bpf_cubic
bpf: selftests: bpf_cubic and bpf_dctcp calling kernel functions
bpf: selftests: Add kfunc_call test

 arch/x86/net/bpf_jit_comp.c                   |   5 +
 arch/x86/net/bpf_jit_comp32.c                 | 198 +++++++++
 include/linux/bpf.h                           |  34 +-
 include/linux/btf.h                           |   6 +
 include/linux/filter.h                        |   4 +-
 include/uapi/linux/bpf.h                      |   4 +
 kernel/bpf/btf.c                              | 218 ++++++----
 kernel/bpf/core.c                             |  47 +--
 kernel/bpf/disasm.c                           |  13 +-
 kernel/bpf/syscall.c                          |   4 +-
 kernel/bpf/verifier.c                         | 376 +++++++++++++++--
 net/bpf/test_run.c                            |  28 ++
 net/core/filter.c                             |   1 +
 net/ipv4/bpf_tcp_ca.c                         |  41 ++
 net/ipv4/tcp_cubic.c                          |  24 +-
 tools/include/uapi/linux/bpf.h                |   4 +
 tools/lib/bpf/libbpf.c                        | 389 +++++++++++++-----
 tools/testing/selftests/bpf/bpf_tcp_helpers.h |  29 +-
 .../selftests/bpf/prog_tests/kfunc_call.c     |  59 +++
 tools/testing/selftests/bpf/progs/bpf_cubic.c |  36 +-
 tools/testing/selftests/bpf/progs/bpf_dctcp.c |  22 +-
 .../selftests/bpf/progs/kfunc_call_test.c     |  47 +++
 .../bpf/progs/kfunc_call_test_subprog.c       |  42 ++
 tools/testing/selftests/bpf/verifier/calls.c  |  12 +-
 .../selftests/bpf/verifier/dead_code.c        |  10 +-
 25 files changed, 1334 insertions(+), 319 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/kfunc_call.c
 create mode 100644 tools/testing/selftests/bpf/progs/kfunc_call_test.c
 create mode 100644 tools/testing/selftests/bpf/progs/kfunc_call_test_subprog.c
Signature · 1 line
2.30.2