Skip to content

bpf: switch to memcg-based memory accounting

Roman Gushchin bpf linux-mm lkml netdev oe-kbuild-all oe-lkp 4 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest30 patches 13 repliesIn review
  2. v329 patches 8 repliesSuperseded
  3. v235 patches 52 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v135 patches0 repliesSuperseded

Changes in v4 from the cover letter

- covered allocations made from an interrupt context, by Daniel
- added some clarifications to the cover letter

Patches in v4 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/30mm: support nesting memalloc_use_memcg()fs/buffer.c fs/notify/fanotify/fanotify.c fs/notify/inotify/inotify_fsnotify.c +2 more+21 -291 reply
2/30bpf: memcg-based memory accounting for bpf progskernel/bpf/core.c+4 -42 replies
3/30bpf: memcg-based memory accounting for bpf mapsinclude/linux/bpf.h kernel/bpf/helpers.c kernel/bpf/syscall.c+66 -22 replies
4/30bpf: refine memcg-based memory accounting for arraymap mapskernel/bpf/arraymap.c+3 -31 reply
5/30bpf: refine memcg-based memory accounting for cpumap mapskernel/bpf/cpumap.c+2 -21 reply
6/30bpf: memcg-based memory accounting for cgroup storage mapskernel/bpf/local_storage.c+8 -91 reply
7/30bpf: refine memcg-based memory accounting for devmap mapskernel/bpf/devmap.c+3 -21 reply
8/30bpf: refine memcg-based memory accounting for hashtab mapskernel/bpf/hashtab.c+7 -71 reply
9/30bpf: memcg-based memory accounting for lpm_trie mapskernel/bpf/lpm_trie.c+2 -20 replies
10/30bpf: memcg-based memory accounting for bpf ringbufferkernel/bpf/ringbuf.c+5 -40 replies
11/30bpf: memcg-based memory accounting for socket storage mapsnet/core/bpf_sk_storage.c+7 -50 replies
12/30bpf: refine memcg-based memory accounting for sockmap and sockhash mapsnet/core/sock_map.c+4 -30 replies
13/30bpf: refine memcg-based memory accounting for xskmap mapsnet/xdp/xskmap.c+2 -10 replies
14/30bpf: eliminate rlimit-based memory accounting for arraymap mapskernel/bpf/arraymap.c+4 -200 replies
15/30bpf: eliminate rlimit-based memory accounting for bpf_struct_ops mapskernel/bpf/bpf_struct_ops.c+3 -160 replies
16/30bpf: eliminate rlimit-based memory accounting for cpumap mapskernel/bpf/cpumap.c+1 -150 replies
17/30bpf: eliminate rlimit-based memory accounting for cgroup storage mapskernel/bpf/local_storage.c+1 -200 replies
18/30bpf: eliminate rlimit-based memory accounting for devmap mapskernel/bpf/devmap.c+2 -160 replies
19/30bpf: eliminate rlimit-based memory accounting for hashtab mapskernel/bpf/hashtab.c+1 -180 replies
20/30bpf: eliminate rlimit-based memory accounting for lpm_trie mapskernel/bpf/lpm_trie.c+0 -130 replies
21/30bpf: eliminate rlimit-based memory accounting for queue_stack_maps mapskernel/bpf/queue_stack_maps.c+4 -120 replies
22/30bpf: eliminate rlimit-based memory accounting for reuseport_array mapskernel/bpf/reuseport_array.c+2 -100 replies
23/30bpf: eliminate rlimit-based memory accounting for bpf ringbufferkernel/bpf/ringbuf.c+4 -200 replies
24/30bpf: eliminate rlimit-based memory accounting for sockmap and sockhash mapsnet/core/sock_map.c+6 -270 replies
25/30bpf: eliminate rlimit-based memory accounting for stackmap mapskernel/bpf/stackmap.c+3 -130 replies
26/30bpf: eliminate rlimit-based memory accounting for socket storage mapsnet/core/bpf_sk_storage.c+0 -110 replies
27/30bpf: eliminate rlimit-based memory accounting for xskmap mapsnet/xdp/xskmap.c+1 -90 replies
28/30bpf: eliminate rlimit-based memory accounting infra for bpf mapsinclude/linux/bpf.h kernel/bpf/syscall.c tools/testing/selftests/bpf/progs/map_ptr_kern.c+2 -792 replies
29/30bpf: eliminate rlimit-based memory accounting for bpf progsinclude/linux/bpf.h kernel/bpf/core.c kernel/bpf/syscall.c+12 -970 replies
30/30bpf: samples: do not touch RLIMIT_MEMLOCKsamples/bpf/map_perf_test_user.c samples/bpf/offwaketime_user.c samples/bpf/sockex2_user.c +24 more+0 -1330 replies

