mbox series

[RFC,bpf-next,00/14] instruction sets and static keys

Message ID 20250318143318.656785-1-aspsk@isovalent.com (mailing list archive)
Headers show
Series instruction sets and static keys | expand

Message

Anton Protopopov March 18, 2025, 2:33 p.m. UTC
This patchset implements new type of map, instruction set, and uses
it to build support for BPF static keys. The same map will be later
used to provide support for indirect jumps and indirect calls. See
[1], [2] for more context.

Short table of contents:

  * patches 1, 9, 10, 11 are simple fixes (which can be sent
    independently, if acked)

  * patches 2, 3 add a new map type, BPF_MAP_TYPE_INSN_SET, and
    corresponding selftests. This map is used to track how original
    instructions were relocated into 'xlated' during the verification

  * patches 4, 5, 6, 7, 8 add support for static keys (kernel only)
    using (an extension) to that new map type. Only x86 support is
    added in this RFC

  * patches 12, 13, 14 add libbpf-side support for static keys and
    selftests

It is RFC for a few reasons:

1) The kernel side of the static keys looks clear, however, the
libbpf side is not _that_ clear. I thought that this is better to
commit to a particular userspace design, as any particular design
requires a lot of changes on the libbpf side. See patch 12 for
the details

2) The libbpf part of the series requires a patched LLVM (see [3]),
which adds support for gotol_or_nop/nop_or_gotol instructions, so
selftests would not compile in CI.

3) Patch 4 adds support for a new BPF instruction. It looks
reasonable to use an extended BPF_JMP|BPF_JA instruction, and not
may_goto. Reasons: a follow up will add support for
BPF_JMP|BPF_JA|BPF_X (indirect jumps), which also utilizes INSN_SET maps (see [2]).
Then another follow up will add support CALL|BPF_X, for which there's
no corresponding magic instruction (to be discussed at the following
LSF/MM/BPF).

Besides these reasons, there are some questions / known bugs,
which will be fixed once the general plan is confirmed:

  * bpf_jit_blind_constants will patch code, which is ignored in this
    RFC series. The solution would be either moving tracking
    instruction sets to bpf_prog from the verifier environment,
    or moving bpf_jit_blind_constants upper the stack (right now,
    this is the first thing which every jit does, so maybe it can
    be actually executed from the verifier, and provide env context)

  * gen-loader not supported, fd_array usage in libbpf should be
    re-designed (see patch 12 for more details)

  * insn_off -> insn_set map mapping should be optimized (now it is
    brute force)

Links:
  1. http://oldvger.kernel.org/bpfconf2024_material/bpf_static_keys.pdf
  2. https://lpc.events/event/18/contributions/1941/
  3. https://github.com/aspsk/llvm-project/tree/static-keys

Anton Protopopov (14):
  bpf: fix a comment describing bpf_attr
  bpf: add new map type: instructions set
  selftests/bpf: add selftests for new insn_set map
  bpf: add support for an extended JA instruction
  bpf: Add kernel/bpftool asm support for new instructions
  bpf: add BPF_STATIC_KEY_UPDATE syscall
  bpf: save the start of functions in bpf_prog_aux
  bpf, x86: implement static key support
  selftests/bpf: add guard macros around likely/unlikely
  libbpf: add likely/unlikely macros
  selftests/bpf: remove likely/unlikely definitions
  libbpf: BPF Static Keys support
  libbpf: Add bpf_static_key_update() API
  selftests/bpf: Add tests for BPF static calls

 arch/x86/net/bpf_jit_comp.c                   |  65 +-
 include/linux/bpf.h                           |  28 +
 include/linux/bpf_types.h                     |   1 +
 include/linux/bpf_verifier.h                  |   2 +
 include/uapi/linux/bpf.h                      |  40 +-
 kernel/bpf/Makefile                           |   2 +-
 kernel/bpf/bpf_insn_set.c                     | 400 +++++++++++
 kernel/bpf/core.c                             |   5 +
 kernel/bpf/disasm.c                           |  33 +-
 kernel/bpf/syscall.c                          |  28 +
 kernel/bpf/verifier.c                         |  94 ++-
 tools/include/uapi/linux/bpf.h                |  40 +-
 tools/lib/bpf/bpf.c                           |  17 +
 tools/lib/bpf/bpf.h                           |  19 +
 tools/lib/bpf/bpf_helpers.h                   |  63 ++
 tools/lib/bpf/libbpf.c                        | 362 +++++++++-
 tools/lib/bpf/libbpf.map                      |   1 +
 tools/lib/bpf/libbpf_internal.h               |   3 +
 tools/lib/bpf/linker.c                        |   6 +-
 .../selftests/bpf/bpf_arena_spin_lock.h       |   3 -
 .../selftests/bpf/prog_tests/bpf_insn_set.c   | 639 ++++++++++++++++++
 .../bpf/prog_tests/bpf_static_keys.c          | 359 ++++++++++
 .../selftests/bpf/progs/bpf_static_keys.c     | 131 ++++
 tools/testing/selftests/bpf/progs/iters.c     |   2 -
 24 files changed, 2315 insertions(+), 28 deletions(-)
 create mode 100644 kernel/bpf/bpf_insn_set.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_insn_set.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_static_keys.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_static_keys.c

Comments

