]> git.baikalelectronics.ru Git - kernel.git/commit
bpftool: Probe for memcg-based accounting before bumping rlimit
authorQuentin Monnet <quentin@isovalent.com>
Wed, 29 Jun 2022 11:13:51 +0000 (12:13 +0100)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 29 Jun 2022 21:33:02 +0000 (23:33 +0200)
commit0e316dbcf942c1a026ce7c30fb5e45a79d5cf4e0
tree5a63af059434adeabcb12b02f8efd02359196776
parent8788b0a629935b5b97c6c90aaa6886dde5a6921d
bpftool: Probe for memcg-based accounting before bumping rlimit

Bpftool used to bump the memlock rlimit to make sure to be able to load
BPF objects. After the kernel has switched to memcg-based memory
accounting [0] in 5.11, bpftool has relied on libbpf to probe the system
for memcg-based accounting support and for raising the rlimit if
necessary [1]. But this was later reverted, because the probe would
sometimes fail, resulting in bpftool not being able to load all required
objects [2].

Here we add a more efficient probe, in bpftool itself. We first lower
the rlimit to 0, then we attempt to load a BPF object (and finally reset
the rlimit): if the load succeeds, then memcg-based memory accounting is
supported.

This approach was earlier proposed for the probe in libbpf itself [3],
but given that the library may be used in multithreaded applications,
the probe could have undesirable consequences if one thread attempts to
lock kernel memory while memlock rlimit is at 0. Since bpftool is
single-threaded and the rlimit is process-based, this is fine to do in
bpftool itself.

This probe was inspired by the similar one from the cilium/ebpf Go
library [4].

  [0] commit 37002c4e8a72 ("Merge branch 'switch to memcg-based memory accounting'")
  [1] commit b12322bdd4f7 ("bpftool: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK")
  [2] commit 374bf573cd11 ("Revert "bpftool: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK"")
  [3] https://lore.kernel.org/bpf/20220609143614.97837-1-quentin@isovalent.com/t/#u
  [4] https://github.com/cilium/ebpf/blob/v0.9.0/rlimit/rlimit.go#L39

Suggested-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Yafang Shao <laoar.shao@gmail.com>
Link: https://lore.kernel.org/bpf/20220629111351.47699-1-quentin@isovalent.com
tools/bpf/bpftool/common.c