Re: [RFC PATCH] libbpf: Support setting map max_entries at runtime
From: Andrii Nakryiko
Date: Thu Sep 03 2020 - 01:29:11 EST
On Mon, Aug 31, 2020 at 4:03 PM Barret Rhoden <brho@xxxxxxxxxx> wrote:
>
> The max_entries for a BPF map may depend on runtime parameters.
> Currently, we need to know the maximum value at BPF compile time. For
> instance, if you want an array map with NR_CPUS entries, you would hard
> code your architecture's largest value for CONFIG_NR_CPUS. This wastes
> memory at runtime.
>
> For the NR_CPU case, one could use a PERCPU map type, but those maps are
> limited in functionality. For instance, BPF programs can only access
> their own PERCPU part of the map, and the maps are not mmappable.
>
> This commit allows the use of sentinel values in BPF map definitions,
> which libbpf patches at runtime.
>
> For starters, we support NUM_POSSIBLE_CPUS: e.g.
>
> struct {
> __uint(type, BPF_MAP_TYPE_ARRAY);
> __uint(max_entries, NUM_POSSIBLE_CPUS);
> __type(key, u32);
> __type(value, struct cpu_data);
> } cpu_blobs SEC(".maps");
>
> This can be extended to other runtime dependent values, such as the
> maximum number of threads (/proc/sys/kernel/threads-max).
>
> Signed-off-by: Barret Rhoden <brho@xxxxxxxxxx>
> ---
libbpf provides bpf_map__set_max_entries() API exactly for such use
cases, please use that.
> tools/lib/bpf/bpf_helpers.h | 4 ++++
> tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++-------
> tools/lib/bpf/libbpf.h | 4 ++++
> 3 files changed, 41 insertions(+), 7 deletions(-)
>
[...]