Skip to content

Commit d407bd2

Browse files
borkmanndavem330
authored andcommitted
bpf: don't trigger OOM killer under pressure with map alloc
This patch adds two helpers, bpf_map_area_alloc() and bpf_map_area_free(), that are to be used for map allocations. Using kmalloc() for very large allocations can cause excessive work within the page allocator, so i) fall back earlier to vmalloc() when the attempt is considered costly anyway, and even more importantly ii) don't trigger OOM killer with any of the allocators. Since this is based on a user space request, for example, when creating maps with element pre-allocation, we really want such requests to fail instead of killing other user space processes. Also, don't spam the kernel log with warnings should any of the allocations fail under pressure. Given that, we can make backend selection in bpf_map_area_alloc() generic, and convert all maps over to use this API for spots with potentially large allocation requests. Note, replacing the one kmalloc_array() is fine as overflow checks happen earlier in htab_map_alloc(), since it must also protect the multiplication for vmalloc() should kmalloc_array() fail. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 9ed5959 commit d407bd2

5 files changed

Lines changed: 52 additions & 36 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref);
247247
void bpf_map_put_with_uref(struct bpf_map *map);
248248
void bpf_map_put(struct bpf_map *map);
249249
int bpf_map_precharge_memlock(u32 pages);
250+
void *bpf_map_area_alloc(size_t size);
251+
void bpf_map_area_free(void *base);
250252

251253
extern int sysctl_unprivileged_bpf_disabled;
252254

‎kernel/bpf/arraymap.c‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212
#include <linux/bpf.h>
1313
#include <linux/err.h>
14-
#include <linux/vmalloc.h>
1514
#include <linux/slab.h>
1615
#include <linux/mm.h>
1716
#include <linux/filter.h>
@@ -74,14 +73,10 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
7473
if (array_size >= U32_MAX - PAGE_SIZE)
7574
return ERR_PTR(-ENOMEM);
7675

77-
7876
/* allocate all map elements and zero-initialize them */
79-
array = kzalloc(array_size, GFP_USER | __GFP_NOWARN);
80-
if (!array) {
81-
array = vzalloc(array_size);
82-
if (!array)
83-
return ERR_PTR(-ENOMEM);
84-
}
77+
array = bpf_map_area_alloc(array_size);
78+
if (!array)
79+
return ERR_PTR(-ENOMEM);
8580

8681
/* copy mandatory map attributes */
8782
array->map.map_type = attr->map_type;
@@ -97,7 +92,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
9792

9893
if (array_size >= U32_MAX - PAGE_SIZE ||
9994
elem_size > PCPU_MIN_UNIT_SIZE || bpf_array_alloc_percpu(array)) {
100-
kvfree(array);
95+
bpf_map_area_free(array);
10196
return ERR_PTR(-ENOMEM);
10297
}
10398
out:
@@ -262,7 +257,7 @@ static void array_map_free(struct bpf_map *map)
262257
if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
263258
bpf_array_free_percpu(array);
264259

265-
kvfree(array);
260+
bpf_map_area_free(array);
266261
}
267262

268263
static const struct bpf_map_ops array_ops = {
@@ -319,7 +314,8 @@ static void fd_array_map_free(struct bpf_map *map)
319314
/* make sure it's empty */
320315
for (i = 0; i < array->map.max_entries; i++)
321316
BUG_ON(array->ptrs[i] != NULL);
322-
kvfree(array);
317+
318+
bpf_map_area_free(array);
323319
}
324320

325321
static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)

‎kernel/bpf/hashtab.c‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <linux/bpf.h>
1414
#include <linux/jhash.h>
1515
#include <linux/filter.h>
16-
#include <linux/vmalloc.h>
1716
#include "percpu_freelist.h"
1817
#include "bpf_lru_list.h"
1918

@@ -103,7 +102,7 @@ static void htab_free_elems(struct bpf_htab *htab)
103102
free_percpu(pptr);
104103
}
105104
free_elems:
106-
vfree(htab->elems);
105+
bpf_map_area_free(htab->elems);
107106
}
108107

