Skip to content

Commit 1fda5bb

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
bpf: Do not allocate percpu memory at init stage
Kirill Shutemov reported significant percpu memory consumption increase after booting in 288-cpu VM ([1]) due to commit 41a5db8 ("bpf: Add support for non-fix-size percpu mem allocation"). The percpu memory consumption is increased from 111MB to 969MB. The number is from /proc/meminfo. I tried to reproduce the issue with my local VM which at most supports upto 255 cpus. With 252 cpus, without the above commit, the percpu memory consumption immediately after boot is 57MB while with the above commit the percpu memory consumption is 231MB. This is not good since so far percpu memory from bpf memory allocator is not widely used yet. Let us change pre-allocation in init stage to on-demand allocation when verifier detects there is a need of percpu memory for bpf program. With this change, percpu memory consumption after boot can be reduced signicantly. [1] https://lore.kernel.org/lkml/[email protected]/ Fixes: 41a5db8 ("bpf: Add support for non-fix-size percpu mem allocation") Reported-and-tested-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Yonghong Song <[email protected]> Acked-by: Hou Tao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent e2e57d6 commit 1fda5bb

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ extern struct idr btf_idr;
5656
extern spinlock_t btf_idr_lock;
5757
extern struct kobject *btf_kobj;
5858
extern struct bpf_mem_alloc bpf_global_ma, bpf_global_percpu_ma;
59-
extern bool bpf_global_ma_set, bpf_global_percpu_ma_set;
59+
extern bool bpf_global_ma_set;
6060

6161
typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64);
6262
typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,

kernel/bpf/core.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
#define OFF insn->off
6565
#define IMM insn->imm
6666

67-
struct bpf_mem_alloc bpf_global_ma, bpf_global_percpu_ma;
68-
bool bpf_global_ma_set, bpf_global_percpu_ma_set;
67+
struct bpf_mem_alloc bpf_global_ma;
68+
bool bpf_global_ma_set;
6969

7070
/* No hurry in this branch
7171
*
@@ -2934,9 +2934,7 @@ static int __init bpf_global_ma_init(void)
29342934

29352935
ret = bpf_mem_alloc_init(&bpf_global_ma, 0, false);
29362936
bpf_global_ma_set = !ret;
2937-
ret = bpf_mem_alloc_init(&bpf_global_percpu_ma, 0, true);
2938-
bpf_global_percpu_ma_set = !ret;
2939-
return !bpf_global_ma_set || !bpf_global_percpu_ma_set;
2937+
return ret;
29402938
}
29412939
late_initcall(bpf_global_ma_init);
29422940
#endif

kernel/bpf/verifier.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/poison.h>
2727
#include <linux/module.h>
2828
#include <linux/cpumask.h>
29+
#include <linux/bpf_mem_alloc.h>
2930
#include <net/xdp.h>
3031

3132
#include "disasm.h"
@@ -41,6 +42,9 @@ static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
4142
#undef BPF_LINK_TYPE
4243
};
4344

45+
struct bpf_mem_alloc bpf_global_percpu_ma;
46+
static bool bpf_global_percpu_ma_set;
47+
4448
/* bpf_check() is a static code analyzer that walks eBPF program
4549
* instruction by instruction and updates register/stack state.
4650
* All paths of conditional branches are analyzed until 'bpf_exit' insn.
@@ -336,6 +340,7 @@ struct bpf_kfunc_call_arg_meta {
336340
struct btf *btf_vmlinux;
337341

338342
static DEFINE_MUTEX(bpf_verifier_lock);
343+
static DEFINE_MUTEX(bpf_percpu_ma_lock);
339344

340345
static const struct bpf_line_info *
341346
find_linfo(const struct bpf_verifier_env *env, u32 insn_off)
@@ -12091,8 +12096,19 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1209112096
if (meta.func_id == special_kfunc_list[KF_bpf_obj_new_impl] && !bpf_global_ma_set)
1209212097
return -ENOMEM;
1209312098

12094-
if (meta.func_id == special_kfunc_list[KF_bpf_percpu_obj_new_impl] && !bpf_global_percpu_ma_set)
12095-
return -ENOMEM;
12099+
if (meta.func_id == special_kfunc_list[KF_bpf_percpu_obj_new_impl]) {
12100+
if (!bpf_global_percpu_ma_set) {
12101+
mutex_lock(&bpf_percpu_ma_lock);
12102+
if (!bpf_global_percpu_ma_set) {
12103+
err = bpf_mem_alloc_init(&bpf_global_percpu_ma, 0, true);
12104+
if (!err)
12105+
bpf_global_percpu_ma_set = true;
12106+
}
12107+
mutex_unlock(&bpf_percpu_ma_lock);
12108+
if (err)
12109+
return err;
12110+
}
12111+
}
1209612112

1209712113
if (((u64)(u32)meta.arg_constant.value) != meta.arg_constant.value) {
1209812114
verbose(env, "local type ID argument must be in range [0, U32_MAX]\n");

0 commit comments

Comments
 (0)