Skip to content

Allow struct_ops to create map id to

Amery Hung bpf netdev 1 version Applied

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

Get series with b4
b4 am [email protected]

Versions newest first

  1. v2 Latest3 patches 2 repliesApplied 3/3

Changes in v2 from the cover letter

Add bpf_struct_ops_id() instead of using bpf_struct_ops_get()

Patches in v2 All 3 applied

#Subject and filesSizeRepliesApplied
1/3bpf: Allow struct_ops to get map id by kdatainclude/linux/bpf.h kernel/bpf/bpf_struct_ops.c+13 -00 repliesApplied
2/3selftests/bpf: Add multi_st_ops that supports multiple instancestools/testing/selftests/bpf/test_kmods/bpf_testmod.c tools/testing/selftests/bpf/test_kmods/bpf_testmod.h tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h+119 -01 replyApplied
3/3selftests/bpf: Test multi_st_ops and calling kfuncs from different programstools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping1.c tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping2.c+195 -01 replyApplied

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH bpf-next v2 0/3] Allow struct_ops to create map id to

v1 -> v2
Add bpf_struct_ops_id() instead of using bpf_struct_ops_get()

Hi,

This patchset allows struct_ops implementors to get map id from kdata in reg(), unreg() and update() so that they create an id to struct_ops instance mapping. This in turn allows struct_ops kfuncs to refer to the calling instance without passing a pointer to the struct_ops. The selftest provides an end-to-end example.

Some struct_ops users extend themselves with other bpf programs, which also need to call struct_ops kfuncs. For example, scx_layered uses syscall bpf programs as a scx_layered specific control plane and uses tracing programs to get additional information for scheduling [0]. The kfuncs may need to refer to the struct_ops instance and perform jobs accordingly. To allow calling struct_ops kfuncs referring to specific instances from different program types and context (e.g., struct_ops, tracing, async callbacks), the traditional way is to pass the struct_ops pointer to kfuncs.

This patchset provides an alternative way, through a combination of bpf map id and global variable. First, a struct_ops implementor will use the map id of the struct_ops map as the id of an instance. Then, it needs to maintain an id to instance mapping: inserting a new mapping during reg() and removing it during unreg(). The map id can be acquired by calling bpf_struct_ops_id().

[0] https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/src/bpf/main.bpf.c

Amery Hung (3):
bpf: Allow struct_ops to get map id by kdata selftests/bpf: Add multi_st_ops that supports multiple instances selftests/bpf: Test multi_st_ops and calling kfuncs from different
programs

 include/linux/bpf.h                           |   1 +
 kernel/bpf/bpf_struct_ops.c                   |  12 ++
 .../test_struct_ops_id_ops_mapping.c          |  77 +++++++++++++
 .../bpf/progs/struct_ops_id_ops_mapping1.c    |  59 ++++++++++
 .../bpf/progs/struct_ops_id_ops_mapping2.c    |  59 ++++++++++
 .../selftests/bpf/test_kmods/bpf_testmod.c    | 109 ++++++++++++++++++
 .../selftests/bpf/test_kmods/bpf_testmod.h    |   8 ++
 .../bpf/test_kmods/bpf_testmod_kfunc.h        |   2 +
 8 files changed, 327 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_id_ops_mapping.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping1.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_id_ops_mapping2.c
Signature · 1 line
2.47.3