Skip to content

bpf: cgroup local storage

Roman Gushchin lkml netdev 7 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v7 Latest14 patches 1 replyIn review
  2. v614 patches 1 replyApplied
  3. v514 patches 0 repliesSuperseded
Earlier versions · 4
VersionPostedPatchesRepliesStatus
v414 patches5 repliesSuperseded
v314 patches4 repliesSuperseded
v214 patches4 repliesSuperseded
v114 patches1 replySuperseded

v7->v6 from the cover letter

- fixed a use-after-free bug, caused by not clearing

Patches in v7 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/14bpf: add ability to charge bpf maps memory dynamicallyinclude/linux/bpf.h kernel/bpf/syscall.c+45 -150 replies
2/14bpf: introduce cgroup storage mapsinclude/linux/bpf-cgroup.h include/linux/bpf.h include/linux/bpf_types.h +5 more+440 -00 replies
3/14bpf: pass a pointer to a cgroup storage using pcpu variableinclude/linux/bpf-cgroup.h kernel/bpf/local_storage.c+17 -00 replies
4/14bpf: allocate cgroup storage entries on attaching bpf programsinclude/linux/bpf-cgroup.h kernel/bpf/cgroup.c+32 -40 replies
5/14bpf: extend bpf_prog_array to store pointers to the cgroup storagedrivers/media/rc/bpf-lirc.c include/linux/bpf.h kernel/bpf/cgroup.c +1 more+70 -560 replies
6/14bpf/verifier: introduce BPF_PTR_TO_MAP_VALUEinclude/linux/bpf.h kernel/bpf/verifier.c+7 -20 replies
7/14bpf: don't allow create maps of cgroup local storageskernel/bpf/map_in_map.c+2 -10 replies
8/14bpf: introduce the bpf_get_local_storage() helper functioninclude/linux/bpf.h include/uapi/linux/bpf.h kernel/bpf/cgroup.c +4 more+85 -20 replies
9/14bpf: sync bpf.h to tools/tools/include/uapi/linux/bpf.h+26 -10 replies
10/14bpftool: add support for CGROUP_STORAGE mapstools/bpf/bpftool/map.c+1 -00 replies
11/14bpf/test_run: support cgroup local storagenet/bpf/test_run.c+11 -20 replies
12/14selftests/bpf: add verifier cgroup storage teststools/testing/selftests/bpf/bpf_helpers.h tools/testing/selftests/bpf/test_verifier.c+141 -10 replies
13/14selftests/bpf: add a cgroup storage testtools/testing/selftests/bpf/Makefile tools/testing/selftests/bpf/test_cgroup_storage.c+132 -10 replies
14/14samples/bpf: extend test_cgrp2_attach2 test to use cgroup storagesamples/bpf/test_cgrp2_attach2.c+20 -10 replies

Reviews trailers per patch

All 14 patches have a Reviewed-by or Acked-by.

PatchMartin KaFai LauJakub Kicinski
1/14A-b
2/14A-b
3/14A-b
4/14A-b
5/14A-b
6/14A-b
7/14A-b
8/14A-b
9/14A-b
10/14A-b A-b
11/14A-b
12/14A-b
13/14A-b
14/14A-b

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

Read the v7 thread

Cover letter [PATCH v7 bpf-next 00/14] bpf: cgroup local storage

This patchset implements cgroup local storage for bpf programs. The main idea is to provide a fast accessible memory for storing various per-cgroup data, e.g. number of transmitted packets.

Cgroup local storage looks as a special type of map for userspace, and is accessible using generic bpf maps API for reading and updating of the data. The (cgroup inode id, attachment type) pair is used as a map key.

A user can't create new entries or destroy existing entries; it happens automatically when a user attaches/detaches a bpf program to a cgroup.

Quoted 1 line

From a bpf program's point of view, cgroup storage is accessible

without lookup using the special get_local_storage() helper function. It takes a map fd as an argument. It always returns a valid pointer to the corresponding memory area.

