Skip to content

Commit d5a3b1f

Browse files
4astdavem330
authored andcommitted
bpf: introduce BPF_MAP_TYPE_STACK_TRACE
add new map type to store stack traces and corresponding helper bpf_get_stackid(ctx, map, flags) - walk user or kernel stack and return id @ctx: struct pt_regs* @Map: pointer to stack_trace map @flags: bits 0-7 - numer of stack frames to skip bit 8 - collect user stack instead of kernel bit 9 - compare stacks by hash only bit 10 - if two different stacks hash into the same stackid discard old other bits - reserved Return: >= 0 stackid on success or negative error stackid is a 32-bit integer handle that can be further combined with other data (including other stackid) and used as a key into maps. Userspace will access stackmap using standard lookup/delete syscall commands to retrieve full stack trace for given stackid. Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 568b329 commit d5a3b1f

File tree

6 files changed

+269
-1
lines changed

6 files changed

+269
-1
lines changed

Diff for: include/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
237237
extern const struct bpf_func_proto bpf_get_current_comm_proto;
238238
extern const struct bpf_func_proto bpf_skb_vlan_push_proto;
239239
extern const struct bpf_func_proto bpf_skb_vlan_pop_proto;
240+
extern const struct bpf_func_proto bpf_get_stackid_proto;
240241

241242
/* Shared helpers among cBPF and eBPF. */
242243
void bpf_user_rnd_init_once(void);

Diff for: include/uapi/linux/bpf.h

+21
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ enum bpf_map_type {
8383
BPF_MAP_TYPE_PERF_EVENT_ARRAY,
8484
BPF_MAP_TYPE_PERCPU_HASH,
8585
BPF_MAP_TYPE_PERCPU_ARRAY,
86+
BPF_MAP_TYPE_STACK_TRACE,
8687
};
8788

