linux/tools/bpf
Quentin Monnet f0cf642c56 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 97306be45f ("Merge branch 'switch to memcg-based memory accounting'")
  [1] commit a777e18f1b ("bpftool: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK")
  [2] commit 6b4384ff10 ("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
2022-06-29 23:33:02 +02:00
..
bpftool bpftool: Probe for memcg-based accounting before bumping rlimit 2022-06-29 23:33:02 +02:00
resolve_btfids Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net 2022-02-03 17:36:16 -08:00
runqslower tools/runqslower: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK 2022-04-10 20:17:16 -07:00
.gitignore .gitignore: add SPDX License Identifier 2020-03-25 11:50:48 +01:00
bpf_asm.c docs: networking: convert filter.txt to ReST 2020-04-28 14:39:46 -07:00
bpf_dbg.c bpf: Simplify the calculation of variables 2021-03-04 19:24:53 -08:00
bpf_exp.l bpf: fix cbpf parser bug for octal numbers 2018-06-03 07:46:55 -07:00
bpf_exp.y tools, bpf_asm: Exit non-zero on errors 2021-02-26 22:53:50 +01:00
bpf_jit_disasm.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 232 2019-06-19 17:09:06 +02:00
Makefile tools: bpf: Fix error in 'make -C tools/ bpf_install' 2021-07-07 14:06:38 -07:00