109108
static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
@@ -125,7 +124,8 @@ static int prealloc_init(struct bpf_htab *htab)
125124
{
126125
int err = -ENOMEM, i;
127126

128-
htab->elems = vzalloc(htab->elem_size * htab->map.max_entries);
127+
htab->elems = bpf_map_area_alloc(htab->elem_size *
128+
htab->map.max_entries);
129129
if (!htab->elems)
130130
return -ENOMEM;
131131

@@ -320,14 +320,10 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
320320
goto free_htab;
321321

322322
err = -ENOMEM;
323-
htab->buckets = kmalloc_array(htab->n_buckets, sizeof(struct bucket),
324-
GFP_USER | __GFP_NOWARN);
325-
326-
if (!htab->buckets) {
327-
htab->buckets = vmalloc(htab->n_buckets * sizeof(struct bucket));
328-
if (!htab->buckets)
329-
goto free_htab;
330-
}
323+
htab->buckets = bpf_map_area_alloc(htab->n_buckets *
324+
sizeof(struct bucket));
325+
if (!htab->buckets)
326+
goto free_htab;
331327

332328
for (i = 0; i < htab->n_buckets; i++) {
333329
INIT_HLIST_HEAD(&htab->buckets[i].head);
@@ -354,7 +350,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
354350
free_extra_elems:
355351
free_percpu(htab->extra_elems);
356352
free_buckets:
357-
kvfree(htab->buckets);
353+
bpf_map_area_free(htab->buckets);
358354
free_htab:
359355
kfree(htab);
360356
return ERR_PTR(err);
@@ -1014,7 +1010,7 @@ static void htab_map_free(struct bpf_map *map)
10141010
prealloc_destroy(htab);
10151011

10161012
free_percpu(htab->extra_elems);
1017-
kvfree(htab->buckets);
1013+
bpf_map_area_free(htab->buckets);
10181014
kfree(htab);
10191015
}
10201016

‎kernel/bpf/stackmap.c‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <linux/bpf.h>
88
#include <linux/jhash.h>
99
#include <linux/filter.h>
10-
#include <linux/vmalloc.h>
1110
#include <linux/stacktrace.h>
1211
#include <linux/perf_event.h>
1312
#include "percpu_freelist.h"
@@ -32,7 +31,7 @@ static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
3231
u32 elem_size = sizeof(struct stack_map_bucket) + smap->map.value_size;
3332
int err;
3433

35-
smap->elems = vzalloc(elem_size * smap->map.max_entries);
34+
smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries);
3635
if (!smap->elems)
3736
return -ENOMEM;
3837

@@ -45,7 +44,7 @@ static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
4544
return 0;
4645

4746
free_elems:
48-
vfree(smap->elems);
47+
bpf_map_area_free(smap->elems);
4948
return err;
5049
}
5150

@@ -76,12 +75,9 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
7675
if (cost >= U32_MAX - PAGE_SIZE)
7776
return ERR_PTR(-E2BIG);
7877

79-
smap = kzalloc(cost, GFP_USER | __GFP_NOWARN);
80-
if (!smap) {
81-
smap = vzalloc(cost);
82-
if (!smap)
83-
return ERR_PTR(-ENOMEM);
84-
}
78+
smap = bpf_map_area_alloc(cost);
79+
if (!smap)
80+
return ERR_PTR(-ENOMEM);
8581

8682
err = -E2BIG;
8783
cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
@@ -112,7 +108,7 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
112108
put_buffers:
113109
put_callchain_buffers();
114110
free_smap:
115-
kvfree(smap);
111+
bpf_map_area_free(smap);
116112
return ERR_PTR(err);
117113
}
118114

@@ -262,9 +258,9 @@ static void stack_map_free(struct bpf_map *map)
262258
/* wait for bpf programs to complete before freeing stack map */
263259
synchronize_rcu();
264260

265-
vfree(smap->elems);
261+
bpf_map_area_free(smap->elems);
266262
pcpu_freelist_destroy(&smap->freelist);
267-
kvfree(smap);
263+
bpf_map_area_free(smap);
268264
put_callchain_buffers();
269265
}
270266

‎kernel/bpf/syscall.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <linux/bpf.h>
1313
#include <linux/syscalls.h>
1414
#include <linux/slab.h>
15+
#include <linux/vmalloc.h>
16+
#include <linux/mmzone.h>
1517
#include <linux/anon_inodes.h>
1618
#include <linux/file.h>
1719
#include <linux/license.h>
@@ -49,6 +51,30 @@ void bpf_register_map_type(struct bpf_map_type_list *tl)
4951
list_add(&tl->list_node, &bpf_map_types);
5052
}
5153

54+
void *bpf_map_area_alloc(size_t size)
55+
{
56+
/* We definitely need __GFP_NORETRY, so OOM killer doesn't
57+
* trigger under memory pressure as we really just want to
58+
* fail instead.
59+
*/
60+
const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
61+
void *area;
62+
63+
if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
64+
area = kmalloc(size, GFP_USER | flags);
65+
if (area != NULL)
66+
return area;
67+
}
68+
69+
return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | flags,
70+
PAGE_KERNEL);
71+
}
72+
73+
void bpf_map_area_free(void *area)
74+
{
75+
kvfree(area);
76+
}
77+
5278
int bpf_map_precharge_memlock(u32 pages)
5379
{
5480
struct user_struct *user = get_current_user();

0 commit comments

Comments
 (0)