Reviews trailers per patch

27 of 30 patches have a Reviewed-by or Acked-by.

PatchSong LiuAndrii NakryikoShakeel Butt
1/30R-b
2/30
3/30
4/30A-b
5/30A-b R-b
6/30A-b R-b
7/30A-b R-b
8/30A-b R-b
9/30A-b
10/30A-b
11/30A-b
12/30A-b
13/30A-b
14/30A-b
15/30A-b
16/30A-b
17/30A-b
18/30A-b
19/30A-b
20/30A-b
21/30A-b
22/30A-b
23/30A-b A-b
24/30A-b
25/30A-b
26/30A-b
27/30A-b
28/30
29/30A-b
30/30A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 3 reviewers.

Read the v4 thread

Cover letter [PATCH bpf-next v4 00/30] bpf: switch to memcg-based memory accounting

Currently bpf is using the memlock rlimit for the memory accounting. This approach has its downsides and over time has created a significant amount of problems:

1) The limit is per-user, but because most bpf operations are performed as root, the limit has a little value.

2) It's hard to come up with a specific maximum value. Especially because the counter is shared with non-bpf users (e.g. memlock() users). Any specific value is either too low and creates false failures or too high and useless.

3) Charging is not connected to the actual memory allocation. Bpf code should manually calculate the estimated cost and precharge the counter, and then take care of uncharging, including all fail paths. It adds to the code complexity and makes it easy to leak a charge.

4) There is no simple way of getting the current value of the counter. We've used drgn for it, but it's far from being convenient.

5) Cryptic -EPERM is returned on exceeding the limit. Libbpf even had a function to "explain" this case for users.

In order to overcome these problems let's switch to the memcg-based memory accounting of bpf objects. With the recent addition of the percpu memory accounting, now it's possible to provide a comprehensive accounting of the memory used by bpf programs and maps.

This approach has the following advantages:
1) The limit is per-cgroup and hierarchical. It's way more flexible and allows a better control over memory usage by different workloads. Of course, it requires enabled cgroups and kernel memory accounting and properly configured cgroup tree, but it's a default configuration for a modern Linux system.

2) The actual memory consumption is taken into account. It happens automatically on the allocation time if __GFP_ACCOUNT flags is passed. Uncharging is also performed automatically on releasing the memory. So the code on the bpf side becomes simpler and safer.

3) There is a simple way to get the current value and statistics.

In general, if a process performs a bpf operation (e.g. creates or updates a map), it's memory cgroup is charged. However map updates performed from an interrupt context are charged to the memory cgroup which contained the process, which created the map.

Providing a 1:1 replacement for the rlimit-based memory accounting is a non-goal of this patchset. Users and memory cgroups are completely orthogonal, so it's not possible even in theory.
Memcg-based memory accounting requires a properly configured cgroup tree to be actually useful. However, it's the way how the memory is managed on a modern Linux system.

The patchset consists of the following parts:
1) an auxiliary patch by Johanness, which adds an ability to charge a custom memory cgroup from an interrupt context
2) memcg-based accounting for various bpf objects: progs and maps
3) removal of the rlimit-based accounting
4) removal of rlimit adjustments in userspace samples

v4:
- covered allocations made from an interrupt context, by Daniel
- added some clarifications to the cover letter

v3:
- droped the userspace part for further discussions/refinements, by Andrii and Song
v2:
- fixed build issue, caused by the remaining rlimit-based accounting for sockhash maps

Johannes Weiner (1):
mm: support nesting memalloc_use_memcg()