Yonghong Song March 18, 2025, 9 p.m. UTC | #1
On 3/18/25 7:33 AM, Anton Protopopov wrote:
> This patchset implements new type of map, instruction set, and uses
> it to build support for BPF static keys. The same map will be later
> used to provide support for indirect jumps and indirect calls. See
> [1], [2] for more context.
>
> Short table of contents:
>
>    * patches 1, 9, 10, 11 are simple fixes (which can be sent
>      independently, if acked)
>
>    * patches 2, 3 add a new map type, BPF_MAP_TYPE_INSN_SET, and
>      corresponding selftests. This map is used to track how original
>      instructions were relocated into 'xlated' during the verification
>
>    * patches 4, 5, 6, 7, 8 add support for static keys (kernel only)
>      using (an extension) to that new map type. Only x86 support is
>      added in this RFC
>
>    * patches 12, 13, 14 add libbpf-side support for static keys and
>      selftests
>
> It is RFC for a few reasons:
>
> 1) The kernel side of the static keys looks clear, however, the
> libbpf side is not _that_ clear. I thought that this is better to
> commit to a particular userspace design, as any particular design
> requires a lot of changes on the libbpf side. See patch 12 for
> the details
>
> 2) The libbpf part of the series requires a patched LLVM (see [3]),
> which adds support for gotol_or_nop/nop_or_gotol instructions, so
> selftests would not compile in CI.
>
> 3) Patch 4 adds support for a new BPF instruction. It looks
> reasonable to use an extended BPF_JMP|BPF_JA instruction, and not
> may_goto. Reasons: a follow up will add support for
> BPF_JMP|BPF_JA|BPF_X (indirect jumps), which also utilizes INSN_SET maps (see [2]).
> Then another follow up will add support CALL|BPF_X, for which there's
> no corresponding magic instruction (to be discussed at the following
> LSF/MM/BPF).
>
> Besides these reasons, there are some questions / known bugs,
> which will be fixed once the general plan is confirmed:
>
>    * bpf_jit_blind_constants will patch code, which is ignored in this
>      RFC series. The solution would be either moving tracking
>      instruction sets to bpf_prog from the verifier environment,
>      or moving bpf_jit_blind_constants upper the stack (right now,
>      this is the first thing which every jit does, so maybe it can
>      be actually executed from the verifier, and provide env context)
>
>    * gen-loader not supported, fd_array usage in libbpf should be
>      re-designed (see patch 12 for more details)
>
>    * insn_off -> insn_set map mapping should be optimized (now it is
>      brute force)
>
> Links:
>    1. http://oldvger.kernel.org/bpfconf2024_material/bpf_static_keys.pdf
>    2. https://lpc.events/event/18/contributions/1941/
>    3. https://github.com/aspsk/llvm-project/tree/static-keys

For llvm patch in [3], please remove changes in function isValidIdInMiddle()
as gotol_or_nop or nop_or_gotol will not appear in the *middle* of any
instruction. "gotol" should not be there either, I may remove it sometime
later.

>
> Anton Protopopov (14):
>    bpf: fix a comment describing bpf_attr
>    bpf: add new map type: instructions set
>    selftests/bpf: add selftests for new insn_set map
>    bpf: add support for an extended JA instruction
>    bpf: Add kernel/bpftool asm support for new instructions
>    bpf: add BPF_STATIC_KEY_UPDATE syscall
>    bpf: save the start of functions in bpf_prog_aux
>    bpf, x86: implement static key support
>    selftests/bpf: add guard macros around likely/unlikely
>    libbpf: add likely/unlikely macros
>    selftests/bpf: remove likely/unlikely definitions
>    libbpf: BPF Static Keys support
>    libbpf: Add bpf_static_key_update() API
>    selftests/bpf: Add tests for BPF static calls
>
>   arch/x86/net/bpf_jit_comp.c                   |  65 +-
>   include/linux/bpf.h                           |  28 +
>   include/linux/bpf_types.h                     |   1 +
>   include/linux/bpf_verifier.h                  |   2 +
>   include/uapi/linux/bpf.h                      |  40 +-
>   kernel/bpf/Makefile                           |   2 +-
>   kernel/bpf/bpf_insn_set.c                     | 400 +++++++++++
>   kernel/bpf/core.c                             |   5 +
>   kernel/bpf/disasm.c                           |  33 +-
>   kernel/bpf/syscall.c                          |  28 +
>   kernel/bpf/verifier.c                         |  94 ++-
>   tools/include/uapi/linux/bpf.h                |  40 +-
>   tools/lib/bpf/bpf.c                           |  17 +
>   tools/lib/bpf/bpf.h                           |  19 +
>   tools/lib/bpf/bpf_helpers.h                   |  63 ++
>   tools/lib/bpf/libbpf.c                        | 362 +++++++++-
>   tools/lib/bpf/libbpf.map                      |   1 +
>   tools/lib/bpf/libbpf_internal.h               |   3 +
>   tools/lib/bpf/linker.c                        |   6 +-
>   .../selftests/bpf/bpf_arena_spin_lock.h       |   3 -
>   .../selftests/bpf/prog_tests/bpf_insn_set.c   | 639 ++++++++++++++++++
>   .../bpf/prog_tests/bpf_static_keys.c          | 359 ++++++++++
>   .../selftests/bpf/progs/bpf_static_keys.c     | 131 ++++
>   tools/testing/selftests/bpf/progs/iters.c     |   2 -
>   24 files changed, 2315 insertions(+), 28 deletions(-)
>   create mode 100644 kernel/bpf/bpf_insn_set.c
>   create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_insn_set.c
>   create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_static_keys.c
>   create mode 100644 tools/testing/selftests/bpf/progs/bpf_static_keys.c
>