Skip to content

bpf: Implement mprog API on top of existing cgroup progs

Yonghong Song bpf 5 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest5 patches 3 repliesIn review 4 of 5 applied
  2. v45 patches 2 repliesSuperseded
  3. v35 patches 4 repliesSuperseded
Earlier versions · 2
VersionPostedPatchesRepliesStatus
v24 patches6 repliesSuperseded
v1 RFC4 patches6 repliesSuperseded

Changes in v5 from the cover letter

- v4: https://lore.kernel.org/bpf/[email protected]/
- Remove early prog/link checking based flags and id_or_fd as later code will do checking as well.
- Do proper cgroup flag checking for bpf_prog_attach().

Patches in v5 4 of 5 applied

#Subject and filesSizeRepliesApplied
1/5cgroup: Add bpf prog revisions to struct cgroup_bpfinclude/linux/bpf-cgroup-defs.h kernel/cgroup/cgroup.c+6 -00 repliesApplied
2/5bpf: Implement mprog API on top of existing cgroup progsinclude/uapi/linux/bpf.h kernel/bpf/cgroup.c kernel/bpf/syscall.c +1 more+209 -373 replies
3/5libbpf: Support link-based cgroup attach with optionstools/lib/bpf/bpf.c tools/lib/bpf/bpf.h tools/lib/bpf/libbpf.c +2 more+93 -00 repliesApplied
4/5selftests/bpf: Move some tc_helpers.h functions to test_progs.htools/testing/selftests/bpf/prog_tests/tc_helpers.h tools/testing/selftests/bpf/test_progs.h+28 -280 repliesApplied
5/5selftests/bpf: Add two selftests for mprog API based cgroup progstools/testing/selftests/bpf/prog_tests/cgroup_mprog_opts.c tools/testing/selftests/bpf/prog_tests/cgroup_mprog_ordering.c tools/testing/selftests/bpf/progs/cgroup_mprog.c+724 -00 repliesApplied

Reviews trailers per patch

2 of 5 patches have a Reviewed-by or Acked-by.

PatchAndrii Nakryiko
1/5A-b
2/5
3/5A-b
4/5
5/5

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

Read the v5 thread

Cover letter [PATCH bpf-next v5 0/5] bpf: Implement mprog API on top of existing cgroup progs

Current cgroup prog ordering is appending at attachment time. This is not ideal. In some cases, users want specific ordering at a particular cgroup level. For example, in Meta, we have a case where three different applications all have cgroup/setsockopt progs and they require specific ordering. Current approach is to use a bpfchainer where one bpf prog contains multiple global functions and each global function can be freplaced by a prog for a specific application. The ordering of global functions decides the ordering of those application specific bpf progs. Using bpfchainer is a centralized approach and is not desirable as one of applications acts as a daemon. The decentralized attachment approach is more favorable for those applications.

To address this, the existing mprog API ([2]) seems an ideal solution with supporting BPF_F_BEFORE and BPF_F_AFTER flags on top of existing cgroup bpf implementation. More specifically, the support is added for prog/link attachment with BPF_F_BEFORE and BPF_F_AFTER. The kernel mprog interface ([2]) is not used and the implementation is directly done in cgroup bpf code base. The mprog 'revision' is also implemented in attach/detach/replace, so users can query revision number to check the change of cgroup prog list.

The patch set contains 5 patches. Patch 1 adds revision support for cgroup bpf progs. Patch 2 implements mprog API implementation for prog/link attach and revision update. Patch 3 adds a new libbpf API to do cgroup link attach with flags like BPF_F_BEFORE/BPF_F_AFTER. Patches 4 and 5 add two tests to validate the implementation.

[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]

Changelogs:
v4 -> v5:
- v4: https://lore.kernel.org/bpf/[email protected]/
- Remove early prog/link checking based flags and id_or_fd as later code will do checking as well.
- Do proper cgroup flag checking for bpf_prog_attach().
v3 -> v4:
- v3: https://lore.kernel.org/bpf/[email protected]/
- Refactor some to make BPF_F_BEFORE/BPF_F_AFTER handling easier to understand.
- Perviously, I degraded 'link' to 'prog' for later mprog handling. This is not correct. Similar to mprog.c, we should be check 'link' instead link->prog since it is possible two different links may have the same underlying prog and we do not want to miss supporting such use case.
v2 -> v3:
- v2: https://lore.kernel.org/bpf/[email protected]/
- Big change to replace get_anchor_prog() to get_prog_list() so the 'struct bpf_prog_list *' is returned directly.
- Support 'BPF_F_BEFORE | BPF_F_AFTER' attachment if the prog list is empty and flags do not have 'BPF_F_LINK | BPF_F_ID' and id_or_fd is 0.
- Add BPF_F_LINK support.
- Patch 4 is added to reuse id_from_prog_fd() and id_from_link_fd().
v1 -> v2:
- v1: https://lore.kernel.org/bpf/[email protected]/
- Change cgroup_bpf.revisions from atomic64_t to u64.
- Added missing bpf_prog_put in various places.
- Rename get_cmp_prog() to get_anchor_prog(). The implementation tries to find the anchor prog regardless of whether id_or_fd is non-NULL or not.
- Rename bpf_cgroup_prog_attached() to is_cgroup_prog_type() and handle BPF_PROG_TYPE_LSM properly (with BPF_LSM_CGROUP attach type).
- I kept 'id || id_or_fd' condition as the condition 'id' is also used in mprog.c so I assume it is okay in cgroup.c as well.

Yonghong Song (5):
cgroup: Add bpf prog revisions to struct cgroup_bpf
bpf: Implement mprog API on top of existing cgroup progs
libbpf: Support link-based cgroup attach with options selftests/bpf: Move some tc_helpers.h functions to test_progs.h selftests/bpf: Add two selftests for mprog API based cgroup progs

 include/linux/bpf-cgroup-defs.h               |   1 +
 include/uapi/linux/bpf.h                      |   7 +
 kernel/bpf/cgroup.c                           | 188 +++++-
 kernel/bpf/syscall.c                          |  44 +-
 kernel/cgroup/cgroup.c                        |   5 +
 tools/include/uapi/linux/bpf.h                |   7 +
 tools/lib/bpf/bpf.c                           |  44 ++
 tools/lib/bpf/bpf.h                           |   5 +
 tools/lib/bpf/libbpf.c                        |  28 +
 tools/lib/bpf/libbpf.h                        |  15 +
 tools/lib/bpf/libbpf.map                      |   1 +
 .../bpf/prog_tests/cgroup_mprog_opts.c        | 617 ++++++++++++++++++
 .../bpf/prog_tests/cgroup_mprog_ordering.c    |  77 +++
 .../selftests/bpf/prog_tests/tc_helpers.h     |  28 -
 .../selftests/bpf/progs/cgroup_mprog.c        |  30 +
 tools/testing/selftests/bpf/test_progs.h      |  28 +
 16 files changed, 1060 insertions(+), 65 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_mprog_opts.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_mprog_ordering.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_mprog.c
Signature · 1 line
2.47.1