8889
enum bpf_prog_type {
@@ -272,6 +273,20 @@ enum bpf_func_id {
272273
*/
273274
BPF_FUNC_perf_event_output,
274275
BPF_FUNC_skb_load_bytes,
276+
277+
/**
278+
* bpf_get_stackid(ctx, map, flags) - walk user or kernel stack and return id
279+
* @ctx: struct pt_regs*
280+
* @map: pointer to stack_trace map
281+
* @flags: bits 0-7 - numer of stack frames to skip
282+
* bit 8 - collect user stack instead of kernel
283+
* bit 9 - compare stacks by hash only
284+
* bit 10 - if two different stacks hash into the same stackid
285+
* discard old
286+
* other bits - reserved
287+
* Return: >= 0 stackid on success or negative error
288+
*/
289+
BPF_FUNC_get_stackid,
275290
__BPF_FUNC_MAX_ID,
276291
};
277292

@@ -294,6 +309,12 @@ enum bpf_func_id {
294309
/* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
295310
#define BPF_F_TUNINFO_IPV6 (1ULL << 0)
296311

312+
/* BPF_FUNC_get_stackid flags. */
313+
#define BPF_F_SKIP_FIELD_MASK 0xffULL
314+
#define BPF_F_USER_STACK (1ULL << 8)
315+
#define BPF_F_FAST_STACK_CMP (1ULL << 9)
316+
#define BPF_F_REUSE_STACKID (1ULL << 10)
317+
297318
/* user accessible mirror of in-kernel sk_buff.
298319
* new fields can only be added to the end of this structure
299320
*/

Diff for: kernel/bpf/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ obj-y := core.o
22

33
obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o
44
obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o
5+
ifeq ($(CONFIG_PERF_EVENTS),y)
6+
obj-$(CONFIG_BPF_SYSCALL) += stackmap.o
7+
endif

Diff for: kernel/bpf/stackmap.c

+237
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
/* Copyright (c) 2016 Facebook
2+
*
3+
* This program is free software; you can redistribute it and/or
4+
* modify it under the terms of version 2 of the GNU General Public
5+
* License as published by the Free Software Foundation.
6+
*/
7+
#include <linux/bpf.h>
8+
#include <linux/jhash.h>
9+
#include <linux/filter.h>
10+
#include <linux/vmalloc.h>
11+
#include <linux/stacktrace.h>
12+
#include <linux/perf_event.h>
13+
14+
struct stack_map_bucket {
15+
struct rcu_head rcu;
16+
u32 hash;
17+
u32 nr;
18+
u64 ip[];
19+
};
20+
21+
struct bpf_stack_map {
22+
struct bpf_map map;
23+
u32 n_buckets;
24+
struct stack_map_bucket __rcu *buckets[];
25+
};
26+
27+
/* Called from syscall */
28+
static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
29+
{
30+
u32 value_size = attr->value_size;
31+
struct bpf_stack_map *smap;
32+
u64 cost, n_buckets;
33+
int err;
34+
35+
if (!capable(CAP_SYS_ADMIN))
36+
return ERR_PTR(-EPERM);
37+
38+
/* check sanity of attributes */
39+
if (attr->max_entries == 0 || attr->key_size != 4 ||
40+
value_size < 8 || value_size % 8 ||
41+
value_size / 8 > PERF_MAX_STACK_DEPTH)
42+
return ERR_PTR(-EINVAL);
43+
44+
/* hash table size must be power of 2 */
45+
n_buckets = roundup_pow_of_two(attr->max_entries);
46+
47+
cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
48+
if (cost >= U32_MAX - PAGE_SIZE)
49+
return ERR_PTR(-E2BIG);
50+
51+
smap = kzalloc(cost, GFP_USER | __GFP_NOWARN);
52+
if (!smap) {
53+
smap = vzalloc(cost);
54+
if (!smap)
55+
return ERR_PTR(-ENOMEM);
56+
}
57+
58+
err = -E2BIG;
59+
cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
60+
if (cost >= U32_MAX - PAGE_SIZE)
61+
goto free_smap;
62+
63+
smap->map.map_type = attr->map_type;
64+
smap->map.key_size = attr->key_size;
65+
smap->map.value_size = value_size;
66+
smap->map.max_entries = attr->max_entries;
67+
smap->n_buckets = n_buckets;
68+
smap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
69+
70+
err = get_callchain_buffers();
71+
if (err)
72+
goto free_smap;
73+
74+
return &smap->map;
75+
76+
free_smap:
77+
kvfree(smap);
78+
return ERR_PTR(err);
79+
}
80+
81+
static u64 bpf_get_stackid(u64 r1, u64 r2, u64 flags, u64 r4, u64 r5)
82+
{
83+
struct pt_regs *regs = (struct pt_regs *) (long) r1;
84+
struct bpf_map *map = (struct bpf_map *) (long) r2;
85+
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
86+
struct perf_callchain_entry *trace;
87+
struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
88+
u32 max_depth = map->value_size / 8;
89+
/* stack_map_alloc() checks that max_depth <= PERF_MAX_STACK_DEPTH */
90+
u32 init_nr = PERF_MAX_STACK_DEPTH - max_depth;
91+
u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
92+
u32 hash, id, trace_nr, trace_len;
93+
bool user = flags & BPF_F_USER_STACK;
94+
bool kernel = !user;
95+
u64 *ips;
96+
97+
if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
98+
BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
99+
return -EINVAL;
100+
101+
trace = get_perf_callchain(regs, init_nr, kernel, user, false, false);
102+
103+
if (unlikely(!trace))
104+
/* couldn't fetch the stack trace */
105+
return -EFAULT;
106+
107+
/* get_perf_callchain() guarantees that trace->nr >= init_nr
108+
* and trace-nr <= PERF_MAX_STACK_DEPTH, so trace_nr <= max_depth
109+
*/
110+
trace_nr = trace->nr - init_nr;
111+
112+
if (trace_nr <= skip)
113+
/* skipping more than usable stack trace */
114+
return -EFAULT;
115+
116+
trace_nr -= skip;
117+
trace_len = trace_nr * sizeof(u64);
118+
ips = trace->ip + skip + init_nr;
119+
hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
120+
id = hash & (smap->n_buckets - 1);
121+
bucket = rcu_dereference(smap->buckets[id]);
122+
123+
if (bucket && bucket->hash == hash) {
124+
if (flags & BPF_F_FAST_STACK_CMP)
125+
return id;
126+
if (bucket->nr == trace_nr &&
127+
memcmp(bucket->ip, ips, trace_len) == 0)
128+
return id;
129+
}
130+
131+
/* this call stack is not in the map, try to add it */
132+
if (bucket && !(flags & BPF_F_REUSE_STACKID))
133+
return -EEXIST;
134+
135+
new_bucket = kmalloc(sizeof(struct stack_map_bucket) + map->value_size,
136+
GFP_ATOMIC | __GFP_NOWARN);
137+
if (unlikely(!new_bucket))
138+
return -ENOMEM;
139+
140+
memcpy(new_bucket->ip, ips, trace_len);
141+
memset(new_bucket->ip + trace_len / 8, 0, map->value_size - trace_len);
142+
new_bucket->hash = hash;
143+
new_bucket->nr = trace_nr;
144+
145+
old_bucket = xchg(&smap->buckets[id], new_bucket);
146+
if (old_bucket)
147+
kfree_rcu(old_bucket, rcu);
148+
return id;
149+
}
150+
151+
const struct bpf_func_proto bpf_get_stackid_proto = {
152+
.func = bpf_get_stackid,
153+
.gpl_only = true,
154+
.ret_type = RET_INTEGER,
155+
.arg1_type = ARG_PTR_TO_CTX,
156+
.arg2_type = ARG_CONST_MAP_PTR,
157+
.arg3_type = ARG_ANYTHING,
158+
};
159+
160+
/* Called from syscall or from eBPF program */
161+
static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
162+
{
163+
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
164+
struct stack_map_bucket *bucket;
165+
u32 id = *(u32 *)key;
166+
167+
if (unlikely(id >= smap->n_buckets))
168+
return NULL;
169+
bucket = rcu_dereference(smap->buckets[id]);
170+
return bucket ? bucket->ip : NULL;
171+
}
172+
173+
static int stack_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
174+
{
175+
return -EINVAL;
176+
}
177+
178+
static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
179+
u64 map_flags)
180+
{
181+
return -EINVAL;
182+
}
183+
184+
/* Called from syscall or from eBPF program */
185+
static int stack_map_delete_elem(struct bpf_map *map, void *key)
186+
{
187+
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
188+
struct stack_map_bucket *old_bucket;
189+
u32 id = *(u32 *)key;
190+
191+
if (unlikely(id >= smap->n_buckets))
192+
return -E2BIG;
193+
194+
old_bucket = xchg(&smap->buckets[id], NULL);
195+
if (old_bucket) {
196+
kfree_rcu(old_bucket, rcu);
197+
return 0;
198+
} else {
199+
return -ENOENT;
200+
}
201+
}
202+
203+
/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
204+
static void stack_map_free(struct bpf_map *map)
205+
{
206+
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
207+
int i;
208+
209+
synchronize_rcu();
210+
211+
for (i = 0; i < smap->n_buckets; i++)
212+
if (smap->buckets[i])
213+
kfree_rcu(smap->buckets[i], rcu);
214+
kvfree(smap);
215+
put_callchain_buffers();
216+
}
217+
218+
static const struct bpf_map_ops stack_map_ops = {
219+
.map_alloc = stack_map_alloc,
220+
.map_free = stack_map_free,
221+
.map_get_next_key = stack_map_get_next_key,
222+
.map_lookup_elem = stack_map_lookup_elem,
223+
.map_update_elem = stack_map_update_elem,
224+
.map_delete_elem = stack_map_delete_elem,
225+
};
226+
227+
static struct bpf_map_type_list stack_map_type __read_mostly = {
228+
.ops = &stack_map_ops,
229+
.type = BPF_MAP_TYPE_STACK_TRACE,
230+
};
231+
232+
static int __init register_stack_map(void)
233+
{
234+
bpf_register_map_type(&stack_map_type);
235+
return 0;
236+
}
237+
late_initcall(register_stack_map);

Diff for: kernel/bpf/verifier.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ static const struct {
246246
{BPF_MAP_TYPE_PROG_ARRAY, BPF_FUNC_tail_call},
247247
{BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_FUNC_perf_event_read},
248248
{BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_FUNC_perf_event_output},
249+
{BPF_MAP_TYPE_STACK_TRACE, BPF_FUNC_get_stackid},
249250
};
250251

251252
static void print_verifier_state(struct verifier_env *env)
@@ -911,8 +912,11 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)
911912
* don't allow any other map type to be passed into
912913
* the special func;
913914
*/
914-
if (bool_func && bool_map != bool_func)
915+
if (bool_func && bool_map != bool_func) {
916+
verbose("cannot pass map_type %d into func %d\n",
917+
map->map_type, func_id);
915918
return -EINVAL;
919+
}
916920
}
917921

918922
return 0;

Diff for: kernel/trace/bpf_trace.c

+2
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func
299299
return &bpf_perf_event_read_proto;
300300
case BPF_FUNC_perf_event_output:
301301
return &bpf_perf_event_output_proto;
302+
case BPF_FUNC_get_stackid:
303+
return &bpf_get_stackid_proto;
302304
default:
303305
return NULL;
304306
}

0 commit comments

Comments
 (0)