Skip to content

Commit 9c2d63b

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: fix mlock precharge on arraymaps
syzkaller recently triggered OOM during percpu map allocation; while there is work in progress by Dennis Zhou to add __GFP_NORETRY semantics for percpu allocator under pressure, there seems also a missing bpf_map_precharge_memlock() check in array map allocation. Given today the actual bpf_map_charge_memlock() happens after the find_and_alloc_map() in syscall path, the bpf_map_precharge_memlock() is there to bail out early before we go and do the map setup work when we find that we hit the limits anyway. Therefore add this for array map as well. Fixes: 6c90598 ("bpf: pre-allocate hash map elements") Fixes: a10423b ("bpf: introduce BPF_MAP_TYPE_PERCPU_ARRAY map") Reported-by: syzbot+adb03f3f0bb57ce3acda@syzkaller.appspotmail.com Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Cc: Dennis Zhou <dennisszhou@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 67e3a17 commit 9c2d63b

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

‎kernel/bpf/arraymap.c‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ static int array_map_alloc_check(union bpf_attr *attr)
7373
static struct bpf_map *array_map_alloc(union bpf_attr *attr)
7474
{
7575
bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
76-
int numa_node = bpf_map_attr_numa_node(attr);
76+
int ret, numa_node = bpf_map_attr_numa_node(attr);
7777
u32 elem_size, index_mask, max_entries;
7878
bool unpriv = !capable(CAP_SYS_ADMIN);
79+
u64 cost, array_size, mask64;
7980
struct bpf_array *array;
80-
u64 array_size, mask64;
8181

8282
elem_size = round_up(attr->value_size, 8);
8383

@@ -109,8 +109,19 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
109109
array_size += (u64) max_entries * elem_size;
110110

111111
/* make sure there is no u32 overflow later in round_up() */
112-
if (array_size >= U32_MAX - PAGE_SIZE)
112+
cost = array_size;
113+
if (cost >= U32_MAX - PAGE_SIZE)
113114
return ERR_PTR(-ENOMEM);
115+
if (percpu) {
116+
cost += (u64)attr->max_entries * elem_size * num_possible_cpus();
117+
if (cost >= U32_MAX - PAGE_SIZE)
118+
return ERR_PTR(-ENOMEM);
119+
}
120+
cost = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
121+
122+
ret = bpf_map_precharge_memlock(cost);
123+
if (ret < 0)
124+
return ERR_PTR(ret);
114125

115126
/* allocate all map elements and zero-initialize them */
116127
array = bpf_map_area_alloc(array_size, numa_node);
@@ -121,20 +132,13 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
121132

122133
/* copy mandatory map attributes */
123134
bpf_map_init_from_attr(&array->map, attr);
135+
array->map.pages = cost;
124136
array->elem_size = elem_size;
125137

126-
if (!percpu)
127-
goto out;
128-
129-
array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
130-
131-
if (array_size >= U32_MAX - PAGE_SIZE ||
132-
bpf_array_alloc_percpu(array)) {
138+
if (percpu && bpf_array_alloc_percpu(array)) {
133139
bpf_map_area_free(array);
134140
return ERR_PTR(-ENOMEM);
135141
}
136-
out:
137-
array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
138142

139143
return &array->map;
140144
}

0 commit comments

Comments
 (0)