To implement such a lookup-free access a pointer to the cgroup storage is saved for an attachment of a bpf program to a cgroup, if required by the program. Before running the program, it's saved in a special global per-cpu variable, which is accessible from the get_local_storage() helper.

This patchset implement only cgroup local storage, however the API is intentionally made extensible to support other local storage types
further: e.g. thread local storage, socket local storage, etc.

v7->v6:
- fixed a use-after-free bug, caused by not clearing

    prog->aux->cgroup_storage pointer after releasing the map

v6->v5:
- fixed an error with returning -EINVAL instead of a pointer

v5->v4:
- fixed an issue in verifier (test that flags == 0 properly)
- added a corresponding test
- added a note about synchronization, sync docs to tools/uapi/...
- switched the cgroup test to use XADD
- added a check for attr->max_entries to be 0, and atter->max_flags to be sane
- use bpf_uncharge_memlock() in bpf_uncharge_memlock()
- rebased to bpf-next

v4->v3:
- fixed a leak in cgroup attachment code (discovered by Daniel)
- cgroup storage map will be released if the corresponding bpf program failed to load by any reason
- introduced bpf_uncharge_memlock() helper

v3->v2:
- fixed more build and sparse issues
- rebased to bpf-next

v2->v1:
- fixed build issues
- removed explicit rlimit calls in patch 14
- rebased to bpf-next

Cc Alexei Starovoitov <[email protected]>
Cc Martin KaFai Lau <[email protected]>

Roman Gushchin (14):
bpf: add ability to charge bpf maps memory dynamically
bpf: introduce cgroup storage maps
bpf: pass a pointer to a cgroup storage using pcpu variable
bpf: allocate cgroup storage entries on attaching bpf programs
bpf: extend bpf_prog_array to store pointers to the cgroup storage bpf/verifier: introduce BPF_PTR_TO_MAP_VALUE
bpf: don't allow create maps of cgroup local storages
bpf: introduce the bpf_get_local_storage() helper function
bpf: sync bpf.h to tools/
bpftool: add support for CGROUP_STORAGE maps bpf/test_run: support cgroup local storage selftests/bpf: add verifier cgroup storage tests selftests/bpf: add a cgroup storage test
samples/bpf: extend test_cgrp2_attach2 test to use cgroup storage

 drivers/media/rc/bpf-lirc.c                       |  10 +-
 include/linux/bpf-cgroup.h                        |  54 ++++
 include/linux/bpf.h                               |  25 +-
 include/linux/bpf_types.h                         |   3 +
 include/uapi/linux/bpf.h                          |  27 +-
 kernel/bpf/Makefile                               |   1 +
 kernel/bpf/cgroup.c                               |  58 +++-
 kernel/bpf/core.c                                 |  77 ++---
 kernel/bpf/helpers.c                              |  20 ++
 kernel/bpf/local_storage.c                        | 378 ++++++++++++++++++++++
 kernel/bpf/map_in_map.c                           |   3 +-
 kernel/bpf/syscall.c                              |  61 +++-
 kernel/bpf/verifier.c                             |  38 ++-
 net/bpf/test_run.c                                |  13 +-
 net/core/filter.c                                 |  23 +-
 samples/bpf/test_cgrp2_attach2.c                  |  21 +-
 tools/bpf/bpftool/map.c                           |   1 +
 tools/include/uapi/linux/bpf.h                    |  27 +-
 tools/testing/selftests/bpf/Makefile              |   3 +-
 tools/testing/selftests/bpf/bpf_helpers.h         |   2 +
 tools/testing/selftests/bpf/test_cgroup_storage.c | 130 ++++++++
 tools/testing/selftests/bpf/test_verifier.c       | 140 +++++++-
 22 files changed, 1029 insertions(+), 86 deletions(-)
 create mode 100644 kernel/bpf/local_storage.c
 create mode 100644 tools/testing/selftests/bpf/test_cgroup_storage.c
Signature · 1 line
2.14.4