From: Alexei Starovoitov max_entries of an arena is its size in pages. The kernel rejects 0. Thus each prog must select a number, and the same number is a different size on a kernel with a different page size. A small arena is not cheaper than a large one. For each arena the kernel reserves 4 Gbyte of kernel addresses and allocates the page tables for all of them: 8 Mbyte with pages of 4 Kbyte. max_entries changes neither. After that only allocated pages use memory. When max_entries is not set, make the arena 4 Gbyte, which is the maximum. libbpf calculates max_entries from the page size. The default is in libbpf. There it also works on the kernels that reject 0, and it is easier to change. For example, a smaller default can be better when the kernel allocates page tables only for the pages that an arena can have. A kernel that accepts 0 is a separate change. libbpf needs the default with it too. libbpf maps the full arena into the process that loads the object. Thus VmSize of this process grows by 4 Gbyte. If the process called mlockall(MCL_FUTURE), the kernel populates all these pages. Signed-off-by: Alexei Starovoitov --- tools/lib/bpf/libbpf.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 02e75b183ed0..76d22a739e00 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -552,6 +552,12 @@ struct bpf_struct_ops { #define STRUCT_OPS_SEC ".struct_ops" #define STRUCT_OPS_LINK_SEC ".struct_ops.link" #define ARENA_SEC ".addr_space.1" +/* + * The size of the arena map when max_entries is not set: the largest one. + * It's in bytes: max_entries of arena is in pages and the size of a page + * depends on the kernel. What an empty arena costs doesn't depend on its size. + */ +#define ARENA_DEFAULT_SZ (1ULL << 32) enum libbpf_map_type { LIBBPF_MAP_UNSPEC, @@ -3165,6 +3171,8 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, return -EINVAL; } obj->arena_map_idx = i; + if (!map->def.max_entries) + map->def.max_entries = ARENA_DEFAULT_SZ / sysconf(_SC_PAGE_SIZE); if (obj->efile.arena_data) { err = init_arena_map_data(obj, map, obj->efile.arena_data->d_buf, -- 2.55.0