2016-02-18 11:58:58 +08:00
|
|
|
/* Copyright (c) 2016 Facebook
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of version 2 of the GNU General Public
|
|
|
|
* License as published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <linux/jhash.h>
|
|
|
|
#include <linux/filter.h>
|
|
|
|
#include <linux/stacktrace.h>
|
|
|
|
#include <linux/perf_event.h>
|
2016-03-08 13:57:17 +08:00
|
|
|
#include "percpu_freelist.h"
|
2016-02-18 11:58:58 +08:00
|
|
|
|
2017-10-19 04:00:22 +08:00
|
|
|
#define STACK_CREATE_FLAG_MASK \
|
|
|
|
(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
|
|
|
|
|
2016-02-18 11:58:58 +08:00
|
|
|
struct stack_map_bucket {
|
2016-03-08 13:57:17 +08:00
|
|
|
struct pcpu_freelist_node fnode;
|
2016-02-18 11:58:58 +08:00
|
|
|
u32 hash;
|
|
|
|
u32 nr;
|
|
|
|
u64 ip[];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct bpf_stack_map {
|
|
|
|
struct bpf_map map;
|
2016-03-08 13:57:17 +08:00
|
|
|
void *elems;
|
|
|
|
struct pcpu_freelist freelist;
|
2016-02-18 11:58:58 +08:00
|
|
|
u32 n_buckets;
|
2016-03-08 13:57:17 +08:00
|
|
|
struct stack_map_bucket *buckets[];
|
2016-02-18 11:58:58 +08:00
|
|
|
};
|
|
|
|
|
2016-03-08 13:57:17 +08:00
|
|
|
static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
|
|
|
|
{
|
|
|
|
u32 elem_size = sizeof(struct stack_map_bucket) + smap->map.value_size;
|
|
|
|
int err;
|
|
|
|
|
2017-08-19 02:28:00 +08:00
|
|
|
smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries,
|
|
|
|
smap->map.numa_node);
|
2016-03-08 13:57:17 +08:00
|
|
|
if (!smap->elems)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
err = pcpu_freelist_init(&smap->freelist);
|
|
|
|
if (err)
|
|
|
|
goto free_elems;
|
|
|
|
|
|
|
|
pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
|
|
|
|
smap->map.max_entries);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
free_elems:
|
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>
2017-01-18 22:14:17 +08:00
|
|
|
bpf_map_area_free(smap->elems);
|
2016-03-08 13:57:17 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-02-18 11:58:58 +08:00
|
|
|
/* Called from syscall */
|
|
|
|
static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
|
|
|
|
{
|
|
|
|
u32 value_size = attr->value_size;
|
|
|
|
struct bpf_stack_map *smap;
|
|
|
|
u64 cost, n_buckets;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return ERR_PTR(-EPERM);
|
|
|
|
|
2017-10-19 04:00:22 +08:00
|
|
|
if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)
|
2016-03-08 13:57:16 +08:00
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2016-02-18 11:58:58 +08:00
|
|
|
/* check sanity of attributes */
|
|
|
|
if (attr->max_entries == 0 || attr->key_size != 4 ||
|
|
|
|
value_size < 8 || value_size % 8 ||
|
2016-04-21 23:28:50 +08:00
|
|
|
value_size / 8 > sysctl_perf_event_max_stack)
|
2016-02-18 11:58:58 +08:00
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
/* hash table size must be power of 2 */
|
|
|
|
n_buckets = roundup_pow_of_two(attr->max_entries);
|
|
|
|
|
|
|
|
cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
|
|
|
|
if (cost >= U32_MAX - PAGE_SIZE)
|
|
|
|
return ERR_PTR(-E2BIG);
|
|
|
|
|
2017-08-19 02:28:00 +08:00
|
|
|
smap = bpf_map_area_alloc(cost, bpf_map_attr_numa_node(attr));
|
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>
2017-01-18 22:14:17 +08:00
|
|
|
if (!smap)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2016-02-18 11:58:58 +08:00
|
|
|
|
|
|
|
err = -E2BIG;
|
|
|
|
cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
|
|
|
|
if (cost >= U32_MAX - PAGE_SIZE)
|
|
|
|
goto free_smap;
|
|
|
|
|
|
|
|
smap->map.map_type = attr->map_type;
|
|
|
|
smap->map.key_size = attr->key_size;
|
|
|
|
smap->map.value_size = value_size;
|
|
|
|
smap->map.max_entries = attr->max_entries;
|
2017-05-25 07:05:08 +08:00
|
|
|
smap->map.map_flags = attr->map_flags;
|
2016-02-18 11:58:58 +08:00
|
|
|
smap->n_buckets = n_buckets;
|
|
|
|
smap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
|
2017-08-19 02:28:00 +08:00
|
|
|
smap->map.numa_node = bpf_map_attr_numa_node(attr);
|
2016-02-18 11:58:58 +08:00
|
|
|
|
2016-03-08 13:57:17 +08:00
|
|
|
err = bpf_map_precharge_memlock(smap->map.pages);
|
|
|
|
if (err)
|
|
|
|
goto free_smap;
|
|
|
|
|
2016-04-29 00:16:33 +08:00
|
|
|
err = get_callchain_buffers(sysctl_perf_event_max_stack);
|
2016-02-18 11:58:58 +08:00
|
|
|
if (err)
|
|
|
|
goto free_smap;
|
|
|
|
|
2016-03-08 13:57:17 +08:00
|
|
|
err = prealloc_elems_and_freelist(smap);
|
|
|
|
if (err)
|
|
|
|
goto put_buffers;
|
|
|
|
|
2016-02-18 11:58:58 +08:00
|
|
|
return &smap->map;
|
|
|
|
|
2016-03-08 13:57:17 +08:00
|
|
|
put_buffers:
|
|
|
|
put_callchain_buffers();
|
2016-02-18 11:58:58 +08:00
|
|
|
free_smap:
|
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>
2017-01-18 22:14:17 +08:00
|
|
|
bpf_map_area_free(smap);
|
2016-02-18 11:58:58 +08:00
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
bpf: add BPF_CALL_x macros for declaring helpers
This work adds BPF_CALL_<n>() macros and converts all the eBPF helper functions
to use them, in a similar fashion like we do with SYSCALL_DEFINE<n>() macros
that are used today. Motivation for this is to hide all the register handling
and all necessary casts from the user, so that it is done automatically in the
background when adding a BPF_CALL_<n>() call.
This makes current helpers easier to review, eases to write future helpers,
avoids getting the casting mess wrong, and allows for extending all helpers at
once (f.e. build time checks, etc). It also helps detecting more easily in
code reviews that unused registers are not instrumented in the code by accident,
breaking compatibility with existing programs.
BPF_CALL_<n>() internals are quite similar to SYSCALL_DEFINE<n>() ones with some
fundamental differences, for example, for generating the actual helper function
that carries all u64 regs, we need to fill unused regs, so that we always end up
with 5 u64 regs as an argument.
I reviewed several 0-5 generated BPF_CALL_<n>() variants of the .i results and
they look all as expected. No sparse issue spotted. We let this also sit for a
few days with Fengguang's kbuild test robot, and there were no issues seen. On
s390, it barked on the "uses dynamic stack allocation" notice, which is an old
one from bpf_perf_event_output{,_tp}() reappearing here due to the conversion
to the call wrapper, just telling that the perf raw record/frag sits on stack
(gcc with s390's -mwarn-dynamicstack), but that's all. Did various runtime tests
and they were fine as well. All eBPF helpers are now converted to use these
macros, getting rid of a good chunk of all the raw castings.
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-09-09 08:45:31 +08:00
|
|
|
BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
|
|
|
|
u64, flags)
|
2016-02-18 11:58:58 +08:00
|
|
|
{
|
|
|
|
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
|
|
|
|
struct perf_callchain_entry *trace;
|
|
|
|
struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
|
|
|
|
u32 max_depth = map->value_size / 8;
|
2016-04-21 23:28:50 +08:00
|
|
|
/* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
|
|
|
|
u32 init_nr = sysctl_perf_event_max_stack - max_depth;
|
2016-02-18 11:58:58 +08:00
|
|
|
u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
|
|
|
|
u32 hash, id, trace_nr, trace_len;
|
|
|
|
bool user = flags & BPF_F_USER_STACK;
|
|
|
|
bool kernel = !user;
|
|
|
|
u64 *ips;
|
|
|
|
|
|
|
|
if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
|
|
|
|
BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2016-04-28 23:30:53 +08:00
|
|
|
trace = get_perf_callchain(regs, init_nr, kernel, user,
|
|
|
|
sysctl_perf_event_max_stack, false, false);
|
2016-02-18 11:58:58 +08:00
|
|
|
|
|
|
|
if (unlikely(!trace))
|
|
|
|
/* couldn't fetch the stack trace */
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
/* get_perf_callchain() guarantees that trace->nr >= init_nr
|
2016-04-21 23:28:50 +08:00
|
|
|
* and trace-nr <= sysctl_perf_event_max_stack, so trace_nr <= max_depth
|
2016-02-18 11:58:58 +08:00
|
|
|
*/
|
|
|
|
trace_nr = trace->nr - init_nr;
|
|
|
|
|
|
|
|
if (trace_nr <= skip)
|
|
|
|
/* skipping more than usable stack trace */
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
trace_nr -= skip;
|
|
|
|
trace_len = trace_nr * sizeof(u64);
|
|
|
|
ips = trace->ip + skip + init_nr;
|
|
|
|
hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
|
|
|
|
id = hash & (smap->n_buckets - 1);
|
2016-03-08 13:57:17 +08:00
|
|
|
bucket = READ_ONCE(smap->buckets[id]);
|
2016-02-18 11:58:58 +08:00
|
|
|
|
|
|
|
if (bucket && bucket->hash == hash) {
|
|
|
|
if (flags & BPF_F_FAST_STACK_CMP)
|
|
|
|
return id;
|
|
|
|
if (bucket->nr == trace_nr &&
|
|
|
|
memcmp(bucket->ip, ips, trace_len) == 0)
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this call stack is not in the map, try to add it */
|
|
|
|
if (bucket && !(flags & BPF_F_REUSE_STACKID))
|
|
|
|
return -EEXIST;
|
|
|
|
|
2016-03-08 13:57:17 +08:00
|
|
|
new_bucket = (struct stack_map_bucket *)
|
|
|
|
pcpu_freelist_pop(&smap->freelist);
|
2016-02-18 11:58:58 +08:00
|
|
|
if (unlikely(!new_bucket))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
memcpy(new_bucket->ip, ips, trace_len);
|
|
|
|
new_bucket->hash = hash;
|
|
|
|
new_bucket->nr = trace_nr;
|
|
|
|
|
|
|
|
old_bucket = xchg(&smap->buckets[id], new_bucket);
|
|
|
|
if (old_bucket)
|
2016-03-08 13:57:17 +08:00
|
|
|
pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
|
2016-02-18 11:58:58 +08:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct bpf_func_proto bpf_get_stackid_proto = {
|
|
|
|
.func = bpf_get_stackid,
|
|
|
|
.gpl_only = true,
|
|
|
|
.ret_type = RET_INTEGER,
|
|
|
|
.arg1_type = ARG_PTR_TO_CTX,
|
|
|
|
.arg2_type = ARG_CONST_MAP_PTR,
|
|
|
|
.arg3_type = ARG_ANYTHING,
|
|
|
|
};
|
|
|
|
|
2016-03-08 13:57:17 +08:00
|
|
|
/* Called from eBPF program */
|
2016-02-18 11:58:58 +08:00
|
|
|
static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
|
2016-03-08 13:57:17 +08:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called from syscall */
|
|
|
|
int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
|
2016-02-18 11:58:58 +08:00
|
|
|
{
|
|
|
|
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
|
2016-03-08 13:57:17 +08:00
|
|
|
struct stack_map_bucket *bucket, *old_bucket;
|
|
|
|
u32 id = *(u32 *)key, trace_len;
|
2016-02-18 11:58:58 +08:00
|
|
|
|
|
|
|
if (unlikely(id >= smap->n_buckets))
|
2016-03-08 13:57:17 +08:00
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
bucket = xchg(&smap->buckets[id], NULL);
|
|
|
|
if (!bucket)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
trace_len = bucket->nr * sizeof(u64);
|
|
|
|
memcpy(value, bucket->ip, trace_len);
|
|
|
|
memset(value + trace_len, 0, map->value_size - trace_len);
|
|
|
|
|
|
|
|
old_bucket = xchg(&smap->buckets[id], bucket);
|
|
|
|
if (old_bucket)
|
|
|
|
pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
|
|
|
|
return 0;
|
2016-02-18 11:58:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int stack_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
|
|
|
|
u64 map_flags)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called from syscall or from eBPF program */
|
|
|
|
static int stack_map_delete_elem(struct bpf_map *map, void *key)
|
|
|
|
{
|
|
|
|
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
|
|
|
|
struct stack_map_bucket *old_bucket;
|
|
|
|
u32 id = *(u32 *)key;
|
|
|
|
|
|
|
|
if (unlikely(id >= smap->n_buckets))
|
|
|
|
return -E2BIG;
|
|
|
|
|
|
|
|
old_bucket = xchg(&smap->buckets[id], NULL);
|
|
|
|
if (old_bucket) {
|
2016-03-08 13:57:17 +08:00
|
|
|
pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
|
2016-02-18 11:58:58 +08:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
|
|
|
|
static void stack_map_free(struct bpf_map *map)
|
|
|
|
{
|
|
|
|
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
|
|
|
|
|
2016-03-08 13:57:17 +08:00
|
|
|
/* wait for bpf programs to complete before freeing stack map */
|
2016-02-18 11:58:58 +08:00
|
|
|
synchronize_rcu();
|
|
|
|
|
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>
2017-01-18 22:14:17 +08:00
|
|
|
bpf_map_area_free(smap->elems);
|
2016-03-08 13:57:17 +08:00
|
|
|
pcpu_freelist_destroy(&smap->freelist);
|
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>
2017-01-18 22:14:17 +08:00
|
|
|
bpf_map_area_free(smap);
|
2016-02-18 11:58:58 +08:00
|
|
|
put_callchain_buffers();
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:34:58 +08:00
|
|
|
const struct bpf_map_ops stack_map_ops = {
|
2016-02-18 11:58:58 +08:00
|
|
|
.map_alloc = stack_map_alloc,
|
|
|
|
.map_free = stack_map_free,
|
|
|
|
.map_get_next_key = stack_map_get_next_key,
|
|
|
|
.map_lookup_elem = stack_map_lookup_elem,
|
|
|
|
.map_update_elem = stack_map_update_elem,
|
|
|
|
.map_delete_elem = stack_map_delete_elem,
|
|
|
|
};
|