Skip to content

Commit 5c1a376

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
bpf: Limit up to 512 bytes for bpf_global_percpu_ma allocation
For percpu data structure allocation with bpf_global_percpu_ma, the maximum data size is 4K. But for a system with large number of cpus, bigger data size (e.g., 2K, 4K) might consume a lot of memory. For example, the percpu memory consumption with unit size 2K and 1024 cpus will be 2K * 1K * 1k = 2GB memory. We should discourage such usage. Let us limit the maximum data size to be 512 for bpf_global_percpu_ma allocation. Acked-by: Hou Tao <[email protected]> Signed-off-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 0e2ba9f commit 5c1a376

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

kernel/bpf/verifier.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ struct bpf_verifier_stack_elem {
195195
POISON_POINTER_DELTA))
196196
#define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV))
197197

198+
#define BPF_GLOBAL_PERCPU_MA_MAX_SIZE 512
199+
198200
static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx);
199201
static int release_reference(struct bpf_verifier_env *env, int ref_obj_id);
200202
static void invalidate_non_owning_refs(struct bpf_verifier_env *env);
@@ -12160,6 +12162,12 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1216012162
}
1216112163

1216212164
if (meta.func_id == special_kfunc_list[KF_bpf_percpu_obj_new_impl]) {
12165+
if (ret_t->size > BPF_GLOBAL_PERCPU_MA_MAX_SIZE) {
12166+
verbose(env, "bpf_percpu_obj_new type size (%d) is greater than %d\n",
12167+
ret_t->size, BPF_GLOBAL_PERCPU_MA_MAX_SIZE);
12168+
return -EINVAL;
12169+
}
12170+
1216312171
if (!bpf_global_percpu_ma_set) {
1216412172
mutex_lock(&bpf_percpu_ma_lock);
1216512173
if (!bpf_global_percpu_ma_set) {

0 commit comments

Comments
 (0)