Roman Gushchin (29):
bpf: memcg-based memory accounting for bpf progs
bpf: memcg-based memory accounting for bpf maps
bpf: refine memcg-based memory accounting for arraymap maps
bpf: refine memcg-based memory accounting for cpumap maps
bpf: memcg-based memory accounting for cgroup storage maps
bpf: refine memcg-based memory accounting for devmap maps
bpf: refine memcg-based memory accounting for hashtab maps
bpf: memcg-based memory accounting for lpm_trie maps
bpf: memcg-based memory accounting for bpf ringbuffer
bpf: memcg-based memory accounting for socket storage maps
bpf: refine memcg-based memory accounting for sockmap and sockhash
maps
bpf: refine memcg-based memory accounting for xskmap maps
bpf: eliminate rlimit-based memory accounting for arraymap maps
bpf: eliminate rlimit-based memory accounting for bpf_struct_ops maps
bpf: eliminate rlimit-based memory accounting for cpumap maps
bpf: eliminate rlimit-based memory accounting for cgroup storage maps
bpf: eliminate rlimit-based memory accounting for devmap maps
bpf: eliminate rlimit-based memory accounting for hashtab maps
bpf: eliminate rlimit-based memory accounting for lpm_trie maps
bpf: eliminate rlimit-based memory accounting for queue_stack_maps
maps
bpf: eliminate rlimit-based memory accounting for reuseport_array maps
bpf: eliminate rlimit-based memory accounting for bpf ringbuffer
bpf: eliminate rlimit-based memory accounting for sockmap and sockhash
maps
bpf: eliminate rlimit-based memory accounting for stackmap maps
bpf: eliminate rlimit-based memory accounting for socket storage maps
bpf: eliminate rlimit-based memory accounting for xskmap maps
bpf: eliminate rlimit-based memory accounting infra for bpf maps
bpf: eliminate rlimit-based memory accounting for bpf progs
bpf: samples: do not touch RLIMIT_MEMLOCK

 fs/buffer.c                                   |   6 +-
 fs/notify/fanotify/fanotify.c                 |   5 +-
 fs/notify/inotify/inotify_fsnotify.c          |   5 +-
 include/linux/bpf.h                           |  27 +--
 include/linux/sched/mm.h                      |  28 +--
 kernel/bpf/arraymap.c                         |  30 +--
 kernel/bpf/bpf_struct_ops.c                   |  19 +-
 kernel/bpf/core.c                             |  20 +-
 kernel/bpf/cpumap.c                           |  20 +-
 kernel/bpf/devmap.c                           |  23 +--
 kernel/bpf/hashtab.c                          |  33 +---
 kernel/bpf/helpers.c                          |  37 +++-
 kernel/bpf/local_storage.c                    |  38 +---
 kernel/bpf/lpm_trie.c                         |  17 +-
 kernel/bpf/queue_stack_maps.c                 |  16 +-
 kernel/bpf/reuseport_array.c                  |  12 +-
 kernel/bpf/ringbuf.c                          |  33 +---
 kernel/bpf/stackmap.c                         |  16 +-
 kernel/bpf/syscall.c                          | 177 ++++--------------
 mm/memcontrol.c                               |   6 +-
 net/core/bpf_sk_storage.c                     |  23 +--
 net/core/sock_map.c                           |  40 +---
 net/xdp/xskmap.c                              |  13 +-
 samples/bpf/map_perf_test_user.c              |  11 --
 samples/bpf/offwaketime_user.c                |   2 -
 samples/bpf/sockex2_user.c                    |   2 -
 samples/bpf/sockex3_user.c                    |   2 -
 samples/bpf/spintest_user.c                   |   2 -
 samples/bpf/syscall_tp_user.c                 |   2 -
 samples/bpf/task_fd_query_user.c              |   5 -
 samples/bpf/test_lru_dist.c                   |   3 -
 samples/bpf/test_map_in_map_user.c            |   9 -
 samples/bpf/test_overhead_user.c              |   2 -
 samples/bpf/trace_event_user.c                |   2 -
 samples/bpf/tracex2_user.c                    |   6 -
 samples/bpf/tracex3_user.c                    |   6 -
 samples/bpf/tracex4_user.c                    |   6 -
 samples/bpf/tracex5_user.c                    |   3 -
 samples/bpf/tracex6_user.c                    |   3 -
 samples/bpf/xdp1_user.c                       |   6 -
 samples/bpf/xdp_adjust_tail_user.c            |   6 -
 samples/bpf/xdp_monitor_user.c                |   6 -
 samples/bpf/xdp_redirect_cpu_user.c           |   6 -
 samples/bpf/xdp_redirect_map_user.c           |   6 -
 samples/bpf/xdp_redirect_user.c               |   6 -
 samples/bpf/xdp_router_ipv4_user.c            |   6 -
 samples/bpf/xdp_rxq_info_user.c               |   6 -
 samples/bpf/xdp_sample_pkts_user.c            |   6 -
 samples/bpf/xdp_tx_iptunnel_user.c            |   6 -
 samples/bpf/xdpsock_user.c                    |   7 -
 .../selftests/bpf/progs/map_ptr_kern.c        |   5 -
 51 files changed, 180 insertions(+), 602 deletions(-)
Signature · 1 line
2.26.2